Implement ISan: unsigned integer overflow checker.
[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 "c-family/c-ubsan.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "omp-low.h"
40 #include "gomp-constants.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR,
110   /* a transaction expression */
111   NIC_TRANSACTION
112 };
113
114 /* The various kinds of errors about name-lookup failing. */
115 enum name_lookup_error {
116   /* NULL */
117   NLE_NULL,
118   /* is not a type */
119   NLE_TYPE,
120   /* is not a class or namespace */
121   NLE_CXX98,
122   /* is not a class, namespace, or enumeration */
123   NLE_NOT_CXX98
124 };
125
126 /* The various kinds of required token */
127 enum required_token {
128   RT_NONE,
129   RT_SEMICOLON,  /* ';' */
130   RT_OPEN_PAREN, /* '(' */
131   RT_CLOSE_BRACE, /* '}' */
132   RT_OPEN_BRACE,  /* '{' */
133   RT_CLOSE_SQUARE, /* ']' */
134   RT_OPEN_SQUARE,  /* '[' */
135   RT_COMMA, /* ',' */
136   RT_SCOPE, /* '::' */
137   RT_LESS, /* '<' */
138   RT_GREATER, /* '>' */
139   RT_EQ, /* '=' */
140   RT_ELLIPSIS, /* '...' */
141   RT_MULT, /* '*' */
142   RT_COMPL, /* '~' */
143   RT_COLON, /* ':' */
144   RT_COLON_SCOPE, /* ':' or '::' */
145   RT_CLOSE_PAREN, /* ')' */
146   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147   RT_PRAGMA_EOL, /* end of line */
148   RT_NAME, /* identifier */
149
150   /* The type is CPP_KEYWORD */
151   RT_NEW, /* new */
152   RT_DELETE, /* delete */
153   RT_RETURN, /* return */
154   RT_WHILE, /* while */
155   RT_EXTERN, /* extern */
156   RT_STATIC_ASSERT, /* static_assert */
157   RT_DECLTYPE, /* decltype */
158   RT_OPERATOR, /* operator */
159   RT_CLASS, /* class */
160   RT_TEMPLATE, /* template */
161   RT_NAMESPACE, /* namespace */
162   RT_USING, /* using */
163   RT_ASM, /* asm */
164   RT_TRY, /* try */
165   RT_CATCH, /* catch */
166   RT_THROW, /* throw */
167   RT_LABEL, /* __label__ */
168   RT_AT_TRY, /* @try */
169   RT_AT_SYNCHRONIZED, /* @synchronized */
170   RT_AT_THROW, /* @throw */
171
172   RT_SELECT,  /* selection-statement */
173   RT_INTERATION, /* iteration-statement */
174   RT_JUMP, /* jump-statement */
175   RT_CLASS_KEY, /* class-key */
176   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179   RT_TRANSACTION_CANCEL /* __transaction_cancel */
180 };
181
182 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
183    reverting it on destruction.  */
184
185 class type_id_in_expr_sentinel
186 {
187   cp_parser *parser;
188   bool saved;
189 public:
190   type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
191     : parser (parser),
192       saved (parser->in_type_id_in_expr_p)
193   { parser->in_type_id_in_expr_p = set; }
194   ~type_id_in_expr_sentinel ()
195   { parser->in_type_id_in_expr_p = saved; }
196 };
197
198 /* Prototypes.  */
199
200 static cp_lexer *cp_lexer_new_main
201   (void);
202 static cp_lexer *cp_lexer_new_from_tokens
203   (cp_token_cache *tokens);
204 static void cp_lexer_destroy
205   (cp_lexer *);
206 static int cp_lexer_saving_tokens
207   (const cp_lexer *);
208 static cp_token *cp_lexer_token_at
209   (cp_lexer *, cp_token_position);
210 static void cp_lexer_get_preprocessor_token
211   (cp_lexer *, cp_token *);
212 static inline cp_token *cp_lexer_peek_token
213   (cp_lexer *);
214 static cp_token *cp_lexer_peek_nth_token
215   (cp_lexer *, size_t);
216 static inline bool cp_lexer_next_token_is
217   (cp_lexer *, enum cpp_ttype);
218 static bool cp_lexer_next_token_is_not
219   (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_keyword
221   (cp_lexer *, enum rid);
222 static cp_token *cp_lexer_consume_token
223   (cp_lexer *);
224 static void cp_lexer_purge_token
225   (cp_lexer *);
226 static void cp_lexer_purge_tokens_after
227   (cp_lexer *, cp_token_position);
228 static void cp_lexer_save_tokens
229   (cp_lexer *);
230 static void cp_lexer_commit_tokens
231   (cp_lexer *);
232 static void cp_lexer_rollback_tokens
233   (cp_lexer *);
234 static void cp_lexer_print_token
235   (FILE *, cp_token *);
236 static inline bool cp_lexer_debugging_p
237   (cp_lexer *);
238 static void cp_lexer_start_debugging
239   (cp_lexer *) ATTRIBUTE_UNUSED;
240 static void cp_lexer_stop_debugging
241   (cp_lexer *) ATTRIBUTE_UNUSED;
242
243 static cp_token_cache *cp_token_cache_new
244   (cp_token *, cp_token *);
245
246 static void cp_parser_initial_pragma
247   (cp_token *);
248
249 static tree cp_literal_operator_id
250   (const char *);
251
252 static void cp_parser_cilk_simd
253   (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255   (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257   (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength 
259   (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261   (cp_parser *, tree, bool);
262
263 /* Manifest constants.  */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
266
267 /* Variables.  */
268
269 /* The stream to which debugging output should be written.  */
270 static FILE *cp_lexer_debug_stream;
271
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273    sizeof, typeof, or alignof.  */
274 int cp_unevaluated_operand;
275
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
278    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
279    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280    highlighted by surrounding it in [[ ]].  */
281
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284                       cp_token *start_token, unsigned num,
285                       cp_token *curr_token)
286 {
287   unsigned i, nprinted;
288   cp_token *token;
289   bool do_print;
290
291   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
292
293   if (buffer == NULL)
294     return;
295
296   if (num == 0)
297     num = buffer->length ();
298
299   if (start_token == NULL)
300     start_token = buffer->address ();
301
302   if (start_token > buffer->address ())
303     {
304       cp_lexer_print_token (file, &(*buffer)[0]);
305       fprintf (file, " ... ");
306     }
307
308   do_print = false;
309   nprinted = 0;
310   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311     {
312       if (token == start_token)
313         do_print = true;
314
315       if (!do_print)
316         continue;
317
318       nprinted++;
319       if (token == curr_token)
320         fprintf (file, "[[");
321
322       cp_lexer_print_token (file, token);
323
324       if (token == curr_token)
325         fprintf (file, "]]");
326
327       switch (token->type)
328         {
329           case CPP_SEMICOLON:
330           case CPP_OPEN_BRACE:
331           case CPP_CLOSE_BRACE:
332           case CPP_EOF:
333             fputc ('\n', file);
334             break;
335
336           default:
337             fputc (' ', file);
338         }
339     }
340
341   if (i == num && i < buffer->length ())
342     {
343       fprintf (file, " ... ");
344       cp_lexer_print_token (file, &buffer->last ());
345     }
346
347   fprintf (file, "\n");
348 }
349
350
351 /* Dump all tokens in BUFFER to stderr.  */
352
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355 {
356   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
357 }
358
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
361 {
362   cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
363 }
364
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
367 {
368   if (ptr)
369     debug (*ptr);
370   else
371     fprintf (stderr, "<nil>\n");
372 }
373
374
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
376    description for T.  */
377
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
380 {
381   if (t)
382     {
383       fprintf (file, "%s: ", desc);
384       print_node_brief (file, "", t, 0);
385     }
386 }
387
388
389 /* Dump parser context C to FILE.  */
390
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
393 {
394   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396   print_node_brief (file, "", c->object_type, 0);
397   fprintf (file, "}\n");
398 }
399
400
401 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
402
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
405 {
406   unsigned i;
407   cp_parser_context *c;
408
409   fprintf (file, "Parsing context stack:\n");
410   for (i = 0, c = first; c; c = c->next, i++)
411     {
412       fprintf (file, "\t#%u: ", i);
413       cp_debug_print_context (file, c);
414     }
415 }
416
417
418 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
419
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
422 {
423   if (flag)
424     fprintf (file, "%s: true\n", desc);
425 }
426
427
428 /* Print an unparsed function entry UF to FILE.  */
429
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
432 {
433   unsigned i;
434   cp_default_arg_entry *default_arg_fn;
435   tree fn;
436
437   fprintf (file, "\tFunctions with default args:\n");
438   for (i = 0;
439        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440        i++)
441     {
442       fprintf (file, "\t\tClass type: ");
443       print_node_brief (file, "", default_arg_fn->class_type, 0);
444       fprintf (file, "\t\tDeclaration: ");
445       print_node_brief (file, "", default_arg_fn->decl, 0);
446       fprintf (file, "\n");
447     }
448
449   fprintf (file, "\n\tFunctions with definitions that require "
450            "post-processing\n\t\t");
451   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452     {
453       print_node_brief (file, "", fn, 0);
454       fprintf (file, " ");
455     }
456   fprintf (file, "\n");
457
458   fprintf (file, "\n\tNon-static data members with initializers that require "
459            "post-processing\n\t\t");
460   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461     {
462       print_node_brief (file, "", fn, 0);
463       fprintf (file, " ");
464     }
465   fprintf (file, "\n");
466 }
467
468
469 /* Print the stack of unparsed member functions S to FILE.  */
470
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473                                 vec<cp_unparsed_functions_entry, va_gc> *s)
474 {
475   unsigned i;
476   cp_unparsed_functions_entry *uf;
477
478   fprintf (file, "Unparsed functions\n");
479   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480     {
481       fprintf (file, "#%u:\n", i);
482       cp_debug_print_unparsed_function (file, uf);
483     }
484 }
485
486
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
489
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492 {
493   cp_token *next_token, *first_token, *start_token;
494
495   if (file == NULL)
496     file = stderr;
497
498   next_token = parser->lexer->next_token;
499   first_token = parser->lexer->buffer->address ();
500   start_token = (next_token > first_token + window_size / 2)
501                 ? next_token - window_size / 2
502                 : first_token;
503   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504                         next_token);
505 }
506
507
508 /* Dump debugging information for the given PARSER.  If FILE is NULL,
509    the output is printed on stderr.  */
510
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
513 {
514   const size_t window_size = 20;
515   cp_token *token;
516   expanded_location eloc;
517
518   if (file == NULL)
519     file = stderr;
520
521   fprintf (file, "Parser state\n\n");
522   fprintf (file, "Number of tokens: %u\n",
523            vec_safe_length (parser->lexer->buffer));
524   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525   cp_debug_print_tree_if_set (file, "Object scope",
526                                      parser->object_scope);
527   cp_debug_print_tree_if_set (file, "Qualifying scope",
528                                      parser->qualifying_scope);
529   cp_debug_print_context_stack (file, parser->context);
530   cp_debug_print_flag (file, "Allow GNU extensions",
531                               parser->allow_gnu_extensions_p);
532   cp_debug_print_flag (file, "'>' token is greater-than",
533                               parser->greater_than_is_operator_p);
534   cp_debug_print_flag (file, "Default args allowed in current "
535                               "parameter list", parser->default_arg_ok_p);
536   cp_debug_print_flag (file, "Parsing integral constant-expression",
537                               parser->integral_constant_expression_p);
538   cp_debug_print_flag (file, "Allow non-constant expression in current "
539                               "constant-expression",
540                               parser->allow_non_integral_constant_expression_p);
541   cp_debug_print_flag (file, "Seen non-constant expression",
542                               parser->non_integral_constant_expression_p);
543   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544                               "current context",
545                               parser->local_variables_forbidden_p);
546   cp_debug_print_flag (file, "In unbraced linkage specification",
547                               parser->in_unbraced_linkage_specification_p);
548   cp_debug_print_flag (file, "Parsing a declarator",
549                               parser->in_declarator_p);
550   cp_debug_print_flag (file, "In template argument list",
551                               parser->in_template_argument_list_p);
552   cp_debug_print_flag (file, "Parsing an iteration statement",
553                               parser->in_statement & IN_ITERATION_STMT);
554   cp_debug_print_flag (file, "Parsing a switch statement",
555                               parser->in_statement & IN_SWITCH_STMT);
556   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557                               parser->in_statement & IN_OMP_BLOCK);
558   cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559                               parser->in_statement & IN_CILK_SIMD_FOR);
560   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561                               parser->in_statement & IN_OMP_FOR);
562   cp_debug_print_flag (file, "Parsing an if statement",
563                               parser->in_statement & IN_IF_STMT);
564   cp_debug_print_flag (file, "Parsing a type-id in an expression "
565                               "context", parser->in_type_id_in_expr_p);
566   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567                               parser->implicit_extern_c);
568   cp_debug_print_flag (file, "String expressions should be translated "
569                               "to execution character set",
570                               parser->translate_strings_p);
571   cp_debug_print_flag (file, "Parsing function body outside of a "
572                               "local class", parser->in_function_body);
573   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574                               parser->colon_corrects_to_scope_p);
575   cp_debug_print_flag (file, "Colon doesn't start a class definition",
576                               parser->colon_doesnt_start_class_def_p);
577   if (parser->type_definition_forbidden_message)
578     fprintf (file, "Error message for forbidden type definitions: %s\n",
579              parser->type_definition_forbidden_message);
580   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581   fprintf (file, "Number of class definitions in progress: %u\n",
582            parser->num_classes_being_defined);
583   fprintf (file, "Number of template parameter lists for the current "
584            "declaration: %u\n", parser->num_template_parameter_lists);
585   cp_debug_parser_tokens (file, parser, window_size);
586   token = parser->lexer->next_token;
587   fprintf (file, "Next token to parse:\n");
588   fprintf (file, "\tToken:  ");
589   cp_lexer_print_token (file, token);
590   eloc = expand_location (token->location);
591   fprintf (file, "\n\tFile:   %s\n", eloc.file);
592   fprintf (file, "\tLine:   %d\n", eloc.line);
593   fprintf (file, "\tColumn: %d\n", eloc.column);
594 }
595
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
598 {
599   cp_debug_parser (stderr, &ref);
600 }
601
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
604 {
605   if (ptr)
606     debug (*ptr);
607   else
608     fprintf (stderr, "<nil>\n");
609 }
610
611 /* Allocate memory for a new lexer object and return it.  */
612
613 static cp_lexer *
614 cp_lexer_alloc (void)
615 {
616   cp_lexer *lexer;
617
618   c_common_no_more_pch ();
619
620   /* Allocate the memory.  */
621   lexer = ggc_cleared_alloc<cp_lexer> ();
622
623   /* Initially we are not debugging.  */
624   lexer->debugging_p = false;
625
626   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627
628   /* Create the buffer.  */
629   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
630
631   return lexer;
632 }
633
634
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636    preprocessor.  */
637
638 static cp_lexer *
639 cp_lexer_new_main (void)
640 {
641   cp_lexer *lexer;
642   cp_token token;
643
644   /* It's possible that parsing the first pragma will load a PCH file,
645      which is a GC collection point.  So we have to do that before
646      allocating any memory.  */
647   cp_parser_initial_pragma (&token);
648
649   lexer = cp_lexer_alloc ();
650
651   /* Put the first token in the buffer.  */
652   lexer->buffer->quick_push (token);
653
654   /* Get the remaining tokens from the preprocessor.  */
655   while (token.type != CPP_EOF)
656     {
657       cp_lexer_get_preprocessor_token (lexer, &token);
658       vec_safe_push (lexer->buffer, token);
659     }
660
661   lexer->last_token = lexer->buffer->address ()
662                       + lexer->buffer->length ()
663                       - 1;
664   lexer->next_token = lexer->buffer->length ()
665                       ? lexer->buffer->address ()
666                       : &eof_token;
667
668   /* Subsequent preprocessor diagnostics should use compiler
669      diagnostic functions to get the compiler source location.  */
670   done_lexing = true;
671
672   gcc_assert (!lexer->next_token->purged_p);
673   return lexer;
674 }
675
676 /* Create a new lexer whose token stream is primed with the tokens in
677    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
678
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
681 {
682   cp_token *first = cache->first;
683   cp_token *last = cache->last;
684   cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
685
686   /* We do not own the buffer.  */
687   lexer->buffer = NULL;
688   lexer->next_token = first == last ? &eof_token : first;
689   lexer->last_token = last;
690
691   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692
693   /* Initially we are not debugging.  */
694   lexer->debugging_p = false;
695
696   gcc_assert (!lexer->next_token->purged_p);
697   return lexer;
698 }
699
700 /* Frees all resources associated with LEXER.  */
701
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
704 {
705   vec_free (lexer->buffer);
706   lexer->saved_tokens.release ();
707   ggc_free (lexer);
708 }
709
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711    be used.  The point of this flag is to help the compiler to fold away calls
712    to cp_lexer_debugging_p within this source file at compile time, when the
713    lexer is not being debugged.  */
714
715 #define LEXER_DEBUGGING_ENABLED_P false
716
717 /* Returns nonzero if debugging information should be output.  */
718
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
721 {
722   if (!LEXER_DEBUGGING_ENABLED_P)
723     return false;
724
725   return lexer->debugging_p;
726 }
727
728
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
731 {
732   gcc_assert (!previous_p || lexer->next_token != &eof_token);
733
734   return lexer->next_token - previous_p;
735 }
736
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
739 {
740   return pos;
741 }
742
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
745 {
746   lexer->next_token = cp_lexer_token_at (lexer, pos);
747 }
748
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
751 {
752   if (lexer->next_token == &eof_token)
753     return lexer->last_token - 1;
754   else
755     return cp_lexer_token_position (lexer, true);
756 }
757
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
760 {
761   cp_token_position tp = cp_lexer_previous_token_position (lexer);
762
763   /* Skip past purged tokens.  */
764   while (tp->purged_p)
765     {
766       gcc_assert (tp != vec_safe_address (lexer->buffer));
767       tp--;
768     }
769
770   return cp_lexer_token_at (lexer, tp);
771 }
772
773 /* nonzero if we are presently saving tokens.  */
774
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
777 {
778   return lexer->saved_tokens.length () != 0;
779 }
780
781 /* Store the next token from the preprocessor in *TOKEN.  Return true
782    if we reach EOF.  If LEXER is NULL, assume we are handling an
783    initial #pragma pch_preprocess, and thus want the lexer to return
784    processed strings.  */
785
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
788 {
789   static int is_extern_c = 0;
790
791    /* Get a new token from the preprocessor.  */
792   token->type
793     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795   token->keyword = RID_MAX;
796   token->purged_p = false;
797   token->error_reported = false;
798
799   /* On some systems, some header files are surrounded by an
800      implicit extern "C" block.  Set a flag in the token if it
801      comes from such a header.  */
802   is_extern_c += pending_lang_change;
803   pending_lang_change = 0;
804   token->implicit_extern_c = is_extern_c > 0;
805
806   /* Check to see if this token is a keyword.  */
807   if (token->type == CPP_NAME)
808     {
809       if (C_IS_RESERVED_WORD (token->u.value))
810         {
811           /* Mark this token as a keyword.  */
812           token->type = CPP_KEYWORD;
813           /* Record which keyword.  */
814           token->keyword = C_RID_CODE (token->u.value);
815         }
816       else
817         {
818           if (warn_cxx11_compat
819               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820               && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
821             {
822               /* Warn about the C++0x keyword (but still treat it as
823                  an identifier).  */
824               warning (OPT_Wc__11_compat, 
825                        "identifier %qE is a keyword in C++11",
826                        token->u.value);
827
828               /* Clear out the C_RID_CODE so we don't warn about this
829                  particular identifier-turned-keyword again.  */
830               C_SET_RID_CODE (token->u.value, RID_MAX);
831             }
832
833           token->keyword = RID_MAX;
834         }
835     }
836   else if (token->type == CPP_AT_NAME)
837     {
838       /* This only happens in Objective-C++; it must be a keyword.  */
839       token->type = CPP_KEYWORD;
840       switch (C_RID_CODE (token->u.value))
841         {
842           /* Replace 'class' with '@class', 'private' with '@private',
843              etc.  This prevents confusion with the C++ keyword
844              'class', and makes the tokens consistent with other
845              Objective-C 'AT' keywords.  For example '@class' is
846              reported as RID_AT_CLASS which is consistent with
847              '@synchronized', which is reported as
848              RID_AT_SYNCHRONIZED.
849           */
850         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
851         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
852         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
854         case RID_THROW:     token->keyword = RID_AT_THROW; break;
855         case RID_TRY:       token->keyword = RID_AT_TRY; break;
856         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
857         case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858         default:            token->keyword = C_RID_CODE (token->u.value);
859         }
860     }
861 }
862
863 /* Update the globals input_location and the input file stack from TOKEN.  */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
866 {
867   if (token->type != CPP_EOF)
868     {
869       input_location = token->location;
870     }
871 }
872
873 /* Update the globals input_location and the input file stack from LEXER.  */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
876 {
877   cp_token *token = cp_lexer_peek_token (lexer);
878   cp_lexer_set_source_position_from_token (token);
879 }
880
881 /* Return a pointer to the next token in the token stream, but do not
882    consume it.  */
883
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
886 {
887   if (cp_lexer_debugging_p (lexer))
888     {
889       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891       putc ('\n', cp_lexer_debug_stream);
892     }
893   return lexer->next_token;
894 }
895
896 /* Return true if the next token has the indicated TYPE.  */
897
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
900 {
901   return cp_lexer_peek_token (lexer)->type == type;
902 }
903
904 /* Return true if the next token does not have the indicated TYPE.  */
905
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
908 {
909   return !cp_lexer_next_token_is (lexer, type);
910 }
911
912 /* Return true if the next token is the indicated KEYWORD.  */
913
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
916 {
917   return cp_lexer_peek_token (lexer)->keyword == keyword;
918 }
919
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
922 {
923   return cp_lexer_peek_nth_token (lexer, n)->type == type;
924 }
925
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
928 {
929   return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
930 }
931
932 /* Return true if the next token is not the indicated KEYWORD.  */
933
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
936 {
937   return cp_lexer_peek_token (lexer)->keyword != keyword;
938 }
939
940 /* Return true if the next token is a keyword for a decl-specifier.  */
941
942 static bool
943 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
944 {
945   cp_token *token;
946
947   token = cp_lexer_peek_token (lexer);
948   switch (token->keyword) 
949     {
950       /* auto specifier: storage-class-specifier in C++,
951          simple-type-specifier in C++0x.  */
952     case RID_AUTO:
953       /* Storage classes.  */
954     case RID_REGISTER:
955     case RID_STATIC:
956     case RID_EXTERN:
957     case RID_MUTABLE:
958     case RID_THREAD:
959       /* Elaborated type specifiers.  */
960     case RID_ENUM:
961     case RID_CLASS:
962     case RID_STRUCT:
963     case RID_UNION:
964     case RID_TYPENAME:
965       /* Simple type specifiers.  */
966     case RID_CHAR:
967     case RID_CHAR16:
968     case RID_CHAR32:
969     case RID_WCHAR:
970     case RID_BOOL:
971     case RID_SHORT:
972     case RID_INT:
973     case RID_LONG:
974     case RID_SIGNED:
975     case RID_UNSIGNED:
976     case RID_FLOAT:
977     case RID_DOUBLE:
978     case RID_VOID:
979       /* GNU extensions.  */ 
980     case RID_ATTRIBUTE:
981     case RID_TYPEOF:
982       /* C++0x extensions.  */
983     case RID_DECLTYPE:
984     case RID_UNDERLYING_TYPE:
985       return true;
986
987     default:
988       if (token->keyword >= RID_FIRST_INT_N
989           && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
990           && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
991         return true;
992       return false;
993     }
994 }
995
996 /* Returns TRUE iff the token T begins a decltype type.  */
997
998 static bool
999 token_is_decltype (cp_token *t)
1000 {
1001   return (t->keyword == RID_DECLTYPE
1002           || t->type == CPP_DECLTYPE);
1003 }
1004
1005 /* Returns TRUE iff the next token begins a decltype type.  */
1006
1007 static bool
1008 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1009 {
1010   cp_token *t = cp_lexer_peek_token (lexer);
1011   return token_is_decltype (t);
1012 }
1013
1014 /* Called when processing a token with tree_check_value; perform or defer the
1015    associated checks and return the value.  */
1016
1017 static tree
1018 saved_checks_value (struct tree_check *check_value)
1019 {
1020   /* Perform any access checks that were deferred.  */
1021   vec<deferred_access_check, va_gc> *checks;
1022   deferred_access_check *chk;
1023   checks = check_value->checks;
1024   if (checks)
1025     {
1026       int i;
1027       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1028         perform_or_defer_access_check (chk->binfo,
1029                                        chk->decl,
1030                                        chk->diag_decl, tf_warning_or_error);
1031     }
1032   /* Return the stored value.  */
1033   return check_value->value;
1034 }
1035
1036 /* Return a pointer to the Nth token in the token stream.  If N is 1,
1037    then this is precisely equivalent to cp_lexer_peek_token (except
1038    that it is not inline).  One would like to disallow that case, but
1039    there is one case (cp_parser_nth_token_starts_template_id) where
1040    the caller passes a variable for N and it might be 1.  */
1041
1042 static cp_token *
1043 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1044 {
1045   cp_token *token;
1046
1047   /* N is 1-based, not zero-based.  */
1048   gcc_assert (n > 0);
1049
1050   if (cp_lexer_debugging_p (lexer))
1051     fprintf (cp_lexer_debug_stream,
1052              "cp_lexer: peeking ahead %ld at token: ", (long)n);
1053
1054   --n;
1055   token = lexer->next_token;
1056   gcc_assert (!n || token != &eof_token);
1057   while (n != 0)
1058     {
1059       ++token;
1060       if (token == lexer->last_token)
1061         {
1062           token = &eof_token;
1063           break;
1064         }
1065
1066       if (!token->purged_p)
1067         --n;
1068     }
1069
1070   if (cp_lexer_debugging_p (lexer))
1071     {
1072       cp_lexer_print_token (cp_lexer_debug_stream, token);
1073       putc ('\n', cp_lexer_debug_stream);
1074     }
1075
1076   return token;
1077 }
1078
1079 /* Return the next token, and advance the lexer's next_token pointer
1080    to point to the next non-purged token.  */
1081
1082 static cp_token *
1083 cp_lexer_consume_token (cp_lexer* lexer)
1084 {
1085   cp_token *token = lexer->next_token;
1086
1087   gcc_assert (token != &eof_token);
1088   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1089
1090   do
1091     {
1092       lexer->next_token++;
1093       if (lexer->next_token == lexer->last_token)
1094         {
1095           lexer->next_token = &eof_token;
1096           break;
1097         }
1098
1099     }
1100   while (lexer->next_token->purged_p);
1101
1102   cp_lexer_set_source_position_from_token (token);
1103
1104   /* Provide debugging output.  */
1105   if (cp_lexer_debugging_p (lexer))
1106     {
1107       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1108       cp_lexer_print_token (cp_lexer_debug_stream, token);
1109       putc ('\n', cp_lexer_debug_stream);
1110     }
1111
1112   return token;
1113 }
1114
1115 /* Permanently remove the next token from the token stream, and
1116    advance the next_token pointer to refer to the next non-purged
1117    token.  */
1118
1119 static void
1120 cp_lexer_purge_token (cp_lexer *lexer)
1121 {
1122   cp_token *tok = lexer->next_token;
1123
1124   gcc_assert (tok != &eof_token);
1125   tok->purged_p = true;
1126   tok->location = UNKNOWN_LOCATION;
1127   tok->u.value = NULL_TREE;
1128   tok->keyword = RID_MAX;
1129
1130   do
1131     {
1132       tok++;
1133       if (tok == lexer->last_token)
1134         {
1135           tok = &eof_token;
1136           break;
1137         }
1138     }
1139   while (tok->purged_p);
1140   lexer->next_token = tok;
1141 }
1142
1143 /* Permanently remove all tokens after TOK, up to, but not
1144    including, the token that will be returned next by
1145    cp_lexer_peek_token.  */
1146
1147 static void
1148 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1149 {
1150   cp_token *peek = lexer->next_token;
1151
1152   if (peek == &eof_token)
1153     peek = lexer->last_token;
1154
1155   gcc_assert (tok < peek);
1156
1157   for ( tok += 1; tok != peek; tok += 1)
1158     {
1159       tok->purged_p = true;
1160       tok->location = UNKNOWN_LOCATION;
1161       tok->u.value = NULL_TREE;
1162       tok->keyword = RID_MAX;
1163     }
1164 }
1165
1166 /* Begin saving tokens.  All tokens consumed after this point will be
1167    preserved.  */
1168
1169 static void
1170 cp_lexer_save_tokens (cp_lexer* lexer)
1171 {
1172   /* Provide debugging output.  */
1173   if (cp_lexer_debugging_p (lexer))
1174     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1175
1176   lexer->saved_tokens.safe_push (lexer->next_token);
1177 }
1178
1179 /* Commit to the portion of the token stream most recently saved.  */
1180
1181 static void
1182 cp_lexer_commit_tokens (cp_lexer* lexer)
1183 {
1184   /* Provide debugging output.  */
1185   if (cp_lexer_debugging_p (lexer))
1186     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1187
1188   lexer->saved_tokens.pop ();
1189 }
1190
1191 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1192    to the token stream.  Stop saving tokens.  */
1193
1194 static void
1195 cp_lexer_rollback_tokens (cp_lexer* lexer)
1196 {
1197   /* Provide debugging output.  */
1198   if (cp_lexer_debugging_p (lexer))
1199     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1200
1201   lexer->next_token = lexer->saved_tokens.pop ();
1202 }
1203
1204 /* RAII wrapper around the above functions, with sanity checking.  Creating
1205    a variable saves tokens, which are committed when the variable is
1206    destroyed unless they are explicitly rolled back by calling the rollback
1207    member function.  */
1208
1209 struct saved_token_sentinel
1210 {
1211   cp_lexer *lexer;
1212   unsigned len;
1213   bool commit;
1214   saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1215   {
1216     len = lexer->saved_tokens.length ();
1217     cp_lexer_save_tokens (lexer);
1218   }
1219   void rollback ()
1220   {
1221     cp_lexer_rollback_tokens (lexer);
1222     commit = false;
1223   }
1224   ~saved_token_sentinel()
1225   {
1226     if (commit)
1227       cp_lexer_commit_tokens (lexer);
1228     gcc_assert (lexer->saved_tokens.length () == len);
1229   }
1230 };
1231
1232 /* Print a representation of the TOKEN on the STREAM.  */
1233
1234 static void
1235 cp_lexer_print_token (FILE * stream, cp_token *token)
1236 {
1237   /* We don't use cpp_type2name here because the parser defines
1238      a few tokens of its own.  */
1239   static const char *const token_names[] = {
1240     /* cpplib-defined token types */
1241 #define OP(e, s) #e,
1242 #define TK(e, s) #e,
1243     TTYPE_TABLE
1244 #undef OP
1245 #undef TK
1246     /* C++ parser token types - see "Manifest constants", above.  */
1247     "KEYWORD",
1248     "TEMPLATE_ID",
1249     "NESTED_NAME_SPECIFIER",
1250   };
1251
1252   /* For some tokens, print the associated data.  */
1253   switch (token->type)
1254     {
1255     case CPP_KEYWORD:
1256       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1257          For example, `struct' is mapped to an INTEGER_CST.  */
1258       if (!identifier_p (token->u.value))
1259         break;
1260       /* else fall through */
1261     case CPP_NAME:
1262       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1263       break;
1264
1265     case CPP_STRING:
1266     case CPP_STRING16:
1267     case CPP_STRING32:
1268     case CPP_WSTRING:
1269     case CPP_UTF8STRING:
1270       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1271       break;
1272
1273     case CPP_NUMBER:
1274       print_generic_expr (stream, token->u.value, 0);
1275       break;
1276
1277     default:
1278       /* If we have a name for the token, print it out.  Otherwise, we
1279          simply give the numeric code.  */
1280       if (token->type < ARRAY_SIZE(token_names))
1281         fputs (token_names[token->type], stream);
1282       else
1283         fprintf (stream, "[%d]", token->type);
1284       break;
1285     }
1286 }
1287
1288 DEBUG_FUNCTION void
1289 debug (cp_token &ref)
1290 {
1291   cp_lexer_print_token (stderr, &ref);
1292   fprintf (stderr, "\n");
1293 }
1294
1295 DEBUG_FUNCTION void
1296 debug (cp_token *ptr)
1297 {
1298   if (ptr)
1299     debug (*ptr);
1300   else
1301     fprintf (stderr, "<nil>\n");
1302 }
1303
1304
1305 /* Start emitting debugging information.  */
1306
1307 static void
1308 cp_lexer_start_debugging (cp_lexer* lexer)
1309 {
1310   if (!LEXER_DEBUGGING_ENABLED_P)
1311     fatal_error (input_location,
1312                  "LEXER_DEBUGGING_ENABLED_P is not set to true");
1313
1314   lexer->debugging_p = true;
1315   cp_lexer_debug_stream = stderr;
1316 }
1317
1318 /* Stop emitting debugging information.  */
1319
1320 static void
1321 cp_lexer_stop_debugging (cp_lexer* lexer)
1322 {
1323   if (!LEXER_DEBUGGING_ENABLED_P)
1324     fatal_error (input_location,
1325                  "LEXER_DEBUGGING_ENABLED_P is not set to true");
1326
1327   lexer->debugging_p = false;
1328   cp_lexer_debug_stream = NULL;
1329 }
1330
1331 /* Create a new cp_token_cache, representing a range of tokens.  */
1332
1333 static cp_token_cache *
1334 cp_token_cache_new (cp_token *first, cp_token *last)
1335 {
1336   cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1337   cache->first = first;
1338   cache->last = last;
1339   return cache;
1340 }
1341
1342 /* Diagnose if #pragma omp declare simd isn't followed immediately
1343    by function declaration or definition.  */
1344
1345 static inline void
1346 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1347 {
1348   if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1349     {
1350       error ("%<#pragma omp declare simd%> not immediately followed by "
1351              "function declaration or definition");
1352       parser->omp_declare_simd = NULL;
1353     }
1354 }
1355
1356 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1357    and put that into "omp declare simd" attribute.  */
1358
1359 static inline void
1360 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1361 {
1362   if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1363     {
1364       if (fndecl == error_mark_node)
1365         {
1366           parser->omp_declare_simd = NULL;
1367           return;
1368         }
1369       if (TREE_CODE (fndecl) != FUNCTION_DECL)
1370         {
1371           cp_ensure_no_omp_declare_simd (parser);
1372           return;
1373         }
1374     }
1375 }
1376
1377 /* Diagnose if #pragma acc routine isn't followed immediately by function
1378    declaration or definition.  */
1379
1380 static inline void
1381 cp_ensure_no_oacc_routine (cp_parser *parser)
1382 {
1383   if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1384     {
1385       tree clauses = parser->oacc_routine->clauses;
1386       location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1387
1388       error_at (loc, "%<#pragma acc routine%> not followed by a function "
1389                 "declaration or definition");
1390       parser->oacc_routine = NULL;
1391     }
1392 }
1393 \f
1394 /* Decl-specifiers.  */
1395
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1397
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1400 {
1401   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1402 }
1403
1404 /* Declarators.  */
1405
1406 /* Nothing other than the parser should be creating declarators;
1407    declarators are a semi-syntactic representation of C++ entities.
1408    Other parts of the front end that need to create entities (like
1409    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1410
1411 static cp_declarator *make_call_declarator
1412   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414   (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416   (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418   (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420   (cp_cv_quals, tree, cp_declarator *, tree);
1421
1422 /* An erroneous declarator.  */
1423 static cp_declarator *cp_error_declarator;
1424
1425 /* The obstack on which declarators and related data structures are
1426    allocated.  */
1427 static struct obstack declarator_obstack;
1428
1429 /* Alloc BYTES from the declarator memory pool.  */
1430
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1433 {
1434   return obstack_alloc (&declarator_obstack, bytes);
1435 }
1436
1437 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1438    common to all declarators.  */
1439
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1442 {
1443   cp_declarator *declarator;
1444
1445   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446   declarator->kind = kind;
1447   declarator->attributes = NULL_TREE;
1448   declarator->std_attributes = NULL_TREE;
1449   declarator->declarator = NULL;
1450   declarator->parameter_pack_p = false;
1451   declarator->id_loc = UNKNOWN_LOCATION;
1452
1453   return declarator;
1454 }
1455
1456 /* Make a declarator for a generalized identifier.  If
1457    QUALIFYING_SCOPE is non-NULL, the identifier is
1458    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1459    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1460    is, if any.   */
1461
1462 static cp_declarator *
1463 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1464                     special_function_kind sfk)
1465 {
1466   cp_declarator *declarator;
1467
1468   /* It is valid to write:
1469
1470        class C { void f(); };
1471        typedef C D;
1472        void D::f();
1473
1474      The standard is not clear about whether `typedef const C D' is
1475      legal; as of 2002-09-15 the committee is considering that
1476      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1477      well.  */
1478   if (qualifying_scope && TYPE_P (qualifying_scope))
1479     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1480
1481   gcc_assert (identifier_p (unqualified_name)
1482               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1483               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1484
1485   declarator = make_declarator (cdk_id);
1486   declarator->u.id.qualifying_scope = qualifying_scope;
1487   declarator->u.id.unqualified_name = unqualified_name;
1488   declarator->u.id.sfk = sfk;
1489   
1490   return declarator;
1491 }
1492
1493 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1494    of modifiers such as const or volatile to apply to the pointer
1495    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1496    appertain to the pointer or reference.  */
1497
1498 cp_declarator *
1499 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1500                          tree attributes)
1501 {
1502   cp_declarator *declarator;
1503
1504   declarator = make_declarator (cdk_pointer);
1505   declarator->declarator = target;
1506   declarator->u.pointer.qualifiers = cv_qualifiers;
1507   declarator->u.pointer.class_type = NULL_TREE;
1508   if (target)
1509     {
1510       declarator->id_loc = target->id_loc;
1511       declarator->parameter_pack_p = target->parameter_pack_p;
1512       target->parameter_pack_p = false;
1513     }
1514   else
1515     declarator->parameter_pack_p = false;
1516
1517   declarator->std_attributes = attributes;
1518
1519   return declarator;
1520 }
1521
1522 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1523    represent the attributes that appertain to the pointer or
1524    reference.  */
1525
1526 cp_declarator *
1527 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1528                            bool rvalue_ref, tree attributes)
1529 {
1530   cp_declarator *declarator;
1531
1532   declarator = make_declarator (cdk_reference);
1533   declarator->declarator = target;
1534   declarator->u.reference.qualifiers = cv_qualifiers;
1535   declarator->u.reference.rvalue_ref = rvalue_ref;
1536   if (target)
1537     {
1538       declarator->id_loc = target->id_loc;
1539       declarator->parameter_pack_p = target->parameter_pack_p;
1540       target->parameter_pack_p = false;
1541     }
1542   else
1543     declarator->parameter_pack_p = false;
1544
1545   declarator->std_attributes = attributes;
1546
1547   return declarator;
1548 }
1549
1550 /* Like make_pointer_declarator -- but for a pointer to a non-static
1551    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1552    appertain to the pointer or reference.  */
1553
1554 cp_declarator *
1555 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1556                         cp_declarator *pointee,
1557                         tree attributes)
1558 {
1559   cp_declarator *declarator;
1560
1561   declarator = make_declarator (cdk_ptrmem);
1562   declarator->declarator = pointee;
1563   declarator->u.pointer.qualifiers = cv_qualifiers;
1564   declarator->u.pointer.class_type = class_type;
1565
1566   if (pointee)
1567     {
1568       declarator->parameter_pack_p = pointee->parameter_pack_p;
1569       pointee->parameter_pack_p = false;
1570     }
1571   else
1572     declarator->parameter_pack_p = false;
1573
1574   declarator->std_attributes = attributes;
1575
1576   return declarator;
1577 }
1578
1579 /* Make a declarator for the function given by TARGET, with the
1580    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1581    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1582    indicates what exceptions can be thrown.  */
1583
1584 cp_declarator *
1585 make_call_declarator (cp_declarator *target,
1586                       tree parms,
1587                       cp_cv_quals cv_qualifiers,
1588                       cp_virt_specifiers virt_specifiers,
1589                       cp_ref_qualifier ref_qualifier,
1590                       tree tx_qualifier,
1591                       tree exception_specification,
1592                       tree late_return_type,
1593                       tree requires_clause)
1594 {
1595   cp_declarator *declarator;
1596
1597   declarator = make_declarator (cdk_function);
1598   declarator->declarator = target;
1599   declarator->u.function.parameters = parms;
1600   declarator->u.function.qualifiers = cv_qualifiers;
1601   declarator->u.function.virt_specifiers = virt_specifiers;
1602   declarator->u.function.ref_qualifier = ref_qualifier;
1603   declarator->u.function.tx_qualifier = tx_qualifier;
1604   declarator->u.function.exception_specification = exception_specification;
1605   declarator->u.function.late_return_type = late_return_type;
1606   declarator->u.function.requires_clause = requires_clause;
1607   if (target)
1608     {
1609       declarator->id_loc = target->id_loc;
1610       declarator->parameter_pack_p = target->parameter_pack_p;
1611       target->parameter_pack_p = false;
1612     }
1613   else
1614     declarator->parameter_pack_p = false;
1615
1616   return declarator;
1617 }
1618
1619 /* Make a declarator for an array of BOUNDS elements, each of which is
1620    defined by ELEMENT.  */
1621
1622 cp_declarator *
1623 make_array_declarator (cp_declarator *element, tree bounds)
1624 {
1625   cp_declarator *declarator;
1626
1627   declarator = make_declarator (cdk_array);
1628   declarator->declarator = element;
1629   declarator->u.array.bounds = bounds;
1630   if (element)
1631     {
1632       declarator->id_loc = element->id_loc;
1633       declarator->parameter_pack_p = element->parameter_pack_p;
1634       element->parameter_pack_p = false;
1635     }
1636   else
1637     declarator->parameter_pack_p = false;
1638
1639   return declarator;
1640 }
1641
1642 /* Determine whether the declarator we've seen so far can be a
1643    parameter pack, when followed by an ellipsis.  */
1644 static bool 
1645 declarator_can_be_parameter_pack (cp_declarator *declarator)
1646 {
1647   if (declarator && declarator->parameter_pack_p)
1648     /* We already saw an ellipsis.  */
1649     return false;
1650
1651   /* Search for a declarator name, or any other declarator that goes
1652      after the point where the ellipsis could appear in a parameter
1653      pack. If we find any of these, then this declarator can not be
1654      made into a parameter pack.  */
1655   bool found = false;
1656   while (declarator && !found)
1657     {
1658       switch ((int)declarator->kind)
1659         {
1660         case cdk_id:
1661         case cdk_array:
1662           found = true;
1663           break;
1664
1665         case cdk_error:
1666           return true;
1667
1668         default:
1669           declarator = declarator->declarator;
1670           break;
1671         }
1672     }
1673
1674   return !found;
1675 }
1676
1677 cp_parameter_declarator *no_parameters;
1678
1679 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1680    DECLARATOR and DEFAULT_ARGUMENT.  */
1681
1682 cp_parameter_declarator *
1683 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1684                            cp_declarator *declarator,
1685                            tree default_argument,
1686                            bool template_parameter_pack_p = false)
1687 {
1688   cp_parameter_declarator *parameter;
1689
1690   parameter = ((cp_parameter_declarator *)
1691                alloc_declarator (sizeof (cp_parameter_declarator)));
1692   parameter->next = NULL;
1693   if (decl_specifiers)
1694     parameter->decl_specifiers = *decl_specifiers;
1695   else
1696     clear_decl_specs (&parameter->decl_specifiers);
1697   parameter->declarator = declarator;
1698   parameter->default_argument = default_argument;
1699   parameter->template_parameter_pack_p = template_parameter_pack_p;
1700
1701   return parameter;
1702 }
1703
1704 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1705
1706 static bool
1707 function_declarator_p (const cp_declarator *declarator)
1708 {
1709   while (declarator)
1710     {
1711       if (declarator->kind == cdk_function
1712           && declarator->declarator->kind == cdk_id)
1713         return true;
1714       if (declarator->kind == cdk_id
1715           || declarator->kind == cdk_error)
1716         return false;
1717       declarator = declarator->declarator;
1718     }
1719   return false;
1720 }
1721  
1722 /* The parser.  */
1723
1724 /* Overview
1725    --------
1726
1727    A cp_parser parses the token stream as specified by the C++
1728    grammar.  Its job is purely parsing, not semantic analysis.  For
1729    example, the parser breaks the token stream into declarators,
1730    expressions, statements, and other similar syntactic constructs.
1731    It does not check that the types of the expressions on either side
1732    of an assignment-statement are compatible, or that a function is
1733    not declared with a parameter of type `void'.
1734
1735    The parser invokes routines elsewhere in the compiler to perform
1736    semantic analysis and to build up the abstract syntax tree for the
1737    code processed.
1738
1739    The parser (and the template instantiation code, which is, in a
1740    way, a close relative of parsing) are the only parts of the
1741    compiler that should be calling push_scope and pop_scope, or
1742    related functions.  The parser (and template instantiation code)
1743    keeps track of what scope is presently active; everything else
1744    should simply honor that.  (The code that generates static
1745    initializers may also need to set the scope, in order to check
1746    access control correctly when emitting the initializers.)
1747
1748    Methodology
1749    -----------
1750
1751    The parser is of the standard recursive-descent variety.  Upcoming
1752    tokens in the token stream are examined in order to determine which
1753    production to use when parsing a non-terminal.  Some C++ constructs
1754    require arbitrary look ahead to disambiguate.  For example, it is
1755    impossible, in the general case, to tell whether a statement is an
1756    expression or declaration without scanning the entire statement.
1757    Therefore, the parser is capable of "parsing tentatively."  When the
1758    parser is not sure what construct comes next, it enters this mode.
1759    Then, while we attempt to parse the construct, the parser queues up
1760    error messages, rather than issuing them immediately, and saves the
1761    tokens it consumes.  If the construct is parsed successfully, the
1762    parser "commits", i.e., it issues any queued error messages and
1763    the tokens that were being preserved are permanently discarded.
1764    If, however, the construct is not parsed successfully, the parser
1765    rolls back its state completely so that it can resume parsing using
1766    a different alternative.
1767
1768    Future Improvements
1769    -------------------
1770
1771    The performance of the parser could probably be improved substantially.
1772    We could often eliminate the need to parse tentatively by looking ahead
1773    a little bit.  In some places, this approach might not entirely eliminate
1774    the need to parse tentatively, but it might still speed up the average
1775    case.  */
1776
1777 /* Flags that are passed to some parsing functions.  These values can
1778    be bitwise-ored together.  */
1779
1780 enum
1781 {
1782   /* No flags.  */
1783   CP_PARSER_FLAGS_NONE = 0x0,
1784   /* The construct is optional.  If it is not present, then no error
1785      should be issued.  */
1786   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1787   /* When parsing a type-specifier, treat user-defined type-names
1788      as non-type identifiers.  */
1789   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1790   /* When parsing a type-specifier, do not try to parse a class-specifier
1791      or enum-specifier.  */
1792   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1793   /* When parsing a decl-specifier-seq, only allow type-specifier or
1794      constexpr.  */
1795   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1796 };
1797
1798 /* This type is used for parameters and variables which hold
1799    combinations of the above flags.  */
1800 typedef int cp_parser_flags;
1801
1802 /* The different kinds of declarators we want to parse.  */
1803
1804 enum cp_parser_declarator_kind
1805 {
1806   /* We want an abstract declarator.  */
1807   CP_PARSER_DECLARATOR_ABSTRACT,
1808   /* We want a named declarator.  */
1809   CP_PARSER_DECLARATOR_NAMED,
1810   /* We don't mind, but the name must be an unqualified-id.  */
1811   CP_PARSER_DECLARATOR_EITHER
1812 };
1813
1814 /* The precedence values used to parse binary expressions.  The minimum value
1815    of PREC must be 1, because zero is reserved to quickly discriminate
1816    binary operators from other tokens.  */
1817
1818 enum cp_parser_prec
1819 {
1820   PREC_NOT_OPERATOR,
1821   PREC_LOGICAL_OR_EXPRESSION,
1822   PREC_LOGICAL_AND_EXPRESSION,
1823   PREC_INCLUSIVE_OR_EXPRESSION,
1824   PREC_EXCLUSIVE_OR_EXPRESSION,
1825   PREC_AND_EXPRESSION,
1826   PREC_EQUALITY_EXPRESSION,
1827   PREC_RELATIONAL_EXPRESSION,
1828   PREC_SHIFT_EXPRESSION,
1829   PREC_ADDITIVE_EXPRESSION,
1830   PREC_MULTIPLICATIVE_EXPRESSION,
1831   PREC_PM_EXPRESSION,
1832   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1833 };
1834
1835 /* A mapping from a token type to a corresponding tree node type, with a
1836    precedence value.  */
1837
1838 struct cp_parser_binary_operations_map_node
1839 {
1840   /* The token type.  */
1841   enum cpp_ttype token_type;
1842   /* The corresponding tree code.  */
1843   enum tree_code tree_type;
1844   /* The precedence of this operator.  */
1845   enum cp_parser_prec prec;
1846 };
1847
1848 struct cp_parser_expression_stack_entry
1849 {
1850   /* Left hand side of the binary operation we are currently
1851      parsing.  */
1852   cp_expr lhs;
1853   /* Original tree code for left hand side, if it was a binary
1854      expression itself (used for -Wparentheses).  */
1855   enum tree_code lhs_type;
1856   /* Tree code for the binary operation we are parsing.  */
1857   enum tree_code tree_type;
1858   /* Precedence of the binary operation we are parsing.  */
1859   enum cp_parser_prec prec;
1860   /* Location of the binary operation we are parsing.  */
1861   location_t loc;
1862 };
1863
1864 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1865    entries because precedence levels on the stack are monotonically
1866    increasing.  */
1867 typedef struct cp_parser_expression_stack_entry
1868   cp_parser_expression_stack[NUM_PREC_VALUES];
1869
1870 /* Prototypes.  */
1871
1872 /* Constructors and destructors.  */
1873
1874 static cp_parser_context *cp_parser_context_new
1875   (cp_parser_context *);
1876
1877 /* Class variables.  */
1878
1879 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1880
1881 /* The operator-precedence table used by cp_parser_binary_expression.
1882    Transformed into an associative array (binops_by_token) by
1883    cp_parser_new.  */
1884
1885 static const cp_parser_binary_operations_map_node binops[] = {
1886   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1887   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1888
1889   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1891   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1892
1893   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1894   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1895
1896   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1897   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1898
1899   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1901   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1902   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1903
1904   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1905   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1906
1907   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1908
1909   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1910
1911   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1912
1913   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1914
1915   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1916 };
1917
1918 /* The same as binops, but initialized by cp_parser_new so that
1919    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1920    for speed.  */
1921 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1922
1923 /* Constructors and destructors.  */
1924
1925 /* Construct a new context.  The context below this one on the stack
1926    is given by NEXT.  */
1927
1928 static cp_parser_context *
1929 cp_parser_context_new (cp_parser_context* next)
1930 {
1931   cp_parser_context *context;
1932
1933   /* Allocate the storage.  */
1934   if (cp_parser_context_free_list != NULL)
1935     {
1936       /* Pull the first entry from the free list.  */
1937       context = cp_parser_context_free_list;
1938       cp_parser_context_free_list = context->next;
1939       memset (context, 0, sizeof (*context));
1940     }
1941   else
1942     context = ggc_cleared_alloc<cp_parser_context> ();
1943
1944   /* No errors have occurred yet in this context.  */
1945   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1946   /* If this is not the bottommost context, copy information that we
1947      need from the previous context.  */
1948   if (next)
1949     {
1950       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1951          expression, then we are parsing one in this context, too.  */
1952       context->object_type = next->object_type;
1953       /* Thread the stack.  */
1954       context->next = next;
1955     }
1956
1957   return context;
1958 }
1959
1960 /* Managing the unparsed function queues.  */
1961
1962 #define unparsed_funs_with_default_args \
1963   parser->unparsed_queues->last ().funs_with_default_args
1964 #define unparsed_funs_with_definitions \
1965   parser->unparsed_queues->last ().funs_with_definitions
1966 #define unparsed_nsdmis \
1967   parser->unparsed_queues->last ().nsdmis
1968 #define unparsed_classes \
1969   parser->unparsed_queues->last ().classes
1970
1971 static void
1972 push_unparsed_function_queues (cp_parser *parser)
1973 {
1974   cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1975   vec_safe_push (parser->unparsed_queues, e);
1976 }
1977
1978 static void
1979 pop_unparsed_function_queues (cp_parser *parser)
1980 {
1981   release_tree_vector (unparsed_funs_with_definitions);
1982   parser->unparsed_queues->pop ();
1983 }
1984
1985 /* Prototypes.  */
1986
1987 /* Constructors and destructors.  */
1988
1989 static cp_parser *cp_parser_new
1990   (void);
1991
1992 /* Routines to parse various constructs.
1993
1994    Those that return `tree' will return the error_mark_node (rather
1995    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1996    Sometimes, they will return an ordinary node if error-recovery was
1997    attempted, even though a parse error occurred.  So, to check
1998    whether or not a parse error occurred, you should always use
1999    cp_parser_error_occurred.  If the construct is optional (indicated
2000    either by an `_opt' in the name of the function that does the
2001    parsing or via a FLAGS parameter), then NULL_TREE is returned if
2002    the construct is not present.  */
2003
2004 /* Lexical conventions [gram.lex]  */
2005
2006 static cp_expr cp_parser_identifier
2007   (cp_parser *);
2008 static cp_expr cp_parser_string_literal
2009   (cp_parser *, bool, bool, bool);
2010 static cp_expr cp_parser_userdef_char_literal
2011   (cp_parser *);
2012 static tree cp_parser_userdef_string_literal
2013   (tree);
2014 static cp_expr cp_parser_userdef_numeric_literal
2015   (cp_parser *);
2016
2017 /* Basic concepts [gram.basic]  */
2018
2019 static bool cp_parser_translation_unit
2020   (cp_parser *);
2021
2022 /* Expressions [gram.expr]  */
2023
2024 static cp_expr cp_parser_primary_expression
2025   (cp_parser *, bool, bool, bool, cp_id_kind *);
2026 static cp_expr cp_parser_id_expression
2027   (cp_parser *, bool, bool, bool *, bool, bool);
2028 static cp_expr cp_parser_unqualified_id
2029   (cp_parser *, bool, bool, bool, bool);
2030 static tree cp_parser_nested_name_specifier_opt
2031   (cp_parser *, bool, bool, bool, bool);
2032 static tree cp_parser_nested_name_specifier
2033   (cp_parser *, bool, bool, bool, bool);
2034 static tree cp_parser_qualifying_entity
2035   (cp_parser *, bool, bool, bool, bool, bool);
2036 static cp_expr cp_parser_postfix_expression
2037   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2038 static tree cp_parser_postfix_open_square_expression
2039   (cp_parser *, tree, bool, bool);
2040 static tree cp_parser_postfix_dot_deref_expression
2041   (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2042 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2043   (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2044 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
2045 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2046 static void cp_parser_pseudo_destructor_name
2047   (cp_parser *, tree, tree *, tree *);
2048 static cp_expr cp_parser_unary_expression
2049   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2050 static enum tree_code cp_parser_unary_operator
2051   (cp_token *);
2052 static tree cp_parser_new_expression
2053   (cp_parser *);
2054 static vec<tree, va_gc> *cp_parser_new_placement
2055   (cp_parser *);
2056 static tree cp_parser_new_type_id
2057   (cp_parser *, tree *);
2058 static cp_declarator *cp_parser_new_declarator_opt
2059   (cp_parser *);
2060 static cp_declarator *cp_parser_direct_new_declarator
2061   (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_initializer
2063   (cp_parser *);
2064 static tree cp_parser_delete_expression
2065   (cp_parser *);
2066 static cp_expr cp_parser_cast_expression
2067   (cp_parser *, bool, bool, bool, cp_id_kind *);
2068 static cp_expr cp_parser_binary_expression
2069   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2070 static tree cp_parser_question_colon_clause
2071   (cp_parser *, cp_expr);
2072 static cp_expr cp_parser_assignment_expression
2073   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2074 static enum tree_code cp_parser_assignment_operator_opt
2075   (cp_parser *);
2076 static cp_expr cp_parser_expression
2077   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2078 static cp_expr cp_parser_constant_expression
2079   (cp_parser *, bool = false, bool * = NULL);
2080 static cp_expr cp_parser_builtin_offsetof
2081   (cp_parser *);
2082 static cp_expr cp_parser_lambda_expression
2083   (cp_parser *);
2084 static void cp_parser_lambda_introducer
2085   (cp_parser *, tree);
2086 static bool cp_parser_lambda_declarator_opt
2087   (cp_parser *, tree);
2088 static void cp_parser_lambda_body
2089   (cp_parser *, tree);
2090
2091 /* Statements [gram.stmt.stmt]  */
2092
2093 static void cp_parser_statement
2094   (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2095 static void cp_parser_label_for_labeled_statement
2096 (cp_parser *, tree);
2097 static tree cp_parser_expression_statement
2098   (cp_parser *, tree);
2099 static tree cp_parser_compound_statement
2100   (cp_parser *, tree, int, bool);
2101 static void cp_parser_statement_seq_opt
2102   (cp_parser *, tree);
2103 static tree cp_parser_selection_statement
2104   (cp_parser *, bool *, vec<tree> *);
2105 static tree cp_parser_condition
2106   (cp_parser *);
2107 static tree cp_parser_iteration_statement
2108   (cp_parser *, bool *, bool);
2109 static bool cp_parser_for_init_statement
2110   (cp_parser *, tree *decl);
2111 static tree cp_parser_for
2112   (cp_parser *, bool);
2113 static tree cp_parser_c_for
2114   (cp_parser *, tree, tree, bool);
2115 static tree cp_parser_range_for
2116   (cp_parser *, tree, tree, tree, bool);
2117 static void do_range_for_auto_deduction
2118   (tree, tree);
2119 static tree cp_parser_perform_range_for_lookup
2120   (tree, tree *, tree *);
2121 static tree cp_parser_range_for_member_function
2122   (tree, tree);
2123 static tree cp_parser_jump_statement
2124   (cp_parser *);
2125 static void cp_parser_declaration_statement
2126   (cp_parser *);
2127
2128 static tree cp_parser_implicitly_scoped_statement
2129   (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2130 static void cp_parser_already_scoped_statement
2131   (cp_parser *, bool *, const token_indent_info &);
2132
2133 /* Declarations [gram.dcl.dcl] */
2134
2135 static void cp_parser_declaration_seq_opt
2136   (cp_parser *);
2137 static void cp_parser_declaration
2138   (cp_parser *);
2139 static void cp_parser_block_declaration
2140   (cp_parser *, bool);
2141 static void cp_parser_simple_declaration
2142   (cp_parser *, bool, tree *);
2143 static void cp_parser_decl_specifier_seq
2144   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2145 static tree cp_parser_storage_class_specifier_opt
2146   (cp_parser *);
2147 static tree cp_parser_function_specifier_opt
2148   (cp_parser *, cp_decl_specifier_seq *);
2149 static tree cp_parser_type_specifier
2150   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2151    int *, bool *);
2152 static tree cp_parser_simple_type_specifier
2153   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2154 static tree cp_parser_type_name
2155   (cp_parser *, bool);
2156 static tree cp_parser_type_name
2157   (cp_parser *);
2158 static tree cp_parser_nonclass_name 
2159   (cp_parser* parser);
2160 static tree cp_parser_elaborated_type_specifier
2161   (cp_parser *, bool, bool);
2162 static tree cp_parser_enum_specifier
2163   (cp_parser *);
2164 static void cp_parser_enumerator_list
2165   (cp_parser *, tree);
2166 static void cp_parser_enumerator_definition
2167   (cp_parser *, tree);
2168 static tree cp_parser_namespace_name
2169   (cp_parser *);
2170 static void cp_parser_namespace_definition
2171   (cp_parser *);
2172 static void cp_parser_namespace_body
2173   (cp_parser *);
2174 static tree cp_parser_qualified_namespace_specifier
2175   (cp_parser *);
2176 static void cp_parser_namespace_alias_definition
2177   (cp_parser *);
2178 static bool cp_parser_using_declaration
2179   (cp_parser *, bool);
2180 static void cp_parser_using_directive
2181   (cp_parser *);
2182 static tree cp_parser_alias_declaration
2183   (cp_parser *);
2184 static void cp_parser_asm_definition
2185   (cp_parser *);
2186 static void cp_parser_linkage_specification
2187   (cp_parser *);
2188 static void cp_parser_static_assert
2189   (cp_parser *, bool);
2190 static tree cp_parser_decltype
2191   (cp_parser *);
2192
2193 /* Declarators [gram.dcl.decl] */
2194
2195 static tree cp_parser_init_declarator
2196   (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2197    bool, bool, int, bool *, tree *, location_t *, tree *);
2198 static cp_declarator *cp_parser_declarator
2199   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2200 static cp_declarator *cp_parser_direct_declarator
2201   (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2202 static enum tree_code cp_parser_ptr_operator
2203   (cp_parser *, tree *, cp_cv_quals *, tree *);
2204 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2205   (cp_parser *);
2206 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2207   (cp_parser *);
2208 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2209   (cp_parser *);
2210 static tree cp_parser_tx_qualifier_opt
2211   (cp_parser *);
2212 static tree cp_parser_late_return_type_opt
2213   (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2214 static tree cp_parser_declarator_id
2215   (cp_parser *, bool);
2216 static tree cp_parser_type_id
2217   (cp_parser *);
2218 static tree cp_parser_template_type_arg
2219   (cp_parser *);
2220 static tree cp_parser_trailing_type_id (cp_parser *);
2221 static tree cp_parser_type_id_1
2222   (cp_parser *, bool, bool);
2223 static void cp_parser_type_specifier_seq
2224   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2225 static tree cp_parser_parameter_declaration_clause
2226   (cp_parser *);
2227 static tree cp_parser_parameter_declaration_list
2228   (cp_parser *, bool *);
2229 static cp_parameter_declarator *cp_parser_parameter_declaration
2230   (cp_parser *, bool, bool *);
2231 static tree cp_parser_default_argument 
2232   (cp_parser *, bool);
2233 static void cp_parser_function_body
2234   (cp_parser *, bool);
2235 static tree cp_parser_initializer
2236   (cp_parser *, bool *, bool *);
2237 static cp_expr cp_parser_initializer_clause
2238   (cp_parser *, bool *);
2239 static cp_expr cp_parser_braced_list
2240   (cp_parser*, bool*);
2241 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2242   (cp_parser *, bool *);
2243
2244 static bool cp_parser_ctor_initializer_opt_and_function_body
2245   (cp_parser *, bool);
2246
2247 static tree cp_parser_late_parsing_omp_declare_simd
2248   (cp_parser *, tree);
2249
2250 static tree cp_parser_late_parsing_cilk_simd_fn_info
2251   (cp_parser *, tree);
2252
2253 static tree cp_parser_late_parsing_oacc_routine
2254   (cp_parser *, tree);
2255
2256 static tree synthesize_implicit_template_parm
2257   (cp_parser *, tree);
2258 static tree finish_fully_implicit_template
2259   (cp_parser *, tree);
2260
2261 /* Classes [gram.class] */
2262
2263 static tree cp_parser_class_name
2264   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2265 static tree cp_parser_class_specifier
2266   (cp_parser *);
2267 static tree cp_parser_class_head
2268   (cp_parser *, bool *);
2269 static enum tag_types cp_parser_class_key
2270   (cp_parser *);
2271 static void cp_parser_type_parameter_key
2272   (cp_parser* parser);
2273 static void cp_parser_member_specification_opt
2274   (cp_parser *);
2275 static void cp_parser_member_declaration
2276   (cp_parser *);
2277 static tree cp_parser_pure_specifier
2278   (cp_parser *);
2279 static tree cp_parser_constant_initializer
2280   (cp_parser *);
2281
2282 /* Derived classes [gram.class.derived] */
2283
2284 static tree cp_parser_base_clause
2285   (cp_parser *);
2286 static tree cp_parser_base_specifier
2287   (cp_parser *);
2288
2289 /* Special member functions [gram.special] */
2290
2291 static tree cp_parser_conversion_function_id
2292   (cp_parser *);
2293 static tree cp_parser_conversion_type_id
2294   (cp_parser *);
2295 static cp_declarator *cp_parser_conversion_declarator_opt
2296   (cp_parser *);
2297 static bool cp_parser_ctor_initializer_opt
2298   (cp_parser *);
2299 static void cp_parser_mem_initializer_list
2300   (cp_parser *);
2301 static tree cp_parser_mem_initializer
2302   (cp_parser *);
2303 static tree cp_parser_mem_initializer_id
2304   (cp_parser *);
2305
2306 /* Overloading [gram.over] */
2307
2308 static cp_expr cp_parser_operator_function_id
2309   (cp_parser *);
2310 static cp_expr cp_parser_operator
2311   (cp_parser *);
2312
2313 /* Templates [gram.temp] */
2314
2315 static void cp_parser_template_declaration
2316   (cp_parser *, bool);
2317 static tree cp_parser_template_parameter_list
2318   (cp_parser *);
2319 static tree cp_parser_template_parameter
2320   (cp_parser *, bool *, bool *);
2321 static tree cp_parser_type_parameter
2322   (cp_parser *, bool *);
2323 static tree cp_parser_template_id
2324   (cp_parser *, bool, bool, enum tag_types, bool);
2325 static tree cp_parser_template_name
2326   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2327 static tree cp_parser_template_argument_list
2328   (cp_parser *);
2329 static tree cp_parser_template_argument
2330   (cp_parser *);
2331 static void cp_parser_explicit_instantiation
2332   (cp_parser *);
2333 static void cp_parser_explicit_specialization
2334   (cp_parser *);
2335
2336 /* Exception handling [gram.exception] */
2337
2338 static tree cp_parser_try_block
2339   (cp_parser *);
2340 static bool cp_parser_function_try_block
2341   (cp_parser *);
2342 static void cp_parser_handler_seq
2343   (cp_parser *);
2344 static void cp_parser_handler
2345   (cp_parser *);
2346 static tree cp_parser_exception_declaration
2347   (cp_parser *);
2348 static tree cp_parser_throw_expression
2349   (cp_parser *);
2350 static tree cp_parser_exception_specification_opt
2351   (cp_parser *);
2352 static tree cp_parser_type_id_list
2353   (cp_parser *);
2354
2355 /* GNU Extensions */
2356
2357 static tree cp_parser_asm_specification_opt
2358   (cp_parser *);
2359 static tree cp_parser_asm_operand_list
2360   (cp_parser *);
2361 static tree cp_parser_asm_clobber_list
2362   (cp_parser *);
2363 static tree cp_parser_asm_label_list
2364   (cp_parser *);
2365 static bool cp_next_tokens_can_be_attribute_p
2366   (cp_parser *);
2367 static bool cp_next_tokens_can_be_gnu_attribute_p
2368   (cp_parser *);
2369 static bool cp_next_tokens_can_be_std_attribute_p
2370   (cp_parser *);
2371 static bool cp_nth_tokens_can_be_std_attribute_p
2372   (cp_parser *, size_t);
2373 static bool cp_nth_tokens_can_be_gnu_attribute_p
2374   (cp_parser *, size_t);
2375 static bool cp_nth_tokens_can_be_attribute_p
2376   (cp_parser *, size_t);
2377 static tree cp_parser_attributes_opt
2378   (cp_parser *);
2379 static tree cp_parser_gnu_attributes_opt
2380   (cp_parser *);
2381 static tree cp_parser_gnu_attribute_list
2382   (cp_parser *);
2383 static tree cp_parser_std_attribute
2384   (cp_parser *);
2385 static tree cp_parser_std_attribute_spec
2386   (cp_parser *);
2387 static tree cp_parser_std_attribute_spec_seq
2388   (cp_parser *);
2389 static bool cp_parser_extension_opt
2390   (cp_parser *, int *);
2391 static void cp_parser_label_declaration
2392   (cp_parser *);
2393
2394 /* Concept Extensions */
2395
2396 static tree cp_parser_requires_clause
2397   (cp_parser *);
2398 static tree cp_parser_requires_clause_opt
2399   (cp_parser *);
2400 static tree cp_parser_requires_expression
2401   (cp_parser *);
2402 static tree cp_parser_requirement_parameter_list
2403   (cp_parser *);
2404 static tree cp_parser_requirement_body
2405   (cp_parser *);
2406 static tree cp_parser_requirement_list
2407   (cp_parser *);
2408 static tree cp_parser_requirement
2409   (cp_parser *);
2410 static tree cp_parser_simple_requirement
2411   (cp_parser *);
2412 static tree cp_parser_compound_requirement
2413   (cp_parser *);
2414 static tree cp_parser_type_requirement
2415   (cp_parser *);
2416 static tree cp_parser_nested_requirement
2417   (cp_parser *);
2418
2419 /* Transactional Memory Extensions */
2420
2421 static tree cp_parser_transaction
2422   (cp_parser *, cp_token *);
2423 static tree cp_parser_transaction_expression
2424   (cp_parser *, enum rid);
2425 static bool cp_parser_function_transaction
2426   (cp_parser *, enum rid);
2427 static tree cp_parser_transaction_cancel
2428   (cp_parser *);
2429
2430 enum pragma_context {
2431   pragma_external,
2432   pragma_member,
2433   pragma_objc_icode,
2434   pragma_stmt,
2435   pragma_compound
2436 };
2437 static bool cp_parser_pragma
2438   (cp_parser *, enum pragma_context, bool *);
2439
2440 /* Objective-C++ Productions */
2441
2442 static tree cp_parser_objc_message_receiver
2443   (cp_parser *);
2444 static tree cp_parser_objc_message_args
2445   (cp_parser *);
2446 static tree cp_parser_objc_message_expression
2447   (cp_parser *);
2448 static cp_expr cp_parser_objc_encode_expression
2449   (cp_parser *);
2450 static tree cp_parser_objc_defs_expression
2451   (cp_parser *);
2452 static tree cp_parser_objc_protocol_expression
2453   (cp_parser *);
2454 static tree cp_parser_objc_selector_expression
2455   (cp_parser *);
2456 static cp_expr cp_parser_objc_expression
2457   (cp_parser *);
2458 static bool cp_parser_objc_selector_p
2459   (enum cpp_ttype);
2460 static tree cp_parser_objc_selector
2461   (cp_parser *);
2462 static tree cp_parser_objc_protocol_refs_opt
2463   (cp_parser *);
2464 static void cp_parser_objc_declaration
2465   (cp_parser *, tree);
2466 static tree cp_parser_objc_statement
2467   (cp_parser *);
2468 static bool cp_parser_objc_valid_prefix_attributes
2469   (cp_parser *, tree *);
2470 static void cp_parser_objc_at_property_declaration 
2471   (cp_parser *) ;
2472 static void cp_parser_objc_at_synthesize_declaration 
2473   (cp_parser *) ;
2474 static void cp_parser_objc_at_dynamic_declaration
2475   (cp_parser *) ;
2476 static tree cp_parser_objc_struct_declaration
2477   (cp_parser *) ;
2478
2479 /* Utility Routines */
2480
2481 static cp_expr cp_parser_lookup_name
2482   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2483 static tree cp_parser_lookup_name_simple
2484   (cp_parser *, tree, location_t);
2485 static tree cp_parser_maybe_treat_template_as_class
2486   (tree, bool);
2487 static bool cp_parser_check_declarator_template_parameters
2488   (cp_parser *, cp_declarator *, location_t);
2489 static bool cp_parser_check_template_parameters
2490   (cp_parser *, unsigned, location_t, cp_declarator *);
2491 static cp_expr cp_parser_simple_cast_expression
2492   (cp_parser *);
2493 static tree cp_parser_global_scope_opt
2494   (cp_parser *, bool);
2495 static bool cp_parser_constructor_declarator_p
2496   (cp_parser *, bool);
2497 static tree cp_parser_function_definition_from_specifiers_and_declarator
2498   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2499 static tree cp_parser_function_definition_after_declarator
2500   (cp_parser *, bool);
2501 static bool cp_parser_template_declaration_after_export
2502   (cp_parser *, bool);
2503 static void cp_parser_perform_template_parameter_access_checks
2504   (vec<deferred_access_check, va_gc> *);
2505 static tree cp_parser_single_declaration
2506   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2507 static cp_expr cp_parser_functional_cast
2508   (cp_parser *, tree);
2509 static tree cp_parser_save_member_function_body
2510   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2511 static tree cp_parser_save_nsdmi
2512   (cp_parser *);
2513 static tree cp_parser_enclosed_template_argument_list
2514   (cp_parser *);
2515 static void cp_parser_save_default_args
2516   (cp_parser *, tree);
2517 static void cp_parser_late_parsing_for_member
2518   (cp_parser *, tree);
2519 static tree cp_parser_late_parse_one_default_arg
2520   (cp_parser *, tree, tree, tree);
2521 static void cp_parser_late_parsing_nsdmi
2522   (cp_parser *, tree);
2523 static void cp_parser_late_parsing_default_args
2524   (cp_parser *, tree);
2525 static tree cp_parser_sizeof_operand
2526   (cp_parser *, enum rid);
2527 static tree cp_parser_trait_expr
2528   (cp_parser *, enum rid);
2529 static bool cp_parser_declares_only_class_p
2530   (cp_parser *);
2531 static void cp_parser_set_storage_class
2532   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2533 static void cp_parser_set_decl_spec_type
2534   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2535 static void set_and_check_decl_spec_loc
2536   (cp_decl_specifier_seq *decl_specs,
2537    cp_decl_spec ds, cp_token *);
2538 static bool cp_parser_friend_p
2539   (const cp_decl_specifier_seq *);
2540 static void cp_parser_required_error
2541   (cp_parser *, required_token, bool);
2542 static cp_token *cp_parser_require
2543   (cp_parser *, enum cpp_ttype, required_token);
2544 static cp_token *cp_parser_require_keyword
2545   (cp_parser *, enum rid, required_token);
2546 static bool cp_parser_token_starts_function_definition_p
2547   (cp_token *);
2548 static bool cp_parser_next_token_starts_class_definition_p
2549   (cp_parser *);
2550 static bool cp_parser_next_token_ends_template_argument_p
2551   (cp_parser *);
2552 static bool cp_parser_nth_token_starts_template_argument_list_p
2553   (cp_parser *, size_t);
2554 static enum tag_types cp_parser_token_is_class_key
2555   (cp_token *);
2556 static enum tag_types cp_parser_token_is_type_parameter_key
2557   (cp_token *);
2558 static void cp_parser_check_class_key
2559   (enum tag_types, tree type);
2560 static void cp_parser_check_access_in_redeclaration
2561   (tree type, location_t location);
2562 static bool cp_parser_optional_template_keyword
2563   (cp_parser *);
2564 static void cp_parser_pre_parsed_nested_name_specifier
2565   (cp_parser *);
2566 static bool cp_parser_cache_group
2567   (cp_parser *, enum cpp_ttype, unsigned);
2568 static tree cp_parser_cache_defarg
2569   (cp_parser *parser, bool nsdmi);
2570 static void cp_parser_parse_tentatively
2571   (cp_parser *);
2572 static void cp_parser_commit_to_tentative_parse
2573   (cp_parser *);
2574 static void cp_parser_commit_to_topmost_tentative_parse
2575   (cp_parser *);
2576 static void cp_parser_abort_tentative_parse
2577   (cp_parser *);
2578 static bool cp_parser_parse_definitely
2579   (cp_parser *);
2580 static inline bool cp_parser_parsing_tentatively
2581   (cp_parser *);
2582 static bool cp_parser_uncommitted_to_tentative_parse_p
2583   (cp_parser *);
2584 static void cp_parser_error
2585   (cp_parser *, const char *);
2586 static void cp_parser_name_lookup_error
2587   (cp_parser *, tree, tree, name_lookup_error, location_t);
2588 static bool cp_parser_simulate_error
2589   (cp_parser *);
2590 static bool cp_parser_check_type_definition
2591   (cp_parser *);
2592 static void cp_parser_check_for_definition_in_return_type
2593   (cp_declarator *, tree, location_t type_location);
2594 static void cp_parser_check_for_invalid_template_id
2595   (cp_parser *, tree, enum tag_types, location_t location);
2596 static bool cp_parser_non_integral_constant_expression
2597   (cp_parser *, non_integral_constant);
2598 static void cp_parser_diagnose_invalid_type_name
2599   (cp_parser *, tree, location_t);
2600 static bool cp_parser_parse_and_diagnose_invalid_type_name
2601   (cp_parser *);
2602 static int cp_parser_skip_to_closing_parenthesis
2603   (cp_parser *, bool, bool, bool);
2604 static void cp_parser_skip_to_end_of_statement
2605   (cp_parser *);
2606 static void cp_parser_consume_semicolon_at_end_of_statement
2607   (cp_parser *);
2608 static void cp_parser_skip_to_end_of_block_or_statement
2609   (cp_parser *);
2610 static bool cp_parser_skip_to_closing_brace
2611   (cp_parser *);
2612 static void cp_parser_skip_to_end_of_template_parameter_list
2613   (cp_parser *);
2614 static void cp_parser_skip_to_pragma_eol
2615   (cp_parser*, cp_token *);
2616 static bool cp_parser_error_occurred
2617   (cp_parser *);
2618 static bool cp_parser_allow_gnu_extensions_p
2619   (cp_parser *);
2620 static bool cp_parser_is_pure_string_literal
2621   (cp_token *);
2622 static bool cp_parser_is_string_literal
2623   (cp_token *);
2624 static bool cp_parser_is_keyword
2625   (cp_token *, enum rid);
2626 static tree cp_parser_make_typename_type
2627   (cp_parser *, tree, location_t location);
2628 static cp_declarator * cp_parser_make_indirect_declarator
2629   (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2630 static bool cp_parser_compound_literal_p
2631   (cp_parser *);
2632 static bool cp_parser_array_designator_p
2633   (cp_parser *);
2634 static bool cp_parser_skip_to_closing_square_bracket
2635   (cp_parser *);
2636
2637 /* Concept-related syntactic transformations */
2638
2639 static tree cp_parser_maybe_concept_name       (cp_parser *, tree);
2640 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2641
2642 // -------------------------------------------------------------------------- //
2643 // Unevaluated Operand Guard
2644 //
2645 // Implementation of an RAII helper for unevaluated operand parsing.
2646 cp_unevaluated::cp_unevaluated ()
2647 {
2648   ++cp_unevaluated_operand;
2649   ++c_inhibit_evaluation_warnings;
2650 }
2651
2652 cp_unevaluated::~cp_unevaluated ()
2653 {
2654   --c_inhibit_evaluation_warnings;
2655   --cp_unevaluated_operand;
2656 }
2657
2658 // -------------------------------------------------------------------------- //
2659 // Tentative Parsing
2660
2661 /* Returns nonzero if we are parsing tentatively.  */
2662
2663 static inline bool
2664 cp_parser_parsing_tentatively (cp_parser* parser)
2665 {
2666   return parser->context->next != NULL;
2667 }
2668
2669 /* Returns nonzero if TOKEN is a string literal.  */
2670
2671 static bool
2672 cp_parser_is_pure_string_literal (cp_token* token)
2673 {
2674   return (token->type == CPP_STRING ||
2675           token->type == CPP_STRING16 ||
2676           token->type == CPP_STRING32 ||
2677           token->type == CPP_WSTRING ||
2678           token->type == CPP_UTF8STRING);
2679 }
2680
2681 /* Returns nonzero if TOKEN is a string literal
2682    of a user-defined string literal.  */
2683
2684 static bool
2685 cp_parser_is_string_literal (cp_token* token)
2686 {
2687   return (cp_parser_is_pure_string_literal (token) ||
2688           token->type == CPP_STRING_USERDEF ||
2689           token->type == CPP_STRING16_USERDEF ||
2690           token->type == CPP_STRING32_USERDEF ||
2691           token->type == CPP_WSTRING_USERDEF ||
2692           token->type == CPP_UTF8STRING_USERDEF);
2693 }
2694
2695 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2696
2697 static bool
2698 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2699 {
2700   return token->keyword == keyword;
2701 }
2702
2703 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2704    PRAGMA_NONE.  */
2705
2706 static enum pragma_kind
2707 cp_parser_pragma_kind (cp_token *token)
2708 {
2709   if (token->type != CPP_PRAGMA)
2710     return PRAGMA_NONE;
2711   /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
2712   return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2713 }
2714
2715 /* Helper function for cp_parser_error.
2716    Having peeked a token of kind TOK1_KIND that might signify
2717    a conflict marker, peek successor tokens to determine
2718    if we actually do have a conflict marker.
2719    Specifically, we consider a run of 7 '<', '=' or '>' characters
2720    at the start of a line as a conflict marker.
2721    These come through the lexer as three pairs and a single,
2722    e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2723    If it returns true, *OUT_LOC is written to with the location/range
2724    of the marker.  */
2725
2726 static bool
2727 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2728                                location_t *out_loc)
2729 {
2730   cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2731   if (token2->type != tok1_kind)
2732     return false;
2733   cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2734   if (token3->type != tok1_kind)
2735     return false;
2736   cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2737   if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2738     return false;
2739
2740   /* It must be at the start of the line.  */
2741   location_t start_loc = cp_lexer_peek_token (lexer)->location;
2742   if (LOCATION_COLUMN (start_loc) != 1)
2743     return false;
2744
2745   /* We have a conflict marker.  Construct a location of the form:
2746        <<<<<<<
2747        ^~~~~~~
2748      with start == caret, finishing at the end of the marker.  */
2749   location_t finish_loc = get_finish (token4->location);
2750   *out_loc = make_location (start_loc, start_loc, finish_loc);
2751
2752   return true;
2753 }
2754
2755 /* If not parsing tentatively, issue a diagnostic of the form
2756       FILE:LINE: MESSAGE before TOKEN
2757    where TOKEN is the next token in the input stream.  MESSAGE
2758    (specified by the caller) is usually of the form "expected
2759    OTHER-TOKEN".  */
2760
2761 static void
2762 cp_parser_error (cp_parser* parser, const char* gmsgid)
2763 {
2764   if (!cp_parser_simulate_error (parser))
2765     {
2766       cp_token *token = cp_lexer_peek_token (parser->lexer);
2767       /* This diagnostic makes more sense if it is tagged to the line
2768          of the token we just peeked at.  */
2769       cp_lexer_set_source_position_from_token (token);
2770
2771       if (token->type == CPP_PRAGMA)
2772         {
2773           error_at (token->location,
2774                     "%<#pragma%> is not allowed here");
2775           cp_parser_skip_to_pragma_eol (parser, token);
2776           return;
2777         }
2778
2779       /* If this is actually a conflict marker, report it as such.  */
2780       if (token->type == CPP_LSHIFT
2781           || token->type == CPP_RSHIFT
2782           || token->type == CPP_EQ_EQ)
2783         {
2784           location_t loc;
2785           if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2786             {
2787               error_at (loc, "version control conflict marker in file");
2788               return;
2789             }
2790         }
2791
2792       c_parse_error (gmsgid,
2793                      /* Because c_parser_error does not understand
2794                         CPP_KEYWORD, keywords are treated like
2795                         identifiers.  */
2796                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2797                      token->u.value, token->flags);
2798     }
2799 }
2800
2801 /* Issue an error about name-lookup failing.  NAME is the
2802    IDENTIFIER_NODE DECL is the result of
2803    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2804    the thing that we hoped to find.  */
2805
2806 static void
2807 cp_parser_name_lookup_error (cp_parser* parser,
2808                              tree name,
2809                              tree decl,
2810                              name_lookup_error desired,
2811                              location_t location)
2812 {
2813   /* If name lookup completely failed, tell the user that NAME was not
2814      declared.  */
2815   if (decl == error_mark_node)
2816     {
2817       if (parser->scope && parser->scope != global_namespace)
2818         error_at (location, "%<%E::%E%> has not been declared",
2819                   parser->scope, name);
2820       else if (parser->scope == global_namespace)
2821         error_at (location, "%<::%E%> has not been declared", name);
2822       else if (parser->object_scope
2823                && !CLASS_TYPE_P (parser->object_scope))
2824         error_at (location, "request for member %qE in non-class type %qT",
2825                   name, parser->object_scope);
2826       else if (parser->object_scope)
2827         error_at (location, "%<%T::%E%> has not been declared",
2828                   parser->object_scope, name);
2829       else
2830         error_at (location, "%qE has not been declared", name);
2831     }
2832   else if (parser->scope && parser->scope != global_namespace)
2833     {
2834       switch (desired)
2835         {
2836           case NLE_TYPE:
2837             error_at (location, "%<%E::%E%> is not a type",
2838                                 parser->scope, name);
2839             break;
2840           case NLE_CXX98:
2841             error_at (location, "%<%E::%E%> is not a class or namespace",
2842                                 parser->scope, name);
2843             break;
2844           case NLE_NOT_CXX98:
2845             error_at (location,
2846                       "%<%E::%E%> is not a class, namespace, or enumeration",
2847                       parser->scope, name);
2848             break;
2849           default:
2850             gcc_unreachable ();
2851             
2852         }
2853     }
2854   else if (parser->scope == global_namespace)
2855     {
2856       switch (desired)
2857         {
2858           case NLE_TYPE:
2859             error_at (location, "%<::%E%> is not a type", name);
2860             break;
2861           case NLE_CXX98:
2862             error_at (location, "%<::%E%> is not a class or namespace", name);
2863             break;
2864           case NLE_NOT_CXX98:
2865             error_at (location,
2866                       "%<::%E%> is not a class, namespace, or enumeration",
2867                       name);
2868             break;
2869           default:
2870             gcc_unreachable ();
2871         }
2872     }
2873   else
2874     {
2875       switch (desired)
2876         {
2877           case NLE_TYPE:
2878             error_at (location, "%qE is not a type", name);
2879             break;
2880           case NLE_CXX98:
2881             error_at (location, "%qE is not a class or namespace", name);
2882             break;
2883           case NLE_NOT_CXX98:
2884             error_at (location,
2885                       "%qE is not a class, namespace, or enumeration", name);
2886             break;
2887           default:
2888             gcc_unreachable ();
2889         }
2890     }
2891 }
2892
2893 /* If we are parsing tentatively, remember that an error has occurred
2894    during this tentative parse.  Returns true if the error was
2895    simulated; false if a message should be issued by the caller.  */
2896
2897 static bool
2898 cp_parser_simulate_error (cp_parser* parser)
2899 {
2900   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2901     {
2902       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2903       return true;
2904     }
2905   return false;
2906 }
2907
2908 /* This function is called when a type is defined.  If type
2909    definitions are forbidden at this point, an error message is
2910    issued.  */
2911
2912 static bool
2913 cp_parser_check_type_definition (cp_parser* parser)
2914 {
2915   /* If types are forbidden here, issue a message.  */
2916   if (parser->type_definition_forbidden_message)
2917     {
2918       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2919          in the message need to be interpreted.  */
2920       error (parser->type_definition_forbidden_message);
2921       return false;
2922     }
2923   return true;
2924 }
2925
2926 /* This function is called when the DECLARATOR is processed.  The TYPE
2927    was a type defined in the decl-specifiers.  If it is invalid to
2928    define a type in the decl-specifiers for DECLARATOR, an error is
2929    issued. TYPE_LOCATION is the location of TYPE and is used
2930    for error reporting.  */
2931
2932 static void
2933 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2934                                                tree type, location_t type_location)
2935 {
2936   /* [dcl.fct] forbids type definitions in return types.
2937      Unfortunately, it's not easy to know whether or not we are
2938      processing a return type until after the fact.  */
2939   while (declarator
2940          && (declarator->kind == cdk_pointer
2941              || declarator->kind == cdk_reference
2942              || declarator->kind == cdk_ptrmem))
2943     declarator = declarator->declarator;
2944   if (declarator
2945       && declarator->kind == cdk_function)
2946     {
2947       error_at (type_location,
2948                 "new types may not be defined in a return type");
2949       inform (type_location, 
2950               "(perhaps a semicolon is missing after the definition of %qT)",
2951               type);
2952     }
2953 }
2954
2955 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2956    "<" in any valid C++ program.  If the next token is indeed "<",
2957    issue a message warning the user about what appears to be an
2958    invalid attempt to form a template-id. LOCATION is the location
2959    of the type-specifier (TYPE) */
2960
2961 static void
2962 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2963                                          tree type,
2964                                          enum tag_types tag_type,
2965                                          location_t location)
2966 {
2967   cp_token_position start = 0;
2968
2969   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2970     {
2971       if (TYPE_P (type))
2972         error_at (location, "%qT is not a template", type);
2973       else if (identifier_p (type))
2974         {
2975           if (tag_type != none_type)
2976             error_at (location, "%qE is not a class template", type);
2977           else
2978             error_at (location, "%qE is not a template", type);
2979         }
2980       else
2981         error_at (location, "invalid template-id");
2982       /* Remember the location of the invalid "<".  */
2983       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2984         start = cp_lexer_token_position (parser->lexer, true);
2985       /* Consume the "<".  */
2986       cp_lexer_consume_token (parser->lexer);
2987       /* Parse the template arguments.  */
2988       cp_parser_enclosed_template_argument_list (parser);
2989       /* Permanently remove the invalid template arguments so that
2990          this error message is not issued again.  */
2991       if (start)
2992         cp_lexer_purge_tokens_after (parser->lexer, start);
2993     }
2994 }
2995
2996 /* If parsing an integral constant-expression, issue an error message
2997    about the fact that THING appeared and return true.  Otherwise,
2998    return false.  In either case, set
2999    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
3000
3001 static bool
3002 cp_parser_non_integral_constant_expression (cp_parser  *parser,
3003                                             non_integral_constant thing)
3004 {
3005   parser->non_integral_constant_expression_p = true;
3006   if (parser->integral_constant_expression_p)
3007     {
3008       if (!parser->allow_non_integral_constant_expression_p)
3009         {
3010           const char *msg = NULL;
3011           switch (thing)
3012             {
3013               case NIC_FLOAT:
3014                 error ("floating-point literal "
3015                        "cannot appear in a constant-expression");
3016                 return true;
3017               case NIC_CAST:
3018                 error ("a cast to a type other than an integral or "
3019                        "enumeration type cannot appear in a "
3020                        "constant-expression");
3021                 return true;
3022               case NIC_TYPEID:
3023                 error ("%<typeid%> operator "
3024                        "cannot appear in a constant-expression");
3025                 return true;
3026               case NIC_NCC:
3027                 error ("non-constant compound literals "
3028                        "cannot appear in a constant-expression");
3029                 return true;
3030               case NIC_FUNC_CALL:
3031                 error ("a function call "
3032                        "cannot appear in a constant-expression");
3033                 return true;
3034               case NIC_INC:
3035                 error ("an increment "
3036                        "cannot appear in a constant-expression");
3037                 return true;
3038               case NIC_DEC:
3039                 error ("an decrement "
3040                        "cannot appear in a constant-expression");
3041                 return true;
3042               case NIC_ARRAY_REF:
3043                 error ("an array reference "
3044                        "cannot appear in a constant-expression");
3045                 return true;
3046               case NIC_ADDR_LABEL:
3047                 error ("the address of a label "
3048                        "cannot appear in a constant-expression");
3049                 return true;
3050               case NIC_OVERLOADED:
3051                 error ("calls to overloaded operators "
3052                        "cannot appear in a constant-expression");
3053                 return true;
3054               case NIC_ASSIGNMENT:
3055                 error ("an assignment cannot appear in a constant-expression");
3056                 return true;
3057               case NIC_COMMA:
3058                 error ("a comma operator "
3059                        "cannot appear in a constant-expression");
3060                 return true;
3061               case NIC_CONSTRUCTOR:
3062                 error ("a call to a constructor "
3063                        "cannot appear in a constant-expression");
3064                 return true;
3065               case NIC_TRANSACTION:
3066                 error ("a transaction expression "
3067                        "cannot appear in a constant-expression");
3068                 return true;
3069               case NIC_THIS:
3070                 msg = "this";
3071                 break;
3072               case NIC_FUNC_NAME:
3073                 msg = "__FUNCTION__";
3074                 break;
3075               case NIC_PRETTY_FUNC:
3076                 msg = "__PRETTY_FUNCTION__";
3077                 break;
3078               case NIC_C99_FUNC:
3079                 msg = "__func__";
3080                 break;
3081               case NIC_VA_ARG:
3082                 msg = "va_arg";
3083                 break;
3084               case NIC_ARROW:
3085                 msg = "->";
3086                 break;
3087               case NIC_POINT:
3088                 msg = ".";
3089                 break;
3090               case NIC_STAR:
3091                 msg = "*";
3092                 break;
3093               case NIC_ADDR:
3094                 msg = "&";
3095                 break;
3096               case NIC_PREINCREMENT:
3097                 msg = "++";
3098                 break;
3099               case NIC_PREDECREMENT:
3100                 msg = "--";
3101                 break;
3102               case NIC_NEW:
3103                 msg = "new";
3104                 break;
3105               case NIC_DEL:
3106                 msg = "delete";
3107                 break;
3108               default:
3109                 gcc_unreachable ();
3110             }
3111           if (msg)
3112             error ("%qs cannot appear in a constant-expression", msg);
3113           return true;
3114         }
3115     }
3116   return false;
3117 }
3118
3119 /* Emit a diagnostic for an invalid type name.  This function commits
3120    to the current active tentative parse, if any.  (Otherwise, the
3121    problematic construct might be encountered again later, resulting
3122    in duplicate error messages.) LOCATION is the location of ID.  */
3123
3124 static void
3125 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3126                                       location_t location)
3127 {
3128   tree decl, ambiguous_decls;
3129   cp_parser_commit_to_tentative_parse (parser);
3130   /* Try to lookup the identifier.  */
3131   decl = cp_parser_lookup_name (parser, id, none_type,
3132                                 /*is_template=*/false,
3133                                 /*is_namespace=*/false,
3134                                 /*check_dependency=*/true,
3135                                 &ambiguous_decls, location);
3136   if (ambiguous_decls)
3137     /* If the lookup was ambiguous, an error will already have
3138        been issued.  */
3139     return;
3140   /* If the lookup found a template-name, it means that the user forgot
3141   to specify an argument list. Emit a useful error message.  */
3142   if (DECL_TYPE_TEMPLATE_P (decl))
3143     {
3144       error_at (location,
3145                 "invalid use of template-name %qE without an argument list",
3146                 decl);
3147       inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3148     }
3149   else if (TREE_CODE (id) == BIT_NOT_EXPR)
3150     error_at (location, "invalid use of destructor %qD as a type", id);
3151   else if (TREE_CODE (decl) == TYPE_DECL)
3152     /* Something like 'unsigned A a;'  */
3153     error_at (location, "invalid combination of multiple type-specifiers");
3154   else if (!parser->scope)
3155     {
3156       /* Issue an error message.  */
3157       error_at (location, "%qE does not name a type", id);
3158       /* If we're in a template class, it's possible that the user was
3159          referring to a type from a base class.  For example:
3160
3161            template <typename T> struct A { typedef T X; };
3162            template <typename T> struct B : public A<T> { X x; };
3163
3164          The user should have said "typename A<T>::X".  */
3165       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3166         inform (location, "C++11 %<constexpr%> only available with "
3167                 "-std=c++11 or -std=gnu++11");
3168       else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3169         inform (location, "C++11 %<noexcept%> only available with "
3170                 "-std=c++11 or -std=gnu++11");
3171       else if (cxx_dialect < cxx11
3172                && TREE_CODE (id) == IDENTIFIER_NODE
3173                && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3174         inform (location, "C++11 %<thread_local%> only available with "
3175                 "-std=c++11 or -std=gnu++11");
3176       else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3177         inform (location, "%<concept%> only available with -fconcepts");
3178       else if (processing_template_decl && current_class_type
3179                && TYPE_BINFO (current_class_type))
3180         {
3181           tree b;
3182
3183           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3184                b;
3185                b = TREE_CHAIN (b))
3186             {
3187               tree base_type = BINFO_TYPE (b);
3188               if (CLASS_TYPE_P (base_type)
3189                   && dependent_type_p (base_type))
3190                 {
3191                   tree field;
3192                   /* Go from a particular instantiation of the
3193                      template (which will have an empty TYPE_FIELDs),
3194                      to the main version.  */
3195                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3196                   for (field = TYPE_FIELDS (base_type);
3197                        field;
3198                        field = DECL_CHAIN (field))
3199                     if (TREE_CODE (field) == TYPE_DECL
3200                         && DECL_NAME (field) == id)
3201                       {
3202                         inform (location, 
3203                                 "(perhaps %<typename %T::%E%> was intended)",
3204                                 BINFO_TYPE (b), id);
3205                         break;
3206                       }
3207                   if (field)
3208                     break;
3209                 }
3210             }
3211         }
3212     }
3213   /* Here we diagnose qualified-ids where the scope is actually correct,
3214      but the identifier does not resolve to a valid type name.  */
3215   else if (parser->scope != error_mark_node)
3216     {
3217       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3218         {
3219           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3220             error_at (location_of (id),
3221                       "%qE in namespace %qE does not name a template type",
3222                       id, parser->scope);
3223           else
3224             error_at (location_of (id),
3225                       "%qE in namespace %qE does not name a type",
3226                       id, parser->scope);
3227           if (DECL_P (decl))
3228             inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3229         }
3230       else if (CLASS_TYPE_P (parser->scope)
3231                && constructor_name_p (id, parser->scope))
3232         {
3233           /* A<T>::A<T>() */
3234           error_at (location, "%<%T::%E%> names the constructor, not"
3235                     " the type", parser->scope, id);
3236           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3237             error_at (location, "and %qT has no template constructors",
3238                       parser->scope);
3239         }
3240       else if (TYPE_P (parser->scope)
3241                && dependent_scope_p (parser->scope))
3242         error_at (location, "need %<typename%> before %<%T::%E%> because "
3243                   "%qT is a dependent scope",
3244                   parser->scope, id, parser->scope);
3245       else if (TYPE_P (parser->scope))
3246         {
3247           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3248             error_at (location_of (id),
3249                       "%qE in %q#T does not name a template type",
3250                       id, parser->scope);
3251           else
3252             error_at (location_of (id),
3253                       "%qE in %q#T does not name a type",
3254                       id, parser->scope);
3255           if (DECL_P (decl))
3256             inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3257         }
3258       else
3259         gcc_unreachable ();
3260     }
3261 }
3262
3263 /* Check for a common situation where a type-name should be present,
3264    but is not, and issue a sensible error message.  Returns true if an
3265    invalid type-name was detected.
3266
3267    The situation handled by this function are variable declarations of the
3268    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3269    Usually, `ID' should name a type, but if we got here it means that it
3270    does not. We try to emit the best possible error message depending on
3271    how exactly the id-expression looks like.  */
3272
3273 static bool
3274 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3275 {
3276   tree id;
3277   cp_token *token = cp_lexer_peek_token (parser->lexer);
3278
3279   /* Avoid duplicate error about ambiguous lookup.  */
3280   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3281     {
3282       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3283       if (next->type == CPP_NAME && next->error_reported)
3284         goto out;
3285     }
3286
3287   cp_parser_parse_tentatively (parser);
3288   id = cp_parser_id_expression (parser,
3289                                 /*template_keyword_p=*/false,
3290                                 /*check_dependency_p=*/true,
3291                                 /*template_p=*/NULL,
3292                                 /*declarator_p=*/true,
3293                                 /*optional_p=*/false);
3294   /* If the next token is a (, this is a function with no explicit return
3295      type, i.e. constructor, destructor or conversion op.  */
3296   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3297       || TREE_CODE (id) == TYPE_DECL)
3298     {
3299       cp_parser_abort_tentative_parse (parser);
3300       return false;
3301     }
3302   if (!cp_parser_parse_definitely (parser))
3303     return false;
3304
3305   /* Emit a diagnostic for the invalid type.  */
3306   cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3307  out:
3308   /* If we aren't in the middle of a declarator (i.e. in a
3309      parameter-declaration-clause), skip to the end of the declaration;
3310      there's no point in trying to process it.  */
3311   if (!parser->in_declarator_p)
3312     cp_parser_skip_to_end_of_block_or_statement (parser);
3313   return true;
3314 }
3315
3316 /* Consume tokens up to, and including, the next non-nested closing `)'.
3317    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3318    are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3319    found an unnested token of that type.  */
3320
3321 static int
3322 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3323                                          bool recovering,
3324                                          cpp_ttype or_ttype,
3325                                          bool consume_paren)
3326 {
3327   unsigned paren_depth = 0;
3328   unsigned brace_depth = 0;
3329   unsigned square_depth = 0;
3330
3331   if (recovering && or_ttype == CPP_EOF
3332       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3333     return 0;
3334
3335   while (true)
3336     {
3337       cp_token * token = cp_lexer_peek_token (parser->lexer);
3338
3339       /* Have we found what we're looking for before the closing paren?  */
3340       if (token->type == or_ttype && or_ttype != CPP_EOF
3341           && !brace_depth && !paren_depth && !square_depth)
3342         return -1;
3343
3344       switch (token->type)
3345         {
3346         case CPP_EOF:
3347         case CPP_PRAGMA_EOL:
3348           /* If we've run out of tokens, then there is no closing `)'.  */
3349           return 0;
3350
3351         /* This is good for lambda expression capture-lists.  */
3352         case CPP_OPEN_SQUARE:
3353           ++square_depth;
3354           break;
3355         case CPP_CLOSE_SQUARE:
3356           if (!square_depth--)
3357             return 0;
3358           break;
3359
3360         case CPP_SEMICOLON:
3361           /* This matches the processing in skip_to_end_of_statement.  */
3362           if (!brace_depth)
3363             return 0;
3364           break;
3365
3366         case CPP_OPEN_BRACE:
3367           ++brace_depth;
3368           break;
3369         case CPP_CLOSE_BRACE:
3370           if (!brace_depth--)
3371             return 0;
3372           break;
3373
3374         case CPP_OPEN_PAREN:
3375           if (!brace_depth)
3376             ++paren_depth;
3377           break;
3378
3379         case CPP_CLOSE_PAREN:
3380           if (!brace_depth && !paren_depth--)
3381             {
3382               if (consume_paren)
3383                 cp_lexer_consume_token (parser->lexer);
3384               return 1;
3385             }
3386           break;
3387
3388         default:
3389           break;
3390         }
3391
3392       /* Consume the token.  */
3393       cp_lexer_consume_token (parser->lexer);
3394     }
3395 }
3396
3397 /* Consume tokens up to, and including, the next non-nested closing `)'.
3398    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3399    are doing error recovery. Returns -1 if OR_COMMA is true and we
3400    found an unnested token of that type.  */
3401
3402 static int
3403 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3404                                        bool recovering,
3405                                        bool or_comma,
3406                                        bool consume_paren)
3407 {
3408   cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3409   return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3410                                                   ttype, consume_paren);
3411 }
3412
3413 /* Consume tokens until we reach the end of the current statement.
3414    Normally, that will be just before consuming a `;'.  However, if a
3415    non-nested `}' comes first, then we stop before consuming that.  */
3416
3417 static void
3418 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3419 {
3420   unsigned nesting_depth = 0;
3421
3422   /* Unwind generic function template scope if necessary.  */
3423   if (parser->fully_implicit_function_template_p)
3424     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3425
3426   while (true)
3427     {
3428       cp_token *token = cp_lexer_peek_token (parser->lexer);
3429
3430       switch (token->type)
3431         {
3432         case CPP_EOF:
3433         case CPP_PRAGMA_EOL:
3434           /* If we've run out of tokens, stop.  */
3435           return;
3436
3437         case CPP_SEMICOLON:
3438           /* If the next token is a `;', we have reached the end of the
3439              statement.  */
3440           if (!nesting_depth)
3441             return;
3442           break;
3443
3444         case CPP_CLOSE_BRACE:
3445           /* If this is a non-nested '}', stop before consuming it.
3446              That way, when confronted with something like:
3447
3448                { 3 + }
3449
3450              we stop before consuming the closing '}', even though we
3451              have not yet reached a `;'.  */
3452           if (nesting_depth == 0)
3453             return;
3454
3455           /* If it is the closing '}' for a block that we have
3456              scanned, stop -- but only after consuming the token.
3457              That way given:
3458
3459                 void f g () { ... }
3460                 typedef int I;
3461
3462              we will stop after the body of the erroneously declared
3463              function, but before consuming the following `typedef'
3464              declaration.  */
3465           if (--nesting_depth == 0)
3466             {
3467               cp_lexer_consume_token (parser->lexer);
3468               return;
3469             }
3470
3471         case CPP_OPEN_BRACE:
3472           ++nesting_depth;
3473           break;
3474
3475         default:
3476           break;
3477         }
3478
3479       /* Consume the token.  */
3480       cp_lexer_consume_token (parser->lexer);
3481     }
3482 }
3483
3484 /* This function is called at the end of a statement or declaration.
3485    If the next token is a semicolon, it is consumed; otherwise, error
3486    recovery is attempted.  */
3487
3488 static void
3489 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3490 {
3491   /* Look for the trailing `;'.  */
3492   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3493     {
3494       /* If there is additional (erroneous) input, skip to the end of
3495          the statement.  */
3496       cp_parser_skip_to_end_of_statement (parser);
3497       /* If the next token is now a `;', consume it.  */
3498       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3499         cp_lexer_consume_token (parser->lexer);
3500     }
3501 }
3502
3503 /* Skip tokens until we have consumed an entire block, or until we
3504    have consumed a non-nested `;'.  */
3505
3506 static void
3507 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3508 {
3509   int nesting_depth = 0;
3510
3511   /* Unwind generic function template scope if necessary.  */
3512   if (parser->fully_implicit_function_template_p)
3513     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3514
3515   while (nesting_depth >= 0)
3516     {
3517       cp_token *token = cp_lexer_peek_token (parser->lexer);
3518
3519       switch (token->type)
3520         {
3521         case CPP_EOF:
3522         case CPP_PRAGMA_EOL:
3523           /* If we've run out of tokens, stop.  */
3524           return;
3525
3526         case CPP_SEMICOLON:
3527           /* Stop if this is an unnested ';'. */
3528           if (!nesting_depth)
3529             nesting_depth = -1;
3530           break;
3531
3532         case CPP_CLOSE_BRACE:
3533           /* Stop if this is an unnested '}', or closes the outermost
3534              nesting level.  */
3535           nesting_depth--;
3536           if (nesting_depth < 0)
3537             return;
3538           if (!nesting_depth)
3539             nesting_depth = -1;
3540           break;
3541
3542         case CPP_OPEN_BRACE:
3543           /* Nest. */
3544           nesting_depth++;
3545           break;
3546
3547         default:
3548           break;
3549         }
3550
3551       /* Consume the token.  */
3552       cp_lexer_consume_token (parser->lexer);
3553     }
3554 }
3555
3556 /* Skip tokens until a non-nested closing curly brace is the next
3557    token, or there are no more tokens. Return true in the first case,
3558    false otherwise.  */
3559
3560 static bool
3561 cp_parser_skip_to_closing_brace (cp_parser *parser)
3562 {
3563   unsigned nesting_depth = 0;
3564
3565   while (true)
3566     {
3567       cp_token *token = cp_lexer_peek_token (parser->lexer);
3568
3569       switch (token->type)
3570         {
3571         case CPP_EOF:
3572         case CPP_PRAGMA_EOL:
3573           /* If we've run out of tokens, stop.  */
3574           return false;
3575
3576         case CPP_CLOSE_BRACE:
3577           /* If the next token is a non-nested `}', then we have reached
3578              the end of the current block.  */
3579           if (nesting_depth-- == 0)
3580             return true;
3581           break;
3582
3583         case CPP_OPEN_BRACE:
3584           /* If it the next token is a `{', then we are entering a new
3585              block.  Consume the entire block.  */
3586           ++nesting_depth;
3587           break;
3588
3589         default:
3590           break;
3591         }
3592
3593       /* Consume the token.  */
3594       cp_lexer_consume_token (parser->lexer);
3595     }
3596 }
3597
3598 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3599    parameter is the PRAGMA token, allowing us to purge the entire pragma
3600    sequence.  */
3601
3602 static void
3603 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3604 {
3605   cp_token *token;
3606
3607   parser->lexer->in_pragma = false;
3608
3609   do
3610     token = cp_lexer_consume_token (parser->lexer);
3611   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3612
3613   /* Ensure that the pragma is not parsed again.  */
3614   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3615 }
3616
3617 /* Require pragma end of line, resyncing with it as necessary.  The
3618    arguments are as for cp_parser_skip_to_pragma_eol.  */
3619
3620 static void
3621 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3622 {
3623   parser->lexer->in_pragma = false;
3624   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3625     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3626 }
3627
3628 /* This is a simple wrapper around make_typename_type. When the id is
3629    an unresolved identifier node, we can provide a superior diagnostic
3630    using cp_parser_diagnose_invalid_type_name.  */
3631
3632 static tree
3633 cp_parser_make_typename_type (cp_parser *parser, tree id,
3634                               location_t id_location)
3635 {
3636   tree result;
3637   if (identifier_p (id))
3638     {
3639       result = make_typename_type (parser->scope, id, typename_type,
3640                                    /*complain=*/tf_none);
3641       if (result == error_mark_node)
3642         cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3643       return result;
3644     }
3645   return make_typename_type (parser->scope, id, typename_type, tf_error);
3646 }
3647
3648 /* This is a wrapper around the
3649    make_{pointer,ptrmem,reference}_declarator functions that decides
3650    which one to call based on the CODE and CLASS_TYPE arguments. The
3651    CODE argument should be one of the values returned by
3652    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3653    appertain to the pointer or reference.  */
3654
3655 static cp_declarator *
3656 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3657                                     cp_cv_quals cv_qualifiers,
3658                                     cp_declarator *target,
3659                                     tree attributes)
3660 {
3661   if (code == ERROR_MARK)
3662     return cp_error_declarator;
3663
3664   if (code == INDIRECT_REF)
3665     if (class_type == NULL_TREE)
3666       return make_pointer_declarator (cv_qualifiers, target, attributes);
3667     else
3668       return make_ptrmem_declarator (cv_qualifiers, class_type,
3669                                      target, attributes);
3670   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3671     return make_reference_declarator (cv_qualifiers, target,
3672                                       false, attributes);
3673   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3674     return make_reference_declarator (cv_qualifiers, target,
3675                                       true, attributes);
3676   gcc_unreachable ();
3677 }
3678
3679 /* Create a new C++ parser.  */
3680
3681 static cp_parser *
3682 cp_parser_new (void)
3683 {
3684   cp_parser *parser;
3685   cp_lexer *lexer;
3686   unsigned i;
3687
3688   /* cp_lexer_new_main is called before doing GC allocation because
3689      cp_lexer_new_main might load a PCH file.  */
3690   lexer = cp_lexer_new_main ();
3691
3692   /* Initialize the binops_by_token so that we can get the tree
3693      directly from the token.  */
3694   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3695     binops_by_token[binops[i].token_type] = binops[i];
3696
3697   parser = ggc_cleared_alloc<cp_parser> ();
3698   parser->lexer = lexer;
3699   parser->context = cp_parser_context_new (NULL);
3700
3701   /* For now, we always accept GNU extensions.  */
3702   parser->allow_gnu_extensions_p = 1;
3703
3704   /* The `>' token is a greater-than operator, not the end of a
3705      template-id.  */
3706   parser->greater_than_is_operator_p = true;
3707
3708   parser->default_arg_ok_p = true;
3709
3710   /* We are not parsing a constant-expression.  */
3711   parser->integral_constant_expression_p = false;
3712   parser->allow_non_integral_constant_expression_p = false;
3713   parser->non_integral_constant_expression_p = false;
3714
3715   /* Local variable names are not forbidden.  */
3716   parser->local_variables_forbidden_p = false;
3717
3718   /* We are not processing an `extern "C"' declaration.  */
3719   parser->in_unbraced_linkage_specification_p = false;
3720
3721   /* We are not processing a declarator.  */
3722   parser->in_declarator_p = false;
3723
3724   /* We are not processing a template-argument-list.  */
3725   parser->in_template_argument_list_p = false;
3726
3727   /* We are not in an iteration statement.  */
3728   parser->in_statement = 0;
3729
3730   /* We are not in a switch statement.  */
3731   parser->in_switch_statement_p = false;
3732
3733   /* We are not parsing a type-id inside an expression.  */
3734   parser->in_type_id_in_expr_p = false;
3735
3736   /* Declarations aren't implicitly extern "C".  */
3737   parser->implicit_extern_c = false;
3738
3739   /* String literals should be translated to the execution character set.  */
3740   parser->translate_strings_p = true;
3741
3742   /* We are not parsing a function body.  */
3743   parser->in_function_body = false;
3744
3745   /* We can correct until told otherwise.  */
3746   parser->colon_corrects_to_scope_p = true;
3747
3748   /* The unparsed function queue is empty.  */
3749   push_unparsed_function_queues (parser);
3750
3751   /* There are no classes being defined.  */
3752   parser->num_classes_being_defined = 0;
3753
3754   /* No template parameters apply.  */
3755   parser->num_template_parameter_lists = 0;
3756
3757   /* Not declaring an implicit function template.  */
3758   parser->auto_is_implicit_function_template_parm_p = false;
3759   parser->fully_implicit_function_template_p = false;
3760   parser->implicit_template_parms = 0;
3761   parser->implicit_template_scope = 0;
3762
3763   /* Active OpenACC routine clauses.  */
3764   parser->oacc_routine = NULL;
3765
3766   /* Allow constrained-type-specifiers. */
3767   parser->prevent_constrained_type_specifiers = 0;
3768
3769   return parser;
3770 }
3771
3772 /* Create a cp_lexer structure which will emit the tokens in CACHE
3773    and push it onto the parser's lexer stack.  This is used for delayed
3774    parsing of in-class method bodies and default arguments, and should
3775    not be confused with tentative parsing.  */
3776 static void
3777 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3778 {
3779   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3780   lexer->next = parser->lexer;
3781   parser->lexer = lexer;
3782
3783   /* Move the current source position to that of the first token in the
3784      new lexer.  */
3785   cp_lexer_set_source_position_from_token (lexer->next_token);
3786 }
3787
3788 /* Pop the top lexer off the parser stack.  This is never used for the
3789    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3790 static void
3791 cp_parser_pop_lexer (cp_parser *parser)
3792 {
3793   cp_lexer *lexer = parser->lexer;
3794   parser->lexer = lexer->next;
3795   cp_lexer_destroy (lexer);
3796
3797   /* Put the current source position back where it was before this
3798      lexer was pushed.  */
3799   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3800 }
3801
3802 /* Lexical conventions [gram.lex]  */
3803
3804 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3805    identifier.  */
3806
3807 static cp_expr
3808 cp_parser_identifier (cp_parser* parser)
3809 {
3810   cp_token *token;
3811
3812   /* Look for the identifier.  */
3813   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3814   /* Return the value.  */
3815   if (token)
3816     return cp_expr (token->u.value, token->location);
3817   else
3818     return error_mark_node;
3819 }
3820
3821 /* Parse a sequence of adjacent string constants.  Returns a
3822    TREE_STRING representing the combined, nul-terminated string
3823    constant.  If TRANSLATE is true, translate the string to the
3824    execution character set.  If WIDE_OK is true, a wide string is
3825    invalid here.
3826
3827    C++98 [lex.string] says that if a narrow string literal token is
3828    adjacent to a wide string literal token, the behavior is undefined.
3829    However, C99 6.4.5p4 says that this results in a wide string literal.
3830    We follow C99 here, for consistency with the C front end.
3831
3832    This code is largely lifted from lex_string() in c-lex.c.
3833
3834    FUTURE: ObjC++ will need to handle @-strings here.  */
3835 static cp_expr
3836 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3837                           bool lookup_udlit = true)
3838 {
3839   tree value;
3840   size_t count;
3841   struct obstack str_ob;
3842   cpp_string str, istr, *strs;
3843   cp_token *tok;
3844   enum cpp_ttype type, curr_type;
3845   int have_suffix_p = 0;
3846   tree string_tree;
3847   tree suffix_id = NULL_TREE;
3848   bool curr_tok_is_userdef_p = false;
3849
3850   tok = cp_lexer_peek_token (parser->lexer);
3851   if (!cp_parser_is_string_literal (tok))
3852     {
3853       cp_parser_error (parser, "expected string-literal");
3854       return error_mark_node;
3855     }
3856
3857   location_t loc = tok->location;
3858
3859   if (cpp_userdef_string_p (tok->type))
3860     {
3861       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3862       curr_type = cpp_userdef_string_remove_type (tok->type);
3863       curr_tok_is_userdef_p = true;
3864     }
3865   else
3866     {
3867       string_tree = tok->u.value;
3868       curr_type = tok->type;
3869     }
3870   type = curr_type;
3871
3872   /* Try to avoid the overhead of creating and destroying an obstack
3873      for the common case of just one string.  */
3874   if (!cp_parser_is_string_literal
3875       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3876     {
3877       cp_lexer_consume_token (parser->lexer);
3878
3879       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3880       str.len = TREE_STRING_LENGTH (string_tree);
3881       count = 1;
3882
3883       if (curr_tok_is_userdef_p)
3884         {
3885           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3886           have_suffix_p = 1;
3887           curr_type = cpp_userdef_string_remove_type (tok->type);
3888         }
3889       else
3890         curr_type = tok->type;
3891
3892       strs = &str;
3893     }
3894   else
3895     {
3896       location_t last_tok_loc;
3897       gcc_obstack_init (&str_ob);
3898       count = 0;
3899
3900       do
3901         {
3902           last_tok_loc = tok->location;
3903           cp_lexer_consume_token (parser->lexer);
3904           count++;
3905           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3906           str.len = TREE_STRING_LENGTH (string_tree);
3907
3908           if (curr_tok_is_userdef_p)
3909             {
3910               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3911               if (have_suffix_p == 0)
3912                 {
3913                   suffix_id = curr_suffix_id;
3914                   have_suffix_p = 1;
3915                 }
3916               else if (have_suffix_p == 1
3917                        && curr_suffix_id != suffix_id)
3918                 {
3919                   error ("inconsistent user-defined literal suffixes"
3920                          " %qD and %qD in string literal",
3921                          suffix_id, curr_suffix_id);
3922                   have_suffix_p = -1;
3923                 }
3924               curr_type = cpp_userdef_string_remove_type (tok->type);
3925             }
3926           else
3927             curr_type = tok->type;
3928
3929           if (type != curr_type)
3930             {
3931               if (type == CPP_STRING)
3932                 type = curr_type;
3933               else if (curr_type != CPP_STRING)
3934                 error_at (tok->location,
3935                           "unsupported non-standard concatenation "
3936                           "of string literals");
3937             }
3938
3939           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3940
3941           tok = cp_lexer_peek_token (parser->lexer);
3942           if (cpp_userdef_string_p (tok->type))
3943             {
3944               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3945               curr_type = cpp_userdef_string_remove_type (tok->type);
3946               curr_tok_is_userdef_p = true;
3947             }
3948           else
3949             {
3950               string_tree = tok->u.value;
3951               curr_type = tok->type;
3952               curr_tok_is_userdef_p = false;
3953             }
3954         }
3955       while (cp_parser_is_string_literal (tok));
3956
3957       /* A string literal built by concatenation has its caret=start at
3958          the start of the initial string, and its finish at the finish of
3959          the final string literal.  */
3960       loc = make_location (loc, loc, get_finish (last_tok_loc));
3961
3962       strs = (cpp_string *) obstack_finish (&str_ob);
3963     }
3964
3965   if (type != CPP_STRING && !wide_ok)
3966     {
3967       cp_parser_error (parser, "a wide string is invalid in this context");
3968       type = CPP_STRING;
3969     }
3970
3971   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3972       (parse_in, strs, count, &istr, type))
3973     {
3974       value = build_string (istr.len, (const char *)istr.text);
3975       free (CONST_CAST (unsigned char *, istr.text));
3976
3977       switch (type)
3978         {
3979         default:
3980         case CPP_STRING:
3981         case CPP_UTF8STRING:
3982           TREE_TYPE (value) = char_array_type_node;
3983           break;
3984         case CPP_STRING16:
3985           TREE_TYPE (value) = char16_array_type_node;
3986           break;
3987         case CPP_STRING32:
3988           TREE_TYPE (value) = char32_array_type_node;
3989           break;
3990         case CPP_WSTRING:
3991           TREE_TYPE (value) = wchar_array_type_node;
3992           break;
3993         }
3994
3995       value = fix_string_type (value);
3996
3997       if (have_suffix_p)
3998         {
3999           tree literal = build_userdef_literal (suffix_id, value,
4000                                                 OT_NONE, NULL_TREE);
4001           if (lookup_udlit)
4002             value = cp_parser_userdef_string_literal (literal);
4003           else
4004             value = literal;
4005         }
4006     }
4007   else
4008     /* cpp_interpret_string has issued an error.  */
4009     value = error_mark_node;
4010
4011   if (count > 1)
4012     obstack_free (&str_ob, 0);
4013
4014   return cp_expr (value, loc);
4015 }
4016
4017 /* Look up a literal operator with the name and the exact arguments.  */
4018
4019 static tree
4020 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4021 {
4022   tree decl, fns;
4023   decl = lookup_name (name);
4024   if (!decl || !is_overloaded_fn (decl))
4025     return error_mark_node;
4026
4027   for (fns = decl; fns; fns = OVL_NEXT (fns))
4028     {
4029       unsigned int ix;
4030       bool found = true;
4031       tree fn = OVL_CURRENT (fns);
4032       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4033       if (parmtypes != NULL_TREE)
4034         {
4035           for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4036                ++ix, parmtypes = TREE_CHAIN (parmtypes))
4037             {
4038               tree tparm = TREE_VALUE (parmtypes);
4039               tree targ = TREE_TYPE ((*args)[ix]);
4040               bool ptr = TYPE_PTR_P (tparm);
4041               bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4042               if ((ptr || arr || !same_type_p (tparm, targ))
4043                   && (!ptr || !arr
4044                       || !same_type_p (TREE_TYPE (tparm),
4045                                        TREE_TYPE (targ))))
4046                 found = false;
4047             }
4048           if (found
4049               && ix == vec_safe_length (args)
4050               /* May be this should be sufficient_parms_p instead,
4051                  depending on how exactly should user-defined literals
4052                  work in presence of default arguments on the literal
4053                  operator parameters.  */
4054               && parmtypes == void_list_node)
4055             return decl;
4056         }
4057     }
4058
4059   return error_mark_node;
4060 }
4061
4062 /* Parse a user-defined char constant.  Returns a call to a user-defined
4063    literal operator taking the character as an argument.  */
4064
4065 static cp_expr
4066 cp_parser_userdef_char_literal (cp_parser *parser)
4067 {
4068   cp_token *token = cp_lexer_consume_token (parser->lexer);
4069   tree literal = token->u.value;
4070   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4071   tree value = USERDEF_LITERAL_VALUE (literal);
4072   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4073   tree decl, result;
4074
4075   /* Build up a call to the user-defined operator  */
4076   /* Lookup the name we got back from the id-expression.  */
4077   vec<tree, va_gc> *args = make_tree_vector ();
4078   vec_safe_push (args, value);
4079   decl = lookup_literal_operator (name, args);
4080   if (!decl || decl == error_mark_node)
4081     {
4082       error ("unable to find character literal operator %qD with %qT argument",
4083              name, TREE_TYPE (value));
4084       release_tree_vector (args);
4085       return error_mark_node;
4086     }
4087   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4088   release_tree_vector (args);
4089   return result;
4090 }
4091
4092 /* A subroutine of cp_parser_userdef_numeric_literal to
4093    create a char... template parameter pack from a string node.  */
4094
4095 static tree
4096 make_char_string_pack (tree value)
4097 {
4098   tree charvec;
4099   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4100   const char *str = TREE_STRING_POINTER (value);
4101   int i, len = TREE_STRING_LENGTH (value) - 1;
4102   tree argvec = make_tree_vec (1);
4103
4104   /* Fill in CHARVEC with all of the parameters.  */
4105   charvec = make_tree_vec (len);
4106   for (i = 0; i < len; ++i)
4107     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4108
4109   /* Build the argument packs.  */
4110   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4111   TREE_TYPE (argpack) = char_type_node;
4112
4113   TREE_VEC_ELT (argvec, 0) = argpack;
4114
4115   return argvec;
4116 }
4117
4118 /* A subroutine of cp_parser_userdef_numeric_literal to
4119    create a char... template parameter pack from a string node.  */
4120
4121 static tree
4122 make_string_pack (tree value)
4123 {
4124   tree charvec;
4125   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4126   const unsigned char *str
4127     = (const unsigned char *) TREE_STRING_POINTER (value);
4128   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4129   int len = TREE_STRING_LENGTH (value) / sz - 1;
4130   tree argvec = make_tree_vec (2);
4131
4132   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4133   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4134
4135   /* First template parm is character type.  */
4136   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4137
4138   /* Fill in CHARVEC with all of the parameters.  */
4139   charvec = make_tree_vec (len);
4140   for (int i = 0; i < len; ++i)
4141     TREE_VEC_ELT (charvec, i)
4142       = double_int_to_tree (str_char_type_node,
4143                             double_int::from_buffer (str + i * sz, sz));
4144
4145   /* Build the argument packs.  */
4146   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4147   TREE_TYPE (argpack) = str_char_type_node;
4148
4149   TREE_VEC_ELT (argvec, 1) = argpack;
4150
4151   return argvec;
4152 }
4153
4154 /* Parse a user-defined numeric constant.  returns a call to a user-defined
4155    literal operator.  */
4156
4157 static cp_expr
4158 cp_parser_userdef_numeric_literal (cp_parser *parser)
4159 {
4160   cp_token *token = cp_lexer_consume_token (parser->lexer);
4161   tree literal = token->u.value;
4162   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4163   tree value = USERDEF_LITERAL_VALUE (literal);
4164   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4165   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4166   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4167   tree decl, result;
4168   vec<tree, va_gc> *args;
4169
4170   /* Look for a literal operator taking the exact type of numeric argument
4171      as the literal value.  */
4172   args = make_tree_vector ();
4173   vec_safe_push (args, value);
4174   decl = lookup_literal_operator (name, args);
4175   if (decl && decl != error_mark_node)
4176     {
4177       result = finish_call_expr (decl, &args, false, true,
4178                                  tf_warning_or_error);
4179
4180       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4181         {
4182           warning_at (token->location, OPT_Woverflow,
4183                       "integer literal exceeds range of %qT type",
4184                       long_long_unsigned_type_node);
4185         }
4186       else
4187         {
4188           if (overflow > 0)
4189             warning_at (token->location, OPT_Woverflow,
4190                         "floating literal exceeds range of %qT type",
4191                         long_double_type_node);
4192           else if (overflow < 0)
4193             warning_at (token->location, OPT_Woverflow,
4194                         "floating literal truncated to zero");
4195         }
4196
4197       release_tree_vector (args);
4198       return result;
4199     }
4200   release_tree_vector (args);
4201
4202   /* If the numeric argument didn't work, look for a raw literal
4203      operator taking a const char* argument consisting of the number
4204      in string format.  */
4205   args = make_tree_vector ();
4206   vec_safe_push (args, num_string);
4207   decl = lookup_literal_operator (name, args);
4208   if (decl && decl != error_mark_node)
4209     {
4210       result = finish_call_expr (decl, &args, false, true,
4211                                  tf_warning_or_error);
4212       release_tree_vector (args);
4213       return result;
4214     }
4215   release_tree_vector (args);
4216
4217   /* If the raw literal didn't work, look for a non-type template
4218      function with parameter pack char....  Call the function with
4219      template parameter characters representing the number.  */
4220   args = make_tree_vector ();
4221   decl = lookup_literal_operator (name, args);
4222   if (decl && decl != error_mark_node)
4223     {
4224       tree tmpl_args = make_char_string_pack (num_string);
4225       decl = lookup_template_function (decl, tmpl_args);
4226       result = finish_call_expr (decl, &args, false, true,
4227                                  tf_warning_or_error);
4228       release_tree_vector (args);
4229       return result;
4230     }
4231
4232   release_tree_vector (args);
4233
4234   error ("unable to find numeric literal operator %qD", name);
4235   if (!cpp_get_options (parse_in)->ext_numeric_literals)
4236     inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4237             "to enable more built-in suffixes");
4238   return error_mark_node;
4239 }
4240
4241 /* Parse a user-defined string constant.  Returns a call to a user-defined
4242    literal operator taking a character pointer and the length of the string
4243    as arguments.  */
4244
4245 static tree
4246 cp_parser_userdef_string_literal (tree literal)
4247 {
4248   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4249   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4250   tree value = USERDEF_LITERAL_VALUE (literal);
4251   int len = TREE_STRING_LENGTH (value)
4252         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4253   tree decl, result;
4254   vec<tree, va_gc> *args;
4255
4256   /* Build up a call to the user-defined operator.  */
4257   /* Lookup the name we got back from the id-expression.  */
4258   args = make_tree_vector ();
4259   vec_safe_push (args, value);
4260   vec_safe_push (args, build_int_cst (size_type_node, len));
4261   decl = lookup_literal_operator (name, args);
4262
4263   if (decl && decl != error_mark_node)
4264     {
4265       result = finish_call_expr (decl, &args, false, true,
4266                                  tf_warning_or_error);
4267       release_tree_vector (args);
4268       return result;
4269     }
4270   release_tree_vector (args);
4271
4272   /* Look for a template function with typename parameter CharT
4273      and parameter pack CharT...  Call the function with
4274      template parameter characters representing the string.  */
4275   args = make_tree_vector ();
4276   decl = lookup_literal_operator (name, args);
4277   if (decl && decl != error_mark_node)
4278     {
4279       tree tmpl_args = make_string_pack (value);
4280       decl = lookup_template_function (decl, tmpl_args);
4281       result = finish_call_expr (decl, &args, false, true,
4282                                  tf_warning_or_error);
4283       release_tree_vector (args);
4284       return result;
4285     }
4286   release_tree_vector (args);
4287
4288   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4289          name, TREE_TYPE (value), size_type_node);
4290   return error_mark_node;
4291 }
4292
4293
4294 /* Basic concepts [gram.basic]  */
4295
4296 /* Parse a translation-unit.
4297
4298    translation-unit:
4299      declaration-seq [opt]
4300
4301    Returns TRUE if all went well.  */
4302
4303 static bool
4304 cp_parser_translation_unit (cp_parser* parser)
4305 {
4306   /* The address of the first non-permanent object on the declarator
4307      obstack.  */
4308   static void *declarator_obstack_base;
4309
4310   bool success;
4311
4312   /* Create the declarator obstack, if necessary.  */
4313   if (!cp_error_declarator)
4314     {
4315       gcc_obstack_init (&declarator_obstack);
4316       /* Create the error declarator.  */
4317       cp_error_declarator = make_declarator (cdk_error);
4318       /* Create the empty parameter list.  */
4319       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4320       /* Remember where the base of the declarator obstack lies.  */
4321       declarator_obstack_base = obstack_next_free (&declarator_obstack);
4322     }
4323
4324   cp_parser_declaration_seq_opt (parser);
4325
4326   /* If there are no tokens left then all went well.  */
4327   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4328     {
4329       /* Get rid of the token array; we don't need it any more.  */
4330       cp_lexer_destroy (parser->lexer);
4331       parser->lexer = NULL;
4332
4333       /* This file might have been a context that's implicitly extern
4334          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
4335       if (parser->implicit_extern_c)
4336         {
4337           pop_lang_context ();
4338           parser->implicit_extern_c = false;
4339         }
4340
4341       /* Finish up.  */
4342       finish_translation_unit ();
4343
4344       success = true;
4345     }
4346   else
4347     {
4348       cp_parser_error (parser, "expected declaration");
4349       success = false;
4350     }
4351
4352   /* Make sure the declarator obstack was fully cleaned up.  */
4353   gcc_assert (obstack_next_free (&declarator_obstack)
4354               == declarator_obstack_base);
4355
4356   /* All went well.  */
4357   return success;
4358 }
4359
4360 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4361    decltype context.  */
4362
4363 static inline tsubst_flags_t
4364 complain_flags (bool decltype_p)
4365 {
4366   tsubst_flags_t complain = tf_warning_or_error;
4367   if (decltype_p)
4368     complain |= tf_decltype;
4369   return complain;
4370 }
4371
4372 /* We're about to parse a collection of statements.  If we're currently
4373    parsing tentatively, set up a firewall so that any nested
4374    cp_parser_commit_to_tentative_parse won't affect the current context.  */
4375
4376 static cp_token_position
4377 cp_parser_start_tentative_firewall (cp_parser *parser)
4378 {
4379   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4380     return 0;
4381
4382   cp_parser_parse_tentatively (parser);
4383   cp_parser_commit_to_topmost_tentative_parse (parser);
4384   return cp_lexer_token_position (parser->lexer, false);
4385 }
4386
4387 /* We've finished parsing the collection of statements.  Wrap up the
4388    firewall and replace the relevant tokens with the parsed form.  */
4389
4390 static void
4391 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4392                                   tree expr)
4393 {
4394   if (!start)
4395     return;
4396
4397   /* Finish the firewall level.  */
4398   cp_parser_parse_definitely (parser);
4399   /* And remember the result of the parse for when we try again.  */
4400   cp_token *token = cp_lexer_token_at (parser->lexer, start);
4401   token->type = CPP_PREPARSED_EXPR;
4402   token->u.value = expr;
4403   token->keyword = RID_MAX;
4404   cp_lexer_purge_tokens_after (parser->lexer, start);
4405 }
4406
4407 /* Like the above functions, but let the user modify the tokens.  Used by
4408    CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4409    later parses, so it makes sense to localize the effects of
4410    cp_parser_commit_to_tentative_parse.  */
4411
4412 struct tentative_firewall
4413 {
4414   cp_parser *parser;
4415   bool set;
4416
4417   tentative_firewall (cp_parser *p): parser(p)
4418   {
4419     /* If we're currently parsing tentatively, start a committed level as a
4420        firewall and then an inner tentative parse.  */
4421     if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4422       {
4423         cp_parser_parse_tentatively (parser);
4424         cp_parser_commit_to_topmost_tentative_parse (parser);
4425         cp_parser_parse_tentatively (parser);
4426       }
4427   }
4428
4429   ~tentative_firewall()
4430   {
4431     if (set)
4432       {
4433         /* Finish the inner tentative parse and the firewall, propagating any
4434            uncommitted error state to the outer tentative parse.  */
4435         bool err = cp_parser_error_occurred (parser);
4436         cp_parser_parse_definitely (parser);
4437         cp_parser_parse_definitely (parser);
4438         if (err)
4439           cp_parser_simulate_error (parser);
4440       }
4441   }
4442 };
4443
4444 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4445    enclosing parentheses.  */
4446
4447 static cp_expr
4448 cp_parser_statement_expr (cp_parser *parser)
4449 {
4450   cp_token_position start = cp_parser_start_tentative_firewall (parser);
4451
4452   /* Consume the '('.  */
4453   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4454   cp_lexer_consume_token (parser->lexer);
4455   /* Start the statement-expression.  */
4456   tree expr = begin_stmt_expr ();
4457   /* Parse the compound-statement.  */
4458   cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4459   /* Finish up.  */
4460   expr = finish_stmt_expr (expr, false);
4461   /* Consume the ')'.  */
4462   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4463   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4464     cp_parser_skip_to_end_of_statement (parser);
4465
4466   cp_parser_end_tentative_firewall (parser, start, expr);
4467   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4468   return cp_expr (expr, combined_loc);
4469 }
4470
4471 /* Expressions [gram.expr] */
4472
4473 /* Parse a fold-operator.
4474
4475     fold-operator:
4476         -  *  /  %  ^  &  |  =  <  >  <<  >>
4477       =  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=
4478       ==  !=  <=  >=  &&  ||  ,  .*  ->*
4479
4480    This returns the tree code corresponding to the matched operator
4481    as an int. When the current token matches a compound assignment
4482    opertor, the resulting tree code is the negative value of the
4483    non-assignment operator. */
4484
4485 static int
4486 cp_parser_fold_operator (cp_token *token)
4487 {
4488   switch (token->type)
4489     {
4490     case CPP_PLUS: return PLUS_EXPR;
4491     case CPP_MINUS: return MINUS_EXPR;
4492     case CPP_MULT: return MULT_EXPR;
4493     case CPP_DIV: return TRUNC_DIV_EXPR;
4494     case CPP_MOD: return TRUNC_MOD_EXPR;
4495     case CPP_XOR: return BIT_XOR_EXPR;
4496     case CPP_AND: return BIT_AND_EXPR;
4497     case CPP_OR: return BIT_IOR_EXPR;
4498     case CPP_LSHIFT: return LSHIFT_EXPR;
4499     case CPP_RSHIFT: return RSHIFT_EXPR;
4500
4501     case CPP_EQ: return -NOP_EXPR;
4502     case CPP_PLUS_EQ: return -PLUS_EXPR;
4503     case CPP_MINUS_EQ: return -MINUS_EXPR;
4504     case CPP_MULT_EQ: return -MULT_EXPR;
4505     case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4506     case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4507     case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4508     case CPP_AND_EQ: return -BIT_AND_EXPR;
4509     case CPP_OR_EQ: return -BIT_IOR_EXPR;
4510     case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4511     case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4512
4513     case CPP_EQ_EQ: return EQ_EXPR;
4514     case CPP_NOT_EQ: return NE_EXPR;
4515     case CPP_LESS: return LT_EXPR;
4516     case CPP_GREATER: return GT_EXPR;
4517     case CPP_LESS_EQ: return LE_EXPR;
4518     case CPP_GREATER_EQ: return GE_EXPR;
4519
4520     case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4521     case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4522
4523     case CPP_COMMA: return COMPOUND_EXPR;
4524
4525     case CPP_DOT_STAR: return DOTSTAR_EXPR;
4526     case CPP_DEREF_STAR: return MEMBER_REF;
4527
4528     default: return ERROR_MARK;
4529     }
4530 }
4531
4532 /* Returns true if CODE indicates a binary expression, which is not allowed in
4533    the LHS of a fold-expression.  More codes will need to be added to use this
4534    function in other contexts.  */
4535
4536 static bool
4537 is_binary_op (tree_code code)
4538 {
4539   switch (code)
4540     {
4541     case PLUS_EXPR:
4542     case POINTER_PLUS_EXPR:
4543     case MINUS_EXPR:
4544     case MULT_EXPR:
4545     case TRUNC_DIV_EXPR:
4546     case TRUNC_MOD_EXPR:
4547     case BIT_XOR_EXPR:
4548     case BIT_AND_EXPR:
4549     case BIT_IOR_EXPR:
4550     case LSHIFT_EXPR:
4551     case RSHIFT_EXPR:
4552
4553     case MODOP_EXPR:
4554
4555     case EQ_EXPR:
4556     case NE_EXPR:
4557     case LE_EXPR:
4558     case GE_EXPR:
4559     case LT_EXPR:
4560     case GT_EXPR:
4561
4562     case TRUTH_ANDIF_EXPR:
4563     case TRUTH_ORIF_EXPR:
4564
4565     case COMPOUND_EXPR:
4566
4567     case DOTSTAR_EXPR:
4568     case MEMBER_REF:
4569       return true;
4570
4571     default:
4572       return false;
4573     }
4574 }
4575
4576 /* If the next token is a suitable fold operator, consume it and return as
4577    the function above.  */
4578
4579 static int
4580 cp_parser_fold_operator (cp_parser *parser)
4581 {
4582   cp_token* token = cp_lexer_peek_token (parser->lexer);
4583   int code = cp_parser_fold_operator (token);
4584   if (code != ERROR_MARK)
4585     cp_lexer_consume_token (parser->lexer);
4586   return code;
4587 }
4588
4589 /* Parse a fold-expression.
4590
4591      fold-expression:
4592        ( ... folding-operator cast-expression)
4593        ( cast-expression folding-operator ... )
4594        ( cast-expression folding operator ... folding-operator cast-expression)
4595
4596    Note that the '(' and ')' are matched in primary expression. */
4597
4598 static cp_expr
4599 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4600 {
4601   cp_id_kind pidk;
4602
4603   // Left fold.
4604   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4605     {
4606       cp_lexer_consume_token (parser->lexer);
4607       int op = cp_parser_fold_operator (parser);
4608       if (op == ERROR_MARK)
4609         {
4610           cp_parser_error (parser, "expected binary operator");
4611           return error_mark_node;
4612         }
4613
4614       tree expr = cp_parser_cast_expression (parser, false, false,
4615                                              false, &pidk);
4616       if (expr == error_mark_node)
4617         return error_mark_node;
4618       return finish_left_unary_fold_expr (expr, op);
4619     }
4620
4621   const cp_token* token = cp_lexer_peek_token (parser->lexer);
4622   int op = cp_parser_fold_operator (parser);
4623   if (op == ERROR_MARK)
4624     {
4625       cp_parser_error (parser, "expected binary operator");
4626       return error_mark_node;
4627     }
4628
4629   if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4630     {
4631       cp_parser_error (parser, "expected ...");
4632       return error_mark_node;
4633     }
4634   cp_lexer_consume_token (parser->lexer);
4635
4636   /* The operands of a fold-expression are cast-expressions, so binary or
4637      conditional expressions are not allowed.  We check this here to avoid
4638      tentative parsing.  */
4639   if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4640     /* OK, the expression was parenthesized.  */;
4641   else if (is_binary_op (TREE_CODE (expr1)))
4642     error_at (location_of (expr1),
4643               "binary expression in operand of fold-expression");
4644   else if (TREE_CODE (expr1) == COND_EXPR)
4645     error_at (location_of (expr1),
4646               "conditional expression in operand of fold-expression");
4647
4648   // Right fold.
4649   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4650     return finish_right_unary_fold_expr (expr1, op);
4651
4652   if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4653     {
4654       cp_parser_error (parser, "mismatched operator in fold-expression");
4655       return error_mark_node;
4656     }
4657   cp_lexer_consume_token (parser->lexer);
4658
4659   // Binary left or right fold.
4660   tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4661   if (expr2 == error_mark_node)
4662     return error_mark_node;
4663   return finish_binary_fold_expr (expr1, expr2, op);
4664 }
4665
4666 /* Parse a primary-expression.
4667
4668    primary-expression:
4669      literal
4670      this
4671      ( expression )
4672      id-expression
4673      lambda-expression (C++11)
4674
4675    GNU Extensions:
4676
4677    primary-expression:
4678      ( compound-statement )
4679      __builtin_va_arg ( assignment-expression , type-id )
4680      __builtin_offsetof ( type-id , offsetof-expression )
4681
4682    C++ Extensions:
4683      __has_nothrow_assign ( type-id )   
4684      __has_nothrow_constructor ( type-id )
4685      __has_nothrow_copy ( type-id )
4686      __has_trivial_assign ( type-id )   
4687      __has_trivial_constructor ( type-id )
4688      __has_trivial_copy ( type-id )
4689      __has_trivial_destructor ( type-id )
4690      __has_virtual_destructor ( type-id )     
4691      __is_abstract ( type-id )
4692      __is_base_of ( type-id , type-id )
4693      __is_class ( type-id )
4694      __is_empty ( type-id )
4695      __is_enum ( type-id )
4696      __is_final ( type-id )
4697      __is_literal_type ( type-id )
4698      __is_pod ( type-id )
4699      __is_polymorphic ( type-id )
4700      __is_std_layout ( type-id )
4701      __is_trivial ( type-id )
4702      __is_union ( type-id )
4703
4704    Objective-C++ Extension:
4705
4706    primary-expression:
4707      objc-expression
4708
4709    literal:
4710      __null
4711
4712    ADDRESS_P is true iff this expression was immediately preceded by
4713    "&" and therefore might denote a pointer-to-member.  CAST_P is true
4714    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
4715    true iff this expression is a template argument.
4716
4717    Returns a representation of the expression.  Upon return, *IDK
4718    indicates what kind of id-expression (if any) was present.  */
4719
4720 static cp_expr
4721 cp_parser_primary_expression (cp_parser *parser,
4722                               bool address_p,
4723                               bool cast_p,
4724                               bool template_arg_p,
4725                               bool decltype_p,
4726                               cp_id_kind *idk)
4727 {
4728   cp_token *token = NULL;
4729
4730   /* Assume the primary expression is not an id-expression.  */
4731   *idk = CP_ID_KIND_NONE;
4732
4733   /* Peek at the next token.  */
4734   token = cp_lexer_peek_token (parser->lexer);
4735   switch ((int) token->type)
4736     {
4737       /* literal:
4738            integer-literal
4739            character-literal
4740            floating-literal
4741            string-literal
4742            boolean-literal
4743            pointer-literal
4744            user-defined-literal  */
4745     case CPP_CHAR:
4746     case CPP_CHAR16:
4747     case CPP_CHAR32:
4748     case CPP_WCHAR:
4749     case CPP_UTF8CHAR:
4750     case CPP_NUMBER:
4751     case CPP_PREPARSED_EXPR:
4752       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4753         return cp_parser_userdef_numeric_literal (parser);
4754       token = cp_lexer_consume_token (parser->lexer);
4755       if (TREE_CODE (token->u.value) == FIXED_CST)
4756         {
4757           error_at (token->location,
4758                     "fixed-point types not supported in C++");
4759           return error_mark_node;
4760         }
4761       /* Floating-point literals are only allowed in an integral
4762          constant expression if they are cast to an integral or
4763          enumeration type.  */
4764       if (TREE_CODE (token->u.value) == REAL_CST
4765           && parser->integral_constant_expression_p
4766           && pedantic)
4767         {
4768           /* CAST_P will be set even in invalid code like "int(2.7 +
4769              ...)".   Therefore, we have to check that the next token
4770              is sure to end the cast.  */
4771           if (cast_p)
4772             {
4773               cp_token *next_token;
4774
4775               next_token = cp_lexer_peek_token (parser->lexer);
4776               if (/* The comma at the end of an
4777                      enumerator-definition.  */
4778                   next_token->type != CPP_COMMA
4779                   /* The curly brace at the end of an enum-specifier.  */
4780                   && next_token->type != CPP_CLOSE_BRACE
4781                   /* The end of a statement.  */
4782                   && next_token->type != CPP_SEMICOLON
4783                   /* The end of the cast-expression.  */
4784                   && next_token->type != CPP_CLOSE_PAREN
4785                   /* The end of an array bound.  */
4786                   && next_token->type != CPP_CLOSE_SQUARE
4787                   /* The closing ">" in a template-argument-list.  */
4788                   && (next_token->type != CPP_GREATER
4789                       || parser->greater_than_is_operator_p)
4790                   /* C++0x only: A ">>" treated like two ">" tokens,
4791                      in a template-argument-list.  */
4792                   && (next_token->type != CPP_RSHIFT
4793                       || (cxx_dialect == cxx98)
4794                       || parser->greater_than_is_operator_p))
4795                 cast_p = false;
4796             }
4797
4798           /* If we are within a cast, then the constraint that the
4799              cast is to an integral or enumeration type will be
4800              checked at that point.  If we are not within a cast, then
4801              this code is invalid.  */
4802           if (!cast_p)
4803             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4804         }
4805       return cp_expr (token->u.value, token->location);
4806
4807     case CPP_CHAR_USERDEF:
4808     case CPP_CHAR16_USERDEF:
4809     case CPP_CHAR32_USERDEF:
4810     case CPP_WCHAR_USERDEF:
4811     case CPP_UTF8CHAR_USERDEF:
4812       return cp_parser_userdef_char_literal (parser);
4813
4814     case CPP_STRING:
4815     case CPP_STRING16:
4816     case CPP_STRING32:
4817     case CPP_WSTRING:
4818     case CPP_UTF8STRING:
4819     case CPP_STRING_USERDEF:
4820     case CPP_STRING16_USERDEF:
4821     case CPP_STRING32_USERDEF:
4822     case CPP_WSTRING_USERDEF:
4823     case CPP_UTF8STRING_USERDEF:
4824       /* ??? Should wide strings be allowed when parser->translate_strings_p
4825          is false (i.e. in attributes)?  If not, we can kill the third
4826          argument to cp_parser_string_literal.  */
4827       return cp_parser_string_literal (parser,
4828                                        parser->translate_strings_p,
4829                                        true);
4830
4831     case CPP_OPEN_PAREN:
4832       /* If we see `( { ' then we are looking at the beginning of
4833          a GNU statement-expression.  */
4834       if (cp_parser_allow_gnu_extensions_p (parser)
4835           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4836         {
4837           /* Statement-expressions are not allowed by the standard.  */
4838           pedwarn (token->location, OPT_Wpedantic,
4839                    "ISO C++ forbids braced-groups within expressions");
4840
4841           /* And they're not allowed outside of a function-body; you
4842              cannot, for example, write:
4843
4844              int i = ({ int j = 3; j + 1; });
4845
4846              at class or namespace scope.  */
4847           if (!parser->in_function_body
4848               || parser->in_template_argument_list_p)
4849             {
4850               error_at (token->location,
4851                         "statement-expressions are not allowed outside "
4852                         "functions nor in template-argument lists");
4853               cp_parser_skip_to_end_of_block_or_statement (parser);
4854               if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4855                 cp_lexer_consume_token (parser->lexer);
4856               return error_mark_node;
4857             }
4858           else
4859             return cp_parser_statement_expr (parser);
4860         }
4861       /* Otherwise it's a normal parenthesized expression.  */
4862       {
4863         cp_expr expr;
4864         bool saved_greater_than_is_operator_p;
4865
4866         location_t open_paren_loc = token->location;
4867
4868         /* Consume the `('.  */
4869         cp_lexer_consume_token (parser->lexer);
4870         /* Within a parenthesized expression, a `>' token is always
4871            the greater-than operator.  */
4872         saved_greater_than_is_operator_p
4873           = parser->greater_than_is_operator_p;
4874         parser->greater_than_is_operator_p = true;
4875
4876         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4877           /* Left fold expression. */
4878           expr = NULL_TREE;
4879         else
4880           /* Parse the parenthesized expression.  */
4881           expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4882
4883         token = cp_lexer_peek_token (parser->lexer);
4884         if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4885           {
4886             expr = cp_parser_fold_expression (parser, expr);
4887             if (expr != error_mark_node
4888                 && cxx_dialect < cxx1z
4889                 && !in_system_header_at (input_location))
4890               pedwarn (input_location, 0, "fold-expressions only available "
4891                        "with -std=c++1z or -std=gnu++1z");
4892           }
4893         else
4894           /* Let the front end know that this expression was
4895              enclosed in parentheses. This matters in case, for
4896              example, the expression is of the form `A::B', since
4897              `&A::B' might be a pointer-to-member, but `&(A::B)' is
4898              not.  */
4899           expr = finish_parenthesized_expr (expr);
4900
4901         /* DR 705: Wrapping an unqualified name in parentheses
4902            suppresses arg-dependent lookup.  We want to pass back
4903            CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4904            (c++/37862), but none of the others.  */
4905         if (*idk != CP_ID_KIND_QUALIFIED)
4906           *idk = CP_ID_KIND_NONE;
4907
4908         /* The `>' token might be the end of a template-id or
4909            template-parameter-list now.  */
4910         parser->greater_than_is_operator_p
4911           = saved_greater_than_is_operator_p;
4912
4913         /* Consume the `)'.  */
4914         token = cp_lexer_peek_token (parser->lexer);
4915         location_t close_paren_loc = token->location;
4916         expr.set_range (open_paren_loc, close_paren_loc);
4917         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4918             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4919           cp_parser_skip_to_end_of_statement (parser);
4920
4921         return expr;
4922       }
4923
4924     case CPP_OPEN_SQUARE:
4925       {
4926         if (c_dialect_objc ())
4927           {
4928             /* We might have an Objective-C++ message. */
4929             cp_parser_parse_tentatively (parser);
4930             tree msg = cp_parser_objc_message_expression (parser);
4931             /* If that works out, we're done ... */
4932             if (cp_parser_parse_definitely (parser))
4933               return msg;
4934             /* ... else, fall though to see if it's a lambda.  */
4935           }
4936         cp_expr lam = cp_parser_lambda_expression (parser);
4937         /* Don't warn about a failed tentative parse.  */
4938         if (cp_parser_error_occurred (parser))
4939           return error_mark_node;
4940         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4941         return lam;
4942       }
4943
4944     case CPP_OBJC_STRING:
4945       if (c_dialect_objc ())
4946         /* We have an Objective-C++ string literal. */
4947         return cp_parser_objc_expression (parser);
4948       cp_parser_error (parser, "expected primary-expression");
4949       return error_mark_node;
4950
4951     case CPP_KEYWORD:
4952       switch (token->keyword)
4953         {
4954           /* These two are the boolean literals.  */
4955         case RID_TRUE:
4956           cp_lexer_consume_token (parser->lexer);
4957           return cp_expr (boolean_true_node, token->location);
4958         case RID_FALSE:
4959           cp_lexer_consume_token (parser->lexer);
4960           return cp_expr (boolean_false_node, token->location);
4961
4962           /* The `__null' literal.  */
4963         case RID_NULL:
4964           cp_lexer_consume_token (parser->lexer);
4965           return cp_expr (null_node, token->location);
4966
4967           /* The `nullptr' literal.  */
4968         case RID_NULLPTR:
4969           cp_lexer_consume_token (parser->lexer);
4970           return cp_expr (nullptr_node, token->location);
4971
4972           /* Recognize the `this' keyword.  */
4973         case RID_THIS:
4974           cp_lexer_consume_token (parser->lexer);
4975           if (parser->local_variables_forbidden_p)
4976             {
4977               error_at (token->location,
4978                         "%<this%> may not be used in this context");
4979               return error_mark_node;
4980             }
4981           /* Pointers cannot appear in constant-expressions.  */
4982           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4983             return error_mark_node;
4984           return cp_expr (finish_this_expr (), token->location);
4985
4986           /* The `operator' keyword can be the beginning of an
4987              id-expression.  */
4988         case RID_OPERATOR:
4989           goto id_expression;
4990
4991         case RID_FUNCTION_NAME:
4992         case RID_PRETTY_FUNCTION_NAME:
4993         case RID_C99_FUNCTION_NAME:
4994           {
4995             non_integral_constant name;
4996
4997             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4998                __func__ are the names of variables -- but they are
4999                treated specially.  Therefore, they are handled here,
5000                rather than relying on the generic id-expression logic
5001                below.  Grammatically, these names are id-expressions.
5002
5003                Consume the token.  */
5004             token = cp_lexer_consume_token (parser->lexer);
5005
5006             switch (token->keyword)
5007               {
5008               case RID_FUNCTION_NAME:
5009                 name = NIC_FUNC_NAME;
5010                 break;
5011               case RID_PRETTY_FUNCTION_NAME:
5012                 name = NIC_PRETTY_FUNC;
5013                 break;
5014               case RID_C99_FUNCTION_NAME:
5015                 name = NIC_C99_FUNC;
5016                 break;
5017               default:
5018                 gcc_unreachable ();
5019               }
5020
5021             if (cp_parser_non_integral_constant_expression (parser, name))
5022               return error_mark_node;
5023
5024             /* Look up the name.  */
5025             return finish_fname (token->u.value);
5026           }
5027
5028         case RID_VA_ARG:
5029           {
5030             tree expression;
5031             tree type;
5032             source_location type_location;
5033             location_t start_loc
5034               = cp_lexer_peek_token (parser->lexer)->location;
5035             /* The `__builtin_va_arg' construct is used to handle
5036                `va_arg'.  Consume the `__builtin_va_arg' token.  */
5037             cp_lexer_consume_token (parser->lexer);
5038             /* Look for the opening `('.  */
5039             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5040             /* Now, parse the assignment-expression.  */
5041             expression = cp_parser_assignment_expression (parser);
5042             /* Look for the `,'.  */
5043             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5044             type_location = cp_lexer_peek_token (parser->lexer)->location;
5045             /* Parse the type-id.  */
5046             {
5047               type_id_in_expr_sentinel s (parser);
5048               type = cp_parser_type_id (parser);
5049             }
5050             /* Look for the closing `)'.  */
5051             location_t finish_loc
5052               = cp_lexer_peek_token (parser->lexer)->location;
5053             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5054             /* Using `va_arg' in a constant-expression is not
5055                allowed.  */
5056             if (cp_parser_non_integral_constant_expression (parser,
5057                                                             NIC_VA_ARG))
5058               return error_mark_node;
5059             /* Construct a location of the form:
5060                  __builtin_va_arg (v, int)
5061                  ~~~~~~~~~~~~~~~~~~~~~^~~~
5062                with the caret at the type, ranging from the start of the
5063                "__builtin_va_arg" token to the close paren.  */
5064             location_t combined_loc
5065               = make_location (type_location, start_loc, finish_loc);
5066             return build_x_va_arg (combined_loc, expression, type);
5067           }
5068
5069         case RID_OFFSETOF:
5070           return cp_parser_builtin_offsetof (parser);
5071
5072         case RID_HAS_NOTHROW_ASSIGN:
5073         case RID_HAS_NOTHROW_CONSTRUCTOR:
5074         case RID_HAS_NOTHROW_COPY:        
5075         case RID_HAS_TRIVIAL_ASSIGN:
5076         case RID_HAS_TRIVIAL_CONSTRUCTOR:
5077         case RID_HAS_TRIVIAL_COPY:        
5078         case RID_HAS_TRIVIAL_DESTRUCTOR:
5079         case RID_HAS_VIRTUAL_DESTRUCTOR:
5080         case RID_IS_ABSTRACT:
5081         case RID_IS_BASE_OF:
5082         case RID_IS_CLASS:
5083         case RID_IS_EMPTY:
5084         case RID_IS_ENUM:
5085         case RID_IS_FINAL:
5086         case RID_IS_LITERAL_TYPE:
5087         case RID_IS_POD:
5088         case RID_IS_POLYMORPHIC:
5089         case RID_IS_SAME_AS:
5090         case RID_IS_STD_LAYOUT:
5091         case RID_IS_TRIVIAL:
5092         case RID_IS_TRIVIALLY_ASSIGNABLE:
5093         case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5094         case RID_IS_TRIVIALLY_COPYABLE:
5095         case RID_IS_UNION:
5096           return cp_parser_trait_expr (parser, token->keyword);
5097
5098         // C++ concepts
5099         case RID_REQUIRES:
5100           return cp_parser_requires_expression (parser);
5101
5102         /* Objective-C++ expressions.  */
5103         case RID_AT_ENCODE:
5104         case RID_AT_PROTOCOL:
5105         case RID_AT_SELECTOR:
5106           return cp_parser_objc_expression (parser);
5107
5108         case RID_TEMPLATE:
5109           if (parser->in_function_body
5110               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5111                   == CPP_LESS))
5112             {
5113               error_at (token->location,
5114                         "a template declaration cannot appear at block scope");
5115               cp_parser_skip_to_end_of_block_or_statement (parser);
5116               return error_mark_node;
5117             }
5118         default:
5119           cp_parser_error (parser, "expected primary-expression");
5120           return error_mark_node;
5121         }
5122
5123       /* An id-expression can start with either an identifier, a
5124          `::' as the beginning of a qualified-id, or the "operator"
5125          keyword.  */
5126     case CPP_NAME:
5127     case CPP_SCOPE:
5128     case CPP_TEMPLATE_ID:
5129     case CPP_NESTED_NAME_SPECIFIER:
5130       {
5131       id_expression:
5132         cp_expr id_expression;
5133         cp_expr decl;
5134         const char *error_msg;
5135         bool template_p;
5136         bool done;
5137         cp_token *id_expr_token;
5138
5139         /* Parse the id-expression.  */
5140         id_expression
5141           = cp_parser_id_expression (parser,
5142                                      /*template_keyword_p=*/false,
5143                                      /*check_dependency_p=*/true,
5144                                      &template_p,
5145                                      /*declarator_p=*/false,
5146                                      /*optional_p=*/false);
5147         if (id_expression == error_mark_node)
5148           return error_mark_node;
5149         id_expr_token = token;
5150         token = cp_lexer_peek_token (parser->lexer);
5151         done = (token->type != CPP_OPEN_SQUARE
5152                 && token->type != CPP_OPEN_PAREN
5153                 && token->type != CPP_DOT
5154                 && token->type != CPP_DEREF
5155                 && token->type != CPP_PLUS_PLUS
5156                 && token->type != CPP_MINUS_MINUS);
5157         /* If we have a template-id, then no further lookup is
5158            required.  If the template-id was for a template-class, we
5159            will sometimes have a TYPE_DECL at this point.  */
5160         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5161                  || TREE_CODE (id_expression) == TYPE_DECL)
5162           decl = id_expression;
5163         /* Look up the name.  */
5164         else
5165           {
5166             tree ambiguous_decls;
5167
5168             /* If we already know that this lookup is ambiguous, then
5169                we've already issued an error message; there's no reason
5170                to check again.  */
5171             if (id_expr_token->type == CPP_NAME
5172                 && id_expr_token->error_reported)
5173               {
5174                 cp_parser_simulate_error (parser);
5175                 return error_mark_node;
5176               }
5177
5178             decl = cp_parser_lookup_name (parser, id_expression,
5179                                           none_type,
5180                                           template_p,
5181                                           /*is_namespace=*/false,
5182                                           /*check_dependency=*/true,
5183                                           &ambiguous_decls,
5184                                           id_expr_token->location);
5185             /* If the lookup was ambiguous, an error will already have
5186                been issued.  */
5187             if (ambiguous_decls)
5188               return error_mark_node;
5189
5190             /* In Objective-C++, we may have an Objective-C 2.0
5191                dot-syntax for classes here.  */
5192             if (c_dialect_objc ()
5193                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5194                 && TREE_CODE (decl) == TYPE_DECL
5195                 && objc_is_class_name (decl))
5196               {
5197                 tree component;
5198                 cp_lexer_consume_token (parser->lexer);
5199                 component = cp_parser_identifier (parser);
5200                 if (component == error_mark_node)
5201                   return error_mark_node;
5202
5203                 tree result = objc_build_class_component_ref (id_expression,
5204                                                               component);
5205                 /* Build a location of the form:
5206                      expr.component
5207                      ~~~~~^~~~~~~~~
5208                    with caret at the start of the component name (at
5209                    input_location), ranging from the start of the id_expression
5210                    to the end of the component name.  */
5211                 location_t combined_loc
5212                   = make_location (input_location, id_expression.get_start (),
5213                                    get_finish (input_location));
5214                 protected_set_expr_location (result, combined_loc);
5215                 return result;
5216               }
5217
5218             /* In Objective-C++, an instance variable (ivar) may be preferred
5219                to whatever cp_parser_lookup_name() found.
5220                Call objc_lookup_ivar.  To avoid exposing cp_expr to the
5221                rest of c-family, we have to do a little extra work to preserve
5222                any location information in cp_expr "decl".  Given that
5223                objc_lookup_ivar is implemented in "c-family" and "objc", we
5224                have a trip through the pure "tree" type, rather than cp_expr.
5225                Naively copying it back to "decl" would implicitly give the
5226                new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5227                store an EXPR_LOCATION.  Hence we only update "decl" (and
5228                hence its location_t) if we get back a different tree node.  */
5229             tree decl_tree = objc_lookup_ivar (decl.get_value (),
5230                                                id_expression);
5231             if (decl_tree != decl.get_value ())
5232               decl = cp_expr (decl_tree);
5233
5234             /* If name lookup gives us a SCOPE_REF, then the
5235                qualifying scope was dependent.  */
5236             if (TREE_CODE (decl) == SCOPE_REF)
5237               {
5238                 /* At this point, we do not know if DECL is a valid
5239                    integral constant expression.  We assume that it is
5240                    in fact such an expression, so that code like:
5241
5242                       template <int N> struct A {
5243                         int a[B<N>::i];
5244                       };
5245                      
5246                    is accepted.  At template-instantiation time, we
5247                    will check that B<N>::i is actually a constant.  */
5248                 return decl;
5249               }
5250             /* Check to see if DECL is a local variable in a context
5251                where that is forbidden.  */
5252             if (parser->local_variables_forbidden_p
5253                 && local_variable_p (decl))
5254               {
5255                 /* It might be that we only found DECL because we are
5256                    trying to be generous with pre-ISO scoping rules.
5257                    For example, consider:
5258
5259                      int i;
5260                      void g() {
5261                        for (int i = 0; i < 10; ++i) {}
5262                        extern void f(int j = i);
5263                      }
5264
5265                    Here, name look up will originally find the out
5266                    of scope `i'.  We need to issue a warning message,
5267                    but then use the global `i'.  */
5268                 decl = check_for_out_of_scope_variable (decl);
5269                 if (local_variable_p (decl))
5270                   {
5271                     error_at (id_expr_token->location,
5272                               "local variable %qD may not appear in this context",
5273                               decl.get_value ());
5274                     return error_mark_node;
5275                   }
5276               }
5277           }
5278
5279         decl = (finish_id_expression
5280                 (id_expression, decl, parser->scope,
5281                  idk,
5282                  parser->integral_constant_expression_p,
5283                  parser->allow_non_integral_constant_expression_p,
5284                  &parser->non_integral_constant_expression_p,
5285                  template_p, done, address_p,
5286                  template_arg_p,
5287                  &error_msg,
5288                  id_expr_token->location));
5289         if (error_msg)
5290           cp_parser_error (parser, error_msg);
5291         decl.set_location (id_expr_token->location);
5292         return decl;
5293       }
5294
5295       /* Anything else is an error.  */
5296     default:
5297       cp_parser_error (parser, "expected primary-expression");
5298       return error_mark_node;
5299     }
5300 }
5301
5302 static inline cp_expr
5303 cp_parser_primary_expression (cp_parser *parser,
5304                               bool address_p,
5305                               bool cast_p,
5306                               bool template_arg_p,
5307                               cp_id_kind *idk)
5308 {
5309   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5310                                        /*decltype*/false, idk);
5311 }
5312
5313 /* Parse an id-expression.
5314
5315    id-expression:
5316      unqualified-id
5317      qualified-id
5318
5319    qualified-id:
5320      :: [opt] nested-name-specifier template [opt] unqualified-id
5321      :: identifier
5322      :: operator-function-id
5323      :: template-id
5324
5325    Return a representation of the unqualified portion of the
5326    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
5327    a `::' or nested-name-specifier.
5328
5329    Often, if the id-expression was a qualified-id, the caller will
5330    want to make a SCOPE_REF to represent the qualified-id.  This
5331    function does not do this in order to avoid wastefully creating
5332    SCOPE_REFs when they are not required.
5333
5334    If TEMPLATE_KEYWORD_P is true, then we have just seen the
5335    `template' keyword.
5336
5337    If CHECK_DEPENDENCY_P is false, then names are looked up inside
5338    uninstantiated templates.
5339
5340    If *TEMPLATE_P is non-NULL, it is set to true iff the
5341    `template' keyword is used to explicitly indicate that the entity
5342    named is a template.
5343
5344    If DECLARATOR_P is true, the id-expression is appearing as part of
5345    a declarator, rather than as part of an expression.  */
5346
5347 static cp_expr
5348 cp_parser_id_expression (cp_parser *parser,
5349                          bool template_keyword_p,
5350                          bool check_dependency_p,
5351                          bool *template_p,
5352                          bool declarator_p,
5353                          bool optional_p)
5354 {
5355   bool global_scope_p;
5356   bool nested_name_specifier_p;
5357
5358   /* Assume the `template' keyword was not used.  */
5359   if (template_p)
5360     *template_p = template_keyword_p;
5361
5362   /* Look for the optional `::' operator.  */
5363   global_scope_p
5364     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5365        != NULL_TREE);
5366   /* Look for the optional nested-name-specifier.  */
5367   nested_name_specifier_p
5368     = (cp_parser_nested_name_specifier_opt (parser,
5369                                             /*typename_keyword_p=*/false,
5370                                             check_dependency_p,
5371                                             /*type_p=*/false,
5372                                             declarator_p)
5373        != NULL_TREE);
5374   /* If there is a nested-name-specifier, then we are looking at
5375      the first qualified-id production.  */
5376   if (nested_name_specifier_p)
5377     {
5378       tree saved_scope;
5379       tree saved_object_scope;
5380       tree saved_qualifying_scope;
5381       tree unqualified_id;
5382       bool is_template;
5383
5384       /* See if the next token is the `template' keyword.  */
5385       if (!template_p)
5386         template_p = &is_template;
5387       *template_p = cp_parser_optional_template_keyword (parser);
5388       /* Name lookup we do during the processing of the
5389          unqualified-id might obliterate SCOPE.  */
5390       saved_scope = parser->scope;
5391       saved_object_scope = parser->object_scope;
5392       saved_qualifying_scope = parser->qualifying_scope;
5393       /* Process the final unqualified-id.  */
5394       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5395                                                  check_dependency_p,
5396                                                  declarator_p,
5397                                                  /*optional_p=*/false);
5398       /* Restore the SAVED_SCOPE for our caller.  */
5399       parser->scope = saved_scope;
5400       parser->object_scope = saved_object_scope;
5401       parser->qualifying_scope = saved_qualifying_scope;
5402
5403       return unqualified_id;
5404     }
5405   /* Otherwise, if we are in global scope, then we are looking at one
5406      of the other qualified-id productions.  */
5407   else if (global_scope_p)
5408     {
5409       cp_token *token;
5410       tree id;
5411
5412       /* Peek at the next token.  */
5413       token = cp_lexer_peek_token (parser->lexer);
5414
5415       /* If it's an identifier, and the next token is not a "<", then
5416          we can avoid the template-id case.  This is an optimization
5417          for this common case.  */
5418       if (token->type == CPP_NAME
5419           && !cp_parser_nth_token_starts_template_argument_list_p
5420                (parser, 2))
5421         return cp_parser_identifier (parser);
5422
5423       cp_parser_parse_tentatively (parser);
5424       /* Try a template-id.  */
5425       id = cp_parser_template_id (parser,
5426                                   /*template_keyword_p=*/false,
5427                                   /*check_dependency_p=*/true,
5428                                   none_type,
5429                                   declarator_p);
5430       /* If that worked, we're done.  */
5431       if (cp_parser_parse_definitely (parser))
5432         return id;
5433
5434       /* Peek at the next token.  (Changes in the token buffer may
5435          have invalidated the pointer obtained above.)  */
5436       token = cp_lexer_peek_token (parser->lexer);
5437
5438       switch (token->type)
5439         {
5440         case CPP_NAME:
5441           return cp_parser_identifier (parser);
5442
5443         case CPP_KEYWORD:
5444           if (token->keyword == RID_OPERATOR)
5445             return cp_parser_operator_function_id (parser);
5446           /* Fall through.  */
5447
5448         default:
5449           cp_parser_error (parser, "expected id-expression");
5450           return error_mark_node;
5451         }
5452     }
5453   else
5454     return cp_parser_unqualified_id (parser, template_keyword_p,
5455                                      /*check_dependency_p=*/true,
5456                                      declarator_p,
5457                                      optional_p);
5458 }
5459
5460 /* Parse an unqualified-id.
5461
5462    unqualified-id:
5463      identifier
5464      operator-function-id
5465      conversion-function-id
5466      ~ class-name
5467      template-id
5468
5469    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5470    keyword, in a construct like `A::template ...'.
5471
5472    Returns a representation of unqualified-id.  For the `identifier'
5473    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
5474    production a BIT_NOT_EXPR is returned; the operand of the
5475    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
5476    other productions, see the documentation accompanying the
5477    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
5478    names are looked up in uninstantiated templates.  If DECLARATOR_P
5479    is true, the unqualified-id is appearing as part of a declarator,
5480    rather than as part of an expression.  */
5481
5482 static cp_expr
5483 cp_parser_unqualified_id (cp_parser* parser,
5484                           bool template_keyword_p,
5485                           bool check_dependency_p,
5486                           bool declarator_p,
5487                           bool optional_p)
5488 {
5489   cp_token *token;
5490
5491   /* Peek at the next token.  */
5492   token = cp_lexer_peek_token (parser->lexer);
5493
5494   switch ((int) token->type)
5495     {
5496     case CPP_NAME:
5497       {
5498         tree id;
5499
5500         /* We don't know yet whether or not this will be a
5501            template-id.  */
5502         cp_parser_parse_tentatively (parser);
5503         /* Try a template-id.  */
5504         id = cp_parser_template_id (parser, template_keyword_p,
5505                                     check_dependency_p,
5506                                     none_type,
5507                                     declarator_p);
5508         /* If it worked, we're done.  */
5509         if (cp_parser_parse_definitely (parser))
5510           return id;
5511         /* Otherwise, it's an ordinary identifier.  */
5512         return cp_parser_identifier (parser);
5513       }
5514
5515     case CPP_TEMPLATE_ID:
5516       return cp_parser_template_id (parser, template_keyword_p,
5517                                     check_dependency_p,
5518                                     none_type,
5519                                     declarator_p);
5520
5521     case CPP_COMPL:
5522       {
5523         tree type_decl;
5524         tree qualifying_scope;
5525         tree object_scope;
5526         tree scope;
5527         bool done;
5528
5529         /* Consume the `~' token.  */
5530         cp_lexer_consume_token (parser->lexer);
5531         /* Parse the class-name.  The standard, as written, seems to
5532            say that:
5533
5534              template <typename T> struct S { ~S (); };
5535              template <typename T> S<T>::~S() {}
5536
5537            is invalid, since `~' must be followed by a class-name, but
5538            `S<T>' is dependent, and so not known to be a class.
5539            That's not right; we need to look in uninstantiated
5540            templates.  A further complication arises from:
5541
5542              template <typename T> void f(T t) {
5543                t.T::~T();
5544              }
5545
5546            Here, it is not possible to look up `T' in the scope of `T'
5547            itself.  We must look in both the current scope, and the
5548            scope of the containing complete expression.
5549
5550            Yet another issue is:
5551
5552              struct S {
5553                int S;
5554                ~S();
5555              };
5556
5557              S::~S() {}
5558
5559            The standard does not seem to say that the `S' in `~S'
5560            should refer to the type `S' and not the data member
5561            `S::S'.  */
5562
5563         /* DR 244 says that we look up the name after the "~" in the
5564            same scope as we looked up the qualifying name.  That idea
5565            isn't fully worked out; it's more complicated than that.  */
5566         scope = parser->scope;
5567         object_scope = parser->object_scope;
5568         qualifying_scope = parser->qualifying_scope;
5569
5570         /* Check for invalid scopes.  */
5571         if (scope == error_mark_node)
5572           {
5573             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5574               cp_lexer_consume_token (parser->lexer);
5575             return error_mark_node;
5576           }
5577         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5578           {
5579             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5580               error_at (token->location,
5581                         "scope %qT before %<~%> is not a class-name",
5582                         scope);
5583             cp_parser_simulate_error (parser);
5584             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5585               cp_lexer_consume_token (parser->lexer);
5586             return error_mark_node;
5587           }
5588         gcc_assert (!scope || TYPE_P (scope));
5589
5590         /* If the name is of the form "X::~X" it's OK even if X is a
5591            typedef.  */
5592         token = cp_lexer_peek_token (parser->lexer);
5593         if (scope
5594             && token->type == CPP_NAME
5595             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5596                 != CPP_LESS)
5597             && (token->u.value == TYPE_IDENTIFIER (scope)
5598                 || (CLASS_TYPE_P (scope)
5599                     && constructor_name_p (token->u.value, scope))))
5600           {
5601             cp_lexer_consume_token (parser->lexer);
5602             return build_nt (BIT_NOT_EXPR, scope);
5603           }
5604
5605         /* ~auto means the destructor of whatever the object is.  */
5606         if (cp_parser_is_keyword (token, RID_AUTO))
5607           {
5608             if (cxx_dialect < cxx14)
5609               pedwarn (input_location, 0,
5610                        "%<~auto%> only available with "
5611                        "-std=c++14 or -std=gnu++14");
5612             cp_lexer_consume_token (parser->lexer);
5613             return build_nt (BIT_NOT_EXPR, make_auto ());
5614           }
5615
5616         /* If there was an explicit qualification (S::~T), first look
5617            in the scope given by the qualification (i.e., S).
5618
5619            Note: in the calls to cp_parser_class_name below we pass
5620            typename_type so that lookup finds the injected-class-name
5621            rather than the constructor.  */
5622         done = false;
5623         type_decl = NULL_TREE;
5624         if (scope)
5625           {
5626             cp_parser_parse_tentatively (parser);
5627             type_decl = cp_parser_class_name (parser,
5628                                               /*typename_keyword_p=*/false,
5629                                               /*template_keyword_p=*/false,
5630                                               typename_type,
5631                                               /*check_dependency=*/false,
5632                                               /*class_head_p=*/false,
5633                                               declarator_p);
5634             if (cp_parser_parse_definitely (parser))
5635               done = true;
5636           }
5637         /* In "N::S::~S", look in "N" as well.  */
5638         if (!done && scope && qualifying_scope)
5639           {
5640             cp_parser_parse_tentatively (parser);
5641             parser->scope = qualifying_scope;
5642             parser->object_scope = NULL_TREE;
5643             parser->qualifying_scope = NULL_TREE;
5644             type_decl
5645               = cp_parser_class_name (parser,
5646                                       /*typename_keyword_p=*/false,
5647                                       /*template_keyword_p=*/false,
5648                                       typename_type,
5649                                       /*check_dependency=*/false,
5650                                       /*class_head_p=*/false,
5651                                       declarator_p);
5652             if (cp_parser_parse_definitely (parser))
5653               done = true;
5654           }
5655         /* In "p->S::~T", look in the scope given by "*p" as well.  */
5656         else if (!done && object_scope)
5657           {
5658             cp_parser_parse_tentatively (parser);
5659             parser->scope = object_scope;
5660             parser->object_scope = NULL_TREE;
5661             parser->qualifying_scope = NULL_TREE;
5662             type_decl
5663               = cp_parser_class_name (parser,
5664                                       /*typename_keyword_p=*/false,
5665                                       /*template_keyword_p=*/false,
5666                                       typename_type,
5667                                       /*check_dependency=*/false,
5668                                       /*class_head_p=*/false,
5669                                       declarator_p);
5670             if (cp_parser_parse_definitely (parser))
5671               done = true;
5672           }
5673         /* Look in the surrounding context.  */
5674         if (!done)
5675           {
5676             parser->scope = NULL_TREE;
5677             parser->object_scope = NULL_TREE;
5678             parser->qualifying_scope = NULL_TREE;
5679             if (processing_template_decl)
5680               cp_parser_parse_tentatively (parser);
5681             type_decl
5682               = cp_parser_class_name (parser,
5683                                       /*typename_keyword_p=*/false,
5684                                       /*template_keyword_p=*/false,
5685                                       typename_type,
5686                                       /*check_dependency=*/false,
5687                                       /*class_head_p=*/false,
5688                                       declarator_p);
5689             if (processing_template_decl
5690                 && ! cp_parser_parse_definitely (parser))
5691               {
5692                 /* We couldn't find a type with this name.  If we're parsing
5693                    tentatively, fail and try something else.  */
5694                 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5695                   {
5696                     cp_parser_simulate_error (parser);
5697                     return error_mark_node;
5698                   }
5699                 /* Otherwise, accept it and check for a match at instantiation
5700                    time.  */
5701                 type_decl = cp_parser_identifier (parser);
5702                 if (type_decl != error_mark_node)
5703                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5704                 return type_decl;
5705               }
5706           }
5707         /* If an error occurred, assume that the name of the
5708            destructor is the same as the name of the qualifying
5709            class.  That allows us to keep parsing after running
5710            into ill-formed destructor names.  */
5711         if (type_decl == error_mark_node && scope)
5712           return build_nt (BIT_NOT_EXPR, scope);
5713         else if (type_decl == error_mark_node)
5714           return error_mark_node;
5715
5716         /* Check that destructor name and scope match.  */
5717         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5718           {
5719             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5720               error_at (token->location,
5721                         "declaration of %<~%T%> as member of %qT",
5722                         type_decl, scope);
5723             cp_parser_simulate_error (parser);
5724             return error_mark_node;
5725           }
5726
5727         /* [class.dtor]
5728
5729            A typedef-name that names a class shall not be used as the
5730            identifier in the declarator for a destructor declaration.  */
5731         if (declarator_p
5732             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5733             && !DECL_SELF_REFERENCE_P (type_decl)
5734             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5735           error_at (token->location,
5736                     "typedef-name %qD used as destructor declarator",
5737                     type_decl);
5738
5739         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5740       }
5741
5742     case CPP_KEYWORD:
5743       if (token->keyword == RID_OPERATOR)
5744         {
5745           cp_expr id;
5746
5747           /* This could be a template-id, so we try that first.  */
5748           cp_parser_parse_tentatively (parser);
5749           /* Try a template-id.  */
5750           id = cp_parser_template_id (parser, template_keyword_p,
5751                                       /*check_dependency_p=*/true,
5752                                       none_type,
5753                                       declarator_p);
5754           /* If that worked, we're done.  */
5755           if (cp_parser_parse_definitely (parser))
5756             return id;
5757           /* We still don't know whether we're looking at an
5758              operator-function-id or a conversion-function-id.  */
5759           cp_parser_parse_tentatively (parser);
5760           /* Try an operator-function-id.  */
5761           id = cp_parser_operator_function_id (parser);
5762           /* If that didn't work, try a conversion-function-id.  */
5763           if (!cp_parser_parse_definitely (parser))
5764             id = cp_parser_conversion_function_id (parser);
5765           else if (UDLIT_OPER_P (id))
5766             {
5767               /* 17.6.3.3.5  */
5768               const char *name = UDLIT_OP_SUFFIX (id);
5769               if (name[0] != '_' && !in_system_header_at (input_location)
5770                   && declarator_p)
5771                 warning (0, "literal operator suffixes not preceded by %<_%>"
5772                             " are reserved for future standardization");
5773             }
5774
5775           return id;
5776         }
5777       /* Fall through.  */
5778
5779     default:
5780       if (optional_p)
5781         return NULL_TREE;
5782       cp_parser_error (parser, "expected unqualified-id");
5783       return error_mark_node;
5784     }
5785 }
5786
5787 /* Parse an (optional) nested-name-specifier.
5788
5789    nested-name-specifier: [C++98]
5790      class-or-namespace-name :: nested-name-specifier [opt]
5791      class-or-namespace-name :: template nested-name-specifier [opt]
5792
5793    nested-name-specifier: [C++0x]
5794      type-name ::
5795      namespace-name ::
5796      nested-name-specifier identifier ::
5797      nested-name-specifier template [opt] simple-template-id ::
5798
5799    PARSER->SCOPE should be set appropriately before this function is
5800    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5801    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
5802    in name lookups.
5803
5804    Sets PARSER->SCOPE to the class (TYPE) or namespace
5805    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5806    it unchanged if there is no nested-name-specifier.  Returns the new
5807    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5808
5809    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5810    part of a declaration and/or decl-specifier.  */
5811
5812 static tree
5813 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5814                                      bool typename_keyword_p,
5815                                      bool check_dependency_p,
5816                                      bool type_p,
5817                                      bool is_declaration)
5818 {
5819   bool success = false;
5820   cp_token_position start = 0;
5821   cp_token *token;
5822
5823   /* Remember where the nested-name-specifier starts.  */
5824   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5825     {
5826       start = cp_lexer_token_position (parser->lexer, false);
5827       push_deferring_access_checks (dk_deferred);
5828     }
5829
5830   while (true)
5831     {
5832       tree new_scope;
5833       tree old_scope;
5834       tree saved_qualifying_scope;
5835       bool template_keyword_p;
5836
5837       /* Spot cases that cannot be the beginning of a
5838          nested-name-specifier.  */
5839       token = cp_lexer_peek_token (parser->lexer);
5840
5841       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5842          the already parsed nested-name-specifier.  */
5843       if (token->type == CPP_NESTED_NAME_SPECIFIER)
5844         {
5845           /* Grab the nested-name-specifier and continue the loop.  */
5846           cp_parser_pre_parsed_nested_name_specifier (parser);
5847           /* If we originally encountered this nested-name-specifier
5848              with IS_DECLARATION set to false, we will not have
5849              resolved TYPENAME_TYPEs, so we must do so here.  */
5850           if (is_declaration
5851               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5852             {
5853               new_scope = resolve_typename_type (parser->scope,
5854                                                  /*only_current_p=*/false);
5855               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5856                 parser->scope = new_scope;
5857             }
5858           success = true;
5859           continue;
5860         }
5861
5862       /* Spot cases that cannot be the beginning of a
5863          nested-name-specifier.  On the second and subsequent times
5864          through the loop, we look for the `template' keyword.  */
5865       if (success && token->keyword == RID_TEMPLATE)
5866         ;
5867       /* A template-id can start a nested-name-specifier.  */
5868       else if (token->type == CPP_TEMPLATE_ID)
5869         ;
5870       /* DR 743: decltype can be used in a nested-name-specifier.  */
5871       else if (token_is_decltype (token))
5872         ;
5873       else
5874         {
5875           /* If the next token is not an identifier, then it is
5876              definitely not a type-name or namespace-name.  */
5877           if (token->type != CPP_NAME)
5878             break;
5879           /* If the following token is neither a `<' (to begin a
5880              template-id), nor a `::', then we are not looking at a
5881              nested-name-specifier.  */
5882           token = cp_lexer_peek_nth_token (parser->lexer, 2);
5883
5884           if (token->type == CPP_COLON
5885               && parser->colon_corrects_to_scope_p
5886               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5887             {
5888               error_at (token->location,
5889                         "found %<:%> in nested-name-specifier, expected %<::%>");
5890               token->type = CPP_SCOPE;
5891             }
5892
5893           if (token->type != CPP_SCOPE
5894               && !cp_parser_nth_token_starts_template_argument_list_p
5895                   (parser, 2))
5896             break;
5897         }
5898
5899       /* The nested-name-specifier is optional, so we parse
5900          tentatively.  */
5901       cp_parser_parse_tentatively (parser);
5902
5903       /* Look for the optional `template' keyword, if this isn't the
5904          first time through the loop.  */
5905       if (success)
5906         template_keyword_p = cp_parser_optional_template_keyword (parser);
5907       else
5908         template_keyword_p = false;
5909
5910       /* Save the old scope since the name lookup we are about to do
5911          might destroy it.  */
5912       old_scope = parser->scope;
5913       saved_qualifying_scope = parser->qualifying_scope;
5914       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5915          look up names in "X<T>::I" in order to determine that "Y" is
5916          a template.  So, if we have a typename at this point, we make
5917          an effort to look through it.  */
5918       if (is_declaration
5919           && !typename_keyword_p
5920           && parser->scope
5921           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5922         parser->scope = resolve_typename_type (parser->scope,
5923                                                /*only_current_p=*/false);
5924       /* Parse the qualifying entity.  */
5925       new_scope
5926         = cp_parser_qualifying_entity (parser,
5927                                        typename_keyword_p,
5928                                        template_keyword_p,
5929                                        check_dependency_p,
5930                                        type_p,
5931                                        is_declaration);
5932       /* Look for the `::' token.  */
5933       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5934
5935       /* If we found what we wanted, we keep going; otherwise, we're
5936          done.  */
5937       if (!cp_parser_parse_definitely (parser))
5938         {
5939           bool error_p = false;
5940
5941           /* Restore the OLD_SCOPE since it was valid before the
5942              failed attempt at finding the last
5943              class-or-namespace-name.  */
5944           parser->scope = old_scope;
5945           parser->qualifying_scope = saved_qualifying_scope;
5946
5947           /* If the next token is a decltype, and the one after that is a
5948              `::', then the decltype has failed to resolve to a class or
5949              enumeration type.  Give this error even when parsing
5950              tentatively since it can't possibly be valid--and we're going
5951              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5952              won't get another chance.*/
5953           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5954               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5955                   == CPP_SCOPE))
5956             {
5957               token = cp_lexer_consume_token (parser->lexer);
5958               error_at (token->location, "decltype evaluates to %qT, "
5959                         "which is not a class or enumeration type",
5960                         token->u.tree_check_value->value);
5961               parser->scope = error_mark_node;
5962               error_p = true;
5963               /* As below.  */
5964               success = true;
5965               cp_lexer_consume_token (parser->lexer);
5966             }
5967
5968           if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5969               && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5970             {
5971               /* If we have a non-type template-id followed by ::, it can't
5972                  possibly be valid.  */
5973               token = cp_lexer_peek_token (parser->lexer);
5974               tree tid = token->u.tree_check_value->value;
5975               if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5976                   && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5977                 {
5978                   tree tmpl = NULL_TREE;
5979                   if (is_overloaded_fn (tid))
5980                     {
5981                       tree fns = get_fns (tid);
5982                       if (!OVL_CHAIN (fns))
5983                         tmpl = OVL_CURRENT (fns);
5984                       error_at (token->location, "function template-id %qD "
5985                                 "in nested-name-specifier", tid);
5986                     }
5987                   else
5988                     {
5989                       /* Variable template.  */
5990                       tmpl = TREE_OPERAND (tid, 0);
5991                       gcc_assert (variable_template_p (tmpl));
5992                       error_at (token->location, "variable template-id %qD "
5993                                 "in nested-name-specifier", tid);
5994                     }
5995                   if (tmpl)
5996                     inform (DECL_SOURCE_LOCATION (tmpl),
5997                             "%qD declared here", tmpl);
5998
5999                   parser->scope = error_mark_node;
6000                   error_p = true;
6001                   /* As below.  */
6002                   success = true;
6003                   cp_lexer_consume_token (parser->lexer);
6004                   cp_lexer_consume_token (parser->lexer);
6005                 }
6006             }
6007
6008           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6009             break;
6010           /* If the next token is an identifier, and the one after
6011              that is a `::', then any valid interpretation would have
6012              found a class-or-namespace-name.  */
6013           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6014                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6015                      == CPP_SCOPE)
6016                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6017                      != CPP_COMPL))
6018             {
6019               token = cp_lexer_consume_token (parser->lexer);
6020               if (!error_p)
6021                 {
6022                   if (!token->error_reported)
6023                     {
6024                       tree decl;
6025                       tree ambiguous_decls;
6026
6027                       decl = cp_parser_lookup_name (parser, token->u.value,
6028                                                     none_type,
6029                                                     /*is_template=*/false,
6030                                                     /*is_namespace=*/false,
6031                                                     /*check_dependency=*/true,
6032                                                     &ambiguous_decls,
6033                                                     token->location);
6034                       if (TREE_CODE (decl) == TEMPLATE_DECL)
6035                         error_at (token->location,
6036                                   "%qD used without template parameters",
6037                                   decl);
6038                       else if (ambiguous_decls)
6039                         {
6040                           // cp_parser_lookup_name has the same diagnostic,
6041                           // thus make sure to emit it at most once.
6042                           if (cp_parser_uncommitted_to_tentative_parse_p
6043                               (parser))
6044                             {
6045                               error_at (token->location,
6046                                         "reference to %qD is ambiguous",
6047                                         token->u.value);
6048                               print_candidates (ambiguous_decls);
6049                             }
6050                           decl = error_mark_node;
6051                         }
6052                       else
6053                         {
6054                           if (cxx_dialect != cxx98)
6055                             cp_parser_name_lookup_error
6056                             (parser, token->u.value, decl, NLE_NOT_CXX98,
6057                              token->location);
6058                           else
6059                             cp_parser_name_lookup_error
6060                             (parser, token->u.value, decl, NLE_CXX98,
6061                              token->location);
6062                         }
6063                     }
6064                   parser->scope = error_mark_node;
6065                   error_p = true;
6066                   /* Treat this as a successful nested-name-specifier
6067                      due to:
6068
6069                      [basic.lookup.qual]
6070
6071                      If the name found is not a class-name (clause
6072                      _class_) or namespace-name (_namespace.def_), the
6073                      program is ill-formed.  */
6074                   success = true;
6075                 }
6076               cp_lexer_consume_token (parser->lexer);
6077             }
6078           break;
6079         }
6080       /* We've found one valid nested-name-specifier.  */
6081       success = true;
6082       /* Name lookup always gives us a DECL.  */
6083       if (TREE_CODE (new_scope) == TYPE_DECL)
6084         new_scope = TREE_TYPE (new_scope);
6085       /* Uses of "template" must be followed by actual templates.  */
6086       if (template_keyword_p
6087           && !(CLASS_TYPE_P (new_scope)
6088                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6089                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6090                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
6091           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6092                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6093                    == TEMPLATE_ID_EXPR)))
6094         permerror (input_location, TYPE_P (new_scope)
6095                    ? G_("%qT is not a template")
6096                    : G_("%qD is not a template"),
6097                    new_scope);
6098       /* If it is a class scope, try to complete it; we are about to
6099          be looking up names inside the class.  */
6100       if (TYPE_P (new_scope)
6101           /* Since checking types for dependency can be expensive,
6102              avoid doing it if the type is already complete.  */
6103           && !COMPLETE_TYPE_P (new_scope)
6104           /* Do not try to complete dependent types.  */
6105           && !dependent_type_p (new_scope))
6106         {
6107           new_scope = complete_type (new_scope);
6108           /* If it is a typedef to current class, use the current
6109              class instead, as the typedef won't have any names inside
6110              it yet.  */
6111           if (!COMPLETE_TYPE_P (new_scope)
6112               && currently_open_class (new_scope))
6113             new_scope = TYPE_MAIN_VARIANT (new_scope);
6114         }
6115       /* Make sure we look in the right scope the next time through
6116          the loop.  */
6117       parser->scope = new_scope;
6118     }
6119
6120   /* If parsing tentatively, replace the sequence of tokens that makes
6121      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6122      token.  That way, should we re-parse the token stream, we will
6123      not have to repeat the effort required to do the parse, nor will
6124      we issue duplicate error messages.  */
6125   if (success && start)
6126     {
6127       cp_token *token;
6128
6129       token = cp_lexer_token_at (parser->lexer, start);
6130       /* Reset the contents of the START token.  */
6131       token->type = CPP_NESTED_NAME_SPECIFIER;
6132       /* Retrieve any deferred checks.  Do not pop this access checks yet
6133          so the memory will not be reclaimed during token replacing below.  */
6134       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6135       token->u.tree_check_value->value = parser->scope;
6136       token->u.tree_check_value->checks = get_deferred_access_checks ();
6137       token->u.tree_check_value->qualifying_scope =
6138         parser->qualifying_scope;
6139       token->keyword = RID_MAX;
6140
6141       /* Purge all subsequent tokens.  */
6142       cp_lexer_purge_tokens_after (parser->lexer, start);
6143     }
6144
6145   if (start)
6146     pop_to_parent_deferring_access_checks ();
6147
6148   return success ? parser->scope : NULL_TREE;
6149 }
6150
6151 /* Parse a nested-name-specifier.  See
6152    cp_parser_nested_name_specifier_opt for details.  This function
6153    behaves identically, except that it will an issue an error if no
6154    nested-name-specifier is present.  */
6155
6156 static tree
6157 cp_parser_nested_name_specifier (cp_parser *parser,
6158                                  bool typename_keyword_p,
6159                                  bool check_dependency_p,
6160                                  bool type_p,
6161                                  bool is_declaration)
6162 {
6163   tree scope;
6164
6165   /* Look for the nested-name-specifier.  */
6166   scope = cp_parser_nested_name_specifier_opt (parser,
6167                                                typename_keyword_p,
6168                                                check_dependency_p,
6169                                                type_p,
6170                                                is_declaration);
6171   /* If it was not present, issue an error message.  */
6172   if (!scope)
6173     {
6174       cp_parser_error (parser, "expected nested-name-specifier");
6175       parser->scope = NULL_TREE;
6176     }
6177
6178   return scope;
6179 }
6180
6181 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6182    this is either a class-name or a namespace-name (which corresponds
6183    to the class-or-namespace-name production in the grammar). For
6184    C++0x, it can also be a type-name that refers to an enumeration
6185    type or a simple-template-id.
6186
6187    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6188    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6189    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6190    TYPE_P is TRUE iff the next name should be taken as a class-name,
6191    even the same name is declared to be another entity in the same
6192    scope.
6193
6194    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6195    specified by the class-or-namespace-name.  If neither is found the
6196    ERROR_MARK_NODE is returned.  */
6197
6198 static tree
6199 cp_parser_qualifying_entity (cp_parser *parser,
6200                              bool typename_keyword_p,
6201                              bool template_keyword_p,
6202                              bool check_dependency_p,
6203                              bool type_p,
6204                              bool is_declaration)
6205 {
6206   tree saved_scope;
6207   tree saved_qualifying_scope;
6208   tree saved_object_scope;
6209   tree scope;
6210   bool only_class_p;
6211   bool successful_parse_p;
6212
6213   /* DR 743: decltype can appear in a nested-name-specifier.  */
6214   if (cp_lexer_next_token_is_decltype (parser->lexer))
6215     {
6216       scope = cp_parser_decltype (parser);
6217       if (TREE_CODE (scope) != ENUMERAL_TYPE
6218           && !MAYBE_CLASS_TYPE_P (scope))
6219         {
6220           cp_parser_simulate_error (parser);
6221           return error_mark_node;
6222         }
6223       if (TYPE_NAME (scope))
6224         scope = TYPE_NAME (scope);
6225       return scope;
6226     }
6227
6228   /* Before we try to parse the class-name, we must save away the
6229      current PARSER->SCOPE since cp_parser_class_name will destroy
6230      it.  */
6231   saved_scope = parser->scope;
6232   saved_qualifying_scope = parser->qualifying_scope;
6233   saved_object_scope = parser->object_scope;
6234   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
6235      there is no need to look for a namespace-name.  */
6236   only_class_p = template_keyword_p 
6237     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6238   if (!only_class_p)
6239     cp_parser_parse_tentatively (parser);
6240   scope = cp_parser_class_name (parser,
6241                                 typename_keyword_p,
6242                                 template_keyword_p,
6243                                 type_p ? class_type : none_type,
6244                                 check_dependency_p,
6245                                 /*class_head_p=*/false,
6246                                 is_declaration,
6247                                 /*enum_ok=*/cxx_dialect > cxx98);
6248   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6249   /* If that didn't work, try for a namespace-name.  */
6250   if (!only_class_p && !successful_parse_p)
6251     {
6252       /* Restore the saved scope.  */
6253       parser->scope = saved_scope;
6254       parser->qualifying_scope = saved_qualifying_scope;
6255       parser->object_scope = saved_object_scope;
6256       /* If we are not looking at an identifier followed by the scope
6257          resolution operator, then this is not part of a
6258          nested-name-specifier.  (Note that this function is only used
6259          to parse the components of a nested-name-specifier.)  */
6260       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6261           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6262         return error_mark_node;
6263       scope = cp_parser_namespace_name (parser);
6264     }
6265
6266   return scope;
6267 }
6268
6269 /* Return true if we are looking at a compound-literal, false otherwise.  */
6270
6271 static bool
6272 cp_parser_compound_literal_p (cp_parser *parser)
6273 {
6274   /* Consume the `('.  */
6275   cp_lexer_consume_token (parser->lexer);
6276
6277   cp_lexer_save_tokens (parser->lexer);
6278
6279   /* Skip tokens until the next token is a closing parenthesis.
6280      If we find the closing `)', and the next token is a `{', then
6281      we are looking at a compound-literal.  */
6282   bool compound_literal_p
6283     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6284                                               /*consume_paren=*/true)
6285        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6286   
6287   /* Roll back the tokens we skipped.  */
6288   cp_lexer_rollback_tokens (parser->lexer);
6289
6290   return compound_literal_p;
6291 }
6292
6293 /* Parse a postfix-expression.
6294
6295    postfix-expression:
6296      primary-expression
6297      postfix-expression [ expression ]
6298      postfix-expression ( expression-list [opt] )
6299      simple-type-specifier ( expression-list [opt] )
6300      typename :: [opt] nested-name-specifier identifier
6301        ( expression-list [opt] )
6302      typename :: [opt] nested-name-specifier template [opt] template-id
6303        ( expression-list [opt] )
6304      postfix-expression . template [opt] id-expression
6305      postfix-expression -> template [opt] id-expression
6306      postfix-expression . pseudo-destructor-name
6307      postfix-expression -> pseudo-destructor-name
6308      postfix-expression ++
6309      postfix-expression --
6310      dynamic_cast < type-id > ( expression )
6311      static_cast < type-id > ( expression )
6312      reinterpret_cast < type-id > ( expression )
6313      const_cast < type-id > ( expression )
6314      typeid ( expression )
6315      typeid ( type-id )
6316
6317    GNU Extension:
6318
6319    postfix-expression:
6320      ( type-id ) { initializer-list , [opt] }
6321
6322    This extension is a GNU version of the C99 compound-literal
6323    construct.  (The C99 grammar uses `type-name' instead of `type-id',
6324    but they are essentially the same concept.)
6325
6326    If ADDRESS_P is true, the postfix expression is the operand of the
6327    `&' operator.  CAST_P is true if this expression is the target of a
6328    cast.
6329
6330    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6331    class member access expressions [expr.ref].
6332
6333    Returns a representation of the expression.  */
6334
6335 static cp_expr
6336 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6337                               bool member_access_only_p, bool decltype_p,
6338                               cp_id_kind * pidk_return)
6339 {
6340   cp_token *token;
6341   location_t loc;
6342   enum rid keyword;
6343   cp_id_kind idk = CP_ID_KIND_NONE;
6344   cp_expr postfix_expression = NULL_TREE;
6345   bool is_member_access = false;
6346   int saved_in_statement = -1;
6347
6348   /* Peek at the next token.  */
6349   token = cp_lexer_peek_token (parser->lexer);
6350   loc = token->location;
6351   location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6352
6353   /* Some of the productions are determined by keywords.  */
6354   keyword = token->keyword;
6355   switch (keyword)
6356     {
6357     case RID_DYNCAST:
6358     case RID_STATCAST:
6359     case RID_REINTCAST:
6360     case RID_CONSTCAST:
6361       {
6362         tree type;
6363         cp_expr expression;
6364         const char *saved_message;
6365         bool saved_in_type_id_in_expr_p;
6366
6367         /* All of these can be handled in the same way from the point
6368            of view of parsing.  Begin by consuming the token
6369            identifying the cast.  */
6370         cp_lexer_consume_token (parser->lexer);
6371
6372         /* New types cannot be defined in the cast.  */
6373         saved_message = parser->type_definition_forbidden_message;
6374         parser->type_definition_forbidden_message
6375           = G_("types may not be defined in casts");
6376
6377         /* Look for the opening `<'.  */
6378         cp_parser_require (parser, CPP_LESS, RT_LESS);
6379         /* Parse the type to which we are casting.  */
6380         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6381         parser->in_type_id_in_expr_p = true;
6382         type = cp_parser_type_id (parser);
6383         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6384         /* Look for the closing `>'.  */
6385         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6386         /* Restore the old message.  */
6387         parser->type_definition_forbidden_message = saved_message;
6388
6389         bool saved_greater_than_is_operator_p
6390           = parser->greater_than_is_operator_p;
6391         parser->greater_than_is_operator_p = true;
6392
6393         /* And the expression which is being cast.  */
6394         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6395         expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6396         cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6397                                                    RT_CLOSE_PAREN);
6398         location_t end_loc = close_paren ?
6399           close_paren->location : UNKNOWN_LOCATION;
6400
6401         parser->greater_than_is_operator_p
6402           = saved_greater_than_is_operator_p;
6403
6404         /* Only type conversions to integral or enumeration types
6405            can be used in constant-expressions.  */
6406         if (!cast_valid_in_integral_constant_expression_p (type)
6407             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6408           {
6409             postfix_expression = error_mark_node;
6410             break;
6411           }
6412
6413         switch (keyword)
6414           {
6415           case RID_DYNCAST:
6416             postfix_expression
6417               = build_dynamic_cast (type, expression, tf_warning_or_error);
6418             break;
6419           case RID_STATCAST:
6420             postfix_expression
6421               = build_static_cast (type, expression, tf_warning_or_error);
6422             break;
6423           case RID_REINTCAST:
6424             postfix_expression
6425               = build_reinterpret_cast (type, expression, 
6426                                         tf_warning_or_error);
6427             break;
6428           case RID_CONSTCAST:
6429             postfix_expression
6430               = build_const_cast (type, expression, tf_warning_or_error);
6431             break;
6432           default:
6433             gcc_unreachable ();
6434           }
6435
6436         /* Construct a location e.g. :
6437              reinterpret_cast <int *> (expr)
6438              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6439            ranging from the start of the "*_cast" token to the final closing
6440            paren, with the caret at the start.  */
6441         location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6442         postfix_expression.set_location (cp_cast_loc);
6443       }
6444       break;
6445
6446     case RID_TYPEID:
6447       {
6448         tree type;
6449         const char *saved_message;
6450         bool saved_in_type_id_in_expr_p;
6451
6452         /* Consume the `typeid' token.  */
6453         cp_lexer_consume_token (parser->lexer);
6454         /* Look for the `(' token.  */
6455         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6456         /* Types cannot be defined in a `typeid' expression.  */
6457         saved_message = parser->type_definition_forbidden_message;
6458         parser->type_definition_forbidden_message
6459           = G_("types may not be defined in a %<typeid%> expression");
6460         /* We can't be sure yet whether we're looking at a type-id or an
6461            expression.  */
6462         cp_parser_parse_tentatively (parser);
6463         /* Try a type-id first.  */
6464         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6465         parser->in_type_id_in_expr_p = true;
6466         type = cp_parser_type_id (parser);
6467         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6468         /* Look for the `)' token.  Otherwise, we can't be sure that
6469            we're not looking at an expression: consider `typeid (int
6470            (3))', for example.  */
6471         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6472         /* If all went well, simply lookup the type-id.  */
6473         if (cp_parser_parse_definitely (parser))
6474           postfix_expression = get_typeid (type, tf_warning_or_error);
6475         /* Otherwise, fall back to the expression variant.  */
6476         else
6477           {
6478             tree expression;
6479
6480             /* Look for an expression.  */
6481             expression = cp_parser_expression (parser, & idk);
6482             /* Compute its typeid.  */
6483             postfix_expression = build_typeid (expression, tf_warning_or_error);
6484             /* Look for the `)' token.  */
6485             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6486           }
6487         /* Restore the saved message.  */
6488         parser->type_definition_forbidden_message = saved_message;
6489         /* `typeid' may not appear in an integral constant expression.  */
6490         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6491           postfix_expression = error_mark_node;
6492       }
6493       break;
6494
6495     case RID_TYPENAME:
6496       {
6497         tree type;
6498         /* The syntax permitted here is the same permitted for an
6499            elaborated-type-specifier.  */
6500         ++parser->prevent_constrained_type_specifiers;
6501         type = cp_parser_elaborated_type_specifier (parser,
6502                                                     /*is_friend=*/false,
6503                                                     /*is_declaration=*/false);
6504         --parser->prevent_constrained_type_specifiers;
6505         postfix_expression = cp_parser_functional_cast (parser, type);
6506       }
6507       break;
6508
6509     case RID_CILK_SPAWN:
6510       {
6511         location_t cilk_spawn_loc
6512           = cp_lexer_peek_token (parser->lexer)->location;
6513         cp_lexer_consume_token (parser->lexer);
6514         token = cp_lexer_peek_token (parser->lexer);
6515         if (token->type == CPP_SEMICOLON)
6516           {
6517             error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6518                       "an expression");
6519             postfix_expression = error_mark_node;
6520             break;
6521           }
6522         else if (!current_function_decl)
6523           {
6524             error_at (token->location, "%<_Cilk_spawn%> may only be used "
6525                       "inside a function");
6526             postfix_expression = error_mark_node;
6527             break;
6528           }
6529         else
6530           {
6531             /* Consecutive _Cilk_spawns are not allowed in a statement.  */
6532             saved_in_statement = parser->in_statement;
6533             parser->in_statement |= IN_CILK_SPAWN;
6534           }
6535         cfun->calls_cilk_spawn = 1;
6536         postfix_expression = 
6537           cp_parser_postfix_expression (parser, false, false, 
6538                                         false, false, &idk);
6539         if (!flag_cilkplus)
6540           {
6541             error_at (token->location, "-fcilkplus must be enabled to use"
6542                       " %<_Cilk_spawn%>");
6543             cfun->calls_cilk_spawn = 0;
6544           }
6545         else if (saved_in_statement & IN_CILK_SPAWN)
6546           {
6547             error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6548                       "are not permitted");
6549             postfix_expression = error_mark_node;
6550             cfun->calls_cilk_spawn = 0; 
6551           }
6552         else
6553           {
6554             location_t loc = postfix_expression.get_location ();
6555             postfix_expression = build_cilk_spawn (token->location, 
6556                                                    postfix_expression);
6557             /* Build a location of the form:
6558                  _Cilk_spawn expr
6559                  ~~~~~~~~~~~~^~~~
6560                with caret at the expr, ranging from the start of the
6561                _Cilk_spawn token to the end of the expression.  */
6562             location_t combined_loc =
6563               make_location (loc, cilk_spawn_loc, get_finish (loc));
6564             postfix_expression.set_location (combined_loc);
6565             if (postfix_expression != error_mark_node) 
6566               SET_EXPR_LOCATION (postfix_expression, input_location);
6567             parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6568           }
6569         break;
6570       }
6571
6572     case RID_BUILTIN_SHUFFLE:
6573       {
6574         vec<tree, va_gc> *vec;
6575         unsigned int i;
6576         tree p;
6577
6578         cp_lexer_consume_token (parser->lexer);
6579         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6580                     /*cast_p=*/false, /*allow_expansion_p=*/true,
6581                     /*non_constant_p=*/NULL);
6582         if (vec == NULL)
6583           {
6584             postfix_expression = error_mark_node;
6585             break;
6586           }
6587
6588         FOR_EACH_VEC_ELT (*vec, i, p)
6589           mark_exp_read (p);
6590
6591         if (vec->length () == 2)
6592           postfix_expression
6593             = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6594                                      tf_warning_or_error);
6595         else if (vec->length () == 3)
6596           postfix_expression
6597             = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6598                                      tf_warning_or_error);
6599         else
6600           {
6601             error_at (loc, "wrong number of arguments to "
6602                            "%<__builtin_shuffle%>");
6603             postfix_expression = error_mark_node;
6604           }
6605         break;
6606       }
6607
6608     default:
6609       {
6610         tree type;
6611
6612         /* If the next thing is a simple-type-specifier, we may be
6613            looking at a functional cast.  We could also be looking at
6614            an id-expression.  So, we try the functional cast, and if
6615            that doesn't work we fall back to the primary-expression.  */
6616         cp_parser_parse_tentatively (parser);
6617         /* Look for the simple-type-specifier.  */
6618         ++parser->prevent_constrained_type_specifiers;
6619         type = cp_parser_simple_type_specifier (parser,
6620                                                 /*decl_specs=*/NULL,
6621                                                 CP_PARSER_FLAGS_NONE);
6622         --parser->prevent_constrained_type_specifiers;
6623         /* Parse the cast itself.  */
6624         if (!cp_parser_error_occurred (parser))
6625           postfix_expression
6626             = cp_parser_functional_cast (parser, type);
6627         /* If that worked, we're done.  */
6628         if (cp_parser_parse_definitely (parser))
6629           break;
6630
6631         /* If the functional-cast didn't work out, try a
6632            compound-literal.  */
6633         if (cp_parser_allow_gnu_extensions_p (parser)
6634             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6635           {
6636             cp_expr initializer = NULL_TREE;
6637
6638             cp_parser_parse_tentatively (parser);
6639
6640             /* Avoid calling cp_parser_type_id pointlessly, see comment
6641                in cp_parser_cast_expression about c++/29234.  */
6642             if (!cp_parser_compound_literal_p (parser))
6643               cp_parser_simulate_error (parser);
6644             else
6645               {
6646                 /* Parse the type.  */
6647                 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6648                 parser->in_type_id_in_expr_p = true;
6649                 type = cp_parser_type_id (parser);
6650                 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6651                 /* Look for the `)'.  */
6652                 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6653               }
6654
6655             /* If things aren't going well, there's no need to
6656                keep going.  */
6657             if (!cp_parser_error_occurred (parser))
6658               {
6659                 bool non_constant_p;
6660                 /* Parse the brace-enclosed initializer list.  */
6661                 initializer = cp_parser_braced_list (parser,
6662                                                      &non_constant_p);
6663               }
6664             /* If that worked, we're definitely looking at a
6665                compound-literal expression.  */
6666             if (cp_parser_parse_definitely (parser))
6667               {
6668                 /* Warn the user that a compound literal is not
6669                    allowed in standard C++.  */
6670                 pedwarn (input_location, OPT_Wpedantic,
6671                          "ISO C++ forbids compound-literals");
6672                 /* For simplicity, we disallow compound literals in
6673                    constant-expressions.  We could
6674                    allow compound literals of integer type, whose
6675                    initializer was a constant, in constant
6676                    expressions.  Permitting that usage, as a further
6677                    extension, would not change the meaning of any
6678                    currently accepted programs.  (Of course, as
6679                    compound literals are not part of ISO C++, the
6680                    standard has nothing to say.)  */
6681                 if (cp_parser_non_integral_constant_expression (parser,
6682                                                                 NIC_NCC))
6683                   {
6684                     postfix_expression = error_mark_node;
6685                     break;
6686                   }
6687                 /* Form the representation of the compound-literal.  */
6688                 postfix_expression
6689                   = finish_compound_literal (type, initializer,
6690                                              tf_warning_or_error);
6691                 postfix_expression.set_location (initializer.get_location ());
6692                 break;
6693               }
6694           }
6695
6696         /* It must be a primary-expression.  */
6697         postfix_expression
6698           = cp_parser_primary_expression (parser, address_p, cast_p,
6699                                           /*template_arg_p=*/false,
6700                                           decltype_p,
6701                                           &idk);
6702       }
6703       break;
6704     }
6705
6706   /* Note that we don't need to worry about calling build_cplus_new on a
6707      class-valued CALL_EXPR in decltype when it isn't the end of the
6708      postfix-expression; unary_complex_lvalue will take care of that for
6709      all these cases.  */
6710
6711   /* Keep looping until the postfix-expression is complete.  */
6712   while (true)
6713     {
6714       if (idk == CP_ID_KIND_UNQUALIFIED
6715           && identifier_p (postfix_expression)
6716           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6717         /* It is not a Koenig lookup function call.  */
6718         postfix_expression
6719           = unqualified_name_lookup_error (postfix_expression);
6720
6721       /* Peek at the next token.  */
6722       token = cp_lexer_peek_token (parser->lexer);
6723
6724       switch (token->type)
6725         {
6726         case CPP_OPEN_SQUARE:
6727           if (cp_next_tokens_can_be_std_attribute_p (parser))
6728             {
6729               cp_parser_error (parser,
6730                                "two consecutive %<[%> shall "
6731                                "only introduce an attribute");
6732               return error_mark_node;
6733             }
6734           postfix_expression
6735             = cp_parser_postfix_open_square_expression (parser,
6736                                                         postfix_expression,
6737                                                         false,
6738                                                         decltype_p);
6739           postfix_expression.set_range (start_loc,
6740                                         postfix_expression.get_location ());
6741
6742           idk = CP_ID_KIND_NONE;
6743           is_member_access = false;
6744           break;
6745
6746         case CPP_OPEN_PAREN:
6747           /* postfix-expression ( expression-list [opt] ) */
6748           {
6749             bool koenig_p;
6750             bool is_builtin_constant_p;
6751             bool saved_integral_constant_expression_p = false;
6752             bool saved_non_integral_constant_expression_p = false;
6753             tsubst_flags_t complain = complain_flags (decltype_p);
6754             vec<tree, va_gc> *args;
6755             location_t close_paren_loc = UNKNOWN_LOCATION;
6756
6757             is_member_access = false;
6758
6759             is_builtin_constant_p
6760               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6761             if (is_builtin_constant_p)
6762               {
6763                 /* The whole point of __builtin_constant_p is to allow
6764                    non-constant expressions to appear as arguments.  */
6765                 saved_integral_constant_expression_p
6766                   = parser->integral_constant_expression_p;
6767                 saved_non_integral_constant_expression_p
6768                   = parser->non_integral_constant_expression_p;
6769                 parser->integral_constant_expression_p = false;
6770               }
6771             args = (cp_parser_parenthesized_expression_list
6772                     (parser, non_attr,
6773                      /*cast_p=*/false, /*allow_expansion_p=*/true,
6774                      /*non_constant_p=*/NULL,
6775                      /*close_paren_loc=*/&close_paren_loc));
6776             if (is_builtin_constant_p)
6777               {
6778                 parser->integral_constant_expression_p
6779                   = saved_integral_constant_expression_p;
6780                 parser->non_integral_constant_expression_p
6781                   = saved_non_integral_constant_expression_p;
6782               }
6783
6784             if (args == NULL)
6785               {
6786                 postfix_expression = error_mark_node;
6787                 break;
6788               }
6789
6790             /* Function calls are not permitted in
6791                constant-expressions.  */
6792             if (! builtin_valid_in_constant_expr_p (postfix_expression)
6793                 && cp_parser_non_integral_constant_expression (parser,
6794                                                                NIC_FUNC_CALL))
6795               {
6796                 postfix_expression = error_mark_node;
6797                 release_tree_vector (args);
6798                 break;
6799               }
6800
6801             koenig_p = false;
6802             if (idk == CP_ID_KIND_UNQUALIFIED
6803                 || idk == CP_ID_KIND_TEMPLATE_ID)
6804               {
6805                 if (identifier_p (postfix_expression))
6806                   {
6807                     if (!args->is_empty ())
6808                       {
6809                         koenig_p = true;
6810                         if (!any_type_dependent_arguments_p (args))
6811                           postfix_expression
6812                             = perform_koenig_lookup (postfix_expression, args,
6813                                                      complain);
6814                       }
6815                     else
6816                       postfix_expression
6817                         = unqualified_fn_lookup_error (postfix_expression);
6818                   }
6819                 /* We do not perform argument-dependent lookup if
6820                    normal lookup finds a non-function, in accordance
6821                    with the expected resolution of DR 218.  */
6822                 else if (!args->is_empty ()
6823                          && is_overloaded_fn (postfix_expression))
6824                   {
6825                     tree fn = get_first_fn (postfix_expression);
6826                     fn = STRIP_TEMPLATE (fn);
6827
6828                     /* Do not do argument dependent lookup if regular
6829                        lookup finds a member function or a block-scope
6830                        function declaration.  [basic.lookup.argdep]/3  */
6831                     if (!DECL_FUNCTION_MEMBER_P (fn)
6832                         && !DECL_LOCAL_FUNCTION_P (fn))
6833                       {
6834                         koenig_p = true;
6835                         if (!any_type_dependent_arguments_p (args))
6836                           postfix_expression
6837                             = perform_koenig_lookup (postfix_expression, args,
6838                                                      complain);
6839                       }
6840                   }
6841               }
6842
6843             if (warn_memset_transposed_args)
6844               {
6845                 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6846                     && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6847                     && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6848                     && vec_safe_length (args) == 3
6849                     && TREE_CODE ((*args)[2]) == INTEGER_CST
6850                     && integer_zerop ((*args)[2])
6851                     && !(TREE_CODE ((*args)[1]) == INTEGER_CST
6852                          && integer_zerop ((*args)[1])))
6853                   warning (OPT_Wmemset_transposed_args,
6854                            "%<memset%> used with constant zero length "
6855                            "parameter; this could be due to transposed "
6856                            "parameters");
6857               }
6858
6859             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6860               {
6861                 tree instance = TREE_OPERAND (postfix_expression, 0);
6862                 tree fn = TREE_OPERAND (postfix_expression, 1);
6863
6864                 if (processing_template_decl
6865                     && (type_dependent_expression_p (instance)
6866                         || (!BASELINK_P (fn)
6867                             && TREE_CODE (fn) != FIELD_DECL)
6868                         || type_dependent_expression_p (fn)
6869                         || any_type_dependent_arguments_p (args)))
6870                   {
6871                     postfix_expression
6872                       = build_nt_call_vec (postfix_expression, args);
6873                     release_tree_vector (args);
6874                     break;
6875                   }
6876
6877                 if (BASELINK_P (fn))
6878                   {
6879                   postfix_expression
6880                     = (build_new_method_call
6881                        (instance, fn, &args, NULL_TREE,
6882                         (idk == CP_ID_KIND_QUALIFIED
6883                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6884                          : LOOKUP_NORMAL),
6885                         /*fn_p=*/NULL,
6886                         complain));
6887                   }
6888                 else
6889                   postfix_expression
6890                     = finish_call_expr (postfix_expression, &args,
6891                                         /*disallow_virtual=*/false,
6892                                         /*koenig_p=*/false,
6893                                         complain);
6894               }
6895             else if (TREE_CODE (postfix_expression) == OFFSET_REF
6896                      || TREE_CODE (postfix_expression) == MEMBER_REF
6897                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6898               postfix_expression = (build_offset_ref_call_from_tree
6899                                     (postfix_expression, &args,
6900                                      complain));
6901             else if (idk == CP_ID_KIND_QUALIFIED)
6902               /* A call to a static class member, or a namespace-scope
6903                  function.  */
6904               postfix_expression
6905                 = finish_call_expr (postfix_expression, &args,
6906                                     /*disallow_virtual=*/true,
6907                                     koenig_p,
6908                                     complain);
6909             else
6910               /* All other function calls.  */
6911               postfix_expression
6912                 = finish_call_expr (postfix_expression, &args,
6913                                     /*disallow_virtual=*/false,
6914                                     koenig_p,
6915                                     complain);
6916
6917             if (close_paren_loc != UNKNOWN_LOCATION)
6918               {
6919                 location_t combined_loc = make_location (token->location,
6920                                                          start_loc,
6921                                                          close_paren_loc);
6922                 postfix_expression.set_location (combined_loc);
6923               }
6924
6925             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
6926             idk = CP_ID_KIND_NONE;
6927
6928             release_tree_vector (args);
6929           }
6930           break;
6931
6932         case CPP_DOT:
6933         case CPP_DEREF:
6934           /* postfix-expression . template [opt] id-expression
6935              postfix-expression . pseudo-destructor-name
6936              postfix-expression -> template [opt] id-expression
6937              postfix-expression -> pseudo-destructor-name */
6938
6939           /* Consume the `.' or `->' operator.  */
6940           cp_lexer_consume_token (parser->lexer);
6941
6942           postfix_expression
6943             = cp_parser_postfix_dot_deref_expression (parser, token->type,
6944                                                       postfix_expression,
6945                                                       false, &idk, loc);
6946
6947           is_member_access = true;
6948           break;
6949
6950         case CPP_PLUS_PLUS:
6951           /* postfix-expression ++  */
6952           /* Consume the `++' token.  */
6953           cp_lexer_consume_token (parser->lexer);
6954           /* Generate a representation for the complete expression.  */
6955           postfix_expression
6956             = finish_increment_expr (postfix_expression,
6957                                      POSTINCREMENT_EXPR);
6958           /* Increments may not appear in constant-expressions.  */
6959           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6960             postfix_expression = error_mark_node;
6961           idk = CP_ID_KIND_NONE;
6962           is_member_access = false;
6963           break;
6964
6965         case CPP_MINUS_MINUS:
6966           /* postfix-expression -- */
6967           /* Consume the `--' token.  */
6968           cp_lexer_consume_token (parser->lexer);
6969           /* Generate a representation for the complete expression.  */
6970           postfix_expression
6971             = finish_increment_expr (postfix_expression,
6972                                      POSTDECREMENT_EXPR);
6973           /* Decrements may not appear in constant-expressions.  */
6974           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6975             postfix_expression = error_mark_node;
6976           idk = CP_ID_KIND_NONE;
6977           is_member_access = false;
6978           break;
6979
6980         default:
6981           if (pidk_return != NULL)
6982             * pidk_return = idk;
6983           if (member_access_only_p)
6984             return is_member_access
6985               ? postfix_expression
6986               : cp_expr (error_mark_node);
6987           else
6988             return postfix_expression;
6989         }
6990     }
6991
6992   /* We should never get here.  */
6993   gcc_unreachable ();
6994   return error_mark_node;
6995 }
6996
6997 /* This function parses Cilk Plus array notations.  If a normal array expr. is
6998    parsed then the array index is passed back to the caller through *INIT_INDEX 
6999    and the function returns a NULL_TREE.  If array notation expr. is parsed, 
7000    then *INIT_INDEX is ignored by the caller and the function returns 
7001    a tree of type ARRAY_NOTATION_REF.  If some error occurred it returns 
7002    error_mark_node.  */
7003
7004 static tree
7005 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7006                           tree array_value)
7007 {
7008   cp_token *token = NULL;
7009   tree length_index, stride = NULL_TREE, value_tree, array_type;
7010   if (!array_value || array_value == error_mark_node)
7011     {
7012       cp_parser_skip_to_end_of_statement (parser);
7013       return error_mark_node;
7014     }
7015
7016   array_type = TREE_TYPE (array_value);
7017   
7018   bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7019   parser->colon_corrects_to_scope_p = false;
7020   token = cp_lexer_peek_token (parser->lexer);
7021   
7022   if (!token)
7023     {
7024       cp_parser_error (parser, "expected %<:%> or numeral");
7025       return error_mark_node;
7026     }
7027   else if (token->type == CPP_COLON)
7028     {
7029       /* Consume the ':'.  */
7030       cp_lexer_consume_token (parser->lexer);
7031       
7032       /* If we are here, then we have a case like this A[:].  */
7033       if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7034         {
7035           cp_parser_error (parser, "expected %<]%>");
7036           cp_parser_skip_to_end_of_statement (parser);
7037           return error_mark_node;
7038         }
7039       *init_index = NULL_TREE;
7040       stride = NULL_TREE;
7041       length_index = NULL_TREE;
7042     }
7043   else
7044     {
7045       /* If we are here, then there are three valid possibilities:
7046          1. ARRAY [ EXP ]
7047          2. ARRAY [ EXP : EXP ]
7048          3. ARRAY [ EXP : EXP : EXP ]  */
7049
7050       *init_index = cp_parser_expression (parser);
7051       if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7052         {  
7053           /* This indicates that we have a normal array expression.  */
7054           parser->colon_corrects_to_scope_p = saved_colon_corrects;
7055           return NULL_TREE;
7056         }
7057       
7058       /* Consume the ':'.  */
7059       cp_lexer_consume_token (parser->lexer);
7060       length_index = cp_parser_expression (parser);
7061       if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7062         {
7063           cp_lexer_consume_token (parser->lexer);
7064           stride = cp_parser_expression (parser);
7065         }
7066     }
7067   parser->colon_corrects_to_scope_p = saved_colon_corrects;
7068
7069   if (*init_index == error_mark_node || length_index == error_mark_node
7070       || stride == error_mark_node || array_type == error_mark_node)
7071     {
7072       if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7073         cp_lexer_consume_token (parser->lexer);
7074       return error_mark_node;
7075     }
7076   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7077
7078   value_tree = build_array_notation_ref (loc, array_value, *init_index, 
7079                                          length_index, stride, array_type);
7080   return value_tree;
7081 }
7082
7083 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7084    by cp_parser_builtin_offsetof.  We're looking for
7085
7086      postfix-expression [ expression ]
7087      postfix-expression [ braced-init-list ] (C++11)
7088
7089    FOR_OFFSETOF is set if we're being called in that context, which
7090    changes how we deal with integer constant expressions.  */
7091
7092 static tree
7093 cp_parser_postfix_open_square_expression (cp_parser *parser,
7094                                           tree postfix_expression,
7095                                           bool for_offsetof,
7096                                           bool decltype_p)
7097 {
7098   tree index = NULL_TREE;
7099   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7100   bool saved_greater_than_is_operator_p;
7101
7102   /* Consume the `[' token.  */
7103   cp_lexer_consume_token (parser->lexer);
7104
7105   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7106   parser->greater_than_is_operator_p = true;
7107
7108   /* Parse the index expression.  */
7109   /* ??? For offsetof, there is a question of what to allow here.  If
7110      offsetof is not being used in an integral constant expression context,
7111      then we *could* get the right answer by computing the value at runtime.
7112      If we are in an integral constant expression context, then we might
7113      could accept any constant expression; hard to say without analysis.
7114      Rather than open the barn door too wide right away, allow only integer
7115      constant expressions here.  */
7116   if (for_offsetof)
7117     index = cp_parser_constant_expression (parser);
7118   else
7119     {
7120       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7121         {
7122           bool expr_nonconst_p;
7123           cp_lexer_set_source_position (parser->lexer);
7124           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7125           index = cp_parser_braced_list (parser, &expr_nonconst_p);
7126           if (flag_cilkplus
7127               && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7128             {
7129               error_at (cp_lexer_peek_token (parser->lexer)->location,
7130                         "braced list index is not allowed with array "
7131                         "notation");
7132               cp_parser_skip_to_end_of_statement (parser);
7133               return error_mark_node;
7134             }
7135         }
7136       else if (flag_cilkplus)
7137         {
7138           /* Here are have these two options:
7139              ARRAY[EXP : EXP]        - Array notation expr with default
7140              stride of 1.
7141              ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7142              stride.  */
7143           tree an_exp = cp_parser_array_notation (loc, parser, &index, 
7144                                                   postfix_expression);
7145           if (an_exp)
7146             return an_exp;
7147         }
7148       else
7149         index = cp_parser_expression (parser);
7150     }
7151
7152   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7153
7154   /* Look for the closing `]'.  */
7155   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7156
7157   /* Build the ARRAY_REF.  */
7158   postfix_expression = grok_array_decl (loc, postfix_expression,
7159                                         index, decltype_p);
7160
7161   /* When not doing offsetof, array references are not permitted in
7162      constant-expressions.  */
7163   if (!for_offsetof
7164       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7165     postfix_expression = error_mark_node;
7166
7167   return postfix_expression;
7168 }
7169
7170 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7171    by cp_parser_builtin_offsetof.  We're looking for
7172
7173      postfix-expression . template [opt] id-expression
7174      postfix-expression . pseudo-destructor-name
7175      postfix-expression -> template [opt] id-expression
7176      postfix-expression -> pseudo-destructor-name
7177
7178    FOR_OFFSETOF is set if we're being called in that context.  That sorta
7179    limits what of the above we'll actually accept, but nevermind.
7180    TOKEN_TYPE is the "." or "->" token, which will already have been
7181    removed from the stream.  */
7182
7183 static tree
7184 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7185                                         enum cpp_ttype token_type,
7186                                         cp_expr postfix_expression,
7187                                         bool for_offsetof, cp_id_kind *idk,
7188                                         location_t location)
7189 {
7190   tree name;
7191   bool dependent_p;
7192   bool pseudo_destructor_p;
7193   tree scope = NULL_TREE;
7194   location_t start_loc = postfix_expression.get_start ();
7195
7196   /* If this is a `->' operator, dereference the pointer.  */
7197   if (token_type == CPP_DEREF)
7198     postfix_expression = build_x_arrow (location, postfix_expression,
7199                                         tf_warning_or_error);
7200   /* Check to see whether or not the expression is type-dependent.  */
7201   dependent_p = type_dependent_expression_p (postfix_expression);
7202   /* The identifier following the `->' or `.' is not qualified.  */
7203   parser->scope = NULL_TREE;
7204   parser->qualifying_scope = NULL_TREE;
7205   parser->object_scope = NULL_TREE;
7206   *idk = CP_ID_KIND_NONE;
7207
7208   /* Enter the scope corresponding to the type of the object
7209      given by the POSTFIX_EXPRESSION.  */
7210   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7211     {
7212       scope = TREE_TYPE (postfix_expression);
7213       /* According to the standard, no expression should ever have
7214          reference type.  Unfortunately, we do not currently match
7215          the standard in this respect in that our internal representation
7216          of an expression may have reference type even when the standard
7217          says it does not.  Therefore, we have to manually obtain the
7218          underlying type here.  */
7219       scope = non_reference (scope);
7220       /* The type of the POSTFIX_EXPRESSION must be complete.  */
7221       if (scope == unknown_type_node)
7222         {
7223           error_at (location, "%qE does not have class type",
7224                     postfix_expression.get_value ());
7225           scope = NULL_TREE;
7226         }
7227       /* Unlike the object expression in other contexts, *this is not
7228          required to be of complete type for purposes of class member
7229          access (5.2.5) outside the member function body.  */
7230       else if (postfix_expression != current_class_ref
7231                && !(processing_template_decl && scope == current_class_type))
7232         scope = complete_type_or_else (scope, NULL_TREE);
7233       /* Let the name lookup machinery know that we are processing a
7234          class member access expression.  */
7235       parser->context->object_type = scope;
7236       /* If something went wrong, we want to be able to discern that case,
7237          as opposed to the case where there was no SCOPE due to the type
7238          of expression being dependent.  */
7239       if (!scope)
7240         scope = error_mark_node;
7241       /* If the SCOPE was erroneous, make the various semantic analysis
7242          functions exit quickly -- and without issuing additional error
7243          messages.  */
7244       if (scope == error_mark_node)
7245         postfix_expression = error_mark_node;
7246     }
7247   else
7248     /* Tell cp_parser_lookup_name that there was an object, even though it's
7249        type-dependent.  */
7250     parser->context->object_type = unknown_type_node;
7251
7252   /* Assume this expression is not a pseudo-destructor access.  */
7253   pseudo_destructor_p = false;
7254
7255   /* If the SCOPE is a scalar type, then, if this is a valid program,
7256      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
7257      is type dependent, it can be pseudo-destructor-name or something else.
7258      Try to parse it as pseudo-destructor-name first.  */
7259   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7260     {
7261       tree s;
7262       tree type;
7263
7264       cp_parser_parse_tentatively (parser);
7265       /* Parse the pseudo-destructor-name.  */
7266       s = NULL_TREE;
7267       cp_parser_pseudo_destructor_name (parser, postfix_expression,
7268                                         &s, &type);
7269       if (dependent_p
7270           && (cp_parser_error_occurred (parser)
7271               || !SCALAR_TYPE_P (type)))
7272         cp_parser_abort_tentative_parse (parser);
7273       else if (cp_parser_parse_definitely (parser))
7274         {
7275           pseudo_destructor_p = true;
7276           postfix_expression
7277             = finish_pseudo_destructor_expr (postfix_expression,
7278                                              s, type, location);
7279         }
7280     }
7281
7282   if (!pseudo_destructor_p)
7283     {
7284       /* If the SCOPE is not a scalar type, we are looking at an
7285          ordinary class member access expression, rather than a
7286          pseudo-destructor-name.  */
7287       bool template_p;
7288       cp_token *token = cp_lexer_peek_token (parser->lexer);
7289       /* Parse the id-expression.  */
7290       name = (cp_parser_id_expression
7291               (parser,
7292                cp_parser_optional_template_keyword (parser),
7293                /*check_dependency_p=*/true,
7294                &template_p,
7295                /*declarator_p=*/false,
7296                /*optional_p=*/false));
7297       /* In general, build a SCOPE_REF if the member name is qualified.
7298          However, if the name was not dependent and has already been
7299          resolved; there is no need to build the SCOPE_REF.  For example;
7300
7301              struct X { void f(); };
7302              template <typename T> void f(T* t) { t->X::f(); }
7303
7304          Even though "t" is dependent, "X::f" is not and has been resolved
7305          to a BASELINK; there is no need to include scope information.  */
7306
7307       /* But we do need to remember that there was an explicit scope for
7308          virtual function calls.  */
7309       if (parser->scope)
7310         *idk = CP_ID_KIND_QUALIFIED;
7311
7312       /* If the name is a template-id that names a type, we will get a
7313          TYPE_DECL here.  That is invalid code.  */
7314       if (TREE_CODE (name) == TYPE_DECL)
7315         {
7316           error_at (token->location, "invalid use of %qD", name);
7317           postfix_expression = error_mark_node;
7318         }
7319       else
7320         {
7321           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7322             {
7323               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7324                 {
7325                   error_at (token->location, "%<%D::%D%> is not a class member",
7326                             parser->scope, name);
7327                   postfix_expression = error_mark_node;
7328                 }
7329               else
7330                 name = build_qualified_name (/*type=*/NULL_TREE,
7331                                              parser->scope,
7332                                              name,
7333                                              template_p);
7334               parser->scope = NULL_TREE;
7335               parser->qualifying_scope = NULL_TREE;
7336               parser->object_scope = NULL_TREE;
7337             }
7338           if (parser->scope && name && BASELINK_P (name))
7339             adjust_result_of_qualified_name_lookup
7340               (name, parser->scope, scope);
7341           postfix_expression
7342             = finish_class_member_access_expr (postfix_expression, name,
7343                                                template_p, 
7344                                                tf_warning_or_error);
7345           /* Build a location e.g.:
7346                ptr->access_expr
7347                ~~~^~~~~~~~~~~~~
7348              where the caret is at the deref token, ranging from
7349              the start of postfix_expression to the end of the access expr.  */
7350           location_t end_loc
7351             = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7352           location_t combined_loc
7353             = make_location (input_location, start_loc, end_loc);
7354           protected_set_expr_location (postfix_expression, combined_loc);
7355         }
7356     }
7357
7358   /* We no longer need to look up names in the scope of the object on
7359      the left-hand side of the `.' or `->' operator.  */
7360   parser->context->object_type = NULL_TREE;
7361
7362   /* Outside of offsetof, these operators may not appear in
7363      constant-expressions.  */
7364   if (!for_offsetof
7365       && (cp_parser_non_integral_constant_expression
7366           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7367     postfix_expression = error_mark_node;
7368
7369   return postfix_expression;
7370 }
7371
7372 /* Parse a parenthesized expression-list.
7373
7374    expression-list:
7375      assignment-expression
7376      expression-list, assignment-expression
7377
7378    attribute-list:
7379      expression-list
7380      identifier
7381      identifier, expression-list
7382
7383    CAST_P is true if this expression is the target of a cast.
7384
7385    ALLOW_EXPANSION_P is true if this expression allows expansion of an
7386    argument pack.
7387
7388    Returns a vector of trees.  Each element is a representation of an
7389    assignment-expression.  NULL is returned if the ( and or ) are
7390    missing.  An empty, but allocated, vector is returned on no
7391    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
7392    if we are parsing an attribute list for an attribute that wants a
7393    plain identifier argument, normal_attr for an attribute that wants
7394    an expression, or non_attr if we aren't parsing an attribute list.  If
7395    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7396    not all of the expressions in the list were constant.
7397    If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7398    will be written to with the location of the closing parenthesis.  If
7399    an error occurs, it may or may not be written to.  */
7400
7401 static vec<tree, va_gc> *
7402 cp_parser_parenthesized_expression_list (cp_parser* parser,
7403                                          int is_attribute_list,
7404                                          bool cast_p,
7405                                          bool allow_expansion_p,
7406                                          bool *non_constant_p,
7407                                          location_t *close_paren_loc)
7408 {
7409   vec<tree, va_gc> *expression_list;
7410   bool fold_expr_p = is_attribute_list != non_attr;
7411   tree identifier = NULL_TREE;
7412   bool saved_greater_than_is_operator_p;
7413
7414   /* Assume all the expressions will be constant.  */
7415   if (non_constant_p)
7416     *non_constant_p = false;
7417
7418   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7419     return NULL;
7420
7421   expression_list = make_tree_vector ();
7422
7423   /* Within a parenthesized expression, a `>' token is always
7424      the greater-than operator.  */
7425   saved_greater_than_is_operator_p
7426     = parser->greater_than_is_operator_p;
7427   parser->greater_than_is_operator_p = true;
7428
7429   /* Consume expressions until there are no more.  */
7430   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7431     while (true)
7432       {
7433         tree expr;
7434
7435         /* At the beginning of attribute lists, check to see if the
7436            next token is an identifier.  */
7437         if (is_attribute_list == id_attr
7438             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7439           {
7440             cp_token *token;
7441
7442             /* Consume the identifier.  */
7443             token = cp_lexer_consume_token (parser->lexer);
7444             /* Save the identifier.  */
7445             identifier = token->u.value;
7446           }
7447         else
7448           {
7449             bool expr_non_constant_p;
7450
7451             /* Parse the next assignment-expression.  */
7452             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7453               {
7454                 /* A braced-init-list.  */
7455                 cp_lexer_set_source_position (parser->lexer);
7456                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7457                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7458                 if (non_constant_p && expr_non_constant_p)
7459                   *non_constant_p = true;
7460               }
7461             else if (non_constant_p)
7462               {
7463                 expr = (cp_parser_constant_expression
7464                         (parser, /*allow_non_constant_p=*/true,
7465                          &expr_non_constant_p));
7466                 if (expr_non_constant_p)
7467                   *non_constant_p = true;
7468               }
7469             else
7470               expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7471                                                       cast_p);
7472
7473             if (fold_expr_p)
7474               expr = instantiate_non_dependent_expr (expr);
7475
7476             /* If we have an ellipsis, then this is an expression
7477                expansion.  */
7478             if (allow_expansion_p
7479                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7480               {
7481                 /* Consume the `...'.  */
7482                 cp_lexer_consume_token (parser->lexer);
7483
7484                 /* Build the argument pack.  */
7485                 expr = make_pack_expansion (expr);
7486               }
7487
7488              /* Add it to the list.  We add error_mark_node
7489                 expressions to the list, so that we can still tell if
7490                 the correct form for a parenthesized expression-list
7491                 is found. That gives better errors.  */
7492             vec_safe_push (expression_list, expr);
7493
7494             if (expr == error_mark_node)
7495               goto skip_comma;
7496           }
7497
7498         /* After the first item, attribute lists look the same as
7499            expression lists.  */
7500         is_attribute_list = non_attr;
7501
7502       get_comma:;
7503         /* If the next token isn't a `,', then we are done.  */
7504         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7505           break;
7506
7507         /* Otherwise, consume the `,' and keep going.  */
7508         cp_lexer_consume_token (parser->lexer);
7509       }
7510
7511   if (close_paren_loc)
7512     *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7513
7514   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7515     {
7516       int ending;
7517
7518     skip_comma:;
7519       /* We try and resync to an unnested comma, as that will give the
7520          user better diagnostics.  */
7521       ending = cp_parser_skip_to_closing_parenthesis (parser,
7522                                                       /*recovering=*/true,
7523                                                       /*or_comma=*/true,
7524                                                       /*consume_paren=*/true);
7525       if (ending < 0)
7526         goto get_comma;
7527       if (!ending)
7528         {
7529           parser->greater_than_is_operator_p
7530             = saved_greater_than_is_operator_p;
7531           return NULL;
7532         }
7533     }
7534
7535   parser->greater_than_is_operator_p
7536     = saved_greater_than_is_operator_p;
7537
7538   if (identifier)
7539     vec_safe_insert (expression_list, 0, identifier);
7540
7541   return expression_list;
7542 }
7543
7544 /* Parse a pseudo-destructor-name.
7545
7546    pseudo-destructor-name:
7547      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7548      :: [opt] nested-name-specifier template template-id :: ~ type-name
7549      :: [opt] nested-name-specifier [opt] ~ type-name
7550
7551    If either of the first two productions is used, sets *SCOPE to the
7552    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7553    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7554    or ERROR_MARK_NODE if the parse fails.  */
7555
7556 static void
7557 cp_parser_pseudo_destructor_name (cp_parser* parser,
7558                                   tree object,
7559                                   tree* scope,
7560                                   tree* type)
7561 {
7562   bool nested_name_specifier_p;
7563
7564   /* Handle ~auto.  */
7565   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7566       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7567       && !type_dependent_expression_p (object))
7568     {
7569       if (cxx_dialect < cxx14)
7570         pedwarn (input_location, 0,
7571                  "%<~auto%> only available with "
7572                  "-std=c++14 or -std=gnu++14");
7573       cp_lexer_consume_token (parser->lexer);
7574       cp_lexer_consume_token (parser->lexer);
7575       *scope = NULL_TREE;
7576       *type = TREE_TYPE (object);
7577       return;
7578     }
7579
7580   /* Assume that things will not work out.  */
7581   *type = error_mark_node;
7582
7583   /* Look for the optional `::' operator.  */
7584   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7585   /* Look for the optional nested-name-specifier.  */
7586   nested_name_specifier_p
7587     = (cp_parser_nested_name_specifier_opt (parser,
7588                                             /*typename_keyword_p=*/false,
7589                                             /*check_dependency_p=*/true,
7590                                             /*type_p=*/false,
7591                                             /*is_declaration=*/false)
7592        != NULL_TREE);
7593   /* Now, if we saw a nested-name-specifier, we might be doing the
7594      second production.  */
7595   if (nested_name_specifier_p
7596       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7597     {
7598       /* Consume the `template' keyword.  */
7599       cp_lexer_consume_token (parser->lexer);
7600       /* Parse the template-id.  */
7601       cp_parser_template_id (parser,
7602                              /*template_keyword_p=*/true,
7603                              /*check_dependency_p=*/false,
7604                              class_type,
7605                              /*is_declaration=*/true);
7606       /* Look for the `::' token.  */
7607       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7608     }
7609   /* If the next token is not a `~', then there might be some
7610      additional qualification.  */
7611   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7612     {
7613       /* At this point, we're looking for "type-name :: ~".  The type-name
7614          must not be a class-name, since this is a pseudo-destructor.  So,
7615          it must be either an enum-name, or a typedef-name -- both of which
7616          are just identifiers.  So, we peek ahead to check that the "::"
7617          and "~" tokens are present; if they are not, then we can avoid
7618          calling type_name.  */
7619       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7620           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7621           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7622         {
7623           cp_parser_error (parser, "non-scalar type");
7624           return;
7625         }
7626
7627       /* Look for the type-name.  */
7628       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7629       if (*scope == error_mark_node)
7630         return;
7631
7632       /* Look for the `::' token.  */
7633       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7634     }
7635   else
7636     *scope = NULL_TREE;
7637
7638   /* Look for the `~'.  */
7639   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7640
7641   /* Once we see the ~, this has to be a pseudo-destructor.  */
7642   if (!processing_template_decl && !cp_parser_error_occurred (parser))
7643     cp_parser_commit_to_topmost_tentative_parse (parser);
7644
7645   /* Look for the type-name again.  We are not responsible for
7646      checking that it matches the first type-name.  */
7647   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7648 }
7649
7650 /* Parse a unary-expression.
7651
7652    unary-expression:
7653      postfix-expression
7654      ++ cast-expression
7655      -- cast-expression
7656      unary-operator cast-expression
7657      sizeof unary-expression
7658      sizeof ( type-id )
7659      alignof ( type-id )  [C++0x]
7660      new-expression
7661      delete-expression
7662
7663    GNU Extensions:
7664
7665    unary-expression:
7666      __extension__ cast-expression
7667      __alignof__ unary-expression
7668      __alignof__ ( type-id )
7669      alignof unary-expression  [C++0x]
7670      __real__ cast-expression
7671      __imag__ cast-expression
7672      && identifier
7673      sizeof ( type-id ) { initializer-list , [opt] }
7674      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7675      __alignof__ ( type-id ) { initializer-list , [opt] }
7676
7677    ADDRESS_P is true iff the unary-expression is appearing as the
7678    operand of the `&' operator.   CAST_P is true if this expression is
7679    the target of a cast.
7680
7681    Returns a representation of the expression.  */
7682
7683 static cp_expr
7684 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7685                             bool address_p, bool cast_p, bool decltype_p)
7686 {
7687   cp_token *token;
7688   enum tree_code unary_operator;
7689
7690   /* Peek at the next token.  */
7691   token = cp_lexer_peek_token (parser->lexer);
7692   /* Some keywords give away the kind of expression.  */
7693   if (token->type == CPP_KEYWORD)
7694     {
7695       enum rid keyword = token->keyword;
7696
7697       switch (keyword)
7698         {
7699         case RID_ALIGNOF:
7700         case RID_SIZEOF:
7701           {
7702             tree operand, ret;
7703             enum tree_code op;
7704             location_t first_loc;
7705
7706             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7707             /* Consume the token.  */
7708             cp_lexer_consume_token (parser->lexer);
7709             first_loc = cp_lexer_peek_token (parser->lexer)->location;
7710             /* Parse the operand.  */
7711             operand = cp_parser_sizeof_operand (parser, keyword);
7712
7713             if (TYPE_P (operand))
7714               ret = cxx_sizeof_or_alignof_type (operand, op, true);
7715             else
7716               {
7717                 /* ISO C++ defines alignof only with types, not with
7718                    expressions. So pedwarn if alignof is used with a non-
7719                    type expression. However, __alignof__ is ok.  */
7720                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7721                   pedwarn (token->location, OPT_Wpedantic,
7722                            "ISO C++ does not allow %<alignof%> "
7723                            "with a non-type");
7724
7725                 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7726               }
7727             /* For SIZEOF_EXPR, just issue diagnostics, but keep
7728                SIZEOF_EXPR with the original operand.  */
7729             if (op == SIZEOF_EXPR && ret != error_mark_node)
7730               {
7731                 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7732                   {
7733                     if (!processing_template_decl && TYPE_P (operand))
7734                       {
7735                         ret = build_min (SIZEOF_EXPR, size_type_node,
7736                                          build1 (NOP_EXPR, operand,
7737                                                  error_mark_node));
7738                         SIZEOF_EXPR_TYPE_P (ret) = 1;
7739                       }
7740                     else
7741                       ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7742                     TREE_SIDE_EFFECTS (ret) = 0;
7743                     TREE_READONLY (ret) = 1;
7744                   }
7745                 SET_EXPR_LOCATION (ret, first_loc);
7746               }
7747             return ret;
7748           }
7749
7750         case RID_NEW:
7751           return cp_parser_new_expression (parser);
7752
7753         case RID_DELETE:
7754           return cp_parser_delete_expression (parser);
7755
7756         case RID_EXTENSION:
7757           {
7758             /* The saved value of the PEDANTIC flag.  */
7759             int saved_pedantic;
7760             tree expr;
7761
7762             /* Save away the PEDANTIC flag.  */
7763             cp_parser_extension_opt (parser, &saved_pedantic);
7764             /* Parse the cast-expression.  */
7765             expr = cp_parser_simple_cast_expression (parser);
7766             /* Restore the PEDANTIC flag.  */
7767             pedantic = saved_pedantic;
7768
7769             return expr;
7770           }
7771
7772         case RID_REALPART:
7773         case RID_IMAGPART:
7774           {
7775             tree expression;
7776
7777             /* Consume the `__real__' or `__imag__' token.  */
7778             cp_lexer_consume_token (parser->lexer);
7779             /* Parse the cast-expression.  */
7780             expression = cp_parser_simple_cast_expression (parser);
7781             /* Create the complete representation.  */
7782             return build_x_unary_op (token->location,
7783                                      (keyword == RID_REALPART
7784                                       ? REALPART_EXPR : IMAGPART_EXPR),
7785                                      expression,
7786                                      tf_warning_or_error);
7787           }
7788           break;
7789
7790         case RID_TRANSACTION_ATOMIC:
7791         case RID_TRANSACTION_RELAXED:
7792           return cp_parser_transaction_expression (parser, keyword);
7793
7794         case RID_NOEXCEPT:
7795           {
7796             tree expr;
7797             const char *saved_message;
7798             bool saved_integral_constant_expression_p;
7799             bool saved_non_integral_constant_expression_p;
7800             bool saved_greater_than_is_operator_p;
7801
7802             cp_lexer_consume_token (parser->lexer);
7803             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7804
7805             saved_message = parser->type_definition_forbidden_message;
7806             parser->type_definition_forbidden_message
7807               = G_("types may not be defined in %<noexcept%> expressions");
7808
7809             saved_integral_constant_expression_p
7810               = parser->integral_constant_expression_p;
7811             saved_non_integral_constant_expression_p
7812               = parser->non_integral_constant_expression_p;
7813             parser->integral_constant_expression_p = false;
7814
7815             saved_greater_than_is_operator_p
7816               = parser->greater_than_is_operator_p;
7817             parser->greater_than_is_operator_p = true;
7818
7819             ++cp_unevaluated_operand;
7820             ++c_inhibit_evaluation_warnings;
7821             ++cp_noexcept_operand;
7822             expr = cp_parser_expression (parser);
7823             --cp_noexcept_operand;
7824             --c_inhibit_evaluation_warnings;
7825             --cp_unevaluated_operand;
7826
7827             parser->greater_than_is_operator_p
7828               = saved_greater_than_is_operator_p;
7829
7830             parser->integral_constant_expression_p
7831               = saved_integral_constant_expression_p;
7832             parser->non_integral_constant_expression_p
7833               = saved_non_integral_constant_expression_p;
7834
7835             parser->type_definition_forbidden_message = saved_message;
7836
7837             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7838             return finish_noexcept_expr (expr, tf_warning_or_error);
7839           }
7840
7841         default:
7842           break;
7843         }
7844     }
7845
7846   /* Look for the `:: new' and `:: delete', which also signal the
7847      beginning of a new-expression, or delete-expression,
7848      respectively.  If the next token is `::', then it might be one of
7849      these.  */
7850   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7851     {
7852       enum rid keyword;
7853
7854       /* See if the token after the `::' is one of the keywords in
7855          which we're interested.  */
7856       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7857       /* If it's `new', we have a new-expression.  */
7858       if (keyword == RID_NEW)
7859         return cp_parser_new_expression (parser);
7860       /* Similarly, for `delete'.  */
7861       else if (keyword == RID_DELETE)
7862         return cp_parser_delete_expression (parser);
7863     }
7864
7865   /* Look for a unary operator.  */
7866   unary_operator = cp_parser_unary_operator (token);
7867   /* The `++' and `--' operators can be handled similarly, even though
7868      they are not technically unary-operators in the grammar.  */
7869   if (unary_operator == ERROR_MARK)
7870     {
7871       if (token->type == CPP_PLUS_PLUS)
7872         unary_operator = PREINCREMENT_EXPR;
7873       else if (token->type == CPP_MINUS_MINUS)
7874         unary_operator = PREDECREMENT_EXPR;
7875       /* Handle the GNU address-of-label extension.  */
7876       else if (cp_parser_allow_gnu_extensions_p (parser)
7877                && token->type == CPP_AND_AND)
7878         {
7879           tree identifier;
7880           tree expression;
7881           location_t start_loc = token->location;
7882
7883           /* Consume the '&&' token.  */
7884           cp_lexer_consume_token (parser->lexer);
7885           /* Look for the identifier.  */
7886           location_t finish_loc
7887             = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7888           identifier = cp_parser_identifier (parser);
7889           /* Construct a location of the form:
7890                &&label
7891                ^~~~~~~
7892              with caret==start at the "&&", finish at the end of the label.  */
7893           location_t combined_loc
7894             = make_location (start_loc, start_loc, finish_loc);
7895           /* Create an expression representing the address.  */
7896           expression = finish_label_address_expr (identifier, combined_loc);
7897           if (cp_parser_non_integral_constant_expression (parser,
7898                                                           NIC_ADDR_LABEL))
7899             expression = error_mark_node;
7900           return expression;
7901         }
7902     }
7903   if (unary_operator != ERROR_MARK)
7904     {
7905       cp_expr cast_expression;
7906       cp_expr expression = error_mark_node;
7907       non_integral_constant non_constant_p = NIC_NONE;
7908       location_t loc = token->location;
7909       tsubst_flags_t complain = complain_flags (decltype_p);
7910
7911       /* Consume the operator token.  */
7912       token = cp_lexer_consume_token (parser->lexer);
7913       enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7914
7915       /* Parse the cast-expression.  */
7916       cast_expression
7917         = cp_parser_cast_expression (parser,
7918                                      unary_operator == ADDR_EXPR,
7919                                      /*cast_p=*/false,
7920                                      /*decltype*/false,
7921                                      pidk);
7922
7923       /* Make a location:
7924             OP_TOKEN  CAST_EXPRESSION
7925             ^~~~~~~~~~~~~~~~~~~~~~~~~
7926          with start==caret at the operator token, and
7927          extending to the end of the cast_expression.  */
7928       loc = make_location (loc, loc, cast_expression.get_finish ());
7929
7930       /* Now, build an appropriate representation.  */
7931       switch (unary_operator)
7932         {
7933         case INDIRECT_REF:
7934           non_constant_p = NIC_STAR;
7935           expression = build_x_indirect_ref (loc, cast_expression,
7936                                              RO_UNARY_STAR,
7937                                              complain);
7938           /* TODO: build_x_indirect_ref does not always honor the
7939              location, so ensure it is set.  */
7940           expression.set_location (loc);
7941           break;
7942
7943         case ADDR_EXPR:
7944            non_constant_p = NIC_ADDR;
7945           /* Fall through.  */
7946         case BIT_NOT_EXPR:
7947           expression = build_x_unary_op (loc, unary_operator,
7948                                          cast_expression,
7949                                          complain);
7950           /* TODO: build_x_unary_op does not always honor the location,
7951              so ensure it is set.  */
7952           expression.set_location (loc);
7953           break;
7954
7955         case PREINCREMENT_EXPR:
7956         case PREDECREMENT_EXPR:
7957           non_constant_p = unary_operator == PREINCREMENT_EXPR
7958                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7959           /* Fall through.  */
7960         case NEGATE_EXPR:
7961           /* Immediately fold negation of a constant, unless the constant is 0
7962              (since -0 == 0) or it would overflow.  */
7963           if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7964               && CONSTANT_CLASS_P (cast_expression)
7965               && !integer_zerop (cast_expression)
7966               && !TREE_OVERFLOW (cast_expression))
7967             {
7968               tree folded = fold_build1 (unary_operator,
7969                                          TREE_TYPE (cast_expression),
7970                                          cast_expression);
7971               if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7972                 {
7973                   expression = cp_expr (folded, loc);
7974                   break;
7975                 }
7976             }
7977           /* Fall through.  */
7978         case UNARY_PLUS_EXPR:
7979         case TRUTH_NOT_EXPR:
7980           expression = finish_unary_op_expr (loc, unary_operator,
7981                                              cast_expression, complain);
7982           break;
7983
7984         default:
7985           gcc_unreachable ();
7986         }
7987
7988       if (non_constant_p != NIC_NONE
7989           && cp_parser_non_integral_constant_expression (parser,
7990                                                          non_constant_p))
7991         expression = error_mark_node;
7992
7993       return expression;
7994     }
7995
7996   return cp_parser_postfix_expression (parser, address_p, cast_p,
7997                                        /*member_access_only_p=*/false,
7998                                        decltype_p,
7999                                        pidk);
8000 }
8001
8002 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
8003    unary-operator, the corresponding tree code is returned.  */
8004
8005 static enum tree_code
8006 cp_parser_unary_operator (cp_token* token)
8007 {
8008   switch (token->type)
8009     {
8010     case CPP_MULT:
8011       return INDIRECT_REF;
8012
8013     case CPP_AND:
8014       return ADDR_EXPR;
8015
8016     case CPP_PLUS:
8017       return UNARY_PLUS_EXPR;
8018
8019     case CPP_MINUS:
8020       return NEGATE_EXPR;
8021
8022     case CPP_NOT:
8023       return TRUTH_NOT_EXPR;
8024
8025     case CPP_COMPL:
8026       return BIT_NOT_EXPR;
8027
8028     default:
8029       return ERROR_MARK;
8030     }
8031 }
8032
8033 /* Parse a new-expression.
8034
8035    new-expression:
8036      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8037      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8038
8039    Returns a representation of the expression.  */
8040
8041 static tree
8042 cp_parser_new_expression (cp_parser* parser)
8043 {
8044   bool global_scope_p;
8045   vec<tree, va_gc> *placement;
8046   tree type;
8047   vec<tree, va_gc> *initializer;
8048   tree nelts = NULL_TREE;
8049   tree ret;
8050
8051   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8052
8053   /* Look for the optional `::' operator.  */
8054   global_scope_p
8055     = (cp_parser_global_scope_opt (parser,
8056                                    /*current_scope_valid_p=*/false)
8057        != NULL_TREE);
8058   /* Look for the `new' operator.  */
8059   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8060   /* There's no easy way to tell a new-placement from the
8061      `( type-id )' construct.  */
8062   cp_parser_parse_tentatively (parser);
8063   /* Look for a new-placement.  */
8064   placement = cp_parser_new_placement (parser);
8065   /* If that didn't work out, there's no new-placement.  */
8066   if (!cp_parser_parse_definitely (parser))
8067     {
8068       if (placement != NULL)
8069         release_tree_vector (placement);
8070       placement = NULL;
8071     }
8072
8073   /* If the next token is a `(', then we have a parenthesized
8074      type-id.  */
8075   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8076     {
8077       cp_token *token;
8078       const char *saved_message = parser->type_definition_forbidden_message;
8079
8080       /* Consume the `('.  */
8081       cp_lexer_consume_token (parser->lexer);
8082
8083       /* Parse the type-id.  */
8084       parser->type_definition_forbidden_message
8085         = G_("types may not be defined in a new-expression");
8086       {
8087         type_id_in_expr_sentinel s (parser);
8088         type = cp_parser_type_id (parser);
8089       }
8090       parser->type_definition_forbidden_message = saved_message;
8091
8092       /* Look for the closing `)'.  */
8093       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8094       token = cp_lexer_peek_token (parser->lexer);
8095       /* There should not be a direct-new-declarator in this production,
8096          but GCC used to allowed this, so we check and emit a sensible error
8097          message for this case.  */
8098       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8099         {
8100           error_at (token->location,
8101                     "array bound forbidden after parenthesized type-id");
8102           inform (token->location, 
8103                   "try removing the parentheses around the type-id");
8104           cp_parser_direct_new_declarator (parser);
8105         }
8106     }
8107   /* Otherwise, there must be a new-type-id.  */
8108   else
8109     type = cp_parser_new_type_id (parser, &nelts);
8110
8111   /* If the next token is a `(' or '{', then we have a new-initializer.  */
8112   cp_token *token = cp_lexer_peek_token (parser->lexer);
8113   if (token->type == CPP_OPEN_PAREN
8114       || token->type == CPP_OPEN_BRACE)
8115     initializer = cp_parser_new_initializer (parser);
8116   else
8117     initializer = NULL;
8118
8119   /* A new-expression may not appear in an integral constant
8120      expression.  */
8121   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8122     ret = error_mark_node;
8123   /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8124      of a new-type-id or type-id of a new-expression, the new-expression shall
8125      contain a new-initializer of the form ( assignment-expression )".
8126      Additionally, consistently with the spirit of DR 1467, we want to accept
8127      'new auto { 2 }' too.  */
8128   else if (type_uses_auto (type)
8129            && (vec_safe_length (initializer) != 1
8130                || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8131                    && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8132     {
8133       error_at (token->location,
8134                 "initialization of new-expression for type %<auto%> "
8135                 "requires exactly one element");
8136       ret = error_mark_node;
8137     }
8138   else
8139     {
8140       /* Construct a location e.g.:
8141            ptr = new int[100]
8142                  ^~~~~~~~~~~~
8143          with caret == start at the start of the "new" token, and the end
8144          at the end of the final token we consumed.  */
8145       cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8146       location_t end_loc = get_finish (end_tok->location);
8147       location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8148
8149       /* Create a representation of the new-expression.  */
8150       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8151                        tf_warning_or_error);
8152       protected_set_expr_location (ret, combined_loc);
8153     }
8154
8155   if (placement != NULL)
8156     release_tree_vector (placement);
8157   if (initializer != NULL)
8158     release_tree_vector (initializer);
8159
8160   return ret;
8161 }
8162
8163 /* Parse a new-placement.
8164
8165    new-placement:
8166      ( expression-list )
8167
8168    Returns the same representation as for an expression-list.  */
8169
8170 static vec<tree, va_gc> *
8171 cp_parser_new_placement (cp_parser* parser)
8172 {
8173   vec<tree, va_gc> *expression_list;
8174
8175   /* Parse the expression-list.  */
8176   expression_list = (cp_parser_parenthesized_expression_list
8177                      (parser, non_attr, /*cast_p=*/false,
8178                       /*allow_expansion_p=*/true,
8179                       /*non_constant_p=*/NULL));
8180
8181   if (expression_list && expression_list->is_empty ())
8182     error ("expected expression-list or type-id");
8183
8184   return expression_list;
8185 }
8186
8187 /* Parse a new-type-id.
8188
8189    new-type-id:
8190      type-specifier-seq new-declarator [opt]
8191
8192    Returns the TYPE allocated.  If the new-type-id indicates an array
8193    type, *NELTS is set to the number of elements in the last array
8194    bound; the TYPE will not include the last array bound.  */
8195
8196 static tree
8197 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8198 {
8199   cp_decl_specifier_seq type_specifier_seq;
8200   cp_declarator *new_declarator;
8201   cp_declarator *declarator;
8202   cp_declarator *outer_declarator;
8203   const char *saved_message;
8204
8205   /* The type-specifier sequence must not contain type definitions.
8206      (It cannot contain declarations of new types either, but if they
8207      are not definitions we will catch that because they are not
8208      complete.)  */
8209   saved_message = parser->type_definition_forbidden_message;
8210   parser->type_definition_forbidden_message
8211     = G_("types may not be defined in a new-type-id");
8212   /* Parse the type-specifier-seq.  */
8213   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8214                                 /*is_trailing_return=*/false,
8215                                 &type_specifier_seq);
8216   /* Restore the old message.  */
8217   parser->type_definition_forbidden_message = saved_message;
8218
8219   if (type_specifier_seq.type == error_mark_node)
8220     return error_mark_node;
8221
8222   /* Parse the new-declarator.  */
8223   new_declarator = cp_parser_new_declarator_opt (parser);
8224
8225   /* Determine the number of elements in the last array dimension, if
8226      any.  */
8227   *nelts = NULL_TREE;
8228   /* Skip down to the last array dimension.  */
8229   declarator = new_declarator;
8230   outer_declarator = NULL;
8231   while (declarator && (declarator->kind == cdk_pointer
8232                         || declarator->kind == cdk_ptrmem))
8233     {
8234       outer_declarator = declarator;
8235       declarator = declarator->declarator;
8236     }
8237   while (declarator
8238          && declarator->kind == cdk_array
8239          && declarator->declarator
8240          && declarator->declarator->kind == cdk_array)
8241     {
8242       outer_declarator = declarator;
8243       declarator = declarator->declarator;
8244     }
8245
8246   if (declarator && declarator->kind == cdk_array)
8247     {
8248       *nelts = declarator->u.array.bounds;
8249       if (*nelts == error_mark_node)
8250         *nelts = integer_one_node;
8251
8252       if (outer_declarator)
8253         outer_declarator->declarator = declarator->declarator;
8254       else
8255         new_declarator = NULL;
8256     }
8257
8258   return groktypename (&type_specifier_seq, new_declarator, false);
8259 }
8260
8261 /* Parse an (optional) new-declarator.
8262
8263    new-declarator:
8264      ptr-operator new-declarator [opt]
8265      direct-new-declarator
8266
8267    Returns the declarator.  */
8268
8269 static cp_declarator *
8270 cp_parser_new_declarator_opt (cp_parser* parser)
8271 {
8272   enum tree_code code;
8273   tree type, std_attributes = NULL_TREE;
8274   cp_cv_quals cv_quals;  
8275
8276   /* We don't know if there's a ptr-operator next, or not.  */
8277   cp_parser_parse_tentatively (parser);
8278   /* Look for a ptr-operator.  */
8279   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8280   /* If that worked, look for more new-declarators.  */
8281   if (cp_parser_parse_definitely (parser))
8282     {
8283       cp_declarator *declarator;
8284
8285       /* Parse another optional declarator.  */
8286       declarator = cp_parser_new_declarator_opt (parser);
8287
8288       declarator = cp_parser_make_indirect_declarator
8289         (code, type, cv_quals, declarator, std_attributes);
8290
8291       return declarator;
8292     }
8293
8294   /* If the next token is a `[', there is a direct-new-declarator.  */
8295   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8296     return cp_parser_direct_new_declarator (parser);
8297
8298   return NULL;
8299 }
8300
8301 /* Parse a direct-new-declarator.
8302
8303    direct-new-declarator:
8304      [ expression ]
8305      direct-new-declarator [constant-expression]
8306
8307    */
8308
8309 static cp_declarator *
8310 cp_parser_direct_new_declarator (cp_parser* parser)
8311 {
8312   cp_declarator *declarator = NULL;
8313
8314   while (true)
8315     {
8316       tree expression;
8317       cp_token *token;
8318
8319       /* Look for the opening `['.  */
8320       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8321
8322       token = cp_lexer_peek_token (parser->lexer);
8323       expression = cp_parser_expression (parser);
8324       /* The standard requires that the expression have integral
8325          type.  DR 74 adds enumeration types.  We believe that the
8326          real intent is that these expressions be handled like the
8327          expression in a `switch' condition, which also allows
8328          classes with a single conversion to integral or
8329          enumeration type.  */
8330       if (!processing_template_decl)
8331         {
8332           expression
8333             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8334                                           expression,
8335                                           /*complain=*/true);
8336           if (!expression)
8337             {
8338               error_at (token->location,
8339                         "expression in new-declarator must have integral "
8340                         "or enumeration type");
8341               expression = error_mark_node;
8342             }
8343         }
8344
8345       /* Look for the closing `]'.  */
8346       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8347
8348       /* Add this bound to the declarator.  */
8349       declarator = make_array_declarator (declarator, expression);
8350
8351       /* If the next token is not a `[', then there are no more
8352          bounds.  */
8353       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8354         break;
8355     }
8356
8357   return declarator;
8358 }
8359
8360 /* Parse a new-initializer.
8361
8362    new-initializer:
8363      ( expression-list [opt] )
8364      braced-init-list
8365
8366    Returns a representation of the expression-list.  */
8367
8368 static vec<tree, va_gc> *
8369 cp_parser_new_initializer (cp_parser* parser)
8370 {
8371   vec<tree, va_gc> *expression_list;
8372
8373   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8374     {
8375       tree t;
8376       bool expr_non_constant_p;
8377       cp_lexer_set_source_position (parser->lexer);
8378       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8379       t = cp_parser_braced_list (parser, &expr_non_constant_p);
8380       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8381       expression_list = make_tree_vector_single (t);
8382     }
8383   else
8384     expression_list = (cp_parser_parenthesized_expression_list
8385                        (parser, non_attr, /*cast_p=*/false,
8386                         /*allow_expansion_p=*/true,
8387                         /*non_constant_p=*/NULL));
8388
8389   return expression_list;
8390 }
8391
8392 /* Parse a delete-expression.
8393
8394    delete-expression:
8395      :: [opt] delete cast-expression
8396      :: [opt] delete [ ] cast-expression
8397
8398    Returns a representation of the expression.  */
8399
8400 static tree
8401 cp_parser_delete_expression (cp_parser* parser)
8402 {
8403   bool global_scope_p;
8404   bool array_p;
8405   tree expression;
8406
8407   /* Look for the optional `::' operator.  */
8408   global_scope_p
8409     = (cp_parser_global_scope_opt (parser,
8410                                    /*current_scope_valid_p=*/false)
8411        != NULL_TREE);
8412   /* Look for the `delete' keyword.  */
8413   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8414   /* See if the array syntax is in use.  */
8415   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8416     {
8417       /* Consume the `[' token.  */
8418       cp_lexer_consume_token (parser->lexer);
8419       /* Look for the `]' token.  */
8420       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8421       /* Remember that this is the `[]' construct.  */
8422       array_p = true;
8423     }
8424   else
8425     array_p = false;
8426
8427   /* Parse the cast-expression.  */
8428   expression = cp_parser_simple_cast_expression (parser);
8429
8430   /* A delete-expression may not appear in an integral constant
8431      expression.  */
8432   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8433     return error_mark_node;
8434
8435   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8436                         tf_warning_or_error);
8437 }
8438
8439 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8440    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8441    0 otherwise.  */
8442
8443 static int
8444 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8445 {
8446   cp_token *token = cp_lexer_peek_token (parser->lexer);
8447   switch (token->type)
8448     {
8449     case CPP_COMMA:
8450     case CPP_SEMICOLON:
8451     case CPP_QUERY:
8452     case CPP_COLON:
8453     case CPP_CLOSE_SQUARE:
8454     case CPP_CLOSE_PAREN:
8455     case CPP_CLOSE_BRACE:
8456     case CPP_OPEN_BRACE:
8457     case CPP_DOT:
8458     case CPP_DOT_STAR:
8459     case CPP_DEREF:
8460     case CPP_DEREF_STAR:
8461     case CPP_DIV:
8462     case CPP_MOD:
8463     case CPP_LSHIFT:
8464     case CPP_RSHIFT:
8465     case CPP_LESS:
8466     case CPP_GREATER:
8467     case CPP_LESS_EQ:
8468     case CPP_GREATER_EQ:
8469     case CPP_EQ_EQ:
8470     case CPP_NOT_EQ:
8471     case CPP_EQ:
8472     case CPP_MULT_EQ:
8473     case CPP_DIV_EQ:
8474     case CPP_MOD_EQ:
8475     case CPP_PLUS_EQ:
8476     case CPP_MINUS_EQ:
8477     case CPP_RSHIFT_EQ:
8478     case CPP_LSHIFT_EQ:
8479     case CPP_AND_EQ:
8480     case CPP_XOR_EQ:
8481     case CPP_OR_EQ:
8482     case CPP_XOR:
8483     case CPP_OR:
8484     case CPP_OR_OR:
8485     case CPP_EOF:
8486     case CPP_ELLIPSIS:
8487       return 0;
8488
8489     case CPP_OPEN_PAREN:
8490       /* In ((type ()) () the last () isn't a valid cast-expression,
8491          so the whole must be parsed as postfix-expression.  */
8492       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8493              != CPP_CLOSE_PAREN;
8494
8495     case CPP_OPEN_SQUARE:
8496       /* '[' may start a primary-expression in obj-c++ and in C++11,
8497          as a lambda-expression, eg, '(void)[]{}'.  */
8498       if (cxx_dialect >= cxx11)
8499         return -1;
8500       return c_dialect_objc ();
8501
8502     case CPP_PLUS_PLUS:
8503     case CPP_MINUS_MINUS:
8504       /* '++' and '--' may or may not start a cast-expression:
8505
8506          struct T { void operator++(int); };
8507          void f() { (T())++; }
8508
8509          vs
8510
8511          int a;
8512          (int)++a;  */
8513       return -1;
8514
8515     default:
8516       return 1;
8517     }
8518 }
8519
8520 /* Parse a cast-expression.
8521
8522    cast-expression:
8523      unary-expression
8524      ( type-id ) cast-expression
8525
8526    ADDRESS_P is true iff the unary-expression is appearing as the
8527    operand of the `&' operator.   CAST_P is true if this expression is
8528    the target of a cast.
8529
8530    Returns a representation of the expression.  */
8531
8532 static cp_expr
8533 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8534                            bool decltype_p, cp_id_kind * pidk)
8535 {
8536   /* If it's a `(', then we might be looking at a cast.  */
8537   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8538     {
8539       tree type = NULL_TREE;
8540       cp_expr expr (NULL_TREE);
8541       int cast_expression = 0;
8542       const char *saved_message;
8543
8544       /* There's no way to know yet whether or not this is a cast.
8545          For example, `(int (3))' is a unary-expression, while `(int)
8546          3' is a cast.  So, we resort to parsing tentatively.  */
8547       cp_parser_parse_tentatively (parser);
8548       /* Types may not be defined in a cast.  */
8549       saved_message = parser->type_definition_forbidden_message;
8550       parser->type_definition_forbidden_message
8551         = G_("types may not be defined in casts");
8552       /* Consume the `('.  */
8553       cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8554       location_t open_paren_loc = open_paren->location;
8555
8556       /* A very tricky bit is that `(struct S) { 3 }' is a
8557          compound-literal (which we permit in C++ as an extension).
8558          But, that construct is not a cast-expression -- it is a
8559          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
8560          is legal; if the compound-literal were a cast-expression,
8561          you'd need an extra set of parentheses.)  But, if we parse
8562          the type-id, and it happens to be a class-specifier, then we
8563          will commit to the parse at that point, because we cannot
8564          undo the action that is done when creating a new class.  So,
8565          then we cannot back up and do a postfix-expression.
8566
8567          Another tricky case is the following (c++/29234):
8568
8569          struct S { void operator () (); };
8570
8571          void foo ()
8572          {
8573            ( S()() );
8574          }
8575
8576          As a type-id we parse the parenthesized S()() as a function
8577          returning a function, groktypename complains and we cannot
8578          back up in this case either.
8579
8580          Therefore, we scan ahead to the closing `)', and check to see
8581          if the tokens after the `)' can start a cast-expression.  Otherwise
8582          we are dealing with an unary-expression, a postfix-expression
8583          or something else.
8584
8585          Yet another tricky case, in C++11, is the following (c++/54891):
8586
8587          (void)[]{};
8588
8589          The issue is that usually, besides the case of lambda-expressions,
8590          the parenthesized type-id cannot be followed by '[', and, eg, we
8591          want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8592          Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8593          we don't commit, we try a cast-expression, then an unary-expression.
8594
8595          Save tokens so that we can put them back.  */
8596       cp_lexer_save_tokens (parser->lexer);
8597
8598       /* We may be looking at a cast-expression.  */
8599       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8600                                                  /*consume_paren=*/true))
8601         cast_expression
8602           = cp_parser_tokens_start_cast_expression (parser);
8603
8604       /* Roll back the tokens we skipped.  */
8605       cp_lexer_rollback_tokens (parser->lexer);
8606       /* If we aren't looking at a cast-expression, simulate an error so
8607          that the call to cp_parser_error_occurred below returns true.  */
8608       if (!cast_expression)
8609         cp_parser_simulate_error (parser);
8610       else
8611         {
8612           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8613           parser->in_type_id_in_expr_p = true;
8614           /* Look for the type-id.  */
8615           type = cp_parser_type_id (parser);
8616           /* Look for the closing `)'.  */
8617           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8618           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8619         }
8620
8621       /* Restore the saved message.  */
8622       parser->type_definition_forbidden_message = saved_message;
8623
8624       /* At this point this can only be either a cast or a
8625          parenthesized ctor such as `(T ())' that looks like a cast to
8626          function returning T.  */
8627       if (!cp_parser_error_occurred (parser))
8628         {
8629           /* Only commit if the cast-expression doesn't start with
8630              '++', '--', or '[' in C++11.  */
8631           if (cast_expression > 0)
8632             cp_parser_commit_to_topmost_tentative_parse (parser);
8633
8634           expr = cp_parser_cast_expression (parser,
8635                                             /*address_p=*/false,
8636                                             /*cast_p=*/true,
8637                                             /*decltype_p=*/false,
8638                                             pidk);
8639
8640           if (cp_parser_parse_definitely (parser))
8641             {
8642               /* Warn about old-style casts, if so requested.  */
8643               if (warn_old_style_cast
8644                   && !in_system_header_at (input_location)
8645                   && !VOID_TYPE_P (type)
8646                   && current_lang_name != lang_name_c)
8647                 warning (OPT_Wold_style_cast, "use of old-style cast");
8648
8649               /* Only type conversions to integral or enumeration types
8650                  can be used in constant-expressions.  */
8651               if (!cast_valid_in_integral_constant_expression_p (type)
8652                   && cp_parser_non_integral_constant_expression (parser,
8653                                                                  NIC_CAST))
8654                 return error_mark_node;
8655
8656               /* Perform the cast.  */
8657               /* Make a location:
8658                    (TYPE) EXPR
8659                    ^~~~~~~~~~~
8660                  with start==caret at the open paren, extending to the
8661                  end of "expr".  */
8662               location_t cast_loc = make_location (open_paren_loc,
8663                                                    open_paren_loc,
8664                                                    expr.get_finish ());
8665               expr = build_c_cast (cast_loc, type, expr);
8666               return expr;
8667             }
8668         }
8669       else 
8670         cp_parser_abort_tentative_parse (parser);
8671     }
8672
8673   /* If we get here, then it's not a cast, so it must be a
8674      unary-expression.  */
8675   return cp_parser_unary_expression (parser, pidk, address_p,
8676                                      cast_p, decltype_p);
8677 }
8678
8679 /* Parse a binary expression of the general form:
8680
8681    pm-expression:
8682      cast-expression
8683      pm-expression .* cast-expression
8684      pm-expression ->* cast-expression
8685
8686    multiplicative-expression:
8687      pm-expression
8688      multiplicative-expression * pm-expression
8689      multiplicative-expression / pm-expression
8690      multiplicative-expression % pm-expression
8691
8692    additive-expression:
8693      multiplicative-expression
8694      additive-expression + multiplicative-expression
8695      additive-expression - multiplicative-expression
8696
8697    shift-expression:
8698      additive-expression
8699      shift-expression << additive-expression
8700      shift-expression >> additive-expression
8701
8702    relational-expression:
8703      shift-expression
8704      relational-expression < shift-expression
8705      relational-expression > shift-expression
8706      relational-expression <= shift-expression
8707      relational-expression >= shift-expression
8708
8709   GNU Extension:
8710
8711    relational-expression:
8712      relational-expression <? shift-expression
8713      relational-expression >? shift-expression
8714
8715    equality-expression:
8716      relational-expression
8717      equality-expression == relational-expression
8718      equality-expression != relational-expression
8719
8720    and-expression:
8721      equality-expression
8722      and-expression & equality-expression
8723
8724    exclusive-or-expression:
8725      and-expression
8726      exclusive-or-expression ^ and-expression
8727
8728    inclusive-or-expression:
8729      exclusive-or-expression
8730      inclusive-or-expression | exclusive-or-expression
8731
8732    logical-and-expression:
8733      inclusive-or-expression
8734      logical-and-expression && inclusive-or-expression
8735
8736    logical-or-expression:
8737      logical-and-expression
8738      logical-or-expression || logical-and-expression
8739
8740    All these are implemented with a single function like:
8741
8742    binary-expression:
8743      simple-cast-expression
8744      binary-expression <token> binary-expression
8745
8746    CAST_P is true if this expression is the target of a cast.
8747
8748    The binops_by_token map is used to get the tree codes for each <token> type.
8749    binary-expressions are associated according to a precedence table.  */
8750
8751 #define TOKEN_PRECEDENCE(token)                              \
8752 (((token->type == CPP_GREATER                                \
8753    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8754   && !parser->greater_than_is_operator_p)                    \
8755  ? PREC_NOT_OPERATOR                                         \
8756  : binops_by_token[token->type].prec)
8757
8758 static cp_expr
8759 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8760                              bool no_toplevel_fold_p,
8761                              bool decltype_p,
8762                              enum cp_parser_prec prec,
8763                              cp_id_kind * pidk)
8764 {
8765   cp_parser_expression_stack stack;
8766   cp_parser_expression_stack_entry *sp = &stack[0];
8767   cp_parser_expression_stack_entry current;
8768   cp_expr rhs;
8769   cp_token *token;
8770   enum tree_code rhs_type;
8771   enum cp_parser_prec new_prec, lookahead_prec;
8772   tree overload;
8773
8774   /* Parse the first expression.  */
8775   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8776                       ? TRUTH_NOT_EXPR : ERROR_MARK);
8777   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8778                                            cast_p, decltype_p, pidk);
8779   current.prec = prec;
8780
8781   if (cp_parser_error_occurred (parser))
8782     return error_mark_node;
8783
8784   for (;;)
8785     {
8786       /* Get an operator token.  */
8787       token = cp_lexer_peek_token (parser->lexer);
8788
8789       if (warn_cxx11_compat
8790           && token->type == CPP_RSHIFT
8791           && !parser->greater_than_is_operator_p)
8792         {
8793           if (warning_at (token->location, OPT_Wc__11_compat,
8794                           "%<>>%> operator is treated"
8795                           " as two right angle brackets in C++11"))
8796             inform (token->location,
8797                     "suggest parentheses around %<>>%> expression");
8798         }
8799
8800       new_prec = TOKEN_PRECEDENCE (token);
8801       if (new_prec != PREC_NOT_OPERATOR
8802           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8803         /* This is a fold-expression; handle it later.  */
8804         new_prec = PREC_NOT_OPERATOR;
8805
8806       /* Popping an entry off the stack means we completed a subexpression:
8807          - either we found a token which is not an operator (`>' where it is not
8808            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8809            will happen repeatedly;
8810          - or, we found an operator which has lower priority.  This is the case
8811            where the recursive descent *ascends*, as in `3 * 4 + 5' after
8812            parsing `3 * 4'.  */
8813       if (new_prec <= current.prec)
8814         {
8815           if (sp == stack)
8816             break;
8817           else
8818             goto pop;
8819         }
8820
8821      get_rhs:
8822       current.tree_type = binops_by_token[token->type].tree_type;
8823       current.loc = token->location;
8824
8825       /* We used the operator token.  */
8826       cp_lexer_consume_token (parser->lexer);
8827
8828       /* For "false && x" or "true || x", x will never be executed;
8829          disable warnings while evaluating it.  */
8830       if (current.tree_type == TRUTH_ANDIF_EXPR)
8831         c_inhibit_evaluation_warnings +=
8832           cp_fully_fold (current.lhs) == truthvalue_false_node;
8833       else if (current.tree_type == TRUTH_ORIF_EXPR)
8834         c_inhibit_evaluation_warnings +=
8835           cp_fully_fold (current.lhs) == truthvalue_true_node;
8836
8837       /* Extract another operand.  It may be the RHS of this expression
8838          or the LHS of a new, higher priority expression.  */
8839       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8840                   ? TRUTH_NOT_EXPR : ERROR_MARK);
8841       rhs = cp_parser_simple_cast_expression (parser);
8842
8843       /* Get another operator token.  Look up its precedence to avoid
8844          building a useless (immediately popped) stack entry for common
8845          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
8846       token = cp_lexer_peek_token (parser->lexer);
8847       lookahead_prec = TOKEN_PRECEDENCE (token);
8848       if (lookahead_prec != PREC_NOT_OPERATOR
8849           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8850         lookahead_prec = PREC_NOT_OPERATOR;
8851       if (lookahead_prec > new_prec)
8852         {
8853           /* ... and prepare to parse the RHS of the new, higher priority
8854              expression.  Since precedence levels on the stack are
8855              monotonically increasing, we do not have to care about
8856              stack overflows.  */
8857           *sp = current;
8858           ++sp;
8859           current.lhs = rhs;
8860           current.lhs_type = rhs_type;
8861           current.prec = new_prec;
8862           new_prec = lookahead_prec;
8863           goto get_rhs;
8864
8865          pop:
8866           lookahead_prec = new_prec;
8867           /* If the stack is not empty, we have parsed into LHS the right side
8868              (`4' in the example above) of an expression we had suspended.
8869              We can use the information on the stack to recover the LHS (`3')
8870              from the stack together with the tree code (`MULT_EXPR'), and
8871              the precedence of the higher level subexpression
8872              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
8873              which will be used to actually build the additive expression.  */
8874           rhs = current.lhs;
8875           rhs_type = current.lhs_type;
8876           --sp;
8877           current = *sp;
8878         }
8879
8880       /* Undo the disabling of warnings done above.  */
8881       if (current.tree_type == TRUTH_ANDIF_EXPR)
8882         c_inhibit_evaluation_warnings -=
8883           cp_fully_fold (current.lhs) == truthvalue_false_node;
8884       else if (current.tree_type == TRUTH_ORIF_EXPR)
8885         c_inhibit_evaluation_warnings -=
8886           cp_fully_fold (current.lhs) == truthvalue_true_node;
8887
8888       if (warn_logical_not_paren
8889           && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8890           && current.lhs_type == TRUTH_NOT_EXPR
8891           /* Avoid warning for !!x == y.  */
8892           && (TREE_CODE (current.lhs) != NE_EXPR
8893               || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8894           && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8895               || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8896                   /* Avoid warning for !b == y where b is boolean.  */
8897                   && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8898                       || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8899                           != BOOLEAN_TYPE))))
8900           /* Avoid warning for !!b == y where b is boolean.  */
8901           && (!DECL_P (current.lhs)
8902               || TREE_TYPE (current.lhs) == NULL_TREE
8903               || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8904         warn_logical_not_parentheses (current.loc, current.tree_type,
8905                                       maybe_constant_value (rhs));
8906
8907       overload = NULL;
8908
8909       location_t combined_loc = make_location (current.loc,
8910                                                current.lhs.get_start (),
8911                                                rhs.get_finish ());
8912
8913       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8914          ERROR_MARK for everything that is not a binary expression.
8915          This makes warn_about_parentheses miss some warnings that
8916          involve unary operators.  For unary expressions we should
8917          pass the correct tree_code unless the unary expression was
8918          surrounded by parentheses.
8919       */
8920       if (no_toplevel_fold_p
8921           && lookahead_prec <= current.prec
8922           && sp == stack)
8923         current.lhs = build2_loc (combined_loc,
8924                                   current.tree_type,
8925                                   TREE_CODE_CLASS (current.tree_type)
8926                                   == tcc_comparison
8927                                   ? boolean_type_node : TREE_TYPE (current.lhs),
8928                                   current.lhs, rhs);
8929       else
8930         {
8931           current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8932                                            current.lhs, current.lhs_type,
8933                                            rhs, rhs_type, &overload,
8934                                            complain_flags (decltype_p));
8935           /* TODO: build_x_binary_op doesn't always honor the location.  */
8936           current.lhs.set_location (combined_loc);
8937         }
8938       current.lhs_type = current.tree_type;
8939
8940       /* If the binary operator required the use of an overloaded operator,
8941          then this expression cannot be an integral constant-expression.
8942          An overloaded operator can be used even if both operands are
8943          otherwise permissible in an integral constant-expression if at
8944          least one of the operands is of enumeration type.  */
8945
8946       if (overload
8947           && cp_parser_non_integral_constant_expression (parser,
8948                                                          NIC_OVERLOADED))
8949         return error_mark_node;
8950     }
8951
8952   return current.lhs;
8953 }
8954
8955 static cp_expr
8956 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8957                              bool no_toplevel_fold_p,
8958                              enum cp_parser_prec prec,
8959                              cp_id_kind * pidk)
8960 {
8961   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8962                                       /*decltype*/false, prec, pidk);
8963 }
8964
8965 /* Parse the `? expression : assignment-expression' part of a
8966    conditional-expression.  The LOGICAL_OR_EXPR is the
8967    logical-or-expression that started the conditional-expression.
8968    Returns a representation of the entire conditional-expression.
8969
8970    This routine is used by cp_parser_assignment_expression.
8971
8972      ? expression : assignment-expression
8973
8974    GNU Extensions:
8975
8976      ? : assignment-expression */
8977
8978 static tree
8979 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8980 {
8981   tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8982   cp_expr assignment_expr;
8983   struct cp_token *token;
8984   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8985
8986   /* Consume the `?' token.  */
8987   cp_lexer_consume_token (parser->lexer);
8988   token = cp_lexer_peek_token (parser->lexer);
8989   if (cp_parser_allow_gnu_extensions_p (parser)
8990       && token->type == CPP_COLON)
8991     {
8992       pedwarn (token->location, OPT_Wpedantic, 
8993                "ISO C++ does not allow ?: with omitted middle operand");
8994       /* Implicit true clause.  */
8995       expr = NULL_TREE;
8996       c_inhibit_evaluation_warnings +=
8997         folded_logical_or_expr == truthvalue_true_node;
8998       warn_for_omitted_condop (token->location, logical_or_expr);
8999     }
9000   else
9001     {
9002       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9003       parser->colon_corrects_to_scope_p = false;
9004       /* Parse the expression.  */
9005       c_inhibit_evaluation_warnings +=
9006         folded_logical_or_expr == truthvalue_false_node;
9007       expr = cp_parser_expression (parser);
9008       c_inhibit_evaluation_warnings +=
9009         ((folded_logical_or_expr == truthvalue_true_node)
9010          - (folded_logical_or_expr == truthvalue_false_node));
9011       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9012     }
9013
9014   /* The next token should be a `:'.  */
9015   cp_parser_require (parser, CPP_COLON, RT_COLON);
9016   /* Parse the assignment-expression.  */
9017   assignment_expr = cp_parser_assignment_expression (parser);
9018   c_inhibit_evaluation_warnings -=
9019     folded_logical_or_expr == truthvalue_true_node;
9020
9021   /* Make a location:
9022        LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9023        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9024      with the caret at the "?", ranging from the start of
9025      the logical_or_expr to the end of the assignment_expr.  */
9026   loc = make_location (loc,
9027                        logical_or_expr.get_start (),
9028                        assignment_expr.get_finish ());
9029
9030   /* Build the conditional-expression.  */
9031   return build_x_conditional_expr (loc, logical_or_expr,
9032                                    expr,
9033                                    assignment_expr,
9034                                    tf_warning_or_error);
9035 }
9036
9037 /* Parse an assignment-expression.
9038
9039    assignment-expression:
9040      conditional-expression
9041      logical-or-expression assignment-operator assignment_expression
9042      throw-expression
9043
9044    CAST_P is true if this expression is the target of a cast.
9045    DECLTYPE_P is true if this expression is the operand of decltype.
9046
9047    Returns a representation for the expression.  */
9048
9049 static cp_expr
9050 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9051                                  bool cast_p, bool decltype_p)
9052 {
9053   cp_expr expr;
9054
9055   /* If the next token is the `throw' keyword, then we're looking at
9056      a throw-expression.  */
9057   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9058     expr = cp_parser_throw_expression (parser);
9059   /* Otherwise, it must be that we are looking at a
9060      logical-or-expression.  */
9061   else
9062     {
9063       /* Parse the binary expressions (logical-or-expression).  */
9064       expr = cp_parser_binary_expression (parser, cast_p, false,
9065                                           decltype_p,
9066                                           PREC_NOT_OPERATOR, pidk);
9067       /* If the next token is a `?' then we're actually looking at a
9068          conditional-expression.  */
9069       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9070         return cp_parser_question_colon_clause (parser, expr);
9071       else
9072         {
9073           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9074
9075           /* If it's an assignment-operator, we're using the second
9076              production.  */
9077           enum tree_code assignment_operator
9078             = cp_parser_assignment_operator_opt (parser);
9079           if (assignment_operator != ERROR_MARK)
9080             {
9081               bool non_constant_p;
9082
9083               /* Parse the right-hand side of the assignment.  */
9084               cp_expr rhs = cp_parser_initializer_clause (parser,
9085                                                           &non_constant_p);
9086
9087               if ((flag_sanitize & SANITIZE_UI_OVERFLOW)
9088                    && isan_internal_fn_p (rhs))
9089                 {
9090                   int uns_p = TYPE_UNSIGNED (TREE_TYPE (expr));
9091                   tree rhs_value = rhs.get_value();
9092                   isan_maybe_change_ifn_sign_arg (&rhs_value, uns_p);
9093                 }
9094
9095               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9096                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9097
9098               /* An assignment may not appear in a
9099                  constant-expression.  */
9100               if (cp_parser_non_integral_constant_expression (parser,
9101                                                               NIC_ASSIGNMENT))
9102                 return error_mark_node;
9103               /* Build the assignment expression.  Its default
9104                  location:
9105                    LHS = RHS
9106                    ~~~~^~~~~
9107                  is the location of the '=' token as the
9108                  caret, ranging from the start of the lhs to the
9109                  end of the rhs.  */
9110               loc = make_location (loc,
9111                                    expr.get_start (),
9112                                    rhs.get_finish ());
9113               expr = build_x_modify_expr (loc, expr,
9114                                           assignment_operator,
9115                                           rhs,
9116                                           complain_flags (decltype_p));
9117               /* TODO: build_x_modify_expr doesn't honor the location,
9118                  so we must set it here.  */
9119               expr.set_location (loc);
9120             }
9121         }
9122     }
9123
9124   return expr;
9125 }
9126
9127 /* Parse an (optional) assignment-operator.
9128
9129    assignment-operator: one of
9130      = *= /= %= += -= >>= <<= &= ^= |=
9131
9132    GNU Extension:
9133
9134    assignment-operator: one of
9135      <?= >?=
9136
9137    If the next token is an assignment operator, the corresponding tree
9138    code is returned, and the token is consumed.  For example, for
9139    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
9140    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
9141    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
9142    operator, ERROR_MARK is returned.  */
9143
9144 static enum tree_code
9145 cp_parser_assignment_operator_opt (cp_parser* parser)
9146 {
9147   enum tree_code op;
9148   cp_token *token;
9149
9150   /* Peek at the next token.  */
9151   token = cp_lexer_peek_token (parser->lexer);
9152
9153   switch (token->type)
9154     {
9155     case CPP_EQ:
9156       op = NOP_EXPR;
9157       break;
9158
9159     case CPP_MULT_EQ:
9160       op = MULT_EXPR;
9161       break;
9162
9163     case CPP_DIV_EQ:
9164       op = TRUNC_DIV_EXPR;
9165       break;
9166
9167     case CPP_MOD_EQ:
9168       op = TRUNC_MOD_EXPR;
9169       break;
9170
9171     case CPP_PLUS_EQ:
9172       op = PLUS_EXPR;
9173       break;
9174
9175     case CPP_MINUS_EQ:
9176       op = MINUS_EXPR;
9177       break;
9178
9179     case CPP_RSHIFT_EQ:
9180       op = RSHIFT_EXPR;
9181       break;
9182
9183     case CPP_LSHIFT_EQ:
9184       op = LSHIFT_EXPR;
9185       break;
9186
9187     case CPP_AND_EQ:
9188       op = BIT_AND_EXPR;
9189       break;
9190
9191     case CPP_XOR_EQ:
9192       op = BIT_XOR_EXPR;
9193       break;
9194
9195     case CPP_OR_EQ:
9196       op = BIT_IOR_EXPR;
9197       break;
9198
9199     default:
9200       /* Nothing else is an assignment operator.  */
9201       op = ERROR_MARK;
9202     }
9203
9204   /* An operator followed by ... is a fold-expression, handled elsewhere.  */
9205   if (op != ERROR_MARK
9206       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9207     op = ERROR_MARK;
9208
9209   /* If it was an assignment operator, consume it.  */
9210   if (op != ERROR_MARK)
9211     cp_lexer_consume_token (parser->lexer);
9212
9213   return op;
9214 }
9215
9216 /* Parse an expression.
9217
9218    expression:
9219      assignment-expression
9220      expression , assignment-expression
9221
9222    CAST_P is true if this expression is the target of a cast.
9223    DECLTYPE_P is true if this expression is the immediate operand of decltype,
9224      except possibly parenthesized or on the RHS of a comma (N3276).
9225
9226    Returns a representation of the expression.  */
9227
9228 static cp_expr
9229 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9230                       bool cast_p, bool decltype_p)
9231 {
9232   cp_expr expression = NULL_TREE;
9233   location_t loc = UNKNOWN_LOCATION;
9234
9235   while (true)
9236     {
9237       cp_expr assignment_expression;
9238
9239       /* Parse the next assignment-expression.  */
9240       assignment_expression
9241         = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9242
9243       /* We don't create a temporary for a call that is the immediate operand
9244          of decltype or on the RHS of a comma.  But when we see a comma, we
9245          need to create a temporary for a call on the LHS.  */
9246       if (decltype_p && !processing_template_decl
9247           && TREE_CODE (assignment_expression) == CALL_EXPR
9248           && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9249           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9250         assignment_expression
9251           = build_cplus_new (TREE_TYPE (assignment_expression),
9252                              assignment_expression, tf_warning_or_error);
9253
9254       /* If this is the first assignment-expression, we can just
9255          save it away.  */
9256       if (!expression)
9257         expression = assignment_expression;
9258       else
9259         {
9260           /* Create a location with caret at the comma, ranging
9261              from the start of the LHS to the end of the RHS.  */
9262           loc = make_location (loc,
9263                                expression.get_start (),
9264                                assignment_expression.get_finish ());
9265           expression = build_x_compound_expr (loc, expression,
9266                                               assignment_expression,
9267                                               complain_flags (decltype_p));
9268           expression.set_location (loc);
9269         }
9270       /* If the next token is not a comma, or we're in a fold-expression, then
9271          we are done with the expression.  */
9272       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9273           || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9274         break;
9275       /* Consume the `,'.  */
9276       loc = cp_lexer_peek_token (parser->lexer)->location;
9277       cp_lexer_consume_token (parser->lexer);
9278       /* A comma operator cannot appear in a constant-expression.  */
9279       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9280         expression = error_mark_node;
9281     }
9282
9283   return expression;
9284 }
9285
9286 /* Parse a constant-expression.
9287
9288    constant-expression:
9289      conditional-expression
9290
9291   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9292   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
9293   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
9294   is false, NON_CONSTANT_P should be NULL.  */
9295
9296 static cp_expr
9297 cp_parser_constant_expression (cp_parser* parser,
9298                                bool allow_non_constant_p,
9299                                bool *non_constant_p)
9300 {
9301   bool saved_integral_constant_expression_p;
9302   bool saved_allow_non_integral_constant_expression_p;
9303   bool saved_non_integral_constant_expression_p;
9304   cp_expr expression;
9305
9306   /* It might seem that we could simply parse the
9307      conditional-expression, and then check to see if it were
9308      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
9309      one that the compiler can figure out is constant, possibly after
9310      doing some simplifications or optimizations.  The standard has a
9311      precise definition of constant-expression, and we must honor
9312      that, even though it is somewhat more restrictive.
9313
9314      For example:
9315
9316        int i[(2, 3)];
9317
9318      is not a legal declaration, because `(2, 3)' is not a
9319      constant-expression.  The `,' operator is forbidden in a
9320      constant-expression.  However, GCC's constant-folding machinery
9321      will fold this operation to an INTEGER_CST for `3'.  */
9322
9323   /* Save the old settings.  */
9324   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9325   saved_allow_non_integral_constant_expression_p
9326     = parser->allow_non_integral_constant_expression_p;
9327   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9328   /* We are now parsing a constant-expression.  */
9329   parser->integral_constant_expression_p = true;
9330   parser->allow_non_integral_constant_expression_p
9331     = (allow_non_constant_p || cxx_dialect >= cxx11);
9332   parser->non_integral_constant_expression_p = false;
9333   /* Although the grammar says "conditional-expression", we parse an
9334      "assignment-expression", which also permits "throw-expression"
9335      and the use of assignment operators.  In the case that
9336      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9337      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
9338      actually essential that we look for an assignment-expression.
9339      For example, cp_parser_initializer_clauses uses this function to
9340      determine whether a particular assignment-expression is in fact
9341      constant.  */
9342   expression = cp_parser_assignment_expression (parser);
9343   /* Restore the old settings.  */
9344   parser->integral_constant_expression_p
9345     = saved_integral_constant_expression_p;
9346   parser->allow_non_integral_constant_expression_p
9347     = saved_allow_non_integral_constant_expression_p;
9348   if (cxx_dialect >= cxx11)
9349     {
9350       /* Require an rvalue constant expression here; that's what our
9351          callers expect.  Reference constant expressions are handled
9352          separately in e.g. cp_parser_template_argument.  */
9353       bool is_const = potential_rvalue_constant_expression (expression);
9354       parser->non_integral_constant_expression_p = !is_const;
9355       if (!is_const && !allow_non_constant_p)
9356         require_potential_rvalue_constant_expression (expression);
9357     }
9358   if (allow_non_constant_p)
9359     *non_constant_p = parser->non_integral_constant_expression_p;
9360   parser->non_integral_constant_expression_p
9361     = saved_non_integral_constant_expression_p;
9362
9363   return expression;
9364 }
9365
9366 /* Parse __builtin_offsetof.
9367
9368    offsetof-expression:
9369      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9370
9371    offsetof-member-designator:
9372      id-expression
9373      | offsetof-member-designator "." id-expression
9374      | offsetof-member-designator "[" expression "]"
9375      | offsetof-member-designator "->" id-expression  */
9376
9377 static cp_expr
9378 cp_parser_builtin_offsetof (cp_parser *parser)
9379 {
9380   int save_ice_p, save_non_ice_p;
9381   tree type;
9382   cp_expr expr;
9383   cp_id_kind dummy;
9384   cp_token *token;
9385   location_t finish_loc;
9386
9387   /* We're about to accept non-integral-constant things, but will
9388      definitely yield an integral constant expression.  Save and
9389      restore these values around our local parsing.  */
9390   save_ice_p = parser->integral_constant_expression_p;
9391   save_non_ice_p = parser->non_integral_constant_expression_p;
9392
9393   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9394
9395   /* Consume the "__builtin_offsetof" token.  */
9396   cp_lexer_consume_token (parser->lexer);
9397   /* Consume the opening `('.  */
9398   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9399   /* Parse the type-id.  */
9400   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9401   type = cp_parser_type_id (parser);
9402   /* Look for the `,'.  */
9403   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9404   token = cp_lexer_peek_token (parser->lexer);
9405
9406   /* Build the (type *)null that begins the traditional offsetof macro.  */
9407   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9408                             tf_warning_or_error);
9409
9410   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
9411   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9412                                                  true, &dummy, token->location);
9413   while (true)
9414     {
9415       token = cp_lexer_peek_token (parser->lexer);
9416       switch (token->type)
9417         {
9418         case CPP_OPEN_SQUARE:
9419           /* offsetof-member-designator "[" expression "]" */
9420           expr = cp_parser_postfix_open_square_expression (parser, expr,
9421                                                            true, false);
9422           break;
9423
9424         case CPP_DEREF:
9425           /* offsetof-member-designator "->" identifier */
9426           expr = grok_array_decl (token->location, expr,
9427                                   integer_zero_node, false);
9428           /* FALLTHRU */
9429
9430         case CPP_DOT:
9431           /* offsetof-member-designator "." identifier */
9432           cp_lexer_consume_token (parser->lexer);
9433           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9434                                                          expr, true, &dummy,
9435                                                          token->location);
9436           break;
9437
9438         case CPP_CLOSE_PAREN:
9439           /* Consume the ")" token.  */
9440           finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9441           cp_lexer_consume_token (parser->lexer);
9442           goto success;
9443
9444         default:
9445           /* Error.  We know the following require will fail, but
9446              that gives the proper error message.  */
9447           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9448           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9449           expr = error_mark_node;
9450           goto failure;
9451         }
9452     }
9453
9454  success:
9455   /* Make a location of the form:
9456        __builtin_offsetof (struct s, f)
9457        ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9458      with caret at the type-id, ranging from the start of the
9459      "_builtin_offsetof" token to the close paren.  */
9460   loc = make_location (loc, start_loc, finish_loc);
9461   /* The result will be an INTEGER_CST, so we need to explicitly
9462      preserve the location.  */
9463   expr = cp_expr (finish_offsetof (expr, loc), loc);
9464
9465  failure:
9466   parser->integral_constant_expression_p = save_ice_p;
9467   parser->non_integral_constant_expression_p = save_non_ice_p;
9468
9469   return expr;
9470 }
9471
9472 /* Parse a trait expression.
9473
9474    Returns a representation of the expression, the underlying type
9475    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
9476
9477 static tree
9478 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9479 {
9480   cp_trait_kind kind;
9481   tree type1, type2 = NULL_TREE;
9482   bool binary = false;
9483   bool variadic = false;
9484
9485   switch (keyword)
9486     {
9487     case RID_HAS_NOTHROW_ASSIGN:
9488       kind = CPTK_HAS_NOTHROW_ASSIGN;
9489       break;
9490     case RID_HAS_NOTHROW_CONSTRUCTOR:
9491       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9492       break;
9493     case RID_HAS_NOTHROW_COPY:
9494       kind = CPTK_HAS_NOTHROW_COPY;
9495       break;
9496     case RID_HAS_TRIVIAL_ASSIGN:
9497       kind = CPTK_HAS_TRIVIAL_ASSIGN;
9498       break;
9499     case RID_HAS_TRIVIAL_CONSTRUCTOR:
9500       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9501       break;
9502     case RID_HAS_TRIVIAL_COPY:
9503       kind = CPTK_HAS_TRIVIAL_COPY;
9504       break;
9505     case RID_HAS_TRIVIAL_DESTRUCTOR:
9506       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9507       break;
9508     case RID_HAS_VIRTUAL_DESTRUCTOR:
9509       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9510       break;
9511     case RID_IS_ABSTRACT:
9512       kind = CPTK_IS_ABSTRACT;
9513       break;
9514     case RID_IS_BASE_OF:
9515       kind = CPTK_IS_BASE_OF;
9516       binary = true;
9517       break;
9518     case RID_IS_CLASS:
9519       kind = CPTK_IS_CLASS;
9520       break;
9521     case RID_IS_EMPTY:
9522       kind = CPTK_IS_EMPTY;
9523       break;
9524     case RID_IS_ENUM:
9525       kind = CPTK_IS_ENUM;
9526       break;
9527     case RID_IS_FINAL:
9528       kind = CPTK_IS_FINAL;
9529       break;
9530     case RID_IS_LITERAL_TYPE:
9531       kind = CPTK_IS_LITERAL_TYPE;
9532       break;
9533     case RID_IS_POD:
9534       kind = CPTK_IS_POD;
9535       break;
9536     case RID_IS_POLYMORPHIC:
9537       kind = CPTK_IS_POLYMORPHIC;
9538       break;
9539     case RID_IS_SAME_AS:
9540       kind = CPTK_IS_SAME_AS;
9541       binary = true;
9542       break;
9543     case RID_IS_STD_LAYOUT:
9544       kind = CPTK_IS_STD_LAYOUT;
9545       break;
9546     case RID_IS_TRIVIAL:
9547       kind = CPTK_IS_TRIVIAL;
9548       break;
9549     case RID_IS_TRIVIALLY_ASSIGNABLE:
9550       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9551       binary = true;
9552       break;
9553     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9554       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9555       variadic = true;
9556       break;
9557     case RID_IS_TRIVIALLY_COPYABLE:
9558       kind = CPTK_IS_TRIVIALLY_COPYABLE;
9559       break;
9560     case RID_IS_UNION:
9561       kind = CPTK_IS_UNION;
9562       break;
9563     case RID_UNDERLYING_TYPE:
9564       kind = CPTK_UNDERLYING_TYPE;
9565       break;
9566     case RID_BASES:
9567       kind = CPTK_BASES;
9568       break;
9569     case RID_DIRECT_BASES:
9570       kind = CPTK_DIRECT_BASES;
9571       break;
9572     default:
9573       gcc_unreachable ();
9574     }
9575
9576   /* Consume the token.  */
9577   cp_lexer_consume_token (parser->lexer);
9578
9579   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9580
9581   {
9582     type_id_in_expr_sentinel s (parser);
9583     type1 = cp_parser_type_id (parser);
9584   }
9585
9586   if (type1 == error_mark_node)
9587     return error_mark_node;
9588
9589   if (binary)
9590     {
9591       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9592  
9593       {
9594         type_id_in_expr_sentinel s (parser);
9595         type2 = cp_parser_type_id (parser);
9596       }
9597
9598       if (type2 == error_mark_node)
9599         return error_mark_node;
9600     }
9601   else if (variadic)
9602     {
9603       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9604         {
9605           cp_lexer_consume_token (parser->lexer);
9606           tree elt = cp_parser_type_id (parser);
9607           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9608             {
9609               cp_lexer_consume_token (parser->lexer);
9610               elt = make_pack_expansion (elt);
9611             }
9612           if (elt == error_mark_node)
9613             return error_mark_node;
9614           type2 = tree_cons (NULL_TREE, elt, type2);
9615         }
9616     }
9617
9618   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9619
9620   /* Complete the trait expression, which may mean either processing
9621      the trait expr now or saving it for template instantiation.  */
9622   switch(kind)
9623     {
9624     case CPTK_UNDERLYING_TYPE:
9625       return finish_underlying_type (type1);
9626     case CPTK_BASES:
9627       return finish_bases (type1, false);
9628     case CPTK_DIRECT_BASES:
9629       return finish_bases (type1, true);
9630     default:
9631       return finish_trait_expr (kind, type1, type2);
9632     }
9633 }
9634
9635 /* Lambdas that appear in variable initializer or default argument scope
9636    get that in their mangling, so we need to record it.  We might as well
9637    use the count for function and namespace scopes as well.  */
9638 static GTY(()) tree lambda_scope;
9639 static GTY(()) int lambda_count;
9640 struct GTY(()) tree_int
9641 {
9642   tree t;
9643   int i;
9644 };
9645 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9646
9647 static void
9648 start_lambda_scope (tree decl)
9649 {
9650   tree_int ti;
9651   gcc_assert (decl);
9652   /* Once we're inside a function, we ignore other scopes and just push
9653      the function again so that popping works properly.  */
9654   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9655     decl = current_function_decl;
9656   ti.t = lambda_scope;
9657   ti.i = lambda_count;
9658   vec_safe_push (lambda_scope_stack, ti);
9659   if (lambda_scope != decl)
9660     {
9661       /* Don't reset the count if we're still in the same function.  */
9662       lambda_scope = decl;
9663       lambda_count = 0;
9664     }
9665 }
9666
9667 static void
9668 record_lambda_scope (tree lambda)
9669 {
9670   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9671   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9672 }
9673
9674 static void
9675 finish_lambda_scope (void)
9676 {
9677   tree_int *p = &lambda_scope_stack->last ();
9678   if (lambda_scope != p->t)
9679     {
9680       lambda_scope = p->t;
9681       lambda_count = p->i;
9682     }
9683   lambda_scope_stack->pop ();
9684 }
9685
9686 /* Parse a lambda expression.
9687
9688    lambda-expression:
9689      lambda-introducer lambda-declarator [opt] compound-statement
9690
9691    Returns a representation of the expression.  */
9692
9693 static cp_expr
9694 cp_parser_lambda_expression (cp_parser* parser)
9695 {
9696   tree lambda_expr = build_lambda_expr ();
9697   tree type;
9698   bool ok = true;
9699   cp_token *token = cp_lexer_peek_token (parser->lexer);
9700   cp_token_position start = 0;
9701
9702   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9703
9704   if (cp_unevaluated_operand)
9705     {
9706       if (!token->error_reported)
9707         {
9708           error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9709                     "lambda-expression in unevaluated context");
9710           token->error_reported = true;
9711         }
9712       ok = false;
9713     }
9714   else if (parser->in_template_argument_list_p)
9715     {
9716       if (!token->error_reported)
9717         {
9718           error_at (token->location, "lambda-expression in template-argument");
9719           token->error_reported = true;
9720         }
9721       ok = false;
9722     }
9723
9724   /* We may be in the middle of deferred access check.  Disable
9725      it now.  */
9726   push_deferring_access_checks (dk_no_deferred);
9727
9728   cp_parser_lambda_introducer (parser, lambda_expr);
9729
9730   type = begin_lambda_type (lambda_expr);
9731   if (type == error_mark_node)
9732     return error_mark_node;
9733
9734   record_lambda_scope (lambda_expr);
9735
9736   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
9737   determine_visibility (TYPE_NAME (type));
9738
9739   /* Now that we've started the type, add the capture fields for any
9740      explicit captures.  */
9741   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9742
9743   {
9744     /* Inside the class, surrounding template-parameter-lists do not apply.  */
9745     unsigned int saved_num_template_parameter_lists
9746         = parser->num_template_parameter_lists;
9747     unsigned char in_statement = parser->in_statement;
9748     bool in_switch_statement_p = parser->in_switch_statement_p;
9749     bool fully_implicit_function_template_p
9750         = parser->fully_implicit_function_template_p;
9751     tree implicit_template_parms = parser->implicit_template_parms;
9752     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9753     bool auto_is_implicit_function_template_parm_p
9754         = parser->auto_is_implicit_function_template_parm_p;
9755
9756     parser->num_template_parameter_lists = 0;
9757     parser->in_statement = 0;
9758     parser->in_switch_statement_p = false;
9759     parser->fully_implicit_function_template_p = false;
9760     parser->implicit_template_parms = 0;
9761     parser->implicit_template_scope = 0;
9762     parser->auto_is_implicit_function_template_parm_p = false;
9763
9764     /* By virtue of defining a local class, a lambda expression has access to
9765        the private variables of enclosing classes.  */
9766
9767     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9768
9769     if (ok && cp_parser_error_occurred (parser))
9770       ok = false;
9771
9772     if (ok)
9773       {
9774         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9775             && cp_parser_start_tentative_firewall (parser))
9776           start = token;
9777         cp_parser_lambda_body (parser, lambda_expr);
9778       }
9779     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9780       {
9781         if (cp_parser_skip_to_closing_brace (parser))
9782           cp_lexer_consume_token (parser->lexer);
9783       }
9784
9785     /* The capture list was built up in reverse order; fix that now.  */
9786     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9787       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9788
9789     if (ok)
9790       maybe_add_lambda_conv_op (type);
9791
9792     type = finish_struct (type, /*attributes=*/NULL_TREE);
9793
9794     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9795     parser->in_statement = in_statement;
9796     parser->in_switch_statement_p = in_switch_statement_p;
9797     parser->fully_implicit_function_template_p
9798         = fully_implicit_function_template_p;
9799     parser->implicit_template_parms = implicit_template_parms;
9800     parser->implicit_template_scope = implicit_template_scope;
9801     parser->auto_is_implicit_function_template_parm_p
9802         = auto_is_implicit_function_template_parm_p;
9803   }
9804
9805   /* This field is only used during parsing of the lambda.  */
9806   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9807
9808   /* This lambda shouldn't have any proxies left at this point.  */
9809   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9810   /* And now that we're done, push proxies for an enclosing lambda.  */
9811   insert_pending_capture_proxies ();
9812
9813   if (ok)
9814     lambda_expr = build_lambda_object (lambda_expr);
9815   else
9816     lambda_expr = error_mark_node;
9817
9818   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9819
9820   pop_deferring_access_checks ();
9821
9822   return lambda_expr;
9823 }
9824
9825 /* Parse the beginning of a lambda expression.
9826
9827    lambda-introducer:
9828      [ lambda-capture [opt] ]
9829
9830    LAMBDA_EXPR is the current representation of the lambda expression.  */
9831
9832 static void
9833 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9834 {
9835   /* Need commas after the first capture.  */
9836   bool first = true;
9837
9838   /* Eat the leading `['.  */
9839   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9840
9841   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
9842   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9843       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9844     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9845   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9846     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9847
9848   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9849     {
9850       cp_lexer_consume_token (parser->lexer);
9851       first = false;
9852     }
9853
9854   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9855     {
9856       cp_token* capture_token;
9857       tree capture_id;
9858       tree capture_init_expr;
9859       cp_id_kind idk = CP_ID_KIND_NONE;
9860       bool explicit_init_p = false;
9861
9862       enum capture_kind_type
9863       {
9864         BY_COPY,
9865         BY_REFERENCE
9866       };
9867       enum capture_kind_type capture_kind = BY_COPY;
9868
9869       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9870         {
9871           error ("expected end of capture-list");
9872           return;
9873         }
9874
9875       if (first)
9876         first = false;
9877       else
9878         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9879
9880       /* Possibly capture `this'.  */
9881       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9882         {
9883           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9884           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9885             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9886                      "with by-copy capture default");
9887           cp_lexer_consume_token (parser->lexer);
9888           add_capture (lambda_expr,
9889                        /*id=*/this_identifier,
9890                        /*initializer=*/finish_this_expr(),
9891                        /*by_reference_p=*/false,
9892                        explicit_init_p);
9893           continue;
9894         }
9895
9896       /* Remember whether we want to capture as a reference or not.  */
9897       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9898         {
9899           capture_kind = BY_REFERENCE;
9900           cp_lexer_consume_token (parser->lexer);
9901         }
9902
9903       /* Get the identifier.  */
9904       capture_token = cp_lexer_peek_token (parser->lexer);
9905       capture_id = cp_parser_identifier (parser);
9906
9907       if (capture_id == error_mark_node)
9908         /* Would be nice to have a cp_parser_skip_to_closing_x for general
9909            delimiters, but I modified this to stop on unnested ']' as well.  It
9910            was already changed to stop on unnested '}', so the
9911            "closing_parenthesis" name is no more misleading with my change.  */
9912         {
9913           cp_parser_skip_to_closing_parenthesis (parser,
9914                                                  /*recovering=*/true,
9915                                                  /*or_comma=*/true,
9916                                                  /*consume_paren=*/true);
9917           break;
9918         }
9919
9920       /* Find the initializer for this capture.  */
9921       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9922           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9923           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9924         {
9925           bool direct, non_constant;
9926           /* An explicit initializer exists.  */
9927           if (cxx_dialect < cxx14)
9928             pedwarn (input_location, 0,
9929                      "lambda capture initializers "
9930                      "only available with -std=c++14 or -std=gnu++14");
9931           capture_init_expr = cp_parser_initializer (parser, &direct,
9932                                                      &non_constant);
9933           explicit_init_p = true;
9934           if (capture_init_expr == NULL_TREE)
9935             {
9936               error ("empty initializer for lambda init-capture");
9937               capture_init_expr = error_mark_node;
9938             }
9939         }
9940       else
9941         {
9942           const char* error_msg;
9943
9944           /* Turn the identifier into an id-expression.  */
9945           capture_init_expr
9946             = cp_parser_lookup_name_simple (parser, capture_id,
9947                                             capture_token->location);
9948
9949           if (capture_init_expr == error_mark_node)
9950             {
9951               unqualified_name_lookup_error (capture_id);
9952               continue;
9953             }
9954           else if (DECL_P (capture_init_expr)
9955                    && (!VAR_P (capture_init_expr)
9956                        && TREE_CODE (capture_init_expr) != PARM_DECL))
9957             {
9958               error_at (capture_token->location,
9959                         "capture of non-variable %qD ",
9960                         capture_init_expr);
9961               inform (DECL_SOURCE_LOCATION (capture_init_expr),
9962                       "%q#D declared here", capture_init_expr);
9963               continue;
9964             }
9965           if (VAR_P (capture_init_expr)
9966               && decl_storage_duration (capture_init_expr) != dk_auto)
9967             {
9968               if (pedwarn (capture_token->location, 0, "capture of variable "
9969                            "%qD with non-automatic storage duration",
9970                            capture_init_expr))
9971                 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9972                         "%q#D declared here", capture_init_expr);
9973               continue;
9974             }
9975
9976           capture_init_expr
9977             = finish_id_expression
9978                 (capture_id,
9979                  capture_init_expr,
9980                  parser->scope,
9981                  &idk,
9982                  /*integral_constant_expression_p=*/false,
9983                  /*allow_non_integral_constant_expression_p=*/false,
9984                  /*non_integral_constant_expression_p=*/NULL,
9985                  /*template_p=*/false,
9986                  /*done=*/true,
9987                  /*address_p=*/false,
9988                  /*template_arg_p=*/false,
9989                  &error_msg,
9990                  capture_token->location);
9991
9992           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9993             {
9994               cp_lexer_consume_token (parser->lexer);
9995               capture_init_expr = make_pack_expansion (capture_init_expr);
9996             }
9997           else
9998             check_for_bare_parameter_packs (capture_init_expr);
9999         }
10000
10001       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10002           && !explicit_init_p)
10003         {
10004           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10005               && capture_kind == BY_COPY)
10006             pedwarn (capture_token->location, 0, "explicit by-copy capture "
10007                      "of %qD redundant with by-copy capture default",
10008                      capture_id);
10009           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10010               && capture_kind == BY_REFERENCE)
10011             pedwarn (capture_token->location, 0, "explicit by-reference "
10012                      "capture of %qD redundant with by-reference capture "
10013                      "default", capture_id);
10014         }
10015
10016       add_capture (lambda_expr,
10017                    capture_id,
10018                    capture_init_expr,
10019                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
10020                    explicit_init_p);
10021     }
10022
10023   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10024 }
10025
10026 /* Parse the (optional) middle of a lambda expression.
10027
10028    lambda-declarator:
10029      < template-parameter-list [opt] >
10030      ( parameter-declaration-clause [opt] )
10031        attribute-specifier [opt]
10032        mutable [opt]
10033        exception-specification [opt]
10034        lambda-return-type-clause [opt]
10035
10036    LAMBDA_EXPR is the current representation of the lambda expression.  */
10037
10038 static bool
10039 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10040 {
10041   /* 5.1.1.4 of the standard says:
10042        If a lambda-expression does not include a lambda-declarator, it is as if
10043        the lambda-declarator were ().
10044      This means an empty parameter list, no attributes, and no exception
10045      specification.  */
10046   tree param_list = void_list_node;
10047   tree attributes = NULL_TREE;
10048   tree exception_spec = NULL_TREE;
10049   tree template_param_list = NULL_TREE;
10050   tree tx_qual = NULL_TREE;
10051
10052   /* The template-parameter-list is optional, but must begin with
10053      an opening angle if present.  */
10054   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10055     {
10056       if (cxx_dialect < cxx14)
10057         pedwarn (parser->lexer->next_token->location, 0,
10058                  "lambda templates are only available with "
10059                  "-std=c++14 or -std=gnu++14");
10060
10061       cp_lexer_consume_token (parser->lexer);
10062
10063       template_param_list = cp_parser_template_parameter_list (parser);
10064
10065       cp_parser_skip_to_end_of_template_parameter_list (parser);
10066
10067       /* We just processed one more parameter list.  */
10068       ++parser->num_template_parameter_lists;
10069     }
10070
10071   /* The parameter-declaration-clause is optional (unless
10072      template-parameter-list was given), but must begin with an
10073      opening parenthesis if present.  */
10074   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10075     {
10076       cp_lexer_consume_token (parser->lexer);
10077
10078       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10079
10080       /* Parse parameters.  */
10081       param_list = cp_parser_parameter_declaration_clause (parser);
10082
10083       /* Default arguments shall not be specified in the
10084          parameter-declaration-clause of a lambda-declarator.  */
10085       for (tree t = param_list; t; t = TREE_CHAIN (t))
10086         if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10087           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10088                    "default argument specified for lambda parameter");
10089
10090       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10091
10092       attributes = cp_parser_attributes_opt (parser);
10093
10094       /* Parse optional `mutable' keyword.  */
10095       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10096         {
10097           cp_lexer_consume_token (parser->lexer);
10098           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10099         }
10100
10101       tx_qual = cp_parser_tx_qualifier_opt (parser);
10102
10103       /* Parse optional exception specification.  */
10104       exception_spec = cp_parser_exception_specification_opt (parser);
10105
10106       /* Parse optional trailing return type.  */
10107       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10108         {
10109           cp_lexer_consume_token (parser->lexer);
10110           LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10111             = cp_parser_trailing_type_id (parser);
10112         }
10113
10114       /* The function parameters must be in scope all the way until after the
10115          trailing-return-type in case of decltype.  */
10116       pop_bindings_and_leave_scope ();
10117     }
10118   else if (template_param_list != NULL_TREE) // generate diagnostic
10119     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10120
10121   /* Create the function call operator.
10122
10123      Messing with declarators like this is no uglier than building up the
10124      FUNCTION_DECL by hand, and this is less likely to get out of sync with
10125      other code.  */
10126   {
10127     cp_decl_specifier_seq return_type_specs;
10128     cp_declarator* declarator;
10129     tree fco;
10130     int quals;
10131     void *p;
10132
10133     clear_decl_specs (&return_type_specs);
10134     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10135       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10136     else
10137       /* Maybe we will deduce the return type later.  */
10138       return_type_specs.type = make_auto ();
10139
10140     p = obstack_alloc (&declarator_obstack, 0);
10141
10142     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10143                                      sfk_none);
10144
10145     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10146              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10147     declarator = make_call_declarator (declarator, param_list, quals,
10148                                        VIRT_SPEC_UNSPECIFIED,
10149                                        REF_QUAL_NONE,
10150                                        tx_qual,
10151                                        exception_spec,
10152                                        /*late_return_type=*/NULL_TREE,
10153                                        /*requires_clause*/NULL_TREE);
10154     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10155
10156     fco = grokmethod (&return_type_specs,
10157                       declarator,
10158                       attributes);
10159     if (fco != error_mark_node)
10160       {
10161         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10162         DECL_ARTIFICIAL (fco) = 1;
10163         /* Give the object parameter a different name.  */
10164         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10165         if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10166           TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10167       }
10168     if (template_param_list)
10169       {
10170         fco = finish_member_template_decl (fco);
10171         finish_template_decl (template_param_list);
10172         --parser->num_template_parameter_lists;
10173       }
10174     else if (parser->fully_implicit_function_template_p)
10175       fco = finish_fully_implicit_template (parser, fco);
10176
10177     finish_member_declaration (fco);
10178
10179     obstack_free (&declarator_obstack, p);
10180
10181     return (fco != error_mark_node);
10182   }
10183 }
10184
10185 /* Parse the body of a lambda expression, which is simply
10186
10187    compound-statement
10188
10189    but which requires special handling.
10190    LAMBDA_EXPR is the current representation of the lambda expression.  */
10191
10192 static void
10193 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10194 {
10195   bool nested = (current_function_decl != NULL_TREE);
10196   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10197   if (nested)
10198     push_function_context ();
10199   else
10200     /* Still increment function_depth so that we don't GC in the
10201        middle of an expression.  */
10202     ++function_depth;
10203   vec<tree> omp_privatization_save;
10204   save_omp_privatization_clauses (omp_privatization_save);
10205   /* Clear this in case we're in the middle of a default argument.  */
10206   parser->local_variables_forbidden_p = false;
10207
10208   /* Finish the function call operator
10209      - class_specifier
10210      + late_parsing_for_member
10211      + function_definition_after_declarator
10212      + ctor_initializer_opt_and_function_body  */
10213   {
10214     tree fco = lambda_function (lambda_expr);
10215     tree body;
10216     bool done = false;
10217     tree compound_stmt;
10218     tree cap;
10219
10220     /* Let the front end know that we are going to be defining this
10221        function.  */
10222     start_preparsed_function (fco,
10223                               NULL_TREE,
10224                               SF_PRE_PARSED | SF_INCLASS_INLINE);
10225
10226     start_lambda_scope (fco);
10227     body = begin_function_body ();
10228
10229     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10230       goto out;
10231
10232     /* Push the proxies for any explicit captures.  */
10233     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10234          cap = TREE_CHAIN (cap))
10235       build_capture_proxy (TREE_PURPOSE (cap));
10236
10237     compound_stmt = begin_compound_stmt (0);
10238
10239     /* 5.1.1.4 of the standard says:
10240          If a lambda-expression does not include a trailing-return-type, it
10241          is as if the trailing-return-type denotes the following type:
10242           * if the compound-statement is of the form
10243                { return attribute-specifier [opt] expression ; }
10244              the type of the returned expression after lvalue-to-rvalue
10245              conversion (_conv.lval_ 4.1), array-to-pointer conversion
10246              (_conv.array_ 4.2), and function-to-pointer conversion
10247              (_conv.func_ 4.3);
10248           * otherwise, void.  */
10249
10250     /* In a lambda that has neither a lambda-return-type-clause
10251        nor a deducible form, errors should be reported for return statements
10252        in the body.  Since we used void as the placeholder return type, parsing
10253        the body as usual will give such desired behavior.  */
10254     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10255         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10256         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10257       {
10258         tree expr = NULL_TREE;
10259         cp_id_kind idk = CP_ID_KIND_NONE;
10260
10261         /* Parse tentatively in case there's more after the initial return
10262            statement.  */
10263         cp_parser_parse_tentatively (parser);
10264
10265         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10266
10267         expr = cp_parser_expression (parser, &idk);
10268
10269         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10270         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10271
10272         if (cp_parser_parse_definitely (parser))
10273           {
10274             if (!processing_template_decl)
10275               {
10276                 tree type = lambda_return_type (expr);
10277                 apply_deduced_return_type (fco, type);
10278                 if (type == error_mark_node)
10279                   expr = error_mark_node;
10280               }
10281
10282             /* Will get error here if type not deduced yet.  */
10283             finish_return_stmt (expr);
10284
10285             done = true;
10286           }
10287       }
10288
10289     if (!done)
10290       {
10291         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10292           cp_parser_label_declaration (parser);
10293         cp_parser_statement_seq_opt (parser, NULL_TREE);
10294         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10295       }
10296
10297     finish_compound_stmt (compound_stmt);
10298
10299   out:
10300     finish_function_body (body);
10301     finish_lambda_scope ();
10302
10303     /* Finish the function and generate code for it if necessary.  */
10304     tree fn = finish_function (/*inline*/2);
10305
10306     /* Only expand if the call op is not a template.  */
10307     if (!DECL_TEMPLATE_INFO (fco))
10308       expand_or_defer_fn (fn);
10309   }
10310
10311   restore_omp_privatization_clauses (omp_privatization_save);
10312   parser->local_variables_forbidden_p = local_variables_forbidden_p;
10313   if (nested)
10314     pop_function_context();
10315   else
10316     --function_depth;
10317 }
10318
10319 /* Statements [gram.stmt.stmt]  */
10320
10321 /* Parse a statement.
10322
10323    statement:
10324      labeled-statement
10325      expression-statement
10326      compound-statement
10327      selection-statement
10328      iteration-statement
10329      jump-statement
10330      declaration-statement
10331      try-block
10332
10333   C++11:
10334
10335   statement:
10336     labeled-statement
10337     attribute-specifier-seq (opt) expression-statement
10338     attribute-specifier-seq (opt) compound-statement
10339     attribute-specifier-seq (opt) selection-statement
10340     attribute-specifier-seq (opt) iteration-statement
10341     attribute-specifier-seq (opt) jump-statement
10342     declaration-statement
10343     attribute-specifier-seq (opt) try-block
10344
10345   TM Extension:
10346
10347    statement:
10348      atomic-statement
10349
10350   IN_COMPOUND is true when the statement is nested inside a
10351   cp_parser_compound_statement; this matters for certain pragmas.
10352
10353   If IF_P is not NULL, *IF_P is set to indicate whether the statement
10354   is a (possibly labeled) if statement which is not enclosed in braces
10355   and has an else clause.  This is used to implement -Wparentheses.
10356
10357   CHAIN is a vector of if-else-if conditions.  */
10358
10359 static void
10360 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10361                      bool in_compound, bool *if_p, vec<tree> *chain)
10362 {
10363   tree statement, std_attrs = NULL_TREE;
10364   cp_token *token;
10365   location_t statement_location, attrs_location;
10366
10367  restart:
10368   if (if_p != NULL)
10369     *if_p = false;
10370   /* There is no statement yet.  */
10371   statement = NULL_TREE;
10372
10373   saved_token_sentinel saved_tokens (parser->lexer);
10374   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10375   if (c_dialect_objc ())
10376     /* In obj-c++, seeing '[[' might be the either the beginning of
10377        c++11 attributes, or a nested objc-message-expression.  So
10378        let's parse the c++11 attributes tentatively.  */
10379     cp_parser_parse_tentatively (parser);
10380   std_attrs = cp_parser_std_attribute_spec_seq (parser);
10381   if (c_dialect_objc ())
10382     {
10383       if (!cp_parser_parse_definitely (parser))
10384         std_attrs = NULL_TREE;
10385     }
10386
10387   /* Peek at the next token.  */
10388   token = cp_lexer_peek_token (parser->lexer);
10389   /* Remember the location of the first token in the statement.  */
10390   statement_location = token->location;
10391   /* If this is a keyword, then that will often determine what kind of
10392      statement we have.  */
10393   if (token->type == CPP_KEYWORD)
10394     {
10395       enum rid keyword = token->keyword;
10396
10397       switch (keyword)
10398         {
10399         case RID_CASE:
10400         case RID_DEFAULT:
10401           /* Looks like a labeled-statement with a case label.
10402              Parse the label, and then use tail recursion to parse
10403              the statement.  */
10404           cp_parser_label_for_labeled_statement (parser, std_attrs);
10405           in_compound = false;
10406           goto restart;
10407
10408         case RID_IF:
10409         case RID_SWITCH:
10410           statement = cp_parser_selection_statement (parser, if_p, chain);
10411           break;
10412
10413         case RID_WHILE:
10414         case RID_DO:
10415         case RID_FOR:
10416           statement = cp_parser_iteration_statement (parser, if_p, false);
10417           break;
10418
10419         case RID_CILK_FOR:
10420           if (!flag_cilkplus)
10421             {
10422               error_at (cp_lexer_peek_token (parser->lexer)->location,
10423                         "-fcilkplus must be enabled to use %<_Cilk_for%>");
10424               cp_lexer_consume_token (parser->lexer);
10425               statement = error_mark_node;
10426             }
10427           else
10428             statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10429           break;
10430
10431         case RID_BREAK:
10432         case RID_CONTINUE:
10433         case RID_RETURN:
10434         case RID_GOTO:
10435           statement = cp_parser_jump_statement (parser);
10436           break;
10437
10438         case RID_CILK_SYNC:
10439           cp_lexer_consume_token (parser->lexer);
10440           if (flag_cilkplus)
10441             {
10442               tree sync_expr = build_cilk_sync ();
10443               SET_EXPR_LOCATION (sync_expr,
10444                                  token->location);
10445               statement = finish_expr_stmt (sync_expr);
10446             }
10447           else
10448             {
10449               error_at (token->location, "-fcilkplus must be enabled to use"
10450                         " %<_Cilk_sync%>");
10451               statement = error_mark_node;
10452             }
10453           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10454           break;
10455
10456           /* Objective-C++ exception-handling constructs.  */
10457         case RID_AT_TRY:
10458         case RID_AT_CATCH:
10459         case RID_AT_FINALLY:
10460         case RID_AT_SYNCHRONIZED:
10461         case RID_AT_THROW:
10462           statement = cp_parser_objc_statement (parser);
10463           break;
10464
10465         case RID_TRY:
10466           statement = cp_parser_try_block (parser);
10467           break;
10468
10469         case RID_NAMESPACE:
10470           /* This must be a namespace alias definition.  */
10471           cp_parser_declaration_statement (parser);
10472           return;
10473           
10474         case RID_TRANSACTION_ATOMIC:
10475         case RID_TRANSACTION_RELAXED:
10476         case RID_SYNCHRONIZED:
10477         case RID_ATOMIC_NOEXCEPT:
10478         case RID_ATOMIC_CANCEL:
10479           statement = cp_parser_transaction (parser, token);
10480           break;
10481         case RID_TRANSACTION_CANCEL:
10482           statement = cp_parser_transaction_cancel (parser);
10483           break;
10484
10485         default:
10486           /* It might be a keyword like `int' that can start a
10487              declaration-statement.  */
10488           break;
10489         }
10490     }
10491   else if (token->type == CPP_NAME)
10492     {
10493       /* If the next token is a `:', then we are looking at a
10494          labeled-statement.  */
10495       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10496       if (token->type == CPP_COLON)
10497         {
10498           /* Looks like a labeled-statement with an ordinary label.
10499              Parse the label, and then use tail recursion to parse
10500              the statement.  */
10501
10502           cp_parser_label_for_labeled_statement (parser, std_attrs);
10503           in_compound = false;
10504           goto restart;
10505         }
10506     }
10507   /* Anything that starts with a `{' must be a compound-statement.  */
10508   else if (token->type == CPP_OPEN_BRACE)
10509     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10510   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10511      a statement all its own.  */
10512   else if (token->type == CPP_PRAGMA)
10513     {
10514       /* Only certain OpenMP pragmas are attached to statements, and thus
10515          are considered statements themselves.  All others are not.  In
10516          the context of a compound, accept the pragma as a "statement" and
10517          return so that we can check for a close brace.  Otherwise we
10518          require a real statement and must go back and read one.  */
10519       if (in_compound)
10520         cp_parser_pragma (parser, pragma_compound, if_p);
10521       else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10522         goto restart;
10523       return;
10524     }
10525   else if (token->type == CPP_EOF)
10526     {
10527       cp_parser_error (parser, "expected statement");
10528       return;
10529     }
10530
10531   /* Everything else must be a declaration-statement or an
10532      expression-statement.  Try for the declaration-statement
10533      first, unless we are looking at a `;', in which case we know that
10534      we have an expression-statement.  */
10535   if (!statement)
10536     {
10537       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10538         {
10539           if (std_attrs != NULL_TREE)
10540             {
10541               /*  Attributes should be parsed as part of the the
10542                   declaration, so let's un-parse them.  */
10543               saved_tokens.rollback();
10544               std_attrs = NULL_TREE;
10545             }
10546
10547           cp_parser_parse_tentatively (parser);
10548           /* Try to parse the declaration-statement.  */
10549           cp_parser_declaration_statement (parser);
10550           /* If that worked, we're done.  */
10551           if (cp_parser_parse_definitely (parser))
10552             return;
10553         }
10554       /* Look for an expression-statement instead.  */
10555       statement = cp_parser_expression_statement (parser, in_statement_expr);
10556     }
10557
10558   /* Set the line number for the statement.  */
10559   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10560     SET_EXPR_LOCATION (statement, statement_location);
10561
10562   /* Note that for now, we don't do anything with c++11 statements
10563      parsed at this level.  */
10564   if (std_attrs != NULL_TREE)
10565     warning_at (attrs_location,
10566                 OPT_Wattributes,
10567                 "attributes at the beginning of statement are ignored");
10568 }
10569
10570 /* Parse the label for a labeled-statement, i.e.
10571
10572    identifier :
10573    case constant-expression :
10574    default :
10575
10576    GNU Extension:
10577    case constant-expression ... constant-expression : statement
10578
10579    When a label is parsed without errors, the label is added to the
10580    parse tree by the finish_* functions, so this function doesn't
10581    have to return the label.  */
10582
10583 static void
10584 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10585 {
10586   cp_token *token;
10587   tree label = NULL_TREE;
10588   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10589
10590   /* The next token should be an identifier.  */
10591   token = cp_lexer_peek_token (parser->lexer);
10592   if (token->type != CPP_NAME
10593       && token->type != CPP_KEYWORD)
10594     {
10595       cp_parser_error (parser, "expected labeled-statement");
10596       return;
10597     }
10598
10599   parser->colon_corrects_to_scope_p = false;
10600   switch (token->keyword)
10601     {
10602     case RID_CASE:
10603       {
10604         tree expr, expr_hi;
10605         cp_token *ellipsis;
10606
10607         /* Consume the `case' token.  */
10608         cp_lexer_consume_token (parser->lexer);
10609         /* Parse the constant-expression.  */
10610         expr = cp_parser_constant_expression (parser);
10611         if (check_for_bare_parameter_packs (expr))
10612           expr = error_mark_node;
10613
10614         ellipsis = cp_lexer_peek_token (parser->lexer);
10615         if (ellipsis->type == CPP_ELLIPSIS)
10616           {
10617             /* Consume the `...' token.  */
10618             cp_lexer_consume_token (parser->lexer);
10619             expr_hi = cp_parser_constant_expression (parser);
10620             if (check_for_bare_parameter_packs (expr_hi))
10621               expr_hi = error_mark_node;
10622
10623             /* We don't need to emit warnings here, as the common code
10624                will do this for us.  */
10625           }
10626         else
10627           expr_hi = NULL_TREE;
10628
10629         if (parser->in_switch_statement_p)
10630           finish_case_label (token->location, expr, expr_hi);
10631         else
10632           error_at (token->location,
10633                     "case label %qE not within a switch statement",
10634                     expr);
10635       }
10636       break;
10637
10638     case RID_DEFAULT:
10639       /* Consume the `default' token.  */
10640       cp_lexer_consume_token (parser->lexer);
10641
10642       if (parser->in_switch_statement_p)
10643         finish_case_label (token->location, NULL_TREE, NULL_TREE);
10644       else
10645         error_at (token->location, "case label not within a switch statement");
10646       break;
10647
10648     default:
10649       /* Anything else must be an ordinary label.  */
10650       label = finish_label_stmt (cp_parser_identifier (parser));
10651       break;
10652     }
10653
10654   /* Require the `:' token.  */
10655   cp_parser_require (parser, CPP_COLON, RT_COLON);
10656
10657   /* An ordinary label may optionally be followed by attributes.
10658      However, this is only permitted if the attributes are then
10659      followed by a semicolon.  This is because, for backward
10660      compatibility, when parsing
10661        lab: __attribute__ ((unused)) int i;
10662      we want the attribute to attach to "i", not "lab".  */
10663   if (label != NULL_TREE
10664       && cp_next_tokens_can_be_gnu_attribute_p (parser))
10665     {
10666       tree attrs;
10667       cp_parser_parse_tentatively (parser);
10668       attrs = cp_parser_gnu_attributes_opt (parser);
10669       if (attrs == NULL_TREE
10670           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10671         cp_parser_abort_tentative_parse (parser);
10672       else if (!cp_parser_parse_definitely (parser))
10673         ;
10674       else
10675         attributes = chainon (attributes, attrs);
10676     }
10677
10678   if (attributes != NULL_TREE)
10679     cplus_decl_attributes (&label, attributes, 0);
10680
10681   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10682 }
10683
10684 /* Parse an expression-statement.
10685
10686    expression-statement:
10687      expression [opt] ;
10688
10689    Returns the new EXPR_STMT -- or NULL_TREE if the expression
10690    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10691    indicates whether this expression-statement is part of an
10692    expression statement.  */
10693
10694 static tree
10695 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10696 {
10697   tree statement = NULL_TREE;
10698   cp_token *token = cp_lexer_peek_token (parser->lexer);
10699
10700   /* If the next token is a ';', then there is no expression
10701      statement.  */
10702   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10703     {
10704       statement = cp_parser_expression (parser);
10705       if (statement == error_mark_node
10706           && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10707         {
10708           cp_parser_skip_to_end_of_block_or_statement (parser);
10709           return error_mark_node;
10710         }
10711     }
10712
10713   /* Give a helpful message for "A<T>::type t;" and the like.  */
10714   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10715       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10716     {
10717       if (TREE_CODE (statement) == SCOPE_REF)
10718         error_at (token->location, "need %<typename%> before %qE because "
10719                   "%qT is a dependent scope",
10720                   statement, TREE_OPERAND (statement, 0));
10721       else if (is_overloaded_fn (statement)
10722                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10723         {
10724           /* A::A a; */
10725           tree fn = get_first_fn (statement);
10726           error_at (token->location,
10727                     "%<%T::%D%> names the constructor, not the type",
10728                     DECL_CONTEXT (fn), DECL_NAME (fn));
10729         }
10730     }
10731
10732   /* Consume the final `;'.  */
10733   cp_parser_consume_semicolon_at_end_of_statement (parser);
10734
10735   if (in_statement_expr
10736       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10737     /* This is the final expression statement of a statement
10738        expression.  */
10739     statement = finish_stmt_expr_expr (statement, in_statement_expr);
10740   else if (statement)
10741     statement = finish_expr_stmt (statement);
10742
10743   return statement;
10744 }
10745
10746 /* Parse a compound-statement.
10747
10748    compound-statement:
10749      { statement-seq [opt] }
10750
10751    GNU extension:
10752
10753    compound-statement:
10754      { label-declaration-seq [opt] statement-seq [opt] }
10755
10756    label-declaration-seq:
10757      label-declaration
10758      label-declaration-seq label-declaration
10759
10760    Returns a tree representing the statement.  */
10761
10762 static tree
10763 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10764                               int bcs_flags, bool function_body)
10765 {
10766   tree compound_stmt;
10767
10768   /* Consume the `{'.  */
10769   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10770     return error_mark_node;
10771   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10772       && !function_body && cxx_dialect < cxx14)
10773     pedwarn (input_location, OPT_Wpedantic,
10774              "compound-statement in constexpr function");
10775   /* Begin the compound-statement.  */
10776   compound_stmt = begin_compound_stmt (bcs_flags);
10777   /* If the next keyword is `__label__' we have a label declaration.  */
10778   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10779     cp_parser_label_declaration (parser);
10780   /* Parse an (optional) statement-seq.  */
10781   cp_parser_statement_seq_opt (parser, in_statement_expr);
10782   /* Consume the `}' and keep location, if there is a `}' and compound_stmt is
10783      not empty. */
10784   cp_token *token = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10785   if (token &&
10786       TREE_CODE(compound_stmt) == STATEMENT_LIST &&
10787       STATEMENT_LIST_HEAD (compound_stmt) &&
10788       STATEMENT_LIST_TAIL (compound_stmt))
10789     {
10790       tree brace_stmt = build0(STATEMENT_LIST_END, void_type_node);
10791       SET_EXPR_LOCATION(brace_stmt, token->location);
10792       add_stmt(brace_stmt);
10793     }
10794   /* Finish the compound-statement.  */
10795   finish_compound_stmt (compound_stmt);
10796   return compound_stmt;
10797 }
10798
10799 /* Parse an (optional) statement-seq.
10800
10801    statement-seq:
10802      statement
10803      statement-seq [opt] statement  */
10804
10805 static void
10806 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10807 {
10808   /* Scan statements until there aren't any more.  */
10809   while (true)
10810     {
10811       cp_token *token = cp_lexer_peek_token (parser->lexer);
10812
10813       /* If we are looking at a `}', then we have run out of
10814          statements; the same is true if we have reached the end
10815          of file, or have stumbled upon a stray '@end'.  */
10816       if (token->type == CPP_CLOSE_BRACE
10817           || token->type == CPP_EOF
10818           || token->type == CPP_PRAGMA_EOL
10819           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10820         break;
10821       
10822       /* If we are in a compound statement and find 'else' then
10823          something went wrong.  */
10824       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10825         {
10826           if (parser->in_statement & IN_IF_STMT) 
10827             break;
10828           else
10829             {
10830               token = cp_lexer_consume_token (parser->lexer);
10831               error_at (token->location, "%<else%> without a previous %<if%>");
10832             }
10833         }
10834
10835       /* Parse the statement.  */
10836       cp_parser_statement (parser, in_statement_expr, true, NULL);
10837     }
10838 }
10839
10840 /* Parse a selection-statement.
10841
10842    selection-statement:
10843      if ( condition ) statement
10844      if ( condition ) statement else statement
10845      switch ( condition ) statement
10846
10847    Returns the new IF_STMT or SWITCH_STMT.
10848
10849    If IF_P is not NULL, *IF_P is set to indicate whether the statement
10850    is a (possibly labeled) if statement which is not enclosed in
10851    braces and has an else clause.  This is used to implement
10852    -Wparentheses.
10853
10854    CHAIN is a vector of if-else-if conditions.  This is used to implement
10855    -Wduplicated-cond.  */
10856
10857 static tree
10858 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10859                                vec<tree> *chain)
10860 {
10861   cp_token *token;
10862   enum rid keyword;
10863   token_indent_info guard_tinfo;
10864
10865   if (if_p != NULL)
10866     *if_p = false;
10867
10868   /* Peek at the next token.  */
10869   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10870   guard_tinfo = get_token_indent_info (token);
10871
10872   /* See what kind of keyword it is.  */
10873   keyword = token->keyword;
10874   switch (keyword)
10875     {
10876     case RID_IF:
10877     case RID_SWITCH:
10878       {
10879         tree statement;
10880         tree condition;
10881
10882         /* Look for the `('.  */
10883         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10884           {
10885             cp_parser_skip_to_end_of_statement (parser);
10886             return error_mark_node;
10887           }
10888
10889         /* Begin the selection-statement.  */
10890         if (keyword == RID_IF)
10891           statement = begin_if_stmt ();
10892         else
10893           statement = begin_switch_stmt ();
10894
10895         /* Parse the condition.  */
10896         condition = cp_parser_condition (parser);
10897         /* Look for the `)'.  */
10898         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10899           cp_parser_skip_to_closing_parenthesis (parser, true, false,
10900                                                  /*consume_paren=*/true);
10901
10902         if (keyword == RID_IF)
10903           {
10904             bool nested_if;
10905             unsigned char in_statement;
10906
10907             /* Add the condition.  */
10908             finish_if_stmt_cond (condition, statement);
10909
10910             if (warn_duplicated_cond)
10911               warn_duplicated_cond_add_or_warn (token->location, condition,
10912                                                 &chain);
10913
10914             /* Parse the then-clause.  */
10915             in_statement = parser->in_statement;
10916             parser->in_statement |= IN_IF_STMT;
10917             cp_parser_implicitly_scoped_statement (parser, &nested_if,
10918                                                    guard_tinfo);
10919             parser->in_statement = in_statement;
10920
10921             finish_then_clause (statement);
10922
10923             /* If the next token is `else', parse the else-clause.  */
10924             if (cp_lexer_next_token_is_keyword (parser->lexer,
10925                                                 RID_ELSE))
10926               {
10927                 guard_tinfo
10928                   = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10929                 /* Consume the `else' keyword.  */
10930                 cp_lexer_consume_token (parser->lexer);
10931                 if (warn_duplicated_cond)
10932                   {
10933                     if (cp_lexer_next_token_is_keyword (parser->lexer,
10934                                                         RID_IF)
10935                         && chain == NULL)
10936                       {
10937                         /* We've got "if (COND) else if (COND2)".  Start
10938                            the condition chain and add COND as the first
10939                            element.  */
10940                         chain = new vec<tree> ();
10941                         if (!CONSTANT_CLASS_P (condition)
10942                             && !TREE_SIDE_EFFECTS (condition))
10943                         {
10944                           /* Wrap it in a NOP_EXPR so that we can set the
10945                              location of the condition.  */
10946                           tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10947                                            condition);
10948                           SET_EXPR_LOCATION (e, token->location);
10949                           chain->safe_push (e);
10950                         }
10951                       }
10952                     else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10953                                                               RID_IF))
10954                       {
10955                         /* This is if-else without subsequent if.  Zap the
10956                            condition chain; we would have already warned at
10957                            this point.  */
10958                         delete chain;
10959                         chain = NULL;
10960                       }
10961                   }
10962                 begin_else_clause (statement);
10963                 /* Parse the else-clause.  */
10964                 cp_parser_implicitly_scoped_statement (parser, NULL,
10965                                                        guard_tinfo, chain);
10966
10967                 finish_else_clause (statement);
10968
10969                 /* If we are currently parsing a then-clause, then
10970                    IF_P will not be NULL.  We set it to true to
10971                    indicate that this if statement has an else clause.
10972                    This may trigger the Wparentheses warning below
10973                    when we get back up to the parent if statement.  */
10974                 if (if_p != NULL)
10975                   *if_p = true;
10976               }
10977             else
10978               {
10979                 /* This if statement does not have an else clause.  If
10980                    NESTED_IF is true, then the then-clause has an if
10981                    statement which does have an else clause.  We warn
10982                    about the potential ambiguity.  */
10983                 if (nested_if)
10984                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10985                               "suggest explicit braces to avoid ambiguous"
10986                               " %<else%>");
10987                 if (warn_duplicated_cond)
10988                   {
10989                     /* We don't need the condition chain anymore.  */
10990                     delete chain;
10991                     chain = NULL;
10992                   }
10993               }
10994
10995             /* Now we're all done with the if-statement.  */
10996             finish_if_stmt (statement);
10997           }
10998         else
10999           {
11000             bool in_switch_statement_p;
11001             unsigned char in_statement;
11002
11003             /* Add the condition.  */
11004             finish_switch_cond (condition, statement);
11005
11006             /* Parse the body of the switch-statement.  */
11007             in_switch_statement_p = parser->in_switch_statement_p;
11008             in_statement = parser->in_statement;
11009             parser->in_switch_statement_p = true;
11010             parser->in_statement |= IN_SWITCH_STMT;
11011             cp_parser_implicitly_scoped_statement (parser, NULL,
11012                                                    guard_tinfo);
11013             parser->in_switch_statement_p = in_switch_statement_p;
11014             parser->in_statement = in_statement;
11015
11016             /* Now we're all done with the switch-statement.  */
11017             finish_switch_stmt (statement);
11018           }
11019
11020         return statement;
11021       }
11022       break;
11023
11024     default:
11025       cp_parser_error (parser, "expected selection-statement");
11026       return error_mark_node;
11027     }
11028 }
11029
11030 /* Parse a condition.
11031
11032    condition:
11033      expression
11034      type-specifier-seq declarator = initializer-clause
11035      type-specifier-seq declarator braced-init-list
11036
11037    GNU Extension:
11038
11039    condition:
11040      type-specifier-seq declarator asm-specification [opt]
11041        attributes [opt] = assignment-expression
11042
11043    Returns the expression that should be tested.  */
11044
11045 static tree
11046 cp_parser_condition (cp_parser* parser)
11047 {
11048   cp_decl_specifier_seq type_specifiers;
11049   const char *saved_message;
11050   int declares_class_or_enum;
11051
11052   /* Try the declaration first.  */
11053   cp_parser_parse_tentatively (parser);
11054   /* New types are not allowed in the type-specifier-seq for a
11055      condition.  */
11056   saved_message = parser->type_definition_forbidden_message;
11057   parser->type_definition_forbidden_message
11058     = G_("types may not be defined in conditions");
11059   /* Parse the type-specifier-seq.  */
11060   cp_parser_decl_specifier_seq (parser,
11061                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11062                                 &type_specifiers,
11063                                 &declares_class_or_enum);
11064   /* Restore the saved message.  */
11065   parser->type_definition_forbidden_message = saved_message;
11066   /* If all is well, we might be looking at a declaration.  */
11067   if (!cp_parser_error_occurred (parser))
11068     {
11069       tree decl;
11070       tree asm_specification;
11071       tree attributes;
11072       cp_declarator *declarator;
11073       tree initializer = NULL_TREE;
11074
11075       /* Parse the declarator.  */
11076       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11077                                          /*ctor_dtor_or_conv_p=*/NULL,
11078                                          /*parenthesized_p=*/NULL,
11079                                          /*member_p=*/false,
11080                                          /*friend_p=*/false);
11081       /* Parse the attributes.  */
11082       attributes = cp_parser_attributes_opt (parser);
11083       /* Parse the asm-specification.  */
11084       asm_specification = cp_parser_asm_specification_opt (parser);
11085       /* If the next token is not an `=' or '{', then we might still be
11086          looking at an expression.  For example:
11087
11088            if (A(a).x)
11089
11090          looks like a decl-specifier-seq and a declarator -- but then
11091          there is no `=', so this is an expression.  */
11092       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11093           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11094         cp_parser_simulate_error (parser);
11095         
11096       /* If we did see an `=' or '{', then we are looking at a declaration
11097          for sure.  */
11098       if (cp_parser_parse_definitely (parser))
11099         {
11100           tree pushed_scope;
11101           bool non_constant_p;
11102           bool flags = LOOKUP_ONLYCONVERTING;
11103
11104           /* Create the declaration.  */
11105           decl = start_decl (declarator, &type_specifiers,
11106                              /*initialized_p=*/true,
11107                              attributes, /*prefix_attributes=*/NULL_TREE,
11108                              &pushed_scope);
11109
11110           /* Parse the initializer.  */
11111           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11112             {
11113               initializer = cp_parser_braced_list (parser, &non_constant_p);
11114               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11115               flags = 0;
11116             }
11117           else
11118             {
11119               /* Consume the `='.  */
11120               cp_parser_require (parser, CPP_EQ, RT_EQ);
11121               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11122             }
11123           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11124             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11125
11126           /* Process the initializer.  */
11127           cp_finish_decl (decl,
11128                           initializer, !non_constant_p,
11129                           asm_specification,
11130                           flags);
11131
11132           if (pushed_scope)
11133             pop_scope (pushed_scope);
11134
11135           return convert_from_reference (decl);
11136         }
11137     }
11138   /* If we didn't even get past the declarator successfully, we are
11139      definitely not looking at a declaration.  */
11140   else
11141     cp_parser_abort_tentative_parse (parser);
11142
11143   /* Otherwise, we are looking at an expression.  */
11144   return cp_parser_expression (parser);
11145 }
11146
11147 /* Parses a for-statement or range-for-statement until the closing ')',
11148    not included. */
11149
11150 static tree
11151 cp_parser_for (cp_parser *parser, bool ivdep)
11152 {
11153   tree init, scope, decl;
11154   bool is_range_for;
11155
11156   /* Begin the for-statement.  */
11157   scope = begin_for_scope (&init);
11158
11159   /* Parse the initialization.  */
11160   is_range_for = cp_parser_for_init_statement (parser, &decl);
11161
11162   if (is_range_for)
11163     return cp_parser_range_for (parser, scope, init, decl, ivdep);
11164   else
11165     return cp_parser_c_for (parser, scope, init, ivdep);
11166 }
11167
11168 static tree
11169 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11170 {
11171   /* Normal for loop */
11172   tree condition = NULL_TREE;
11173   tree expression = NULL_TREE;
11174   tree stmt;
11175
11176   stmt = begin_for_stmt (scope, init);
11177   /* The for-init-statement has already been parsed in
11178      cp_parser_for_init_statement, so no work is needed here.  */
11179   finish_for_init_stmt (stmt);
11180
11181   /* If there's a condition, process it.  */
11182   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11183     condition = cp_parser_condition (parser);
11184   else if (ivdep)
11185     {
11186       cp_parser_error (parser, "missing loop condition in loop with "
11187                        "%<GCC ivdep%> pragma");
11188       condition = error_mark_node;
11189     }
11190   finish_for_cond (condition, stmt, ivdep);
11191   /* Look for the `;'.  */
11192   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11193
11194   /* If there's an expression, process it.  */
11195   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11196     expression = cp_parser_expression (parser);
11197   finish_for_expr (expression, stmt);
11198
11199   return stmt;
11200 }
11201
11202 /* Tries to parse a range-based for-statement:
11203
11204   range-based-for:
11205     decl-specifier-seq declarator : expression
11206
11207   The decl-specifier-seq declarator and the `:' are already parsed by
11208   cp_parser_for_init_statement. If processing_template_decl it returns a
11209   newly created RANGE_FOR_STMT; if not, it is converted to a
11210   regular FOR_STMT.  */
11211
11212 static tree
11213 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11214                      bool ivdep)
11215 {
11216   tree stmt, range_expr;
11217   cxx_binding *binding = NULL;
11218   tree name = NULL_TREE;
11219
11220   /* Get the range declaration momentarily out of the way so that
11221      the range expression doesn't clash with it. */
11222   if (range_decl != error_mark_node)
11223     {
11224       name = DECL_NAME (range_decl);
11225       binding = IDENTIFIER_BINDING (name);
11226       IDENTIFIER_BINDING (name) = binding->previous;
11227     }
11228
11229   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11230     {
11231       bool expr_non_constant_p;
11232       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11233     }
11234   else
11235     range_expr = cp_parser_expression (parser);
11236
11237   /* Put the range declaration back into scope. */
11238   if (range_decl != error_mark_node)
11239     {
11240       binding->previous = IDENTIFIER_BINDING (name);
11241       IDENTIFIER_BINDING (name) = binding;
11242     }
11243
11244   /* If in template, STMT is converted to a normal for-statement
11245      at instantiation. If not, it is done just ahead. */
11246   if (processing_template_decl)
11247     {
11248       if (check_for_bare_parameter_packs (range_expr))
11249         range_expr = error_mark_node;
11250       stmt = begin_range_for_stmt (scope, init);
11251       if (ivdep)
11252         RANGE_FOR_IVDEP (stmt) = 1;
11253       finish_range_for_decl (stmt, range_decl, range_expr);
11254       if (!type_dependent_expression_p (range_expr)
11255           /* do_auto_deduction doesn't mess with template init-lists.  */
11256           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11257         do_range_for_auto_deduction (range_decl, range_expr);
11258     }
11259   else
11260     {
11261       stmt = begin_for_stmt (scope, init);
11262       stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11263     }
11264   return stmt;
11265 }
11266
11267 /* Subroutine of cp_convert_range_for: given the initializer expression,
11268    builds up the range temporary.  */
11269
11270 static tree
11271 build_range_temp (tree range_expr)
11272 {
11273   tree range_type, range_temp;
11274
11275   /* Find out the type deduced by the declaration
11276      `auto &&__range = range_expr'.  */
11277   range_type = cp_build_reference_type (make_auto (), true);
11278   range_type = do_auto_deduction (range_type, range_expr,
11279                                   type_uses_auto (range_type));
11280
11281   /* Create the __range variable.  */
11282   range_temp = build_decl (input_location, VAR_DECL,
11283                            get_identifier ("__for_range"), range_type);
11284   TREE_USED (range_temp) = 1;
11285   DECL_ARTIFICIAL (range_temp) = 1;
11286
11287   return range_temp;
11288 }
11289
11290 /* Used by cp_parser_range_for in template context: we aren't going to
11291    do a full conversion yet, but we still need to resolve auto in the
11292    type of the for-range-declaration if present.  This is basically
11293    a shortcut version of cp_convert_range_for.  */
11294
11295 static void
11296 do_range_for_auto_deduction (tree decl, tree range_expr)
11297 {
11298   tree auto_node = type_uses_auto (TREE_TYPE (decl));
11299   if (auto_node)
11300     {
11301       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11302       range_temp = convert_from_reference (build_range_temp (range_expr));
11303       iter_type = (cp_parser_perform_range_for_lookup
11304                    (range_temp, &begin_dummy, &end_dummy));
11305       if (iter_type)
11306         {
11307           iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11308                                   iter_type);
11309           iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11310                                             tf_warning_or_error);
11311           TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11312                                                 iter_decl, auto_node);
11313         }
11314     }
11315 }
11316
11317 /* Converts a range-based for-statement into a normal
11318    for-statement, as per the definition.
11319
11320       for (RANGE_DECL : RANGE_EXPR)
11321         BLOCK
11322
11323    should be equivalent to:
11324
11325       {
11326         auto &&__range = RANGE_EXPR;
11327         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11328               __begin != __end;
11329               ++__begin)
11330           {
11331               RANGE_DECL = *__begin;
11332               BLOCK
11333           }
11334       }
11335
11336    If RANGE_EXPR is an array:
11337         BEGIN_EXPR = __range
11338         END_EXPR = __range + ARRAY_SIZE(__range)
11339    Else if RANGE_EXPR has a member 'begin' or 'end':
11340         BEGIN_EXPR = __range.begin()
11341         END_EXPR = __range.end()
11342    Else:
11343         BEGIN_EXPR = begin(__range)
11344         END_EXPR = end(__range);
11345
11346    If __range has a member 'begin' but not 'end', or vice versa, we must
11347    still use the second alternative (it will surely fail, however).
11348    When calling begin()/end() in the third alternative we must use
11349    argument dependent lookup, but always considering 'std' as an associated
11350    namespace.  */
11351
11352 tree
11353 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11354                       bool ivdep)
11355 {
11356   tree begin, end;
11357   tree iter_type, begin_expr, end_expr;
11358   tree condition, expression;
11359
11360   if (range_decl == error_mark_node || range_expr == error_mark_node)
11361     /* If an error happened previously do nothing or else a lot of
11362        unhelpful errors would be issued.  */
11363     begin_expr = end_expr = iter_type = error_mark_node;
11364   else
11365     {
11366       tree range_temp;
11367
11368       if (VAR_P (range_expr)
11369           && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11370         /* Can't bind a reference to an array of runtime bound.  */
11371         range_temp = range_expr;
11372       else
11373         {
11374           range_temp = build_range_temp (range_expr);
11375           pushdecl (range_temp);
11376           cp_finish_decl (range_temp, range_expr,
11377                           /*is_constant_init*/false, NULL_TREE,
11378                           LOOKUP_ONLYCONVERTING);
11379           range_temp = convert_from_reference (range_temp);
11380         }
11381       iter_type = cp_parser_perform_range_for_lookup (range_temp,
11382                                                       &begin_expr, &end_expr);
11383     }
11384
11385   /* The new for initialization statement.  */
11386   begin = build_decl (input_location, VAR_DECL,
11387                       get_identifier ("__for_begin"), iter_type);
11388   TREE_USED (begin) = 1;
11389   DECL_ARTIFICIAL (begin) = 1;
11390   pushdecl (begin);
11391   cp_finish_decl (begin, begin_expr,
11392                   /*is_constant_init*/false, NULL_TREE,
11393                   LOOKUP_ONLYCONVERTING);
11394
11395   if (cxx_dialect >= cxx1z)
11396     iter_type = cv_unqualified (TREE_TYPE (end_expr));
11397   end = build_decl (input_location, VAR_DECL,
11398                     get_identifier ("__for_end"), iter_type);
11399   TREE_USED (end) = 1;
11400   DECL_ARTIFICIAL (end) = 1;
11401   pushdecl (end);
11402   cp_finish_decl (end, end_expr,
11403                   /*is_constant_init*/false, NULL_TREE,
11404                   LOOKUP_ONLYCONVERTING);
11405
11406   finish_for_init_stmt (statement);
11407
11408   /* The new for condition.  */
11409   condition = build_x_binary_op (input_location, NE_EXPR,
11410                                  begin, ERROR_MARK,
11411                                  end, ERROR_MARK,
11412                                  NULL, tf_warning_or_error);
11413   finish_for_cond (condition, statement, ivdep);
11414
11415   /* The new increment expression.  */
11416   expression = finish_unary_op_expr (input_location,
11417                                      PREINCREMENT_EXPR, begin,
11418                                      tf_warning_or_error);
11419   finish_for_expr (expression, statement);
11420
11421   /* The declaration is initialized with *__begin inside the loop body.  */
11422   cp_finish_decl (range_decl,
11423                   build_x_indirect_ref (input_location, begin, RO_NULL,
11424                                         tf_warning_or_error),
11425                   /*is_constant_init*/false, NULL_TREE,
11426                   LOOKUP_ONLYCONVERTING);
11427
11428   return statement;
11429 }
11430
11431 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11432    We need to solve both at the same time because the method used
11433    depends on the existence of members begin or end.
11434    Returns the type deduced for the iterator expression.  */
11435
11436 static tree
11437 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11438 {
11439   if (error_operand_p (range))
11440     {
11441       *begin = *end = error_mark_node;
11442       return error_mark_node;
11443     }
11444
11445   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11446     {
11447       error ("range-based %<for%> expression of type %qT "
11448              "has incomplete type", TREE_TYPE (range));
11449       *begin = *end = error_mark_node;
11450       return error_mark_node;
11451     }
11452   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11453     {
11454       /* If RANGE is an array, we will use pointer arithmetic.  */
11455       *begin = range;
11456       *end = build_binary_op (input_location, PLUS_EXPR,
11457                               range,
11458                               array_type_nelts_top (TREE_TYPE (range)),
11459                               0);
11460       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11461     }
11462   else
11463     {
11464       /* If it is not an array, we must do a bit of magic.  */
11465       tree id_begin, id_end;
11466       tree member_begin, member_end;
11467
11468       *begin = *end = error_mark_node;
11469
11470       id_begin = get_identifier ("begin");
11471       id_end = get_identifier ("end");
11472       member_begin = lookup_member (TREE_TYPE (range), id_begin,
11473                                     /*protect=*/2, /*want_type=*/false,
11474                                     tf_warning_or_error);
11475       member_end = lookup_member (TREE_TYPE (range), id_end,
11476                                   /*protect=*/2, /*want_type=*/false,
11477                                   tf_warning_or_error);
11478
11479       if (member_begin != NULL_TREE || member_end != NULL_TREE)
11480         {
11481           /* Use the member functions.  */
11482           if (member_begin != NULL_TREE)
11483             *begin = cp_parser_range_for_member_function (range, id_begin);
11484           else
11485             error ("range-based %<for%> expression of type %qT has an "
11486                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11487
11488           if (member_end != NULL_TREE)
11489             *end = cp_parser_range_for_member_function (range, id_end);
11490           else
11491             error ("range-based %<for%> expression of type %qT has a "
11492                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11493         }
11494       else
11495         {
11496           /* Use global functions with ADL.  */
11497           vec<tree, va_gc> *vec;
11498           vec = make_tree_vector ();
11499
11500           vec_safe_push (vec, range);
11501
11502           member_begin = perform_koenig_lookup (id_begin, vec,
11503                                                 tf_warning_or_error);
11504           *begin = finish_call_expr (member_begin, &vec, false, true,
11505                                      tf_warning_or_error);
11506           member_end = perform_koenig_lookup (id_end, vec,
11507                                               tf_warning_or_error);
11508           *end = finish_call_expr (member_end, &vec, false, true,
11509                                    tf_warning_or_error);
11510
11511           release_tree_vector (vec);
11512         }
11513
11514       /* Last common checks.  */
11515       if (*begin == error_mark_node || *end == error_mark_node)
11516         {
11517           /* If one of the expressions is an error do no more checks.  */
11518           *begin = *end = error_mark_node;
11519           return error_mark_node;
11520         }
11521       else if (type_dependent_expression_p (*begin)
11522                || type_dependent_expression_p (*end))
11523         /* Can happen, when, eg, in a template context, Koenig lookup
11524            can't resolve begin/end (c++/58503).  */
11525         return NULL_TREE;
11526       else
11527         {
11528           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11529           /* The unqualified type of the __begin and __end temporaries should
11530              be the same, as required by the multiple auto declaration.  */
11531           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11532             {
11533               if (cxx_dialect >= cxx1z
11534                   && (build_x_binary_op (input_location, NE_EXPR,
11535                                          *begin, ERROR_MARK,
11536                                          *end, ERROR_MARK,
11537                                          NULL, tf_none)
11538                       != error_mark_node))
11539                 /* P0184R0 allows __begin and __end to have different types,
11540                    but make sure they are comparable so we can give a better
11541                    diagnostic.  */;
11542               else
11543                 error ("inconsistent begin/end types in range-based %<for%> "
11544                        "statement: %qT and %qT",
11545                        TREE_TYPE (*begin), TREE_TYPE (*end));
11546             }
11547           return iter_type;
11548         }
11549     }
11550 }
11551
11552 /* Helper function for cp_parser_perform_range_for_lookup.
11553    Builds a tree for RANGE.IDENTIFIER().  */
11554
11555 static tree
11556 cp_parser_range_for_member_function (tree range, tree identifier)
11557 {
11558   tree member, res;
11559   vec<tree, va_gc> *vec;
11560
11561   member = finish_class_member_access_expr (range, identifier,
11562                                             false, tf_warning_or_error);
11563   if (member == error_mark_node)
11564     return error_mark_node;
11565
11566   vec = make_tree_vector ();
11567   res = finish_call_expr (member, &vec,
11568                           /*disallow_virtual=*/false,
11569                           /*koenig_p=*/false,
11570                           tf_warning_or_error);
11571   release_tree_vector (vec);
11572   return res;
11573 }
11574
11575 /* Parse an iteration-statement.
11576
11577    iteration-statement:
11578      while ( condition ) statement
11579      do statement while ( expression ) ;
11580      for ( for-init-statement condition [opt] ; expression [opt] )
11581        statement
11582
11583    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
11584
11585 static tree
11586 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11587 {
11588   cp_token *token;
11589   enum rid keyword;
11590   tree statement;
11591   unsigned char in_statement;
11592   token_indent_info guard_tinfo;
11593
11594   /* Peek at the next token.  */
11595   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11596   if (!token)
11597     return error_mark_node;
11598
11599   guard_tinfo = get_token_indent_info (token);
11600
11601   /* Remember whether or not we are already within an iteration
11602      statement.  */
11603   in_statement = parser->in_statement;
11604
11605   /* See what kind of keyword it is.  */
11606   keyword = token->keyword;
11607   switch (keyword)
11608     {
11609     case RID_WHILE:
11610       {
11611         tree condition;
11612
11613         /* Begin the while-statement.  */
11614         statement = begin_while_stmt ();
11615         /* Look for the `('.  */
11616         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11617         /* Parse the condition.  */
11618         condition = cp_parser_condition (parser);
11619         finish_while_stmt_cond (condition, statement, ivdep);
11620         /* Look for the `)'.  */
11621         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11622         /* Parse the dependent statement.  */
11623         parser->in_statement = IN_ITERATION_STMT;
11624         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11625         parser->in_statement = in_statement;
11626         /* We're done with the while-statement.  */
11627         finish_while_stmt (statement);
11628       }
11629       break;
11630
11631     case RID_DO:
11632       {
11633         tree expression;
11634
11635         /* Begin the do-statement.  */
11636         statement = begin_do_stmt ();
11637         /* Parse the body of the do-statement.  */
11638         parser->in_statement = IN_ITERATION_STMT;
11639         cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11640         parser->in_statement = in_statement;
11641         finish_do_body (statement);
11642         /* Look for the `while' keyword.  */
11643         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11644         /* Look for the `('.  */
11645         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11646         /* Parse the expression.  */
11647         expression = cp_parser_expression (parser);
11648         /* We're done with the do-statement.  */
11649         finish_do_stmt (expression, statement, ivdep);
11650         /* Look for the `)'.  */
11651         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11652         /* Look for the `;'.  */
11653         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11654       }
11655       break;
11656
11657     case RID_FOR:
11658       {
11659         /* Look for the `('.  */
11660         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11661
11662         statement = cp_parser_for (parser, ivdep);
11663
11664         /* Look for the `)'.  */
11665         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11666
11667         /* Parse the body of the for-statement.  */
11668         parser->in_statement = IN_ITERATION_STMT;
11669         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11670         parser->in_statement = in_statement;
11671
11672         /* We're done with the for-statement.  */
11673         finish_for_stmt (statement);
11674       }
11675       break;
11676
11677     default:
11678       cp_parser_error (parser, "expected iteration-statement");
11679       statement = error_mark_node;
11680       break;
11681     }
11682
11683   return statement;
11684 }
11685
11686 /* Parse a for-init-statement or the declarator of a range-based-for.
11687    Returns true if a range-based-for declaration is seen.
11688
11689    for-init-statement:
11690      expression-statement
11691      simple-declaration  */
11692
11693 static bool
11694 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11695 {
11696   /* If the next token is a `;', then we have an empty
11697      expression-statement.  Grammatically, this is also a
11698      simple-declaration, but an invalid one, because it does not
11699      declare anything.  Therefore, if we did not handle this case
11700      specially, we would issue an error message about an invalid
11701      declaration.  */
11702   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11703     {
11704       bool is_range_for = false;
11705       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11706
11707       /* A colon is used in range-based for.  */
11708       parser->colon_corrects_to_scope_p = false;
11709
11710       /* We're going to speculatively look for a declaration, falling back
11711          to an expression, if necessary.  */
11712       cp_parser_parse_tentatively (parser);
11713       /* Parse the declaration.  */
11714       cp_parser_simple_declaration (parser,
11715                                     /*function_definition_allowed_p=*/false,
11716                                     decl);
11717       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11718       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11719         {
11720           /* It is a range-for, consume the ':' */
11721           cp_lexer_consume_token (parser->lexer);
11722           is_range_for = true;
11723           if (cxx_dialect < cxx11)
11724             {
11725               pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11726                        "range-based %<for%> loops only available with "
11727                        "-std=c++11 or -std=gnu++11");
11728               *decl = error_mark_node;
11729             }
11730         }
11731       else
11732           /* The ';' is not consumed yet because we told
11733              cp_parser_simple_declaration not to.  */
11734           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11735
11736       if (cp_parser_parse_definitely (parser))
11737         return is_range_for;
11738       /* If the tentative parse failed, then we shall need to look for an
11739          expression-statement.  */
11740     }
11741   /* If we are here, it is an expression-statement.  */
11742   cp_parser_expression_statement (parser, NULL_TREE);
11743   return false;
11744 }
11745
11746 /* Parse a jump-statement.
11747
11748    jump-statement:
11749      break ;
11750      continue ;
11751      return expression [opt] ;
11752      return braced-init-list ;
11753      goto identifier ;
11754
11755    GNU extension:
11756
11757    jump-statement:
11758      goto * expression ;
11759
11760    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
11761
11762 static tree
11763 cp_parser_jump_statement (cp_parser* parser)
11764 {
11765   tree statement = error_mark_node;
11766   cp_token *token;
11767   enum rid keyword;
11768   unsigned char in_statement;
11769
11770   /* Peek at the next token.  */
11771   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11772   if (!token)
11773     return error_mark_node;
11774
11775   /* See what kind of keyword it is.  */
11776   keyword = token->keyword;
11777   switch (keyword)
11778     {
11779     case RID_BREAK:
11780       in_statement = parser->in_statement & ~IN_IF_STMT;      
11781       switch (in_statement)
11782         {
11783         case 0:
11784           error_at (token->location, "break statement not within loop or switch");
11785           break;
11786         default:
11787           gcc_assert ((in_statement & IN_SWITCH_STMT)
11788                       || in_statement == IN_ITERATION_STMT);
11789           statement = finish_break_stmt ();
11790           if (in_statement == IN_ITERATION_STMT)
11791             break_maybe_infinite_loop ();
11792           break;
11793         case IN_OMP_BLOCK:
11794           error_at (token->location, "invalid exit from OpenMP structured block");
11795           break;
11796         case IN_OMP_FOR:
11797           error_at (token->location, "break statement used with OpenMP for loop");
11798           break;
11799         case IN_CILK_SIMD_FOR:
11800           error_at (token->location, "break statement used with Cilk Plus for loop");
11801           break;
11802         }
11803       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11804       break;
11805
11806     case RID_CONTINUE:
11807       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11808         {
11809         case 0:
11810           error_at (token->location, "continue statement not within a loop");
11811           break;
11812         case IN_CILK_SIMD_FOR:
11813           error_at (token->location,
11814                     "continue statement within %<#pragma simd%> loop body");
11815           /* Fall through.  */
11816         case IN_ITERATION_STMT:
11817         case IN_OMP_FOR:
11818           statement = finish_continue_stmt ();
11819           break;
11820         case IN_OMP_BLOCK:
11821           error_at (token->location, "invalid exit from OpenMP structured block");
11822           break;
11823         default:
11824           gcc_unreachable ();
11825         }
11826       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11827       break;
11828
11829     case RID_RETURN:
11830       {
11831         tree expr;
11832         bool expr_non_constant_p;
11833
11834         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11835           {
11836             cp_lexer_set_source_position (parser->lexer);
11837             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11838             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11839           }
11840         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11841           expr = cp_parser_expression (parser);
11842         else
11843           /* If the next token is a `;', then there is no
11844              expression.  */
11845           expr = NULL_TREE;
11846
11847         if ((flag_sanitize & SANITIZE_UI_OVERFLOW)
11848              && isan_internal_fn_p (expr))
11849           {
11850             int uns_p = TYPE_UNSIGNED (TREE_TYPE
11851                                       (DECL_RESULT (current_function_decl)));
11852             isan_maybe_change_ifn_sign_arg (&expr, uns_p);
11853           }
11854
11855         /* Build the return-statement.  */
11856         statement = finish_return_stmt (expr);
11857         /* Look for the final `;'.  */
11858         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11859       }
11860       break;
11861
11862     case RID_GOTO:
11863       if (parser->in_function_body
11864           && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11865         {
11866           error ("%<goto%> in %<constexpr%> function");
11867           cp_function_chain->invalid_constexpr = true;
11868         }
11869
11870       /* Create the goto-statement.  */
11871       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11872         {
11873           /* Issue a warning about this use of a GNU extension.  */
11874           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11875           /* Consume the '*' token.  */
11876           cp_lexer_consume_token (parser->lexer);
11877           /* Parse the dependent expression.  */
11878           finish_goto_stmt (cp_parser_expression (parser));
11879         }
11880       else
11881         finish_goto_stmt (cp_parser_identifier (parser));
11882       /* Look for the final `;'.  */
11883       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11884       break;
11885
11886     default:
11887       cp_parser_error (parser, "expected jump-statement");
11888       break;
11889     }
11890
11891   return statement;
11892 }
11893
11894 /* Parse a declaration-statement.
11895
11896    declaration-statement:
11897      block-declaration  */
11898
11899 static void
11900 cp_parser_declaration_statement (cp_parser* parser)
11901 {
11902   void *p;
11903
11904   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11905   p = obstack_alloc (&declarator_obstack, 0);
11906
11907  /* Parse the block-declaration.  */
11908   cp_parser_block_declaration (parser, /*statement_p=*/true);
11909
11910   /* Free any declarators allocated.  */
11911   obstack_free (&declarator_obstack, p);
11912 }
11913
11914 /* Some dependent statements (like `if (cond) statement'), are
11915    implicitly in their own scope.  In other words, if the statement is
11916    a single statement (as opposed to a compound-statement), it is
11917    none-the-less treated as if it were enclosed in braces.  Any
11918    declarations appearing in the dependent statement are out of scope
11919    after control passes that point.  This function parses a statement,
11920    but ensures that is in its own scope, even if it is not a
11921    compound-statement.
11922
11923    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11924    is a (possibly labeled) if statement which is not enclosed in
11925    braces and has an else clause.  This is used to implement
11926    -Wparentheses.
11927
11928    CHAIN is a vector of if-else-if conditions.  This is used to implement
11929    -Wduplicated-cond.
11930
11931    Returns the new statement.  */
11932
11933 static tree
11934 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11935                                        const token_indent_info &guard_tinfo,
11936                                        vec<tree> *chain)
11937 {
11938   tree statement;
11939   location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11940   token_indent_info body_tinfo
11941     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11942
11943   if (if_p != NULL)
11944     *if_p = false;
11945
11946   /* Mark if () ; with a special NOP_EXPR.  */
11947   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11948     {
11949       cp_lexer_consume_token (parser->lexer);
11950       statement = add_stmt (build_empty_stmt (body_loc));
11951
11952       if (guard_tinfo.keyword == RID_IF
11953           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11954         warning_at (body_loc, OPT_Wempty_body,
11955                     "suggest braces around empty body in an %<if%> statement");
11956       else if (guard_tinfo.keyword == RID_ELSE)
11957         warning_at (body_loc, OPT_Wempty_body,
11958                     "suggest braces around empty body in an %<else%> statement");
11959     }
11960   /* if a compound is opened, we simply parse the statement directly.  */
11961   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11962     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11963   /* If the token is not a `{', then we must take special action.  */
11964   else
11965     {
11966       /* Create a compound-statement.  */
11967       statement = begin_compound_stmt (0);
11968       /* Parse the dependent-statement.  */
11969       cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11970       /* Finish the dummy compound-statement.  */
11971       finish_compound_stmt (statement);
11972     }
11973
11974   token_indent_info next_tinfo
11975     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11976   warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11977
11978   /* Return the statement.  */
11979   return statement;
11980 }
11981
11982 /* For some dependent statements (like `while (cond) statement'), we
11983    have already created a scope.  Therefore, even if the dependent
11984    statement is a compound-statement, we do not want to create another
11985    scope.  */
11986
11987 static void
11988 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11989                                     const token_indent_info &guard_tinfo)
11990 {
11991   /* If the token is a `{', then we must take special action.  */
11992   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11993     {
11994       token_indent_info body_tinfo
11995         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11996
11997       cp_parser_statement (parser, NULL_TREE, false, if_p);
11998       token_indent_info next_tinfo
11999         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12000       warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12001     }
12002   else
12003     {
12004       /* Avoid calling cp_parser_compound_statement, so that we
12005          don't create a new scope.  Do everything else by hand.  */
12006       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12007       /* If the next keyword is `__label__' we have a label declaration.  */
12008       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12009         cp_parser_label_declaration (parser);
12010       /* Parse an (optional) statement-seq.  */
12011       cp_parser_statement_seq_opt (parser, NULL_TREE);
12012       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12013     }
12014 }
12015
12016 /* Declarations [gram.dcl.dcl] */
12017
12018 /* Parse an optional declaration-sequence.
12019
12020    declaration-seq:
12021      declaration
12022      declaration-seq declaration  */
12023
12024 static void
12025 cp_parser_declaration_seq_opt (cp_parser* parser)
12026 {
12027   while (true)
12028     {
12029       cp_token *token;
12030
12031       token = cp_lexer_peek_token (parser->lexer);
12032
12033       if (token->type == CPP_CLOSE_BRACE
12034           || token->type == CPP_EOF
12035           || token->type == CPP_PRAGMA_EOL)
12036         break;
12037
12038       if (token->type == CPP_SEMICOLON)
12039         {
12040           /* A declaration consisting of a single semicolon is
12041              invalid.  Allow it unless we're being pedantic.  */
12042           cp_lexer_consume_token (parser->lexer);
12043           if (!in_system_header_at (input_location))
12044             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12045           continue;
12046         }
12047
12048       /* If we're entering or exiting a region that's implicitly
12049          extern "C", modify the lang context appropriately.  */
12050       if (!parser->implicit_extern_c && token->implicit_extern_c)
12051         {
12052           push_lang_context (lang_name_c);
12053           parser->implicit_extern_c = true;
12054         }
12055       else if (parser->implicit_extern_c && !token->implicit_extern_c)
12056         {
12057           pop_lang_context ();
12058           parser->implicit_extern_c = false;
12059         }
12060
12061       if (token->type == CPP_PRAGMA)
12062         {
12063           /* A top-level declaration can consist solely of a #pragma.
12064              A nested declaration cannot, so this is done here and not
12065              in cp_parser_declaration.  (A #pragma at block scope is
12066              handled in cp_parser_statement.)  */
12067           cp_parser_pragma (parser, pragma_external, NULL);
12068           continue;
12069         }
12070
12071       /* Parse the declaration itself.  */
12072       cp_parser_declaration (parser);
12073     }
12074 }
12075
12076 /* Parse a declaration.
12077
12078    declaration:
12079      block-declaration
12080      function-definition
12081      template-declaration
12082      explicit-instantiation
12083      explicit-specialization
12084      linkage-specification
12085      namespace-definition
12086
12087    GNU extension:
12088
12089    declaration:
12090       __extension__ declaration */
12091
12092 static void
12093 cp_parser_declaration (cp_parser* parser)
12094 {
12095   cp_token token1;
12096   cp_token token2;
12097   int saved_pedantic;
12098   void *p;
12099   tree attributes = NULL_TREE;
12100
12101   /* Check for the `__extension__' keyword.  */
12102   if (cp_parser_extension_opt (parser, &saved_pedantic))
12103     {
12104       /* Parse the qualified declaration.  */
12105       cp_parser_declaration (parser);
12106       /* Restore the PEDANTIC flag.  */
12107       pedantic = saved_pedantic;
12108
12109       return;
12110     }
12111
12112   /* Try to figure out what kind of declaration is present.  */
12113   token1 = *cp_lexer_peek_token (parser->lexer);
12114
12115   if (token1.type != CPP_EOF)
12116     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12117   else
12118     {
12119       token2.type = CPP_EOF;
12120       token2.keyword = RID_MAX;
12121     }
12122
12123   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12124   p = obstack_alloc (&declarator_obstack, 0);
12125
12126   /* If the next token is `extern' and the following token is a string
12127      literal, then we have a linkage specification.  */
12128   if (token1.keyword == RID_EXTERN
12129       && cp_parser_is_pure_string_literal (&token2))
12130     cp_parser_linkage_specification (parser);
12131   /* If the next token is `template', then we have either a template
12132      declaration, an explicit instantiation, or an explicit
12133      specialization.  */
12134   else if (token1.keyword == RID_TEMPLATE)
12135     {
12136       /* `template <>' indicates a template specialization.  */
12137       if (token2.type == CPP_LESS
12138           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12139         cp_parser_explicit_specialization (parser);
12140       /* `template <' indicates a template declaration.  */
12141       else if (token2.type == CPP_LESS)
12142         cp_parser_template_declaration (parser, /*member_p=*/false);
12143       /* Anything else must be an explicit instantiation.  */
12144       else
12145         cp_parser_explicit_instantiation (parser);
12146     }
12147   /* If the next token is `export', then we have a template
12148      declaration.  */
12149   else if (token1.keyword == RID_EXPORT)
12150     cp_parser_template_declaration (parser, /*member_p=*/false);
12151   /* If the next token is `extern', 'static' or 'inline' and the one
12152      after that is `template', we have a GNU extended explicit
12153      instantiation directive.  */
12154   else if (cp_parser_allow_gnu_extensions_p (parser)
12155            && (token1.keyword == RID_EXTERN
12156                || token1.keyword == RID_STATIC
12157                || token1.keyword == RID_INLINE)
12158            && token2.keyword == RID_TEMPLATE)
12159     cp_parser_explicit_instantiation (parser);
12160   /* If the next token is `namespace', check for a named or unnamed
12161      namespace definition.  */
12162   else if (token1.keyword == RID_NAMESPACE
12163            && (/* A named namespace definition.  */
12164                (token2.type == CPP_NAME
12165                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12166                     != CPP_EQ))
12167                || (token2.type == CPP_OPEN_SQUARE
12168                    && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12169                    == CPP_OPEN_SQUARE)
12170                /* An unnamed namespace definition.  */
12171                || token2.type == CPP_OPEN_BRACE
12172                || token2.keyword == RID_ATTRIBUTE))
12173     cp_parser_namespace_definition (parser);
12174   /* An inline (associated) namespace definition.  */
12175   else if (token1.keyword == RID_INLINE
12176            && token2.keyword == RID_NAMESPACE)
12177     cp_parser_namespace_definition (parser);
12178   /* Objective-C++ declaration/definition.  */
12179   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12180     cp_parser_objc_declaration (parser, NULL_TREE);
12181   else if (c_dialect_objc ()
12182            && token1.keyword == RID_ATTRIBUTE
12183            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12184     cp_parser_objc_declaration (parser, attributes);
12185   /* At this point we may have a template declared by a concept
12186      introduction.  */
12187   else if (flag_concepts
12188            && cp_parser_template_declaration_after_export (parser,
12189                                                            /*member_p=*/false))
12190     /* We did.  */;
12191   else
12192     /* Try to parse a block-declaration, or a function-definition.  */
12193     cp_parser_block_declaration (parser, /*statement_p=*/false);
12194
12195   /* Free any declarators allocated.  */
12196   obstack_free (&declarator_obstack, p);
12197 }
12198
12199 /* Parse a block-declaration.
12200
12201    block-declaration:
12202      simple-declaration
12203      asm-definition
12204      namespace-alias-definition
12205      using-declaration
12206      using-directive
12207
12208    GNU Extension:
12209
12210    block-declaration:
12211      __extension__ block-declaration
12212
12213    C++0x Extension:
12214
12215    block-declaration:
12216      static_assert-declaration
12217
12218    If STATEMENT_P is TRUE, then this block-declaration is occurring as
12219    part of a declaration-statement.  */
12220
12221 static void
12222 cp_parser_block_declaration (cp_parser *parser,
12223                              bool      statement_p)
12224 {
12225   cp_token *token1;
12226   int saved_pedantic;
12227
12228   /* Check for the `__extension__' keyword.  */
12229   if (cp_parser_extension_opt (parser, &saved_pedantic))
12230     {
12231       /* Parse the qualified declaration.  */
12232       cp_parser_block_declaration (parser, statement_p);
12233       /* Restore the PEDANTIC flag.  */
12234       pedantic = saved_pedantic;
12235
12236       return;
12237     }
12238
12239   /* Peek at the next token to figure out which kind of declaration is
12240      present.  */
12241   token1 = cp_lexer_peek_token (parser->lexer);
12242
12243   /* If the next keyword is `asm', we have an asm-definition.  */
12244   if (token1->keyword == RID_ASM)
12245     {
12246       if (statement_p)
12247         cp_parser_commit_to_tentative_parse (parser);
12248       cp_parser_asm_definition (parser);
12249     }
12250   /* If the next keyword is `namespace', we have a
12251      namespace-alias-definition.  */
12252   else if (token1->keyword == RID_NAMESPACE)
12253     cp_parser_namespace_alias_definition (parser);
12254   /* If the next keyword is `using', we have a
12255      using-declaration, a using-directive, or an alias-declaration.  */
12256   else if (token1->keyword == RID_USING)
12257     {
12258       cp_token *token2;
12259
12260       if (statement_p)
12261         cp_parser_commit_to_tentative_parse (parser);
12262       /* If the token after `using' is `namespace', then we have a
12263          using-directive.  */
12264       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12265       if (token2->keyword == RID_NAMESPACE)
12266         cp_parser_using_directive (parser);
12267       /* If the second token after 'using' is '=', then we have an
12268          alias-declaration.  */
12269       else if (cxx_dialect >= cxx11
12270                && token2->type == CPP_NAME
12271                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12272                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12273         cp_parser_alias_declaration (parser);
12274       /* Otherwise, it's a using-declaration.  */
12275       else
12276         cp_parser_using_declaration (parser,
12277                                      /*access_declaration_p=*/false);
12278     }
12279   /* If the next keyword is `__label__' we have a misplaced label
12280      declaration.  */
12281   else if (token1->keyword == RID_LABEL)
12282     {
12283       cp_lexer_consume_token (parser->lexer);
12284       error_at (token1->location, "%<__label__%> not at the beginning of a block");
12285       cp_parser_skip_to_end_of_statement (parser);
12286       /* If the next token is now a `;', consume it.  */
12287       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12288         cp_lexer_consume_token (parser->lexer);
12289     }
12290   /* If the next token is `static_assert' we have a static assertion.  */
12291   else if (token1->keyword == RID_STATIC_ASSERT)
12292     cp_parser_static_assert (parser, /*member_p=*/false);
12293   /* Anything else must be a simple-declaration.  */
12294   else
12295     cp_parser_simple_declaration (parser, !statement_p,
12296                                   /*maybe_range_for_decl*/NULL);
12297 }
12298
12299 /* Parse a simple-declaration.
12300
12301    simple-declaration:
12302      decl-specifier-seq [opt] init-declarator-list [opt] ;
12303
12304    init-declarator-list:
12305      init-declarator
12306      init-declarator-list , init-declarator
12307
12308    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12309    function-definition as a simple-declaration.
12310
12311    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12312    parsed declaration if it is an uninitialized single declarator not followed
12313    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12314    if present, will not be consumed.  */
12315
12316 static void
12317 cp_parser_simple_declaration (cp_parser* parser,
12318                               bool function_definition_allowed_p,
12319                               tree *maybe_range_for_decl)
12320 {
12321   cp_decl_specifier_seq decl_specifiers;
12322   int declares_class_or_enum;
12323   bool saw_declarator;
12324   location_t comma_loc = UNKNOWN_LOCATION;
12325   location_t init_loc = UNKNOWN_LOCATION;
12326
12327   if (maybe_range_for_decl)
12328     *maybe_range_for_decl = NULL_TREE;
12329
12330   /* Defer access checks until we know what is being declared; the
12331      checks for names appearing in the decl-specifier-seq should be
12332      done as if we were in the scope of the thing being declared.  */
12333   push_deferring_access_checks (dk_deferred);
12334
12335   /* Parse the decl-specifier-seq.  We have to keep track of whether
12336      or not the decl-specifier-seq declares a named class or
12337      enumeration type, since that is the only case in which the
12338      init-declarator-list is allowed to be empty.
12339
12340      [dcl.dcl]
12341
12342      In a simple-declaration, the optional init-declarator-list can be
12343      omitted only when declaring a class or enumeration, that is when
12344      the decl-specifier-seq contains either a class-specifier, an
12345      elaborated-type-specifier, or an enum-specifier.  */
12346   cp_parser_decl_specifier_seq (parser,
12347                                 CP_PARSER_FLAGS_OPTIONAL,
12348                                 &decl_specifiers,
12349                                 &declares_class_or_enum);
12350   /* We no longer need to defer access checks.  */
12351   stop_deferring_access_checks ();
12352
12353   /* In a block scope, a valid declaration must always have a
12354      decl-specifier-seq.  By not trying to parse declarators, we can
12355      resolve the declaration/expression ambiguity more quickly.  */
12356   if (!function_definition_allowed_p
12357       && !decl_specifiers.any_specifiers_p)
12358     {
12359       cp_parser_error (parser, "expected declaration");
12360       goto done;
12361     }
12362
12363   /* If the next two tokens are both identifiers, the code is
12364      erroneous. The usual cause of this situation is code like:
12365
12366        T t;
12367
12368      where "T" should name a type -- but does not.  */
12369   if (!decl_specifiers.any_type_specifiers_p
12370       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12371     {
12372       /* If parsing tentatively, we should commit; we really are
12373          looking at a declaration.  */
12374       cp_parser_commit_to_tentative_parse (parser);
12375       /* Give up.  */
12376       goto done;
12377     }
12378
12379   /* If we have seen at least one decl-specifier, and the next token
12380      is not a parenthesis, then we must be looking at a declaration.
12381      (After "int (" we might be looking at a functional cast.)  */
12382   if (decl_specifiers.any_specifiers_p
12383       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12384       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12385       && !cp_parser_error_occurred (parser))
12386     cp_parser_commit_to_tentative_parse (parser);
12387
12388   tree last_type;
12389
12390   last_type = NULL_TREE;
12391
12392   /* Keep going until we hit the `;' at the end of the simple
12393      declaration.  */
12394   saw_declarator = false;
12395   while (cp_lexer_next_token_is_not (parser->lexer,
12396                                      CPP_SEMICOLON))
12397     {
12398       cp_token *token;
12399       bool function_definition_p;
12400       tree decl;
12401       tree auto_result = NULL_TREE;
12402
12403       if (saw_declarator)
12404         {
12405           /* If we are processing next declarator, comma is expected */
12406           token = cp_lexer_peek_token (parser->lexer);
12407           gcc_assert (token->type == CPP_COMMA);
12408           cp_lexer_consume_token (parser->lexer);
12409           if (maybe_range_for_decl)
12410             {
12411               *maybe_range_for_decl = error_mark_node;
12412               if (comma_loc == UNKNOWN_LOCATION)
12413                 comma_loc = token->location;
12414             }
12415         }
12416       else
12417         saw_declarator = true;
12418
12419       /* Parse the init-declarator.  */
12420       decl = cp_parser_init_declarator (parser, &decl_specifiers,
12421                                         /*checks=*/NULL,
12422                                         function_definition_allowed_p,
12423                                         /*member_p=*/false,
12424                                         declares_class_or_enum,
12425                                         &function_definition_p,
12426                                         maybe_range_for_decl,
12427                                         &init_loc,
12428                                         &auto_result);
12429       /* If an error occurred while parsing tentatively, exit quickly.
12430          (That usually happens when in the body of a function; each
12431          statement is treated as a declaration-statement until proven
12432          otherwise.)  */
12433       if (cp_parser_error_occurred (parser))
12434         goto done;
12435
12436       if (auto_result
12437           && (!processing_template_decl || !type_uses_auto (auto_result)))
12438         {
12439           if (last_type
12440               && last_type != error_mark_node
12441               && !same_type_p (auto_result, last_type))
12442             {
12443               /* If the list of declarators contains more than one declarator,
12444                  the type of each declared variable is determined as described
12445                  above. If the type deduced for the template parameter U is not
12446                  the same in each deduction, the program is ill-formed.  */
12447               error_at (decl_specifiers.locations[ds_type_spec],
12448                         "inconsistent deduction for %qT: %qT and then %qT",
12449                         decl_specifiers.type, last_type, auto_result);
12450               last_type = error_mark_node;
12451             }
12452           else
12453             last_type = auto_result;
12454         }
12455
12456       /* Handle function definitions specially.  */
12457       if (function_definition_p)
12458         {
12459           /* If the next token is a `,', then we are probably
12460              processing something like:
12461
12462                void f() {}, *p;
12463
12464              which is erroneous.  */
12465           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12466             {
12467               cp_token *token = cp_lexer_peek_token (parser->lexer);
12468               error_at (token->location,
12469                         "mixing"
12470                         " declarations and function-definitions is forbidden");
12471             }
12472           /* Otherwise, we're done with the list of declarators.  */
12473           else
12474             {
12475               pop_deferring_access_checks ();
12476               return;
12477             }
12478         }
12479       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12480         *maybe_range_for_decl = decl;
12481       /* The next token should be either a `,' or a `;'.  */
12482       token = cp_lexer_peek_token (parser->lexer);
12483       /* If it's a `,', there are more declarators to come.  */
12484       if (token->type == CPP_COMMA)
12485         /* will be consumed next time around */;
12486       /* If it's a `;', we are done.  */
12487       else if (token->type == CPP_SEMICOLON)
12488         break;
12489       else if (maybe_range_for_decl)
12490         {
12491           if (declares_class_or_enum && token->type == CPP_COLON)
12492             pedwarn (decl_specifiers.locations[ds_type_spec], 0,
12493                      "types may not be defined in a for-range-declaration");
12494           break;
12495         }
12496       /* Anything else is an error.  */
12497       else
12498         {
12499           /* If we have already issued an error message we don't need
12500              to issue another one.  */
12501           if ((decl != error_mark_node
12502                && DECL_INITIAL (decl) != error_mark_node)
12503               || cp_parser_uncommitted_to_tentative_parse_p (parser))
12504             cp_parser_error (parser, "expected %<,%> or %<;%>");
12505           /* Skip tokens until we reach the end of the statement.  */
12506           cp_parser_skip_to_end_of_statement (parser);
12507           /* If the next token is now a `;', consume it.  */
12508           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12509             cp_lexer_consume_token (parser->lexer);
12510           goto done;
12511         }
12512       /* After the first time around, a function-definition is not
12513          allowed -- even if it was OK at first.  For example:
12514
12515            int i, f() {}
12516
12517          is not valid.  */
12518       function_definition_allowed_p = false;
12519     }
12520
12521   /* Issue an error message if no declarators are present, and the
12522      decl-specifier-seq does not itself declare a class or
12523      enumeration: [dcl.dcl]/3.  */
12524   if (!saw_declarator)
12525     {
12526       if (cp_parser_declares_only_class_p (parser))
12527         {
12528           if (!declares_class_or_enum
12529               && decl_specifiers.type
12530               && OVERLOAD_TYPE_P (decl_specifiers.type))
12531             /* Ensure an error is issued anyway when finish_decltype_type,
12532                called via cp_parser_decl_specifier_seq, returns a class or
12533                an enumeration (c++/51786).  */
12534             decl_specifiers.type = NULL_TREE;
12535           shadow_tag (&decl_specifiers);
12536         }
12537       /* Perform any deferred access checks.  */
12538       perform_deferred_access_checks (tf_warning_or_error);
12539     }
12540
12541   /* Consume the `;'.  */
12542   if (!maybe_range_for_decl)
12543     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12544   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12545     {
12546       if (init_loc != UNKNOWN_LOCATION)
12547         error_at (init_loc, "initializer in range-based %<for%> loop");
12548       if (comma_loc != UNKNOWN_LOCATION)
12549         error_at (comma_loc,
12550                   "multiple declarations in range-based %<for%> loop");
12551     }
12552
12553  done:
12554   pop_deferring_access_checks ();
12555 }
12556
12557 /* Parse a decl-specifier-seq.
12558
12559    decl-specifier-seq:
12560      decl-specifier-seq [opt] decl-specifier
12561      decl-specifier attribute-specifier-seq [opt] (C++11)
12562
12563    decl-specifier:
12564      storage-class-specifier
12565      type-specifier
12566      function-specifier
12567      friend
12568      typedef
12569
12570    GNU Extension:
12571
12572    decl-specifier:
12573      attributes
12574
12575    Concepts Extension:
12576
12577    decl-specifier:
12578      concept
12579
12580    Set *DECL_SPECS to a representation of the decl-specifier-seq.
12581
12582    The parser flags FLAGS is used to control type-specifier parsing.
12583
12584    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12585    flags:
12586
12587      1: one of the decl-specifiers is an elaborated-type-specifier
12588         (i.e., a type declaration)
12589      2: one of the decl-specifiers is an enum-specifier or a
12590         class-specifier (i.e., a type definition)
12591
12592    */
12593
12594 static void
12595 cp_parser_decl_specifier_seq (cp_parser* parser,
12596                               cp_parser_flags flags,
12597                               cp_decl_specifier_seq *decl_specs,
12598                               int* declares_class_or_enum)
12599 {
12600   bool constructor_possible_p = !parser->in_declarator_p;
12601   bool found_decl_spec = false;
12602   cp_token *start_token = NULL;
12603   cp_decl_spec ds;
12604
12605   /* Clear DECL_SPECS.  */
12606   clear_decl_specs (decl_specs);
12607
12608   /* Assume no class or enumeration type is declared.  */
12609   *declares_class_or_enum = 0;
12610
12611   /* Keep reading specifiers until there are no more to read.  */
12612   while (true)
12613     {
12614       bool constructor_p;
12615       cp_token *token;
12616       ds = ds_last;
12617
12618       /* Peek at the next token.  */
12619       token = cp_lexer_peek_token (parser->lexer);
12620
12621       /* Save the first token of the decl spec list for error
12622          reporting.  */
12623       if (!start_token)
12624         start_token = token;
12625       /* Handle attributes.  */
12626       if (cp_next_tokens_can_be_attribute_p (parser))
12627         {
12628           /* Parse the attributes.  */
12629           tree attrs = cp_parser_attributes_opt (parser);
12630
12631           /* In a sequence of declaration specifiers, c++11 attributes
12632              appertain to the type that precede them. In that case
12633              [dcl.spec]/1 says:
12634
12635                  The attribute-specifier-seq affects the type only for
12636                  the declaration it appears in, not other declarations
12637                  involving the same type.
12638
12639              But for now let's force the user to position the
12640              attribute either at the beginning of the declaration or
12641              after the declarator-id, which would clearly mean that it
12642              applies to the declarator.  */
12643           if (cxx11_attribute_p (attrs))
12644             {
12645               if (!found_decl_spec)
12646                 /* The c++11 attribute is at the beginning of the
12647                    declaration.  It appertains to the entity being
12648                    declared.  */;
12649               else
12650                 {
12651                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12652                     {
12653                       /*  This is an attribute following a
12654                           class-specifier.  */
12655                       if (decl_specs->type_definition_p)
12656                         warn_misplaced_attr_for_class_type (token->location,
12657                                                             decl_specs->type);
12658                       attrs = NULL_TREE;
12659                     }
12660                   else
12661                     {
12662                       decl_specs->std_attributes
12663                         = chainon (decl_specs->std_attributes,
12664                                    attrs);
12665                       if (decl_specs->locations[ds_std_attribute] == 0)
12666                         decl_specs->locations[ds_std_attribute] = token->location;
12667                     }
12668                   continue;
12669                 }
12670             }
12671
12672             decl_specs->attributes
12673               = chainon (decl_specs->attributes,
12674                          attrs);
12675           if (decl_specs->locations[ds_attribute] == 0)
12676             decl_specs->locations[ds_attribute] = token->location;
12677           continue;
12678         }
12679       /* Assume we will find a decl-specifier keyword.  */
12680       found_decl_spec = true;
12681       /* If the next token is an appropriate keyword, we can simply
12682          add it to the list.  */
12683       switch (token->keyword)
12684         {
12685           /* decl-specifier:
12686                friend
12687                constexpr */
12688         case RID_FRIEND:
12689           if (!at_class_scope_p ())
12690             {
12691               error_at (token->location, "%<friend%> used outside of class");
12692               cp_lexer_purge_token (parser->lexer);
12693             }
12694           else
12695             {
12696               ds = ds_friend;
12697               /* Consume the token.  */
12698               cp_lexer_consume_token (parser->lexer);
12699             }
12700           break;
12701
12702         case RID_CONSTEXPR:
12703           ds = ds_constexpr;
12704           cp_lexer_consume_token (parser->lexer);
12705           break;
12706
12707         case RID_CONCEPT:
12708           ds = ds_concept;
12709           cp_lexer_consume_token (parser->lexer);
12710           break;
12711
12712           /* function-specifier:
12713                inline
12714                virtual
12715                explicit  */
12716         case RID_INLINE:
12717         case RID_VIRTUAL:
12718         case RID_EXPLICIT:
12719           cp_parser_function_specifier_opt (parser, decl_specs);
12720           break;
12721
12722           /* decl-specifier:
12723                typedef  */
12724         case RID_TYPEDEF:
12725           ds = ds_typedef;
12726           /* Consume the token.  */
12727           cp_lexer_consume_token (parser->lexer);
12728           /* A constructor declarator cannot appear in a typedef.  */
12729           constructor_possible_p = false;
12730           /* The "typedef" keyword can only occur in a declaration; we
12731              may as well commit at this point.  */
12732           cp_parser_commit_to_tentative_parse (parser);
12733
12734           if (decl_specs->storage_class != sc_none)
12735             decl_specs->conflicting_specifiers_p = true;
12736           break;
12737
12738           /* storage-class-specifier:
12739                auto
12740                register
12741                static
12742                extern
12743                mutable
12744
12745              GNU Extension:
12746                thread  */
12747         case RID_AUTO:
12748           if (cxx_dialect == cxx98) 
12749             {
12750               /* Consume the token.  */
12751               cp_lexer_consume_token (parser->lexer);
12752
12753               /* Complain about `auto' as a storage specifier, if
12754                  we're complaining about C++0x compatibility.  */
12755               warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12756                           " changes meaning in C++11; please remove it");
12757
12758               /* Set the storage class anyway.  */
12759               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12760                                            token);
12761             }
12762           else
12763             /* C++0x auto type-specifier.  */
12764             found_decl_spec = false;
12765           break;
12766
12767         case RID_REGISTER:
12768         case RID_STATIC:
12769         case RID_EXTERN:
12770         case RID_MUTABLE:
12771           /* Consume the token.  */
12772           cp_lexer_consume_token (parser->lexer);
12773           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12774                                        token);
12775           break;
12776         case RID_THREAD:
12777           /* Consume the token.  */
12778           ds = ds_thread;
12779           cp_lexer_consume_token (parser->lexer);
12780           break;
12781
12782         default:
12783           /* We did not yet find a decl-specifier yet.  */
12784           found_decl_spec = false;
12785           break;
12786         }
12787
12788       if (found_decl_spec
12789           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12790           && token->keyword != RID_CONSTEXPR)
12791         error ("decl-specifier invalid in condition");
12792
12793       if (ds != ds_last)
12794         set_and_check_decl_spec_loc (decl_specs, ds, token);
12795
12796       /* Constructors are a special case.  The `S' in `S()' is not a
12797          decl-specifier; it is the beginning of the declarator.  */
12798       constructor_p
12799         = (!found_decl_spec
12800            && constructor_possible_p
12801            && (cp_parser_constructor_declarator_p
12802                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12803
12804       /* If we don't have a DECL_SPEC yet, then we must be looking at
12805          a type-specifier.  */
12806       if (!found_decl_spec && !constructor_p)
12807         {
12808           int decl_spec_declares_class_or_enum;
12809           bool is_cv_qualifier;
12810           tree type_spec;
12811
12812           type_spec
12813             = cp_parser_type_specifier (parser, flags,
12814                                         decl_specs,
12815                                         /*is_declaration=*/true,
12816                                         &decl_spec_declares_class_or_enum,
12817                                         &is_cv_qualifier);
12818           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12819
12820           /* If this type-specifier referenced a user-defined type
12821              (a typedef, class-name, etc.), then we can't allow any
12822              more such type-specifiers henceforth.
12823
12824              [dcl.spec]
12825
12826              The longest sequence of decl-specifiers that could
12827              possibly be a type name is taken as the
12828              decl-specifier-seq of a declaration.  The sequence shall
12829              be self-consistent as described below.
12830
12831              [dcl.type]
12832
12833              As a general rule, at most one type-specifier is allowed
12834              in the complete decl-specifier-seq of a declaration.  The
12835              only exceptions are the following:
12836
12837              -- const or volatile can be combined with any other
12838                 type-specifier.
12839
12840              -- signed or unsigned can be combined with char, long,
12841                 short, or int.
12842
12843              -- ..
12844
12845              Example:
12846
12847                typedef char* Pc;
12848                void g (const int Pc);
12849
12850              Here, Pc is *not* part of the decl-specifier seq; it's
12851              the declarator.  Therefore, once we see a type-specifier
12852              (other than a cv-qualifier), we forbid any additional
12853              user-defined types.  We *do* still allow things like `int
12854              int' to be considered a decl-specifier-seq, and issue the
12855              error message later.  */
12856           if (type_spec && !is_cv_qualifier)
12857             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12858           /* A constructor declarator cannot follow a type-specifier.  */
12859           if (type_spec)
12860             {
12861               constructor_possible_p = false;
12862               found_decl_spec = true;
12863               if (!is_cv_qualifier)
12864                 decl_specs->any_type_specifiers_p = true;
12865             }
12866         }
12867
12868       /* If we still do not have a DECL_SPEC, then there are no more
12869          decl-specifiers.  */
12870       if (!found_decl_spec)
12871         break;
12872
12873       decl_specs->any_specifiers_p = true;
12874       /* After we see one decl-specifier, further decl-specifiers are
12875          always optional.  */
12876       flags |= CP_PARSER_FLAGS_OPTIONAL;
12877     }
12878
12879   /* Don't allow a friend specifier with a class definition.  */
12880   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12881       && (*declares_class_or_enum & 2))
12882     error_at (decl_specs->locations[ds_friend],
12883               "class definition may not be declared a friend");
12884 }
12885
12886 /* Parse an (optional) storage-class-specifier.
12887
12888    storage-class-specifier:
12889      auto
12890      register
12891      static
12892      extern
12893      mutable
12894
12895    GNU Extension:
12896
12897    storage-class-specifier:
12898      thread
12899
12900    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
12901
12902 static tree
12903 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12904 {
12905   switch (cp_lexer_peek_token (parser->lexer)->keyword)
12906     {
12907     case RID_AUTO:
12908       if (cxx_dialect != cxx98)
12909         return NULL_TREE;
12910       /* Fall through for C++98.  */
12911
12912     case RID_REGISTER:
12913     case RID_STATIC:
12914     case RID_EXTERN:
12915     case RID_MUTABLE:
12916     case RID_THREAD:
12917       /* Consume the token.  */
12918       return cp_lexer_consume_token (parser->lexer)->u.value;
12919
12920     default:
12921       return NULL_TREE;
12922     }
12923 }
12924
12925 /* Parse an (optional) function-specifier.
12926
12927    function-specifier:
12928      inline
12929      virtual
12930      explicit
12931
12932    Returns an IDENTIFIER_NODE corresponding to the keyword used.
12933    Updates DECL_SPECS, if it is non-NULL.  */
12934
12935 static tree
12936 cp_parser_function_specifier_opt (cp_parser* parser,
12937                                   cp_decl_specifier_seq *decl_specs)
12938 {
12939   cp_token *token = cp_lexer_peek_token (parser->lexer);
12940   switch (token->keyword)
12941     {
12942     case RID_INLINE:
12943       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12944       break;
12945
12946     case RID_VIRTUAL:
12947       /* 14.5.2.3 [temp.mem]
12948
12949          A member function template shall not be virtual.  */
12950       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12951         error_at (token->location, "templates may not be %<virtual%>");
12952       else
12953         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12954       break;
12955
12956     case RID_EXPLICIT:
12957       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12958       break;
12959
12960     default:
12961       return NULL_TREE;
12962     }
12963
12964   /* Consume the token.  */
12965   return cp_lexer_consume_token (parser->lexer)->u.value;
12966 }
12967
12968 /* Parse a linkage-specification.
12969
12970    linkage-specification:
12971      extern string-literal { declaration-seq [opt] }
12972      extern string-literal declaration  */
12973
12974 static void
12975 cp_parser_linkage_specification (cp_parser* parser)
12976 {
12977   tree linkage;
12978
12979   /* Look for the `extern' keyword.  */
12980   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12981
12982   /* Look for the string-literal.  */
12983   linkage = cp_parser_string_literal (parser, false, false);
12984
12985   /* Transform the literal into an identifier.  If the literal is a
12986      wide-character string, or contains embedded NULs, then we can't
12987      handle it as the user wants.  */
12988   if (strlen (TREE_STRING_POINTER (linkage))
12989       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12990     {
12991       cp_parser_error (parser, "invalid linkage-specification");
12992       /* Assume C++ linkage.  */
12993       linkage = lang_name_cplusplus;
12994     }
12995   else
12996     linkage = get_identifier (TREE_STRING_POINTER (linkage));
12997
12998   /* We're now using the new linkage.  */
12999   push_lang_context (linkage);
13000
13001   /* If the next token is a `{', then we're using the first
13002      production.  */
13003   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13004     {
13005       cp_ensure_no_omp_declare_simd (parser);
13006       cp_ensure_no_oacc_routine (parser);
13007
13008       /* Consume the `{' token.  */
13009       cp_lexer_consume_token (parser->lexer);
13010       /* Parse the declarations.  */
13011       cp_parser_declaration_seq_opt (parser);
13012       /* Look for the closing `}'.  */
13013       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13014     }
13015   /* Otherwise, there's just one declaration.  */
13016   else
13017     {
13018       bool saved_in_unbraced_linkage_specification_p;
13019
13020       saved_in_unbraced_linkage_specification_p
13021         = parser->in_unbraced_linkage_specification_p;
13022       parser->in_unbraced_linkage_specification_p = true;
13023       cp_parser_declaration (parser);
13024       parser->in_unbraced_linkage_specification_p
13025         = saved_in_unbraced_linkage_specification_p;
13026     }
13027
13028   /* We're done with the linkage-specification.  */
13029   pop_lang_context ();
13030 }
13031
13032 /* Parse a static_assert-declaration.
13033
13034    static_assert-declaration:
13035      static_assert ( constant-expression , string-literal ) ; 
13036      static_assert ( constant-expression ) ; (C++1Z)
13037
13038    If MEMBER_P, this static_assert is a class member.  */
13039
13040 static void 
13041 cp_parser_static_assert(cp_parser *parser, bool member_p)
13042 {
13043   tree condition;
13044   tree message;
13045   cp_token *token;
13046   location_t saved_loc;
13047   bool dummy;
13048
13049   /* Peek at the `static_assert' token so we can keep track of exactly
13050      where the static assertion started.  */
13051   token = cp_lexer_peek_token (parser->lexer);
13052   saved_loc = token->location;
13053
13054   /* Look for the `static_assert' keyword.  */
13055   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
13056                                   RT_STATIC_ASSERT))
13057     return;
13058
13059   /*  We know we are in a static assertion; commit to any tentative
13060       parse.  */
13061   if (cp_parser_parsing_tentatively (parser))
13062     cp_parser_commit_to_tentative_parse (parser);
13063
13064   /* Parse the `(' starting the static assertion condition.  */
13065   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13066
13067   /* Parse the constant-expression.  Allow a non-constant expression
13068      here in order to give better diagnostics in finish_static_assert.  */
13069   condition = 
13070     cp_parser_constant_expression (parser,
13071                                    /*allow_non_constant_p=*/true,
13072                                    /*non_constant_p=*/&dummy);
13073
13074   if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13075     {
13076       if (cxx_dialect < cxx1z)
13077         pedwarn (input_location, OPT_Wpedantic,
13078                  "static_assert without a message "
13079                  "only available with -std=c++1z or -std=gnu++1z");
13080       /* Eat the ')'  */
13081       cp_lexer_consume_token (parser->lexer);
13082       message = build_string (1, "");
13083       TREE_TYPE (message) = char_array_type_node;
13084       fix_string_type (message);
13085     }
13086   else
13087     {
13088       /* Parse the separating `,'.  */
13089       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13090
13091       /* Parse the string-literal message.  */
13092       message = cp_parser_string_literal (parser, 
13093                                           /*translate=*/false,
13094                                           /*wide_ok=*/true);
13095
13096       /* A `)' completes the static assertion.  */
13097       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13098         cp_parser_skip_to_closing_parenthesis (parser, 
13099                                                /*recovering=*/true, 
13100                                                /*or_comma=*/false,
13101                                                /*consume_paren=*/true);
13102     }
13103
13104   /* A semicolon terminates the declaration.  */
13105   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13106
13107   /* Complete the static assertion, which may mean either processing 
13108      the static assert now or saving it for template instantiation.  */
13109   finish_static_assert (condition, message, saved_loc, member_p);
13110 }
13111
13112 /* Parse the expression in decltype ( expression ).  */
13113
13114 static tree
13115 cp_parser_decltype_expr (cp_parser *parser,
13116                          bool &id_expression_or_member_access_p)
13117 {
13118   cp_token *id_expr_start_token;
13119   tree expr;
13120
13121   /* Since we're going to preserve any side-effects from this parse, set up a
13122      firewall to protect our callers from cp_parser_commit_to_tentative_parse
13123      in the expression.  */
13124   tentative_firewall firewall (parser);
13125
13126   /* First, try parsing an id-expression.  */
13127   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13128   cp_parser_parse_tentatively (parser);
13129   expr = cp_parser_id_expression (parser,
13130                                   /*template_keyword_p=*/false,
13131                                   /*check_dependency_p=*/true,
13132                                   /*template_p=*/NULL,
13133                                   /*declarator_p=*/false,
13134                                   /*optional_p=*/false);
13135
13136   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13137     {
13138       bool non_integral_constant_expression_p = false;
13139       tree id_expression = expr;
13140       cp_id_kind idk;
13141       const char *error_msg;
13142
13143       if (identifier_p (expr))
13144         /* Lookup the name we got back from the id-expression.  */
13145         expr = cp_parser_lookup_name_simple (parser, expr,
13146                                              id_expr_start_token->location);
13147
13148       if (expr
13149           && expr != error_mark_node
13150           && TREE_CODE (expr) != TYPE_DECL
13151           && (TREE_CODE (expr) != BIT_NOT_EXPR
13152               || !TYPE_P (TREE_OPERAND (expr, 0)))
13153           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13154         {
13155           /* Complete lookup of the id-expression.  */
13156           expr = (finish_id_expression
13157                   (id_expression, expr, parser->scope, &idk,
13158                    /*integral_constant_expression_p=*/false,
13159                    /*allow_non_integral_constant_expression_p=*/true,
13160                    &non_integral_constant_expression_p,
13161                    /*template_p=*/false,
13162                    /*done=*/true,
13163                    /*address_p=*/false,
13164                    /*template_arg_p=*/false,
13165                    &error_msg,
13166                    id_expr_start_token->location));
13167
13168           if (expr == error_mark_node)
13169             /* We found an id-expression, but it was something that we
13170                should not have found. This is an error, not something
13171                we can recover from, so note that we found an
13172                id-expression and we'll recover as gracefully as
13173                possible.  */
13174             id_expression_or_member_access_p = true;
13175         }
13176
13177       if (expr 
13178           && expr != error_mark_node
13179           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13180         /* We have an id-expression.  */
13181         id_expression_or_member_access_p = true;
13182     }
13183
13184   if (!id_expression_or_member_access_p)
13185     {
13186       /* Abort the id-expression parse.  */
13187       cp_parser_abort_tentative_parse (parser);
13188
13189       /* Parsing tentatively, again.  */
13190       cp_parser_parse_tentatively (parser);
13191
13192       /* Parse a class member access.  */
13193       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13194                                            /*cast_p=*/false, /*decltype*/true,
13195                                            /*member_access_only_p=*/true, NULL);
13196
13197       if (expr 
13198           && expr != error_mark_node
13199           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13200         /* We have an id-expression.  */
13201         id_expression_or_member_access_p = true;
13202     }
13203
13204   if (id_expression_or_member_access_p)
13205     /* We have parsed the complete id-expression or member access.  */
13206     cp_parser_parse_definitely (parser);
13207   else
13208     {
13209       /* Abort our attempt to parse an id-expression or member access
13210          expression.  */
13211       cp_parser_abort_tentative_parse (parser);
13212
13213       /* Parse a full expression.  */
13214       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13215                                    /*decltype_p=*/true);
13216     }
13217
13218   return expr;
13219 }
13220
13221 /* Parse a `decltype' type. Returns the type.
13222
13223    simple-type-specifier:
13224      decltype ( expression )
13225    C++14 proposal:
13226      decltype ( auto )  */
13227
13228 static tree
13229 cp_parser_decltype (cp_parser *parser)
13230 {
13231   tree expr;
13232   bool id_expression_or_member_access_p = false;
13233   const char *saved_message;
13234   bool saved_integral_constant_expression_p;
13235   bool saved_non_integral_constant_expression_p;
13236   bool saved_greater_than_is_operator_p;
13237   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13238
13239   if (start_token->type == CPP_DECLTYPE)
13240     {
13241       /* Already parsed.  */
13242       cp_lexer_consume_token (parser->lexer);
13243       return saved_checks_value (start_token->u.tree_check_value);
13244     }
13245
13246   /* Look for the `decltype' token.  */
13247   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13248     return error_mark_node;
13249
13250   /* Parse the opening `('.  */
13251   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13252     return error_mark_node;
13253
13254   /* decltype (auto) */
13255   if (cxx_dialect >= cxx14
13256       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13257     {
13258       cp_lexer_consume_token (parser->lexer);
13259       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13260         return error_mark_node;
13261       expr = make_decltype_auto ();
13262       AUTO_IS_DECLTYPE (expr) = true;
13263       goto rewrite;
13264     }
13265
13266   /* Types cannot be defined in a `decltype' expression.  Save away the
13267      old message.  */
13268   saved_message = parser->type_definition_forbidden_message;
13269
13270   /* And create the new one.  */
13271   parser->type_definition_forbidden_message
13272     = G_("types may not be defined in %<decltype%> expressions");
13273
13274   /* The restrictions on constant-expressions do not apply inside
13275      decltype expressions.  */
13276   saved_integral_constant_expression_p
13277     = parser->integral_constant_expression_p;
13278   saved_non_integral_constant_expression_p
13279     = parser->non_integral_constant_expression_p;
13280   parser->integral_constant_expression_p = false;
13281
13282   /* Within a parenthesized expression, a `>' token is always
13283      the greater-than operator.  */
13284   saved_greater_than_is_operator_p
13285     = parser->greater_than_is_operator_p;
13286   parser->greater_than_is_operator_p = true;
13287
13288   /* Do not actually evaluate the expression.  */
13289   ++cp_unevaluated_operand;
13290
13291   /* Do not warn about problems with the expression.  */
13292   ++c_inhibit_evaluation_warnings;
13293
13294   expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13295
13296   /* Go back to evaluating expressions.  */
13297   --cp_unevaluated_operand;
13298   --c_inhibit_evaluation_warnings;
13299
13300   /* The `>' token might be the end of a template-id or
13301      template-parameter-list now.  */
13302   parser->greater_than_is_operator_p
13303     = saved_greater_than_is_operator_p;
13304
13305   /* Restore the old message and the integral constant expression
13306      flags.  */
13307   parser->type_definition_forbidden_message = saved_message;
13308   parser->integral_constant_expression_p
13309     = saved_integral_constant_expression_p;
13310   parser->non_integral_constant_expression_p
13311     = saved_non_integral_constant_expression_p;
13312
13313   /* Parse to the closing `)'.  */
13314   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13315     {
13316       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13317                                              /*consume_paren=*/true);
13318       return error_mark_node;
13319     }
13320
13321   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13322                                tf_warning_or_error);
13323
13324  rewrite:
13325   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13326      it again.  */
13327   start_token->type = CPP_DECLTYPE;
13328   start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13329   start_token->u.tree_check_value->value = expr;
13330   start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13331   start_token->keyword = RID_MAX;
13332   cp_lexer_purge_tokens_after (parser->lexer, start_token);
13333
13334   return expr;
13335 }
13336
13337 /* Special member functions [gram.special] */
13338
13339 /* Parse a conversion-function-id.
13340
13341    conversion-function-id:
13342      operator conversion-type-id
13343
13344    Returns an IDENTIFIER_NODE representing the operator.  */
13345
13346 static tree
13347 cp_parser_conversion_function_id (cp_parser* parser)
13348 {
13349   tree type;
13350   tree saved_scope;
13351   tree saved_qualifying_scope;
13352   tree saved_object_scope;
13353   tree pushed_scope = NULL_TREE;
13354
13355   /* Look for the `operator' token.  */
13356   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13357     return error_mark_node;
13358   /* When we parse the conversion-type-id, the current scope will be
13359      reset.  However, we need that information in able to look up the
13360      conversion function later, so we save it here.  */
13361   saved_scope = parser->scope;
13362   saved_qualifying_scope = parser->qualifying_scope;
13363   saved_object_scope = parser->object_scope;
13364   /* We must enter the scope of the class so that the names of
13365      entities declared within the class are available in the
13366      conversion-type-id.  For example, consider:
13367
13368        struct S {
13369          typedef int I;
13370          operator I();
13371        };
13372
13373        S::operator I() { ... }
13374
13375      In order to see that `I' is a type-name in the definition, we
13376      must be in the scope of `S'.  */
13377   if (saved_scope)
13378     pushed_scope = push_scope (saved_scope);
13379   /* Parse the conversion-type-id.  */
13380   type = cp_parser_conversion_type_id (parser);
13381   /* Leave the scope of the class, if any.  */
13382   if (pushed_scope)
13383     pop_scope (pushed_scope);
13384   /* Restore the saved scope.  */
13385   parser->scope = saved_scope;
13386   parser->qualifying_scope = saved_qualifying_scope;
13387   parser->object_scope = saved_object_scope;
13388   /* If the TYPE is invalid, indicate failure.  */
13389   if (type == error_mark_node)
13390     return error_mark_node;
13391   return mangle_conv_op_name_for_type (type);
13392 }
13393
13394 /* Parse a conversion-type-id:
13395
13396    conversion-type-id:
13397      type-specifier-seq conversion-declarator [opt]
13398
13399    Returns the TYPE specified.  */
13400
13401 static tree
13402 cp_parser_conversion_type_id (cp_parser* parser)
13403 {
13404   tree attributes;
13405   cp_decl_specifier_seq type_specifiers;
13406   cp_declarator *declarator;
13407   tree type_specified;
13408   const char *saved_message;
13409
13410   /* Parse the attributes.  */
13411   attributes = cp_parser_attributes_opt (parser);
13412
13413   saved_message = parser->type_definition_forbidden_message;
13414   parser->type_definition_forbidden_message
13415     = G_("types may not be defined in a conversion-type-id");
13416
13417   /* Parse the type-specifiers.  */
13418   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13419                                 /*is_trailing_return=*/false,
13420                                 &type_specifiers);
13421
13422   parser->type_definition_forbidden_message = saved_message;
13423
13424   /* If that didn't work, stop.  */
13425   if (type_specifiers.type == error_mark_node)
13426     return error_mark_node;
13427   /* Parse the conversion-declarator.  */
13428   declarator = cp_parser_conversion_declarator_opt (parser);
13429
13430   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
13431                                     /*initialized=*/0, &attributes);
13432   if (attributes)
13433     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13434
13435   /* Don't give this error when parsing tentatively.  This happens to
13436      work because we always parse this definitively once.  */
13437   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13438       && type_uses_auto (type_specified))
13439     {
13440       if (cxx_dialect < cxx14)
13441         {
13442           error ("invalid use of %<auto%> in conversion operator");
13443           return error_mark_node;
13444         }
13445       else if (template_parm_scope_p ())
13446         warning (0, "use of %<auto%> in member template "
13447                  "conversion operator can never be deduced");
13448     }
13449
13450   return type_specified;
13451 }
13452
13453 /* Parse an (optional) conversion-declarator.
13454
13455    conversion-declarator:
13456      ptr-operator conversion-declarator [opt]
13457
13458    */
13459
13460 static cp_declarator *
13461 cp_parser_conversion_declarator_opt (cp_parser* parser)
13462 {
13463   enum tree_code code;
13464   tree class_type, std_attributes = NULL_TREE;
13465   cp_cv_quals cv_quals;
13466
13467   /* We don't know if there's a ptr-operator next, or not.  */
13468   cp_parser_parse_tentatively (parser);
13469   /* Try the ptr-operator.  */
13470   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13471                                  &std_attributes);
13472   /* If it worked, look for more conversion-declarators.  */
13473   if (cp_parser_parse_definitely (parser))
13474     {
13475       cp_declarator *declarator;
13476
13477       /* Parse another optional declarator.  */
13478       declarator = cp_parser_conversion_declarator_opt (parser);
13479
13480       declarator = cp_parser_make_indirect_declarator
13481         (code, class_type, cv_quals, declarator, std_attributes);
13482
13483       return declarator;
13484    }
13485
13486   return NULL;
13487 }
13488
13489 /* Parse an (optional) ctor-initializer.
13490
13491    ctor-initializer:
13492      : mem-initializer-list
13493
13494    Returns TRUE iff the ctor-initializer was actually present.  */
13495
13496 static bool
13497 cp_parser_ctor_initializer_opt (cp_parser* parser)
13498 {
13499   /* If the next token is not a `:', then there is no
13500      ctor-initializer.  */
13501   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13502     {
13503       /* Do default initialization of any bases and members.  */
13504       if (DECL_CONSTRUCTOR_P (current_function_decl))
13505         finish_mem_initializers (NULL_TREE);
13506
13507       return false;
13508     }
13509
13510   /* Consume the `:' token.  */
13511   cp_lexer_consume_token (parser->lexer);
13512   /* And the mem-initializer-list.  */
13513   cp_parser_mem_initializer_list (parser);
13514
13515   return true;
13516 }
13517
13518 /* Parse a mem-initializer-list.
13519
13520    mem-initializer-list:
13521      mem-initializer ... [opt]
13522      mem-initializer ... [opt] , mem-initializer-list  */
13523
13524 static void
13525 cp_parser_mem_initializer_list (cp_parser* parser)
13526 {
13527   tree mem_initializer_list = NULL_TREE;
13528   tree target_ctor = error_mark_node;
13529   cp_token *token = cp_lexer_peek_token (parser->lexer);
13530
13531   /* Let the semantic analysis code know that we are starting the
13532      mem-initializer-list.  */
13533   if (!DECL_CONSTRUCTOR_P (current_function_decl))
13534     error_at (token->location,
13535               "only constructors take member initializers");
13536
13537   /* Loop through the list.  */
13538   while (true)
13539     {
13540       tree mem_initializer;
13541
13542       token = cp_lexer_peek_token (parser->lexer);
13543       /* Parse the mem-initializer.  */
13544       mem_initializer = cp_parser_mem_initializer (parser);
13545       /* If the next token is a `...', we're expanding member initializers. */
13546       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13547         {
13548           /* Consume the `...'. */
13549           cp_lexer_consume_token (parser->lexer);
13550
13551           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13552              can be expanded but members cannot. */
13553           if (mem_initializer != error_mark_node
13554               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13555             {
13556               error_at (token->location,
13557                         "cannot expand initializer for member %<%D%>",
13558                         TREE_PURPOSE (mem_initializer));
13559               mem_initializer = error_mark_node;
13560             }
13561
13562           /* Construct the pack expansion type. */
13563           if (mem_initializer != error_mark_node)
13564             mem_initializer = make_pack_expansion (mem_initializer);
13565         }
13566       if (target_ctor != error_mark_node
13567           && mem_initializer != error_mark_node)
13568         {
13569           error ("mem-initializer for %qD follows constructor delegation",
13570                  TREE_PURPOSE (mem_initializer));
13571           mem_initializer = error_mark_node;
13572         }
13573       /* Look for a target constructor. */
13574       if (mem_initializer != error_mark_node
13575           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13576           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13577         {
13578           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13579           if (mem_initializer_list)
13580             {
13581               error ("constructor delegation follows mem-initializer for %qD",
13582                      TREE_PURPOSE (mem_initializer_list));
13583               mem_initializer = error_mark_node;
13584             }
13585           target_ctor = mem_initializer;
13586         }
13587       /* Add it to the list, unless it was erroneous.  */
13588       if (mem_initializer != error_mark_node)
13589         {
13590           TREE_CHAIN (mem_initializer) = mem_initializer_list;
13591           mem_initializer_list = mem_initializer;
13592         }
13593       /* If the next token is not a `,', we're done.  */
13594       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13595         break;
13596       /* Consume the `,' token.  */
13597       cp_lexer_consume_token (parser->lexer);
13598     }
13599
13600   /* Perform semantic analysis.  */
13601   if (DECL_CONSTRUCTOR_P (current_function_decl))
13602     finish_mem_initializers (mem_initializer_list);
13603 }
13604
13605 /* Parse a mem-initializer.
13606
13607    mem-initializer:
13608      mem-initializer-id ( expression-list [opt] )
13609      mem-initializer-id braced-init-list
13610
13611    GNU extension:
13612
13613    mem-initializer:
13614      ( expression-list [opt] )
13615
13616    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
13617    class) or FIELD_DECL (for a non-static data member) to initialize;
13618    the TREE_VALUE is the expression-list.  An empty initialization
13619    list is represented by void_list_node.  */
13620
13621 static tree
13622 cp_parser_mem_initializer (cp_parser* parser)
13623 {
13624   tree mem_initializer_id;
13625   tree expression_list;
13626   tree member;
13627   cp_token *token = cp_lexer_peek_token (parser->lexer);
13628
13629   /* Find out what is being initialized.  */
13630   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13631     {
13632       permerror (token->location,
13633                  "anachronistic old-style base class initializer");
13634       mem_initializer_id = NULL_TREE;
13635     }
13636   else
13637     {
13638       mem_initializer_id = cp_parser_mem_initializer_id (parser);
13639       if (mem_initializer_id == error_mark_node)
13640         return mem_initializer_id;
13641     }
13642   member = expand_member_init (mem_initializer_id);
13643   if (member && !DECL_P (member))
13644     in_base_initializer = 1;
13645
13646   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13647     {
13648       bool expr_non_constant_p;
13649       cp_lexer_set_source_position (parser->lexer);
13650       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13651       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13652       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13653       expression_list = build_tree_list (NULL_TREE, expression_list);
13654     }
13655   else
13656     {
13657       vec<tree, va_gc> *vec;
13658       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13659                                                      /*cast_p=*/false,
13660                                                      /*allow_expansion_p=*/true,
13661                                                      /*non_constant_p=*/NULL);
13662       if (vec == NULL)
13663         return error_mark_node;
13664       expression_list = build_tree_list_vec (vec);
13665       release_tree_vector (vec);
13666     }
13667
13668   if (expression_list == error_mark_node)
13669     return error_mark_node;
13670   if (!expression_list)
13671     expression_list = void_type_node;
13672
13673   in_base_initializer = 0;
13674
13675   return member ? build_tree_list (member, expression_list) : error_mark_node;
13676 }
13677
13678 /* Parse a mem-initializer-id.
13679
13680    mem-initializer-id:
13681      :: [opt] nested-name-specifier [opt] class-name
13682      decltype-specifier (C++11)
13683      identifier
13684
13685    Returns a TYPE indicating the class to be initialized for the first
13686    production (and the second in C++11).  Returns an IDENTIFIER_NODE
13687    indicating the data member to be initialized for the last production.  */
13688
13689 static tree
13690 cp_parser_mem_initializer_id (cp_parser* parser)
13691 {
13692   bool global_scope_p;
13693   bool nested_name_specifier_p;
13694   bool template_p = false;
13695   tree id;
13696
13697   cp_token *token = cp_lexer_peek_token (parser->lexer);
13698
13699   /* `typename' is not allowed in this context ([temp.res]).  */
13700   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13701     {
13702       error_at (token->location, 
13703                 "keyword %<typename%> not allowed in this context (a qualified "
13704                 "member initializer is implicitly a type)");
13705       cp_lexer_consume_token (parser->lexer);
13706     }
13707   /* Look for the optional `::' operator.  */
13708   global_scope_p
13709     = (cp_parser_global_scope_opt (parser,
13710                                    /*current_scope_valid_p=*/false)
13711        != NULL_TREE);
13712   /* Look for the optional nested-name-specifier.  The simplest way to
13713      implement:
13714
13715        [temp.res]
13716
13717        The keyword `typename' is not permitted in a base-specifier or
13718        mem-initializer; in these contexts a qualified name that
13719        depends on a template-parameter is implicitly assumed to be a
13720        type name.
13721
13722      is to assume that we have seen the `typename' keyword at this
13723      point.  */
13724   nested_name_specifier_p
13725     = (cp_parser_nested_name_specifier_opt (parser,
13726                                             /*typename_keyword_p=*/true,
13727                                             /*check_dependency_p=*/true,
13728                                             /*type_p=*/true,
13729                                             /*is_declaration=*/true)
13730        != NULL_TREE);
13731   if (nested_name_specifier_p)
13732     template_p = cp_parser_optional_template_keyword (parser);
13733   /* If there is a `::' operator or a nested-name-specifier, then we
13734      are definitely looking for a class-name.  */
13735   if (global_scope_p || nested_name_specifier_p)
13736     return cp_parser_class_name (parser,
13737                                  /*typename_keyword_p=*/true,
13738                                  /*template_keyword_p=*/template_p,
13739                                  typename_type,
13740                                  /*check_dependency_p=*/true,
13741                                  /*class_head_p=*/false,
13742                                  /*is_declaration=*/true);
13743   /* Otherwise, we could also be looking for an ordinary identifier.  */
13744   cp_parser_parse_tentatively (parser);
13745   if (cp_lexer_next_token_is_decltype (parser->lexer))
13746     /* Try a decltype-specifier.  */
13747     id = cp_parser_decltype (parser);
13748   else
13749     /* Otherwise, try a class-name.  */
13750     id = cp_parser_class_name (parser,
13751                                /*typename_keyword_p=*/true,
13752                                /*template_keyword_p=*/false,
13753                                none_type,
13754                                /*check_dependency_p=*/true,
13755                                /*class_head_p=*/false,
13756                                /*is_declaration=*/true);
13757   /* If we found one, we're done.  */
13758   if (cp_parser_parse_definitely (parser))
13759     return id;
13760   /* Otherwise, look for an ordinary identifier.  */
13761   return cp_parser_identifier (parser);
13762 }
13763
13764 /* Overloading [gram.over] */
13765
13766 /* Parse an operator-function-id.
13767
13768    operator-function-id:
13769      operator operator
13770
13771    Returns an IDENTIFIER_NODE for the operator which is a
13772    human-readable spelling of the identifier, e.g., `operator +'.  */
13773
13774 static cp_expr
13775 cp_parser_operator_function_id (cp_parser* parser)
13776 {
13777   /* Look for the `operator' keyword.  */
13778   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13779     return error_mark_node;
13780   /* And then the name of the operator itself.  */
13781   return cp_parser_operator (parser);
13782 }
13783
13784 /* Return an identifier node for a user-defined literal operator.
13785    The suffix identifier is chained to the operator name identifier.  */
13786
13787 static tree
13788 cp_literal_operator_id (const char* name)
13789 {
13790   tree identifier;
13791   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13792                               + strlen (name) + 10);
13793   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13794   identifier = get_identifier (buffer);
13795
13796   return identifier;
13797 }
13798
13799 /* Parse an operator.
13800
13801    operator:
13802      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13803      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13804      || ++ -- , ->* -> () []
13805
13806    GNU Extensions:
13807
13808    operator:
13809      <? >? <?= >?=
13810
13811    Returns an IDENTIFIER_NODE for the operator which is a
13812    human-readable spelling of the identifier, e.g., `operator +'.  */
13813
13814 static cp_expr
13815 cp_parser_operator (cp_parser* parser)
13816 {
13817   tree id = NULL_TREE;
13818   cp_token *token;
13819   bool utf8 = false;
13820
13821   /* Peek at the next token.  */
13822   token = cp_lexer_peek_token (parser->lexer);
13823
13824   location_t start_loc = token->location;
13825
13826   /* Figure out which operator we have.  */
13827   switch (token->type)
13828     {
13829     case CPP_KEYWORD:
13830       {
13831         enum tree_code op;
13832
13833         /* The keyword should be either `new' or `delete'.  */
13834         if (token->keyword == RID_NEW)
13835           op = NEW_EXPR;
13836         else if (token->keyword == RID_DELETE)
13837           op = DELETE_EXPR;
13838         else
13839           break;
13840
13841         /* Consume the `new' or `delete' token.  */
13842         location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13843
13844         /* Peek at the next token.  */
13845         token = cp_lexer_peek_token (parser->lexer);
13846         /* If it's a `[' token then this is the array variant of the
13847            operator.  */
13848         if (token->type == CPP_OPEN_SQUARE)
13849           {
13850             /* Consume the `[' token.  */
13851             cp_lexer_consume_token (parser->lexer);
13852             /* Look for the `]' token.  */
13853             if (cp_token *close_token
13854                 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13855               end_loc = close_token->location;
13856             id = ansi_opname (op == NEW_EXPR
13857                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13858           }
13859         /* Otherwise, we have the non-array variant.  */
13860         else
13861           id = ansi_opname (op);
13862
13863         location_t loc = make_location (start_loc, start_loc, end_loc);
13864
13865         return cp_expr (id, loc);
13866       }
13867
13868     case CPP_PLUS:
13869       id = ansi_opname (PLUS_EXPR);
13870       break;
13871
13872     case CPP_MINUS:
13873       id = ansi_opname (MINUS_EXPR);
13874       break;
13875
13876     case CPP_MULT:
13877       id = ansi_opname (MULT_EXPR);
13878       break;
13879
13880     case CPP_DIV:
13881       id = ansi_opname (TRUNC_DIV_EXPR);
13882       break;
13883
13884     case CPP_MOD:
13885       id = ansi_opname (TRUNC_MOD_EXPR);
13886       break;
13887
13888     case CPP_XOR:
13889       id = ansi_opname (BIT_XOR_EXPR);
13890       break;
13891
13892     case CPP_AND:
13893       id = ansi_opname (BIT_AND_EXPR);
13894       break;
13895
13896     case CPP_OR:
13897       id = ansi_opname (BIT_IOR_EXPR);
13898       break;
13899
13900     case CPP_COMPL:
13901       id = ansi_opname (BIT_NOT_EXPR);
13902       break;
13903
13904     case CPP_NOT:
13905       id = ansi_opname (TRUTH_NOT_EXPR);
13906       break;
13907
13908     case CPP_EQ:
13909       id = ansi_assopname (NOP_EXPR);
13910       break;
13911
13912     case CPP_LESS:
13913       id = ansi_opname (LT_EXPR);
13914       break;
13915
13916     case CPP_GREATER:
13917       id = ansi_opname (GT_EXPR);
13918       break;
13919
13920     case CPP_PLUS_EQ:
13921       id = ansi_assopname (PLUS_EXPR);
13922       break;
13923
13924     case CPP_MINUS_EQ:
13925       id = ansi_assopname (MINUS_EXPR);
13926       break;
13927
13928     case CPP_MULT_EQ:
13929       id = ansi_assopname (MULT_EXPR);
13930       break;
13931
13932     case CPP_DIV_EQ:
13933       id = ansi_assopname (TRUNC_DIV_EXPR);
13934       break;
13935
13936     case CPP_MOD_EQ:
13937       id = ansi_assopname (TRUNC_MOD_EXPR);
13938       break;
13939
13940     case CPP_XOR_EQ:
13941       id = ansi_assopname (BIT_XOR_EXPR);
13942       break;
13943
13944     case CPP_AND_EQ:
13945       id = ansi_assopname (BIT_AND_EXPR);
13946       break;
13947
13948     case CPP_OR_EQ:
13949       id = ansi_assopname (BIT_IOR_EXPR);
13950       break;
13951
13952     case CPP_LSHIFT:
13953       id = ansi_opname (LSHIFT_EXPR);
13954       break;
13955
13956     case CPP_RSHIFT:
13957       id = ansi_opname (RSHIFT_EXPR);
13958       break;
13959
13960     case CPP_LSHIFT_EQ:
13961       id = ansi_assopname (LSHIFT_EXPR);
13962       break;
13963
13964     case CPP_RSHIFT_EQ:
13965       id = ansi_assopname (RSHIFT_EXPR);
13966       break;
13967
13968     case CPP_EQ_EQ:
13969       id = ansi_opname (EQ_EXPR);
13970       break;
13971
13972     case CPP_NOT_EQ:
13973       id = ansi_opname (NE_EXPR);
13974       break;
13975
13976     case CPP_LESS_EQ:
13977       id = ansi_opname (LE_EXPR);
13978       break;
13979
13980     case CPP_GREATER_EQ:
13981       id = ansi_opname (GE_EXPR);
13982       break;
13983
13984     case CPP_AND_AND:
13985       id = ansi_opname (TRUTH_ANDIF_EXPR);
13986       break;
13987
13988     case CPP_OR_OR:
13989       id = ansi_opname (TRUTH_ORIF_EXPR);
13990       break;
13991
13992     case CPP_PLUS_PLUS:
13993       id = ansi_opname (POSTINCREMENT_EXPR);
13994       break;
13995
13996     case CPP_MINUS_MINUS:
13997       id = ansi_opname (PREDECREMENT_EXPR);
13998       break;
13999
14000     case CPP_COMMA:
14001       id = ansi_opname (COMPOUND_EXPR);
14002       break;
14003
14004     case CPP_DEREF_STAR:
14005       id = ansi_opname (MEMBER_REF);
14006       break;
14007
14008     case CPP_DEREF:
14009       id = ansi_opname (COMPONENT_REF);
14010       break;
14011
14012     case CPP_OPEN_PAREN:
14013       /* Consume the `('.  */
14014       cp_lexer_consume_token (parser->lexer);
14015       /* Look for the matching `)'.  */
14016       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14017       return ansi_opname (CALL_EXPR);
14018
14019     case CPP_OPEN_SQUARE:
14020       /* Consume the `['.  */
14021       cp_lexer_consume_token (parser->lexer);
14022       /* Look for the matching `]'.  */
14023       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14024       return ansi_opname (ARRAY_REF);
14025
14026     case CPP_UTF8STRING:
14027     case CPP_UTF8STRING_USERDEF:
14028       utf8 = true;
14029     case CPP_STRING:
14030     case CPP_WSTRING:
14031     case CPP_STRING16:
14032     case CPP_STRING32:
14033     case CPP_STRING_USERDEF:
14034     case CPP_WSTRING_USERDEF:
14035     case CPP_STRING16_USERDEF:
14036     case CPP_STRING32_USERDEF:
14037       {
14038         tree str, string_tree;
14039         int sz, len;
14040
14041         if (cxx_dialect == cxx98)
14042           maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14043
14044         /* Consume the string.  */
14045         str = cp_parser_string_literal (parser, /*translate=*/true,
14046                                       /*wide_ok=*/true, /*lookup_udlit=*/false);
14047         if (str == error_mark_node)
14048           return error_mark_node;
14049         else if (TREE_CODE (str) == USERDEF_LITERAL)
14050           {
14051             string_tree = USERDEF_LITERAL_VALUE (str);
14052             id = USERDEF_LITERAL_SUFFIX_ID (str);
14053           }
14054         else
14055           {
14056             string_tree = str;
14057             /* Look for the suffix identifier.  */
14058             token = cp_lexer_peek_token (parser->lexer);
14059             if (token->type == CPP_NAME)
14060               id = cp_parser_identifier (parser);
14061             else if (token->type == CPP_KEYWORD)
14062               {
14063                 error ("unexpected keyword;"
14064                        " remove space between quotes and suffix identifier");
14065                 return error_mark_node;
14066               }
14067             else
14068               {
14069                 error ("expected suffix identifier");
14070                 return error_mark_node;
14071               }
14072           }
14073         sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14074                                (TREE_TYPE (TREE_TYPE (string_tree))));
14075         len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14076         if (len != 0)
14077           {
14078             error ("expected empty string after %<operator%> keyword");
14079             return error_mark_node;
14080           }
14081         if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14082             != char_type_node)
14083           {
14084             error ("invalid encoding prefix in literal operator");
14085             return error_mark_node;
14086           }
14087         if (id != error_mark_node)
14088           {
14089             const char *name = IDENTIFIER_POINTER (id);
14090             id = cp_literal_operator_id (name);
14091           }
14092         return id;
14093       }
14094
14095     default:
14096       /* Anything else is an error.  */
14097       break;
14098     }
14099
14100   /* If we have selected an identifier, we need to consume the
14101      operator token.  */
14102   if (id)
14103     cp_lexer_consume_token (parser->lexer);
14104   /* Otherwise, no valid operator name was present.  */
14105   else
14106     {
14107       cp_parser_error (parser, "expected operator");
14108       id = error_mark_node;
14109     }
14110
14111   return cp_expr (id, start_loc);
14112 }
14113
14114 /* Parse a template-declaration.
14115
14116    template-declaration:
14117      export [opt] template < template-parameter-list > declaration
14118
14119    If MEMBER_P is TRUE, this template-declaration occurs within a
14120    class-specifier.
14121
14122    The grammar rule given by the standard isn't correct.  What
14123    is really meant is:
14124
14125    template-declaration:
14126      export [opt] template-parameter-list-seq
14127        decl-specifier-seq [opt] init-declarator [opt] ;
14128      export [opt] template-parameter-list-seq
14129        function-definition
14130
14131    template-parameter-list-seq:
14132      template-parameter-list-seq [opt]
14133      template < template-parameter-list >
14134
14135    Concept Extensions:
14136
14137    template-parameter-list-seq:
14138      template < template-parameter-list > requires-clause [opt]
14139
14140    requires-clause:
14141      requires logical-or-expression  */
14142
14143 static void
14144 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14145 {
14146   /* Check for `export'.  */
14147   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14148     {
14149       /* Consume the `export' token.  */
14150       cp_lexer_consume_token (parser->lexer);
14151       /* Warn that we do not support `export'.  */
14152       warning (0, "keyword %<export%> not implemented, and will be ignored");
14153     }
14154
14155   cp_parser_template_declaration_after_export (parser, member_p);
14156 }
14157
14158 /* Parse a template-parameter-list.
14159
14160    template-parameter-list:
14161      template-parameter
14162      template-parameter-list , template-parameter
14163
14164    Returns a TREE_LIST.  Each node represents a template parameter.
14165    The nodes are connected via their TREE_CHAINs.  */
14166
14167 static tree
14168 cp_parser_template_parameter_list (cp_parser* parser)
14169 {
14170   tree parameter_list = NULL_TREE;
14171
14172   begin_template_parm_list ();
14173
14174   /* The loop below parses the template parms.  We first need to know
14175      the total number of template parms to be able to compute proper
14176      canonical types of each dependent type. So after the loop, when
14177      we know the total number of template parms,
14178      end_template_parm_list computes the proper canonical types and
14179      fixes up the dependent types accordingly.  */
14180   while (true)
14181     {
14182       tree parameter;
14183       bool is_non_type;
14184       bool is_parameter_pack;
14185       location_t parm_loc;
14186
14187       /* Parse the template-parameter.  */
14188       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14189       parameter = cp_parser_template_parameter (parser, 
14190                                                 &is_non_type,
14191                                                 &is_parameter_pack);
14192       /* Add it to the list.  */
14193       if (parameter != error_mark_node)
14194         parameter_list = process_template_parm (parameter_list,
14195                                                 parm_loc,
14196                                                 parameter,
14197                                                 is_non_type,
14198                                                 is_parameter_pack);
14199       else
14200        {
14201          tree err_parm = build_tree_list (parameter, parameter);
14202          parameter_list = chainon (parameter_list, err_parm);
14203        }
14204
14205       /* If the next token is not a `,', we're done.  */
14206       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14207         break;
14208       /* Otherwise, consume the `,' token.  */
14209       cp_lexer_consume_token (parser->lexer);
14210     }
14211
14212   return end_template_parm_list (parameter_list);
14213 }
14214
14215 /* Parse a introduction-list.
14216
14217    introduction-list:
14218      introduced-parameter
14219      introduction-list , introduced-parameter
14220
14221    introduced-parameter:
14222      ...[opt] identifier
14223
14224    Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
14225    then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
14226    WILDCARD_DECL will also have DECL_NAME set and token location in
14227    DECL_SOURCE_LOCATION.  */
14228
14229 static tree
14230 cp_parser_introduction_list (cp_parser *parser)
14231 {
14232   vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14233
14234   while (true)
14235     {
14236       bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14237       if (is_pack)
14238         cp_lexer_consume_token (parser->lexer);
14239
14240       /* Build placeholder. */
14241       tree parm = build_nt (WILDCARD_DECL);
14242       DECL_SOURCE_LOCATION (parm)
14243         = cp_lexer_peek_token (parser->lexer)->location;
14244       DECL_NAME (parm) = cp_parser_identifier (parser);
14245       WILDCARD_PACK_P (parm) = is_pack;
14246       vec_safe_push (introduction_vec, parm);
14247
14248       /* If the next token is not a `,', we're done.  */
14249       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14250         break;
14251       /* Otherwise, consume the `,' token.  */
14252       cp_lexer_consume_token (parser->lexer);
14253     }
14254
14255   /* Convert the vec into a TREE_VEC.  */
14256   tree introduction_list = make_tree_vec (introduction_vec->length ());
14257   unsigned int n;
14258   tree parm;
14259   FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14260     TREE_VEC_ELT (introduction_list, n) = parm;
14261
14262   release_tree_vector (introduction_vec);
14263   return introduction_list;
14264 }
14265
14266 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14267    is an abstract declarator. */
14268
14269 static inline cp_declarator*
14270 get_id_declarator (cp_declarator *declarator)
14271 {
14272   cp_declarator *d = declarator;
14273   while (d && d->kind != cdk_id)
14274     d = d->declarator;
14275   return d;
14276 }
14277
14278 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14279    is an abstract declarator. */
14280
14281 static inline tree
14282 get_unqualified_id (cp_declarator *declarator)
14283 {
14284   declarator = get_id_declarator (declarator);
14285   if (declarator)
14286     return declarator->u.id.unqualified_name;
14287   else
14288     return NULL_TREE;
14289 }
14290
14291 /* Returns true if DECL represents a constrained-parameter.  */
14292
14293 static inline bool
14294 is_constrained_parameter (tree decl)
14295 {
14296   return (decl
14297           && TREE_CODE (decl) == TYPE_DECL
14298           && CONSTRAINED_PARM_CONCEPT (decl)
14299           && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14300 }
14301
14302 /* Returns true if PARM declares a constrained-parameter. */
14303
14304 static inline bool
14305 is_constrained_parameter (cp_parameter_declarator *parm)
14306 {
14307   return is_constrained_parameter (parm->decl_specifiers.type);
14308 }
14309
14310 /* Check that the type parameter is only a declarator-id, and that its
14311    type is not cv-qualified. */
14312
14313 bool
14314 cp_parser_check_constrained_type_parm (cp_parser *parser,
14315                                        cp_parameter_declarator *parm)
14316 {
14317   if (!parm->declarator)
14318     return true;
14319
14320   if (parm->declarator->kind != cdk_id)
14321     {
14322       cp_parser_error (parser, "invalid constrained type parameter");
14323       return false;
14324     }
14325
14326   /* Don't allow cv-qualified type parameters.  */
14327   if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14328       || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14329     {
14330       cp_parser_error (parser, "cv-qualified type parameter");
14331       return false;
14332     }
14333
14334   return true;
14335 }
14336
14337 /* Finish parsing/processing a template type parameter and checking
14338    various restrictions. */
14339
14340 static inline tree
14341 cp_parser_constrained_type_template_parm (cp_parser *parser,
14342                                           tree id,
14343                                           cp_parameter_declarator* parmdecl)
14344 {
14345   if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14346     return finish_template_type_parm (class_type_node, id);
14347   else
14348     return error_mark_node;
14349 }
14350
14351 static tree
14352 finish_constrained_template_template_parm (tree proto, tree id)
14353 {
14354   /* FIXME: This should probably be copied, and we may need to adjust
14355      the template parameter depths.  */
14356   tree saved_parms = current_template_parms;
14357   begin_template_parm_list ();
14358   current_template_parms = DECL_TEMPLATE_PARMS (proto);
14359   end_template_parm_list ();
14360
14361   tree parm = finish_template_template_parm (class_type_node, id);
14362   current_template_parms = saved_parms;
14363
14364   return parm;
14365 }
14366
14367 /* Finish parsing/processing a template template parameter by borrowing
14368    the template parameter list from the prototype parameter.  */
14369
14370 static tree
14371 cp_parser_constrained_template_template_parm (cp_parser *parser,
14372                                               tree proto,
14373                                               tree id,
14374                                               cp_parameter_declarator *parmdecl)
14375 {
14376   if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14377     return error_mark_node;
14378   return finish_constrained_template_template_parm (proto, id);
14379 }
14380
14381 /* Create a new non-type template parameter from the given PARM
14382    declarator.  */
14383
14384 static tree
14385 constrained_non_type_template_parm (bool *is_non_type,
14386                                     cp_parameter_declarator *parm)
14387 {
14388   *is_non_type = true;
14389   cp_declarator *decl = parm->declarator;
14390   cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14391   specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14392   return grokdeclarator (decl, specs, TPARM, 0, NULL);
14393 }
14394
14395 /* Build a constrained template parameter based on the PARMDECL
14396    declarator. The type of PARMDECL is the constrained type, which
14397    refers to the prototype template parameter that ultimately
14398    specifies the type of the declared parameter. */
14399
14400 static tree
14401 finish_constrained_parameter (cp_parser *parser,
14402                               cp_parameter_declarator *parmdecl,
14403                               bool *is_non_type,
14404                               bool *is_parameter_pack)
14405 {
14406   tree decl = parmdecl->decl_specifiers.type;
14407   tree id = get_unqualified_id (parmdecl->declarator);
14408   tree def = parmdecl->default_argument;
14409   tree proto = DECL_INITIAL (decl);
14410
14411   /* A template parameter constrained by a variadic concept shall also
14412      be declared as a template parameter pack.  */
14413   bool is_variadic = template_parameter_pack_p (proto);
14414   if (is_variadic && !*is_parameter_pack)
14415     cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14416
14417   /* Build the parameter. Return an error if the declarator was invalid. */
14418   tree parm;
14419   if (TREE_CODE (proto) == TYPE_DECL)
14420     parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14421   else if (TREE_CODE (proto) == TEMPLATE_DECL)
14422     parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14423                                                          parmdecl);
14424   else
14425     parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14426   if (parm == error_mark_node)
14427     return error_mark_node;
14428
14429   /* Finish the parameter decl and create a node attaching the
14430      default argument and constraint.  */
14431   parm = build_tree_list (def, parm);
14432   TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14433
14434   return parm;
14435 }
14436
14437 /* Returns true if the parsed type actually represents the declaration
14438    of a type template-parameter.  */
14439
14440 static inline bool
14441 declares_constrained_type_template_parameter (tree type)
14442 {
14443   return (is_constrained_parameter (type)
14444           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14445 }
14446
14447
14448 /* Returns true if the parsed type actually represents the declaration of
14449    a template template-parameter.  */
14450
14451 static bool
14452 declares_constrained_template_template_parameter (tree type)
14453 {
14454   return (is_constrained_parameter (type)
14455           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14456 }
14457
14458 /* Parse a default argument for a type template-parameter.
14459    Note that diagnostics are handled in cp_parser_template_parameter.  */
14460
14461 static tree
14462 cp_parser_default_type_template_argument (cp_parser *parser)
14463 {
14464   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14465
14466   /* Consume the `=' token.  */
14467   cp_lexer_consume_token (parser->lexer);
14468
14469   cp_token *token = cp_lexer_peek_token (parser->lexer);
14470
14471   /* Parse the default-argument.  */
14472   push_deferring_access_checks (dk_no_deferred);
14473   tree default_argument = cp_parser_type_id (parser);
14474   pop_deferring_access_checks ();
14475
14476   if (flag_concepts && type_uses_auto (default_argument))
14477     {
14478       error_at (token->location,
14479                 "invalid use of %<auto%> in default template argument");
14480       return error_mark_node;
14481     }
14482
14483   return default_argument;
14484 }
14485
14486 /* Parse a default argument for a template template-parameter.  */
14487
14488 static tree
14489 cp_parser_default_template_template_argument (cp_parser *parser)
14490 {
14491   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14492
14493   bool is_template;
14494
14495   /* Consume the `='.  */
14496   cp_lexer_consume_token (parser->lexer);
14497   /* Parse the id-expression.  */
14498   push_deferring_access_checks (dk_no_deferred);
14499   /* save token before parsing the id-expression, for error
14500      reporting */
14501   const cp_token* token = cp_lexer_peek_token (parser->lexer);
14502   tree default_argument
14503     = cp_parser_id_expression (parser,
14504                                /*template_keyword_p=*/false,
14505                                /*check_dependency_p=*/true,
14506                                /*template_p=*/&is_template,
14507                                /*declarator_p=*/false,
14508                                /*optional_p=*/false);
14509   if (TREE_CODE (default_argument) == TYPE_DECL)
14510     /* If the id-expression was a template-id that refers to
14511        a template-class, we already have the declaration here,
14512        so no further lookup is needed.  */
14513     ;
14514   else
14515     /* Look up the name.  */
14516     default_argument
14517       = cp_parser_lookup_name (parser, default_argument,
14518                                none_type,
14519                                /*is_template=*/is_template,
14520                                /*is_namespace=*/false,
14521                                /*check_dependency=*/true,
14522                                /*ambiguous_decls=*/NULL,
14523                                token->location);
14524   /* See if the default argument is valid.  */
14525   default_argument = check_template_template_default_arg (default_argument);
14526   pop_deferring_access_checks ();
14527   return default_argument;
14528 }
14529
14530 /* Parse a template-parameter.
14531
14532    template-parameter:
14533      type-parameter
14534      parameter-declaration
14535
14536    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
14537    the parameter.  The TREE_PURPOSE is the default value, if any.
14538    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
14539    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
14540    set to true iff this parameter is a parameter pack. */
14541
14542 static tree
14543 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14544                               bool *is_parameter_pack)
14545 {
14546   cp_token *token;
14547   cp_parameter_declarator *parameter_declarator;
14548   tree parm;
14549
14550   /* Assume it is a type parameter or a template parameter.  */
14551   *is_non_type = false;
14552   /* Assume it not a parameter pack. */
14553   *is_parameter_pack = false;
14554   /* Peek at the next token.  */
14555   token = cp_lexer_peek_token (parser->lexer);
14556   /* If it is `class' or `template', we have a type-parameter.  */
14557   if (token->keyword == RID_TEMPLATE)
14558     return cp_parser_type_parameter (parser, is_parameter_pack);
14559   /* If it is `class' or `typename' we do not know yet whether it is a
14560      type parameter or a non-type parameter.  Consider:
14561
14562        template <typename T, typename T::X X> ...
14563
14564      or:
14565
14566        template <class C, class D*> ...
14567
14568      Here, the first parameter is a type parameter, and the second is
14569      a non-type parameter.  We can tell by looking at the token after
14570      the identifier -- if it is a `,', `=', or `>' then we have a type
14571      parameter.  */
14572   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14573     {
14574       /* Peek at the token after `class' or `typename'.  */
14575       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14576       /* If it's an ellipsis, we have a template type parameter
14577          pack. */
14578       if (token->type == CPP_ELLIPSIS)
14579         return cp_parser_type_parameter (parser, is_parameter_pack);
14580       /* If it's an identifier, skip it.  */
14581       if (token->type == CPP_NAME)
14582         token = cp_lexer_peek_nth_token (parser->lexer, 3);
14583       /* Now, see if the token looks like the end of a template
14584          parameter.  */
14585       if (token->type == CPP_COMMA
14586           || token->type == CPP_EQ
14587           || token->type == CPP_GREATER)
14588         return cp_parser_type_parameter (parser, is_parameter_pack);
14589     }
14590
14591   /* Otherwise, it is a non-type parameter or a constrained parameter.
14592
14593      [temp.param]
14594
14595      When parsing a default template-argument for a non-type
14596      template-parameter, the first non-nested `>' is taken as the end
14597      of the template parameter-list rather than a greater-than
14598      operator.  */
14599   parameter_declarator
14600      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14601                                         /*parenthesized_p=*/NULL);
14602
14603   if (!parameter_declarator)
14604     return error_mark_node;
14605
14606   /* If the parameter declaration is marked as a parameter pack, set
14607    *IS_PARAMETER_PACK to notify the caller.  */
14608   if (parameter_declarator->template_parameter_pack_p)
14609     *is_parameter_pack = true;
14610
14611   if (parameter_declarator->default_argument)
14612     {
14613       /* Can happen in some cases of erroneous input (c++/34892).  */
14614       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14615         /* Consume the `...' for better error recovery.  */
14616         cp_lexer_consume_token (parser->lexer);
14617     }
14618
14619   // The parameter may have been constrained.
14620   if (is_constrained_parameter (parameter_declarator))
14621     return finish_constrained_parameter (parser,
14622                                          parameter_declarator,
14623                                          is_non_type,
14624                                          is_parameter_pack);
14625
14626   // Now we're sure that the parameter is a non-type parameter.
14627   *is_non_type = true;
14628
14629   parm = grokdeclarator (parameter_declarator->declarator,
14630                          &parameter_declarator->decl_specifiers,
14631                          TPARM, /*initialized=*/0,
14632                          /*attrlist=*/NULL);
14633   if (parm == error_mark_node)
14634     return error_mark_node;
14635
14636   return build_tree_list (parameter_declarator->default_argument, parm);
14637 }
14638
14639 /* Parse a type-parameter.
14640
14641    type-parameter:
14642      class identifier [opt]
14643      class identifier [opt] = type-id
14644      typename identifier [opt]
14645      typename identifier [opt] = type-id
14646      template < template-parameter-list > class identifier [opt]
14647      template < template-parameter-list > class identifier [opt]
14648        = id-expression
14649
14650    GNU Extension (variadic templates):
14651
14652    type-parameter:
14653      class ... identifier [opt]
14654      typename ... identifier [opt]
14655
14656    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
14657    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
14658    the declaration of the parameter.
14659
14660    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14661
14662 static tree
14663 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14664 {
14665   cp_token *token;
14666   tree parameter;
14667
14668   /* Look for a keyword to tell us what kind of parameter this is.  */
14669   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14670   if (!token)
14671     return error_mark_node;
14672
14673   switch (token->keyword)
14674     {
14675     case RID_CLASS:
14676     case RID_TYPENAME:
14677       {
14678         tree identifier;
14679         tree default_argument;
14680
14681         /* If the next token is an ellipsis, we have a template
14682            argument pack. */
14683         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14684           {
14685             /* Consume the `...' token. */
14686             cp_lexer_consume_token (parser->lexer);
14687             maybe_warn_variadic_templates ();
14688
14689             *is_parameter_pack = true;
14690           }
14691
14692         /* If the next token is an identifier, then it names the
14693            parameter.  */
14694         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14695           identifier = cp_parser_identifier (parser);
14696         else
14697           identifier = NULL_TREE;
14698
14699         /* Create the parameter.  */
14700         parameter = finish_template_type_parm (class_type_node, identifier);
14701
14702         /* If the next token is an `=', we have a default argument.  */
14703         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14704           {
14705             default_argument
14706               = cp_parser_default_type_template_argument (parser);
14707
14708             /* Template parameter packs cannot have default
14709                arguments. */
14710             if (*is_parameter_pack)
14711               {
14712                 if (identifier)
14713                   error_at (token->location,
14714                             "template parameter pack %qD cannot have a "
14715                             "default argument", identifier);
14716                 else
14717                   error_at (token->location,
14718                             "template parameter packs cannot have "
14719                             "default arguments");
14720                 default_argument = NULL_TREE;
14721               }
14722             else if (check_for_bare_parameter_packs (default_argument))
14723               default_argument = error_mark_node;
14724           }
14725         else
14726           default_argument = NULL_TREE;
14727
14728         /* Create the combined representation of the parameter and the
14729            default argument.  */
14730         parameter = build_tree_list (default_argument, parameter);
14731       }
14732       break;
14733
14734     case RID_TEMPLATE:
14735       {
14736         tree identifier;
14737         tree default_argument;
14738
14739         /* Look for the `<'.  */
14740         cp_parser_require (parser, CPP_LESS, RT_LESS);
14741         /* Parse the template-parameter-list.  */
14742         cp_parser_template_parameter_list (parser);
14743         /* Look for the `>'.  */
14744         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14745
14746         // If template requirements are present, parse them.
14747         if (flag_concepts)
14748           {
14749             tree reqs = get_shorthand_constraints (current_template_parms);
14750             if (tree r = cp_parser_requires_clause_opt (parser))
14751               reqs = conjoin_constraints (reqs, normalize_expression (r));
14752             TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14753           }
14754
14755         /* Look for the `class' or 'typename' keywords.  */
14756         cp_parser_type_parameter_key (parser);
14757         /* If the next token is an ellipsis, we have a template
14758            argument pack. */
14759         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14760           {
14761             /* Consume the `...' token. */
14762             cp_lexer_consume_token (parser->lexer);
14763             maybe_warn_variadic_templates ();
14764
14765             *is_parameter_pack = true;
14766           }
14767         /* If the next token is an `=', then there is a
14768            default-argument.  If the next token is a `>', we are at
14769            the end of the parameter-list.  If the next token is a `,',
14770            then we are at the end of this parameter.  */
14771         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14772             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14773             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14774           {
14775             identifier = cp_parser_identifier (parser);
14776             /* Treat invalid names as if the parameter were nameless.  */
14777             if (identifier == error_mark_node)
14778               identifier = NULL_TREE;
14779           }
14780         else
14781           identifier = NULL_TREE;
14782
14783         /* Create the template parameter.  */
14784         parameter = finish_template_template_parm (class_type_node,
14785                                                    identifier);
14786
14787         /* If the next token is an `=', then there is a
14788            default-argument.  */
14789         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14790           {
14791             default_argument
14792               = cp_parser_default_template_template_argument (parser);
14793
14794             /* Template parameter packs cannot have default
14795                arguments. */
14796             if (*is_parameter_pack)
14797               {
14798                 if (identifier)
14799                   error_at (token->location,
14800                             "template parameter pack %qD cannot "
14801                             "have a default argument",
14802                             identifier);
14803                 else
14804                   error_at (token->location, "template parameter packs cannot "
14805                             "have default arguments");
14806                 default_argument = NULL_TREE;
14807               }
14808           }
14809         else
14810           default_argument = NULL_TREE;
14811
14812         /* Create the combined representation of the parameter and the
14813            default argument.  */
14814         parameter = build_tree_list (default_argument, parameter);
14815       }
14816       break;
14817
14818     default:
14819       gcc_unreachable ();
14820       break;
14821     }
14822
14823   return parameter;
14824 }
14825
14826 /* Parse a template-id.
14827
14828    template-id:
14829      template-name < template-argument-list [opt] >
14830
14831    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14832    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
14833    returned.  Otherwise, if the template-name names a function, or set
14834    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
14835    names a class, returns a TYPE_DECL for the specialization.
14836
14837    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14838    uninstantiated templates.  */
14839
14840 static tree
14841 cp_parser_template_id (cp_parser *parser,
14842                        bool template_keyword_p,
14843                        bool check_dependency_p,
14844                        enum tag_types tag_type,
14845                        bool is_declaration)
14846 {
14847   tree templ;
14848   tree arguments;
14849   tree template_id;
14850   cp_token_position start_of_id = 0;
14851   cp_token *next_token = NULL, *next_token_2 = NULL;
14852   bool is_identifier;
14853
14854   /* If the next token corresponds to a template-id, there is no need
14855      to reparse it.  */
14856   next_token = cp_lexer_peek_token (parser->lexer);
14857   if (next_token->type == CPP_TEMPLATE_ID)
14858     {
14859       cp_lexer_consume_token (parser->lexer);
14860       return saved_checks_value (next_token->u.tree_check_value);
14861     }
14862
14863   /* Avoid performing name lookup if there is no possibility of
14864      finding a template-id.  */
14865   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14866       || (next_token->type == CPP_NAME
14867           && !cp_parser_nth_token_starts_template_argument_list_p
14868                (parser, 2)))
14869     {
14870       cp_parser_error (parser, "expected template-id");
14871       return error_mark_node;
14872     }
14873
14874   /* Remember where the template-id starts.  */
14875   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14876     start_of_id = cp_lexer_token_position (parser->lexer, false);
14877
14878   push_deferring_access_checks (dk_deferred);
14879
14880   /* Parse the template-name.  */
14881   is_identifier = false;
14882   templ = cp_parser_template_name (parser, template_keyword_p,
14883                                    check_dependency_p,
14884                                    is_declaration,
14885                                    tag_type,
14886                                    &is_identifier);
14887   if (templ == error_mark_node || is_identifier)
14888     {
14889       pop_deferring_access_checks ();
14890       return templ;
14891     }
14892
14893   /* Since we're going to preserve any side-effects from this parse, set up a
14894      firewall to protect our callers from cp_parser_commit_to_tentative_parse
14895      in the template arguments.  */
14896   tentative_firewall firewall (parser);
14897
14898   /* If we find the sequence `[:' after a template-name, it's probably
14899      a digraph-typo for `< ::'. Substitute the tokens and check if we can
14900      parse correctly the argument list.  */
14901   next_token = cp_lexer_peek_token (parser->lexer);
14902   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14903   if (next_token->type == CPP_OPEN_SQUARE
14904       && next_token->flags & DIGRAPH
14905       && next_token_2->type == CPP_COLON
14906       && !(next_token_2->flags & PREV_WHITE))
14907     {
14908       cp_parser_parse_tentatively (parser);
14909       /* Change `:' into `::'.  */
14910       next_token_2->type = CPP_SCOPE;
14911       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14912          CPP_LESS.  */
14913       cp_lexer_consume_token (parser->lexer);
14914
14915       /* Parse the arguments.  */
14916       arguments = cp_parser_enclosed_template_argument_list (parser);
14917       if (!cp_parser_parse_definitely (parser))
14918         {
14919           /* If we couldn't parse an argument list, then we revert our changes
14920              and return simply an error. Maybe this is not a template-id
14921              after all.  */
14922           next_token_2->type = CPP_COLON;
14923           cp_parser_error (parser, "expected %<<%>");
14924           pop_deferring_access_checks ();
14925           return error_mark_node;
14926         }
14927       /* Otherwise, emit an error about the invalid digraph, but continue
14928          parsing because we got our argument list.  */
14929       if (permerror (next_token->location,
14930                      "%<<::%> cannot begin a template-argument list"))
14931         {
14932           static bool hint = false;
14933           inform (next_token->location,
14934                   "%<<:%> is an alternate spelling for %<[%>."
14935                   " Insert whitespace between %<<%> and %<::%>");
14936           if (!hint && !flag_permissive)
14937             {
14938               inform (next_token->location, "(if you use %<-fpermissive%> "
14939                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14940                       "accept your code)");
14941               hint = true;
14942             }
14943         }
14944     }
14945   else
14946     {
14947       /* Look for the `<' that starts the template-argument-list.  */
14948       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14949         {
14950           pop_deferring_access_checks ();
14951           return error_mark_node;
14952         }
14953       /* Parse the arguments.  */
14954       arguments = cp_parser_enclosed_template_argument_list (parser);
14955     }
14956
14957   /* Build a representation of the specialization.  */
14958   if (identifier_p (templ))
14959     template_id = build_min_nt_loc (next_token->location,
14960                                     TEMPLATE_ID_EXPR,
14961                                     templ, arguments);
14962   else if (DECL_TYPE_TEMPLATE_P (templ)
14963            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14964     {
14965       bool entering_scope;
14966       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14967          template (rather than some instantiation thereof) only if
14968          is not nested within some other construct.  For example, in
14969          "template <typename T> void f(T) { A<T>::", A<T> is just an
14970          instantiation of A.  */
14971       entering_scope = (template_parm_scope_p ()
14972                         && cp_lexer_next_token_is (parser->lexer,
14973                                                    CPP_SCOPE));
14974       template_id
14975         = finish_template_type (templ, arguments, entering_scope);
14976     }
14977   /* A template-like identifier may be a partial concept id. */
14978   else if (flag_concepts
14979            && (template_id = (cp_parser_maybe_partial_concept_id
14980                               (parser, templ, arguments))))
14981     return template_id;
14982   else if (variable_template_p (templ))
14983     {
14984       template_id = lookup_template_variable (templ, arguments);
14985       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14986         SET_EXPR_LOCATION (template_id, next_token->location);
14987     }
14988   else
14989     {
14990       /* If it's not a class-template or a template-template, it should be
14991          a function-template.  */
14992       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14993                    || TREE_CODE (templ) == OVERLOAD
14994                    || BASELINK_P (templ)));
14995
14996       template_id = lookup_template_function (templ, arguments);
14997       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14998         SET_EXPR_LOCATION (template_id, next_token->location);
14999     }
15000
15001   /* If parsing tentatively, replace the sequence of tokens that makes
15002      up the template-id with a CPP_TEMPLATE_ID token.  That way,
15003      should we re-parse the token stream, we will not have to repeat
15004      the effort required to do the parse, nor will we issue duplicate
15005      error messages about problems during instantiation of the
15006      template.  */
15007   if (start_of_id
15008       /* Don't do this if we had a parse error in a declarator; re-parsing
15009          might succeed if a name changes meaning (60361).  */
15010       && !(cp_parser_error_occurred (parser)
15011            && cp_parser_parsing_tentatively (parser)
15012            && parser->in_declarator_p))
15013     {
15014       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15015
15016       /* Reset the contents of the START_OF_ID token.  */
15017       token->type = CPP_TEMPLATE_ID;
15018
15019       /* Update the location to be of the form:
15020            template-name < template-argument-list [opt] >
15021            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15022          with caret == start at the start of the template-name,
15023          ranging until the closing '>'.  */
15024       location_t finish_loc
15025         = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15026       location_t combined_loc
15027         = make_location (token->location, token->location, finish_loc);
15028       token->location = combined_loc;
15029
15030       /* Retrieve any deferred checks.  Do not pop this access checks yet
15031          so the memory will not be reclaimed during token replacing below.  */
15032       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15033       token->u.tree_check_value->value = template_id;
15034       token->u.tree_check_value->checks = get_deferred_access_checks ();
15035       token->keyword = RID_MAX;
15036
15037       /* Purge all subsequent tokens.  */
15038       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15039
15040       /* ??? Can we actually assume that, if template_id ==
15041          error_mark_node, we will have issued a diagnostic to the
15042          user, as opposed to simply marking the tentative parse as
15043          failed?  */
15044       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15045         error_at (token->location, "parse error in template argument list");
15046     }
15047
15048   pop_to_parent_deferring_access_checks ();
15049   return template_id;
15050 }
15051
15052 /* Parse a template-name.
15053
15054    template-name:
15055      identifier
15056
15057    The standard should actually say:
15058
15059    template-name:
15060      identifier
15061      operator-function-id
15062
15063    A defect report has been filed about this issue.
15064
15065    A conversion-function-id cannot be a template name because they cannot
15066    be part of a template-id. In fact, looking at this code:
15067
15068    a.operator K<int>()
15069
15070    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15071    It is impossible to call a templated conversion-function-id with an
15072    explicit argument list, since the only allowed template parameter is
15073    the type to which it is converting.
15074
15075    If TEMPLATE_KEYWORD_P is true, then we have just seen the
15076    `template' keyword, in a construction like:
15077
15078      T::template f<3>()
15079
15080    In that case `f' is taken to be a template-name, even though there
15081    is no way of knowing for sure.
15082
15083    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15084    name refers to a set of overloaded functions, at least one of which
15085    is a template, or an IDENTIFIER_NODE with the name of the template,
15086    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
15087    names are looked up inside uninstantiated templates.  */
15088
15089 static tree
15090 cp_parser_template_name (cp_parser* parser,
15091                          bool template_keyword_p,
15092                          bool check_dependency_p,
15093                          bool is_declaration,
15094                          enum tag_types tag_type,
15095                          bool *is_identifier)
15096 {
15097   tree identifier;
15098   tree decl;
15099   tree fns;
15100   cp_token *token = cp_lexer_peek_token (parser->lexer);
15101
15102   /* If the next token is `operator', then we have either an
15103      operator-function-id or a conversion-function-id.  */
15104   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15105     {
15106       /* We don't know whether we're looking at an
15107          operator-function-id or a conversion-function-id.  */
15108       cp_parser_parse_tentatively (parser);
15109       /* Try an operator-function-id.  */
15110       identifier = cp_parser_operator_function_id (parser);
15111       /* If that didn't work, try a conversion-function-id.  */
15112       if (!cp_parser_parse_definitely (parser))
15113         {
15114           cp_parser_error (parser, "expected template-name");
15115           return error_mark_node;
15116         }
15117     }
15118   /* Look for the identifier.  */
15119   else
15120     identifier = cp_parser_identifier (parser);
15121
15122   /* If we didn't find an identifier, we don't have a template-id.  */
15123   if (identifier == error_mark_node)
15124     return error_mark_node;
15125
15126   /* If the name immediately followed the `template' keyword, then it
15127      is a template-name.  However, if the next token is not `<', then
15128      we do not treat it as a template-name, since it is not being used
15129      as part of a template-id.  This enables us to handle constructs
15130      like:
15131
15132        template <typename T> struct S { S(); };
15133        template <typename T> S<T>::S();
15134
15135      correctly.  We would treat `S' as a template -- if it were `S<T>'
15136      -- but we do not if there is no `<'.  */
15137
15138   if (processing_template_decl
15139       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15140     {
15141       /* In a declaration, in a dependent context, we pretend that the
15142          "template" keyword was present in order to improve error
15143          recovery.  For example, given:
15144
15145            template <typename T> void f(T::X<int>);
15146
15147          we want to treat "X<int>" as a template-id.  */
15148       if (is_declaration
15149           && !template_keyword_p
15150           && parser->scope && TYPE_P (parser->scope)
15151           && check_dependency_p
15152           && dependent_scope_p (parser->scope)
15153           /* Do not do this for dtors (or ctors), since they never
15154              need the template keyword before their name.  */
15155           && !constructor_name_p (identifier, parser->scope))
15156         {
15157           cp_token_position start = 0;
15158
15159           /* Explain what went wrong.  */
15160           error_at (token->location, "non-template %qD used as template",
15161                     identifier);
15162           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15163                   parser->scope, identifier);
15164           /* If parsing tentatively, find the location of the "<" token.  */
15165           if (cp_parser_simulate_error (parser))
15166             start = cp_lexer_token_position (parser->lexer, true);
15167           /* Parse the template arguments so that we can issue error
15168              messages about them.  */
15169           cp_lexer_consume_token (parser->lexer);
15170           cp_parser_enclosed_template_argument_list (parser);
15171           /* Skip tokens until we find a good place from which to
15172              continue parsing.  */
15173           cp_parser_skip_to_closing_parenthesis (parser,
15174                                                  /*recovering=*/true,
15175                                                  /*or_comma=*/true,
15176                                                  /*consume_paren=*/false);
15177           /* If parsing tentatively, permanently remove the
15178              template argument list.  That will prevent duplicate
15179              error messages from being issued about the missing
15180              "template" keyword.  */
15181           if (start)
15182             cp_lexer_purge_tokens_after (parser->lexer, start);
15183           if (is_identifier)
15184             *is_identifier = true;
15185           return identifier;
15186         }
15187
15188       /* If the "template" keyword is present, then there is generally
15189          no point in doing name-lookup, so we just return IDENTIFIER.
15190          But, if the qualifying scope is non-dependent then we can
15191          (and must) do name-lookup normally.  */
15192       if (template_keyword_p
15193           && (!parser->scope
15194               || (TYPE_P (parser->scope)
15195                   && dependent_type_p (parser->scope))))
15196         return identifier;
15197     }
15198
15199   /* Look up the name.  */
15200   decl = cp_parser_lookup_name (parser, identifier,
15201                                 tag_type,
15202                                 /*is_template=*/true,
15203                                 /*is_namespace=*/false,
15204                                 check_dependency_p,
15205                                 /*ambiguous_decls=*/NULL,
15206                                 token->location);
15207
15208   decl = strip_using_decl (decl);
15209
15210   /* If DECL is a template, then the name was a template-name.  */
15211   if (TREE_CODE (decl) == TEMPLATE_DECL)
15212     {
15213       if (TREE_DEPRECATED (decl)
15214           && deprecated_state != DEPRECATED_SUPPRESS)
15215         warn_deprecated_use (decl, NULL_TREE);
15216     }
15217   else
15218     {
15219       tree fn = NULL_TREE;
15220
15221       /* The standard does not explicitly indicate whether a name that
15222          names a set of overloaded declarations, some of which are
15223          templates, is a template-name.  However, such a name should
15224          be a template-name; otherwise, there is no way to form a
15225          template-id for the overloaded templates.  */
15226       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15227       if (TREE_CODE (fns) == OVERLOAD)
15228         for (fn = fns; fn; fn = OVL_NEXT (fn))
15229           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15230             break;
15231
15232       if (!fn)
15233         {
15234           /* The name does not name a template.  */
15235           cp_parser_error (parser, "expected template-name");
15236           return error_mark_node;
15237         }
15238     }
15239
15240   /* If DECL is dependent, and refers to a function, then just return
15241      its name; we will look it up again during template instantiation.  */
15242   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15243     {
15244       tree scope = ovl_scope (decl);
15245       if (TYPE_P (scope) && dependent_type_p (scope))
15246         return identifier;
15247     }
15248
15249   return decl;
15250 }
15251
15252 /* Parse a template-argument-list.
15253
15254    template-argument-list:
15255      template-argument ... [opt]
15256      template-argument-list , template-argument ... [opt]
15257
15258    Returns a TREE_VEC containing the arguments.  */
15259
15260 static tree
15261 cp_parser_template_argument_list (cp_parser* parser)
15262 {
15263   tree fixed_args[10];
15264   unsigned n_args = 0;
15265   unsigned alloced = 10;
15266   tree *arg_ary = fixed_args;
15267   tree vec;
15268   bool saved_in_template_argument_list_p;
15269   bool saved_ice_p;
15270   bool saved_non_ice_p;
15271
15272   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15273   parser->in_template_argument_list_p = true;
15274   /* Even if the template-id appears in an integral
15275      constant-expression, the contents of the argument list do
15276      not.  */
15277   saved_ice_p = parser->integral_constant_expression_p;
15278   parser->integral_constant_expression_p = false;
15279   saved_non_ice_p = parser->non_integral_constant_expression_p;
15280   parser->non_integral_constant_expression_p = false;
15281
15282   /* Parse the arguments.  */
15283   do
15284     {
15285       tree argument;
15286
15287       if (n_args)
15288         /* Consume the comma.  */
15289         cp_lexer_consume_token (parser->lexer);
15290
15291       /* Parse the template-argument.  */
15292       argument = cp_parser_template_argument (parser);
15293
15294       /* If the next token is an ellipsis, we're expanding a template
15295          argument pack. */
15296       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15297         {
15298           if (argument == error_mark_node)
15299             {
15300               cp_token *token = cp_lexer_peek_token (parser->lexer);
15301               error_at (token->location,
15302                         "expected parameter pack before %<...%>");
15303             }
15304           /* Consume the `...' token. */
15305           cp_lexer_consume_token (parser->lexer);
15306
15307           /* Make the argument into a TYPE_PACK_EXPANSION or
15308              EXPR_PACK_EXPANSION. */
15309           argument = make_pack_expansion (argument);
15310         }
15311
15312       if (n_args == alloced)
15313         {
15314           alloced *= 2;
15315
15316           if (arg_ary == fixed_args)
15317             {
15318               arg_ary = XNEWVEC (tree, alloced);
15319               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15320             }
15321           else
15322             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15323         }
15324       arg_ary[n_args++] = argument;
15325     }
15326   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15327
15328   vec = make_tree_vec (n_args);
15329
15330   while (n_args--)
15331     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15332
15333   if (arg_ary != fixed_args)
15334     free (arg_ary);
15335   parser->non_integral_constant_expression_p = saved_non_ice_p;
15336   parser->integral_constant_expression_p = saved_ice_p;
15337   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15338   if (CHECKING_P)
15339     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15340   return vec;
15341 }
15342
15343 /* Parse a template-argument.
15344
15345    template-argument:
15346      assignment-expression
15347      type-id
15348      id-expression
15349
15350    The representation is that of an assignment-expression, type-id, or
15351    id-expression -- except that the qualified id-expression is
15352    evaluated, so that the value returned is either a DECL or an
15353    OVERLOAD.
15354
15355    Although the standard says "assignment-expression", it forbids
15356    throw-expressions or assignments in the template argument.
15357    Therefore, we use "conditional-expression" instead.  */
15358
15359 static tree
15360 cp_parser_template_argument (cp_parser* parser)
15361 {
15362   tree argument;
15363   bool template_p;
15364   bool address_p;
15365   bool maybe_type_id = false;
15366   cp_token *token = NULL, *argument_start_token = NULL;
15367   location_t loc = 0;
15368   cp_id_kind idk;
15369
15370   /* There's really no way to know what we're looking at, so we just
15371      try each alternative in order.
15372
15373        [temp.arg]
15374
15375        In a template-argument, an ambiguity between a type-id and an
15376        expression is resolved to a type-id, regardless of the form of
15377        the corresponding template-parameter.
15378
15379      Therefore, we try a type-id first.  */
15380   cp_parser_parse_tentatively (parser);
15381   argument = cp_parser_template_type_arg (parser);
15382   /* If there was no error parsing the type-id but the next token is a
15383      '>>', our behavior depends on which dialect of C++ we're
15384      parsing. In C++98, we probably found a typo for '> >'. But there
15385      are type-id which are also valid expressions. For instance:
15386
15387      struct X { int operator >> (int); };
15388      template <int V> struct Foo {};
15389      Foo<X () >> 5> r;
15390
15391      Here 'X()' is a valid type-id of a function type, but the user just
15392      wanted to write the expression "X() >> 5". Thus, we remember that we
15393      found a valid type-id, but we still try to parse the argument as an
15394      expression to see what happens. 
15395
15396      In C++0x, the '>>' will be considered two separate '>'
15397      tokens.  */
15398   if (!cp_parser_error_occurred (parser)
15399       && cxx_dialect == cxx98
15400       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15401     {
15402       maybe_type_id = true;
15403       cp_parser_abort_tentative_parse (parser);
15404     }
15405   else
15406     {
15407       /* If the next token isn't a `,' or a `>', then this argument wasn't
15408       really finished. This means that the argument is not a valid
15409       type-id.  */
15410       if (!cp_parser_next_token_ends_template_argument_p (parser))
15411         cp_parser_error (parser, "expected template-argument");
15412       /* If that worked, we're done.  */
15413       if (cp_parser_parse_definitely (parser))
15414         return argument;
15415     }
15416   /* We're still not sure what the argument will be.  */
15417   cp_parser_parse_tentatively (parser);
15418   /* Try a template.  */
15419   argument_start_token = cp_lexer_peek_token (parser->lexer);
15420   argument = cp_parser_id_expression (parser,
15421                                       /*template_keyword_p=*/false,
15422                                       /*check_dependency_p=*/true,
15423                                       &template_p,
15424                                       /*declarator_p=*/false,
15425                                       /*optional_p=*/false);
15426   /* If the next token isn't a `,' or a `>', then this argument wasn't
15427      really finished.  */
15428   if (!cp_parser_next_token_ends_template_argument_p (parser))
15429     cp_parser_error (parser, "expected template-argument");
15430   if (!cp_parser_error_occurred (parser))
15431     {
15432       /* Figure out what is being referred to.  If the id-expression
15433          was for a class template specialization, then we will have a
15434          TYPE_DECL at this point.  There is no need to do name lookup
15435          at this point in that case.  */
15436       if (TREE_CODE (argument) != TYPE_DECL)
15437         argument = cp_parser_lookup_name (parser, argument,
15438                                           none_type,
15439                                           /*is_template=*/template_p,
15440                                           /*is_namespace=*/false,
15441                                           /*check_dependency=*/true,
15442                                           /*ambiguous_decls=*/NULL,
15443                                           argument_start_token->location);
15444       /* Handle a constrained-type-specifier for a non-type template
15445          parameter.  */
15446       if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15447         argument = decl;
15448       else if (TREE_CODE (argument) != TEMPLATE_DECL
15449                && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15450         cp_parser_error (parser, "expected template-name");
15451     }
15452   if (cp_parser_parse_definitely (parser))
15453     {
15454       if (TREE_DEPRECATED (argument))
15455         warn_deprecated_use (argument, NULL_TREE);
15456       return argument;
15457     }
15458   /* It must be a non-type argument.  In C++17 any constant-expression is
15459      allowed.  */
15460   if (cxx_dialect > cxx14)
15461     goto general_expr;
15462
15463   /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15464
15465      -- an integral constant-expression of integral or enumeration
15466         type; or
15467
15468      -- the name of a non-type template-parameter; or
15469
15470      -- the name of an object or function with external linkage...
15471
15472      -- the address of an object or function with external linkage...
15473
15474      -- a pointer to member...  */
15475   /* Look for a non-type template parameter.  */
15476   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15477     {
15478       cp_parser_parse_tentatively (parser);
15479       argument = cp_parser_primary_expression (parser,
15480                                                /*address_p=*/false,
15481                                                /*cast_p=*/false,
15482                                                /*template_arg_p=*/true,
15483                                                &idk);
15484       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15485           || !cp_parser_next_token_ends_template_argument_p (parser))
15486         cp_parser_simulate_error (parser);
15487       if (cp_parser_parse_definitely (parser))
15488         return argument;
15489     }
15490
15491   /* If the next token is "&", the argument must be the address of an
15492      object or function with external linkage.  */
15493   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15494   if (address_p)
15495     {
15496       loc = cp_lexer_peek_token (parser->lexer)->location;
15497       cp_lexer_consume_token (parser->lexer);
15498     }
15499   /* See if we might have an id-expression.  */
15500   token = cp_lexer_peek_token (parser->lexer);
15501   if (token->type == CPP_NAME
15502       || token->keyword == RID_OPERATOR
15503       || token->type == CPP_SCOPE
15504       || token->type == CPP_TEMPLATE_ID
15505       || token->type == CPP_NESTED_NAME_SPECIFIER)
15506     {
15507       cp_parser_parse_tentatively (parser);
15508       argument = cp_parser_primary_expression (parser,
15509                                                address_p,
15510                                                /*cast_p=*/false,
15511                                                /*template_arg_p=*/true,
15512                                                &idk);
15513       if (cp_parser_error_occurred (parser)
15514           || !cp_parser_next_token_ends_template_argument_p (parser))
15515         cp_parser_abort_tentative_parse (parser);
15516       else
15517         {
15518           tree probe;
15519
15520           if (INDIRECT_REF_P (argument))
15521             {
15522               /* Strip the dereference temporarily.  */
15523               gcc_assert (REFERENCE_REF_P (argument));
15524               argument = TREE_OPERAND (argument, 0);
15525             }
15526
15527           /* If we're in a template, we represent a qualified-id referring
15528              to a static data member as a SCOPE_REF even if the scope isn't
15529              dependent so that we can check access control later.  */
15530           probe = argument;
15531           if (TREE_CODE (probe) == SCOPE_REF)
15532             probe = TREE_OPERAND (probe, 1);
15533           if (VAR_P (probe))
15534             {
15535               /* A variable without external linkage might still be a
15536                  valid constant-expression, so no error is issued here
15537                  if the external-linkage check fails.  */
15538               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15539                 cp_parser_simulate_error (parser);
15540             }
15541           else if (is_overloaded_fn (argument))
15542             /* All overloaded functions are allowed; if the external
15543                linkage test does not pass, an error will be issued
15544                later.  */
15545             ;
15546           else if (address_p
15547                    && (TREE_CODE (argument) == OFFSET_REF
15548                        || TREE_CODE (argument) == SCOPE_REF))
15549             /* A pointer-to-member.  */
15550             ;
15551           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15552             ;
15553           else
15554             cp_parser_simulate_error (parser);
15555
15556           if (cp_parser_parse_definitely (parser))
15557             {
15558               if (address_p)
15559                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15560                                              tf_warning_or_error);
15561               else
15562                 argument = convert_from_reference (argument);
15563               return argument;
15564             }
15565         }
15566     }
15567   /* If the argument started with "&", there are no other valid
15568      alternatives at this point.  */
15569   if (address_p)
15570     {
15571       cp_parser_error (parser, "invalid non-type template argument");
15572       return error_mark_node;
15573     }
15574
15575  general_expr:
15576   /* If the argument wasn't successfully parsed as a type-id followed
15577      by '>>', the argument can only be a constant expression now.
15578      Otherwise, we try parsing the constant-expression tentatively,
15579      because the argument could really be a type-id.  */
15580   if (maybe_type_id)
15581     cp_parser_parse_tentatively (parser);
15582
15583   if (cxx_dialect <= cxx14)
15584     argument = cp_parser_constant_expression (parser);
15585   else
15586     {
15587       /* With C++17 generalized non-type template arguments we need to handle
15588          lvalue constant expressions, too.  */
15589       argument = cp_parser_assignment_expression (parser);
15590       require_potential_constant_expression (argument);
15591     }
15592
15593   if (!maybe_type_id)
15594     return argument;
15595   if (!cp_parser_next_token_ends_template_argument_p (parser))
15596     cp_parser_error (parser, "expected template-argument");
15597   if (cp_parser_parse_definitely (parser))
15598     return argument;
15599   /* We did our best to parse the argument as a non type-id, but that
15600      was the only alternative that matched (albeit with a '>' after
15601      it). We can assume it's just a typo from the user, and a
15602      diagnostic will then be issued.  */
15603   return cp_parser_template_type_arg (parser);
15604 }
15605
15606 /* Parse an explicit-instantiation.
15607
15608    explicit-instantiation:
15609      template declaration
15610
15611    Although the standard says `declaration', what it really means is:
15612
15613    explicit-instantiation:
15614      template decl-specifier-seq [opt] declarator [opt] ;
15615
15616    Things like `template int S<int>::i = 5, int S<double>::j;' are not
15617    supposed to be allowed.  A defect report has been filed about this
15618    issue.
15619
15620    GNU Extension:
15621
15622    explicit-instantiation:
15623      storage-class-specifier template
15624        decl-specifier-seq [opt] declarator [opt] ;
15625      function-specifier template
15626        decl-specifier-seq [opt] declarator [opt] ;  */
15627
15628 static void
15629 cp_parser_explicit_instantiation (cp_parser* parser)
15630 {
15631   int declares_class_or_enum;
15632   cp_decl_specifier_seq decl_specifiers;
15633   tree extension_specifier = NULL_TREE;
15634
15635   timevar_push (TV_TEMPLATE_INST);
15636
15637   /* Look for an (optional) storage-class-specifier or
15638      function-specifier.  */
15639   if (cp_parser_allow_gnu_extensions_p (parser))
15640     {
15641       extension_specifier
15642         = cp_parser_storage_class_specifier_opt (parser);
15643       if (!extension_specifier)
15644         extension_specifier
15645           = cp_parser_function_specifier_opt (parser,
15646                                               /*decl_specs=*/NULL);
15647     }
15648
15649   /* Look for the `template' keyword.  */
15650   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15651   /* Let the front end know that we are processing an explicit
15652      instantiation.  */
15653   begin_explicit_instantiation ();
15654   /* [temp.explicit] says that we are supposed to ignore access
15655      control while processing explicit instantiation directives.  */
15656   push_deferring_access_checks (dk_no_check);
15657   /* Parse a decl-specifier-seq.  */
15658   cp_parser_decl_specifier_seq (parser,
15659                                 CP_PARSER_FLAGS_OPTIONAL,
15660                                 &decl_specifiers,
15661                                 &declares_class_or_enum);
15662   /* If there was exactly one decl-specifier, and it declared a class,
15663      and there's no declarator, then we have an explicit type
15664      instantiation.  */
15665   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15666     {
15667       tree type;
15668
15669       type = check_tag_decl (&decl_specifiers,
15670                              /*explicit_type_instantiation_p=*/true);
15671       /* Turn access control back on for names used during
15672          template instantiation.  */
15673       pop_deferring_access_checks ();
15674       if (type)
15675         do_type_instantiation (type, extension_specifier,
15676                                /*complain=*/tf_error);
15677     }
15678   else
15679     {
15680       cp_declarator *declarator;
15681       tree decl;
15682
15683       /* Parse the declarator.  */
15684       declarator
15685         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15686                                 /*ctor_dtor_or_conv_p=*/NULL,
15687                                 /*parenthesized_p=*/NULL,
15688                                 /*member_p=*/false,
15689                                 /*friend_p=*/false);
15690       if (declares_class_or_enum & 2)
15691         cp_parser_check_for_definition_in_return_type (declarator,
15692                                                        decl_specifiers.type,
15693                                                        decl_specifiers.locations[ds_type_spec]);
15694       if (declarator != cp_error_declarator)
15695         {
15696           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15697             permerror (decl_specifiers.locations[ds_inline],
15698                        "explicit instantiation shall not use"
15699                        " %<inline%> specifier");
15700           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15701             permerror (decl_specifiers.locations[ds_constexpr],
15702                        "explicit instantiation shall not use"
15703                        " %<constexpr%> specifier");
15704
15705           decl = grokdeclarator (declarator, &decl_specifiers,
15706                                  NORMAL, 0, &decl_specifiers.attributes);
15707           /* Turn access control back on for names used during
15708              template instantiation.  */
15709           pop_deferring_access_checks ();
15710           /* Do the explicit instantiation.  */
15711           do_decl_instantiation (decl, extension_specifier);
15712         }
15713       else
15714         {
15715           pop_deferring_access_checks ();
15716           /* Skip the body of the explicit instantiation.  */
15717           cp_parser_skip_to_end_of_statement (parser);
15718         }
15719     }
15720   /* We're done with the instantiation.  */
15721   end_explicit_instantiation ();
15722
15723   cp_parser_consume_semicolon_at_end_of_statement (parser);
15724
15725   timevar_pop (TV_TEMPLATE_INST);
15726 }
15727
15728 /* Parse an explicit-specialization.
15729
15730    explicit-specialization:
15731      template < > declaration
15732
15733    Although the standard says `declaration', what it really means is:
15734
15735    explicit-specialization:
15736      template <> decl-specifier [opt] init-declarator [opt] ;
15737      template <> function-definition
15738      template <> explicit-specialization
15739      template <> template-declaration  */
15740
15741 static void
15742 cp_parser_explicit_specialization (cp_parser* parser)
15743 {
15744   bool need_lang_pop;
15745   cp_token *token = cp_lexer_peek_token (parser->lexer);
15746
15747   /* Look for the `template' keyword.  */
15748   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15749   /* Look for the `<'.  */
15750   cp_parser_require (parser, CPP_LESS, RT_LESS);
15751   /* Look for the `>'.  */
15752   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15753   /* We have processed another parameter list.  */
15754   ++parser->num_template_parameter_lists;
15755   /* [temp]
15756
15757      A template ... explicit specialization ... shall not have C
15758      linkage.  */
15759   if (current_lang_name == lang_name_c)
15760     {
15761       error_at (token->location, "template specialization with C linkage");
15762       /* Give it C++ linkage to avoid confusing other parts of the
15763          front end.  */
15764       push_lang_context (lang_name_cplusplus);
15765       need_lang_pop = true;
15766     }
15767   else
15768     need_lang_pop = false;
15769   /* Let the front end know that we are beginning a specialization.  */
15770   if (!begin_specialization ())
15771     {
15772       end_specialization ();
15773       return;
15774     }
15775
15776   /* If the next keyword is `template', we need to figure out whether
15777      or not we're looking a template-declaration.  */
15778   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15779     {
15780       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15781           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15782         cp_parser_template_declaration_after_export (parser,
15783                                                      /*member_p=*/false);
15784       else
15785         cp_parser_explicit_specialization (parser);
15786     }
15787   else
15788     /* Parse the dependent declaration.  */
15789     cp_parser_single_declaration (parser,
15790                                   /*checks=*/NULL,
15791                                   /*member_p=*/false,
15792                                   /*explicit_specialization_p=*/true,
15793                                   /*friend_p=*/NULL);
15794   /* We're done with the specialization.  */
15795   end_specialization ();
15796   /* For the erroneous case of a template with C linkage, we pushed an
15797      implicit C++ linkage scope; exit that scope now.  */
15798   if (need_lang_pop)
15799     pop_lang_context ();
15800   /* We're done with this parameter list.  */
15801   --parser->num_template_parameter_lists;
15802 }
15803
15804 /* Parse a type-specifier.
15805
15806    type-specifier:
15807      simple-type-specifier
15808      class-specifier
15809      enum-specifier
15810      elaborated-type-specifier
15811      cv-qualifier
15812
15813    GNU Extension:
15814
15815    type-specifier:
15816      __complex__
15817
15818    Returns a representation of the type-specifier.  For a
15819    class-specifier, enum-specifier, or elaborated-type-specifier, a
15820    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15821
15822    The parser flags FLAGS is used to control type-specifier parsing.
15823
15824    If IS_DECLARATION is TRUE, then this type-specifier is appearing
15825    in a decl-specifier-seq.
15826
15827    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15828    class-specifier, enum-specifier, or elaborated-type-specifier, then
15829    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
15830    if a type is declared; 2 if it is defined.  Otherwise, it is set to
15831    zero.
15832
15833    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15834    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
15835    is set to FALSE.  */
15836
15837 static tree
15838 cp_parser_type_specifier (cp_parser* parser,
15839                           cp_parser_flags flags,
15840                           cp_decl_specifier_seq *decl_specs,
15841                           bool is_declaration,
15842                           int* declares_class_or_enum,
15843                           bool* is_cv_qualifier)
15844 {
15845   tree type_spec = NULL_TREE;
15846   cp_token *token;
15847   enum rid keyword;
15848   cp_decl_spec ds = ds_last;
15849
15850   /* Assume this type-specifier does not declare a new type.  */
15851   if (declares_class_or_enum)
15852     *declares_class_or_enum = 0;
15853   /* And that it does not specify a cv-qualifier.  */
15854   if (is_cv_qualifier)
15855     *is_cv_qualifier = false;
15856   /* Peek at the next token.  */
15857   token = cp_lexer_peek_token (parser->lexer);
15858
15859   /* If we're looking at a keyword, we can use that to guide the
15860      production we choose.  */
15861   keyword = token->keyword;
15862   switch (keyword)
15863     {
15864     case RID_ENUM:
15865       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15866         goto elaborated_type_specifier;
15867
15868       /* Look for the enum-specifier.  */
15869       type_spec = cp_parser_enum_specifier (parser);
15870       /* If that worked, we're done.  */
15871       if (type_spec)
15872         {
15873           if (declares_class_or_enum)
15874             *declares_class_or_enum = 2;
15875           if (decl_specs)
15876             cp_parser_set_decl_spec_type (decl_specs,
15877                                           type_spec,
15878                                           token,
15879                                           /*type_definition_p=*/true);
15880           return type_spec;
15881         }
15882       else
15883         goto elaborated_type_specifier;
15884
15885       /* Any of these indicate either a class-specifier, or an
15886          elaborated-type-specifier.  */
15887     case RID_CLASS:
15888     case RID_STRUCT:
15889     case RID_UNION:
15890       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15891         goto elaborated_type_specifier;
15892
15893       /* Parse tentatively so that we can back up if we don't find a
15894          class-specifier.  */
15895       cp_parser_parse_tentatively (parser);
15896       /* Look for the class-specifier.  */
15897       type_spec = cp_parser_class_specifier (parser);
15898       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15899       /* If that worked, we're done.  */
15900       if (cp_parser_parse_definitely (parser))
15901         {
15902           if (declares_class_or_enum)
15903             *declares_class_or_enum = 2;
15904           if (decl_specs)
15905             cp_parser_set_decl_spec_type (decl_specs,
15906                                           type_spec,
15907                                           token,
15908                                           /*type_definition_p=*/true);
15909           return type_spec;
15910         }
15911
15912       /* Fall through.  */
15913     elaborated_type_specifier:
15914       /* We're declaring (not defining) a class or enum.  */
15915       if (declares_class_or_enum)
15916         *declares_class_or_enum = 1;
15917
15918       /* Fall through.  */
15919     case RID_TYPENAME:
15920       /* Look for an elaborated-type-specifier.  */
15921       type_spec
15922         = (cp_parser_elaborated_type_specifier
15923            (parser,
15924             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15925             is_declaration));
15926       if (decl_specs)
15927         cp_parser_set_decl_spec_type (decl_specs,
15928                                       type_spec,
15929                                       token,
15930                                       /*type_definition_p=*/false);
15931       return type_spec;
15932
15933     case RID_CONST:
15934       ds = ds_const;
15935       if (is_cv_qualifier)
15936         *is_cv_qualifier = true;
15937       break;
15938
15939     case RID_VOLATILE:
15940       ds = ds_volatile;
15941       if (is_cv_qualifier)
15942         *is_cv_qualifier = true;
15943       break;
15944
15945     case RID_RESTRICT:
15946       ds = ds_restrict;
15947       if (is_cv_qualifier)
15948         *is_cv_qualifier = true;
15949       break;
15950
15951     case RID_COMPLEX:
15952       /* The `__complex__' keyword is a GNU extension.  */
15953       ds = ds_complex;
15954       break;
15955
15956     default:
15957       break;
15958     }
15959
15960   /* Handle simple keywords.  */
15961   if (ds != ds_last)
15962     {
15963       if (decl_specs)
15964         {
15965           set_and_check_decl_spec_loc (decl_specs, ds, token);
15966           decl_specs->any_specifiers_p = true;
15967         }
15968       return cp_lexer_consume_token (parser->lexer)->u.value;
15969     }
15970
15971   /* If we do not already have a type-specifier, assume we are looking
15972      at a simple-type-specifier.  */
15973   type_spec = cp_parser_simple_type_specifier (parser,
15974                                                decl_specs,
15975                                                flags);
15976
15977   /* If we didn't find a type-specifier, and a type-specifier was not
15978      optional in this context, issue an error message.  */
15979   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15980     {
15981       cp_parser_error (parser, "expected type specifier");
15982       return error_mark_node;
15983     }
15984
15985   return type_spec;
15986 }
15987
15988 /* Parse a simple-type-specifier.
15989
15990    simple-type-specifier:
15991      :: [opt] nested-name-specifier [opt] type-name
15992      :: [opt] nested-name-specifier template template-id
15993      char
15994      wchar_t
15995      bool
15996      short
15997      int
15998      long
15999      signed
16000      unsigned
16001      float
16002      double
16003      void
16004
16005    C++0x Extension:
16006
16007    simple-type-specifier:
16008      auto
16009      decltype ( expression )   
16010      char16_t
16011      char32_t
16012      __underlying_type ( type-id )
16013
16014    GNU Extension:
16015
16016    simple-type-specifier:
16017      __int128
16018      __typeof__ unary-expression
16019      __typeof__ ( type-id )
16020      __typeof__ ( type-id ) { initializer-list , [opt] }
16021
16022    Concepts Extension:
16023
16024    simple-type-specifier:
16025      constrained-type-specifier
16026
16027    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
16028    appropriately updated.  */
16029
16030 static tree
16031 cp_parser_simple_type_specifier (cp_parser* parser,
16032                                  cp_decl_specifier_seq *decl_specs,
16033                                  cp_parser_flags flags)
16034 {
16035   tree type = NULL_TREE;
16036   cp_token *token;
16037   int idx;
16038
16039   /* Peek at the next token.  */
16040   token = cp_lexer_peek_token (parser->lexer);
16041
16042   /* If we're looking at a keyword, things are easy.  */
16043   switch (token->keyword)
16044     {
16045     case RID_CHAR:
16046       if (decl_specs)
16047         decl_specs->explicit_char_p = true;
16048       type = char_type_node;
16049       break;
16050     case RID_CHAR16:
16051       type = char16_type_node;
16052       break;
16053     case RID_CHAR32:
16054       type = char32_type_node;
16055       break;
16056     case RID_WCHAR:
16057       type = wchar_type_node;
16058       break;
16059     case RID_BOOL:
16060       type = boolean_type_node;
16061       break;
16062     case RID_SHORT:
16063       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16064       type = short_integer_type_node;
16065       break;
16066     case RID_INT:
16067       if (decl_specs)
16068         decl_specs->explicit_int_p = true;
16069       type = integer_type_node;
16070       break;
16071     case RID_INT_N_0:
16072     case RID_INT_N_1:
16073     case RID_INT_N_2:
16074     case RID_INT_N_3:
16075       idx = token->keyword - RID_INT_N_0;
16076       if (! int_n_enabled_p [idx])
16077         break;
16078       if (decl_specs)
16079         {
16080           decl_specs->explicit_intN_p = true;
16081           decl_specs->int_n_idx = idx;
16082         }
16083       type = int_n_trees [idx].signed_type;
16084       break;
16085     case RID_LONG:
16086       if (decl_specs)
16087         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16088       type = long_integer_type_node;
16089       break;
16090     case RID_SIGNED:
16091       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16092       type = integer_type_node;
16093       break;
16094     case RID_UNSIGNED:
16095       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16096       type = unsigned_type_node;
16097       break;
16098     case RID_FLOAT:
16099       type = float_type_node;
16100       break;
16101     case RID_DOUBLE:
16102       type = double_type_node;
16103       break;
16104     case RID_VOID:
16105       type = void_type_node;
16106       break;
16107
16108     case RID_AUTO:
16109       maybe_warn_cpp0x (CPP0X_AUTO);
16110       if (parser->auto_is_implicit_function_template_parm_p)
16111         {
16112           /* The 'auto' might be the placeholder return type for a function decl
16113              with trailing return type.  */
16114           bool have_trailing_return_fn_decl = false;
16115
16116           cp_parser_parse_tentatively (parser);
16117           cp_lexer_consume_token (parser->lexer);
16118           while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16119                  && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16120                  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16121                  && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16122             {
16123               if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16124                 {
16125                   cp_lexer_consume_token (parser->lexer);
16126                   cp_parser_skip_to_closing_parenthesis (parser,
16127                                                          /*recovering*/false,
16128                                                          /*or_comma*/false,
16129                                                          /*consume_paren*/true);
16130                   continue;
16131                 }
16132
16133               if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16134                 {
16135                   have_trailing_return_fn_decl = true;
16136                   break;
16137                 }
16138
16139               cp_lexer_consume_token (parser->lexer);
16140             }
16141           cp_parser_abort_tentative_parse (parser);
16142
16143           if (have_trailing_return_fn_decl)
16144             {
16145               type = make_auto ();
16146               break;
16147             }
16148
16149           if (cxx_dialect >= cxx14)
16150             {
16151               type = synthesize_implicit_template_parm (parser, NULL_TREE);
16152               type = TREE_TYPE (type);
16153             }
16154           else
16155             type = error_mark_node;
16156
16157           if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16158             {
16159               if (cxx_dialect < cxx14)
16160                 error_at (token->location,
16161                          "use of %<auto%> in lambda parameter declaration "
16162                          "only available with "
16163                          "-std=c++14 or -std=gnu++14");
16164             }
16165           else if (cxx_dialect < cxx14)
16166             error_at (token->location,
16167                      "use of %<auto%> in parameter declaration "
16168                      "only available with "
16169                      "-std=c++14 or -std=gnu++14");
16170           else if (!flag_concepts)
16171             pedwarn (token->location, OPT_Wpedantic,
16172                      "ISO C++ forbids use of %<auto%> in parameter "
16173                      "declaration");
16174         }
16175       else
16176         type = make_auto ();
16177       break;
16178
16179     case RID_DECLTYPE:
16180       /* Since DR 743, decltype can either be a simple-type-specifier by
16181          itself or begin a nested-name-specifier.  Parsing it will replace
16182          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16183          handling below decide what to do.  */
16184       cp_parser_decltype (parser);
16185       cp_lexer_set_token_position (parser->lexer, token);
16186       break;
16187
16188     case RID_TYPEOF:
16189       /* Consume the `typeof' token.  */
16190       cp_lexer_consume_token (parser->lexer);
16191       /* Parse the operand to `typeof'.  */
16192       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16193       /* If it is not already a TYPE, take its type.  */
16194       if (!TYPE_P (type))
16195         type = finish_typeof (type);
16196
16197       if (decl_specs)
16198         cp_parser_set_decl_spec_type (decl_specs, type,
16199                                       token,
16200                                       /*type_definition_p=*/false);
16201
16202       return type;
16203
16204     case RID_UNDERLYING_TYPE:
16205       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16206       if (decl_specs)
16207         cp_parser_set_decl_spec_type (decl_specs, type,
16208                                       token,
16209                                       /*type_definition_p=*/false);
16210
16211       return type;
16212
16213     case RID_BASES:
16214     case RID_DIRECT_BASES:
16215       type = cp_parser_trait_expr (parser, token->keyword);
16216       if (decl_specs)
16217        cp_parser_set_decl_spec_type (decl_specs, type,
16218                                      token,
16219                                      /*type_definition_p=*/false);
16220       return type;
16221     default:
16222       break;
16223     }
16224
16225   /* If token is an already-parsed decltype not followed by ::,
16226      it's a simple-type-specifier.  */
16227   if (token->type == CPP_DECLTYPE
16228       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16229     {
16230       type = saved_checks_value (token->u.tree_check_value);
16231       if (decl_specs)
16232         {
16233           cp_parser_set_decl_spec_type (decl_specs, type,
16234                                         token,
16235                                         /*type_definition_p=*/false);
16236           /* Remember that we are handling a decltype in order to
16237              implement the resolution of DR 1510 when the argument
16238              isn't instantiation dependent.  */
16239           decl_specs->decltype_p = true;
16240         }
16241       cp_lexer_consume_token (parser->lexer);
16242       return type;
16243     }
16244
16245   /* If the type-specifier was for a built-in type, we're done.  */
16246   if (type)
16247     {
16248       /* Record the type.  */
16249       if (decl_specs
16250           && (token->keyword != RID_SIGNED
16251               && token->keyword != RID_UNSIGNED
16252               && token->keyword != RID_SHORT
16253               && token->keyword != RID_LONG))
16254         cp_parser_set_decl_spec_type (decl_specs,
16255                                       type,
16256                                       token,
16257                                       /*type_definition_p=*/false);
16258       if (decl_specs)
16259         decl_specs->any_specifiers_p = true;
16260
16261       /* Consume the token.  */
16262       cp_lexer_consume_token (parser->lexer);
16263
16264       if (type == error_mark_node)
16265         return error_mark_node;
16266
16267       /* There is no valid C++ program where a non-template type is
16268          followed by a "<".  That usually indicates that the user thought
16269          that the type was a template.  */
16270       cp_parser_check_for_invalid_template_id (parser, type, none_type,
16271                                                token->location);
16272
16273       return TYPE_NAME (type);
16274     }
16275
16276   /* The type-specifier must be a user-defined type.  */
16277   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16278     {
16279       bool qualified_p;
16280       bool global_p;
16281
16282       /* Don't gobble tokens or issue error messages if this is an
16283          optional type-specifier.  */
16284       if (flags & CP_PARSER_FLAGS_OPTIONAL)
16285         cp_parser_parse_tentatively (parser);
16286
16287       /* Look for the optional `::' operator.  */
16288       global_p
16289         = (cp_parser_global_scope_opt (parser,
16290                                        /*current_scope_valid_p=*/false)
16291            != NULL_TREE);
16292       /* Look for the nested-name specifier.  */
16293       qualified_p
16294         = (cp_parser_nested_name_specifier_opt (parser,
16295                                                 /*typename_keyword_p=*/false,
16296                                                 /*check_dependency_p=*/true,
16297                                                 /*type_p=*/false,
16298                                                 /*is_declaration=*/false)
16299            != NULL_TREE);
16300       token = cp_lexer_peek_token (parser->lexer);
16301       /* If we have seen a nested-name-specifier, and the next token
16302          is `template', then we are using the template-id production.  */
16303       if (parser->scope
16304           && cp_parser_optional_template_keyword (parser))
16305         {
16306           /* Look for the template-id.  */
16307           type = cp_parser_template_id (parser,
16308                                         /*template_keyword_p=*/true,
16309                                         /*check_dependency_p=*/true,
16310                                         none_type,
16311                                         /*is_declaration=*/false);
16312           /* If the template-id did not name a type, we are out of
16313              luck.  */
16314           if (TREE_CODE (type) != TYPE_DECL)
16315             {
16316               cp_parser_error (parser, "expected template-id for type");
16317               type = NULL_TREE;
16318             }
16319         }
16320       /* Otherwise, look for a type-name.  */
16321       else
16322         type = cp_parser_type_name (parser);
16323       /* Keep track of all name-lookups performed in class scopes.  */
16324       if (type
16325           && !global_p
16326           && !qualified_p
16327           && TREE_CODE (type) == TYPE_DECL
16328           && identifier_p (DECL_NAME (type)))
16329         maybe_note_name_used_in_class (DECL_NAME (type), type);
16330       /* If it didn't work out, we don't have a TYPE.  */
16331       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16332           && !cp_parser_parse_definitely (parser))
16333         type = NULL_TREE;
16334       if (type && decl_specs)
16335         cp_parser_set_decl_spec_type (decl_specs, type,
16336                                       token,
16337                                       /*type_definition_p=*/false);
16338     }
16339
16340   /* If we didn't get a type-name, issue an error message.  */
16341   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16342     {
16343       cp_parser_error (parser, "expected type-name");
16344       return error_mark_node;
16345     }
16346
16347   if (type && type != error_mark_node)
16348     {
16349       /* See if TYPE is an Objective-C type, and if so, parse and
16350          accept any protocol references following it.  Do this before
16351          the cp_parser_check_for_invalid_template_id() call, because
16352          Objective-C types can be followed by '<...>' which would
16353          enclose protocol names rather than template arguments, and so
16354          everything is fine.  */
16355       if (c_dialect_objc () && !parser->scope
16356           && (objc_is_id (type) || objc_is_class_name (type)))
16357         {
16358           tree protos = cp_parser_objc_protocol_refs_opt (parser);
16359           tree qual_type = objc_get_protocol_qualified_type (type, protos);
16360
16361           /* Clobber the "unqualified" type previously entered into
16362              DECL_SPECS with the new, improved protocol-qualified version.  */
16363           if (decl_specs)
16364             decl_specs->type = qual_type;
16365
16366           return qual_type;
16367         }
16368
16369       /* There is no valid C++ program where a non-template type is
16370          followed by a "<".  That usually indicates that the user
16371          thought that the type was a template.  */
16372       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16373                                                none_type,
16374                                                token->location);
16375     }
16376
16377   return type;
16378 }
16379
16380 /* Parse a type-name.
16381
16382    type-name:
16383      class-name
16384      enum-name
16385      typedef-name
16386      simple-template-id [in c++0x]
16387
16388    enum-name:
16389      identifier
16390
16391    typedef-name:
16392      identifier
16393
16394   Concepts:
16395
16396    type-name:
16397      concept-name
16398      partial-concept-id
16399
16400    concept-name:
16401      identifier
16402
16403    Returns a TYPE_DECL for the type.  */
16404
16405 static tree
16406 cp_parser_type_name (cp_parser* parser)
16407 {
16408   return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16409 }
16410
16411 /* See above. */
16412 static tree
16413 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16414 {
16415   tree type_decl;
16416
16417   /* We can't know yet whether it is a class-name or not.  */
16418   cp_parser_parse_tentatively (parser);
16419   /* Try a class-name.  */
16420   type_decl = cp_parser_class_name (parser,
16421                                     typename_keyword_p,
16422                                     /*template_keyword_p=*/false,
16423                                     none_type,
16424                                     /*check_dependency_p=*/true,
16425                                     /*class_head_p=*/false,
16426                                     /*is_declaration=*/false);
16427   /* If it's not a class-name, keep looking.  */
16428   if (!cp_parser_parse_definitely (parser))
16429     {
16430       if (cxx_dialect < cxx11)
16431         /* It must be a typedef-name or an enum-name.  */
16432         return cp_parser_nonclass_name (parser);
16433
16434       cp_parser_parse_tentatively (parser);
16435       /* It is either a simple-template-id representing an
16436          instantiation of an alias template...  */
16437       type_decl = cp_parser_template_id (parser,
16438                                          /*template_keyword_p=*/false,
16439                                          /*check_dependency_p=*/true,
16440                                          none_type,
16441                                          /*is_declaration=*/false);
16442       /* Note that this must be an instantiation of an alias template
16443          because [temp.names]/6 says:
16444          
16445              A template-id that names an alias template specialization
16446              is a type-name.
16447
16448          Whereas [temp.names]/7 says:
16449          
16450              A simple-template-id that names a class template
16451              specialization is a class-name.
16452
16453          With concepts, this could also be a partial-concept-id that
16454          declares a non-type template parameter. */
16455       if (type_decl != NULL_TREE
16456           && TREE_CODE (type_decl) == TYPE_DECL
16457           && TYPE_DECL_ALIAS_P (type_decl))
16458         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16459       else if (is_constrained_parameter (type_decl))
16460         /* Don't do anything. */ ;
16461       else
16462         cp_parser_simulate_error (parser);
16463
16464       if (!cp_parser_parse_definitely (parser))
16465         /* ... Or a typedef-name or an enum-name.  */
16466         return cp_parser_nonclass_name (parser);
16467     }
16468
16469   return type_decl;
16470 }
16471
16472 /*  Check if DECL and ARGS can form a constrained-type-specifier.
16473     If ARGS is non-null, we try to form a concept check of the
16474     form DECL<?, ARGS> where ? is a wildcard that matches any
16475     kind of template argument. If ARGS is NULL, then we try to
16476     form a concept check of the form DECL<?>. */
16477
16478 static tree
16479 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16480                                             tree decl, tree args)
16481 {
16482   gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16483
16484   /* If we a constrained-type-specifier cannot be deduced. */
16485   if (parser->prevent_constrained_type_specifiers)
16486     return NULL_TREE;
16487
16488   /* A constrained type specifier can only be found in an
16489      overload set or as a reference to a template declaration.
16490
16491      FIXME: This might be masking a bug.  It's possible that
16492      that the deduction below is causing template specializations
16493      to be formed with the wildcard as an argument.  */
16494   if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16495     return NULL_TREE;
16496
16497   /* Try to build a call expression that evaluates the
16498      concept. This can fail if the overload set refers
16499      only to non-templates. */
16500   tree placeholder = build_nt (WILDCARD_DECL);
16501   tree check = build_concept_check (decl, placeholder, args);
16502   if (check == error_mark_node)
16503     return NULL_TREE;
16504
16505   /* Deduce the checked constraint and the prototype parameter.
16506
16507      FIXME: In certain cases, failure to deduce should be a
16508      diagnosable error.  */
16509   tree conc;
16510   tree proto;
16511   if (!deduce_constrained_parameter (check, conc, proto))
16512     return NULL_TREE;
16513
16514   /* In template parameter scope, this results in a constrained
16515      parameter. Return a descriptor of that parm. */
16516   if (processing_template_parmlist)
16517     return build_constrained_parameter (conc, proto, args);
16518
16519   /* In a parameter-declaration-clause, constrained-type
16520      specifiers result in invented template parameters.  */
16521   if (parser->auto_is_implicit_function_template_parm_p)
16522     {
16523       tree x = build_constrained_parameter (conc, proto, args);
16524       return synthesize_implicit_template_parm (parser, x);
16525     }
16526   else
16527     {
16528      /* Otherwise, we're in a context where the constrained
16529         type name is deduced and the constraint applies
16530         after deduction. */
16531       return make_constrained_auto (conc, args);
16532     }
16533
16534   return NULL_TREE;
16535 }
16536
16537 /* If DECL refers to a concept, return a TYPE_DECL representing
16538    the result of using the constrained type specifier in the
16539    current context.  DECL refers to a concept if
16540
16541   - it is an overload set containing a function concept taking a single
16542     type argument, or
16543
16544   - it is a variable concept taking a single type argument.  */
16545
16546 static tree
16547 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16548 {
16549   if (flag_concepts
16550       && (TREE_CODE (decl) == OVERLOAD
16551           || BASELINK_P (decl)
16552           || variable_concept_p (decl)))
16553     return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16554   else
16555     return NULL_TREE;
16556 }
16557
16558 /* Check if DECL and ARGS form a partial-concept-id.  If so,
16559    assign ID to the resulting constrained placeholder.
16560
16561    Returns true if the partial-concept-id designates a placeholder
16562    and false otherwise. Note that *id is set to NULL_TREE in
16563    this case. */
16564
16565 static tree
16566 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16567 {
16568   return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16569 }
16570
16571 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16572    or a concept-name.
16573
16574    enum-name:
16575      identifier
16576
16577    typedef-name:
16578      identifier
16579
16580    concept-name:
16581      identifier
16582
16583    Returns a TYPE_DECL for the type.  */
16584
16585 static tree
16586 cp_parser_nonclass_name (cp_parser* parser)
16587 {
16588   tree type_decl;
16589   tree identifier;
16590
16591   cp_token *token = cp_lexer_peek_token (parser->lexer);
16592   identifier = cp_parser_identifier (parser);
16593   if (identifier == error_mark_node)
16594     return error_mark_node;
16595
16596   /* Look up the type-name.  */
16597   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16598
16599   type_decl = strip_using_decl (type_decl);
16600   
16601   /* If we found an overload set, then it may refer to a concept-name. */
16602   if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16603     type_decl = decl;
16604
16605   if (TREE_CODE (type_decl) != TYPE_DECL
16606       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16607     {
16608       /* See if this is an Objective-C type.  */
16609       tree protos = cp_parser_objc_protocol_refs_opt (parser);
16610       tree type = objc_get_protocol_qualified_type (identifier, protos);
16611       if (type)
16612         type_decl = TYPE_NAME (type);
16613     }
16614
16615   /* Issue an error if we did not find a type-name.  */
16616   if (TREE_CODE (type_decl) != TYPE_DECL
16617       /* In Objective-C, we have the complication that class names are
16618          normally type names and start declarations (eg, the
16619          "NSObject" in "NSObject *object;"), but can be used in an
16620          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16621          is an expression.  So, a classname followed by a dot is not a
16622          valid type-name.  */
16623       || (objc_is_class_name (TREE_TYPE (type_decl))
16624           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16625     {
16626       if (!cp_parser_simulate_error (parser))
16627         cp_parser_name_lookup_error (parser, identifier, type_decl,
16628                                      NLE_TYPE, token->location);
16629       return error_mark_node;
16630     }
16631   /* Remember that the name was used in the definition of the
16632      current class so that we can check later to see if the
16633      meaning would have been different after the class was
16634      entirely defined.  */
16635   else if (type_decl != error_mark_node
16636            && !parser->scope)
16637     maybe_note_name_used_in_class (identifier, type_decl);
16638   
16639   return type_decl;
16640 }
16641
16642 /* Parse an elaborated-type-specifier.  Note that the grammar given
16643    here incorporates the resolution to DR68.
16644
16645    elaborated-type-specifier:
16646      class-key :: [opt] nested-name-specifier [opt] identifier
16647      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16648      enum-key :: [opt] nested-name-specifier [opt] identifier
16649      typename :: [opt] nested-name-specifier identifier
16650      typename :: [opt] nested-name-specifier template [opt]
16651        template-id
16652
16653    GNU extension:
16654
16655    elaborated-type-specifier:
16656      class-key attributes :: [opt] nested-name-specifier [opt] identifier
16657      class-key attributes :: [opt] nested-name-specifier [opt]
16658                template [opt] template-id
16659      enum attributes :: [opt] nested-name-specifier [opt] identifier
16660
16661    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16662    declared `friend'.  If IS_DECLARATION is TRUE, then this
16663    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16664    something is being declared.
16665
16666    Returns the TYPE specified.  */
16667
16668 static tree
16669 cp_parser_elaborated_type_specifier (cp_parser* parser,
16670                                      bool is_friend,
16671                                      bool is_declaration)
16672 {
16673   enum tag_types tag_type;
16674   tree identifier;
16675   tree type = NULL_TREE;
16676   tree attributes = NULL_TREE;
16677   tree globalscope;
16678   cp_token *token = NULL;
16679
16680   /* See if we're looking at the `enum' keyword.  */
16681   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16682     {
16683       /* Consume the `enum' token.  */
16684       cp_lexer_consume_token (parser->lexer);
16685       /* Remember that it's an enumeration type.  */
16686       tag_type = enum_type;
16687       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16688          enums) is used here.  */
16689       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16690           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16691         {
16692             pedwarn (input_location, 0, "elaborated-type-specifier "
16693                       "for a scoped enum must not use the %<%D%> keyword",
16694                       cp_lexer_peek_token (parser->lexer)->u.value);
16695           /* Consume the `struct' or `class' and parse it anyway.  */
16696           cp_lexer_consume_token (parser->lexer);
16697         }
16698       /* Parse the attributes.  */
16699       attributes = cp_parser_attributes_opt (parser);
16700     }
16701   /* Or, it might be `typename'.  */
16702   else if (cp_lexer_next_token_is_keyword (parser->lexer,
16703                                            RID_TYPENAME))
16704     {
16705       /* Consume the `typename' token.  */
16706       cp_lexer_consume_token (parser->lexer);
16707       /* Remember that it's a `typename' type.  */
16708       tag_type = typename_type;
16709     }
16710   /* Otherwise it must be a class-key.  */
16711   else
16712     {
16713       tag_type = cp_parser_class_key (parser);
16714       if (tag_type == none_type)
16715         return error_mark_node;
16716       /* Parse the attributes.  */
16717       attributes = cp_parser_attributes_opt (parser);
16718     }
16719
16720   /* Look for the `::' operator.  */
16721   globalscope =  cp_parser_global_scope_opt (parser,
16722                                              /*current_scope_valid_p=*/false);
16723   /* Look for the nested-name-specifier.  */
16724   if (tag_type == typename_type && !globalscope)
16725     {
16726       if (!cp_parser_nested_name_specifier (parser,
16727                                            /*typename_keyword_p=*/true,
16728                                            /*check_dependency_p=*/true,
16729                                            /*type_p=*/true,
16730                                             is_declaration))
16731         return error_mark_node;
16732     }
16733   else
16734     /* Even though `typename' is not present, the proposed resolution
16735        to Core Issue 180 says that in `class A<T>::B', `B' should be
16736        considered a type-name, even if `A<T>' is dependent.  */
16737     cp_parser_nested_name_specifier_opt (parser,
16738                                          /*typename_keyword_p=*/true,
16739                                          /*check_dependency_p=*/true,
16740                                          /*type_p=*/true,
16741                                          is_declaration);
16742  /* For everything but enumeration types, consider a template-id.
16743     For an enumeration type, consider only a plain identifier.  */
16744   if (tag_type != enum_type)
16745     {
16746       bool template_p = false;
16747       tree decl;
16748
16749       /* Allow the `template' keyword.  */
16750       template_p = cp_parser_optional_template_keyword (parser);
16751       /* If we didn't see `template', we don't know if there's a
16752          template-id or not.  */
16753       if (!template_p)
16754         cp_parser_parse_tentatively (parser);
16755       /* Parse the template-id.  */
16756       token = cp_lexer_peek_token (parser->lexer);
16757       decl = cp_parser_template_id (parser, template_p,
16758                                     /*check_dependency_p=*/true,
16759                                     tag_type,
16760                                     is_declaration);
16761       /* If we didn't find a template-id, look for an ordinary
16762          identifier.  */
16763       if (!template_p && !cp_parser_parse_definitely (parser))
16764         ;
16765       /* We can get here when cp_parser_template_id, called by
16766          cp_parser_class_name with tag_type == none_type, succeeds
16767          and caches a BASELINK.  Then, when called again here,
16768          instead of failing and returning an error_mark_node
16769          returns it (see template/typename17.C in C++11).
16770          ??? Could we diagnose this earlier?  */
16771       else if (tag_type == typename_type && BASELINK_P (decl))
16772         {
16773           cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16774           type = error_mark_node;
16775         }
16776       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16777          in effect, then we must assume that, upon instantiation, the
16778          template will correspond to a class.  */
16779       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16780                && tag_type == typename_type)
16781         type = make_typename_type (parser->scope, decl,
16782                                    typename_type,
16783                                    /*complain=*/tf_error);
16784       /* If the `typename' keyword is in effect and DECL is not a type
16785          decl, then type is non existent.   */
16786       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16787         ; 
16788       else if (TREE_CODE (decl) == TYPE_DECL)
16789         type = check_elaborated_type_specifier (tag_type, decl,
16790                                                 /*allow_template_p=*/true);
16791       else if (decl == error_mark_node)
16792         type = error_mark_node; 
16793     }
16794
16795   if (!type)
16796     {
16797       token = cp_lexer_peek_token (parser->lexer);
16798       identifier = cp_parser_identifier (parser);
16799
16800       if (identifier == error_mark_node)
16801         {
16802           parser->scope = NULL_TREE;
16803           return error_mark_node;
16804         }
16805
16806       /* For a `typename', we needn't call xref_tag.  */
16807       if (tag_type == typename_type
16808           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16809         return cp_parser_make_typename_type (parser, identifier,
16810                                              token->location);
16811
16812       /* Template parameter lists apply only if we are not within a
16813          function parameter list.  */
16814       bool template_parm_lists_apply
16815           = parser->num_template_parameter_lists;
16816       if (template_parm_lists_apply)
16817         for (cp_binding_level *s = current_binding_level;
16818              s && s->kind != sk_template_parms;
16819              s = s->level_chain)
16820           if (s->kind == sk_function_parms)
16821             template_parm_lists_apply = false;
16822
16823       /* Look up a qualified name in the usual way.  */
16824       if (parser->scope)
16825         {
16826           tree decl;
16827           tree ambiguous_decls;
16828
16829           decl = cp_parser_lookup_name (parser, identifier,
16830                                         tag_type,
16831                                         /*is_template=*/false,
16832                                         /*is_namespace=*/false,
16833                                         /*check_dependency=*/true,
16834                                         &ambiguous_decls,
16835                                         token->location);
16836
16837           /* If the lookup was ambiguous, an error will already have been
16838              issued.  */
16839           if (ambiguous_decls)
16840             return error_mark_node;
16841
16842           /* If we are parsing friend declaration, DECL may be a
16843              TEMPLATE_DECL tree node here.  However, we need to check
16844              whether this TEMPLATE_DECL results in valid code.  Consider
16845              the following example:
16846
16847                namespace N {
16848                  template <class T> class C {};
16849                }
16850                class X {
16851                  template <class T> friend class N::C; // #1, valid code
16852                };
16853                template <class T> class Y {
16854                  friend class N::C;                    // #2, invalid code
16855                };
16856
16857              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16858              name lookup of `N::C'.  We see that friend declaration must
16859              be template for the code to be valid.  Note that
16860              processing_template_decl does not work here since it is
16861              always 1 for the above two cases.  */
16862
16863           decl = (cp_parser_maybe_treat_template_as_class
16864                   (decl, /*tag_name_p=*/is_friend
16865                          && template_parm_lists_apply));
16866
16867           if (TREE_CODE (decl) != TYPE_DECL)
16868             {
16869               cp_parser_diagnose_invalid_type_name (parser,
16870                                                     identifier,
16871                                                     token->location);
16872               return error_mark_node;
16873             }
16874
16875           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16876             {
16877               bool allow_template = (template_parm_lists_apply
16878                                      || DECL_SELF_REFERENCE_P (decl));
16879               type = check_elaborated_type_specifier (tag_type, decl,
16880                                                       allow_template);
16881
16882               if (type == error_mark_node)
16883                 return error_mark_node;
16884             }
16885
16886           /* Forward declarations of nested types, such as
16887
16888                class C1::C2;
16889                class C1::C2::C3;
16890
16891              are invalid unless all components preceding the final '::'
16892              are complete.  If all enclosing types are complete, these
16893              declarations become merely pointless.
16894
16895              Invalid forward declarations of nested types are errors
16896              caught elsewhere in parsing.  Those that are pointless arrive
16897              here.  */
16898
16899           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16900               && !is_friend && !processing_explicit_instantiation)
16901             warning (0, "declaration %qD does not declare anything", decl);
16902
16903           type = TREE_TYPE (decl);
16904         }
16905       else
16906         {
16907           /* An elaborated-type-specifier sometimes introduces a new type and
16908              sometimes names an existing type.  Normally, the rule is that it
16909              introduces a new type only if there is not an existing type of
16910              the same name already in scope.  For example, given:
16911
16912                struct S {};
16913                void f() { struct S s; }
16914
16915              the `struct S' in the body of `f' is the same `struct S' as in
16916              the global scope; the existing definition is used.  However, if
16917              there were no global declaration, this would introduce a new
16918              local class named `S'.
16919
16920              An exception to this rule applies to the following code:
16921
16922                namespace N { struct S; }
16923
16924              Here, the elaborated-type-specifier names a new type
16925              unconditionally; even if there is already an `S' in the
16926              containing scope this declaration names a new type.
16927              This exception only applies if the elaborated-type-specifier
16928              forms the complete declaration:
16929
16930                [class.name]
16931
16932                A declaration consisting solely of `class-key identifier ;' is
16933                either a redeclaration of the name in the current scope or a
16934                forward declaration of the identifier as a class name.  It
16935                introduces the name into the current scope.
16936
16937              We are in this situation precisely when the next token is a `;'.
16938
16939              An exception to the exception is that a `friend' declaration does
16940              *not* name a new type; i.e., given:
16941
16942                struct S { friend struct T; };
16943
16944              `T' is not a new type in the scope of `S'.
16945
16946              Also, `new struct S' or `sizeof (struct S)' never results in the
16947              definition of a new type; a new type can only be declared in a
16948              declaration context.  */
16949
16950           tag_scope ts;
16951           bool template_p;
16952
16953           if (is_friend)
16954             /* Friends have special name lookup rules.  */
16955             ts = ts_within_enclosing_non_class;
16956           else if (is_declaration
16957                    && cp_lexer_next_token_is (parser->lexer,
16958                                               CPP_SEMICOLON))
16959             /* This is a `class-key identifier ;' */
16960             ts = ts_current;
16961           else
16962             ts = ts_global;
16963
16964           template_p =
16965             (template_parm_lists_apply
16966              && (cp_parser_next_token_starts_class_definition_p (parser)
16967                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16968           /* An unqualified name was used to reference this type, so
16969              there were no qualifying templates.  */
16970           if (template_parm_lists_apply
16971               && !cp_parser_check_template_parameters (parser,
16972                                                        /*num_templates=*/0,
16973                                                        token->location,
16974                                                        /*declarator=*/NULL))
16975             return error_mark_node;
16976           type = xref_tag (tag_type, identifier, ts, template_p);
16977         }
16978     }
16979
16980   if (type == error_mark_node)
16981     return error_mark_node;
16982
16983   /* Allow attributes on forward declarations of classes.  */
16984   if (attributes)
16985     {
16986       if (TREE_CODE (type) == TYPENAME_TYPE)
16987         warning (OPT_Wattributes,
16988                  "attributes ignored on uninstantiated type");
16989       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16990                && ! processing_explicit_instantiation)
16991         warning (OPT_Wattributes,
16992                  "attributes ignored on template instantiation");
16993       else if (is_declaration && cp_parser_declares_only_class_p (parser))
16994         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16995       else
16996         warning (OPT_Wattributes,
16997                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16998     }
16999
17000   if (tag_type != enum_type)
17001     {
17002       /* Indicate whether this class was declared as a `class' or as a
17003          `struct'.  */
17004       if (CLASS_TYPE_P (type))
17005         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17006       cp_parser_check_class_key (tag_type, type);
17007     }
17008
17009   /* A "<" cannot follow an elaborated type specifier.  If that
17010      happens, the user was probably trying to form a template-id.  */
17011   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17012                                            token->location);
17013
17014   return type;
17015 }
17016
17017 /* Parse an enum-specifier.
17018
17019    enum-specifier:
17020      enum-head { enumerator-list [opt] }
17021      enum-head { enumerator-list , } [C++0x]
17022
17023    enum-head:
17024      enum-key identifier [opt] enum-base [opt]
17025      enum-key nested-name-specifier identifier enum-base [opt]
17026
17027    enum-key:
17028      enum
17029      enum class   [C++0x]
17030      enum struct  [C++0x]
17031
17032    enum-base:   [C++0x]
17033      : type-specifier-seq
17034
17035    opaque-enum-specifier:
17036      enum-key identifier enum-base [opt] ;
17037
17038    GNU Extensions:
17039      enum-key attributes[opt] identifier [opt] enum-base [opt] 
17040        { enumerator-list [opt] }attributes[opt]
17041      enum-key attributes[opt] identifier [opt] enum-base [opt]
17042        { enumerator-list, }attributes[opt] [C++0x]
17043
17044    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17045    if the token stream isn't an enum-specifier after all.  */
17046
17047 static tree
17048 cp_parser_enum_specifier (cp_parser* parser)
17049 {
17050   tree identifier;
17051   tree type = NULL_TREE;
17052   tree prev_scope;
17053   tree nested_name_specifier = NULL_TREE;
17054   tree attributes;
17055   bool scoped_enum_p = false;
17056   bool has_underlying_type = false;
17057   bool nested_being_defined = false;
17058   bool new_value_list = false;
17059   bool is_new_type = false;
17060   bool is_anonymous = false;
17061   tree underlying_type = NULL_TREE;
17062   cp_token *type_start_token = NULL;
17063   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17064
17065   parser->colon_corrects_to_scope_p = false;
17066
17067   /* Parse tentatively so that we can back up if we don't find a
17068      enum-specifier.  */
17069   cp_parser_parse_tentatively (parser);
17070
17071   /* Caller guarantees that the current token is 'enum', an identifier
17072      possibly follows, and the token after that is an opening brace.
17073      If we don't have an identifier, fabricate an anonymous name for
17074      the enumeration being defined.  */
17075   cp_lexer_consume_token (parser->lexer);
17076
17077   /* Parse the "class" or "struct", which indicates a scoped
17078      enumeration type in C++0x.  */
17079   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17080       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17081     {
17082       if (cxx_dialect < cxx11)
17083         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17084
17085       /* Consume the `struct' or `class' token.  */
17086       cp_lexer_consume_token (parser->lexer);
17087
17088       scoped_enum_p = true;
17089     }
17090
17091   attributes = cp_parser_attributes_opt (parser);
17092
17093   /* Clear the qualification.  */
17094   parser->scope = NULL_TREE;
17095   parser->qualifying_scope = NULL_TREE;
17096   parser->object_scope = NULL_TREE;
17097
17098   /* Figure out in what scope the declaration is being placed.  */
17099   prev_scope = current_scope ();
17100
17101   type_start_token = cp_lexer_peek_token (parser->lexer);
17102
17103   push_deferring_access_checks (dk_no_check);
17104   nested_name_specifier
17105       = cp_parser_nested_name_specifier_opt (parser,
17106                                              /*typename_keyword_p=*/true,
17107                                              /*check_dependency_p=*/false,
17108                                              /*type_p=*/false,
17109                                              /*is_declaration=*/false);
17110
17111   if (nested_name_specifier)
17112     {
17113       tree name;
17114
17115       identifier = cp_parser_identifier (parser);
17116       name =  cp_parser_lookup_name (parser, identifier,
17117                                      enum_type,
17118                                      /*is_template=*/false,
17119                                      /*is_namespace=*/false,
17120                                      /*check_dependency=*/true,
17121                                      /*ambiguous_decls=*/NULL,
17122                                      input_location);
17123       if (name && name != error_mark_node)
17124         {
17125           type = TREE_TYPE (name);
17126           if (TREE_CODE (type) == TYPENAME_TYPE)
17127             {
17128               /* Are template enums allowed in ISO? */
17129               if (template_parm_scope_p ())
17130                 pedwarn (type_start_token->location, OPT_Wpedantic,
17131                          "%qD is an enumeration template", name);
17132               /* ignore a typename reference, for it will be solved by name
17133                  in start_enum.  */
17134               type = NULL_TREE;
17135             }
17136         }
17137       else if (nested_name_specifier == error_mark_node)
17138         /* We already issued an error.  */;
17139       else
17140         {
17141           error_at (type_start_token->location,
17142                     "%qD does not name an enumeration in %qT",
17143                     identifier, nested_name_specifier);
17144           nested_name_specifier = error_mark_node;
17145         }
17146     }
17147   else
17148     {
17149       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17150         identifier = cp_parser_identifier (parser);
17151       else
17152         {
17153           identifier = make_anon_name ();
17154           is_anonymous = true;
17155           if (scoped_enum_p)
17156             error_at (type_start_token->location,
17157                       "anonymous scoped enum is not allowed");
17158         }
17159     }
17160   pop_deferring_access_checks ();
17161
17162   /* Check for the `:' that denotes a specified underlying type in C++0x.
17163      Note that a ':' could also indicate a bitfield width, however.  */
17164   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17165     {
17166       cp_decl_specifier_seq type_specifiers;
17167
17168       /* Consume the `:'.  */
17169       cp_lexer_consume_token (parser->lexer);
17170
17171       /* Parse the type-specifier-seq.  */
17172       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17173                                     /*is_trailing_return=*/false,
17174                                     &type_specifiers);
17175
17176       /* At this point this is surely not elaborated type specifier.  */
17177       if (!cp_parser_parse_definitely (parser))
17178         return NULL_TREE;
17179
17180       if (cxx_dialect < cxx11)
17181         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17182
17183       has_underlying_type = true;
17184
17185       /* If that didn't work, stop.  */
17186       if (type_specifiers.type != error_mark_node)
17187         {
17188           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17189                                             /*initialized=*/0, NULL);
17190           if (underlying_type == error_mark_node
17191               || check_for_bare_parameter_packs (underlying_type))
17192             underlying_type = NULL_TREE;
17193         }
17194     }
17195
17196   /* Look for the `{' but don't consume it yet.  */
17197   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17198     {
17199       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17200         {
17201           cp_parser_error (parser, "expected %<{%>");
17202           if (has_underlying_type)
17203             {
17204               type = NULL_TREE;
17205               goto out;
17206             }
17207         }
17208       /* An opaque-enum-specifier must have a ';' here.  */
17209       if ((scoped_enum_p || underlying_type)
17210           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17211         {
17212           cp_parser_error (parser, "expected %<;%> or %<{%>");
17213           if (has_underlying_type)
17214             {
17215               type = NULL_TREE;
17216               goto out;
17217             }
17218         }
17219     }
17220
17221   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17222     return NULL_TREE;
17223
17224   if (nested_name_specifier)
17225     {
17226       if (CLASS_TYPE_P (nested_name_specifier))
17227         {
17228           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17229           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17230           push_scope (nested_name_specifier);
17231         }
17232       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17233         {
17234           push_nested_namespace (nested_name_specifier);
17235         }
17236     }
17237
17238   /* Issue an error message if type-definitions are forbidden here.  */
17239   if (!cp_parser_check_type_definition (parser))
17240     type = error_mark_node;
17241   else
17242     /* Create the new type.  We do this before consuming the opening
17243        brace so the enum will be recorded as being on the line of its
17244        tag (or the 'enum' keyword, if there is no tag).  */
17245     type = start_enum (identifier, type, underlying_type,
17246                        attributes, scoped_enum_p, &is_new_type);
17247
17248   /* If the next token is not '{' it is an opaque-enum-specifier or an
17249      elaborated-type-specifier.  */
17250   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17251     {
17252       timevar_push (TV_PARSE_ENUM);
17253       if (nested_name_specifier
17254           && nested_name_specifier != error_mark_node)
17255         {
17256           /* The following catches invalid code such as:
17257              enum class S<int>::E { A, B, C }; */
17258           if (!processing_specialization
17259               && CLASS_TYPE_P (nested_name_specifier)
17260               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17261             error_at (type_start_token->location, "cannot add an enumerator "
17262                       "list to a template instantiation");
17263
17264           if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17265             {
17266               error_at (type_start_token->location,
17267                         "%<%T::%E%> has not been declared",
17268                         TYPE_CONTEXT (nested_name_specifier),
17269                         nested_name_specifier);
17270               type = error_mark_node;
17271             }
17272           else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17273                    && !CLASS_TYPE_P (nested_name_specifier))
17274             {
17275               error_at (type_start_token->location, "nested name specifier "
17276                         "%qT for enum declaration does not name a class "
17277                         "or namespace", nested_name_specifier);
17278               type = error_mark_node;
17279             }
17280           /* If that scope does not contain the scope in which the
17281              class was originally declared, the program is invalid.  */
17282           else if (prev_scope && !is_ancestor (prev_scope,
17283                                                nested_name_specifier))
17284             {
17285               if (at_namespace_scope_p ())
17286                 error_at (type_start_token->location,
17287                           "declaration of %qD in namespace %qD which does not "
17288                           "enclose %qD",
17289                           type, prev_scope, nested_name_specifier);
17290               else
17291                 error_at (type_start_token->location,
17292                           "declaration of %qD in %qD which does not "
17293                           "enclose %qD",
17294                           type, prev_scope, nested_name_specifier);
17295               type = error_mark_node;
17296             }
17297         }
17298
17299       if (scoped_enum_p)
17300         begin_scope (sk_scoped_enum, type);
17301
17302       /* Consume the opening brace.  */
17303       cp_lexer_consume_token (parser->lexer);
17304
17305       if (type == error_mark_node)
17306         ; /* Nothing to add */
17307       else if (OPAQUE_ENUM_P (type)
17308                || (cxx_dialect > cxx98 && processing_specialization))
17309         {
17310           new_value_list = true;
17311           SET_OPAQUE_ENUM_P (type, false);
17312           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17313         }
17314       else
17315         {
17316           error_at (type_start_token->location,
17317                     "multiple definition of %q#T", type);
17318           inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17319                   "previous definition here");
17320           type = error_mark_node;
17321         }
17322
17323       if (type == error_mark_node)
17324         cp_parser_skip_to_end_of_block_or_statement (parser);
17325       /* If the next token is not '}', then there are some enumerators.  */
17326       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17327         {
17328           if (is_anonymous && !scoped_enum_p)
17329             pedwarn (type_start_token->location, OPT_Wpedantic,
17330                      "ISO C++ forbids empty anonymous enum");
17331         }
17332       else
17333         cp_parser_enumerator_list (parser, type);
17334
17335       /* Consume the final '}'.  */
17336       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17337
17338       if (scoped_enum_p)
17339         finish_scope ();
17340       timevar_pop (TV_PARSE_ENUM);
17341     }
17342   else
17343     {
17344       /* If a ';' follows, then it is an opaque-enum-specifier
17345         and additional restrictions apply.  */
17346       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17347         {
17348           if (is_anonymous)
17349             error_at (type_start_token->location,
17350                       "opaque-enum-specifier without name");
17351           else if (nested_name_specifier)
17352             error_at (type_start_token->location,
17353                       "opaque-enum-specifier must use a simple identifier");
17354         }
17355     }
17356
17357   /* Look for trailing attributes to apply to this enumeration, and
17358      apply them if appropriate.  */
17359   if (cp_parser_allow_gnu_extensions_p (parser))
17360     {
17361       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17362       cplus_decl_attributes (&type,
17363                              trailing_attr,
17364                              (int) ATTR_FLAG_TYPE_IN_PLACE);
17365     }
17366
17367   /* Finish up the enumeration.  */
17368   if (type != error_mark_node)
17369     {
17370       if (new_value_list)
17371         finish_enum_value_list (type);
17372       if (is_new_type)
17373         finish_enum (type);
17374     }
17375
17376   if (nested_name_specifier)
17377     {
17378       if (CLASS_TYPE_P (nested_name_specifier))
17379         {
17380           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17381           pop_scope (nested_name_specifier);
17382         }
17383       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17384         {
17385           pop_nested_namespace (nested_name_specifier);
17386         }
17387     }
17388  out:
17389   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17390   return type;
17391 }
17392
17393 /* Parse an enumerator-list.  The enumerators all have the indicated
17394    TYPE.
17395
17396    enumerator-list:
17397      enumerator-definition
17398      enumerator-list , enumerator-definition  */
17399
17400 static void
17401 cp_parser_enumerator_list (cp_parser* parser, tree type)
17402 {
17403   while (true)
17404     {
17405       /* Parse an enumerator-definition.  */
17406       cp_parser_enumerator_definition (parser, type);
17407
17408       /* If the next token is not a ',', we've reached the end of
17409          the list.  */
17410       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17411         break;
17412       /* Otherwise, consume the `,' and keep going.  */
17413       cp_lexer_consume_token (parser->lexer);
17414       /* If the next token is a `}', there is a trailing comma.  */
17415       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17416         {
17417           if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17418             pedwarn (input_location, OPT_Wpedantic,
17419                      "comma at end of enumerator list");
17420           break;
17421         }
17422     }
17423 }
17424
17425 /* Parse an enumerator-definition.  The enumerator has the indicated
17426    TYPE.
17427
17428    enumerator-definition:
17429      enumerator
17430      enumerator = constant-expression
17431
17432    enumerator:
17433      identifier
17434
17435    GNU Extensions:
17436
17437    enumerator-definition:
17438      enumerator attributes [opt]
17439      enumerator attributes [opt] = constant-expression  */
17440
17441 static void
17442 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17443 {
17444   tree identifier;
17445   tree value;
17446   location_t loc;
17447
17448   /* Save the input location because we are interested in the location
17449      of the identifier and not the location of the explicit value.  */
17450   loc = cp_lexer_peek_token (parser->lexer)->location;
17451
17452   /* Look for the identifier.  */
17453   identifier = cp_parser_identifier (parser);
17454   if (identifier == error_mark_node)
17455     return;
17456
17457   /* Parse any specified attributes.  */
17458   tree attrs = cp_parser_attributes_opt (parser);
17459
17460   /* If the next token is an '=', then there is an explicit value.  */
17461   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17462     {
17463       /* Consume the `=' token.  */
17464       cp_lexer_consume_token (parser->lexer);
17465       /* Parse the value.  */
17466       value = cp_parser_constant_expression (parser);
17467     }
17468   else
17469     value = NULL_TREE;
17470
17471   /* If we are processing a template, make sure the initializer of the
17472      enumerator doesn't contain any bare template parameter pack.  */
17473   if (check_for_bare_parameter_packs (value))
17474     value = error_mark_node;
17475
17476   /* Create the enumerator.  */
17477   build_enumerator (identifier, value, type, attrs, loc);
17478 }
17479
17480 /* Parse a namespace-name.
17481
17482    namespace-name:
17483      original-namespace-name
17484      namespace-alias
17485
17486    Returns the NAMESPACE_DECL for the namespace.  */
17487
17488 static tree
17489 cp_parser_namespace_name (cp_parser* parser)
17490 {
17491   tree identifier;
17492   tree namespace_decl;
17493
17494   cp_token *token = cp_lexer_peek_token (parser->lexer);
17495
17496   /* Get the name of the namespace.  */
17497   identifier = cp_parser_identifier (parser);
17498   if (identifier == error_mark_node)
17499     return error_mark_node;
17500
17501   /* Look up the identifier in the currently active scope.  Look only
17502      for namespaces, due to:
17503
17504        [basic.lookup.udir]
17505
17506        When looking up a namespace-name in a using-directive or alias
17507        definition, only namespace names are considered.
17508
17509      And:
17510
17511        [basic.lookup.qual]
17512
17513        During the lookup of a name preceding the :: scope resolution
17514        operator, object, function, and enumerator names are ignored.
17515
17516      (Note that cp_parser_qualifying_entity only calls this
17517      function if the token after the name is the scope resolution
17518      operator.)  */
17519   namespace_decl = cp_parser_lookup_name (parser, identifier,
17520                                           none_type,
17521                                           /*is_template=*/false,
17522                                           /*is_namespace=*/true,
17523                                           /*check_dependency=*/true,
17524                                           /*ambiguous_decls=*/NULL,
17525                                           token->location);
17526   /* If it's not a namespace, issue an error.  */
17527   if (namespace_decl == error_mark_node
17528       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17529     {
17530       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17531         error_at (token->location, "%qD is not a namespace-name", identifier);
17532       cp_parser_error (parser, "expected namespace-name");
17533       namespace_decl = error_mark_node;
17534     }
17535
17536   return namespace_decl;
17537 }
17538
17539 /* Parse a namespace-definition.
17540
17541    namespace-definition:
17542      named-namespace-definition
17543      unnamed-namespace-definition
17544
17545    named-namespace-definition:
17546      original-namespace-definition
17547      extension-namespace-definition
17548
17549    original-namespace-definition:
17550      namespace identifier { namespace-body }
17551
17552    extension-namespace-definition:
17553      namespace original-namespace-name { namespace-body }
17554
17555    unnamed-namespace-definition:
17556      namespace { namespace-body } */
17557
17558 static void
17559 cp_parser_namespace_definition (cp_parser* parser)
17560 {
17561   tree identifier, attribs;
17562   bool has_visibility;
17563   bool is_inline;
17564   cp_token* token;
17565   int nested_definition_count = 0;
17566
17567   cp_ensure_no_omp_declare_simd (parser);
17568   cp_ensure_no_oacc_routine (parser);
17569   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17570     {
17571       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17572       is_inline = true;
17573       cp_lexer_consume_token (parser->lexer);
17574     }
17575   else
17576     is_inline = false;
17577
17578   /* Look for the `namespace' keyword.  */
17579   token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17580
17581   /* Parse any specified attributes before the identifier.  */
17582   attribs = cp_parser_attributes_opt (parser);
17583
17584   /* Get the name of the namespace.  We do not attempt to distinguish
17585      between an original-namespace-definition and an
17586      extension-namespace-definition at this point.  The semantic
17587      analysis routines are responsible for that.  */
17588   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17589     identifier = cp_parser_identifier (parser);
17590   else
17591     identifier = NULL_TREE;
17592
17593   /* Parse any specified attributes after the identifier.  */
17594   tree post_ident_attribs = cp_parser_attributes_opt (parser);
17595   if (post_ident_attribs)
17596     {
17597       if (attribs)
17598         attribs = chainon (attribs, post_ident_attribs);
17599       else
17600         attribs = post_ident_attribs;
17601     }
17602
17603   /* Start the namespace.  */
17604   push_namespace (identifier);
17605
17606   /* Parse any nested namespace definition. */
17607   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17608     {
17609       if (attribs)
17610         error_at (token->location, "a nested namespace definition cannot have attributes");
17611       if (cxx_dialect < cxx1z)
17612         pedwarn (input_location, OPT_Wpedantic,
17613                  "nested namespace definitions only available with "
17614                  "-std=c++1z or -std=gnu++1z");
17615       if (is_inline)
17616         error_at (token->location, "a nested namespace definition cannot be inline");
17617       while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17618         {
17619           cp_lexer_consume_token (parser->lexer);
17620           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17621             identifier = cp_parser_identifier (parser);
17622           else
17623             {
17624               cp_parser_error (parser, "nested identifier required");
17625               break;
17626             }
17627           ++nested_definition_count;
17628           push_namespace (identifier);
17629         }
17630     }
17631
17632   /* Look for the `{' to validate starting the namespace.  */
17633   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17634
17635   /* "inline namespace" is equivalent to a stub namespace definition
17636      followed by a strong using directive.  */
17637   if (is_inline)
17638     {
17639       tree name_space = current_namespace;
17640       /* Set up namespace association.  */
17641       DECL_NAMESPACE_ASSOCIATIONS (name_space)
17642         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17643                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
17644       /* Import the contents of the inline namespace.  */
17645       pop_namespace ();
17646       do_using_directive (name_space);
17647       push_namespace (identifier);
17648     }
17649
17650   has_visibility = handle_namespace_attrs (current_namespace, attribs);
17651
17652   warning  (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17653
17654   /* Parse the body of the namespace.  */
17655   cp_parser_namespace_body (parser);
17656
17657   if (has_visibility)
17658     pop_visibility (1);
17659
17660   /* Finish the nested namespace definitions.  */
17661   while (nested_definition_count--)
17662     pop_namespace ();
17663
17664   /* Finish the namespace.  */
17665   pop_namespace ();
17666   /* Look for the final `}'.  */
17667   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17668 }
17669
17670 /* Parse a namespace-body.
17671
17672    namespace-body:
17673      declaration-seq [opt]  */
17674
17675 static void
17676 cp_parser_namespace_body (cp_parser* parser)
17677 {
17678   cp_parser_declaration_seq_opt (parser);
17679 }
17680
17681 /* Parse a namespace-alias-definition.
17682
17683    namespace-alias-definition:
17684      namespace identifier = qualified-namespace-specifier ;  */
17685
17686 static void
17687 cp_parser_namespace_alias_definition (cp_parser* parser)
17688 {
17689   tree identifier;
17690   tree namespace_specifier;
17691
17692   cp_token *token = cp_lexer_peek_token (parser->lexer);
17693
17694   /* Look for the `namespace' keyword.  */
17695   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17696   /* Look for the identifier.  */
17697   identifier = cp_parser_identifier (parser);
17698   if (identifier == error_mark_node)
17699     return;
17700   /* Look for the `=' token.  */
17701   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17702       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
17703     {
17704       error_at (token->location, "%<namespace%> definition is not allowed here");
17705       /* Skip the definition.  */
17706       cp_lexer_consume_token (parser->lexer);
17707       if (cp_parser_skip_to_closing_brace (parser))
17708         cp_lexer_consume_token (parser->lexer);
17709       return;
17710     }
17711   cp_parser_require (parser, CPP_EQ, RT_EQ);
17712   /* Look for the qualified-namespace-specifier.  */
17713   namespace_specifier
17714     = cp_parser_qualified_namespace_specifier (parser);
17715   /* Look for the `;' token.  */
17716   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17717
17718   /* Register the alias in the symbol table.  */
17719   do_namespace_alias (identifier, namespace_specifier);
17720 }
17721
17722 /* Parse a qualified-namespace-specifier.
17723
17724    qualified-namespace-specifier:
17725      :: [opt] nested-name-specifier [opt] namespace-name
17726
17727    Returns a NAMESPACE_DECL corresponding to the specified
17728    namespace.  */
17729
17730 static tree
17731 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17732 {
17733   /* Look for the optional `::'.  */
17734   cp_parser_global_scope_opt (parser,
17735                               /*current_scope_valid_p=*/false);
17736
17737   /* Look for the optional nested-name-specifier.  */
17738   cp_parser_nested_name_specifier_opt (parser,
17739                                        /*typename_keyword_p=*/false,
17740                                        /*check_dependency_p=*/true,
17741                                        /*type_p=*/false,
17742                                        /*is_declaration=*/true);
17743
17744   return cp_parser_namespace_name (parser);
17745 }
17746
17747 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17748    access declaration.
17749
17750    using-declaration:
17751      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17752      using :: unqualified-id ;  
17753
17754    access-declaration:
17755      qualified-id ;  
17756
17757    */
17758
17759 static bool
17760 cp_parser_using_declaration (cp_parser* parser, 
17761                              bool access_declaration_p)
17762 {
17763   cp_token *token;
17764   bool typename_p = false;
17765   bool global_scope_p;
17766   tree decl;
17767   tree identifier;
17768   tree qscope;
17769   int oldcount = errorcount;
17770   cp_token *diag_token = NULL;
17771
17772   if (access_declaration_p)
17773     {
17774       diag_token = cp_lexer_peek_token (parser->lexer);
17775       cp_parser_parse_tentatively (parser);
17776     }
17777   else
17778     {
17779       /* Look for the `using' keyword.  */
17780       cp_parser_require_keyword (parser, RID_USING, RT_USING);
17781       
17782       /* Peek at the next token.  */
17783       token = cp_lexer_peek_token (parser->lexer);
17784       /* See if it's `typename'.  */
17785       if (token->keyword == RID_TYPENAME)
17786         {
17787           /* Remember that we've seen it.  */
17788           typename_p = true;
17789           /* Consume the `typename' token.  */
17790           cp_lexer_consume_token (parser->lexer);
17791         }
17792     }
17793
17794   /* Look for the optional global scope qualification.  */
17795   global_scope_p
17796     = (cp_parser_global_scope_opt (parser,
17797                                    /*current_scope_valid_p=*/false)
17798        != NULL_TREE);
17799
17800   /* If we saw `typename', or didn't see `::', then there must be a
17801      nested-name-specifier present.  */
17802   if (typename_p || !global_scope_p)
17803     {
17804       qscope = cp_parser_nested_name_specifier (parser, typename_p,
17805                                                 /*check_dependency_p=*/true,
17806                                                 /*type_p=*/false,
17807                                                 /*is_declaration=*/true);
17808       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17809         {
17810           cp_parser_skip_to_end_of_block_or_statement (parser);
17811           return false;
17812         }
17813     }
17814   /* Otherwise, we could be in either of the two productions.  In that
17815      case, treat the nested-name-specifier as optional.  */
17816   else
17817     qscope = cp_parser_nested_name_specifier_opt (parser,
17818                                                   /*typename_keyword_p=*/false,
17819                                                   /*check_dependency_p=*/true,
17820                                                   /*type_p=*/false,
17821                                                   /*is_declaration=*/true);
17822   if (!qscope)
17823     qscope = global_namespace;
17824   else if (UNSCOPED_ENUM_P (qscope))
17825     qscope = CP_TYPE_CONTEXT (qscope);
17826
17827   if (access_declaration_p && cp_parser_error_occurred (parser))
17828     /* Something has already gone wrong; there's no need to parse
17829        further.  Since an error has occurred, the return value of
17830        cp_parser_parse_definitely will be false, as required.  */
17831     return cp_parser_parse_definitely (parser);
17832
17833   token = cp_lexer_peek_token (parser->lexer);
17834   /* Parse the unqualified-id.  */
17835   identifier = cp_parser_unqualified_id (parser,
17836                                          /*template_keyword_p=*/false,
17837                                          /*check_dependency_p=*/true,
17838                                          /*declarator_p=*/true,
17839                                          /*optional_p=*/false);
17840
17841   if (access_declaration_p)
17842     {
17843       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17844         cp_parser_simulate_error (parser);
17845       if (!cp_parser_parse_definitely (parser))
17846         return false;
17847     }
17848
17849   /* The function we call to handle a using-declaration is different
17850      depending on what scope we are in.  */
17851   if (qscope == error_mark_node || identifier == error_mark_node)
17852     ;
17853   else if (!identifier_p (identifier)
17854            && TREE_CODE (identifier) != BIT_NOT_EXPR)
17855     /* [namespace.udecl]
17856
17857        A using declaration shall not name a template-id.  */
17858     error_at (token->location,
17859               "a template-id may not appear in a using-declaration");
17860   else
17861     {
17862       if (at_class_scope_p ())
17863         {
17864           /* Create the USING_DECL.  */
17865           decl = do_class_using_decl (parser->scope, identifier);
17866
17867           if (decl && typename_p)
17868             USING_DECL_TYPENAME_P (decl) = 1;
17869
17870           if (check_for_bare_parameter_packs (decl))
17871             {
17872               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17873               return false;
17874             }
17875           else
17876             /* Add it to the list of members in this class.  */
17877             finish_member_declaration (decl);
17878         }
17879       else
17880         {
17881           decl = cp_parser_lookup_name_simple (parser,
17882                                                identifier,
17883                                                token->location);
17884           if (decl == error_mark_node)
17885             cp_parser_name_lookup_error (parser, identifier,
17886                                          decl, NLE_NULL,
17887                                          token->location);
17888           else if (check_for_bare_parameter_packs (decl))
17889             {
17890               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17891               return false;
17892             }
17893           else if (!at_namespace_scope_p ())
17894             do_local_using_decl (decl, qscope, identifier);
17895           else
17896             do_toplevel_using_decl (decl, qscope, identifier);
17897         }
17898     }
17899
17900   /* Look for the final `;'.  */
17901   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17902
17903   if (access_declaration_p && errorcount == oldcount)
17904     warning_at (diag_token->location, OPT_Wdeprecated,
17905                 "access declarations are deprecated "
17906                 "in favour of using-declarations; "
17907                 "suggestion: add the %<using%> keyword");
17908
17909   return true;
17910 }
17911
17912 /* Parse an alias-declaration.
17913
17914    alias-declaration:
17915      using identifier attribute-specifier-seq [opt] = type-id  */
17916
17917 static tree
17918 cp_parser_alias_declaration (cp_parser* parser)
17919 {
17920   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17921   location_t id_location;
17922   cp_declarator *declarator;
17923   cp_decl_specifier_seq decl_specs;
17924   bool member_p;
17925   const char *saved_message = NULL;
17926
17927   /* Look for the `using' keyword.  */
17928   cp_token *using_token
17929     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17930   if (using_token == NULL)
17931     return error_mark_node;
17932
17933   id_location = cp_lexer_peek_token (parser->lexer)->location;
17934   id = cp_parser_identifier (parser);
17935   if (id == error_mark_node)
17936     return error_mark_node;
17937
17938   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17939   attributes = cp_parser_attributes_opt (parser);
17940   if (attributes == error_mark_node)
17941     return error_mark_node;
17942
17943   cp_parser_require (parser, CPP_EQ, RT_EQ);
17944
17945   if (cp_parser_error_occurred (parser))
17946     return error_mark_node;
17947
17948   cp_parser_commit_to_tentative_parse (parser);
17949
17950   /* Now we are going to parse the type-id of the declaration.  */
17951
17952   /*
17953     [dcl.type]/3 says:
17954
17955         "A type-specifier-seq shall not define a class or enumeration
17956          unless it appears in the type-id of an alias-declaration (7.1.3) that
17957          is not the declaration of a template-declaration."
17958
17959     In other words, if we currently are in an alias template, the
17960     type-id should not define a type.
17961
17962     So let's set parser->type_definition_forbidden_message in that
17963     case; cp_parser_check_type_definition (called by
17964     cp_parser_class_specifier) will then emit an error if a type is
17965     defined in the type-id.  */
17966   if (parser->num_template_parameter_lists)
17967     {
17968       saved_message = parser->type_definition_forbidden_message;
17969       parser->type_definition_forbidden_message =
17970         G_("types may not be defined in alias template declarations");
17971     }
17972
17973   type = cp_parser_type_id (parser);
17974
17975   /* Restore the error message if need be.  */
17976   if (parser->num_template_parameter_lists)
17977     parser->type_definition_forbidden_message = saved_message;
17978
17979   if (type == error_mark_node
17980       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17981     {
17982       cp_parser_skip_to_end_of_block_or_statement (parser);
17983       return error_mark_node;
17984     }
17985
17986   /* A typedef-name can also be introduced by an alias-declaration. The
17987      identifier following the using keyword becomes a typedef-name. It has
17988      the same semantics as if it were introduced by the typedef
17989      specifier. In particular, it does not define a new type and it shall
17990      not appear in the type-id.  */
17991
17992   clear_decl_specs (&decl_specs);
17993   decl_specs.type = type;
17994   if (attributes != NULL_TREE)
17995     {
17996       decl_specs.attributes = attributes;
17997       set_and_check_decl_spec_loc (&decl_specs,
17998                                    ds_attribute,
17999                                    attrs_token);
18000     }
18001   set_and_check_decl_spec_loc (&decl_specs,
18002                                ds_typedef,
18003                                using_token);
18004   set_and_check_decl_spec_loc (&decl_specs,
18005                                ds_alias,
18006                                using_token);
18007
18008   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18009   declarator->id_loc = id_location;
18010
18011   member_p = at_class_scope_p ();
18012   if (member_p)
18013     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18014                       NULL_TREE, attributes);
18015   else
18016     decl = start_decl (declarator, &decl_specs, 0,
18017                        attributes, NULL_TREE, &pushed_scope);
18018   if (decl == error_mark_node)
18019     return decl;
18020
18021   // Attach constraints to the alias declaration.
18022   if (flag_concepts && current_template_parms)
18023     {
18024       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18025       tree constr = build_constraints (reqs, NULL_TREE);
18026       set_constraints (decl, constr);
18027     }
18028
18029   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18030
18031   if (pushed_scope)
18032     pop_scope (pushed_scope);
18033
18034   /* If decl is a template, return its TEMPLATE_DECL so that it gets
18035      added into the symbol table; otherwise, return the TYPE_DECL.  */
18036   if (DECL_LANG_SPECIFIC (decl)
18037       && DECL_TEMPLATE_INFO (decl)
18038       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18039     {
18040       decl = DECL_TI_TEMPLATE (decl);
18041       if (member_p)
18042         check_member_template (decl);
18043     }
18044
18045   return decl;
18046 }
18047
18048 /* Parse a using-directive.
18049
18050    using-directive:
18051      using namespace :: [opt] nested-name-specifier [opt]
18052        namespace-name ;  */
18053
18054 static void
18055 cp_parser_using_directive (cp_parser* parser)
18056 {
18057   tree namespace_decl;
18058   tree attribs;
18059
18060   /* Look for the `using' keyword.  */
18061   cp_parser_require_keyword (parser, RID_USING, RT_USING);
18062   /* And the `namespace' keyword.  */
18063   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18064   /* Look for the optional `::' operator.  */
18065   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18066   /* And the optional nested-name-specifier.  */
18067   cp_parser_nested_name_specifier_opt (parser,
18068                                        /*typename_keyword_p=*/false,
18069                                        /*check_dependency_p=*/true,
18070                                        /*type_p=*/false,
18071                                        /*is_declaration=*/true);
18072   /* Get the namespace being used.  */
18073   namespace_decl = cp_parser_namespace_name (parser);
18074   /* And any specified attributes.  */
18075   attribs = cp_parser_attributes_opt (parser);
18076   /* Update the symbol table.  */
18077   parse_using_directive (namespace_decl, attribs);
18078   /* Look for the final `;'.  */
18079   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18080 }
18081
18082 /* Parse an asm-definition.
18083
18084    asm-definition:
18085      asm ( string-literal ) ;
18086
18087    GNU Extension:
18088
18089    asm-definition:
18090      asm volatile [opt] ( string-literal ) ;
18091      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18092      asm volatile [opt] ( string-literal : asm-operand-list [opt]
18093                           : asm-operand-list [opt] ) ;
18094      asm volatile [opt] ( string-literal : asm-operand-list [opt]
18095                           : asm-operand-list [opt]
18096                           : asm-clobber-list [opt] ) ;
18097      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18098                                : asm-clobber-list [opt]
18099                                : asm-goto-list ) ;  */
18100
18101 static void
18102 cp_parser_asm_definition (cp_parser* parser)
18103 {
18104   tree string;
18105   tree outputs = NULL_TREE;
18106   tree inputs = NULL_TREE;
18107   tree clobbers = NULL_TREE;
18108   tree labels = NULL_TREE;
18109   tree asm_stmt;
18110   bool volatile_p = false;
18111   bool extended_p = false;
18112   bool invalid_inputs_p = false;
18113   bool invalid_outputs_p = false;
18114   bool goto_p = false;
18115   required_token missing = RT_NONE;
18116
18117   /* Look for the `asm' keyword.  */
18118   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18119
18120   if (parser->in_function_body
18121       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18122     {
18123       error ("%<asm%> in %<constexpr%> function");
18124       cp_function_chain->invalid_constexpr = true;
18125     }
18126
18127   /* See if the next token is `volatile'.  */
18128   if (cp_parser_allow_gnu_extensions_p (parser)
18129       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18130     {
18131       /* Remember that we saw the `volatile' keyword.  */
18132       volatile_p = true;
18133       /* Consume the token.  */
18134       cp_lexer_consume_token (parser->lexer);
18135     }
18136   if (cp_parser_allow_gnu_extensions_p (parser)
18137       && parser->in_function_body
18138       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18139     {
18140       /* Remember that we saw the `goto' keyword.  */
18141       goto_p = true;
18142       /* Consume the token.  */
18143       cp_lexer_consume_token (parser->lexer);
18144     }
18145   /* Look for the opening `('.  */
18146   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18147     return;
18148   /* Look for the string.  */
18149   string = cp_parser_string_literal (parser, false, false);
18150   if (string == error_mark_node)
18151     {
18152       cp_parser_skip_to_closing_parenthesis (parser, true, false,
18153                                              /*consume_paren=*/true);
18154       return;
18155     }
18156
18157   /* If we're allowing GNU extensions, check for the extended assembly
18158      syntax.  Unfortunately, the `:' tokens need not be separated by
18159      a space in C, and so, for compatibility, we tolerate that here
18160      too.  Doing that means that we have to treat the `::' operator as
18161      two `:' tokens.  */
18162   if (cp_parser_allow_gnu_extensions_p (parser)
18163       && parser->in_function_body
18164       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18165           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18166     {
18167       bool inputs_p = false;
18168       bool clobbers_p = false;
18169       bool labels_p = false;
18170
18171       /* The extended syntax was used.  */
18172       extended_p = true;
18173
18174       /* Look for outputs.  */
18175       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18176         {
18177           /* Consume the `:'.  */
18178           cp_lexer_consume_token (parser->lexer);
18179           /* Parse the output-operands.  */
18180           if (cp_lexer_next_token_is_not (parser->lexer,
18181                                           CPP_COLON)
18182               && cp_lexer_next_token_is_not (parser->lexer,
18183                                              CPP_SCOPE)
18184               && cp_lexer_next_token_is_not (parser->lexer,
18185                                              CPP_CLOSE_PAREN)
18186               && !goto_p)
18187             {
18188               outputs = cp_parser_asm_operand_list (parser);
18189               if (outputs == error_mark_node)
18190                 invalid_outputs_p = true;
18191             }
18192         }
18193       /* If the next token is `::', there are no outputs, and the
18194          next token is the beginning of the inputs.  */
18195       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18196         /* The inputs are coming next.  */
18197         inputs_p = true;
18198
18199       /* Look for inputs.  */
18200       if (inputs_p
18201           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18202         {
18203           /* Consume the `:' or `::'.  */
18204           cp_lexer_consume_token (parser->lexer);
18205           /* Parse the output-operands.  */
18206           if (cp_lexer_next_token_is_not (parser->lexer,
18207                                           CPP_COLON)
18208               && cp_lexer_next_token_is_not (parser->lexer,
18209                                              CPP_SCOPE)
18210               && cp_lexer_next_token_is_not (parser->lexer,
18211                                              CPP_CLOSE_PAREN))
18212             {
18213               inputs = cp_parser_asm_operand_list (parser);
18214               if (inputs == error_mark_node)
18215                 invalid_inputs_p = true;
18216             }
18217         }
18218       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18219         /* The clobbers are coming next.  */
18220         clobbers_p = true;
18221
18222       /* Look for clobbers.  */
18223       if (clobbers_p
18224           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18225         {
18226           clobbers_p = true;
18227           /* Consume the `:' or `::'.  */
18228           cp_lexer_consume_token (parser->lexer);
18229           /* Parse the clobbers.  */
18230           if (cp_lexer_next_token_is_not (parser->lexer,
18231                                           CPP_COLON)
18232               && cp_lexer_next_token_is_not (parser->lexer,
18233                                              CPP_CLOSE_PAREN))
18234             clobbers = cp_parser_asm_clobber_list (parser);
18235         }
18236       else if (goto_p
18237                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18238         /* The labels are coming next.  */
18239         labels_p = true;
18240
18241       /* Look for labels.  */
18242       if (labels_p
18243           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18244         {
18245           labels_p = true;
18246           /* Consume the `:' or `::'.  */
18247           cp_lexer_consume_token (parser->lexer);
18248           /* Parse the labels.  */
18249           labels = cp_parser_asm_label_list (parser);
18250         }
18251
18252       if (goto_p && !labels_p)
18253         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18254     }
18255   else if (goto_p)
18256     missing = RT_COLON_SCOPE;
18257
18258   /* Look for the closing `)'.  */
18259   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18260                           missing ? missing : RT_CLOSE_PAREN))
18261     cp_parser_skip_to_closing_parenthesis (parser, true, false,
18262                                            /*consume_paren=*/true);
18263   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18264
18265   if (!invalid_inputs_p && !invalid_outputs_p)
18266     {
18267       /* Create the ASM_EXPR.  */
18268       if (parser->in_function_body)
18269         {
18270           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18271                                       inputs, clobbers, labels);
18272           /* If the extended syntax was not used, mark the ASM_EXPR.  */
18273           if (!extended_p)
18274             {
18275               tree temp = asm_stmt;
18276               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18277                 temp = TREE_OPERAND (temp, 0);
18278
18279               ASM_INPUT_P (temp) = 1;
18280             }
18281         }
18282       else
18283         symtab->finalize_toplevel_asm (string);
18284     }
18285 }
18286
18287 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18288    type that comes from the decl-specifier-seq.  */
18289
18290 static tree
18291 strip_declarator_types (tree type, cp_declarator *declarator)
18292 {
18293   for (cp_declarator *d = declarator; d;)
18294     switch (d->kind)
18295       {
18296       case cdk_id:
18297       case cdk_error:
18298         d = NULL;
18299         break;
18300
18301       default:
18302         if (TYPE_PTRMEMFUNC_P (type))
18303           type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18304         type = TREE_TYPE (type);
18305         d = d->declarator;
18306         break;
18307       }
18308
18309   return type;
18310 }
18311
18312 /* Declarators [gram.dcl.decl] */
18313
18314 /* Parse an init-declarator.
18315
18316    init-declarator:
18317      declarator initializer [opt]
18318
18319    GNU Extension:
18320
18321    init-declarator:
18322      declarator asm-specification [opt] attributes [opt] initializer [opt]
18323
18324    function-definition:
18325      decl-specifier-seq [opt] declarator ctor-initializer [opt]
18326        function-body
18327      decl-specifier-seq [opt] declarator function-try-block
18328
18329    GNU Extension:
18330
18331    function-definition:
18332      __extension__ function-definition
18333
18334    TM Extension:
18335
18336    function-definition:
18337      decl-specifier-seq [opt] declarator function-transaction-block
18338
18339    The DECL_SPECIFIERS apply to this declarator.  Returns a
18340    representation of the entity declared.  If MEMBER_P is TRUE, then
18341    this declarator appears in a class scope.  The new DECL created by
18342    this declarator is returned.
18343
18344    The CHECKS are access checks that should be performed once we know
18345    what entity is being declared (and, therefore, what classes have
18346    befriended it).
18347
18348    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18349    for a function-definition here as well.  If the declarator is a
18350    declarator for a function-definition, *FUNCTION_DEFINITION_P will
18351    be TRUE upon return.  By that point, the function-definition will
18352    have been completely parsed.
18353
18354    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18355    is FALSE.
18356
18357    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18358    parsed declaration if it is an uninitialized single declarator not followed
18359    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18360    if present, will not be consumed.  If returned, this declarator will be
18361    created with SD_INITIALIZED but will not call cp_finish_decl.
18362
18363    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18364    and there is an initializer, the pointed location_t is set to the
18365    location of the '=' or `(', or '{' in C++11 token introducing the
18366    initializer.  */
18367
18368 static tree
18369 cp_parser_init_declarator (cp_parser* parser,
18370                            cp_decl_specifier_seq *decl_specifiers,
18371                            vec<deferred_access_check, va_gc> *checks,
18372                            bool function_definition_allowed_p,
18373                            bool member_p,
18374                            int declares_class_or_enum,
18375                            bool* function_definition_p,
18376                            tree* maybe_range_for_decl,
18377                            location_t* init_loc,
18378                            tree* auto_result)
18379 {
18380   cp_token *token = NULL, *asm_spec_start_token = NULL,
18381            *attributes_start_token = NULL;
18382   cp_declarator *declarator;
18383   tree prefix_attributes;
18384   tree attributes = NULL;
18385   tree asm_specification;
18386   tree initializer;
18387   tree decl = NULL_TREE;
18388   tree scope;
18389   int is_initialized;
18390   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
18391      initialized with "= ..", CPP_OPEN_PAREN if initialized with
18392      "(...)".  */
18393   enum cpp_ttype initialization_kind;
18394   bool is_direct_init = false;
18395   bool is_non_constant_init;
18396   int ctor_dtor_or_conv_p;
18397   bool friend_p = cp_parser_friend_p (decl_specifiers);
18398   tree pushed_scope = NULL_TREE;
18399   bool range_for_decl_p = false;
18400   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18401   location_t tmp_init_loc = UNKNOWN_LOCATION;
18402
18403   /* Gather the attributes that were provided with the
18404      decl-specifiers.  */
18405   prefix_attributes = decl_specifiers->attributes;
18406
18407   /* Assume that this is not the declarator for a function
18408      definition.  */
18409   if (function_definition_p)
18410     *function_definition_p = false;
18411
18412   /* Default arguments are only permitted for function parameters.  */
18413   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18414     parser->default_arg_ok_p = false;
18415
18416   /* Defer access checks while parsing the declarator; we cannot know
18417      what names are accessible until we know what is being
18418      declared.  */
18419   resume_deferring_access_checks ();
18420
18421   /* Parse the declarator.  */
18422   token = cp_lexer_peek_token (parser->lexer);
18423   declarator
18424     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18425                             &ctor_dtor_or_conv_p,
18426                             /*parenthesized_p=*/NULL,
18427                             member_p, friend_p);
18428   /* Gather up the deferred checks.  */
18429   stop_deferring_access_checks ();
18430
18431   parser->default_arg_ok_p = saved_default_arg_ok_p;
18432
18433   /* If the DECLARATOR was erroneous, there's no need to go
18434      further.  */
18435   if (declarator == cp_error_declarator)
18436     return error_mark_node;
18437
18438   /* Check that the number of template-parameter-lists is OK.  */
18439   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18440                                                        token->location))
18441     return error_mark_node;
18442
18443   if (declares_class_or_enum & 2)
18444     cp_parser_check_for_definition_in_return_type (declarator,
18445                                                    decl_specifiers->type,
18446                                                    decl_specifiers->locations[ds_type_spec]);
18447
18448   /* Figure out what scope the entity declared by the DECLARATOR is
18449      located in.  `grokdeclarator' sometimes changes the scope, so
18450      we compute it now.  */
18451   scope = get_scope_of_declarator (declarator);
18452
18453   /* Perform any lookups in the declared type which were thought to be
18454      dependent, but are not in the scope of the declarator.  */
18455   decl_specifiers->type
18456     = maybe_update_decl_type (decl_specifiers->type, scope);
18457
18458   /* If we're allowing GNU extensions, look for an
18459      asm-specification.  */
18460   if (cp_parser_allow_gnu_extensions_p (parser))
18461     {
18462       /* Look for an asm-specification.  */
18463       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18464       asm_specification = cp_parser_asm_specification_opt (parser);
18465     }
18466   else
18467     asm_specification = NULL_TREE;
18468
18469   /* Look for attributes.  */
18470   attributes_start_token = cp_lexer_peek_token (parser->lexer);
18471   attributes = cp_parser_attributes_opt (parser);
18472
18473   /* Peek at the next token.  */
18474   token = cp_lexer_peek_token (parser->lexer);
18475
18476   bool bogus_implicit_tmpl = false;
18477
18478   if (function_declarator_p (declarator))
18479     {
18480       /* Check to see if the token indicates the start of a
18481          function-definition.  */
18482       if (cp_parser_token_starts_function_definition_p (token))
18483         {
18484           if (!function_definition_allowed_p)
18485             {
18486               /* If a function-definition should not appear here, issue an
18487                  error message.  */
18488               cp_parser_error (parser,
18489                                "a function-definition is not allowed here");
18490               return error_mark_node;
18491             }
18492
18493           location_t func_brace_location
18494             = cp_lexer_peek_token (parser->lexer)->location;
18495
18496           /* Neither attributes nor an asm-specification are allowed
18497              on a function-definition.  */
18498           if (asm_specification)
18499             error_at (asm_spec_start_token->location,
18500                       "an asm-specification is not allowed "
18501                       "on a function-definition");
18502           if (attributes)
18503             error_at (attributes_start_token->location,
18504                       "attributes are not allowed "
18505                       "on a function-definition");
18506           /* This is a function-definition.  */
18507           *function_definition_p = true;
18508
18509           /* Parse the function definition.  */
18510           if (member_p)
18511             decl = cp_parser_save_member_function_body (parser,
18512                                                         decl_specifiers,
18513                                                         declarator,
18514                                                         prefix_attributes);
18515           else
18516             decl =
18517               (cp_parser_function_definition_from_specifiers_and_declarator
18518                (parser, decl_specifiers, prefix_attributes, declarator));
18519
18520           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18521             {
18522               /* This is where the prologue starts...  */
18523               DECL_STRUCT_FUNCTION (decl)->function_start_locus
18524                 = func_brace_location;
18525             }
18526
18527           return decl;
18528         }
18529     }
18530   else if (parser->fully_implicit_function_template_p)
18531     {
18532       /* A non-template declaration involving a function parameter list
18533          containing an implicit template parameter will be made into a
18534          template.  If the resulting declaration is not going to be an
18535          actual function then finish the template scope here to prevent it.
18536          An error message will be issued once we have a decl to talk about.
18537
18538          FIXME probably we should do type deduction rather than create an
18539          implicit template, but the standard currently doesn't allow it. */
18540       bogus_implicit_tmpl = true;
18541       finish_fully_implicit_template (parser, NULL_TREE);
18542     }
18543
18544   /* [dcl.dcl]
18545
18546      Only in function declarations for constructors, destructors, and
18547      type conversions can the decl-specifier-seq be omitted.
18548
18549      We explicitly postpone this check past the point where we handle
18550      function-definitions because we tolerate function-definitions
18551      that are missing their return types in some modes.  */
18552   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18553     {
18554       cp_parser_error (parser,
18555                        "expected constructor, destructor, or type conversion");
18556       return error_mark_node;
18557     }
18558
18559   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
18560   if (token->type == CPP_EQ
18561       || token->type == CPP_OPEN_PAREN
18562       || token->type == CPP_OPEN_BRACE)
18563     {
18564       is_initialized = SD_INITIALIZED;
18565       initialization_kind = token->type;
18566       if (maybe_range_for_decl)
18567         *maybe_range_for_decl = error_mark_node;
18568       tmp_init_loc = token->location;
18569       if (init_loc && *init_loc == UNKNOWN_LOCATION)
18570         *init_loc = tmp_init_loc;
18571
18572       if (token->type == CPP_EQ
18573           && function_declarator_p (declarator))
18574         {
18575           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18576           if (t2->keyword == RID_DEFAULT)
18577             is_initialized = SD_DEFAULTED;
18578           else if (t2->keyword == RID_DELETE)
18579             is_initialized = SD_DELETED;
18580         }
18581     }
18582   else
18583     {
18584       /* If the init-declarator isn't initialized and isn't followed by a
18585          `,' or `;', it's not a valid init-declarator.  */
18586       if (token->type != CPP_COMMA
18587           && token->type != CPP_SEMICOLON)
18588         {
18589           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18590             range_for_decl_p = true;
18591           else
18592             {
18593               if (!maybe_range_for_decl)
18594                 cp_parser_error (parser, "expected initializer");
18595               return error_mark_node;
18596             }
18597         }
18598       is_initialized = SD_UNINITIALIZED;
18599       initialization_kind = CPP_EOF;
18600     }
18601
18602   /* Because start_decl has side-effects, we should only call it if we
18603      know we're going ahead.  By this point, we know that we cannot
18604      possibly be looking at any other construct.  */
18605   cp_parser_commit_to_tentative_parse (parser);
18606
18607   /* Enter the newly declared entry in the symbol table.  If we're
18608      processing a declaration in a class-specifier, we wait until
18609      after processing the initializer.  */
18610   if (!member_p)
18611     {
18612       if (parser->in_unbraced_linkage_specification_p)
18613         decl_specifiers->storage_class = sc_extern;
18614       decl = start_decl (declarator, decl_specifiers,
18615                          range_for_decl_p? SD_INITIALIZED : is_initialized,
18616                          attributes, prefix_attributes, &pushed_scope);
18617       cp_finalize_omp_declare_simd (parser, decl);
18618       cp_finalize_oacc_routine (parser, decl, false);
18619       /* Adjust location of decl if declarator->id_loc is more appropriate:
18620          set, and decl wasn't merged with another decl, in which case its
18621          location would be different from input_location, and more accurate.  */
18622       if (DECL_P (decl)
18623           && declarator->id_loc != UNKNOWN_LOCATION
18624           && DECL_SOURCE_LOCATION (decl) == input_location)
18625         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18626     }
18627   else if (scope)
18628     /* Enter the SCOPE.  That way unqualified names appearing in the
18629        initializer will be looked up in SCOPE.  */
18630     pushed_scope = push_scope (scope);
18631
18632   /* Perform deferred access control checks, now that we know in which
18633      SCOPE the declared entity resides.  */
18634   if (!member_p && decl)
18635     {
18636       tree saved_current_function_decl = NULL_TREE;
18637
18638       /* If the entity being declared is a function, pretend that we
18639          are in its scope.  If it is a `friend', it may have access to
18640          things that would not otherwise be accessible.  */
18641       if (TREE_CODE (decl) == FUNCTION_DECL)
18642         {
18643           saved_current_function_decl = current_function_decl;
18644           current_function_decl = decl;
18645         }
18646
18647       /* Perform access checks for template parameters.  */
18648       cp_parser_perform_template_parameter_access_checks (checks);
18649
18650       /* Perform the access control checks for the declarator and the
18651          decl-specifiers.  */
18652       perform_deferred_access_checks (tf_warning_or_error);
18653
18654       /* Restore the saved value.  */
18655       if (TREE_CODE (decl) == FUNCTION_DECL)
18656         current_function_decl = saved_current_function_decl;
18657     }
18658
18659   /* Parse the initializer.  */
18660   initializer = NULL_TREE;
18661   is_direct_init = false;
18662   is_non_constant_init = true;
18663   if (is_initialized)
18664     {
18665       if (function_declarator_p (declarator))
18666         {
18667            if (initialization_kind == CPP_EQ)
18668              initializer = cp_parser_pure_specifier (parser);
18669            else
18670              {
18671                /* If the declaration was erroneous, we don't really
18672                   know what the user intended, so just silently
18673                   consume the initializer.  */
18674                if (decl != error_mark_node)
18675                  error_at (tmp_init_loc, "initializer provided for function");
18676                cp_parser_skip_to_closing_parenthesis (parser,
18677                                                       /*recovering=*/true,
18678                                                       /*or_comma=*/false,
18679                                                       /*consume_paren=*/true);
18680              }
18681         }
18682       else
18683         {
18684           /* We want to record the extra mangling scope for in-class
18685              initializers of class members and initializers of static data
18686              member templates.  The former involves deferring
18687              parsing of the initializer until end of class as with default
18688              arguments.  So right here we only handle the latter.  */
18689           if (!member_p && processing_template_decl)
18690             start_lambda_scope (decl);
18691           initializer = cp_parser_initializer (parser,
18692                                                &is_direct_init,
18693                                                &is_non_constant_init);
18694           if (!member_p && processing_template_decl)
18695             finish_lambda_scope ();
18696           if (initializer == error_mark_node)
18697             cp_parser_skip_to_end_of_statement (parser);
18698           else if ((flag_sanitize & SANITIZE_UI_OVERFLOW)
18699                     && isan_internal_fn_p (initializer))
18700             {
18701               int uns_p = TYPE_UNSIGNED (TREE_TYPE (decl));
18702               isan_maybe_change_ifn_sign_arg (&initializer, uns_p);
18703             }
18704         }
18705     }
18706
18707   /* The old parser allows attributes to appear after a parenthesized
18708      initializer.  Mark Mitchell proposed removing this functionality
18709      on the GCC mailing lists on 2002-08-13.  This parser accepts the
18710      attributes -- but ignores them.  */
18711   if (cp_parser_allow_gnu_extensions_p (parser)
18712       && initialization_kind == CPP_OPEN_PAREN)
18713     if (cp_parser_attributes_opt (parser))
18714       warning (OPT_Wattributes,
18715                "attributes after parenthesized initializer ignored");
18716
18717   /* And now complain about a non-function implicit template.  */
18718   if (bogus_implicit_tmpl && decl != error_mark_node)
18719     error_at (DECL_SOURCE_LOCATION (decl),
18720               "non-function %qD declared as implicit template", decl);
18721
18722   /* For an in-class declaration, use `grokfield' to create the
18723      declaration.  */
18724   if (member_p)
18725     {
18726       if (pushed_scope)
18727         {
18728           pop_scope (pushed_scope);
18729           pushed_scope = NULL_TREE;
18730         }
18731       decl = grokfield (declarator, decl_specifiers,
18732                         initializer, !is_non_constant_init,
18733                         /*asmspec=*/NULL_TREE,
18734                         chainon (attributes, prefix_attributes));
18735       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18736         cp_parser_save_default_args (parser, decl);
18737       cp_finalize_omp_declare_simd (parser, decl);
18738       cp_finalize_oacc_routine (parser, decl, false);
18739     }
18740
18741   /* Finish processing the declaration.  But, skip member
18742      declarations.  */
18743   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18744     {
18745       cp_finish_decl (decl,
18746                       initializer, !is_non_constant_init,
18747                       asm_specification,
18748                       /* If the initializer is in parentheses, then this is
18749                          a direct-initialization, which means that an
18750                          `explicit' constructor is OK.  Otherwise, an
18751                          `explicit' constructor cannot be used.  */
18752                       ((is_direct_init || !is_initialized)
18753                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18754     }
18755   else if ((cxx_dialect != cxx98) && friend_p
18756            && decl && TREE_CODE (decl) == FUNCTION_DECL)
18757     /* Core issue #226 (C++0x only): A default template-argument
18758        shall not be specified in a friend class template
18759        declaration. */
18760     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
18761                              /*is_partial=*/false, /*is_friend_decl=*/1);
18762
18763   if (!friend_p && pushed_scope)
18764     pop_scope (pushed_scope);
18765
18766   if (function_declarator_p (declarator)
18767       && parser->fully_implicit_function_template_p)
18768     {
18769       if (member_p)
18770         decl = finish_fully_implicit_template (parser, decl);
18771       else
18772         finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18773     }
18774
18775   if (auto_result && is_initialized && decl_specifiers->type
18776       && type_uses_auto (decl_specifiers->type))
18777     *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18778
18779   return decl;
18780 }
18781
18782 /* Parse a declarator.
18783
18784    declarator:
18785      direct-declarator
18786      ptr-operator declarator
18787
18788    abstract-declarator:
18789      ptr-operator abstract-declarator [opt]
18790      direct-abstract-declarator
18791
18792    GNU Extensions:
18793
18794    declarator:
18795      attributes [opt] direct-declarator
18796      attributes [opt] ptr-operator declarator
18797
18798    abstract-declarator:
18799      attributes [opt] ptr-operator abstract-declarator [opt]
18800      attributes [opt] direct-abstract-declarator
18801
18802    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18803    detect constructor, destructor or conversion operators. It is set
18804    to -1 if the declarator is a name, and +1 if it is a
18805    function. Otherwise it is set to zero. Usually you just want to
18806    test for >0, but internally the negative value is used.
18807
18808    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18809    a decl-specifier-seq unless it declares a constructor, destructor,
18810    or conversion.  It might seem that we could check this condition in
18811    semantic analysis, rather than parsing, but that makes it difficult
18812    to handle something like `f()'.  We want to notice that there are
18813    no decl-specifiers, and therefore realize that this is an
18814    expression, not a declaration.)
18815
18816    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18817    the declarator is a direct-declarator of the form "(...)".
18818
18819    MEMBER_P is true iff this declarator is a member-declarator.
18820
18821    FRIEND_P is true iff this declarator is a friend.  */
18822
18823 static cp_declarator *
18824 cp_parser_declarator (cp_parser* parser,
18825                       cp_parser_declarator_kind dcl_kind,
18826                       int* ctor_dtor_or_conv_p,
18827                       bool* parenthesized_p,
18828                       bool member_p, bool friend_p)
18829 {
18830   cp_declarator *declarator;
18831   enum tree_code code;
18832   cp_cv_quals cv_quals;
18833   tree class_type;
18834   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18835
18836   /* Assume this is not a constructor, destructor, or type-conversion
18837      operator.  */
18838   if (ctor_dtor_or_conv_p)
18839     *ctor_dtor_or_conv_p = 0;
18840
18841   if (cp_parser_allow_gnu_extensions_p (parser))
18842     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18843
18844   /* Check for the ptr-operator production.  */
18845   cp_parser_parse_tentatively (parser);
18846   /* Parse the ptr-operator.  */
18847   code = cp_parser_ptr_operator (parser,
18848                                  &class_type,
18849                                  &cv_quals,
18850                                  &std_attributes);
18851
18852   /* If that worked, then we have a ptr-operator.  */
18853   if (cp_parser_parse_definitely (parser))
18854     {
18855       /* If a ptr-operator was found, then this declarator was not
18856          parenthesized.  */
18857       if (parenthesized_p)
18858         *parenthesized_p = true;
18859       /* The dependent declarator is optional if we are parsing an
18860          abstract-declarator.  */
18861       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18862         cp_parser_parse_tentatively (parser);
18863
18864       /* Parse the dependent declarator.  */
18865       declarator = cp_parser_declarator (parser, dcl_kind,
18866                                          /*ctor_dtor_or_conv_p=*/NULL,
18867                                          /*parenthesized_p=*/NULL,
18868                                          /*member_p=*/false,
18869                                          friend_p);
18870
18871       /* If we are parsing an abstract-declarator, we must handle the
18872          case where the dependent declarator is absent.  */
18873       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18874           && !cp_parser_parse_definitely (parser))
18875         declarator = NULL;
18876
18877       declarator = cp_parser_make_indirect_declarator
18878         (code, class_type, cv_quals, declarator, std_attributes);
18879     }
18880   /* Everything else is a direct-declarator.  */
18881   else
18882     {
18883       if (parenthesized_p)
18884         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18885                                                    CPP_OPEN_PAREN);
18886       declarator = cp_parser_direct_declarator (parser, dcl_kind,
18887                                                 ctor_dtor_or_conv_p,
18888                                                 member_p, friend_p);
18889     }
18890
18891   if (gnu_attributes && declarator && declarator != cp_error_declarator)
18892     declarator->attributes = gnu_attributes;
18893   return declarator;
18894 }
18895
18896 /* Parse a direct-declarator or direct-abstract-declarator.
18897
18898    direct-declarator:
18899      declarator-id
18900      direct-declarator ( parameter-declaration-clause )
18901        cv-qualifier-seq [opt]
18902        ref-qualifier [opt]
18903        exception-specification [opt]
18904      direct-declarator [ constant-expression [opt] ]
18905      ( declarator )
18906
18907    direct-abstract-declarator:
18908      direct-abstract-declarator [opt]
18909        ( parameter-declaration-clause )
18910        cv-qualifier-seq [opt]
18911        ref-qualifier [opt]
18912        exception-specification [opt]
18913      direct-abstract-declarator [opt] [ constant-expression [opt] ]
18914      ( abstract-declarator )
18915
18916    Returns a representation of the declarator.  DCL_KIND is
18917    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18918    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
18919    we are parsing a direct-declarator.  It is
18920    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18921    of ambiguity we prefer an abstract declarator, as per
18922    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18923    as for cp_parser_declarator.  */
18924
18925 static cp_declarator *
18926 cp_parser_direct_declarator (cp_parser* parser,
18927                              cp_parser_declarator_kind dcl_kind,
18928                              int* ctor_dtor_or_conv_p,
18929                              bool member_p, bool friend_p)
18930 {
18931   cp_token *token;
18932   cp_declarator *declarator = NULL;
18933   tree scope = NULL_TREE;
18934   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18935   bool saved_in_declarator_p = parser->in_declarator_p;
18936   bool first = true;
18937   tree pushed_scope = NULL_TREE;
18938
18939   while (true)
18940     {
18941       /* Peek at the next token.  */
18942       token = cp_lexer_peek_token (parser->lexer);
18943       if (token->type == CPP_OPEN_PAREN)
18944         {
18945           /* This is either a parameter-declaration-clause, or a
18946              parenthesized declarator. When we know we are parsing a
18947              named declarator, it must be a parenthesized declarator
18948              if FIRST is true. For instance, `(int)' is a
18949              parameter-declaration-clause, with an omitted
18950              direct-abstract-declarator. But `((*))', is a
18951              parenthesized abstract declarator. Finally, when T is a
18952              template parameter `(T)' is a
18953              parameter-declaration-clause, and not a parenthesized
18954              named declarator.
18955
18956              We first try and parse a parameter-declaration-clause,
18957              and then try a nested declarator (if FIRST is true).
18958
18959              It is not an error for it not to be a
18960              parameter-declaration-clause, even when FIRST is
18961              false. Consider,
18962
18963                int i (int);
18964                int i (3);
18965
18966              The first is the declaration of a function while the
18967              second is the definition of a variable, including its
18968              initializer.
18969
18970              Having seen only the parenthesis, we cannot know which of
18971              these two alternatives should be selected.  Even more
18972              complex are examples like:
18973
18974                int i (int (a));
18975                int i (int (3));
18976
18977              The former is a function-declaration; the latter is a
18978              variable initialization.
18979
18980              Thus again, we try a parameter-declaration-clause, and if
18981              that fails, we back out and return.  */
18982
18983           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18984             {
18985               tree params;
18986               bool is_declarator = false;
18987
18988               /* In a member-declarator, the only valid interpretation
18989                  of a parenthesis is the start of a
18990                  parameter-declaration-clause.  (It is invalid to
18991                  initialize a static data member with a parenthesized
18992                  initializer; only the "=" form of initialization is
18993                  permitted.)  */
18994               if (!member_p)
18995                 cp_parser_parse_tentatively (parser);
18996
18997               /* Consume the `('.  */
18998               cp_lexer_consume_token (parser->lexer);
18999               if (first)
19000                 {
19001                   /* If this is going to be an abstract declarator, we're
19002                      in a declarator and we can't have default args.  */
19003                   parser->default_arg_ok_p = false;
19004                   parser->in_declarator_p = true;
19005                 }
19006
19007               begin_scope (sk_function_parms, NULL_TREE);
19008
19009               /* Parse the parameter-declaration-clause.  */
19010               params = cp_parser_parameter_declaration_clause (parser);
19011
19012               /* Consume the `)'.  */
19013               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19014
19015               /* If all went well, parse the cv-qualifier-seq,
19016                  ref-qualifier and the exception-specification.  */
19017               if (member_p || cp_parser_parse_definitely (parser))
19018                 {
19019                   cp_cv_quals cv_quals;
19020                   cp_virt_specifiers virt_specifiers;
19021                   cp_ref_qualifier ref_qual;
19022                   tree exception_specification;
19023                   tree late_return;
19024                   tree attrs;
19025                   bool memfn = (member_p || (pushed_scope
19026                                              && CLASS_TYPE_P (pushed_scope)));
19027
19028                   is_declarator = true;
19029
19030                   if (ctor_dtor_or_conv_p)
19031                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19032                   first = false;
19033
19034                   /* Parse the cv-qualifier-seq.  */
19035                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19036                   /* Parse the ref-qualifier. */
19037                   ref_qual = cp_parser_ref_qualifier_opt (parser);
19038                   /* Parse the tx-qualifier.  */
19039                   tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19040                   /* And the exception-specification.  */
19041                   exception_specification
19042                     = cp_parser_exception_specification_opt (parser);
19043
19044                   attrs = cp_parser_std_attribute_spec_seq (parser);
19045
19046                   /* In here, we handle cases where attribute is used after
19047                      the function declaration.  For example:
19048                      void func (int x) __attribute__((vector(..)));  */
19049                   tree gnu_attrs = NULL_TREE;
19050                   if (flag_cilkplus
19051                       && cp_next_tokens_can_be_gnu_attribute_p (parser))
19052                     {
19053                       cp_parser_parse_tentatively (parser);
19054                       tree attr = cp_parser_gnu_attributes_opt (parser);
19055                       if (cp_lexer_next_token_is_not (parser->lexer,
19056                                                       CPP_SEMICOLON)
19057                           && cp_lexer_next_token_is_not (parser->lexer,
19058                                                          CPP_OPEN_BRACE))
19059                         cp_parser_abort_tentative_parse (parser);
19060                       else if (!cp_parser_parse_definitely (parser))
19061                         ;
19062                       else
19063                         gnu_attrs = attr;
19064                     }
19065                   tree requires_clause = NULL_TREE;
19066                   late_return = (cp_parser_late_return_type_opt
19067                                  (parser, declarator, requires_clause,
19068                                   memfn ? cv_quals : -1));
19069
19070                   /* Parse the virt-specifier-seq.  */
19071                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19072
19073                   /* Create the function-declarator.  */
19074                   declarator = make_call_declarator (declarator,
19075                                                      params,
19076                                                      cv_quals,
19077                                                      virt_specifiers,
19078                                                      ref_qual,
19079                                                      tx_qual,
19080                                                      exception_specification,
19081                                                      late_return,
19082                                                      requires_clause);
19083                   declarator->std_attributes = attrs;
19084                   declarator->attributes = gnu_attrs;
19085                   /* Any subsequent parameter lists are to do with
19086                      return type, so are not those of the declared
19087                      function.  */
19088                   parser->default_arg_ok_p = false;
19089                 }
19090
19091               /* Remove the function parms from scope.  */
19092               pop_bindings_and_leave_scope ();
19093
19094               if (is_declarator)
19095                 /* Repeat the main loop.  */
19096                 continue;
19097             }
19098
19099           /* If this is the first, we can try a parenthesized
19100              declarator.  */
19101           if (first)
19102             {
19103               bool saved_in_type_id_in_expr_p;
19104
19105               parser->default_arg_ok_p = saved_default_arg_ok_p;
19106               parser->in_declarator_p = saved_in_declarator_p;
19107
19108               /* Consume the `('.  */
19109               cp_lexer_consume_token (parser->lexer);
19110               /* Parse the nested declarator.  */
19111               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19112               parser->in_type_id_in_expr_p = true;
19113               declarator
19114                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19115                                         /*parenthesized_p=*/NULL,
19116                                         member_p, friend_p);
19117               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19118               first = false;
19119               /* Expect a `)'.  */
19120               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19121                 declarator = cp_error_declarator;
19122               if (declarator == cp_error_declarator)
19123                 break;
19124
19125               goto handle_declarator;
19126             }
19127           /* Otherwise, we must be done.  */
19128           else
19129             break;
19130         }
19131       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19132                && token->type == CPP_OPEN_SQUARE
19133                && !cp_next_tokens_can_be_attribute_p (parser))
19134         {
19135           /* Parse an array-declarator.  */
19136           tree bounds, attrs;
19137
19138           if (ctor_dtor_or_conv_p)
19139             *ctor_dtor_or_conv_p = 0;
19140
19141           first = false;
19142           parser->default_arg_ok_p = false;
19143           parser->in_declarator_p = true;
19144           /* Consume the `['.  */
19145           cp_lexer_consume_token (parser->lexer);
19146           /* Peek at the next token.  */
19147           token = cp_lexer_peek_token (parser->lexer);
19148           /* If the next token is `]', then there is no
19149              constant-expression.  */
19150           if (token->type != CPP_CLOSE_SQUARE)
19151             {
19152               bool non_constant_p;
19153               bounds
19154                 = cp_parser_constant_expression (parser,
19155                                                  /*allow_non_constant=*/true,
19156                                                  &non_constant_p);
19157               if (!non_constant_p)
19158                 /* OK */;
19159               else if (error_operand_p (bounds))
19160                 /* Already gave an error.  */;
19161               else if (!parser->in_function_body
19162                        || current_binding_level->kind == sk_function_parms)
19163                 {
19164                   /* Normally, the array bound must be an integral constant
19165                      expression.  However, as an extension, we allow VLAs
19166                      in function scopes as long as they aren't part of a
19167                      parameter declaration.  */
19168                   cp_parser_error (parser,
19169                                    "array bound is not an integer constant");
19170                   bounds = error_mark_node;
19171                 }
19172               else if (processing_template_decl
19173                        && !type_dependent_expression_p (bounds))
19174                 {
19175                   /* Remember this wasn't a constant-expression.  */
19176                   bounds = build_nop (TREE_TYPE (bounds), bounds);
19177                   TREE_SIDE_EFFECTS (bounds) = 1;
19178                 }
19179             }
19180           else
19181             bounds = NULL_TREE;
19182           /* Look for the closing `]'.  */
19183           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19184             {
19185               declarator = cp_error_declarator;
19186               break;
19187             }
19188
19189           attrs = cp_parser_std_attribute_spec_seq (parser);
19190           declarator = make_array_declarator (declarator, bounds);
19191           declarator->std_attributes = attrs;
19192         }
19193       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19194         {
19195           {
19196             tree qualifying_scope;
19197             tree unqualified_name;
19198             tree attrs;
19199             special_function_kind sfk;
19200             bool abstract_ok;
19201             bool pack_expansion_p = false;
19202             cp_token *declarator_id_start_token;
19203
19204             /* Parse a declarator-id */
19205             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19206             if (abstract_ok)
19207               {
19208                 cp_parser_parse_tentatively (parser);
19209
19210                 /* If we see an ellipsis, we should be looking at a
19211                    parameter pack. */
19212                 if (token->type == CPP_ELLIPSIS)
19213                   {
19214                     /* Consume the `...' */
19215                     cp_lexer_consume_token (parser->lexer);
19216
19217                     pack_expansion_p = true;
19218                   }
19219               }
19220
19221             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19222             unqualified_name
19223               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19224             qualifying_scope = parser->scope;
19225             if (abstract_ok)
19226               {
19227                 bool okay = false;
19228
19229                 if (!unqualified_name && pack_expansion_p)
19230                   {
19231                     /* Check whether an error occurred. */
19232                     okay = !cp_parser_error_occurred (parser);
19233
19234                     /* We already consumed the ellipsis to mark a
19235                        parameter pack, but we have no way to report it,
19236                        so abort the tentative parse. We will be exiting
19237                        immediately anyway. */
19238                     cp_parser_abort_tentative_parse (parser);
19239                   }
19240                 else
19241                   okay = cp_parser_parse_definitely (parser);
19242
19243                 if (!okay)
19244                   unqualified_name = error_mark_node;
19245                 else if (unqualified_name
19246                          && (qualifying_scope
19247                              || (!identifier_p (unqualified_name))))
19248                   {
19249                     cp_parser_error (parser, "expected unqualified-id");
19250                     unqualified_name = error_mark_node;
19251                   }
19252               }
19253
19254             if (!unqualified_name)
19255               return NULL;
19256             if (unqualified_name == error_mark_node)
19257               {
19258                 declarator = cp_error_declarator;
19259                 pack_expansion_p = false;
19260                 declarator->parameter_pack_p = false;
19261                 break;
19262               }
19263
19264             attrs = cp_parser_std_attribute_spec_seq (parser);
19265
19266             if (qualifying_scope && at_namespace_scope_p ()
19267                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19268               {
19269                 /* In the declaration of a member of a template class
19270                    outside of the class itself, the SCOPE will sometimes
19271                    be a TYPENAME_TYPE.  For example, given:
19272
19273                    template <typename T>
19274                    int S<T>::R::i = 3;
19275
19276                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
19277                    this context, we must resolve S<T>::R to an ordinary
19278                    type, rather than a typename type.
19279
19280                    The reason we normally avoid resolving TYPENAME_TYPEs
19281                    is that a specialization of `S' might render
19282                    `S<T>::R' not a type.  However, if `S' is
19283                    specialized, then this `i' will not be used, so there
19284                    is no harm in resolving the types here.  */
19285                 tree type;
19286
19287                 /* Resolve the TYPENAME_TYPE.  */
19288                 type = resolve_typename_type (qualifying_scope,
19289                                               /*only_current_p=*/false);
19290                 /* If that failed, the declarator is invalid.  */
19291                 if (TREE_CODE (type) == TYPENAME_TYPE)
19292                   {
19293                     if (typedef_variant_p (type))
19294                       error_at (declarator_id_start_token->location,
19295                                 "cannot define member of dependent typedef "
19296                                 "%qT", type);
19297                     else
19298                       error_at (declarator_id_start_token->location,
19299                                 "%<%T::%E%> is not a type",
19300                                 TYPE_CONTEXT (qualifying_scope),
19301                                 TYPE_IDENTIFIER (qualifying_scope));
19302                   }
19303                 qualifying_scope = type;
19304               }
19305
19306             sfk = sfk_none;
19307
19308             if (unqualified_name)
19309               {
19310                 tree class_type;
19311
19312                 if (qualifying_scope
19313                     && CLASS_TYPE_P (qualifying_scope))
19314                   class_type = qualifying_scope;
19315                 else
19316                   class_type = current_class_type;
19317
19318                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19319                   {
19320                     tree name_type = TREE_TYPE (unqualified_name);
19321                     if (class_type && same_type_p (name_type, class_type))
19322                       {
19323                         if (qualifying_scope
19324                             && CLASSTYPE_USE_TEMPLATE (name_type))
19325                           {
19326                             error_at (declarator_id_start_token->location,
19327                                       "invalid use of constructor as a template");
19328                             inform (declarator_id_start_token->location,
19329                                     "use %<%T::%D%> instead of %<%T::%D%> to "
19330                                     "name the constructor in a qualified name",
19331                                     class_type,
19332                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19333                                     class_type, name_type);
19334                             declarator = cp_error_declarator;
19335                             break;
19336                           }
19337                         else
19338                           unqualified_name = constructor_name (class_type);
19339                       }
19340                     else
19341                       {
19342                         /* We do not attempt to print the declarator
19343                            here because we do not have enough
19344                            information about its original syntactic
19345                            form.  */
19346                         cp_parser_error (parser, "invalid declarator");
19347                         declarator = cp_error_declarator;
19348                         break;
19349                       }
19350                   }
19351
19352                 if (class_type)
19353                   {
19354                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19355                       sfk = sfk_destructor;
19356                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19357                       sfk = sfk_conversion;
19358                     else if (/* There's no way to declare a constructor
19359                                 for an anonymous type, even if the type
19360                                 got a name for linkage purposes.  */
19361                              !TYPE_WAS_ANONYMOUS (class_type)
19362                              /* Handle correctly (c++/19200):
19363
19364                                 struct S {
19365                                   struct T{};
19366                                   friend void S(T);
19367                                 };
19368
19369                                 and also:
19370
19371                                 namespace N {
19372                                   void S();
19373                                 }
19374
19375                                 struct S {
19376                                   friend void N::S();
19377                                 };  */
19378                              && !(friend_p
19379                                   && class_type != qualifying_scope)
19380                              && constructor_name_p (unqualified_name,
19381                                                     class_type))
19382                       {
19383                         unqualified_name = constructor_name (class_type);
19384                         sfk = sfk_constructor;
19385                       }
19386                     else if (is_overloaded_fn (unqualified_name)
19387                              && DECL_CONSTRUCTOR_P (get_first_fn
19388                                                     (unqualified_name)))
19389                       sfk = sfk_constructor;
19390
19391                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
19392                       *ctor_dtor_or_conv_p = -1;
19393                   }
19394               }
19395             declarator = make_id_declarator (qualifying_scope,
19396                                              unqualified_name,
19397                                              sfk);
19398             declarator->std_attributes = attrs;
19399             declarator->id_loc = token->location;
19400             declarator->parameter_pack_p = pack_expansion_p;
19401
19402             if (pack_expansion_p)
19403               maybe_warn_variadic_templates ();
19404           }
19405
19406         handle_declarator:;
19407           scope = get_scope_of_declarator (declarator);
19408           if (scope)
19409             {
19410               /* Any names that appear after the declarator-id for a
19411                  member are looked up in the containing scope.  */
19412               if (at_function_scope_p ())
19413                 {
19414                   /* But declarations with qualified-ids can't appear in a
19415                      function.  */
19416                   cp_parser_error (parser, "qualified-id in declaration");
19417                   declarator = cp_error_declarator;
19418                   break;
19419                 }
19420               pushed_scope = push_scope (scope);
19421             }
19422           parser->in_declarator_p = true;
19423           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19424               || (declarator && declarator->kind == cdk_id))
19425             /* Default args are only allowed on function
19426                declarations.  */
19427             parser->default_arg_ok_p = saved_default_arg_ok_p;
19428           else
19429             parser->default_arg_ok_p = false;
19430
19431           first = false;
19432         }
19433       /* We're done.  */
19434       else
19435         break;
19436     }
19437
19438   /* For an abstract declarator, we might wind up with nothing at this
19439      point.  That's an error; the declarator is not optional.  */
19440   if (!declarator)
19441     cp_parser_error (parser, "expected declarator");
19442
19443   /* If we entered a scope, we must exit it now.  */
19444   if (pushed_scope)
19445     pop_scope (pushed_scope);
19446
19447   parser->default_arg_ok_p = saved_default_arg_ok_p;
19448   parser->in_declarator_p = saved_in_declarator_p;
19449
19450   return declarator;
19451 }
19452
19453 /* Parse a ptr-operator.
19454
19455    ptr-operator:
19456      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19457      * cv-qualifier-seq [opt]
19458      &
19459      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19460      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19461
19462    GNU Extension:
19463
19464    ptr-operator:
19465      & cv-qualifier-seq [opt]
19466
19467    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19468    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19469    an rvalue reference. In the case of a pointer-to-member, *TYPE is
19470    filled in with the TYPE containing the member.  *CV_QUALS is
19471    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19472    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
19473    Note that the tree codes returned by this function have nothing
19474    to do with the types of trees that will be eventually be created
19475    to represent the pointer or reference type being parsed. They are
19476    just constants with suggestive names. */
19477 static enum tree_code
19478 cp_parser_ptr_operator (cp_parser* parser,
19479                         tree* type,
19480                         cp_cv_quals *cv_quals,
19481                         tree *attributes)
19482 {
19483   enum tree_code code = ERROR_MARK;
19484   cp_token *token;
19485   tree attrs = NULL_TREE;
19486
19487   /* Assume that it's not a pointer-to-member.  */
19488   *type = NULL_TREE;
19489   /* And that there are no cv-qualifiers.  */
19490   *cv_quals = TYPE_UNQUALIFIED;
19491
19492   /* Peek at the next token.  */
19493   token = cp_lexer_peek_token (parser->lexer);
19494
19495   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
19496   if (token->type == CPP_MULT)
19497     code = INDIRECT_REF;
19498   else if (token->type == CPP_AND)
19499     code = ADDR_EXPR;
19500   else if ((cxx_dialect != cxx98) &&
19501            token->type == CPP_AND_AND) /* C++0x only */
19502     code = NON_LVALUE_EXPR;
19503
19504   if (code != ERROR_MARK)
19505     {
19506       /* Consume the `*', `&' or `&&'.  */
19507       cp_lexer_consume_token (parser->lexer);
19508
19509       /* A `*' can be followed by a cv-qualifier-seq, and so can a
19510          `&', if we are allowing GNU extensions.  (The only qualifier
19511          that can legally appear after `&' is `restrict', but that is
19512          enforced during semantic analysis.  */
19513       if (code == INDIRECT_REF
19514           || cp_parser_allow_gnu_extensions_p (parser))
19515         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19516
19517       attrs = cp_parser_std_attribute_spec_seq (parser);
19518       if (attributes != NULL)
19519         *attributes = attrs;
19520     }
19521   else
19522     {
19523       /* Try the pointer-to-member case.  */
19524       cp_parser_parse_tentatively (parser);
19525       /* Look for the optional `::' operator.  */
19526       cp_parser_global_scope_opt (parser,
19527                                   /*current_scope_valid_p=*/false);
19528       /* Look for the nested-name specifier.  */
19529       token = cp_lexer_peek_token (parser->lexer);
19530       cp_parser_nested_name_specifier (parser,
19531                                        /*typename_keyword_p=*/false,
19532                                        /*check_dependency_p=*/true,
19533                                        /*type_p=*/false,
19534                                        /*is_declaration=*/false);
19535       /* If we found it, and the next token is a `*', then we are
19536          indeed looking at a pointer-to-member operator.  */
19537       if (!cp_parser_error_occurred (parser)
19538           && cp_parser_require (parser, CPP_MULT, RT_MULT))
19539         {
19540           /* Indicate that the `*' operator was used.  */
19541           code = INDIRECT_REF;
19542
19543           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19544             error_at (token->location, "%qD is a namespace", parser->scope);
19545           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19546             error_at (token->location, "cannot form pointer to member of "
19547                       "non-class %q#T", parser->scope);
19548           else
19549             {
19550               /* The type of which the member is a member is given by the
19551                  current SCOPE.  */
19552               *type = parser->scope;
19553               /* The next name will not be qualified.  */
19554               parser->scope = NULL_TREE;
19555               parser->qualifying_scope = NULL_TREE;
19556               parser->object_scope = NULL_TREE;
19557               /* Look for optional c++11 attributes.  */
19558               attrs = cp_parser_std_attribute_spec_seq (parser);
19559               if (attributes != NULL)
19560                 *attributes = attrs;
19561               /* Look for the optional cv-qualifier-seq.  */
19562               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19563             }
19564         }
19565       /* If that didn't work we don't have a ptr-operator.  */
19566       if (!cp_parser_parse_definitely (parser))
19567         cp_parser_error (parser, "expected ptr-operator");
19568     }
19569
19570   return code;
19571 }
19572
19573 /* Parse an (optional) cv-qualifier-seq.
19574
19575    cv-qualifier-seq:
19576      cv-qualifier cv-qualifier-seq [opt]
19577
19578    cv-qualifier:
19579      const
19580      volatile
19581
19582    GNU Extension:
19583
19584    cv-qualifier:
19585      __restrict__
19586
19587    Returns a bitmask representing the cv-qualifiers.  */
19588
19589 static cp_cv_quals
19590 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19591 {
19592   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19593
19594   while (true)
19595     {
19596       cp_token *token;
19597       cp_cv_quals cv_qualifier;
19598
19599       /* Peek at the next token.  */
19600       token = cp_lexer_peek_token (parser->lexer);
19601       /* See if it's a cv-qualifier.  */
19602       switch (token->keyword)
19603         {
19604         case RID_CONST:
19605           cv_qualifier = TYPE_QUAL_CONST;
19606           break;
19607
19608         case RID_VOLATILE:
19609           cv_qualifier = TYPE_QUAL_VOLATILE;
19610           break;
19611
19612         case RID_RESTRICT:
19613           cv_qualifier = TYPE_QUAL_RESTRICT;
19614           break;
19615
19616         default:
19617           cv_qualifier = TYPE_UNQUALIFIED;
19618           break;
19619         }
19620
19621       if (!cv_qualifier)
19622         break;
19623
19624       if (cv_quals & cv_qualifier)
19625         {
19626           error_at (token->location, "duplicate cv-qualifier");
19627           cp_lexer_purge_token (parser->lexer);
19628         }
19629       else
19630         {
19631           cp_lexer_consume_token (parser->lexer);
19632           cv_quals |= cv_qualifier;
19633         }
19634     }
19635
19636   return cv_quals;
19637 }
19638
19639 /* Parse an (optional) ref-qualifier
19640
19641    ref-qualifier:
19642      &
19643      &&
19644
19645    Returns cp_ref_qualifier representing ref-qualifier. */
19646
19647 static cp_ref_qualifier
19648 cp_parser_ref_qualifier_opt (cp_parser* parser)
19649 {
19650   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19651
19652   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
19653   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19654     return ref_qual;
19655
19656   while (true)
19657     {
19658       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19659       cp_token *token = cp_lexer_peek_token (parser->lexer);
19660
19661       switch (token->type)
19662         {
19663         case CPP_AND:
19664           curr_ref_qual = REF_QUAL_LVALUE;
19665           break;
19666
19667         case CPP_AND_AND:
19668           curr_ref_qual = REF_QUAL_RVALUE;
19669           break;
19670
19671         default:
19672           curr_ref_qual = REF_QUAL_NONE;
19673           break;
19674         }
19675
19676       if (!curr_ref_qual)
19677         break;
19678       else if (ref_qual)
19679         {
19680           error_at (token->location, "multiple ref-qualifiers");
19681           cp_lexer_purge_token (parser->lexer);
19682         }
19683       else
19684         {
19685           ref_qual = curr_ref_qual;
19686           cp_lexer_consume_token (parser->lexer);
19687         }
19688     }
19689
19690   return ref_qual;
19691 }
19692
19693 /* Parse an optional tx-qualifier.
19694
19695    tx-qualifier:
19696      transaction_safe
19697      transaction_safe_dynamic  */
19698
19699 static tree
19700 cp_parser_tx_qualifier_opt (cp_parser *parser)
19701 {
19702   cp_token *token = cp_lexer_peek_token (parser->lexer);
19703   if (token->type == CPP_NAME)
19704     {
19705       tree name = token->u.value;
19706       const char *p = IDENTIFIER_POINTER (name);
19707       const int len = strlen ("transaction_safe");
19708       if (!strncmp (p, "transaction_safe", len))
19709         {
19710           p += len;
19711           if (*p == '\0'
19712               || !strcmp (p, "_dynamic"))
19713             {
19714               cp_lexer_consume_token (parser->lexer);
19715               if (!flag_tm)
19716                 {
19717                   error ("%E requires %<-fgnu-tm%>", name);
19718                   return NULL_TREE;
19719                 }
19720               else
19721                 return name;
19722             }
19723         }
19724     }
19725   return NULL_TREE;
19726 }
19727
19728 /* Parse an (optional) virt-specifier-seq.
19729
19730    virt-specifier-seq:
19731      virt-specifier virt-specifier-seq [opt]
19732
19733    virt-specifier:
19734      override
19735      final
19736
19737    Returns a bitmask representing the virt-specifiers.  */
19738
19739 static cp_virt_specifiers
19740 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19741 {
19742   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19743
19744   while (true)
19745     {
19746       cp_token *token;
19747       cp_virt_specifiers virt_specifier;
19748
19749       /* Peek at the next token.  */
19750       token = cp_lexer_peek_token (parser->lexer);
19751       /* See if it's a virt-specifier-qualifier.  */
19752       if (token->type != CPP_NAME)
19753         break;
19754       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19755         {
19756           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19757           virt_specifier = VIRT_SPEC_OVERRIDE;
19758         }
19759       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19760         {
19761           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19762           virt_specifier = VIRT_SPEC_FINAL;
19763         }
19764       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19765         {
19766           virt_specifier = VIRT_SPEC_FINAL;
19767         }
19768       else
19769         break;
19770
19771       if (virt_specifiers & virt_specifier)
19772         {
19773           error_at (token->location, "duplicate virt-specifier");
19774           cp_lexer_purge_token (parser->lexer);
19775         }
19776       else
19777         {
19778           cp_lexer_consume_token (parser->lexer);
19779           virt_specifiers |= virt_specifier;
19780         }
19781     }
19782   return virt_specifiers;
19783 }
19784
19785 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19786    is in scope even though it isn't real.  */
19787
19788 void
19789 inject_this_parameter (tree ctype, cp_cv_quals quals)
19790 {
19791   tree this_parm;
19792
19793   if (current_class_ptr)
19794     {
19795       /* We don't clear this between NSDMIs.  Is it already what we want?  */
19796       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19797       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19798           && cp_type_quals (type) == quals)
19799         return;
19800     }
19801
19802   this_parm = build_this_parm (ctype, quals);
19803   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
19804   current_class_ptr = NULL_TREE;
19805   current_class_ref
19806     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19807   current_class_ptr = this_parm;
19808 }
19809
19810 /* Return true iff our current scope is a non-static data member
19811    initializer.  */
19812
19813 bool
19814 parsing_nsdmi (void)
19815 {
19816   /* We recognize NSDMI context by the context-less 'this' pointer set up
19817      by the function above.  */
19818   if (current_class_ptr
19819       && TREE_CODE (current_class_ptr) == PARM_DECL
19820       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19821     return true;
19822   return false;
19823 }
19824
19825 /* Parse a late-specified return type, if any.  This is not a separate
19826    non-terminal, but part of a function declarator, which looks like
19827
19828    -> trailing-type-specifier-seq abstract-declarator(opt)
19829
19830    Returns the type indicated by the type-id.
19831
19832    In addition to this, parse any queued up omp declare simd
19833    clauses and Cilk Plus SIMD-enabled function's vector attributes.
19834
19835    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19836    function.  */
19837
19838 static tree
19839 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19840                                 tree& requires_clause, cp_cv_quals quals)
19841 {
19842   cp_token *token;
19843   tree type = NULL_TREE;
19844   bool declare_simd_p = (parser->omp_declare_simd
19845                          && declarator
19846                          && declarator->kind == cdk_id);
19847
19848   bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info 
19849                                 && declarator && declarator->kind == cdk_id);
19850
19851   bool oacc_routine_p = (parser->oacc_routine
19852                          && declarator
19853                          && declarator->kind == cdk_id);
19854
19855   /* Peek at the next token.  */
19856   token = cp_lexer_peek_token (parser->lexer);
19857   /* A late-specified return type is indicated by an initial '->'. */
19858   if (token->type != CPP_DEREF
19859       && token->keyword != RID_REQUIRES
19860       && !(token->type == CPP_NAME
19861            && token->u.value == ridpointers[RID_REQUIRES])
19862       && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19863     return NULL_TREE;
19864
19865   tree save_ccp = current_class_ptr;
19866   tree save_ccr = current_class_ref;
19867   if (quals >= 0)
19868     {
19869       /* DR 1207: 'this' is in scope in the trailing return type.  */
19870       inject_this_parameter (current_class_type, quals);
19871     }
19872
19873   if (token->type == CPP_DEREF)
19874     {
19875       /* Consume the ->.  */
19876       cp_lexer_consume_token (parser->lexer);
19877
19878       type = cp_parser_trailing_type_id (parser);
19879     }
19880
19881   /* Function declarations may be followed by a trailing
19882      requires-clause.  */
19883   requires_clause = cp_parser_requires_clause_opt (parser);
19884
19885   if (cilk_simd_fn_vector_p)
19886     declarator->attributes
19887       = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19888                                                   declarator->attributes);
19889   if (declare_simd_p)
19890     declarator->attributes
19891       = cp_parser_late_parsing_omp_declare_simd (parser,
19892                                                  declarator->attributes);
19893   if (oacc_routine_p)
19894     declarator->attributes
19895       = cp_parser_late_parsing_oacc_routine (parser,
19896                                              declarator->attributes);
19897
19898   if (quals >= 0)
19899     {
19900       current_class_ptr = save_ccp;
19901       current_class_ref = save_ccr;
19902     }
19903
19904   return type;
19905 }
19906
19907 /* Parse a declarator-id.
19908
19909    declarator-id:
19910      id-expression
19911      :: [opt] nested-name-specifier [opt] type-name
19912
19913    In the `id-expression' case, the value returned is as for
19914    cp_parser_id_expression if the id-expression was an unqualified-id.
19915    If the id-expression was a qualified-id, then a SCOPE_REF is
19916    returned.  The first operand is the scope (either a NAMESPACE_DECL
19917    or TREE_TYPE), but the second is still just a representation of an
19918    unqualified-id.  */
19919
19920 static tree
19921 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19922 {
19923   tree id;
19924   /* The expression must be an id-expression.  Assume that qualified
19925      names are the names of types so that:
19926
19927        template <class T>
19928        int S<T>::R::i = 3;
19929
19930      will work; we must treat `S<T>::R' as the name of a type.
19931      Similarly, assume that qualified names are templates, where
19932      required, so that:
19933
19934        template <class T>
19935        int S<T>::R<T>::i = 3;
19936
19937      will work, too.  */
19938   id = cp_parser_id_expression (parser,
19939                                 /*template_keyword_p=*/false,
19940                                 /*check_dependency_p=*/false,
19941                                 /*template_p=*/NULL,
19942                                 /*declarator_p=*/true,
19943                                 optional_p);
19944   if (id && BASELINK_P (id))
19945     id = BASELINK_FUNCTIONS (id);
19946   return id;
19947 }
19948
19949 /* Parse a type-id.
19950
19951    type-id:
19952      type-specifier-seq abstract-declarator [opt]
19953
19954    Returns the TYPE specified.  */
19955
19956 static tree
19957 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19958                      bool is_trailing_return)
19959 {
19960   cp_decl_specifier_seq type_specifier_seq;
19961   cp_declarator *abstract_declarator;
19962
19963   /* Parse the type-specifier-seq.  */
19964   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19965                                 is_trailing_return,
19966                                 &type_specifier_seq);
19967   if (type_specifier_seq.type == error_mark_node)
19968     return error_mark_node;
19969
19970   /* There might or might not be an abstract declarator.  */
19971   cp_parser_parse_tentatively (parser);
19972   /* Look for the declarator.  */
19973   abstract_declarator
19974     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19975                             /*parenthesized_p=*/NULL,
19976                             /*member_p=*/false,
19977                             /*friend_p=*/false);
19978   /* Check to see if there really was a declarator.  */
19979   if (!cp_parser_parse_definitely (parser))
19980     abstract_declarator = NULL;
19981
19982   if (type_specifier_seq.type
19983       /* The concepts TS allows 'auto' as a type-id.  */
19984       && (!flag_concepts || parser->in_type_id_in_expr_p)
19985       /* None of the valid uses of 'auto' in C++14 involve the type-id
19986          nonterminal, but it is valid in a trailing-return-type.  */
19987       && !(cxx_dialect >= cxx14 && is_trailing_return)
19988       && type_uses_auto (type_specifier_seq.type))
19989     {
19990       /* A type-id with type 'auto' is only ok if the abstract declarator
19991          is a function declarator with a late-specified return type.
19992
19993          A type-id with 'auto' is also valid in a trailing-return-type
19994          in a compound-requirement. */
19995       if (abstract_declarator
19996           && abstract_declarator->kind == cdk_function
19997           && abstract_declarator->u.function.late_return_type)
19998         /* OK */;
19999       else if (parser->in_result_type_constraint_p)
20000         /* OK */;
20001       else
20002         {
20003           error ("invalid use of %<auto%>");
20004           return error_mark_node;
20005         }
20006     }
20007   
20008   return groktypename (&type_specifier_seq, abstract_declarator,
20009                        is_template_arg);
20010 }
20011
20012 static tree
20013 cp_parser_type_id (cp_parser *parser)
20014 {
20015   return cp_parser_type_id_1 (parser, false, false);
20016 }
20017
20018 static tree
20019 cp_parser_template_type_arg (cp_parser *parser)
20020 {
20021   tree r;
20022   const char *saved_message = parser->type_definition_forbidden_message;
20023   parser->type_definition_forbidden_message
20024     = G_("types may not be defined in template arguments");
20025   r = cp_parser_type_id_1 (parser, true, false);
20026   parser->type_definition_forbidden_message = saved_message;
20027   if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20028     {
20029       error ("invalid use of %<auto%> in template argument");
20030       r = error_mark_node;
20031     }
20032   return r;
20033 }
20034
20035 static tree
20036 cp_parser_trailing_type_id (cp_parser *parser)
20037 {
20038   return cp_parser_type_id_1 (parser, false, true);
20039 }
20040
20041 /* Parse a type-specifier-seq.
20042
20043    type-specifier-seq:
20044      type-specifier type-specifier-seq [opt]
20045
20046    GNU extension:
20047
20048    type-specifier-seq:
20049      attributes type-specifier-seq [opt]
20050
20051    If IS_DECLARATION is true, we are at the start of a "condition" or
20052    exception-declaration, so we might be followed by a declarator-id.
20053
20054    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20055    i.e. we've just seen "->".
20056
20057    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
20058
20059 static void
20060 cp_parser_type_specifier_seq (cp_parser* parser,
20061                               bool is_declaration,
20062                               bool is_trailing_return,
20063                               cp_decl_specifier_seq *type_specifier_seq)
20064 {
20065   bool seen_type_specifier = false;
20066   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20067   cp_token *start_token = NULL;
20068
20069   /* Clear the TYPE_SPECIFIER_SEQ.  */
20070   clear_decl_specs (type_specifier_seq);
20071
20072   /* In the context of a trailing return type, enum E { } is an
20073      elaborated-type-specifier followed by a function-body, not an
20074      enum-specifier.  */
20075   if (is_trailing_return)
20076     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20077
20078   /* Parse the type-specifiers and attributes.  */
20079   while (true)
20080     {
20081       tree type_specifier;
20082       bool is_cv_qualifier;
20083
20084       /* Check for attributes first.  */
20085       if (cp_next_tokens_can_be_attribute_p (parser))
20086         {
20087           type_specifier_seq->attributes =
20088             chainon (type_specifier_seq->attributes,
20089                      cp_parser_attributes_opt (parser));
20090           continue;
20091         }
20092
20093       /* record the token of the beginning of the type specifier seq,
20094          for error reporting purposes*/
20095      if (!start_token)
20096        start_token = cp_lexer_peek_token (parser->lexer);
20097
20098       /* Look for the type-specifier.  */
20099       type_specifier = cp_parser_type_specifier (parser,
20100                                                  flags,
20101                                                  type_specifier_seq,
20102                                                  /*is_declaration=*/false,
20103                                                  NULL,
20104                                                  &is_cv_qualifier);
20105       if (!type_specifier)
20106         {
20107           /* If the first type-specifier could not be found, this is not a
20108              type-specifier-seq at all.  */
20109           if (!seen_type_specifier)
20110             {
20111               /* Set in_declarator_p to avoid skipping to the semicolon.  */
20112               int in_decl = parser->in_declarator_p;
20113               parser->in_declarator_p = true;
20114
20115               if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20116                   || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20117                 cp_parser_error (parser, "expected type-specifier");
20118
20119               parser->in_declarator_p = in_decl;
20120
20121               type_specifier_seq->type = error_mark_node;
20122               return;
20123             }
20124           /* If subsequent type-specifiers could not be found, the
20125              type-specifier-seq is complete.  */
20126           break;
20127         }
20128
20129       seen_type_specifier = true;
20130       /* The standard says that a condition can be:
20131
20132             type-specifier-seq declarator = assignment-expression
20133
20134          However, given:
20135
20136            struct S {};
20137            if (int S = ...)
20138
20139          we should treat the "S" as a declarator, not as a
20140          type-specifier.  The standard doesn't say that explicitly for
20141          type-specifier-seq, but it does say that for
20142          decl-specifier-seq in an ordinary declaration.  Perhaps it
20143          would be clearer just to allow a decl-specifier-seq here, and
20144          then add a semantic restriction that if any decl-specifiers
20145          that are not type-specifiers appear, the program is invalid.  */
20146       if (is_declaration && !is_cv_qualifier)
20147         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20148     }
20149 }
20150
20151 /* Return whether the function currently being declared has an associated
20152    template parameter list.  */
20153
20154 static bool
20155 function_being_declared_is_template_p (cp_parser* parser)
20156 {
20157   if (!current_template_parms || processing_template_parmlist)
20158     return false;
20159
20160   if (parser->implicit_template_scope)
20161     return true;
20162
20163   if (at_class_scope_p ()
20164       && TYPE_BEING_DEFINED (current_class_type))
20165     return parser->num_template_parameter_lists != 0;
20166
20167   return ((int) parser->num_template_parameter_lists > template_class_depth
20168           (current_class_type));
20169 }
20170
20171 /* Parse a parameter-declaration-clause.
20172
20173    parameter-declaration-clause:
20174      parameter-declaration-list [opt] ... [opt]
20175      parameter-declaration-list , ...
20176
20177    Returns a representation for the parameter declarations.  A return
20178    value of NULL indicates a parameter-declaration-clause consisting
20179    only of an ellipsis.  */
20180
20181 static tree
20182 cp_parser_parameter_declaration_clause (cp_parser* parser)
20183 {
20184   tree parameters;
20185   cp_token *token;
20186   bool ellipsis_p;
20187   bool is_error;
20188
20189   struct cleanup {
20190     cp_parser* parser;
20191     int auto_is_implicit_function_template_parm_p;
20192     ~cleanup() {
20193       parser->auto_is_implicit_function_template_parm_p
20194         = auto_is_implicit_function_template_parm_p;
20195     }
20196   } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20197
20198   (void) cleanup;
20199
20200   if (!processing_specialization
20201       && !processing_template_parmlist
20202       && !processing_explicit_instantiation)
20203     if (!current_function_decl
20204         || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20205       parser->auto_is_implicit_function_template_parm_p = true;
20206
20207   /* Peek at the next token.  */
20208   token = cp_lexer_peek_token (parser->lexer);
20209   /* Check for trivial parameter-declaration-clauses.  */
20210   if (token->type == CPP_ELLIPSIS)
20211     {
20212       /* Consume the `...' token.  */
20213       cp_lexer_consume_token (parser->lexer);
20214       return NULL_TREE;
20215     }
20216   else if (token->type == CPP_CLOSE_PAREN)
20217     /* There are no parameters.  */
20218     {
20219 #ifndef NO_IMPLICIT_EXTERN_C
20220       if (in_system_header_at (input_location)
20221           && current_class_type == NULL
20222           && current_lang_name == lang_name_c)
20223         return NULL_TREE;
20224       else
20225 #endif
20226         return void_list_node;
20227     }
20228   /* Check for `(void)', too, which is a special case.  */
20229   else if (token->keyword == RID_VOID
20230            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20231                == CPP_CLOSE_PAREN))
20232     {
20233       /* Consume the `void' token.  */
20234       cp_lexer_consume_token (parser->lexer);
20235       /* There are no parameters.  */
20236       return void_list_node;
20237     }
20238
20239   /* Parse the parameter-declaration-list.  */
20240   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20241   /* If a parse error occurred while parsing the
20242      parameter-declaration-list, then the entire
20243      parameter-declaration-clause is erroneous.  */
20244   if (is_error)
20245     return NULL;
20246
20247   /* Peek at the next token.  */
20248   token = cp_lexer_peek_token (parser->lexer);
20249   /* If it's a `,', the clause should terminate with an ellipsis.  */
20250   if (token->type == CPP_COMMA)
20251     {
20252       /* Consume the `,'.  */
20253       cp_lexer_consume_token (parser->lexer);
20254       /* Expect an ellipsis.  */
20255       ellipsis_p
20256         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20257     }
20258   /* It might also be `...' if the optional trailing `,' was
20259      omitted.  */
20260   else if (token->type == CPP_ELLIPSIS)
20261     {
20262       /* Consume the `...' token.  */
20263       cp_lexer_consume_token (parser->lexer);
20264       /* And remember that we saw it.  */
20265       ellipsis_p = true;
20266     }
20267   else
20268     ellipsis_p = false;
20269
20270   /* Finish the parameter list.  */
20271   if (!ellipsis_p)
20272     parameters = chainon (parameters, void_list_node);
20273
20274   return parameters;
20275 }
20276
20277 /* Parse a parameter-declaration-list.
20278
20279    parameter-declaration-list:
20280      parameter-declaration
20281      parameter-declaration-list , parameter-declaration
20282
20283    Returns a representation of the parameter-declaration-list, as for
20284    cp_parser_parameter_declaration_clause.  However, the
20285    `void_list_node' is never appended to the list.  Upon return,
20286    *IS_ERROR will be true iff an error occurred.  */
20287
20288 static tree
20289 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20290 {
20291   tree parameters = NULL_TREE;
20292   tree *tail = &parameters;
20293   bool saved_in_unbraced_linkage_specification_p;
20294   int index = 0;
20295
20296   /* Assume all will go well.  */
20297   *is_error = false;
20298   /* The special considerations that apply to a function within an
20299      unbraced linkage specifications do not apply to the parameters
20300      to the function.  */
20301   saved_in_unbraced_linkage_specification_p
20302     = parser->in_unbraced_linkage_specification_p;
20303   parser->in_unbraced_linkage_specification_p = false;
20304
20305   /* Look for more parameters.  */
20306   while (true)
20307     {
20308       cp_parameter_declarator *parameter;
20309       tree decl = error_mark_node;
20310       bool parenthesized_p = false;
20311       int template_parm_idx = (function_being_declared_is_template_p (parser)?
20312                                TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20313                                                 (current_template_parms)) : 0);
20314
20315       /* Parse the parameter.  */
20316       parameter
20317         = cp_parser_parameter_declaration (parser,
20318                                            /*template_parm_p=*/false,
20319                                            &parenthesized_p);
20320
20321       /* We don't know yet if the enclosing context is deprecated, so wait
20322          and warn in grokparms if appropriate.  */
20323       deprecated_state = DEPRECATED_SUPPRESS;
20324
20325       if (parameter)
20326         {
20327           /* If a function parameter pack was specified and an implicit template
20328              parameter was introduced during cp_parser_parameter_declaration,
20329              change any implicit parameters introduced into packs.  */
20330           if (parser->implicit_template_parms
20331               && parameter->declarator
20332               && parameter->declarator->parameter_pack_p)
20333             {
20334               int latest_template_parm_idx = TREE_VEC_LENGTH
20335                 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20336
20337               if (latest_template_parm_idx != template_parm_idx)
20338                 parameter->decl_specifiers.type = convert_generic_types_to_packs
20339                   (parameter->decl_specifiers.type,
20340                    template_parm_idx, latest_template_parm_idx);
20341             }
20342
20343           decl = grokdeclarator (parameter->declarator,
20344                                  &parameter->decl_specifiers,
20345                                  PARM,
20346                                  parameter->default_argument != NULL_TREE,
20347                                  &parameter->decl_specifiers.attributes);
20348         }
20349
20350       deprecated_state = DEPRECATED_NORMAL;
20351
20352       /* If a parse error occurred parsing the parameter declaration,
20353          then the entire parameter-declaration-list is erroneous.  */
20354       if (decl == error_mark_node)
20355         {
20356           *is_error = true;
20357           parameters = error_mark_node;
20358           break;
20359         }
20360
20361       if (parameter->decl_specifiers.attributes)
20362         cplus_decl_attributes (&decl,
20363                                parameter->decl_specifiers.attributes,
20364                                0);
20365       if (DECL_NAME (decl))
20366         decl = pushdecl (decl);
20367
20368       if (decl != error_mark_node)
20369         {
20370           retrofit_lang_decl (decl);
20371           DECL_PARM_INDEX (decl) = ++index;
20372           DECL_PARM_LEVEL (decl) = function_parm_depth ();
20373         }
20374
20375       /* Add the new parameter to the list.  */
20376       *tail = build_tree_list (parameter->default_argument, decl);
20377       tail = &TREE_CHAIN (*tail);
20378
20379       /* Peek at the next token.  */
20380       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20381           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20382           /* These are for Objective-C++ */
20383           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20384           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20385         /* The parameter-declaration-list is complete.  */
20386         break;
20387       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20388         {
20389           cp_token *token;
20390
20391           /* Peek at the next token.  */
20392           token = cp_lexer_peek_nth_token (parser->lexer, 2);
20393           /* If it's an ellipsis, then the list is complete.  */
20394           if (token->type == CPP_ELLIPSIS)
20395             break;
20396           /* Otherwise, there must be more parameters.  Consume the
20397              `,'.  */
20398           cp_lexer_consume_token (parser->lexer);
20399           /* When parsing something like:
20400
20401                 int i(float f, double d)
20402
20403              we can tell after seeing the declaration for "f" that we
20404              are not looking at an initialization of a variable "i",
20405              but rather at the declaration of a function "i".
20406
20407              Due to the fact that the parsing of template arguments
20408              (as specified to a template-id) requires backtracking we
20409              cannot use this technique when inside a template argument
20410              list.  */
20411           if (!parser->in_template_argument_list_p
20412               && !parser->in_type_id_in_expr_p
20413               && cp_parser_uncommitted_to_tentative_parse_p (parser)
20414               /* However, a parameter-declaration of the form
20415                  "float(f)" (which is a valid declaration of a
20416                  parameter "f") can also be interpreted as an
20417                  expression (the conversion of "f" to "float").  */
20418               && !parenthesized_p)
20419             cp_parser_commit_to_tentative_parse (parser);
20420         }
20421       else
20422         {
20423           cp_parser_error (parser, "expected %<,%> or %<...%>");
20424           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20425             cp_parser_skip_to_closing_parenthesis (parser,
20426                                                    /*recovering=*/true,
20427                                                    /*or_comma=*/false,
20428                                                    /*consume_paren=*/false);
20429           break;
20430         }
20431     }
20432
20433   parser->in_unbraced_linkage_specification_p
20434     = saved_in_unbraced_linkage_specification_p;
20435
20436   /* Reset implicit_template_scope if we are about to leave the function
20437      parameter list that introduced it.  Note that for out-of-line member
20438      definitions, there will be one or more class scopes before we get to
20439      the template parameter scope.  */
20440
20441   if (cp_binding_level *its = parser->implicit_template_scope)
20442     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20443       {
20444         while (maybe_its->kind == sk_class)
20445           maybe_its = maybe_its->level_chain;
20446         if (maybe_its == its)
20447           {
20448             parser->implicit_template_parms = 0;
20449             parser->implicit_template_scope = 0;
20450           }
20451       }
20452
20453   return parameters;
20454 }
20455
20456 /* Parse a parameter declaration.
20457
20458    parameter-declaration:
20459      decl-specifier-seq ... [opt] declarator
20460      decl-specifier-seq declarator = assignment-expression
20461      decl-specifier-seq ... [opt] abstract-declarator [opt]
20462      decl-specifier-seq abstract-declarator [opt] = assignment-expression
20463
20464    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20465    declares a template parameter.  (In that case, a non-nested `>'
20466    token encountered during the parsing of the assignment-expression
20467    is not interpreted as a greater-than operator.)
20468
20469    Returns a representation of the parameter, or NULL if an error
20470    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20471    true iff the declarator is of the form "(p)".  */
20472
20473 static cp_parameter_declarator *
20474 cp_parser_parameter_declaration (cp_parser *parser,
20475                                  bool template_parm_p,
20476                                  bool *parenthesized_p)
20477 {
20478   int declares_class_or_enum;
20479   cp_decl_specifier_seq decl_specifiers;
20480   cp_declarator *declarator;
20481   tree default_argument;
20482   cp_token *token = NULL, *declarator_token_start = NULL;
20483   const char *saved_message;
20484   bool template_parameter_pack_p = false;
20485
20486   /* In a template parameter, `>' is not an operator.
20487
20488      [temp.param]
20489
20490      When parsing a default template-argument for a non-type
20491      template-parameter, the first non-nested `>' is taken as the end
20492      of the template parameter-list rather than a greater-than
20493      operator.  */
20494
20495   /* Type definitions may not appear in parameter types.  */
20496   saved_message = parser->type_definition_forbidden_message;
20497   parser->type_definition_forbidden_message
20498     = G_("types may not be defined in parameter types");
20499
20500   /* Parse the declaration-specifiers.  */
20501   cp_parser_decl_specifier_seq (parser,
20502                                 CP_PARSER_FLAGS_NONE,
20503                                 &decl_specifiers,
20504                                 &declares_class_or_enum);
20505
20506   /* Complain about missing 'typename' or other invalid type names.  */
20507   if (!decl_specifiers.any_type_specifiers_p
20508       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20509     decl_specifiers.type = error_mark_node;
20510
20511   /* If an error occurred, there's no reason to attempt to parse the
20512      rest of the declaration.  */
20513   if (cp_parser_error_occurred (parser))
20514     {
20515       parser->type_definition_forbidden_message = saved_message;
20516       return NULL;
20517     }
20518
20519   /* Peek at the next token.  */
20520   token = cp_lexer_peek_token (parser->lexer);
20521
20522   /* If the next token is a `)', `,', `=', `>', or `...', then there
20523      is no declarator. However, when variadic templates are enabled,
20524      there may be a declarator following `...'.  */
20525   if (token->type == CPP_CLOSE_PAREN
20526       || token->type == CPP_COMMA
20527       || token->type == CPP_EQ
20528       || token->type == CPP_GREATER)
20529     {
20530       declarator = NULL;
20531       if (parenthesized_p)
20532         *parenthesized_p = false;
20533     }
20534   /* Otherwise, there should be a declarator.  */
20535   else
20536     {
20537       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20538       parser->default_arg_ok_p = false;
20539
20540       /* After seeing a decl-specifier-seq, if the next token is not a
20541          "(", there is no possibility that the code is a valid
20542          expression.  Therefore, if parsing tentatively, we commit at
20543          this point.  */
20544       if (!parser->in_template_argument_list_p
20545           /* In an expression context, having seen:
20546
20547                (int((char ...
20548
20549              we cannot be sure whether we are looking at a
20550              function-type (taking a "char" as a parameter) or a cast
20551              of some object of type "char" to "int".  */
20552           && !parser->in_type_id_in_expr_p
20553           && cp_parser_uncommitted_to_tentative_parse_p (parser)
20554           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20555           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20556         cp_parser_commit_to_tentative_parse (parser);
20557       /* Parse the declarator.  */
20558       declarator_token_start = token;
20559       declarator = cp_parser_declarator (parser,
20560                                          CP_PARSER_DECLARATOR_EITHER,
20561                                          /*ctor_dtor_or_conv_p=*/NULL,
20562                                          parenthesized_p,
20563                                          /*member_p=*/false,
20564                                          /*friend_p=*/false);
20565       parser->default_arg_ok_p = saved_default_arg_ok_p;
20566       /* After the declarator, allow more attributes.  */
20567       decl_specifiers.attributes
20568         = chainon (decl_specifiers.attributes,
20569                    cp_parser_attributes_opt (parser));
20570
20571       /* If the declarator is a template parameter pack, remember that and
20572          clear the flag in the declarator itself so we don't get errors
20573          from grokdeclarator.  */
20574       if (template_parm_p && declarator && declarator->parameter_pack_p)
20575         {
20576           declarator->parameter_pack_p = false;
20577           template_parameter_pack_p = true;
20578         }
20579     }
20580
20581   /* If the next token is an ellipsis, and we have not seen a declarator
20582      name, and if either the type of the declarator contains parameter
20583      packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20584      for, eg, abbreviated integral type names), then we actually have a
20585      parameter pack expansion expression. Otherwise, leave the ellipsis
20586      for a C-style variadic function. */
20587   token = cp_lexer_peek_token (parser->lexer);
20588   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20589     {
20590       tree type = decl_specifiers.type;
20591
20592       if (type && DECL_P (type))
20593         type = TREE_TYPE (type);
20594
20595       if (((type
20596             && TREE_CODE (type) != TYPE_PACK_EXPANSION
20597             && (template_parm_p || uses_parameter_packs (type)))
20598            || (!type && template_parm_p))
20599           && declarator_can_be_parameter_pack (declarator))
20600         {
20601           /* Consume the `...'. */
20602           cp_lexer_consume_token (parser->lexer);
20603           maybe_warn_variadic_templates ();
20604           
20605           /* Build a pack expansion type */
20606           if (template_parm_p)
20607             template_parameter_pack_p = true;
20608           else if (declarator)
20609             declarator->parameter_pack_p = true;
20610           else
20611             decl_specifiers.type = make_pack_expansion (type);
20612         }
20613     }
20614
20615   /* The restriction on defining new types applies only to the type
20616      of the parameter, not to the default argument.  */
20617   parser->type_definition_forbidden_message = saved_message;
20618
20619   /* If the next token is `=', then process a default argument.  */
20620   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20621     {
20622       tree type = decl_specifiers.type;
20623       token = cp_lexer_peek_token (parser->lexer);
20624       /* If we are defining a class, then the tokens that make up the
20625          default argument must be saved and processed later.  */
20626       if (!template_parm_p && at_class_scope_p ()
20627           && TYPE_BEING_DEFINED (current_class_type)
20628           && !LAMBDA_TYPE_P (current_class_type))
20629         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20630
20631       // A constrained-type-specifier may declare a type template-parameter.
20632       else if (declares_constrained_type_template_parameter (type))
20633         default_argument
20634           = cp_parser_default_type_template_argument (parser);
20635
20636       // A constrained-type-specifier may declare a template-template-parameter.
20637       else if (declares_constrained_template_template_parameter (type))
20638         default_argument
20639           = cp_parser_default_template_template_argument (parser);
20640
20641       /* Outside of a class definition, we can just parse the
20642          assignment-expression.  */
20643       else
20644         default_argument
20645           = cp_parser_default_argument (parser, template_parm_p);
20646
20647       if (!parser->default_arg_ok_p)
20648         {
20649           permerror (token->location,
20650                      "default arguments are only "
20651                      "permitted for function parameters");
20652         }
20653       else if ((declarator && declarator->parameter_pack_p)
20654                || template_parameter_pack_p
20655                || (decl_specifiers.type
20656                    && PACK_EXPANSION_P (decl_specifiers.type)))
20657         {
20658           /* Find the name of the parameter pack.  */     
20659           cp_declarator *id_declarator = declarator;
20660           while (id_declarator && id_declarator->kind != cdk_id)
20661             id_declarator = id_declarator->declarator;
20662           
20663           if (id_declarator && id_declarator->kind == cdk_id)
20664             error_at (declarator_token_start->location,
20665                       template_parm_p
20666                       ? G_("template parameter pack %qD "
20667                            "cannot have a default argument")
20668                       : G_("parameter pack %qD cannot have "
20669                            "a default argument"),
20670                       id_declarator->u.id.unqualified_name);
20671           else
20672             error_at (declarator_token_start->location,
20673                       template_parm_p
20674                       ? G_("template parameter pack cannot have "
20675                            "a default argument")
20676                       : G_("parameter pack cannot have a "
20677                            "default argument"));
20678
20679           default_argument = NULL_TREE;
20680         }
20681     }
20682   else
20683     default_argument = NULL_TREE;
20684
20685   return make_parameter_declarator (&decl_specifiers,
20686                                     declarator,
20687                                     default_argument,
20688                                     template_parameter_pack_p);
20689 }
20690
20691 /* Parse a default argument and return it.
20692
20693    TEMPLATE_PARM_P is true if this is a default argument for a
20694    non-type template parameter.  */
20695 static tree
20696 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20697 {
20698   tree default_argument = NULL_TREE;
20699   bool saved_greater_than_is_operator_p;
20700   bool saved_local_variables_forbidden_p;
20701   bool non_constant_p, is_direct_init;
20702
20703   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20704      set correctly.  */
20705   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20706   parser->greater_than_is_operator_p = !template_parm_p;
20707   /* Local variable names (and the `this' keyword) may not
20708      appear in a default argument.  */
20709   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20710   parser->local_variables_forbidden_p = true;
20711   /* Parse the assignment-expression.  */
20712   if (template_parm_p)
20713     push_deferring_access_checks (dk_no_deferred);
20714   tree saved_class_ptr = NULL_TREE;
20715   tree saved_class_ref = NULL_TREE;
20716   /* The "this" pointer is not valid in a default argument.  */
20717   if (cfun)
20718     {
20719       saved_class_ptr = current_class_ptr;
20720       cp_function_chain->x_current_class_ptr = NULL_TREE;
20721       saved_class_ref = current_class_ref;
20722       cp_function_chain->x_current_class_ref = NULL_TREE;
20723     }
20724   default_argument
20725     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20726   /* Restore the "this" pointer.  */
20727   if (cfun)
20728     {
20729       cp_function_chain->x_current_class_ptr = saved_class_ptr;
20730       cp_function_chain->x_current_class_ref = saved_class_ref;
20731     }
20732   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20733     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20734   if (template_parm_p)
20735     pop_deferring_access_checks ();
20736   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20737   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20738
20739   return default_argument;
20740 }
20741
20742 /* Parse a function-body.
20743
20744    function-body:
20745      compound_statement  */
20746
20747 static void
20748 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20749 {
20750   cp_parser_compound_statement (parser, NULL, (in_function_try_block
20751                                                ? BCS_TRY_BLOCK : BCS_NORMAL),
20752                                 true);
20753 }
20754
20755 /* Parse a ctor-initializer-opt followed by a function-body.  Return
20756    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
20757    is true we are parsing a function-try-block.  */
20758
20759 static bool
20760 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20761                                                   bool in_function_try_block)
20762 {
20763   tree body, list;
20764   bool ctor_initializer_p;
20765   const bool check_body_p =
20766      DECL_CONSTRUCTOR_P (current_function_decl)
20767      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20768   tree last = NULL;
20769
20770   /* Begin the function body.  */
20771   body = begin_function_body ();
20772   /* Parse the optional ctor-initializer.  */
20773   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20774
20775   /* If we're parsing a constexpr constructor definition, we need
20776      to check that the constructor body is indeed empty.  However,
20777      before we get to cp_parser_function_body lot of junk has been
20778      generated, so we can't just check that we have an empty block.
20779      Rather we take a snapshot of the outermost block, and check whether
20780      cp_parser_function_body changed its state.  */
20781   if (check_body_p)
20782     {
20783       list = cur_stmt_list;
20784       if (STATEMENT_LIST_TAIL (list))
20785         last = STATEMENT_LIST_TAIL (list)->stmt;
20786     }
20787   /* Parse the function-body.  */
20788   cp_parser_function_body (parser, in_function_try_block);
20789   if (check_body_p)
20790     check_constexpr_ctor_body (last, list, /*complain=*/true);
20791   /* Finish the function body.  */
20792   finish_function_body (body);
20793
20794   return ctor_initializer_p;
20795 }
20796
20797 /* Parse an initializer.
20798
20799    initializer:
20800      = initializer-clause
20801      ( expression-list )
20802
20803    Returns an expression representing the initializer.  If no
20804    initializer is present, NULL_TREE is returned.
20805
20806    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20807    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
20808    set to TRUE if there is no initializer present.  If there is an
20809    initializer, and it is not a constant-expression, *NON_CONSTANT_P
20810    is set to true; otherwise it is set to false.  */
20811
20812 static tree
20813 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20814                        bool* non_constant_p)
20815 {
20816   cp_token *token;
20817   tree init;
20818
20819   /* Peek at the next token.  */
20820   token = cp_lexer_peek_token (parser->lexer);
20821
20822   /* Let our caller know whether or not this initializer was
20823      parenthesized.  */
20824   *is_direct_init = (token->type != CPP_EQ);
20825   /* Assume that the initializer is constant.  */
20826   *non_constant_p = false;
20827
20828   if (token->type == CPP_EQ)
20829     {
20830       /* Consume the `='.  */
20831       cp_lexer_consume_token (parser->lexer);
20832       /* Parse the initializer-clause.  */
20833       init = cp_parser_initializer_clause (parser, non_constant_p);
20834     }
20835   else if (token->type == CPP_OPEN_PAREN)
20836     {
20837       vec<tree, va_gc> *vec;
20838       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20839                                                      /*cast_p=*/false,
20840                                                      /*allow_expansion_p=*/true,
20841                                                      non_constant_p);
20842       if (vec == NULL)
20843         return error_mark_node;
20844       init = build_tree_list_vec (vec);
20845       release_tree_vector (vec);
20846     }
20847   else if (token->type == CPP_OPEN_BRACE)
20848     {
20849       cp_lexer_set_source_position (parser->lexer);
20850       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20851       init = cp_parser_braced_list (parser, non_constant_p);
20852       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20853     }
20854   else
20855     {
20856       /* Anything else is an error.  */
20857       cp_parser_error (parser, "expected initializer");
20858       init = error_mark_node;
20859     }
20860
20861   return init;
20862 }
20863
20864 /* Parse an initializer-clause.
20865
20866    initializer-clause:
20867      assignment-expression
20868      braced-init-list
20869
20870    Returns an expression representing the initializer.
20871
20872    If the `assignment-expression' production is used the value
20873    returned is simply a representation for the expression.
20874
20875    Otherwise, calls cp_parser_braced_list.  */
20876
20877 static cp_expr
20878 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20879 {
20880   cp_expr initializer;
20881
20882   /* Assume the expression is constant.  */
20883   *non_constant_p = false;
20884
20885   /* If it is not a `{', then we are looking at an
20886      assignment-expression.  */
20887   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20888     {
20889       initializer
20890         = cp_parser_constant_expression (parser,
20891                                         /*allow_non_constant_p=*/true,
20892                                         non_constant_p);
20893     }
20894   else
20895     initializer = cp_parser_braced_list (parser, non_constant_p);
20896
20897   return initializer;
20898 }
20899
20900 /* Parse a brace-enclosed initializer list.
20901
20902    braced-init-list:
20903      { initializer-list , [opt] }
20904      { }
20905
20906    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
20907    the elements of the initializer-list (or NULL, if the last
20908    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
20909    NULL_TREE.  There is no way to detect whether or not the optional
20910    trailing `,' was provided.  NON_CONSTANT_P is as for
20911    cp_parser_initializer.  */     
20912
20913 static cp_expr
20914 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20915 {
20916   tree initializer;
20917   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20918
20919   /* Consume the `{' token.  */
20920   cp_lexer_consume_token (parser->lexer);
20921   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
20922   initializer = make_node (CONSTRUCTOR);
20923   /* If it's not a `}', then there is a non-trivial initializer.  */
20924   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20925     {
20926       /* Parse the initializer list.  */
20927       CONSTRUCTOR_ELTS (initializer)
20928         = cp_parser_initializer_list (parser, non_constant_p);
20929       /* A trailing `,' token is allowed.  */
20930       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20931         cp_lexer_consume_token (parser->lexer);
20932     }
20933   else
20934     *non_constant_p = false;
20935   /* Now, there should be a trailing `}'.  */
20936   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20937   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20938   TREE_TYPE (initializer) = init_list_type_node;
20939
20940   cp_expr result (initializer);
20941   /* Build a location of the form:
20942        { ... }
20943        ^~~~~~~
20944      with caret==start at the open brace, finish at the close brace.  */
20945   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20946   result.set_location (combined_loc);
20947   return result;
20948 }
20949
20950 /* Consume tokens up to, and including, the next non-nested closing `]'.
20951    Returns true iff we found a closing `]'.  */
20952
20953 static bool
20954 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20955 {
20956   unsigned square_depth = 0;
20957
20958   while (true)
20959     {
20960       cp_token * token = cp_lexer_peek_token (parser->lexer);
20961
20962       switch (token->type)
20963         {
20964         case CPP_EOF:
20965         case CPP_PRAGMA_EOL:
20966           /* If we've run out of tokens, then there is no closing `]'.  */
20967           return false;
20968
20969         case CPP_OPEN_SQUARE:
20970           ++square_depth;
20971           break;
20972
20973         case CPP_CLOSE_SQUARE:
20974           if (!square_depth--)
20975             {
20976               cp_lexer_consume_token (parser->lexer);
20977               return true;
20978             }
20979           break;
20980
20981         default:
20982           break;
20983         }
20984
20985       /* Consume the token.  */
20986       cp_lexer_consume_token (parser->lexer);
20987     }
20988 }
20989
20990 /* Return true if we are looking at an array-designator, false otherwise.  */
20991
20992 static bool
20993 cp_parser_array_designator_p (cp_parser *parser)
20994 {
20995   /* Consume the `['.  */
20996   cp_lexer_consume_token (parser->lexer);
20997
20998   cp_lexer_save_tokens (parser->lexer);
20999
21000   /* Skip tokens until the next token is a closing square bracket.
21001      If we find the closing `]', and the next token is a `=', then
21002      we are looking at an array designator.  */
21003   bool array_designator_p
21004     = (cp_parser_skip_to_closing_square_bracket (parser)
21005        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21006   
21007   /* Roll back the tokens we skipped.  */
21008   cp_lexer_rollback_tokens (parser->lexer);
21009
21010   return array_designator_p;
21011 }
21012
21013 /* Parse an initializer-list.
21014
21015    initializer-list:
21016      initializer-clause ... [opt]
21017      initializer-list , initializer-clause ... [opt]
21018
21019    GNU Extension:
21020
21021    initializer-list:
21022      designation initializer-clause ...[opt]
21023      initializer-list , designation initializer-clause ...[opt]
21024
21025    designation:
21026      . identifier =
21027      identifier :
21028      [ constant-expression ] =
21029
21030    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
21031    for the initializer.  If the INDEX of the elt is non-NULL, it is the
21032    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
21033    as for cp_parser_initializer.  */
21034
21035 static vec<constructor_elt, va_gc> *
21036 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21037 {
21038   vec<constructor_elt, va_gc> *v = NULL;
21039
21040   /* Assume all of the expressions are constant.  */
21041   *non_constant_p = false;
21042
21043   /* Parse the rest of the list.  */
21044   while (true)
21045     {
21046       cp_token *token;
21047       tree designator;
21048       tree initializer;
21049       bool clause_non_constant_p;
21050
21051       /* If the next token is an identifier and the following one is a
21052          colon, we are looking at the GNU designated-initializer
21053          syntax.  */
21054       if (cp_parser_allow_gnu_extensions_p (parser)
21055           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21056           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21057         {
21058           /* Warn the user that they are using an extension.  */
21059           pedwarn (input_location, OPT_Wpedantic, 
21060                    "ISO C++ does not allow designated initializers");
21061           /* Consume the identifier.  */
21062           designator = cp_lexer_consume_token (parser->lexer)->u.value;
21063           /* Consume the `:'.  */
21064           cp_lexer_consume_token (parser->lexer);
21065         }
21066       /* Also handle the C99 syntax, '. id ='.  */
21067       else if (cp_parser_allow_gnu_extensions_p (parser)
21068                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21069                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21070                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21071         {
21072           /* Warn the user that they are using an extension.  */
21073           pedwarn (input_location, OPT_Wpedantic,
21074                    "ISO C++ does not allow C99 designated initializers");
21075           /* Consume the `.'.  */
21076           cp_lexer_consume_token (parser->lexer);
21077           /* Consume the identifier.  */
21078           designator = cp_lexer_consume_token (parser->lexer)->u.value;
21079           /* Consume the `='.  */
21080           cp_lexer_consume_token (parser->lexer);
21081         }
21082       /* Also handle C99 array designators, '[ const ] ='.  */
21083       else if (cp_parser_allow_gnu_extensions_p (parser)
21084                && !c_dialect_objc ()
21085                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21086         {
21087           /* In C++11, [ could start a lambda-introducer.  */
21088           bool non_const = false;
21089
21090           cp_parser_parse_tentatively (parser);
21091
21092           if (!cp_parser_array_designator_p (parser))
21093             {
21094               cp_parser_simulate_error (parser);
21095               designator = NULL_TREE;
21096             }
21097           else
21098             {
21099               designator = cp_parser_constant_expression (parser, true,
21100                                                           &non_const);
21101               cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21102               cp_parser_require (parser, CPP_EQ, RT_EQ);
21103             }
21104
21105           if (!cp_parser_parse_definitely (parser))
21106             designator = NULL_TREE;
21107           else if (non_const)
21108             require_potential_rvalue_constant_expression (designator);
21109         }
21110       else
21111         designator = NULL_TREE;
21112
21113       /* Parse the initializer.  */
21114       initializer = cp_parser_initializer_clause (parser,
21115                                                   &clause_non_constant_p);
21116       /* If any clause is non-constant, so is the entire initializer.  */
21117       if (clause_non_constant_p)
21118         *non_constant_p = true;
21119
21120       /* If we have an ellipsis, this is an initializer pack
21121          expansion.  */
21122       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21123         {
21124           /* Consume the `...'.  */
21125           cp_lexer_consume_token (parser->lexer);
21126
21127           /* Turn the initializer into an initializer expansion.  */
21128           initializer = make_pack_expansion (initializer);
21129         }
21130
21131       /* Add it to the vector.  */
21132       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21133
21134       /* If the next token is not a comma, we have reached the end of
21135          the list.  */
21136       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21137         break;
21138
21139       /* Peek at the next token.  */
21140       token = cp_lexer_peek_nth_token (parser->lexer, 2);
21141       /* If the next token is a `}', then we're still done.  An
21142          initializer-clause can have a trailing `,' after the
21143          initializer-list and before the closing `}'.  */
21144       if (token->type == CPP_CLOSE_BRACE)
21145         break;
21146
21147       /* Consume the `,' token.  */
21148       cp_lexer_consume_token (parser->lexer);
21149     }
21150
21151   return v;
21152 }
21153
21154 /* Classes [gram.class] */
21155
21156 /* Parse a class-name.
21157
21158    class-name:
21159      identifier
21160      template-id
21161
21162    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21163    to indicate that names looked up in dependent types should be
21164    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
21165    keyword has been used to indicate that the name that appears next
21166    is a template.  TAG_TYPE indicates the explicit tag given before
21167    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
21168    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
21169    is the class being defined in a class-head.  If ENUM_OK is TRUE,
21170    enum-names are also accepted.
21171
21172    Returns the TYPE_DECL representing the class.  */
21173
21174 static tree
21175 cp_parser_class_name (cp_parser *parser,
21176                       bool typename_keyword_p,
21177                       bool template_keyword_p,
21178                       enum tag_types tag_type,
21179                       bool check_dependency_p,
21180                       bool class_head_p,
21181                       bool is_declaration,
21182                       bool enum_ok)
21183 {
21184   tree decl;
21185   tree scope;
21186   bool typename_p;
21187   cp_token *token;
21188   tree identifier = NULL_TREE;
21189
21190   /* All class-names start with an identifier.  */
21191   token = cp_lexer_peek_token (parser->lexer);
21192   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21193     {
21194       cp_parser_error (parser, "expected class-name");
21195       return error_mark_node;
21196     }
21197
21198   /* PARSER->SCOPE can be cleared when parsing the template-arguments
21199      to a template-id, so we save it here.  */
21200   scope = parser->scope;
21201   if (scope == error_mark_node)
21202     return error_mark_node;
21203
21204   /* Any name names a type if we're following the `typename' keyword
21205      in a qualified name where the enclosing scope is type-dependent.  */
21206   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21207                 && dependent_type_p (scope));
21208   /* Handle the common case (an identifier, but not a template-id)
21209      efficiently.  */
21210   if (token->type == CPP_NAME
21211       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21212     {
21213       cp_token *identifier_token;
21214       bool ambiguous_p;
21215
21216       /* Look for the identifier.  */
21217       identifier_token = cp_lexer_peek_token (parser->lexer);
21218       ambiguous_p = identifier_token->error_reported;
21219       identifier = cp_parser_identifier (parser);
21220       /* If the next token isn't an identifier, we are certainly not
21221          looking at a class-name.  */
21222       if (identifier == error_mark_node)
21223         decl = error_mark_node;
21224       /* If we know this is a type-name, there's no need to look it
21225          up.  */
21226       else if (typename_p)
21227         decl = identifier;
21228       else
21229         {
21230           tree ambiguous_decls;
21231           /* If we already know that this lookup is ambiguous, then
21232              we've already issued an error message; there's no reason
21233              to check again.  */
21234           if (ambiguous_p)
21235             {
21236               cp_parser_simulate_error (parser);
21237               return error_mark_node;
21238             }
21239           /* If the next token is a `::', then the name must be a type
21240              name.
21241
21242              [basic.lookup.qual]
21243
21244              During the lookup for a name preceding the :: scope
21245              resolution operator, object, function, and enumerator
21246              names are ignored.  */
21247           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21248             tag_type = scope_type;
21249           /* Look up the name.  */
21250           decl = cp_parser_lookup_name (parser, identifier,
21251                                         tag_type,
21252                                         /*is_template=*/false,
21253                                         /*is_namespace=*/false,
21254                                         check_dependency_p,
21255                                         &ambiguous_decls,
21256                                         identifier_token->location);
21257           if (ambiguous_decls)
21258             {
21259               if (cp_parser_parsing_tentatively (parser))
21260                 cp_parser_simulate_error (parser);
21261               return error_mark_node;
21262             }
21263         }
21264     }
21265   else
21266     {
21267       /* Try a template-id.  */
21268       decl = cp_parser_template_id (parser, template_keyword_p,
21269                                     check_dependency_p,
21270                                     tag_type,
21271                                     is_declaration);
21272       if (decl == error_mark_node)
21273         return error_mark_node;
21274     }
21275
21276   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21277
21278   /* If this is a typename, create a TYPENAME_TYPE.  */
21279   if (typename_p && decl != error_mark_node)
21280     {
21281       decl = make_typename_type (scope, decl, typename_type,
21282                                  /*complain=*/tf_error);
21283       if (decl != error_mark_node)
21284         decl = TYPE_NAME (decl);
21285     }
21286
21287   decl = strip_using_decl (decl);
21288
21289   /* Check to see that it is really the name of a class.  */
21290   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21291       && identifier_p (TREE_OPERAND (decl, 0))
21292       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21293     /* Situations like this:
21294
21295          template <typename T> struct A {
21296            typename T::template X<int>::I i;
21297          };
21298
21299        are problematic.  Is `T::template X<int>' a class-name?  The
21300        standard does not seem to be definitive, but there is no other
21301        valid interpretation of the following `::'.  Therefore, those
21302        names are considered class-names.  */
21303     {
21304       decl = make_typename_type (scope, decl, tag_type, tf_error);
21305       if (decl != error_mark_node)
21306         decl = TYPE_NAME (decl);
21307     }
21308   else if (TREE_CODE (decl) != TYPE_DECL
21309            || TREE_TYPE (decl) == error_mark_node
21310            || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21311                 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21312            /* In Objective-C 2.0, a classname followed by '.' starts a
21313               dot-syntax expression, and it's not a type-name.  */
21314            || (c_dialect_objc ()
21315                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
21316                && objc_is_class_name (decl)))
21317     decl = error_mark_node;
21318
21319   if (decl == error_mark_node)
21320     cp_parser_error (parser, "expected class-name");
21321   else if (identifier && !parser->scope)
21322     maybe_note_name_used_in_class (identifier, decl);
21323
21324   return decl;
21325 }
21326
21327 /* Parse a class-specifier.
21328
21329    class-specifier:
21330      class-head { member-specification [opt] }
21331
21332    Returns the TREE_TYPE representing the class.  */
21333
21334 static tree
21335 cp_parser_class_specifier_1 (cp_parser* parser)
21336 {
21337   tree type;
21338   tree attributes = NULL_TREE;
21339   bool nested_name_specifier_p;
21340   unsigned saved_num_template_parameter_lists;
21341   bool saved_in_function_body;
21342   unsigned char in_statement;
21343   bool in_switch_statement_p;
21344   bool saved_in_unbraced_linkage_specification_p;
21345   tree old_scope = NULL_TREE;
21346   tree scope = NULL_TREE;
21347   cp_token *closing_brace;
21348
21349   push_deferring_access_checks (dk_no_deferred);
21350
21351   /* Parse the class-head.  */
21352   type = cp_parser_class_head (parser,
21353                                &nested_name_specifier_p);
21354   /* If the class-head was a semantic disaster, skip the entire body
21355      of the class.  */
21356   if (!type)
21357     {
21358       cp_parser_skip_to_end_of_block_or_statement (parser);
21359       pop_deferring_access_checks ();
21360       return error_mark_node;
21361     }
21362
21363   /* Look for the `{'.  */
21364   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21365     {
21366       pop_deferring_access_checks ();
21367       return error_mark_node;
21368     }
21369
21370   cp_ensure_no_omp_declare_simd (parser);
21371   cp_ensure_no_oacc_routine (parser);
21372
21373   /* Issue an error message if type-definitions are forbidden here.  */
21374   cp_parser_check_type_definition (parser);
21375   /* Remember that we are defining one more class.  */
21376   ++parser->num_classes_being_defined;
21377   /* Inside the class, surrounding template-parameter-lists do not
21378      apply.  */
21379   saved_num_template_parameter_lists
21380     = parser->num_template_parameter_lists;
21381   parser->num_template_parameter_lists = 0;
21382   /* We are not in a function body.  */
21383   saved_in_function_body = parser->in_function_body;
21384   parser->in_function_body = false;
21385   /* Or in a loop.  */
21386   in_statement = parser->in_statement;
21387   parser->in_statement = 0;
21388   /* Or in a switch.  */
21389   in_switch_statement_p = parser->in_switch_statement_p;
21390   parser->in_switch_statement_p = false;
21391   /* We are not immediately inside an extern "lang" block.  */
21392   saved_in_unbraced_linkage_specification_p
21393     = parser->in_unbraced_linkage_specification_p;
21394   parser->in_unbraced_linkage_specification_p = false;
21395
21396   // Associate constraints with the type.
21397   if (flag_concepts)
21398     type = associate_classtype_constraints (type);
21399
21400   /* Start the class.  */
21401   if (nested_name_specifier_p)
21402     {
21403       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21404       old_scope = push_inner_scope (scope);
21405     }
21406   type = begin_class_definition (type);
21407
21408   if (type == error_mark_node)
21409     /* If the type is erroneous, skip the entire body of the class.  */
21410     cp_parser_skip_to_closing_brace (parser);
21411   else
21412     /* Parse the member-specification.  */
21413     cp_parser_member_specification_opt (parser);
21414
21415   /* Look for the trailing `}'.  */
21416   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21417   /* Look for trailing attributes to apply to this class.  */
21418   if (cp_parser_allow_gnu_extensions_p (parser))
21419     attributes = cp_parser_gnu_attributes_opt (parser);
21420   if (type != error_mark_node)
21421     type = finish_struct (type, attributes);
21422   if (nested_name_specifier_p)
21423     pop_inner_scope (old_scope, scope);
21424
21425   /* We've finished a type definition.  Check for the common syntax
21426      error of forgetting a semicolon after the definition.  We need to
21427      be careful, as we can't just check for not-a-semicolon and be done
21428      with it; the user might have typed:
21429
21430      class X { } c = ...;
21431      class X { } *p = ...;
21432
21433      and so forth.  Instead, enumerate all the possible tokens that
21434      might follow this production; if we don't see one of them, then
21435      complain and silently insert the semicolon.  */
21436   {
21437     cp_token *token = cp_lexer_peek_token (parser->lexer);
21438     bool want_semicolon = true;
21439
21440     if (cp_next_tokens_can_be_std_attribute_p (parser))
21441       /* Don't try to parse c++11 attributes here.  As per the
21442          grammar, that should be a task for
21443          cp_parser_decl_specifier_seq.  */
21444       want_semicolon = false;
21445
21446     switch (token->type)
21447       {
21448       case CPP_NAME:
21449       case CPP_SEMICOLON:
21450       case CPP_MULT:
21451       case CPP_AND:
21452       case CPP_OPEN_PAREN:
21453       case CPP_CLOSE_PAREN:
21454       case CPP_COMMA:
21455         want_semicolon = false;
21456         break;
21457
21458         /* While it's legal for type qualifiers and storage class
21459            specifiers to follow type definitions in the grammar, only
21460            compiler testsuites contain code like that.  Assume that if
21461            we see such code, then what we're really seeing is a case
21462            like:
21463
21464            class X { }
21465            const <type> var = ...;
21466
21467            or
21468
21469            class Y { }
21470            static <type> func (...) ...
21471
21472            i.e. the qualifier or specifier applies to the next
21473            declaration.  To do so, however, we need to look ahead one
21474            more token to see if *that* token is a type specifier.
21475
21476            This code could be improved to handle:
21477
21478            class Z { }
21479            static const <type> var = ...;  */
21480       case CPP_KEYWORD:
21481         if (keyword_is_decl_specifier (token->keyword))
21482           {
21483             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21484
21485             /* Handling user-defined types here would be nice, but very
21486                tricky.  */
21487             want_semicolon
21488               = (lookahead->type == CPP_KEYWORD
21489                  && keyword_begins_type_specifier (lookahead->keyword));
21490           }
21491         break;
21492       default:
21493         break;
21494       }
21495
21496     /* If we don't have a type, then something is very wrong and we
21497        shouldn't try to do anything clever.  Likewise for not seeing the
21498        closing brace.  */
21499     if (closing_brace && TYPE_P (type) && want_semicolon)
21500       {
21501         cp_token_position prev
21502           = cp_lexer_previous_token_position (parser->lexer);
21503         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21504         location_t loc = prev_token->location;
21505
21506         if (CLASSTYPE_DECLARED_CLASS (type))
21507           error_at (loc, "expected %<;%> after class definition");
21508         else if (TREE_CODE (type) == RECORD_TYPE)
21509           error_at (loc, "expected %<;%> after struct definition");
21510         else if (TREE_CODE (type) == UNION_TYPE)
21511           error_at (loc, "expected %<;%> after union definition");
21512         else
21513           gcc_unreachable ();
21514
21515         /* Unget one token and smash it to look as though we encountered
21516            a semicolon in the input stream.  */
21517         cp_lexer_set_token_position (parser->lexer, prev);
21518         token = cp_lexer_peek_token (parser->lexer);
21519         token->type = CPP_SEMICOLON;
21520         token->keyword = RID_MAX;
21521       }
21522   }
21523
21524   /* If this class is not itself within the scope of another class,
21525      then we need to parse the bodies of all of the queued function
21526      definitions.  Note that the queued functions defined in a class
21527      are not always processed immediately following the
21528      class-specifier for that class.  Consider:
21529
21530        struct A {
21531          struct B { void f() { sizeof (A); } };
21532        };
21533
21534      If `f' were processed before the processing of `A' were
21535      completed, there would be no way to compute the size of `A'.
21536      Note that the nesting we are interested in here is lexical --
21537      not the semantic nesting given by TYPE_CONTEXT.  In particular,
21538      for:
21539
21540        struct A { struct B; };
21541        struct A::B { void f() { } };
21542
21543      there is no need to delay the parsing of `A::B::f'.  */
21544   if (--parser->num_classes_being_defined == 0)
21545     {
21546       tree decl;
21547       tree class_type = NULL_TREE;
21548       tree pushed_scope = NULL_TREE;
21549       unsigned ix;
21550       cp_default_arg_entry *e;
21551       tree save_ccp, save_ccr;
21552
21553       /* In a first pass, parse default arguments to the functions.
21554          Then, in a second pass, parse the bodies of the functions.
21555          This two-phased approach handles cases like:
21556
21557             struct S {
21558               void f() { g(); }
21559               void g(int i = 3);
21560             };
21561
21562          */
21563       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21564         {
21565           decl = e->decl;
21566           /* If there are default arguments that have not yet been processed,
21567              take care of them now.  */
21568           if (class_type != e->class_type)
21569             {
21570               if (pushed_scope)
21571                 pop_scope (pushed_scope);
21572               class_type = e->class_type;
21573               pushed_scope = push_scope (class_type);
21574             }
21575           /* Make sure that any template parameters are in scope.  */
21576           maybe_begin_member_template_processing (decl);
21577           /* Parse the default argument expressions.  */
21578           cp_parser_late_parsing_default_args (parser, decl);
21579           /* Remove any template parameters from the symbol table.  */
21580           maybe_end_member_template_processing ();
21581         }
21582       vec_safe_truncate (unparsed_funs_with_default_args, 0);
21583       /* Now parse any NSDMIs.  */
21584       save_ccp = current_class_ptr;
21585       save_ccr = current_class_ref;
21586       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21587         {
21588           if (class_type != DECL_CONTEXT (decl))
21589             {
21590               if (pushed_scope)
21591                 pop_scope (pushed_scope);
21592               class_type = DECL_CONTEXT (decl);
21593               pushed_scope = push_scope (class_type);
21594             }
21595           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21596           cp_parser_late_parsing_nsdmi (parser, decl);
21597         }
21598       vec_safe_truncate (unparsed_nsdmis, 0);
21599       current_class_ptr = save_ccp;
21600       current_class_ref = save_ccr;
21601       if (pushed_scope)
21602         pop_scope (pushed_scope);
21603
21604       /* Now do some post-NSDMI bookkeeping.  */
21605       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21606         after_nsdmi_defaulted_late_checks (class_type);
21607       vec_safe_truncate (unparsed_classes, 0);
21608       after_nsdmi_defaulted_late_checks (type);
21609
21610       /* Now parse the body of the functions.  */
21611       if (flag_openmp)
21612         {
21613           /* OpenMP UDRs need to be parsed before all other functions.  */
21614           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21615             if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21616               cp_parser_late_parsing_for_member (parser, decl);
21617           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21618             if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21619               cp_parser_late_parsing_for_member (parser, decl);
21620         }
21621       else
21622         FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21623           cp_parser_late_parsing_for_member (parser, decl);
21624       vec_safe_truncate (unparsed_funs_with_definitions, 0);
21625     }
21626   else
21627     vec_safe_push (unparsed_classes, type);
21628
21629   /* Put back any saved access checks.  */
21630   pop_deferring_access_checks ();
21631
21632   /* Restore saved state.  */
21633   parser->in_switch_statement_p = in_switch_statement_p;
21634   parser->in_statement = in_statement;
21635   parser->in_function_body = saved_in_function_body;
21636   parser->num_template_parameter_lists
21637     = saved_num_template_parameter_lists;
21638   parser->in_unbraced_linkage_specification_p
21639     = saved_in_unbraced_linkage_specification_p;
21640
21641   return type;
21642 }
21643
21644 static tree
21645 cp_parser_class_specifier (cp_parser* parser)
21646 {
21647   tree ret;
21648   timevar_push (TV_PARSE_STRUCT);
21649   ret = cp_parser_class_specifier_1 (parser);
21650   timevar_pop (TV_PARSE_STRUCT);
21651   return ret;
21652 }
21653
21654 /* Parse a class-head.
21655
21656    class-head:
21657      class-key identifier [opt] base-clause [opt]
21658      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21659      class-key nested-name-specifier [opt] template-id
21660        base-clause [opt]
21661
21662    class-virt-specifier:
21663      final
21664
21665    GNU Extensions:
21666      class-key attributes identifier [opt] base-clause [opt]
21667      class-key attributes nested-name-specifier identifier base-clause [opt]
21668      class-key attributes nested-name-specifier [opt] template-id
21669        base-clause [opt]
21670
21671    Upon return BASES is initialized to the list of base classes (or
21672    NULL, if there are none) in the same form returned by
21673    cp_parser_base_clause.
21674
21675    Returns the TYPE of the indicated class.  Sets
21676    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21677    involving a nested-name-specifier was used, and FALSE otherwise.
21678
21679    Returns error_mark_node if this is not a class-head.
21680
21681    Returns NULL_TREE if the class-head is syntactically valid, but
21682    semantically invalid in a way that means we should skip the entire
21683    body of the class.  */
21684
21685 static tree
21686 cp_parser_class_head (cp_parser* parser,
21687                       bool* nested_name_specifier_p)
21688 {
21689   tree nested_name_specifier;
21690   enum tag_types class_key;
21691   tree id = NULL_TREE;
21692   tree type = NULL_TREE;
21693   tree attributes;
21694   tree bases;
21695   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21696   bool template_id_p = false;
21697   bool qualified_p = false;
21698   bool invalid_nested_name_p = false;
21699   bool invalid_explicit_specialization_p = false;
21700   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21701   tree pushed_scope = NULL_TREE;
21702   unsigned num_templates;
21703   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21704   /* Assume no nested-name-specifier will be present.  */
21705   *nested_name_specifier_p = false;
21706   /* Assume no template parameter lists will be used in defining the
21707      type.  */
21708   num_templates = 0;
21709   parser->colon_corrects_to_scope_p = false;
21710
21711   /* Look for the class-key.  */
21712   class_key = cp_parser_class_key (parser);
21713   if (class_key == none_type)
21714     return error_mark_node;
21715
21716   /* Parse the attributes.  */
21717   attributes = cp_parser_attributes_opt (parser);
21718
21719   /* If the next token is `::', that is invalid -- but sometimes
21720      people do try to write:
21721
21722        struct ::S {};
21723
21724      Handle this gracefully by accepting the extra qualifier, and then
21725      issuing an error about it later if this really is a
21726      class-head.  If it turns out just to be an elaborated type
21727      specifier, remain silent.  */
21728   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21729     qualified_p = true;
21730
21731   push_deferring_access_checks (dk_no_check);
21732
21733   /* Determine the name of the class.  Begin by looking for an
21734      optional nested-name-specifier.  */
21735   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21736   nested_name_specifier
21737     = cp_parser_nested_name_specifier_opt (parser,
21738                                            /*typename_keyword_p=*/false,
21739                                            /*check_dependency_p=*/false,
21740                                            /*type_p=*/true,
21741                                            /*is_declaration=*/false);
21742   /* If there was a nested-name-specifier, then there *must* be an
21743      identifier.  */
21744   if (nested_name_specifier)
21745     {
21746       type_start_token = cp_lexer_peek_token (parser->lexer);
21747       /* Although the grammar says `identifier', it really means
21748          `class-name' or `template-name'.  You are only allowed to
21749          define a class that has already been declared with this
21750          syntax.
21751
21752          The proposed resolution for Core Issue 180 says that wherever
21753          you see `class T::X' you should treat `X' as a type-name.
21754
21755          It is OK to define an inaccessible class; for example:
21756
21757            class A { class B; };
21758            class A::B {};
21759
21760          We do not know if we will see a class-name, or a
21761          template-name.  We look for a class-name first, in case the
21762          class-name is a template-id; if we looked for the
21763          template-name first we would stop after the template-name.  */
21764       cp_parser_parse_tentatively (parser);
21765       type = cp_parser_class_name (parser,
21766                                    /*typename_keyword_p=*/false,
21767                                    /*template_keyword_p=*/false,
21768                                    class_type,
21769                                    /*check_dependency_p=*/false,
21770                                    /*class_head_p=*/true,
21771                                    /*is_declaration=*/false);
21772       /* If that didn't work, ignore the nested-name-specifier.  */
21773       if (!cp_parser_parse_definitely (parser))
21774         {
21775           invalid_nested_name_p = true;
21776           type_start_token = cp_lexer_peek_token (parser->lexer);
21777           id = cp_parser_identifier (parser);
21778           if (id == error_mark_node)
21779             id = NULL_TREE;
21780         }
21781       /* If we could not find a corresponding TYPE, treat this
21782          declaration like an unqualified declaration.  */
21783       if (type == error_mark_node)
21784         nested_name_specifier = NULL_TREE;
21785       /* Otherwise, count the number of templates used in TYPE and its
21786          containing scopes.  */
21787       else
21788         {
21789           tree scope;
21790
21791           for (scope = TREE_TYPE (type);
21792                scope && TREE_CODE (scope) != NAMESPACE_DECL;
21793                scope = get_containing_scope (scope))
21794             if (TYPE_P (scope)
21795                 && CLASS_TYPE_P (scope)
21796                 && CLASSTYPE_TEMPLATE_INFO (scope)
21797                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21798                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21799                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21800               ++num_templates;
21801         }
21802     }
21803   /* Otherwise, the identifier is optional.  */
21804   else
21805     {
21806       /* We don't know whether what comes next is a template-id,
21807          an identifier, or nothing at all.  */
21808       cp_parser_parse_tentatively (parser);
21809       /* Check for a template-id.  */
21810       type_start_token = cp_lexer_peek_token (parser->lexer);
21811       id = cp_parser_template_id (parser,
21812                                   /*template_keyword_p=*/false,
21813                                   /*check_dependency_p=*/true,
21814                                   class_key,
21815                                   /*is_declaration=*/true);
21816       /* If that didn't work, it could still be an identifier.  */
21817       if (!cp_parser_parse_definitely (parser))
21818         {
21819           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21820             {
21821               type_start_token = cp_lexer_peek_token (parser->lexer);
21822               id = cp_parser_identifier (parser);
21823             }
21824           else
21825             id = NULL_TREE;
21826         }
21827       else
21828         {
21829           template_id_p = true;
21830           ++num_templates;
21831         }
21832     }
21833
21834   pop_deferring_access_checks ();
21835
21836   if (id)
21837     {
21838       cp_parser_check_for_invalid_template_id (parser, id,
21839                                                class_key,
21840                                                type_start_token->location);
21841     }
21842   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21843
21844   /* If it's not a `:' or a `{' then we can't really be looking at a
21845      class-head, since a class-head only appears as part of a
21846      class-specifier.  We have to detect this situation before calling
21847      xref_tag, since that has irreversible side-effects.  */
21848   if (!cp_parser_next_token_starts_class_definition_p (parser))
21849     {
21850       cp_parser_error (parser, "expected %<{%> or %<:%>");
21851       type = error_mark_node;
21852       goto out;
21853     }
21854
21855   /* At this point, we're going ahead with the class-specifier, even
21856      if some other problem occurs.  */
21857   cp_parser_commit_to_tentative_parse (parser);
21858   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21859     {
21860       cp_parser_error (parser,
21861                        "cannot specify %<override%> for a class");
21862       type = error_mark_node;
21863       goto out;
21864     }
21865   /* Issue the error about the overly-qualified name now.  */
21866   if (qualified_p)
21867     {
21868       cp_parser_error (parser,
21869                        "global qualification of class name is invalid");
21870       type = error_mark_node;
21871       goto out;
21872     }
21873   else if (invalid_nested_name_p)
21874     {
21875       cp_parser_error (parser,
21876                        "qualified name does not name a class");
21877       type = error_mark_node;
21878       goto out;
21879     }
21880   else if (nested_name_specifier)
21881     {
21882       tree scope;
21883
21884       /* Reject typedef-names in class heads.  */
21885       if (!DECL_IMPLICIT_TYPEDEF_P (type))
21886         {
21887           error_at (type_start_token->location,
21888                     "invalid class name in declaration of %qD",
21889                     type);
21890           type = NULL_TREE;
21891           goto done;
21892         }
21893
21894       /* Figure out in what scope the declaration is being placed.  */
21895       scope = current_scope ();
21896       /* If that scope does not contain the scope in which the
21897          class was originally declared, the program is invalid.  */
21898       if (scope && !is_ancestor (scope, nested_name_specifier))
21899         {
21900           if (at_namespace_scope_p ())
21901             error_at (type_start_token->location,
21902                       "declaration of %qD in namespace %qD which does not "
21903                       "enclose %qD",
21904                       type, scope, nested_name_specifier);
21905           else
21906             error_at (type_start_token->location,
21907                       "declaration of %qD in %qD which does not enclose %qD",
21908                       type, scope, nested_name_specifier);
21909           type = NULL_TREE;
21910           goto done;
21911         }
21912       /* [dcl.meaning]
21913
21914          A declarator-id shall not be qualified except for the
21915          definition of a ... nested class outside of its class
21916          ... [or] the definition or explicit instantiation of a
21917          class member of a namespace outside of its namespace.  */
21918       if (scope == nested_name_specifier)
21919         {
21920           permerror (nested_name_specifier_token_start->location,
21921                      "extra qualification not allowed");
21922           nested_name_specifier = NULL_TREE;
21923           num_templates = 0;
21924         }
21925     }
21926   /* An explicit-specialization must be preceded by "template <>".  If
21927      it is not, try to recover gracefully.  */
21928   if (at_namespace_scope_p ()
21929       && parser->num_template_parameter_lists == 0
21930       && template_id_p)
21931     {
21932       error_at (type_start_token->location,
21933                 "an explicit specialization must be preceded by %<template <>%>");
21934       invalid_explicit_specialization_p = true;
21935       /* Take the same action that would have been taken by
21936          cp_parser_explicit_specialization.  */
21937       ++parser->num_template_parameter_lists;
21938       begin_specialization ();
21939     }
21940   /* There must be no "return" statements between this point and the
21941      end of this function; set "type "to the correct return value and
21942      use "goto done;" to return.  */
21943   /* Make sure that the right number of template parameters were
21944      present.  */
21945   if (!cp_parser_check_template_parameters (parser, num_templates,
21946                                             type_start_token->location,
21947                                             /*declarator=*/NULL))
21948     {
21949       /* If something went wrong, there is no point in even trying to
21950          process the class-definition.  */
21951       type = NULL_TREE;
21952       goto done;
21953     }
21954
21955   /* Look up the type.  */
21956   if (template_id_p)
21957     {
21958       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21959           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21960               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21961         {
21962           error_at (type_start_token->location,
21963                     "function template %qD redeclared as a class template", id);
21964           type = error_mark_node;
21965         }
21966       else
21967         {
21968           type = TREE_TYPE (id);
21969           type = maybe_process_partial_specialization (type);
21970         }
21971       if (nested_name_specifier)
21972         pushed_scope = push_scope (nested_name_specifier);
21973     }
21974   else if (nested_name_specifier)
21975     {
21976       tree class_type;
21977
21978       /* Given:
21979
21980             template <typename T> struct S { struct T };
21981             template <typename T> struct S<T>::T { };
21982
21983          we will get a TYPENAME_TYPE when processing the definition of
21984          `S::T'.  We need to resolve it to the actual type before we
21985          try to define it.  */
21986       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21987         {
21988           class_type = resolve_typename_type (TREE_TYPE (type),
21989                                               /*only_current_p=*/false);
21990           if (TREE_CODE (class_type) != TYPENAME_TYPE)
21991             type = TYPE_NAME (class_type);
21992           else
21993             {
21994               cp_parser_error (parser, "could not resolve typename type");
21995               type = error_mark_node;
21996             }
21997         }
21998
21999       if (maybe_process_partial_specialization (TREE_TYPE (type))
22000           == error_mark_node)
22001         {
22002           type = NULL_TREE;
22003           goto done;
22004         }
22005
22006       class_type = current_class_type;
22007       /* Enter the scope indicated by the nested-name-specifier.  */
22008       pushed_scope = push_scope (nested_name_specifier);
22009       /* Get the canonical version of this type.  */
22010       type = TYPE_MAIN_DECL (TREE_TYPE (type));
22011       /* Call push_template_decl if it seems like we should be defining a
22012          template either from the template headers or the type we're
22013          defining, so that we diagnose both extra and missing headers.  */
22014       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22015            || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22016           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22017         {
22018           type = push_template_decl (type);
22019           if (type == error_mark_node)
22020             {
22021               type = NULL_TREE;
22022               goto done;
22023             }
22024         }
22025
22026       type = TREE_TYPE (type);
22027       *nested_name_specifier_p = true;
22028     }
22029   else      /* The name is not a nested name.  */
22030     {
22031       /* If the class was unnamed, create a dummy name.  */
22032       if (!id)
22033         id = make_anon_name ();
22034       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22035                        parser->num_template_parameter_lists);
22036     }
22037
22038   /* Indicate whether this class was declared as a `class' or as a
22039      `struct'.  */
22040   if (TREE_CODE (type) == RECORD_TYPE)
22041     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22042   cp_parser_check_class_key (class_key, type);
22043
22044   /* If this type was already complete, and we see another definition,
22045      that's an error.  */
22046   if (type != error_mark_node && COMPLETE_TYPE_P (type))
22047     {
22048       error_at (type_start_token->location, "redefinition of %q#T",
22049                 type);
22050       error_at (type_start_token->location, "previous definition of %q+#T",
22051                 type);
22052       type = NULL_TREE;
22053       goto done;
22054     }
22055   else if (type == error_mark_node)
22056     type = NULL_TREE;
22057
22058   if (type)
22059     {
22060       /* Apply attributes now, before any use of the class as a template
22061          argument in its base list.  */
22062       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22063       fixup_attribute_variants (type);
22064     }
22065
22066   /* We will have entered the scope containing the class; the names of
22067      base classes should be looked up in that context.  For example:
22068
22069        struct A { struct B {}; struct C; };
22070        struct A::C : B {};
22071
22072      is valid.  */
22073
22074   /* Get the list of base-classes, if there is one.  */
22075   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22076     {
22077       /* PR59482: enter the class scope so that base-specifiers are looked
22078          up correctly.  */
22079       if (type)
22080         pushclass (type);
22081       bases = cp_parser_base_clause (parser);
22082       /* PR59482: get out of the previously pushed class scope so that the
22083          subsequent pops pop the right thing.  */
22084       if (type)
22085         popclass ();
22086     }
22087   else
22088     bases = NULL_TREE;
22089
22090   /* If we're really defining a class, process the base classes.
22091      If they're invalid, fail.  */
22092   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22093       && !xref_basetypes (type, bases))
22094     type = NULL_TREE;
22095
22096  done:
22097   /* Leave the scope given by the nested-name-specifier.  We will
22098      enter the class scope itself while processing the members.  */
22099   if (pushed_scope)
22100     pop_scope (pushed_scope);
22101
22102   if (invalid_explicit_specialization_p)
22103     {
22104       end_specialization ();
22105       --parser->num_template_parameter_lists;
22106     }
22107
22108   if (type)
22109     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22110   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22111     CLASSTYPE_FINAL (type) = 1;
22112  out:
22113   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22114   return type;
22115 }
22116
22117 /* Parse a class-key.
22118
22119    class-key:
22120      class
22121      struct
22122      union
22123
22124    Returns the kind of class-key specified, or none_type to indicate
22125    error.  */
22126
22127 static enum tag_types
22128 cp_parser_class_key (cp_parser* parser)
22129 {
22130   cp_token *token;
22131   enum tag_types tag_type;
22132
22133   /* Look for the class-key.  */
22134   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22135   if (!token)
22136     return none_type;
22137
22138   /* Check to see if the TOKEN is a class-key.  */
22139   tag_type = cp_parser_token_is_class_key (token);
22140   if (!tag_type)
22141     cp_parser_error (parser, "expected class-key");
22142   return tag_type;
22143 }
22144
22145 /* Parse a type-parameter-key.
22146
22147    type-parameter-key:
22148      class
22149      typename
22150  */
22151
22152 static void
22153 cp_parser_type_parameter_key (cp_parser* parser)
22154 {
22155   /* Look for the type-parameter-key.  */
22156   enum tag_types tag_type = none_type;
22157   cp_token *token = cp_lexer_peek_token (parser->lexer);
22158   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22159     {
22160       cp_lexer_consume_token (parser->lexer);
22161       if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22162         /* typename is not allowed in a template template parameter
22163            by the standard until C++1Z.  */
22164         pedwarn (token->location, OPT_Wpedantic, 
22165                  "ISO C++ forbids typename key in template template parameter;"
22166                  " use -std=c++1z or -std=gnu++1z");
22167     }
22168   else
22169     cp_parser_error (parser, "expected %<class%> or %<typename%>");
22170
22171   return;
22172 }
22173
22174 /* Parse an (optional) member-specification.
22175
22176    member-specification:
22177      member-declaration member-specification [opt]
22178      access-specifier : member-specification [opt]  */
22179
22180 static void
22181 cp_parser_member_specification_opt (cp_parser* parser)
22182 {
22183   while (true)
22184     {
22185       cp_token *token;
22186       enum rid keyword;
22187
22188       /* Peek at the next token.  */
22189       token = cp_lexer_peek_token (parser->lexer);
22190       /* If it's a `}', or EOF then we've seen all the members.  */
22191       if (token->type == CPP_CLOSE_BRACE
22192           || token->type == CPP_EOF
22193           || token->type == CPP_PRAGMA_EOL)
22194         break;
22195
22196       /* See if this token is a keyword.  */
22197       keyword = token->keyword;
22198       switch (keyword)
22199         {
22200         case RID_PUBLIC:
22201         case RID_PROTECTED:
22202         case RID_PRIVATE:
22203           /* Consume the access-specifier.  */
22204           cp_lexer_consume_token (parser->lexer);
22205           /* Remember which access-specifier is active.  */
22206           current_access_specifier = token->u.value;
22207           /* Look for the `:'.  */
22208           cp_parser_require (parser, CPP_COLON, RT_COLON);
22209           break;
22210
22211         default:
22212           /* Accept #pragmas at class scope.  */
22213           if (token->type == CPP_PRAGMA)
22214             {
22215               cp_parser_pragma (parser, pragma_member, NULL);
22216               break;
22217             }
22218
22219           /* Otherwise, the next construction must be a
22220              member-declaration.  */
22221           cp_parser_member_declaration (parser);
22222         }
22223     }
22224 }
22225
22226 /* Parse a member-declaration.
22227
22228    member-declaration:
22229      decl-specifier-seq [opt] member-declarator-list [opt] ;
22230      function-definition ; [opt]
22231      :: [opt] nested-name-specifier template [opt] unqualified-id ;
22232      using-declaration
22233      template-declaration
22234      alias-declaration
22235
22236    member-declarator-list:
22237      member-declarator
22238      member-declarator-list , member-declarator
22239
22240    member-declarator:
22241      declarator pure-specifier [opt]
22242      declarator constant-initializer [opt]
22243      identifier [opt] : constant-expression
22244
22245    GNU Extensions:
22246
22247    member-declaration:
22248      __extension__ member-declaration
22249
22250    member-declarator:
22251      declarator attributes [opt] pure-specifier [opt]
22252      declarator attributes [opt] constant-initializer [opt]
22253      identifier [opt] attributes [opt] : constant-expression  
22254
22255    C++0x Extensions:
22256
22257    member-declaration:
22258      static_assert-declaration  */
22259
22260 static void
22261 cp_parser_member_declaration (cp_parser* parser)
22262 {
22263   cp_decl_specifier_seq decl_specifiers;
22264   tree prefix_attributes;
22265   tree decl;
22266   int declares_class_or_enum;
22267   bool friend_p;
22268   cp_token *token = NULL;
22269   cp_token *decl_spec_token_start = NULL;
22270   cp_token *initializer_token_start = NULL;
22271   int saved_pedantic;
22272   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22273
22274   /* Check for the `__extension__' keyword.  */
22275   if (cp_parser_extension_opt (parser, &saved_pedantic))
22276     {
22277       /* Recurse.  */
22278       cp_parser_member_declaration (parser);
22279       /* Restore the old value of the PEDANTIC flag.  */
22280       pedantic = saved_pedantic;
22281
22282       return;
22283     }
22284
22285   /* Check for a template-declaration.  */
22286   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22287     {
22288       /* An explicit specialization here is an error condition, and we
22289          expect the specialization handler to detect and report this.  */
22290       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22291           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22292         cp_parser_explicit_specialization (parser);
22293       else
22294         cp_parser_template_declaration (parser, /*member_p=*/true);
22295
22296       return;
22297     }
22298   /* Check for a template introduction.  */
22299   else if (cp_parser_template_declaration_after_export (parser, true))
22300     return;
22301
22302   /* Check for a using-declaration.  */
22303   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22304     {
22305       if (cxx_dialect < cxx11)
22306         {
22307           /* Parse the using-declaration.  */
22308           cp_parser_using_declaration (parser,
22309                                        /*access_declaration_p=*/false);
22310           return;
22311         }
22312       else
22313         {
22314           tree decl;
22315           bool alias_decl_expected;
22316           cp_parser_parse_tentatively (parser);
22317           decl = cp_parser_alias_declaration (parser);
22318           /* Note that if we actually see the '=' token after the
22319              identifier, cp_parser_alias_declaration commits the
22320              tentative parse.  In that case, we really expect an
22321              alias-declaration.  Otherwise, we expect a using
22322              declaration.  */
22323           alias_decl_expected =
22324             !cp_parser_uncommitted_to_tentative_parse_p (parser);
22325           cp_parser_parse_definitely (parser);
22326
22327           if (alias_decl_expected)
22328             finish_member_declaration (decl);
22329           else
22330             cp_parser_using_declaration (parser,
22331                                          /*access_declaration_p=*/false);
22332           return;
22333         }
22334     }
22335
22336   /* Check for @defs.  */
22337   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22338     {
22339       tree ivar, member;
22340       tree ivar_chains = cp_parser_objc_defs_expression (parser);
22341       ivar = ivar_chains;
22342       while (ivar)
22343         {
22344           member = ivar;
22345           ivar = TREE_CHAIN (member);
22346           TREE_CHAIN (member) = NULL_TREE;
22347           finish_member_declaration (member);
22348         }
22349       return;
22350     }
22351
22352   /* If the next token is `static_assert' we have a static assertion.  */
22353   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22354     {
22355       cp_parser_static_assert (parser, /*member_p=*/true);
22356       return;
22357     }
22358
22359   parser->colon_corrects_to_scope_p = false;
22360
22361   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22362       goto out;
22363
22364   /* Parse the decl-specifier-seq.  */
22365   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22366   cp_parser_decl_specifier_seq (parser,
22367                                 CP_PARSER_FLAGS_OPTIONAL,
22368                                 &decl_specifiers,
22369                                 &declares_class_or_enum);
22370   /* Check for an invalid type-name.  */
22371   if (!decl_specifiers.any_type_specifiers_p
22372       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22373     goto out;
22374   /* If there is no declarator, then the decl-specifier-seq should
22375      specify a type.  */
22376   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22377     {
22378       /* If there was no decl-specifier-seq, and the next token is a
22379          `;', then we have something like:
22380
22381            struct S { ; };
22382
22383          [class.mem]
22384
22385          Each member-declaration shall declare at least one member
22386          name of the class.  */
22387       if (!decl_specifiers.any_specifiers_p)
22388         {
22389           cp_token *token = cp_lexer_peek_token (parser->lexer);
22390           if (!in_system_header_at (token->location))
22391             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22392         }
22393       else
22394         {
22395           tree type;
22396
22397           /* See if this declaration is a friend.  */
22398           friend_p = cp_parser_friend_p (&decl_specifiers);
22399           /* If there were decl-specifiers, check to see if there was
22400              a class-declaration.  */
22401           type = check_tag_decl (&decl_specifiers,
22402                                  /*explicit_type_instantiation_p=*/false);
22403           /* Nested classes have already been added to the class, but
22404              a `friend' needs to be explicitly registered.  */
22405           if (friend_p)
22406             {
22407               /* If the `friend' keyword was present, the friend must
22408                  be introduced with a class-key.  */
22409                if (!declares_class_or_enum && cxx_dialect < cxx11)
22410                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22411                           "in C++03 a class-key must be used "
22412                           "when declaring a friend");
22413                /* In this case:
22414
22415                     template <typename T> struct A {
22416                       friend struct A<T>::B;
22417                     };
22418
22419                   A<T>::B will be represented by a TYPENAME_TYPE, and
22420                   therefore not recognized by check_tag_decl.  */
22421                if (!type)
22422                  {
22423                    type = decl_specifiers.type;
22424                    if (type && TREE_CODE (type) == TYPE_DECL)
22425                      type = TREE_TYPE (type);
22426                  }
22427                if (!type || !TYPE_P (type))
22428                  error_at (decl_spec_token_start->location,
22429                            "friend declaration does not name a class or "
22430                            "function");
22431                else
22432                  make_friend_class (current_class_type, type,
22433                                     /*complain=*/true);
22434             }
22435           /* If there is no TYPE, an error message will already have
22436              been issued.  */
22437           else if (!type || type == error_mark_node)
22438             ;
22439           /* An anonymous aggregate has to be handled specially; such
22440              a declaration really declares a data member (with a
22441              particular type), as opposed to a nested class.  */
22442           else if (ANON_AGGR_TYPE_P (type))
22443             {
22444               /* C++11 9.5/6.  */
22445               if (decl_specifiers.storage_class != sc_none)
22446                 error_at (decl_spec_token_start->location,
22447                           "a storage class on an anonymous aggregate "
22448                           "in class scope is not allowed");
22449
22450               /* Remove constructors and such from TYPE, now that we
22451                  know it is an anonymous aggregate.  */
22452               fixup_anonymous_aggr (type);
22453               /* And make the corresponding data member.  */
22454               decl = build_decl (decl_spec_token_start->location,
22455                                  FIELD_DECL, NULL_TREE, type);
22456               /* Add it to the class.  */
22457               finish_member_declaration (decl);
22458             }
22459           else
22460             cp_parser_check_access_in_redeclaration
22461                                               (TYPE_NAME (type),
22462                                                decl_spec_token_start->location);
22463         }
22464     }
22465   else
22466     {
22467       bool assume_semicolon = false;
22468
22469       /* Clear attributes from the decl_specifiers but keep them
22470          around as prefix attributes that apply them to the entity
22471          being declared.  */
22472       prefix_attributes = decl_specifiers.attributes;
22473       decl_specifiers.attributes = NULL_TREE;
22474
22475       /* See if these declarations will be friends.  */
22476       friend_p = cp_parser_friend_p (&decl_specifiers);
22477
22478       /* Keep going until we hit the `;' at the end of the
22479          declaration.  */
22480       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22481         {
22482           tree attributes = NULL_TREE;
22483           tree first_attribute;
22484
22485           /* Peek at the next token.  */
22486           token = cp_lexer_peek_token (parser->lexer);
22487
22488           /* Check for a bitfield declaration.  */
22489           if (token->type == CPP_COLON
22490               || (token->type == CPP_NAME
22491                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22492                   == CPP_COLON))
22493             {
22494               tree identifier;
22495               tree width;
22496
22497               /* Get the name of the bitfield.  Note that we cannot just
22498                  check TOKEN here because it may have been invalidated by
22499                  the call to cp_lexer_peek_nth_token above.  */
22500               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22501                 identifier = cp_parser_identifier (parser);
22502               else
22503                 identifier = NULL_TREE;
22504
22505               /* Consume the `:' token.  */
22506               cp_lexer_consume_token (parser->lexer);
22507               /* Get the width of the bitfield.  */
22508               width
22509                 = cp_parser_constant_expression (parser);
22510
22511               /* Look for attributes that apply to the bitfield.  */
22512               attributes = cp_parser_attributes_opt (parser);
22513               /* Remember which attributes are prefix attributes and
22514                  which are not.  */
22515               first_attribute = attributes;
22516               /* Combine the attributes.  */
22517               attributes = chainon (prefix_attributes, attributes);
22518
22519               /* Create the bitfield declaration.  */
22520               decl = grokbitfield (identifier
22521                                    ? make_id_declarator (NULL_TREE,
22522                                                          identifier,
22523                                                          sfk_none)
22524                                    : NULL,
22525                                    &decl_specifiers,
22526                                    width,
22527                                    attributes);
22528             }
22529           else
22530             {
22531               cp_declarator *declarator;
22532               tree initializer;
22533               tree asm_specification;
22534               int ctor_dtor_or_conv_p;
22535
22536               /* Parse the declarator.  */
22537               declarator
22538                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22539                                         &ctor_dtor_or_conv_p,
22540                                         /*parenthesized_p=*/NULL,
22541                                         /*member_p=*/true,
22542                                         friend_p);
22543
22544               /* If something went wrong parsing the declarator, make sure
22545                  that we at least consume some tokens.  */
22546               if (declarator == cp_error_declarator)
22547                 {
22548                   /* Skip to the end of the statement.  */
22549                   cp_parser_skip_to_end_of_statement (parser);
22550                   /* If the next token is not a semicolon, that is
22551                      probably because we just skipped over the body of
22552                      a function.  So, we consume a semicolon if
22553                      present, but do not issue an error message if it
22554                      is not present.  */
22555                   if (cp_lexer_next_token_is (parser->lexer,
22556                                               CPP_SEMICOLON))
22557                     cp_lexer_consume_token (parser->lexer);
22558                   goto out;
22559                 }
22560
22561               if (declares_class_or_enum & 2)
22562                 cp_parser_check_for_definition_in_return_type
22563                                             (declarator, decl_specifiers.type,
22564                                              decl_specifiers.locations[ds_type_spec]);
22565
22566               /* Look for an asm-specification.  */
22567               asm_specification = cp_parser_asm_specification_opt (parser);
22568               /* Look for attributes that apply to the declaration.  */
22569               attributes = cp_parser_attributes_opt (parser);
22570               /* Remember which attributes are prefix attributes and
22571                  which are not.  */
22572               first_attribute = attributes;
22573               /* Combine the attributes.  */
22574               attributes = chainon (prefix_attributes, attributes);
22575
22576               /* If it's an `=', then we have a constant-initializer or a
22577                  pure-specifier.  It is not correct to parse the
22578                  initializer before registering the member declaration
22579                  since the member declaration should be in scope while
22580                  its initializer is processed.  However, the rest of the
22581                  front end does not yet provide an interface that allows
22582                  us to handle this correctly.  */
22583               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22584                 {
22585                   /* In [class.mem]:
22586
22587                      A pure-specifier shall be used only in the declaration of
22588                      a virtual function.
22589
22590                      A member-declarator can contain a constant-initializer
22591                      only if it declares a static member of integral or
22592                      enumeration type.
22593
22594                      Therefore, if the DECLARATOR is for a function, we look
22595                      for a pure-specifier; otherwise, we look for a
22596                      constant-initializer.  When we call `grokfield', it will
22597                      perform more stringent semantics checks.  */
22598                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
22599                   if (function_declarator_p (declarator)
22600                       || (decl_specifiers.type
22601                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22602                           && declarator->kind == cdk_id
22603                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22604                               == FUNCTION_TYPE)))
22605                     initializer = cp_parser_pure_specifier (parser);
22606                   else if (decl_specifiers.storage_class != sc_static)
22607                     initializer = cp_parser_save_nsdmi (parser);
22608                   else if (cxx_dialect >= cxx11)
22609                     {
22610                       bool nonconst;
22611                       /* Don't require a constant rvalue in C++11, since we
22612                          might want a reference constant.  We'll enforce
22613                          constancy later.  */
22614                       cp_lexer_consume_token (parser->lexer);
22615                       /* Parse the initializer.  */
22616                       initializer = cp_parser_initializer_clause (parser,
22617                                                                   &nonconst);
22618                     }
22619                   else
22620                     /* Parse the initializer.  */
22621                     initializer = cp_parser_constant_initializer (parser);
22622                 }
22623               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22624                        && !function_declarator_p (declarator))
22625                 {
22626                   bool x;
22627                   if (decl_specifiers.storage_class != sc_static)
22628                     initializer = cp_parser_save_nsdmi (parser);
22629                   else
22630                     initializer = cp_parser_initializer (parser, &x, &x);
22631                 }
22632               /* Otherwise, there is no initializer.  */
22633               else
22634                 initializer = NULL_TREE;
22635
22636               /* See if we are probably looking at a function
22637                  definition.  We are certainly not looking at a
22638                  member-declarator.  Calling `grokfield' has
22639                  side-effects, so we must not do it unless we are sure
22640                  that we are looking at a member-declarator.  */
22641               if (cp_parser_token_starts_function_definition_p
22642                   (cp_lexer_peek_token (parser->lexer)))
22643                 {
22644                   /* The grammar does not allow a pure-specifier to be
22645                      used when a member function is defined.  (It is
22646                      possible that this fact is an oversight in the
22647                      standard, since a pure function may be defined
22648                      outside of the class-specifier.  */
22649                   if (initializer && initializer_token_start)
22650                     error_at (initializer_token_start->location,
22651                               "pure-specifier on function-definition");
22652                   decl = cp_parser_save_member_function_body (parser,
22653                                                               &decl_specifiers,
22654                                                               declarator,
22655                                                               attributes);
22656                   if (parser->fully_implicit_function_template_p)
22657                     decl = finish_fully_implicit_template (parser, decl);
22658                   /* If the member was not a friend, declare it here.  */
22659                   if (!friend_p)
22660                     finish_member_declaration (decl);
22661                   /* Peek at the next token.  */
22662                   token = cp_lexer_peek_token (parser->lexer);
22663                   /* If the next token is a semicolon, consume it.  */
22664                   if (token->type == CPP_SEMICOLON)
22665                     cp_lexer_consume_token (parser->lexer);
22666                   goto out;
22667                 }
22668               else
22669                 if (declarator->kind == cdk_function)
22670                   declarator->id_loc = token->location;
22671               /* Create the declaration.  */
22672               decl = grokfield (declarator, &decl_specifiers,
22673                                 initializer, /*init_const_expr_p=*/true,
22674                                 asm_specification, attributes);
22675               if (parser->fully_implicit_function_template_p)
22676                 {
22677                   if (friend_p)
22678                     finish_fully_implicit_template (parser, 0);
22679                   else
22680                     decl = finish_fully_implicit_template (parser, decl);
22681                 }
22682             }
22683
22684           cp_finalize_omp_declare_simd (parser, decl);
22685           cp_finalize_oacc_routine (parser, decl, false);
22686
22687           /* Reset PREFIX_ATTRIBUTES.  */
22688           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22689             attributes = TREE_CHAIN (attributes);
22690           if (attributes)
22691             TREE_CHAIN (attributes) = NULL_TREE;
22692
22693           /* If there is any qualification still in effect, clear it
22694              now; we will be starting fresh with the next declarator.  */
22695           parser->scope = NULL_TREE;
22696           parser->qualifying_scope = NULL_TREE;
22697           parser->object_scope = NULL_TREE;
22698           /* If it's a `,', then there are more declarators.  */
22699           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22700             {
22701               cp_lexer_consume_token (parser->lexer);
22702               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22703                 {
22704                   cp_token *token = cp_lexer_previous_token (parser->lexer);
22705                   error_at (token->location,
22706                             "stray %<,%> at end of member declaration");
22707                 }
22708             }
22709           /* If the next token isn't a `;', then we have a parse error.  */
22710           else if (cp_lexer_next_token_is_not (parser->lexer,
22711                                                CPP_SEMICOLON))
22712             {
22713               /* The next token might be a ways away from where the
22714                  actual semicolon is missing.  Find the previous token
22715                  and use that for our error position.  */
22716               cp_token *token = cp_lexer_previous_token (parser->lexer);
22717               error_at (token->location,
22718                         "expected %<;%> at end of member declaration");
22719
22720               /* Assume that the user meant to provide a semicolon.  If
22721                  we were to cp_parser_skip_to_end_of_statement, we might
22722                  skip to a semicolon inside a member function definition
22723                  and issue nonsensical error messages.  */
22724               assume_semicolon = true;
22725             }
22726
22727           if (decl)
22728             {
22729               /* Add DECL to the list of members.  */
22730               if (!friend_p
22731                   /* Explicitly include, eg, NSDMIs, for better error
22732                      recovery (c++/58650).  */
22733                   || !DECL_DECLARES_FUNCTION_P (decl))
22734                 finish_member_declaration (decl);
22735
22736               if (TREE_CODE (decl) == FUNCTION_DECL)
22737                 cp_parser_save_default_args (parser, decl);
22738               else if (TREE_CODE (decl) == FIELD_DECL
22739                        && !DECL_C_BIT_FIELD (decl)
22740                        && DECL_INITIAL (decl))
22741                 /* Add DECL to the queue of NSDMI to be parsed later.  */
22742                 vec_safe_push (unparsed_nsdmis, decl);
22743             }
22744
22745           if (assume_semicolon)
22746             goto out;
22747         }
22748     }
22749
22750   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22751  out:
22752   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22753 }
22754
22755 /* Parse a pure-specifier.
22756
22757    pure-specifier:
22758      = 0
22759
22760    Returns INTEGER_ZERO_NODE if a pure specifier is found.
22761    Otherwise, ERROR_MARK_NODE is returned.  */
22762
22763 static tree
22764 cp_parser_pure_specifier (cp_parser* parser)
22765 {
22766   cp_token *token;
22767
22768   /* Look for the `=' token.  */
22769   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22770     return error_mark_node;
22771   /* Look for the `0' token.  */
22772   token = cp_lexer_peek_token (parser->lexer);
22773
22774   if (token->type == CPP_EOF
22775       || token->type == CPP_PRAGMA_EOL)
22776     return error_mark_node;
22777
22778   cp_lexer_consume_token (parser->lexer);
22779
22780   /* Accept = default or = delete in c++0x mode.  */
22781   if (token->keyword == RID_DEFAULT
22782       || token->keyword == RID_DELETE)
22783     {
22784       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22785       return token->u.value;
22786     }
22787
22788   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
22789   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22790     {
22791       cp_parser_error (parser,
22792                        "invalid pure specifier (only %<= 0%> is allowed)");
22793       cp_parser_skip_to_end_of_statement (parser);
22794       return error_mark_node;
22795     }
22796   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22797     {
22798       error_at (token->location, "templates may not be %<virtual%>");
22799       return error_mark_node;
22800     }
22801
22802   return integer_zero_node;
22803 }
22804
22805 /* Parse a constant-initializer.
22806
22807    constant-initializer:
22808      = constant-expression
22809
22810    Returns a representation of the constant-expression.  */
22811
22812 static tree
22813 cp_parser_constant_initializer (cp_parser* parser)
22814 {
22815   /* Look for the `=' token.  */
22816   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22817     return error_mark_node;
22818
22819   /* It is invalid to write:
22820
22821        struct S { static const int i = { 7 }; };
22822
22823      */
22824   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22825     {
22826       cp_parser_error (parser,
22827                        "a brace-enclosed initializer is not allowed here");
22828       /* Consume the opening brace.  */
22829       cp_lexer_consume_token (parser->lexer);
22830       /* Skip the initializer.  */
22831       cp_parser_skip_to_closing_brace (parser);
22832       /* Look for the trailing `}'.  */
22833       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22834
22835       return error_mark_node;
22836     }
22837
22838   return cp_parser_constant_expression (parser);
22839 }
22840
22841 /* Derived classes [gram.class.derived] */
22842
22843 /* Parse a base-clause.
22844
22845    base-clause:
22846      : base-specifier-list
22847
22848    base-specifier-list:
22849      base-specifier ... [opt]
22850      base-specifier-list , base-specifier ... [opt]
22851
22852    Returns a TREE_LIST representing the base-classes, in the order in
22853    which they were declared.  The representation of each node is as
22854    described by cp_parser_base_specifier.
22855
22856    In the case that no bases are specified, this function will return
22857    NULL_TREE, not ERROR_MARK_NODE.  */
22858
22859 static tree
22860 cp_parser_base_clause (cp_parser* parser)
22861 {
22862   tree bases = NULL_TREE;
22863
22864   /* Look for the `:' that begins the list.  */
22865   cp_parser_require (parser, CPP_COLON, RT_COLON);
22866
22867   /* Scan the base-specifier-list.  */
22868   while (true)
22869     {
22870       cp_token *token;
22871       tree base;
22872       bool pack_expansion_p = false;
22873
22874       /* Look for the base-specifier.  */
22875       base = cp_parser_base_specifier (parser);
22876       /* Look for the (optional) ellipsis. */
22877       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22878         {
22879           /* Consume the `...'. */
22880           cp_lexer_consume_token (parser->lexer);
22881
22882           pack_expansion_p = true;
22883         }
22884
22885       /* Add BASE to the front of the list.  */
22886       if (base && base != error_mark_node)
22887         {
22888           if (pack_expansion_p)
22889             /* Make this a pack expansion type. */
22890             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22891
22892           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22893             {
22894               TREE_CHAIN (base) = bases;
22895               bases = base;
22896             }
22897         }
22898       /* Peek at the next token.  */
22899       token = cp_lexer_peek_token (parser->lexer);
22900       /* If it's not a comma, then the list is complete.  */
22901       if (token->type != CPP_COMMA)
22902         break;
22903       /* Consume the `,'.  */
22904       cp_lexer_consume_token (parser->lexer);
22905     }
22906
22907   /* PARSER->SCOPE may still be non-NULL at this point, if the last
22908      base class had a qualified name.  However, the next name that
22909      appears is certainly not qualified.  */
22910   parser->scope = NULL_TREE;
22911   parser->qualifying_scope = NULL_TREE;
22912   parser->object_scope = NULL_TREE;
22913
22914   return nreverse (bases);
22915 }
22916
22917 /* Parse a base-specifier.
22918
22919    base-specifier:
22920      :: [opt] nested-name-specifier [opt] class-name
22921      virtual access-specifier [opt] :: [opt] nested-name-specifier
22922        [opt] class-name
22923      access-specifier virtual [opt] :: [opt] nested-name-specifier
22924        [opt] class-name
22925
22926    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
22927    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22928    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
22929    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
22930
22931 static tree
22932 cp_parser_base_specifier (cp_parser* parser)
22933 {
22934   cp_token *token;
22935   bool done = false;
22936   bool virtual_p = false;
22937   bool duplicate_virtual_error_issued_p = false;
22938   bool duplicate_access_error_issued_p = false;
22939   bool class_scope_p, template_p;
22940   tree access = access_default_node;
22941   tree type;
22942
22943   /* Process the optional `virtual' and `access-specifier'.  */
22944   while (!done)
22945     {
22946       /* Peek at the next token.  */
22947       token = cp_lexer_peek_token (parser->lexer);
22948       /* Process `virtual'.  */
22949       switch (token->keyword)
22950         {
22951         case RID_VIRTUAL:
22952           /* If `virtual' appears more than once, issue an error.  */
22953           if (virtual_p && !duplicate_virtual_error_issued_p)
22954             {
22955               cp_parser_error (parser,
22956                                "%<virtual%> specified more than once in base-specified");
22957               duplicate_virtual_error_issued_p = true;
22958             }
22959
22960           virtual_p = true;
22961
22962           /* Consume the `virtual' token.  */
22963           cp_lexer_consume_token (parser->lexer);
22964
22965           break;
22966
22967         case RID_PUBLIC:
22968         case RID_PROTECTED:
22969         case RID_PRIVATE:
22970           /* If more than one access specifier appears, issue an
22971              error.  */
22972           if (access != access_default_node
22973               && !duplicate_access_error_issued_p)
22974             {
22975               cp_parser_error (parser,
22976                                "more than one access specifier in base-specified");
22977               duplicate_access_error_issued_p = true;
22978             }
22979
22980           access = ridpointers[(int) token->keyword];
22981
22982           /* Consume the access-specifier.  */
22983           cp_lexer_consume_token (parser->lexer);
22984
22985           break;
22986
22987         default:
22988           done = true;
22989           break;
22990         }
22991     }
22992   /* It is not uncommon to see programs mechanically, erroneously, use
22993      the 'typename' keyword to denote (dependent) qualified types
22994      as base classes.  */
22995   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22996     {
22997       token = cp_lexer_peek_token (parser->lexer);
22998       if (!processing_template_decl)
22999         error_at (token->location,
23000                   "keyword %<typename%> not allowed outside of templates");
23001       else
23002         error_at (token->location,
23003                   "keyword %<typename%> not allowed in this context "
23004                   "(the base class is implicitly a type)");
23005       cp_lexer_consume_token (parser->lexer);
23006     }
23007
23008   /* Look for the optional `::' operator.  */
23009   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23010   /* Look for the nested-name-specifier.  The simplest way to
23011      implement:
23012
23013        [temp.res]
23014
23015        The keyword `typename' is not permitted in a base-specifier or
23016        mem-initializer; in these contexts a qualified name that
23017        depends on a template-parameter is implicitly assumed to be a
23018        type name.
23019
23020      is to pretend that we have seen the `typename' keyword at this
23021      point.  */
23022   cp_parser_nested_name_specifier_opt (parser,
23023                                        /*typename_keyword_p=*/true,
23024                                        /*check_dependency_p=*/true,
23025                                        typename_type,
23026                                        /*is_declaration=*/true);
23027   /* If the base class is given by a qualified name, assume that names
23028      we see are type names or templates, as appropriate.  */
23029   class_scope_p = (parser->scope && TYPE_P (parser->scope));
23030   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23031
23032   if (!parser->scope
23033       && cp_lexer_next_token_is_decltype (parser->lexer))
23034     /* DR 950 allows decltype as a base-specifier.  */
23035     type = cp_parser_decltype (parser);
23036   else
23037     {
23038       /* Otherwise, look for the class-name.  */
23039       type = cp_parser_class_name (parser,
23040                                    class_scope_p,
23041                                    template_p,
23042                                    typename_type,
23043                                    /*check_dependency_p=*/true,
23044                                    /*class_head_p=*/false,
23045                                    /*is_declaration=*/true);
23046       type = TREE_TYPE (type);
23047     }
23048
23049   if (type == error_mark_node)
23050     return error_mark_node;
23051
23052   return finish_base_specifier (type, access, virtual_p);
23053 }
23054
23055 /* Exception handling [gram.exception] */
23056
23057 /* Parse an (optional) noexcept-specification.
23058
23059    noexcept-specification:
23060      noexcept ( constant-expression ) [opt]
23061
23062    If no noexcept-specification is present, returns NULL_TREE.
23063    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23064    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23065    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
23066    Otherwise, returns a noexcept specification unless RETURN_COND is true,
23067    in which case a boolean condition is returned instead.  */
23068
23069 static tree
23070 cp_parser_noexcept_specification_opt (cp_parser* parser,
23071                                       bool require_constexpr,
23072                                       bool* consumed_expr,
23073                                       bool return_cond)
23074 {
23075   cp_token *token;
23076   const char *saved_message;
23077
23078   /* Peek at the next token.  */
23079   token = cp_lexer_peek_token (parser->lexer);
23080
23081   /* Is it a noexcept-specification?  */
23082   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23083     {
23084       tree expr;
23085       cp_lexer_consume_token (parser->lexer);
23086
23087       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23088         {
23089           cp_lexer_consume_token (parser->lexer);
23090
23091           if (require_constexpr)
23092             {
23093               /* Types may not be defined in an exception-specification.  */
23094               saved_message = parser->type_definition_forbidden_message;
23095               parser->type_definition_forbidden_message
23096               = G_("types may not be defined in an exception-specification");
23097
23098               expr = cp_parser_constant_expression (parser);
23099
23100               /* Restore the saved message.  */
23101               parser->type_definition_forbidden_message = saved_message;
23102             }
23103           else
23104             {
23105               expr = cp_parser_expression (parser);
23106               *consumed_expr = true;
23107             }
23108
23109           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23110         }
23111       else
23112         {
23113           expr = boolean_true_node;
23114           if (!require_constexpr)
23115             *consumed_expr = false;
23116         }
23117
23118       /* We cannot build a noexcept-spec right away because this will check
23119          that expr is a constexpr.  */
23120       if (!return_cond)
23121         return build_noexcept_spec (expr, tf_warning_or_error);
23122       else
23123         return expr;
23124     }
23125   else
23126     return NULL_TREE;
23127 }
23128
23129 /* Parse an (optional) exception-specification.
23130
23131    exception-specification:
23132      throw ( type-id-list [opt] )
23133
23134    Returns a TREE_LIST representing the exception-specification.  The
23135    TREE_VALUE of each node is a type.  */
23136
23137 static tree
23138 cp_parser_exception_specification_opt (cp_parser* parser)
23139 {
23140   cp_token *token;
23141   tree type_id_list;
23142   const char *saved_message;
23143
23144   /* Peek at the next token.  */
23145   token = cp_lexer_peek_token (parser->lexer);
23146
23147   /* Is it a noexcept-specification?  */
23148   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23149                                                       false);
23150   if (type_id_list != NULL_TREE)
23151     return type_id_list;
23152
23153   /* If it's not `throw', then there's no exception-specification.  */
23154   if (!cp_parser_is_keyword (token, RID_THROW))
23155     return NULL_TREE;
23156
23157 #if 0
23158   /* Enable this once a lot of code has transitioned to noexcept?  */
23159   if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23160     warning (OPT_Wdeprecated, "dynamic exception specifications are "
23161              "deprecated in C++0x; use %<noexcept%> instead");
23162 #endif
23163
23164   /* Consume the `throw'.  */
23165   cp_lexer_consume_token (parser->lexer);
23166
23167   /* Look for the `('.  */
23168   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23169
23170   /* Peek at the next token.  */
23171   token = cp_lexer_peek_token (parser->lexer);
23172   /* If it's not a `)', then there is a type-id-list.  */
23173   if (token->type != CPP_CLOSE_PAREN)
23174     {
23175       /* Types may not be defined in an exception-specification.  */
23176       saved_message = parser->type_definition_forbidden_message;
23177       parser->type_definition_forbidden_message
23178         = G_("types may not be defined in an exception-specification");
23179       /* Parse the type-id-list.  */
23180       type_id_list = cp_parser_type_id_list (parser);
23181       /* Restore the saved message.  */
23182       parser->type_definition_forbidden_message = saved_message;
23183     }
23184   else
23185     type_id_list = empty_except_spec;
23186
23187   /* Look for the `)'.  */
23188   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23189
23190   return type_id_list;
23191 }
23192
23193 /* Parse an (optional) type-id-list.
23194
23195    type-id-list:
23196      type-id ... [opt]
23197      type-id-list , type-id ... [opt]
23198
23199    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
23200    in the order that the types were presented.  */
23201
23202 static tree
23203 cp_parser_type_id_list (cp_parser* parser)
23204 {
23205   tree types = NULL_TREE;
23206
23207   while (true)
23208     {
23209       cp_token *token;
23210       tree type;
23211
23212       token = cp_lexer_peek_token (parser->lexer);
23213
23214       /* Get the next type-id.  */
23215       type = cp_parser_type_id (parser);
23216       /* Check for invalid 'auto'.  */
23217       if (flag_concepts && type_uses_auto (type))
23218         {
23219           error_at (token->location,
23220                     "invalid use of %<auto%> in exception-specification");
23221           type = error_mark_node;
23222         }
23223       /* Parse the optional ellipsis. */
23224       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23225         {
23226           /* Consume the `...'. */
23227           cp_lexer_consume_token (parser->lexer);
23228
23229           /* Turn the type into a pack expansion expression. */
23230           type = make_pack_expansion (type);
23231         }
23232       /* Add it to the list.  */
23233       types = add_exception_specifier (types, type, /*complain=*/1);
23234       /* Peek at the next token.  */
23235       token = cp_lexer_peek_token (parser->lexer);
23236       /* If it is not a `,', we are done.  */
23237       if (token->type != CPP_COMMA)
23238         break;
23239       /* Consume the `,'.  */
23240       cp_lexer_consume_token (parser->lexer);
23241     }
23242
23243   return nreverse (types);
23244 }
23245
23246 /* Parse a try-block.
23247
23248    try-block:
23249      try compound-statement handler-seq  */
23250
23251 static tree
23252 cp_parser_try_block (cp_parser* parser)
23253 {
23254   tree try_block;
23255
23256   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23257   if (parser->in_function_body
23258       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23259     error ("%<try%> in %<constexpr%> function");
23260
23261   try_block = begin_try_block ();
23262   cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23263   finish_try_block (try_block);
23264   cp_parser_handler_seq (parser);
23265   finish_handler_sequence (try_block);
23266
23267   return try_block;
23268 }
23269
23270 /* Parse a function-try-block.
23271
23272    function-try-block:
23273      try ctor-initializer [opt] function-body handler-seq  */
23274
23275 static bool
23276 cp_parser_function_try_block (cp_parser* parser)
23277 {
23278   tree compound_stmt;
23279   tree try_block;
23280   bool ctor_initializer_p;
23281
23282   /* Look for the `try' keyword.  */
23283   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23284     return false;
23285   /* Let the rest of the front end know where we are.  */
23286   try_block = begin_function_try_block (&compound_stmt);
23287   /* Parse the function-body.  */
23288   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23289     (parser, /*in_function_try_block=*/true);
23290   /* We're done with the `try' part.  */
23291   finish_function_try_block (try_block);
23292   /* Parse the handlers.  */
23293   cp_parser_handler_seq (parser);
23294   /* We're done with the handlers.  */
23295   finish_function_handler_sequence (try_block, compound_stmt);
23296
23297   return ctor_initializer_p;
23298 }
23299
23300 /* Parse a handler-seq.
23301
23302    handler-seq:
23303      handler handler-seq [opt]  */
23304
23305 static void
23306 cp_parser_handler_seq (cp_parser* parser)
23307 {
23308   while (true)
23309     {
23310       cp_token *token;
23311
23312       /* Parse the handler.  */
23313       cp_parser_handler (parser);
23314       /* Peek at the next token.  */
23315       token = cp_lexer_peek_token (parser->lexer);
23316       /* If it's not `catch' then there are no more handlers.  */
23317       if (!cp_parser_is_keyword (token, RID_CATCH))
23318         break;
23319     }
23320 }
23321
23322 /* Parse a handler.
23323
23324    handler:
23325      catch ( exception-declaration ) compound-statement  */
23326
23327 static void
23328 cp_parser_handler (cp_parser* parser)
23329 {
23330   tree handler;
23331   tree declaration;
23332
23333   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23334   handler = begin_handler ();
23335   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23336   declaration = cp_parser_exception_declaration (parser);
23337   finish_handler_parms (declaration, handler);
23338   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23339   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23340   finish_handler (handler);
23341 }
23342
23343 /* Parse an exception-declaration.
23344
23345    exception-declaration:
23346      type-specifier-seq declarator
23347      type-specifier-seq abstract-declarator
23348      type-specifier-seq
23349      ...
23350
23351    Returns a VAR_DECL for the declaration, or NULL_TREE if the
23352    ellipsis variant is used.  */
23353
23354 static tree
23355 cp_parser_exception_declaration (cp_parser* parser)
23356 {
23357   cp_decl_specifier_seq type_specifiers;
23358   cp_declarator *declarator;
23359   const char *saved_message;
23360
23361   /* If it's an ellipsis, it's easy to handle.  */
23362   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23363     {
23364       /* Consume the `...' token.  */
23365       cp_lexer_consume_token (parser->lexer);
23366       return NULL_TREE;
23367     }
23368
23369   /* Types may not be defined in exception-declarations.  */
23370   saved_message = parser->type_definition_forbidden_message;
23371   parser->type_definition_forbidden_message
23372     = G_("types may not be defined in exception-declarations");
23373
23374   /* Parse the type-specifier-seq.  */
23375   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23376                                 /*is_trailing_return=*/false,
23377                                 &type_specifiers);
23378   /* If it's a `)', then there is no declarator.  */
23379   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23380     declarator = NULL;
23381   else
23382     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23383                                        /*ctor_dtor_or_conv_p=*/NULL,
23384                                        /*parenthesized_p=*/NULL,
23385                                        /*member_p=*/false,
23386                                        /*friend_p=*/false);
23387
23388   /* Restore the saved message.  */
23389   parser->type_definition_forbidden_message = saved_message;
23390
23391   if (!type_specifiers.any_specifiers_p)
23392     return error_mark_node;
23393
23394   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23395 }
23396
23397 /* Parse a throw-expression.
23398
23399    throw-expression:
23400      throw assignment-expression [opt]
23401
23402    Returns a THROW_EXPR representing the throw-expression.  */
23403
23404 static tree
23405 cp_parser_throw_expression (cp_parser* parser)
23406 {
23407   tree expression;
23408   cp_token* token;
23409
23410   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23411   token = cp_lexer_peek_token (parser->lexer);
23412   /* Figure out whether or not there is an assignment-expression
23413      following the "throw" keyword.  */
23414   if (token->type == CPP_COMMA
23415       || token->type == CPP_SEMICOLON
23416       || token->type == CPP_CLOSE_PAREN
23417       || token->type == CPP_CLOSE_SQUARE
23418       || token->type == CPP_CLOSE_BRACE
23419       || token->type == CPP_COLON)
23420     expression = NULL_TREE;
23421   else
23422     expression = cp_parser_assignment_expression (parser);
23423
23424   return build_throw (expression);
23425 }
23426
23427 /* GNU Extensions */
23428
23429 /* Parse an (optional) asm-specification.
23430
23431    asm-specification:
23432      asm ( string-literal )
23433
23434    If the asm-specification is present, returns a STRING_CST
23435    corresponding to the string-literal.  Otherwise, returns
23436    NULL_TREE.  */
23437
23438 static tree
23439 cp_parser_asm_specification_opt (cp_parser* parser)
23440 {
23441   cp_token *token;
23442   tree asm_specification;
23443
23444   /* Peek at the next token.  */
23445   token = cp_lexer_peek_token (parser->lexer);
23446   /* If the next token isn't the `asm' keyword, then there's no
23447      asm-specification.  */
23448   if (!cp_parser_is_keyword (token, RID_ASM))
23449     return NULL_TREE;
23450
23451   /* Consume the `asm' token.  */
23452   cp_lexer_consume_token (parser->lexer);
23453   /* Look for the `('.  */
23454   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23455
23456   /* Look for the string-literal.  */
23457   asm_specification = cp_parser_string_literal (parser, false, false);
23458
23459   /* Look for the `)'.  */
23460   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23461
23462   return asm_specification;
23463 }
23464
23465 /* Parse an asm-operand-list.
23466
23467    asm-operand-list:
23468      asm-operand
23469      asm-operand-list , asm-operand
23470
23471    asm-operand:
23472      string-literal ( expression )
23473      [ string-literal ] string-literal ( expression )
23474
23475    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
23476    each node is the expression.  The TREE_PURPOSE is itself a
23477    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23478    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23479    is a STRING_CST for the string literal before the parenthesis. Returns
23480    ERROR_MARK_NODE if any of the operands are invalid.  */
23481
23482 static tree
23483 cp_parser_asm_operand_list (cp_parser* parser)
23484 {
23485   tree asm_operands = NULL_TREE;
23486   bool invalid_operands = false;
23487
23488   while (true)
23489     {
23490       tree string_literal;
23491       tree expression;
23492       tree name;
23493
23494       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23495         {
23496           /* Consume the `[' token.  */
23497           cp_lexer_consume_token (parser->lexer);
23498           /* Read the operand name.  */
23499           name = cp_parser_identifier (parser);
23500           if (name != error_mark_node)
23501             name = build_string (IDENTIFIER_LENGTH (name),
23502                                  IDENTIFIER_POINTER (name));
23503           /* Look for the closing `]'.  */
23504           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23505         }
23506       else
23507         name = NULL_TREE;
23508       /* Look for the string-literal.  */
23509       string_literal = cp_parser_string_literal (parser, false, false);
23510
23511       /* Look for the `('.  */
23512       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23513       /* Parse the expression.  */
23514       expression = cp_parser_expression (parser);
23515       /* Look for the `)'.  */
23516       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23517
23518       if (name == error_mark_node 
23519           || string_literal == error_mark_node 
23520           || expression == error_mark_node)
23521         invalid_operands = true;
23522
23523       /* Add this operand to the list.  */
23524       asm_operands = tree_cons (build_tree_list (name, string_literal),
23525                                 expression,
23526                                 asm_operands);
23527       /* If the next token is not a `,', there are no more
23528          operands.  */
23529       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23530         break;
23531       /* Consume the `,'.  */
23532       cp_lexer_consume_token (parser->lexer);
23533     }
23534
23535   return invalid_operands ? error_mark_node : nreverse (asm_operands);
23536 }
23537
23538 /* Parse an asm-clobber-list.
23539
23540    asm-clobber-list:
23541      string-literal
23542      asm-clobber-list , string-literal
23543
23544    Returns a TREE_LIST, indicating the clobbers in the order that they
23545    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
23546
23547 static tree
23548 cp_parser_asm_clobber_list (cp_parser* parser)
23549 {
23550   tree clobbers = NULL_TREE;
23551
23552   while (true)
23553     {
23554       tree string_literal;
23555
23556       /* Look for the string literal.  */
23557       string_literal = cp_parser_string_literal (parser, false, false);
23558       /* Add it to the list.  */
23559       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23560       /* If the next token is not a `,', then the list is
23561          complete.  */
23562       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23563         break;
23564       /* Consume the `,' token.  */
23565       cp_lexer_consume_token (parser->lexer);
23566     }
23567
23568   return clobbers;
23569 }
23570
23571 /* Parse an asm-label-list.
23572
23573    asm-label-list:
23574      identifier
23575      asm-label-list , identifier
23576
23577    Returns a TREE_LIST, indicating the labels in the order that they
23578    appeared.  The TREE_VALUE of each node is a label.  */
23579
23580 static tree
23581 cp_parser_asm_label_list (cp_parser* parser)
23582 {
23583   tree labels = NULL_TREE;
23584
23585   while (true)
23586     {
23587       tree identifier, label, name;
23588
23589       /* Look for the identifier.  */
23590       identifier = cp_parser_identifier (parser);
23591       if (!error_operand_p (identifier))
23592         {
23593           label = lookup_label (identifier);
23594           if (TREE_CODE (label) == LABEL_DECL)
23595             {
23596               TREE_USED (label) = 1;
23597               check_goto (label);
23598               name = build_string (IDENTIFIER_LENGTH (identifier),
23599                                    IDENTIFIER_POINTER (identifier));
23600               labels = tree_cons (name, label, labels);
23601             }
23602         }
23603       /* If the next token is not a `,', then the list is
23604          complete.  */
23605       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23606         break;
23607       /* Consume the `,' token.  */
23608       cp_lexer_consume_token (parser->lexer);
23609     }
23610
23611   return nreverse (labels);
23612 }
23613
23614 /* Return TRUE iff the next tokens in the stream are possibly the
23615    beginning of a GNU extension attribute. */
23616
23617 static bool
23618 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23619 {
23620   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23621 }
23622
23623 /* Return TRUE iff the next tokens in the stream are possibly the
23624    beginning of a standard C++-11 attribute specifier.  */
23625
23626 static bool
23627 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23628 {
23629   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23630 }
23631
23632 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23633    beginning of a standard C++-11 attribute specifier.  */
23634
23635 static bool
23636 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23637 {
23638   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23639
23640   return (cxx_dialect >= cxx11
23641           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23642               || (token->type == CPP_OPEN_SQUARE
23643                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23644                   && token->type == CPP_OPEN_SQUARE)));
23645 }
23646
23647 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23648    beginning of a GNU extension attribute.  */
23649
23650 static bool
23651 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23652 {
23653   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23654
23655   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23656 }
23657
23658 /* Return true iff the next tokens can be the beginning of either a
23659    GNU attribute list, or a standard C++11 attribute sequence.  */
23660
23661 static bool
23662 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23663 {
23664   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23665           || cp_next_tokens_can_be_std_attribute_p (parser));
23666 }
23667
23668 /* Return true iff the next Nth tokens can be the beginning of either
23669    a GNU attribute list, or a standard C++11 attribute sequence.  */
23670
23671 static bool
23672 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23673 {
23674   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23675           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23676 }
23677
23678 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23679    of GNU attributes, or return NULL.  */
23680
23681 static tree
23682 cp_parser_attributes_opt (cp_parser *parser)
23683 {
23684   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23685       return cp_parser_gnu_attributes_opt (parser);
23686   return cp_parser_std_attribute_spec_seq (parser);
23687 }
23688
23689 #define CILK_SIMD_FN_CLAUSE_MASK                                  \
23690         ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH)   \
23691          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR)       \
23692          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM)      \
23693          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK)         \
23694          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23695
23696 /* Parses the Cilk Plus SIMD-enabled function's attribute.  Syntax:
23697    vector [(<clauses>)]  */
23698
23699 static void
23700 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23701 {  
23702   bool first_p = parser->cilk_simd_fn_info == NULL;
23703   cp_token *token = v_token;
23704   if (first_p)
23705     {
23706       parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23707       parser->cilk_simd_fn_info->error_seen = false;
23708       parser->cilk_simd_fn_info->fndecl_seen = false;
23709       parser->cilk_simd_fn_info->tokens = vNULL;
23710     }
23711   int paren_scope = 0;
23712   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23713     {
23714       cp_lexer_consume_token (parser->lexer);
23715       v_token = cp_lexer_peek_token (parser->lexer);
23716       paren_scope++;
23717     }
23718   while (paren_scope > 0)
23719     {
23720       token = cp_lexer_peek_token (parser->lexer);
23721       if (token->type == CPP_OPEN_PAREN)
23722         paren_scope++;
23723       else if (token->type == CPP_CLOSE_PAREN)
23724         paren_scope--;
23725       /* Do not push the last ')'  */
23726       if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23727         cp_lexer_consume_token (parser->lexer);
23728     }
23729
23730   token->type = CPP_PRAGMA_EOL;
23731   parser->lexer->next_token = token;
23732   cp_lexer_consume_token (parser->lexer);
23733
23734   struct cp_token_cache *cp
23735     = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23736   parser->cilk_simd_fn_info->tokens.safe_push (cp);
23737 }
23738
23739 /* Parse an (optional) series of attributes.
23740
23741    attributes:
23742      attributes attribute
23743
23744    attribute:
23745      __attribute__ (( attribute-list [opt] ))
23746
23747    The return value is as for cp_parser_gnu_attribute_list.  */
23748
23749 static tree
23750 cp_parser_gnu_attributes_opt (cp_parser* parser)
23751 {
23752   tree attributes = NULL_TREE;
23753
23754   while (true)
23755     {
23756       cp_token *token;
23757       tree attribute_list;
23758       bool ok = true;
23759
23760       /* Peek at the next token.  */
23761       token = cp_lexer_peek_token (parser->lexer);
23762       /* If it's not `__attribute__', then we're done.  */
23763       if (token->keyword != RID_ATTRIBUTE)
23764         break;
23765
23766       /* Consume the `__attribute__' keyword.  */
23767       cp_lexer_consume_token (parser->lexer);
23768       /* Look for the two `(' tokens.  */
23769       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23770       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23771
23772       /* Peek at the next token.  */
23773       token = cp_lexer_peek_token (parser->lexer);
23774       if (token->type != CPP_CLOSE_PAREN)
23775         /* Parse the attribute-list.  */
23776         attribute_list = cp_parser_gnu_attribute_list (parser);
23777       else
23778         /* If the next token is a `)', then there is no attribute
23779            list.  */
23780         attribute_list = NULL;
23781
23782       /* Look for the two `)' tokens.  */
23783       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23784         ok = false;
23785       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23786         ok = false;
23787       if (!ok)
23788         cp_parser_skip_to_end_of_statement (parser);
23789
23790       /* Add these new attributes to the list.  */
23791       attributes = chainon (attributes, attribute_list);
23792     }
23793
23794   return attributes;
23795 }
23796
23797 /* Parse a GNU attribute-list.
23798
23799    attribute-list:
23800      attribute
23801      attribute-list , attribute
23802
23803    attribute:
23804      identifier
23805      identifier ( identifier )
23806      identifier ( identifier , expression-list )
23807      identifier ( expression-list )
23808
23809    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
23810    to an attribute.  The TREE_PURPOSE of each node is the identifier
23811    indicating which attribute is in use.  The TREE_VALUE represents
23812    the arguments, if any.  */
23813
23814 static tree
23815 cp_parser_gnu_attribute_list (cp_parser* parser)
23816 {
23817   tree attribute_list = NULL_TREE;
23818   bool save_translate_strings_p = parser->translate_strings_p;
23819
23820   parser->translate_strings_p = false;
23821   while (true)
23822     {
23823       cp_token *token;
23824       tree identifier;
23825       tree attribute;
23826
23827       /* Look for the identifier.  We also allow keywords here; for
23828          example `__attribute__ ((const))' is legal.  */
23829       token = cp_lexer_peek_token (parser->lexer);
23830       if (token->type == CPP_NAME
23831           || token->type == CPP_KEYWORD)
23832         {
23833           tree arguments = NULL_TREE;
23834
23835           /* Consume the token, but save it since we need it for the
23836              SIMD enabled function parsing.  */
23837           cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23838
23839           /* Save away the identifier that indicates which attribute
23840              this is.  */
23841           identifier = (token->type == CPP_KEYWORD) 
23842             /* For keywords, use the canonical spelling, not the
23843                parsed identifier.  */
23844             ? ridpointers[(int) token->keyword]
23845             : id_token->u.value;
23846           
23847           attribute = build_tree_list (identifier, NULL_TREE);
23848
23849           /* Peek at the next token.  */
23850           token = cp_lexer_peek_token (parser->lexer);
23851           /* If it's an `(', then parse the attribute arguments.  */
23852           if (token->type == CPP_OPEN_PAREN)
23853             {
23854               vec<tree, va_gc> *vec;
23855               int attr_flag = (attribute_takes_identifier_p (identifier)
23856                                ? id_attr : normal_attr);
23857               if (is_cilkplus_vector_p (identifier))
23858                 {
23859                   cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23860                   continue;
23861                 }
23862               else
23863                 vec = cp_parser_parenthesized_expression_list 
23864                   (parser, attr_flag, /*cast_p=*/false, 
23865                    /*allow_expansion_p=*/false, 
23866                    /*non_constant_p=*/NULL);
23867               if (vec == NULL)
23868                 arguments = error_mark_node;
23869               else
23870                 {
23871                   arguments = build_tree_list_vec (vec);
23872                   release_tree_vector (vec);
23873                 }
23874               /* Save the arguments away.  */
23875               TREE_VALUE (attribute) = arguments;
23876             }
23877           else if (is_cilkplus_vector_p (identifier))
23878             {
23879               cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23880               continue;
23881             }
23882
23883           if (arguments != error_mark_node)
23884             {
23885               /* Add this attribute to the list.  */
23886               TREE_CHAIN (attribute) = attribute_list;
23887               attribute_list = attribute;
23888             }
23889
23890           token = cp_lexer_peek_token (parser->lexer);
23891         }
23892       /* Now, look for more attributes.  If the next token isn't a
23893          `,', we're done.  */
23894       if (token->type != CPP_COMMA)
23895         break;
23896
23897       /* Consume the comma and keep going.  */
23898       cp_lexer_consume_token (parser->lexer);
23899     }
23900   parser->translate_strings_p = save_translate_strings_p;
23901
23902   /* We built up the list in reverse order.  */
23903   return nreverse (attribute_list);
23904 }
23905
23906 /*  Parse a standard C++11 attribute.
23907
23908     The returned representation is a TREE_LIST which TREE_PURPOSE is
23909     the scoped name of the attribute, and the TREE_VALUE is its
23910     arguments list.
23911
23912     Note that the scoped name of the attribute is itself a TREE_LIST
23913     which TREE_PURPOSE is the namespace of the attribute, and
23914     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
23915     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23916     and which TREE_PURPOSE is directly the attribute name.
23917
23918     Clients of the attribute code should use get_attribute_namespace
23919     and get_attribute_name to get the actual namespace and name of
23920     attributes, regardless of their being GNU or C++11 attributes.
23921
23922     attribute:
23923       attribute-token attribute-argument-clause [opt]
23924
23925     attribute-token:
23926       identifier
23927       attribute-scoped-token
23928
23929     attribute-scoped-token:
23930       attribute-namespace :: identifier
23931
23932     attribute-namespace:
23933       identifier
23934
23935     attribute-argument-clause:
23936       ( balanced-token-seq )
23937
23938     balanced-token-seq:
23939       balanced-token [opt]
23940       balanced-token-seq balanced-token
23941
23942     balanced-token:
23943       ( balanced-token-seq )
23944       [ balanced-token-seq ]
23945       { balanced-token-seq }.  */
23946
23947 static tree
23948 cp_parser_std_attribute (cp_parser *parser)
23949 {
23950   tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23951   cp_token *token;
23952
23953   /* First, parse name of the attribute, a.k.a attribute-token.  */
23954
23955   token = cp_lexer_peek_token (parser->lexer);
23956   if (token->type == CPP_NAME)
23957     attr_id = token->u.value;
23958   else if (token->type == CPP_KEYWORD)
23959     attr_id = ridpointers[(int) token->keyword];
23960   else if (token->flags & NAMED_OP)
23961     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23962
23963   if (attr_id == NULL_TREE)
23964     return NULL_TREE;
23965
23966   cp_lexer_consume_token (parser->lexer);
23967
23968   token = cp_lexer_peek_token (parser->lexer);
23969   if (token->type == CPP_SCOPE)
23970     {
23971       /* We are seeing a scoped attribute token.  */
23972
23973       cp_lexer_consume_token (parser->lexer);
23974       attr_ns = attr_id;
23975
23976       token = cp_lexer_consume_token (parser->lexer);
23977       if (token->type == CPP_NAME)
23978         attr_id = token->u.value;
23979       else if (token->type == CPP_KEYWORD)
23980         attr_id = ridpointers[(int) token->keyword];
23981       else
23982         {
23983           error_at (token->location,
23984                     "expected an identifier for the attribute name");
23985           return error_mark_node;
23986         }
23987       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23988                                    NULL_TREE);
23989       token = cp_lexer_peek_token (parser->lexer);
23990     }
23991   else
23992     {
23993       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23994                                    NULL_TREE);
23995       /* C++11 noreturn attribute is equivalent to GNU's.  */
23996       if (is_attribute_p ("noreturn", attr_id))
23997         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23998       /* C++14 deprecated attribute is equivalent to GNU's.  */
23999       else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
24000         {
24001           if (cxx_dialect == cxx11)
24002             pedwarn (token->location, OPT_Wpedantic,
24003                      "%<deprecated%> is a C++14 feature;"
24004                      " use %<gnu::deprecated%>");
24005           TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24006         }
24007       /* Transactional Memory TS optimize_for_synchronized attribute is
24008          equivalent to GNU transaction_callable.  */
24009       else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24010         TREE_PURPOSE (attribute)
24011           = get_identifier ("transaction_callable");
24012       /* Transactional Memory attributes are GNU attributes.  */
24013       else if (tm_attr_to_mask (attr_id))
24014         TREE_PURPOSE (attribute) = attr_id;
24015     }
24016
24017   /* Now parse the optional argument clause of the attribute.  */
24018
24019   if (token->type != CPP_OPEN_PAREN)
24020     return attribute;
24021
24022   {
24023     vec<tree, va_gc> *vec;
24024     int attr_flag = normal_attr;
24025
24026     if (attr_ns == get_identifier ("gnu")
24027         && attribute_takes_identifier_p (attr_id))
24028       /* A GNU attribute that takes an identifier in parameter.  */
24029       attr_flag = id_attr;
24030
24031     vec = cp_parser_parenthesized_expression_list
24032       (parser, attr_flag, /*cast_p=*/false,
24033        /*allow_expansion_p=*/true,
24034        /*non_constant_p=*/NULL);
24035     if (vec == NULL)
24036       arguments = error_mark_node;
24037     else
24038       {
24039         arguments = build_tree_list_vec (vec);
24040         release_tree_vector (vec);
24041       }
24042
24043     if (arguments == error_mark_node)
24044       attribute = error_mark_node;
24045     else
24046       TREE_VALUE (attribute) = arguments;
24047   }
24048
24049   return attribute;
24050 }
24051
24052 /* Check that the attribute ATTRIBUTE appears at most once in the
24053    attribute-list ATTRIBUTES.  This is enforced for noreturn (7.6.3)
24054    and deprecated (7.6.5).  Note that carries_dependency (7.6.4)
24055    isn't implemented yet in GCC.  */
24056
24057 static void
24058 cp_parser_check_std_attribute (tree attributes, tree attribute)
24059 {
24060   if (attributes)
24061     {
24062       tree name = get_attribute_name (attribute);
24063       if (is_attribute_p ("noreturn", name)
24064           && lookup_attribute ("noreturn", attributes))
24065         error ("attribute noreturn can appear at most once "
24066                "in an attribute-list");
24067       else if (is_attribute_p ("deprecated", name)
24068                && lookup_attribute ("deprecated", attributes))
24069         error ("attribute deprecated can appear at most once "
24070                "in an attribute-list");
24071     }
24072 }
24073
24074 /* Parse a list of standard C++-11 attributes.
24075
24076    attribute-list:
24077      attribute [opt]
24078      attribute-list , attribute[opt]
24079      attribute ...
24080      attribute-list , attribute ...
24081 */
24082
24083 static tree
24084 cp_parser_std_attribute_list (cp_parser *parser)
24085 {
24086   tree attributes = NULL_TREE, attribute = NULL_TREE;
24087   cp_token *token = NULL;
24088
24089   while (true)
24090     {
24091       attribute = cp_parser_std_attribute (parser);
24092       if (attribute == error_mark_node)
24093         break;
24094       if (attribute != NULL_TREE)
24095         {
24096           cp_parser_check_std_attribute (attributes, attribute);
24097           TREE_CHAIN (attribute) = attributes;
24098           attributes = attribute;
24099         }
24100       token = cp_lexer_peek_token (parser->lexer);
24101       if (token->type == CPP_ELLIPSIS)
24102         {
24103           cp_lexer_consume_token (parser->lexer);
24104           if (attribute == NULL_TREE)
24105             error_at (token->location,
24106                       "expected attribute before %<...%>");
24107           else
24108             TREE_VALUE (attribute)
24109               = make_pack_expansion (TREE_VALUE (attribute));
24110           token = cp_lexer_peek_token (parser->lexer);
24111         }
24112       if (token->type != CPP_COMMA)
24113         break;
24114       cp_lexer_consume_token (parser->lexer);
24115     }
24116   attributes = nreverse (attributes);
24117   return attributes;
24118 }
24119
24120 /* Parse a standard C++-11 attribute specifier.
24121
24122    attribute-specifier:
24123      [ [ attribute-list ] ]
24124      alignment-specifier
24125
24126    alignment-specifier:
24127      alignas ( type-id ... [opt] )
24128      alignas ( alignment-expression ... [opt] ).  */
24129
24130 static tree
24131 cp_parser_std_attribute_spec (cp_parser *parser)
24132 {
24133   tree attributes = NULL_TREE;
24134   cp_token *token = cp_lexer_peek_token (parser->lexer);
24135
24136   if (token->type == CPP_OPEN_SQUARE
24137       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24138     {
24139       cp_lexer_consume_token (parser->lexer);
24140       cp_lexer_consume_token (parser->lexer);
24141
24142       attributes = cp_parser_std_attribute_list (parser);
24143
24144       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24145           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24146         cp_parser_skip_to_end_of_statement (parser);
24147       else
24148         /* Warn about parsing c++11 attribute in non-c++1 mode, only
24149            when we are sure that we have actually parsed them.  */
24150         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24151     }
24152   else
24153     {
24154       tree alignas_expr;
24155
24156       /* Look for an alignment-specifier.  */
24157
24158       token = cp_lexer_peek_token (parser->lexer);
24159
24160       if (token->type != CPP_KEYWORD
24161           || token->keyword != RID_ALIGNAS)
24162         return NULL_TREE;
24163
24164       cp_lexer_consume_token (parser->lexer);
24165       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24166
24167       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24168         {
24169           cp_parser_error (parser, "expected %<(%>");
24170           return error_mark_node;
24171         }
24172
24173       cp_parser_parse_tentatively (parser);
24174       alignas_expr = cp_parser_type_id (parser);
24175
24176       if (!cp_parser_parse_definitely (parser))
24177         {
24178           alignas_expr = cp_parser_assignment_expression (parser);
24179           if (alignas_expr == error_mark_node)
24180             cp_parser_skip_to_end_of_statement (parser);
24181           if (alignas_expr == NULL_TREE
24182               || alignas_expr == error_mark_node)
24183             return alignas_expr;
24184         }
24185
24186       alignas_expr = cxx_alignas_expr (alignas_expr);
24187       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24188
24189       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24190         {
24191           cp_lexer_consume_token (parser->lexer);
24192           alignas_expr = make_pack_expansion (alignas_expr);
24193         }
24194
24195       if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24196         {
24197           cp_parser_error (parser, "expected %<)%>");
24198           return error_mark_node;
24199         }
24200
24201       /* Build the C++-11 representation of an 'aligned'
24202          attribute.  */
24203       attributes =
24204         build_tree_list (build_tree_list (get_identifier ("gnu"),
24205                                           get_identifier ("aligned")),
24206                          alignas_expr);
24207     }
24208
24209   return attributes;
24210 }
24211
24212 /* Parse a standard C++-11 attribute-specifier-seq.
24213
24214    attribute-specifier-seq:
24215      attribute-specifier-seq [opt] attribute-specifier
24216  */
24217
24218 static tree
24219 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24220 {
24221   tree attr_specs = NULL_TREE;
24222   tree attr_last = NULL_TREE;
24223
24224   while (true)
24225     {
24226       tree attr_spec = cp_parser_std_attribute_spec (parser);
24227       if (attr_spec == NULL_TREE)
24228         break;
24229       if (attr_spec == error_mark_node)
24230         return error_mark_node;
24231
24232       if (attr_last)
24233         TREE_CHAIN (attr_last) = attr_spec;
24234       else
24235         attr_specs = attr_last = attr_spec;
24236       attr_last = tree_last (attr_last);
24237     }
24238
24239   return attr_specs;
24240 }
24241
24242 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
24243    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
24244    current value of the PEDANTIC flag, regardless of whether or not
24245    the `__extension__' keyword is present.  The caller is responsible
24246    for restoring the value of the PEDANTIC flag.  */
24247
24248 static bool
24249 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24250 {
24251   /* Save the old value of the PEDANTIC flag.  */
24252   *saved_pedantic = pedantic;
24253
24254   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24255     {
24256       /* Consume the `__extension__' token.  */
24257       cp_lexer_consume_token (parser->lexer);
24258       /* We're not being pedantic while the `__extension__' keyword is
24259          in effect.  */
24260       pedantic = 0;
24261
24262       return true;
24263     }
24264
24265   return false;
24266 }
24267
24268 /* Parse a label declaration.
24269
24270    label-declaration:
24271      __label__ label-declarator-seq ;
24272
24273    label-declarator-seq:
24274      identifier , label-declarator-seq
24275      identifier  */
24276
24277 static void
24278 cp_parser_label_declaration (cp_parser* parser)
24279 {
24280   /* Look for the `__label__' keyword.  */
24281   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24282
24283   while (true)
24284     {
24285       tree identifier;
24286
24287       /* Look for an identifier.  */
24288       identifier = cp_parser_identifier (parser);
24289       /* If we failed, stop.  */
24290       if (identifier == error_mark_node)
24291         break;
24292       /* Declare it as a label.  */
24293       finish_label_decl (identifier);
24294       /* If the next token is a `;', stop.  */
24295       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24296         break;
24297       /* Look for the `,' separating the label declarations.  */
24298       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24299     }
24300
24301   /* Look for the final `;'.  */
24302   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24303 }
24304
24305 // -------------------------------------------------------------------------- //
24306 // Requires Clause
24307
24308 // Parse a requires clause.
24309 //
24310 //    requires-clause:
24311 //      'requires' logical-or-expression
24312 //
24313 // The required logical-or-expression must be a constant expression. Note
24314 // that we don't check that the expression is constepxr here. We defer until
24315 // we analyze constraints and then, we only check atomic constraints.
24316 static tree
24317 cp_parser_requires_clause (cp_parser *parser)
24318 {
24319   // Parse the requires clause so that it is not automatically folded.
24320   ++processing_template_decl;
24321   tree expr = cp_parser_binary_expression (parser, false, false,
24322                                            PREC_NOT_OPERATOR, NULL);
24323   if (check_for_bare_parameter_packs (expr))
24324     expr = error_mark_node;
24325   --processing_template_decl;
24326   return expr;
24327 }
24328
24329 // Optionally parse a requires clause:
24330 static tree
24331 cp_parser_requires_clause_opt (cp_parser *parser)
24332 {
24333   cp_token *tok = cp_lexer_peek_token (parser->lexer);
24334   if (tok->keyword != RID_REQUIRES)
24335     {
24336       if (!flag_concepts && tok->type == CPP_NAME
24337           && tok->u.value == ridpointers[RID_REQUIRES])
24338         {
24339           error_at (cp_lexer_peek_token (parser->lexer)->location,
24340                     "%<requires%> only available with -fconcepts");
24341           /* Parse and discard the requires-clause.  */
24342           cp_lexer_consume_token (parser->lexer);
24343           cp_parser_requires_clause (parser);
24344         }
24345       return NULL_TREE;
24346     }
24347   cp_lexer_consume_token (parser->lexer);
24348   return cp_parser_requires_clause (parser);
24349 }
24350
24351
24352 /*---------------------------------------------------------------------------
24353                            Requires expressions
24354 ---------------------------------------------------------------------------*/
24355
24356 /* Parse a requires expression
24357
24358    requirement-expression:
24359        'requires' requirement-parameter-list [opt] requirement-body */
24360 static tree
24361 cp_parser_requires_expression (cp_parser *parser)
24362 {
24363   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24364   location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24365
24366   /* A requires-expression shall appear only within a concept
24367      definition or a requires-clause.
24368
24369      TODO: Implement this diagnostic correctly. */
24370   if (!processing_template_decl)
24371     {
24372       error_at (loc, "a requires expression cannot appear outside a template");
24373       cp_parser_skip_to_end_of_statement (parser);
24374       return error_mark_node;
24375     }
24376
24377   tree parms, reqs;
24378   {
24379     /* Local parameters are delared as variables within the scope
24380        of the expression.  They are not visible past the end of
24381        the expression.  Expressions within the requires-expression
24382        are unevaluated.  */
24383     struct scope_sentinel
24384     {
24385       scope_sentinel ()
24386       {
24387         ++cp_unevaluated_operand;
24388         begin_scope (sk_block, NULL_TREE);
24389       }
24390
24391       ~scope_sentinel ()
24392       {
24393         pop_bindings_and_leave_scope ();
24394         --cp_unevaluated_operand;
24395       }
24396     } s;
24397
24398     /* Parse the optional parameter list. */
24399     if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24400       {
24401         parms = cp_parser_requirement_parameter_list (parser);
24402         if (parms == error_mark_node)
24403           return error_mark_node;
24404       }
24405     else
24406       parms = NULL_TREE;
24407
24408     /* Parse the requirement body. */
24409     reqs = cp_parser_requirement_body (parser);
24410     if (reqs == error_mark_node)
24411       return error_mark_node;
24412   }
24413
24414   /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24415      the parm chain.  */
24416   grokparms (parms, &parms);
24417   return finish_requires_expr (parms, reqs);
24418 }
24419
24420 /* Parse a parameterized requirement.
24421
24422    requirement-parameter-list:
24423        '(' parameter-declaration-clause ')' */
24424 static tree
24425 cp_parser_requirement_parameter_list (cp_parser *parser)
24426 {
24427   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24428     return error_mark_node;
24429
24430   tree parms = cp_parser_parameter_declaration_clause (parser);
24431
24432   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24433     return error_mark_node;
24434
24435   return parms;
24436 }
24437
24438 /* Parse the body of a requirement.
24439
24440    requirement-body:
24441        '{' requirement-list '}' */
24442 static tree
24443 cp_parser_requirement_body (cp_parser *parser)
24444 {
24445   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24446     return error_mark_node;
24447
24448   tree reqs = cp_parser_requirement_list (parser);
24449
24450   if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24451     return error_mark_node;
24452
24453   return reqs;
24454 }
24455
24456 /* Parse a list of requirements.
24457
24458    requirement-list:
24459        requirement
24460        requirement-list ';' requirement[opt] */
24461 static tree
24462 cp_parser_requirement_list (cp_parser *parser)
24463 {
24464   tree result = NULL_TREE;
24465   while (true)
24466     {
24467       tree req = cp_parser_requirement (parser);
24468       if (req == error_mark_node)
24469         return error_mark_node;
24470
24471       result = tree_cons (NULL_TREE, req, result);
24472
24473       /* If we see a semi-colon, consume it. */
24474       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24475         cp_lexer_consume_token (parser->lexer);
24476
24477       /* Stop processing at the end of the list. */
24478       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24479         break;
24480     }
24481
24482   /* Reverse the order of requirements so they are analyzed in
24483      declaration order. */
24484   return nreverse (result);
24485 }
24486
24487 /* Parse a syntactic requirement or type requirement.
24488
24489      requirement:
24490        simple-requirement
24491        compound-requirement
24492        type-requirement
24493        nested-requirement */
24494 static tree
24495 cp_parser_requirement (cp_parser *parser)
24496 {
24497   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24498     return cp_parser_compound_requirement (parser);
24499   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24500     return cp_parser_type_requirement (parser);
24501   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24502     return cp_parser_nested_requirement (parser);
24503   else
24504     return cp_parser_simple_requirement (parser);
24505 }
24506
24507 /* Parse a simple requirement.
24508
24509      simple-requirement:
24510        expression ';' */
24511 static tree
24512 cp_parser_simple_requirement (cp_parser *parser)
24513 {
24514   tree expr = cp_parser_expression (parser, NULL, false, false);
24515   if (!expr || expr == error_mark_node)
24516     return error_mark_node;
24517
24518   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24519     return error_mark_node;
24520
24521   return finish_simple_requirement (expr);
24522 }
24523
24524 /* Parse a type requirement
24525
24526      type-requirement
24527          nested-name-specifier [opt] required-type-name ';'
24528
24529      required-type-name:
24530          type-name
24531          'template' [opt] simple-template-id  */
24532 static tree
24533 cp_parser_type_requirement (cp_parser *parser)
24534 {
24535   cp_lexer_consume_token (parser->lexer);
24536
24537   // Save the scope before parsing name specifiers.
24538   tree saved_scope = parser->scope;
24539   tree saved_object_scope = parser->object_scope;
24540   tree saved_qualifying_scope = parser->qualifying_scope;
24541   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24542   cp_parser_nested_name_specifier_opt (parser,
24543                                        /*typename_keyword_p=*/true,
24544                                        /*check_dependency_p=*/false,
24545                                        /*type_p=*/true,
24546                                        /*is_declaration=*/false);
24547
24548   tree type;
24549   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24550     {
24551       cp_lexer_consume_token (parser->lexer);
24552       type = cp_parser_template_id (parser,
24553                                     /*template_keyword_p=*/true,
24554                                     /*check_dependency=*/false,
24555                                     /*tag_type=*/none_type,
24556                                     /*is_declaration=*/false);
24557       type = make_typename_type (parser->scope, type, typename_type,
24558                                  /*complain=*/tf_error);
24559     }
24560   else
24561    type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24562
24563   if (TREE_CODE (type) == TYPE_DECL)
24564     type = TREE_TYPE (type);
24565
24566   parser->scope = saved_scope;
24567   parser->object_scope = saved_object_scope;
24568   parser->qualifying_scope = saved_qualifying_scope;
24569
24570   if (type == error_mark_node)
24571     cp_parser_skip_to_end_of_statement (parser);
24572
24573   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24574     return error_mark_node;
24575   if (type == error_mark_node)
24576     return error_mark_node;
24577
24578   return finish_type_requirement (type);
24579 }
24580
24581 /* Parse a compound requirement
24582
24583      compound-requirement:
24584          '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24585 static tree
24586 cp_parser_compound_requirement (cp_parser *parser)
24587 {
24588   /* Parse an expression enclosed in '{ }'s. */
24589   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24590     return error_mark_node;
24591
24592   tree expr = cp_parser_expression (parser, NULL, false, false);
24593   if (!expr || expr == error_mark_node)
24594     return error_mark_node;
24595
24596   if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24597     return error_mark_node;
24598
24599   /* Parse the optional noexcept. */
24600   bool noexcept_p = false;
24601   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24602     {
24603       cp_lexer_consume_token (parser->lexer);
24604       noexcept_p = true;
24605     }
24606
24607   /* Parse the optional trailing return type. */
24608   tree type = NULL_TREE;
24609   if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24610     {
24611       cp_lexer_consume_token (parser->lexer);
24612       bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24613       parser->in_result_type_constraint_p = true;
24614       type = cp_parser_trailing_type_id (parser);
24615       parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24616       if (type == error_mark_node)
24617         return error_mark_node;
24618     }
24619
24620   return finish_compound_requirement (expr, type, noexcept_p);
24621 }
24622
24623 /* Parse a nested requirement. This is the same as a requires clause.
24624
24625    nested-requirement:
24626      requires-clause */
24627 static tree
24628 cp_parser_nested_requirement (cp_parser *parser)
24629 {
24630   cp_lexer_consume_token (parser->lexer);
24631   tree req = cp_parser_requires_clause (parser);
24632   if (req == error_mark_node)
24633     return error_mark_node;
24634   return finish_nested_requirement (req);
24635 }
24636
24637 /* Support Functions */
24638
24639 /* Return the appropriate prefer_type argument for lookup_name_real based on
24640    tag_type.  */
24641
24642 static inline int
24643 prefer_type_arg (tag_types tag_type)
24644 {
24645   switch (tag_type)
24646     {
24647     case none_type:  return 0;  // No preference.
24648     case scope_type: return 1;  // Type or namespace.
24649     default:         return 2;  // Type only.
24650     }
24651 }
24652
24653 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24654    NAME should have one of the representations used for an
24655    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24656    is returned.  If PARSER->SCOPE is a dependent type, then a
24657    SCOPE_REF is returned.
24658
24659    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24660    returned; the name was already resolved when the TEMPLATE_ID_EXPR
24661    was formed.  Abstractly, such entities should not be passed to this
24662    function, because they do not need to be looked up, but it is
24663    simpler to check for this special case here, rather than at the
24664    call-sites.
24665
24666    In cases not explicitly covered above, this function returns a
24667    DECL, OVERLOAD, or baselink representing the result of the lookup.
24668    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24669    is returned.
24670
24671    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24672    (e.g., "struct") that was used.  In that case bindings that do not
24673    refer to types are ignored.
24674
24675    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24676    ignored.
24677
24678    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24679    are ignored.
24680
24681    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24682    types.
24683
24684    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24685    TREE_LIST of candidates if name-lookup results in an ambiguity, and
24686    NULL_TREE otherwise.  */
24687
24688 static cp_expr
24689 cp_parser_lookup_name (cp_parser *parser, tree name,
24690                        enum tag_types tag_type,
24691                        bool is_template,
24692                        bool is_namespace,
24693                        bool check_dependency,
24694                        tree *ambiguous_decls,
24695                        location_t name_location)
24696 {
24697   tree decl;
24698   tree object_type = parser->context->object_type;
24699
24700   /* Assume that the lookup will be unambiguous.  */
24701   if (ambiguous_decls)
24702     *ambiguous_decls = NULL_TREE;
24703
24704   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24705      no longer valid.  Note that if we are parsing tentatively, and
24706      the parse fails, OBJECT_TYPE will be automatically restored.  */
24707   parser->context->object_type = NULL_TREE;
24708
24709   if (name == error_mark_node)
24710     return error_mark_node;
24711
24712   /* A template-id has already been resolved; there is no lookup to
24713      do.  */
24714   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24715     return name;
24716   if (BASELINK_P (name))
24717     {
24718       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24719                   == TEMPLATE_ID_EXPR);
24720       return name;
24721     }
24722
24723   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
24724      it should already have been checked to make sure that the name
24725      used matches the type being destroyed.  */
24726   if (TREE_CODE (name) == BIT_NOT_EXPR)
24727     {
24728       tree type;
24729
24730       /* Figure out to which type this destructor applies.  */
24731       if (parser->scope)
24732         type = parser->scope;
24733       else if (object_type)
24734         type = object_type;
24735       else
24736         type = current_class_type;
24737       /* If that's not a class type, there is no destructor.  */
24738       if (!type || !CLASS_TYPE_P (type))
24739         return error_mark_node;
24740       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24741         lazily_declare_fn (sfk_destructor, type);
24742       if (!CLASSTYPE_DESTRUCTORS (type))
24743           return error_mark_node;
24744       /* If it was a class type, return the destructor.  */
24745       return CLASSTYPE_DESTRUCTORS (type);
24746     }
24747
24748   /* By this point, the NAME should be an ordinary identifier.  If
24749      the id-expression was a qualified name, the qualifying scope is
24750      stored in PARSER->SCOPE at this point.  */
24751   gcc_assert (identifier_p (name));
24752
24753   /* Perform the lookup.  */
24754   if (parser->scope)
24755     {
24756       bool dependent_p;
24757
24758       if (parser->scope == error_mark_node)
24759         return error_mark_node;
24760
24761       /* If the SCOPE is dependent, the lookup must be deferred until
24762          the template is instantiated -- unless we are explicitly
24763          looking up names in uninstantiated templates.  Even then, we
24764          cannot look up the name if the scope is not a class type; it
24765          might, for example, be a template type parameter.  */
24766       dependent_p = (TYPE_P (parser->scope)
24767                      && dependent_scope_p (parser->scope));
24768       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24769           && dependent_p)
24770         /* Defer lookup.  */
24771         decl = error_mark_node;
24772       else
24773         {
24774           tree pushed_scope = NULL_TREE;
24775
24776           /* If PARSER->SCOPE is a dependent type, then it must be a
24777              class type, and we must not be checking dependencies;
24778              otherwise, we would have processed this lookup above.  So
24779              that PARSER->SCOPE is not considered a dependent base by
24780              lookup_member, we must enter the scope here.  */
24781           if (dependent_p)
24782             pushed_scope = push_scope (parser->scope);
24783
24784           /* If the PARSER->SCOPE is a template specialization, it
24785              may be instantiated during name lookup.  In that case,
24786              errors may be issued.  Even if we rollback the current
24787              tentative parse, those errors are valid.  */
24788           decl = lookup_qualified_name (parser->scope, name,
24789                                         prefer_type_arg (tag_type),
24790                                         /*complain=*/true);
24791
24792           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24793              lookup result and the nested-name-specifier nominates a class C:
24794                * if the name specified after the nested-name-specifier, when
24795                looked up in C, is the injected-class-name of C (Clause 9), or
24796                * if the name specified after the nested-name-specifier is the
24797                same as the identifier or the simple-template-id's template-
24798                name in the last component of the nested-name-specifier,
24799              the name is instead considered to name the constructor of
24800              class C. [ Note: for example, the constructor is not an
24801              acceptable lookup result in an elaborated-type-specifier so
24802              the constructor would not be used in place of the
24803              injected-class-name. --end note ] Such a constructor name
24804              shall be used only in the declarator-id of a declaration that
24805              names a constructor or in a using-declaration.  */
24806           if (tag_type == none_type
24807               && DECL_SELF_REFERENCE_P (decl)
24808               && same_type_p (DECL_CONTEXT (decl), parser->scope))
24809             decl = lookup_qualified_name (parser->scope, ctor_identifier,
24810                                           prefer_type_arg (tag_type),
24811                                           /*complain=*/true);
24812
24813           /* If we have a single function from a using decl, pull it out.  */
24814           if (TREE_CODE (decl) == OVERLOAD
24815               && !really_overloaded_fn (decl))
24816             decl = OVL_FUNCTION (decl);
24817
24818           if (pushed_scope)
24819             pop_scope (pushed_scope);
24820         }
24821
24822       /* If the scope is a dependent type and either we deferred lookup or
24823          we did lookup but didn't find the name, rememeber the name.  */
24824       if (decl == error_mark_node && TYPE_P (parser->scope)
24825           && dependent_type_p (parser->scope))
24826         {
24827           if (tag_type)
24828             {
24829               tree type;
24830
24831               /* The resolution to Core Issue 180 says that `struct
24832                  A::B' should be considered a type-name, even if `A'
24833                  is dependent.  */
24834               type = make_typename_type (parser->scope, name, tag_type,
24835                                          /*complain=*/tf_error);
24836               if (type != error_mark_node)
24837                 decl = TYPE_NAME (type);
24838             }
24839           else if (is_template
24840                    && (cp_parser_next_token_ends_template_argument_p (parser)
24841                        || cp_lexer_next_token_is (parser->lexer,
24842                                                   CPP_CLOSE_PAREN)))
24843             decl = make_unbound_class_template (parser->scope,
24844                                                 name, NULL_TREE,
24845                                                 /*complain=*/tf_error);
24846           else
24847             decl = build_qualified_name (/*type=*/NULL_TREE,
24848                                          parser->scope, name,
24849                                          is_template);
24850         }
24851       parser->qualifying_scope = parser->scope;
24852       parser->object_scope = NULL_TREE;
24853     }
24854   else if (object_type)
24855     {
24856       /* Look up the name in the scope of the OBJECT_TYPE, unless the
24857          OBJECT_TYPE is not a class.  */
24858       if (CLASS_TYPE_P (object_type))
24859         /* If the OBJECT_TYPE is a template specialization, it may
24860            be instantiated during name lookup.  In that case, errors
24861            may be issued.  Even if we rollback the current tentative
24862            parse, those errors are valid.  */
24863         decl = lookup_member (object_type,
24864                               name,
24865                               /*protect=*/0,
24866                               prefer_type_arg (tag_type),
24867                               tf_warning_or_error);
24868       else
24869         decl = NULL_TREE;
24870
24871       if (!decl)
24872         {
24873           /* Look it up in the enclosing context.  */
24874           decl = lookup_name_real (name, prefer_type_arg (tag_type),
24875                                    /*nonclass=*/0,
24876                                    /*block_p=*/true, is_namespace, 0);
24877           /* DR 141 says when looking for a template-name after -> or ., only
24878              consider class templates.  We need to fix our handling of
24879              dependent expressions to implement that properly, but for now
24880              let's ignore namespace-scope function templates.  */
24881           if (decl && is_template && !DECL_TYPE_TEMPLATE_P (decl))
24882             {
24883               tree d = decl;
24884               if (is_overloaded_fn (d))
24885                 d = get_first_fn (d);
24886               if (DECL_P (d) && !DECL_CLASS_SCOPE_P (d))
24887                 decl = NULL_TREE;
24888             }
24889         }
24890       if (object_type == unknown_type_node)
24891         /* The object is type-dependent, so we can't look anything up; we used
24892            this to get the DR 141 behavior.  */
24893         object_type = NULL_TREE;
24894       parser->object_scope = object_type;
24895       parser->qualifying_scope = NULL_TREE;
24896     }
24897   else
24898     {
24899       decl = lookup_name_real (name, prefer_type_arg (tag_type),
24900                                /*nonclass=*/0,
24901                                /*block_p=*/true, is_namespace, 0);
24902       parser->qualifying_scope = NULL_TREE;
24903       parser->object_scope = NULL_TREE;
24904     }
24905
24906   /* If the lookup failed, let our caller know.  */
24907   if (!decl || decl == error_mark_node)
24908     return error_mark_node;
24909
24910   /* Pull out the template from an injected-class-name (or multiple).  */
24911   if (is_template)
24912     decl = maybe_get_template_decl_from_type_decl (decl);
24913
24914   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
24915   if (TREE_CODE (decl) == TREE_LIST)
24916     {
24917       if (ambiguous_decls)
24918         *ambiguous_decls = decl;
24919       /* The error message we have to print is too complicated for
24920          cp_parser_error, so we incorporate its actions directly.  */
24921       if (!cp_parser_simulate_error (parser))
24922         {
24923           error_at (name_location, "reference to %qD is ambiguous",
24924                     name);
24925           print_candidates (decl);
24926         }
24927       return error_mark_node;
24928     }
24929
24930   gcc_assert (DECL_P (decl)
24931               || TREE_CODE (decl) == OVERLOAD
24932               || TREE_CODE (decl) == SCOPE_REF
24933               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24934               || BASELINK_P (decl));
24935
24936   /* If we have resolved the name of a member declaration, check to
24937      see if the declaration is accessible.  When the name resolves to
24938      set of overloaded functions, accessibility is checked when
24939      overload resolution is done.
24940
24941      During an explicit instantiation, access is not checked at all,
24942      as per [temp.explicit].  */
24943   if (DECL_P (decl))
24944     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24945
24946   maybe_record_typedef_use (decl);
24947
24948   return cp_expr (decl, name_location);
24949 }
24950
24951 /* Like cp_parser_lookup_name, but for use in the typical case where
24952    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24953    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
24954
24955 static tree
24956 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24957 {
24958   return cp_parser_lookup_name (parser, name,
24959                                 none_type,
24960                                 /*is_template=*/false,
24961                                 /*is_namespace=*/false,
24962                                 /*check_dependency=*/true,
24963                                 /*ambiguous_decls=*/NULL,
24964                                 location);
24965 }
24966
24967 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24968    the current context, return the TYPE_DECL.  If TAG_NAME_P is
24969    true, the DECL indicates the class being defined in a class-head,
24970    or declared in an elaborated-type-specifier.
24971
24972    Otherwise, return DECL.  */
24973
24974 static tree
24975 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24976 {
24977   /* If the TEMPLATE_DECL is being declared as part of a class-head,
24978      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24979
24980        struct A {
24981          template <typename T> struct B;
24982        };
24983
24984        template <typename T> struct A::B {};
24985
24986      Similarly, in an elaborated-type-specifier:
24987
24988        namespace N { struct X{}; }
24989
24990        struct A {
24991          template <typename T> friend struct N::X;
24992        };
24993
24994      However, if the DECL refers to a class type, and we are in
24995      the scope of the class, then the name lookup automatically
24996      finds the TYPE_DECL created by build_self_reference rather
24997      than a TEMPLATE_DECL.  For example, in:
24998
24999        template <class T> struct S {
25000          S s;
25001        };
25002
25003      there is no need to handle such case.  */
25004
25005   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25006     return DECL_TEMPLATE_RESULT (decl);
25007
25008   return decl;
25009 }
25010
25011 /* If too many, or too few, template-parameter lists apply to the
25012    declarator, issue an error message.  Returns TRUE if all went well,
25013    and FALSE otherwise.  */
25014
25015 static bool
25016 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25017                                                 cp_declarator *declarator,
25018                                                 location_t declarator_location)
25019 {
25020   switch (declarator->kind)
25021     {
25022     case cdk_id:
25023       {
25024         unsigned num_templates = 0;
25025         tree scope = declarator->u.id.qualifying_scope;
25026
25027         if (scope)
25028           num_templates = num_template_headers_for_class (scope);
25029         else if (TREE_CODE (declarator->u.id.unqualified_name)
25030                  == TEMPLATE_ID_EXPR)
25031           /* If the DECLARATOR has the form `X<y>' then it uses one
25032              additional level of template parameters.  */
25033           ++num_templates;
25034
25035         return cp_parser_check_template_parameters 
25036           (parser, num_templates, declarator_location, declarator);
25037       }
25038
25039     case cdk_function:
25040     case cdk_array:
25041     case cdk_pointer:
25042     case cdk_reference:
25043     case cdk_ptrmem:
25044       return (cp_parser_check_declarator_template_parameters
25045               (parser, declarator->declarator, declarator_location));
25046
25047     case cdk_error:
25048       return true;
25049
25050     default:
25051       gcc_unreachable ();
25052     }
25053   return false;
25054 }
25055
25056 /* NUM_TEMPLATES were used in the current declaration.  If that is
25057    invalid, return FALSE and issue an error messages.  Otherwise,
25058    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
25059    declarator and we can print more accurate diagnostics.  */
25060
25061 static bool
25062 cp_parser_check_template_parameters (cp_parser* parser,
25063                                      unsigned num_templates,
25064                                      location_t location,
25065                                      cp_declarator *declarator)
25066 {
25067   /* If there are the same number of template classes and parameter
25068      lists, that's OK.  */
25069   if (parser->num_template_parameter_lists == num_templates)
25070     return true;
25071   /* If there are more, but only one more, then we are referring to a
25072      member template.  That's OK too.  */
25073   if (parser->num_template_parameter_lists == num_templates + 1)
25074     return true;
25075   /* If there are more template classes than parameter lists, we have
25076      something like:
25077
25078        template <class T> void S<T>::R<T>::f ();  */
25079   if (parser->num_template_parameter_lists < num_templates)
25080     {
25081       if (declarator && !current_function_decl)
25082         error_at (location, "specializing member %<%T::%E%> "
25083                   "requires %<template<>%> syntax", 
25084                   declarator->u.id.qualifying_scope,
25085                   declarator->u.id.unqualified_name);
25086       else if (declarator)
25087         error_at (location, "invalid declaration of %<%T::%E%>",
25088                   declarator->u.id.qualifying_scope,
25089                   declarator->u.id.unqualified_name);
25090       else 
25091         error_at (location, "too few template-parameter-lists");
25092       return false;
25093     }
25094   /* Otherwise, there are too many template parameter lists.  We have
25095      something like:
25096
25097      template <class T> template <class U> void S::f();  */
25098   error_at (location, "too many template-parameter-lists");
25099   return false;
25100 }
25101
25102 /* Parse an optional `::' token indicating that the following name is
25103    from the global namespace.  If so, PARSER->SCOPE is set to the
25104    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25105    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25106    Returns the new value of PARSER->SCOPE, if the `::' token is
25107    present, and NULL_TREE otherwise.  */
25108
25109 static tree
25110 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25111 {
25112   cp_token *token;
25113
25114   /* Peek at the next token.  */
25115   token = cp_lexer_peek_token (parser->lexer);
25116   /* If we're looking at a `::' token then we're starting from the
25117      global namespace, not our current location.  */
25118   if (token->type == CPP_SCOPE)
25119     {
25120       /* Consume the `::' token.  */
25121       cp_lexer_consume_token (parser->lexer);
25122       /* Set the SCOPE so that we know where to start the lookup.  */
25123       parser->scope = global_namespace;
25124       parser->qualifying_scope = global_namespace;
25125       parser->object_scope = NULL_TREE;
25126
25127       return parser->scope;
25128     }
25129   else if (!current_scope_valid_p)
25130     {
25131       parser->scope = NULL_TREE;
25132       parser->qualifying_scope = NULL_TREE;
25133       parser->object_scope = NULL_TREE;
25134     }
25135
25136   return NULL_TREE;
25137 }
25138
25139 /* Returns TRUE if the upcoming token sequence is the start of a
25140    constructor declarator.  If FRIEND_P is true, the declarator is
25141    preceded by the `friend' specifier.  */
25142
25143 static bool
25144 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25145 {
25146   bool constructor_p;
25147   bool outside_class_specifier_p;
25148   tree nested_name_specifier;
25149   cp_token *next_token;
25150
25151   /* The common case is that this is not a constructor declarator, so
25152      try to avoid doing lots of work if at all possible.  It's not
25153      valid declare a constructor at function scope.  */
25154   if (parser->in_function_body)
25155     return false;
25156   /* And only certain tokens can begin a constructor declarator.  */
25157   next_token = cp_lexer_peek_token (parser->lexer);
25158   if (next_token->type != CPP_NAME
25159       && next_token->type != CPP_SCOPE
25160       && next_token->type != CPP_NESTED_NAME_SPECIFIER
25161       && next_token->type != CPP_TEMPLATE_ID)
25162     return false;
25163
25164   /* Parse tentatively; we are going to roll back all of the tokens
25165      consumed here.  */
25166   cp_parser_parse_tentatively (parser);
25167   /* Assume that we are looking at a constructor declarator.  */
25168   constructor_p = true;
25169
25170   /* Look for the optional `::' operator.  */
25171   cp_parser_global_scope_opt (parser,
25172                               /*current_scope_valid_p=*/false);
25173   /* Look for the nested-name-specifier.  */
25174   nested_name_specifier
25175     = (cp_parser_nested_name_specifier_opt (parser,
25176                                             /*typename_keyword_p=*/false,
25177                                             /*check_dependency_p=*/false,
25178                                             /*type_p=*/false,
25179                                             /*is_declaration=*/false));
25180
25181   outside_class_specifier_p = (!at_class_scope_p ()
25182                                || !TYPE_BEING_DEFINED (current_class_type)
25183                                || friend_p);
25184
25185   /* Outside of a class-specifier, there must be a
25186      nested-name-specifier.  */
25187   if (!nested_name_specifier && outside_class_specifier_p)
25188     constructor_p = false;
25189   else if (nested_name_specifier == error_mark_node)
25190     constructor_p = false;
25191
25192   /* If we have a class scope, this is easy; DR 147 says that S::S always
25193      names the constructor, and no other qualified name could.  */
25194   if (constructor_p && nested_name_specifier
25195       && CLASS_TYPE_P (nested_name_specifier))
25196     {
25197       tree id = cp_parser_unqualified_id (parser,
25198                                           /*template_keyword_p=*/false,
25199                                           /*check_dependency_p=*/false,
25200                                           /*declarator_p=*/true,
25201                                           /*optional_p=*/false);
25202       if (is_overloaded_fn (id))
25203         id = DECL_NAME (get_first_fn (id));
25204       if (!constructor_name_p (id, nested_name_specifier))
25205         constructor_p = false;
25206     }
25207   /* If we still think that this might be a constructor-declarator,
25208      look for a class-name.  */
25209   else if (constructor_p)
25210     {
25211       /* If we have:
25212
25213            template <typename T> struct S {
25214              S();
25215            };
25216
25217          we must recognize that the nested `S' names a class.  */
25218       tree type_decl;
25219       type_decl = cp_parser_class_name (parser,
25220                                         /*typename_keyword_p=*/false,
25221                                         /*template_keyword_p=*/false,
25222                                         none_type,
25223                                         /*check_dependency_p=*/false,
25224                                         /*class_head_p=*/false,
25225                                         /*is_declaration=*/false);
25226       /* If there was no class-name, then this is not a constructor.
25227          Otherwise, if we are in a class-specifier and we aren't
25228          handling a friend declaration, check that its type matches
25229          current_class_type (c++/38313).  Note: error_mark_node
25230          is left alone for error recovery purposes.  */
25231       constructor_p = (!cp_parser_error_occurred (parser)
25232                        && (outside_class_specifier_p
25233                            || type_decl == error_mark_node
25234                            || same_type_p (current_class_type,
25235                                            TREE_TYPE (type_decl))));
25236
25237       /* If we're still considering a constructor, we have to see a `(',
25238          to begin the parameter-declaration-clause, followed by either a
25239          `)', an `...', or a decl-specifier.  We need to check for a
25240          type-specifier to avoid being fooled into thinking that:
25241
25242            S (f) (int);
25243
25244          is a constructor.  (It is actually a function named `f' that
25245          takes one parameter (of type `int') and returns a value of type
25246          `S'.  */
25247       if (constructor_p
25248           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25249         constructor_p = false;
25250
25251       if (constructor_p
25252           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25253           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25254           /* A parameter declaration begins with a decl-specifier,
25255              which is either the "attribute" keyword, a storage class
25256              specifier, or (usually) a type-specifier.  */
25257           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25258         {
25259           tree type;
25260           tree pushed_scope = NULL_TREE;
25261           unsigned saved_num_template_parameter_lists;
25262
25263           /* Names appearing in the type-specifier should be looked up
25264              in the scope of the class.  */
25265           if (current_class_type)
25266             type = NULL_TREE;
25267           else
25268             {
25269               type = TREE_TYPE (type_decl);
25270               if (TREE_CODE (type) == TYPENAME_TYPE)
25271                 {
25272                   type = resolve_typename_type (type,
25273                                                 /*only_current_p=*/false);
25274                   if (TREE_CODE (type) == TYPENAME_TYPE)
25275                     {
25276                       cp_parser_abort_tentative_parse (parser);
25277                       return false;
25278                     }
25279                 }
25280               pushed_scope = push_scope (type);
25281             }
25282
25283           /* Inside the constructor parameter list, surrounding
25284              template-parameter-lists do not apply.  */
25285           saved_num_template_parameter_lists
25286             = parser->num_template_parameter_lists;
25287           parser->num_template_parameter_lists = 0;
25288
25289           /* Look for the type-specifier.  */
25290           cp_parser_type_specifier (parser,
25291                                     CP_PARSER_FLAGS_NONE,
25292                                     /*decl_specs=*/NULL,
25293                                     /*is_declarator=*/true,
25294                                     /*declares_class_or_enum=*/NULL,
25295                                     /*is_cv_qualifier=*/NULL);
25296
25297           parser->num_template_parameter_lists
25298             = saved_num_template_parameter_lists;
25299
25300           /* Leave the scope of the class.  */
25301           if (pushed_scope)
25302             pop_scope (pushed_scope);
25303
25304           constructor_p = !cp_parser_error_occurred (parser);
25305         }
25306     }
25307
25308   /* We did not really want to consume any tokens.  */
25309   cp_parser_abort_tentative_parse (parser);
25310
25311   return constructor_p;
25312 }
25313
25314 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25315    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
25316    they must be performed once we are in the scope of the function.
25317
25318    Returns the function defined.  */
25319
25320 static tree
25321 cp_parser_function_definition_from_specifiers_and_declarator
25322   (cp_parser* parser,
25323    cp_decl_specifier_seq *decl_specifiers,
25324    tree attributes,
25325    const cp_declarator *declarator)
25326 {
25327   tree fn;
25328   bool success_p;
25329
25330   /* Begin the function-definition.  */
25331   success_p = start_function (decl_specifiers, declarator, attributes);
25332
25333   /* The things we're about to see are not directly qualified by any
25334      template headers we've seen thus far.  */
25335   reset_specialization ();
25336
25337   /* If there were names looked up in the decl-specifier-seq that we
25338      did not check, check them now.  We must wait until we are in the
25339      scope of the function to perform the checks, since the function
25340      might be a friend.  */
25341   perform_deferred_access_checks (tf_warning_or_error);
25342
25343   if (success_p)
25344     {
25345       cp_finalize_omp_declare_simd (parser, current_function_decl);
25346       parser->omp_declare_simd = NULL;
25347       cp_finalize_oacc_routine (parser, current_function_decl, true);
25348       parser->oacc_routine = NULL;
25349     }
25350
25351   if (!success_p)
25352     {
25353       /* Skip the entire function.  */
25354       cp_parser_skip_to_end_of_block_or_statement (parser);
25355       fn = error_mark_node;
25356     }
25357   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25358     {
25359       /* Seen already, skip it.  An error message has already been output.  */
25360       cp_parser_skip_to_end_of_block_or_statement (parser);
25361       fn = current_function_decl;
25362       current_function_decl = NULL_TREE;
25363       /* If this is a function from a class, pop the nested class.  */
25364       if (current_class_name)
25365         pop_nested_class ();
25366     }
25367   else
25368     {
25369       timevar_id_t tv;
25370       if (DECL_DECLARED_INLINE_P (current_function_decl))
25371         tv = TV_PARSE_INLINE;
25372       else
25373         tv = TV_PARSE_FUNC;
25374       timevar_push (tv);
25375       fn = cp_parser_function_definition_after_declarator (parser,
25376                                                          /*inline_p=*/false);
25377       timevar_pop (tv);
25378     }
25379
25380   return fn;
25381 }
25382
25383 /* Parse the part of a function-definition that follows the
25384    declarator.  INLINE_P is TRUE iff this function is an inline
25385    function defined within a class-specifier.
25386
25387    Returns the function defined.  */
25388
25389 static tree
25390 cp_parser_function_definition_after_declarator (cp_parser* parser,
25391                                                 bool inline_p)
25392 {
25393   tree fn;
25394   bool ctor_initializer_p = false;
25395   bool saved_in_unbraced_linkage_specification_p;
25396   bool saved_in_function_body;
25397   unsigned saved_num_template_parameter_lists;
25398   cp_token *token;
25399   bool fully_implicit_function_template_p
25400     = parser->fully_implicit_function_template_p;
25401   parser->fully_implicit_function_template_p = false;
25402   tree implicit_template_parms
25403     = parser->implicit_template_parms;
25404   parser->implicit_template_parms = 0;
25405   cp_binding_level* implicit_template_scope
25406     = parser->implicit_template_scope;
25407   parser->implicit_template_scope = 0;
25408
25409   saved_in_function_body = parser->in_function_body;
25410   parser->in_function_body = true;
25411   /* If the next token is `return', then the code may be trying to
25412      make use of the "named return value" extension that G++ used to
25413      support.  */
25414   token = cp_lexer_peek_token (parser->lexer);
25415   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25416     {
25417       /* Consume the `return' keyword.  */
25418       cp_lexer_consume_token (parser->lexer);
25419       /* Look for the identifier that indicates what value is to be
25420          returned.  */
25421       cp_parser_identifier (parser);
25422       /* Issue an error message.  */
25423       error_at (token->location,
25424                 "named return values are no longer supported");
25425       /* Skip tokens until we reach the start of the function body.  */
25426       while (true)
25427         {
25428           cp_token *token = cp_lexer_peek_token (parser->lexer);
25429           if (token->type == CPP_OPEN_BRACE
25430               || token->type == CPP_EOF
25431               || token->type == CPP_PRAGMA_EOL)
25432             break;
25433           cp_lexer_consume_token (parser->lexer);
25434         }
25435     }
25436   /* The `extern' in `extern "C" void f () { ... }' does not apply to
25437      anything declared inside `f'.  */
25438   saved_in_unbraced_linkage_specification_p
25439     = parser->in_unbraced_linkage_specification_p;
25440   parser->in_unbraced_linkage_specification_p = false;
25441   /* Inside the function, surrounding template-parameter-lists do not
25442      apply.  */
25443   saved_num_template_parameter_lists
25444     = parser->num_template_parameter_lists;
25445   parser->num_template_parameter_lists = 0;
25446
25447   start_lambda_scope (current_function_decl);
25448
25449   /* If the next token is `try', `__transaction_atomic', or
25450      `__transaction_relaxed`, then we are looking at either function-try-block
25451      or function-transaction-block.  Note that all of these include the
25452      function-body.  */
25453   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25454     ctor_initializer_p = cp_parser_function_transaction (parser,
25455         RID_TRANSACTION_ATOMIC);
25456   else if (cp_lexer_next_token_is_keyword (parser->lexer,
25457       RID_TRANSACTION_RELAXED))
25458     ctor_initializer_p = cp_parser_function_transaction (parser,
25459         RID_TRANSACTION_RELAXED);
25460   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25461     ctor_initializer_p = cp_parser_function_try_block (parser);
25462   else
25463     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25464       (parser, /*in_function_try_block=*/false);
25465
25466   finish_lambda_scope ();
25467
25468   /* Finish the function.  */
25469   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25470                         (inline_p ? 2 : 0));
25471   /* Generate code for it, if necessary.  */
25472   expand_or_defer_fn (fn);
25473   /* Restore the saved values.  */
25474   parser->in_unbraced_linkage_specification_p
25475     = saved_in_unbraced_linkage_specification_p;
25476   parser->num_template_parameter_lists
25477     = saved_num_template_parameter_lists;
25478   parser->in_function_body = saved_in_function_body;
25479
25480   parser->fully_implicit_function_template_p
25481     = fully_implicit_function_template_p;
25482   parser->implicit_template_parms
25483     = implicit_template_parms;
25484   parser->implicit_template_scope
25485     = implicit_template_scope;
25486
25487   if (parser->fully_implicit_function_template_p)
25488     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25489
25490   return fn;
25491 }
25492
25493 /* Parse a template-declaration body (following argument list).  */
25494
25495 static void
25496 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25497                                                  tree parameter_list,
25498                                                  bool member_p)
25499 {
25500   tree decl = NULL_TREE;
25501   bool friend_p = false;
25502
25503   /* We just processed one more parameter list.  */
25504   ++parser->num_template_parameter_lists;
25505
25506   /* Get the deferred access checks from the parameter list.  These
25507      will be checked once we know what is being declared, as for a
25508      member template the checks must be performed in the scope of the
25509      class containing the member.  */
25510   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25511
25512   /* Tentatively parse for a new template parameter list, which can either be
25513      the template keyword or a template introduction.  */
25514   if (cp_parser_template_declaration_after_export (parser, member_p))
25515     /* OK */;
25516   else if (cxx_dialect >= cxx11
25517            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25518     decl = cp_parser_alias_declaration (parser);
25519   else
25520     {
25521       /* There are no access checks when parsing a template, as we do not
25522          know if a specialization will be a friend.  */
25523       push_deferring_access_checks (dk_no_check);
25524       cp_token *token = cp_lexer_peek_token (parser->lexer);
25525       decl = cp_parser_single_declaration (parser,
25526                                            checks,
25527                                            member_p,
25528                                            /*explicit_specialization_p=*/false,
25529                                            &friend_p);
25530       pop_deferring_access_checks ();
25531
25532       /* If this is a member template declaration, let the front
25533          end know.  */
25534       if (member_p && !friend_p && decl)
25535         {
25536           if (TREE_CODE (decl) == TYPE_DECL)
25537             cp_parser_check_access_in_redeclaration (decl, token->location);
25538
25539           decl = finish_member_template_decl (decl);
25540         }
25541       else if (friend_p && decl
25542                && DECL_DECLARES_TYPE_P (decl))
25543         make_friend_class (current_class_type, TREE_TYPE (decl),
25544                            /*complain=*/true);
25545     }
25546   /* We are done with the current parameter list.  */
25547   --parser->num_template_parameter_lists;
25548
25549   pop_deferring_access_checks ();
25550
25551   /* Finish up.  */
25552   finish_template_decl (parameter_list);
25553
25554   /* Check the template arguments for a literal operator template.  */
25555   if (decl
25556       && DECL_DECLARES_FUNCTION_P (decl)
25557       && UDLIT_OPER_P (DECL_NAME (decl)))
25558     {
25559       bool ok = true;
25560       if (parameter_list == NULL_TREE)
25561         ok = false;
25562       else
25563         {
25564           int num_parms = TREE_VEC_LENGTH (parameter_list);
25565           if (num_parms == 1)
25566             {
25567               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25568               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25569               if (TREE_TYPE (parm) != char_type_node
25570                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25571                 ok = false;
25572             }
25573           else if (num_parms == 2 && cxx_dialect >= cxx14)
25574             {
25575               tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25576               tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25577               tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25578               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25579               if (parm == error_mark_node
25580                   || TREE_TYPE (parm) != TREE_TYPE (type)
25581                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25582                 ok = false;
25583             }
25584           else
25585             ok = false;
25586         }
25587       if (!ok)
25588         {
25589           if (cxx_dialect >= cxx14)
25590             error ("literal operator template %qD has invalid parameter list."
25591                    "  Expected non-type template argument pack <char...>"
25592                    " or <typename CharT, CharT...>",
25593                    decl);
25594           else
25595             error ("literal operator template %qD has invalid parameter list."
25596                    "  Expected non-type template argument pack <char...>",
25597                    decl);
25598         }
25599     }
25600
25601   /* Register member declarations.  */
25602   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25603     finish_member_declaration (decl);
25604   /* If DECL is a function template, we must return to parse it later.
25605      (Even though there is no definition, there might be default
25606      arguments that need handling.)  */
25607   if (member_p && decl
25608       && DECL_DECLARES_FUNCTION_P (decl))
25609     vec_safe_push (unparsed_funs_with_definitions, decl);
25610 }
25611
25612 /* Parse a template introduction header for a template-declaration.  Returns
25613    false if tentative parse fails.  */
25614
25615 static bool
25616 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25617 {
25618   cp_parser_parse_tentatively (parser);
25619
25620   tree saved_scope = parser->scope;
25621   tree saved_object_scope = parser->object_scope;
25622   tree saved_qualifying_scope = parser->qualifying_scope;
25623
25624   /* Look for the optional `::' operator.  */
25625   cp_parser_global_scope_opt (parser,
25626                               /*current_scope_valid_p=*/false);
25627   /* Look for the nested-name-specifier.  */
25628   cp_parser_nested_name_specifier_opt (parser,
25629                                        /*typename_keyword_p=*/false,
25630                                        /*check_dependency_p=*/true,
25631                                        /*type_p=*/false,
25632                                        /*is_declaration=*/false);
25633
25634   cp_token *token = cp_lexer_peek_token (parser->lexer);
25635   tree concept_name = cp_parser_identifier (parser);
25636
25637   /* Look up the concept for which we will be matching
25638      template parameters.  */
25639   tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25640                                                  token->location);
25641   parser->scope = saved_scope;
25642   parser->object_scope = saved_object_scope;
25643   parser->qualifying_scope = saved_qualifying_scope;
25644
25645   if (concept_name == error_mark_node)
25646     cp_parser_simulate_error (parser);
25647
25648   /* Look for opening brace for introduction.  */
25649   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25650
25651   if (!cp_parser_parse_definitely (parser))
25652     return false;
25653
25654   push_deferring_access_checks (dk_deferred);
25655
25656   /* Build vector of placeholder parameters and grab
25657      matching identifiers.  */
25658   tree introduction_list = cp_parser_introduction_list (parser);
25659
25660   /* The introduction-list shall not be empty.  */
25661   int nargs = TREE_VEC_LENGTH (introduction_list);
25662   if (nargs == 0)
25663     {
25664       error ("empty introduction-list");
25665       return true;
25666     }
25667
25668   /* Look for closing brace for introduction.  */
25669   if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25670     return true;
25671
25672   if (tmpl_decl == error_mark_node)
25673     {
25674       cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25675                                    token->location);
25676       return true;
25677     }
25678
25679   /* Build and associate the constraint.  */
25680   tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25681   if (parms && parms != error_mark_node)
25682     {
25683       cp_parser_template_declaration_after_parameters (parser, parms,
25684                                                        member_p);
25685       return true;
25686     }
25687
25688   error_at (token->location, "no matching concept for template-introduction");
25689   return true;
25690 }
25691
25692 /* Parse a normal template-declaration following the template keyword.  */
25693
25694 static void
25695 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25696 {
25697   tree parameter_list;
25698   bool need_lang_pop;
25699   location_t location = input_location;
25700
25701   /* Look for the `<' token.  */
25702   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25703     return;
25704   if (at_class_scope_p () && current_function_decl)
25705     {
25706       /* 14.5.2.2 [temp.mem]
25707
25708          A local class shall not have member templates.  */
25709       error_at (location,
25710                 "invalid declaration of member template in local class");
25711       cp_parser_skip_to_end_of_block_or_statement (parser);
25712       return;
25713     }
25714   /* [temp]
25715
25716      A template ... shall not have C linkage.  */
25717   if (current_lang_name == lang_name_c)
25718     {
25719       error_at (location, "template with C linkage");
25720       /* Give it C++ linkage to avoid confusing other parts of the
25721          front end.  */
25722       push_lang_context (lang_name_cplusplus);
25723       need_lang_pop = true;
25724     }
25725   else
25726     need_lang_pop = false;
25727
25728   /* We cannot perform access checks on the template parameter
25729      declarations until we know what is being declared, just as we
25730      cannot check the decl-specifier list.  */
25731   push_deferring_access_checks (dk_deferred);
25732
25733   /* If the next token is `>', then we have an invalid
25734      specialization.  Rather than complain about an invalid template
25735      parameter, issue an error message here.  */
25736   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25737     {
25738       cp_parser_error (parser, "invalid explicit specialization");
25739       begin_specialization ();
25740       parameter_list = NULL_TREE;
25741     }
25742   else
25743     {
25744       /* Parse the template parameters.  */
25745       parameter_list = cp_parser_template_parameter_list (parser);
25746     }
25747
25748   /* Look for the `>'.  */
25749   cp_parser_skip_to_end_of_template_parameter_list (parser);
25750
25751   /* Manage template requirements */
25752   if (flag_concepts)
25753   {
25754     tree reqs = get_shorthand_constraints (current_template_parms);
25755     if (tree r = cp_parser_requires_clause_opt (parser))
25756       reqs = conjoin_constraints (reqs, normalize_expression (r));
25757     TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25758   }
25759
25760   cp_parser_template_declaration_after_parameters (parser, parameter_list,
25761                                                    member_p);
25762
25763   /* For the erroneous case of a template with C linkage, we pushed an
25764      implicit C++ linkage scope; exit that scope now.  */
25765   if (need_lang_pop)
25766     pop_lang_context ();
25767 }
25768
25769 /* Parse a template-declaration, assuming that the `export' (and
25770    `extern') keywords, if present, has already been scanned.  MEMBER_P
25771    is as for cp_parser_template_declaration.  */
25772
25773 static bool
25774 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25775 {
25776   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25777     {
25778       cp_lexer_consume_token (parser->lexer);
25779       cp_parser_explicit_template_declaration (parser, member_p);
25780       return true;
25781     }
25782   else if (flag_concepts)
25783     return cp_parser_template_introduction (parser, member_p);
25784
25785   return false;
25786 }
25787
25788 /* Perform the deferred access checks from a template-parameter-list.
25789    CHECKS is a TREE_LIST of access checks, as returned by
25790    get_deferred_access_checks.  */
25791
25792 static void
25793 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25794 {
25795   ++processing_template_parmlist;
25796   perform_access_checks (checks, tf_warning_or_error);
25797   --processing_template_parmlist;
25798 }
25799
25800 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25801    `function-definition' sequence that follows a template header.
25802    If MEMBER_P is true, this declaration appears in a class scope.
25803
25804    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
25805    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
25806
25807 static tree
25808 cp_parser_single_declaration (cp_parser* parser,
25809                               vec<deferred_access_check, va_gc> *checks,
25810                               bool member_p,
25811                               bool explicit_specialization_p,
25812                               bool* friend_p)
25813 {
25814   int declares_class_or_enum;
25815   tree decl = NULL_TREE;
25816   cp_decl_specifier_seq decl_specifiers;
25817   bool function_definition_p = false;
25818   cp_token *decl_spec_token_start;
25819
25820   /* This function is only used when processing a template
25821      declaration.  */
25822   gcc_assert (innermost_scope_kind () == sk_template_parms
25823               || innermost_scope_kind () == sk_template_spec);
25824
25825   /* Defer access checks until we know what is being declared.  */
25826   push_deferring_access_checks (dk_deferred);
25827
25828   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25829      alternative.  */
25830   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25831   cp_parser_decl_specifier_seq (parser,
25832                                 CP_PARSER_FLAGS_OPTIONAL,
25833                                 &decl_specifiers,
25834                                 &declares_class_or_enum);
25835   if (friend_p)
25836     *friend_p = cp_parser_friend_p (&decl_specifiers);
25837
25838   /* There are no template typedefs.  */
25839   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25840     {
25841       error_at (decl_spec_token_start->location,
25842                 "template declaration of %<typedef%>");
25843       decl = error_mark_node;
25844     }
25845
25846   /* Gather up the access checks that occurred the
25847      decl-specifier-seq.  */
25848   stop_deferring_access_checks ();
25849
25850   /* Check for the declaration of a template class.  */
25851   if (declares_class_or_enum)
25852     {
25853       if (cp_parser_declares_only_class_p (parser)
25854           || (declares_class_or_enum & 2))
25855         {
25856           // If this is a declaration, but not a definition, associate
25857           // any constraints with the type declaration. Constraints
25858           // are associated with definitions in cp_parser_class_specifier.
25859           if (declares_class_or_enum == 1)
25860             associate_classtype_constraints (decl_specifiers.type);
25861
25862           decl = shadow_tag (&decl_specifiers);
25863
25864           /* In this case:
25865
25866                struct C {
25867                  friend template <typename T> struct A<T>::B;
25868                };
25869
25870              A<T>::B will be represented by a TYPENAME_TYPE, and
25871              therefore not recognized by shadow_tag.  */
25872           if (friend_p && *friend_p
25873               && !decl
25874               && decl_specifiers.type
25875               && TYPE_P (decl_specifiers.type))
25876             decl = decl_specifiers.type;
25877
25878           if (decl && decl != error_mark_node)
25879             decl = TYPE_NAME (decl);
25880           else
25881             decl = error_mark_node;
25882
25883           /* Perform access checks for template parameters.  */
25884           cp_parser_perform_template_parameter_access_checks (checks);
25885
25886           /* Give a helpful diagnostic for
25887                template <class T> struct A { } a;
25888              if we aren't already recovering from an error.  */
25889           if (!cp_parser_declares_only_class_p (parser)
25890               && !seen_error ())
25891             {
25892               error_at (cp_lexer_peek_token (parser->lexer)->location,
25893                         "a class template declaration must not declare "
25894                         "anything else");
25895               cp_parser_skip_to_end_of_block_or_statement (parser);
25896               goto out;
25897             }
25898         }
25899     }
25900
25901   /* Complain about missing 'typename' or other invalid type names.  */
25902   if (!decl_specifiers.any_type_specifiers_p
25903       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25904     {
25905       /* cp_parser_parse_and_diagnose_invalid_type_name calls
25906          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25907          the rest of this declaration.  */
25908       decl = error_mark_node;
25909       goto out;
25910     }
25911
25912   /* If it's not a template class, try for a template function.  If
25913      the next token is a `;', then this declaration does not declare
25914      anything.  But, if there were errors in the decl-specifiers, then
25915      the error might well have come from an attempted class-specifier.
25916      In that case, there's no need to warn about a missing declarator.  */
25917   if (!decl
25918       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25919           || decl_specifiers.type != error_mark_node))
25920     {
25921       decl = cp_parser_init_declarator (parser,
25922                                         &decl_specifiers,
25923                                         checks,
25924                                         /*function_definition_allowed_p=*/true,
25925                                         member_p,
25926                                         declares_class_or_enum,
25927                                         &function_definition_p,
25928                                         NULL, NULL, NULL);
25929
25930     /* 7.1.1-1 [dcl.stc]
25931
25932        A storage-class-specifier shall not be specified in an explicit
25933        specialization...  */
25934     if (decl
25935         && explicit_specialization_p
25936         && decl_specifiers.storage_class != sc_none)
25937       {
25938         error_at (decl_spec_token_start->location,
25939                   "explicit template specialization cannot have a storage class");
25940         decl = error_mark_node;
25941       }
25942
25943     if (decl && VAR_P (decl))
25944       check_template_variable (decl);
25945     }
25946
25947   /* Look for a trailing `;' after the declaration.  */
25948   if (!function_definition_p
25949       && (decl == error_mark_node
25950           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25951     cp_parser_skip_to_end_of_block_or_statement (parser);
25952
25953  out:
25954   pop_deferring_access_checks ();
25955
25956   /* Clear any current qualification; whatever comes next is the start
25957      of something new.  */
25958   parser->scope = NULL_TREE;
25959   parser->qualifying_scope = NULL_TREE;
25960   parser->object_scope = NULL_TREE;
25961
25962   return decl;
25963 }
25964
25965 /* Parse a cast-expression that is not the operand of a unary "&".  */
25966
25967 static cp_expr
25968 cp_parser_simple_cast_expression (cp_parser *parser)
25969 {
25970   return cp_parser_cast_expression (parser, /*address_p=*/false,
25971                                     /*cast_p=*/false, /*decltype*/false, NULL);
25972 }
25973
25974 /* Parse a functional cast to TYPE.  Returns an expression
25975    representing the cast.  */
25976
25977 static cp_expr
25978 cp_parser_functional_cast (cp_parser* parser, tree type)
25979 {
25980   vec<tree, va_gc> *vec;
25981   tree expression_list;
25982   cp_expr cast;
25983   bool nonconst_p;
25984
25985   location_t start_loc = input_location;
25986
25987   if (!type)
25988     type = error_mark_node;
25989
25990   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25991     {
25992       cp_lexer_set_source_position (parser->lexer);
25993       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25994       expression_list = cp_parser_braced_list (parser, &nonconst_p);
25995       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25996       if (TREE_CODE (type) == TYPE_DECL)
25997         type = TREE_TYPE (type);
25998
25999       cast = finish_compound_literal (type, expression_list,
26000                                       tf_warning_or_error);
26001       /* Create a location of the form:
26002             type_name{i, f}
26003             ^~~~~~~~~~~~~~~
26004          with caret == start at the start of the type name,
26005          finishing at the closing brace.  */
26006       location_t finish_loc
26007         = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26008       location_t combined_loc = make_location (start_loc, start_loc,
26009                                                finish_loc);
26010       cast.set_location (combined_loc);
26011       return cast;
26012    }
26013
26014
26015   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26016                                                  /*cast_p=*/true,
26017                                                  /*allow_expansion_p=*/true,
26018                                                  /*non_constant_p=*/NULL);
26019   if (vec == NULL)
26020     expression_list = error_mark_node;
26021   else
26022     {
26023       expression_list = build_tree_list_vec (vec);
26024       release_tree_vector (vec);
26025     }
26026
26027   cast = build_functional_cast (type, expression_list,
26028                                 tf_warning_or_error);
26029   /* [expr.const]/1: In an integral constant expression "only type
26030      conversions to integral or enumeration type can be used".  */
26031   if (TREE_CODE (type) == TYPE_DECL)
26032     type = TREE_TYPE (type);
26033   if (cast != error_mark_node
26034       && !cast_valid_in_integral_constant_expression_p (type)
26035       && cp_parser_non_integral_constant_expression (parser,
26036                                                      NIC_CONSTRUCTOR))
26037     return error_mark_node;
26038
26039   /* Create a location of the form:
26040        float(i)
26041        ^~~~~~~~
26042      with caret == start at the start of the type name,
26043      finishing at the closing paren.  */
26044   location_t finish_loc
26045     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26046   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26047   cast.set_location (combined_loc);
26048   return cast;
26049 }
26050
26051 /* Save the tokens that make up the body of a member function defined
26052    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
26053    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
26054    specifiers applied to the declaration.  Returns the FUNCTION_DECL
26055    for the member function.  */
26056
26057 static tree
26058 cp_parser_save_member_function_body (cp_parser* parser,
26059                                      cp_decl_specifier_seq *decl_specifiers,
26060                                      cp_declarator *declarator,
26061                                      tree attributes)
26062 {
26063   cp_token *first;
26064   cp_token *last;
26065   tree fn;
26066   bool function_try_block = false;
26067
26068   /* Create the FUNCTION_DECL.  */
26069   fn = grokmethod (decl_specifiers, declarator, attributes);
26070   cp_finalize_omp_declare_simd (parser, fn);
26071   cp_finalize_oacc_routine (parser, fn, true);
26072   /* If something went badly wrong, bail out now.  */
26073   if (fn == error_mark_node)
26074     {
26075       /* If there's a function-body, skip it.  */
26076       if (cp_parser_token_starts_function_definition_p
26077           (cp_lexer_peek_token (parser->lexer)))
26078         cp_parser_skip_to_end_of_block_or_statement (parser);
26079       return error_mark_node;
26080     }
26081
26082   /* Remember it, if there default args to post process.  */
26083   cp_parser_save_default_args (parser, fn);
26084
26085   /* Save away the tokens that make up the body of the
26086      function.  */
26087   first = parser->lexer->next_token;
26088
26089   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26090     cp_lexer_consume_token (parser->lexer);
26091   else if (cp_lexer_next_token_is_keyword (parser->lexer,
26092                                            RID_TRANSACTION_ATOMIC))
26093     {
26094       cp_lexer_consume_token (parser->lexer);
26095       /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
26096       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26097           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26098           && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26099               || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26100           && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26101           && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26102         {
26103           cp_lexer_consume_token (parser->lexer);
26104           cp_lexer_consume_token (parser->lexer);
26105           cp_lexer_consume_token (parser->lexer);
26106           cp_lexer_consume_token (parser->lexer);
26107           cp_lexer_consume_token (parser->lexer);
26108         }
26109       else
26110         while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26111                && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26112           {
26113             cp_lexer_consume_token (parser->lexer);
26114             if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26115               break;
26116           }
26117     }
26118
26119   /* Handle function try blocks.  */
26120   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26121     {
26122       cp_lexer_consume_token (parser->lexer);
26123       function_try_block = true;
26124     }
26125   /* We can have braced-init-list mem-initializers before the fn body.  */
26126   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26127     {
26128       cp_lexer_consume_token (parser->lexer);
26129       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26130         {
26131           /* cache_group will stop after an un-nested { } pair, too.  */
26132           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26133             break;
26134
26135           /* variadic mem-inits have ... after the ')'.  */
26136           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26137             cp_lexer_consume_token (parser->lexer);
26138         }
26139     }
26140   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26141   /* Handle function try blocks.  */
26142   if (function_try_block)
26143     while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26144       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26145   last = parser->lexer->next_token;
26146
26147   /* Save away the inline definition; we will process it when the
26148      class is complete.  */
26149   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26150   DECL_PENDING_INLINE_P (fn) = 1;
26151
26152   /* We need to know that this was defined in the class, so that
26153      friend templates are handled correctly.  */
26154   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26155
26156   /* Add FN to the queue of functions to be parsed later.  */
26157   vec_safe_push (unparsed_funs_with_definitions, fn);
26158
26159   return fn;
26160 }
26161
26162 /* Save the tokens that make up the in-class initializer for a non-static
26163    data member.  Returns a DEFAULT_ARG.  */
26164
26165 static tree
26166 cp_parser_save_nsdmi (cp_parser* parser)
26167 {
26168   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26169 }
26170
26171 /* Parse a template-argument-list, as well as the trailing ">" (but
26172    not the opening "<").  See cp_parser_template_argument_list for the
26173    return value.  */
26174
26175 static tree
26176 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26177 {
26178   tree arguments;
26179   tree saved_scope;
26180   tree saved_qualifying_scope;
26181   tree saved_object_scope;
26182   bool saved_greater_than_is_operator_p;
26183   int saved_unevaluated_operand;
26184   int saved_inhibit_evaluation_warnings;
26185
26186   /* [temp.names]
26187
26188      When parsing a template-id, the first non-nested `>' is taken as
26189      the end of the template-argument-list rather than a greater-than
26190      operator.  */
26191   saved_greater_than_is_operator_p
26192     = parser->greater_than_is_operator_p;
26193   parser->greater_than_is_operator_p = false;
26194   /* Parsing the argument list may modify SCOPE, so we save it
26195      here.  */
26196   saved_scope = parser->scope;
26197   saved_qualifying_scope = parser->qualifying_scope;
26198   saved_object_scope = parser->object_scope;
26199   /* We need to evaluate the template arguments, even though this
26200      template-id may be nested within a "sizeof".  */
26201   saved_unevaluated_operand = cp_unevaluated_operand;
26202   cp_unevaluated_operand = 0;
26203   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26204   c_inhibit_evaluation_warnings = 0;
26205   /* Parse the template-argument-list itself.  */
26206   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26207       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26208     arguments = NULL_TREE;
26209   else
26210     arguments = cp_parser_template_argument_list (parser);
26211   /* Look for the `>' that ends the template-argument-list. If we find
26212      a '>>' instead, it's probably just a typo.  */
26213   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26214     {
26215       if (cxx_dialect != cxx98)
26216         {
26217           /* In C++0x, a `>>' in a template argument list or cast
26218              expression is considered to be two separate `>'
26219              tokens. So, change the current token to a `>', but don't
26220              consume it: it will be consumed later when the outer
26221              template argument list (or cast expression) is parsed.
26222              Note that this replacement of `>' for `>>' is necessary
26223              even if we are parsing tentatively: in the tentative
26224              case, after calling
26225              cp_parser_enclosed_template_argument_list we will always
26226              throw away all of the template arguments and the first
26227              closing `>', either because the template argument list
26228              was erroneous or because we are replacing those tokens
26229              with a CPP_TEMPLATE_ID token.  The second `>' (which will
26230              not have been thrown away) is needed either to close an
26231              outer template argument list or to complete a new-style
26232              cast.  */
26233           cp_token *token = cp_lexer_peek_token (parser->lexer);
26234           token->type = CPP_GREATER;
26235         }
26236       else if (!saved_greater_than_is_operator_p)
26237         {
26238           /* If we're in a nested template argument list, the '>>' has
26239             to be a typo for '> >'. We emit the error message, but we
26240             continue parsing and we push a '>' as next token, so that
26241             the argument list will be parsed correctly.  Note that the
26242             global source location is still on the token before the
26243             '>>', so we need to say explicitly where we want it.  */
26244           cp_token *token = cp_lexer_peek_token (parser->lexer);
26245           error_at (token->location, "%<>>%> should be %<> >%> "
26246                     "within a nested template argument list");
26247
26248           token->type = CPP_GREATER;
26249         }
26250       else
26251         {
26252           /* If this is not a nested template argument list, the '>>'
26253             is a typo for '>'. Emit an error message and continue.
26254             Same deal about the token location, but here we can get it
26255             right by consuming the '>>' before issuing the diagnostic.  */
26256           cp_token *token = cp_lexer_consume_token (parser->lexer);
26257           error_at (token->location,
26258                     "spurious %<>>%>, use %<>%> to terminate "
26259                     "a template argument list");
26260         }
26261     }
26262   else
26263     cp_parser_skip_to_end_of_template_parameter_list (parser);
26264   /* The `>' token might be a greater-than operator again now.  */
26265   parser->greater_than_is_operator_p
26266     = saved_greater_than_is_operator_p;
26267   /* Restore the SAVED_SCOPE.  */
26268   parser->scope = saved_scope;
26269   parser->qualifying_scope = saved_qualifying_scope;
26270   parser->object_scope = saved_object_scope;
26271   cp_unevaluated_operand = saved_unevaluated_operand;
26272   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26273
26274   return arguments;
26275 }
26276
26277 /* MEMBER_FUNCTION is a member function, or a friend.  If default
26278    arguments, or the body of the function have not yet been parsed,
26279    parse them now.  */
26280
26281 static void
26282 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26283 {
26284   timevar_push (TV_PARSE_INMETH);
26285   /* If this member is a template, get the underlying
26286      FUNCTION_DECL.  */
26287   if (DECL_FUNCTION_TEMPLATE_P (member_function))
26288     member_function = DECL_TEMPLATE_RESULT (member_function);
26289
26290   /* There should not be any class definitions in progress at this
26291      point; the bodies of members are only parsed outside of all class
26292      definitions.  */
26293   gcc_assert (parser->num_classes_being_defined == 0);
26294   /* While we're parsing the member functions we might encounter more
26295      classes.  We want to handle them right away, but we don't want
26296      them getting mixed up with functions that are currently in the
26297      queue.  */
26298   push_unparsed_function_queues (parser);
26299
26300   /* Make sure that any template parameters are in scope.  */
26301   maybe_begin_member_template_processing (member_function);
26302
26303   /* If the body of the function has not yet been parsed, parse it
26304      now.  */
26305   if (DECL_PENDING_INLINE_P (member_function))
26306     {
26307       tree function_scope;
26308       cp_token_cache *tokens;
26309
26310       /* The function is no longer pending; we are processing it.  */
26311       tokens = DECL_PENDING_INLINE_INFO (member_function);
26312       DECL_PENDING_INLINE_INFO (member_function) = NULL;
26313       DECL_PENDING_INLINE_P (member_function) = 0;
26314
26315       /* If this is a local class, enter the scope of the containing
26316          function.  */
26317       function_scope = current_function_decl;
26318       if (function_scope)
26319         push_function_context ();
26320
26321       /* Push the body of the function onto the lexer stack.  */
26322       cp_parser_push_lexer_for_tokens (parser, tokens);
26323
26324       /* Let the front end know that we going to be defining this
26325          function.  */
26326       start_preparsed_function (member_function, NULL_TREE,
26327                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
26328
26329       /* Don't do access checking if it is a templated function.  */
26330       if (processing_template_decl)
26331         push_deferring_access_checks (dk_no_check);
26332
26333       /* #pragma omp declare reduction needs special parsing.  */
26334       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26335         {
26336           parser->lexer->in_pragma = true;
26337           cp_parser_omp_declare_reduction_exprs (member_function, parser);
26338           finish_function (/*inline*/2);
26339           cp_check_omp_declare_reduction (member_function);
26340         }
26341       else
26342         /* Now, parse the body of the function.  */
26343         cp_parser_function_definition_after_declarator (parser,
26344                                                         /*inline_p=*/true);
26345
26346       if (processing_template_decl)
26347         pop_deferring_access_checks ();
26348
26349       /* Leave the scope of the containing function.  */
26350       if (function_scope)
26351         pop_function_context ();
26352       cp_parser_pop_lexer (parser);
26353     }
26354
26355   /* Remove any template parameters from the symbol table.  */
26356   maybe_end_member_template_processing ();
26357
26358   /* Restore the queue.  */
26359   pop_unparsed_function_queues (parser);
26360   timevar_pop (TV_PARSE_INMETH);
26361 }
26362
26363 /* If DECL contains any default args, remember it on the unparsed
26364    functions queue.  */
26365
26366 static void
26367 cp_parser_save_default_args (cp_parser* parser, tree decl)
26368 {
26369   tree probe;
26370
26371   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26372        probe;
26373        probe = TREE_CHAIN (probe))
26374     if (TREE_PURPOSE (probe))
26375       {
26376         cp_default_arg_entry entry = {current_class_type, decl};
26377         vec_safe_push (unparsed_funs_with_default_args, entry);
26378         break;
26379       }
26380 }
26381
26382 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26383    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
26384    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
26385    from the parameter-type-list.  */
26386
26387 static tree
26388 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26389                                       tree default_arg, tree parmtype)
26390 {
26391   cp_token_cache *tokens;
26392   tree parsed_arg;
26393   bool dummy;
26394
26395   if (default_arg == error_mark_node)
26396     return error_mark_node;
26397
26398   /* Push the saved tokens for the default argument onto the parser's
26399      lexer stack.  */
26400   tokens = DEFARG_TOKENS (default_arg);
26401   cp_parser_push_lexer_for_tokens (parser, tokens);
26402
26403   start_lambda_scope (decl);
26404
26405   /* Parse the default argument.  */
26406   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26407   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26408     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26409
26410   finish_lambda_scope ();
26411
26412   if (parsed_arg == error_mark_node)
26413     cp_parser_skip_to_end_of_statement (parser);
26414
26415   if (!processing_template_decl)
26416     {
26417       /* In a non-template class, check conversions now.  In a template,
26418          we'll wait and instantiate these as needed.  */
26419       if (TREE_CODE (decl) == PARM_DECL)
26420         parsed_arg = check_default_argument (parmtype, parsed_arg,
26421                                              tf_warning_or_error);
26422       else
26423         parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26424     }
26425
26426   /* If the token stream has not been completely used up, then
26427      there was extra junk after the end of the default
26428      argument.  */
26429   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26430     {
26431       if (TREE_CODE (decl) == PARM_DECL)
26432         cp_parser_error (parser, "expected %<,%>");
26433       else
26434         cp_parser_error (parser, "expected %<;%>");
26435     }
26436
26437   /* Revert to the main lexer.  */
26438   cp_parser_pop_lexer (parser);
26439
26440   return parsed_arg;
26441 }
26442
26443 /* FIELD is a non-static data member with an initializer which we saved for
26444    later; parse it now.  */
26445
26446 static void
26447 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26448 {
26449   tree def;
26450
26451   maybe_begin_member_template_processing (field);
26452
26453   push_unparsed_function_queues (parser);
26454   def = cp_parser_late_parse_one_default_arg (parser, field,
26455                                               DECL_INITIAL (field),
26456                                               NULL_TREE);
26457   pop_unparsed_function_queues (parser);
26458
26459   maybe_end_member_template_processing ();
26460
26461   DECL_INITIAL (field) = def;
26462 }
26463
26464 /* FN is a FUNCTION_DECL which may contains a parameter with an
26465    unparsed DEFAULT_ARG.  Parse the default args now.  This function
26466    assumes that the current scope is the scope in which the default
26467    argument should be processed.  */
26468
26469 static void
26470 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26471 {
26472   bool saved_local_variables_forbidden_p;
26473   tree parm, parmdecl;
26474
26475   /* While we're parsing the default args, we might (due to the
26476      statement expression extension) encounter more classes.  We want
26477      to handle them right away, but we don't want them getting mixed
26478      up with default args that are currently in the queue.  */
26479   push_unparsed_function_queues (parser);
26480
26481   /* Local variable names (and the `this' keyword) may not appear
26482      in a default argument.  */
26483   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26484   parser->local_variables_forbidden_p = true;
26485
26486   push_defarg_context (fn);
26487
26488   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26489          parmdecl = DECL_ARGUMENTS (fn);
26490        parm && parm != void_list_node;
26491        parm = TREE_CHAIN (parm),
26492          parmdecl = DECL_CHAIN (parmdecl))
26493     {
26494       tree default_arg = TREE_PURPOSE (parm);
26495       tree parsed_arg;
26496       vec<tree, va_gc> *insts;
26497       tree copy;
26498       unsigned ix;
26499
26500       if (!default_arg)
26501         continue;
26502
26503       if (TREE_CODE (default_arg) != DEFAULT_ARG)
26504         /* This can happen for a friend declaration for a function
26505            already declared with default arguments.  */
26506         continue;
26507
26508       parsed_arg
26509         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26510                                                 default_arg,
26511                                                 TREE_VALUE (parm));
26512       if (parsed_arg == error_mark_node)
26513         {
26514           continue;
26515         }
26516
26517       TREE_PURPOSE (parm) = parsed_arg;
26518
26519       /* Update any instantiations we've already created.  */
26520       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26521            vec_safe_iterate (insts, ix, &copy); ix++)
26522         TREE_PURPOSE (copy) = parsed_arg;
26523     }
26524
26525   pop_defarg_context ();
26526
26527   /* Make sure no default arg is missing.  */
26528   check_default_args (fn);
26529
26530   /* Restore the state of local_variables_forbidden_p.  */
26531   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26532
26533   /* Restore the queue.  */
26534   pop_unparsed_function_queues (parser);
26535 }
26536
26537 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26538
26539      sizeof ... ( identifier )
26540
26541    where the 'sizeof' token has already been consumed.  */
26542
26543 static tree
26544 cp_parser_sizeof_pack (cp_parser *parser)
26545 {
26546   /* Consume the `...'.  */
26547   cp_lexer_consume_token (parser->lexer);
26548   maybe_warn_variadic_templates ();
26549
26550   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26551   if (paren)
26552     cp_lexer_consume_token (parser->lexer);
26553   else
26554     permerror (cp_lexer_peek_token (parser->lexer)->location,
26555                "%<sizeof...%> argument must be surrounded by parentheses");
26556
26557   cp_token *token = cp_lexer_peek_token (parser->lexer);
26558   tree name = cp_parser_identifier (parser);
26559   if (name == error_mark_node)
26560     return error_mark_node;
26561   /* The name is not qualified.  */
26562   parser->scope = NULL_TREE;
26563   parser->qualifying_scope = NULL_TREE;
26564   parser->object_scope = NULL_TREE;
26565   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26566   if (expr == error_mark_node)
26567     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26568                                  token->location);
26569   if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26570     expr = TREE_TYPE (expr);
26571   else if (TREE_CODE (expr) == CONST_DECL)
26572     expr = DECL_INITIAL (expr);
26573   expr = make_pack_expansion (expr);
26574   PACK_EXPANSION_SIZEOF_P (expr) = true;
26575
26576   if (paren)
26577     cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26578
26579   return expr;
26580 }
26581
26582 /* Parse the operand of `sizeof' (or a similar operator).  Returns
26583    either a TYPE or an expression, depending on the form of the
26584    input.  The KEYWORD indicates which kind of expression we have
26585    encountered.  */
26586
26587 static tree
26588 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26589 {
26590   tree expr = NULL_TREE;
26591   const char *saved_message;
26592   char *tmp;
26593   bool saved_integral_constant_expression_p;
26594   bool saved_non_integral_constant_expression_p;
26595
26596   /* If it's a `...', then we are computing the length of a parameter
26597      pack.  */
26598   if (keyword == RID_SIZEOF
26599       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26600     return cp_parser_sizeof_pack (parser);
26601
26602   /* Types cannot be defined in a `sizeof' expression.  Save away the
26603      old message.  */
26604   saved_message = parser->type_definition_forbidden_message;
26605   /* And create the new one.  */
26606   tmp = concat ("types may not be defined in %<",
26607                 IDENTIFIER_POINTER (ridpointers[keyword]),
26608                 "%> expressions", NULL);
26609   parser->type_definition_forbidden_message = tmp;
26610
26611   /* The restrictions on constant-expressions do not apply inside
26612      sizeof expressions.  */
26613   saved_integral_constant_expression_p
26614     = parser->integral_constant_expression_p;
26615   saved_non_integral_constant_expression_p
26616     = parser->non_integral_constant_expression_p;
26617   parser->integral_constant_expression_p = false;
26618
26619   /* Do not actually evaluate the expression.  */
26620   ++cp_unevaluated_operand;
26621   ++c_inhibit_evaluation_warnings;
26622   /* If it's a `(', then we might be looking at the type-id
26623      construction.  */
26624   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26625     {
26626       tree type = NULL_TREE;
26627
26628       /* We can't be sure yet whether we're looking at a type-id or an
26629          expression.  */
26630       cp_parser_parse_tentatively (parser);
26631       /* Note: as a GNU Extension, compound literals are considered
26632          postfix-expressions as they are in C99, so they are valid
26633          arguments to sizeof.  See comment in cp_parser_cast_expression
26634          for details.  */
26635       if (cp_parser_compound_literal_p (parser))
26636         cp_parser_simulate_error (parser);
26637       else
26638         {
26639           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26640           parser->in_type_id_in_expr_p = true;
26641           /* Look for the type-id.  */
26642           type = cp_parser_type_id (parser);
26643           /* Look for the closing `)'.  */
26644           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26645           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26646         }
26647
26648       /* If all went well, then we're done.  */
26649       if (cp_parser_parse_definitely (parser))
26650         {
26651           cp_decl_specifier_seq decl_specs;
26652
26653           /* Build a trivial decl-specifier-seq.  */
26654           clear_decl_specs (&decl_specs);
26655           decl_specs.type = type;
26656
26657           /* Call grokdeclarator to figure out what type this is.  */
26658           expr = grokdeclarator (NULL,
26659                                  &decl_specs,
26660                                  TYPENAME,
26661                                  /*initialized=*/0,
26662                                  /*attrlist=*/NULL);
26663         }
26664     }
26665
26666   /* If the type-id production did not work out, then we must be
26667      looking at the unary-expression production.  */
26668   if (!expr)
26669     expr = cp_parser_unary_expression (parser);
26670
26671   /* Go back to evaluating expressions.  */
26672   --cp_unevaluated_operand;
26673   --c_inhibit_evaluation_warnings;
26674
26675   /* Free the message we created.  */
26676   free (tmp);
26677   /* And restore the old one.  */
26678   parser->type_definition_forbidden_message = saved_message;
26679   parser->integral_constant_expression_p
26680     = saved_integral_constant_expression_p;
26681   parser->non_integral_constant_expression_p
26682     = saved_non_integral_constant_expression_p;
26683
26684   return expr;
26685 }
26686
26687 /* If the current declaration has no declarator, return true.  */
26688
26689 static bool
26690 cp_parser_declares_only_class_p (cp_parser *parser)
26691 {
26692   /* If the next token is a `;' or a `,' then there is no
26693      declarator.  */
26694   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26695           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26696 }
26697
26698 /* Update the DECL_SPECS to reflect the storage class indicated by
26699    KEYWORD.  */
26700
26701 static void
26702 cp_parser_set_storage_class (cp_parser *parser,
26703                              cp_decl_specifier_seq *decl_specs,
26704                              enum rid keyword,
26705                              cp_token *token)
26706 {
26707   cp_storage_class storage_class;
26708
26709   if (parser->in_unbraced_linkage_specification_p)
26710     {
26711       error_at (token->location, "invalid use of %qD in linkage specification",
26712                 ridpointers[keyword]);
26713       return;
26714     }
26715   else if (decl_specs->storage_class != sc_none)
26716     {
26717       decl_specs->conflicting_specifiers_p = true;
26718       return;
26719     }
26720
26721   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26722       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26723       && decl_specs->gnu_thread_keyword_p)
26724     {
26725       pedwarn (decl_specs->locations[ds_thread], 0,
26726                 "%<__thread%> before %qD", ridpointers[keyword]);
26727     }
26728
26729   switch (keyword)
26730     {
26731     case RID_AUTO:
26732       storage_class = sc_auto;
26733       break;
26734     case RID_REGISTER:
26735       storage_class = sc_register;
26736       break;
26737     case RID_STATIC:
26738       storage_class = sc_static;
26739       break;
26740     case RID_EXTERN:
26741       storage_class = sc_extern;
26742       break;
26743     case RID_MUTABLE:
26744       storage_class = sc_mutable;
26745       break;
26746     default:
26747       gcc_unreachable ();
26748     }
26749   decl_specs->storage_class = storage_class;
26750   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26751
26752   /* A storage class specifier cannot be applied alongside a typedef 
26753      specifier. If there is a typedef specifier present then set 
26754      conflicting_specifiers_p which will trigger an error later
26755      on in grokdeclarator. */
26756   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26757     decl_specs->conflicting_specifiers_p = true;
26758 }
26759
26760 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
26761    is true, the type is a class or enum definition.  */
26762
26763 static void
26764 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26765                               tree type_spec,
26766                               cp_token *token,
26767                               bool type_definition_p)
26768 {
26769   decl_specs->any_specifiers_p = true;
26770
26771   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26772      (with, for example, in "typedef int wchar_t;") we remember that
26773      this is what happened.  In system headers, we ignore these
26774      declarations so that G++ can work with system headers that are not
26775      C++-safe.  */
26776   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26777       && !type_definition_p
26778       && (type_spec == boolean_type_node
26779           || type_spec == char16_type_node
26780           || type_spec == char32_type_node
26781           || type_spec == wchar_type_node)
26782       && (decl_specs->type
26783           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26784           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26785           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26786           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26787     {
26788       decl_specs->redefined_builtin_type = type_spec;
26789       set_and_check_decl_spec_loc (decl_specs,
26790                                    ds_redefined_builtin_type_spec,
26791                                    token);
26792       if (!decl_specs->type)
26793         {
26794           decl_specs->type = type_spec;
26795           decl_specs->type_definition_p = false;
26796           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26797         }
26798     }
26799   else if (decl_specs->type)
26800     decl_specs->multiple_types_p = true;
26801   else
26802     {
26803       decl_specs->type = type_spec;
26804       decl_specs->type_definition_p = type_definition_p;
26805       decl_specs->redefined_builtin_type = NULL_TREE;
26806       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26807     }
26808 }
26809
26810 /* True iff TOKEN is the GNU keyword __thread.  */
26811
26812 static bool
26813 token_is__thread (cp_token *token)
26814 {
26815   gcc_assert (token->keyword == RID_THREAD);
26816   return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26817 }
26818
26819 /* Set the location for a declarator specifier and check if it is
26820    duplicated.
26821
26822    DECL_SPECS is the sequence of declarator specifiers onto which to
26823    set the location.
26824
26825    DS is the single declarator specifier to set which location  is to
26826    be set onto the existing sequence of declarators.
26827
26828    LOCATION is the location for the declarator specifier to
26829    consider.  */
26830
26831 static void
26832 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26833                              cp_decl_spec ds, cp_token *token)
26834 {
26835   gcc_assert (ds < ds_last);
26836
26837   if (decl_specs == NULL)
26838     return;
26839
26840   source_location location = token->location;
26841
26842   if (decl_specs->locations[ds] == 0)
26843     {
26844       decl_specs->locations[ds] = location;
26845       if (ds == ds_thread)
26846         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26847     }
26848   else
26849     {
26850       if (ds == ds_long)
26851         {
26852           if (decl_specs->locations[ds_long_long] != 0)
26853             error_at (location,
26854                       "%<long long long%> is too long for GCC");
26855           else
26856             {
26857               decl_specs->locations[ds_long_long] = location;
26858               pedwarn_cxx98 (location,
26859                              OPT_Wlong_long, 
26860                              "ISO C++ 1998 does not support %<long long%>");
26861             }
26862         }
26863       else if (ds == ds_thread)
26864         {
26865           bool gnu = token_is__thread (token);
26866           if (gnu != decl_specs->gnu_thread_keyword_p)
26867             error_at (location,
26868                       "both %<__thread%> and %<thread_local%> specified");
26869           else
26870             error_at (location, "duplicate %qD", token->u.value);
26871         }
26872       else
26873         {
26874           static const char *const decl_spec_names[] = {
26875             "signed",
26876             "unsigned",
26877             "short",
26878             "long",
26879             "const",
26880             "volatile",
26881             "restrict",
26882             "inline",
26883             "virtual",
26884             "explicit",
26885             "friend",
26886             "typedef",
26887             "using",
26888             "constexpr",
26889             "__complex"
26890           };
26891           error_at (location,
26892                     "duplicate %qs", decl_spec_names[ds]);
26893         }
26894     }
26895 }
26896
26897 /* Return true iff the declarator specifier DS is present in the
26898    sequence of declarator specifiers DECL_SPECS.  */
26899
26900 bool
26901 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26902                           cp_decl_spec ds)
26903 {
26904   gcc_assert (ds < ds_last);
26905
26906   if (decl_specs == NULL)
26907     return false;
26908
26909   return decl_specs->locations[ds] != 0;
26910 }
26911
26912 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26913    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
26914
26915 static bool
26916 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26917 {
26918   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26919 }
26920
26921 /* Issue an error message indicating that TOKEN_DESC was expected.
26922    If KEYWORD is true, it indicated this function is called by
26923    cp_parser_require_keword and the required token can only be
26924    a indicated keyword. */
26925
26926 static void
26927 cp_parser_required_error (cp_parser *parser,
26928                           required_token token_desc,
26929                           bool keyword)
26930 {
26931   switch (token_desc)
26932     {
26933       case RT_NEW:
26934         cp_parser_error (parser, "expected %<new%>");
26935         return;
26936       case RT_DELETE:
26937         cp_parser_error (parser, "expected %<delete%>");
26938         return;
26939       case RT_RETURN:
26940         cp_parser_error (parser, "expected %<return%>");
26941         return;
26942       case RT_WHILE:
26943         cp_parser_error (parser, "expected %<while%>");
26944         return;
26945       case RT_EXTERN:
26946         cp_parser_error (parser, "expected %<extern%>");
26947         return;
26948       case RT_STATIC_ASSERT:
26949         cp_parser_error (parser, "expected %<static_assert%>");
26950         return;
26951       case RT_DECLTYPE:
26952         cp_parser_error (parser, "expected %<decltype%>");
26953         return;
26954       case RT_OPERATOR:
26955         cp_parser_error (parser, "expected %<operator%>");
26956         return;
26957       case RT_CLASS:
26958         cp_parser_error (parser, "expected %<class%>");
26959         return;
26960       case RT_TEMPLATE:
26961         cp_parser_error (parser, "expected %<template%>");
26962         return;
26963       case RT_NAMESPACE:
26964         cp_parser_error (parser, "expected %<namespace%>");
26965         return;
26966       case RT_USING:
26967         cp_parser_error (parser, "expected %<using%>");
26968         return;
26969       case RT_ASM:
26970         cp_parser_error (parser, "expected %<asm%>");
26971         return;
26972       case RT_TRY:
26973         cp_parser_error (parser, "expected %<try%>");
26974         return;
26975       case RT_CATCH:
26976         cp_parser_error (parser, "expected %<catch%>");
26977         return;
26978       case RT_THROW:
26979         cp_parser_error (parser, "expected %<throw%>");
26980         return;
26981       case RT_LABEL:
26982         cp_parser_error (parser, "expected %<__label__%>");
26983         return;
26984       case RT_AT_TRY:
26985         cp_parser_error (parser, "expected %<@try%>");
26986         return;
26987       case RT_AT_SYNCHRONIZED:
26988         cp_parser_error (parser, "expected %<@synchronized%>");
26989         return;
26990       case RT_AT_THROW:
26991         cp_parser_error (parser, "expected %<@throw%>");
26992         return;
26993       case RT_TRANSACTION_ATOMIC:
26994         cp_parser_error (parser, "expected %<__transaction_atomic%>");
26995         return;
26996       case RT_TRANSACTION_RELAXED:
26997         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26998         return;
26999       default:
27000         break;
27001     }
27002   if (!keyword)
27003     {
27004       switch (token_desc)
27005         {
27006           case RT_SEMICOLON:
27007             cp_parser_error (parser, "expected %<;%>");
27008             return;
27009           case RT_OPEN_PAREN:
27010             cp_parser_error (parser, "expected %<(%>");
27011             return;
27012           case RT_CLOSE_BRACE:
27013             cp_parser_error (parser, "expected %<}%>");
27014             return;
27015           case RT_OPEN_BRACE:
27016             cp_parser_error (parser, "expected %<{%>");
27017             return;
27018           case RT_CLOSE_SQUARE:
27019             cp_parser_error (parser, "expected %<]%>");
27020             return;
27021           case RT_OPEN_SQUARE:
27022             cp_parser_error (parser, "expected %<[%>");
27023             return;
27024           case RT_COMMA:
27025             cp_parser_error (parser, "expected %<,%>");
27026             return;
27027           case RT_SCOPE:
27028             cp_parser_error (parser, "expected %<::%>");
27029             return;
27030           case RT_LESS:
27031             cp_parser_error (parser, "expected %<<%>");
27032             return;
27033           case RT_GREATER:
27034             cp_parser_error (parser, "expected %<>%>");
27035             return;
27036           case RT_EQ:
27037             cp_parser_error (parser, "expected %<=%>");
27038             return;
27039           case RT_ELLIPSIS:
27040             cp_parser_error (parser, "expected %<...%>");
27041             return;
27042           case RT_MULT:
27043             cp_parser_error (parser, "expected %<*%>");
27044             return;
27045           case RT_COMPL:
27046             cp_parser_error (parser, "expected %<~%>");
27047             return;
27048           case RT_COLON:
27049             cp_parser_error (parser, "expected %<:%>");
27050             return;
27051           case RT_COLON_SCOPE:
27052             cp_parser_error (parser, "expected %<:%> or %<::%>");
27053             return;
27054           case RT_CLOSE_PAREN:
27055             cp_parser_error (parser, "expected %<)%>");
27056             return;
27057           case RT_COMMA_CLOSE_PAREN:
27058             cp_parser_error (parser, "expected %<,%> or %<)%>");
27059             return;
27060           case RT_PRAGMA_EOL:
27061             cp_parser_error (parser, "expected end of line");
27062             return;
27063           case RT_NAME:
27064             cp_parser_error (parser, "expected identifier");
27065             return;
27066           case RT_SELECT:
27067             cp_parser_error (parser, "expected selection-statement");
27068             return;
27069           case RT_INTERATION:
27070             cp_parser_error (parser, "expected iteration-statement");
27071             return;
27072           case RT_JUMP:
27073             cp_parser_error (parser, "expected jump-statement");
27074             return;
27075           case RT_CLASS_KEY:
27076             cp_parser_error (parser, "expected class-key");
27077             return;
27078           case RT_CLASS_TYPENAME_TEMPLATE:
27079             cp_parser_error (parser,
27080                  "expected %<class%>, %<typename%>, or %<template%>");
27081             return;
27082           default:
27083             gcc_unreachable ();
27084         }
27085     }
27086   else
27087     gcc_unreachable ();
27088 }
27089
27090
27091
27092 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
27093    issue an error message indicating that TOKEN_DESC was expected.
27094
27095    Returns the token consumed, if the token had the appropriate type.
27096    Otherwise, returns NULL.  */
27097
27098 static cp_token *
27099 cp_parser_require (cp_parser* parser,
27100                    enum cpp_ttype type,
27101                    required_token token_desc)
27102 {
27103   if (cp_lexer_next_token_is (parser->lexer, type))
27104     return cp_lexer_consume_token (parser->lexer);
27105   else
27106     {
27107       /* Output the MESSAGE -- unless we're parsing tentatively.  */
27108       if (!cp_parser_simulate_error (parser))
27109         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27110       return NULL;
27111     }
27112 }
27113
27114 /* An error message is produced if the next token is not '>'.
27115    All further tokens are skipped until the desired token is
27116    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
27117
27118 static void
27119 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27120 {
27121   /* Current level of '< ... >'.  */
27122   unsigned level = 0;
27123   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
27124   unsigned nesting_depth = 0;
27125
27126   /* Are we ready, yet?  If not, issue error message.  */
27127   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27128     return;
27129
27130   /* Skip tokens until the desired token is found.  */
27131   while (true)
27132     {
27133       /* Peek at the next token.  */
27134       switch (cp_lexer_peek_token (parser->lexer)->type)
27135         {
27136         case CPP_LESS:
27137           if (!nesting_depth)
27138             ++level;
27139           break;
27140
27141         case CPP_RSHIFT:
27142           if (cxx_dialect == cxx98)
27143             /* C++0x views the `>>' operator as two `>' tokens, but
27144                C++98 does not. */
27145             break;
27146           else if (!nesting_depth && level-- == 0)
27147             {
27148               /* We've hit a `>>' where the first `>' closes the
27149                  template argument list, and the second `>' is
27150                  spurious.  Just consume the `>>' and stop; we've
27151                  already produced at least one error.  */
27152               cp_lexer_consume_token (parser->lexer);
27153               return;
27154             }
27155           /* Fall through for C++0x, so we handle the second `>' in
27156              the `>>'.  */
27157
27158         case CPP_GREATER:
27159           if (!nesting_depth && level-- == 0)
27160             {
27161               /* We've reached the token we want, consume it and stop.  */
27162               cp_lexer_consume_token (parser->lexer);
27163               return;
27164             }
27165           break;
27166
27167         case CPP_OPEN_PAREN:
27168         case CPP_OPEN_SQUARE:
27169           ++nesting_depth;
27170           break;
27171
27172         case CPP_CLOSE_PAREN:
27173         case CPP_CLOSE_SQUARE:
27174           if (nesting_depth-- == 0)
27175             return;
27176           break;
27177
27178         case CPP_EOF:
27179         case CPP_PRAGMA_EOL:
27180         case CPP_SEMICOLON:
27181         case CPP_OPEN_BRACE:
27182         case CPP_CLOSE_BRACE:
27183           /* The '>' was probably forgotten, don't look further.  */
27184           return;
27185
27186         default:
27187           break;
27188         }
27189
27190       /* Consume this token.  */
27191       cp_lexer_consume_token (parser->lexer);
27192     }
27193 }
27194
27195 /* If the next token is the indicated keyword, consume it.  Otherwise,
27196    issue an error message indicating that TOKEN_DESC was expected.
27197
27198    Returns the token consumed, if the token had the appropriate type.
27199    Otherwise, returns NULL.  */
27200
27201 static cp_token *
27202 cp_parser_require_keyword (cp_parser* parser,
27203                            enum rid keyword,
27204                            required_token token_desc)
27205 {
27206   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27207
27208   if (token && token->keyword != keyword)
27209     {
27210       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
27211       return NULL;
27212     }
27213
27214   return token;
27215 }
27216
27217 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27218    function-definition.  */
27219
27220 static bool
27221 cp_parser_token_starts_function_definition_p (cp_token* token)
27222 {
27223   return (/* An ordinary function-body begins with an `{'.  */
27224           token->type == CPP_OPEN_BRACE
27225           /* A ctor-initializer begins with a `:'.  */
27226           || token->type == CPP_COLON
27227           /* A function-try-block begins with `try'.  */
27228           || token->keyword == RID_TRY
27229           /* A function-transaction-block begins with `__transaction_atomic'
27230              or `__transaction_relaxed'.  */
27231           || token->keyword == RID_TRANSACTION_ATOMIC
27232           || token->keyword == RID_TRANSACTION_RELAXED
27233           /* The named return value extension begins with `return'.  */
27234           || token->keyword == RID_RETURN);
27235 }
27236
27237 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27238    definition.  */
27239
27240 static bool
27241 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27242 {
27243   cp_token *token;
27244
27245   token = cp_lexer_peek_token (parser->lexer);
27246   return (token->type == CPP_OPEN_BRACE
27247           || (token->type == CPP_COLON
27248               && !parser->colon_doesnt_start_class_def_p));
27249 }
27250
27251 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27252    C++0x) ending a template-argument.  */
27253
27254 static bool
27255 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27256 {
27257   cp_token *token;
27258
27259   token = cp_lexer_peek_token (parser->lexer);
27260   return (token->type == CPP_COMMA 
27261           || token->type == CPP_GREATER
27262           || token->type == CPP_ELLIPSIS
27263           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27264 }
27265
27266 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27267    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
27268
27269 static bool
27270 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27271                                                      size_t n)
27272 {
27273   cp_token *token;
27274
27275   token = cp_lexer_peek_nth_token (parser->lexer, n);
27276   if (token->type == CPP_LESS)
27277     return true;
27278   /* Check for the sequence `<::' in the original code. It would be lexed as
27279      `[:', where `[' is a digraph, and there is no whitespace before
27280      `:'.  */
27281   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27282     {
27283       cp_token *token2;
27284       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27285       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27286         return true;
27287     }
27288   return false;
27289 }
27290
27291 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27292    or none_type otherwise.  */
27293
27294 static enum tag_types
27295 cp_parser_token_is_class_key (cp_token* token)
27296 {
27297   switch (token->keyword)
27298     {
27299     case RID_CLASS:
27300       return class_type;
27301     case RID_STRUCT:
27302       return record_type;
27303     case RID_UNION:
27304       return union_type;
27305
27306     default:
27307       return none_type;
27308     }
27309 }
27310
27311 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27312    or none_type otherwise or if the token is null.  */
27313
27314 static enum tag_types
27315 cp_parser_token_is_type_parameter_key (cp_token* token)
27316 {
27317   if (!token)
27318     return none_type;
27319
27320   switch (token->keyword)
27321     {
27322     case RID_CLASS:
27323       return class_type;
27324     case RID_TYPENAME:
27325       return typename_type;
27326
27327     default:
27328       return none_type;
27329     }
27330 }
27331
27332 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
27333
27334 static void
27335 cp_parser_check_class_key (enum tag_types class_key, tree type)
27336 {
27337   if (type == error_mark_node)
27338     return;
27339   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27340     {
27341       if (permerror (input_location, "%qs tag used in naming %q#T",
27342                      class_key == union_type ? "union"
27343                      : class_key == record_type ? "struct" : "class",
27344                      type))
27345         inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27346                 "%q#T was previously declared here", type);
27347     }
27348 }
27349
27350 /* Issue an error message if DECL is redeclared with different
27351    access than its original declaration [class.access.spec/3].
27352    This applies to nested classes and nested class templates.
27353    [class.mem/1].  */
27354
27355 static void
27356 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27357 {
27358   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
27359     return;
27360
27361   if ((TREE_PRIVATE (decl)
27362        != (current_access_specifier == access_private_node))
27363       || (TREE_PROTECTED (decl)
27364           != (current_access_specifier == access_protected_node)))
27365     error_at (location, "%qD redeclared with different access", decl);
27366 }
27367
27368 /* Look for the `template' keyword, as a syntactic disambiguator.
27369    Return TRUE iff it is present, in which case it will be
27370    consumed.  */
27371
27372 static bool
27373 cp_parser_optional_template_keyword (cp_parser *parser)
27374 {
27375   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27376     {
27377       /* In C++98 the `template' keyword can only be used within templates;
27378          outside templates the parser can always figure out what is a
27379          template and what is not.  In C++11,  per the resolution of DR 468,
27380          `template' is allowed in cases where it is not strictly necessary.  */
27381       if (!processing_template_decl
27382           && pedantic && cxx_dialect == cxx98)
27383         {
27384           cp_token *token = cp_lexer_peek_token (parser->lexer);
27385           pedwarn (token->location, OPT_Wpedantic,
27386                    "in C++98 %<template%> (as a disambiguator) is only "
27387                    "allowed within templates");
27388           /* If this part of the token stream is rescanned, the same
27389              error message would be generated.  So, we purge the token
27390              from the stream.  */
27391           cp_lexer_purge_token (parser->lexer);
27392           return false;
27393         }
27394       else
27395         {
27396           /* Consume the `template' keyword.  */
27397           cp_lexer_consume_token (parser->lexer);
27398           return true;
27399         }
27400     }
27401   return false;
27402 }
27403
27404 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
27405    set PARSER->SCOPE, and perform other related actions.  */
27406
27407 static void
27408 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27409 {
27410   struct tree_check *check_value;
27411
27412   /* Get the stored value.  */
27413   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27414   /* Set the scope from the stored value.  */
27415   parser->scope = saved_checks_value (check_value);
27416   parser->qualifying_scope = check_value->qualifying_scope;
27417   parser->object_scope = NULL_TREE;
27418 }
27419
27420 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
27421    encounter the end of a block before what we were looking for.  */
27422
27423 static bool
27424 cp_parser_cache_group (cp_parser *parser,
27425                        enum cpp_ttype end,
27426                        unsigned depth)
27427 {
27428   while (true)
27429     {
27430       cp_token *token = cp_lexer_peek_token (parser->lexer);
27431
27432       /* Abort a parenthesized expression if we encounter a semicolon.  */
27433       if ((end == CPP_CLOSE_PAREN || depth == 0)
27434           && token->type == CPP_SEMICOLON)
27435         return true;
27436       /* If we've reached the end of the file, stop.  */
27437       if (token->type == CPP_EOF
27438           || (end != CPP_PRAGMA_EOL
27439               && token->type == CPP_PRAGMA_EOL))
27440         return true;
27441       if (token->type == CPP_CLOSE_BRACE && depth == 0)
27442         /* We've hit the end of an enclosing block, so there's been some
27443            kind of syntax error.  */
27444         return true;
27445
27446       /* Consume the token.  */
27447       cp_lexer_consume_token (parser->lexer);
27448       /* See if it starts a new group.  */
27449       if (token->type == CPP_OPEN_BRACE)
27450         {
27451           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27452           /* In theory this should probably check end == '}', but
27453              cp_parser_save_member_function_body needs it to exit
27454              after either '}' or ')' when called with ')'.  */
27455           if (depth == 0)
27456             return false;
27457         }
27458       else if (token->type == CPP_OPEN_PAREN)
27459         {
27460           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27461           if (depth == 0 && end == CPP_CLOSE_PAREN)
27462             return false;
27463         }
27464       else if (token->type == CPP_PRAGMA)
27465         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27466       else if (token->type == end)
27467         return false;
27468     }
27469 }
27470
27471 /* Like above, for caching a default argument or NSDMI.  Both of these are
27472    terminated by a non-nested comma, but it can be unclear whether or not a
27473    comma is nested in a template argument list unless we do more parsing.
27474    In order to handle this ambiguity, when we encounter a ',' after a '<'
27475    we try to parse what follows as a parameter-declaration-list (in the
27476    case of a default argument) or a member-declarator (in the case of an
27477    NSDMI).  If that succeeds, then we stop caching.  */
27478
27479 static tree
27480 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27481 {
27482   unsigned depth = 0;
27483   int maybe_template_id = 0;
27484   cp_token *first_token;
27485   cp_token *token;
27486   tree default_argument;
27487
27488   /* Add tokens until we have processed the entire default
27489      argument.  We add the range [first_token, token).  */
27490   first_token = cp_lexer_peek_token (parser->lexer);
27491   if (first_token->type == CPP_OPEN_BRACE)
27492     {
27493       /* For list-initialization, this is straightforward.  */
27494       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27495       token = cp_lexer_peek_token (parser->lexer);
27496     }
27497   else while (true)
27498     {
27499       bool done = false;
27500
27501       /* Peek at the next token.  */
27502       token = cp_lexer_peek_token (parser->lexer);
27503       /* What we do depends on what token we have.  */
27504       switch (token->type)
27505         {
27506           /* In valid code, a default argument must be
27507              immediately followed by a `,' `)', or `...'.  */
27508         case CPP_COMMA:
27509           if (depth == 0 && maybe_template_id)
27510             {
27511               /* If we've seen a '<', we might be in a
27512                  template-argument-list.  Until Core issue 325 is
27513                  resolved, we don't know how this situation ought
27514                  to be handled, so try to DTRT.  We check whether
27515                  what comes after the comma is a valid parameter
27516                  declaration list.  If it is, then the comma ends
27517                  the default argument; otherwise the default
27518                  argument continues.  */
27519               bool error = false;
27520               cp_token *peek;
27521
27522               /* Set ITALP so cp_parser_parameter_declaration_list
27523                  doesn't decide to commit to this parse.  */
27524               bool saved_italp = parser->in_template_argument_list_p;
27525               parser->in_template_argument_list_p = true;
27526
27527               cp_parser_parse_tentatively (parser);
27528
27529               if (nsdmi)
27530                 {
27531                   /* Parse declarators until we reach a non-comma or
27532                      somthing that cannot be an initializer.
27533                      Just checking whether we're looking at a single
27534                      declarator is insufficient.  Consider:
27535                        int var = tuple<T,U>::x;
27536                      The template parameter 'U' looks exactly like a
27537                      declarator.  */
27538                   do
27539                     {
27540                       int ctor_dtor_or_conv_p;
27541                       cp_lexer_consume_token (parser->lexer);
27542                       cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27543                                             &ctor_dtor_or_conv_p,
27544                                             /*parenthesized_p=*/NULL,
27545                                             /*member_p=*/true,
27546                                             /*friend_p=*/false);
27547                       peek = cp_lexer_peek_token (parser->lexer);
27548                       if (cp_parser_error_occurred (parser))
27549                         break;
27550                     }
27551                   while (peek->type == CPP_COMMA);
27552                   /* If we met an '=' or ';' then the original comma
27553                      was the end of the NSDMI.  Otherwise assume
27554                      we're still in the NSDMI.  */
27555                   error = (peek->type != CPP_EQ
27556                            && peek->type != CPP_SEMICOLON);
27557                 }
27558               else
27559                 {
27560                   cp_lexer_consume_token (parser->lexer);
27561                   begin_scope (sk_function_parms, NULL_TREE);
27562                   cp_parser_parameter_declaration_list (parser, &error);
27563                   pop_bindings_and_leave_scope ();
27564                 }
27565               if (!cp_parser_error_occurred (parser) && !error)
27566                 done = true;
27567               cp_parser_abort_tentative_parse (parser);
27568
27569               parser->in_template_argument_list_p = saved_italp;
27570               break;
27571             }
27572         case CPP_CLOSE_PAREN:
27573         case CPP_ELLIPSIS:
27574           /* If we run into a non-nested `;', `}', or `]',
27575              then the code is invalid -- but the default
27576              argument is certainly over.  */
27577         case CPP_SEMICOLON:
27578         case CPP_CLOSE_BRACE:
27579         case CPP_CLOSE_SQUARE:
27580           if (depth == 0
27581               /* Handle correctly int n = sizeof ... ( p );  */
27582               && token->type != CPP_ELLIPSIS)
27583             done = true;
27584           /* Update DEPTH, if necessary.  */
27585           else if (token->type == CPP_CLOSE_PAREN
27586                    || token->type == CPP_CLOSE_BRACE
27587                    || token->type == CPP_CLOSE_SQUARE)
27588             --depth;
27589           break;
27590
27591         case CPP_OPEN_PAREN:
27592         case CPP_OPEN_SQUARE:
27593         case CPP_OPEN_BRACE:
27594           ++depth;
27595           break;
27596
27597         case CPP_LESS:
27598           if (depth == 0)
27599             /* This might be the comparison operator, or it might
27600                start a template argument list.  */
27601             ++maybe_template_id;
27602           break;
27603
27604         case CPP_RSHIFT:
27605           if (cxx_dialect == cxx98)
27606             break;
27607           /* Fall through for C++0x, which treats the `>>'
27608              operator like two `>' tokens in certain
27609              cases.  */
27610
27611         case CPP_GREATER:
27612           if (depth == 0)
27613             {
27614               /* This might be an operator, or it might close a
27615                  template argument list.  But if a previous '<'
27616                  started a template argument list, this will have
27617                  closed it, so we can't be in one anymore.  */
27618               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27619               if (maybe_template_id < 0)
27620                 maybe_template_id = 0;
27621             }
27622           break;
27623
27624           /* If we run out of tokens, issue an error message.  */
27625         case CPP_EOF:
27626         case CPP_PRAGMA_EOL:
27627           error_at (token->location, "file ends in default argument");
27628           return error_mark_node;
27629
27630         case CPP_NAME:
27631         case CPP_SCOPE:
27632           /* In these cases, we should look for template-ids.
27633              For example, if the default argument is
27634              `X<int, double>()', we need to do name lookup to
27635              figure out whether or not `X' is a template; if
27636              so, the `,' does not end the default argument.
27637
27638              That is not yet done.  */
27639           break;
27640
27641         default:
27642           break;
27643         }
27644
27645       /* If we've reached the end, stop.  */
27646       if (done)
27647         break;
27648
27649       /* Add the token to the token block.  */
27650       token = cp_lexer_consume_token (parser->lexer);
27651     }
27652
27653   /* Create a DEFAULT_ARG to represent the unparsed default
27654      argument.  */
27655   default_argument = make_node (DEFAULT_ARG);
27656   DEFARG_TOKENS (default_argument)
27657     = cp_token_cache_new (first_token, token);
27658   DEFARG_INSTANTIATIONS (default_argument) = NULL;
27659
27660   return default_argument;
27661 }
27662
27663 /* Begin parsing tentatively.  We always save tokens while parsing
27664    tentatively so that if the tentative parsing fails we can restore the
27665    tokens.  */
27666
27667 static void
27668 cp_parser_parse_tentatively (cp_parser* parser)
27669 {
27670   /* Enter a new parsing context.  */
27671   parser->context = cp_parser_context_new (parser->context);
27672   /* Begin saving tokens.  */
27673   cp_lexer_save_tokens (parser->lexer);
27674   /* In order to avoid repetitive access control error messages,
27675      access checks are queued up until we are no longer parsing
27676      tentatively.  */
27677   push_deferring_access_checks (dk_deferred);
27678 }
27679
27680 /* Commit to the currently active tentative parse.  */
27681
27682 static void
27683 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27684 {
27685   cp_parser_context *context;
27686   cp_lexer *lexer;
27687
27688   /* Mark all of the levels as committed.  */
27689   lexer = parser->lexer;
27690   for (context = parser->context; context->next; context = context->next)
27691     {
27692       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27693         break;
27694       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27695       while (!cp_lexer_saving_tokens (lexer))
27696         lexer = lexer->next;
27697       cp_lexer_commit_tokens (lexer);
27698     }
27699 }
27700
27701 /* Commit to the topmost currently active tentative parse.
27702
27703    Note that this function shouldn't be called when there are
27704    irreversible side-effects while in a tentative state.  For
27705    example, we shouldn't create a permanent entry in the symbol
27706    table, or issue an error message that might not apply if the
27707    tentative parse is aborted.  */
27708
27709 static void
27710 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27711 {
27712   cp_parser_context *context = parser->context;
27713   cp_lexer *lexer = parser->lexer;
27714
27715   if (context)
27716     {
27717       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27718         return;
27719       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27720
27721       while (!cp_lexer_saving_tokens (lexer))
27722         lexer = lexer->next;
27723       cp_lexer_commit_tokens (lexer);
27724     }
27725 }
27726
27727 /* Abort the currently active tentative parse.  All consumed tokens
27728    will be rolled back, and no diagnostics will be issued.  */
27729
27730 static void
27731 cp_parser_abort_tentative_parse (cp_parser* parser)
27732 {
27733   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27734               || errorcount > 0);
27735   cp_parser_simulate_error (parser);
27736   /* Now, pretend that we want to see if the construct was
27737      successfully parsed.  */
27738   cp_parser_parse_definitely (parser);
27739 }
27740
27741 /* Stop parsing tentatively.  If a parse error has occurred, restore the
27742    token stream.  Otherwise, commit to the tokens we have consumed.
27743    Returns true if no error occurred; false otherwise.  */
27744
27745 static bool
27746 cp_parser_parse_definitely (cp_parser* parser)
27747 {
27748   bool error_occurred;
27749   cp_parser_context *context;
27750
27751   /* Remember whether or not an error occurred, since we are about to
27752      destroy that information.  */
27753   error_occurred = cp_parser_error_occurred (parser);
27754   /* Remove the topmost context from the stack.  */
27755   context = parser->context;
27756   parser->context = context->next;
27757   /* If no parse errors occurred, commit to the tentative parse.  */
27758   if (!error_occurred)
27759     {
27760       /* Commit to the tokens read tentatively, unless that was
27761          already done.  */
27762       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27763         cp_lexer_commit_tokens (parser->lexer);
27764
27765       pop_to_parent_deferring_access_checks ();
27766     }
27767   /* Otherwise, if errors occurred, roll back our state so that things
27768      are just as they were before we began the tentative parse.  */
27769   else
27770     {
27771       cp_lexer_rollback_tokens (parser->lexer);
27772       pop_deferring_access_checks ();
27773     }
27774   /* Add the context to the front of the free list.  */
27775   context->next = cp_parser_context_free_list;
27776   cp_parser_context_free_list = context;
27777
27778   return !error_occurred;
27779 }
27780
27781 /* Returns true if we are parsing tentatively and are not committed to
27782    this tentative parse.  */
27783
27784 static bool
27785 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27786 {
27787   return (cp_parser_parsing_tentatively (parser)
27788           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27789 }
27790
27791 /* Returns nonzero iff an error has occurred during the most recent
27792    tentative parse.  */
27793
27794 static bool
27795 cp_parser_error_occurred (cp_parser* parser)
27796 {
27797   return (cp_parser_parsing_tentatively (parser)
27798           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27799 }
27800
27801 /* Returns nonzero if GNU extensions are allowed.  */
27802
27803 static bool
27804 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27805 {
27806   return parser->allow_gnu_extensions_p;
27807 }
27808 \f
27809 /* Objective-C++ Productions */
27810
27811
27812 /* Parse an Objective-C expression, which feeds into a primary-expression
27813    above.
27814
27815    objc-expression:
27816      objc-message-expression
27817      objc-string-literal
27818      objc-encode-expression
27819      objc-protocol-expression
27820      objc-selector-expression
27821
27822   Returns a tree representation of the expression.  */
27823
27824 static cp_expr
27825 cp_parser_objc_expression (cp_parser* parser)
27826 {
27827   /* Try to figure out what kind of declaration is present.  */
27828   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27829
27830   switch (kwd->type)
27831     {
27832     case CPP_OPEN_SQUARE:
27833       return cp_parser_objc_message_expression (parser);
27834
27835     case CPP_OBJC_STRING:
27836       kwd = cp_lexer_consume_token (parser->lexer);
27837       return objc_build_string_object (kwd->u.value);
27838
27839     case CPP_KEYWORD:
27840       switch (kwd->keyword)
27841         {
27842         case RID_AT_ENCODE:
27843           return cp_parser_objc_encode_expression (parser);
27844
27845         case RID_AT_PROTOCOL:
27846           return cp_parser_objc_protocol_expression (parser);
27847
27848         case RID_AT_SELECTOR:
27849           return cp_parser_objc_selector_expression (parser);
27850
27851         default:
27852           break;
27853         }
27854     default:
27855       error_at (kwd->location,
27856                 "misplaced %<@%D%> Objective-C++ construct",
27857                 kwd->u.value);
27858       cp_parser_skip_to_end_of_block_or_statement (parser);
27859     }
27860
27861   return error_mark_node;
27862 }
27863
27864 /* Parse an Objective-C message expression.
27865
27866    objc-message-expression:
27867      [ objc-message-receiver objc-message-args ]
27868
27869    Returns a representation of an Objective-C message.  */
27870
27871 static tree
27872 cp_parser_objc_message_expression (cp_parser* parser)
27873 {
27874   tree receiver, messageargs;
27875
27876   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27877   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
27878   receiver = cp_parser_objc_message_receiver (parser);
27879   messageargs = cp_parser_objc_message_args (parser);
27880   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27881   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27882
27883   tree result = objc_build_message_expr (receiver, messageargs);
27884
27885   /* Construct a location e.g.
27886        [self func1:5]
27887        ^~~~~~~~~~~~~~
27888      ranging from the '[' to the ']', with the caret at the start.  */
27889   location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27890   protected_set_expr_location (result, combined_loc);
27891
27892   return result;
27893 }
27894
27895 /* Parse an objc-message-receiver.
27896
27897    objc-message-receiver:
27898      expression
27899      simple-type-specifier
27900
27901   Returns a representation of the type or expression.  */
27902
27903 static tree
27904 cp_parser_objc_message_receiver (cp_parser* parser)
27905 {
27906   tree rcv;
27907
27908   /* An Objective-C message receiver may be either (1) a type
27909      or (2) an expression.  */
27910   cp_parser_parse_tentatively (parser);
27911   rcv = cp_parser_expression (parser);
27912
27913   /* If that worked out, fine.  */
27914   if (cp_parser_parse_definitely (parser))
27915     return rcv;
27916
27917   cp_parser_parse_tentatively (parser);
27918   rcv = cp_parser_simple_type_specifier (parser,
27919                                          /*decl_specs=*/NULL,
27920                                          CP_PARSER_FLAGS_NONE);
27921
27922   if (cp_parser_parse_definitely (parser))
27923     return objc_get_class_reference (rcv);
27924   
27925   cp_parser_error (parser, "objective-c++ message receiver expected");
27926   return error_mark_node;
27927 }
27928
27929 /* Parse the arguments and selectors comprising an Objective-C message.
27930
27931    objc-message-args:
27932      objc-selector
27933      objc-selector-args
27934      objc-selector-args , objc-comma-args
27935
27936    objc-selector-args:
27937      objc-selector [opt] : assignment-expression
27938      objc-selector-args objc-selector [opt] : assignment-expression
27939
27940    objc-comma-args:
27941      assignment-expression
27942      objc-comma-args , assignment-expression
27943
27944    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27945    selector arguments and TREE_VALUE containing a list of comma
27946    arguments.  */
27947
27948 static tree
27949 cp_parser_objc_message_args (cp_parser* parser)
27950 {
27951   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27952   bool maybe_unary_selector_p = true;
27953   cp_token *token = cp_lexer_peek_token (parser->lexer);
27954
27955   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27956     {
27957       tree selector = NULL_TREE, arg;
27958
27959       if (token->type != CPP_COLON)
27960         selector = cp_parser_objc_selector (parser);
27961
27962       /* Detect if we have a unary selector.  */
27963       if (maybe_unary_selector_p
27964           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27965         return build_tree_list (selector, NULL_TREE);
27966
27967       maybe_unary_selector_p = false;
27968       cp_parser_require (parser, CPP_COLON, RT_COLON);
27969       arg = cp_parser_assignment_expression (parser);
27970
27971       sel_args
27972         = chainon (sel_args,
27973                    build_tree_list (selector, arg));
27974
27975       token = cp_lexer_peek_token (parser->lexer);
27976     }
27977
27978   /* Handle non-selector arguments, if any. */
27979   while (token->type == CPP_COMMA)
27980     {
27981       tree arg;
27982
27983       cp_lexer_consume_token (parser->lexer);
27984       arg = cp_parser_assignment_expression (parser);
27985
27986       addl_args
27987         = chainon (addl_args,
27988                    build_tree_list (NULL_TREE, arg));
27989
27990       token = cp_lexer_peek_token (parser->lexer);
27991     }
27992
27993   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27994     {
27995       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27996       return build_tree_list (error_mark_node, error_mark_node);
27997     }
27998
27999   return build_tree_list (sel_args, addl_args);
28000 }
28001
28002 /* Parse an Objective-C encode expression.
28003
28004    objc-encode-expression:
28005      @encode objc-typename
28006
28007    Returns an encoded representation of the type argument.  */
28008
28009 static cp_expr
28010 cp_parser_objc_encode_expression (cp_parser* parser)
28011 {
28012   tree type;
28013   cp_token *token;
28014   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28015
28016   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
28017   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28018   token = cp_lexer_peek_token (parser->lexer);
28019   type = complete_type (cp_parser_type_id (parser));
28020   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28021
28022   if (!type)
28023     {
28024       error_at (token->location, 
28025                 "%<@encode%> must specify a type as an argument");
28026       return error_mark_node;
28027     }
28028
28029   /* This happens if we find @encode(T) (where T is a template
28030      typename or something dependent on a template typename) when
28031      parsing a template.  In that case, we can't compile it
28032      immediately, but we rather create an AT_ENCODE_EXPR which will
28033      need to be instantiated when the template is used.
28034   */
28035   if (dependent_type_p (type))
28036     {
28037       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28038       TREE_READONLY (value) = 1;
28039       return value;
28040     }
28041
28042
28043   /* Build a location of the form:
28044        @encode(int)
28045        ^~~~~~~~~~~~
28046      with caret==start at the @ token, finishing at the close paren.  */
28047   location_t combined_loc
28048     = make_location (start_loc, start_loc,
28049                      cp_lexer_previous_token (parser->lexer)->location);
28050
28051   return cp_expr (objc_build_encode_expr (type), combined_loc);
28052 }
28053
28054 /* Parse an Objective-C @defs expression.  */
28055
28056 static tree
28057 cp_parser_objc_defs_expression (cp_parser *parser)
28058 {
28059   tree name;
28060
28061   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
28062   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28063   name = cp_parser_identifier (parser);
28064   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28065
28066   return objc_get_class_ivars (name);
28067 }
28068
28069 /* Parse an Objective-C protocol expression.
28070
28071   objc-protocol-expression:
28072     @protocol ( identifier )
28073
28074   Returns a representation of the protocol expression.  */
28075
28076 static tree
28077 cp_parser_objc_protocol_expression (cp_parser* parser)
28078 {
28079   tree proto;
28080   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28081
28082   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
28083   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28084   proto = cp_parser_identifier (parser);
28085   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28086
28087   /* Build a location of the form:
28088        @protocol(prot)
28089        ^~~~~~~~~~~~~~~
28090      with caret==start at the @ token, finishing at the close paren.  */
28091   location_t combined_loc
28092     = make_location (start_loc, start_loc,
28093                      cp_lexer_previous_token (parser->lexer)->location);
28094   tree result = objc_build_protocol_expr (proto);
28095   protected_set_expr_location (result, combined_loc);
28096   return result;
28097 }
28098
28099 /* Parse an Objective-C selector expression.
28100
28101    objc-selector-expression:
28102      @selector ( objc-method-signature )
28103
28104    objc-method-signature:
28105      objc-selector
28106      objc-selector-seq
28107
28108    objc-selector-seq:
28109      objc-selector :
28110      objc-selector-seq objc-selector :
28111
28112   Returns a representation of the method selector.  */
28113
28114 static tree
28115 cp_parser_objc_selector_expression (cp_parser* parser)
28116 {
28117   tree sel_seq = NULL_TREE;
28118   bool maybe_unary_selector_p = true;
28119   cp_token *token;
28120   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28121
28122   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
28123   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28124   token = cp_lexer_peek_token (parser->lexer);
28125
28126   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28127          || token->type == CPP_SCOPE)
28128     {
28129       tree selector = NULL_TREE;
28130
28131       if (token->type != CPP_COLON
28132           || token->type == CPP_SCOPE)
28133         selector = cp_parser_objc_selector (parser);
28134
28135       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28136           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28137         {
28138           /* Detect if we have a unary selector.  */
28139           if (maybe_unary_selector_p)
28140             {
28141               sel_seq = selector;
28142               goto finish_selector;
28143             }
28144           else
28145             {
28146               cp_parser_error (parser, "expected %<:%>");
28147             }
28148         }
28149       maybe_unary_selector_p = false;
28150       token = cp_lexer_consume_token (parser->lexer);
28151
28152       if (token->type == CPP_SCOPE)
28153         {
28154           sel_seq
28155             = chainon (sel_seq,
28156                        build_tree_list (selector, NULL_TREE));
28157           sel_seq
28158             = chainon (sel_seq,
28159                        build_tree_list (NULL_TREE, NULL_TREE));
28160         }
28161       else
28162         sel_seq
28163           = chainon (sel_seq,
28164                      build_tree_list (selector, NULL_TREE));
28165
28166       token = cp_lexer_peek_token (parser->lexer);
28167     }
28168
28169  finish_selector:
28170   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28171
28172
28173   /* Build a location of the form:
28174        @selector(func)
28175        ^~~~~~~~~~~~~~~
28176      with caret==start at the @ token, finishing at the close paren.  */
28177   location_t combined_loc
28178     = make_location (loc, loc,
28179                      cp_lexer_previous_token (parser->lexer)->location);
28180   tree result = objc_build_selector_expr (combined_loc, sel_seq);
28181   /* TODO: objc_build_selector_expr doesn't always honor the location.  */
28182   protected_set_expr_location (result, combined_loc);
28183   return result;
28184 }
28185
28186 /* Parse a list of identifiers.
28187
28188    objc-identifier-list:
28189      identifier
28190      objc-identifier-list , identifier
28191
28192    Returns a TREE_LIST of identifier nodes.  */
28193
28194 static tree
28195 cp_parser_objc_identifier_list (cp_parser* parser)
28196 {
28197   tree identifier;
28198   tree list;
28199   cp_token *sep;
28200
28201   identifier = cp_parser_identifier (parser);
28202   if (identifier == error_mark_node)
28203     return error_mark_node;      
28204
28205   list = build_tree_list (NULL_TREE, identifier);
28206   sep = cp_lexer_peek_token (parser->lexer);
28207
28208   while (sep->type == CPP_COMMA)
28209     {
28210       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
28211       identifier = cp_parser_identifier (parser);
28212       if (identifier == error_mark_node)
28213         return list;
28214
28215       list = chainon (list, build_tree_list (NULL_TREE,
28216                                              identifier));
28217       sep = cp_lexer_peek_token (parser->lexer);
28218     }
28219   
28220   return list;
28221 }
28222
28223 /* Parse an Objective-C alias declaration.
28224
28225    objc-alias-declaration:
28226      @compatibility_alias identifier identifier ;
28227
28228    This function registers the alias mapping with the Objective-C front end.
28229    It returns nothing.  */
28230
28231 static void
28232 cp_parser_objc_alias_declaration (cp_parser* parser)
28233 {
28234   tree alias, orig;
28235
28236   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
28237   alias = cp_parser_identifier (parser);
28238   orig = cp_parser_identifier (parser);
28239   objc_declare_alias (alias, orig);
28240   cp_parser_consume_semicolon_at_end_of_statement (parser);
28241 }
28242
28243 /* Parse an Objective-C class forward-declaration.
28244
28245    objc-class-declaration:
28246      @class objc-identifier-list ;
28247
28248    The function registers the forward declarations with the Objective-C
28249    front end.  It returns nothing.  */
28250
28251 static void
28252 cp_parser_objc_class_declaration (cp_parser* parser)
28253 {
28254   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
28255   while (true)
28256     {
28257       tree id;
28258       
28259       id = cp_parser_identifier (parser);
28260       if (id == error_mark_node)
28261         break;
28262       
28263       objc_declare_class (id);
28264
28265       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28266         cp_lexer_consume_token (parser->lexer);
28267       else
28268         break;
28269     }
28270   cp_parser_consume_semicolon_at_end_of_statement (parser);
28271 }
28272
28273 /* Parse a list of Objective-C protocol references.
28274
28275    objc-protocol-refs-opt:
28276      objc-protocol-refs [opt]
28277
28278    objc-protocol-refs:
28279      < objc-identifier-list >
28280
28281    Returns a TREE_LIST of identifiers, if any.  */
28282
28283 static tree
28284 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28285 {
28286   tree protorefs = NULL_TREE;
28287
28288   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28289     {
28290       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
28291       protorefs = cp_parser_objc_identifier_list (parser);
28292       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28293     }
28294
28295   return protorefs;
28296 }
28297
28298 /* Parse a Objective-C visibility specification.  */
28299
28300 static void
28301 cp_parser_objc_visibility_spec (cp_parser* parser)
28302 {
28303   cp_token *vis = cp_lexer_peek_token (parser->lexer);
28304
28305   switch (vis->keyword)
28306     {
28307     case RID_AT_PRIVATE:
28308       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28309       break;
28310     case RID_AT_PROTECTED:
28311       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28312       break;
28313     case RID_AT_PUBLIC:
28314       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28315       break;
28316     case RID_AT_PACKAGE:
28317       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28318       break;
28319     default:
28320       return;
28321     }
28322
28323   /* Eat '@private'/'@protected'/'@public'.  */
28324   cp_lexer_consume_token (parser->lexer);
28325 }
28326
28327 /* Parse an Objective-C method type.  Return 'true' if it is a class
28328    (+) method, and 'false' if it is an instance (-) method.  */
28329
28330 static inline bool
28331 cp_parser_objc_method_type (cp_parser* parser)
28332 {
28333   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28334     return true;
28335   else
28336     return false;
28337 }
28338
28339 /* Parse an Objective-C protocol qualifier.  */
28340
28341 static tree
28342 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28343 {
28344   tree quals = NULL_TREE, node;
28345   cp_token *token = cp_lexer_peek_token (parser->lexer);
28346
28347   node = token->u.value;
28348
28349   while (node && identifier_p (node)
28350          && (node == ridpointers [(int) RID_IN]
28351              || node == ridpointers [(int) RID_OUT]
28352              || node == ridpointers [(int) RID_INOUT]
28353              || node == ridpointers [(int) RID_BYCOPY]
28354              || node == ridpointers [(int) RID_BYREF]
28355              || node == ridpointers [(int) RID_ONEWAY]))
28356     {
28357       quals = tree_cons (NULL_TREE, node, quals);
28358       cp_lexer_consume_token (parser->lexer);
28359       token = cp_lexer_peek_token (parser->lexer);
28360       node = token->u.value;
28361     }
28362
28363   return quals;
28364 }
28365
28366 /* Parse an Objective-C typename.  */
28367
28368 static tree
28369 cp_parser_objc_typename (cp_parser* parser)
28370 {
28371   tree type_name = NULL_TREE;
28372
28373   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28374     {
28375       tree proto_quals, cp_type = NULL_TREE;
28376
28377       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
28378       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28379
28380       /* An ObjC type name may consist of just protocol qualifiers, in which
28381          case the type shall default to 'id'.  */
28382       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28383         {
28384           cp_type = cp_parser_type_id (parser);
28385           
28386           /* If the type could not be parsed, an error has already
28387              been produced.  For error recovery, behave as if it had
28388              not been specified, which will use the default type
28389              'id'.  */
28390           if (cp_type == error_mark_node)
28391             {
28392               cp_type = NULL_TREE;
28393               /* We need to skip to the closing parenthesis as
28394                  cp_parser_type_id() does not seem to do it for
28395                  us.  */
28396               cp_parser_skip_to_closing_parenthesis (parser,
28397                                                      /*recovering=*/true,
28398                                                      /*or_comma=*/false,
28399                                                      /*consume_paren=*/false);
28400             }
28401         }
28402
28403       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28404       type_name = build_tree_list (proto_quals, cp_type);
28405     }
28406
28407   return type_name;
28408 }
28409
28410 /* Check to see if TYPE refers to an Objective-C selector name.  */
28411
28412 static bool
28413 cp_parser_objc_selector_p (enum cpp_ttype type)
28414 {
28415   return (type == CPP_NAME || type == CPP_KEYWORD
28416           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28417           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28418           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28419           || type == CPP_XOR || type == CPP_XOR_EQ);
28420 }
28421
28422 /* Parse an Objective-C selector.  */
28423
28424 static tree
28425 cp_parser_objc_selector (cp_parser* parser)
28426 {
28427   cp_token *token = cp_lexer_consume_token (parser->lexer);
28428
28429   if (!cp_parser_objc_selector_p (token->type))
28430     {
28431       error_at (token->location, "invalid Objective-C++ selector name");
28432       return error_mark_node;
28433     }
28434
28435   /* C++ operator names are allowed to appear in ObjC selectors.  */
28436   switch (token->type)
28437     {
28438     case CPP_AND_AND: return get_identifier ("and");
28439     case CPP_AND_EQ: return get_identifier ("and_eq");
28440     case CPP_AND: return get_identifier ("bitand");
28441     case CPP_OR: return get_identifier ("bitor");
28442     case CPP_COMPL: return get_identifier ("compl");
28443     case CPP_NOT: return get_identifier ("not");
28444     case CPP_NOT_EQ: return get_identifier ("not_eq");
28445     case CPP_OR_OR: return get_identifier ("or");
28446     case CPP_OR_EQ: return get_identifier ("or_eq");
28447     case CPP_XOR: return get_identifier ("xor");
28448     case CPP_XOR_EQ: return get_identifier ("xor_eq");
28449     default: return token->u.value;
28450     }
28451 }
28452
28453 /* Parse an Objective-C params list.  */
28454
28455 static tree
28456 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28457 {
28458   tree params = NULL_TREE;
28459   bool maybe_unary_selector_p = true;
28460   cp_token *token = cp_lexer_peek_token (parser->lexer);
28461
28462   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28463     {
28464       tree selector = NULL_TREE, type_name, identifier;
28465       tree parm_attr = NULL_TREE;
28466
28467       if (token->keyword == RID_ATTRIBUTE)
28468         break;
28469
28470       if (token->type != CPP_COLON)
28471         selector = cp_parser_objc_selector (parser);
28472
28473       /* Detect if we have a unary selector.  */
28474       if (maybe_unary_selector_p
28475           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28476         {
28477           params = selector; /* Might be followed by attributes.  */
28478           break;
28479         }
28480
28481       maybe_unary_selector_p = false;
28482       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28483         {
28484           /* Something went quite wrong.  There should be a colon
28485              here, but there is not.  Stop parsing parameters.  */
28486           break;
28487         }
28488       type_name = cp_parser_objc_typename (parser);
28489       /* New ObjC allows attributes on parameters too.  */
28490       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28491         parm_attr = cp_parser_attributes_opt (parser);
28492       identifier = cp_parser_identifier (parser);
28493
28494       params
28495         = chainon (params,
28496                    objc_build_keyword_decl (selector,
28497                                             type_name,
28498                                             identifier,
28499                                             parm_attr));
28500
28501       token = cp_lexer_peek_token (parser->lexer);
28502     }
28503
28504   if (params == NULL_TREE)
28505     {
28506       cp_parser_error (parser, "objective-c++ method declaration is expected");
28507       return error_mark_node;
28508     }
28509
28510   /* We allow tail attributes for the method.  */
28511   if (token->keyword == RID_ATTRIBUTE)
28512     {
28513       *attributes = cp_parser_attributes_opt (parser);
28514       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28515           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28516         return params;
28517       cp_parser_error (parser, 
28518                        "method attributes must be specified at the end");
28519       return error_mark_node;
28520     }
28521
28522   if (params == NULL_TREE)
28523     {
28524       cp_parser_error (parser, "objective-c++ method declaration is expected");
28525       return error_mark_node;
28526     }
28527   return params;
28528 }
28529
28530 /* Parse the non-keyword Objective-C params.  */
28531
28532 static tree
28533 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
28534                                        tree* attributes)
28535 {
28536   tree params = make_node (TREE_LIST);
28537   cp_token *token = cp_lexer_peek_token (parser->lexer);
28538   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
28539
28540   while (token->type == CPP_COMMA)
28541     {
28542       cp_parameter_declarator *parmdecl;
28543       tree parm;
28544
28545       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
28546       token = cp_lexer_peek_token (parser->lexer);
28547
28548       if (token->type == CPP_ELLIPSIS)
28549         {
28550           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
28551           *ellipsisp = true;
28552           token = cp_lexer_peek_token (parser->lexer);
28553           break;
28554         }
28555
28556       /* TODO: parse attributes for tail parameters.  */
28557       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28558       parm = grokdeclarator (parmdecl->declarator,
28559                              &parmdecl->decl_specifiers,
28560                              PARM, /*initialized=*/0,
28561                              /*attrlist=*/NULL);
28562
28563       chainon (params, build_tree_list (NULL_TREE, parm));
28564       token = cp_lexer_peek_token (parser->lexer);
28565     }
28566
28567   /* We allow tail attributes for the method.  */
28568   if (token->keyword == RID_ATTRIBUTE)
28569     {
28570       if (*attributes == NULL_TREE)
28571         {
28572           *attributes = cp_parser_attributes_opt (parser);
28573           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28574               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28575             return params;
28576         }
28577       else        
28578         /* We have an error, but parse the attributes, so that we can 
28579            carry on.  */
28580         *attributes = cp_parser_attributes_opt (parser);
28581
28582       cp_parser_error (parser, 
28583                        "method attributes must be specified at the end");
28584       return error_mark_node;
28585     }
28586
28587   return params;
28588 }
28589
28590 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
28591
28592 static void
28593 cp_parser_objc_interstitial_code (cp_parser* parser)
28594 {
28595   cp_token *token = cp_lexer_peek_token (parser->lexer);
28596
28597   /* If the next token is `extern' and the following token is a string
28598      literal, then we have a linkage specification.  */
28599   if (token->keyword == RID_EXTERN
28600       && cp_parser_is_pure_string_literal
28601          (cp_lexer_peek_nth_token (parser->lexer, 2)))
28602     cp_parser_linkage_specification (parser);
28603   /* Handle #pragma, if any.  */
28604   else if (token->type == CPP_PRAGMA)
28605     cp_parser_pragma (parser, pragma_objc_icode, NULL);
28606   /* Allow stray semicolons.  */
28607   else if (token->type == CPP_SEMICOLON)
28608     cp_lexer_consume_token (parser->lexer);
28609   /* Mark methods as optional or required, when building protocols.  */
28610   else if (token->keyword == RID_AT_OPTIONAL)
28611     {
28612       cp_lexer_consume_token (parser->lexer);
28613       objc_set_method_opt (true);
28614     }
28615   else if (token->keyword == RID_AT_REQUIRED)
28616     {
28617       cp_lexer_consume_token (parser->lexer);
28618       objc_set_method_opt (false);
28619     }
28620   else if (token->keyword == RID_NAMESPACE)
28621     cp_parser_namespace_definition (parser);
28622   /* Other stray characters must generate errors.  */
28623   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28624     {
28625       cp_lexer_consume_token (parser->lexer);
28626       error ("stray %qs between Objective-C++ methods",
28627              token->type == CPP_OPEN_BRACE ? "{" : "}");
28628     }
28629   /* Finally, try to parse a block-declaration, or a function-definition.  */
28630   else
28631     cp_parser_block_declaration (parser, /*statement_p=*/false);
28632 }
28633
28634 /* Parse a method signature.  */
28635
28636 static tree
28637 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28638 {
28639   tree rettype, kwdparms, optparms;
28640   bool ellipsis = false;
28641   bool is_class_method;
28642
28643   is_class_method = cp_parser_objc_method_type (parser);
28644   rettype = cp_parser_objc_typename (parser);
28645   *attributes = NULL_TREE;
28646   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28647   if (kwdparms == error_mark_node)
28648     return error_mark_node;
28649   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28650   if (optparms == error_mark_node)
28651     return error_mark_node;
28652
28653   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28654 }
28655
28656 static bool
28657 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28658 {
28659   tree tattr;  
28660   cp_lexer_save_tokens (parser->lexer);
28661   tattr = cp_parser_attributes_opt (parser);
28662   gcc_assert (tattr) ;
28663   
28664   /* If the attributes are followed by a method introducer, this is not allowed.
28665      Dump the attributes and flag the situation.  */
28666   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28667       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28668     return true;
28669
28670   /* Otherwise, the attributes introduce some interstitial code, possibly so
28671      rewind to allow that check.  */
28672   cp_lexer_rollback_tokens (parser->lexer);
28673   return false;  
28674 }
28675
28676 /* Parse an Objective-C method prototype list.  */
28677
28678 static void
28679 cp_parser_objc_method_prototype_list (cp_parser* parser)
28680 {
28681   cp_token *token = cp_lexer_peek_token (parser->lexer);
28682
28683   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28684     {
28685       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28686         {
28687           tree attributes, sig;
28688           bool is_class_method;
28689           if (token->type == CPP_PLUS)
28690             is_class_method = true;
28691           else
28692             is_class_method = false;
28693           sig = cp_parser_objc_method_signature (parser, &attributes);
28694           if (sig == error_mark_node)
28695             {
28696               cp_parser_skip_to_end_of_block_or_statement (parser);
28697               token = cp_lexer_peek_token (parser->lexer);
28698               continue;
28699             }
28700           objc_add_method_declaration (is_class_method, sig, attributes);
28701           cp_parser_consume_semicolon_at_end_of_statement (parser);
28702         }
28703       else if (token->keyword == RID_AT_PROPERTY)
28704         cp_parser_objc_at_property_declaration (parser);
28705       else if (token->keyword == RID_ATTRIBUTE 
28706                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28707         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
28708                     OPT_Wattributes, 
28709                     "prefix attributes are ignored for methods");
28710       else
28711         /* Allow for interspersed non-ObjC++ code.  */
28712         cp_parser_objc_interstitial_code (parser);
28713
28714       token = cp_lexer_peek_token (parser->lexer);
28715     }
28716
28717   if (token->type != CPP_EOF)
28718     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
28719   else
28720     cp_parser_error (parser, "expected %<@end%>");
28721
28722   objc_finish_interface ();
28723 }
28724
28725 /* Parse an Objective-C method definition list.  */
28726
28727 static void
28728 cp_parser_objc_method_definition_list (cp_parser* parser)
28729 {
28730   cp_token *token = cp_lexer_peek_token (parser->lexer);
28731
28732   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28733     {
28734       tree meth;
28735
28736       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28737         {
28738           cp_token *ptk;
28739           tree sig, attribute;
28740           bool is_class_method;
28741           if (token->type == CPP_PLUS)
28742             is_class_method = true;
28743           else
28744             is_class_method = false;
28745           push_deferring_access_checks (dk_deferred);
28746           sig = cp_parser_objc_method_signature (parser, &attribute);
28747           if (sig == error_mark_node)
28748             {
28749               cp_parser_skip_to_end_of_block_or_statement (parser);
28750               token = cp_lexer_peek_token (parser->lexer);
28751               continue;
28752             }
28753           objc_start_method_definition (is_class_method, sig, attribute,
28754                                         NULL_TREE);
28755
28756           /* For historical reasons, we accept an optional semicolon.  */
28757           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28758             cp_lexer_consume_token (parser->lexer);
28759
28760           ptk = cp_lexer_peek_token (parser->lexer);
28761           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
28762                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28763             {
28764               perform_deferred_access_checks (tf_warning_or_error);
28765               stop_deferring_access_checks ();
28766               meth = cp_parser_function_definition_after_declarator (parser,
28767                                                                      false);
28768               pop_deferring_access_checks ();
28769               objc_finish_method_definition (meth);
28770             }
28771         }
28772       /* The following case will be removed once @synthesize is
28773          completely implemented.  */
28774       else if (token->keyword == RID_AT_PROPERTY)
28775         cp_parser_objc_at_property_declaration (parser);
28776       else if (token->keyword == RID_AT_SYNTHESIZE)
28777         cp_parser_objc_at_synthesize_declaration (parser);
28778       else if (token->keyword == RID_AT_DYNAMIC)
28779         cp_parser_objc_at_dynamic_declaration (parser);
28780       else if (token->keyword == RID_ATTRIBUTE 
28781                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28782         warning_at (token->location, OPT_Wattributes,
28783                     "prefix attributes are ignored for methods");
28784       else
28785         /* Allow for interspersed non-ObjC++ code.  */
28786         cp_parser_objc_interstitial_code (parser);
28787
28788       token = cp_lexer_peek_token (parser->lexer);
28789     }
28790
28791   if (token->type != CPP_EOF)
28792     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
28793   else
28794     cp_parser_error (parser, "expected %<@end%>");
28795
28796   objc_finish_implementation ();
28797 }
28798
28799 /* Parse Objective-C ivars.  */
28800
28801 static void
28802 cp_parser_objc_class_ivars (cp_parser* parser)
28803 {
28804   cp_token *token = cp_lexer_peek_token (parser->lexer);
28805
28806   if (token->type != CPP_OPEN_BRACE)
28807     return;     /* No ivars specified.  */
28808
28809   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
28810   token = cp_lexer_peek_token (parser->lexer);
28811
28812   while (token->type != CPP_CLOSE_BRACE 
28813         && token->keyword != RID_AT_END && token->type != CPP_EOF)
28814     {
28815       cp_decl_specifier_seq declspecs;
28816       int decl_class_or_enum_p;
28817       tree prefix_attributes;
28818
28819       cp_parser_objc_visibility_spec (parser);
28820
28821       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28822         break;
28823
28824       cp_parser_decl_specifier_seq (parser,
28825                                     CP_PARSER_FLAGS_OPTIONAL,
28826                                     &declspecs,
28827                                     &decl_class_or_enum_p);
28828
28829       /* auto, register, static, extern, mutable.  */
28830       if (declspecs.storage_class != sc_none)
28831         {
28832           cp_parser_error (parser, "invalid type for instance variable");         
28833           declspecs.storage_class = sc_none;
28834         }
28835
28836       /* thread_local.  */
28837       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28838         {
28839           cp_parser_error (parser, "invalid type for instance variable");
28840           declspecs.locations[ds_thread] = 0;
28841         }
28842       
28843       /* typedef.  */
28844       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28845         {
28846           cp_parser_error (parser, "invalid type for instance variable");
28847           declspecs.locations[ds_typedef] = 0;
28848         }
28849
28850       prefix_attributes = declspecs.attributes;
28851       declspecs.attributes = NULL_TREE;
28852
28853       /* Keep going until we hit the `;' at the end of the
28854          declaration.  */
28855       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28856         {
28857           tree width = NULL_TREE, attributes, first_attribute, decl;
28858           cp_declarator *declarator = NULL;
28859           int ctor_dtor_or_conv_p;
28860
28861           /* Check for a (possibly unnamed) bitfield declaration.  */
28862           token = cp_lexer_peek_token (parser->lexer);
28863           if (token->type == CPP_COLON)
28864             goto eat_colon;
28865
28866           if (token->type == CPP_NAME
28867               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28868                   == CPP_COLON))
28869             {
28870               /* Get the name of the bitfield.  */
28871               declarator = make_id_declarator (NULL_TREE,
28872                                                cp_parser_identifier (parser),
28873                                                sfk_none);
28874
28875              eat_colon:
28876               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
28877               /* Get the width of the bitfield.  */
28878               width
28879                 = cp_parser_constant_expression (parser);
28880             }
28881           else
28882             {
28883               /* Parse the declarator.  */
28884               declarator
28885                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28886                                         &ctor_dtor_or_conv_p,
28887                                         /*parenthesized_p=*/NULL,
28888                                         /*member_p=*/false,
28889                                         /*friend_p=*/false);
28890             }
28891
28892           /* Look for attributes that apply to the ivar.  */
28893           attributes = cp_parser_attributes_opt (parser);
28894           /* Remember which attributes are prefix attributes and
28895              which are not.  */
28896           first_attribute = attributes;
28897           /* Combine the attributes.  */
28898           attributes = chainon (prefix_attributes, attributes);
28899
28900           if (width)
28901               /* Create the bitfield declaration.  */
28902               decl = grokbitfield (declarator, &declspecs,
28903                                    width,
28904                                    attributes);
28905           else
28906             decl = grokfield (declarator, &declspecs,
28907                               NULL_TREE, /*init_const_expr_p=*/false,
28908                               NULL_TREE, attributes);
28909
28910           /* Add the instance variable.  */
28911           if (decl != error_mark_node && decl != NULL_TREE)
28912             objc_add_instance_variable (decl);
28913
28914           /* Reset PREFIX_ATTRIBUTES.  */
28915           while (attributes && TREE_CHAIN (attributes) != first_attribute)
28916             attributes = TREE_CHAIN (attributes);
28917           if (attributes)
28918             TREE_CHAIN (attributes) = NULL_TREE;
28919
28920           token = cp_lexer_peek_token (parser->lexer);
28921
28922           if (token->type == CPP_COMMA)
28923             {
28924               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
28925               continue;
28926             }
28927           break;
28928         }
28929
28930       cp_parser_consume_semicolon_at_end_of_statement (parser);
28931       token = cp_lexer_peek_token (parser->lexer);
28932     }
28933
28934   if (token->keyword == RID_AT_END)
28935     cp_parser_error (parser, "expected %<}%>");
28936
28937   /* Do not consume the RID_AT_END, so it will be read again as terminating
28938      the @interface of @implementation.  */ 
28939   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28940     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
28941     
28942   /* For historical reasons, we accept an optional semicolon.  */
28943   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28944     cp_lexer_consume_token (parser->lexer);
28945 }
28946
28947 /* Parse an Objective-C protocol declaration.  */
28948
28949 static void
28950 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28951 {
28952   tree proto, protorefs;
28953   cp_token *tok;
28954
28955   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
28956   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28957     {
28958       tok = cp_lexer_peek_token (parser->lexer);
28959       error_at (tok->location, "identifier expected after %<@protocol%>");
28960       cp_parser_consume_semicolon_at_end_of_statement (parser);
28961       return;
28962     }
28963
28964   /* See if we have a forward declaration or a definition.  */
28965   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28966
28967   /* Try a forward declaration first.  */
28968   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28969     {
28970       while (true)
28971         {
28972           tree id;
28973           
28974           id = cp_parser_identifier (parser);
28975           if (id == error_mark_node)
28976             break;
28977           
28978           objc_declare_protocol (id, attributes);
28979           
28980           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28981             cp_lexer_consume_token (parser->lexer);
28982           else
28983             break;
28984         }
28985       cp_parser_consume_semicolon_at_end_of_statement (parser);
28986     }
28987
28988   /* Ok, we got a full-fledged definition (or at least should).  */
28989   else
28990     {
28991       proto = cp_parser_identifier (parser);
28992       protorefs = cp_parser_objc_protocol_refs_opt (parser);
28993       objc_start_protocol (proto, protorefs, attributes);
28994       cp_parser_objc_method_prototype_list (parser);
28995     }
28996 }
28997
28998 /* Parse an Objective-C superclass or category.  */
28999
29000 static void
29001 cp_parser_objc_superclass_or_category (cp_parser *parser, 
29002                                        bool iface_p,
29003                                        tree *super,
29004                                        tree *categ, bool *is_class_extension)
29005 {
29006   cp_token *next = cp_lexer_peek_token (parser->lexer);
29007
29008   *super = *categ = NULL_TREE;
29009   *is_class_extension = false;
29010   if (next->type == CPP_COLON)
29011     {
29012       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
29013       *super = cp_parser_identifier (parser);
29014     }
29015   else if (next->type == CPP_OPEN_PAREN)
29016     {
29017       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
29018
29019       /* If there is no category name, and this is an @interface, we
29020          have a class extension.  */
29021       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29022         {
29023           *categ = NULL_TREE;
29024           *is_class_extension = true;
29025         }
29026       else
29027         *categ = cp_parser_identifier (parser);
29028
29029       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29030     }
29031 }
29032
29033 /* Parse an Objective-C class interface.  */
29034
29035 static void
29036 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29037 {
29038   tree name, super, categ, protos;
29039   bool is_class_extension;
29040
29041   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
29042   name = cp_parser_identifier (parser);
29043   if (name == error_mark_node)
29044     {
29045       /* It's hard to recover because even if valid @interface stuff
29046          is to follow, we can't compile it (or validate it) if we
29047          don't even know which class it refers to.  Let's assume this
29048          was a stray '@interface' token in the stream and skip it.
29049       */
29050       return;
29051     }
29052   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29053                                          &is_class_extension);
29054   protos = cp_parser_objc_protocol_refs_opt (parser);
29055
29056   /* We have either a class or a category on our hands.  */
29057   if (categ || is_class_extension)
29058     objc_start_category_interface (name, categ, protos, attributes);
29059   else
29060     {
29061       objc_start_class_interface (name, super, protos, attributes);
29062       /* Handle instance variable declarations, if any.  */
29063       cp_parser_objc_class_ivars (parser);
29064       objc_continue_interface ();
29065     }
29066
29067   cp_parser_objc_method_prototype_list (parser);
29068 }
29069
29070 /* Parse an Objective-C class implementation.  */
29071
29072 static void
29073 cp_parser_objc_class_implementation (cp_parser* parser)
29074 {
29075   tree name, super, categ;
29076   bool is_class_extension;
29077
29078   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
29079   name = cp_parser_identifier (parser);
29080   if (name == error_mark_node)
29081     {
29082       /* It's hard to recover because even if valid @implementation
29083          stuff is to follow, we can't compile it (or validate it) if
29084          we don't even know which class it refers to.  Let's assume
29085          this was a stray '@implementation' token in the stream and
29086          skip it.
29087       */
29088       return;
29089     }
29090   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29091                                          &is_class_extension);
29092
29093   /* We have either a class or a category on our hands.  */
29094   if (categ)
29095     objc_start_category_implementation (name, categ);
29096   else
29097     {
29098       objc_start_class_implementation (name, super);
29099       /* Handle instance variable declarations, if any.  */
29100       cp_parser_objc_class_ivars (parser);
29101       objc_continue_implementation ();
29102     }
29103
29104   cp_parser_objc_method_definition_list (parser);
29105 }
29106
29107 /* Consume the @end token and finish off the implementation.  */
29108
29109 static void
29110 cp_parser_objc_end_implementation (cp_parser* parser)
29111 {
29112   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
29113   objc_finish_implementation ();
29114 }
29115
29116 /* Parse an Objective-C declaration.  */
29117
29118 static void
29119 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29120 {
29121   /* Try to figure out what kind of declaration is present.  */
29122   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29123
29124   if (attributes)
29125     switch (kwd->keyword)
29126       {
29127         case RID_AT_ALIAS:
29128         case RID_AT_CLASS:
29129         case RID_AT_END:
29130           error_at (kwd->location, "attributes may not be specified before"
29131                     " the %<@%D%> Objective-C++ keyword",
29132                     kwd->u.value);
29133           attributes = NULL;
29134           break;
29135         case RID_AT_IMPLEMENTATION:
29136           warning_at (kwd->location, OPT_Wattributes,
29137                       "prefix attributes are ignored before %<@%D%>",
29138                       kwd->u.value);
29139           attributes = NULL;
29140         default:
29141           break;
29142       }
29143
29144   switch (kwd->keyword)
29145     {
29146     case RID_AT_ALIAS:
29147       cp_parser_objc_alias_declaration (parser);
29148       break;
29149     case RID_AT_CLASS:
29150       cp_parser_objc_class_declaration (parser);
29151       break;
29152     case RID_AT_PROTOCOL:
29153       cp_parser_objc_protocol_declaration (parser, attributes);
29154       break;
29155     case RID_AT_INTERFACE:
29156       cp_parser_objc_class_interface (parser, attributes);
29157       break;
29158     case RID_AT_IMPLEMENTATION:
29159       cp_parser_objc_class_implementation (parser);
29160       break;
29161     case RID_AT_END:
29162       cp_parser_objc_end_implementation (parser);
29163       break;
29164     default:
29165       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29166                 kwd->u.value);
29167       cp_parser_skip_to_end_of_block_or_statement (parser);
29168     }
29169 }
29170
29171 /* Parse an Objective-C try-catch-finally statement.
29172
29173    objc-try-catch-finally-stmt:
29174      @try compound-statement objc-catch-clause-seq [opt]
29175        objc-finally-clause [opt]
29176
29177    objc-catch-clause-seq:
29178      objc-catch-clause objc-catch-clause-seq [opt]
29179
29180    objc-catch-clause:
29181      @catch ( objc-exception-declaration ) compound-statement
29182
29183    objc-finally-clause:
29184      @finally compound-statement
29185
29186    objc-exception-declaration:
29187      parameter-declaration
29188      '...'
29189
29190    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29191
29192    Returns NULL_TREE.
29193
29194    PS: This function is identical to c_parser_objc_try_catch_finally_statement
29195    for C.  Keep them in sync.  */   
29196
29197 static tree
29198 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29199 {
29200   location_t location;
29201   tree stmt;
29202
29203   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29204   location = cp_lexer_peek_token (parser->lexer)->location;
29205   objc_maybe_warn_exceptions (location);
29206   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29207      node, lest it get absorbed into the surrounding block.  */
29208   stmt = push_stmt_list ();
29209   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29210   objc_begin_try_stmt (location, pop_stmt_list (stmt));
29211
29212   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29213     {
29214       cp_parameter_declarator *parm;
29215       tree parameter_declaration = error_mark_node;
29216       bool seen_open_paren = false;
29217
29218       cp_lexer_consume_token (parser->lexer);
29219       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29220         seen_open_paren = true;
29221       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29222         {
29223           /* We have "@catch (...)" (where the '...' are literally
29224              what is in the code).  Skip the '...'.
29225              parameter_declaration is set to NULL_TREE, and
29226              objc_being_catch_clauses() knows that that means
29227              '...'.  */
29228           cp_lexer_consume_token (parser->lexer);
29229           parameter_declaration = NULL_TREE;
29230         }
29231       else
29232         {
29233           /* We have "@catch (NSException *exception)" or something
29234              like that.  Parse the parameter declaration.  */
29235           parm = cp_parser_parameter_declaration (parser, false, NULL);
29236           if (parm == NULL)
29237             parameter_declaration = error_mark_node;
29238           else
29239             parameter_declaration = grokdeclarator (parm->declarator,
29240                                                     &parm->decl_specifiers,
29241                                                     PARM, /*initialized=*/0,
29242                                                     /*attrlist=*/NULL);
29243         }
29244       if (seen_open_paren)
29245         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29246       else
29247         {
29248           /* If there was no open parenthesis, we are recovering from
29249              an error, and we are trying to figure out what mistake
29250              the user has made.  */
29251
29252           /* If there is an immediate closing parenthesis, the user
29253              probably forgot the opening one (ie, they typed "@catch
29254              NSException *e)".  Parse the closing parenthesis and keep
29255              going.  */
29256           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29257             cp_lexer_consume_token (parser->lexer);
29258           
29259           /* If these is no immediate closing parenthesis, the user
29260              probably doesn't know that parenthesis are required at
29261              all (ie, they typed "@catch NSException *e").  So, just
29262              forget about the closing parenthesis and keep going.  */
29263         }
29264       objc_begin_catch_clause (parameter_declaration);
29265       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29266       objc_finish_catch_clause ();
29267     }
29268   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29269     {
29270       cp_lexer_consume_token (parser->lexer);
29271       location = cp_lexer_peek_token (parser->lexer)->location;
29272       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29273          node, lest it get absorbed into the surrounding block.  */
29274       stmt = push_stmt_list ();
29275       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29276       objc_build_finally_clause (location, pop_stmt_list (stmt));
29277     }
29278
29279   return objc_finish_try_stmt ();
29280 }
29281
29282 /* Parse an Objective-C synchronized statement.
29283
29284    objc-synchronized-stmt:
29285      @synchronized ( expression ) compound-statement
29286
29287    Returns NULL_TREE.  */
29288
29289 static tree
29290 cp_parser_objc_synchronized_statement (cp_parser *parser)
29291 {
29292   location_t location;
29293   tree lock, stmt;
29294
29295   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29296
29297   location = cp_lexer_peek_token (parser->lexer)->location;
29298   objc_maybe_warn_exceptions (location);
29299   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29300   lock = cp_parser_expression (parser);
29301   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29302
29303   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29304      node, lest it get absorbed into the surrounding block.  */
29305   stmt = push_stmt_list ();
29306   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29307
29308   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29309 }
29310
29311 /* Parse an Objective-C throw statement.
29312
29313    objc-throw-stmt:
29314      @throw assignment-expression [opt] ;
29315
29316    Returns a constructed '@throw' statement.  */
29317
29318 static tree
29319 cp_parser_objc_throw_statement (cp_parser *parser)
29320 {
29321   tree expr = NULL_TREE;
29322   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29323
29324   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29325
29326   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29327     expr = cp_parser_expression (parser);
29328
29329   cp_parser_consume_semicolon_at_end_of_statement (parser);
29330
29331   return objc_build_throw_stmt (loc, expr);
29332 }
29333
29334 /* Parse an Objective-C statement.  */
29335
29336 static tree
29337 cp_parser_objc_statement (cp_parser * parser)
29338 {
29339   /* Try to figure out what kind of declaration is present.  */
29340   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29341
29342   switch (kwd->keyword)
29343     {
29344     case RID_AT_TRY:
29345       return cp_parser_objc_try_catch_finally_statement (parser);
29346     case RID_AT_SYNCHRONIZED:
29347       return cp_parser_objc_synchronized_statement (parser);
29348     case RID_AT_THROW:
29349       return cp_parser_objc_throw_statement (parser);
29350     default:
29351       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29352                kwd->u.value);
29353       cp_parser_skip_to_end_of_block_or_statement (parser);
29354     }
29355
29356   return error_mark_node;
29357 }
29358
29359 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
29360    look ahead to see if an objc keyword follows the attributes.  This
29361    is to detect the use of prefix attributes on ObjC @interface and 
29362    @protocol.  */
29363
29364 static bool
29365 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29366 {
29367   cp_lexer_save_tokens (parser->lexer);
29368   *attrib = cp_parser_attributes_opt (parser);
29369   gcc_assert (*attrib);
29370   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29371     {
29372       cp_lexer_commit_tokens (parser->lexer);
29373       return true;
29374     }
29375   cp_lexer_rollback_tokens (parser->lexer);
29376   return false;  
29377 }
29378
29379 /* This routine is a minimal replacement for
29380    c_parser_struct_declaration () used when parsing the list of
29381    types/names or ObjC++ properties.  For example, when parsing the
29382    code
29383
29384    @property (readonly) int a, b, c;
29385
29386    this function is responsible for parsing "int a, int b, int c" and
29387    returning the declarations as CHAIN of DECLs.
29388
29389    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
29390    similar parsing.  */
29391 static tree
29392 cp_parser_objc_struct_declaration (cp_parser *parser)
29393 {
29394   tree decls = NULL_TREE;
29395   cp_decl_specifier_seq declspecs;
29396   int decl_class_or_enum_p;
29397   tree prefix_attributes;
29398
29399   cp_parser_decl_specifier_seq (parser,
29400                                 CP_PARSER_FLAGS_NONE,
29401                                 &declspecs,
29402                                 &decl_class_or_enum_p);
29403
29404   if (declspecs.type == error_mark_node)
29405     return error_mark_node;
29406
29407   /* auto, register, static, extern, mutable.  */
29408   if (declspecs.storage_class != sc_none)
29409     {
29410       cp_parser_error (parser, "invalid type for property");
29411       declspecs.storage_class = sc_none;
29412     }
29413   
29414   /* thread_local.  */
29415   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29416     {
29417       cp_parser_error (parser, "invalid type for property");
29418       declspecs.locations[ds_thread] = 0;
29419     }
29420   
29421   /* typedef.  */
29422   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29423     {
29424       cp_parser_error (parser, "invalid type for property");
29425       declspecs.locations[ds_typedef] = 0;
29426     }
29427
29428   prefix_attributes = declspecs.attributes;
29429   declspecs.attributes = NULL_TREE;
29430
29431   /* Keep going until we hit the `;' at the end of the declaration. */
29432   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29433     {
29434       tree attributes, first_attribute, decl;
29435       cp_declarator *declarator;
29436       cp_token *token;
29437
29438       /* Parse the declarator.  */
29439       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29440                                          NULL, NULL, false, false);
29441
29442       /* Look for attributes that apply to the ivar.  */
29443       attributes = cp_parser_attributes_opt (parser);
29444       /* Remember which attributes are prefix attributes and
29445          which are not.  */
29446       first_attribute = attributes;
29447       /* Combine the attributes.  */
29448       attributes = chainon (prefix_attributes, attributes);
29449       
29450       decl = grokfield (declarator, &declspecs,
29451                         NULL_TREE, /*init_const_expr_p=*/false,
29452                         NULL_TREE, attributes);
29453
29454       if (decl == error_mark_node || decl == NULL_TREE)
29455         return error_mark_node;
29456       
29457       /* Reset PREFIX_ATTRIBUTES.  */
29458       while (attributes && TREE_CHAIN (attributes) != first_attribute)
29459         attributes = TREE_CHAIN (attributes);
29460       if (attributes)
29461         TREE_CHAIN (attributes) = NULL_TREE;
29462
29463       DECL_CHAIN (decl) = decls;
29464       decls = decl;
29465
29466       token = cp_lexer_peek_token (parser->lexer);
29467       if (token->type == CPP_COMMA)
29468         {
29469           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
29470           continue;
29471         }
29472       else
29473         break;
29474     }
29475   return decls;
29476 }
29477
29478 /* Parse an Objective-C @property declaration.  The syntax is:
29479
29480    objc-property-declaration:
29481      '@property' objc-property-attributes[opt] struct-declaration ;
29482
29483    objc-property-attributes:
29484     '(' objc-property-attribute-list ')'
29485
29486    objc-property-attribute-list:
29487      objc-property-attribute
29488      objc-property-attribute-list, objc-property-attribute
29489
29490    objc-property-attribute
29491      'getter' = identifier
29492      'setter' = identifier
29493      'readonly'
29494      'readwrite'
29495      'assign'
29496      'retain'
29497      'copy'
29498      'nonatomic'
29499
29500   For example:
29501     @property NSString *name;
29502     @property (readonly) id object;
29503     @property (retain, nonatomic, getter=getTheName) id name;
29504     @property int a, b, c;
29505
29506    PS: This function is identical to
29507    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
29508 static void 
29509 cp_parser_objc_at_property_declaration (cp_parser *parser)
29510 {
29511   /* The following variables hold the attributes of the properties as
29512      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
29513      seen.  When we see an attribute, we set them to 'true' (if they
29514      are boolean properties) or to the identifier (if they have an
29515      argument, ie, for getter and setter).  Note that here we only
29516      parse the list of attributes, check the syntax and accumulate the
29517      attributes that we find.  objc_add_property_declaration() will
29518      then process the information.  */
29519   bool property_assign = false;
29520   bool property_copy = false;
29521   tree property_getter_ident = NULL_TREE;
29522   bool property_nonatomic = false;
29523   bool property_readonly = false;
29524   bool property_readwrite = false;
29525   bool property_retain = false;
29526   tree property_setter_ident = NULL_TREE;
29527
29528   /* 'properties' is the list of properties that we read.  Usually a
29529      single one, but maybe more (eg, in "@property int a, b, c;" there
29530      are three).  */
29531   tree properties;
29532   location_t loc;
29533
29534   loc = cp_lexer_peek_token (parser->lexer)->location;
29535
29536   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
29537
29538   /* Parse the optional attribute list...  */
29539   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29540     {
29541       /* Eat the '('.  */
29542       cp_lexer_consume_token (parser->lexer);
29543
29544       while (true)
29545         {
29546           bool syntax_error = false;
29547           cp_token *token = cp_lexer_peek_token (parser->lexer);
29548           enum rid keyword;
29549
29550           if (token->type != CPP_NAME)
29551             {
29552               cp_parser_error (parser, "expected identifier");
29553               break;
29554             }
29555           keyword = C_RID_CODE (token->u.value);
29556           cp_lexer_consume_token (parser->lexer);
29557           switch (keyword)
29558             {
29559             case RID_ASSIGN:    property_assign = true;    break;
29560             case RID_COPY:      property_copy = true;      break;
29561             case RID_NONATOMIC: property_nonatomic = true; break;
29562             case RID_READONLY:  property_readonly = true;  break;
29563             case RID_READWRITE: property_readwrite = true; break;
29564             case RID_RETAIN:    property_retain = true;    break;
29565
29566             case RID_GETTER:
29567             case RID_SETTER:
29568               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29569                 {
29570                   if (keyword == RID_GETTER)
29571                     cp_parser_error (parser,
29572                                      "missing %<=%> (after %<getter%> attribute)");
29573                   else
29574                     cp_parser_error (parser,
29575                                      "missing %<=%> (after %<setter%> attribute)");
29576                   syntax_error = true;
29577                   break;
29578                 }
29579               cp_lexer_consume_token (parser->lexer); /* eat the = */
29580               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29581                 {
29582                   cp_parser_error (parser, "expected identifier");
29583                   syntax_error = true;
29584                   break;
29585                 }
29586               if (keyword == RID_SETTER)
29587                 {
29588                   if (property_setter_ident != NULL_TREE)
29589                     {
29590                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29591                       cp_lexer_consume_token (parser->lexer);
29592                     }
29593                   else
29594                     property_setter_ident = cp_parser_objc_selector (parser);
29595                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29596                     cp_parser_error (parser, "setter name must terminate with %<:%>");
29597                   else
29598                     cp_lexer_consume_token (parser->lexer);
29599                 }
29600               else
29601                 {
29602                   if (property_getter_ident != NULL_TREE)
29603                     {
29604                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29605                       cp_lexer_consume_token (parser->lexer);
29606                     }
29607                   else
29608                     property_getter_ident = cp_parser_objc_selector (parser);
29609                 }
29610               break;
29611             default:
29612               cp_parser_error (parser, "unknown property attribute");
29613               syntax_error = true;
29614               break;
29615             }
29616
29617           if (syntax_error)
29618             break;
29619
29620           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29621             cp_lexer_consume_token (parser->lexer);
29622           else
29623             break;
29624         }
29625
29626       /* FIXME: "@property (setter, assign);" will generate a spurious
29627          "error: expected â€˜)’ before â€˜,’ token".  This is because
29628          cp_parser_require, unlike the C counterpart, will produce an
29629          error even if we are in error recovery.  */
29630       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29631         {
29632           cp_parser_skip_to_closing_parenthesis (parser,
29633                                                  /*recovering=*/true,
29634                                                  /*or_comma=*/false,
29635                                                  /*consume_paren=*/true);
29636         }
29637     }
29638
29639   /* ... and the property declaration(s).  */
29640   properties = cp_parser_objc_struct_declaration (parser);
29641
29642   if (properties == error_mark_node)
29643     {
29644       cp_parser_skip_to_end_of_statement (parser);
29645       /* If the next token is now a `;', consume it.  */
29646       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29647         cp_lexer_consume_token (parser->lexer);
29648       return;
29649     }
29650
29651   if (properties == NULL_TREE)
29652     cp_parser_error (parser, "expected identifier");
29653   else
29654     {
29655       /* Comma-separated properties are chained together in
29656          reverse order; add them one by one.  */
29657       properties = nreverse (properties);
29658       
29659       for (; properties; properties = TREE_CHAIN (properties))
29660         objc_add_property_declaration (loc, copy_node (properties),
29661                                        property_readonly, property_readwrite,
29662                                        property_assign, property_retain,
29663                                        property_copy, property_nonatomic,
29664                                        property_getter_ident, property_setter_ident);
29665     }
29666   
29667   cp_parser_consume_semicolon_at_end_of_statement (parser);
29668 }
29669
29670 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
29671
29672    objc-synthesize-declaration:
29673      @synthesize objc-synthesize-identifier-list ;
29674
29675    objc-synthesize-identifier-list:
29676      objc-synthesize-identifier
29677      objc-synthesize-identifier-list, objc-synthesize-identifier
29678
29679    objc-synthesize-identifier
29680      identifier
29681      identifier = identifier
29682
29683   For example:
29684     @synthesize MyProperty;
29685     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29686
29687   PS: This function is identical to c_parser_objc_at_synthesize_declaration
29688   for C.  Keep them in sync.
29689 */
29690 static void 
29691 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29692 {
29693   tree list = NULL_TREE;
29694   location_t loc;
29695   loc = cp_lexer_peek_token (parser->lexer)->location;
29696
29697   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
29698   while (true)
29699     {
29700       tree property, ivar;
29701       property = cp_parser_identifier (parser);
29702       if (property == error_mark_node)
29703         {
29704           cp_parser_consume_semicolon_at_end_of_statement (parser);
29705           return;
29706         }
29707       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29708         {
29709           cp_lexer_consume_token (parser->lexer);
29710           ivar = cp_parser_identifier (parser);
29711           if (ivar == error_mark_node)
29712             {
29713               cp_parser_consume_semicolon_at_end_of_statement (parser);
29714               return;
29715             }
29716         }
29717       else
29718         ivar = NULL_TREE;
29719       list = chainon (list, build_tree_list (ivar, property));
29720       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29721         cp_lexer_consume_token (parser->lexer);
29722       else
29723         break;
29724     }
29725   cp_parser_consume_semicolon_at_end_of_statement (parser);
29726   objc_add_synthesize_declaration (loc, list);
29727 }
29728
29729 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
29730
29731    objc-dynamic-declaration:
29732      @dynamic identifier-list ;
29733
29734    For example:
29735      @dynamic MyProperty;
29736      @dynamic MyProperty, AnotherProperty;
29737
29738   PS: This function is identical to c_parser_objc_at_dynamic_declaration
29739   for C.  Keep them in sync.
29740 */
29741 static void 
29742 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29743 {
29744   tree list = NULL_TREE;
29745   location_t loc;
29746   loc = cp_lexer_peek_token (parser->lexer)->location;
29747
29748   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
29749   while (true)
29750     {
29751       tree property;
29752       property = cp_parser_identifier (parser);
29753       if (property == error_mark_node)
29754         {
29755           cp_parser_consume_semicolon_at_end_of_statement (parser);
29756           return;
29757         }
29758       list = chainon (list, build_tree_list (NULL, property));
29759       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29760         cp_lexer_consume_token (parser->lexer);
29761       else
29762         break;
29763     }
29764   cp_parser_consume_semicolon_at_end_of_statement (parser);
29765   objc_add_dynamic_declaration (loc, list);
29766 }
29767
29768 \f
29769 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
29770
29771 /* Returns name of the next clause.
29772    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29773    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
29774    returned and the token is consumed.  */
29775
29776 static pragma_omp_clause
29777 cp_parser_omp_clause_name (cp_parser *parser)
29778 {
29779   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29780
29781   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29782     result = PRAGMA_OACC_CLAUSE_AUTO;
29783   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29784     result = PRAGMA_OMP_CLAUSE_IF;
29785   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29786     result = PRAGMA_OMP_CLAUSE_DEFAULT;
29787   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29788     result = PRAGMA_OACC_CLAUSE_DELETE;
29789   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29790     result = PRAGMA_OMP_CLAUSE_PRIVATE;
29791   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29792     result = PRAGMA_OMP_CLAUSE_FOR;
29793   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29794     {
29795       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29796       const char *p = IDENTIFIER_POINTER (id);
29797
29798       switch (p[0])
29799         {
29800         case 'a':
29801           if (!strcmp ("aligned", p))
29802             result = PRAGMA_OMP_CLAUSE_ALIGNED;
29803           else if (!strcmp ("async", p))
29804             result = PRAGMA_OACC_CLAUSE_ASYNC;
29805           break;
29806         case 'c':
29807           if (!strcmp ("collapse", p))
29808             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29809           else if (!strcmp ("copy", p))
29810             result = PRAGMA_OACC_CLAUSE_COPY;
29811           else if (!strcmp ("copyin", p))
29812             result = PRAGMA_OMP_CLAUSE_COPYIN;
29813           else if (!strcmp ("copyout", p))
29814             result = PRAGMA_OACC_CLAUSE_COPYOUT;
29815           else if (!strcmp ("copyprivate", p))
29816             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29817           else if (!strcmp ("create", p))
29818             result = PRAGMA_OACC_CLAUSE_CREATE;
29819           break;
29820         case 'd':
29821           if (!strcmp ("defaultmap", p))
29822             result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29823           else if (!strcmp ("depend", p))
29824             result = PRAGMA_OMP_CLAUSE_DEPEND;
29825           else if (!strcmp ("device", p))
29826             result = PRAGMA_OMP_CLAUSE_DEVICE;
29827           else if (!strcmp ("deviceptr", p))
29828             result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29829           else if (!strcmp ("device_resident", p))
29830             result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29831           else if (!strcmp ("dist_schedule", p))
29832             result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29833           break;
29834         case 'f':
29835           if (!strcmp ("final", p))
29836             result = PRAGMA_OMP_CLAUSE_FINAL;
29837           else if (!strcmp ("firstprivate", p))
29838             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29839           else if (!strcmp ("from", p))
29840             result = PRAGMA_OMP_CLAUSE_FROM;
29841           break;
29842         case 'g':
29843           if (!strcmp ("gang", p))
29844             result = PRAGMA_OACC_CLAUSE_GANG;
29845           else if (!strcmp ("grainsize", p))
29846             result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29847           break;
29848         case 'h':
29849           if (!strcmp ("hint", p))
29850             result = PRAGMA_OMP_CLAUSE_HINT;
29851           else if (!strcmp ("host", p))
29852             result = PRAGMA_OACC_CLAUSE_HOST;
29853           break;
29854         case 'i':
29855           if (!strcmp ("inbranch", p))
29856             result = PRAGMA_OMP_CLAUSE_INBRANCH;
29857           else if (!strcmp ("independent", p))
29858             result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29859           else if (!strcmp ("is_device_ptr", p))
29860             result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29861           break;
29862         case 'l':
29863           if (!strcmp ("lastprivate", p))
29864             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29865           else if (!strcmp ("linear", p))
29866             result = PRAGMA_OMP_CLAUSE_LINEAR;
29867           else if (!strcmp ("link", p))
29868             result = PRAGMA_OMP_CLAUSE_LINK;
29869           break;
29870         case 'm':
29871           if (!strcmp ("map", p))
29872             result = PRAGMA_OMP_CLAUSE_MAP;
29873           else if (!strcmp ("mergeable", p))
29874             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29875           else if (flag_cilkplus && !strcmp ("mask", p))
29876             result = PRAGMA_CILK_CLAUSE_MASK;
29877           break;
29878         case 'n':
29879           if (!strcmp ("nogroup", p))
29880             result = PRAGMA_OMP_CLAUSE_NOGROUP;
29881           else if (!strcmp ("notinbranch", p))
29882             result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29883           else if (!strcmp ("nowait", p))
29884             result = PRAGMA_OMP_CLAUSE_NOWAIT;
29885           else if (flag_cilkplus && !strcmp ("nomask", p))
29886             result = PRAGMA_CILK_CLAUSE_NOMASK;
29887           else if (!strcmp ("num_gangs", p))
29888             result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29889           else if (!strcmp ("num_tasks", p))
29890             result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29891           else if (!strcmp ("num_teams", p))
29892             result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29893           else if (!strcmp ("num_threads", p))
29894             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29895           else if (!strcmp ("num_workers", p))
29896             result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29897           break;
29898         case 'o':
29899           if (!strcmp ("ordered", p))
29900             result = PRAGMA_OMP_CLAUSE_ORDERED;
29901           break;
29902         case 'p':
29903           if (!strcmp ("parallel", p))
29904             result = PRAGMA_OMP_CLAUSE_PARALLEL;
29905           else if (!strcmp ("present", p))
29906             result = PRAGMA_OACC_CLAUSE_PRESENT;
29907           else if (!strcmp ("present_or_copy", p)
29908                    || !strcmp ("pcopy", p))
29909             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29910           else if (!strcmp ("present_or_copyin", p)
29911                    || !strcmp ("pcopyin", p))
29912             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29913           else if (!strcmp ("present_or_copyout", p)
29914                    || !strcmp ("pcopyout", p))
29915             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29916           else if (!strcmp ("present_or_create", p)
29917                    || !strcmp ("pcreate", p))
29918             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29919           else if (!strcmp ("priority", p))
29920             result = PRAGMA_OMP_CLAUSE_PRIORITY;
29921           else if (!strcmp ("proc_bind", p))
29922             result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29923           break;
29924         case 'r':
29925           if (!strcmp ("reduction", p))
29926             result = PRAGMA_OMP_CLAUSE_REDUCTION;
29927           break;
29928         case 's':
29929           if (!strcmp ("safelen", p))
29930             result = PRAGMA_OMP_CLAUSE_SAFELEN;
29931           else if (!strcmp ("schedule", p))
29932             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29933           else if (!strcmp ("sections", p))
29934             result = PRAGMA_OMP_CLAUSE_SECTIONS;
29935           else if (!strcmp ("self", p))
29936             result = PRAGMA_OACC_CLAUSE_SELF;
29937           else if (!strcmp ("seq", p))
29938             result = PRAGMA_OACC_CLAUSE_SEQ;
29939           else if (!strcmp ("shared", p))
29940             result = PRAGMA_OMP_CLAUSE_SHARED;
29941           else if (!strcmp ("simd", p))
29942             result = PRAGMA_OMP_CLAUSE_SIMD;
29943           else if (!strcmp ("simdlen", p))
29944             result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29945           break;
29946         case 't':
29947           if (!strcmp ("taskgroup", p))
29948             result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29949           else if (!strcmp ("thread_limit", p))
29950             result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29951           else if (!strcmp ("threads", p))
29952             result = PRAGMA_OMP_CLAUSE_THREADS;
29953           else if (!strcmp ("tile", p))
29954             result = PRAGMA_OACC_CLAUSE_TILE;
29955           else if (!strcmp ("to", p))
29956             result = PRAGMA_OMP_CLAUSE_TO;
29957           break;
29958         case 'u':
29959           if (!strcmp ("uniform", p))
29960             result = PRAGMA_OMP_CLAUSE_UNIFORM;
29961           else if (!strcmp ("untied", p))
29962             result = PRAGMA_OMP_CLAUSE_UNTIED;
29963           else if (!strcmp ("use_device", p))
29964             result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29965           else if (!strcmp ("use_device_ptr", p))
29966             result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29967           break;
29968         case 'v':
29969           if (!strcmp ("vector", p))
29970             result = PRAGMA_OACC_CLAUSE_VECTOR;
29971           else if (!strcmp ("vector_length", p))
29972             result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29973           else if (flag_cilkplus && !strcmp ("vectorlength", p))
29974             result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29975           break;
29976         case 'w':
29977           if (!strcmp ("wait", p))
29978             result = PRAGMA_OACC_CLAUSE_WAIT;
29979           else if (!strcmp ("worker", p))
29980             result = PRAGMA_OACC_CLAUSE_WORKER;
29981           break;
29982         }
29983     }
29984
29985   if (result != PRAGMA_OMP_CLAUSE_NONE)
29986     cp_lexer_consume_token (parser->lexer);
29987
29988   return result;
29989 }
29990
29991 /* Validate that a clause of the given type does not already exist.  */
29992
29993 static void
29994 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29995                            const char *name, location_t location)
29996 {
29997   tree c;
29998
29999   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30000     if (OMP_CLAUSE_CODE (c) == code)
30001       {
30002         error_at (location, "too many %qs clauses", name);
30003         break;
30004       }
30005 }
30006
30007 /* OpenMP 2.5:
30008    variable-list:
30009      identifier
30010      variable-list , identifier
30011
30012    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30013    colon).  An opening parenthesis will have been consumed by the caller.
30014
30015    If KIND is nonzero, create the appropriate node and install the decl
30016    in OMP_CLAUSE_DECL and add the node to the head of the list.
30017
30018    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30019    return the list created.
30020
30021    COLON can be NULL if only closing parenthesis should end the list,
30022    or pointer to bool which will receive false if the list is terminated
30023    by closing parenthesis or true if the list is terminated by colon.  */
30024
30025 static tree
30026 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30027                                 tree list, bool *colon)
30028 {
30029   cp_token *token;
30030   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30031   if (colon)
30032     {
30033       parser->colon_corrects_to_scope_p = false;
30034       *colon = false;
30035     }
30036   while (1)
30037     {
30038       tree name, decl;
30039
30040       token = cp_lexer_peek_token (parser->lexer);
30041       if (kind != 0
30042           && current_class_ptr
30043           && cp_parser_is_keyword (token, RID_THIS))
30044         {
30045           decl = finish_this_expr ();
30046           if (TREE_CODE (decl) == NON_LVALUE_EXPR
30047               || CONVERT_EXPR_P (decl))
30048             decl = TREE_OPERAND (decl, 0);
30049           cp_lexer_consume_token (parser->lexer);
30050         }
30051       else
30052         {
30053           name = cp_parser_id_expression (parser, /*template_p=*/false,
30054                                           /*check_dependency_p=*/true,
30055                                           /*template_p=*/NULL,
30056                                           /*declarator_p=*/false,
30057                                           /*optional_p=*/false);
30058           if (name == error_mark_node)
30059             goto skip_comma;
30060
30061           decl = cp_parser_lookup_name_simple (parser, name, token->location);
30062           if (decl == error_mark_node)
30063             cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30064                                          token->location);
30065         }
30066       if (decl == error_mark_node)
30067         ;
30068       else if (kind != 0)
30069         {
30070           switch (kind)
30071             {
30072             case OMP_CLAUSE__CACHE_:
30073               /* The OpenACC cache directive explicitly only allows "array
30074                  elements or subarrays".  */
30075               if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30076                 {
30077                   error_at (token->location, "expected %<[%>");
30078                   decl = error_mark_node;
30079                   break;
30080                 }
30081               /* FALLTHROUGH.  */
30082             case OMP_CLAUSE_MAP:
30083             case OMP_CLAUSE_FROM:
30084             case OMP_CLAUSE_TO:
30085               while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30086                 {
30087                   location_t loc
30088                     = cp_lexer_peek_token (parser->lexer)->location;
30089                   cp_id_kind idk = CP_ID_KIND_NONE;
30090                   cp_lexer_consume_token (parser->lexer);
30091                   decl = convert_from_reference (decl);
30092                   decl
30093                     = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30094                                                               decl, false,
30095                                                               &idk, loc);
30096                 }
30097               /* FALLTHROUGH.  */
30098             case OMP_CLAUSE_DEPEND:
30099             case OMP_CLAUSE_REDUCTION:
30100               while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30101                 {
30102                   tree low_bound = NULL_TREE, length = NULL_TREE;
30103
30104                   parser->colon_corrects_to_scope_p = false;
30105                   cp_lexer_consume_token (parser->lexer);
30106                   if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30107                     low_bound = cp_parser_expression (parser);
30108                   if (!colon)
30109                     parser->colon_corrects_to_scope_p
30110                       = saved_colon_corrects_to_scope_p;
30111                   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30112                     length = integer_one_node;
30113                   else
30114                     {
30115                       /* Look for `:'.  */
30116                       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30117                         goto skip_comma;
30118                       if (!cp_lexer_next_token_is (parser->lexer,
30119                                                    CPP_CLOSE_SQUARE))
30120                         length = cp_parser_expression (parser);
30121                     }
30122                   /* Look for the closing `]'.  */
30123                   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30124                                           RT_CLOSE_SQUARE))
30125                     goto skip_comma;
30126
30127                   decl = tree_cons (low_bound, length, decl);
30128                 }
30129               break;
30130             default:
30131               break;
30132             }
30133
30134           tree u = build_omp_clause (token->location, kind);
30135           OMP_CLAUSE_DECL (u) = decl;
30136           OMP_CLAUSE_CHAIN (u) = list;
30137           list = u;
30138         }
30139       else
30140         list = tree_cons (decl, NULL_TREE, list);
30141
30142     get_comma:
30143       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30144         break;
30145       cp_lexer_consume_token (parser->lexer);
30146     }
30147
30148   if (colon)
30149     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30150
30151   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30152     {
30153       *colon = true;
30154       cp_parser_require (parser, CPP_COLON, RT_COLON);
30155       return list;
30156     }
30157
30158   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30159     {
30160       int ending;
30161
30162       /* Try to resync to an unnested comma.  Copied from
30163          cp_parser_parenthesized_expression_list.  */
30164     skip_comma:
30165       if (colon)
30166         parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30167       ending = cp_parser_skip_to_closing_parenthesis (parser,
30168                                                       /*recovering=*/true,
30169                                                       /*or_comma=*/true,
30170                                                       /*consume_paren=*/true);
30171       if (ending < 0)
30172         goto get_comma;
30173     }
30174
30175   return list;
30176 }
30177
30178 /* Similarly, but expect leading and trailing parenthesis.  This is a very
30179    common case for omp clauses.  */
30180
30181 static tree
30182 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30183 {
30184   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30185     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30186   return list;
30187 }
30188
30189 /* OpenACC 2.0:
30190    copy ( variable-list )
30191    copyin ( variable-list )
30192    copyout ( variable-list )
30193    create ( variable-list )
30194    delete ( variable-list )
30195    present ( variable-list )
30196    present_or_copy ( variable-list )
30197      pcopy ( variable-list )
30198    present_or_copyin ( variable-list )
30199      pcopyin ( variable-list )
30200    present_or_copyout ( variable-list )
30201      pcopyout ( variable-list )
30202    present_or_create ( variable-list )
30203      pcreate ( variable-list ) */
30204
30205 static tree
30206 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30207                             tree list)
30208 {
30209   enum gomp_map_kind kind;
30210   switch (c_kind)
30211     {
30212     case PRAGMA_OACC_CLAUSE_COPY:
30213       kind = GOMP_MAP_FORCE_TOFROM;
30214       break;
30215     case PRAGMA_OACC_CLAUSE_COPYIN:
30216       kind = GOMP_MAP_FORCE_TO;
30217       break;
30218     case PRAGMA_OACC_CLAUSE_COPYOUT:
30219       kind = GOMP_MAP_FORCE_FROM;
30220       break;
30221     case PRAGMA_OACC_CLAUSE_CREATE:
30222       kind = GOMP_MAP_FORCE_ALLOC;
30223       break;
30224     case PRAGMA_OACC_CLAUSE_DELETE:
30225       kind = GOMP_MAP_DELETE;
30226       break;
30227     case PRAGMA_OACC_CLAUSE_DEVICE:
30228       kind = GOMP_MAP_FORCE_TO;
30229       break;
30230     case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30231       kind = GOMP_MAP_DEVICE_RESIDENT;
30232       break;
30233     case PRAGMA_OACC_CLAUSE_HOST:
30234     case PRAGMA_OACC_CLAUSE_SELF:
30235       kind = GOMP_MAP_FORCE_FROM;
30236       break;
30237     case PRAGMA_OACC_CLAUSE_LINK:
30238       kind = GOMP_MAP_LINK;
30239       break;
30240     case PRAGMA_OACC_CLAUSE_PRESENT:
30241       kind = GOMP_MAP_FORCE_PRESENT;
30242       break;
30243     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30244       kind = GOMP_MAP_TOFROM;
30245       break;
30246     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30247       kind = GOMP_MAP_TO;
30248       break;
30249     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30250       kind = GOMP_MAP_FROM;
30251       break;
30252     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30253       kind = GOMP_MAP_ALLOC;
30254       break;
30255     default:
30256       gcc_unreachable ();
30257     }
30258   tree nl, c;
30259   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30260
30261   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30262     OMP_CLAUSE_SET_MAP_KIND (c, kind);
30263
30264   return nl;
30265 }
30266
30267 /* OpenACC 2.0:
30268    deviceptr ( variable-list ) */
30269
30270 static tree
30271 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30272 {
30273   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30274   tree vars, t;
30275
30276   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30277      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30278      variable-list must only allow for pointer variables.  */
30279   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30280   for (t = vars; t; t = TREE_CHAIN (t))
30281     {
30282       tree v = TREE_PURPOSE (t);
30283       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30284       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30285       OMP_CLAUSE_DECL (u) = v;
30286       OMP_CLAUSE_CHAIN (u) = list;
30287       list = u;
30288     }
30289
30290   return list;
30291 }
30292
30293 /* OpenACC 2.0:
30294    auto
30295    independent
30296    nohost
30297    seq */
30298
30299 static tree
30300 cp_parser_oacc_simple_clause (cp_parser * /* parser  */,
30301                               enum omp_clause_code code,
30302                               tree list, location_t location)
30303 {
30304   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30305   tree c = build_omp_clause (location, code);
30306   OMP_CLAUSE_CHAIN (c) = list;
30307   return c;
30308 }
30309
30310  /* OpenACC:
30311    num_gangs ( expression )
30312    num_workers ( expression )
30313    vector_length ( expression )  */
30314
30315 static tree
30316 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30317                                   const char *str, tree list)
30318 {
30319   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30320
30321   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30322     return list;
30323
30324   tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30325
30326   if (t == error_mark_node
30327       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30328     {
30329       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30330                                              /*or_comma=*/false,
30331                                              /*consume_paren=*/true);
30332       return list;
30333     }
30334
30335   check_no_duplicate_clause (list, code, str, loc);
30336
30337   tree c = build_omp_clause (loc, code);
30338   OMP_CLAUSE_OPERAND (c, 0) = t;
30339   OMP_CLAUSE_CHAIN (c) = list;
30340   return c;
30341 }
30342
30343 /* OpenACC:
30344
30345     gang [( gang-arg-list )]
30346     worker [( [num:] int-expr )]
30347     vector [( [length:] int-expr )]
30348
30349   where gang-arg is one of:
30350
30351     [num:] int-expr
30352     static: size-expr
30353
30354   and size-expr may be:
30355
30356     *
30357     int-expr
30358 */
30359
30360 static tree
30361 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30362                              const char *str, tree list)
30363 {
30364   const char *id = "num";
30365   cp_lexer *lexer = parser->lexer;
30366   tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30367   location_t loc = cp_lexer_peek_token (lexer)->location;
30368
30369   if (kind == OMP_CLAUSE_VECTOR)
30370     id = "length";
30371
30372   if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30373     {
30374       cp_lexer_consume_token (lexer);
30375
30376       do
30377         {
30378           cp_token *next = cp_lexer_peek_token (lexer);
30379           int idx = 0;
30380
30381           /* Gang static argument.  */
30382           if (kind == OMP_CLAUSE_GANG
30383               && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30384             {
30385               cp_lexer_consume_token (lexer);
30386
30387               if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30388                 goto cleanup_error;
30389
30390               idx = 1;
30391               if (ops[idx] != NULL)
30392                 {
30393                   cp_parser_error (parser, "too many %<static%> arguments");
30394                   goto cleanup_error;
30395                 }
30396
30397               /* Check for the '*' argument.  */
30398               if (cp_lexer_next_token_is (lexer, CPP_MULT)
30399                   && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30400                       || cp_lexer_nth_token_is (parser->lexer, 2,
30401                                                 CPP_CLOSE_PAREN)))
30402                 {
30403                   cp_lexer_consume_token (lexer);
30404                   ops[idx] = integer_minus_one_node;
30405
30406                   if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30407                     {
30408                       cp_lexer_consume_token (lexer);
30409                       continue;
30410                     }
30411                   else break;
30412                 }
30413             }
30414           /* Worker num: argument and vector length: arguments.  */
30415           else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30416                    && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30417                    && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30418             {
30419               cp_lexer_consume_token (lexer);  /* id  */
30420               cp_lexer_consume_token (lexer);  /* ':'  */
30421             }
30422
30423           /* Now collect the actual argument.  */
30424           if (ops[idx] != NULL_TREE)
30425             {
30426               cp_parser_error (parser, "unexpected argument");
30427               goto cleanup_error;
30428             }
30429
30430           tree expr = cp_parser_assignment_expression (parser, NULL, false,
30431                                                        false);
30432           if (expr == error_mark_node)
30433             goto cleanup_error;
30434
30435           mark_exp_read (expr);
30436           ops[idx] = expr;
30437
30438           if (kind == OMP_CLAUSE_GANG
30439               && cp_lexer_next_token_is (lexer, CPP_COMMA))
30440             {
30441               cp_lexer_consume_token (lexer);
30442               continue;
30443             }
30444           break;
30445         }
30446       while (1);
30447
30448       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30449         goto cleanup_error;
30450     }
30451
30452   check_no_duplicate_clause (list, kind, str, loc);
30453
30454   c = build_omp_clause (loc, kind);
30455
30456   if (ops[1])
30457     OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30458
30459   OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30460   OMP_CLAUSE_CHAIN (c) = list;
30461
30462   return c;
30463
30464  cleanup_error:
30465   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30466   return list;
30467 }
30468
30469 /* OpenACC 2.0:
30470    tile ( size-expr-list ) */
30471
30472 static tree
30473 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30474 {
30475   tree c, expr = error_mark_node;
30476   tree tile = NULL_TREE;
30477
30478   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30479
30480   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30481     return list;
30482
30483   do
30484     {
30485       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30486           && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30487               || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30488         {
30489           cp_lexer_consume_token (parser->lexer);
30490           expr = integer_minus_one_node;
30491         }
30492       else
30493         expr = cp_parser_assignment_expression (parser, NULL, false, false);
30494
30495       if (expr == error_mark_node)
30496         return list;
30497
30498       tile = tree_cons (NULL_TREE, expr, tile);
30499
30500       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30501         cp_lexer_consume_token (parser->lexer);
30502     }
30503   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30504
30505   /* Consume the trailing ')'.  */
30506   cp_lexer_consume_token (parser->lexer);
30507
30508   c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30509   tile = nreverse (tile);
30510   OMP_CLAUSE_TILE_LIST (c) = tile;
30511   OMP_CLAUSE_CHAIN (c) = list;
30512   return c;
30513 }
30514
30515 /* OpenACC 2.0
30516    Parse wait clause or directive parameters.  */
30517
30518 static tree
30519 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30520 {
30521   vec<tree, va_gc> *args;
30522   tree t, args_tree;
30523
30524   args = cp_parser_parenthesized_expression_list (parser, non_attr,
30525                                                   /*cast_p=*/false,
30526                                                   /*allow_expansion_p=*/true,
30527                                                   /*non_constant_p=*/NULL);
30528
30529   if (args == NULL || args->length () == 0)
30530     {
30531       cp_parser_error (parser, "expected integer expression before ')'");
30532       if (args != NULL)
30533         release_tree_vector (args);
30534       return list;
30535     }
30536
30537   args_tree = build_tree_list_vec (args);
30538
30539   release_tree_vector (args);
30540
30541   for (t = args_tree; t; t = TREE_CHAIN (t))
30542     {
30543       tree targ = TREE_VALUE (t);
30544
30545       if (targ != error_mark_node)
30546         {
30547           if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30548             error ("%<wait%> expression must be integral");
30549           else
30550             {
30551               tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30552
30553               mark_rvalue_use (targ);
30554               OMP_CLAUSE_DECL (c) = targ;
30555               OMP_CLAUSE_CHAIN (c) = list;
30556               list = c;
30557             }
30558         }
30559     }
30560
30561   return list;
30562 }
30563
30564 /* OpenACC:
30565    wait ( int-expr-list ) */
30566
30567 static tree
30568 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30569 {
30570   location_t location = cp_lexer_peek_token (parser->lexer)->location;
30571
30572   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30573     return list;
30574
30575   list = cp_parser_oacc_wait_list (parser, location, list);
30576
30577   return list;
30578 }
30579
30580 /* OpenMP 3.0:
30581    collapse ( constant-expression ) */
30582
30583 static tree
30584 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30585 {
30586   tree c, num;
30587   location_t loc;
30588   HOST_WIDE_INT n;
30589
30590   loc = cp_lexer_peek_token (parser->lexer)->location;
30591   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30592     return list;
30593
30594   num = cp_parser_constant_expression (parser);
30595
30596   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30597     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30598                                            /*or_comma=*/false,
30599                                            /*consume_paren=*/true);
30600
30601   if (num == error_mark_node)
30602     return list;
30603   num = fold_non_dependent_expr (num);
30604   if (!tree_fits_shwi_p (num)
30605       || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30606       || (n = tree_to_shwi (num)) <= 0
30607       || (int) n != n)
30608     {
30609       error_at (loc, "collapse argument needs positive constant integer expression");
30610       return list;
30611     }
30612
30613   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30614   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30615   OMP_CLAUSE_CHAIN (c) = list;
30616   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30617
30618   return c;
30619 }
30620
30621 /* OpenMP 2.5:
30622    default ( shared | none )
30623
30624    OpenACC 2.0
30625    default (none) */
30626
30627 static tree
30628 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30629                               location_t location, bool is_oacc)
30630 {
30631   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30632   tree c;
30633
30634   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30635     return list;
30636   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30637     {
30638       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30639       const char *p = IDENTIFIER_POINTER (id);
30640
30641       switch (p[0])
30642         {
30643         case 'n':
30644           if (strcmp ("none", p) != 0)
30645             goto invalid_kind;
30646           kind = OMP_CLAUSE_DEFAULT_NONE;
30647           break;
30648
30649         case 's':
30650           if (strcmp ("shared", p) != 0 || is_oacc)
30651             goto invalid_kind;
30652           kind = OMP_CLAUSE_DEFAULT_SHARED;
30653           break;
30654
30655         default:
30656           goto invalid_kind;
30657         }
30658
30659       cp_lexer_consume_token (parser->lexer);
30660     }
30661   else
30662     {
30663     invalid_kind:
30664       if (is_oacc)
30665         cp_parser_error (parser, "expected %<none%>");
30666       else
30667         cp_parser_error (parser, "expected %<none%> or %<shared%>");
30668     }
30669
30670   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30671     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30672                                            /*or_comma=*/false,
30673                                            /*consume_paren=*/true);
30674
30675   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30676     return list;
30677
30678   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30679   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30680   OMP_CLAUSE_CHAIN (c) = list;
30681   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30682
30683   return c;
30684 }
30685
30686 /* OpenMP 3.1:
30687    final ( expression ) */
30688
30689 static tree
30690 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30691 {
30692   tree t, c;
30693
30694   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30695     return list;
30696
30697   t = cp_parser_condition (parser);
30698
30699   if (t == error_mark_node
30700       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30701     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30702                                            /*or_comma=*/false,
30703                                            /*consume_paren=*/true);
30704
30705   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30706
30707   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30708   OMP_CLAUSE_FINAL_EXPR (c) = t;
30709   OMP_CLAUSE_CHAIN (c) = list;
30710
30711   return c;
30712 }
30713
30714 /* OpenMP 2.5:
30715    if ( expression )
30716
30717    OpenMP 4.5:
30718    if ( directive-name-modifier : expression )
30719
30720    directive-name-modifier:
30721      parallel | task | taskloop | target data | target | target update
30722      | target enter data | target exit data  */
30723
30724 static tree
30725 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30726                          bool is_omp)
30727 {
30728   tree t, c;
30729   enum tree_code if_modifier = ERROR_MARK;
30730
30731   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30732     return list;
30733
30734   if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30735     {
30736       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30737       const char *p = IDENTIFIER_POINTER (id);
30738       int n = 2;
30739
30740       if (strcmp ("parallel", p) == 0)
30741         if_modifier = OMP_PARALLEL;
30742       else if (strcmp ("task", p) == 0)
30743         if_modifier = OMP_TASK;
30744       else if (strcmp ("taskloop", p) == 0)
30745         if_modifier = OMP_TASKLOOP;
30746       else if (strcmp ("target", p) == 0)
30747         {
30748           if_modifier = OMP_TARGET;
30749           if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30750             {
30751               id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30752               p = IDENTIFIER_POINTER (id);
30753               if (strcmp ("data", p) == 0)
30754                 if_modifier = OMP_TARGET_DATA;
30755               else if (strcmp ("update", p) == 0)
30756                 if_modifier = OMP_TARGET_UPDATE;
30757               else if (strcmp ("enter", p) == 0)
30758                 if_modifier = OMP_TARGET_ENTER_DATA;
30759               else if (strcmp ("exit", p) == 0)
30760                 if_modifier = OMP_TARGET_EXIT_DATA;
30761               if (if_modifier != OMP_TARGET)
30762                 n = 3;
30763               else
30764                 {
30765                   location_t loc
30766                     = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30767                   error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30768                                  "or %<exit%>");
30769                   if_modifier = ERROR_MARK;
30770                 }
30771               if (if_modifier == OMP_TARGET_ENTER_DATA
30772                   || if_modifier == OMP_TARGET_EXIT_DATA)
30773                 {
30774                   if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30775                     {
30776                       id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30777                       p = IDENTIFIER_POINTER (id);
30778                       if (strcmp ("data", p) == 0)
30779                         n = 4;
30780                     }
30781                   if (n != 4)
30782                     {
30783                       location_t loc
30784                         = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30785                       error_at (loc, "expected %<data%>");
30786                       if_modifier = ERROR_MARK;
30787                     }
30788                 }
30789             }
30790         }
30791       if (if_modifier != ERROR_MARK)
30792         {
30793           if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30794             {
30795               while (n-- > 0)
30796                 cp_lexer_consume_token (parser->lexer);
30797             }
30798           else
30799             {
30800               if (n > 2)
30801                 {
30802                   location_t loc
30803                     = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30804                   error_at (loc, "expected %<:%>");
30805                 }
30806               if_modifier = ERROR_MARK;
30807             }
30808         }
30809     }
30810
30811   t = cp_parser_condition (parser);
30812
30813   if (t == error_mark_node
30814       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30815     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30816                                            /*or_comma=*/false,
30817                                            /*consume_paren=*/true);
30818
30819   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30820     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30821       {
30822         if (if_modifier != ERROR_MARK
30823             && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30824           {
30825             const char *p = NULL;
30826             switch (if_modifier)
30827               {
30828               case OMP_PARALLEL: p = "parallel"; break;
30829               case OMP_TASK: p = "task"; break;
30830               case OMP_TASKLOOP: p = "taskloop"; break;
30831               case OMP_TARGET_DATA: p = "target data"; break;
30832               case OMP_TARGET: p = "target"; break;
30833               case OMP_TARGET_UPDATE: p = "target update"; break;
30834               case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30835               case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30836               default: gcc_unreachable ();
30837               }
30838             error_at (location, "too many %<if%> clauses with %qs modifier",
30839                       p);
30840             return list;
30841           }
30842         else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30843           {
30844             if (!is_omp)
30845               error_at (location, "too many %<if%> clauses");
30846             else
30847               error_at (location, "too many %<if%> clauses without modifier");
30848             return list;
30849           }
30850         else if (if_modifier == ERROR_MARK
30851                  || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30852           {
30853             error_at (location, "if any %<if%> clause has modifier, then all "
30854                                 "%<if%> clauses have to use modifier");
30855             return list;
30856           }
30857       }
30858
30859   c = build_omp_clause (location, OMP_CLAUSE_IF);
30860   OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30861   OMP_CLAUSE_IF_EXPR (c) = t;
30862   OMP_CLAUSE_CHAIN (c) = list;
30863
30864   return c;
30865 }
30866
30867 /* OpenMP 3.1:
30868    mergeable */
30869
30870 static tree
30871 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30872                                 tree list, location_t location)
30873 {
30874   tree c;
30875
30876   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30877                              location);
30878
30879   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30880   OMP_CLAUSE_CHAIN (c) = list;
30881   return c;
30882 }
30883
30884 /* OpenMP 2.5:
30885    nowait */
30886
30887 static tree
30888 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30889                              tree list, location_t location)
30890 {
30891   tree c;
30892
30893   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30894
30895   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30896   OMP_CLAUSE_CHAIN (c) = list;
30897   return c;
30898 }
30899
30900 /* OpenMP 2.5:
30901    num_threads ( expression ) */
30902
30903 static tree
30904 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30905                                   location_t location)
30906 {
30907   tree t, c;
30908
30909   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30910     return list;
30911
30912   t = cp_parser_expression (parser);
30913
30914   if (t == error_mark_node
30915       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30916     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30917                                            /*or_comma=*/false,
30918                                            /*consume_paren=*/true);
30919
30920   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30921                              "num_threads", location);
30922
30923   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30924   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30925   OMP_CLAUSE_CHAIN (c) = list;
30926
30927   return c;
30928 }
30929
30930 /* OpenMP 4.5:
30931    num_tasks ( expression ) */
30932
30933 static tree
30934 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30935                                 location_t location)
30936 {
30937   tree t, c;
30938
30939   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30940     return list;
30941
30942   t = cp_parser_expression (parser);
30943
30944   if (t == error_mark_node
30945       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30946     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30947                                            /*or_comma=*/false,
30948                                            /*consume_paren=*/true);
30949
30950   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30951                              "num_tasks", location);
30952
30953   c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30954   OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30955   OMP_CLAUSE_CHAIN (c) = list;
30956
30957   return c;
30958 }
30959
30960 /* OpenMP 4.5:
30961    grainsize ( expression ) */
30962
30963 static tree
30964 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30965                                 location_t location)
30966 {
30967   tree t, c;
30968
30969   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30970     return list;
30971
30972   t = cp_parser_expression (parser);
30973
30974   if (t == error_mark_node
30975       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30976     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30977                                            /*or_comma=*/false,
30978                                            /*consume_paren=*/true);
30979
30980   check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30981                              "grainsize", location);
30982
30983   c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30984   OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30985   OMP_CLAUSE_CHAIN (c) = list;
30986
30987   return c;
30988 }
30989
30990 /* OpenMP 4.5:
30991    priority ( expression ) */
30992
30993 static tree
30994 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30995                                location_t location)
30996 {
30997   tree t, c;
30998
30999   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31000     return list;
31001
31002   t = cp_parser_expression (parser);
31003
31004   if (t == error_mark_node
31005       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31006     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31007                                            /*or_comma=*/false,
31008                                            /*consume_paren=*/true);
31009
31010   check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31011                              "priority", location);
31012
31013   c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31014   OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31015   OMP_CLAUSE_CHAIN (c) = list;
31016
31017   return c;
31018 }
31019
31020 /* OpenMP 4.5:
31021    hint ( expression ) */
31022
31023 static tree
31024 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31025                            location_t location)
31026 {
31027   tree t, c;
31028
31029   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31030     return list;
31031
31032   t = cp_parser_expression (parser);
31033
31034   if (t == error_mark_node
31035       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31036     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31037                                            /*or_comma=*/false,
31038                                            /*consume_paren=*/true);
31039
31040   check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31041
31042   c = build_omp_clause (location, OMP_CLAUSE_HINT);
31043   OMP_CLAUSE_HINT_EXPR (c) = t;
31044   OMP_CLAUSE_CHAIN (c) = list;
31045
31046   return c;
31047 }
31048
31049 /* OpenMP 4.5:
31050    defaultmap ( tofrom : scalar ) */
31051
31052 static tree
31053 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31054                                  location_t location)
31055 {
31056   tree c, id;
31057   const char *p;
31058
31059   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31060     return list;
31061
31062   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31063     {
31064       cp_parser_error (parser, "expected %<tofrom%>");
31065       goto out_err;
31066     }
31067   id = cp_lexer_peek_token (parser->lexer)->u.value;
31068   p = IDENTIFIER_POINTER (id);
31069   if (strcmp (p, "tofrom") != 0)
31070     {
31071       cp_parser_error (parser, "expected %<tofrom%>");
31072       goto out_err;
31073     }
31074   cp_lexer_consume_token (parser->lexer);
31075   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31076     goto out_err;
31077
31078   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31079     {
31080       cp_parser_error (parser, "expected %<scalar%>");
31081       goto out_err;
31082     }
31083   id = cp_lexer_peek_token (parser->lexer)->u.value;
31084   p = IDENTIFIER_POINTER (id);
31085   if (strcmp (p, "scalar") != 0)
31086     {
31087       cp_parser_error (parser, "expected %<scalar%>");
31088       goto out_err;
31089     }
31090   cp_lexer_consume_token (parser->lexer);
31091   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31092     goto out_err;
31093
31094   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31095                              location);
31096
31097   c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31098   OMP_CLAUSE_CHAIN (c) = list;
31099   return c;
31100
31101  out_err:
31102   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31103                                          /*or_comma=*/false,
31104                                          /*consume_paren=*/true);
31105   return list;
31106 }
31107
31108 /* OpenMP 2.5:
31109    ordered
31110
31111    OpenMP 4.5:
31112    ordered ( constant-expression ) */
31113
31114 static tree
31115 cp_parser_omp_clause_ordered (cp_parser *parser,
31116                               tree list, location_t location)
31117 {
31118   tree c, num = NULL_TREE;
31119   HOST_WIDE_INT n;
31120
31121   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31122                              "ordered", location);
31123
31124   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31125     {
31126       cp_lexer_consume_token (parser->lexer);
31127
31128       num = cp_parser_constant_expression (parser);
31129
31130       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31131         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31132                                                /*or_comma=*/false,
31133                                                /*consume_paren=*/true);
31134
31135       if (num == error_mark_node)
31136         return list;
31137       num = fold_non_dependent_expr (num);
31138       if (!tree_fits_shwi_p (num)
31139           || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31140           || (n = tree_to_shwi (num)) <= 0
31141           || (int) n != n)
31142         {
31143           error_at (location,
31144                     "ordered argument needs positive constant integer "
31145                     "expression");
31146           return list;
31147         }
31148     }
31149
31150   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31151   OMP_CLAUSE_ORDERED_EXPR (c) = num;
31152   OMP_CLAUSE_CHAIN (c) = list;
31153   return c;
31154 }
31155
31156 /* OpenMP 2.5:
31157    reduction ( reduction-operator : variable-list )
31158
31159    reduction-operator:
31160      One of: + * - & ^ | && ||
31161
31162    OpenMP 3.1:
31163
31164    reduction-operator:
31165      One of: + * - & ^ | && || min max
31166
31167    OpenMP 4.0:
31168
31169    reduction-operator:
31170      One of: + * - & ^ | && ||
31171      id-expression  */
31172
31173 static tree
31174 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31175 {
31176   enum tree_code code = ERROR_MARK;
31177   tree nlist, c, id = NULL_TREE;
31178
31179   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31180     return list;
31181
31182   switch (cp_lexer_peek_token (parser->lexer)->type)
31183     {
31184     case CPP_PLUS: code = PLUS_EXPR; break;
31185     case CPP_MULT: code = MULT_EXPR; break;
31186     case CPP_MINUS: code = MINUS_EXPR; break;
31187     case CPP_AND: code = BIT_AND_EXPR; break;
31188     case CPP_XOR: code = BIT_XOR_EXPR; break;
31189     case CPP_OR: code = BIT_IOR_EXPR; break;
31190     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31191     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31192     default: break;
31193     }
31194
31195   if (code != ERROR_MARK)
31196     cp_lexer_consume_token (parser->lexer);
31197   else
31198     {
31199       bool saved_colon_corrects_to_scope_p;
31200       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31201       parser->colon_corrects_to_scope_p = false;
31202       id = cp_parser_id_expression (parser, /*template_p=*/false,
31203                                     /*check_dependency_p=*/true,
31204                                     /*template_p=*/NULL,
31205                                     /*declarator_p=*/false,
31206                                     /*optional_p=*/false);
31207       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31208       if (identifier_p (id))
31209         {
31210           const char *p = IDENTIFIER_POINTER (id);
31211
31212           if (strcmp (p, "min") == 0)
31213             code = MIN_EXPR;
31214           else if (strcmp (p, "max") == 0)
31215             code = MAX_EXPR;
31216           else if (id == ansi_opname (PLUS_EXPR))
31217             code = PLUS_EXPR;
31218           else if (id == ansi_opname (MULT_EXPR))
31219             code = MULT_EXPR;
31220           else if (id == ansi_opname (MINUS_EXPR))
31221             code = MINUS_EXPR;
31222           else if (id == ansi_opname (BIT_AND_EXPR))
31223             code = BIT_AND_EXPR;
31224           else if (id == ansi_opname (BIT_IOR_EXPR))
31225             code = BIT_IOR_EXPR;
31226           else if (id == ansi_opname (BIT_XOR_EXPR))
31227             code = BIT_XOR_EXPR;
31228           else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31229             code = TRUTH_ANDIF_EXPR;
31230           else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31231             code = TRUTH_ORIF_EXPR;
31232           id = omp_reduction_id (code, id, NULL_TREE);
31233           tree scope = parser->scope;
31234           if (scope)
31235             id = build_qualified_name (NULL_TREE, scope, id, false);
31236           parser->scope = NULL_TREE;
31237           parser->qualifying_scope = NULL_TREE;
31238           parser->object_scope = NULL_TREE;
31239         }
31240       else
31241         {
31242           error ("invalid reduction-identifier");
31243          resync_fail:
31244           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31245                                                  /*or_comma=*/false,
31246                                                  /*consume_paren=*/true);
31247           return list;
31248         }
31249     }
31250
31251   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31252     goto resync_fail;
31253
31254   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31255                                           NULL);
31256   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31257     {
31258       OMP_CLAUSE_REDUCTION_CODE (c) = code;
31259       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31260     }
31261
31262   return nlist;
31263 }
31264
31265 /* OpenMP 2.5:
31266    schedule ( schedule-kind )
31267    schedule ( schedule-kind , expression )
31268
31269    schedule-kind:
31270      static | dynamic | guided | runtime | auto
31271
31272    OpenMP 4.5:
31273    schedule ( schedule-modifier : schedule-kind )
31274    schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31275
31276    schedule-modifier:
31277      simd
31278      monotonic
31279      nonmonotonic  */
31280
31281 static tree
31282 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31283 {
31284   tree c, t;
31285   int modifiers = 0, nmodifiers = 0;
31286
31287   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31288     return list;
31289
31290   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31291
31292   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31293     {
31294       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31295       const char *p = IDENTIFIER_POINTER (id);
31296       if (strcmp ("simd", p) == 0)
31297         OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31298       else if (strcmp ("monotonic", p) == 0)
31299         modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31300       else if (strcmp ("nonmonotonic", p) == 0)
31301         modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31302       else
31303         break;
31304       cp_lexer_consume_token (parser->lexer);
31305       if (nmodifiers++ == 0
31306           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31307         cp_lexer_consume_token (parser->lexer);
31308       else
31309         {
31310           cp_parser_require (parser, CPP_COLON, RT_COLON);
31311           break;
31312         }
31313     }
31314
31315   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31316     {
31317       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31318       const char *p = IDENTIFIER_POINTER (id);
31319
31320       switch (p[0])
31321         {
31322         case 'd':
31323           if (strcmp ("dynamic", p) != 0)
31324             goto invalid_kind;
31325           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31326           break;
31327
31328         case 'g':
31329           if (strcmp ("guided", p) != 0)
31330             goto invalid_kind;
31331           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31332           break;
31333
31334         case 'r':
31335           if (strcmp ("runtime", p) != 0)
31336             goto invalid_kind;
31337           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31338           break;
31339
31340         default:
31341           goto invalid_kind;
31342         }
31343     }
31344   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31345     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31346   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31347     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31348   else
31349     goto invalid_kind;
31350   cp_lexer_consume_token (parser->lexer);
31351
31352   if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31353                     | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31354       == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31355           | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31356     {
31357       error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31358                           "specified");
31359       modifiers = 0;
31360     }
31361
31362   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31363     {
31364       cp_token *token;
31365       cp_lexer_consume_token (parser->lexer);
31366
31367       token = cp_lexer_peek_token (parser->lexer);
31368       t = cp_parser_assignment_expression (parser);
31369
31370       if (t == error_mark_node)
31371         goto resync_fail;
31372       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31373         error_at (token->location, "schedule %<runtime%> does not take "
31374                   "a %<chunk_size%> parameter");
31375       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31376         error_at (token->location, "schedule %<auto%> does not take "
31377                   "a %<chunk_size%> parameter");
31378       else
31379         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31380
31381       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31382         goto resync_fail;
31383     }
31384   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31385     goto resync_fail;
31386
31387   OMP_CLAUSE_SCHEDULE_KIND (c)
31388     = (enum omp_clause_schedule_kind)
31389       (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31390
31391   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31392   OMP_CLAUSE_CHAIN (c) = list;
31393   return c;
31394
31395  invalid_kind:
31396   cp_parser_error (parser, "invalid schedule kind");
31397  resync_fail:
31398   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31399                                          /*or_comma=*/false,
31400                                          /*consume_paren=*/true);
31401   return list;
31402 }
31403
31404 /* OpenMP 3.0:
31405    untied */
31406
31407 static tree
31408 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31409                              tree list, location_t location)
31410 {
31411   tree c;
31412
31413   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31414
31415   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31416   OMP_CLAUSE_CHAIN (c) = list;
31417   return c;
31418 }
31419
31420 /* OpenMP 4.0:
31421    inbranch
31422    notinbranch */
31423
31424 static tree
31425 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31426                              tree list, location_t location)
31427 {
31428   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31429   tree c = build_omp_clause (location, code);
31430   OMP_CLAUSE_CHAIN (c) = list;
31431   return c;
31432 }
31433
31434 /* OpenMP 4.0:
31435    parallel
31436    for
31437    sections
31438    taskgroup */
31439
31440 static tree
31441 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31442                                  enum omp_clause_code code,
31443                                  tree list, location_t location)
31444 {
31445   tree c = build_omp_clause (location, code);
31446   OMP_CLAUSE_CHAIN (c) = list;
31447   return c;
31448 }
31449
31450 /* OpenMP 4.5:
31451    nogroup */
31452
31453 static tree
31454 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31455                               tree list, location_t location)
31456 {
31457   check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31458   tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31459   OMP_CLAUSE_CHAIN (c) = list;
31460   return c;
31461 }
31462
31463 /* OpenMP 4.5:
31464    simd
31465    threads */
31466
31467 static tree
31468 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31469                                   enum omp_clause_code code,
31470                                   tree list, location_t location)
31471 {
31472   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31473   tree c = build_omp_clause (location, code);
31474   OMP_CLAUSE_CHAIN (c) = list;
31475   return c;
31476 }
31477
31478 /* OpenMP 4.0:
31479    num_teams ( expression ) */
31480
31481 static tree
31482 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31483                                 location_t location)
31484 {
31485   tree t, c;
31486
31487   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31488     return list;
31489
31490   t = cp_parser_expression (parser);
31491
31492   if (t == error_mark_node
31493       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31494     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31495                                            /*or_comma=*/false,
31496                                            /*consume_paren=*/true);
31497
31498   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31499                              "num_teams", location);
31500
31501   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31502   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31503   OMP_CLAUSE_CHAIN (c) = list;
31504
31505   return c;
31506 }
31507
31508 /* OpenMP 4.0:
31509    thread_limit ( expression ) */
31510
31511 static tree
31512 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31513                                    location_t location)
31514 {
31515   tree t, c;
31516
31517   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31518     return list;
31519
31520   t = cp_parser_expression (parser);
31521
31522   if (t == error_mark_node
31523       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31524     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31525                                            /*or_comma=*/false,
31526                                            /*consume_paren=*/true);
31527
31528   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31529                              "thread_limit", location);
31530
31531   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31532   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31533   OMP_CLAUSE_CHAIN (c) = list;
31534
31535   return c;
31536 }
31537
31538 /* OpenMP 4.0:
31539    aligned ( variable-list )
31540    aligned ( variable-list : constant-expression )  */
31541
31542 static tree
31543 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31544 {
31545   tree nlist, c, alignment = NULL_TREE;
31546   bool colon;
31547
31548   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31549     return list;
31550
31551   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31552                                           &colon);
31553
31554   if (colon)
31555     {
31556       alignment = cp_parser_constant_expression (parser);
31557
31558       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31559         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31560                                                /*or_comma=*/false,
31561                                                /*consume_paren=*/true);
31562
31563       if (alignment == error_mark_node)
31564         alignment = NULL_TREE;
31565     }
31566
31567   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31568     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31569
31570   return nlist;
31571 }
31572
31573 /* OpenMP 4.0:
31574    linear ( variable-list )
31575    linear ( variable-list : expression )
31576
31577    OpenMP 4.5:
31578    linear ( modifier ( variable-list ) )
31579    linear ( modifier ( variable-list ) : expression ) */
31580
31581 static tree
31582 cp_parser_omp_clause_linear (cp_parser *parser, tree list, 
31583                              bool is_cilk_simd_fn, bool declare_simd)
31584 {
31585   tree nlist, c, step = integer_one_node;
31586   bool colon;
31587   enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31588
31589   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31590     return list;
31591
31592   if (!is_cilk_simd_fn
31593       && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31594     {
31595       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31596       const char *p = IDENTIFIER_POINTER (id);
31597
31598       if (strcmp ("ref", p) == 0)
31599         kind = OMP_CLAUSE_LINEAR_REF;
31600       else if (strcmp ("val", p) == 0)
31601         kind = OMP_CLAUSE_LINEAR_VAL;
31602       else if (strcmp ("uval", p) == 0)
31603         kind = OMP_CLAUSE_LINEAR_UVAL;
31604       if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31605         cp_lexer_consume_token (parser->lexer);
31606       else
31607         kind = OMP_CLAUSE_LINEAR_DEFAULT;
31608     }
31609
31610   if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31611     nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31612                                             &colon);
31613   else
31614     {
31615       nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31616       colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31617       if (colon)
31618         cp_parser_require (parser, CPP_COLON, RT_COLON);
31619       else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31620         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31621                                                /*or_comma=*/false,
31622                                                /*consume_paren=*/true);
31623     }
31624
31625   if (colon)
31626     {
31627       step = NULL_TREE;
31628       if (declare_simd
31629           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31630           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31631         {
31632           cp_token *token = cp_lexer_peek_token (parser->lexer);
31633           cp_parser_parse_tentatively (parser);
31634           step = cp_parser_id_expression (parser, /*template_p=*/false,
31635                                           /*check_dependency_p=*/true,
31636                                           /*template_p=*/NULL,
31637                                           /*declarator_p=*/false,
31638                                           /*optional_p=*/false);
31639           if (step != error_mark_node)
31640             step = cp_parser_lookup_name_simple (parser, step, token->location);
31641           if (step == error_mark_node)
31642             {
31643               step = NULL_TREE;
31644               cp_parser_abort_tentative_parse (parser);
31645             }
31646           else if (!cp_parser_parse_definitely (parser))
31647             step = NULL_TREE;
31648         }
31649       if (!step)
31650         step = cp_parser_expression (parser);
31651
31652       if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31653         {
31654           sorry ("using parameters for %<linear%> step is not supported yet");
31655           step = integer_one_node;
31656         }
31657       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31658         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31659                                                /*or_comma=*/false,
31660                                                /*consume_paren=*/true);
31661
31662       if (step == error_mark_node)
31663         return list;
31664     }
31665
31666   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31667     {
31668       OMP_CLAUSE_LINEAR_STEP (c) = step;
31669       OMP_CLAUSE_LINEAR_KIND (c) = kind;
31670     }
31671
31672   return nlist;
31673 }
31674
31675 /* OpenMP 4.0:
31676    safelen ( constant-expression )  */
31677
31678 static tree
31679 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31680                               location_t location)
31681 {
31682   tree t, c;
31683
31684   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31685     return list;
31686
31687   t = cp_parser_constant_expression (parser);
31688
31689   if (t == error_mark_node
31690       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31691     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31692                                            /*or_comma=*/false,
31693                                            /*consume_paren=*/true);
31694
31695   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31696
31697   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31698   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31699   OMP_CLAUSE_CHAIN (c) = list;
31700
31701   return c;
31702 }
31703
31704 /* OpenMP 4.0:
31705    simdlen ( constant-expression )  */
31706
31707 static tree
31708 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31709                               location_t location)
31710 {
31711   tree t, c;
31712
31713   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31714     return list;
31715
31716   t = cp_parser_constant_expression (parser);
31717
31718   if (t == error_mark_node
31719       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31720     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31721                                            /*or_comma=*/false,
31722                                            /*consume_paren=*/true);
31723
31724   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31725
31726   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31727   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31728   OMP_CLAUSE_CHAIN (c) = list;
31729
31730   return c;
31731 }
31732
31733 /* OpenMP 4.5:
31734    vec:
31735      identifier [+/- integer]
31736      vec , identifier [+/- integer]
31737 */
31738
31739 static tree
31740 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31741                                   tree list)
31742 {
31743   tree vec = NULL;
31744
31745   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31746     {
31747       cp_parser_error (parser, "expected identifier");
31748       return list;
31749     }
31750
31751   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31752     {
31753       location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31754       tree t, identifier = cp_parser_identifier (parser);
31755       tree addend = NULL;
31756
31757       if (identifier == error_mark_node)
31758         t = error_mark_node;
31759       else
31760         {
31761           t = cp_parser_lookup_name_simple
31762                 (parser, identifier,
31763                  cp_lexer_peek_token (parser->lexer)->location);
31764           if (t == error_mark_node)
31765             cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31766                                          id_loc);
31767         }
31768
31769       bool neg = false;
31770       if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31771         neg = true;
31772       else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31773         {
31774           addend = integer_zero_node;
31775           goto add_to_vector;
31776         }
31777       cp_lexer_consume_token (parser->lexer);
31778
31779       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31780         {
31781           cp_parser_error (parser, "expected integer");
31782           return list;
31783         }
31784
31785       addend = cp_lexer_peek_token (parser->lexer)->u.value;
31786       if (TREE_CODE (addend) != INTEGER_CST)
31787         {
31788           cp_parser_error (parser, "expected integer");
31789           return list;
31790         }
31791       cp_lexer_consume_token (parser->lexer);
31792
31793     add_to_vector:
31794       if (t != error_mark_node)
31795         {
31796           vec = tree_cons (addend, t, vec);
31797           if (neg)
31798             OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31799         }
31800
31801       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31802         break;
31803
31804       cp_lexer_consume_token (parser->lexer);
31805     }
31806
31807   if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31808     {
31809       tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31810       OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31811       OMP_CLAUSE_DECL (u) = nreverse (vec);
31812       OMP_CLAUSE_CHAIN (u) = list;
31813       return u;
31814     }
31815   return list;
31816 }
31817
31818 /* OpenMP 4.0:
31819    depend ( depend-kind : variable-list )
31820
31821    depend-kind:
31822      in | out | inout
31823
31824    OpenMP 4.5:
31825    depend ( source )
31826
31827    depend ( sink : vec ) */
31828
31829 static tree
31830 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31831 {
31832   tree nlist, c;
31833   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31834
31835   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31836     return list;
31837
31838   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31839     {
31840       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31841       const char *p = IDENTIFIER_POINTER (id);
31842
31843       if (strcmp ("in", p) == 0)
31844         kind = OMP_CLAUSE_DEPEND_IN;
31845       else if (strcmp ("inout", p) == 0)
31846         kind = OMP_CLAUSE_DEPEND_INOUT;
31847       else if (strcmp ("out", p) == 0)
31848         kind = OMP_CLAUSE_DEPEND_OUT;
31849       else if (strcmp ("source", p) == 0)
31850         kind = OMP_CLAUSE_DEPEND_SOURCE;
31851       else if (strcmp ("sink", p) == 0)
31852         kind = OMP_CLAUSE_DEPEND_SINK;
31853       else
31854         goto invalid_kind;
31855     }
31856   else
31857     goto invalid_kind;
31858
31859   cp_lexer_consume_token (parser->lexer);
31860
31861   if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31862     {
31863       c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31864       OMP_CLAUSE_DEPEND_KIND (c) = kind;
31865       OMP_CLAUSE_DECL (c) = NULL_TREE;
31866       OMP_CLAUSE_CHAIN (c) = list;
31867       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31868         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31869                                                /*or_comma=*/false,
31870                                                /*consume_paren=*/true);
31871       return c;
31872     }
31873
31874   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31875     goto resync_fail;
31876
31877   if (kind == OMP_CLAUSE_DEPEND_SINK)
31878     nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31879   else
31880     {
31881       nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31882                                               list, NULL);
31883
31884       for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31885         OMP_CLAUSE_DEPEND_KIND (c) = kind;
31886     }
31887   return nlist;
31888
31889  invalid_kind:
31890   cp_parser_error (parser, "invalid depend kind");
31891  resync_fail:
31892   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31893                                          /*or_comma=*/false,
31894                                          /*consume_paren=*/true);
31895   return list;
31896 }
31897
31898 /* OpenMP 4.0:
31899    map ( map-kind : variable-list )
31900    map ( variable-list )
31901
31902    map-kind:
31903      alloc | to | from | tofrom
31904
31905    OpenMP 4.5:
31906    map-kind:
31907      alloc | to | from | tofrom | release | delete
31908
31909    map ( always [,] map-kind: variable-list ) */
31910
31911 static tree
31912 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31913 {
31914   tree nlist, c;
31915   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31916   bool always = false;
31917
31918   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31919     return list;
31920
31921   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31922     {
31923       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31924       const char *p = IDENTIFIER_POINTER (id);
31925
31926       if (strcmp ("always", p) == 0)
31927         {
31928           int nth = 2;
31929           if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31930             nth++;
31931           if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31932                || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31933                    == RID_DELETE))
31934               && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31935                   == CPP_COLON))
31936             {
31937               always = true;
31938               cp_lexer_consume_token (parser->lexer);
31939               if (nth == 3)
31940                 cp_lexer_consume_token (parser->lexer);
31941             }
31942         }
31943     }
31944
31945   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31946       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31947     {
31948       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31949       const char *p = IDENTIFIER_POINTER (id);
31950
31951       if (strcmp ("alloc", p) == 0)
31952         kind = GOMP_MAP_ALLOC;
31953       else if (strcmp ("to", p) == 0)
31954         kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31955       else if (strcmp ("from", p) == 0)
31956         kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31957       else if (strcmp ("tofrom", p) == 0)
31958         kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31959       else if (strcmp ("release", p) == 0)
31960         kind = GOMP_MAP_RELEASE;
31961       else
31962         {
31963           cp_parser_error (parser, "invalid map kind");
31964           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31965                                                  /*or_comma=*/false,
31966                                                  /*consume_paren=*/true);
31967           return list;
31968         }
31969       cp_lexer_consume_token (parser->lexer);
31970       cp_lexer_consume_token (parser->lexer);
31971     }
31972   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31973            && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31974     {
31975       kind = GOMP_MAP_DELETE;
31976       cp_lexer_consume_token (parser->lexer);
31977       cp_lexer_consume_token (parser->lexer);
31978     }
31979
31980   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31981                                           NULL);
31982
31983   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31984     OMP_CLAUSE_SET_MAP_KIND (c, kind);
31985
31986   return nlist;
31987 }
31988
31989 /* OpenMP 4.0:
31990    device ( expression ) */
31991
31992 static tree
31993 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31994                              location_t location)
31995 {
31996   tree t, c;
31997
31998   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31999     return list;
32000
32001   t = cp_parser_expression (parser);
32002
32003   if (t == error_mark_node
32004       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32005     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32006                                            /*or_comma=*/false,
32007                                            /*consume_paren=*/true);
32008
32009   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32010                              "device", location);
32011
32012   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32013   OMP_CLAUSE_DEVICE_ID (c) = t;
32014   OMP_CLAUSE_CHAIN (c) = list;
32015
32016   return c;
32017 }
32018
32019 /* OpenMP 4.0:
32020    dist_schedule ( static )
32021    dist_schedule ( static , expression )  */
32022
32023 static tree
32024 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32025                                     location_t location)
32026 {
32027   tree c, t;
32028
32029   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32030     return list;
32031
32032   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32033
32034   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32035     goto invalid_kind;
32036   cp_lexer_consume_token (parser->lexer);
32037
32038   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32039     {
32040       cp_lexer_consume_token (parser->lexer);
32041
32042       t = cp_parser_assignment_expression (parser);
32043
32044       if (t == error_mark_node)
32045         goto resync_fail;
32046       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32047
32048       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32049         goto resync_fail;
32050     }
32051   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32052     goto resync_fail;
32053
32054   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32055                              location);
32056   OMP_CLAUSE_CHAIN (c) = list;
32057   return c;
32058
32059  invalid_kind:
32060   cp_parser_error (parser, "invalid dist_schedule kind");
32061  resync_fail:
32062   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32063                                          /*or_comma=*/false,
32064                                          /*consume_paren=*/true);
32065   return list;
32066 }
32067
32068 /* OpenMP 4.0:
32069    proc_bind ( proc-bind-kind )
32070
32071    proc-bind-kind:
32072      master | close | spread  */
32073
32074 static tree
32075 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32076                                 location_t location)
32077 {
32078   tree c;
32079   enum omp_clause_proc_bind_kind kind;
32080
32081   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32082     return list;
32083
32084   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32085     {
32086       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32087       const char *p = IDENTIFIER_POINTER (id);
32088
32089       if (strcmp ("master", p) == 0)
32090         kind = OMP_CLAUSE_PROC_BIND_MASTER;
32091       else if (strcmp ("close", p) == 0)
32092         kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32093       else if (strcmp ("spread", p) == 0)
32094         kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32095       else
32096         goto invalid_kind;
32097     }
32098   else
32099     goto invalid_kind;
32100
32101   cp_lexer_consume_token (parser->lexer);
32102   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32103     goto resync_fail;
32104
32105   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32106   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32107                              location);
32108   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32109   OMP_CLAUSE_CHAIN (c) = list;
32110   return c;
32111
32112  invalid_kind:
32113   cp_parser_error (parser, "invalid depend kind");
32114  resync_fail:
32115   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32116                                          /*or_comma=*/false,
32117                                          /*consume_paren=*/true);
32118   return list;
32119 }
32120
32121 /* OpenACC:
32122    async [( int-expr )] */
32123
32124 static tree
32125 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32126 {
32127   tree c, t;
32128   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32129
32130   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32131
32132   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32133     {
32134       cp_lexer_consume_token (parser->lexer);
32135
32136       t = cp_parser_expression (parser);
32137       if (t == error_mark_node
32138           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32139         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32140                                                 /*or_comma=*/false,
32141                                                 /*consume_paren=*/true);
32142     }
32143
32144   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32145
32146   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32147   OMP_CLAUSE_ASYNC_EXPR (c) = t;
32148   OMP_CLAUSE_CHAIN (c) = list;
32149   list = c;
32150
32151   return list;
32152 }
32153
32154 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
32155    is a bitmask in MASK.  Return the list of clauses found.  */
32156
32157 static tree
32158 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32159                            const char *where, cp_token *pragma_tok,
32160                            bool finish_p = true)
32161 {
32162   tree clauses = NULL;
32163   bool first = true;
32164
32165   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32166     {
32167       location_t here;
32168       pragma_omp_clause c_kind;
32169       omp_clause_code code;
32170       const char *c_name;
32171       tree prev = clauses;
32172
32173       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32174         cp_lexer_consume_token (parser->lexer);
32175
32176       here = cp_lexer_peek_token (parser->lexer)->location;
32177       c_kind = cp_parser_omp_clause_name (parser);
32178
32179       switch (c_kind)
32180         {
32181         case PRAGMA_OACC_CLAUSE_ASYNC:
32182           clauses = cp_parser_oacc_clause_async (parser, clauses);
32183           c_name = "async";
32184           break;
32185         case PRAGMA_OACC_CLAUSE_AUTO:
32186           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32187                                                  clauses, here);
32188           c_name = "auto";
32189           break;
32190         case PRAGMA_OACC_CLAUSE_COLLAPSE:
32191           clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32192           c_name = "collapse";
32193           break;
32194         case PRAGMA_OACC_CLAUSE_COPY:
32195           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32196           c_name = "copy";
32197           break;
32198         case PRAGMA_OACC_CLAUSE_COPYIN:
32199           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32200           c_name = "copyin";
32201           break;
32202         case PRAGMA_OACC_CLAUSE_COPYOUT:
32203           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32204           c_name = "copyout";
32205           break;
32206         case PRAGMA_OACC_CLAUSE_CREATE:
32207           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32208           c_name = "create";
32209           break;
32210         case PRAGMA_OACC_CLAUSE_DELETE:
32211           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32212           c_name = "delete";
32213           break;
32214         case PRAGMA_OMP_CLAUSE_DEFAULT:
32215           clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32216           c_name = "default";
32217           break;
32218         case PRAGMA_OACC_CLAUSE_DEVICE:
32219           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32220           c_name = "device";
32221           break;
32222         case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32223           clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32224           c_name = "deviceptr";
32225           break;
32226         case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32227           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32228           c_name = "device_resident";
32229           break;
32230         case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32231           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32232                                             clauses);
32233           c_name = "firstprivate";
32234           break;
32235         case PRAGMA_OACC_CLAUSE_GANG:
32236           c_name = "gang";
32237           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32238                                                  c_name, clauses);
32239           break;
32240         case PRAGMA_OACC_CLAUSE_HOST:
32241           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32242           c_name = "host";
32243           break;
32244         case PRAGMA_OACC_CLAUSE_IF:
32245           clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32246           c_name = "if";
32247           break;
32248         case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32249           clauses = cp_parser_oacc_simple_clause (parser,
32250                                                   OMP_CLAUSE_INDEPENDENT,
32251                                                   clauses, here);
32252           c_name = "independent";
32253           break;
32254         case PRAGMA_OACC_CLAUSE_LINK:
32255           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32256           c_name = "link";
32257           break;
32258         case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32259           code = OMP_CLAUSE_NUM_GANGS;
32260           c_name = "num_gangs";
32261           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32262                                                       clauses);
32263           break;
32264         case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32265           c_name = "num_workers";
32266           code = OMP_CLAUSE_NUM_WORKERS;
32267           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32268                                                       clauses);
32269           break;
32270         case PRAGMA_OACC_CLAUSE_PRESENT:
32271           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32272           c_name = "present";
32273           break;
32274         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32275           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32276           c_name = "present_or_copy";
32277           break;
32278         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32279           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32280           c_name = "present_or_copyin";
32281           break;
32282         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32283           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32284           c_name = "present_or_copyout";
32285           break;
32286         case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32287           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32288           c_name = "present_or_create";
32289           break;
32290         case PRAGMA_OACC_CLAUSE_PRIVATE:
32291           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32292                                             clauses);
32293           c_name = "private";
32294           break;
32295         case PRAGMA_OACC_CLAUSE_REDUCTION:
32296           clauses = cp_parser_omp_clause_reduction (parser, clauses);
32297           c_name = "reduction";
32298           break;
32299         case PRAGMA_OACC_CLAUSE_SELF:
32300           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32301           c_name = "self";
32302           break;
32303         case PRAGMA_OACC_CLAUSE_SEQ:
32304           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32305                                                  clauses, here);
32306           c_name = "seq";
32307           break;
32308         case PRAGMA_OACC_CLAUSE_TILE:
32309           clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32310           c_name = "tile";
32311           break;
32312         case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32313           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32314                                             clauses);
32315           c_name = "use_device";
32316           break;
32317         case PRAGMA_OACC_CLAUSE_VECTOR:
32318           c_name = "vector";
32319           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32320                                                  c_name, clauses);
32321           break;
32322         case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32323           c_name = "vector_length";
32324           code = OMP_CLAUSE_VECTOR_LENGTH;
32325           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32326                                                       clauses);
32327           break;
32328         case PRAGMA_OACC_CLAUSE_WAIT:
32329           clauses = cp_parser_oacc_clause_wait (parser, clauses);
32330           c_name = "wait";
32331           break;
32332         case PRAGMA_OACC_CLAUSE_WORKER:
32333           c_name = "worker";
32334           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32335                                                  c_name, clauses);
32336           break;
32337         default:
32338           cp_parser_error (parser, "expected %<#pragma acc%> clause");
32339           goto saw_error;
32340         }
32341
32342       first = false;
32343
32344       if (((mask >> c_kind) & 1) == 0)
32345         {
32346           /* Remove the invalid clause(s) from the list to avoid
32347              confusing the rest of the compiler.  */
32348           clauses = prev;
32349           error_at (here, "%qs is not valid for %qs", c_name, where);
32350         }
32351     }
32352
32353  saw_error:
32354   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32355
32356   if (finish_p)
32357     return finish_omp_clauses (clauses, false);
32358
32359   return clauses;
32360 }
32361
32362 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
32363    is a bitmask in MASK.  Return the list of clauses found; the result
32364    of clause default goes in *pdefault.  */
32365
32366 static tree
32367 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32368                            const char *where, cp_token *pragma_tok,
32369                            bool finish_p = true)
32370 {
32371   tree clauses = NULL;
32372   bool first = true;
32373   cp_token *token = NULL;
32374
32375   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32376     {
32377       pragma_omp_clause c_kind;
32378       const char *c_name;
32379       tree prev = clauses;
32380
32381       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32382         cp_lexer_consume_token (parser->lexer);
32383
32384       token = cp_lexer_peek_token (parser->lexer);
32385       c_kind = cp_parser_omp_clause_name (parser);
32386
32387       switch (c_kind)
32388         {
32389         case PRAGMA_OMP_CLAUSE_COLLAPSE:
32390           clauses = cp_parser_omp_clause_collapse (parser, clauses,
32391                                                    token->location);
32392           c_name = "collapse";
32393           break;
32394         case PRAGMA_OMP_CLAUSE_COPYIN:
32395           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32396           c_name = "copyin";
32397           break;
32398         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32399           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32400                                             clauses);
32401           c_name = "copyprivate";
32402           break;
32403         case PRAGMA_OMP_CLAUSE_DEFAULT:
32404           clauses = cp_parser_omp_clause_default (parser, clauses,
32405                                                   token->location, false);
32406           c_name = "default";
32407           break;
32408         case PRAGMA_OMP_CLAUSE_FINAL:
32409           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32410           c_name = "final";
32411           break;
32412         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32413           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32414                                             clauses);
32415           c_name = "firstprivate";
32416           break;
32417         case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32418           clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32419                                                     token->location);
32420           c_name = "grainsize";
32421           break;
32422         case PRAGMA_OMP_CLAUSE_HINT:
32423           clauses = cp_parser_omp_clause_hint (parser, clauses,
32424                                                token->location);
32425           c_name = "hint";
32426           break;
32427         case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32428           clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32429                                                      token->location);
32430           c_name = "defaultmap";
32431           break;
32432         case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32433           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32434                                             clauses);
32435           c_name = "use_device_ptr";
32436           break;
32437         case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32438           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32439                                             clauses);
32440           c_name = "is_device_ptr";
32441           break;
32442         case PRAGMA_OMP_CLAUSE_IF:
32443           clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32444                                              true);
32445           c_name = "if";
32446           break;
32447         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32448           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32449                                             clauses);
32450           c_name = "lastprivate";
32451           break;
32452         case PRAGMA_OMP_CLAUSE_MERGEABLE:
32453           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32454                                                     token->location);
32455           c_name = "mergeable";
32456           break;
32457         case PRAGMA_OMP_CLAUSE_NOWAIT:
32458           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32459           c_name = "nowait";
32460           break;
32461         case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32462           clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32463                                                     token->location);
32464           c_name = "num_tasks";
32465           break;
32466         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32467           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32468                                                       token->location);
32469           c_name = "num_threads";
32470           break;
32471         case PRAGMA_OMP_CLAUSE_ORDERED:
32472           clauses = cp_parser_omp_clause_ordered (parser, clauses,
32473                                                   token->location);
32474           c_name = "ordered";
32475           break;
32476         case PRAGMA_OMP_CLAUSE_PRIORITY:
32477           clauses = cp_parser_omp_clause_priority (parser, clauses,
32478                                                    token->location);
32479           c_name = "priority";
32480           break;
32481         case PRAGMA_OMP_CLAUSE_PRIVATE:
32482           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32483                                             clauses);
32484           c_name = "private";
32485           break;
32486         case PRAGMA_OMP_CLAUSE_REDUCTION:
32487           clauses = cp_parser_omp_clause_reduction (parser, clauses);
32488           c_name = "reduction";
32489           break;
32490         case PRAGMA_OMP_CLAUSE_SCHEDULE:
32491           clauses = cp_parser_omp_clause_schedule (parser, clauses,
32492                                                    token->location);
32493           c_name = "schedule";
32494           break;
32495         case PRAGMA_OMP_CLAUSE_SHARED:
32496           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32497                                             clauses);
32498           c_name = "shared";
32499           break;
32500         case PRAGMA_OMP_CLAUSE_UNTIED:
32501           clauses = cp_parser_omp_clause_untied (parser, clauses,
32502                                                  token->location);
32503           c_name = "untied";
32504           break;
32505         case PRAGMA_OMP_CLAUSE_INBRANCH:
32506         case PRAGMA_CILK_CLAUSE_MASK:
32507           clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32508                                                  clauses, token->location);
32509           c_name = "inbranch";
32510           break;
32511         case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32512         case PRAGMA_CILK_CLAUSE_NOMASK:
32513           clauses = cp_parser_omp_clause_branch (parser,
32514                                                  OMP_CLAUSE_NOTINBRANCH,
32515                                                  clauses, token->location);
32516           c_name = "notinbranch";
32517           break;
32518         case PRAGMA_OMP_CLAUSE_PARALLEL:
32519           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32520                                                      clauses, token->location);
32521           c_name = "parallel";
32522           if (!first)
32523             {
32524              clause_not_first:
32525               error_at (token->location, "%qs must be the first clause of %qs",
32526                         c_name, where);
32527               clauses = prev;
32528             }
32529           break;
32530         case PRAGMA_OMP_CLAUSE_FOR:
32531           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32532                                                      clauses, token->location);
32533           c_name = "for";
32534           if (!first)
32535             goto clause_not_first;
32536           break;
32537         case PRAGMA_OMP_CLAUSE_SECTIONS:
32538           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32539                                                      clauses, token->location);
32540           c_name = "sections";
32541           if (!first)
32542             goto clause_not_first;
32543           break;
32544         case PRAGMA_OMP_CLAUSE_TASKGROUP:
32545           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32546                                                      clauses, token->location);
32547           c_name = "taskgroup";
32548           if (!first)
32549             goto clause_not_first;
32550           break;
32551         case PRAGMA_OMP_CLAUSE_LINK:
32552           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32553           c_name = "to";
32554           break;
32555         case PRAGMA_OMP_CLAUSE_TO:
32556           if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32557             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32558                                               clauses);
32559           else
32560             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32561           c_name = "to";
32562           break;
32563         case PRAGMA_OMP_CLAUSE_FROM:
32564           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32565           c_name = "from";
32566           break;
32567         case PRAGMA_OMP_CLAUSE_UNIFORM:
32568           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32569                                             clauses);
32570           c_name = "uniform";
32571           break;
32572         case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32573           clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32574                                                     token->location);
32575           c_name = "num_teams";
32576           break;
32577         case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32578           clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32579                                                        token->location);
32580           c_name = "thread_limit";
32581           break;
32582         case PRAGMA_OMP_CLAUSE_ALIGNED:
32583           clauses = cp_parser_omp_clause_aligned (parser, clauses);
32584           c_name = "aligned";
32585           break;
32586         case PRAGMA_OMP_CLAUSE_LINEAR:
32587           {
32588             bool cilk_simd_fn = false, declare_simd = false;
32589             if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32590               cilk_simd_fn = true;
32591             else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32592               declare_simd = true;
32593             clauses = cp_parser_omp_clause_linear (parser, clauses,
32594                                                    cilk_simd_fn, declare_simd);
32595           }
32596           c_name = "linear";
32597           break;
32598         case PRAGMA_OMP_CLAUSE_DEPEND:
32599           clauses = cp_parser_omp_clause_depend (parser, clauses,
32600                                                  token->location);
32601           c_name = "depend";
32602           break;
32603         case PRAGMA_OMP_CLAUSE_MAP:
32604           clauses = cp_parser_omp_clause_map (parser, clauses);
32605           c_name = "map";
32606           break;
32607         case PRAGMA_OMP_CLAUSE_DEVICE:
32608           clauses = cp_parser_omp_clause_device (parser, clauses,
32609                                                  token->location);
32610           c_name = "device";
32611           break;
32612         case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32613           clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32614                                                         token->location);
32615           c_name = "dist_schedule";
32616           break;
32617         case PRAGMA_OMP_CLAUSE_PROC_BIND:
32618           clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32619                                                     token->location);
32620           c_name = "proc_bind";
32621           break;
32622         case PRAGMA_OMP_CLAUSE_SAFELEN:
32623           clauses = cp_parser_omp_clause_safelen (parser, clauses,
32624                                                   token->location);
32625           c_name = "safelen";
32626           break;
32627         case PRAGMA_OMP_CLAUSE_SIMDLEN:
32628           clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32629                                                   token->location);
32630           c_name = "simdlen";
32631           break;
32632         case PRAGMA_OMP_CLAUSE_NOGROUP:
32633           clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32634                                                   token->location);
32635           c_name = "nogroup";
32636           break;
32637         case PRAGMA_OMP_CLAUSE_THREADS:
32638           clauses
32639             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32640                                                 clauses, token->location);
32641           c_name = "threads";
32642           break;
32643         case PRAGMA_OMP_CLAUSE_SIMD:
32644           clauses
32645             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32646                                                 clauses, token->location);
32647           c_name = "simd";
32648           break;
32649         case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32650           clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32651           c_name = "simdlen";
32652           break;
32653         default:
32654           cp_parser_error (parser, "expected %<#pragma omp%> clause");
32655           goto saw_error;
32656         }
32657
32658       first = false;
32659
32660       if (((mask >> c_kind) & 1) == 0)
32661         {
32662           /* Remove the invalid clause(s) from the list to avoid
32663              confusing the rest of the compiler.  */
32664           clauses = prev;
32665           error_at (token->location, "%qs is not valid for %qs", c_name, where);
32666         }
32667     }
32668  saw_error:
32669   /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32670      no reason to skip to the end.  */
32671   if (!(flag_cilkplus && pragma_tok == NULL))
32672     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32673   if (finish_p)
32674     {
32675       if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32676         return finish_omp_clauses (clauses, false, true);
32677       else
32678         return finish_omp_clauses (clauses, true);
32679     }
32680   return clauses;
32681 }
32682
32683 /* OpenMP 2.5:
32684    structured-block:
32685      statement
32686
32687    In practice, we're also interested in adding the statement to an
32688    outer node.  So it is convenient if we work around the fact that
32689    cp_parser_statement calls add_stmt.  */
32690
32691 static unsigned
32692 cp_parser_begin_omp_structured_block (cp_parser *parser)
32693 {
32694   unsigned save = parser->in_statement;
32695
32696   /* Only move the values to IN_OMP_BLOCK if they weren't false.
32697      This preserves the "not within loop or switch" style error messages
32698      for nonsense cases like
32699         void foo() {
32700         #pragma omp single
32701           break;
32702         }
32703   */
32704   if (parser->in_statement)
32705     parser->in_statement = IN_OMP_BLOCK;
32706
32707   return save;
32708 }
32709
32710 static void
32711 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32712 {
32713   parser->in_statement = save;
32714 }
32715
32716 static tree
32717 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32718 {
32719   tree stmt = begin_omp_structured_block ();
32720   unsigned int save = cp_parser_begin_omp_structured_block (parser);
32721
32722   cp_parser_statement (parser, NULL_TREE, false, if_p);
32723
32724   cp_parser_end_omp_structured_block (parser, save);
32725   return finish_omp_structured_block (stmt);
32726 }
32727
32728 /* OpenMP 2.5:
32729    # pragma omp atomic new-line
32730      expression-stmt
32731
32732    expression-stmt:
32733      x binop= expr | x++ | ++x | x-- | --x
32734    binop:
32735      +, *, -, /, &, ^, |, <<, >>
32736
32737   where x is an lvalue expression with scalar type.
32738
32739    OpenMP 3.1:
32740    # pragma omp atomic new-line
32741      update-stmt
32742
32743    # pragma omp atomic read new-line
32744      read-stmt
32745
32746    # pragma omp atomic write new-line
32747      write-stmt
32748
32749    # pragma omp atomic update new-line
32750      update-stmt
32751
32752    # pragma omp atomic capture new-line
32753      capture-stmt
32754
32755    # pragma omp atomic capture new-line
32756      capture-block
32757
32758    read-stmt:
32759      v = x
32760    write-stmt:
32761      x = expr
32762    update-stmt:
32763      expression-stmt | x = x binop expr
32764    capture-stmt:
32765      v = expression-stmt
32766    capture-block:
32767      { v = x; update-stmt; } | { update-stmt; v = x; }
32768
32769    OpenMP 4.0:
32770    update-stmt:
32771      expression-stmt | x = x binop expr | x = expr binop x
32772    capture-stmt:
32773      v = update-stmt
32774    capture-block:
32775      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32776
32777   where x and v are lvalue expressions with scalar type.  */
32778
32779 static void
32780 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32781 {
32782   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32783   tree rhs1 = NULL_TREE, orig_lhs;
32784   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32785   bool structured_block = false;
32786   bool seq_cst = false;
32787
32788   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32789     {
32790       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32791       const char *p = IDENTIFIER_POINTER (id);
32792
32793       if (!strcmp (p, "seq_cst"))
32794         {
32795           seq_cst = true;
32796           cp_lexer_consume_token (parser->lexer);
32797           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32798               && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32799             cp_lexer_consume_token (parser->lexer);
32800         }
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, "read"))
32808         code = OMP_ATOMIC_READ;
32809       else if (!strcmp (p, "write"))
32810         code = NOP_EXPR;
32811       else if (!strcmp (p, "update"))
32812         code = OMP_ATOMIC;
32813       else if (!strcmp (p, "capture"))
32814         code = OMP_ATOMIC_CAPTURE_NEW;
32815       else
32816         p = NULL;
32817       if (p)
32818         cp_lexer_consume_token (parser->lexer);
32819     }
32820   if (!seq_cst)
32821     {
32822       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32823           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32824         cp_lexer_consume_token (parser->lexer);
32825
32826       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32827         {
32828           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32829           const char *p = IDENTIFIER_POINTER (id);
32830
32831           if (!strcmp (p, "seq_cst"))
32832             {
32833               seq_cst = true;
32834               cp_lexer_consume_token (parser->lexer);
32835             }
32836         }
32837     }
32838   cp_parser_require_pragma_eol (parser, pragma_tok);
32839
32840   switch (code)
32841     {
32842     case OMP_ATOMIC_READ:
32843     case NOP_EXPR: /* atomic write */
32844       v = cp_parser_unary_expression (parser);
32845       if (v == error_mark_node)
32846         goto saw_error;
32847       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32848         goto saw_error;
32849       if (code == NOP_EXPR)
32850         lhs = cp_parser_expression (parser);
32851       else
32852         lhs = cp_parser_unary_expression (parser);
32853       if (lhs == error_mark_node)
32854         goto saw_error;
32855       if (code == NOP_EXPR)
32856         {
32857           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32858              opcode.  */
32859           code = OMP_ATOMIC;
32860           rhs = lhs;
32861           lhs = v;
32862           v = NULL_TREE;
32863         }
32864       goto done;
32865     case OMP_ATOMIC_CAPTURE_NEW:
32866       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32867         {
32868           cp_lexer_consume_token (parser->lexer);
32869           structured_block = true;
32870         }
32871       else
32872         {
32873           v = cp_parser_unary_expression (parser);
32874           if (v == error_mark_node)
32875             goto saw_error;
32876           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32877             goto saw_error;
32878         }
32879     default:
32880       break;
32881     }
32882
32883 restart:
32884   lhs = cp_parser_unary_expression (parser);
32885   orig_lhs = lhs;
32886   switch (TREE_CODE (lhs))
32887     {
32888     case ERROR_MARK:
32889       goto saw_error;
32890
32891     case POSTINCREMENT_EXPR:
32892       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32893         code = OMP_ATOMIC_CAPTURE_OLD;
32894       /* FALLTHROUGH */
32895     case PREINCREMENT_EXPR:
32896       lhs = TREE_OPERAND (lhs, 0);
32897       opcode = PLUS_EXPR;
32898       rhs = integer_one_node;
32899       break;
32900
32901     case POSTDECREMENT_EXPR:
32902       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32903         code = OMP_ATOMIC_CAPTURE_OLD;
32904       /* FALLTHROUGH */
32905     case PREDECREMENT_EXPR:
32906       lhs = TREE_OPERAND (lhs, 0);
32907       opcode = MINUS_EXPR;
32908       rhs = integer_one_node;
32909       break;
32910
32911     case COMPOUND_EXPR:
32912       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32913          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32914          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32915          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32916          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32917                                              (TREE_OPERAND (lhs, 1), 0), 0)))
32918             == BOOLEAN_TYPE)
32919        /* Undo effects of boolean_increment for post {in,de}crement.  */
32920        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32921       /* FALLTHRU */
32922     case MODIFY_EXPR:
32923       if (TREE_CODE (lhs) == MODIFY_EXPR
32924          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32925         {
32926           /* Undo effects of boolean_increment.  */
32927           if (integer_onep (TREE_OPERAND (lhs, 1)))
32928             {
32929               /* This is pre or post increment.  */
32930               rhs = TREE_OPERAND (lhs, 1);
32931               lhs = TREE_OPERAND (lhs, 0);
32932               opcode = NOP_EXPR;
32933               if (code == OMP_ATOMIC_CAPTURE_NEW
32934                   && !structured_block
32935                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32936                 code = OMP_ATOMIC_CAPTURE_OLD;
32937               break;
32938             }
32939         }
32940       /* FALLTHRU */
32941     default:
32942       switch (cp_lexer_peek_token (parser->lexer)->type)
32943         {
32944         case CPP_MULT_EQ:
32945           opcode = MULT_EXPR;
32946           break;
32947         case CPP_DIV_EQ:
32948           opcode = TRUNC_DIV_EXPR;
32949           break;
32950         case CPP_PLUS_EQ:
32951           opcode = PLUS_EXPR;
32952           break;
32953         case CPP_MINUS_EQ:
32954           opcode = MINUS_EXPR;
32955           break;
32956         case CPP_LSHIFT_EQ:
32957           opcode = LSHIFT_EXPR;
32958           break;
32959         case CPP_RSHIFT_EQ:
32960           opcode = RSHIFT_EXPR;
32961           break;
32962         case CPP_AND_EQ:
32963           opcode = BIT_AND_EXPR;
32964           break;
32965         case CPP_OR_EQ:
32966           opcode = BIT_IOR_EXPR;
32967           break;
32968         case CPP_XOR_EQ:
32969           opcode = BIT_XOR_EXPR;
32970           break;
32971         case CPP_EQ:
32972           enum cp_parser_prec oprec;
32973           cp_token *token;
32974           cp_lexer_consume_token (parser->lexer);
32975           cp_parser_parse_tentatively (parser);
32976           rhs1 = cp_parser_simple_cast_expression (parser);
32977           if (rhs1 == error_mark_node)
32978             {
32979               cp_parser_abort_tentative_parse (parser);
32980               cp_parser_simple_cast_expression (parser);
32981               goto saw_error;
32982             }
32983           token = cp_lexer_peek_token (parser->lexer);
32984           if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32985             {
32986               cp_parser_abort_tentative_parse (parser);
32987               cp_parser_parse_tentatively (parser);
32988               rhs = cp_parser_binary_expression (parser, false, true,
32989                                                  PREC_NOT_OPERATOR, NULL);
32990               if (rhs == error_mark_node)
32991                 {
32992                   cp_parser_abort_tentative_parse (parser);
32993                   cp_parser_binary_expression (parser, false, true,
32994                                                PREC_NOT_OPERATOR, NULL);
32995                   goto saw_error;
32996                 }
32997               switch (TREE_CODE (rhs))
32998                 {
32999                 case MULT_EXPR:
33000                 case TRUNC_DIV_EXPR:
33001                 case RDIV_EXPR:
33002                 case PLUS_EXPR:
33003                 case MINUS_EXPR:
33004                 case LSHIFT_EXPR:
33005                 case RSHIFT_EXPR:
33006                 case BIT_AND_EXPR:
33007                 case BIT_IOR_EXPR:
33008                 case BIT_XOR_EXPR:
33009                   if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33010                     {
33011                       if (cp_parser_parse_definitely (parser))
33012                         {
33013                           opcode = TREE_CODE (rhs);
33014                           rhs1 = TREE_OPERAND (rhs, 0);
33015                           rhs = TREE_OPERAND (rhs, 1);
33016                           goto stmt_done;
33017                         }
33018                       else
33019                         goto saw_error;
33020                     }
33021                   break;
33022                 default:
33023                   break;
33024                 }
33025               cp_parser_abort_tentative_parse (parser);
33026               if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33027                 {
33028                   rhs = cp_parser_expression (parser);
33029                   if (rhs == error_mark_node)
33030                     goto saw_error;
33031                   opcode = NOP_EXPR;
33032                   rhs1 = NULL_TREE;
33033                   goto stmt_done;
33034                 }
33035               cp_parser_error (parser,
33036                                "invalid form of %<#pragma omp atomic%>");
33037               goto saw_error;
33038             }
33039           if (!cp_parser_parse_definitely (parser))
33040             goto saw_error;
33041           switch (token->type)
33042             {
33043             case CPP_SEMICOLON:
33044               if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33045                 {
33046                   code = OMP_ATOMIC_CAPTURE_OLD;
33047                   v = lhs;
33048                   lhs = NULL_TREE;
33049                   lhs1 = rhs1;
33050                   rhs1 = NULL_TREE;
33051                   cp_lexer_consume_token (parser->lexer);
33052                   goto restart;
33053                 }
33054               else if (structured_block)
33055                 {
33056                   opcode = NOP_EXPR;
33057                   rhs = rhs1;
33058                   rhs1 = NULL_TREE;
33059                   goto stmt_done;
33060                 }
33061               cp_parser_error (parser,
33062                                "invalid form of %<#pragma omp atomic%>");
33063               goto saw_error;
33064             case CPP_MULT:
33065               opcode = MULT_EXPR;
33066               break;
33067             case CPP_DIV:
33068               opcode = TRUNC_DIV_EXPR;
33069               break;
33070             case CPP_PLUS:
33071               opcode = PLUS_EXPR;
33072               break;
33073             case CPP_MINUS:
33074               opcode = MINUS_EXPR;
33075               break;
33076             case CPP_LSHIFT:
33077               opcode = LSHIFT_EXPR;
33078               break;
33079             case CPP_RSHIFT:
33080               opcode = RSHIFT_EXPR;
33081               break;
33082             case CPP_AND:
33083               opcode = BIT_AND_EXPR;
33084               break;
33085             case CPP_OR:
33086               opcode = BIT_IOR_EXPR;
33087               break;
33088             case CPP_XOR:
33089               opcode = BIT_XOR_EXPR;
33090               break;
33091             default:
33092               cp_parser_error (parser,
33093                                "invalid operator for %<#pragma omp atomic%>");
33094               goto saw_error;
33095             }
33096           oprec = TOKEN_PRECEDENCE (token);
33097           gcc_assert (oprec != PREC_NOT_OPERATOR);
33098           if (commutative_tree_code (opcode))
33099             oprec = (enum cp_parser_prec) (oprec - 1);
33100           cp_lexer_consume_token (parser->lexer);
33101           rhs = cp_parser_binary_expression (parser, false, false,
33102                                              oprec, NULL);
33103           if (rhs == error_mark_node)
33104             goto saw_error;
33105           goto stmt_done;
33106           /* FALLTHROUGH */
33107         default:
33108           cp_parser_error (parser,
33109                            "invalid operator for %<#pragma omp atomic%>");
33110           goto saw_error;
33111         }
33112       cp_lexer_consume_token (parser->lexer);
33113
33114       rhs = cp_parser_expression (parser);
33115       if (rhs == error_mark_node)
33116         goto saw_error;
33117       break;
33118     }
33119 stmt_done:
33120   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33121     {
33122       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33123         goto saw_error;
33124       v = cp_parser_unary_expression (parser);
33125       if (v == error_mark_node)
33126         goto saw_error;
33127       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33128         goto saw_error;
33129       lhs1 = cp_parser_unary_expression (parser);
33130       if (lhs1 == error_mark_node)
33131         goto saw_error;
33132     }
33133   if (structured_block)
33134     {
33135       cp_parser_consume_semicolon_at_end_of_statement (parser);
33136       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33137     }
33138 done:
33139   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33140   if (!structured_block)
33141     cp_parser_consume_semicolon_at_end_of_statement (parser);
33142   return;
33143
33144  saw_error:
33145   cp_parser_skip_to_end_of_block_or_statement (parser);
33146   if (structured_block)
33147     {
33148       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33149         cp_lexer_consume_token (parser->lexer);
33150       else if (code == OMP_ATOMIC_CAPTURE_NEW)
33151         {
33152           cp_parser_skip_to_end_of_block_or_statement (parser);
33153           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33154             cp_lexer_consume_token (parser->lexer);
33155         }
33156     }
33157 }
33158
33159
33160 /* OpenMP 2.5:
33161    # pragma omp barrier new-line  */
33162
33163 static void
33164 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33165 {
33166   cp_parser_require_pragma_eol (parser, pragma_tok);
33167   finish_omp_barrier ();
33168 }
33169
33170 /* OpenMP 2.5:
33171    # pragma omp critical [(name)] new-line
33172      structured-block
33173
33174    OpenMP 4.5:
33175    # pragma omp critical [(name) [hint(expression)]] new-line
33176      structured-block  */
33177
33178 #define OMP_CRITICAL_CLAUSE_MASK                \
33179         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33180
33181 static tree
33182 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33183 {
33184   tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33185
33186   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33187     {
33188       cp_lexer_consume_token (parser->lexer);
33189
33190       name = cp_parser_identifier (parser);
33191
33192       if (name == error_mark_node
33193           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33194         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33195                                                /*or_comma=*/false,
33196                                                /*consume_paren=*/true);
33197       if (name == error_mark_node)
33198         name = NULL;
33199
33200       clauses = cp_parser_omp_all_clauses (parser,
33201                                            OMP_CRITICAL_CLAUSE_MASK,
33202                                            "#pragma omp critical", pragma_tok);
33203     }
33204   else
33205     cp_parser_require_pragma_eol (parser, pragma_tok);
33206
33207   stmt = cp_parser_omp_structured_block (parser, if_p);
33208   return c_finish_omp_critical (input_location, stmt, name, clauses);
33209 }
33210
33211 /* OpenMP 2.5:
33212    # pragma omp flush flush-vars[opt] new-line
33213
33214    flush-vars:
33215      ( variable-list ) */
33216
33217 static void
33218 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33219 {
33220   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33221     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33222   cp_parser_require_pragma_eol (parser, pragma_tok);
33223
33224   finish_omp_flush ();
33225 }
33226
33227 /* Helper function, to parse omp for increment expression.  */
33228
33229 static tree
33230 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33231 {
33232   tree cond = cp_parser_binary_expression (parser, false, true,
33233                                            PREC_NOT_OPERATOR, NULL);
33234   if (cond == error_mark_node
33235       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33236     {
33237       cp_parser_skip_to_end_of_statement (parser);
33238       return error_mark_node;
33239     }
33240
33241   switch (TREE_CODE (cond))
33242     {
33243     case GT_EXPR:
33244     case GE_EXPR:
33245     case LT_EXPR:
33246     case LE_EXPR:
33247       break;
33248     case NE_EXPR:
33249       if (code == CILK_SIMD || code == CILK_FOR)
33250         break;
33251       /* Fall through: OpenMP disallows NE_EXPR.  */
33252     default:
33253       return error_mark_node;
33254     }
33255
33256   /* If decl is an iterator, preserve LHS and RHS of the relational
33257      expr until finish_omp_for.  */
33258   if (decl
33259       && (type_dependent_expression_p (decl)
33260           || CLASS_TYPE_P (TREE_TYPE (decl))))
33261     return cond;
33262
33263   return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33264                             TREE_CODE (cond),
33265                             TREE_OPERAND (cond, 0), ERROR_MARK,
33266                             TREE_OPERAND (cond, 1), ERROR_MARK,
33267                             /*overload=*/NULL, tf_warning_or_error);
33268 }
33269
33270 /* Helper function, to parse omp for increment expression.  */
33271
33272 static tree
33273 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33274 {
33275   cp_token *token = cp_lexer_peek_token (parser->lexer);
33276   enum tree_code op;
33277   tree lhs, rhs;
33278   cp_id_kind idk;
33279   bool decl_first;
33280
33281   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33282     {
33283       op = (token->type == CPP_PLUS_PLUS
33284             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33285       cp_lexer_consume_token (parser->lexer);
33286       lhs = cp_parser_simple_cast_expression (parser);
33287       if (lhs != decl
33288           && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33289         return error_mark_node;
33290       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33291     }
33292
33293   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33294   if (lhs != decl
33295       && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33296     return error_mark_node;
33297
33298   token = cp_lexer_peek_token (parser->lexer);
33299   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33300     {
33301       op = (token->type == CPP_PLUS_PLUS
33302             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33303       cp_lexer_consume_token (parser->lexer);
33304       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33305     }
33306
33307   op = cp_parser_assignment_operator_opt (parser);
33308   if (op == ERROR_MARK)
33309     return error_mark_node;
33310
33311   if (op != NOP_EXPR)
33312     {
33313       rhs = cp_parser_assignment_expression (parser);
33314       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33315       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33316     }
33317
33318   lhs = cp_parser_binary_expression (parser, false, false,
33319                                      PREC_ADDITIVE_EXPRESSION, NULL);
33320   token = cp_lexer_peek_token (parser->lexer);
33321   decl_first = (lhs == decl
33322                 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33323   if (decl_first)
33324     lhs = NULL_TREE;
33325   if (token->type != CPP_PLUS
33326       && token->type != CPP_MINUS)
33327     return error_mark_node;
33328
33329   do
33330     {
33331       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33332       cp_lexer_consume_token (parser->lexer);
33333       rhs = cp_parser_binary_expression (parser, false, false,
33334                                          PREC_ADDITIVE_EXPRESSION, NULL);
33335       token = cp_lexer_peek_token (parser->lexer);
33336       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33337         {
33338           if (lhs == NULL_TREE)
33339             {
33340               if (op == PLUS_EXPR)
33341                 lhs = rhs;
33342               else
33343                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33344                                         tf_warning_or_error);
33345             }
33346           else
33347             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33348                                      ERROR_MARK, NULL, tf_warning_or_error);
33349         }
33350     }
33351   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33352
33353   if (!decl_first)
33354     {
33355       if ((rhs != decl
33356            && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33357           || op == MINUS_EXPR)
33358         return error_mark_node;
33359       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33360     }
33361   else
33362     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33363
33364   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33365 }
33366
33367 /* Parse the initialization statement of either an OpenMP for loop or
33368    a Cilk Plus for loop.
33369
33370    Return true if the resulting construct should have an
33371    OMP_CLAUSE_PRIVATE added to it.  */
33372
33373 static tree
33374 cp_parser_omp_for_loop_init (cp_parser *parser,
33375                              enum tree_code code,
33376                              tree &this_pre_body,
33377                              vec<tree, va_gc> *for_block,
33378                              tree &init,
33379                              tree &orig_init,
33380                              tree &decl,
33381                              tree &real_decl)
33382 {
33383   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33384     return NULL_TREE;
33385
33386   tree add_private_clause = NULL_TREE;
33387
33388   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33389
33390      init-expr:
33391      var = lb
33392      integer-type var = lb
33393      random-access-iterator-type var = lb
33394      pointer-type var = lb
33395   */
33396   cp_decl_specifier_seq type_specifiers;
33397
33398   /* First, try to parse as an initialized declaration.  See
33399      cp_parser_condition, from whence the bulk of this is copied.  */
33400
33401   cp_parser_parse_tentatively (parser);
33402   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33403                                 /*is_trailing_return=*/false,
33404                                 &type_specifiers);
33405   if (cp_parser_parse_definitely (parser))
33406     {
33407       /* If parsing a type specifier seq succeeded, then this
33408          MUST be a initialized declaration.  */
33409       tree asm_specification, attributes;
33410       cp_declarator *declarator;
33411
33412       declarator = cp_parser_declarator (parser,
33413                                          CP_PARSER_DECLARATOR_NAMED,
33414                                          /*ctor_dtor_or_conv_p=*/NULL,
33415                                          /*parenthesized_p=*/NULL,
33416                                          /*member_p=*/false,
33417                                          /*friend_p=*/false);
33418       attributes = cp_parser_attributes_opt (parser);
33419       asm_specification = cp_parser_asm_specification_opt (parser);
33420
33421       if (declarator == cp_error_declarator) 
33422         cp_parser_skip_to_end_of_statement (parser);
33423
33424       else 
33425         {
33426           tree pushed_scope, auto_node;
33427
33428           decl = start_decl (declarator, &type_specifiers,
33429                              SD_INITIALIZED, attributes,
33430                              /*prefix_attributes=*/NULL_TREE,
33431                              &pushed_scope);
33432
33433           auto_node = type_uses_auto (TREE_TYPE (decl));
33434           if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33435             {
33436               if (cp_lexer_next_token_is (parser->lexer, 
33437                                           CPP_OPEN_PAREN))
33438                 {
33439                   if (code != CILK_SIMD && code != CILK_FOR)
33440                     error ("parenthesized initialization is not allowed in "
33441                            "OpenMP %<for%> loop");
33442                   else
33443                     error ("parenthesized initialization is "
33444                            "not allowed in for-loop");
33445                 }
33446               else
33447                 /* Trigger an error.  */
33448                 cp_parser_require (parser, CPP_EQ, RT_EQ);
33449
33450               init = error_mark_node;
33451               cp_parser_skip_to_end_of_statement (parser);
33452             }
33453           else if (CLASS_TYPE_P (TREE_TYPE (decl))
33454                    || type_dependent_expression_p (decl)
33455                    || auto_node)
33456             {
33457               bool is_direct_init, is_non_constant_init;
33458
33459               init = cp_parser_initializer (parser,
33460                                             &is_direct_init,
33461                                             &is_non_constant_init);
33462
33463               if (auto_node)
33464                 {
33465                   TREE_TYPE (decl)
33466                     = do_auto_deduction (TREE_TYPE (decl), init,
33467                                          auto_node);
33468
33469                   if (!CLASS_TYPE_P (TREE_TYPE (decl))
33470                       && !type_dependent_expression_p (decl))
33471                     goto non_class;
33472                 }
33473                       
33474               cp_finish_decl (decl, init, !is_non_constant_init,
33475                               asm_specification,
33476                               LOOKUP_ONLYCONVERTING);
33477               orig_init = init;
33478               if (CLASS_TYPE_P (TREE_TYPE (decl)))
33479                 {
33480                   vec_safe_push (for_block, this_pre_body);
33481                   init = NULL_TREE;
33482                 }
33483               else
33484                 init = pop_stmt_list (this_pre_body);
33485               this_pre_body = NULL_TREE;
33486             }
33487           else
33488             {
33489               /* Consume '='.  */
33490               cp_lexer_consume_token (parser->lexer);
33491               init = cp_parser_assignment_expression (parser);
33492
33493             non_class:
33494               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33495                 init = error_mark_node;
33496               else
33497                 cp_finish_decl (decl, NULL_TREE,
33498                                 /*init_const_expr_p=*/false,
33499                                 asm_specification,
33500                                 LOOKUP_ONLYCONVERTING);
33501             }
33502
33503           if (pushed_scope)
33504             pop_scope (pushed_scope);
33505         }
33506     }
33507   else 
33508     {
33509       cp_id_kind idk;
33510       /* If parsing a type specifier sequence failed, then
33511          this MUST be a simple expression.  */
33512       if (code == CILK_FOR)
33513         error ("%<_Cilk_for%> allows expression instead of declaration only "
33514                "in C, not in C++");
33515       cp_parser_parse_tentatively (parser);
33516       decl = cp_parser_primary_expression (parser, false, false,
33517                                            false, &idk);
33518       cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33519       if (!cp_parser_error_occurred (parser)
33520           && decl
33521           && (TREE_CODE (decl) == COMPONENT_REF
33522               || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33523         {
33524           cp_parser_abort_tentative_parse (parser);
33525           cp_parser_parse_tentatively (parser);
33526           cp_token *token = cp_lexer_peek_token (parser->lexer);
33527           tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33528                                                /*check_dependency_p=*/true,
33529                                                /*template_p=*/NULL,
33530                                                /*declarator_p=*/false,
33531                                                /*optional_p=*/false);
33532           if (name != error_mark_node
33533               && last_tok == cp_lexer_peek_token (parser->lexer))
33534             {
33535               decl = cp_parser_lookup_name_simple (parser, name,
33536                                                    token->location);
33537               if (TREE_CODE (decl) == FIELD_DECL)
33538                 add_private_clause = omp_privatize_field (decl, false);
33539             }
33540           cp_parser_abort_tentative_parse (parser);
33541           cp_parser_parse_tentatively (parser);
33542           decl = cp_parser_primary_expression (parser, false, false,
33543                                                false, &idk);
33544         }
33545       if (!cp_parser_error_occurred (parser)
33546           && decl
33547           && DECL_P (decl)
33548           && CLASS_TYPE_P (TREE_TYPE (decl)))
33549         {
33550           tree rhs;
33551
33552           cp_parser_parse_definitely (parser);
33553           cp_parser_require (parser, CPP_EQ, RT_EQ);
33554           rhs = cp_parser_assignment_expression (parser);
33555           orig_init = rhs;
33556           finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33557                                                  decl, NOP_EXPR,
33558                                                  rhs,
33559                                                  tf_warning_or_error));
33560           if (!add_private_clause)
33561             add_private_clause = decl;
33562         }
33563       else
33564         {
33565           decl = NULL;
33566           cp_parser_abort_tentative_parse (parser);
33567           init = cp_parser_expression (parser);
33568           if (init)
33569             {
33570               if (TREE_CODE (init) == MODIFY_EXPR
33571                   || TREE_CODE (init) == MODOP_EXPR)
33572                 real_decl = TREE_OPERAND (init, 0);
33573             }
33574         }
33575     }
33576   return add_private_clause;
33577 }
33578
33579 /* Parse the restricted form of the for statement allowed by OpenMP.  */
33580
33581 static tree
33582 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33583                         tree *cclauses, bool *if_p)
33584 {
33585   tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33586   tree real_decl, initv, condv, incrv, declv;
33587   tree this_pre_body, cl, ordered_cl = NULL_TREE;
33588   location_t loc_first;
33589   bool collapse_err = false;
33590   int i, collapse = 1, ordered = 0, count, nbraces = 0;
33591   vec<tree, va_gc> *for_block = make_tree_vector ();
33592   auto_vec<tree, 4> orig_inits;
33593
33594   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33595     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33596       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33597     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33598              && OMP_CLAUSE_ORDERED_EXPR (cl))
33599       {
33600         ordered_cl = cl;
33601         ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33602       }
33603
33604   if (ordered && ordered < collapse)
33605     {
33606       error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33607                 "%<ordered%> clause parameter is less than %<collapse%>");
33608       OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33609         = build_int_cst (NULL_TREE, collapse);
33610       ordered = collapse;
33611     }
33612   if (ordered)
33613     {
33614       for (tree *pc = &clauses; *pc; )
33615         if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33616           {
33617             error_at (OMP_CLAUSE_LOCATION (*pc),
33618                       "%<linear%> clause may not be specified together "
33619                       "with %<ordered%> clause with a parameter");
33620             *pc = OMP_CLAUSE_CHAIN (*pc);
33621           }
33622         else
33623           pc = &OMP_CLAUSE_CHAIN (*pc);
33624     }
33625
33626   gcc_assert (collapse >= 1 && ordered >= 0);
33627   count = ordered ? ordered : collapse;
33628
33629   declv = make_tree_vec (count);
33630   initv = make_tree_vec (count);
33631   condv = make_tree_vec (count);
33632   incrv = make_tree_vec (count);
33633
33634   loc_first = cp_lexer_peek_token (parser->lexer)->location;
33635
33636   for (i = 0; i < count; i++)
33637     {
33638       int bracecount = 0;
33639       tree add_private_clause = NULL_TREE;
33640       location_t loc;
33641
33642       if (code != CILK_FOR
33643           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33644         {
33645           cp_parser_error (parser, "for statement expected");
33646           return NULL;
33647         }
33648       if (code == CILK_FOR
33649           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33650         {
33651           cp_parser_error (parser, "_Cilk_for statement expected");
33652           return NULL;
33653         }
33654       loc = cp_lexer_consume_token (parser->lexer)->location;
33655
33656       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33657         return NULL;
33658
33659       init = orig_init = decl = real_decl = NULL;
33660       this_pre_body = push_stmt_list ();
33661
33662       add_private_clause
33663         = cp_parser_omp_for_loop_init (parser, code,
33664                                        this_pre_body, for_block,
33665                                        init, orig_init, decl, real_decl);
33666
33667       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33668       if (this_pre_body)
33669         {
33670           this_pre_body = pop_stmt_list (this_pre_body);
33671           if (pre_body)
33672             {
33673               tree t = pre_body;
33674               pre_body = push_stmt_list ();
33675               add_stmt (t);
33676               add_stmt (this_pre_body);
33677               pre_body = pop_stmt_list (pre_body);
33678             }
33679           else
33680             pre_body = this_pre_body;
33681         }
33682
33683       if (decl)
33684         real_decl = decl;
33685       if (cclauses != NULL
33686           && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33687           && real_decl != NULL_TREE)
33688         {
33689           tree *c;
33690           for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33691             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33692                 && OMP_CLAUSE_DECL (*c) == real_decl)
33693               {
33694                 error_at (loc, "iteration variable %qD"
33695                           " should not be firstprivate", real_decl);
33696                 *c = OMP_CLAUSE_CHAIN (*c);
33697               }
33698             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33699                      && OMP_CLAUSE_DECL (*c) == real_decl)
33700               {
33701                 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
33702                 tree l = *c;
33703                 *c = OMP_CLAUSE_CHAIN (*c);
33704                 if (code == OMP_SIMD)
33705                   {
33706                     OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33707                     cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33708                   }
33709                 else
33710                   {
33711                     OMP_CLAUSE_CHAIN (l) = clauses;
33712                     clauses = l;
33713                   }
33714                 add_private_clause = NULL_TREE;
33715               }
33716             else
33717               {
33718                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33719                     && OMP_CLAUSE_DECL (*c) == real_decl)
33720                   add_private_clause = NULL_TREE;
33721                 c = &OMP_CLAUSE_CHAIN (*c);
33722               }
33723         }
33724
33725       if (add_private_clause)
33726         {
33727           tree c;
33728           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33729             {
33730               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33731                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33732                   && OMP_CLAUSE_DECL (c) == decl)
33733                 break;
33734               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33735                        && OMP_CLAUSE_DECL (c) == decl)
33736                 error_at (loc, "iteration variable %qD "
33737                           "should not be firstprivate",
33738                           decl);
33739               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33740                        && OMP_CLAUSE_DECL (c) == decl)
33741                 error_at (loc, "iteration variable %qD should not be reduction",
33742                           decl);
33743             }
33744           if (c == NULL)
33745             {
33746               if (code != OMP_SIMD)
33747                 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33748               else if (collapse == 1)
33749                 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33750               else
33751                 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33752               OMP_CLAUSE_DECL (c) = add_private_clause;
33753               c = finish_omp_clauses (c, true);
33754               if (c)
33755                 {
33756                   OMP_CLAUSE_CHAIN (c) = clauses;
33757                   clauses = c;
33758                   /* For linear, signal that we need to fill up
33759                      the so far unknown linear step.  */
33760                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33761                     OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33762                 }
33763             }
33764         }
33765
33766       cond = NULL;
33767       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33768         cond = cp_parser_omp_for_cond (parser, decl, code);
33769       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33770
33771       incr = NULL;
33772       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33773         {
33774           /* If decl is an iterator, preserve the operator on decl
33775              until finish_omp_for.  */
33776           if (real_decl
33777               && ((processing_template_decl
33778                    && (TREE_TYPE (real_decl) == NULL_TREE
33779                        || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33780                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33781             incr = cp_parser_omp_for_incr (parser, real_decl);
33782           else
33783             incr = cp_parser_expression (parser);
33784           if (!EXPR_HAS_LOCATION (incr))
33785             protected_set_expr_location (incr, input_location);
33786         }
33787
33788       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33789         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33790                                                /*or_comma=*/false,
33791                                                /*consume_paren=*/true);
33792
33793       TREE_VEC_ELT (declv, i) = decl;
33794       TREE_VEC_ELT (initv, i) = init;
33795       TREE_VEC_ELT (condv, i) = cond;
33796       TREE_VEC_ELT (incrv, i) = incr;
33797       if (orig_init)
33798         {
33799           orig_inits.safe_grow_cleared (i + 1);
33800           orig_inits[i] = orig_init;
33801         }
33802
33803       if (i == count - 1)
33804         break;
33805
33806       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33807          in between the collapsed for loops to be still considered perfectly
33808          nested.  Hopefully the final version clarifies this.
33809          For now handle (multiple) {'s and empty statements.  */
33810       cp_parser_parse_tentatively (parser);
33811       do
33812         {
33813           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33814             break;
33815           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33816             {
33817               cp_lexer_consume_token (parser->lexer);
33818               bracecount++;
33819             }
33820           else if (bracecount
33821                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33822             cp_lexer_consume_token (parser->lexer);
33823           else
33824             {
33825               loc = cp_lexer_peek_token (parser->lexer)->location;
33826               error_at (loc, "not enough collapsed for loops");
33827               collapse_err = true;
33828               cp_parser_abort_tentative_parse (parser);
33829               declv = NULL_TREE;
33830               break;
33831             }
33832         }
33833       while (1);
33834
33835       if (declv)
33836         {
33837           cp_parser_parse_definitely (parser);
33838           nbraces += bracecount;
33839         }
33840     }
33841
33842   if (nbraces)
33843     if_p = NULL;
33844
33845   /* Note that we saved the original contents of this flag when we entered
33846      the structured block, and so we don't need to re-save it here.  */
33847   if (code == CILK_SIMD || code == CILK_FOR)
33848     parser->in_statement = IN_CILK_SIMD_FOR;
33849   else
33850     parser->in_statement = IN_OMP_FOR;
33851
33852   /* Note that the grammar doesn't call for a structured block here,
33853      though the loop as a whole is a structured block.  */
33854   body = push_stmt_list ();
33855   cp_parser_statement (parser, NULL_TREE, false, if_p);
33856   body = pop_stmt_list (body);
33857
33858   if (declv == NULL_TREE)
33859     ret = NULL_TREE;
33860   else
33861     ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33862                           body, pre_body, &orig_inits, clauses);
33863
33864   while (nbraces)
33865     {
33866       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33867         {
33868           cp_lexer_consume_token (parser->lexer);
33869           nbraces--;
33870         }
33871       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33872         cp_lexer_consume_token (parser->lexer);
33873       else
33874         {
33875           if (!collapse_err)
33876             {
33877               error_at (cp_lexer_peek_token (parser->lexer)->location,
33878                         "collapsed loops not perfectly nested");
33879             }
33880           collapse_err = true;
33881           cp_parser_statement_seq_opt (parser, NULL);
33882           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33883             break;
33884         }
33885     }
33886
33887   while (!for_block->is_empty ())
33888     add_stmt (pop_stmt_list (for_block->pop ()));
33889   release_tree_vector (for_block);
33890
33891   return ret;
33892 }
33893
33894 /* Helper function for OpenMP parsing, split clauses and call
33895    finish_omp_clauses on each of the set of clauses afterwards.  */
33896
33897 static void
33898 cp_omp_split_clauses (location_t loc, enum tree_code code,
33899                       omp_clause_mask mask, tree clauses, tree *cclauses)
33900 {
33901   int i;
33902   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33903   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33904     if (cclauses[i])
33905       cclauses[i] = finish_omp_clauses (cclauses[i], true);
33906 }
33907
33908 /* OpenMP 4.0:
33909    #pragma omp simd simd-clause[optseq] new-line
33910      for-loop  */
33911
33912 #define OMP_SIMD_CLAUSE_MASK                                    \
33913         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)      \
33914         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
33915         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
33916         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
33917         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
33918         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
33919         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
33920         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33921
33922 static tree
33923 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33924                     char *p_name, omp_clause_mask mask, tree *cclauses,
33925                     bool *if_p)
33926 {
33927   tree clauses, sb, ret;
33928   unsigned int save;
33929   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33930
33931   strcat (p_name, " simd");
33932   mask |= OMP_SIMD_CLAUSE_MASK;
33933
33934   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33935                                        cclauses == NULL);
33936   if (cclauses)
33937     {
33938       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33939       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33940       tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33941                                 OMP_CLAUSE_ORDERED);
33942       if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33943         {
33944           error_at (OMP_CLAUSE_LOCATION (c),
33945                     "%<ordered%> clause with parameter may not be specified "
33946                     "on %qs construct", p_name);
33947           OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33948         }
33949     }
33950
33951   sb = begin_omp_structured_block ();
33952   save = cp_parser_begin_omp_structured_block (parser);
33953
33954   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33955
33956   cp_parser_end_omp_structured_block (parser, save);
33957   add_stmt (finish_omp_structured_block (sb));
33958
33959   return ret;
33960 }
33961
33962 /* OpenMP 2.5:
33963    #pragma omp for for-clause[optseq] new-line
33964      for-loop
33965
33966    OpenMP 4.0:
33967    #pragma omp for simd for-simd-clause[optseq] new-line
33968      for-loop  */
33969
33970 #define OMP_FOR_CLAUSE_MASK                                     \
33971         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
33972         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33973         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
33974         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
33975         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
33976         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)      \
33977         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)     \
33978         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
33979         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33980
33981 static tree
33982 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33983                    char *p_name, omp_clause_mask mask, tree *cclauses,
33984                    bool *if_p)
33985 {
33986   tree clauses, sb, ret;
33987   unsigned int save;
33988   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33989
33990   strcat (p_name, " for");
33991   mask |= OMP_FOR_CLAUSE_MASK;
33992   /* parallel for{, simd} disallows nowait clause, but for
33993      target {teams distribute ,}parallel for{, simd} it should be accepted.  */
33994   if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
33995     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33996   /* Composite distribute parallel for{, simd} disallows ordered clause.  */
33997   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33998     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33999
34000   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34001     {
34002       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34003       const char *p = IDENTIFIER_POINTER (id);
34004
34005       if (strcmp (p, "simd") == 0)
34006         {
34007           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34008           if (cclauses == NULL)
34009             cclauses = cclauses_buf;
34010
34011           cp_lexer_consume_token (parser->lexer);
34012           if (!flag_openmp)  /* flag_openmp_simd  */
34013             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34014                                        cclauses, if_p);
34015           sb = begin_omp_structured_block ();
34016           save = cp_parser_begin_omp_structured_block (parser);
34017           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34018                                     cclauses, if_p);
34019           cp_parser_end_omp_structured_block (parser, save);
34020           tree body = finish_omp_structured_block (sb);
34021           if (ret == NULL)
34022             return ret;
34023           ret = make_node (OMP_FOR);
34024           TREE_TYPE (ret) = void_type_node;
34025           OMP_FOR_BODY (ret) = body;
34026           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34027           SET_EXPR_LOCATION (ret, loc);
34028           add_stmt (ret);
34029           return ret;
34030         }
34031     }
34032   if (!flag_openmp)  /* flag_openmp_simd  */
34033     {
34034       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34035       return NULL_TREE;
34036     }
34037
34038   /* Composite distribute parallel for disallows linear clause.  */
34039   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34040     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34041
34042   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34043                                        cclauses == NULL);
34044   if (cclauses)
34045     {
34046       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34047       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34048     }
34049
34050   sb = begin_omp_structured_block ();
34051   save = cp_parser_begin_omp_structured_block (parser);
34052
34053   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34054
34055   cp_parser_end_omp_structured_block (parser, save);
34056   add_stmt (finish_omp_structured_block (sb));
34057
34058   return ret;
34059 }
34060
34061 /* OpenMP 2.5:
34062    # pragma omp master new-line
34063      structured-block  */
34064
34065 static tree
34066 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34067 {
34068   cp_parser_require_pragma_eol (parser, pragma_tok);
34069   return c_finish_omp_master (input_location,
34070                               cp_parser_omp_structured_block (parser, if_p));
34071 }
34072
34073 /* OpenMP 2.5:
34074    # pragma omp ordered new-line
34075      structured-block
34076
34077    OpenMP 4.5:
34078    # pragma omp ordered ordered-clauses new-line
34079      structured-block  */
34080
34081 #define OMP_ORDERED_CLAUSE_MASK                                 \
34082         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)      \
34083         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34084
34085 #define OMP_ORDERED_DEPEND_CLAUSE_MASK                          \
34086         (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34087
34088 static bool
34089 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34090                        enum pragma_context context, bool *if_p)
34091 {
34092   location_t loc = pragma_tok->location;
34093
34094   if (context != pragma_stmt && context != pragma_compound)
34095     {
34096       cp_parser_error (parser, "expected declaration specifiers");
34097       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34098       return false;
34099     }
34100
34101   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34102     {
34103       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34104       const char *p = IDENTIFIER_POINTER (id);
34105
34106       if (strcmp (p, "depend") == 0)
34107         {
34108           if (context == pragma_stmt)
34109             {
34110               error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34111                         "%<depend%> clause may only be used in compound "
34112                         "statements");
34113               cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34114               return false;
34115             }
34116           tree clauses
34117             = cp_parser_omp_all_clauses (parser,
34118                                          OMP_ORDERED_DEPEND_CLAUSE_MASK,
34119                                          "#pragma omp ordered", pragma_tok);
34120           c_finish_omp_ordered (loc, clauses, NULL_TREE);
34121           return false;
34122         }
34123     }
34124
34125   tree clauses
34126     = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34127                                  "#pragma omp ordered", pragma_tok);
34128   c_finish_omp_ordered (loc, clauses,
34129                         cp_parser_omp_structured_block (parser, if_p));
34130   return true;
34131 }
34132
34133 /* OpenMP 2.5:
34134
34135    section-scope:
34136      { section-sequence }
34137
34138    section-sequence:
34139      section-directive[opt] structured-block
34140      section-sequence section-directive structured-block  */
34141
34142 static tree
34143 cp_parser_omp_sections_scope (cp_parser *parser)
34144 {
34145   tree stmt, substmt;
34146   bool error_suppress = false;
34147   cp_token *tok;
34148
34149   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34150     return NULL_TREE;
34151
34152   stmt = push_stmt_list ();
34153
34154   if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34155       != PRAGMA_OMP_SECTION)
34156     {
34157       substmt = cp_parser_omp_structured_block (parser, NULL);
34158       substmt = build1 (OMP_SECTION, void_type_node, substmt);
34159       add_stmt (substmt);
34160     }
34161
34162   while (1)
34163     {
34164       tok = cp_lexer_peek_token (parser->lexer);
34165       if (tok->type == CPP_CLOSE_BRACE)
34166         break;
34167       if (tok->type == CPP_EOF)
34168         break;
34169
34170       if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34171         {
34172           cp_lexer_consume_token (parser->lexer);
34173           cp_parser_require_pragma_eol (parser, tok);
34174           error_suppress = false;
34175         }
34176       else if (!error_suppress)
34177         {
34178           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34179           error_suppress = true;
34180         }
34181
34182       substmt = cp_parser_omp_structured_block (parser, NULL);
34183       substmt = build1 (OMP_SECTION, void_type_node, substmt);
34184       add_stmt (substmt);
34185     }
34186   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34187
34188   substmt = pop_stmt_list (stmt);
34189
34190   stmt = make_node (OMP_SECTIONS);
34191   TREE_TYPE (stmt) = void_type_node;
34192   OMP_SECTIONS_BODY (stmt) = substmt;
34193
34194   add_stmt (stmt);
34195   return stmt;
34196 }
34197
34198 /* OpenMP 2.5:
34199    # pragma omp sections sections-clause[optseq] newline
34200      sections-scope  */
34201
34202 #define OMP_SECTIONS_CLAUSE_MASK                                \
34203         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34204         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34205         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
34206         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
34207         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34208
34209 static tree
34210 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34211                         char *p_name, omp_clause_mask mask, tree *cclauses)
34212 {
34213   tree clauses, ret;
34214   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34215
34216   strcat (p_name, " sections");
34217   mask |= OMP_SECTIONS_CLAUSE_MASK;
34218   if (cclauses)
34219     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34220
34221   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34222                                        cclauses == NULL);
34223   if (cclauses)
34224     {
34225       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34226       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34227     }
34228
34229   ret = cp_parser_omp_sections_scope (parser);
34230   if (ret)
34231     OMP_SECTIONS_CLAUSES (ret) = clauses;
34232
34233   return ret;
34234 }
34235
34236 /* OpenMP 2.5:
34237    # pragma omp parallel parallel-clause[optseq] new-line
34238      structured-block
34239    # pragma omp parallel for parallel-for-clause[optseq] new-line
34240      structured-block
34241    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34242      structured-block
34243
34244    OpenMP 4.0:
34245    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34246      structured-block */
34247
34248 #define OMP_PARALLEL_CLAUSE_MASK                                \
34249         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34250         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34251         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34252         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
34253         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
34254         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)       \
34255         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
34256         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)  \
34257         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34258
34259 static tree
34260 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34261                         char *p_name, omp_clause_mask mask, tree *cclauses,
34262                         bool *if_p)
34263 {
34264   tree stmt, clauses, block;
34265   unsigned int save;
34266   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34267
34268   strcat (p_name, " parallel");
34269   mask |= OMP_PARALLEL_CLAUSE_MASK;
34270   /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
34271   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34272       && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34273     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34274
34275   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34276     {
34277       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34278       if (cclauses == NULL)
34279         cclauses = cclauses_buf;
34280
34281       cp_lexer_consume_token (parser->lexer);
34282       if (!flag_openmp)  /* flag_openmp_simd  */
34283         return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34284                                   if_p);
34285       block = begin_omp_parallel ();
34286       save = cp_parser_begin_omp_structured_block (parser);
34287       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34288                                     if_p);
34289       cp_parser_end_omp_structured_block (parser, save);
34290       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34291                                   block);
34292       if (ret == NULL_TREE)
34293         return ret;
34294       OMP_PARALLEL_COMBINED (stmt) = 1;
34295       return stmt;
34296     }
34297   /* When combined with distribute, parallel has to be followed by for.
34298      #pragma omp target parallel is allowed though.  */
34299   else if (cclauses
34300            && (mask & (OMP_CLAUSE_MASK_1
34301                        << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34302     {
34303       error_at (loc, "expected %<for%> after %qs", p_name);
34304       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34305       return NULL_TREE;
34306     }
34307   else if (!flag_openmp)  /* flag_openmp_simd  */
34308     {
34309       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34310       return NULL_TREE;
34311     }
34312   else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34313     {
34314       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34315       const char *p = IDENTIFIER_POINTER (id);
34316       if (strcmp (p, "sections") == 0)
34317         {
34318           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34319           cclauses = cclauses_buf;
34320
34321           cp_lexer_consume_token (parser->lexer);
34322           block = begin_omp_parallel ();
34323           save = cp_parser_begin_omp_structured_block (parser);
34324           cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34325           cp_parser_end_omp_structured_block (parser, save);
34326           stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34327                                       block);
34328           OMP_PARALLEL_COMBINED (stmt) = 1;
34329           return stmt;
34330         }
34331     }
34332
34333   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34334                                        cclauses == NULL);
34335   if (cclauses)
34336     {
34337       cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34338       clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34339     }
34340
34341   block = begin_omp_parallel ();
34342   save = cp_parser_begin_omp_structured_block (parser);
34343   cp_parser_statement (parser, NULL_TREE, false, if_p);
34344   cp_parser_end_omp_structured_block (parser, save);
34345   stmt = finish_omp_parallel (clauses, block);
34346   return stmt;
34347 }
34348
34349 /* OpenMP 2.5:
34350    # pragma omp single single-clause[optseq] new-line
34351      structured-block  */
34352
34353 #define OMP_SINGLE_CLAUSE_MASK                                  \
34354         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34355         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34356         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)  \
34357         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34358
34359 static tree
34360 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34361 {
34362   tree stmt = make_node (OMP_SINGLE);
34363   TREE_TYPE (stmt) = void_type_node;
34364
34365   OMP_SINGLE_CLAUSES (stmt)
34366     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34367                                  "#pragma omp single", pragma_tok);
34368   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34369
34370   return add_stmt (stmt);
34371 }
34372
34373 /* OpenMP 3.0:
34374    # pragma omp task task-clause[optseq] new-line
34375      structured-block  */
34376
34377 #define OMP_TASK_CLAUSE_MASK                                    \
34378         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34379         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
34380         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
34381         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34382         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34383         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
34384         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
34385         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
34386         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34387         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34388
34389 static tree
34390 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34391 {
34392   tree clauses, block;
34393   unsigned int save;
34394
34395   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34396                                        "#pragma omp task", pragma_tok);
34397   block = begin_omp_task ();
34398   save = cp_parser_begin_omp_structured_block (parser);
34399   cp_parser_statement (parser, NULL_TREE, false, if_p);
34400   cp_parser_end_omp_structured_block (parser, save);
34401   return finish_omp_task (clauses, block);
34402 }
34403
34404 /* OpenMP 3.0:
34405    # pragma omp taskwait new-line  */
34406
34407 static void
34408 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34409 {
34410   cp_parser_require_pragma_eol (parser, pragma_tok);
34411   finish_omp_taskwait ();
34412 }
34413
34414 /* OpenMP 3.1:
34415    # pragma omp taskyield new-line  */
34416
34417 static void
34418 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34419 {
34420   cp_parser_require_pragma_eol (parser, pragma_tok);
34421   finish_omp_taskyield ();
34422 }
34423
34424 /* OpenMP 4.0:
34425    # pragma omp taskgroup new-line
34426      structured-block  */
34427
34428 static tree
34429 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34430 {
34431   cp_parser_require_pragma_eol (parser, pragma_tok);
34432   return c_finish_omp_taskgroup (input_location,
34433                                  cp_parser_omp_structured_block (parser,
34434                                                                  if_p));
34435 }
34436
34437
34438 /* OpenMP 2.5:
34439    # pragma omp threadprivate (variable-list) */
34440
34441 static void
34442 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34443 {
34444   tree vars;
34445
34446   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34447   cp_parser_require_pragma_eol (parser, pragma_tok);
34448
34449   finish_omp_threadprivate (vars);
34450 }
34451
34452 /* OpenMP 4.0:
34453    # pragma omp cancel cancel-clause[optseq] new-line  */
34454
34455 #define OMP_CANCEL_CLAUSE_MASK                                  \
34456         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
34457         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
34458         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
34459         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)    \
34460         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34461
34462 static void
34463 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34464 {
34465   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34466                                             "#pragma omp cancel", pragma_tok);
34467   finish_omp_cancel (clauses);
34468 }
34469
34470 /* OpenMP 4.0:
34471    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
34472
34473 #define OMP_CANCELLATION_POINT_CLAUSE_MASK                      \
34474         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
34475         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
34476         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
34477         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34478
34479 static void
34480 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34481 {
34482   tree clauses;
34483   bool point_seen = false;
34484
34485   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34486     {
34487       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34488       const char *p = IDENTIFIER_POINTER (id);
34489
34490       if (strcmp (p, "point") == 0)
34491         {
34492           cp_lexer_consume_token (parser->lexer);
34493           point_seen = true;
34494         }
34495     }
34496   if (!point_seen)
34497     {
34498       cp_parser_error (parser, "expected %<point%>");
34499       cp_parser_require_pragma_eol (parser, pragma_tok);
34500       return;
34501     }
34502
34503   clauses = cp_parser_omp_all_clauses (parser,
34504                                        OMP_CANCELLATION_POINT_CLAUSE_MASK,
34505                                        "#pragma omp cancellation point",
34506                                        pragma_tok);
34507   finish_omp_cancellation_point (clauses);
34508 }
34509
34510 /* OpenMP 4.0:
34511    #pragma omp distribute distribute-clause[optseq] new-line
34512      for-loop  */
34513
34514 #define OMP_DISTRIBUTE_CLAUSE_MASK                              \
34515         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34516         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34517         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
34518         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34519         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34520
34521 static tree
34522 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34523                           char *p_name, omp_clause_mask mask, tree *cclauses,
34524                           bool *if_p)
34525 {
34526   tree clauses, sb, ret;
34527   unsigned int save;
34528   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34529
34530   strcat (p_name, " distribute");
34531   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34532
34533   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34534     {
34535       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34536       const char *p = IDENTIFIER_POINTER (id);
34537       bool simd = false;
34538       bool parallel = false;
34539
34540       if (strcmp (p, "simd") == 0)
34541         simd = true;
34542       else
34543         parallel = strcmp (p, "parallel") == 0;
34544       if (parallel || simd)
34545         {
34546           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34547           if (cclauses == NULL)
34548             cclauses = cclauses_buf;
34549           cp_lexer_consume_token (parser->lexer);
34550           if (!flag_openmp)  /* flag_openmp_simd  */
34551             {
34552               if (simd)
34553                 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34554                                            cclauses, if_p);
34555               else
34556                 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34557                                                cclauses, if_p);
34558             }
34559           sb = begin_omp_structured_block ();
34560           save = cp_parser_begin_omp_structured_block (parser);
34561           if (simd)
34562             ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34563                                       cclauses, if_p);
34564           else
34565             ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34566                                           cclauses, if_p);
34567           cp_parser_end_omp_structured_block (parser, save);
34568           tree body = finish_omp_structured_block (sb);
34569           if (ret == NULL)
34570             return ret;
34571           ret = make_node (OMP_DISTRIBUTE);
34572           TREE_TYPE (ret) = void_type_node;
34573           OMP_FOR_BODY (ret) = body;
34574           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34575           SET_EXPR_LOCATION (ret, loc);
34576           add_stmt (ret);
34577           return ret;
34578         }
34579     }
34580   if (!flag_openmp)  /* flag_openmp_simd  */
34581     {
34582       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34583       return NULL_TREE;
34584     }
34585
34586   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34587                                        cclauses == NULL);
34588   if (cclauses)
34589     {
34590       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34591       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34592     }
34593
34594   sb = begin_omp_structured_block ();
34595   save = cp_parser_begin_omp_structured_block (parser);
34596
34597   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34598
34599   cp_parser_end_omp_structured_block (parser, save);
34600   add_stmt (finish_omp_structured_block (sb));
34601
34602   return ret;
34603 }
34604
34605 /* OpenMP 4.0:
34606    # pragma omp teams teams-clause[optseq] new-line
34607      structured-block  */
34608
34609 #define OMP_TEAMS_CLAUSE_MASK                                   \
34610         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34611         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34612         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
34613         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
34614         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)    \
34615         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34616         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34617
34618 static tree
34619 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34620                      char *p_name, omp_clause_mask mask, tree *cclauses,
34621                      bool *if_p)
34622 {
34623   tree clauses, sb, ret;
34624   unsigned int save;
34625   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34626
34627   strcat (p_name, " teams");
34628   mask |= OMP_TEAMS_CLAUSE_MASK;
34629
34630   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34631     {
34632       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34633       const char *p = IDENTIFIER_POINTER (id);
34634       if (strcmp (p, "distribute") == 0)
34635         {
34636           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34637           if (cclauses == NULL)
34638             cclauses = cclauses_buf;
34639
34640           cp_lexer_consume_token (parser->lexer);
34641           if (!flag_openmp)  /* flag_openmp_simd  */
34642             return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34643                                              cclauses, if_p);
34644           sb = begin_omp_structured_block ();
34645           save = cp_parser_begin_omp_structured_block (parser);
34646           ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34647                                           cclauses, if_p);
34648           cp_parser_end_omp_structured_block (parser, save);
34649           tree body = finish_omp_structured_block (sb);
34650           if (ret == NULL)
34651             return ret;
34652           clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34653           ret = make_node (OMP_TEAMS);
34654           TREE_TYPE (ret) = void_type_node;
34655           OMP_TEAMS_CLAUSES (ret) = clauses;
34656           OMP_TEAMS_BODY (ret) = body;
34657           OMP_TEAMS_COMBINED (ret) = 1;
34658           return add_stmt (ret);
34659         }
34660     }
34661   if (!flag_openmp)  /* flag_openmp_simd  */
34662     {
34663       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34664       return NULL_TREE;
34665     }
34666
34667   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34668                                        cclauses == NULL);
34669   if (cclauses)
34670     {
34671       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34672       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34673     }
34674
34675   tree stmt = make_node (OMP_TEAMS);
34676   TREE_TYPE (stmt) = void_type_node;
34677   OMP_TEAMS_CLAUSES (stmt) = clauses;
34678   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34679
34680   return add_stmt (stmt);
34681 }
34682
34683 /* OpenMP 4.0:
34684    # pragma omp target data target-data-clause[optseq] new-line
34685      structured-block  */
34686
34687 #define OMP_TARGET_DATA_CLAUSE_MASK                             \
34688         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34689         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34690         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34691         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34692
34693 static tree
34694 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34695 {
34696   tree clauses
34697     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34698                                  "#pragma omp target data", pragma_tok);
34699   int map_seen = 0;
34700   for (tree *pc = &clauses; *pc;)
34701     {
34702       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34703         switch (OMP_CLAUSE_MAP_KIND (*pc))
34704           {
34705           case GOMP_MAP_TO:
34706           case GOMP_MAP_ALWAYS_TO:
34707           case GOMP_MAP_FROM:
34708           case GOMP_MAP_ALWAYS_FROM:
34709           case GOMP_MAP_TOFROM:
34710           case GOMP_MAP_ALWAYS_TOFROM:
34711           case GOMP_MAP_ALLOC:
34712             map_seen = 3;
34713             break;
34714           case GOMP_MAP_FIRSTPRIVATE_POINTER:
34715           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34716           case GOMP_MAP_ALWAYS_POINTER:
34717             break;
34718           default:
34719             map_seen |= 1;
34720             error_at (OMP_CLAUSE_LOCATION (*pc),
34721                       "%<#pragma omp target data%> with map-type other "
34722                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34723                       "on %<map%> clause");
34724             *pc = OMP_CLAUSE_CHAIN (*pc);
34725             continue;
34726           }
34727       pc = &OMP_CLAUSE_CHAIN (*pc);
34728     }
34729
34730   if (map_seen != 3)
34731     {
34732       if (map_seen == 0)
34733         error_at (pragma_tok->location,
34734                   "%<#pragma omp target data%> must contain at least "
34735                   "one %<map%> clause");
34736       return NULL_TREE;
34737     }
34738
34739   tree stmt = make_node (OMP_TARGET_DATA);
34740   TREE_TYPE (stmt) = void_type_node;
34741   OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34742
34743   keep_next_level (true);
34744   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34745
34746   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34747   return add_stmt (stmt);
34748 }
34749
34750 /* OpenMP 4.5:
34751    # pragma omp target enter data target-enter-data-clause[optseq] new-line
34752      structured-block  */
34753
34754 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK                       \
34755         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34756         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34757         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34758         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34759         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34760
34761 static tree
34762 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34763                                  enum pragma_context context)
34764 {
34765   bool data_seen = false;
34766   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34767     {
34768       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34769       const char *p = IDENTIFIER_POINTER (id);
34770
34771       if (strcmp (p, "data") == 0)
34772         {
34773           cp_lexer_consume_token (parser->lexer);
34774           data_seen = true;
34775         }
34776     }
34777   if (!data_seen)
34778     {
34779       cp_parser_error (parser, "expected %<data%>");
34780       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34781       return NULL_TREE;
34782     }
34783
34784   if (context == pragma_stmt)
34785     {
34786       error_at (pragma_tok->location,
34787                 "%<#pragma omp target enter data%> may only be "
34788                 "used in compound statements");
34789       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34790       return NULL_TREE;
34791     }
34792
34793   tree clauses
34794     = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34795                                  "#pragma omp target enter data", pragma_tok);
34796   int map_seen = 0;
34797   for (tree *pc = &clauses; *pc;)
34798     {
34799       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34800         switch (OMP_CLAUSE_MAP_KIND (*pc))
34801           {
34802           case GOMP_MAP_TO:
34803           case GOMP_MAP_ALWAYS_TO:
34804           case GOMP_MAP_ALLOC:
34805             map_seen = 3;
34806             break;
34807           case GOMP_MAP_FIRSTPRIVATE_POINTER:
34808           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34809           case GOMP_MAP_ALWAYS_POINTER:
34810             break;
34811           default:
34812             map_seen |= 1;
34813             error_at (OMP_CLAUSE_LOCATION (*pc),
34814                       "%<#pragma omp target enter data%> with map-type other "
34815                       "than %<to%> or %<alloc%> on %<map%> clause");
34816             *pc = OMP_CLAUSE_CHAIN (*pc);
34817             continue;
34818           }
34819       pc = &OMP_CLAUSE_CHAIN (*pc);
34820     }
34821
34822   if (map_seen != 3)
34823     {
34824       if (map_seen == 0)
34825         error_at (pragma_tok->location,
34826                   "%<#pragma omp target enter data%> must contain at least "
34827                   "one %<map%> clause");
34828       return NULL_TREE;
34829     }
34830
34831   tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34832   TREE_TYPE (stmt) = void_type_node;
34833   OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34834   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34835   return add_stmt (stmt);
34836 }
34837
34838 /* OpenMP 4.5:
34839    # pragma omp target exit data target-enter-data-clause[optseq] new-line
34840      structured-block  */
34841
34842 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK                        \
34843         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34844         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34845         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34846         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34847         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34848
34849 static tree
34850 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34851                                 enum pragma_context context)
34852 {
34853   bool data_seen = false;
34854   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34855     {
34856       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34857       const char *p = IDENTIFIER_POINTER (id);
34858
34859       if (strcmp (p, "data") == 0)
34860         {
34861           cp_lexer_consume_token (parser->lexer);
34862           data_seen = true;
34863         }
34864     }
34865   if (!data_seen)
34866     {
34867       cp_parser_error (parser, "expected %<data%>");
34868       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34869       return NULL_TREE;
34870     }
34871
34872   if (context == pragma_stmt)
34873     {
34874       error_at (pragma_tok->location,
34875                 "%<#pragma omp target exit data%> may only be "
34876                 "used in compound statements");
34877       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34878       return NULL_TREE;
34879     }
34880
34881   tree clauses
34882     = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34883                                  "#pragma omp target exit data", pragma_tok);
34884   int map_seen = 0;
34885   for (tree *pc = &clauses; *pc;)
34886     {
34887       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34888         switch (OMP_CLAUSE_MAP_KIND (*pc))
34889           {
34890           case GOMP_MAP_FROM:
34891           case GOMP_MAP_ALWAYS_FROM:
34892           case GOMP_MAP_RELEASE:
34893           case GOMP_MAP_DELETE:
34894             map_seen = 3;
34895             break;
34896           case GOMP_MAP_FIRSTPRIVATE_POINTER:
34897           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34898           case GOMP_MAP_ALWAYS_POINTER:
34899             break;
34900           default:
34901             map_seen |= 1;
34902             error_at (OMP_CLAUSE_LOCATION (*pc),
34903                       "%<#pragma omp target exit data%> with map-type other "
34904                       "than %<from%>, %<release%> or %<delete%> on %<map%>"
34905                       " clause");
34906             *pc = OMP_CLAUSE_CHAIN (*pc);
34907             continue;
34908           }
34909       pc = &OMP_CLAUSE_CHAIN (*pc);
34910     }
34911
34912   if (map_seen != 3)
34913     {
34914       if (map_seen == 0)
34915         error_at (pragma_tok->location,
34916                   "%<#pragma omp target exit data%> must contain at least "
34917                   "one %<map%> clause");
34918       return NULL_TREE;
34919     }
34920
34921   tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34922   TREE_TYPE (stmt) = void_type_node;
34923   OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34924   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34925   return add_stmt (stmt);
34926 }
34927
34928 /* OpenMP 4.0:
34929    # pragma omp target update target-update-clause[optseq] new-line */
34930
34931 #define OMP_TARGET_UPDATE_CLAUSE_MASK                           \
34932         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)         \
34933         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
34934         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34935         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34936         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34937         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34938
34939 static bool
34940 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34941                              enum pragma_context context)
34942 {
34943   if (context == pragma_stmt)
34944     {
34945       error_at (pragma_tok->location,
34946                 "%<#pragma omp target update%> may only be "
34947                 "used in compound statements");
34948       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34949       return false;
34950     }
34951
34952   tree clauses
34953     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34954                                  "#pragma omp target update", pragma_tok);
34955   if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34956       && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34957     {
34958       error_at (pragma_tok->location,
34959                 "%<#pragma omp target update%> must contain at least one "
34960                 "%<from%> or %<to%> clauses");
34961       return false;
34962     }
34963
34964   tree stmt = make_node (OMP_TARGET_UPDATE);
34965   TREE_TYPE (stmt) = void_type_node;
34966   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34967   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34968   add_stmt (stmt);
34969   return false;
34970 }
34971
34972 /* OpenMP 4.0:
34973    # pragma omp target target-clause[optseq] new-line
34974      structured-block  */
34975
34976 #define OMP_TARGET_CLAUSE_MASK                                  \
34977         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34978         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34979         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34980         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34981         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
34982         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34983         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34984         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)   \
34985         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34986
34987 static bool
34988 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34989                       enum pragma_context context, bool *if_p)
34990 {
34991   tree *pc = NULL, stmt;
34992
34993   if (context != pragma_stmt && context != pragma_compound)
34994     {
34995       cp_parser_error (parser, "expected declaration specifiers");
34996       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34997       return false;
34998     }
34999
35000   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35001     {
35002       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35003       const char *p = IDENTIFIER_POINTER (id);
35004       enum tree_code ccode = ERROR_MARK;
35005
35006       if (strcmp (p, "teams") == 0)
35007         ccode = OMP_TEAMS;
35008       else if (strcmp (p, "parallel") == 0)
35009         ccode = OMP_PARALLEL;
35010       else if (strcmp (p, "simd") == 0)
35011         ccode = OMP_SIMD;
35012       if (ccode != ERROR_MARK)
35013         {
35014           tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35015           char p_name[sizeof ("#pragma omp target teams distribute "
35016                               "parallel for simd")];
35017
35018           cp_lexer_consume_token (parser->lexer);
35019           strcpy (p_name, "#pragma omp target");
35020           if (!flag_openmp)  /* flag_openmp_simd  */
35021             {
35022               tree stmt;
35023               switch (ccode)
35024                 {
35025                 case OMP_TEAMS:
35026                   stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35027                                               OMP_TARGET_CLAUSE_MASK,
35028                                               cclauses, if_p);
35029                   break;
35030                 case OMP_PARALLEL:
35031                   stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35032                                                  OMP_TARGET_CLAUSE_MASK,
35033                                                  cclauses, if_p);
35034                   break;
35035                 case OMP_SIMD:
35036                   stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35037                                              OMP_TARGET_CLAUSE_MASK,
35038                                              cclauses, if_p);
35039                   break;
35040                 default:
35041                   gcc_unreachable ();
35042                 }
35043               return stmt != NULL_TREE;
35044             }
35045           keep_next_level (true);
35046           tree sb = begin_omp_structured_block (), ret;
35047           unsigned save = cp_parser_begin_omp_structured_block (parser);
35048           switch (ccode)
35049             {
35050             case OMP_TEAMS:
35051               ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35052                                          OMP_TARGET_CLAUSE_MASK, cclauses,
35053                                          if_p);
35054               break;
35055             case OMP_PARALLEL:
35056               ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35057                                             OMP_TARGET_CLAUSE_MASK, cclauses,
35058                                             if_p);
35059               break;
35060             case OMP_SIMD:
35061               ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35062                                         OMP_TARGET_CLAUSE_MASK, cclauses,
35063                                         if_p);
35064               break;
35065             default:
35066               gcc_unreachable ();
35067             }
35068           cp_parser_end_omp_structured_block (parser, save);
35069           tree body = finish_omp_structured_block (sb);
35070           if (ret == NULL_TREE)
35071             return false;
35072           if (ccode == OMP_TEAMS && !processing_template_decl)
35073             {
35074               /* For combined target teams, ensure the num_teams and
35075                  thread_limit clause expressions are evaluated on the host,
35076                  before entering the target construct.  */
35077               tree c;
35078               for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35079                    c; c = OMP_CLAUSE_CHAIN (c))
35080                 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35081                      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35082                     && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35083                   {
35084                     tree expr = OMP_CLAUSE_OPERAND (c, 0);
35085                     expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35086                     if (expr == error_mark_node)
35087                       continue;
35088                     tree tmp = TARGET_EXPR_SLOT (expr);
35089                     add_stmt (expr);
35090                     OMP_CLAUSE_OPERAND (c, 0) = expr;
35091                     tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35092                                                 OMP_CLAUSE_FIRSTPRIVATE);
35093                     OMP_CLAUSE_DECL (tc) = tmp;
35094                     OMP_CLAUSE_CHAIN (tc)
35095                       = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35096                     cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35097                   }
35098             }
35099           tree stmt = make_node (OMP_TARGET);
35100           TREE_TYPE (stmt) = void_type_node;
35101           OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35102           OMP_TARGET_BODY (stmt) = body;
35103           OMP_TARGET_COMBINED (stmt) = 1;
35104           add_stmt (stmt);
35105           pc = &OMP_TARGET_CLAUSES (stmt);
35106           goto check_clauses;
35107         }
35108       else if (!flag_openmp)  /* flag_openmp_simd  */
35109         {
35110           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35111           return false;
35112         }
35113       else if (strcmp (p, "data") == 0)
35114         {
35115           cp_lexer_consume_token (parser->lexer);
35116           cp_parser_omp_target_data (parser, pragma_tok, if_p);
35117           return true;
35118         }
35119       else if (strcmp (p, "enter") == 0)
35120         {
35121           cp_lexer_consume_token (parser->lexer);
35122           cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35123           return false;
35124         }
35125       else if (strcmp (p, "exit") == 0)
35126         {
35127           cp_lexer_consume_token (parser->lexer);
35128           cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35129           return false;
35130         }
35131       else if (strcmp (p, "update") == 0)
35132         {
35133           cp_lexer_consume_token (parser->lexer);
35134           return cp_parser_omp_target_update (parser, pragma_tok, context);
35135         }
35136     }
35137
35138   stmt = make_node (OMP_TARGET);
35139   TREE_TYPE (stmt) = void_type_node;
35140
35141   OMP_TARGET_CLAUSES (stmt)
35142     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35143                                  "#pragma omp target", pragma_tok);
35144   pc = &OMP_TARGET_CLAUSES (stmt);
35145   keep_next_level (true);
35146   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35147
35148   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35149   add_stmt (stmt);
35150
35151 check_clauses:
35152   while (*pc)
35153     {
35154       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35155         switch (OMP_CLAUSE_MAP_KIND (*pc))
35156           {
35157           case GOMP_MAP_TO:
35158           case GOMP_MAP_ALWAYS_TO:
35159           case GOMP_MAP_FROM:
35160           case GOMP_MAP_ALWAYS_FROM:
35161           case GOMP_MAP_TOFROM:
35162           case GOMP_MAP_ALWAYS_TOFROM:
35163           case GOMP_MAP_ALLOC:
35164           case GOMP_MAP_FIRSTPRIVATE_POINTER:
35165           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35166           case GOMP_MAP_ALWAYS_POINTER:
35167             break;
35168           default:
35169             error_at (OMP_CLAUSE_LOCATION (*pc),
35170                       "%<#pragma omp target%> with map-type other "
35171                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35172                       "on %<map%> clause");
35173             *pc = OMP_CLAUSE_CHAIN (*pc);
35174             continue;
35175           }
35176       pc = &OMP_CLAUSE_CHAIN (*pc);
35177     }
35178   return true;
35179 }
35180
35181 /* OpenACC 2.0:
35182    # pragma acc cache (variable-list) new-line
35183 */
35184
35185 static tree
35186 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35187 {
35188   tree stmt, clauses;
35189
35190   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35191   clauses = finish_omp_clauses (clauses, false);
35192
35193   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35194
35195   stmt = make_node (OACC_CACHE);
35196   TREE_TYPE (stmt) = void_type_node;
35197   OACC_CACHE_CLAUSES (stmt) = clauses;
35198   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35199   add_stmt (stmt);
35200
35201   return stmt;
35202 }
35203
35204 /* OpenACC 2.0:
35205    # pragma acc data oacc-data-clause[optseq] new-line
35206      structured-block  */
35207
35208 #define OACC_DATA_CLAUSE_MASK                                           \
35209         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35210         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35211         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35212         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35213         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35214         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35215         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35216         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35217         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35218         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35219         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35220
35221 static tree
35222 cp_parser_oacc_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_DATA_CLAUSE_MASK,
35228                                         "#pragma acc 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_data (clauses, block);
35235   return stmt;
35236 }
35237
35238 /* OpenACC 2.0:
35239   # pragma acc host_data <clauses> new-line
35240   structured-block  */
35241
35242 #define OACC_HOST_DATA_CLAUSE_MASK                                      \
35243   ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35244
35245 static tree
35246 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35247 {
35248   tree stmt, clauses, block;
35249   unsigned int save;
35250
35251   clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35252                                         "#pragma acc host_data", pragma_tok);
35253
35254   block = begin_omp_parallel ();
35255   save = cp_parser_begin_omp_structured_block (parser);
35256   cp_parser_statement (parser, NULL_TREE, false, if_p);
35257   cp_parser_end_omp_structured_block (parser, save);
35258   stmt = finish_oacc_host_data (clauses, block);
35259   return stmt;
35260 }
35261
35262 /* OpenACC 2.0:
35263    # pragma acc declare oacc-data-clause[optseq] new-line
35264 */
35265
35266 #define OACC_DECLARE_CLAUSE_MASK                                        \
35267         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35268         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35269         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35270         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35271         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35272         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)     \
35273         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)                \
35274         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35275         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35276         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35277         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35278         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35279
35280 static tree
35281 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35282 {
35283   tree clauses, stmt;
35284   bool error = false;
35285
35286   clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35287                                         "#pragma acc declare", pragma_tok, true);
35288
35289
35290   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35291     {
35292       error_at (pragma_tok->location,
35293                 "no valid clauses specified in %<#pragma acc declare%>");
35294       return NULL_TREE;
35295     }
35296
35297   for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35298     {
35299       location_t loc = OMP_CLAUSE_LOCATION (t);
35300       tree decl = OMP_CLAUSE_DECL (t);
35301       if (!DECL_P (decl))
35302         {
35303           error_at (loc, "array section in %<#pragma acc declare%>");
35304           error = true;
35305           continue;
35306         }
35307       gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35308       switch (OMP_CLAUSE_MAP_KIND (t))
35309         {
35310         case GOMP_MAP_FORCE_ALLOC:
35311         case GOMP_MAP_FORCE_TO:
35312         case GOMP_MAP_FORCE_DEVICEPTR:
35313         case GOMP_MAP_DEVICE_RESIDENT:
35314           break;
35315
35316         case GOMP_MAP_POINTER:
35317           /* Generated by c_finish_omp_clauses from array sections;
35318              avoid spurious diagnostics.  */
35319           break;
35320
35321         case GOMP_MAP_LINK:
35322           if (!global_bindings_p ()
35323               && (TREE_STATIC (decl)
35324                || !DECL_EXTERNAL (decl)))
35325             {
35326               error_at (loc,
35327                         "%qD must be a global variable in"
35328                         "%<#pragma acc declare link%>",
35329                         decl);
35330               error = true;
35331               continue;
35332             }
35333           break;
35334
35335         default:
35336           if (global_bindings_p ())
35337             {
35338               error_at (loc, "invalid OpenACC clause at file scope");
35339               error = true;
35340               continue;
35341             }
35342           if (DECL_EXTERNAL (decl))
35343             {
35344               error_at (loc,
35345                         "invalid use of %<extern%> variable %qD "
35346                         "in %<#pragma acc declare%>", decl);
35347               error = true;
35348               continue;
35349             }
35350           else if (TREE_PUBLIC (decl))
35351             {
35352               error_at (loc,
35353                         "invalid use of %<global%> variable %qD "
35354                         "in %<#pragma acc declare%>", decl);
35355               error = true;
35356               continue;
35357             }
35358           break;
35359         }
35360
35361       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35362           || lookup_attribute ("omp declare target link",
35363                                DECL_ATTRIBUTES (decl)))
35364         {
35365           error_at (loc, "variable %qD used more than once with "
35366                     "%<#pragma acc declare%>", decl);
35367           error = true;
35368           continue;
35369         }
35370
35371       if (!error)
35372         {
35373           tree id;
35374
35375           if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35376             id = get_identifier ("omp declare target link");
35377           else
35378             id = get_identifier ("omp declare target");
35379
35380           DECL_ATTRIBUTES (decl)
35381                            = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35382           if (global_bindings_p ())
35383             {
35384               symtab_node *node = symtab_node::get (decl);
35385               if (node != NULL)
35386                 {
35387                   node->offloadable = 1;
35388                   if (ENABLE_OFFLOADING)
35389                     {
35390                       g->have_offload = true;
35391                       if (is_a <varpool_node *> (node))
35392                         vec_safe_push (offload_vars, decl);
35393                     }
35394                 }
35395             }
35396         }
35397     }
35398
35399   if (error || global_bindings_p ())
35400     return NULL_TREE;
35401
35402   stmt = make_node (OACC_DECLARE);
35403   TREE_TYPE (stmt) = void_type_node;
35404   OACC_DECLARE_CLAUSES (stmt) = clauses;
35405   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35406
35407   add_stmt (stmt);
35408
35409   return NULL_TREE;
35410 }
35411
35412 /* OpenACC 2.0:
35413    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35414
35415    or
35416
35417    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35418
35419    LOC is the location of the #pragma token.
35420 */
35421
35422 #define OACC_ENTER_DATA_CLAUSE_MASK                                     \
35423         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35424         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35425         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35426         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35427         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35428         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
35429         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35430
35431 #define OACC_EXIT_DATA_CLAUSE_MASK                                      \
35432         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35433         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35434         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35435         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE)              \
35436         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35437
35438 static tree
35439 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35440                                 bool enter)
35441 {
35442   tree stmt, clauses;
35443
35444   if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35445      || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35446     {
35447       cp_parser_error (parser, enter
35448                        ? "expected %<data%> in %<#pragma acc enter data%>"
35449                        : "expected %<data%> in %<#pragma acc exit data%>");
35450       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35451       return NULL_TREE;
35452     }
35453
35454   const char *p =
35455     IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35456   if (strcmp (p, "data") != 0)
35457     {
35458       cp_parser_error (parser, "invalid pragma");
35459       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35460       return NULL_TREE;
35461     }
35462
35463   cp_lexer_consume_token (parser->lexer);
35464
35465   if (enter)
35466     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35467                                          "#pragma acc enter data", pragma_tok);
35468   else
35469     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35470                                          "#pragma acc exit data", pragma_tok);
35471
35472   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35473     {
35474       error_at (pragma_tok->location,
35475                 "%<#pragma acc enter data%> has no data movement clause");
35476       return NULL_TREE;
35477     }
35478
35479   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35480   TREE_TYPE (stmt) = void_type_node;
35481   OMP_STANDALONE_CLAUSES (stmt) = clauses;
35482   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35483   add_stmt (stmt);
35484   return stmt;
35485 }
35486
35487 /* OpenACC 2.0:
35488    # pragma acc loop oacc-loop-clause[optseq] new-line
35489      structured-block  */
35490
35491 #define OACC_LOOP_CLAUSE_MASK                                           \
35492         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)            \
35493         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
35494         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
35495         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
35496         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
35497         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
35498         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)                \
35499         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)         \
35500         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)                 \
35501         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35502
35503 static tree
35504 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35505                      omp_clause_mask mask, tree *cclauses, bool *if_p)
35506 {
35507   bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35508
35509   strcat (p_name, " loop");
35510   mask |= OACC_LOOP_CLAUSE_MASK;
35511
35512   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35513                                              cclauses == NULL);
35514   if (cclauses)
35515     {
35516       clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35517       if (*cclauses)
35518         *cclauses = finish_omp_clauses (*cclauses, false);
35519       if (clauses)
35520         clauses = finish_omp_clauses (clauses, false);
35521     }
35522
35523   tree block = begin_omp_structured_block ();
35524   int save = cp_parser_begin_omp_structured_block (parser);
35525   tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35526   cp_parser_end_omp_structured_block (parser, save);
35527   add_stmt (finish_omp_structured_block (block));
35528
35529   return stmt;
35530 }
35531
35532 /* OpenACC 2.0:
35533    # pragma acc kernels oacc-kernels-clause[optseq] new-line
35534      structured-block
35535
35536    or
35537
35538    # pragma acc parallel oacc-parallel-clause[optseq] new-line
35539      structured-block
35540 */
35541
35542 #define OACC_KERNELS_CLAUSE_MASK                                        \
35543         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35544         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35545         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35546         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35547         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35548         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
35549         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35550         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35551         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35552         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35553         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35554         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35555         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
35556         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35557
35558 #define OACC_PARALLEL_CLAUSE_MASK                                       \
35559         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35560         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35561         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35562         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35563         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35564         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
35565         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35566         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)        \
35567         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35568         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
35569         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
35570         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35571         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35572         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35573         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35574         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
35575         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
35576         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
35577         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
35578         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35579
35580 static tree
35581 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35582                                  char *p_name, bool *if_p)
35583 {
35584   omp_clause_mask mask;
35585   enum tree_code code;
35586   switch (cp_parser_pragma_kind (pragma_tok))
35587     {
35588     case PRAGMA_OACC_KERNELS:
35589       strcat (p_name, " kernels");
35590       mask = OACC_KERNELS_CLAUSE_MASK;
35591       code = OACC_KERNELS;
35592       break;
35593     case PRAGMA_OACC_PARALLEL:
35594       strcat (p_name, " parallel");
35595       mask = OACC_PARALLEL_CLAUSE_MASK;
35596       code = OACC_PARALLEL;
35597       break;
35598     default:
35599       gcc_unreachable ();
35600     }
35601
35602   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35603     {
35604       const char *p
35605         = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35606       if (strcmp (p, "loop") == 0)
35607         {
35608           cp_lexer_consume_token (parser->lexer);
35609           tree block = begin_omp_parallel ();
35610           tree clauses;
35611           cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35612                                if_p);
35613           return finish_omp_construct (code, block, clauses);
35614         }
35615     }
35616
35617   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35618
35619   tree block = begin_omp_parallel ();
35620   unsigned int save = cp_parser_begin_omp_structured_block (parser);
35621   cp_parser_statement (parser, NULL_TREE, false, if_p);
35622   cp_parser_end_omp_structured_block (parser, save);
35623   return finish_omp_construct (code, block, clauses);
35624 }
35625
35626 /* OpenACC 2.0:
35627    # pragma acc update oacc-update-clause[optseq] new-line
35628 */
35629
35630 #define OACC_UPDATE_CLAUSE_MASK                                         \
35631         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35632         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)              \
35633         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)                \
35634         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35635         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)                \
35636         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35637
35638 static tree
35639 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35640 {
35641   tree stmt, clauses;
35642
35643   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35644                                          "#pragma acc update", pragma_tok);
35645
35646   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35647     {
35648       error_at (pragma_tok->location,
35649                 "%<#pragma acc update%> must contain at least one "
35650                 "%<device%> or %<host%> or %<self%> clause");
35651       return NULL_TREE;
35652     }
35653
35654   stmt = make_node (OACC_UPDATE);
35655   TREE_TYPE (stmt) = void_type_node;
35656   OACC_UPDATE_CLAUSES (stmt) = clauses;
35657   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35658   add_stmt (stmt);
35659   return stmt;
35660 }
35661
35662 /* OpenACC 2.0:
35663    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35664
35665    LOC is the location of the #pragma token.
35666 */
35667
35668 #define OACC_WAIT_CLAUSE_MASK                                   \
35669         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35670
35671 static tree
35672 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35673 {
35674   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35675   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35676
35677   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35678     list = cp_parser_oacc_wait_list (parser, loc, list);
35679
35680   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35681                                         "#pragma acc wait", pragma_tok);
35682
35683   stmt = c_finish_oacc_wait (loc, list, clauses);
35684   stmt = finish_expr_stmt (stmt);
35685
35686   return stmt;
35687 }
35688
35689 /* OpenMP 4.0:
35690    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
35691
35692 #define OMP_DECLARE_SIMD_CLAUSE_MASK                            \
35693         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
35694         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
35695         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
35696         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)      \
35697         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)     \
35698         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35699
35700 static void
35701 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35702                             enum pragma_context context)
35703 {
35704   bool first_p = parser->omp_declare_simd == NULL;
35705   cp_omp_declare_simd_data data;
35706   if (first_p)
35707     {
35708       data.error_seen = false;
35709       data.fndecl_seen = false;
35710       data.tokens = vNULL;
35711       parser->omp_declare_simd = &data;
35712     }
35713   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35714          && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35715     cp_lexer_consume_token (parser->lexer);
35716   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35717     parser->omp_declare_simd->error_seen = true;
35718   cp_parser_require_pragma_eol (parser, pragma_tok);
35719   struct cp_token_cache *cp
35720     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35721   parser->omp_declare_simd->tokens.safe_push (cp);
35722   if (first_p)
35723     {
35724       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35725         cp_parser_pragma (parser, context, NULL);
35726       switch (context)
35727         {
35728         case pragma_external:
35729           cp_parser_declaration (parser);
35730           break;
35731         case pragma_member:
35732           cp_parser_member_declaration (parser);
35733           break;
35734         case pragma_objc_icode:
35735           cp_parser_block_declaration (parser, /*statement_p=*/false);
35736           break;
35737         default:
35738           cp_parser_declaration_statement (parser);
35739           break;
35740         }
35741       if (parser->omp_declare_simd
35742           && !parser->omp_declare_simd->error_seen
35743           && !parser->omp_declare_simd->fndecl_seen)
35744         error_at (pragma_tok->location,
35745                   "%<#pragma omp declare simd%> not immediately followed by "
35746                   "function declaration or definition");
35747       data.tokens.release ();
35748       parser->omp_declare_simd = NULL;
35749     }
35750 }
35751
35752 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.  
35753    This function is modelled similar to the late parsing of omp declare 
35754    simd.  */
35755
35756 static tree
35757 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35758 {
35759   struct cp_token_cache *ce;
35760   cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35761   int ii = 0;
35762
35763   if (parser->omp_declare_simd != NULL
35764       || lookup_attribute ("simd", attrs))
35765     {
35766       error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35767              "used in the same function marked as a Cilk Plus SIMD-enabled "
35768              " function");
35769       parser->cilk_simd_fn_info->tokens.release ();
35770       XDELETE (parser->cilk_simd_fn_info);
35771       parser->cilk_simd_fn_info = NULL;
35772       return attrs;
35773     }
35774   if (!info->error_seen && info->fndecl_seen)
35775     {
35776       error ("vector attribute not immediately followed by a single function"
35777              " declaration or definition");
35778       info->error_seen = true;
35779     }
35780   if (info->error_seen)
35781     return attrs;
35782
35783   FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35784     {
35785       tree c, cl;
35786
35787       cp_parser_push_lexer_for_tokens (parser, ce);
35788       parser->lexer->in_pragma = true;
35789       cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35790                                       "SIMD-enabled functions attribute", 
35791                                       NULL);
35792       cp_parser_pop_lexer (parser);
35793       if (cl)
35794         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35795
35796       c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35797       TREE_CHAIN (c) = attrs;
35798       attrs = c;
35799
35800       c = build_tree_list (get_identifier ("omp declare simd"), cl);
35801       TREE_CHAIN (c) = attrs;
35802       if (processing_template_decl)
35803         ATTR_IS_DEPENDENT (c) = 1;
35804       attrs = c;
35805     }
35806   info->fndecl_seen = true;
35807   parser->cilk_simd_fn_info->tokens.release ();
35808   XDELETE (parser->cilk_simd_fn_info);
35809   parser->cilk_simd_fn_info = NULL;
35810   return attrs;
35811 }
35812
35813 /* Finalize #pragma omp declare simd clauses after direct declarator has
35814    been parsed, and put that into "omp declare simd" attribute.  */
35815
35816 static tree
35817 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35818 {
35819   struct cp_token_cache *ce;
35820   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35821   int i;
35822
35823   if (!data->error_seen && data->fndecl_seen)
35824     {
35825       error ("%<#pragma omp declare simd%> not immediately followed by "
35826              "a single function declaration or definition");
35827       data->error_seen = true;
35828       return attrs;
35829     }
35830   if (data->error_seen)
35831     return attrs;
35832
35833   FOR_EACH_VEC_ELT (data->tokens, i, ce)
35834     {
35835       tree c, cl;
35836
35837       cp_parser_push_lexer_for_tokens (parser, ce);
35838       parser->lexer->in_pragma = true;
35839       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35840       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35841       cp_lexer_consume_token (parser->lexer);
35842       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35843                                       "#pragma omp declare simd", pragma_tok);
35844       cp_parser_pop_lexer (parser);
35845       if (cl)
35846         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35847       c = build_tree_list (get_identifier ("omp declare simd"), cl);
35848       TREE_CHAIN (c) = attrs;
35849       if (processing_template_decl)
35850         ATTR_IS_DEPENDENT (c) = 1;
35851       attrs = c;
35852     }
35853
35854   data->fndecl_seen = true;
35855   return attrs;
35856 }
35857
35858
35859 /* OpenMP 4.0:
35860    # pragma omp declare target new-line
35861    declarations and definitions
35862    # pragma omp end declare target new-line
35863
35864    OpenMP 4.5:
35865    # pragma omp declare target ( extended-list ) new-line
35866
35867    # pragma omp declare target declare-target-clauses[seq] new-line  */
35868
35869 #define OMP_DECLARE_TARGET_CLAUSE_MASK                          \
35870         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
35871         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35872
35873 static void
35874 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35875 {
35876   tree clauses = NULL_TREE;
35877   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35878     clauses
35879       = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35880                                    "#pragma omp declare target", pragma_tok);
35881   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35882     {
35883       clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35884                                         clauses);
35885       clauses = finish_omp_clauses (clauses, true);
35886       cp_parser_require_pragma_eol (parser, pragma_tok);
35887     }
35888   else
35889     {
35890       cp_parser_require_pragma_eol (parser, pragma_tok);
35891       scope_chain->omp_declare_target_attribute++;
35892       return;
35893     }
35894   if (scope_chain->omp_declare_target_attribute)
35895     error_at (pragma_tok->location,
35896               "%<#pragma omp declare target%> with clauses in between "
35897               "%<#pragma omp declare target%> without clauses and "
35898               "%<#pragma omp end declare target%>");
35899   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35900     {
35901       tree t = OMP_CLAUSE_DECL (c), id;
35902       tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35903       tree at2 = lookup_attribute ("omp declare target link",
35904                                    DECL_ATTRIBUTES (t));
35905       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35906         {
35907           id = get_identifier ("omp declare target link");
35908           std::swap (at1, at2);
35909         }
35910       else
35911         id = get_identifier ("omp declare target");
35912       if (at2)
35913         {
35914           error_at (OMP_CLAUSE_LOCATION (c),
35915                     "%qD specified both in declare target %<link%> and %<to%>"
35916                     " clauses", t);
35917           continue;
35918         }
35919       if (!at1)
35920         {
35921           symtab_node *node = symtab_node::get (t);
35922           DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35923           if (node != NULL)
35924             {
35925               node->offloadable = 1;
35926               if (ENABLE_OFFLOADING)
35927                 {
35928                   g->have_offload = true;
35929                   if (is_a <varpool_node *> (node))
35930                     vec_safe_push (offload_vars, t);
35931                 }
35932             }
35933         }
35934     }
35935 }
35936
35937 static void
35938 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35939 {
35940   const char *p = "";
35941   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35942     {
35943       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35944       p = IDENTIFIER_POINTER (id);
35945     }
35946   if (strcmp (p, "declare") == 0)
35947     {
35948       cp_lexer_consume_token (parser->lexer);
35949       p = "";
35950       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35951         {
35952           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35953           p = IDENTIFIER_POINTER (id);
35954         }
35955       if (strcmp (p, "target") == 0)
35956         cp_lexer_consume_token (parser->lexer);
35957       else
35958         {
35959           cp_parser_error (parser, "expected %<target%>");
35960           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35961           return;
35962         }
35963     }
35964   else
35965     {
35966       cp_parser_error (parser, "expected %<declare%>");
35967       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35968       return;
35969     }
35970   cp_parser_require_pragma_eol (parser, pragma_tok);
35971   if (!scope_chain->omp_declare_target_attribute)
35972     error_at (pragma_tok->location,
35973               "%<#pragma omp end declare target%> without corresponding "
35974               "%<#pragma omp declare target%>");
35975   else
35976     scope_chain->omp_declare_target_attribute--;
35977 }
35978
35979 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
35980    expression and optional initializer clause of
35981    #pragma omp declare reduction.  We store the expression(s) as
35982    either 3, 6 or 7 special statements inside of the artificial function's
35983    body.  The first two statements are DECL_EXPRs for the artificial
35984    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35985    expression that uses those variables.
35986    If there was any INITIALIZER clause, this is followed by further statements,
35987    the fourth and fifth statements are DECL_EXPRs for the artificial
35988    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
35989    constructor variant (first token after open paren is not omp_priv),
35990    then the sixth statement is a statement with the function call expression
35991    that uses the OMP_PRIV and optionally OMP_ORIG variable.
35992    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35993    to initialize the OMP_PRIV artificial variable and there is seventh
35994    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
35995
35996 static bool
35997 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35998 {
35999   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36000   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36001   type = TREE_TYPE (type);
36002   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36003   DECL_ARTIFICIAL (omp_out) = 1;
36004   pushdecl (omp_out);
36005   add_decl_expr (omp_out);
36006   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36007   DECL_ARTIFICIAL (omp_in) = 1;
36008   pushdecl (omp_in);
36009   add_decl_expr (omp_in);
36010   tree combiner;
36011   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36012
36013   keep_next_level (true);
36014   tree block = begin_omp_structured_block ();
36015   combiner = cp_parser_expression (parser);
36016   finish_expr_stmt (combiner);
36017   block = finish_omp_structured_block (block);
36018   add_stmt (block);
36019
36020   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36021     return false;
36022
36023   const char *p = "";
36024   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36025     {
36026       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36027       p = IDENTIFIER_POINTER (id);
36028     }
36029
36030   if (strcmp (p, "initializer") == 0)
36031     {
36032       cp_lexer_consume_token (parser->lexer);
36033       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36034         return false;
36035
36036       p = "";
36037       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36038         {
36039           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36040           p = IDENTIFIER_POINTER (id);
36041         }
36042
36043       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36044       DECL_ARTIFICIAL (omp_priv) = 1;
36045       pushdecl (omp_priv);
36046       add_decl_expr (omp_priv);
36047       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36048       DECL_ARTIFICIAL (omp_orig) = 1;
36049       pushdecl (omp_orig);
36050       add_decl_expr (omp_orig);
36051
36052       keep_next_level (true);
36053       block = begin_omp_structured_block ();
36054
36055       bool ctor = false;
36056       if (strcmp (p, "omp_priv") == 0)
36057         {
36058           bool is_direct_init, is_non_constant_init;
36059           ctor = true;
36060           cp_lexer_consume_token (parser->lexer);
36061           /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
36062           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36063               || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36064                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36065                      == CPP_CLOSE_PAREN
36066                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36067                      == CPP_CLOSE_PAREN))
36068             {
36069               finish_omp_structured_block (block);
36070               error ("invalid initializer clause");
36071               return false;
36072             }
36073           initializer = cp_parser_initializer (parser, &is_direct_init,
36074                                                &is_non_constant_init);
36075           cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36076                           NULL_TREE, LOOKUP_ONLYCONVERTING);
36077         }
36078       else
36079         {
36080           cp_parser_parse_tentatively (parser);
36081           tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36082                                                   /*check_dependency_p=*/true,
36083                                                   /*template_p=*/NULL,
36084                                                   /*declarator_p=*/false,
36085                                                   /*optional_p=*/false);
36086           vec<tree, va_gc> *args;
36087           if (fn_name == error_mark_node
36088               || cp_parser_error_occurred (parser)
36089               || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36090               || ((args = cp_parser_parenthesized_expression_list
36091                                 (parser, non_attr, /*cast_p=*/false,
36092                                  /*allow_expansion_p=*/true,
36093                                  /*non_constant_p=*/NULL)),
36094                   cp_parser_error_occurred (parser)))
36095             {
36096               finish_omp_structured_block (block);
36097               cp_parser_abort_tentative_parse (parser);
36098               cp_parser_error (parser, "expected id-expression (arguments)");
36099               return false;
36100             }
36101           unsigned int i;
36102           tree arg;
36103           FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36104             if (arg == omp_priv
36105                 || (TREE_CODE (arg) == ADDR_EXPR
36106                     && TREE_OPERAND (arg, 0) == omp_priv))
36107               break;
36108           cp_parser_abort_tentative_parse (parser);
36109           if (arg == NULL_TREE)
36110             error ("one of the initializer call arguments should be %<omp_priv%>"
36111                    " or %<&omp_priv%>");
36112           initializer = cp_parser_postfix_expression (parser, false, false, false,
36113                                                       false, NULL);
36114           finish_expr_stmt (initializer);
36115         }
36116
36117       block = finish_omp_structured_block (block);
36118       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36119       add_stmt (block);
36120
36121       if (ctor)
36122         add_decl_expr (omp_orig);
36123
36124       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36125         return false;
36126     }
36127
36128   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36129     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36130
36131   return true;
36132 }
36133
36134 /* OpenMP 4.0
36135    #pragma omp declare reduction (reduction-id : typename-list : expression) \
36136       initializer-clause[opt] new-line
36137
36138    initializer-clause:
36139       initializer (omp_priv initializer)
36140       initializer (function-name (argument-list))  */
36141
36142 static void
36143 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36144                                  enum pragma_context)
36145 {
36146   auto_vec<tree> types;
36147   enum tree_code reduc_code = ERROR_MARK;
36148   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36149   unsigned int i;
36150   cp_token *first_token;
36151   cp_token_cache *cp;
36152   int errs;
36153   void *p;
36154     
36155   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
36156   p = obstack_alloc (&declarator_obstack, 0);
36157
36158   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36159     goto fail;
36160
36161   switch (cp_lexer_peek_token (parser->lexer)->type)
36162     {
36163     case CPP_PLUS:
36164       reduc_code = PLUS_EXPR;
36165       break;
36166     case CPP_MULT:
36167       reduc_code = MULT_EXPR;
36168       break;
36169     case CPP_MINUS:
36170       reduc_code = MINUS_EXPR;
36171       break;
36172     case CPP_AND:
36173       reduc_code = BIT_AND_EXPR;
36174       break;
36175     case CPP_XOR:
36176       reduc_code = BIT_XOR_EXPR;
36177       break;
36178     case CPP_OR:
36179       reduc_code = BIT_IOR_EXPR;
36180       break;
36181     case CPP_AND_AND:
36182       reduc_code = TRUTH_ANDIF_EXPR;
36183       break;
36184     case CPP_OR_OR:
36185       reduc_code = TRUTH_ORIF_EXPR;
36186       break;
36187     case CPP_NAME:
36188       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36189       break;
36190     default:
36191       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36192                                "%<|%>, %<&&%>, %<||%> or identifier");
36193       goto fail;
36194     }
36195
36196   if (reduc_code != ERROR_MARK)
36197     cp_lexer_consume_token (parser->lexer);
36198
36199   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36200   if (reduc_id == error_mark_node)
36201     goto fail;
36202
36203   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36204     goto fail;
36205
36206   /* Types may not be defined in declare reduction type list.  */
36207   const char *saved_message;
36208   saved_message = parser->type_definition_forbidden_message;
36209   parser->type_definition_forbidden_message
36210     = G_("types may not be defined in declare reduction type list");
36211   bool saved_colon_corrects_to_scope_p;
36212   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36213   parser->colon_corrects_to_scope_p = false;
36214   bool saved_colon_doesnt_start_class_def_p;
36215   saved_colon_doesnt_start_class_def_p
36216     = parser->colon_doesnt_start_class_def_p;
36217   parser->colon_doesnt_start_class_def_p = true;
36218
36219   while (true)
36220     {
36221       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36222       type = cp_parser_type_id (parser);
36223       if (type == error_mark_node)
36224         ;
36225       else if (ARITHMETIC_TYPE_P (type)
36226                && (orig_reduc_id == NULL_TREE
36227                    || (TREE_CODE (type) != COMPLEX_TYPE
36228                        && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36229                                    "min") == 0
36230                            || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36231                                       "max") == 0))))
36232         error_at (loc, "predeclared arithmetic type %qT in "
36233                        "%<#pragma omp declare reduction%>", type);
36234       else if (TREE_CODE (type) == FUNCTION_TYPE
36235                || TREE_CODE (type) == METHOD_TYPE
36236                || TREE_CODE (type) == ARRAY_TYPE)
36237         error_at (loc, "function or array type %qT in "
36238                        "%<#pragma omp declare reduction%>", type);
36239       else if (TREE_CODE (type) == REFERENCE_TYPE)
36240         error_at (loc, "reference type %qT in "
36241                        "%<#pragma omp declare reduction%>", type);
36242       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36243         error_at (loc, "const, volatile or __restrict qualified type %qT in "
36244                        "%<#pragma omp declare reduction%>", type);
36245       else
36246         types.safe_push (type);
36247
36248       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36249         cp_lexer_consume_token (parser->lexer);
36250       else
36251         break;
36252     }
36253
36254   /* Restore the saved message.  */
36255   parser->type_definition_forbidden_message = saved_message;
36256   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36257   parser->colon_doesnt_start_class_def_p
36258     = saved_colon_doesnt_start_class_def_p;
36259
36260   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36261       || types.is_empty ())
36262     {
36263      fail:
36264       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36265       goto done;
36266     }
36267
36268   first_token = cp_lexer_peek_token (parser->lexer);
36269   cp = NULL;
36270   errs = errorcount;
36271   FOR_EACH_VEC_ELT (types, i, type)
36272     {
36273       tree fntype
36274         = build_function_type_list (void_type_node,
36275                                     cp_build_reference_type (type, false),
36276                                     NULL_TREE);
36277       tree this_reduc_id = reduc_id;
36278       if (!dependent_type_p (type))
36279         this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36280       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36281       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36282       DECL_ARTIFICIAL (fndecl) = 1;
36283       DECL_EXTERNAL (fndecl) = 1;
36284       DECL_DECLARED_INLINE_P (fndecl) = 1;
36285       DECL_IGNORED_P (fndecl) = 1;
36286       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36287       SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36288       DECL_ATTRIBUTES (fndecl)
36289         = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36290                      DECL_ATTRIBUTES (fndecl));
36291       if (processing_template_decl)
36292         fndecl = push_template_decl (fndecl);
36293       bool block_scope = false;
36294       tree block = NULL_TREE;
36295       if (current_function_decl)
36296         {
36297           block_scope = true;
36298           DECL_CONTEXT (fndecl) = global_namespace;
36299           if (!processing_template_decl)
36300             pushdecl (fndecl);
36301         }
36302       else if (current_class_type)
36303         {
36304           if (cp == NULL)
36305             {
36306               while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36307                      && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36308                 cp_lexer_consume_token (parser->lexer);
36309               if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36310                 goto fail;
36311               cp = cp_token_cache_new (first_token,
36312                                        cp_lexer_peek_nth_token (parser->lexer,
36313                                                                 2));
36314             }
36315           DECL_STATIC_FUNCTION_P (fndecl) = 1;
36316           finish_member_declaration (fndecl);
36317           DECL_PENDING_INLINE_INFO (fndecl) = cp;
36318           DECL_PENDING_INLINE_P (fndecl) = 1;
36319           vec_safe_push (unparsed_funs_with_definitions, fndecl);
36320           continue;
36321         }
36322       else
36323         {
36324           DECL_CONTEXT (fndecl) = current_namespace;
36325           pushdecl (fndecl);
36326         }
36327       if (!block_scope)
36328         start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36329       else
36330         block = begin_omp_structured_block ();
36331       if (cp)
36332         {
36333           cp_parser_push_lexer_for_tokens (parser, cp);
36334           parser->lexer->in_pragma = true;
36335         }
36336       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36337         {
36338           if (!block_scope)
36339             finish_function (0);
36340           else
36341             DECL_CONTEXT (fndecl) = current_function_decl;
36342           if (cp)
36343             cp_parser_pop_lexer (parser);
36344           goto fail;
36345         }
36346       if (cp)
36347         cp_parser_pop_lexer (parser);
36348       if (!block_scope)
36349         finish_function (0);
36350       else
36351         {
36352           DECL_CONTEXT (fndecl) = current_function_decl;
36353           block = finish_omp_structured_block (block);
36354           if (TREE_CODE (block) == BIND_EXPR)
36355             DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36356           else if (TREE_CODE (block) == STATEMENT_LIST)
36357             DECL_SAVED_TREE (fndecl) = block;
36358           if (processing_template_decl)
36359             add_decl_expr (fndecl);
36360         }
36361       cp_check_omp_declare_reduction (fndecl);
36362       if (cp == NULL && types.length () > 1)
36363         cp = cp_token_cache_new (first_token,
36364                                  cp_lexer_peek_nth_token (parser->lexer, 2));
36365       if (errs != errorcount)
36366         break;
36367     }
36368
36369   cp_parser_require_pragma_eol (parser, pragma_tok);
36370
36371  done:
36372   /* Free any declarators allocated.  */
36373   obstack_free (&declarator_obstack, p);
36374 }
36375
36376 /* OpenMP 4.0
36377    #pragma omp declare simd declare-simd-clauses[optseq] new-line
36378    #pragma omp declare reduction (reduction-id : typename-list : expression) \
36379       initializer-clause[opt] new-line
36380    #pragma omp declare target new-line  */
36381
36382 static void
36383 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36384                        enum pragma_context context)
36385 {
36386   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36387     {
36388       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36389       const char *p = IDENTIFIER_POINTER (id);
36390
36391       if (strcmp (p, "simd") == 0)
36392         {
36393           cp_lexer_consume_token (parser->lexer);
36394           cp_parser_omp_declare_simd (parser, pragma_tok,
36395                                       context);
36396           return;
36397         }
36398       cp_ensure_no_omp_declare_simd (parser);
36399       if (strcmp (p, "reduction") == 0)
36400         {
36401           cp_lexer_consume_token (parser->lexer);
36402           cp_parser_omp_declare_reduction (parser, pragma_tok,
36403                                            context);
36404           return;
36405         }
36406       if (!flag_openmp)  /* flag_openmp_simd  */
36407         {
36408           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36409           return;
36410         }
36411       if (strcmp (p, "target") == 0)
36412         {
36413           cp_lexer_consume_token (parser->lexer);
36414           cp_parser_omp_declare_target (parser, pragma_tok);
36415           return;
36416         }
36417     }
36418   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36419                            "or %<target%>");
36420   cp_parser_require_pragma_eol (parser, pragma_tok);
36421 }
36422
36423 /* OpenMP 4.5:
36424    #pragma omp taskloop taskloop-clause[optseq] new-line
36425      for-loop
36426
36427    #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36428      for-loop  */
36429
36430 #define OMP_TASKLOOP_CLAUSE_MASK                                \
36431         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
36432         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
36433         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36434         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
36435         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
36436         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)    \
36437         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)    \
36438         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)     \
36439         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
36440         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36441         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
36442         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
36443         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)      \
36444         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36445
36446 static tree
36447 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36448                         char *p_name, omp_clause_mask mask, tree *cclauses,
36449                         bool *if_p)
36450 {
36451   tree clauses, sb, ret;
36452   unsigned int save;
36453   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36454
36455   strcat (p_name, " taskloop");
36456   mask |= OMP_TASKLOOP_CLAUSE_MASK;
36457
36458   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36459     {
36460       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36461       const char *p = IDENTIFIER_POINTER (id);
36462
36463       if (strcmp (p, "simd") == 0)
36464         {
36465           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36466           if (cclauses == NULL)
36467             cclauses = cclauses_buf;
36468
36469           cp_lexer_consume_token (parser->lexer);
36470           if (!flag_openmp)  /* flag_openmp_simd  */
36471             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36472                                        cclauses, if_p);
36473           sb = begin_omp_structured_block ();
36474           save = cp_parser_begin_omp_structured_block (parser);
36475           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36476                                     cclauses, if_p);
36477           cp_parser_end_omp_structured_block (parser, save);
36478           tree body = finish_omp_structured_block (sb);
36479           if (ret == NULL)
36480             return ret;
36481           ret = make_node (OMP_TASKLOOP);
36482           TREE_TYPE (ret) = void_type_node;
36483           OMP_FOR_BODY (ret) = body;
36484           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36485           SET_EXPR_LOCATION (ret, loc);
36486           add_stmt (ret);
36487           return ret;
36488         }
36489     }
36490   if (!flag_openmp)  /* flag_openmp_simd  */
36491     {
36492       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36493       return NULL_TREE;
36494     }
36495
36496   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36497                                        cclauses == NULL);
36498   if (cclauses)
36499     {
36500       cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36501       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36502     }
36503
36504   sb = begin_omp_structured_block ();
36505   save = cp_parser_begin_omp_structured_block (parser);
36506
36507   ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36508                                 if_p);
36509
36510   cp_parser_end_omp_structured_block (parser, save);
36511   add_stmt (finish_omp_structured_block (sb));
36512
36513   return ret;
36514 }
36515
36516
36517 /* OpenACC 2.0:
36518    # pragma acc routine oacc-routine-clause[optseq] new-line
36519      function-definition
36520
36521    # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36522 */
36523
36524 #define OACC_ROUTINE_CLAUSE_MASK                                        \
36525         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
36526         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
36527         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
36528         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36529
36530
36531 /* Parse the OpenACC routine pragma.  This has an optional '( name )'
36532    component, which must resolve to a declared namespace-scope
36533    function.  The clauses are either processed directly (for a named
36534    function), or defered until the immediatley following declaration
36535    is parsed.  */
36536
36537 static void
36538 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36539                         enum pragma_context context)
36540 {
36541   bool first_p = parser->oacc_routine == NULL;
36542   location_t loc = pragma_tok->location;
36543   cp_omp_declare_simd_data data;
36544   if (first_p)
36545     {
36546       data.error_seen = false;
36547       data.fndecl_seen = false;
36548       data.tokens = vNULL;
36549       data.clauses = NULL_TREE;
36550       parser->oacc_routine = &data;
36551     }
36552
36553   tree decl = NULL_TREE;
36554   /* Create a dummy claue, to record location.  */
36555   tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36556
36557   if (context != pragma_external)
36558     {
36559       cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36560       parser->oacc_routine->error_seen = true;
36561       parser->oacc_routine = NULL;
36562       return;
36563     }
36564
36565   /* Look for optional '( name )'.  */
36566   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36567     {
36568       if (!first_p)
36569         {
36570           while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36571                  && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36572             cp_lexer_consume_token (parser->lexer);
36573           if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36574             parser->oacc_routine->error_seen = true;
36575           cp_parser_require_pragma_eol (parser, pragma_tok);
36576
36577           error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36578                     "%<#pragma acc routine%> not followed by a "
36579                     "function declaration or definition");
36580
36581           parser->oacc_routine->error_seen = true;
36582           return;
36583         }
36584
36585       cp_lexer_consume_token (parser->lexer);
36586       cp_token *token = cp_lexer_peek_token (parser->lexer);
36587
36588       /* We parse the name as an id-expression.  If it resolves to
36589          anything other than a non-overloaded function at namespace
36590          scope, it's an error.  */
36591       tree id = cp_parser_id_expression (parser,
36592                                          /*template_keyword_p=*/false,
36593                                          /*check_dependency_p=*/false,
36594                                          /*template_p=*/NULL,
36595                                          /*declarator_p=*/false,
36596                                          /*optional_p=*/false);
36597       decl = cp_parser_lookup_name_simple (parser, id, token->location);
36598       if (id != error_mark_node && decl == error_mark_node)
36599         cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36600                                      token->location);
36601
36602       if (decl == error_mark_node
36603           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36604         {
36605           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36606           parser->oacc_routine = NULL;
36607           return;
36608         }
36609
36610       /* Build a chain of clauses.  */
36611       parser->lexer->in_pragma = true;
36612       tree clauses = NULL_TREE;
36613       clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36614                                             "#pragma acc routine",
36615                                             cp_lexer_peek_token
36616                                             (parser->lexer));
36617
36618       /* Force clauses to be non-null, by attaching context to it.  */
36619       clauses = tree_cons (c_head, clauses, NULL_TREE);
36620
36621       if (decl && is_overloaded_fn (decl)
36622           && (TREE_CODE (decl) != FUNCTION_DECL
36623               || DECL_FUNCTION_TEMPLATE_P  (decl)))
36624         {
36625           error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36626           parser->oacc_routine = NULL;
36627           return;
36628         }
36629
36630       /* Perhaps we should use the same rule as declarations in different
36631          namespaces?  */
36632       if (!DECL_NAMESPACE_SCOPE_P (decl))
36633         {
36634           error_at (loc, "%<#pragma acc routine%> does not refer to a "
36635                     "namespace scope function");
36636           parser->oacc_routine = NULL;
36637           return;
36638         }
36639
36640       if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36641         {
36642           error_at (loc,
36643                     "%<#pragma acc routine%> does not refer to a function");
36644           parser->oacc_routine = NULL;
36645           return;
36646         }
36647
36648       data.clauses = clauses;
36649
36650       cp_finalize_oacc_routine (parser, decl, false);
36651       data.tokens.release ();
36652       parser->oacc_routine = NULL;
36653     }
36654   else
36655     {
36656       while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36657              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36658         cp_lexer_consume_token (parser->lexer);
36659       if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36660         parser->oacc_routine->error_seen = true;
36661       cp_parser_require_pragma_eol (parser, pragma_tok);
36662
36663       struct cp_token_cache *cp
36664         = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36665       parser->oacc_routine->tokens.safe_push (cp);
36666
36667       if (first_p)
36668         parser->oacc_routine->clauses = c_head;
36669
36670       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36671         cp_parser_pragma (parser, context, NULL);
36672
36673       if (first_p)
36674         {
36675           /* Create an empty list of clauses.  */
36676           parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36677                                                      NULL_TREE);
36678           cp_parser_declaration (parser);
36679
36680           if (parser->oacc_routine
36681               && !parser->oacc_routine->error_seen
36682               && !parser->oacc_routine->fndecl_seen)
36683             error_at (loc, "%<#pragma acc routine%> not followed by a "
36684                       "function declaration or definition");
36685
36686           data.tokens.release ();
36687           parser->oacc_routine = NULL;
36688         }
36689     }
36690 }
36691
36692 /* Finalize #pragma acc routine clauses after direct declarator has
36693    been parsed, and put that into "oacc function" attribute.  */
36694
36695 static tree
36696 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36697 {
36698   struct cp_token_cache *ce;
36699   cp_omp_declare_simd_data *data = parser->oacc_routine;
36700   tree cl, clauses = parser->oacc_routine->clauses;
36701   location_t loc;
36702
36703   loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36704   
36705   if ((!data->error_seen && data->fndecl_seen)
36706       || data->tokens.length () != 1)
36707     {
36708       error_at (loc, "%<#pragma acc routine%> not followed by a "
36709                 "function declaration or definition");
36710       data->error_seen = true;
36711       return attrs;
36712     }
36713   if (data->error_seen)
36714     return attrs;
36715
36716   ce = data->tokens[0];
36717
36718   cp_parser_push_lexer_for_tokens (parser, ce);
36719   parser->lexer->in_pragma = true;
36720   gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36721
36722   cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36723   cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36724                                   "#pragma acc routine", pragma_tok);
36725   cp_parser_pop_lexer (parser);
36726
36727   tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36728
36729   /* Force clauses to be non-null, by attaching context to it.  */
36730   parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36731
36732   data->fndecl_seen = true;
36733   return attrs;
36734 }
36735
36736 /* Apply any saved OpenACC routine clauses to a just-parsed
36737    declaration.  */
36738
36739 static void
36740 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36741 {
36742   if (__builtin_expect (parser->oacc_routine != NULL, 0))
36743     {
36744       tree clauses = parser->oacc_routine->clauses;
36745       location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36746
36747       if (parser->oacc_routine->error_seen)
36748         return;
36749       
36750       if (fndecl == error_mark_node)
36751         {
36752           parser->oacc_routine = NULL;
36753           return;
36754         }
36755
36756       if (TREE_CODE (fndecl) != FUNCTION_DECL)
36757         {
36758           cp_ensure_no_oacc_routine (parser);
36759           return;
36760         }
36761
36762       if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36763         {
36764           error_at (loc,
36765                     "%<#pragma acc routine%> not followed by a function "
36766                     "declaration or definition");
36767           parser->oacc_routine = NULL;
36768         }
36769           
36770       if (get_oacc_fn_attrib (fndecl))
36771         {
36772           error_at (loc, "%<#pragma acc routine%> already applied to %D",
36773                     fndecl);
36774           parser->oacc_routine = NULL;
36775         }
36776
36777       if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36778         {
36779           error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36780                     TREE_USED (fndecl) ? "use" : "definition");
36781           parser->oacc_routine = NULL;
36782         }
36783
36784       /* Process for function attrib  */
36785       tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36786       replace_oacc_fn_attrib (fndecl, dims);
36787       
36788       /* Add an "omp target" attribute.  */
36789       DECL_ATTRIBUTES (fndecl)
36790         = tree_cons (get_identifier ("omp declare target"),
36791                      NULL_TREE, DECL_ATTRIBUTES (fndecl));
36792     }
36793 }
36794
36795 /* Main entry point to OpenMP statement pragmas.  */
36796
36797 static void
36798 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36799 {
36800   tree stmt;
36801   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36802   omp_clause_mask mask (0);
36803
36804   switch (cp_parser_pragma_kind (pragma_tok))
36805     {
36806     case PRAGMA_OACC_ATOMIC:
36807       cp_parser_omp_atomic (parser, pragma_tok);
36808       return;
36809     case PRAGMA_OACC_CACHE:
36810       stmt = cp_parser_oacc_cache (parser, pragma_tok);
36811       break;
36812     case PRAGMA_OACC_DATA:
36813       stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36814       break;
36815     case PRAGMA_OACC_ENTER_DATA:
36816       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36817       break;
36818     case PRAGMA_OACC_EXIT_DATA:
36819       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36820       break;
36821     case PRAGMA_OACC_HOST_DATA:
36822       stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36823       break;
36824     case PRAGMA_OACC_KERNELS:
36825     case PRAGMA_OACC_PARALLEL:
36826       strcpy (p_name, "#pragma acc");
36827       stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36828                                               if_p);
36829       break;
36830     case PRAGMA_OACC_LOOP:
36831       strcpy (p_name, "#pragma acc");
36832       stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36833                                   if_p);
36834       break;
36835     case PRAGMA_OACC_UPDATE:
36836       stmt = cp_parser_oacc_update (parser, pragma_tok);
36837       break;
36838     case PRAGMA_OACC_WAIT:
36839       stmt = cp_parser_oacc_wait (parser, pragma_tok);
36840       break;
36841     case PRAGMA_OMP_ATOMIC:
36842       cp_parser_omp_atomic (parser, pragma_tok);
36843       return;
36844     case PRAGMA_OMP_CRITICAL:
36845       stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36846       break;
36847     case PRAGMA_OMP_DISTRIBUTE:
36848       strcpy (p_name, "#pragma omp");
36849       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36850                                        if_p);
36851       break;
36852     case PRAGMA_OMP_FOR:
36853       strcpy (p_name, "#pragma omp");
36854       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36855                                 if_p);
36856       break;
36857     case PRAGMA_OMP_MASTER:
36858       stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36859       break;
36860     case PRAGMA_OMP_PARALLEL:
36861       strcpy (p_name, "#pragma omp");
36862       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36863                                      if_p);
36864       break;
36865     case PRAGMA_OMP_SECTIONS:
36866       strcpy (p_name, "#pragma omp");
36867       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36868       break;
36869     case PRAGMA_OMP_SIMD:
36870       strcpy (p_name, "#pragma omp");
36871       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36872                                  if_p);
36873       break;
36874     case PRAGMA_OMP_SINGLE:
36875       stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36876       break;
36877     case PRAGMA_OMP_TASK:
36878       stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36879       break;
36880     case PRAGMA_OMP_TASKGROUP:
36881       stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36882       break;
36883     case PRAGMA_OMP_TASKLOOP:
36884       strcpy (p_name, "#pragma omp");
36885       stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36886                                      if_p);
36887       break;
36888     case PRAGMA_OMP_TEAMS:
36889       strcpy (p_name, "#pragma omp");
36890       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36891                                   if_p);
36892       break;
36893     default:
36894       gcc_unreachable ();
36895     }
36896
36897   protected_set_expr_location (stmt, pragma_tok->location);
36898 }
36899 \f
36900 /* Transactional Memory parsing routines.  */
36901
36902 /* Parse a transaction attribute.
36903
36904    txn-attribute:
36905         attribute
36906         [ [ identifier ] ]
36907
36908    We use this instead of cp_parser_attributes_opt for transactions to avoid
36909    the pedwarn in C++98 mode.  */
36910
36911 static tree
36912 cp_parser_txn_attribute_opt (cp_parser *parser)
36913 {
36914   cp_token *token;
36915   tree attr_name, attr = NULL;
36916
36917   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36918     return cp_parser_attributes_opt (parser);
36919
36920   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36921     return NULL_TREE;
36922   cp_lexer_consume_token (parser->lexer);
36923   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36924     goto error1;
36925
36926   token = cp_lexer_peek_token (parser->lexer);
36927   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36928     {
36929       token = cp_lexer_consume_token (parser->lexer);
36930
36931       attr_name = (token->type == CPP_KEYWORD
36932                    /* For keywords, use the canonical spelling,
36933                       not the parsed identifier.  */
36934                    ? ridpointers[(int) token->keyword]
36935                    : token->u.value);
36936       attr = build_tree_list (attr_name, NULL_TREE);
36937     }
36938   else
36939     cp_parser_error (parser, "expected identifier");
36940
36941   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36942  error1:
36943   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36944   return attr;
36945 }
36946
36947 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36948
36949    transaction-statement:
36950      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36951        compound-statement
36952      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36953 */
36954
36955 static tree
36956 cp_parser_transaction (cp_parser *parser, cp_token *token)
36957 {
36958   unsigned char old_in = parser->in_transaction;
36959   unsigned char this_in = 1, new_in;
36960   enum rid keyword = token->keyword;
36961   tree stmt, attrs, noex;
36962
36963   cp_lexer_consume_token (parser->lexer);
36964
36965   if (keyword == RID_TRANSACTION_RELAXED
36966       || keyword == RID_SYNCHRONIZED)
36967     this_in |= TM_STMT_ATTR_RELAXED;
36968   else
36969     {
36970       attrs = cp_parser_txn_attribute_opt (parser);
36971       if (attrs)
36972         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36973     }
36974
36975   /* Parse a noexcept specification.  */
36976   if (keyword == RID_ATOMIC_NOEXCEPT)
36977     noex = boolean_true_node;
36978   else if (keyword == RID_ATOMIC_CANCEL)
36979     {
36980       /* cancel-and-throw is unimplemented.  */
36981       sorry ("atomic_cancel");
36982       noex = NULL_TREE;
36983     }
36984   else
36985     noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36986
36987   /* Keep track if we're in the lexical scope of an outer transaction.  */
36988   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36989
36990   stmt = begin_transaction_stmt (token->location, NULL, this_in);
36991
36992   parser->in_transaction = new_in;
36993   cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36994   parser->in_transaction = old_in;
36995
36996   finish_transaction_stmt (stmt, NULL, this_in, noex);
36997
36998   return stmt;
36999 }
37000
37001 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37002
37003    transaction-expression:
37004      __transaction_atomic txn-noexcept-spec[opt] ( expression )
37005      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37006 */
37007
37008 static tree
37009 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37010 {
37011   unsigned char old_in = parser->in_transaction;
37012   unsigned char this_in = 1;
37013   cp_token *token;
37014   tree expr, noex;
37015   bool noex_expr;
37016   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37017
37018   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37019       || keyword == RID_TRANSACTION_RELAXED);
37020
37021   if (!flag_tm)
37022     error_at (loc,
37023               keyword == RID_TRANSACTION_RELAXED
37024               ? G_("%<__transaction_relaxed%> without transactional memory "
37025                   "support enabled")
37026               : G_("%<__transaction_atomic%> without transactional memory "
37027                    "support enabled"));
37028
37029   token = cp_parser_require_keyword (parser, keyword,
37030       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37031           : RT_TRANSACTION_RELAXED));
37032   gcc_assert (token != NULL);
37033
37034   if (keyword == RID_TRANSACTION_RELAXED)
37035     this_in |= TM_STMT_ATTR_RELAXED;
37036
37037   /* Set this early.  This might mean that we allow transaction_cancel in
37038      an expression that we find out later actually has to be a constexpr.
37039      However, we expect that cxx_constant_value will be able to deal with
37040      this; also, if the noexcept has no constexpr, then what we parse next
37041      really is a transaction's body.  */
37042   parser->in_transaction = this_in;
37043
37044   /* Parse a noexcept specification.  */
37045   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37046                                                true);
37047
37048   if (!noex || !noex_expr
37049       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37050     {
37051       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37052
37053       expr = cp_parser_expression (parser);
37054       expr = finish_parenthesized_expr (expr);
37055
37056       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37057     }
37058   else
37059     {
37060       /* The only expression that is available got parsed for the noexcept
37061          already.  noexcept is true then.  */
37062       expr = noex;
37063       noex = boolean_true_node;
37064     }
37065
37066   expr = build_transaction_expr (token->location, expr, this_in, noex);
37067   parser->in_transaction = old_in;
37068
37069   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37070     return error_mark_node;
37071
37072   return (flag_tm ? expr : error_mark_node);
37073 }
37074
37075 /* Parse a function-transaction-block.
37076
37077    function-transaction-block:
37078      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37079          function-body
37080      __transaction_atomic txn-attribute[opt] function-try-block
37081      __transaction_relaxed ctor-initializer[opt] function-body
37082      __transaction_relaxed function-try-block
37083 */
37084
37085 static bool
37086 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37087 {
37088   unsigned char old_in = parser->in_transaction;
37089   unsigned char new_in = 1;
37090   tree compound_stmt, stmt, attrs;
37091   bool ctor_initializer_p;
37092   cp_token *token;
37093
37094   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37095       || keyword == RID_TRANSACTION_RELAXED);
37096   token = cp_parser_require_keyword (parser, keyword,
37097       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37098           : RT_TRANSACTION_RELAXED));
37099   gcc_assert (token != NULL);
37100
37101   if (keyword == RID_TRANSACTION_RELAXED)
37102     new_in |= TM_STMT_ATTR_RELAXED;
37103   else
37104     {
37105       attrs = cp_parser_txn_attribute_opt (parser);
37106       if (attrs)
37107         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37108     }
37109
37110   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37111
37112   parser->in_transaction = new_in;
37113
37114   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37115     ctor_initializer_p = cp_parser_function_try_block (parser);
37116   else
37117     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37118       (parser, /*in_function_try_block=*/false);
37119
37120   parser->in_transaction = old_in;
37121
37122   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37123
37124   return ctor_initializer_p;
37125 }
37126
37127 /* Parse a __transaction_cancel statement.
37128
37129    cancel-statement:
37130      __transaction_cancel txn-attribute[opt] ;
37131      __transaction_cancel txn-attribute[opt] throw-expression ;
37132
37133    ??? Cancel and throw is not yet implemented.  */
37134
37135 static tree
37136 cp_parser_transaction_cancel (cp_parser *parser)
37137 {
37138   cp_token *token;
37139   bool is_outer = false;
37140   tree stmt, attrs;
37141
37142   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37143                                      RT_TRANSACTION_CANCEL);
37144   gcc_assert (token != NULL);
37145
37146   attrs = cp_parser_txn_attribute_opt (parser);
37147   if (attrs)
37148     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37149
37150   /* ??? Parse cancel-and-throw here.  */
37151
37152   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37153
37154   if (!flag_tm)
37155     {
37156       error_at (token->location, "%<__transaction_cancel%> without "
37157                 "transactional memory support enabled");
37158       return error_mark_node;
37159     }
37160   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37161     {
37162       error_at (token->location, "%<__transaction_cancel%> within a "
37163                 "%<__transaction_relaxed%>");
37164       return error_mark_node;
37165     }
37166   else if (is_outer)
37167     {
37168       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37169           && !is_tm_may_cancel_outer (current_function_decl))
37170         {
37171           error_at (token->location, "outer %<__transaction_cancel%> not "
37172                     "within outer %<__transaction_atomic%>");
37173           error_at (token->location,
37174                     "  or a %<transaction_may_cancel_outer%> function");
37175           return error_mark_node;
37176         }
37177     }
37178   else if (parser->in_transaction == 0)
37179     {
37180       error_at (token->location, "%<__transaction_cancel%> not within "
37181                 "%<__transaction_atomic%>");
37182       return error_mark_node;
37183     }
37184
37185   stmt = build_tm_abort_call (token->location, is_outer);
37186   add_stmt (stmt);
37187
37188   return stmt;
37189 }
37190 \f
37191 /* The parser.  */
37192
37193 static GTY (()) cp_parser *the_parser;
37194
37195 \f
37196 /* Special handling for the first token or line in the file.  The first
37197    thing in the file might be #pragma GCC pch_preprocess, which loads a
37198    PCH file, which is a GC collection point.  So we need to handle this
37199    first pragma without benefit of an existing lexer structure.
37200
37201    Always returns one token to the caller in *FIRST_TOKEN.  This is
37202    either the true first token of the file, or the first token after
37203    the initial pragma.  */
37204
37205 static void
37206 cp_parser_initial_pragma (cp_token *first_token)
37207 {
37208   tree name = NULL;
37209
37210   cp_lexer_get_preprocessor_token (NULL, first_token);
37211   if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37212     return;
37213
37214   cp_lexer_get_preprocessor_token (NULL, first_token);
37215   if (first_token->type == CPP_STRING)
37216     {
37217       name = first_token->u.value;
37218
37219       cp_lexer_get_preprocessor_token (NULL, first_token);
37220       if (first_token->type != CPP_PRAGMA_EOL)
37221         error_at (first_token->location,
37222                   "junk at end of %<#pragma GCC pch_preprocess%>");
37223     }
37224   else
37225     error_at (first_token->location, "expected string literal");
37226
37227   /* Skip to the end of the pragma.  */
37228   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37229     cp_lexer_get_preprocessor_token (NULL, first_token);
37230
37231   /* Now actually load the PCH file.  */
37232   if (name)
37233     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37234
37235   /* Read one more token to return to our caller.  We have to do this
37236      after reading the PCH file in, since its pointers have to be
37237      live.  */
37238   cp_lexer_get_preprocessor_token (NULL, first_token);
37239 }
37240
37241 /* Parses the grainsize pragma for the _Cilk_for statement.
37242    Syntax:
37243    #pragma cilk grainsize = <VALUE>.  */
37244
37245 static void
37246 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37247 {
37248   if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37249     {
37250       tree exp = cp_parser_binary_expression (parser, false, false,
37251                                               PREC_NOT_OPERATOR, NULL);
37252       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37253       if (!exp || exp == error_mark_node)
37254         {
37255           error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37256           return;
37257         }
37258
37259       /* Make sure the next token is _Cilk_for, it is invalid otherwise.  */
37260       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37261         cp_parser_cilk_for (parser, exp, if_p);
37262       else
37263         warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37264                     "%<#pragma cilk grainsize%> is not followed by "
37265                     "%<_Cilk_for%>");
37266       return;
37267     }
37268   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37269 }
37270
37271 /* Normal parsing of a pragma token.  Here we can (and must) use the
37272    regular lexer.  */
37273
37274 static bool
37275 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37276 {
37277   cp_token *pragma_tok;
37278   unsigned int id;
37279   tree stmt;
37280   bool ret;
37281
37282   pragma_tok = cp_lexer_consume_token (parser->lexer);
37283   gcc_assert (pragma_tok->type == CPP_PRAGMA);
37284   parser->lexer->in_pragma = true;
37285
37286   id = cp_parser_pragma_kind (pragma_tok);
37287   if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37288     cp_ensure_no_omp_declare_simd (parser);
37289   switch (id)
37290     {
37291     case PRAGMA_GCC_PCH_PREPROCESS:
37292       error_at (pragma_tok->location,
37293                 "%<#pragma GCC pch_preprocess%> must be first");
37294       break;
37295
37296     case PRAGMA_OMP_BARRIER:
37297       switch (context)
37298         {
37299         case pragma_compound:
37300           cp_parser_omp_barrier (parser, pragma_tok);
37301           return false;
37302         case pragma_stmt:
37303           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37304                     "used in compound statements");
37305           break;
37306         default:
37307           goto bad_stmt;
37308         }
37309       break;
37310
37311     case PRAGMA_OMP_FLUSH:
37312       switch (context)
37313         {
37314         case pragma_compound:
37315           cp_parser_omp_flush (parser, pragma_tok);
37316           return false;
37317         case pragma_stmt:
37318           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37319                     "used in compound statements");
37320           break;
37321         default:
37322           goto bad_stmt;
37323         }
37324       break;
37325
37326     case PRAGMA_OMP_TASKWAIT:
37327       switch (context)
37328         {
37329         case pragma_compound:
37330           cp_parser_omp_taskwait (parser, pragma_tok);
37331           return false;
37332         case pragma_stmt:
37333           error_at (pragma_tok->location,
37334                     "%<#pragma omp taskwait%> may only be "
37335                     "used in compound statements");
37336           break;
37337         default:
37338           goto bad_stmt;
37339         }
37340       break;
37341
37342     case PRAGMA_OMP_TASKYIELD:
37343       switch (context)
37344         {
37345         case pragma_compound:
37346           cp_parser_omp_taskyield (parser, pragma_tok);
37347           return false;
37348         case pragma_stmt:
37349           error_at (pragma_tok->location,
37350                     "%<#pragma omp taskyield%> may only be "
37351                     "used in compound statements");
37352           break;
37353         default:
37354           goto bad_stmt;
37355         }
37356       break;
37357
37358     case PRAGMA_OMP_CANCEL:
37359       switch (context)
37360         {
37361         case pragma_compound:
37362           cp_parser_omp_cancel (parser, pragma_tok);
37363           return false;
37364         case pragma_stmt:
37365           error_at (pragma_tok->location,
37366                     "%<#pragma omp cancel%> may only be "
37367                     "used in compound statements");
37368           break;
37369         default:
37370           goto bad_stmt;
37371         }
37372       break;
37373
37374     case PRAGMA_OMP_CANCELLATION_POINT:
37375       switch (context)
37376         {
37377         case pragma_compound:
37378           cp_parser_omp_cancellation_point (parser, pragma_tok);
37379           return false;
37380         case pragma_stmt:
37381           error_at (pragma_tok->location,
37382                     "%<#pragma omp cancellation point%> may only be "
37383                     "used in compound statements");
37384           break;
37385         default:
37386           goto bad_stmt;
37387         }
37388       break;
37389
37390     case PRAGMA_OMP_THREADPRIVATE:
37391       cp_parser_omp_threadprivate (parser, pragma_tok);
37392       return false;
37393
37394     case PRAGMA_OMP_DECLARE_REDUCTION:
37395       cp_parser_omp_declare (parser, pragma_tok, context);
37396       return false;
37397
37398     case PRAGMA_OACC_DECLARE:
37399       cp_parser_oacc_declare (parser, pragma_tok);
37400       return false;
37401
37402     case PRAGMA_OACC_ROUTINE:
37403       cp_parser_oacc_routine (parser, pragma_tok, context);
37404       return false;
37405
37406     case PRAGMA_OACC_ATOMIC:
37407     case PRAGMA_OACC_CACHE:
37408     case PRAGMA_OACC_DATA:
37409     case PRAGMA_OACC_ENTER_DATA:
37410     case PRAGMA_OACC_EXIT_DATA:
37411     case PRAGMA_OACC_HOST_DATA:
37412     case PRAGMA_OACC_KERNELS:
37413     case PRAGMA_OACC_PARALLEL:
37414     case PRAGMA_OACC_LOOP:
37415     case PRAGMA_OACC_UPDATE:
37416     case PRAGMA_OACC_WAIT:
37417     case PRAGMA_OMP_ATOMIC:
37418     case PRAGMA_OMP_CRITICAL:
37419     case PRAGMA_OMP_DISTRIBUTE:
37420     case PRAGMA_OMP_FOR:
37421     case PRAGMA_OMP_MASTER:
37422     case PRAGMA_OMP_PARALLEL:
37423     case PRAGMA_OMP_SECTIONS:
37424     case PRAGMA_OMP_SIMD:
37425     case PRAGMA_OMP_SINGLE:
37426     case PRAGMA_OMP_TASK:
37427     case PRAGMA_OMP_TASKGROUP:
37428     case PRAGMA_OMP_TASKLOOP:
37429     case PRAGMA_OMP_TEAMS:
37430       if (context != pragma_stmt && context != pragma_compound)
37431         goto bad_stmt;
37432       stmt = push_omp_privatization_clauses (false);
37433       cp_parser_omp_construct (parser, pragma_tok, if_p);
37434       pop_omp_privatization_clauses (stmt);
37435       return true;
37436
37437     case PRAGMA_OMP_ORDERED:
37438       stmt = push_omp_privatization_clauses (false);
37439       ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37440       pop_omp_privatization_clauses (stmt);
37441       return ret;
37442
37443     case PRAGMA_OMP_TARGET:
37444       stmt = push_omp_privatization_clauses (false);
37445       ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37446       pop_omp_privatization_clauses (stmt);
37447       return ret;
37448
37449     case PRAGMA_OMP_END_DECLARE_TARGET:
37450       cp_parser_omp_end_declare_target (parser, pragma_tok);
37451       return false;
37452
37453     case PRAGMA_OMP_SECTION:
37454       error_at (pragma_tok->location, 
37455                 "%<#pragma omp section%> may only be used in "
37456                 "%<#pragma omp sections%> construct");
37457       break;
37458
37459     case PRAGMA_IVDEP:
37460       {
37461         if (context == pragma_external)
37462           {
37463             error_at (pragma_tok->location,
37464                       "%<#pragma GCC ivdep%> must be inside a function");
37465             break;
37466           }
37467         cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37468         cp_token *tok;
37469         tok = cp_lexer_peek_token (the_parser->lexer);
37470         if (tok->type != CPP_KEYWORD
37471             || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37472                 && tok->keyword != RID_DO))
37473           {
37474             cp_parser_error (parser, "for, while or do statement expected");
37475             return false;
37476           }
37477         cp_parser_iteration_statement (parser, if_p, true);
37478         return true;
37479       }
37480
37481     case PRAGMA_CILK_SIMD:
37482       if (context == pragma_external)
37483         {
37484           error_at (pragma_tok->location,
37485                     "%<#pragma simd%> must be inside a function");
37486           break;
37487         }
37488       stmt = push_omp_privatization_clauses (false);
37489       cp_parser_cilk_simd (parser, pragma_tok, if_p);
37490       pop_omp_privatization_clauses (stmt);
37491       return true;
37492
37493     case PRAGMA_CILK_GRAINSIZE:
37494       if (context == pragma_external)
37495         {
37496           error_at (pragma_tok->location,
37497                     "%<#pragma cilk grainsize%> must be inside a function");
37498           break;
37499         }
37500
37501       /* Ignore the pragma if Cilk Plus is not enabled.  */
37502       if (flag_cilkplus)
37503         {
37504           cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37505           return true;
37506         }
37507       else
37508         {
37509           error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37510                     "%<#pragma cilk grainsize%>");
37511           break;
37512         }
37513
37514     default:
37515       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37516       c_invoke_pragma_handler (id);
37517       break;
37518
37519     bad_stmt:
37520       cp_parser_error (parser, "expected declaration specifiers");
37521       break;
37522     }
37523
37524   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37525   return false;
37526 }
37527
37528 /* The interface the pragma parsers have to the lexer.  */
37529
37530 enum cpp_ttype
37531 pragma_lex (tree *value, location_t *loc)
37532 {
37533   cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37534   enum cpp_ttype ret = tok->type;
37535
37536   *value = tok->u.value;
37537   if (loc)
37538     *loc = tok->location;
37539
37540   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37541     ret = CPP_EOF;
37542   else if (ret == CPP_STRING)
37543     *value = cp_parser_string_literal (the_parser, false, false);
37544   else
37545     {
37546       if (ret == CPP_KEYWORD)
37547         ret = CPP_NAME;
37548       cp_lexer_consume_token (the_parser->lexer);
37549     }
37550
37551   return ret;
37552 }
37553
37554 \f
37555 /* External interface.  */
37556
37557 /* Parse one entire translation unit.  */
37558
37559 void
37560 c_parse_file (void)
37561 {
37562   static bool already_called = false;
37563
37564   if (already_called)
37565     fatal_error (input_location,
37566                  "inter-module optimizations not implemented for C++");
37567   already_called = true;
37568
37569   the_parser = cp_parser_new ();
37570   push_deferring_access_checks (flag_access_control
37571                                 ? dk_no_deferred : dk_no_check);
37572   cp_parser_translation_unit (the_parser);
37573   the_parser = NULL;
37574 }
37575
37576 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's 
37577    vectorlength clause:
37578    Syntax:
37579    vectorlength ( constant-expression )  */
37580
37581 static tree
37582 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37583                                   bool is_simd_fn)
37584 {
37585   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37586   tree expr;
37587   /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37588      safelen clause.  Thus, vectorlength is represented as OMP 4.0
37589      safelen.  For SIMD-enabled function it is represented by OMP 4.0
37590      simdlen.  */
37591   if (!is_simd_fn)
37592     check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", 
37593                                loc);
37594   else
37595     check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37596                                loc);
37597
37598   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37599     return error_mark_node;
37600
37601   expr = cp_parser_constant_expression (parser);
37602   expr = maybe_constant_value (expr);
37603
37604   /* If expr == error_mark_node, then don't emit any errors nor
37605      create a clause.  if any of the above functions returns
37606      error mark node then they would have emitted an error message.  */
37607   if (expr == error_mark_node)
37608     ;
37609   else if (!TREE_TYPE (expr)
37610            || !TREE_CONSTANT (expr)
37611            || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37612     error_at (loc, "vectorlength must be an integer constant");
37613   else if (TREE_CONSTANT (expr)
37614            && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37615     error_at (loc, "vectorlength must be a power of 2");
37616   else 
37617     {
37618       tree c;
37619       if (!is_simd_fn)
37620         { 
37621           c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN); 
37622           OMP_CLAUSE_SAFELEN_EXPR (c) = expr; 
37623           OMP_CLAUSE_CHAIN (c) = clauses; 
37624           clauses = c;
37625         }
37626       else
37627         {
37628           c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37629           OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37630           OMP_CLAUSE_CHAIN (c) = clauses;
37631           clauses = c;
37632         }
37633     }
37634
37635   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37636     return error_mark_node;
37637   return clauses;
37638 }
37639
37640 /* Handles the Cilk Plus #pragma simd linear clause.
37641    Syntax:
37642    linear ( simd-linear-variable-list )
37643
37644    simd-linear-variable-list:
37645      simd-linear-variable
37646      simd-linear-variable-list , simd-linear-variable
37647
37648    simd-linear-variable:
37649      id-expression
37650      id-expression : simd-linear-step
37651
37652    simd-linear-step:
37653    conditional-expression */
37654
37655 static tree
37656 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37657 {
37658   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37659
37660   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37661     return clauses;
37662   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37663     {
37664       cp_parser_error (parser, "expected identifier");
37665       cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37666       return error_mark_node;
37667     }
37668
37669   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37670   parser->colon_corrects_to_scope_p = false;
37671   while (1)
37672     {
37673       cp_token *token = cp_lexer_peek_token (parser->lexer);
37674       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37675         {
37676           cp_parser_error (parser, "expected variable-name");
37677           clauses = error_mark_node;
37678           break;
37679         }
37680
37681       tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37682                                                false, false);
37683       tree decl = cp_parser_lookup_name_simple (parser, var_name,
37684                                                 token->location);
37685       if (decl == error_mark_node)
37686         {
37687           cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37688                                        token->location);
37689           clauses = error_mark_node;
37690         }
37691       else
37692         {
37693           tree e = NULL_TREE;
37694           tree step_size = integer_one_node;
37695
37696           /* If present, parse the linear step.  Otherwise, assume the default
37697              value of 1.  */
37698           if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37699             {
37700               cp_lexer_consume_token (parser->lexer);
37701
37702               e = cp_parser_assignment_expression (parser);
37703               e = maybe_constant_value (e);
37704
37705               if (e == error_mark_node)
37706                 {
37707                   /* If an error has occurred,  then the whole pragma is
37708                      considered ill-formed.  Thus, no reason to keep
37709                      parsing.  */
37710                   clauses = error_mark_node;
37711                   break;
37712                 }
37713               else if (type_dependent_expression_p (e)
37714                        || value_dependent_expression_p (e)
37715                        || (TREE_TYPE (e)
37716                            && INTEGRAL_TYPE_P (TREE_TYPE (e))
37717                            && (TREE_CONSTANT (e)
37718                                || DECL_P (e))))
37719                 step_size = e;
37720               else
37721                 cp_parser_error (parser,
37722                                  "step size must be an integer constant "
37723                                  "expression or an integer variable");
37724             }
37725
37726           /* Use the OMP_CLAUSE_LINEAR,  which has the same semantics.  */
37727           tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37728           OMP_CLAUSE_DECL (l) = decl;
37729           OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37730           OMP_CLAUSE_CHAIN (l) = clauses;
37731           clauses = l;
37732         }
37733       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37734         cp_lexer_consume_token (parser->lexer);
37735       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37736         break;
37737       else
37738         {
37739           error_at (cp_lexer_peek_token (parser->lexer)->location,
37740                     "expected %<,%> or %<)%> after %qE", decl);
37741           clauses = error_mark_node;
37742           break;
37743         }
37744     }
37745   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37746   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37747   return clauses;
37748 }
37749
37750 /* Returns the name of the next clause.  If the clause is not
37751    recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37752    token is not consumed.  Otherwise, the appropriate enum from the
37753    pragma_simd_clause is returned and the token is consumed.  */
37754
37755 static pragma_omp_clause
37756 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37757 {
37758   pragma_omp_clause clause_type;
37759   cp_token *token = cp_lexer_peek_token (parser->lexer);
37760
37761   if (token->keyword == RID_PRIVATE)
37762     clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37763   else if (!token->u.value || token->type != CPP_NAME)
37764     return PRAGMA_CILK_CLAUSE_NONE;
37765   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37766     clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37767   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37768     clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37769   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37770     clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37771   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37772     clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37773   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37774     clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37775   else
37776     return PRAGMA_CILK_CLAUSE_NONE;
37777
37778   cp_lexer_consume_token (parser->lexer);
37779   return clause_type;
37780 }
37781
37782 /* Parses all the #pragma simd clauses.  Returns a list of clauses found.  */
37783
37784 static tree
37785 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37786 {
37787   tree clauses = NULL_TREE;
37788
37789   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37790          && clauses != error_mark_node)
37791     {
37792       pragma_omp_clause c_kind;
37793       c_kind = cp_parser_cilk_simd_clause_name (parser);
37794       if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37795         clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37796       else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37797         clauses = cp_parser_cilk_simd_linear (parser, clauses);
37798       else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37799         /* Use the OpenMP 4.0 equivalent function.  */
37800         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37801       else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37802         /* Use the OpenMP 4.0 equivalent function.  */
37803         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37804                                           clauses);
37805       else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37806         /* Use the OMP 4.0 equivalent function.  */
37807         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37808                                           clauses);
37809       else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37810         /* Use the OMP 4.0 equivalent function.  */
37811         clauses = cp_parser_omp_clause_reduction (parser, clauses);
37812       else
37813         {
37814           clauses = error_mark_node;
37815           cp_parser_error (parser, "expected %<#pragma simd%> clause");
37816           break;
37817         }
37818     }
37819
37820   cp_parser_skip_to_pragma_eol (parser, pragma_token);
37821
37822   if (clauses == error_mark_node)
37823     return error_mark_node;
37824   else
37825     return c_finish_cilk_clauses (clauses);
37826 }
37827
37828 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops.  */
37829
37830 static void
37831 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37832 {
37833   tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37834
37835   if (clauses == error_mark_node)
37836     return;
37837
37838   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37839     {
37840       error_at (cp_lexer_peek_token (parser->lexer)->location,
37841                 "for statement expected");
37842       return;
37843     }
37844
37845   tree sb = begin_omp_structured_block ();
37846   int save = cp_parser_begin_omp_structured_block (parser);
37847   tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37848   if (ret)
37849     cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37850   cp_parser_end_omp_structured_block (parser, save);
37851   add_stmt (finish_omp_structured_block (sb));
37852 }
37853
37854 /* Main entry-point for parsing Cilk Plus _Cilk_for
37855    loops.  The return value is error_mark_node
37856    when errors happen and CILK_FOR tree on success.  */
37857
37858 static tree
37859 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37860 {
37861   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37862     gcc_unreachable ();
37863
37864   tree sb = begin_omp_structured_block ();
37865   int save = cp_parser_begin_omp_structured_block (parser);
37866
37867   tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37868   OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37869   OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37870   clauses = finish_omp_clauses (clauses, false);
37871
37872   tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37873   if (ret)
37874     cpp_validate_cilk_plus_loop (ret);
37875   else
37876     ret = error_mark_node;
37877
37878   cp_parser_end_omp_structured_block (parser, save);
37879   add_stmt (finish_omp_structured_block (sb));
37880   return ret;
37881 }
37882
37883 /* Create an identifier for a generic parameter type (a synthesized
37884    template parameter implied by `auto' or a concept identifier). */
37885
37886 static GTY(()) int generic_parm_count;
37887 static tree
37888 make_generic_type_name ()
37889 {
37890   char buf[32];
37891   sprintf (buf, "auto:%d", ++generic_parm_count);
37892   return get_identifier (buf);
37893 }
37894
37895 /* Predicate that behaves as is_auto_or_concept but matches the parent
37896    node of the generic type rather than the generic type itself.  This
37897    allows for type transformation in add_implicit_template_parms.  */
37898
37899 static inline bool
37900 tree_type_is_auto_or_concept (const_tree t)
37901 {
37902   return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37903 }
37904
37905 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37906    (creating a new template parameter list if necessary).  Returns the newly
37907    created template type parm.  */
37908
37909 static tree
37910 synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
37911 {
37912   gcc_assert (current_binding_level->kind == sk_function_parms);
37913
37914    /* Before committing to modifying any scope, if we're in an
37915       implicit template scope, and we're trying to synthesize a
37916       constrained parameter, try to find a previous parameter with
37917       the same name.  This is the same-type rule for abbreviated
37918       function templates.
37919
37920       NOTE: We can generate implicit parameters when tentatively
37921       parsing a nested name specifier, only to reject that parse
37922       later. However, matching the same template-id as part of a
37923       direct-declarator should generate an identical template
37924       parameter, so this rule will merge them. */
37925   if (parser->implicit_template_scope && constr)
37926     {
37927       tree t = parser->implicit_template_parms;
37928       while (t)
37929         {
37930           if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37931             {
37932               tree d = TREE_VALUE (t);
37933               if (TREE_CODE (d) == PARM_DECL)
37934                 /* Return the TEMPLATE_PARM_INDEX.  */
37935                 d = DECL_INITIAL (d);
37936               return d;
37937             }
37938           t = TREE_CHAIN (t);
37939         }
37940     }
37941
37942   /* We are either continuing a function template that already contains implicit
37943      template parameters, creating a new fully-implicit function template, or
37944      extending an existing explicit function template with implicit template
37945      parameters.  */
37946
37947   cp_binding_level *const entry_scope = current_binding_level;
37948
37949   bool become_template = false;
37950   cp_binding_level *parent_scope = 0;
37951
37952   if (parser->implicit_template_scope)
37953     {
37954       gcc_assert (parser->implicit_template_parms);
37955
37956       current_binding_level = parser->implicit_template_scope;
37957     }
37958   else
37959     {
37960       /* Roll back to the existing template parameter scope (in the case of
37961          extending an explicit function template) or introduce a new template
37962          parameter scope ahead of the function parameter scope (or class scope
37963          in the case of out-of-line member definitions).  The function scope is
37964          added back after template parameter synthesis below.  */
37965
37966       cp_binding_level *scope = entry_scope;
37967
37968       while (scope->kind == sk_function_parms)
37969         {
37970           parent_scope = scope;
37971           scope = scope->level_chain;
37972         }
37973       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37974         {
37975           /* If not defining a class, then any class scope is a scope level in
37976              an out-of-line member definition.  In this case simply wind back
37977              beyond the first such scope to inject the template parameter list.
37978              Otherwise wind back to the class being defined.  The latter can
37979              occur in class member friend declarations such as:
37980
37981                class A {
37982                  void foo (auto);
37983                };
37984                class B {
37985                  friend void A::foo (auto);
37986                };
37987
37988             The template parameter list synthesized for the friend declaration
37989             must be injected in the scope of 'B'.  This can also occur in
37990             erroneous cases such as:
37991
37992                struct A {
37993                  struct B {
37994                    void foo (auto);
37995                  };
37996                  void B::foo (auto) {}
37997                };
37998
37999             Here the attempted definition of 'B::foo' within 'A' is ill-formed
38000             but, nevertheless, the template parameter list synthesized for the
38001             declarator should be injected into the scope of 'A' as if the
38002             ill-formed template was specified explicitly.  */
38003
38004           while (scope->kind == sk_class && !scope->defining_class_p)
38005             {
38006               parent_scope = scope;
38007               scope = scope->level_chain;
38008             }
38009         }
38010
38011       current_binding_level = scope;
38012
38013       if (scope->kind != sk_template_parms
38014           || !function_being_declared_is_template_p (parser))
38015         {
38016           /* Introduce a new template parameter list for implicit template
38017              parameters.  */
38018
38019           become_template = true;
38020
38021           parser->implicit_template_scope
38022               = begin_scope (sk_template_parms, NULL);
38023
38024           ++processing_template_decl;
38025
38026           parser->fully_implicit_function_template_p = true;
38027           ++parser->num_template_parameter_lists;
38028         }
38029       else
38030         {
38031           /* Synthesize implicit template parameters at the end of the explicit
38032              template parameter list.  */
38033
38034           gcc_assert (current_template_parms);
38035
38036           parser->implicit_template_scope = scope;
38037
38038           tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38039           parser->implicit_template_parms
38040             = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38041         }
38042     }
38043
38044   /* Synthesize a new template parameter and track the current template
38045      parameter chain with implicit_template_parms.  */
38046
38047   tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38048   tree synth_id = make_generic_type_name ();
38049   tree synth_tmpl_parm;
38050   bool non_type = false;
38051
38052   if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38053     synth_tmpl_parm
38054       = finish_template_type_parm (class_type_node, synth_id);
38055   else if (TREE_CODE (proto) == TEMPLATE_DECL)
38056     synth_tmpl_parm
38057       = finish_constrained_template_template_parm (proto, synth_id);
38058   else
38059     {
38060       synth_tmpl_parm = copy_decl (proto);
38061       DECL_NAME (synth_tmpl_parm) = synth_id;
38062       non_type = true;
38063     }
38064
38065   // Attach the constraint to the parm before processing.
38066   tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38067   TREE_TYPE (node) = constr;
38068   tree new_parm
38069     = process_template_parm (parser->implicit_template_parms,
38070                              input_location,
38071                              node,
38072                              /*non_type=*/non_type,
38073                              /*param_pack=*/false);
38074
38075   // Chain the new parameter to the list of implicit parameters.
38076   if (parser->implicit_template_parms)
38077     parser->implicit_template_parms
38078       = TREE_CHAIN (parser->implicit_template_parms);
38079   else
38080     parser->implicit_template_parms = new_parm;
38081
38082   tree new_decl = getdecls ();
38083   if (non_type)
38084     /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
38085     new_decl = DECL_INITIAL (new_decl);
38086
38087   /* If creating a fully implicit function template, start the new implicit
38088      template parameter list with this synthesized type, otherwise grow the
38089      current template parameter list.  */
38090
38091   if (become_template)
38092     {
38093       parent_scope->level_chain = current_binding_level;
38094
38095       tree new_parms = make_tree_vec (1);
38096       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38097       current_template_parms = tree_cons (size_int (processing_template_decl),
38098                                           new_parms, current_template_parms);
38099     }
38100   else
38101     {
38102       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38103       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38104       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38105       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38106     }
38107
38108   // If the new parameter was constrained, we need to add that to the
38109   // constraints in the template parameter list.
38110   if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38111     {
38112       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38113       reqs = conjoin_constraints (reqs, req);
38114       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38115     }
38116
38117   current_binding_level = entry_scope;
38118
38119   return new_decl;
38120 }
38121
38122 /* Finish the declaration of a fully implicit function template.  Such a
38123    template has no explicit template parameter list so has not been through the
38124    normal template head and tail processing.  synthesize_implicit_template_parm
38125    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
38126    provided if the declaration is a class member such that its template
38127    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
38128    form is returned.  Otherwise NULL_TREE is returned. */
38129
38130 static tree
38131 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38132 {
38133   gcc_assert (parser->fully_implicit_function_template_p);
38134
38135   if (member_decl_opt && member_decl_opt != error_mark_node
38136       && DECL_VIRTUAL_P (member_decl_opt))
38137     {
38138       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38139                 "implicit templates may not be %<virtual%>");
38140       DECL_VIRTUAL_P (member_decl_opt) = false;
38141     }
38142
38143   if (member_decl_opt)
38144     member_decl_opt = finish_member_template_decl (member_decl_opt);
38145   end_template_decl ();
38146
38147   parser->fully_implicit_function_template_p = false;
38148   --parser->num_template_parameter_lists;
38149
38150   return member_decl_opt;
38151 }
38152
38153 #include "gt-cp-parser.h"