Bring in branch-8 bugfixes into GCC80.
[dragonfly.git] / contrib / gcc-8.0 / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2    Copyright (C) 2000-2018 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 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
47
48 \f
49 /* The lexer.  */
50
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52    and c-lex.c) and the C++ parser.  */
53
54 static cp_token eof_token =
55 {
56   CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
57 };
58
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61   NIC_NONE,
62   /* floating-point literal */
63   NIC_FLOAT,
64   /* %<this%> */
65   NIC_THIS,
66   /* %<__FUNCTION__%> */
67   NIC_FUNC_NAME,
68   /* %<__PRETTY_FUNCTION__%> */
69   NIC_PRETTY_FUNC,
70   /* %<__func__%> */
71   NIC_C99_FUNC,
72   /* "%<va_arg%> */
73   NIC_VA_ARG,
74   /* a cast */
75   NIC_CAST,
76   /* %<typeid%> operator */
77   NIC_TYPEID,
78   /* non-constant compound literals */
79   NIC_NCC,
80   /* a function call */
81   NIC_FUNC_CALL,
82   /* an increment */
83   NIC_INC,
84   /* an decrement */
85   NIC_DEC,
86   /* an array reference */
87   NIC_ARRAY_REF,
88   /* %<->%> */
89   NIC_ARROW,
90   /* %<.%> */
91   NIC_POINT,
92   /* the address of a label */
93   NIC_ADDR_LABEL,
94   /* %<*%> */
95   NIC_STAR,
96   /* %<&%> */
97   NIC_ADDR,
98   /* %<++%> */
99   NIC_PREINCREMENT,
100   /* %<--%> */
101   NIC_PREDECREMENT,
102   /* %<new%> */
103   NIC_NEW,
104   /* %<delete%> */
105   NIC_DEL,
106   /* calls to overloaded operators */
107   NIC_OVERLOADED,
108   /* an assignment */
109   NIC_ASSIGNMENT,
110   /* a comma operator */
111   NIC_COMMA,
112   /* a call to a constructor */
113   NIC_CONSTRUCTOR,
114   /* a transaction expression */
115   NIC_TRANSACTION
116 };
117
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120   /* NULL */
121   NLE_NULL,
122   /* is not a type */
123   NLE_TYPE,
124   /* is not a class or namespace */
125   NLE_CXX98,
126   /* is not a class, namespace, or enumeration */
127   NLE_NOT_CXX98
128 };
129
130 /* The various kinds of required token */
131 enum required_token {
132   RT_NONE,
133   RT_SEMICOLON,  /* ';' */
134   RT_OPEN_PAREN, /* '(' */
135   RT_CLOSE_BRACE, /* '}' */
136   RT_OPEN_BRACE,  /* '{' */
137   RT_CLOSE_SQUARE, /* ']' */
138   RT_OPEN_SQUARE,  /* '[' */
139   RT_COMMA, /* ',' */
140   RT_SCOPE, /* '::' */
141   RT_LESS, /* '<' */
142   RT_GREATER, /* '>' */
143   RT_EQ, /* '=' */
144   RT_ELLIPSIS, /* '...' */
145   RT_MULT, /* '*' */
146   RT_COMPL, /* '~' */
147   RT_COLON, /* ':' */
148   RT_COLON_SCOPE, /* ':' or '::' */
149   RT_CLOSE_PAREN, /* ')' */
150   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151   RT_PRAGMA_EOL, /* end of line */
152   RT_NAME, /* identifier */
153
154   /* The type is CPP_KEYWORD */
155   RT_NEW, /* new */
156   RT_DELETE, /* delete */
157   RT_RETURN, /* return */
158   RT_WHILE, /* while */
159   RT_EXTERN, /* extern */
160   RT_STATIC_ASSERT, /* static_assert */
161   RT_DECLTYPE, /* decltype */
162   RT_OPERATOR, /* operator */
163   RT_CLASS, /* class */
164   RT_TEMPLATE, /* template */
165   RT_NAMESPACE, /* namespace */
166   RT_USING, /* using */
167   RT_ASM, /* asm */
168   RT_TRY, /* try */
169   RT_CATCH, /* catch */
170   RT_THROW, /* throw */
171   RT_LABEL, /* __label__ */
172   RT_AT_TRY, /* @try */
173   RT_AT_SYNCHRONIZED, /* @synchronized */
174   RT_AT_THROW, /* @throw */
175
176   RT_SELECT,  /* selection-statement */
177   RT_ITERATION, /* iteration-statement */
178   RT_JUMP, /* jump-statement */
179   RT_CLASS_KEY, /* class-key */
180   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183   RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 };
185
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187    reverting it on destruction.  */
188
189 class type_id_in_expr_sentinel
190 {
191   cp_parser *parser;
192   bool saved;
193 public:
194   type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195     : parser (parser),
196       saved (parser->in_type_id_in_expr_p)
197   { parser->in_type_id_in_expr_p = set; }
198   ~type_id_in_expr_sentinel ()
199   { parser->in_type_id_in_expr_p = saved; }
200 };
201
202 /* Prototypes.  */
203
204 static cp_lexer *cp_lexer_new_main
205   (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207   (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209   (cp_lexer *);
210 static int cp_lexer_saving_tokens
211   (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213   (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215   (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217   (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219   (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221   (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223   (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225   (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227   (cp_lexer *);
228 static void cp_lexer_purge_token
229   (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231   (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233   (cp_lexer *);
234 static void cp_lexer_commit_tokens
235   (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237   (cp_lexer *);
238 static void cp_lexer_print_token
239   (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241   (cp_lexer *);
242 static void cp_lexer_start_debugging
243   (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245   (cp_lexer *) ATTRIBUTE_UNUSED;
246
247 static cp_token_cache *cp_token_cache_new
248   (cp_token *, cp_token *);
249
250 static void cp_parser_initial_pragma
251   (cp_token *);
252
253 static bool cp_parser_omp_declare_reduction_exprs
254   (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256   (cp_parser *, tree, bool);
257
258 /* Manifest constants.  */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
261
262 /* Variables.  */
263
264 /* The stream to which debugging output should be written.  */
265 static FILE *cp_lexer_debug_stream;
266
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268    sizeof, typeof, or alignof.  */
269 int cp_unevaluated_operand;
270
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
273    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
274    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275    highlighted by surrounding it in [[ ]].  */
276
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279                       cp_token *start_token, unsigned num,
280                       cp_token *curr_token)
281 {
282   unsigned i, nprinted;
283   cp_token *token;
284   bool do_print;
285
286   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
287
288   if (buffer == NULL)
289     return;
290
291   if (num == 0)
292     num = buffer->length ();
293
294   if (start_token == NULL)
295     start_token = buffer->address ();
296
297   if (start_token > buffer->address ())
298     {
299       cp_lexer_print_token (file, &(*buffer)[0]);
300       fprintf (file, " ... ");
301     }
302
303   do_print = false;
304   nprinted = 0;
305   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
306     {
307       if (token == start_token)
308         do_print = true;
309
310       if (!do_print)
311         continue;
312
313       nprinted++;
314       if (token == curr_token)
315         fprintf (file, "[[");
316
317       cp_lexer_print_token (file, token);
318
319       if (token == curr_token)
320         fprintf (file, "]]");
321
322       switch (token->type)
323         {
324           case CPP_SEMICOLON:
325           case CPP_OPEN_BRACE:
326           case CPP_CLOSE_BRACE:
327           case CPP_EOF:
328             fputc ('\n', file);
329             break;
330
331           default:
332             fputc (' ', file);
333         }
334     }
335
336   if (i == num && i < buffer->length ())
337     {
338       fprintf (file, " ... ");
339       cp_lexer_print_token (file, &buffer->last ());
340     }
341
342   fprintf (file, "\n");
343 }
344
345
346 /* Dump all tokens in BUFFER to stderr.  */
347
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
350 {
351   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
352 }
353
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
356 {
357   cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
358 }
359
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
362 {
363   if (ptr)
364     debug (*ptr);
365   else
366     fprintf (stderr, "<nil>\n");
367 }
368
369
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
371    description for T.  */
372
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
375 {
376   if (t)
377     {
378       fprintf (file, "%s: ", desc);
379       print_node_brief (file, "", t, 0);
380     }
381 }
382
383
384 /* Dump parser context C to FILE.  */
385
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
388 {
389   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391   print_node_brief (file, "", c->object_type, 0);
392   fprintf (file, "}\n");
393 }
394
395
396 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
397
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
400 {
401   unsigned i;
402   cp_parser_context *c;
403
404   fprintf (file, "Parsing context stack:\n");
405   for (i = 0, c = first; c; c = c->next, i++)
406     {
407       fprintf (file, "\t#%u: ", i);
408       cp_debug_print_context (file, c);
409     }
410 }
411
412
413 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
414
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
417 {
418   if (flag)
419     fprintf (file, "%s: true\n", desc);
420 }
421
422
423 /* Print an unparsed function entry UF to FILE.  */
424
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
427 {
428   unsigned i;
429   cp_default_arg_entry *default_arg_fn;
430   tree fn;
431
432   fprintf (file, "\tFunctions with default args:\n");
433   for (i = 0;
434        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435        i++)
436     {
437       fprintf (file, "\t\tClass type: ");
438       print_node_brief (file, "", default_arg_fn->class_type, 0);
439       fprintf (file, "\t\tDeclaration: ");
440       print_node_brief (file, "", default_arg_fn->decl, 0);
441       fprintf (file, "\n");
442     }
443
444   fprintf (file, "\n\tFunctions with definitions that require "
445            "post-processing\n\t\t");
446   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
447     {
448       print_node_brief (file, "", fn, 0);
449       fprintf (file, " ");
450     }
451   fprintf (file, "\n");
452
453   fprintf (file, "\n\tNon-static data members with initializers that require "
454            "post-processing\n\t\t");
455   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
456     {
457       print_node_brief (file, "", fn, 0);
458       fprintf (file, " ");
459     }
460   fprintf (file, "\n");
461 }
462
463
464 /* Print the stack of unparsed member functions S to FILE.  */
465
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468                                 vec<cp_unparsed_functions_entry, va_gc> *s)
469 {
470   unsigned i;
471   cp_unparsed_functions_entry *uf;
472
473   fprintf (file, "Unparsed functions\n");
474   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
475     {
476       fprintf (file, "#%u:\n", i);
477       cp_debug_print_unparsed_function (file, uf);
478     }
479 }
480
481
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
484
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
487 {
488   cp_token *next_token, *first_token, *start_token;
489
490   if (file == NULL)
491     file = stderr;
492
493   next_token = parser->lexer->next_token;
494   first_token = parser->lexer->buffer->address ();
495   start_token = (next_token > first_token + window_size / 2)
496                 ? next_token - window_size / 2
497                 : first_token;
498   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499                         next_token);
500 }
501
502
503 /* Dump debugging information for the given PARSER.  If FILE is NULL,
504    the output is printed on stderr.  */
505
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
508 {
509   const size_t window_size = 20;
510   cp_token *token;
511   expanded_location eloc;
512
513   if (file == NULL)
514     file = stderr;
515
516   fprintf (file, "Parser state\n\n");
517   fprintf (file, "Number of tokens: %u\n",
518            vec_safe_length (parser->lexer->buffer));
519   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520   cp_debug_print_tree_if_set (file, "Object scope",
521                                      parser->object_scope);
522   cp_debug_print_tree_if_set (file, "Qualifying scope",
523                                      parser->qualifying_scope);
524   cp_debug_print_context_stack (file, parser->context);
525   cp_debug_print_flag (file, "Allow GNU extensions",
526                               parser->allow_gnu_extensions_p);
527   cp_debug_print_flag (file, "'>' token is greater-than",
528                               parser->greater_than_is_operator_p);
529   cp_debug_print_flag (file, "Default args allowed in current "
530                               "parameter list", parser->default_arg_ok_p);
531   cp_debug_print_flag (file, "Parsing integral constant-expression",
532                               parser->integral_constant_expression_p);
533   cp_debug_print_flag (file, "Allow non-constant expression in current "
534                               "constant-expression",
535                               parser->allow_non_integral_constant_expression_p);
536   cp_debug_print_flag (file, "Seen non-constant expression",
537                               parser->non_integral_constant_expression_p);
538   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539                               "current context",
540                               parser->local_variables_forbidden_p);
541   cp_debug_print_flag (file, "In unbraced linkage specification",
542                               parser->in_unbraced_linkage_specification_p);
543   cp_debug_print_flag (file, "Parsing a declarator",
544                               parser->in_declarator_p);
545   cp_debug_print_flag (file, "In template argument list",
546                               parser->in_template_argument_list_p);
547   cp_debug_print_flag (file, "Parsing an iteration statement",
548                               parser->in_statement & IN_ITERATION_STMT);
549   cp_debug_print_flag (file, "Parsing a switch statement",
550                               parser->in_statement & IN_SWITCH_STMT);
551   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552                               parser->in_statement & IN_OMP_BLOCK);
553   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554                               parser->in_statement & IN_OMP_FOR);
555   cp_debug_print_flag (file, "Parsing an if statement",
556                               parser->in_statement & IN_IF_STMT);
557   cp_debug_print_flag (file, "Parsing a type-id in an expression "
558                               "context", parser->in_type_id_in_expr_p);
559   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560                               parser->implicit_extern_c);
561   cp_debug_print_flag (file, "String expressions should be translated "
562                               "to execution character set",
563                               parser->translate_strings_p);
564   cp_debug_print_flag (file, "Parsing function body outside of a "
565                               "local class", parser->in_function_body);
566   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567                               parser->colon_corrects_to_scope_p);
568   cp_debug_print_flag (file, "Colon doesn't start a class definition",
569                               parser->colon_doesnt_start_class_def_p);
570   if (parser->type_definition_forbidden_message)
571     fprintf (file, "Error message for forbidden type definitions: %s\n",
572              parser->type_definition_forbidden_message);
573   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574   fprintf (file, "Number of class definitions in progress: %u\n",
575            parser->num_classes_being_defined);
576   fprintf (file, "Number of template parameter lists for the current "
577            "declaration: %u\n", parser->num_template_parameter_lists);
578   cp_debug_parser_tokens (file, parser, window_size);
579   token = parser->lexer->next_token;
580   fprintf (file, "Next token to parse:\n");
581   fprintf (file, "\tToken:  ");
582   cp_lexer_print_token (file, token);
583   eloc = expand_location (token->location);
584   fprintf (file, "\n\tFile:   %s\n", eloc.file);
585   fprintf (file, "\tLine:   %d\n", eloc.line);
586   fprintf (file, "\tColumn: %d\n", eloc.column);
587 }
588
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
591 {
592   cp_debug_parser (stderr, &ref);
593 }
594
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
597 {
598   if (ptr)
599     debug (*ptr);
600   else
601     fprintf (stderr, "<nil>\n");
602 }
603
604 /* Allocate memory for a new lexer object and return it.  */
605
606 static cp_lexer *
607 cp_lexer_alloc (void)
608 {
609   cp_lexer *lexer;
610
611   c_common_no_more_pch ();
612
613   /* Allocate the memory.  */
614   lexer = ggc_cleared_alloc<cp_lexer> ();
615
616   /* Initially we are not debugging.  */
617   lexer->debugging_p = false;
618
619   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620
621   /* Create the buffer.  */
622   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623
624   return lexer;
625 }
626
627
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629    preprocessor.  */
630
631 static cp_lexer *
632 cp_lexer_new_main (void)
633 {
634   cp_lexer *lexer;
635   cp_token token;
636
637   /* It's possible that parsing the first pragma will load a PCH file,
638      which is a GC collection point.  So we have to do that before
639      allocating any memory.  */
640   cp_parser_initial_pragma (&token);
641
642   lexer = cp_lexer_alloc ();
643
644   /* Put the first token in the buffer.  */
645   lexer->buffer->quick_push (token);
646
647   /* Get the remaining tokens from the preprocessor.  */
648   while (token.type != CPP_EOF)
649     {
650       cp_lexer_get_preprocessor_token (lexer, &token);
651       vec_safe_push (lexer->buffer, token);
652     }
653
654   lexer->last_token = lexer->buffer->address ()
655                       + lexer->buffer->length ()
656                       - 1;
657   lexer->next_token = lexer->buffer->length ()
658                       ? lexer->buffer->address ()
659                       : &eof_token;
660
661   /* Subsequent preprocessor diagnostics should use compiler
662      diagnostic functions to get the compiler source location.  */
663   done_lexing = true;
664
665   gcc_assert (!lexer->next_token->purged_p);
666   return lexer;
667 }
668
669 /* Create a new lexer whose token stream is primed with the tokens in
670    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
671
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 {
675   cp_token *first = cache->first;
676   cp_token *last = cache->last;
677   cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678
679   /* We do not own the buffer.  */
680   lexer->buffer = NULL;
681   lexer->next_token = first == last ? &eof_token : first;
682   lexer->last_token = last;
683
684   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685
686   /* Initially we are not debugging.  */
687   lexer->debugging_p = false;
688
689   gcc_assert (!lexer->next_token->purged_p);
690   return lexer;
691 }
692
693 /* Frees all resources associated with LEXER.  */
694
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
697 {
698   vec_free (lexer->buffer);
699   lexer->saved_tokens.release ();
700   ggc_free (lexer);
701 }
702
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704    be used.  The point of this flag is to help the compiler to fold away calls
705    to cp_lexer_debugging_p within this source file at compile time, when the
706    lexer is not being debugged.  */
707
708 #define LEXER_DEBUGGING_ENABLED_P false
709
710 /* Returns nonzero if debugging information should be output.  */
711
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
714 {
715   if (!LEXER_DEBUGGING_ENABLED_P)
716     return false;
717
718   return lexer->debugging_p;
719 }
720
721
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 {
725   gcc_assert (!previous_p || lexer->next_token != &eof_token);
726
727   return lexer->next_token - previous_p;
728 }
729
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 {
733   return pos;
734 }
735
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 {
739   lexer->next_token = cp_lexer_token_at (lexer, pos);
740 }
741
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
744 {
745   if (lexer->next_token == &eof_token)
746     return lexer->last_token - 1;
747   else
748     return cp_lexer_token_position (lexer, true);
749 }
750
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
753 {
754   cp_token_position tp = cp_lexer_previous_token_position (lexer);
755
756   /* Skip past purged tokens.  */
757   while (tp->purged_p)
758     {
759       gcc_assert (tp != vec_safe_address (lexer->buffer));
760       tp--;
761     }
762
763   return cp_lexer_token_at (lexer, tp);
764 }
765
766 /* nonzero if we are presently saving tokens.  */
767
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
770 {
771   return lexer->saved_tokens.length () != 0;
772 }
773
774 /* Store the next token from the preprocessor in *TOKEN.  Return true
775    if we reach EOF.  If LEXER is NULL, assume we are handling an
776    initial #pragma pch_preprocess, and thus want the lexer to return
777    processed strings.  */
778
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
781 {
782   static int is_extern_c = 0;
783
784    /* Get a new token from the preprocessor.  */
785   token->type
786     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788   token->keyword = RID_MAX;
789   token->purged_p = false;
790   token->error_reported = false;
791
792   /* On some systems, some header files are surrounded by an
793      implicit extern "C" block.  Set a flag in the token if it
794      comes from such a header.  */
795   is_extern_c += pending_lang_change;
796   pending_lang_change = 0;
797   token->implicit_extern_c = is_extern_c > 0;
798
799   /* Check to see if this token is a keyword.  */
800   if (token->type == CPP_NAME)
801     {
802       if (IDENTIFIER_KEYWORD_P (token->u.value))
803         {
804           /* Mark this token as a keyword.  */
805           token->type = CPP_KEYWORD;
806           /* Record which keyword.  */
807           token->keyword = C_RID_CODE (token->u.value);
808         }
809       else
810         {
811           if (warn_cxx11_compat
812               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813               && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
814             {
815               /* Warn about the C++0x keyword (but still treat it as
816                  an identifier).  */
817               warning (OPT_Wc__11_compat, 
818                        "identifier %qE is a keyword in C++11",
819                        token->u.value);
820
821               /* Clear out the C_RID_CODE so we don't warn about this
822                  particular identifier-turned-keyword again.  */
823               C_SET_RID_CODE (token->u.value, RID_MAX);
824             }
825
826           token->keyword = RID_MAX;
827         }
828     }
829   else if (token->type == CPP_AT_NAME)
830     {
831       /* This only happens in Objective-C++; it must be a keyword.  */
832       token->type = CPP_KEYWORD;
833       switch (C_RID_CODE (token->u.value))
834         {
835           /* Replace 'class' with '@class', 'private' with '@private',
836              etc.  This prevents confusion with the C++ keyword
837              'class', and makes the tokens consistent with other
838              Objective-C 'AT' keywords.  For example '@class' is
839              reported as RID_AT_CLASS which is consistent with
840              '@synchronized', which is reported as
841              RID_AT_SYNCHRONIZED.
842           */
843         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
844         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
845         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
847         case RID_THROW:     token->keyword = RID_AT_THROW; break;
848         case RID_TRY:       token->keyword = RID_AT_TRY; break;
849         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
850         case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851         default:            token->keyword = C_RID_CODE (token->u.value);
852         }
853     }
854 }
855
856 /* Update the globals input_location and the input file stack from TOKEN.  */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
859 {
860   if (token->type != CPP_EOF)
861     {
862       input_location = token->location;
863     }
864 }
865
866 /* Update the globals input_location and the input file stack from LEXER.  */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
869 {
870   cp_token *token = cp_lexer_peek_token (lexer);
871   cp_lexer_set_source_position_from_token (token);
872 }
873
874 /* Return a pointer to the next token in the token stream, but do not
875    consume it.  */
876
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
879 {
880   if (cp_lexer_debugging_p (lexer))
881     {
882       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884       putc ('\n', cp_lexer_debug_stream);
885     }
886   return lexer->next_token;
887 }
888
889 /* Return true if the next token has the indicated TYPE.  */
890
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 {
894   return cp_lexer_peek_token (lexer)->type == type;
895 }
896
897 /* Return true if the next token does not have the indicated TYPE.  */
898
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 {
902   return !cp_lexer_next_token_is (lexer, type);
903 }
904
905 /* Return true if the next token is the indicated KEYWORD.  */
906
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 {
910   return cp_lexer_peek_token (lexer)->keyword == keyword;
911 }
912
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 {
916   return cp_lexer_peek_nth_token (lexer, n)->type == type;
917 }
918
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 {
922   return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
923 }
924
925 /* Return true if the next token is not the indicated KEYWORD.  */
926
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
929 {
930   return cp_lexer_peek_token (lexer)->keyword != keyword;
931 }
932
933 /* Return true if KEYWORD can start a decl-specifier.  */
934
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
937 {
938   switch (keyword)
939     {
940       /* auto specifier: storage-class-specifier in C++,
941          simple-type-specifier in C++0x.  */
942     case RID_AUTO:
943       /* Storage classes.  */
944     case RID_REGISTER:
945     case RID_STATIC:
946     case RID_EXTERN:
947     case RID_MUTABLE:
948     case RID_THREAD:
949       /* Elaborated type specifiers.  */
950     case RID_ENUM:
951     case RID_CLASS:
952     case RID_STRUCT:
953     case RID_UNION:
954     case RID_TYPENAME:
955       /* Simple type specifiers.  */
956     case RID_CHAR:
957     case RID_CHAR16:
958     case RID_CHAR32:
959     case RID_WCHAR:
960     case RID_BOOL:
961     case RID_SHORT:
962     case RID_INT:
963     case RID_LONG:
964     case RID_SIGNED:
965     case RID_UNSIGNED:
966     case RID_FLOAT:
967     case RID_DOUBLE:
968     case RID_VOID:
969       /* GNU extensions.  */ 
970     case RID_ATTRIBUTE:
971     case RID_TYPEOF:
972       /* C++0x extensions.  */
973     case RID_DECLTYPE:
974     case RID_UNDERLYING_TYPE:
975     case RID_CONSTEXPR:
976       return true;
977
978     default:
979       if (keyword >= RID_FIRST_INT_N
980           && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981           && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982         return true;
983       return false;
984     }
985 }
986
987 /* Return true if the next token is a keyword for a decl-specifier.  */
988
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
991 {
992   cp_token *token;
993
994   token = cp_lexer_peek_token (lexer);
995   return cp_keyword_starts_decl_specifier_p (token->keyword);
996 }
997
998 /* Returns TRUE iff the token T begins a decltype type.  */
999
1000 static bool
1001 token_is_decltype (cp_token *t)
1002 {
1003   return (t->keyword == RID_DECLTYPE
1004           || t->type == CPP_DECLTYPE);
1005 }
1006
1007 /* Returns TRUE iff the next token begins a decltype type.  */
1008
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1011 {
1012   cp_token *t = cp_lexer_peek_token (lexer);
1013   return token_is_decltype (t);
1014 }
1015
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017    associated checks and return the value.  */
1018
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1021 {
1022   /* Perform any access checks that were deferred.  */
1023   vec<deferred_access_check, va_gc> *checks;
1024   deferred_access_check *chk;
1025   checks = check_value->checks;
1026   if (checks)
1027     {
1028       int i;
1029       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030         perform_or_defer_access_check (chk->binfo,
1031                                        chk->decl,
1032                                        chk->diag_decl, tf_warning_or_error);
1033     }
1034   /* Return the stored value.  */
1035   return check_value->value;
1036 }
1037
1038 /* Return a pointer to the Nth token in the token stream.  If N is 1,
1039    then this is precisely equivalent to cp_lexer_peek_token (except
1040    that it is not inline).  One would like to disallow that case, but
1041    there is one case (cp_parser_nth_token_starts_template_id) where
1042    the caller passes a variable for N and it might be 1.  */
1043
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1046 {
1047   cp_token *token;
1048
1049   /* N is 1-based, not zero-based.  */
1050   gcc_assert (n > 0);
1051
1052   if (cp_lexer_debugging_p (lexer))
1053     fprintf (cp_lexer_debug_stream,
1054              "cp_lexer: peeking ahead %ld at token: ", (long)n);
1055
1056   --n;
1057   token = lexer->next_token;
1058   gcc_assert (!n || token != &eof_token);
1059   while (n != 0)
1060     {
1061       ++token;
1062       if (token == lexer->last_token)
1063         {
1064           token = &eof_token;
1065           break;
1066         }
1067
1068       if (!token->purged_p)
1069         --n;
1070     }
1071
1072   if (cp_lexer_debugging_p (lexer))
1073     {
1074       cp_lexer_print_token (cp_lexer_debug_stream, token);
1075       putc ('\n', cp_lexer_debug_stream);
1076     }
1077
1078   return token;
1079 }
1080
1081 /* Return the next token, and advance the lexer's next_token pointer
1082    to point to the next non-purged token.  */
1083
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1086 {
1087   cp_token *token = lexer->next_token;
1088
1089   gcc_assert (token != &eof_token);
1090   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1091
1092   do
1093     {
1094       lexer->next_token++;
1095       if (lexer->next_token == lexer->last_token)
1096         {
1097           lexer->next_token = &eof_token;
1098           break;
1099         }
1100
1101     }
1102   while (lexer->next_token->purged_p);
1103
1104   cp_lexer_set_source_position_from_token (token);
1105
1106   /* Provide debugging output.  */
1107   if (cp_lexer_debugging_p (lexer))
1108     {
1109       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110       cp_lexer_print_token (cp_lexer_debug_stream, token);
1111       putc ('\n', cp_lexer_debug_stream);
1112     }
1113
1114   return token;
1115 }
1116
1117 /* Permanently remove the next token from the token stream, and
1118    advance the next_token pointer to refer to the next non-purged
1119    token.  */
1120
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1123 {
1124   cp_token *tok = lexer->next_token;
1125
1126   gcc_assert (tok != &eof_token);
1127   tok->purged_p = true;
1128   tok->location = UNKNOWN_LOCATION;
1129   tok->u.value = NULL_TREE;
1130   tok->keyword = RID_MAX;
1131
1132   do
1133     {
1134       tok++;
1135       if (tok == lexer->last_token)
1136         {
1137           tok = &eof_token;
1138           break;
1139         }
1140     }
1141   while (tok->purged_p);
1142   lexer->next_token = tok;
1143 }
1144
1145 /* Permanently remove all tokens after TOK, up to, but not
1146    including, the token that will be returned next by
1147    cp_lexer_peek_token.  */
1148
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1151 {
1152   cp_token *peek = lexer->next_token;
1153
1154   if (peek == &eof_token)
1155     peek = lexer->last_token;
1156
1157   gcc_assert (tok < peek);
1158
1159   for ( tok += 1; tok != peek; tok += 1)
1160     {
1161       tok->purged_p = true;
1162       tok->location = UNKNOWN_LOCATION;
1163       tok->u.value = NULL_TREE;
1164       tok->keyword = RID_MAX;
1165     }
1166 }
1167
1168 /* Begin saving tokens.  All tokens consumed after this point will be
1169    preserved.  */
1170
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1173 {
1174   /* Provide debugging output.  */
1175   if (cp_lexer_debugging_p (lexer))
1176     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1177
1178   lexer->saved_tokens.safe_push (lexer->next_token);
1179 }
1180
1181 /* Commit to the portion of the token stream most recently saved.  */
1182
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1185 {
1186   /* Provide debugging output.  */
1187   if (cp_lexer_debugging_p (lexer))
1188     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1189
1190   lexer->saved_tokens.pop ();
1191 }
1192
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194    to the token stream.  Stop saving tokens.  */
1195
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1198 {
1199   /* Provide debugging output.  */
1200   if (cp_lexer_debugging_p (lexer))
1201     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1202
1203   lexer->next_token = lexer->saved_tokens.pop ();
1204 }
1205
1206 /* RAII wrapper around the above functions, with sanity checking.  Creating
1207    a variable saves tokens, which are committed when the variable is
1208    destroyed unless they are explicitly rolled back by calling the rollback
1209    member function.  */
1210
1211 struct saved_token_sentinel
1212 {
1213   cp_lexer *lexer;
1214   unsigned len;
1215   bool commit;
1216   saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1217   {
1218     len = lexer->saved_tokens.length ();
1219     cp_lexer_save_tokens (lexer);
1220   }
1221   void rollback ()
1222   {
1223     cp_lexer_rollback_tokens (lexer);
1224     commit = false;
1225   }
1226   ~saved_token_sentinel()
1227   {
1228     if (commit)
1229       cp_lexer_commit_tokens (lexer);
1230     gcc_assert (lexer->saved_tokens.length () == len);
1231   }
1232 };
1233
1234 /* Print a representation of the TOKEN on the STREAM.  */
1235
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1238 {
1239   /* We don't use cpp_type2name here because the parser defines
1240      a few tokens of its own.  */
1241   static const char *const token_names[] = {
1242     /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245     TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248     /* C++ parser token types - see "Manifest constants", above.  */
1249     "KEYWORD",
1250     "TEMPLATE_ID",
1251     "NESTED_NAME_SPECIFIER",
1252   };
1253
1254   /* For some tokens, print the associated data.  */
1255   switch (token->type)
1256     {
1257     case CPP_KEYWORD:
1258       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259          For example, `struct' is mapped to an INTEGER_CST.  */
1260       if (!identifier_p (token->u.value))
1261         break;
1262       /* fall through */
1263     case CPP_NAME:
1264       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265       break;
1266
1267     case CPP_STRING:
1268     case CPP_STRING16:
1269     case CPP_STRING32:
1270     case CPP_WSTRING:
1271     case CPP_UTF8STRING:
1272       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273       break;
1274
1275     case CPP_NUMBER:
1276       print_generic_expr (stream, token->u.value);
1277       break;
1278
1279     default:
1280       /* If we have a name for the token, print it out.  Otherwise, we
1281          simply give the numeric code.  */
1282       if (token->type < ARRAY_SIZE(token_names))
1283         fputs (token_names[token->type], stream);
1284       else
1285         fprintf (stream, "[%d]", token->type);
1286       break;
1287     }
1288 }
1289
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1292 {
1293   cp_lexer_print_token (stderr, &ref);
1294   fprintf (stderr, "\n");
1295 }
1296
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1299 {
1300   if (ptr)
1301     debug (*ptr);
1302   else
1303     fprintf (stderr, "<nil>\n");
1304 }
1305
1306
1307 /* Start emitting debugging information.  */
1308
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1311 {
1312   if (!LEXER_DEBUGGING_ENABLED_P)
1313     fatal_error (input_location,
1314                  "LEXER_DEBUGGING_ENABLED_P is not set to true");
1315
1316   lexer->debugging_p = true;
1317   cp_lexer_debug_stream = stderr;
1318 }
1319
1320 /* Stop emitting debugging information.  */
1321
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1324 {
1325   if (!LEXER_DEBUGGING_ENABLED_P)
1326     fatal_error (input_location,
1327                  "LEXER_DEBUGGING_ENABLED_P is not set to true");
1328
1329   lexer->debugging_p = false;
1330   cp_lexer_debug_stream = NULL;
1331 }
1332
1333 /* Create a new cp_token_cache, representing a range of tokens.  */
1334
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1337 {
1338   cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339   cache->first = first;
1340   cache->last = last;
1341   return cache;
1342 }
1343
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345    by function declaration or definition.  */
1346
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1349 {
1350   if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1351     {
1352       error ("%<#pragma omp declare simd%> not immediately followed by "
1353              "function declaration or definition");
1354       parser->omp_declare_simd = NULL;
1355     }
1356 }
1357
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359    and put that into "omp declare simd" attribute.  */
1360
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1363 {
1364   if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1365     {
1366       if (fndecl == error_mark_node)
1367         {
1368           parser->omp_declare_simd = NULL;
1369           return;
1370         }
1371       if (TREE_CODE (fndecl) != FUNCTION_DECL)
1372         {
1373           cp_ensure_no_omp_declare_simd (parser);
1374           return;
1375         }
1376     }
1377 }
1378
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380    declaration or definition.  */
1381
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1384 {
1385   if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1386     {
1387       error_at (parser->oacc_routine->loc,
1388                 "%<#pragma acc routine%> not immediately followed by "
1389                 "function 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->parenthesized = UNKNOWN_LOCATION;
1448   declarator->attributes = NULL_TREE;
1449   declarator->std_attributes = NULL_TREE;
1450   declarator->declarator = NULL;
1451   declarator->parameter_pack_p = false;
1452   declarator->id_loc = UNKNOWN_LOCATION;
1453
1454   return declarator;
1455 }
1456
1457 /* Make a declarator for a generalized identifier.  If
1458    QUALIFYING_SCOPE is non-NULL, the identifier is
1459    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1461    is, if any.   */
1462
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465                     special_function_kind sfk)
1466 {
1467   cp_declarator *declarator;
1468
1469   /* It is valid to write:
1470
1471        class C { void f(); };
1472        typedef C D;
1473        void D::f();
1474
1475      The standard is not clear about whether `typedef const C D' is
1476      legal; as of 2002-09-15 the committee is considering that
1477      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1478      well.  */
1479   if (qualifying_scope && TYPE_P (qualifying_scope))
1480     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1481
1482   gcc_assert (identifier_p (unqualified_name)
1483               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1485
1486   declarator = make_declarator (cdk_id);
1487   declarator->u.id.qualifying_scope = qualifying_scope;
1488   declarator->u.id.unqualified_name = unqualified_name;
1489   declarator->u.id.sfk = sfk;
1490   
1491   return declarator;
1492 }
1493
1494 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1495    of modifiers such as const or volatile to apply to the pointer
1496    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1497    appertain to the pointer or reference.  */
1498
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501                          tree attributes)
1502 {
1503   cp_declarator *declarator;
1504
1505   declarator = make_declarator (cdk_pointer);
1506   declarator->declarator = target;
1507   declarator->u.pointer.qualifiers = cv_qualifiers;
1508   declarator->u.pointer.class_type = NULL_TREE;
1509   if (target)
1510     {
1511       declarator->id_loc = target->id_loc;
1512       declarator->parameter_pack_p = target->parameter_pack_p;
1513       target->parameter_pack_p = false;
1514     }
1515   else
1516     declarator->parameter_pack_p = false;
1517
1518   declarator->std_attributes = attributes;
1519
1520   return declarator;
1521 }
1522
1523 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1524    represent the attributes that appertain to the pointer or
1525    reference.  */
1526
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529                            bool rvalue_ref, tree attributes)
1530 {
1531   cp_declarator *declarator;
1532
1533   declarator = make_declarator (cdk_reference);
1534   declarator->declarator = target;
1535   declarator->u.reference.qualifiers = cv_qualifiers;
1536   declarator->u.reference.rvalue_ref = rvalue_ref;
1537   if (target)
1538     {
1539       declarator->id_loc = target->id_loc;
1540       declarator->parameter_pack_p = target->parameter_pack_p;
1541       target->parameter_pack_p = false;
1542     }
1543   else
1544     declarator->parameter_pack_p = false;
1545
1546   declarator->std_attributes = attributes;
1547
1548   return declarator;
1549 }
1550
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1553    appertain to the pointer or reference.  */
1554
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557                         cp_declarator *pointee,
1558                         tree attributes)
1559 {
1560   cp_declarator *declarator;
1561
1562   declarator = make_declarator (cdk_ptrmem);
1563   declarator->declarator = pointee;
1564   declarator->u.pointer.qualifiers = cv_qualifiers;
1565   declarator->u.pointer.class_type = class_type;
1566
1567   if (pointee)
1568     {
1569       declarator->parameter_pack_p = pointee->parameter_pack_p;
1570       pointee->parameter_pack_p = false;
1571     }
1572   else
1573     declarator->parameter_pack_p = false;
1574
1575   declarator->std_attributes = attributes;
1576
1577   return declarator;
1578 }
1579
1580 /* Make a declarator for the function given by TARGET, with the
1581    indicated PARMS.  The CV_QUALIFIERS apply to the function, as in
1582    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1583    indicates what exceptions can be thrown.  */
1584
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587                       tree parms,
1588                       cp_cv_quals cv_qualifiers,
1589                       cp_virt_specifiers virt_specifiers,
1590                       cp_ref_qualifier ref_qualifier,
1591                       tree tx_qualifier,
1592                       tree exception_specification,
1593                       tree late_return_type,
1594                       tree requires_clause)
1595 {
1596   cp_declarator *declarator;
1597
1598   declarator = make_declarator (cdk_function);
1599   declarator->declarator = target;
1600   declarator->u.function.parameters = parms;
1601   declarator->u.function.qualifiers = cv_qualifiers;
1602   declarator->u.function.virt_specifiers = virt_specifiers;
1603   declarator->u.function.ref_qualifier = ref_qualifier;
1604   declarator->u.function.tx_qualifier = tx_qualifier;
1605   declarator->u.function.exception_specification = exception_specification;
1606   declarator->u.function.late_return_type = late_return_type;
1607   declarator->u.function.requires_clause = requires_clause;
1608   if (target)
1609     {
1610       declarator->id_loc = target->id_loc;
1611       declarator->parameter_pack_p = target->parameter_pack_p;
1612       target->parameter_pack_p = false;
1613     }
1614   else
1615     declarator->parameter_pack_p = false;
1616
1617   return declarator;
1618 }
1619
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621    defined by ELEMENT.  */
1622
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1625 {
1626   cp_declarator *declarator;
1627
1628   declarator = make_declarator (cdk_array);
1629   declarator->declarator = element;
1630   declarator->u.array.bounds = bounds;
1631   if (element)
1632     {
1633       declarator->id_loc = element->id_loc;
1634       declarator->parameter_pack_p = element->parameter_pack_p;
1635       element->parameter_pack_p = false;
1636     }
1637   else
1638     declarator->parameter_pack_p = false;
1639
1640   return declarator;
1641 }
1642
1643 /* Determine whether the declarator we've seen so far can be a
1644    parameter pack, when followed by an ellipsis.  */
1645 static bool 
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1647 {
1648   if (declarator && declarator->parameter_pack_p)
1649     /* We already saw an ellipsis.  */
1650     return false;
1651
1652   /* Search for a declarator name, or any other declarator that goes
1653      after the point where the ellipsis could appear in a parameter
1654      pack. If we find any of these, then this declarator can not be
1655      made into a parameter pack.  */
1656   bool found = false;
1657   while (declarator && !found)
1658     {
1659       switch ((int)declarator->kind)
1660         {
1661         case cdk_id:
1662         case cdk_array:
1663         case cdk_decomp:
1664           found = true;
1665           break;
1666
1667         case cdk_error:
1668           return true;
1669
1670         default:
1671           declarator = declarator->declarator;
1672           break;
1673         }
1674     }
1675
1676   return !found;
1677 }
1678
1679 cp_parameter_declarator *no_parameters;
1680
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682    DECLARATOR and DEFAULT_ARGUMENT.  */
1683
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686                            cp_declarator *declarator,
1687                            tree default_argument,
1688                            location_t loc,
1689                            bool template_parameter_pack_p = false)
1690 {
1691   cp_parameter_declarator *parameter;
1692
1693   parameter = ((cp_parameter_declarator *)
1694                alloc_declarator (sizeof (cp_parameter_declarator)));
1695   parameter->next = NULL;
1696   if (decl_specifiers)
1697     parameter->decl_specifiers = *decl_specifiers;
1698   else
1699     clear_decl_specs (&parameter->decl_specifiers);
1700   parameter->declarator = declarator;
1701   parameter->default_argument = default_argument;
1702   parameter->template_parameter_pack_p = template_parameter_pack_p;
1703   parameter->loc = loc;
1704
1705   return parameter;
1706 }
1707
1708 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1709
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1712 {
1713   while (declarator)
1714     {
1715       if (declarator->kind == cdk_function
1716           && declarator->declarator->kind == cdk_id)
1717         return true;
1718       if (declarator->kind == cdk_id
1719           || declarator->kind == cdk_decomp
1720           || declarator->kind == cdk_error)
1721         return false;
1722       declarator = declarator->declarator;
1723     }
1724   return false;
1725 }
1726  
1727 /* The parser.  */
1728
1729 /* Overview
1730    --------
1731
1732    A cp_parser parses the token stream as specified by the C++
1733    grammar.  Its job is purely parsing, not semantic analysis.  For
1734    example, the parser breaks the token stream into declarators,
1735    expressions, statements, and other similar syntactic constructs.
1736    It does not check that the types of the expressions on either side
1737    of an assignment-statement are compatible, or that a function is
1738    not declared with a parameter of type `void'.
1739
1740    The parser invokes routines elsewhere in the compiler to perform
1741    semantic analysis and to build up the abstract syntax tree for the
1742    code processed.
1743
1744    The parser (and the template instantiation code, which is, in a
1745    way, a close relative of parsing) are the only parts of the
1746    compiler that should be calling push_scope and pop_scope, or
1747    related functions.  The parser (and template instantiation code)
1748    keeps track of what scope is presently active; everything else
1749    should simply honor that.  (The code that generates static
1750    initializers may also need to set the scope, in order to check
1751    access control correctly when emitting the initializers.)
1752
1753    Methodology
1754    -----------
1755
1756    The parser is of the standard recursive-descent variety.  Upcoming
1757    tokens in the token stream are examined in order to determine which
1758    production to use when parsing a non-terminal.  Some C++ constructs
1759    require arbitrary look ahead to disambiguate.  For example, it is
1760    impossible, in the general case, to tell whether a statement is an
1761    expression or declaration without scanning the entire statement.
1762    Therefore, the parser is capable of "parsing tentatively."  When the
1763    parser is not sure what construct comes next, it enters this mode.
1764    Then, while we attempt to parse the construct, the parser queues up
1765    error messages, rather than issuing them immediately, and saves the
1766    tokens it consumes.  If the construct is parsed successfully, the
1767    parser "commits", i.e., it issues any queued error messages and
1768    the tokens that were being preserved are permanently discarded.
1769    If, however, the construct is not parsed successfully, the parser
1770    rolls back its state completely so that it can resume parsing using
1771    a different alternative.
1772
1773    Future Improvements
1774    -------------------
1775
1776    The performance of the parser could probably be improved substantially.
1777    We could often eliminate the need to parse tentatively by looking ahead
1778    a little bit.  In some places, this approach might not entirely eliminate
1779    the need to parse tentatively, but it might still speed up the average
1780    case.  */
1781
1782 /* Flags that are passed to some parsing functions.  These values can
1783    be bitwise-ored together.  */
1784
1785 enum
1786 {
1787   /* No flags.  */
1788   CP_PARSER_FLAGS_NONE = 0x0,
1789   /* The construct is optional.  If it is not present, then no error
1790      should be issued.  */
1791   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792   /* When parsing a type-specifier, treat user-defined type-names
1793      as non-type identifiers.  */
1794   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795   /* When parsing a type-specifier, do not try to parse a class-specifier
1796      or enum-specifier.  */
1797   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798   /* When parsing a decl-specifier-seq, only allow type-specifier or
1799      constexpr.  */
1800   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801   /* When parsing a decl-specifier-seq, only allow mutable or constexpr.  */
1802   CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1803 };
1804
1805 /* This type is used for parameters and variables which hold
1806    combinations of the above flags.  */
1807 typedef int cp_parser_flags;
1808
1809 /* The different kinds of declarators we want to parse.  */
1810
1811 enum cp_parser_declarator_kind
1812 {
1813   /* We want an abstract declarator.  */
1814   CP_PARSER_DECLARATOR_ABSTRACT,
1815   /* We want a named declarator.  */
1816   CP_PARSER_DECLARATOR_NAMED,
1817   /* We don't mind, but the name must be an unqualified-id.  */
1818   CP_PARSER_DECLARATOR_EITHER
1819 };
1820
1821 /* The precedence values used to parse binary expressions.  The minimum value
1822    of PREC must be 1, because zero is reserved to quickly discriminate
1823    binary operators from other tokens.  */
1824
1825 enum cp_parser_prec
1826 {
1827   PREC_NOT_OPERATOR,
1828   PREC_LOGICAL_OR_EXPRESSION,
1829   PREC_LOGICAL_AND_EXPRESSION,
1830   PREC_INCLUSIVE_OR_EXPRESSION,
1831   PREC_EXCLUSIVE_OR_EXPRESSION,
1832   PREC_AND_EXPRESSION,
1833   PREC_EQUALITY_EXPRESSION,
1834   PREC_RELATIONAL_EXPRESSION,
1835   PREC_SHIFT_EXPRESSION,
1836   PREC_ADDITIVE_EXPRESSION,
1837   PREC_MULTIPLICATIVE_EXPRESSION,
1838   PREC_PM_EXPRESSION,
1839   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840 };
1841
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843    precedence value.  */
1844
1845 struct cp_parser_binary_operations_map_node
1846 {
1847   /* The token type.  */
1848   enum cpp_ttype token_type;
1849   /* The corresponding tree code.  */
1850   enum tree_code tree_type;
1851   /* The precedence of this operator.  */
1852   enum cp_parser_prec prec;
1853 };
1854
1855 struct cp_parser_expression_stack_entry
1856 {
1857   /* Left hand side of the binary operation we are currently
1858      parsing.  */
1859   cp_expr lhs;
1860   /* Original tree code for left hand side, if it was a binary
1861      expression itself (used for -Wparentheses).  */
1862   enum tree_code lhs_type;
1863   /* Tree code for the binary operation we are parsing.  */
1864   enum tree_code tree_type;
1865   /* Precedence of the binary operation we are parsing.  */
1866   enum cp_parser_prec prec;
1867   /* Location of the binary operation we are parsing.  */
1868   location_t loc;
1869 };
1870
1871 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1872    entries because precedence levels on the stack are monotonically
1873    increasing.  */
1874 typedef struct cp_parser_expression_stack_entry
1875   cp_parser_expression_stack[NUM_PREC_VALUES];
1876
1877 /* Prototypes.  */
1878
1879 /* Constructors and destructors.  */
1880
1881 static cp_parser_context *cp_parser_context_new
1882   (cp_parser_context *);
1883
1884 /* Class variables.  */
1885
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1887
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889    Transformed into an associative array (binops_by_token) by
1890    cp_parser_new.  */
1891
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1895
1896   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1899
1900   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1902
1903   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1905
1906   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1910
1911   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1913
1914   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1915
1916   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1917
1918   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1919
1920   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1921
1922   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923 };
1924
1925 /* The same as binops, but initialized by cp_parser_new so that
1926    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1927    for speed.  */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1929
1930 /* Constructors and destructors.  */
1931
1932 /* Construct a new context.  The context below this one on the stack
1933    is given by NEXT.  */
1934
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1937 {
1938   cp_parser_context *context;
1939
1940   /* Allocate the storage.  */
1941   if (cp_parser_context_free_list != NULL)
1942     {
1943       /* Pull the first entry from the free list.  */
1944       context = cp_parser_context_free_list;
1945       cp_parser_context_free_list = context->next;
1946       memset (context, 0, sizeof (*context));
1947     }
1948   else
1949     context = ggc_cleared_alloc<cp_parser_context> ();
1950
1951   /* No errors have occurred yet in this context.  */
1952   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953   /* If this is not the bottommost context, copy information that we
1954      need from the previous context.  */
1955   if (next)
1956     {
1957       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958          expression, then we are parsing one in this context, too.  */
1959       context->object_type = next->object_type;
1960       /* Thread the stack.  */
1961       context->next = next;
1962     }
1963
1964   return context;
1965 }
1966
1967 /* Managing the unparsed function queues.  */
1968
1969 #define unparsed_funs_with_default_args \
1970   parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972   parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974   parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976   parser->unparsed_queues->last ().classes
1977
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1980 {
1981   cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982   vec_safe_push (parser->unparsed_queues, e);
1983 }
1984
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1987 {
1988   release_tree_vector (unparsed_funs_with_definitions);
1989   parser->unparsed_queues->pop ();
1990 }
1991
1992 /* Prototypes.  */
1993
1994 /* Constructors and destructors.  */
1995
1996 static cp_parser *cp_parser_new
1997   (void);
1998
1999 /* Routines to parse various constructs.
2000
2001    Those that return `tree' will return the error_mark_node (rather
2002    than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003    Sometimes, they will return an ordinary node if error-recovery was
2004    attempted, even though a parse error occurred.  So, to check
2005    whether or not a parse error occurred, you should always use
2006    cp_parser_error_occurred.  If the construct is optional (indicated
2007    either by an `_opt' in the name of the function that does the
2008    parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009    the construct is not present.  */
2010
2011 /* Lexical conventions [gram.lex]  */
2012
2013 static cp_expr cp_parser_identifier
2014   (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016   (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018   (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020   (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022   (cp_parser *);
2023
2024 /* Basic concepts [gram.basic]  */
2025
2026 static bool cp_parser_translation_unit
2027   (cp_parser *);
2028
2029 /* Expressions [gram.expr]  */
2030
2031 static cp_expr cp_parser_primary_expression
2032   (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034   (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036   (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038   (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040   (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042   (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046   (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048   (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050   (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051    bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055   (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059   (cp_token *);
2060 static tree cp_parser_new_expression
2061   (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063   (cp_parser *);
2064 static tree cp_parser_new_type_id
2065   (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067   (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069   (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071   (cp_parser *);
2072 static tree cp_parser_delete_expression
2073   (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075   (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079   (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083   (cp_parser *);
2084 static cp_expr cp_parser_expression
2085   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087   (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089   (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091   (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093   (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095   (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097   (cp_parser *, tree);
2098
2099 /* Statements [gram.stmt.stmt]  */
2100
2101 static void cp_parser_statement
2102   (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106   (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108   (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110   (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112   (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114   (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116   (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118   (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120   (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122   (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124   (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126   (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128   (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130   (tree, tree);
2131 static tree cp_parser_jump_statement
2132   (cp_parser *);
2133 static void cp_parser_declaration_statement
2134   (cp_parser *);
2135
2136 static tree cp_parser_implicitly_scoped_statement
2137   (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139   (cp_parser *, bool *, const token_indent_info &);
2140
2141 /* Declarations [gram.dcl.dcl] */
2142
2143 static void cp_parser_declaration_seq_opt
2144   (cp_parser *);
2145 static void cp_parser_declaration
2146   (cp_parser *);
2147 static void cp_parser_block_declaration
2148   (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150   (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154   (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156   (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159    int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163   (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165   (cp_parser *);
2166 static tree cp_parser_nonclass_name 
2167   (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169   (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171   (cp_parser *);
2172 static void cp_parser_enumerator_list
2173   (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175   (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177   (cp_parser *);
2178 static void cp_parser_namespace_definition
2179   (cp_parser *);
2180 static void cp_parser_namespace_body
2181   (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183   (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185   (cp_parser *);
2186 static bool cp_parser_using_declaration
2187   (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189   (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191   (cp_parser *);
2192 static void cp_parser_asm_definition
2193   (cp_parser *);
2194 static void cp_parser_linkage_specification
2195   (cp_parser *);
2196 static void cp_parser_static_assert
2197   (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199   (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201   (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2202
2203 /* Declarators [gram.dcl.decl] */
2204
2205 static tree cp_parser_init_declarator
2206   (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207    bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211   (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213   (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215   (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217   (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219   (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221   (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223   (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225   (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227   (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229   (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232   (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236   (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238   (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240   (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument 
2242   (cp_parser *, bool);
2243 static void cp_parser_function_body
2244   (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246   (cp_parser *, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248   (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250   (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252   (cp_parser *, bool *);
2253
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255   (cp_parser *, bool);
2256
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258   (cp_parser *, tree);
2259
2260 static tree cp_parser_late_parsing_oacc_routine
2261   (cp_parser *, tree);
2262
2263 static tree synthesize_implicit_template_parm
2264   (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266   (cp_parser *, tree);
2267 static void abort_fully_implicit_template
2268   (cp_parser *);
2269
2270 /* Classes [gram.class] */
2271
2272 static tree cp_parser_class_name
2273   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275   (cp_parser *);
2276 static tree cp_parser_class_head
2277   (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279   (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281   (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283   (cp_parser *);
2284 static void cp_parser_member_declaration
2285   (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287   (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289   (cp_parser *);
2290
2291 /* Derived classes [gram.class.derived] */
2292
2293 static tree cp_parser_base_clause
2294   (cp_parser *);
2295 static tree cp_parser_base_specifier
2296   (cp_parser *);
2297
2298 /* Special member functions [gram.special] */
2299
2300 static tree cp_parser_conversion_function_id
2301   (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303   (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305   (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307   (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309   (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311   (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313   (cp_parser *);
2314
2315 /* Overloading [gram.over] */
2316
2317 static cp_expr cp_parser_operator_function_id
2318   (cp_parser *);
2319 static cp_expr cp_parser_operator
2320   (cp_parser *);
2321
2322 /* Templates [gram.temp] */
2323
2324 static void cp_parser_template_declaration
2325   (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327   (cp_parser *);
2328 static tree cp_parser_template_parameter
2329   (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331   (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333   (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337   (cp_parser *);
2338 static tree cp_parser_template_argument
2339   (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341   (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343   (cp_parser *);
2344
2345 /* Exception handling [gram.exception] */
2346
2347 static tree cp_parser_try_block
2348   (cp_parser *);
2349 static void cp_parser_function_try_block
2350   (cp_parser *);
2351 static void cp_parser_handler_seq
2352   (cp_parser *);
2353 static void cp_parser_handler
2354   (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356   (cp_parser *);
2357 static tree cp_parser_throw_expression
2358   (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360   (cp_parser *);
2361 static tree cp_parser_type_id_list
2362   (cp_parser *);
2363
2364 /* GNU Extensions */
2365
2366 static tree cp_parser_asm_specification_opt
2367   (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369   (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371   (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373   (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375   (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377   (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379   (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381   (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383   (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385   (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387   (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389   (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391   (cp_parser *);
2392 static tree cp_parser_std_attribute
2393   (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395   (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397   (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399   (cp_parser *, size_t);
2400 static bool cp_parser_extension_opt
2401   (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403   (cp_parser *);
2404
2405 /* Concept Extensions */
2406
2407 static tree cp_parser_requires_clause
2408   (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410   (cp_parser *);
2411 static tree cp_parser_requires_expression
2412   (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414   (cp_parser *);
2415 static tree cp_parser_requirement_body
2416   (cp_parser *);
2417 static tree cp_parser_requirement_list
2418   (cp_parser *);
2419 static tree cp_parser_requirement
2420   (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422   (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424   (cp_parser *);
2425 static tree cp_parser_type_requirement
2426   (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428   (cp_parser *);
2429
2430 /* Transactional Memory Extensions */
2431
2432 static tree cp_parser_transaction
2433   (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435   (cp_parser *, enum rid);
2436 static void cp_parser_function_transaction
2437   (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439   (cp_parser *);
2440
2441 enum pragma_context {
2442   pragma_external,
2443   pragma_member,
2444   pragma_objc_icode,
2445   pragma_stmt,
2446   pragma_compound
2447 };
2448 static bool cp_parser_pragma
2449   (cp_parser *, enum pragma_context, bool *);
2450
2451 /* Objective-C++ Productions */
2452
2453 static tree cp_parser_objc_message_receiver
2454   (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456   (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458   (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460   (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462   (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464   (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466   (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468   (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470   (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472   (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474   (cp_parser *);
2475 static void cp_parser_objc_declaration
2476   (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478   (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480   (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration 
2482   (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration 
2484   (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486   (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488   (cp_parser *) ;
2489
2490 /* Utility Routines */
2491
2492 static cp_expr cp_parser_lookup_name
2493   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495   (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497   (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499   (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501   (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503   (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505   (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507   (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511   (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513   (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515   (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519   (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523   (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525   (cp_parser *);
2526 static void cp_parser_save_default_args
2527   (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529   (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531   (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533   (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535   (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537   (cp_parser *, enum rid);
2538 static cp_expr cp_parser_trait_expr
2539   (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541   (cp_parser *);
2542 static void cp_parser_set_storage_class
2543   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547   (cp_decl_specifier_seq *decl_specs,
2548    cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550   (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552   (cp_parser *, required_token, bool, location_t);
2553 static cp_token *cp_parser_require
2554   (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2555 static cp_token *cp_parser_require_keyword
2556   (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558   (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560   (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562   (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564   (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566   (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568   (cp_token *);
2569 static void cp_parser_check_class_key
2570   (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572   (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574   (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576   (cp_parser *);
2577 static bool cp_parser_cache_group
2578   (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580   (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582   (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584   (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586   (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588   (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590   (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592   (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594   (cp_parser *);
2595 static void cp_parser_error
2596   (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598   (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600   (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602   (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604   (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606   (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608   (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610   (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612   (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614   (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616   (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618   (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620   (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622   (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624   (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626   (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628   (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630   (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632   (cp_token *);
2633 static bool cp_parser_is_string_literal
2634   (cp_token *);
2635 static bool cp_parser_is_keyword
2636   (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638   (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640   (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642   (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644   (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646   (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648   (cp_parser *);
2649
2650 /* Concept-related syntactic transformations */
2651
2652 static tree cp_parser_maybe_concept_name       (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2654
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2657 //
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2660 {
2661   ++cp_unevaluated_operand;
2662   ++c_inhibit_evaluation_warnings;
2663 }
2664
2665 cp_unevaluated::~cp_unevaluated ()
2666 {
2667   --c_inhibit_evaluation_warnings;
2668   --cp_unevaluated_operand;
2669 }
2670
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2673
2674 /* Returns nonzero if we are parsing tentatively.  */
2675
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2678 {
2679   return parser->context->next != NULL;
2680 }
2681
2682 /* Returns nonzero if TOKEN is a string literal.  */
2683
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2686 {
2687   return (token->type == CPP_STRING ||
2688           token->type == CPP_STRING16 ||
2689           token->type == CPP_STRING32 ||
2690           token->type == CPP_WSTRING ||
2691           token->type == CPP_UTF8STRING);
2692 }
2693
2694 /* Returns nonzero if TOKEN is a string literal
2695    of a user-defined string literal.  */
2696
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2699 {
2700   return (cp_parser_is_pure_string_literal (token) ||
2701           token->type == CPP_STRING_USERDEF ||
2702           token->type == CPP_STRING16_USERDEF ||
2703           token->type == CPP_STRING32_USERDEF ||
2704           token->type == CPP_WSTRING_USERDEF ||
2705           token->type == CPP_UTF8STRING_USERDEF);
2706 }
2707
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2709
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2712 {
2713   return token->keyword == keyword;
2714 }
2715
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717    PRAGMA_NONE.  */
2718
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2721 {
2722   if (token->type != CPP_PRAGMA)
2723     return PRAGMA_NONE;
2724   /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
2725   return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2726 }
2727
2728 /* Helper function for cp_parser_error.
2729    Having peeked a token of kind TOK1_KIND that might signify
2730    a conflict marker, peek successor tokens to determine
2731    if we actually do have a conflict marker.
2732    Specifically, we consider a run of 7 '<', '=' or '>' characters
2733    at the start of a line as a conflict marker.
2734    These come through the lexer as three pairs and a single,
2735    e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736    If it returns true, *OUT_LOC is written to with the location/range
2737    of the marker.  */
2738
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741                                location_t *out_loc)
2742 {
2743   cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744   if (token2->type != tok1_kind)
2745     return false;
2746   cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747   if (token3->type != tok1_kind)
2748     return false;
2749   cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750   if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751     return false;
2752
2753   /* It must be at the start of the line.  */
2754   location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755   if (LOCATION_COLUMN (start_loc) != 1)
2756     return false;
2757
2758   /* We have a conflict marker.  Construct a location of the form:
2759        <<<<<<<
2760        ^~~~~~~
2761      with start == caret, finishing at the end of the marker.  */
2762   location_t finish_loc = get_finish (token4->location);
2763   *out_loc = make_location (start_loc, start_loc, finish_loc);
2764
2765   return true;
2766 }
2767
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769    RT_CLOSE_PAREN.  */
2770
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2773 {
2774   switch (token_desc)
2775     {
2776     default:
2777       gcc_unreachable ();
2778       return "";
2779     case RT_CLOSE_BRACE:
2780       return "{";
2781     case RT_CLOSE_PAREN:
2782       return "(";
2783     }
2784 }
2785
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787    enum cpp_ttype, returning CPP_EOF if there is no good conversion.  */
2788
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2791 {
2792   switch (token_desc)
2793     {
2794     case RT_SEMICOLON:
2795       return CPP_SEMICOLON;
2796     case RT_OPEN_PAREN:
2797       return CPP_OPEN_PAREN;
2798     case RT_CLOSE_BRACE:
2799       return CPP_CLOSE_BRACE;
2800     case RT_OPEN_BRACE:
2801       return CPP_OPEN_BRACE;
2802     case RT_CLOSE_SQUARE:
2803       return CPP_CLOSE_SQUARE;
2804     case RT_OPEN_SQUARE:
2805       return CPP_OPEN_SQUARE;
2806     case RT_COMMA:
2807       return CPP_COMMA;
2808     case RT_COLON:
2809       return CPP_COLON;
2810     case RT_CLOSE_PAREN:
2811       return CPP_CLOSE_PAREN;
2812
2813     default:
2814       /* Use CPP_EOF as a "no completions possible" code.  */
2815       return CPP_EOF;
2816     }
2817 }
2818
2819
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2821
2822    Issue a diagnostic of the form
2823       FILE:LINE: MESSAGE before TOKEN
2824    where TOKEN is the next token in the input stream.  MESSAGE
2825    (specified by the caller) is usually of the form "expected
2826    OTHER-TOKEN".
2827
2828    This bypasses the check for tentative passing, and potentially
2829    adds material needed by cp_parser_required_error.
2830
2831    If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832    suggesting insertion of the missing token.
2833
2834    Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835    have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836    location.  */
2837
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840                    required_token missing_token_desc,
2841                    location_t matching_location)
2842 {
2843   cp_token *token = cp_lexer_peek_token (parser->lexer);
2844   /* This diagnostic makes more sense if it is tagged to the line
2845      of the token we just peeked at.  */
2846   cp_lexer_set_source_position_from_token (token);
2847
2848   if (token->type == CPP_PRAGMA)
2849     {
2850       error_at (token->location,
2851                 "%<#pragma%> is not allowed here");
2852       cp_parser_skip_to_pragma_eol (parser, token);
2853       return;
2854     }
2855
2856   /* If this is actually a conflict marker, report it as such.  */
2857   if (token->type == CPP_LSHIFT
2858       || token->type == CPP_RSHIFT
2859       || token->type == CPP_EQ_EQ)
2860     {
2861       location_t loc;
2862       if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2863         {
2864           error_at (loc, "version control conflict marker in file");
2865           return;
2866         }
2867     }
2868
2869   gcc_rich_location richloc (input_location);
2870
2871   bool added_matching_location = false;
2872
2873   if (missing_token_desc != RT_NONE)
2874     {
2875       /* Potentially supply a fix-it hint, suggesting to add the
2876          missing token immediately after the *previous* token.
2877          This may move the primary location within richloc.  */
2878       enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2879       location_t prev_token_loc
2880         = cp_lexer_previous_token (parser->lexer)->location;
2881       maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2882
2883       /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884          Attempt to consolidate diagnostics by printing it as a
2885         secondary range within the main diagnostic.  */
2886       if (matching_location != UNKNOWN_LOCATION)
2887         added_matching_location
2888           = richloc.add_location_if_nearby (matching_location);
2889     }
2890
2891   /* Actually emit the error.  */
2892   c_parse_error (gmsgid,
2893                  /* Because c_parser_error does not understand
2894                     CPP_KEYWORD, keywords are treated like
2895                     identifiers.  */
2896                  (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2897                  token->u.value, token->flags, &richloc);
2898
2899   if (missing_token_desc != RT_NONE)
2900     {
2901       /* If we weren't able to consolidate matching_location, then
2902          print it as a secondary diagnostic.  */
2903       if (matching_location != UNKNOWN_LOCATION
2904           && !added_matching_location)
2905         inform (matching_location, "to match this %qs",
2906                 get_matching_symbol (missing_token_desc));
2907     }
2908 }
2909
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911       FILE:LINE: MESSAGE before TOKEN
2912    where TOKEN is the next token in the input stream.  MESSAGE
2913    (specified by the caller) is usually of the form "expected
2914    OTHER-TOKEN".  */
2915
2916 static void
2917 cp_parser_error (cp_parser* parser, const char* gmsgid)
2918 {
2919   if (!cp_parser_simulate_error (parser))
2920     cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2921 }
2922
2923 /* Issue an error about name-lookup failing.  NAME is the
2924    IDENTIFIER_NODE DECL is the result of
2925    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2926    the thing that we hoped to find.  */
2927
2928 static void
2929 cp_parser_name_lookup_error (cp_parser* parser,
2930                              tree name,
2931                              tree decl,
2932                              name_lookup_error desired,
2933                              location_t location)
2934 {
2935   /* If name lookup completely failed, tell the user that NAME was not
2936      declared.  */
2937   if (decl == error_mark_node)
2938     {
2939       if (parser->scope && parser->scope != global_namespace)
2940         error_at (location, "%<%E::%E%> has not been declared",
2941                   parser->scope, name);
2942       else if (parser->scope == global_namespace)
2943         error_at (location, "%<::%E%> has not been declared", name);
2944       else if (parser->object_scope
2945                && !CLASS_TYPE_P (parser->object_scope))
2946         error_at (location, "request for member %qE in non-class type %qT",
2947                   name, parser->object_scope);
2948       else if (parser->object_scope)
2949         error_at (location, "%<%T::%E%> has not been declared",
2950                   parser->object_scope, name);
2951       else
2952         error_at (location, "%qE has not been declared", name);
2953     }
2954   else if (parser->scope && parser->scope != global_namespace)
2955     {
2956       switch (desired)
2957         {
2958           case NLE_TYPE:
2959             error_at (location, "%<%E::%E%> is not a type",
2960                                 parser->scope, name);
2961             break;
2962           case NLE_CXX98:
2963             error_at (location, "%<%E::%E%> is not a class or namespace",
2964                                 parser->scope, name);
2965             break;
2966           case NLE_NOT_CXX98:
2967             error_at (location,
2968                       "%<%E::%E%> is not a class, namespace, or enumeration",
2969                       parser->scope, name);
2970             break;
2971           default:
2972             gcc_unreachable ();
2973             
2974         }
2975     }
2976   else if (parser->scope == global_namespace)
2977     {
2978       switch (desired)
2979         {
2980           case NLE_TYPE:
2981             error_at (location, "%<::%E%> is not a type", name);
2982             break;
2983           case NLE_CXX98:
2984             error_at (location, "%<::%E%> is not a class or namespace", name);
2985             break;
2986           case NLE_NOT_CXX98:
2987             error_at (location,
2988                       "%<::%E%> is not a class, namespace, or enumeration",
2989                       name);
2990             break;
2991           default:
2992             gcc_unreachable ();
2993         }
2994     }
2995   else
2996     {
2997       switch (desired)
2998         {
2999           case NLE_TYPE:
3000             error_at (location, "%qE is not a type", name);
3001             break;
3002           case NLE_CXX98:
3003             error_at (location, "%qE is not a class or namespace", name);
3004             break;
3005           case NLE_NOT_CXX98:
3006             error_at (location,
3007                       "%qE is not a class, namespace, or enumeration", name);
3008             break;
3009           default:
3010             gcc_unreachable ();
3011         }
3012     }
3013 }
3014
3015 /* If we are parsing tentatively, remember that an error has occurred
3016    during this tentative parse.  Returns true if the error was
3017    simulated; false if a message should be issued by the caller.  */
3018
3019 static bool
3020 cp_parser_simulate_error (cp_parser* parser)
3021 {
3022   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3023     {
3024       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3025       return true;
3026     }
3027   return false;
3028 }
3029
3030 /* This function is called when a type is defined.  If type
3031    definitions are forbidden at this point, an error message is
3032    issued.  */
3033
3034 static bool
3035 cp_parser_check_type_definition (cp_parser* parser)
3036 {
3037   /* If types are forbidden here, issue a message.  */
3038   if (parser->type_definition_forbidden_message)
3039     {
3040       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041          in the message need to be interpreted.  */
3042       error (parser->type_definition_forbidden_message);
3043       return false;
3044     }
3045   return true;
3046 }
3047
3048 /* This function is called when the DECLARATOR is processed.  The TYPE
3049    was a type defined in the decl-specifiers.  If it is invalid to
3050    define a type in the decl-specifiers for DECLARATOR, an error is
3051    issued. TYPE_LOCATION is the location of TYPE and is used
3052    for error reporting.  */
3053
3054 static void
3055 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3056                                                tree type, location_t type_location)
3057 {
3058   /* [dcl.fct] forbids type definitions in return types.
3059      Unfortunately, it's not easy to know whether or not we are
3060      processing a return type until after the fact.  */
3061   while (declarator
3062          && (declarator->kind == cdk_pointer
3063              || declarator->kind == cdk_reference
3064              || declarator->kind == cdk_ptrmem))
3065     declarator = declarator->declarator;
3066   if (declarator
3067       && declarator->kind == cdk_function)
3068     {
3069       error_at (type_location,
3070                 "new types may not be defined in a return type");
3071       inform (type_location, 
3072               "(perhaps a semicolon is missing after the definition of %qT)",
3073               type);
3074     }
3075 }
3076
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078    "<" in any valid C++ program.  If the next token is indeed "<",
3079    issue a message warning the user about what appears to be an
3080    invalid attempt to form a template-id. LOCATION is the location
3081    of the type-specifier (TYPE) */
3082
3083 static void
3084 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3085                                          tree type,
3086                                          enum tag_types tag_type,
3087                                          location_t location)
3088 {
3089   cp_token_position start = 0;
3090
3091   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3092     {
3093       if (TREE_CODE (type) == TYPE_DECL)
3094         type = TREE_TYPE (type);
3095       if (TYPE_P (type) && !template_placeholder_p (type))
3096         error_at (location, "%qT is not a template", type);
3097       else if (identifier_p (type))
3098         {
3099           if (tag_type != none_type)
3100             error_at (location, "%qE is not a class template", type);
3101           else
3102             error_at (location, "%qE is not a template", type);
3103         }
3104       else
3105         error_at (location, "invalid template-id");
3106       /* Remember the location of the invalid "<".  */
3107       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3108         start = cp_lexer_token_position (parser->lexer, true);
3109       /* Consume the "<".  */
3110       cp_lexer_consume_token (parser->lexer);
3111       /* Parse the template arguments.  */
3112       cp_parser_enclosed_template_argument_list (parser);
3113       /* Permanently remove the invalid template arguments so that
3114          this error message is not issued again.  */
3115       if (start)
3116         cp_lexer_purge_tokens_after (parser->lexer, start);
3117     }
3118 }
3119
3120 /* If parsing an integral constant-expression, issue an error message
3121    about the fact that THING appeared and return true.  Otherwise,
3122    return false.  In either case, set
3123    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
3124
3125 static bool
3126 cp_parser_non_integral_constant_expression (cp_parser  *parser,
3127                                             non_integral_constant thing)
3128 {
3129   parser->non_integral_constant_expression_p = true;
3130   if (parser->integral_constant_expression_p)
3131     {
3132       if (!parser->allow_non_integral_constant_expression_p)
3133         {
3134           const char *msg = NULL;
3135           switch (thing)
3136             {
3137               case NIC_FLOAT:
3138                 pedwarn (input_location, OPT_Wpedantic,
3139                          "ISO C++ forbids using a floating-point literal "
3140                          "in a constant-expression");
3141                 return true;
3142               case NIC_CAST:
3143                 error ("a cast to a type other than an integral or "
3144                        "enumeration type cannot appear in a "
3145                        "constant-expression");
3146                 return true;
3147               case NIC_TYPEID:
3148                 error ("%<typeid%> operator "
3149                        "cannot appear in a constant-expression");
3150                 return true;
3151               case NIC_NCC:
3152                 error ("non-constant compound literals "
3153                        "cannot appear in a constant-expression");
3154                 return true;
3155               case NIC_FUNC_CALL:
3156                 error ("a function call "
3157                        "cannot appear in a constant-expression");
3158                 return true;
3159               case NIC_INC:
3160                 error ("an increment "
3161                        "cannot appear in a constant-expression");
3162                 return true;
3163               case NIC_DEC:
3164                 error ("an decrement "
3165                        "cannot appear in a constant-expression");
3166                 return true;
3167               case NIC_ARRAY_REF:
3168                 error ("an array reference "
3169                        "cannot appear in a constant-expression");
3170                 return true;
3171               case NIC_ADDR_LABEL:
3172                 error ("the address of a label "
3173                        "cannot appear in a constant-expression");
3174                 return true;
3175               case NIC_OVERLOADED:
3176                 error ("calls to overloaded operators "
3177                        "cannot appear in a constant-expression");
3178                 return true;
3179               case NIC_ASSIGNMENT:
3180                 error ("an assignment cannot appear in a constant-expression");
3181                 return true;
3182               case NIC_COMMA:
3183                 error ("a comma operator "
3184                        "cannot appear in a constant-expression");
3185                 return true;
3186               case NIC_CONSTRUCTOR:
3187                 error ("a call to a constructor "
3188                        "cannot appear in a constant-expression");
3189                 return true;
3190               case NIC_TRANSACTION:
3191                 error ("a transaction expression "
3192                        "cannot appear in a constant-expression");
3193                 return true;
3194               case NIC_THIS:
3195                 msg = "this";
3196                 break;
3197               case NIC_FUNC_NAME:
3198                 msg = "__FUNCTION__";
3199                 break;
3200               case NIC_PRETTY_FUNC:
3201                 msg = "__PRETTY_FUNCTION__";
3202                 break;
3203               case NIC_C99_FUNC:
3204                 msg = "__func__";
3205                 break;
3206               case NIC_VA_ARG:
3207                 msg = "va_arg";
3208                 break;
3209               case NIC_ARROW:
3210                 msg = "->";
3211                 break;
3212               case NIC_POINT:
3213                 msg = ".";
3214                 break;
3215               case NIC_STAR:
3216                 msg = "*";
3217                 break;
3218               case NIC_ADDR:
3219                 msg = "&";
3220                 break;
3221               case NIC_PREINCREMENT:
3222                 msg = "++";
3223                 break;
3224               case NIC_PREDECREMENT:
3225                 msg = "--";
3226                 break;
3227               case NIC_NEW:
3228                 msg = "new";
3229                 break;
3230               case NIC_DEL:
3231                 msg = "delete";
3232                 break;
3233               default:
3234                 gcc_unreachable ();
3235             }
3236           if (msg)
3237             error ("%qs cannot appear in a constant-expression", msg);
3238           return true;
3239         }
3240     }
3241   return false;
3242 }
3243
3244 /* Emit a diagnostic for an invalid type name.  This function commits
3245    to the current active tentative parse, if any.  (Otherwise, the
3246    problematic construct might be encountered again later, resulting
3247    in duplicate error messages.) LOCATION is the location of ID.  */
3248
3249 static void
3250 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3251                                       location_t location)
3252 {
3253   tree decl, ambiguous_decls;
3254   cp_parser_commit_to_tentative_parse (parser);
3255   /* Try to lookup the identifier.  */
3256   decl = cp_parser_lookup_name (parser, id, none_type,
3257                                 /*is_template=*/false,
3258                                 /*is_namespace=*/false,
3259                                 /*check_dependency=*/true,
3260                                 &ambiguous_decls, location);
3261   if (ambiguous_decls)
3262     /* If the lookup was ambiguous, an error will already have
3263        been issued.  */
3264     return;
3265   /* If the lookup found a template-name, it means that the user forgot
3266   to specify an argument list. Emit a useful error message.  */
3267   if (DECL_TYPE_TEMPLATE_P (decl))
3268     {
3269       error_at (location,
3270                 "invalid use of template-name %qE without an argument list",
3271                 decl);
3272       if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3273         inform (location, "class template argument deduction is only available "
3274                 "with -std=c++17 or -std=gnu++17");
3275       inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3276     }
3277   else if (TREE_CODE (id) == BIT_NOT_EXPR)
3278     error_at (location, "invalid use of destructor %qD as a type", id);
3279   else if (TREE_CODE (decl) == TYPE_DECL)
3280     /* Something like 'unsigned A a;'  */
3281     error_at (location, "invalid combination of multiple type-specifiers");
3282   else if (!parser->scope)
3283     {
3284       /* Issue an error message.  */
3285       name_hint hint;
3286       if (TREE_CODE (id) == IDENTIFIER_NODE)
3287         hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3288       if (hint)
3289         {
3290           gcc_rich_location richloc (location);
3291           richloc.add_fixit_replace (hint.suggestion ());
3292           error_at (&richloc,
3293                     "%qE does not name a type; did you mean %qs?",
3294                     id, hint.suggestion ());
3295         }
3296       else
3297         error_at (location, "%qE does not name a type", id);
3298       /* If we're in a template class, it's possible that the user was
3299          referring to a type from a base class.  For example:
3300
3301            template <typename T> struct A { typedef T X; };
3302            template <typename T> struct B : public A<T> { X x; };
3303
3304          The user should have said "typename A<T>::X".  */
3305       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3306         inform (location, "C++11 %<constexpr%> only available with "
3307                 "-std=c++11 or -std=gnu++11");
3308       else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3309         inform (location, "C++11 %<noexcept%> only available with "
3310                 "-std=c++11 or -std=gnu++11");
3311       else if (cxx_dialect < cxx11
3312                && TREE_CODE (id) == IDENTIFIER_NODE
3313                && id_equal (id, "thread_local"))
3314         inform (location, "C++11 %<thread_local%> only available with "
3315                 "-std=c++11 or -std=gnu++11");
3316       else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3317         inform (location, "%<concept%> only available with -fconcepts");
3318       else if (processing_template_decl && current_class_type
3319                && TYPE_BINFO (current_class_type))
3320         {
3321           tree b;
3322
3323           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3324                b;
3325                b = TREE_CHAIN (b))
3326             {
3327               tree base_type = BINFO_TYPE (b);
3328               if (CLASS_TYPE_P (base_type)
3329                   && dependent_type_p (base_type))
3330                 {
3331                   tree field;
3332                   /* Go from a particular instantiation of the
3333                      template (which will have an empty TYPE_FIELDs),
3334                      to the main version.  */
3335                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3336                   for (field = TYPE_FIELDS (base_type);
3337                        field;
3338                        field = DECL_CHAIN (field))
3339                     if (TREE_CODE (field) == TYPE_DECL
3340                         && DECL_NAME (field) == id)
3341                       {
3342                         inform (location, 
3343                                 "(perhaps %<typename %T::%E%> was intended)",
3344                                 BINFO_TYPE (b), id);
3345                         break;
3346                       }
3347                   if (field)
3348                     break;
3349                 }
3350             }
3351         }
3352     }
3353   /* Here we diagnose qualified-ids where the scope is actually correct,
3354      but the identifier does not resolve to a valid type name.  */
3355   else if (parser->scope != error_mark_node)
3356     {
3357       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3358         {
3359           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3360             error_at (location_of (id),
3361                       "%qE in namespace %qE does not name a template type",
3362                       id, parser->scope);
3363           else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3364             error_at (location_of (id),
3365                       "%qE in namespace %qE does not name a template type",
3366                       TREE_OPERAND (id, 0), parser->scope);
3367           else
3368             error_at (location_of (id),
3369                       "%qE in namespace %qE does not name a type",
3370                       id, parser->scope);
3371           if (DECL_P (decl))
3372             inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3373           else if (decl == error_mark_node)
3374             suggest_alternative_in_explicit_scope (location, id,
3375                                                    parser->scope);
3376         }
3377       else if (CLASS_TYPE_P (parser->scope)
3378                && constructor_name_p (id, parser->scope))
3379         {
3380           /* A<T>::A<T>() */
3381           error_at (location, "%<%T::%E%> names the constructor, not"
3382                     " the type", parser->scope, id);
3383           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3384             error_at (location, "and %qT has no template constructors",
3385                       parser->scope);
3386         }
3387       else if (TYPE_P (parser->scope)
3388                && dependent_scope_p (parser->scope))
3389         {
3390           if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3391             error_at (location,
3392                       "need %<typename%> before %<%T::%D::%E%> because "
3393                       "%<%T::%D%> is a dependent scope",
3394                       TYPE_CONTEXT (parser->scope),
3395                       TYPENAME_TYPE_FULLNAME (parser->scope),
3396                       id,
3397                       TYPE_CONTEXT (parser->scope),
3398                       TYPENAME_TYPE_FULLNAME (parser->scope));
3399           else
3400             error_at (location, "need %<typename%> before %<%T::%E%> because "
3401                       "%qT is a dependent scope",
3402                       parser->scope, id, parser->scope);
3403         }
3404       else if (TYPE_P (parser->scope))
3405         {
3406           if (!COMPLETE_TYPE_P (parser->scope))
3407             cxx_incomplete_type_error (location_of (id), NULL_TREE,
3408                                        parser->scope);
3409           else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3410             error_at (location_of (id),
3411                       "%qE in %q#T does not name a template type",
3412                       id, parser->scope);
3413           else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3414             error_at (location_of (id),
3415                       "%qE in %q#T does not name a template type",
3416                       TREE_OPERAND (id, 0), parser->scope);
3417           else
3418             error_at (location_of (id),
3419                       "%qE in %q#T does not name a type",
3420                       id, parser->scope);
3421           if (DECL_P (decl))
3422             inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3423         }
3424       else
3425         gcc_unreachable ();
3426     }
3427 }
3428
3429 /* Check for a common situation where a type-name should be present,
3430    but is not, and issue a sensible error message.  Returns true if an
3431    invalid type-name was detected.
3432
3433    The situation handled by this function are variable declarations of the
3434    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435    Usually, `ID' should name a type, but if we got here it means that it
3436    does not. We try to emit the best possible error message depending on
3437    how exactly the id-expression looks like.  */
3438
3439 static bool
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3441 {
3442   tree id;
3443   cp_token *token = cp_lexer_peek_token (parser->lexer);
3444
3445   /* Avoid duplicate error about ambiguous lookup.  */
3446   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3447     {
3448       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3449       if (next->type == CPP_NAME && next->error_reported)
3450         goto out;
3451     }
3452
3453   cp_parser_parse_tentatively (parser);
3454   id = cp_parser_id_expression (parser,
3455                                 /*template_keyword_p=*/false,
3456                                 /*check_dependency_p=*/true,
3457                                 /*template_p=*/NULL,
3458                                 /*declarator_p=*/false,
3459                                 /*optional_p=*/false);
3460   /* If the next token is a (, this is a function with no explicit return
3461      type, i.e. constructor, destructor or conversion op.  */
3462   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3463       || TREE_CODE (id) == TYPE_DECL)
3464     {
3465       cp_parser_abort_tentative_parse (parser);
3466       return false;
3467     }
3468   if (!cp_parser_parse_definitely (parser))
3469     return false;
3470
3471   /* Emit a diagnostic for the invalid type.  */
3472   cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3473  out:
3474   /* If we aren't in the middle of a declarator (i.e. in a
3475      parameter-declaration-clause), skip to the end of the declaration;
3476      there's no point in trying to process it.  */
3477   if (!parser->in_declarator_p)
3478     cp_parser_skip_to_end_of_block_or_statement (parser);
3479   return true;
3480 }
3481
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3484    are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485    found an unnested token of that type.  */
3486
3487 static int
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3489                                          bool recovering,
3490                                          cpp_ttype or_ttype,
3491                                          bool consume_paren)
3492 {
3493   unsigned paren_depth = 0;
3494   unsigned brace_depth = 0;
3495   unsigned square_depth = 0;
3496
3497   if (recovering && or_ttype == CPP_EOF
3498       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3499     return 0;
3500
3501   while (true)
3502     {
3503       cp_token * token = cp_lexer_peek_token (parser->lexer);
3504
3505       /* Have we found what we're looking for before the closing paren?  */
3506       if (token->type == or_ttype && or_ttype != CPP_EOF
3507           && !brace_depth && !paren_depth && !square_depth)
3508         return -1;
3509
3510       switch (token->type)
3511         {
3512         case CPP_EOF:
3513         case CPP_PRAGMA_EOL:
3514           /* If we've run out of tokens, then there is no closing `)'.  */
3515           return 0;
3516
3517         /* This is good for lambda expression capture-lists.  */
3518         case CPP_OPEN_SQUARE:
3519           ++square_depth;
3520           break;
3521         case CPP_CLOSE_SQUARE:
3522           if (!square_depth--)
3523             return 0;
3524           break;
3525
3526         case CPP_SEMICOLON:
3527           /* This matches the processing in skip_to_end_of_statement.  */
3528           if (!brace_depth)
3529             return 0;
3530           break;
3531
3532         case CPP_OPEN_BRACE:
3533           ++brace_depth;
3534           break;
3535         case CPP_CLOSE_BRACE:
3536           if (!brace_depth--)
3537             return 0;
3538           break;
3539
3540         case CPP_OPEN_PAREN:
3541           if (!brace_depth)
3542             ++paren_depth;
3543           break;
3544
3545         case CPP_CLOSE_PAREN:
3546           if (!brace_depth && !paren_depth--)
3547             {
3548               if (consume_paren)
3549                 cp_lexer_consume_token (parser->lexer);
3550               return 1;
3551             }
3552           break;
3553
3554         default:
3555           break;
3556         }
3557
3558       /* Consume the token.  */
3559       cp_lexer_consume_token (parser->lexer);
3560     }
3561 }
3562
3563 /* Consume tokens up to, and including, the next non-nested closing `)'.
3564    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3565    are doing error recovery. Returns -1 if OR_COMMA is true and we
3566    found an unnested token of that type.  */
3567
3568 static int
3569 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3570                                        bool recovering,
3571                                        bool or_comma,
3572                                        bool consume_paren)
3573 {
3574   cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3575   return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3576                                                   ttype, consume_paren);
3577 }
3578
3579 /* Consume tokens until we reach the end of the current statement.
3580    Normally, that will be just before consuming a `;'.  However, if a
3581    non-nested `}' comes first, then we stop before consuming that.  */
3582
3583 static void
3584 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3585 {
3586   unsigned nesting_depth = 0;
3587
3588   /* Unwind generic function template scope if necessary.  */
3589   if (parser->fully_implicit_function_template_p)
3590     abort_fully_implicit_template (parser);
3591
3592   while (true)
3593     {
3594       cp_token *token = cp_lexer_peek_token (parser->lexer);
3595
3596       switch (token->type)
3597         {
3598         case CPP_EOF:
3599         case CPP_PRAGMA_EOL:
3600           /* If we've run out of tokens, stop.  */
3601           return;
3602
3603         case CPP_SEMICOLON:
3604           /* If the next token is a `;', we have reached the end of the
3605              statement.  */
3606           if (!nesting_depth)
3607             return;
3608           break;
3609
3610         case CPP_CLOSE_BRACE:
3611           /* If this is a non-nested '}', stop before consuming it.
3612              That way, when confronted with something like:
3613
3614                { 3 + }
3615
3616              we stop before consuming the closing '}', even though we
3617              have not yet reached a `;'.  */
3618           if (nesting_depth == 0)
3619             return;
3620
3621           /* If it is the closing '}' for a block that we have
3622              scanned, stop -- but only after consuming the token.
3623              That way given:
3624
3625                 void f g () { ... }
3626                 typedef int I;
3627
3628              we will stop after the body of the erroneously declared
3629              function, but before consuming the following `typedef'
3630              declaration.  */
3631           if (--nesting_depth == 0)
3632             {
3633               cp_lexer_consume_token (parser->lexer);
3634               return;
3635             }
3636           break;
3637
3638         case CPP_OPEN_BRACE:
3639           ++nesting_depth;
3640           break;
3641
3642         default:
3643           break;
3644         }
3645
3646       /* Consume the token.  */
3647       cp_lexer_consume_token (parser->lexer);
3648     }
3649 }
3650
3651 /* This function is called at the end of a statement or declaration.
3652    If the next token is a semicolon, it is consumed; otherwise, error
3653    recovery is attempted.  */
3654
3655 static void
3656 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3657 {
3658   /* Look for the trailing `;'.  */
3659   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3660     {
3661       /* If there is additional (erroneous) input, skip to the end of
3662          the statement.  */
3663       cp_parser_skip_to_end_of_statement (parser);
3664       /* If the next token is now a `;', consume it.  */
3665       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3666         cp_lexer_consume_token (parser->lexer);
3667     }
3668 }
3669
3670 /* Skip tokens until we have consumed an entire block, or until we
3671    have consumed a non-nested `;'.  */
3672
3673 static void
3674 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3675 {
3676   int nesting_depth = 0;
3677
3678   /* Unwind generic function template scope if necessary.  */
3679   if (parser->fully_implicit_function_template_p)
3680     abort_fully_implicit_template (parser);
3681
3682   while (nesting_depth >= 0)
3683     {
3684       cp_token *token = cp_lexer_peek_token (parser->lexer);
3685
3686       switch (token->type)
3687         {
3688         case CPP_EOF:
3689         case CPP_PRAGMA_EOL:
3690           /* If we've run out of tokens, stop.  */
3691           return;
3692
3693         case CPP_SEMICOLON:
3694           /* Stop if this is an unnested ';'. */
3695           if (!nesting_depth)
3696             nesting_depth = -1;
3697           break;
3698
3699         case CPP_CLOSE_BRACE:
3700           /* Stop if this is an unnested '}', or closes the outermost
3701              nesting level.  */
3702           nesting_depth--;
3703           if (nesting_depth < 0)
3704             return;
3705           if (!nesting_depth)
3706             nesting_depth = -1;
3707           break;
3708
3709         case CPP_OPEN_BRACE:
3710           /* Nest. */
3711           nesting_depth++;
3712           break;
3713
3714         default:
3715           break;
3716         }
3717
3718       /* Consume the token.  */
3719       cp_lexer_consume_token (parser->lexer);
3720     }
3721 }
3722
3723 /* Skip tokens until a non-nested closing curly brace is the next
3724    token, or there are no more tokens. Return true in the first case,
3725    false otherwise.  */
3726
3727 static bool
3728 cp_parser_skip_to_closing_brace (cp_parser *parser)
3729 {
3730   unsigned nesting_depth = 0;
3731
3732   while (true)
3733     {
3734       cp_token *token = cp_lexer_peek_token (parser->lexer);
3735
3736       switch (token->type)
3737         {
3738         case CPP_EOF:
3739         case CPP_PRAGMA_EOL:
3740           /* If we've run out of tokens, stop.  */
3741           return false;
3742
3743         case CPP_CLOSE_BRACE:
3744           /* If the next token is a non-nested `}', then we have reached
3745              the end of the current block.  */
3746           if (nesting_depth-- == 0)
3747             return true;
3748           break;
3749
3750         case CPP_OPEN_BRACE:
3751           /* If it the next token is a `{', then we are entering a new
3752              block.  Consume the entire block.  */
3753           ++nesting_depth;
3754           break;
3755
3756         default:
3757           break;
3758         }
3759
3760       /* Consume the token.  */
3761       cp_lexer_consume_token (parser->lexer);
3762     }
3763 }
3764
3765 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3766    parameter is the PRAGMA token, allowing us to purge the entire pragma
3767    sequence.  */
3768
3769 static void
3770 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3771 {
3772   cp_token *token;
3773
3774   parser->lexer->in_pragma = false;
3775
3776   do
3777     token = cp_lexer_consume_token (parser->lexer);
3778   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3779
3780   /* Ensure that the pragma is not parsed again.  */
3781   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3782 }
3783
3784 /* Require pragma end of line, resyncing with it as necessary.  The
3785    arguments are as for cp_parser_skip_to_pragma_eol.  */
3786
3787 static void
3788 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3789 {
3790   parser->lexer->in_pragma = false;
3791   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3792     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3793 }
3794
3795 /* This is a simple wrapper around make_typename_type. When the id is
3796    an unresolved identifier node, we can provide a superior diagnostic
3797    using cp_parser_diagnose_invalid_type_name.  */
3798
3799 static tree
3800 cp_parser_make_typename_type (cp_parser *parser, tree id,
3801                               location_t id_location)
3802 {
3803   tree result;
3804   if (identifier_p (id))
3805     {
3806       result = make_typename_type (parser->scope, id, typename_type,
3807                                    /*complain=*/tf_none);
3808       if (result == error_mark_node)
3809         cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3810       return result;
3811     }
3812   return make_typename_type (parser->scope, id, typename_type, tf_error);
3813 }
3814
3815 /* This is a wrapper around the
3816    make_{pointer,ptrmem,reference}_declarator functions that decides
3817    which one to call based on the CODE and CLASS_TYPE arguments. The
3818    CODE argument should be one of the values returned by
3819    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3820    appertain to the pointer or reference.  */
3821
3822 static cp_declarator *
3823 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3824                                     cp_cv_quals cv_qualifiers,
3825                                     cp_declarator *target,
3826                                     tree attributes)
3827 {
3828   if (code == ERROR_MARK || target == cp_error_declarator)
3829     return cp_error_declarator;
3830
3831   if (code == INDIRECT_REF)
3832     if (class_type == NULL_TREE)
3833       return make_pointer_declarator (cv_qualifiers, target, attributes);
3834     else
3835       return make_ptrmem_declarator (cv_qualifiers, class_type,
3836                                      target, attributes);
3837   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3838     return make_reference_declarator (cv_qualifiers, target,
3839                                       false, attributes);
3840   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3841     return make_reference_declarator (cv_qualifiers, target,
3842                                       true, attributes);
3843   gcc_unreachable ();
3844 }
3845
3846 /* Create a new C++ parser.  */
3847
3848 static cp_parser *
3849 cp_parser_new (void)
3850 {
3851   cp_parser *parser;
3852   cp_lexer *lexer;
3853   unsigned i;
3854
3855   /* cp_lexer_new_main is called before doing GC allocation because
3856      cp_lexer_new_main might load a PCH file.  */
3857   lexer = cp_lexer_new_main ();
3858
3859   /* Initialize the binops_by_token so that we can get the tree
3860      directly from the token.  */
3861   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3862     binops_by_token[binops[i].token_type] = binops[i];
3863
3864   parser = ggc_cleared_alloc<cp_parser> ();
3865   parser->lexer = lexer;
3866   parser->context = cp_parser_context_new (NULL);
3867
3868   /* For now, we always accept GNU extensions.  */
3869   parser->allow_gnu_extensions_p = 1;
3870
3871   /* The `>' token is a greater-than operator, not the end of a
3872      template-id.  */
3873   parser->greater_than_is_operator_p = true;
3874
3875   parser->default_arg_ok_p = true;
3876
3877   /* We are not parsing a constant-expression.  */
3878   parser->integral_constant_expression_p = false;
3879   parser->allow_non_integral_constant_expression_p = false;
3880   parser->non_integral_constant_expression_p = false;
3881
3882   /* Local variable names are not forbidden.  */
3883   parser->local_variables_forbidden_p = false;
3884
3885   /* We are not processing an `extern "C"' declaration.  */
3886   parser->in_unbraced_linkage_specification_p = false;
3887
3888   /* We are not processing a declarator.  */
3889   parser->in_declarator_p = false;
3890
3891   /* We are not processing a template-argument-list.  */
3892   parser->in_template_argument_list_p = false;
3893
3894   /* We are not in an iteration statement.  */
3895   parser->in_statement = 0;
3896
3897   /* We are not in a switch statement.  */
3898   parser->in_switch_statement_p = false;
3899
3900   /* We are not parsing a type-id inside an expression.  */
3901   parser->in_type_id_in_expr_p = false;
3902
3903   /* Declarations aren't implicitly extern "C".  */
3904   parser->implicit_extern_c = false;
3905
3906   /* String literals should be translated to the execution character set.  */
3907   parser->translate_strings_p = true;
3908
3909   /* We are not parsing a function body.  */
3910   parser->in_function_body = false;
3911
3912   /* We can correct until told otherwise.  */
3913   parser->colon_corrects_to_scope_p = true;
3914
3915   /* The unparsed function queue is empty.  */
3916   push_unparsed_function_queues (parser);
3917
3918   /* There are no classes being defined.  */
3919   parser->num_classes_being_defined = 0;
3920
3921   /* No template parameters apply.  */
3922   parser->num_template_parameter_lists = 0;
3923
3924   /* Special parsing data structures.  */
3925   parser->omp_declare_simd = NULL;
3926   parser->oacc_routine = NULL;
3927
3928   /* Not declaring an implicit function template.  */
3929   parser->auto_is_implicit_function_template_parm_p = false;
3930   parser->fully_implicit_function_template_p = false;
3931   parser->implicit_template_parms = 0;
3932   parser->implicit_template_scope = 0;
3933
3934   /* Allow constrained-type-specifiers. */
3935   parser->prevent_constrained_type_specifiers = 0;
3936
3937   /* We haven't yet seen an 'extern "C"'.  */
3938   parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3939
3940   return parser;
3941 }
3942
3943 /* Create a cp_lexer structure which will emit the tokens in CACHE
3944    and push it onto the parser's lexer stack.  This is used for delayed
3945    parsing of in-class method bodies and default arguments, and should
3946    not be confused with tentative parsing.  */
3947 static void
3948 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3949 {
3950   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3951   lexer->next = parser->lexer;
3952   parser->lexer = lexer;
3953
3954   /* Move the current source position to that of the first token in the
3955      new lexer.  */
3956   cp_lexer_set_source_position_from_token (lexer->next_token);
3957 }
3958
3959 /* Pop the top lexer off the parser stack.  This is never used for the
3960    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3961 static void
3962 cp_parser_pop_lexer (cp_parser *parser)
3963 {
3964   cp_lexer *lexer = parser->lexer;
3965   parser->lexer = lexer->next;
3966   cp_lexer_destroy (lexer);
3967
3968   /* Put the current source position back where it was before this
3969      lexer was pushed.  */
3970   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3971 }
3972
3973 /* Lexical conventions [gram.lex]  */
3974
3975 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3976    identifier.  */
3977
3978 static cp_expr
3979 cp_parser_identifier (cp_parser* parser)
3980 {
3981   cp_token *token;
3982
3983   /* Look for the identifier.  */
3984   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3985   /* Return the value.  */
3986   if (token)
3987     return cp_expr (token->u.value, token->location);
3988   else
3989     return error_mark_node;
3990 }
3991
3992 /* Parse a sequence of adjacent string constants.  Returns a
3993    TREE_STRING representing the combined, nul-terminated string
3994    constant.  If TRANSLATE is true, translate the string to the
3995    execution character set.  If WIDE_OK is true, a wide string is
3996    invalid here.
3997
3998    C++98 [lex.string] says that if a narrow string literal token is
3999    adjacent to a wide string literal token, the behavior is undefined.
4000    However, C99 6.4.5p4 says that this results in a wide string literal.
4001    We follow C99 here, for consistency with the C front end.
4002
4003    This code is largely lifted from lex_string() in c-lex.c.
4004
4005    FUTURE: ObjC++ will need to handle @-strings here.  */
4006 static cp_expr
4007 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4008                           bool lookup_udlit = true)
4009 {
4010   tree value;
4011   size_t count;
4012   struct obstack str_ob;
4013   cpp_string str, istr, *strs;
4014   cp_token *tok;
4015   enum cpp_ttype type, curr_type;
4016   int have_suffix_p = 0;
4017   tree string_tree;
4018   tree suffix_id = NULL_TREE;
4019   bool curr_tok_is_userdef_p = false;
4020
4021   tok = cp_lexer_peek_token (parser->lexer);
4022   if (!cp_parser_is_string_literal (tok))
4023     {
4024       cp_parser_error (parser, "expected string-literal");
4025       return error_mark_node;
4026     }
4027
4028   location_t loc = tok->location;
4029
4030   if (cpp_userdef_string_p (tok->type))
4031     {
4032       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4033       curr_type = cpp_userdef_string_remove_type (tok->type);
4034       curr_tok_is_userdef_p = true;
4035     }
4036   else
4037     {
4038       string_tree = tok->u.value;
4039       curr_type = tok->type;
4040     }
4041   type = curr_type;
4042
4043   /* Try to avoid the overhead of creating and destroying an obstack
4044      for the common case of just one string.  */
4045   if (!cp_parser_is_string_literal
4046       (cp_lexer_peek_nth_token (parser->lexer, 2)))
4047     {
4048       cp_lexer_consume_token (parser->lexer);
4049
4050       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4051       str.len = TREE_STRING_LENGTH (string_tree);
4052       count = 1;
4053
4054       if (curr_tok_is_userdef_p)
4055         {
4056           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4057           have_suffix_p = 1;
4058           curr_type = cpp_userdef_string_remove_type (tok->type);
4059         }
4060       else
4061         curr_type = tok->type;
4062
4063       strs = &str;
4064     }
4065   else
4066     {
4067       location_t last_tok_loc = tok->location;
4068       gcc_obstack_init (&str_ob);
4069       count = 0;
4070
4071       do
4072         {
4073           cp_lexer_consume_token (parser->lexer);
4074           count++;
4075           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4076           str.len = TREE_STRING_LENGTH (string_tree);
4077
4078           if (curr_tok_is_userdef_p)
4079             {
4080               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4081               if (have_suffix_p == 0)
4082                 {
4083                   suffix_id = curr_suffix_id;
4084                   have_suffix_p = 1;
4085                 }
4086               else if (have_suffix_p == 1
4087                        && curr_suffix_id != suffix_id)
4088                 {
4089                   error ("inconsistent user-defined literal suffixes"
4090                          " %qD and %qD in string literal",
4091                          suffix_id, curr_suffix_id);
4092                   have_suffix_p = -1;
4093                 }
4094               curr_type = cpp_userdef_string_remove_type (tok->type);
4095             }
4096           else
4097             curr_type = tok->type;
4098
4099           if (type != curr_type)
4100             {
4101               if (type == CPP_STRING)
4102                 type = curr_type;
4103               else if (curr_type != CPP_STRING)
4104                 {
4105                   rich_location rich_loc (line_table, tok->location);
4106                   rich_loc.add_range (last_tok_loc, false);
4107                   error_at (&rich_loc,
4108                             "unsupported non-standard concatenation "
4109                             "of string literals");
4110                 }
4111             }
4112
4113           obstack_grow (&str_ob, &str, sizeof (cpp_string));
4114
4115           last_tok_loc = tok->location;
4116
4117           tok = cp_lexer_peek_token (parser->lexer);
4118           if (cpp_userdef_string_p (tok->type))
4119             {
4120               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4121               curr_type = cpp_userdef_string_remove_type (tok->type);
4122               curr_tok_is_userdef_p = true;
4123             }
4124           else
4125             {
4126               string_tree = tok->u.value;
4127               curr_type = tok->type;
4128               curr_tok_is_userdef_p = false;
4129             }
4130         }
4131       while (cp_parser_is_string_literal (tok));
4132
4133       /* A string literal built by concatenation has its caret=start at
4134          the start of the initial string, and its finish at the finish of
4135          the final string literal.  */
4136       loc = make_location (loc, loc, get_finish (last_tok_loc));
4137
4138       strs = (cpp_string *) obstack_finish (&str_ob);
4139     }
4140
4141   if (type != CPP_STRING && !wide_ok)
4142     {
4143       cp_parser_error (parser, "a wide string is invalid in this context");
4144       type = CPP_STRING;
4145     }
4146
4147   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4148       (parse_in, strs, count, &istr, type))
4149     {
4150       value = build_string (istr.len, (const char *)istr.text);
4151       free (CONST_CAST (unsigned char *, istr.text));
4152
4153       switch (type)
4154         {
4155         default:
4156         case CPP_STRING:
4157         case CPP_UTF8STRING:
4158           TREE_TYPE (value) = char_array_type_node;
4159           break;
4160         case CPP_STRING16:
4161           TREE_TYPE (value) = char16_array_type_node;
4162           break;
4163         case CPP_STRING32:
4164           TREE_TYPE (value) = char32_array_type_node;
4165           break;
4166         case CPP_WSTRING:
4167           TREE_TYPE (value) = wchar_array_type_node;
4168           break;
4169         }
4170
4171       value = fix_string_type (value);
4172
4173       if (have_suffix_p)
4174         {
4175           tree literal = build_userdef_literal (suffix_id, value,
4176                                                 OT_NONE, NULL_TREE);
4177           if (lookup_udlit)
4178             value = cp_parser_userdef_string_literal (literal);
4179           else
4180             value = literal;
4181         }
4182     }
4183   else
4184     /* cpp_interpret_string has issued an error.  */
4185     value = error_mark_node;
4186
4187   if (count > 1)
4188     obstack_free (&str_ob, 0);
4189
4190   return cp_expr (value, loc);
4191 }
4192
4193 /* Look up a literal operator with the name and the exact arguments.  */
4194
4195 static tree
4196 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4197 {
4198   tree decl;
4199   decl = lookup_name (name);
4200   if (!decl || !is_overloaded_fn (decl))
4201     return error_mark_node;
4202
4203   for (lkp_iterator iter (decl); iter; ++iter)
4204     {
4205       unsigned int ix;
4206       bool found = true;
4207       tree fn = *iter;
4208       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4209       if (parmtypes != NULL_TREE)
4210         {
4211           for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4212                ++ix, parmtypes = TREE_CHAIN (parmtypes))
4213             {
4214               tree tparm = TREE_VALUE (parmtypes);
4215               tree targ = TREE_TYPE ((*args)[ix]);
4216               bool ptr = TYPE_PTR_P (tparm);
4217               bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4218               if ((ptr || arr || !same_type_p (tparm, targ))
4219                   && (!ptr || !arr
4220                       || !same_type_p (TREE_TYPE (tparm),
4221                                        TREE_TYPE (targ))))
4222                 found = false;
4223             }
4224           if (found
4225               && ix == vec_safe_length (args)
4226               /* May be this should be sufficient_parms_p instead,
4227                  depending on how exactly should user-defined literals
4228                  work in presence of default arguments on the literal
4229                  operator parameters.  */
4230               && parmtypes == void_list_node)
4231             return decl;
4232         }
4233     }
4234
4235   return error_mark_node;
4236 }
4237
4238 /* Parse a user-defined char constant.  Returns a call to a user-defined
4239    literal operator taking the character as an argument.  */
4240
4241 static cp_expr
4242 cp_parser_userdef_char_literal (cp_parser *parser)
4243 {
4244   cp_token *token = cp_lexer_consume_token (parser->lexer);
4245   tree literal = token->u.value;
4246   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4247   tree value = USERDEF_LITERAL_VALUE (literal);
4248   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4249   tree decl, result;
4250
4251   /* Build up a call to the user-defined operator  */
4252   /* Lookup the name we got back from the id-expression.  */
4253   vec<tree, va_gc> *args = make_tree_vector ();
4254   vec_safe_push (args, value);
4255   decl = lookup_literal_operator (name, args);
4256   if (!decl || decl == error_mark_node)
4257     {
4258       error ("unable to find character literal operator %qD with %qT argument",
4259              name, TREE_TYPE (value));
4260       release_tree_vector (args);
4261       return error_mark_node;
4262     }
4263   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4264   release_tree_vector (args);
4265   return result;
4266 }
4267
4268 /* A subroutine of cp_parser_userdef_numeric_literal to
4269    create a char... template parameter pack from a string node.  */
4270
4271 static tree
4272 make_char_string_pack (tree value)
4273 {
4274   tree charvec;
4275   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4276   const char *str = TREE_STRING_POINTER (value);
4277   int i, len = TREE_STRING_LENGTH (value) - 1;
4278   tree argvec = make_tree_vec (1);
4279
4280   /* Fill in CHARVEC with all of the parameters.  */
4281   charvec = make_tree_vec (len);
4282   for (i = 0; i < len; ++i)
4283     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4284
4285   /* Build the argument packs.  */
4286   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4287
4288   TREE_VEC_ELT (argvec, 0) = argpack;
4289
4290   return argvec;
4291 }
4292
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294    create a char... template parameter pack from a string node.  */
4295
4296 static tree
4297 make_string_pack (tree value)
4298 {
4299   tree charvec;
4300   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4301   const unsigned char *str
4302     = (const unsigned char *) TREE_STRING_POINTER (value);
4303   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4304   int len = TREE_STRING_LENGTH (value) / sz - 1;
4305   tree argvec = make_tree_vec (2);
4306
4307   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4308   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4309
4310   /* First template parm is character type.  */
4311   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4312
4313   /* Fill in CHARVEC with all of the parameters.  */
4314   charvec = make_tree_vec (len);
4315   for (int i = 0; i < len; ++i)
4316     TREE_VEC_ELT (charvec, i)
4317       = double_int_to_tree (str_char_type_node,
4318                             double_int::from_buffer (str + i * sz, sz));
4319
4320   /* Build the argument packs.  */
4321   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4322
4323   TREE_VEC_ELT (argvec, 1) = argpack;
4324
4325   return argvec;
4326 }
4327
4328 /* Parse a user-defined numeric constant.  returns a call to a user-defined
4329    literal operator.  */
4330
4331 static cp_expr
4332 cp_parser_userdef_numeric_literal (cp_parser *parser)
4333 {
4334   cp_token *token = cp_lexer_consume_token (parser->lexer);
4335   tree literal = token->u.value;
4336   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4337   tree value = USERDEF_LITERAL_VALUE (literal);
4338   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4339   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4340   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341   tree decl, result;
4342   vec<tree, va_gc> *args;
4343
4344   /* Look for a literal operator taking the exact type of numeric argument
4345      as the literal value.  */
4346   args = make_tree_vector ();
4347   vec_safe_push (args, value);
4348   decl = lookup_literal_operator (name, args);
4349   if (decl && decl != error_mark_node)
4350     {
4351       result = finish_call_expr (decl, &args, false, true,
4352                                  tf_warning_or_error);
4353
4354       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4355         {
4356           warning_at (token->location, OPT_Woverflow,
4357                       "integer literal exceeds range of %qT type",
4358                       long_long_unsigned_type_node);
4359         }
4360       else
4361         {
4362           if (overflow > 0)
4363             warning_at (token->location, OPT_Woverflow,
4364                         "floating literal exceeds range of %qT type",
4365                         long_double_type_node);
4366           else if (overflow < 0)
4367             warning_at (token->location, OPT_Woverflow,
4368                         "floating literal truncated to zero");
4369         }
4370
4371       release_tree_vector (args);
4372       return result;
4373     }
4374   release_tree_vector (args);
4375
4376   /* If the numeric argument didn't work, look for a raw literal
4377      operator taking a const char* argument consisting of the number
4378      in string format.  */
4379   args = make_tree_vector ();
4380   vec_safe_push (args, num_string);
4381   decl = lookup_literal_operator (name, args);
4382   if (decl && decl != error_mark_node)
4383     {
4384       result = finish_call_expr (decl, &args, false, true,
4385                                  tf_warning_or_error);
4386       release_tree_vector (args);
4387       return result;
4388     }
4389   release_tree_vector (args);
4390
4391   /* If the raw literal didn't work, look for a non-type template
4392      function with parameter pack char....  Call the function with
4393      template parameter characters representing the number.  */
4394   args = make_tree_vector ();
4395   decl = lookup_literal_operator (name, args);
4396   if (decl && decl != error_mark_node)
4397     {
4398       tree tmpl_args = make_char_string_pack (num_string);
4399       decl = lookup_template_function (decl, tmpl_args);
4400       result = finish_call_expr (decl, &args, false, true,
4401                                  tf_warning_or_error);
4402       release_tree_vector (args);
4403       return result;
4404     }
4405
4406   release_tree_vector (args);
4407
4408   /* In C++14 the standard library defines complex number suffixes that
4409      conflict with GNU extensions.  Prefer them if <complex> is #included.  */
4410   bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4411   bool i14 = (cxx_dialect > cxx11
4412               && (id_equal (suffix_id, "i")
4413                   || id_equal (suffix_id, "if")
4414                   || id_equal (suffix_id, "il")));
4415   diagnostic_t kind = DK_ERROR;
4416   int opt = 0;
4417
4418   if (i14 && ext)
4419     {
4420       tree cxlit = lookup_qualified_name (std_node,
4421                                           get_identifier ("complex_literals"),
4422                                           0, false, false);
4423       if (cxlit == error_mark_node)
4424         {
4425           /* No <complex>, so pedwarn and use GNU semantics.  */
4426           kind = DK_PEDWARN;
4427           opt = OPT_Wpedantic;
4428         }
4429     }
4430
4431   bool complained
4432     = emit_diagnostic (kind, input_location, opt,
4433                        "unable to find numeric literal operator %qD", name);
4434
4435   if (!complained)
4436     /* Don't inform either.  */;
4437   else if (i14)
4438     {
4439       inform (token->location, "add %<using namespace std::complex_literals%> "
4440               "(from <complex>) to enable the C++14 user-defined literal "
4441               "suffixes");
4442       if (ext)
4443         inform (token->location, "or use %<j%> instead of %<i%> for the "
4444                 "GNU built-in suffix");
4445     }
4446   else if (!ext)
4447     inform (token->location, "use -fext-numeric-literals "
4448             "to enable more built-in suffixes");
4449
4450   if (kind == DK_ERROR)
4451     value = error_mark_node;
4452   else
4453     {
4454       /* Use the built-in semantics.  */
4455       tree type;
4456       if (id_equal (suffix_id, "i"))
4457         {
4458           if (TREE_CODE (value) == INTEGER_CST)
4459             type = integer_type_node;
4460           else
4461             type = double_type_node;
4462         }
4463       else if (id_equal (suffix_id, "if"))
4464         type = float_type_node;
4465       else /* if (id_equal (suffix_id, "il")) */
4466         type = long_double_type_node;
4467
4468       value = build_complex (build_complex_type (type),
4469                              fold_convert (type, integer_zero_node),
4470                              fold_convert (type, value));
4471     }
4472
4473   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4474     /* Avoid repeated diagnostics.  */
4475     token->u.value = value;
4476   return value;
4477 }
4478
4479 /* Parse a user-defined string constant.  Returns a call to a user-defined
4480    literal operator taking a character pointer and the length of the string
4481    as arguments.  */
4482
4483 static tree
4484 cp_parser_userdef_string_literal (tree literal)
4485 {
4486   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4487   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4488   tree value = USERDEF_LITERAL_VALUE (literal);
4489   int len = TREE_STRING_LENGTH (value)
4490         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4491   tree decl, result;
4492   vec<tree, va_gc> *args;
4493
4494   /* Build up a call to the user-defined operator.  */
4495   /* Lookup the name we got back from the id-expression.  */
4496   args = make_tree_vector ();
4497   vec_safe_push (args, value);
4498   vec_safe_push (args, build_int_cst (size_type_node, len));
4499   decl = lookup_literal_operator (name, args);
4500
4501   if (decl && decl != error_mark_node)
4502     {
4503       result = finish_call_expr (decl, &args, false, true,
4504                                  tf_warning_or_error);
4505       release_tree_vector (args);
4506       return result;
4507     }
4508   release_tree_vector (args);
4509
4510   /* Look for a template function with typename parameter CharT
4511      and parameter pack CharT...  Call the function with
4512      template parameter characters representing the string.  */
4513   args = make_tree_vector ();
4514   decl = lookup_literal_operator (name, args);
4515   if (decl && decl != error_mark_node)
4516     {
4517       tree tmpl_args = make_string_pack (value);
4518       decl = lookup_template_function (decl, tmpl_args);
4519       result = finish_call_expr (decl, &args, false, true,
4520                                  tf_warning_or_error);
4521       release_tree_vector (args);
4522       return result;
4523     }
4524   release_tree_vector (args);
4525
4526   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4527          name, TREE_TYPE (value), size_type_node);
4528   return error_mark_node;
4529 }
4530
4531
4532 /* Basic concepts [gram.basic]  */
4533
4534 /* Parse a translation-unit.
4535
4536    translation-unit:
4537      declaration-seq [opt]
4538
4539    Returns TRUE if all went well.  */
4540
4541 static bool
4542 cp_parser_translation_unit (cp_parser* parser)
4543 {
4544   /* The address of the first non-permanent object on the declarator
4545      obstack.  */
4546   static void *declarator_obstack_base;
4547
4548   bool success;
4549
4550   /* Create the declarator obstack, if necessary.  */
4551   if (!cp_error_declarator)
4552     {
4553       gcc_obstack_init (&declarator_obstack);
4554       /* Create the error declarator.  */
4555       cp_error_declarator = make_declarator (cdk_error);
4556       /* Create the empty parameter list.  */
4557       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4558                                                  UNKNOWN_LOCATION);
4559       /* Remember where the base of the declarator obstack lies.  */
4560       declarator_obstack_base = obstack_next_free (&declarator_obstack);
4561     }
4562
4563   cp_parser_declaration_seq_opt (parser);
4564
4565   /* If there are no tokens left then all went well.  */
4566   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4567     {
4568       /* Get rid of the token array; we don't need it any more.  */
4569       cp_lexer_destroy (parser->lexer);
4570       parser->lexer = NULL;
4571
4572       /* This file might have been a context that's implicitly extern
4573          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
4574       if (parser->implicit_extern_c)
4575         {
4576           pop_lang_context ();
4577           parser->implicit_extern_c = false;
4578         }
4579
4580       /* Finish up.  */
4581       finish_translation_unit ();
4582
4583       success = true;
4584     }
4585   else
4586     {
4587       cp_parser_error (parser, "expected declaration");
4588       success = false;
4589     }
4590
4591   /* Make sure the declarator obstack was fully cleaned up.  */
4592   gcc_assert (obstack_next_free (&declarator_obstack)
4593               == declarator_obstack_base);
4594
4595   /* All went well.  */
4596   return success;
4597 }
4598
4599 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4600    decltype context.  */
4601
4602 static inline tsubst_flags_t
4603 complain_flags (bool decltype_p)
4604 {
4605   tsubst_flags_t complain = tf_warning_or_error;
4606   if (decltype_p)
4607     complain |= tf_decltype;
4608   return complain;
4609 }
4610
4611 /* We're about to parse a collection of statements.  If we're currently
4612    parsing tentatively, set up a firewall so that any nested
4613    cp_parser_commit_to_tentative_parse won't affect the current context.  */
4614
4615 static cp_token_position
4616 cp_parser_start_tentative_firewall (cp_parser *parser)
4617 {
4618   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4619     return 0;
4620
4621   cp_parser_parse_tentatively (parser);
4622   cp_parser_commit_to_topmost_tentative_parse (parser);
4623   return cp_lexer_token_position (parser->lexer, false);
4624 }
4625
4626 /* We've finished parsing the collection of statements.  Wrap up the
4627    firewall and replace the relevant tokens with the parsed form.  */
4628
4629 static void
4630 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4631                                   tree expr)
4632 {
4633   if (!start)
4634     return;
4635
4636   /* Finish the firewall level.  */
4637   cp_parser_parse_definitely (parser);
4638   /* And remember the result of the parse for when we try again.  */
4639   cp_token *token = cp_lexer_token_at (parser->lexer, start);
4640   token->type = CPP_PREPARSED_EXPR;
4641   token->u.value = expr;
4642   token->keyword = RID_MAX;
4643   cp_lexer_purge_tokens_after (parser->lexer, start);
4644 }
4645
4646 /* Like the above functions, but let the user modify the tokens.  Used by
4647    CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4648    later parses, so it makes sense to localize the effects of
4649    cp_parser_commit_to_tentative_parse.  */
4650
4651 struct tentative_firewall
4652 {
4653   cp_parser *parser;
4654   bool set;
4655
4656   tentative_firewall (cp_parser *p): parser(p)
4657   {
4658     /* If we're currently parsing tentatively, start a committed level as a
4659        firewall and then an inner tentative parse.  */
4660     if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4661       {
4662         cp_parser_parse_tentatively (parser);
4663         cp_parser_commit_to_topmost_tentative_parse (parser);
4664         cp_parser_parse_tentatively (parser);
4665       }
4666   }
4667
4668   ~tentative_firewall()
4669   {
4670     if (set)
4671       {
4672         /* Finish the inner tentative parse and the firewall, propagating any
4673            uncommitted error state to the outer tentative parse.  */
4674         bool err = cp_parser_error_occurred (parser);
4675         cp_parser_parse_definitely (parser);
4676         cp_parser_parse_definitely (parser);
4677         if (err)
4678           cp_parser_simulate_error (parser);
4679       }
4680   }
4681 };
4682
4683 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4684    This class is for tracking such a matching pair of symbols.
4685    In particular, it tracks the location of the first token,
4686    so that if the second token is missing, we can highlight the
4687    location of the first token when notifying the user about the
4688    problem.  */
4689
4690 template <typename traits_t>
4691 class token_pair
4692 {
4693  public:
4694   /* token_pair's ctor.  */
4695   token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4696
4697   /* If the next token is the opening symbol for this pair, consume it and
4698      return true.
4699      Otherwise, issue an error and return false.
4700      In either case, record the location of the opening token.  */
4701
4702   bool require_open (cp_parser *parser)
4703   {
4704     m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4705     return cp_parser_require (parser, traits_t::open_token_type,
4706                               traits_t::required_token_open);
4707   }
4708
4709   /* Consume the next token from PARSER, recording its location as
4710      that of the opening token within the pair.  */
4711
4712   cp_token * consume_open (cp_parser *parser)
4713   {
4714     cp_token *tok = cp_lexer_consume_token (parser->lexer);
4715     gcc_assert (tok->type == traits_t::open_token_type);
4716     m_open_loc = tok->location;
4717     return tok;
4718   }
4719
4720   /* If the next token is the closing symbol for this pair, consume it
4721      and return it.
4722      Otherwise, issue an error, highlighting the location of the
4723      corresponding opening token, and return NULL.  */
4724
4725   cp_token *require_close (cp_parser *parser) const
4726   {
4727     return cp_parser_require (parser, traits_t::close_token_type,
4728                               traits_t::required_token_close,
4729                               m_open_loc);
4730   }
4731
4732  private:
4733   location_t m_open_loc;
4734 };
4735
4736 /* Traits for token_pair<T> for tracking matching pairs of parentheses.  */
4737
4738 struct matching_paren_traits
4739 {
4740   static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4741   static const enum required_token required_token_open  = RT_OPEN_PAREN;
4742   static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4743   static const enum required_token required_token_close = RT_CLOSE_PAREN;
4744 };
4745
4746 /* "matching_parens" is a token_pair<T> class for tracking matching
4747    pairs of parentheses.  */
4748
4749 typedef token_pair<matching_paren_traits> matching_parens;
4750
4751 /* Traits for token_pair<T> for tracking matching pairs of braces.  */
4752
4753 struct matching_brace_traits
4754 {
4755   static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4756   static const enum required_token required_token_open = RT_OPEN_BRACE;
4757   static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4758   static const enum required_token required_token_close = RT_CLOSE_BRACE;
4759 };
4760
4761 /* "matching_braces" is a token_pair<T> class for tracking matching
4762    pairs of braces.  */
4763
4764 typedef token_pair<matching_brace_traits> matching_braces;
4765
4766
4767 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4768    enclosing parentheses.  */
4769
4770 static cp_expr
4771 cp_parser_statement_expr (cp_parser *parser)
4772 {
4773   cp_token_position start = cp_parser_start_tentative_firewall (parser);
4774
4775   /* Consume the '('.  */
4776   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4777   matching_parens parens;
4778   parens.consume_open (parser);
4779   /* Start the statement-expression.  */
4780   tree expr = begin_stmt_expr ();
4781   /* Parse the compound-statement.  */
4782   cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4783   /* Finish up.  */
4784   expr = finish_stmt_expr (expr, false);
4785   /* Consume the ')'.  */
4786   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4787   if (!parens.require_close (parser))
4788     cp_parser_skip_to_end_of_statement (parser);
4789
4790   cp_parser_end_tentative_firewall (parser, start, expr);
4791   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4792   return cp_expr (expr, combined_loc);
4793 }
4794
4795 /* Expressions [gram.expr] */
4796
4797 /* Parse a fold-operator.
4798
4799     fold-operator:
4800         -  *  /  %  ^  &  |  =  <  >  <<  >>
4801       =  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=
4802       ==  !=  <=  >=  &&  ||  ,  .*  ->*
4803
4804    This returns the tree code corresponding to the matched operator
4805    as an int. When the current token matches a compound assignment
4806    opertor, the resulting tree code is the negative value of the
4807    non-assignment operator. */
4808
4809 static int
4810 cp_parser_fold_operator (cp_token *token)
4811 {
4812   switch (token->type)
4813     {
4814     case CPP_PLUS: return PLUS_EXPR;
4815     case CPP_MINUS: return MINUS_EXPR;
4816     case CPP_MULT: return MULT_EXPR;
4817     case CPP_DIV: return TRUNC_DIV_EXPR;
4818     case CPP_MOD: return TRUNC_MOD_EXPR;
4819     case CPP_XOR: return BIT_XOR_EXPR;
4820     case CPP_AND: return BIT_AND_EXPR;
4821     case CPP_OR: return BIT_IOR_EXPR;
4822     case CPP_LSHIFT: return LSHIFT_EXPR;
4823     case CPP_RSHIFT: return RSHIFT_EXPR;
4824
4825     case CPP_EQ: return -NOP_EXPR;
4826     case CPP_PLUS_EQ: return -PLUS_EXPR;
4827     case CPP_MINUS_EQ: return -MINUS_EXPR;
4828     case CPP_MULT_EQ: return -MULT_EXPR;
4829     case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4830     case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4831     case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4832     case CPP_AND_EQ: return -BIT_AND_EXPR;
4833     case CPP_OR_EQ: return -BIT_IOR_EXPR;
4834     case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4835     case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4836
4837     case CPP_EQ_EQ: return EQ_EXPR;
4838     case CPP_NOT_EQ: return NE_EXPR;
4839     case CPP_LESS: return LT_EXPR;
4840     case CPP_GREATER: return GT_EXPR;
4841     case CPP_LESS_EQ: return LE_EXPR;
4842     case CPP_GREATER_EQ: return GE_EXPR;
4843
4844     case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4845     case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4846
4847     case CPP_COMMA: return COMPOUND_EXPR;
4848
4849     case CPP_DOT_STAR: return DOTSTAR_EXPR;
4850     case CPP_DEREF_STAR: return MEMBER_REF;
4851
4852     default: return ERROR_MARK;
4853     }
4854 }
4855
4856 /* Returns true if CODE indicates a binary expression, which is not allowed in
4857    the LHS of a fold-expression.  More codes will need to be added to use this
4858    function in other contexts.  */
4859
4860 static bool
4861 is_binary_op (tree_code code)
4862 {
4863   switch (code)
4864     {
4865     case PLUS_EXPR:
4866     case POINTER_PLUS_EXPR:
4867     case MINUS_EXPR:
4868     case MULT_EXPR:
4869     case TRUNC_DIV_EXPR:
4870     case TRUNC_MOD_EXPR:
4871     case BIT_XOR_EXPR:
4872     case BIT_AND_EXPR:
4873     case BIT_IOR_EXPR:
4874     case LSHIFT_EXPR:
4875     case RSHIFT_EXPR:
4876
4877     case MODOP_EXPR:
4878
4879     case EQ_EXPR:
4880     case NE_EXPR:
4881     case LE_EXPR:
4882     case GE_EXPR:
4883     case LT_EXPR:
4884     case GT_EXPR:
4885
4886     case TRUTH_ANDIF_EXPR:
4887     case TRUTH_ORIF_EXPR:
4888
4889     case COMPOUND_EXPR:
4890
4891     case DOTSTAR_EXPR:
4892     case MEMBER_REF:
4893       return true;
4894
4895     default:
4896       return false;
4897     }
4898 }
4899
4900 /* If the next token is a suitable fold operator, consume it and return as
4901    the function above.  */
4902
4903 static int
4904 cp_parser_fold_operator (cp_parser *parser)
4905 {
4906   cp_token* token = cp_lexer_peek_token (parser->lexer);
4907   int code = cp_parser_fold_operator (token);
4908   if (code != ERROR_MARK)
4909     cp_lexer_consume_token (parser->lexer);
4910   return code;
4911 }
4912
4913 /* Parse a fold-expression.
4914
4915      fold-expression:
4916        ( ... folding-operator cast-expression)
4917        ( cast-expression folding-operator ... )
4918        ( cast-expression folding operator ... folding-operator cast-expression)
4919
4920    Note that the '(' and ')' are matched in primary expression. */
4921
4922 static cp_expr
4923 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4924 {
4925   cp_id_kind pidk;
4926
4927   // Left fold.
4928   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4929     {
4930       cp_lexer_consume_token (parser->lexer);
4931       int op = cp_parser_fold_operator (parser);
4932       if (op == ERROR_MARK)
4933         {
4934           cp_parser_error (parser, "expected binary operator");
4935           return error_mark_node;
4936         }
4937
4938       tree expr = cp_parser_cast_expression (parser, false, false,
4939                                              false, &pidk);
4940       if (expr == error_mark_node)
4941         return error_mark_node;
4942       return finish_left_unary_fold_expr (expr, op);
4943     }
4944
4945   const cp_token* token = cp_lexer_peek_token (parser->lexer);
4946   int op = cp_parser_fold_operator (parser);
4947   if (op == ERROR_MARK)
4948     {
4949       cp_parser_error (parser, "expected binary operator");
4950       return error_mark_node;
4951     }
4952
4953   if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4954     {
4955       cp_parser_error (parser, "expected ...");
4956       return error_mark_node;
4957     }
4958   cp_lexer_consume_token (parser->lexer);
4959
4960   /* The operands of a fold-expression are cast-expressions, so binary or
4961      conditional expressions are not allowed.  We check this here to avoid
4962      tentative parsing.  */
4963   if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4964     /* OK, the expression was parenthesized.  */;
4965   else if (is_binary_op (TREE_CODE (expr1)))
4966     error_at (location_of (expr1),
4967               "binary expression in operand of fold-expression");
4968   else if (TREE_CODE (expr1) == COND_EXPR
4969            || (REFERENCE_REF_P (expr1)
4970                && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4971     error_at (location_of (expr1),
4972               "conditional expression in operand of fold-expression");
4973
4974   // Right fold.
4975   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4976     return finish_right_unary_fold_expr (expr1, op);
4977
4978   if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4979     {
4980       cp_parser_error (parser, "mismatched operator in fold-expression");
4981       return error_mark_node;
4982     }
4983   cp_lexer_consume_token (parser->lexer);
4984
4985   // Binary left or right fold.
4986   tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4987   if (expr2 == error_mark_node)
4988     return error_mark_node;
4989   return finish_binary_fold_expr (expr1, expr2, op);
4990 }
4991
4992 /* Parse a primary-expression.
4993
4994    primary-expression:
4995      literal
4996      this
4997      ( expression )
4998      id-expression
4999      lambda-expression (C++11)
5000
5001    GNU Extensions:
5002
5003    primary-expression:
5004      ( compound-statement )
5005      __builtin_va_arg ( assignment-expression , type-id )
5006      __builtin_offsetof ( type-id , offsetof-expression )
5007
5008    C++ Extensions:
5009      __has_nothrow_assign ( type-id )   
5010      __has_nothrow_constructor ( type-id )
5011      __has_nothrow_copy ( type-id )
5012      __has_trivial_assign ( type-id )   
5013      __has_trivial_constructor ( type-id )
5014      __has_trivial_copy ( type-id )
5015      __has_trivial_destructor ( type-id )
5016      __has_virtual_destructor ( type-id )     
5017      __is_abstract ( type-id )
5018      __is_base_of ( type-id , type-id )
5019      __is_class ( type-id )
5020      __is_empty ( type-id )
5021      __is_enum ( type-id )
5022      __is_final ( type-id )
5023      __is_literal_type ( type-id )
5024      __is_pod ( type-id )
5025      __is_polymorphic ( type-id )
5026      __is_std_layout ( type-id )
5027      __is_trivial ( type-id )
5028      __is_union ( type-id )
5029
5030    Objective-C++ Extension:
5031
5032    primary-expression:
5033      objc-expression
5034
5035    literal:
5036      __null
5037
5038    ADDRESS_P is true iff this expression was immediately preceded by
5039    "&" and therefore might denote a pointer-to-member.  CAST_P is true
5040    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
5041    true iff this expression is a template argument.
5042
5043    Returns a representation of the expression.  Upon return, *IDK
5044    indicates what kind of id-expression (if any) was present.  */
5045
5046 static cp_expr
5047 cp_parser_primary_expression (cp_parser *parser,
5048                               bool address_p,
5049                               bool cast_p,
5050                               bool template_arg_p,
5051                               bool decltype_p,
5052                               cp_id_kind *idk)
5053 {
5054   cp_token *token = NULL;
5055
5056   /* Assume the primary expression is not an id-expression.  */
5057   *idk = CP_ID_KIND_NONE;
5058
5059   /* Peek at the next token.  */
5060   token = cp_lexer_peek_token (parser->lexer);
5061   switch ((int) token->type)
5062     {
5063       /* literal:
5064            integer-literal
5065            character-literal
5066            floating-literal
5067            string-literal
5068            boolean-literal
5069            pointer-literal
5070            user-defined-literal  */
5071     case CPP_CHAR:
5072     case CPP_CHAR16:
5073     case CPP_CHAR32:
5074     case CPP_WCHAR:
5075     case CPP_UTF8CHAR:
5076     case CPP_NUMBER:
5077     case CPP_PREPARSED_EXPR:
5078       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5079         return cp_parser_userdef_numeric_literal (parser);
5080       token = cp_lexer_consume_token (parser->lexer);
5081       if (TREE_CODE (token->u.value) == FIXED_CST)
5082         {
5083           error_at (token->location,
5084                     "fixed-point types not supported in C++");
5085           return error_mark_node;
5086         }
5087       /* Floating-point literals are only allowed in an integral
5088          constant expression if they are cast to an integral or
5089          enumeration type.  */
5090       if (TREE_CODE (token->u.value) == REAL_CST
5091           && parser->integral_constant_expression_p
5092           && pedantic)
5093         {
5094           /* CAST_P will be set even in invalid code like "int(2.7 +
5095              ...)".   Therefore, we have to check that the next token
5096              is sure to end the cast.  */
5097           if (cast_p)
5098             {
5099               cp_token *next_token;
5100
5101               next_token = cp_lexer_peek_token (parser->lexer);
5102               if (/* The comma at the end of an
5103                      enumerator-definition.  */
5104                   next_token->type != CPP_COMMA
5105                   /* The curly brace at the end of an enum-specifier.  */
5106                   && next_token->type != CPP_CLOSE_BRACE
5107                   /* The end of a statement.  */
5108                   && next_token->type != CPP_SEMICOLON
5109                   /* The end of the cast-expression.  */
5110                   && next_token->type != CPP_CLOSE_PAREN
5111                   /* The end of an array bound.  */
5112                   && next_token->type != CPP_CLOSE_SQUARE
5113                   /* The closing ">" in a template-argument-list.  */
5114                   && (next_token->type != CPP_GREATER
5115                       || parser->greater_than_is_operator_p)
5116                   /* C++0x only: A ">>" treated like two ">" tokens,
5117                      in a template-argument-list.  */
5118                   && (next_token->type != CPP_RSHIFT
5119                       || (cxx_dialect == cxx98)
5120                       || parser->greater_than_is_operator_p))
5121                 cast_p = false;
5122             }
5123
5124           /* If we are within a cast, then the constraint that the
5125              cast is to an integral or enumeration type will be
5126              checked at that point.  If we are not within a cast, then
5127              this code is invalid.  */
5128           if (!cast_p)
5129             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5130         }
5131       return cp_expr (token->u.value, token->location);
5132
5133     case CPP_CHAR_USERDEF:
5134     case CPP_CHAR16_USERDEF:
5135     case CPP_CHAR32_USERDEF:
5136     case CPP_WCHAR_USERDEF:
5137     case CPP_UTF8CHAR_USERDEF:
5138       return cp_parser_userdef_char_literal (parser);
5139
5140     case CPP_STRING:
5141     case CPP_STRING16:
5142     case CPP_STRING32:
5143     case CPP_WSTRING:
5144     case CPP_UTF8STRING:
5145     case CPP_STRING_USERDEF:
5146     case CPP_STRING16_USERDEF:
5147     case CPP_STRING32_USERDEF:
5148     case CPP_WSTRING_USERDEF:
5149     case CPP_UTF8STRING_USERDEF:
5150       /* ??? Should wide strings be allowed when parser->translate_strings_p
5151          is false (i.e. in attributes)?  If not, we can kill the third
5152          argument to cp_parser_string_literal.  */
5153       return cp_parser_string_literal (parser,
5154                                        parser->translate_strings_p,
5155                                        true);
5156
5157     case CPP_OPEN_PAREN:
5158       /* If we see `( { ' then we are looking at the beginning of
5159          a GNU statement-expression.  */
5160       if (cp_parser_allow_gnu_extensions_p (parser)
5161           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5162         {
5163           /* Statement-expressions are not allowed by the standard.  */
5164           pedwarn (token->location, OPT_Wpedantic,
5165                    "ISO C++ forbids braced-groups within expressions");
5166
5167           /* And they're not allowed outside of a function-body; you
5168              cannot, for example, write:
5169
5170              int i = ({ int j = 3; j + 1; });
5171
5172              at class or namespace scope.  */
5173           if (!parser->in_function_body
5174               || parser->in_template_argument_list_p)
5175             {
5176               error_at (token->location,
5177                         "statement-expressions are not allowed outside "
5178                         "functions nor in template-argument lists");
5179               cp_parser_skip_to_end_of_block_or_statement (parser);
5180               if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5181                 cp_lexer_consume_token (parser->lexer);
5182               return error_mark_node;
5183             }
5184           else
5185             return cp_parser_statement_expr (parser);
5186         }
5187       /* Otherwise it's a normal parenthesized expression.  */
5188       {
5189         cp_expr expr;
5190         bool saved_greater_than_is_operator_p;
5191
5192         location_t open_paren_loc = token->location;
5193
5194         /* Consume the `('.  */
5195         matching_parens parens;
5196         parens.consume_open (parser);
5197         /* Within a parenthesized expression, a `>' token is always
5198            the greater-than operator.  */
5199         saved_greater_than_is_operator_p
5200           = parser->greater_than_is_operator_p;
5201         parser->greater_than_is_operator_p = true;
5202
5203         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5204           /* Left fold expression. */
5205           expr = NULL_TREE;
5206         else
5207           /* Parse the parenthesized expression.  */
5208           expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5209
5210         token = cp_lexer_peek_token (parser->lexer);
5211         if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5212           {
5213             expr = cp_parser_fold_expression (parser, expr);
5214             if (expr != error_mark_node
5215                 && cxx_dialect < cxx17
5216                 && !in_system_header_at (input_location))
5217               pedwarn (input_location, 0, "fold-expressions only available "
5218                        "with -std=c++17 or -std=gnu++17");
5219           }
5220         else
5221           /* Let the front end know that this expression was
5222              enclosed in parentheses. This matters in case, for
5223              example, the expression is of the form `A::B', since
5224              `&A::B' might be a pointer-to-member, but `&(A::B)' is
5225              not.  */
5226           expr = finish_parenthesized_expr (expr);
5227
5228         /* DR 705: Wrapping an unqualified name in parentheses
5229            suppresses arg-dependent lookup.  We want to pass back
5230            CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5231            (c++/37862), but none of the others.  */
5232         if (*idk != CP_ID_KIND_QUALIFIED)
5233           *idk = CP_ID_KIND_NONE;
5234
5235         /* The `>' token might be the end of a template-id or
5236            template-parameter-list now.  */
5237         parser->greater_than_is_operator_p
5238           = saved_greater_than_is_operator_p;
5239
5240         /* Consume the `)'.  */
5241         token = cp_lexer_peek_token (parser->lexer);
5242         location_t close_paren_loc = token->location;
5243         expr.set_range (open_paren_loc, close_paren_loc);
5244         if (!parens.require_close (parser)
5245             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5246           cp_parser_skip_to_end_of_statement (parser);
5247
5248         return expr;
5249       }
5250
5251     case CPP_OPEN_SQUARE:
5252       {
5253         if (c_dialect_objc ())
5254           {
5255             /* We might have an Objective-C++ message. */
5256             cp_parser_parse_tentatively (parser);
5257             tree msg = cp_parser_objc_message_expression (parser);
5258             /* If that works out, we're done ... */
5259             if (cp_parser_parse_definitely (parser))
5260               return msg;
5261             /* ... else, fall though to see if it's a lambda.  */
5262           }
5263         cp_expr lam = cp_parser_lambda_expression (parser);
5264         /* Don't warn about a failed tentative parse.  */
5265         if (cp_parser_error_occurred (parser))
5266           return error_mark_node;
5267         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5268         return lam;
5269       }
5270
5271     case CPP_OBJC_STRING:
5272       if (c_dialect_objc ())
5273         /* We have an Objective-C++ string literal. */
5274         return cp_parser_objc_expression (parser);
5275       cp_parser_error (parser, "expected primary-expression");
5276       return error_mark_node;
5277
5278     case CPP_KEYWORD:
5279       switch (token->keyword)
5280         {
5281           /* These two are the boolean literals.  */
5282         case RID_TRUE:
5283           cp_lexer_consume_token (parser->lexer);
5284           return cp_expr (boolean_true_node, token->location);
5285         case RID_FALSE:
5286           cp_lexer_consume_token (parser->lexer);
5287           return cp_expr (boolean_false_node, token->location);
5288
5289           /* The `__null' literal.  */
5290         case RID_NULL:
5291           cp_lexer_consume_token (parser->lexer);
5292           return cp_expr (null_node, token->location);
5293
5294           /* The `nullptr' literal.  */
5295         case RID_NULLPTR:
5296           cp_lexer_consume_token (parser->lexer);
5297           return cp_expr (nullptr_node, token->location);
5298
5299           /* Recognize the `this' keyword.  */
5300         case RID_THIS:
5301           cp_lexer_consume_token (parser->lexer);
5302           if (parser->local_variables_forbidden_p)
5303             {
5304               error_at (token->location,
5305                         "%<this%> may not be used in this context");
5306               return error_mark_node;
5307             }
5308           /* Pointers cannot appear in constant-expressions.  */
5309           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5310             return error_mark_node;
5311           return cp_expr (finish_this_expr (), token->location);
5312
5313           /* The `operator' keyword can be the beginning of an
5314              id-expression.  */
5315         case RID_OPERATOR:
5316           goto id_expression;
5317
5318         case RID_FUNCTION_NAME:
5319         case RID_PRETTY_FUNCTION_NAME:
5320         case RID_C99_FUNCTION_NAME:
5321           {
5322             non_integral_constant name;
5323
5324             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5325                __func__ are the names of variables -- but they are
5326                treated specially.  Therefore, they are handled here,
5327                rather than relying on the generic id-expression logic
5328                below.  Grammatically, these names are id-expressions.
5329
5330                Consume the token.  */
5331             token = cp_lexer_consume_token (parser->lexer);
5332
5333             switch (token->keyword)
5334               {
5335               case RID_FUNCTION_NAME:
5336                 name = NIC_FUNC_NAME;
5337                 break;
5338               case RID_PRETTY_FUNCTION_NAME:
5339                 name = NIC_PRETTY_FUNC;
5340                 break;
5341               case RID_C99_FUNCTION_NAME:
5342                 name = NIC_C99_FUNC;
5343                 break;
5344               default:
5345                 gcc_unreachable ();
5346               }
5347
5348             if (cp_parser_non_integral_constant_expression (parser, name))
5349               return error_mark_node;
5350
5351             /* Look up the name.  */
5352             return finish_fname (token->u.value);
5353           }
5354
5355         case RID_VA_ARG:
5356           {
5357             tree expression;
5358             tree type;
5359             source_location type_location;
5360             location_t start_loc
5361               = cp_lexer_peek_token (parser->lexer)->location;
5362             /* The `__builtin_va_arg' construct is used to handle
5363                `va_arg'.  Consume the `__builtin_va_arg' token.  */
5364             cp_lexer_consume_token (parser->lexer);
5365             /* Look for the opening `('.  */
5366             matching_parens parens;
5367             parens.require_open (parser);
5368             /* Now, parse the assignment-expression.  */
5369             expression = cp_parser_assignment_expression (parser);
5370             /* Look for the `,'.  */
5371             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5372             type_location = cp_lexer_peek_token (parser->lexer)->location;
5373             /* Parse the type-id.  */
5374             {
5375               type_id_in_expr_sentinel s (parser);
5376               type = cp_parser_type_id (parser);
5377             }
5378             /* Look for the closing `)'.  */
5379             location_t finish_loc
5380               = cp_lexer_peek_token (parser->lexer)->location;
5381             parens.require_close (parser);
5382             /* Using `va_arg' in a constant-expression is not
5383                allowed.  */
5384             if (cp_parser_non_integral_constant_expression (parser,
5385                                                             NIC_VA_ARG))
5386               return error_mark_node;
5387             /* Construct a location of the form:
5388                  __builtin_va_arg (v, int)
5389                  ~~~~~~~~~~~~~~~~~~~~~^~~~
5390                with the caret at the type, ranging from the start of the
5391                "__builtin_va_arg" token to the close paren.  */
5392             location_t combined_loc
5393               = make_location (type_location, start_loc, finish_loc);
5394             return build_x_va_arg (combined_loc, expression, type);
5395           }
5396
5397         case RID_OFFSETOF:
5398           return cp_parser_builtin_offsetof (parser);
5399
5400         case RID_HAS_NOTHROW_ASSIGN:
5401         case RID_HAS_NOTHROW_CONSTRUCTOR:
5402         case RID_HAS_NOTHROW_COPY:        
5403         case RID_HAS_TRIVIAL_ASSIGN:
5404         case RID_HAS_TRIVIAL_CONSTRUCTOR:
5405         case RID_HAS_TRIVIAL_COPY:        
5406         case RID_HAS_TRIVIAL_DESTRUCTOR:
5407         case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5408         case RID_HAS_VIRTUAL_DESTRUCTOR:
5409         case RID_IS_ABSTRACT:
5410         case RID_IS_AGGREGATE:
5411         case RID_IS_BASE_OF:
5412         case RID_IS_CLASS:
5413         case RID_IS_EMPTY:
5414         case RID_IS_ENUM:
5415         case RID_IS_FINAL:
5416         case RID_IS_LITERAL_TYPE:
5417         case RID_IS_POD:
5418         case RID_IS_POLYMORPHIC:
5419         case RID_IS_SAME_AS:
5420         case RID_IS_STD_LAYOUT:
5421         case RID_IS_TRIVIAL:
5422         case RID_IS_TRIVIALLY_ASSIGNABLE:
5423         case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5424         case RID_IS_TRIVIALLY_COPYABLE:
5425         case RID_IS_UNION:
5426         case RID_IS_ASSIGNABLE:
5427         case RID_IS_CONSTRUCTIBLE:
5428           return cp_parser_trait_expr (parser, token->keyword);
5429
5430         // C++ concepts
5431         case RID_REQUIRES:
5432           return cp_parser_requires_expression (parser);
5433
5434         /* Objective-C++ expressions.  */
5435         case RID_AT_ENCODE:
5436         case RID_AT_PROTOCOL:
5437         case RID_AT_SELECTOR:
5438           return cp_parser_objc_expression (parser);
5439
5440         case RID_TEMPLATE:
5441           if (parser->in_function_body
5442               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5443                   == CPP_LESS))
5444             {
5445               error_at (token->location,
5446                         "a template declaration cannot appear at block scope");
5447               cp_parser_skip_to_end_of_block_or_statement (parser);
5448               return error_mark_node;
5449             }
5450           /* FALLTHRU */
5451         default:
5452           cp_parser_error (parser, "expected primary-expression");
5453           return error_mark_node;
5454         }
5455
5456       /* An id-expression can start with either an identifier, a
5457          `::' as the beginning of a qualified-id, or the "operator"
5458          keyword.  */
5459     case CPP_NAME:
5460     case CPP_SCOPE:
5461     case CPP_TEMPLATE_ID:
5462     case CPP_NESTED_NAME_SPECIFIER:
5463       {
5464       id_expression:
5465         cp_expr id_expression;
5466         cp_expr decl;
5467         const char *error_msg;
5468         bool template_p;
5469         bool done;
5470         cp_token *id_expr_token;
5471
5472         /* Parse the id-expression.  */
5473         id_expression
5474           = cp_parser_id_expression (parser,
5475                                      /*template_keyword_p=*/false,
5476                                      /*check_dependency_p=*/true,
5477                                      &template_p,
5478                                      /*declarator_p=*/false,
5479                                      /*optional_p=*/false);
5480         if (id_expression == error_mark_node)
5481           return error_mark_node;
5482         id_expr_token = token;
5483         token = cp_lexer_peek_token (parser->lexer);
5484         done = (token->type != CPP_OPEN_SQUARE
5485                 && token->type != CPP_OPEN_PAREN
5486                 && token->type != CPP_DOT
5487                 && token->type != CPP_DEREF
5488                 && token->type != CPP_PLUS_PLUS
5489                 && token->type != CPP_MINUS_MINUS);
5490         /* If we have a template-id, then no further lookup is
5491            required.  If the template-id was for a template-class, we
5492            will sometimes have a TYPE_DECL at this point.  */
5493         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5494                  || TREE_CODE (id_expression) == TYPE_DECL)
5495           decl = id_expression;
5496         /* Look up the name.  */
5497         else
5498           {
5499             tree ambiguous_decls;
5500
5501             /* If we already know that this lookup is ambiguous, then
5502                we've already issued an error message; there's no reason
5503                to check again.  */
5504             if (id_expr_token->type == CPP_NAME
5505                 && id_expr_token->error_reported)
5506               {
5507                 cp_parser_simulate_error (parser);
5508                 return error_mark_node;
5509               }
5510
5511             decl = cp_parser_lookup_name (parser, id_expression,
5512                                           none_type,
5513                                           template_p,
5514                                           /*is_namespace=*/false,
5515                                           /*check_dependency=*/true,
5516                                           &ambiguous_decls,
5517                                           id_expr_token->location);
5518             /* If the lookup was ambiguous, an error will already have
5519                been issued.  */
5520             if (ambiguous_decls)
5521               return error_mark_node;
5522
5523             /* In Objective-C++, we may have an Objective-C 2.0
5524                dot-syntax for classes here.  */
5525             if (c_dialect_objc ()
5526                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5527                 && TREE_CODE (decl) == TYPE_DECL
5528                 && objc_is_class_name (decl))
5529               {
5530                 tree component;
5531                 cp_lexer_consume_token (parser->lexer);
5532                 component = cp_parser_identifier (parser);
5533                 if (component == error_mark_node)
5534                   return error_mark_node;
5535
5536                 tree result = objc_build_class_component_ref (id_expression,
5537                                                               component);
5538                 /* Build a location of the form:
5539                      expr.component
5540                      ~~~~~^~~~~~~~~
5541                    with caret at the start of the component name (at
5542                    input_location), ranging from the start of the id_expression
5543                    to the end of the component name.  */
5544                 location_t combined_loc
5545                   = make_location (input_location, id_expression.get_start (),
5546                                    get_finish (input_location));
5547                 protected_set_expr_location (result, combined_loc);
5548                 return result;
5549               }
5550
5551             /* In Objective-C++, an instance variable (ivar) may be preferred
5552                to whatever cp_parser_lookup_name() found.
5553                Call objc_lookup_ivar.  To avoid exposing cp_expr to the
5554                rest of c-family, we have to do a little extra work to preserve
5555                any location information in cp_expr "decl".  Given that
5556                objc_lookup_ivar is implemented in "c-family" and "objc", we
5557                have a trip through the pure "tree" type, rather than cp_expr.
5558                Naively copying it back to "decl" would implicitly give the
5559                new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5560                store an EXPR_LOCATION.  Hence we only update "decl" (and
5561                hence its location_t) if we get back a different tree node.  */
5562             tree decl_tree = objc_lookup_ivar (decl.get_value (),
5563                                                id_expression);
5564             if (decl_tree != decl.get_value ())
5565               decl = cp_expr (decl_tree);
5566
5567             /* If name lookup gives us a SCOPE_REF, then the
5568                qualifying scope was dependent.  */
5569             if (TREE_CODE (decl) == SCOPE_REF)
5570               {
5571                 /* At this point, we do not know if DECL is a valid
5572                    integral constant expression.  We assume that it is
5573                    in fact such an expression, so that code like:
5574
5575                       template <int N> struct A {
5576                         int a[B<N>::i];
5577                       };
5578                      
5579                    is accepted.  At template-instantiation time, we
5580                    will check that B<N>::i is actually a constant.  */
5581                 return decl;
5582               }
5583             /* Check to see if DECL is a local variable in a context
5584                where that is forbidden.  */
5585             if (parser->local_variables_forbidden_p
5586                 && local_variable_p (decl))
5587               {
5588                 /* It might be that we only found DECL because we are
5589                    trying to be generous with pre-ISO scoping rules.
5590                    For example, consider:
5591
5592                      int i;
5593                      void g() {
5594                        for (int i = 0; i < 10; ++i) {}
5595                        extern void f(int j = i);
5596                      }
5597
5598                    Here, name look up will originally find the out
5599                    of scope `i'.  We need to issue a warning message,
5600                    but then use the global `i'.  */
5601                 decl = check_for_out_of_scope_variable (decl);
5602                 if (local_variable_p (decl))
5603                   {
5604                     error_at (id_expr_token->location,
5605                               "local variable %qD may not appear in this context",
5606                               decl.get_value ());
5607                     return error_mark_node;
5608                   }
5609               }
5610           }
5611
5612         if (processing_template_decl && is_overloaded_fn (decl))
5613           lookup_keep (get_fns (decl), true);
5614
5615         decl = (finish_id_expression
5616                 (id_expression, decl, parser->scope,
5617                  idk,
5618                  parser->integral_constant_expression_p,
5619                  parser->allow_non_integral_constant_expression_p,
5620                  &parser->non_integral_constant_expression_p,
5621                  template_p, done, address_p,
5622                  template_arg_p,
5623                  &error_msg,
5624                  id_expression.get_location ()));
5625         if (error_msg)
5626           cp_parser_error (parser, error_msg);
5627         decl.set_location (id_expr_token->location);
5628         return decl;
5629       }
5630
5631       /* Anything else is an error.  */
5632     default:
5633       cp_parser_error (parser, "expected primary-expression");
5634       return error_mark_node;
5635     }
5636 }
5637
5638 static inline cp_expr
5639 cp_parser_primary_expression (cp_parser *parser,
5640                               bool address_p,
5641                               bool cast_p,
5642                               bool template_arg_p,
5643                               cp_id_kind *idk)
5644 {
5645   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5646                                        /*decltype*/false, idk);
5647 }
5648
5649 /* Parse an id-expression.
5650
5651    id-expression:
5652      unqualified-id
5653      qualified-id
5654
5655    qualified-id:
5656      :: [opt] nested-name-specifier template [opt] unqualified-id
5657      :: identifier
5658      :: operator-function-id
5659      :: template-id
5660
5661    Return a representation of the unqualified portion of the
5662    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
5663    a `::' or nested-name-specifier.
5664
5665    Often, if the id-expression was a qualified-id, the caller will
5666    want to make a SCOPE_REF to represent the qualified-id.  This
5667    function does not do this in order to avoid wastefully creating
5668    SCOPE_REFs when they are not required.
5669
5670    If TEMPLATE_KEYWORD_P is true, then we have just seen the
5671    `template' keyword.
5672
5673    If CHECK_DEPENDENCY_P is false, then names are looked up inside
5674    uninstantiated templates.
5675
5676    If *TEMPLATE_P is non-NULL, it is set to true iff the
5677    `template' keyword is used to explicitly indicate that the entity
5678    named is a template.
5679
5680    If DECLARATOR_P is true, the id-expression is appearing as part of
5681    a declarator, rather than as part of an expression.  */
5682
5683 static cp_expr
5684 cp_parser_id_expression (cp_parser *parser,
5685                          bool template_keyword_p,
5686                          bool check_dependency_p,
5687                          bool *template_p,
5688                          bool declarator_p,
5689                          bool optional_p)
5690 {
5691   bool global_scope_p;
5692   bool nested_name_specifier_p;
5693
5694   /* Assume the `template' keyword was not used.  */
5695   if (template_p)
5696     *template_p = template_keyword_p;
5697
5698   /* Look for the optional `::' operator.  */
5699   global_scope_p
5700     = (!template_keyword_p
5701        && (cp_parser_global_scope_opt (parser,
5702                                        /*current_scope_valid_p=*/false)
5703            != NULL_TREE));
5704
5705   /* Look for the optional nested-name-specifier.  */
5706   nested_name_specifier_p
5707     = (cp_parser_nested_name_specifier_opt (parser,
5708                                             /*typename_keyword_p=*/false,
5709                                             check_dependency_p,
5710                                             /*type_p=*/false,
5711                                             declarator_p,
5712                                             template_keyword_p)
5713        != NULL_TREE);
5714
5715   /* If there is a nested-name-specifier, then we are looking at
5716      the first qualified-id production.  */
5717   if (nested_name_specifier_p)
5718     {
5719       tree saved_scope;
5720       tree saved_object_scope;
5721       tree saved_qualifying_scope;
5722       cp_expr unqualified_id;
5723       bool is_template;
5724
5725       /* See if the next token is the `template' keyword.  */
5726       if (!template_p)
5727         template_p = &is_template;
5728       *template_p = cp_parser_optional_template_keyword (parser);
5729       /* Name lookup we do during the processing of the
5730          unqualified-id might obliterate SCOPE.  */
5731       saved_scope = parser->scope;
5732       saved_object_scope = parser->object_scope;
5733       saved_qualifying_scope = parser->qualifying_scope;
5734       /* Process the final unqualified-id.  */
5735       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5736                                                  check_dependency_p,
5737                                                  declarator_p,
5738                                                  /*optional_p=*/false);
5739       /* Restore the SAVED_SCOPE for our caller.  */
5740       parser->scope = saved_scope;
5741       parser->object_scope = saved_object_scope;
5742       parser->qualifying_scope = saved_qualifying_scope;
5743
5744       return unqualified_id;
5745     }
5746   /* Otherwise, if we are in global scope, then we are looking at one
5747      of the other qualified-id productions.  */
5748   else if (global_scope_p)
5749     {
5750       cp_token *token;
5751       tree id;
5752
5753       /* Peek at the next token.  */
5754       token = cp_lexer_peek_token (parser->lexer);
5755
5756       /* If it's an identifier, and the next token is not a "<", then
5757          we can avoid the template-id case.  This is an optimization
5758          for this common case.  */
5759       if (token->type == CPP_NAME
5760           && !cp_parser_nth_token_starts_template_argument_list_p
5761                (parser, 2))
5762         return cp_parser_identifier (parser);
5763
5764       cp_parser_parse_tentatively (parser);
5765       /* Try a template-id.  */
5766       id = cp_parser_template_id (parser,
5767                                   /*template_keyword_p=*/false,
5768                                   /*check_dependency_p=*/true,
5769                                   none_type,
5770                                   declarator_p);
5771       /* If that worked, we're done.  */
5772       if (cp_parser_parse_definitely (parser))
5773         return id;
5774
5775       /* Peek at the next token.  (Changes in the token buffer may
5776          have invalidated the pointer obtained above.)  */
5777       token = cp_lexer_peek_token (parser->lexer);
5778
5779       switch (token->type)
5780         {
5781         case CPP_NAME:
5782           return cp_parser_identifier (parser);
5783
5784         case CPP_KEYWORD:
5785           if (token->keyword == RID_OPERATOR)
5786             return cp_parser_operator_function_id (parser);
5787           /* Fall through.  */
5788
5789         default:
5790           cp_parser_error (parser, "expected id-expression");
5791           return error_mark_node;
5792         }
5793     }
5794   else
5795     return cp_parser_unqualified_id (parser, template_keyword_p,
5796                                      /*check_dependency_p=*/true,
5797                                      declarator_p,
5798                                      optional_p);
5799 }
5800
5801 /* Parse an unqualified-id.
5802
5803    unqualified-id:
5804      identifier
5805      operator-function-id
5806      conversion-function-id
5807      ~ class-name
5808      template-id
5809
5810    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5811    keyword, in a construct like `A::template ...'.
5812
5813    Returns a representation of unqualified-id.  For the `identifier'
5814    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
5815    production a BIT_NOT_EXPR is returned; the operand of the
5816    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
5817    other productions, see the documentation accompanying the
5818    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
5819    names are looked up in uninstantiated templates.  If DECLARATOR_P
5820    is true, the unqualified-id is appearing as part of a declarator,
5821    rather than as part of an expression.  */
5822
5823 static cp_expr
5824 cp_parser_unqualified_id (cp_parser* parser,
5825                           bool template_keyword_p,
5826                           bool check_dependency_p,
5827                           bool declarator_p,
5828                           bool optional_p)
5829 {
5830   cp_token *token;
5831
5832   /* Peek at the next token.  */
5833   token = cp_lexer_peek_token (parser->lexer);
5834
5835   switch ((int) token->type)
5836     {
5837     case CPP_NAME:
5838       {
5839         tree id;
5840
5841         /* We don't know yet whether or not this will be a
5842            template-id.  */
5843         cp_parser_parse_tentatively (parser);
5844         /* Try a template-id.  */
5845         id = cp_parser_template_id (parser, template_keyword_p,
5846                                     check_dependency_p,
5847                                     none_type,
5848                                     declarator_p);
5849         /* If it worked, we're done.  */
5850         if (cp_parser_parse_definitely (parser))
5851           return id;
5852         /* Otherwise, it's an ordinary identifier.  */
5853         return cp_parser_identifier (parser);
5854       }
5855
5856     case CPP_TEMPLATE_ID:
5857       return cp_parser_template_id (parser, template_keyword_p,
5858                                     check_dependency_p,
5859                                     none_type,
5860                                     declarator_p);
5861
5862     case CPP_COMPL:
5863       {
5864         tree type_decl;
5865         tree qualifying_scope;
5866         tree object_scope;
5867         tree scope;
5868         bool done;
5869
5870         /* Consume the `~' token.  */
5871         cp_lexer_consume_token (parser->lexer);
5872         /* Parse the class-name.  The standard, as written, seems to
5873            say that:
5874
5875              template <typename T> struct S { ~S (); };
5876              template <typename T> S<T>::~S() {}
5877
5878            is invalid, since `~' must be followed by a class-name, but
5879            `S<T>' is dependent, and so not known to be a class.
5880            That's not right; we need to look in uninstantiated
5881            templates.  A further complication arises from:
5882
5883              template <typename T> void f(T t) {
5884                t.T::~T();
5885              }
5886
5887            Here, it is not possible to look up `T' in the scope of `T'
5888            itself.  We must look in both the current scope, and the
5889            scope of the containing complete expression.
5890
5891            Yet another issue is:
5892
5893              struct S {
5894                int S;
5895                ~S();
5896              };
5897
5898              S::~S() {}
5899
5900            The standard does not seem to say that the `S' in `~S'
5901            should refer to the type `S' and not the data member
5902            `S::S'.  */
5903
5904         /* DR 244 says that we look up the name after the "~" in the
5905            same scope as we looked up the qualifying name.  That idea
5906            isn't fully worked out; it's more complicated than that.  */
5907         scope = parser->scope;
5908         object_scope = parser->object_scope;
5909         qualifying_scope = parser->qualifying_scope;
5910
5911         /* Check for invalid scopes.  */
5912         if (scope == error_mark_node)
5913           {
5914             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5915               cp_lexer_consume_token (parser->lexer);
5916             return error_mark_node;
5917           }
5918         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5919           {
5920             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5921               error_at (token->location,
5922                         "scope %qT before %<~%> is not a class-name",
5923                         scope);
5924             cp_parser_simulate_error (parser);
5925             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5926               cp_lexer_consume_token (parser->lexer);
5927             return error_mark_node;
5928           }
5929         gcc_assert (!scope || TYPE_P (scope));
5930
5931         /* If the name is of the form "X::~X" it's OK even if X is a
5932            typedef.  */
5933         token = cp_lexer_peek_token (parser->lexer);
5934         if (scope
5935             && token->type == CPP_NAME
5936             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5937                 != CPP_LESS)
5938             && (token->u.value == TYPE_IDENTIFIER (scope)
5939                 || (CLASS_TYPE_P (scope)
5940                     && constructor_name_p (token->u.value, scope))))
5941           {
5942             cp_lexer_consume_token (parser->lexer);
5943             return build_nt (BIT_NOT_EXPR, scope);
5944           }
5945
5946         /* ~auto means the destructor of whatever the object is.  */
5947         if (cp_parser_is_keyword (token, RID_AUTO))
5948           {
5949             if (cxx_dialect < cxx14)
5950               pedwarn (input_location, 0,
5951                        "%<~auto%> only available with "
5952                        "-std=c++14 or -std=gnu++14");
5953             cp_lexer_consume_token (parser->lexer);
5954             return build_nt (BIT_NOT_EXPR, make_auto ());
5955           }
5956
5957         /* If there was an explicit qualification (S::~T), first look
5958            in the scope given by the qualification (i.e., S).
5959
5960            Note: in the calls to cp_parser_class_name below we pass
5961            typename_type so that lookup finds the injected-class-name
5962            rather than the constructor.  */
5963         done = false;
5964         type_decl = NULL_TREE;
5965         if (scope)
5966           {
5967             cp_parser_parse_tentatively (parser);
5968             type_decl = cp_parser_class_name (parser,
5969                                               /*typename_keyword_p=*/false,
5970                                               /*template_keyword_p=*/false,
5971                                               typename_type,
5972                                               /*check_dependency=*/false,
5973                                               /*class_head_p=*/false,
5974                                               declarator_p);
5975             if (cp_parser_parse_definitely (parser))
5976               done = true;
5977           }
5978         /* In "N::S::~S", look in "N" as well.  */
5979         if (!done && scope && qualifying_scope)
5980           {
5981             cp_parser_parse_tentatively (parser);
5982             parser->scope = qualifying_scope;
5983             parser->object_scope = NULL_TREE;
5984             parser->qualifying_scope = NULL_TREE;
5985             type_decl
5986               = cp_parser_class_name (parser,
5987                                       /*typename_keyword_p=*/false,
5988                                       /*template_keyword_p=*/false,
5989                                       typename_type,
5990                                       /*check_dependency=*/false,
5991                                       /*class_head_p=*/false,
5992                                       declarator_p);
5993             if (cp_parser_parse_definitely (parser))
5994               done = true;
5995           }
5996         /* In "p->S::~T", look in the scope given by "*p" as well.  */
5997         else if (!done && object_scope)
5998           {
5999             cp_parser_parse_tentatively (parser);
6000             parser->scope = object_scope;
6001             parser->object_scope = NULL_TREE;
6002             parser->qualifying_scope = NULL_TREE;
6003             type_decl
6004               = cp_parser_class_name (parser,
6005                                       /*typename_keyword_p=*/false,
6006                                       /*template_keyword_p=*/false,
6007                                       typename_type,
6008                                       /*check_dependency=*/false,
6009                                       /*class_head_p=*/false,
6010                                       declarator_p);
6011             if (cp_parser_parse_definitely (parser))
6012               done = true;
6013           }
6014         /* Look in the surrounding context.  */
6015         if (!done)
6016           {
6017             parser->scope = NULL_TREE;
6018             parser->object_scope = NULL_TREE;
6019             parser->qualifying_scope = NULL_TREE;
6020             if (processing_template_decl)
6021               cp_parser_parse_tentatively (parser);
6022             type_decl
6023               = cp_parser_class_name (parser,
6024                                       /*typename_keyword_p=*/false,
6025                                       /*template_keyword_p=*/false,
6026                                       typename_type,
6027                                       /*check_dependency=*/false,
6028                                       /*class_head_p=*/false,
6029                                       declarator_p);
6030             if (processing_template_decl
6031                 && ! cp_parser_parse_definitely (parser))
6032               {
6033                 /* We couldn't find a type with this name.  If we're parsing
6034                    tentatively, fail and try something else.  */
6035                 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6036                   {
6037                     cp_parser_simulate_error (parser);
6038                     return error_mark_node;
6039                   }
6040                 /* Otherwise, accept it and check for a match at instantiation
6041                    time.  */
6042                 type_decl = cp_parser_identifier (parser);
6043                 if (type_decl != error_mark_node)
6044                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6045                 return type_decl;
6046               }
6047           }
6048         /* If an error occurred, assume that the name of the
6049            destructor is the same as the name of the qualifying
6050            class.  That allows us to keep parsing after running
6051            into ill-formed destructor names.  */
6052         if (type_decl == error_mark_node && scope)
6053           return build_nt (BIT_NOT_EXPR, scope);
6054         else if (type_decl == error_mark_node)
6055           return error_mark_node;
6056
6057         /* Check that destructor name and scope match.  */
6058         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6059           {
6060             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6061               error_at (token->location,
6062                         "declaration of %<~%T%> as member of %qT",
6063                         type_decl, scope);
6064             cp_parser_simulate_error (parser);
6065             return error_mark_node;
6066           }
6067
6068         /* [class.dtor]
6069
6070            A typedef-name that names a class shall not be used as the
6071            identifier in the declarator for a destructor declaration.  */
6072         if (declarator_p
6073             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6074             && !DECL_SELF_REFERENCE_P (type_decl)
6075             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6076           error_at (token->location,
6077                     "typedef-name %qD used as destructor declarator",
6078                     type_decl);
6079
6080         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6081       }
6082
6083     case CPP_KEYWORD:
6084       if (token->keyword == RID_OPERATOR)
6085         {
6086           cp_expr id;
6087
6088           /* This could be a template-id, so we try that first.  */
6089           cp_parser_parse_tentatively (parser);
6090           /* Try a template-id.  */
6091           id = cp_parser_template_id (parser, template_keyword_p,
6092                                       /*check_dependency_p=*/true,
6093                                       none_type,
6094                                       declarator_p);
6095           /* If that worked, we're done.  */
6096           if (cp_parser_parse_definitely (parser))
6097             return id;
6098           /* We still don't know whether we're looking at an
6099              operator-function-id or a conversion-function-id.  */
6100           cp_parser_parse_tentatively (parser);
6101           /* Try an operator-function-id.  */
6102           id = cp_parser_operator_function_id (parser);
6103           /* If that didn't work, try a conversion-function-id.  */
6104           if (!cp_parser_parse_definitely (parser))
6105             id = cp_parser_conversion_function_id (parser);
6106
6107           return id;
6108         }
6109       /* Fall through.  */
6110
6111     default:
6112       if (optional_p)
6113         return NULL_TREE;
6114       cp_parser_error (parser, "expected unqualified-id");
6115       return error_mark_node;
6116     }
6117 }
6118
6119 /* Parse an (optional) nested-name-specifier.
6120
6121    nested-name-specifier: [C++98]
6122      class-or-namespace-name :: nested-name-specifier [opt]
6123      class-or-namespace-name :: template nested-name-specifier [opt]
6124
6125    nested-name-specifier: [C++0x]
6126      type-name ::
6127      namespace-name ::
6128      nested-name-specifier identifier ::
6129      nested-name-specifier template [opt] simple-template-id ::
6130
6131    PARSER->SCOPE should be set appropriately before this function is
6132    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6133    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
6134    in name lookups.
6135
6136    Sets PARSER->SCOPE to the class (TYPE) or namespace
6137    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6138    it unchanged if there is no nested-name-specifier.  Returns the new
6139    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6140
6141    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6142    part of a declaration and/or decl-specifier.  */
6143
6144 static tree
6145 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6146                                      bool typename_keyword_p,
6147                                      bool check_dependency_p,
6148                                      bool type_p,
6149                                      bool is_declaration,
6150                                      bool template_keyword_p /* = false */)
6151 {
6152   bool success = false;
6153   cp_token_position start = 0;
6154   cp_token *token;
6155
6156   /* Remember where the nested-name-specifier starts.  */
6157   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6158     {
6159       start = cp_lexer_token_position (parser->lexer, false);
6160       push_deferring_access_checks (dk_deferred);
6161     }
6162
6163   while (true)
6164     {
6165       tree new_scope;
6166       tree old_scope;
6167       tree saved_qualifying_scope;
6168
6169       /* Spot cases that cannot be the beginning of a
6170          nested-name-specifier.  */
6171       token = cp_lexer_peek_token (parser->lexer);
6172
6173       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6174          the already parsed nested-name-specifier.  */
6175       if (token->type == CPP_NESTED_NAME_SPECIFIER)
6176         {
6177           /* Grab the nested-name-specifier and continue the loop.  */
6178           cp_parser_pre_parsed_nested_name_specifier (parser);
6179           /* If we originally encountered this nested-name-specifier
6180              with IS_DECLARATION set to false, we will not have
6181              resolved TYPENAME_TYPEs, so we must do so here.  */
6182           if (is_declaration
6183               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6184             {
6185               new_scope = resolve_typename_type (parser->scope,
6186                                                  /*only_current_p=*/false);
6187               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6188                 parser->scope = new_scope;
6189             }
6190           success = true;
6191           continue;
6192         }
6193
6194       /* Spot cases that cannot be the beginning of a
6195          nested-name-specifier.  On the second and subsequent times
6196          through the loop, we look for the `template' keyword.  */
6197       if (success && token->keyword == RID_TEMPLATE)
6198         ;
6199       /* A template-id can start a nested-name-specifier.  */
6200       else if (token->type == CPP_TEMPLATE_ID)
6201         ;
6202       /* DR 743: decltype can be used in a nested-name-specifier.  */
6203       else if (token_is_decltype (token))
6204         ;
6205       else
6206         {
6207           /* If the next token is not an identifier, then it is
6208              definitely not a type-name or namespace-name.  */
6209           if (token->type != CPP_NAME)
6210             break;
6211           /* If the following token is neither a `<' (to begin a
6212              template-id), nor a `::', then we are not looking at a
6213              nested-name-specifier.  */
6214           token = cp_lexer_peek_nth_token (parser->lexer, 2);
6215
6216           if (token->type == CPP_COLON
6217               && parser->colon_corrects_to_scope_p
6218               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6219             {
6220               gcc_rich_location richloc (token->location);
6221               richloc.add_fixit_replace ("::");
6222               error_at (&richloc,
6223                         "found %<:%> in nested-name-specifier, "
6224                         "expected %<::%>");
6225               token->type = CPP_SCOPE;
6226             }
6227
6228           if (token->type != CPP_SCOPE
6229               && !cp_parser_nth_token_starts_template_argument_list_p
6230                   (parser, 2))
6231             break;
6232         }
6233
6234       /* The nested-name-specifier is optional, so we parse
6235          tentatively.  */
6236       cp_parser_parse_tentatively (parser);
6237
6238       /* Look for the optional `template' keyword, if this isn't the
6239          first time through the loop.  */
6240       if (success)
6241         template_keyword_p = cp_parser_optional_template_keyword (parser);
6242
6243       /* Save the old scope since the name lookup we are about to do
6244          might destroy it.  */
6245       old_scope = parser->scope;
6246       saved_qualifying_scope = parser->qualifying_scope;
6247       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6248          look up names in "X<T>::I" in order to determine that "Y" is
6249          a template.  So, if we have a typename at this point, we make
6250          an effort to look through it.  */
6251       if (is_declaration
6252           && !typename_keyword_p
6253           && parser->scope
6254           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6255         parser->scope = resolve_typename_type (parser->scope,
6256                                                /*only_current_p=*/false);
6257       /* Parse the qualifying entity.  */
6258       new_scope
6259         = cp_parser_qualifying_entity (parser,
6260                                        typename_keyword_p,
6261                                        template_keyword_p,
6262                                        check_dependency_p,
6263                                        type_p,
6264                                        is_declaration);
6265       /* Look for the `::' token.  */
6266       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6267
6268       /* If we found what we wanted, we keep going; otherwise, we're
6269          done.  */
6270       if (!cp_parser_parse_definitely (parser))
6271         {
6272           bool error_p = false;
6273
6274           /* Restore the OLD_SCOPE since it was valid before the
6275              failed attempt at finding the last
6276              class-or-namespace-name.  */
6277           parser->scope = old_scope;
6278           parser->qualifying_scope = saved_qualifying_scope;
6279
6280           /* If the next token is a decltype, and the one after that is a
6281              `::', then the decltype has failed to resolve to a class or
6282              enumeration type.  Give this error even when parsing
6283              tentatively since it can't possibly be valid--and we're going
6284              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6285              won't get another chance.*/
6286           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6287               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6288                   == CPP_SCOPE))
6289             {
6290               token = cp_lexer_consume_token (parser->lexer);
6291               error_at (token->location, "decltype evaluates to %qT, "
6292                         "which is not a class or enumeration type",
6293                         token->u.tree_check_value->value);
6294               parser->scope = error_mark_node;
6295               error_p = true;
6296               /* As below.  */
6297               success = true;
6298               cp_lexer_consume_token (parser->lexer);
6299             }
6300
6301           if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6302               && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6303             {
6304               /* If we have a non-type template-id followed by ::, it can't
6305                  possibly be valid.  */
6306               token = cp_lexer_peek_token (parser->lexer);
6307               tree tid = token->u.tree_check_value->value;
6308               if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6309                   && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6310                 {
6311                   tree tmpl = NULL_TREE;
6312                   if (is_overloaded_fn (tid))
6313                     {
6314                       tree fns = get_fns (tid);
6315                       if (OVL_SINGLE_P (fns))
6316                         tmpl = OVL_FIRST (fns);
6317                       error_at (token->location, "function template-id %qD "
6318                                 "in nested-name-specifier", tid);
6319                     }
6320                   else
6321                     {
6322                       /* Variable template.  */
6323                       tmpl = TREE_OPERAND (tid, 0);
6324                       gcc_assert (variable_template_p (tmpl));
6325                       error_at (token->location, "variable template-id %qD "
6326                                 "in nested-name-specifier", tid);
6327                     }
6328                   if (tmpl)
6329                     inform (DECL_SOURCE_LOCATION (tmpl),
6330                             "%qD declared here", tmpl);
6331
6332                   parser->scope = error_mark_node;
6333                   error_p = true;
6334                   /* As below.  */
6335                   success = true;
6336                   cp_lexer_consume_token (parser->lexer);
6337                   cp_lexer_consume_token (parser->lexer);
6338                 }
6339             }
6340
6341           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6342             break;
6343           /* If the next token is an identifier, and the one after
6344              that is a `::', then any valid interpretation would have
6345              found a class-or-namespace-name.  */
6346           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6347                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6348                      == CPP_SCOPE)
6349                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6350                      != CPP_COMPL))
6351             {
6352               token = cp_lexer_consume_token (parser->lexer);
6353               if (!error_p)
6354                 {
6355                   if (!token->error_reported)
6356                     {
6357                       tree decl;
6358                       tree ambiguous_decls;
6359
6360                       decl = cp_parser_lookup_name (parser, token->u.value,
6361                                                     none_type,
6362                                                     /*is_template=*/false,
6363                                                     /*is_namespace=*/false,
6364                                                     /*check_dependency=*/true,
6365                                                     &ambiguous_decls,
6366                                                     token->location);
6367                       if (TREE_CODE (decl) == TEMPLATE_DECL)
6368                         error_at (token->location,
6369                                   "%qD used without template parameters",
6370                                   decl);
6371                       else if (ambiguous_decls)
6372                         {
6373                           // cp_parser_lookup_name has the same diagnostic,
6374                           // thus make sure to emit it at most once.
6375                           if (cp_parser_uncommitted_to_tentative_parse_p
6376                               (parser))
6377                             {
6378                               error_at (token->location,
6379                                         "reference to %qD is ambiguous",
6380                                         token->u.value);
6381                               print_candidates (ambiguous_decls);
6382                             }
6383                           decl = error_mark_node;
6384                         }
6385                       else
6386                         {
6387                           if (cxx_dialect != cxx98)
6388                             cp_parser_name_lookup_error
6389                             (parser, token->u.value, decl, NLE_NOT_CXX98,
6390                              token->location);
6391                           else
6392                             cp_parser_name_lookup_error
6393                             (parser, token->u.value, decl, NLE_CXX98,
6394                              token->location);
6395                         }
6396                     }
6397                   parser->scope = error_mark_node;
6398                   error_p = true;
6399                   /* Treat this as a successful nested-name-specifier
6400                      due to:
6401
6402                      [basic.lookup.qual]
6403
6404                      If the name found is not a class-name (clause
6405                      _class_) or namespace-name (_namespace.def_), the
6406                      program is ill-formed.  */
6407                   success = true;
6408                 }
6409               cp_lexer_consume_token (parser->lexer);
6410             }
6411           break;
6412         }
6413       /* We've found one valid nested-name-specifier.  */
6414       success = true;
6415       /* Name lookup always gives us a DECL.  */
6416       if (TREE_CODE (new_scope) == TYPE_DECL)
6417         new_scope = TREE_TYPE (new_scope);
6418       /* Uses of "template" must be followed by actual templates.  */
6419       if (template_keyword_p
6420           && !(CLASS_TYPE_P (new_scope)
6421                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6422                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6423                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
6424           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6425                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6426                    == TEMPLATE_ID_EXPR)))
6427         permerror (input_location, TYPE_P (new_scope)
6428                    ? G_("%qT is not a template")
6429                    : G_("%qD is not a template"),
6430                    new_scope);
6431       /* If it is a class scope, try to complete it; we are about to
6432          be looking up names inside the class.  */
6433       if (TYPE_P (new_scope)
6434           /* Since checking types for dependency can be expensive,
6435              avoid doing it if the type is already complete.  */
6436           && !COMPLETE_TYPE_P (new_scope)
6437           /* Do not try to complete dependent types.  */
6438           && !dependent_type_p (new_scope))
6439         {
6440           new_scope = complete_type (new_scope);
6441           /* If it is a typedef to current class, use the current
6442              class instead, as the typedef won't have any names inside
6443              it yet.  */
6444           if (!COMPLETE_TYPE_P (new_scope)
6445               && currently_open_class (new_scope))
6446             new_scope = TYPE_MAIN_VARIANT (new_scope);
6447         }
6448       /* Make sure we look in the right scope the next time through
6449          the loop.  */
6450       parser->scope = new_scope;
6451     }
6452
6453   /* If parsing tentatively, replace the sequence of tokens that makes
6454      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6455      token.  That way, should we re-parse the token stream, we will
6456      not have to repeat the effort required to do the parse, nor will
6457      we issue duplicate error messages.  */
6458   if (success && start)
6459     {
6460       cp_token *token;
6461
6462       token = cp_lexer_token_at (parser->lexer, start);
6463       /* Reset the contents of the START token.  */
6464       token->type = CPP_NESTED_NAME_SPECIFIER;
6465       /* Retrieve any deferred checks.  Do not pop this access checks yet
6466          so the memory will not be reclaimed during token replacing below.  */
6467       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6468       token->u.tree_check_value->value = parser->scope;
6469       token->u.tree_check_value->checks = get_deferred_access_checks ();
6470       token->u.tree_check_value->qualifying_scope =
6471         parser->qualifying_scope;
6472       token->keyword = RID_MAX;
6473
6474       /* Purge all subsequent tokens.  */
6475       cp_lexer_purge_tokens_after (parser->lexer, start);
6476     }
6477
6478   if (start)
6479     pop_to_parent_deferring_access_checks ();
6480
6481   return success ? parser->scope : NULL_TREE;
6482 }
6483
6484 /* Parse a nested-name-specifier.  See
6485    cp_parser_nested_name_specifier_opt for details.  This function
6486    behaves identically, except that it will an issue an error if no
6487    nested-name-specifier is present.  */
6488
6489 static tree
6490 cp_parser_nested_name_specifier (cp_parser *parser,
6491                                  bool typename_keyword_p,
6492                                  bool check_dependency_p,
6493                                  bool type_p,
6494                                  bool is_declaration)
6495 {
6496   tree scope;
6497
6498   /* Look for the nested-name-specifier.  */
6499   scope = cp_parser_nested_name_specifier_opt (parser,
6500                                                typename_keyword_p,
6501                                                check_dependency_p,
6502                                                type_p,
6503                                                is_declaration);
6504   /* If it was not present, issue an error message.  */
6505   if (!scope)
6506     {
6507       cp_parser_error (parser, "expected nested-name-specifier");
6508       parser->scope = NULL_TREE;
6509     }
6510
6511   return scope;
6512 }
6513
6514 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6515    this is either a class-name or a namespace-name (which corresponds
6516    to the class-or-namespace-name production in the grammar). For
6517    C++0x, it can also be a type-name that refers to an enumeration
6518    type or a simple-template-id.
6519
6520    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6521    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6522    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6523    TYPE_P is TRUE iff the next name should be taken as a class-name,
6524    even the same name is declared to be another entity in the same
6525    scope.
6526
6527    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6528    specified by the class-or-namespace-name.  If neither is found the
6529    ERROR_MARK_NODE is returned.  */
6530
6531 static tree
6532 cp_parser_qualifying_entity (cp_parser *parser,
6533                              bool typename_keyword_p,
6534                              bool template_keyword_p,
6535                              bool check_dependency_p,
6536                              bool type_p,
6537                              bool is_declaration)
6538 {
6539   tree saved_scope;
6540   tree saved_qualifying_scope;
6541   tree saved_object_scope;
6542   tree scope;
6543   bool only_class_p;
6544   bool successful_parse_p;
6545
6546   /* DR 743: decltype can appear in a nested-name-specifier.  */
6547   if (cp_lexer_next_token_is_decltype (parser->lexer))
6548     {
6549       scope = cp_parser_decltype (parser);
6550       if (TREE_CODE (scope) != ENUMERAL_TYPE
6551           && !MAYBE_CLASS_TYPE_P (scope))
6552         {
6553           cp_parser_simulate_error (parser);
6554           return error_mark_node;
6555         }
6556       if (TYPE_NAME (scope))
6557         scope = TYPE_NAME (scope);
6558       return scope;
6559     }
6560
6561   /* Before we try to parse the class-name, we must save away the
6562      current PARSER->SCOPE since cp_parser_class_name will destroy
6563      it.  */
6564   saved_scope = parser->scope;
6565   saved_qualifying_scope = parser->qualifying_scope;
6566   saved_object_scope = parser->object_scope;
6567   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
6568      there is no need to look for a namespace-name.  */
6569   only_class_p = template_keyword_p 
6570     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6571   if (!only_class_p)
6572     cp_parser_parse_tentatively (parser);
6573   scope = cp_parser_class_name (parser,
6574                                 typename_keyword_p,
6575                                 template_keyword_p,
6576                                 type_p ? class_type : none_type,
6577                                 check_dependency_p,
6578                                 /*class_head_p=*/false,
6579                                 is_declaration,
6580                                 /*enum_ok=*/cxx_dialect > cxx98);
6581   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6582   /* If that didn't work, try for a namespace-name.  */
6583   if (!only_class_p && !successful_parse_p)
6584     {
6585       /* Restore the saved scope.  */
6586       parser->scope = saved_scope;
6587       parser->qualifying_scope = saved_qualifying_scope;
6588       parser->object_scope = saved_object_scope;
6589       /* If we are not looking at an identifier followed by the scope
6590          resolution operator, then this is not part of a
6591          nested-name-specifier.  (Note that this function is only used
6592          to parse the components of a nested-name-specifier.)  */
6593       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6594           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6595         return error_mark_node;
6596       scope = cp_parser_namespace_name (parser);
6597     }
6598
6599   return scope;
6600 }
6601
6602 /* Return true if we are looking at a compound-literal, false otherwise.  */
6603
6604 static bool
6605 cp_parser_compound_literal_p (cp_parser *parser)
6606 {
6607   cp_lexer_save_tokens (parser->lexer);
6608
6609   /* Skip tokens until the next token is a closing parenthesis.
6610      If we find the closing `)', and the next token is a `{', then
6611      we are looking at a compound-literal.  */
6612   bool compound_literal_p
6613     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6614                                               /*consume_paren=*/true)
6615        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6616   
6617   /* Roll back the tokens we skipped.  */
6618   cp_lexer_rollback_tokens (parser->lexer);
6619
6620   return compound_literal_p;
6621 }
6622
6623 /* Return true if EXPR is the integer constant zero or a complex constant
6624    of zero, without any folding, but ignoring location wrappers.  */
6625
6626 static bool
6627 literal_integer_zerop (const_tree expr)
6628 {
6629   STRIP_ANY_LOCATION_WRAPPER (expr);
6630   return integer_zerop (expr);
6631 }
6632
6633 /* Parse a postfix-expression.
6634
6635    postfix-expression:
6636      primary-expression
6637      postfix-expression [ expression ]
6638      postfix-expression ( expression-list [opt] )
6639      simple-type-specifier ( expression-list [opt] )
6640      typename :: [opt] nested-name-specifier identifier
6641        ( expression-list [opt] )
6642      typename :: [opt] nested-name-specifier template [opt] template-id
6643        ( expression-list [opt] )
6644      postfix-expression . template [opt] id-expression
6645      postfix-expression -> template [opt] id-expression
6646      postfix-expression . pseudo-destructor-name
6647      postfix-expression -> pseudo-destructor-name
6648      postfix-expression ++
6649      postfix-expression --
6650      dynamic_cast < type-id > ( expression )
6651      static_cast < type-id > ( expression )
6652      reinterpret_cast < type-id > ( expression )
6653      const_cast < type-id > ( expression )
6654      typeid ( expression )
6655      typeid ( type-id )
6656
6657    GNU Extension:
6658
6659    postfix-expression:
6660      ( type-id ) { initializer-list , [opt] }
6661
6662    This extension is a GNU version of the C99 compound-literal
6663    construct.  (The C99 grammar uses `type-name' instead of `type-id',
6664    but they are essentially the same concept.)
6665
6666    If ADDRESS_P is true, the postfix expression is the operand of the
6667    `&' operator.  CAST_P is true if this expression is the target of a
6668    cast.
6669
6670    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6671    class member access expressions [expr.ref].
6672
6673    Returns a representation of the expression.  */
6674
6675 static cp_expr
6676 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6677                               bool member_access_only_p, bool decltype_p,
6678                               cp_id_kind * pidk_return)
6679 {
6680   cp_token *token;
6681   location_t loc;
6682   enum rid keyword;
6683   cp_id_kind idk = CP_ID_KIND_NONE;
6684   cp_expr postfix_expression = NULL_TREE;
6685   bool is_member_access = false;
6686
6687   /* Peek at the next token.  */
6688   token = cp_lexer_peek_token (parser->lexer);
6689   loc = token->location;
6690   location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6691
6692   /* Some of the productions are determined by keywords.  */
6693   keyword = token->keyword;
6694   switch (keyword)
6695     {
6696     case RID_DYNCAST:
6697     case RID_STATCAST:
6698     case RID_REINTCAST:
6699     case RID_CONSTCAST:
6700       {
6701         tree type;
6702         cp_expr expression;
6703         const char *saved_message;
6704         bool saved_in_type_id_in_expr_p;
6705
6706         /* All of these can be handled in the same way from the point
6707            of view of parsing.  Begin by consuming the token
6708            identifying the cast.  */
6709         cp_lexer_consume_token (parser->lexer);
6710
6711         /* New types cannot be defined in the cast.  */
6712         saved_message = parser->type_definition_forbidden_message;
6713         parser->type_definition_forbidden_message
6714           = G_("types may not be defined in casts");
6715
6716         /* Look for the opening `<'.  */
6717         cp_parser_require (parser, CPP_LESS, RT_LESS);
6718         /* Parse the type to which we are casting.  */
6719         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6720         parser->in_type_id_in_expr_p = true;
6721         type = cp_parser_type_id (parser);
6722         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6723         /* Look for the closing `>'.  */
6724         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6725         /* Restore the old message.  */
6726         parser->type_definition_forbidden_message = saved_message;
6727
6728         bool saved_greater_than_is_operator_p
6729           = parser->greater_than_is_operator_p;
6730         parser->greater_than_is_operator_p = true;
6731
6732         /* And the expression which is being cast.  */
6733         matching_parens parens;
6734         parens.require_open (parser);
6735         expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6736         cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6737                                                    RT_CLOSE_PAREN);
6738         location_t end_loc = close_paren ?
6739           close_paren->location : UNKNOWN_LOCATION;
6740
6741         parser->greater_than_is_operator_p
6742           = saved_greater_than_is_operator_p;
6743
6744         /* Only type conversions to integral or enumeration types
6745            can be used in constant-expressions.  */
6746         if (!cast_valid_in_integral_constant_expression_p (type)
6747             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6748           {
6749             postfix_expression = error_mark_node;
6750             break;
6751           }
6752
6753         switch (keyword)
6754           {
6755           case RID_DYNCAST:
6756             postfix_expression
6757               = build_dynamic_cast (type, expression, tf_warning_or_error);
6758             break;
6759           case RID_STATCAST:
6760             postfix_expression
6761               = build_static_cast (type, expression, tf_warning_or_error);
6762             break;
6763           case RID_REINTCAST:
6764             postfix_expression
6765               = build_reinterpret_cast (type, expression, 
6766                                         tf_warning_or_error);
6767             break;
6768           case RID_CONSTCAST:
6769             postfix_expression
6770               = build_const_cast (type, expression, tf_warning_or_error);
6771             break;
6772           default:
6773             gcc_unreachable ();
6774           }
6775
6776         /* Construct a location e.g. :
6777              reinterpret_cast <int *> (expr)
6778              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6779            ranging from the start of the "*_cast" token to the final closing
6780            paren, with the caret at the start.  */
6781         location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6782         postfix_expression.set_location (cp_cast_loc);
6783       }
6784       break;
6785
6786     case RID_TYPEID:
6787       {
6788         tree type;
6789         const char *saved_message;
6790         bool saved_in_type_id_in_expr_p;
6791
6792         /* Consume the `typeid' token.  */
6793         cp_lexer_consume_token (parser->lexer);
6794         /* Look for the `(' token.  */
6795         matching_parens parens;
6796         parens.require_open (parser);
6797         /* Types cannot be defined in a `typeid' expression.  */
6798         saved_message = parser->type_definition_forbidden_message;
6799         parser->type_definition_forbidden_message
6800           = G_("types may not be defined in a %<typeid%> expression");
6801         /* We can't be sure yet whether we're looking at a type-id or an
6802            expression.  */
6803         cp_parser_parse_tentatively (parser);
6804         /* Try a type-id first.  */
6805         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6806         parser->in_type_id_in_expr_p = true;
6807         type = cp_parser_type_id (parser);
6808         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6809         /* Look for the `)' token.  Otherwise, we can't be sure that
6810            we're not looking at an expression: consider `typeid (int
6811            (3))', for example.  */
6812         cp_token *close_paren = parens.require_close (parser);
6813         /* If all went well, simply lookup the type-id.  */
6814         if (cp_parser_parse_definitely (parser))
6815           postfix_expression = get_typeid (type, tf_warning_or_error);
6816         /* Otherwise, fall back to the expression variant.  */
6817         else
6818           {
6819             tree expression;
6820
6821             /* Look for an expression.  */
6822             expression = cp_parser_expression (parser, & idk);
6823             /* Compute its typeid.  */
6824             postfix_expression = build_typeid (expression, tf_warning_or_error);
6825             /* Look for the `)' token.  */
6826             close_paren = parens.require_close (parser);
6827           }
6828         /* Restore the saved message.  */
6829         parser->type_definition_forbidden_message = saved_message;
6830         /* `typeid' may not appear in an integral constant expression.  */
6831         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6832           postfix_expression = error_mark_node;
6833
6834         /* Construct a location e.g. :
6835              typeid (expr)
6836              ^~~~~~~~~~~~~
6837            ranging from the start of the "typeid" token to the final closing
6838            paren, with the caret at the start.  */
6839         if (close_paren)
6840           {
6841             location_t typeid_loc
6842               = make_location (start_loc, start_loc, close_paren->location);
6843             postfix_expression.set_location (typeid_loc);
6844             postfix_expression.maybe_add_location_wrapper ();
6845           }
6846       }
6847       break;
6848
6849     case RID_TYPENAME:
6850       {
6851         tree type;
6852         /* The syntax permitted here is the same permitted for an
6853            elaborated-type-specifier.  */
6854         ++parser->prevent_constrained_type_specifiers;
6855         type = cp_parser_elaborated_type_specifier (parser,
6856                                                     /*is_friend=*/false,
6857                                                     /*is_declaration=*/false);
6858         --parser->prevent_constrained_type_specifiers;
6859         postfix_expression = cp_parser_functional_cast (parser, type);
6860       }
6861       break;
6862
6863     case RID_ADDRESSOF:
6864     case RID_BUILTIN_SHUFFLE:
6865     case RID_BUILTIN_LAUNDER:
6866       {
6867         vec<tree, va_gc> *vec;
6868         unsigned int i;
6869         tree p;
6870
6871         cp_lexer_consume_token (parser->lexer);
6872         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6873                     /*cast_p=*/false, /*allow_expansion_p=*/true,
6874                     /*non_constant_p=*/NULL);
6875         if (vec == NULL)
6876           {
6877             postfix_expression = error_mark_node;
6878             break;
6879           }
6880
6881         FOR_EACH_VEC_ELT (*vec, i, p)
6882           mark_exp_read (p);
6883
6884         switch (keyword)
6885           {
6886           case RID_ADDRESSOF:
6887             if (vec->length () == 1)
6888               postfix_expression
6889                 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6890             else
6891               {
6892                 error_at (loc, "wrong number of arguments to "
6893                                "%<__builtin_addressof%>");
6894                 postfix_expression = error_mark_node;
6895               }
6896             break;
6897
6898           case RID_BUILTIN_LAUNDER:
6899             if (vec->length () == 1)
6900               postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6901                                                            tf_warning_or_error);
6902             else
6903               {
6904                 error_at (loc, "wrong number of arguments to "
6905                                "%<__builtin_launder%>");
6906                 postfix_expression = error_mark_node;
6907               }
6908             break;
6909
6910           case RID_BUILTIN_SHUFFLE:
6911             if (vec->length () == 2)
6912               postfix_expression
6913                 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6914                                          (*vec)[1], tf_warning_or_error);
6915             else if (vec->length () == 3)
6916               postfix_expression
6917                 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6918                                          (*vec)[2], tf_warning_or_error);
6919             else
6920               {
6921                 error_at (loc, "wrong number of arguments to "
6922                                "%<__builtin_shuffle%>");
6923                 postfix_expression = error_mark_node;
6924               }
6925             break;
6926
6927           default:
6928             gcc_unreachable ();
6929           }
6930         break;
6931       }
6932
6933     default:
6934       {
6935         tree type;
6936
6937         /* If the next thing is a simple-type-specifier, we may be
6938            looking at a functional cast.  We could also be looking at
6939            an id-expression.  So, we try the functional cast, and if
6940            that doesn't work we fall back to the primary-expression.  */
6941         cp_parser_parse_tentatively (parser);
6942         /* Look for the simple-type-specifier.  */
6943         ++parser->prevent_constrained_type_specifiers;
6944         type = cp_parser_simple_type_specifier (parser,
6945                                                 /*decl_specs=*/NULL,
6946                                                 CP_PARSER_FLAGS_NONE);
6947         --parser->prevent_constrained_type_specifiers;
6948         /* Parse the cast itself.  */
6949         if (!cp_parser_error_occurred (parser))
6950           postfix_expression
6951             = cp_parser_functional_cast (parser, type);
6952         /* If that worked, we're done.  */
6953         if (cp_parser_parse_definitely (parser))
6954           break;
6955
6956         /* If the functional-cast didn't work out, try a
6957            compound-literal.  */
6958         if (cp_parser_allow_gnu_extensions_p (parser)
6959             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6960           {
6961             cp_expr initializer = NULL_TREE;
6962
6963             cp_parser_parse_tentatively (parser);
6964
6965             matching_parens parens;
6966             parens.consume_open (parser);
6967
6968             /* Avoid calling cp_parser_type_id pointlessly, see comment
6969                in cp_parser_cast_expression about c++/29234.  */
6970             if (!cp_parser_compound_literal_p (parser))
6971               cp_parser_simulate_error (parser);
6972             else
6973               {
6974                 /* Parse the type.  */
6975                 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6976                 parser->in_type_id_in_expr_p = true;
6977                 type = cp_parser_type_id (parser);
6978                 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6979                 parens.require_close (parser);
6980               }
6981
6982             /* If things aren't going well, there's no need to
6983                keep going.  */
6984             if (!cp_parser_error_occurred (parser))
6985               {
6986                 bool non_constant_p;
6987                 /* Parse the brace-enclosed initializer list.  */
6988                 initializer = cp_parser_braced_list (parser,
6989                                                      &non_constant_p);
6990               }
6991             /* If that worked, we're definitely looking at a
6992                compound-literal expression.  */
6993             if (cp_parser_parse_definitely (parser))
6994               {
6995                 /* Warn the user that a compound literal is not
6996                    allowed in standard C++.  */
6997                 pedwarn (input_location, OPT_Wpedantic,
6998                          "ISO C++ forbids compound-literals");
6999                 /* For simplicity, we disallow compound literals in
7000                    constant-expressions.  We could
7001                    allow compound literals of integer type, whose
7002                    initializer was a constant, in constant
7003                    expressions.  Permitting that usage, as a further
7004                    extension, would not change the meaning of any
7005                    currently accepted programs.  (Of course, as
7006                    compound literals are not part of ISO C++, the
7007                    standard has nothing to say.)  */
7008                 if (cp_parser_non_integral_constant_expression (parser,
7009                                                                 NIC_NCC))
7010                   {
7011                     postfix_expression = error_mark_node;
7012                     break;
7013                   }
7014                 /* Form the representation of the compound-literal.  */
7015                 postfix_expression
7016                   = finish_compound_literal (type, initializer,
7017                                              tf_warning_or_error, fcl_c99);
7018                 postfix_expression.set_location (initializer.get_location ());
7019                 break;
7020               }
7021           }
7022
7023         /* It must be a primary-expression.  */
7024         postfix_expression
7025           = cp_parser_primary_expression (parser, address_p, cast_p,
7026                                           /*template_arg_p=*/false,
7027                                           decltype_p,
7028                                           &idk);
7029       }
7030       break;
7031     }
7032
7033   /* Note that we don't need to worry about calling build_cplus_new on a
7034      class-valued CALL_EXPR in decltype when it isn't the end of the
7035      postfix-expression; unary_complex_lvalue will take care of that for
7036      all these cases.  */
7037
7038   /* Keep looping until the postfix-expression is complete.  */
7039   while (true)
7040     {
7041       if (idk == CP_ID_KIND_UNQUALIFIED
7042           && identifier_p (postfix_expression)
7043           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7044         /* It is not a Koenig lookup function call.  */
7045         postfix_expression
7046           = unqualified_name_lookup_error (postfix_expression);
7047
7048       /* Peek at the next token.  */
7049       token = cp_lexer_peek_token (parser->lexer);
7050
7051       switch (token->type)
7052         {
7053         case CPP_OPEN_SQUARE:
7054           if (cp_next_tokens_can_be_std_attribute_p (parser))
7055             {
7056               cp_parser_error (parser,
7057                                "two consecutive %<[%> shall "
7058                                "only introduce an attribute");
7059               return error_mark_node;
7060             }
7061           postfix_expression
7062             = cp_parser_postfix_open_square_expression (parser,
7063                                                         postfix_expression,
7064                                                         false,
7065                                                         decltype_p);
7066           postfix_expression.set_range (start_loc,
7067                                         postfix_expression.get_location ());
7068
7069           idk = CP_ID_KIND_NONE;
7070           is_member_access = false;
7071           break;
7072
7073         case CPP_OPEN_PAREN:
7074           /* postfix-expression ( expression-list [opt] ) */
7075           {
7076             bool koenig_p;
7077             bool is_builtin_constant_p;
7078             bool saved_integral_constant_expression_p = false;
7079             bool saved_non_integral_constant_expression_p = false;
7080             tsubst_flags_t complain = complain_flags (decltype_p);
7081             vec<tree, va_gc> *args;
7082             location_t close_paren_loc = UNKNOWN_LOCATION;
7083
7084             is_member_access = false;
7085
7086             is_builtin_constant_p
7087               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7088             if (is_builtin_constant_p)
7089               {
7090                 /* The whole point of __builtin_constant_p is to allow
7091                    non-constant expressions to appear as arguments.  */
7092                 saved_integral_constant_expression_p
7093                   = parser->integral_constant_expression_p;
7094                 saved_non_integral_constant_expression_p
7095                   = parser->non_integral_constant_expression_p;
7096                 parser->integral_constant_expression_p = false;
7097               }
7098             args = (cp_parser_parenthesized_expression_list
7099                     (parser, non_attr,
7100                      /*cast_p=*/false, /*allow_expansion_p=*/true,
7101                      /*non_constant_p=*/NULL,
7102                      /*close_paren_loc=*/&close_paren_loc,
7103                      /*wrap_locations_p=*/true));
7104             if (is_builtin_constant_p)
7105               {
7106                 parser->integral_constant_expression_p
7107                   = saved_integral_constant_expression_p;
7108                 parser->non_integral_constant_expression_p
7109                   = saved_non_integral_constant_expression_p;
7110               }
7111
7112             if (args == NULL)
7113               {
7114                 postfix_expression = error_mark_node;
7115                 break;
7116               }
7117
7118             /* Function calls are not permitted in
7119                constant-expressions.  */
7120             if (! builtin_valid_in_constant_expr_p (postfix_expression)
7121                 && cp_parser_non_integral_constant_expression (parser,
7122                                                                NIC_FUNC_CALL))
7123               {
7124                 postfix_expression = error_mark_node;
7125                 release_tree_vector (args);
7126                 break;
7127               }
7128
7129             koenig_p = false;
7130             if (idk == CP_ID_KIND_UNQUALIFIED
7131                 || idk == CP_ID_KIND_TEMPLATE_ID)
7132               {
7133                 if (identifier_p (postfix_expression))
7134                   {
7135                     if (!args->is_empty ())
7136                       {
7137                         koenig_p = true;
7138                         if (!any_type_dependent_arguments_p (args))
7139                           postfix_expression
7140                             = perform_koenig_lookup (postfix_expression, args,
7141                                                      complain);
7142                       }
7143                     else
7144                       postfix_expression
7145                         = unqualified_fn_lookup_error (postfix_expression);
7146                   }
7147                 /* We do not perform argument-dependent lookup if
7148                    normal lookup finds a non-function, in accordance
7149                    with the expected resolution of DR 218.  */
7150                 else if (!args->is_empty ()
7151                          && is_overloaded_fn (postfix_expression))
7152                   {
7153                     /* We only need to look at the first function,
7154                        because all the fns share the attribute we're
7155                        concerned with (all member fns or all local
7156                        fns).  */
7157                     tree fn = get_first_fn (postfix_expression);
7158                     fn = STRIP_TEMPLATE (fn);
7159
7160                     /* Do not do argument dependent lookup if regular
7161                        lookup finds a member function or a block-scope
7162                        function declaration.  [basic.lookup.argdep]/3  */
7163                     if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7164                           || DECL_FUNCTION_MEMBER_P (fn)
7165                           || DECL_LOCAL_FUNCTION_P (fn)))
7166                       {
7167                         koenig_p = true;
7168                         if (!any_type_dependent_arguments_p (args))
7169                           postfix_expression
7170                             = perform_koenig_lookup (postfix_expression, args,
7171                                                      complain);
7172                       }
7173                   }
7174               }
7175
7176             if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7177                 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7178                 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7179                 && vec_safe_length (args) == 3)
7180               {
7181                 tree arg0 = (*args)[0];
7182                 tree arg1 = (*args)[1];
7183                 tree arg2 = (*args)[2];
7184                 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7185                                     | (literal_integer_zerop (arg2) << 2));
7186                 warn_for_memset (input_location, arg0, arg2, literal_mask);
7187               }
7188
7189             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7190               {
7191                 tree instance = TREE_OPERAND (postfix_expression, 0);
7192                 tree fn = TREE_OPERAND (postfix_expression, 1);
7193
7194                 if (processing_template_decl
7195                     && (type_dependent_object_expression_p (instance)
7196                         || (!BASELINK_P (fn)
7197                             && TREE_CODE (fn) != FIELD_DECL)
7198                         || type_dependent_expression_p (fn)
7199                         || any_type_dependent_arguments_p (args)))
7200                   {
7201                     maybe_generic_this_capture (instance, fn);
7202                     postfix_expression
7203                       = build_min_nt_call_vec (postfix_expression, args);
7204                     release_tree_vector (args);
7205                     break;
7206                   }
7207
7208                 if (BASELINK_P (fn))
7209                   {
7210                   postfix_expression
7211                     = (build_new_method_call
7212                        (instance, fn, &args, NULL_TREE,
7213                         (idk == CP_ID_KIND_QUALIFIED
7214                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7215                          : LOOKUP_NORMAL),
7216                         /*fn_p=*/NULL,
7217                         complain));
7218                   }
7219                 else
7220                   postfix_expression
7221                     = finish_call_expr (postfix_expression, &args,
7222                                         /*disallow_virtual=*/false,
7223                                         /*koenig_p=*/false,
7224                                         complain);
7225               }
7226             else if (TREE_CODE (postfix_expression) == OFFSET_REF
7227                      || TREE_CODE (postfix_expression) == MEMBER_REF
7228                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7229               postfix_expression = (build_offset_ref_call_from_tree
7230                                     (postfix_expression, &args,
7231                                      complain));
7232             else if (idk == CP_ID_KIND_QUALIFIED)
7233               /* A call to a static class member, or a namespace-scope
7234                  function.  */
7235               postfix_expression
7236                 = finish_call_expr (postfix_expression, &args,
7237                                     /*disallow_virtual=*/true,
7238                                     koenig_p,
7239                                     complain);
7240             else
7241               /* All other function calls.  */
7242               postfix_expression
7243                 = finish_call_expr (postfix_expression, &args,
7244                                     /*disallow_virtual=*/false,
7245                                     koenig_p,
7246                                     complain);
7247
7248             if (close_paren_loc != UNKNOWN_LOCATION)
7249               {
7250                 location_t combined_loc = make_location (token->location,
7251                                                          start_loc,
7252                                                          close_paren_loc);
7253                 postfix_expression.set_location (combined_loc);
7254               }
7255
7256             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
7257             idk = CP_ID_KIND_NONE;
7258
7259             release_tree_vector (args);
7260           }
7261           break;
7262
7263         case CPP_DOT:
7264         case CPP_DEREF:
7265           /* postfix-expression . template [opt] id-expression
7266              postfix-expression . pseudo-destructor-name
7267              postfix-expression -> template [opt] id-expression
7268              postfix-expression -> pseudo-destructor-name */
7269
7270           /* Consume the `.' or `->' operator.  */
7271           cp_lexer_consume_token (parser->lexer);
7272
7273           postfix_expression
7274             = cp_parser_postfix_dot_deref_expression (parser, token->type,
7275                                                       postfix_expression,
7276                                                       false, &idk, loc);
7277
7278           is_member_access = true;
7279           break;
7280
7281         case CPP_PLUS_PLUS:
7282           /* postfix-expression ++  */
7283           /* Consume the `++' token.  */
7284           cp_lexer_consume_token (parser->lexer);
7285           /* Generate a representation for the complete expression.  */
7286           postfix_expression
7287             = finish_increment_expr (postfix_expression,
7288                                      POSTINCREMENT_EXPR);
7289           /* Increments may not appear in constant-expressions.  */
7290           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7291             postfix_expression = error_mark_node;
7292           idk = CP_ID_KIND_NONE;
7293           is_member_access = false;
7294           break;
7295
7296         case CPP_MINUS_MINUS:
7297           /* postfix-expression -- */
7298           /* Consume the `--' token.  */
7299           cp_lexer_consume_token (parser->lexer);
7300           /* Generate a representation for the complete expression.  */
7301           postfix_expression
7302             = finish_increment_expr (postfix_expression,
7303                                      POSTDECREMENT_EXPR);
7304           /* Decrements may not appear in constant-expressions.  */
7305           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7306             postfix_expression = error_mark_node;
7307           idk = CP_ID_KIND_NONE;
7308           is_member_access = false;
7309           break;
7310
7311         default:
7312           if (pidk_return != NULL)
7313             * pidk_return = idk;
7314           if (member_access_only_p)
7315             return is_member_access
7316               ? postfix_expression
7317               : cp_expr (error_mark_node);
7318           else
7319             return postfix_expression;
7320         }
7321     }
7322
7323   /* We should never get here.  */
7324   gcc_unreachable ();
7325   return error_mark_node;
7326 }
7327
7328 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7329    by cp_parser_builtin_offsetof.  We're looking for
7330
7331      postfix-expression [ expression ]
7332      postfix-expression [ braced-init-list ] (C++11)
7333
7334    FOR_OFFSETOF is set if we're being called in that context, which
7335    changes how we deal with integer constant expressions.  */
7336
7337 static tree
7338 cp_parser_postfix_open_square_expression (cp_parser *parser,
7339                                           tree postfix_expression,
7340                                           bool for_offsetof,
7341                                           bool decltype_p)
7342 {
7343   tree index = NULL_TREE;
7344   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7345   bool saved_greater_than_is_operator_p;
7346
7347   /* Consume the `[' token.  */
7348   cp_lexer_consume_token (parser->lexer);
7349
7350   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7351   parser->greater_than_is_operator_p = true;
7352
7353   /* Parse the index expression.  */
7354   /* ??? For offsetof, there is a question of what to allow here.  If
7355      offsetof is not being used in an integral constant expression context,
7356      then we *could* get the right answer by computing the value at runtime.
7357      If we are in an integral constant expression context, then we might
7358      could accept any constant expression; hard to say without analysis.
7359      Rather than open the barn door too wide right away, allow only integer
7360      constant expressions here.  */
7361   if (for_offsetof)
7362     index = cp_parser_constant_expression (parser);
7363   else
7364     {
7365       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7366         {
7367           bool expr_nonconst_p;
7368           cp_lexer_set_source_position (parser->lexer);
7369           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7370           index = cp_parser_braced_list (parser, &expr_nonconst_p);
7371         }
7372       else
7373         index = cp_parser_expression (parser);
7374     }
7375
7376   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7377
7378   /* Look for the closing `]'.  */
7379   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7380
7381   /* Build the ARRAY_REF.  */
7382   postfix_expression = grok_array_decl (loc, postfix_expression,
7383                                         index, decltype_p);
7384
7385   /* When not doing offsetof, array references are not permitted in
7386      constant-expressions.  */
7387   if (!for_offsetof
7388       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7389     postfix_expression = error_mark_node;
7390
7391   return postfix_expression;
7392 }
7393
7394 /* A subroutine of cp_parser_postfix_dot_deref_expression.  Handle dot
7395    dereference of incomplete type, returns true if error_mark_node should
7396    be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7397    and *DEPENDENT_P.  */
7398
7399 bool
7400 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7401                                 bool *dependent_p)
7402 {
7403   /* In a template, be permissive by treating an object expression
7404      of incomplete type as dependent (after a pedwarn).  */
7405   diagnostic_t kind = (processing_template_decl
7406                        && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7407
7408   switch (TREE_CODE (*postfix_expression))
7409     {
7410     case CAST_EXPR:
7411     case REINTERPRET_CAST_EXPR:
7412     case CONST_CAST_EXPR:
7413     case STATIC_CAST_EXPR:
7414     case DYNAMIC_CAST_EXPR:
7415     case IMPLICIT_CONV_EXPR:
7416     case VIEW_CONVERT_EXPR:
7417     case NON_LVALUE_EXPR:
7418       kind = DK_ERROR;
7419       break;
7420     case OVERLOAD:
7421       /* Don't emit any diagnostic for OVERLOADs.  */
7422       kind = DK_IGNORED;
7423       break;
7424     default:
7425       /* Avoid clobbering e.g. DECLs.  */
7426       if (!EXPR_P (*postfix_expression))
7427         kind = DK_ERROR;
7428       break;
7429     }
7430
7431   if (kind == DK_IGNORED)
7432     return false;
7433
7434   location_t exploc = location_of (*postfix_expression);
7435   cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7436   if (!MAYBE_CLASS_TYPE_P (*scope))
7437     return true;
7438   if (kind == DK_ERROR)
7439     *scope = *postfix_expression = error_mark_node;
7440   else if (processing_template_decl)
7441     {
7442       *dependent_p = true;
7443       *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7444     }
7445   return false;
7446 }
7447
7448 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7449    by cp_parser_builtin_offsetof.  We're looking for
7450
7451      postfix-expression . template [opt] id-expression
7452      postfix-expression . pseudo-destructor-name
7453      postfix-expression -> template [opt] id-expression
7454      postfix-expression -> pseudo-destructor-name
7455
7456    FOR_OFFSETOF is set if we're being called in that context.  That sorta
7457    limits what of the above we'll actually accept, but nevermind.
7458    TOKEN_TYPE is the "." or "->" token, which will already have been
7459    removed from the stream.  */
7460
7461 static tree
7462 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7463                                         enum cpp_ttype token_type,
7464                                         cp_expr postfix_expression,
7465                                         bool for_offsetof, cp_id_kind *idk,
7466                                         location_t location)
7467 {
7468   tree name;
7469   bool dependent_p;
7470   bool pseudo_destructor_p;
7471   tree scope = NULL_TREE;
7472   location_t start_loc = postfix_expression.get_start ();
7473
7474   /* If this is a `->' operator, dereference the pointer.  */
7475   if (token_type == CPP_DEREF)
7476     postfix_expression = build_x_arrow (location, postfix_expression,
7477                                         tf_warning_or_error);
7478   /* Check to see whether or not the expression is type-dependent and
7479      not the current instantiation.  */
7480   dependent_p = type_dependent_object_expression_p (postfix_expression);
7481   /* The identifier following the `->' or `.' is not qualified.  */
7482   parser->scope = NULL_TREE;
7483   parser->qualifying_scope = NULL_TREE;
7484   parser->object_scope = NULL_TREE;
7485   *idk = CP_ID_KIND_NONE;
7486
7487   /* Enter the scope corresponding to the type of the object
7488      given by the POSTFIX_EXPRESSION.  */
7489   if (!dependent_p)
7490     {
7491       scope = TREE_TYPE (postfix_expression);
7492       /* According to the standard, no expression should ever have
7493          reference type.  Unfortunately, we do not currently match
7494          the standard in this respect in that our internal representation
7495          of an expression may have reference type even when the standard
7496          says it does not.  Therefore, we have to manually obtain the
7497          underlying type here.  */
7498       scope = non_reference (scope);
7499       /* The type of the POSTFIX_EXPRESSION must be complete.  */
7500       /* Unlike the object expression in other contexts, *this is not
7501          required to be of complete type for purposes of class member
7502          access (5.2.5) outside the member function body.  */
7503       if (postfix_expression != current_class_ref
7504           && scope != error_mark_node
7505           && !currently_open_class (scope))
7506         {
7507           scope = complete_type (scope);
7508           if (!COMPLETE_TYPE_P (scope)
7509               && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7510                                                  &dependent_p))
7511             return error_mark_node;
7512         }
7513
7514       if (!dependent_p)
7515         {
7516           /* Let the name lookup machinery know that we are processing a
7517              class member access expression.  */
7518           parser->context->object_type = scope;
7519           /* If something went wrong, we want to be able to discern that case,
7520              as opposed to the case where there was no SCOPE due to the type
7521              of expression being dependent.  */
7522           if (!scope)
7523             scope = error_mark_node;
7524           /* If the SCOPE was erroneous, make the various semantic analysis
7525              functions exit quickly -- and without issuing additional error
7526              messages.  */
7527           if (scope == error_mark_node)
7528             postfix_expression = error_mark_node;
7529         }
7530     }
7531
7532   if (dependent_p)
7533     /* Tell cp_parser_lookup_name that there was an object, even though it's
7534        type-dependent.  */
7535     parser->context->object_type = unknown_type_node;
7536
7537   /* Assume this expression is not a pseudo-destructor access.  */
7538   pseudo_destructor_p = false;
7539
7540   /* If the SCOPE is a scalar type, then, if this is a valid program,
7541      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
7542      is type dependent, it can be pseudo-destructor-name or something else.
7543      Try to parse it as pseudo-destructor-name first.  */
7544   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7545     {
7546       tree s;
7547       tree type;
7548
7549       cp_parser_parse_tentatively (parser);
7550       /* Parse the pseudo-destructor-name.  */
7551       s = NULL_TREE;
7552       cp_parser_pseudo_destructor_name (parser, postfix_expression,
7553                                         &s, &type);
7554       if (dependent_p
7555           && (cp_parser_error_occurred (parser)
7556               || !SCALAR_TYPE_P (type)))
7557         cp_parser_abort_tentative_parse (parser);
7558       else if (cp_parser_parse_definitely (parser))
7559         {
7560           pseudo_destructor_p = true;
7561           postfix_expression
7562             = finish_pseudo_destructor_expr (postfix_expression,
7563                                              s, type, location);
7564         }
7565     }
7566
7567   if (!pseudo_destructor_p)
7568     {
7569       /* If the SCOPE is not a scalar type, we are looking at an
7570          ordinary class member access expression, rather than a
7571          pseudo-destructor-name.  */
7572       bool template_p;
7573       cp_token *token = cp_lexer_peek_token (parser->lexer);
7574       /* Parse the id-expression.  */
7575       name = (cp_parser_id_expression
7576               (parser,
7577                cp_parser_optional_template_keyword (parser),
7578                /*check_dependency_p=*/true,
7579                &template_p,
7580                /*declarator_p=*/false,
7581                /*optional_p=*/false));
7582       /* In general, build a SCOPE_REF if the member name is qualified.
7583          However, if the name was not dependent and has already been
7584          resolved; there is no need to build the SCOPE_REF.  For example;
7585
7586              struct X { void f(); };
7587              template <typename T> void f(T* t) { t->X::f(); }
7588
7589          Even though "t" is dependent, "X::f" is not and has been resolved
7590          to a BASELINK; there is no need to include scope information.  */
7591
7592       /* But we do need to remember that there was an explicit scope for
7593          virtual function calls.  */
7594       if (parser->scope)
7595         *idk = CP_ID_KIND_QUALIFIED;
7596
7597       /* If the name is a template-id that names a type, we will get a
7598          TYPE_DECL here.  That is invalid code.  */
7599       if (TREE_CODE (name) == TYPE_DECL)
7600         {
7601           error_at (token->location, "invalid use of %qD", name);
7602           postfix_expression = error_mark_node;
7603         }
7604       else
7605         {
7606           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7607             {
7608               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7609                 {
7610                   error_at (token->location, "%<%D::%D%> is not a class member",
7611                             parser->scope, name);
7612                   postfix_expression = error_mark_node;
7613                 }
7614               else
7615                 name = build_qualified_name (/*type=*/NULL_TREE,
7616                                              parser->scope,
7617                                              name,
7618                                              template_p);
7619               parser->scope = NULL_TREE;
7620               parser->qualifying_scope = NULL_TREE;
7621               parser->object_scope = NULL_TREE;
7622             }
7623           if (parser->scope && name && BASELINK_P (name))
7624             adjust_result_of_qualified_name_lookup
7625               (name, parser->scope, scope);
7626           postfix_expression
7627             = finish_class_member_access_expr (postfix_expression, name,
7628                                                template_p, 
7629                                                tf_warning_or_error);
7630           /* Build a location e.g.:
7631                ptr->access_expr
7632                ~~~^~~~~~~~~~~~~
7633              where the caret is at the deref token, ranging from
7634              the start of postfix_expression to the end of the access expr.  */
7635           location_t end_loc
7636             = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7637           location_t combined_loc
7638             = make_location (input_location, start_loc, end_loc);
7639           protected_set_expr_location (postfix_expression, combined_loc);
7640         }
7641     }
7642
7643   /* We no longer need to look up names in the scope of the object on
7644      the left-hand side of the `.' or `->' operator.  */
7645   parser->context->object_type = NULL_TREE;
7646
7647   /* Outside of offsetof, these operators may not appear in
7648      constant-expressions.  */
7649   if (!for_offsetof
7650       && (cp_parser_non_integral_constant_expression
7651           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7652     postfix_expression = error_mark_node;
7653
7654   return postfix_expression;
7655 }
7656
7657 /* Parse a parenthesized expression-list.
7658
7659    expression-list:
7660      assignment-expression
7661      expression-list, assignment-expression
7662
7663    attribute-list:
7664      expression-list
7665      identifier
7666      identifier, expression-list
7667
7668    CAST_P is true if this expression is the target of a cast.
7669
7670    ALLOW_EXPANSION_P is true if this expression allows expansion of an
7671    argument pack.
7672
7673    WRAP_LOCATIONS_P is true if expressions within this list for which
7674    CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7675    their source locations.
7676
7677    Returns a vector of trees.  Each element is a representation of an
7678    assignment-expression.  NULL is returned if the ( and or ) are
7679    missing.  An empty, but allocated, vector is returned on no
7680    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
7681    if we are parsing an attribute list for an attribute that wants a
7682    plain identifier argument, normal_attr for an attribute that wants
7683    an expression, or non_attr if we aren't parsing an attribute list.  If
7684    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7685    not all of the expressions in the list were constant.
7686    If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7687    will be written to with the location of the closing parenthesis.  If
7688    an error occurs, it may or may not be written to.  */
7689
7690 static vec<tree, va_gc> *
7691 cp_parser_parenthesized_expression_list (cp_parser* parser,
7692                                          int is_attribute_list,
7693                                          bool cast_p,
7694                                          bool allow_expansion_p,
7695                                          bool *non_constant_p,
7696                                          location_t *close_paren_loc,
7697                                          bool wrap_locations_p)
7698 {
7699   vec<tree, va_gc> *expression_list;
7700   bool fold_expr_p = is_attribute_list != non_attr;
7701   tree identifier = NULL_TREE;
7702   bool saved_greater_than_is_operator_p;
7703
7704   /* Assume all the expressions will be constant.  */
7705   if (non_constant_p)
7706     *non_constant_p = false;
7707
7708   matching_parens parens;
7709   if (!parens.require_open (parser))
7710     return NULL;
7711
7712   expression_list = make_tree_vector ();
7713
7714   /* Within a parenthesized expression, a `>' token is always
7715      the greater-than operator.  */
7716   saved_greater_than_is_operator_p
7717     = parser->greater_than_is_operator_p;
7718   parser->greater_than_is_operator_p = true;
7719
7720   cp_expr expr (NULL_TREE);
7721
7722   /* Consume expressions until there are no more.  */
7723   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7724     while (true)
7725       {
7726         /* At the beginning of attribute lists, check to see if the
7727            next token is an identifier.  */
7728         if (is_attribute_list == id_attr
7729             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7730           {
7731             cp_token *token;
7732
7733             /* Consume the identifier.  */
7734             token = cp_lexer_consume_token (parser->lexer);
7735             /* Save the identifier.  */
7736             identifier = token->u.value;
7737           }
7738         else
7739           {
7740             bool expr_non_constant_p;
7741
7742             /* Parse the next assignment-expression.  */
7743             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7744               {
7745                 /* A braced-init-list.  */
7746                 cp_lexer_set_source_position (parser->lexer);
7747                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7748                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7749                 if (non_constant_p && expr_non_constant_p)
7750                   *non_constant_p = true;
7751               }
7752             else if (non_constant_p)
7753               {
7754                 expr = (cp_parser_constant_expression
7755                         (parser, /*allow_non_constant_p=*/true,
7756                          &expr_non_constant_p));
7757                 if (expr_non_constant_p)
7758                   *non_constant_p = true;
7759               }
7760             else
7761               expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7762                                                       cast_p);
7763
7764             if (fold_expr_p)
7765               expr = instantiate_non_dependent_expr (expr);
7766
7767             /* If we have an ellipsis, then this is an expression
7768                expansion.  */
7769             if (allow_expansion_p
7770                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7771               {
7772                 /* Consume the `...'.  */
7773                 cp_lexer_consume_token (parser->lexer);
7774
7775                 /* Build the argument pack.  */
7776                 expr = make_pack_expansion (expr);
7777               }
7778
7779             if (wrap_locations_p)
7780               expr.maybe_add_location_wrapper ();
7781
7782              /* Add it to the list.  We add error_mark_node
7783                 expressions to the list, so that we can still tell if
7784                 the correct form for a parenthesized expression-list
7785                 is found. That gives better errors.  */
7786             vec_safe_push (expression_list, expr.get_value ());
7787
7788             if (expr == error_mark_node)
7789               goto skip_comma;
7790           }
7791
7792         /* After the first item, attribute lists look the same as
7793            expression lists.  */
7794         is_attribute_list = non_attr;
7795
7796       get_comma:;
7797         /* If the next token isn't a `,', then we are done.  */
7798         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7799           break;
7800
7801         /* Otherwise, consume the `,' and keep going.  */
7802         cp_lexer_consume_token (parser->lexer);
7803       }
7804
7805   if (close_paren_loc)
7806     *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7807
7808   if (!parens.require_close (parser))
7809     {
7810       int ending;
7811
7812     skip_comma:;
7813       /* We try and resync to an unnested comma, as that will give the
7814          user better diagnostics.  */
7815       ending = cp_parser_skip_to_closing_parenthesis (parser,
7816                                                       /*recovering=*/true,
7817                                                       /*or_comma=*/true,
7818                                                       /*consume_paren=*/true);
7819       if (ending < 0)
7820         goto get_comma;
7821       if (!ending)
7822         {
7823           parser->greater_than_is_operator_p
7824             = saved_greater_than_is_operator_p;
7825           return NULL;
7826         }
7827     }
7828
7829   parser->greater_than_is_operator_p
7830     = saved_greater_than_is_operator_p;
7831
7832   if (identifier)
7833     vec_safe_insert (expression_list, 0, identifier);
7834
7835   return expression_list;
7836 }
7837
7838 /* Parse a pseudo-destructor-name.
7839
7840    pseudo-destructor-name:
7841      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7842      :: [opt] nested-name-specifier template template-id :: ~ type-name
7843      :: [opt] nested-name-specifier [opt] ~ type-name
7844
7845    If either of the first two productions is used, sets *SCOPE to the
7846    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7847    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7848    or ERROR_MARK_NODE if the parse fails.  */
7849
7850 static void
7851 cp_parser_pseudo_destructor_name (cp_parser* parser,
7852                                   tree object,
7853                                   tree* scope,
7854                                   tree* type)
7855 {
7856   bool nested_name_specifier_p;
7857
7858   /* Handle ~auto.  */
7859   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7860       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7861       && !type_dependent_expression_p (object))
7862     {
7863       if (cxx_dialect < cxx14)
7864         pedwarn (input_location, 0,
7865                  "%<~auto%> only available with "
7866                  "-std=c++14 or -std=gnu++14");
7867       cp_lexer_consume_token (parser->lexer);
7868       cp_lexer_consume_token (parser->lexer);
7869       *scope = NULL_TREE;
7870       *type = TREE_TYPE (object);
7871       return;
7872     }
7873
7874   /* Assume that things will not work out.  */
7875   *type = error_mark_node;
7876
7877   /* Look for the optional `::' operator.  */
7878   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7879   /* Look for the optional nested-name-specifier.  */
7880   nested_name_specifier_p
7881     = (cp_parser_nested_name_specifier_opt (parser,
7882                                             /*typename_keyword_p=*/false,
7883                                             /*check_dependency_p=*/true,
7884                                             /*type_p=*/false,
7885                                             /*is_declaration=*/false)
7886        != NULL_TREE);
7887   /* Now, if we saw a nested-name-specifier, we might be doing the
7888      second production.  */
7889   if (nested_name_specifier_p
7890       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7891     {
7892       /* Consume the `template' keyword.  */
7893       cp_lexer_consume_token (parser->lexer);
7894       /* Parse the template-id.  */
7895       cp_parser_template_id (parser,
7896                              /*template_keyword_p=*/true,
7897                              /*check_dependency_p=*/false,
7898                              class_type,
7899                              /*is_declaration=*/true);
7900       /* Look for the `::' token.  */
7901       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7902     }
7903   /* If the next token is not a `~', then there might be some
7904      additional qualification.  */
7905   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7906     {
7907       /* At this point, we're looking for "type-name :: ~".  The type-name
7908          must not be a class-name, since this is a pseudo-destructor.  So,
7909          it must be either an enum-name, or a typedef-name -- both of which
7910          are just identifiers.  So, we peek ahead to check that the "::"
7911          and "~" tokens are present; if they are not, then we can avoid
7912          calling type_name.  */
7913       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7914           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7915           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7916         {
7917           cp_parser_error (parser, "non-scalar type");
7918           return;
7919         }
7920
7921       /* Look for the type-name.  */
7922       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7923       if (*scope == error_mark_node)
7924         return;
7925
7926       /* Look for the `::' token.  */
7927       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7928     }
7929   else
7930     *scope = NULL_TREE;
7931
7932   /* Look for the `~'.  */
7933   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7934
7935   /* Once we see the ~, this has to be a pseudo-destructor.  */
7936   if (!processing_template_decl && !cp_parser_error_occurred (parser))
7937     cp_parser_commit_to_topmost_tentative_parse (parser);
7938
7939   /* Look for the type-name again.  We are not responsible for
7940      checking that it matches the first type-name.  */
7941   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7942 }
7943
7944 /* Parse a unary-expression.
7945
7946    unary-expression:
7947      postfix-expression
7948      ++ cast-expression
7949      -- cast-expression
7950      unary-operator cast-expression
7951      sizeof unary-expression
7952      sizeof ( type-id )
7953      alignof ( type-id )  [C++0x]
7954      new-expression
7955      delete-expression
7956
7957    GNU Extensions:
7958
7959    unary-expression:
7960      __extension__ cast-expression
7961      __alignof__ unary-expression
7962      __alignof__ ( type-id )
7963      alignof unary-expression  [C++0x]
7964      __real__ cast-expression
7965      __imag__ cast-expression
7966      && identifier
7967      sizeof ( type-id ) { initializer-list , [opt] }
7968      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7969      __alignof__ ( type-id ) { initializer-list , [opt] }
7970
7971    ADDRESS_P is true iff the unary-expression is appearing as the
7972    operand of the `&' operator.   CAST_P is true if this expression is
7973    the target of a cast.
7974
7975    Returns a representation of the expression.  */
7976
7977 static cp_expr
7978 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7979                             bool address_p, bool cast_p, bool decltype_p)
7980 {
7981   cp_token *token;
7982   enum tree_code unary_operator;
7983
7984   /* Peek at the next token.  */
7985   token = cp_lexer_peek_token (parser->lexer);
7986   /* Some keywords give away the kind of expression.  */
7987   if (token->type == CPP_KEYWORD)
7988     {
7989       enum rid keyword = token->keyword;
7990
7991       switch (keyword)
7992         {
7993         case RID_ALIGNOF:
7994         case RID_SIZEOF:
7995           {
7996             tree operand, ret;
7997             enum tree_code op;
7998             location_t start_loc = token->location;
7999
8000             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8001             bool std_alignof = id_equal (token->u.value, "alignof");
8002
8003             /* Consume the token.  */
8004             cp_lexer_consume_token (parser->lexer);
8005             /* Parse the operand.  */
8006             operand = cp_parser_sizeof_operand (parser, keyword);
8007
8008             if (TYPE_P (operand))
8009               ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8010                                                 true);
8011             else
8012               {
8013                 /* ISO C++ defines alignof only with types, not with
8014                    expressions. So pedwarn if alignof is used with a non-
8015                    type expression. However, __alignof__ is ok.  */
8016                 if (std_alignof)
8017                   pedwarn (token->location, OPT_Wpedantic,
8018                            "ISO C++ does not allow %<alignof%> "
8019                            "with a non-type");
8020
8021                 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8022               }
8023             /* For SIZEOF_EXPR, just issue diagnostics, but keep
8024                SIZEOF_EXPR with the original operand.  */
8025             if (op == SIZEOF_EXPR && ret != error_mark_node)
8026               {
8027                 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8028                   {
8029                     if (!processing_template_decl && TYPE_P (operand))
8030                       {
8031                         ret = build_min (SIZEOF_EXPR, size_type_node,
8032                                          build1 (NOP_EXPR, operand,
8033                                                  error_mark_node));
8034                         SIZEOF_EXPR_TYPE_P (ret) = 1;
8035                       }
8036                     else
8037                       ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8038                     TREE_SIDE_EFFECTS (ret) = 0;
8039                     TREE_READONLY (ret) = 1;
8040                   }
8041               }
8042
8043             /* Construct a location e.g. :
8044                alignof (expr)
8045                ^~~~~~~~~~~~~~
8046                with start == caret at the start of the "alignof"/"sizeof"
8047                token, with the endpoint at the final closing paren.  */
8048             location_t finish_loc
8049               = cp_lexer_previous_token (parser->lexer)->location;
8050             location_t compound_loc
8051               = make_location (start_loc, start_loc, finish_loc);
8052
8053             cp_expr ret_expr (ret);
8054             ret_expr.set_location (compound_loc);
8055             ret_expr = ret_expr.maybe_add_location_wrapper ();
8056             return ret_expr;
8057           }
8058
8059         case RID_NEW:
8060           return cp_parser_new_expression (parser);
8061
8062         case RID_DELETE:
8063           return cp_parser_delete_expression (parser);
8064
8065         case RID_EXTENSION:
8066           {
8067             /* The saved value of the PEDANTIC flag.  */
8068             int saved_pedantic;
8069             tree expr;
8070
8071             /* Save away the PEDANTIC flag.  */
8072             cp_parser_extension_opt (parser, &saved_pedantic);
8073             /* Parse the cast-expression.  */
8074             expr = cp_parser_simple_cast_expression (parser);
8075             /* Restore the PEDANTIC flag.  */
8076             pedantic = saved_pedantic;
8077
8078             return expr;
8079           }
8080
8081         case RID_REALPART:
8082         case RID_IMAGPART:
8083           {
8084             tree expression;
8085
8086             /* Consume the `__real__' or `__imag__' token.  */
8087             cp_lexer_consume_token (parser->lexer);
8088             /* Parse the cast-expression.  */
8089             expression = cp_parser_simple_cast_expression (parser);
8090             /* Create the complete representation.  */
8091             return build_x_unary_op (token->location,
8092                                      (keyword == RID_REALPART
8093                                       ? REALPART_EXPR : IMAGPART_EXPR),
8094                                      expression,
8095                                      tf_warning_or_error);
8096           }
8097           break;
8098
8099         case RID_TRANSACTION_ATOMIC:
8100         case RID_TRANSACTION_RELAXED:
8101           return cp_parser_transaction_expression (parser, keyword);
8102
8103         case RID_NOEXCEPT:
8104           {
8105             tree expr;
8106             const char *saved_message;
8107             bool saved_integral_constant_expression_p;
8108             bool saved_non_integral_constant_expression_p;
8109             bool saved_greater_than_is_operator_p;
8110
8111             location_t start_loc = token->location;
8112
8113             cp_lexer_consume_token (parser->lexer);
8114             matching_parens parens;
8115             parens.require_open (parser);
8116
8117             saved_message = parser->type_definition_forbidden_message;
8118             parser->type_definition_forbidden_message
8119               = G_("types may not be defined in %<noexcept%> expressions");
8120
8121             saved_integral_constant_expression_p
8122               = parser->integral_constant_expression_p;
8123             saved_non_integral_constant_expression_p
8124               = parser->non_integral_constant_expression_p;
8125             parser->integral_constant_expression_p = false;
8126
8127             saved_greater_than_is_operator_p
8128               = parser->greater_than_is_operator_p;
8129             parser->greater_than_is_operator_p = true;
8130
8131             ++cp_unevaluated_operand;
8132             ++c_inhibit_evaluation_warnings;
8133             ++cp_noexcept_operand;
8134             expr = cp_parser_expression (parser);
8135             --cp_noexcept_operand;
8136             --c_inhibit_evaluation_warnings;
8137             --cp_unevaluated_operand;
8138
8139             parser->greater_than_is_operator_p
8140               = saved_greater_than_is_operator_p;
8141
8142             parser->integral_constant_expression_p
8143               = saved_integral_constant_expression_p;
8144             parser->non_integral_constant_expression_p
8145               = saved_non_integral_constant_expression_p;
8146
8147             parser->type_definition_forbidden_message = saved_message;
8148
8149             location_t finish_loc
8150               = cp_lexer_peek_token (parser->lexer)->location;
8151             parens.require_close (parser);
8152
8153             /* Construct a location of the form:
8154                noexcept (expr)
8155                ^~~~~~~~~~~~~~~
8156                with start == caret, finishing at the close-paren.  */
8157             location_t noexcept_loc
8158               = make_location (start_loc, start_loc, finish_loc);
8159
8160             return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8161                             noexcept_loc);
8162           }
8163
8164         default:
8165           break;
8166         }
8167     }
8168
8169   /* Look for the `:: new' and `:: delete', which also signal the
8170      beginning of a new-expression, or delete-expression,
8171      respectively.  If the next token is `::', then it might be one of
8172      these.  */
8173   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8174     {
8175       enum rid keyword;
8176
8177       /* See if the token after the `::' is one of the keywords in
8178          which we're interested.  */
8179       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8180       /* If it's `new', we have a new-expression.  */
8181       if (keyword == RID_NEW)
8182         return cp_parser_new_expression (parser);
8183       /* Similarly, for `delete'.  */
8184       else if (keyword == RID_DELETE)
8185         return cp_parser_delete_expression (parser);
8186     }
8187
8188   /* Look for a unary operator.  */
8189   unary_operator = cp_parser_unary_operator (token);
8190   /* The `++' and `--' operators can be handled similarly, even though
8191      they are not technically unary-operators in the grammar.  */
8192   if (unary_operator == ERROR_MARK)
8193     {
8194       if (token->type == CPP_PLUS_PLUS)
8195         unary_operator = PREINCREMENT_EXPR;
8196       else if (token->type == CPP_MINUS_MINUS)
8197         unary_operator = PREDECREMENT_EXPR;
8198       /* Handle the GNU address-of-label extension.  */
8199       else if (cp_parser_allow_gnu_extensions_p (parser)
8200                && token->type == CPP_AND_AND)
8201         {
8202           tree identifier;
8203           tree expression;
8204           location_t start_loc = token->location;
8205
8206           /* Consume the '&&' token.  */
8207           cp_lexer_consume_token (parser->lexer);
8208           /* Look for the identifier.  */
8209           location_t finish_loc
8210             = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8211           identifier = cp_parser_identifier (parser);
8212           /* Construct a location of the form:
8213                &&label
8214                ^~~~~~~
8215              with caret==start at the "&&", finish at the end of the label.  */
8216           location_t combined_loc
8217             = make_location (start_loc, start_loc, finish_loc);
8218           /* Create an expression representing the address.  */
8219           expression = finish_label_address_expr (identifier, combined_loc);
8220           if (cp_parser_non_integral_constant_expression (parser,
8221                                                           NIC_ADDR_LABEL))
8222             expression = error_mark_node;
8223           return expression;
8224         }
8225     }
8226   if (unary_operator != ERROR_MARK)
8227     {
8228       cp_expr cast_expression;
8229       cp_expr expression = error_mark_node;
8230       non_integral_constant non_constant_p = NIC_NONE;
8231       location_t loc = token->location;
8232       tsubst_flags_t complain = complain_flags (decltype_p);
8233
8234       /* Consume the operator token.  */
8235       token = cp_lexer_consume_token (parser->lexer);
8236       enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8237
8238       /* Parse the cast-expression.  */
8239       cast_expression
8240         = cp_parser_cast_expression (parser,
8241                                      unary_operator == ADDR_EXPR,
8242                                      /*cast_p=*/false,
8243                                      /*decltype*/false,
8244                                      pidk);
8245
8246       /* Make a location:
8247             OP_TOKEN  CAST_EXPRESSION
8248             ^~~~~~~~~~~~~~~~~~~~~~~~~
8249          with start==caret at the operator token, and
8250          extending to the end of the cast_expression.  */
8251       loc = make_location (loc, loc, cast_expression.get_finish ());
8252
8253       /* Now, build an appropriate representation.  */
8254       switch (unary_operator)
8255         {
8256         case INDIRECT_REF:
8257           non_constant_p = NIC_STAR;
8258           expression = build_x_indirect_ref (loc, cast_expression,
8259                                              RO_UNARY_STAR,
8260                                              complain);
8261           /* TODO: build_x_indirect_ref does not always honor the
8262              location, so ensure it is set.  */
8263           expression.set_location (loc);
8264           break;
8265
8266         case ADDR_EXPR:
8267            non_constant_p = NIC_ADDR;
8268           /* Fall through.  */
8269         case BIT_NOT_EXPR:
8270           expression = build_x_unary_op (loc, unary_operator,
8271                                          cast_expression,
8272                                          complain);
8273           /* TODO: build_x_unary_op does not always honor the location,
8274              so ensure it is set.  */
8275           expression.set_location (loc);
8276           break;
8277
8278         case PREINCREMENT_EXPR:
8279         case PREDECREMENT_EXPR:
8280           non_constant_p = unary_operator == PREINCREMENT_EXPR
8281                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8282           /* Fall through.  */
8283         case NEGATE_EXPR:
8284           /* Immediately fold negation of a constant, unless the constant is 0
8285              (since -0 == 0) or it would overflow.  */
8286           if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8287               && CONSTANT_CLASS_P (cast_expression)
8288               && !integer_zerop (cast_expression)
8289               && !TREE_OVERFLOW (cast_expression))
8290             {
8291               tree folded = fold_build1 (unary_operator,
8292                                          TREE_TYPE (cast_expression),
8293                                          cast_expression);
8294               if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8295                 {
8296                   expression = cp_expr (folded, loc);
8297                   break;
8298                 }
8299             }
8300           /* Fall through.  */
8301         case UNARY_PLUS_EXPR:
8302         case TRUTH_NOT_EXPR:
8303           expression = finish_unary_op_expr (loc, unary_operator,
8304                                              cast_expression, complain);
8305           break;
8306
8307         default:
8308           gcc_unreachable ();
8309         }
8310
8311       if (non_constant_p != NIC_NONE
8312           && cp_parser_non_integral_constant_expression (parser,
8313                                                          non_constant_p))
8314         expression = error_mark_node;
8315
8316       return expression;
8317     }
8318
8319   return cp_parser_postfix_expression (parser, address_p, cast_p,
8320                                        /*member_access_only_p=*/false,
8321                                        decltype_p,
8322                                        pidk);
8323 }
8324
8325 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
8326    unary-operator, the corresponding tree code is returned.  */
8327
8328 static enum tree_code
8329 cp_parser_unary_operator (cp_token* token)
8330 {
8331   switch (token->type)
8332     {
8333     case CPP_MULT:
8334       return INDIRECT_REF;
8335
8336     case CPP_AND:
8337       return ADDR_EXPR;
8338
8339     case CPP_PLUS:
8340       return UNARY_PLUS_EXPR;
8341
8342     case CPP_MINUS:
8343       return NEGATE_EXPR;
8344
8345     case CPP_NOT:
8346       return TRUTH_NOT_EXPR;
8347
8348     case CPP_COMPL:
8349       return BIT_NOT_EXPR;
8350
8351     default:
8352       return ERROR_MARK;
8353     }
8354 }
8355
8356 /* Parse a new-expression.
8357
8358    new-expression:
8359      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8360      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8361
8362    Returns a representation of the expression.  */
8363
8364 static tree
8365 cp_parser_new_expression (cp_parser* parser)
8366 {
8367   bool global_scope_p;
8368   vec<tree, va_gc> *placement;
8369   tree type;
8370   vec<tree, va_gc> *initializer;
8371   tree nelts = NULL_TREE;
8372   tree ret;
8373
8374   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8375
8376   /* Look for the optional `::' operator.  */
8377   global_scope_p
8378     = (cp_parser_global_scope_opt (parser,
8379                                    /*current_scope_valid_p=*/false)
8380        != NULL_TREE);
8381   /* Look for the `new' operator.  */
8382   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8383   /* There's no easy way to tell a new-placement from the
8384      `( type-id )' construct.  */
8385   cp_parser_parse_tentatively (parser);
8386   /* Look for a new-placement.  */
8387   placement = cp_parser_new_placement (parser);
8388   /* If that didn't work out, there's no new-placement.  */
8389   if (!cp_parser_parse_definitely (parser))
8390     {
8391       if (placement != NULL)
8392         release_tree_vector (placement);
8393       placement = NULL;
8394     }
8395
8396   /* If the next token is a `(', then we have a parenthesized
8397      type-id.  */
8398   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8399     {
8400       cp_token *token;
8401       const char *saved_message = parser->type_definition_forbidden_message;
8402
8403       /* Consume the `('.  */
8404       matching_parens parens;
8405       parens.consume_open (parser);
8406
8407       /* Parse the type-id.  */
8408       parser->type_definition_forbidden_message
8409         = G_("types may not be defined in a new-expression");
8410       {
8411         type_id_in_expr_sentinel s (parser);
8412         type = cp_parser_type_id (parser);
8413       }
8414       parser->type_definition_forbidden_message = saved_message;
8415
8416       /* Look for the closing `)'.  */
8417       parens.require_close (parser);
8418       token = cp_lexer_peek_token (parser->lexer);
8419       /* There should not be a direct-new-declarator in this production,
8420          but GCC used to allowed this, so we check and emit a sensible error
8421          message for this case.  */
8422       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8423         {
8424           error_at (token->location,
8425                     "array bound forbidden after parenthesized type-id");
8426           inform (token->location, 
8427                   "try removing the parentheses around the type-id");
8428           cp_parser_direct_new_declarator (parser);
8429         }
8430     }
8431   /* Otherwise, there must be a new-type-id.  */
8432   else
8433     type = cp_parser_new_type_id (parser, &nelts);
8434
8435   /* If the next token is a `(' or '{', then we have a new-initializer.  */
8436   cp_token *token = cp_lexer_peek_token (parser->lexer);
8437   if (token->type == CPP_OPEN_PAREN
8438       || token->type == CPP_OPEN_BRACE)
8439     initializer = cp_parser_new_initializer (parser);
8440   else
8441     initializer = NULL;
8442
8443   /* A new-expression may not appear in an integral constant
8444      expression.  */
8445   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8446     ret = error_mark_node;
8447   /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8448      of a new-type-id or type-id of a new-expression, the new-expression shall
8449      contain a new-initializer of the form ( assignment-expression )".
8450      Additionally, consistently with the spirit of DR 1467, we want to accept
8451      'new auto { 2 }' too.  */
8452   else if ((ret = type_uses_auto (type))
8453            && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8454            && (vec_safe_length (initializer) != 1
8455                || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8456                    && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8457     {
8458       error_at (token->location,
8459                 "initialization of new-expression for type %<auto%> "
8460                 "requires exactly one element");
8461       ret = error_mark_node;
8462     }
8463   else
8464     {
8465       /* Construct a location e.g.:
8466            ptr = new int[100]
8467                  ^~~~~~~~~~~~
8468          with caret == start at the start of the "new" token, and the end
8469          at the end of the final token we consumed.  */
8470       cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8471       location_t end_loc = get_finish (end_tok->location);
8472       location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8473
8474       /* Create a representation of the new-expression.  */
8475       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8476                        tf_warning_or_error);
8477       protected_set_expr_location (ret, combined_loc);
8478     }
8479
8480   if (placement != NULL)
8481     release_tree_vector (placement);
8482   if (initializer != NULL)
8483     release_tree_vector (initializer);
8484
8485   return ret;
8486 }
8487
8488 /* Parse a new-placement.
8489
8490    new-placement:
8491      ( expression-list )
8492
8493    Returns the same representation as for an expression-list.  */
8494
8495 static vec<tree, va_gc> *
8496 cp_parser_new_placement (cp_parser* parser)
8497 {
8498   vec<tree, va_gc> *expression_list;
8499
8500   /* Parse the expression-list.  */
8501   expression_list = (cp_parser_parenthesized_expression_list
8502                      (parser, non_attr, /*cast_p=*/false,
8503                       /*allow_expansion_p=*/true,
8504                       /*non_constant_p=*/NULL));
8505
8506   if (expression_list && expression_list->is_empty ())
8507     error ("expected expression-list or type-id");
8508
8509   return expression_list;
8510 }
8511
8512 /* Parse a new-type-id.
8513
8514    new-type-id:
8515      type-specifier-seq new-declarator [opt]
8516
8517    Returns the TYPE allocated.  If the new-type-id indicates an array
8518    type, *NELTS is set to the number of elements in the last array
8519    bound; the TYPE will not include the last array bound.  */
8520
8521 static tree
8522 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8523 {
8524   cp_decl_specifier_seq type_specifier_seq;
8525   cp_declarator *new_declarator;
8526   cp_declarator *declarator;
8527   cp_declarator *outer_declarator;
8528   const char *saved_message;
8529
8530   /* The type-specifier sequence must not contain type definitions.
8531      (It cannot contain declarations of new types either, but if they
8532      are not definitions we will catch that because they are not
8533      complete.)  */
8534   saved_message = parser->type_definition_forbidden_message;
8535   parser->type_definition_forbidden_message
8536     = G_("types may not be defined in a new-type-id");
8537   /* Parse the type-specifier-seq.  */
8538   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8539                                 /*is_trailing_return=*/false,
8540                                 &type_specifier_seq);
8541   /* Restore the old message.  */
8542   parser->type_definition_forbidden_message = saved_message;
8543
8544   if (type_specifier_seq.type == error_mark_node)
8545     return error_mark_node;
8546
8547   /* Parse the new-declarator.  */
8548   new_declarator = cp_parser_new_declarator_opt (parser);
8549
8550   /* Determine the number of elements in the last array dimension, if
8551      any.  */
8552   *nelts = NULL_TREE;
8553   /* Skip down to the last array dimension.  */
8554   declarator = new_declarator;
8555   outer_declarator = NULL;
8556   while (declarator && (declarator->kind == cdk_pointer
8557                         || declarator->kind == cdk_ptrmem))
8558     {
8559       outer_declarator = declarator;
8560       declarator = declarator->declarator;
8561     }
8562   while (declarator
8563          && declarator->kind == cdk_array
8564          && declarator->declarator
8565          && declarator->declarator->kind == cdk_array)
8566     {
8567       outer_declarator = declarator;
8568       declarator = declarator->declarator;
8569     }
8570
8571   if (declarator && declarator->kind == cdk_array)
8572     {
8573       *nelts = declarator->u.array.bounds;
8574       if (*nelts == error_mark_node)
8575         *nelts = integer_one_node;
8576
8577       if (outer_declarator)
8578         outer_declarator->declarator = declarator->declarator;
8579       else
8580         new_declarator = NULL;
8581     }
8582
8583   return groktypename (&type_specifier_seq, new_declarator, false);
8584 }
8585
8586 /* Parse an (optional) new-declarator.
8587
8588    new-declarator:
8589      ptr-operator new-declarator [opt]
8590      direct-new-declarator
8591
8592    Returns the declarator.  */
8593
8594 static cp_declarator *
8595 cp_parser_new_declarator_opt (cp_parser* parser)
8596 {
8597   enum tree_code code;
8598   tree type, std_attributes = NULL_TREE;
8599   cp_cv_quals cv_quals;  
8600
8601   /* We don't know if there's a ptr-operator next, or not.  */
8602   cp_parser_parse_tentatively (parser);
8603   /* Look for a ptr-operator.  */
8604   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8605   /* If that worked, look for more new-declarators.  */
8606   if (cp_parser_parse_definitely (parser))
8607     {
8608       cp_declarator *declarator;
8609
8610       /* Parse another optional declarator.  */
8611       declarator = cp_parser_new_declarator_opt (parser);
8612
8613       declarator = cp_parser_make_indirect_declarator
8614         (code, type, cv_quals, declarator, std_attributes);
8615
8616       return declarator;
8617     }
8618
8619   /* If the next token is a `[', there is a direct-new-declarator.  */
8620   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8621     return cp_parser_direct_new_declarator (parser);
8622
8623   return NULL;
8624 }
8625
8626 /* Parse a direct-new-declarator.
8627
8628    direct-new-declarator:
8629      [ expression ]
8630      direct-new-declarator [constant-expression]
8631
8632    */
8633
8634 static cp_declarator *
8635 cp_parser_direct_new_declarator (cp_parser* parser)
8636 {
8637   cp_declarator *declarator = NULL;
8638
8639   while (true)
8640     {
8641       tree expression;
8642       cp_token *token;
8643
8644       /* Look for the opening `['.  */
8645       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8646
8647       token = cp_lexer_peek_token (parser->lexer);
8648       expression = cp_parser_expression (parser);
8649       /* The standard requires that the expression have integral
8650          type.  DR 74 adds enumeration types.  We believe that the
8651          real intent is that these expressions be handled like the
8652          expression in a `switch' condition, which also allows
8653          classes with a single conversion to integral or
8654          enumeration type.  */
8655       if (!processing_template_decl)
8656         {
8657           expression
8658             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8659                                           expression,
8660                                           /*complain=*/true);
8661           if (!expression)
8662             {
8663               error_at (token->location,
8664                         "expression in new-declarator must have integral "
8665                         "or enumeration type");
8666               expression = error_mark_node;
8667             }
8668         }
8669
8670       /* Look for the closing `]'.  */
8671       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8672
8673       /* Add this bound to the declarator.  */
8674       declarator = make_array_declarator (declarator, expression);
8675
8676       /* If the next token is not a `[', then there are no more
8677          bounds.  */
8678       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8679         break;
8680     }
8681
8682   return declarator;
8683 }
8684
8685 /* Parse a new-initializer.
8686
8687    new-initializer:
8688      ( expression-list [opt] )
8689      braced-init-list
8690
8691    Returns a representation of the expression-list.  */
8692
8693 static vec<tree, va_gc> *
8694 cp_parser_new_initializer (cp_parser* parser)
8695 {
8696   vec<tree, va_gc> *expression_list;
8697
8698   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8699     {
8700       tree t;
8701       bool expr_non_constant_p;
8702       cp_lexer_set_source_position (parser->lexer);
8703       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8704       t = cp_parser_braced_list (parser, &expr_non_constant_p);
8705       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8706       expression_list = make_tree_vector_single (t);
8707     }
8708   else
8709     expression_list = (cp_parser_parenthesized_expression_list
8710                        (parser, non_attr, /*cast_p=*/false,
8711                         /*allow_expansion_p=*/true,
8712                         /*non_constant_p=*/NULL));
8713
8714   return expression_list;
8715 }
8716
8717 /* Parse a delete-expression.
8718
8719    delete-expression:
8720      :: [opt] delete cast-expression
8721      :: [opt] delete [ ] cast-expression
8722
8723    Returns a representation of the expression.  */
8724
8725 static tree
8726 cp_parser_delete_expression (cp_parser* parser)
8727 {
8728   bool global_scope_p;
8729   bool array_p;
8730   tree expression;
8731
8732   /* Look for the optional `::' operator.  */
8733   global_scope_p
8734     = (cp_parser_global_scope_opt (parser,
8735                                    /*current_scope_valid_p=*/false)
8736        != NULL_TREE);
8737   /* Look for the `delete' keyword.  */
8738   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8739   /* See if the array syntax is in use.  */
8740   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8741     {
8742       /* Consume the `[' token.  */
8743       cp_lexer_consume_token (parser->lexer);
8744       /* Look for the `]' token.  */
8745       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8746       /* Remember that this is the `[]' construct.  */
8747       array_p = true;
8748     }
8749   else
8750     array_p = false;
8751
8752   /* Parse the cast-expression.  */
8753   expression = cp_parser_simple_cast_expression (parser);
8754
8755   /* A delete-expression may not appear in an integral constant
8756      expression.  */
8757   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8758     return error_mark_node;
8759
8760   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8761                         tf_warning_or_error);
8762 }
8763
8764 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8765    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8766    0 otherwise.  */
8767
8768 static int
8769 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8770 {
8771   cp_token *token = cp_lexer_peek_token (parser->lexer);
8772   switch (token->type)
8773     {
8774     case CPP_COMMA:
8775     case CPP_SEMICOLON:
8776     case CPP_QUERY:
8777     case CPP_COLON:
8778     case CPP_CLOSE_SQUARE:
8779     case CPP_CLOSE_PAREN:
8780     case CPP_CLOSE_BRACE:
8781     case CPP_OPEN_BRACE:
8782     case CPP_DOT:
8783     case CPP_DOT_STAR:
8784     case CPP_DEREF:
8785     case CPP_DEREF_STAR:
8786     case CPP_DIV:
8787     case CPP_MOD:
8788     case CPP_LSHIFT:
8789     case CPP_RSHIFT:
8790     case CPP_LESS:
8791     case CPP_GREATER:
8792     case CPP_LESS_EQ:
8793     case CPP_GREATER_EQ:
8794     case CPP_EQ_EQ:
8795     case CPP_NOT_EQ:
8796     case CPP_EQ:
8797     case CPP_MULT_EQ:
8798     case CPP_DIV_EQ:
8799     case CPP_MOD_EQ:
8800     case CPP_PLUS_EQ:
8801     case CPP_MINUS_EQ:
8802     case CPP_RSHIFT_EQ:
8803     case CPP_LSHIFT_EQ:
8804     case CPP_AND_EQ:
8805     case CPP_XOR_EQ:
8806     case CPP_OR_EQ:
8807     case CPP_XOR:
8808     case CPP_OR:
8809     case CPP_OR_OR:
8810     case CPP_EOF:
8811     case CPP_ELLIPSIS:
8812       return 0;
8813
8814     case CPP_OPEN_PAREN:
8815       /* In ((type ()) () the last () isn't a valid cast-expression,
8816          so the whole must be parsed as postfix-expression.  */
8817       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8818              != CPP_CLOSE_PAREN;
8819
8820     case CPP_OPEN_SQUARE:
8821       /* '[' may start a primary-expression in obj-c++ and in C++11,
8822          as a lambda-expression, eg, '(void)[]{}'.  */
8823       if (cxx_dialect >= cxx11)
8824         return -1;
8825       return c_dialect_objc ();
8826
8827     case CPP_PLUS_PLUS:
8828     case CPP_MINUS_MINUS:
8829       /* '++' and '--' may or may not start a cast-expression:
8830
8831          struct T { void operator++(int); };
8832          void f() { (T())++; }
8833
8834          vs
8835
8836          int a;
8837          (int)++a;  */
8838       return -1;
8839
8840     default:
8841       return 1;
8842     }
8843 }
8844
8845 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8846    in the order: const_cast, static_cast, reinterpret_cast.
8847
8848    Don't suggest dynamic_cast.
8849
8850    Return the first legal cast kind found, or NULL otherwise.  */
8851
8852 static const char *
8853 get_cast_suggestion (tree dst_type, tree orig_expr)
8854 {
8855   tree trial;
8856
8857   /* Reuse the parser logic by attempting to build the various kinds of
8858      cast, with "complain" disabled.
8859      Identify the first such cast that is valid.  */
8860
8861   /* Don't attempt to run such logic within template processing.  */
8862   if (processing_template_decl)
8863     return NULL;
8864
8865   /* First try const_cast.  */
8866   trial = build_const_cast (dst_type, orig_expr, tf_none);
8867   if (trial != error_mark_node)
8868     return "const_cast";
8869
8870   /* If that fails, try static_cast.  */
8871   trial = build_static_cast (dst_type, orig_expr, tf_none);
8872   if (trial != error_mark_node)
8873     return "static_cast";
8874
8875   /* Finally, try reinterpret_cast.  */
8876   trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8877   if (trial != error_mark_node)
8878     return "reinterpret_cast";
8879
8880   /* No such cast possible.  */
8881   return NULL;
8882 }
8883
8884 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8885    suggesting how to convert a C-style cast of the form:
8886
8887      (DST_TYPE)ORIG_EXPR
8888
8889    to a C++-style cast.
8890
8891    The primary range of RICHLOC is asssumed to be that of the original
8892    expression.  OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8893    of the parens in the C-style cast.  */
8894
8895 static void
8896 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8897                       location_t close_paren_loc, tree orig_expr,
8898                       tree dst_type)
8899 {
8900   /* This function is non-trivial, so bail out now if the warning isn't
8901      going to be emitted.  */
8902   if (!warn_old_style_cast)
8903     return;
8904
8905   /* Try to find a legal C++ cast, trying them in order:
8906      const_cast, static_cast, reinterpret_cast.  */
8907   const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8908   if (!cast_suggestion)
8909     return;
8910
8911   /* Replace the open paren with "CAST_SUGGESTION<".  */
8912   pretty_printer pp;
8913   pp_printf (&pp, "%s<", cast_suggestion);
8914   rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8915
8916   /* Replace the close paren with "> (".  */
8917   rich_loc->add_fixit_replace (close_paren_loc, "> (");
8918
8919   /* Add a closing paren after the expr (the primary range of RICH_LOC).  */
8920   rich_loc->add_fixit_insert_after (")");
8921 }
8922
8923
8924 /* Parse a cast-expression.
8925
8926    cast-expression:
8927      unary-expression
8928      ( type-id ) cast-expression
8929
8930    ADDRESS_P is true iff the unary-expression is appearing as the
8931    operand of the `&' operator.   CAST_P is true if this expression is
8932    the target of a cast.
8933
8934    Returns a representation of the expression.  */
8935
8936 static cp_expr
8937 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8938                            bool decltype_p, cp_id_kind * pidk)
8939 {
8940   /* If it's a `(', then we might be looking at a cast.  */
8941   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8942     {
8943       tree type = NULL_TREE;
8944       cp_expr expr (NULL_TREE);
8945       int cast_expression = 0;
8946       const char *saved_message;
8947
8948       /* There's no way to know yet whether or not this is a cast.
8949          For example, `(int (3))' is a unary-expression, while `(int)
8950          3' is a cast.  So, we resort to parsing tentatively.  */
8951       cp_parser_parse_tentatively (parser);
8952       /* Types may not be defined in a cast.  */
8953       saved_message = parser->type_definition_forbidden_message;
8954       parser->type_definition_forbidden_message
8955         = G_("types may not be defined in casts");
8956       /* Consume the `('.  */
8957       matching_parens parens;
8958       cp_token *open_paren = parens.consume_open (parser);
8959       location_t open_paren_loc = open_paren->location;
8960       location_t close_paren_loc = UNKNOWN_LOCATION;
8961
8962       /* A very tricky bit is that `(struct S) { 3 }' is a
8963          compound-literal (which we permit in C++ as an extension).
8964          But, that construct is not a cast-expression -- it is a
8965          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
8966          is legal; if the compound-literal were a cast-expression,
8967          you'd need an extra set of parentheses.)  But, if we parse
8968          the type-id, and it happens to be a class-specifier, then we
8969          will commit to the parse at that point, because we cannot
8970          undo the action that is done when creating a new class.  So,
8971          then we cannot back up and do a postfix-expression.
8972
8973          Another tricky case is the following (c++/29234):
8974
8975          struct S { void operator () (); };
8976
8977          void foo ()
8978          {
8979            ( S()() );
8980          }
8981
8982          As a type-id we parse the parenthesized S()() as a function
8983          returning a function, groktypename complains and we cannot
8984          back up in this case either.
8985
8986          Therefore, we scan ahead to the closing `)', and check to see
8987          if the tokens after the `)' can start a cast-expression.  Otherwise
8988          we are dealing with an unary-expression, a postfix-expression
8989          or something else.
8990
8991          Yet another tricky case, in C++11, is the following (c++/54891):
8992
8993          (void)[]{};
8994
8995          The issue is that usually, besides the case of lambda-expressions,
8996          the parenthesized type-id cannot be followed by '[', and, eg, we
8997          want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8998          Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8999          we don't commit, we try a cast-expression, then an unary-expression.
9000
9001          Save tokens so that we can put them back.  */
9002       cp_lexer_save_tokens (parser->lexer);
9003
9004       /* We may be looking at a cast-expression.  */
9005       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9006                                                  /*consume_paren=*/true))
9007         cast_expression
9008           = cp_parser_tokens_start_cast_expression (parser);
9009
9010       /* Roll back the tokens we skipped.  */
9011       cp_lexer_rollback_tokens (parser->lexer);
9012       /* If we aren't looking at a cast-expression, simulate an error so
9013          that the call to cp_parser_error_occurred below returns true.  */
9014       if (!cast_expression)
9015         cp_parser_simulate_error (parser);
9016       else
9017         {
9018           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9019           parser->in_type_id_in_expr_p = true;
9020           /* Look for the type-id.  */
9021           type = cp_parser_type_id (parser);
9022           /* Look for the closing `)'.  */
9023           cp_token *close_paren = parens.require_close (parser);
9024           if (close_paren)
9025             close_paren_loc = close_paren->location;
9026           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9027         }
9028
9029       /* Restore the saved message.  */
9030       parser->type_definition_forbidden_message = saved_message;
9031
9032       /* At this point this can only be either a cast or a
9033          parenthesized ctor such as `(T ())' that looks like a cast to
9034          function returning T.  */
9035       if (!cp_parser_error_occurred (parser))
9036         {
9037           /* Only commit if the cast-expression doesn't start with
9038              '++', '--', or '[' in C++11.  */
9039           if (cast_expression > 0)
9040             cp_parser_commit_to_topmost_tentative_parse (parser);
9041
9042           expr = cp_parser_cast_expression (parser,
9043                                             /*address_p=*/false,
9044                                             /*cast_p=*/true,
9045                                             /*decltype_p=*/false,
9046                                             pidk);
9047
9048           if (cp_parser_parse_definitely (parser))
9049             {
9050               /* Warn about old-style casts, if so requested.  */
9051               if (warn_old_style_cast
9052                   && !in_system_header_at (input_location)
9053                   && !VOID_TYPE_P (type)
9054                   && current_lang_name != lang_name_c)
9055                 {
9056                   gcc_rich_location rich_loc (input_location);
9057                   maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9058                                         expr, type);
9059                   warning_at (&rich_loc, OPT_Wold_style_cast,
9060                               "use of old-style cast to %q#T", type);
9061                 }
9062
9063               /* Only type conversions to integral or enumeration types
9064                  can be used in constant-expressions.  */
9065               if (!cast_valid_in_integral_constant_expression_p (type)
9066                   && cp_parser_non_integral_constant_expression (parser,
9067                                                                  NIC_CAST))
9068                 return error_mark_node;
9069
9070               /* Perform the cast.  */
9071               /* Make a location:
9072                    (TYPE) EXPR
9073                    ^~~~~~~~~~~
9074                  with start==caret at the open paren, extending to the
9075                  end of "expr".  */
9076               location_t cast_loc = make_location (open_paren_loc,
9077                                                    open_paren_loc,
9078                                                    expr.get_finish ());
9079               expr = build_c_cast (cast_loc, type, expr);
9080               return expr;
9081             }
9082         }
9083       else 
9084         cp_parser_abort_tentative_parse (parser);
9085     }
9086
9087   /* If we get here, then it's not a cast, so it must be a
9088      unary-expression.  */
9089   return cp_parser_unary_expression (parser, pidk, address_p,
9090                                      cast_p, decltype_p);
9091 }
9092
9093 /* Parse a binary expression of the general form:
9094
9095    pm-expression:
9096      cast-expression
9097      pm-expression .* cast-expression
9098      pm-expression ->* cast-expression
9099
9100    multiplicative-expression:
9101      pm-expression
9102      multiplicative-expression * pm-expression
9103      multiplicative-expression / pm-expression
9104      multiplicative-expression % pm-expression
9105
9106    additive-expression:
9107      multiplicative-expression
9108      additive-expression + multiplicative-expression
9109      additive-expression - multiplicative-expression
9110
9111    shift-expression:
9112      additive-expression
9113      shift-expression << additive-expression
9114      shift-expression >> additive-expression
9115
9116    relational-expression:
9117      shift-expression
9118      relational-expression < shift-expression
9119      relational-expression > shift-expression
9120      relational-expression <= shift-expression
9121      relational-expression >= shift-expression
9122
9123   GNU Extension:
9124
9125    relational-expression:
9126      relational-expression <? shift-expression
9127      relational-expression >? shift-expression
9128
9129    equality-expression:
9130      relational-expression
9131      equality-expression == relational-expression
9132      equality-expression != relational-expression
9133
9134    and-expression:
9135      equality-expression
9136      and-expression & equality-expression
9137
9138    exclusive-or-expression:
9139      and-expression
9140      exclusive-or-expression ^ and-expression
9141
9142    inclusive-or-expression:
9143      exclusive-or-expression
9144      inclusive-or-expression | exclusive-or-expression
9145
9146    logical-and-expression:
9147      inclusive-or-expression
9148      logical-and-expression && inclusive-or-expression
9149
9150    logical-or-expression:
9151      logical-and-expression
9152      logical-or-expression || logical-and-expression
9153
9154    All these are implemented with a single function like:
9155
9156    binary-expression:
9157      simple-cast-expression
9158      binary-expression <token> binary-expression
9159
9160    CAST_P is true if this expression is the target of a cast.
9161
9162    The binops_by_token map is used to get the tree codes for each <token> type.
9163    binary-expressions are associated according to a precedence table.  */
9164
9165 #define TOKEN_PRECEDENCE(token)                              \
9166 (((token->type == CPP_GREATER                                \
9167    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9168   && !parser->greater_than_is_operator_p)                    \
9169  ? PREC_NOT_OPERATOR                                         \
9170  : binops_by_token[token->type].prec)
9171
9172 static cp_expr
9173 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9174                              bool no_toplevel_fold_p,
9175                              bool decltype_p,
9176                              enum cp_parser_prec prec,
9177                              cp_id_kind * pidk)
9178 {
9179   cp_parser_expression_stack stack;
9180   cp_parser_expression_stack_entry *sp = &stack[0];
9181   cp_parser_expression_stack_entry current;
9182   cp_expr rhs;
9183   cp_token *token;
9184   enum tree_code rhs_type;
9185   enum cp_parser_prec new_prec, lookahead_prec;
9186   tree overload;
9187
9188   /* Parse the first expression.  */
9189   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9190                       ? TRUTH_NOT_EXPR : ERROR_MARK);
9191   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9192                                            cast_p, decltype_p, pidk);
9193   current.prec = prec;
9194
9195   if (cp_parser_error_occurred (parser))
9196     return error_mark_node;
9197
9198   for (;;)
9199     {
9200       /* Get an operator token.  */
9201       token = cp_lexer_peek_token (parser->lexer);
9202
9203       if (warn_cxx11_compat
9204           && token->type == CPP_RSHIFT
9205           && !parser->greater_than_is_operator_p)
9206         {
9207           if (warning_at (token->location, OPT_Wc__11_compat,
9208                           "%<>>%> operator is treated"
9209                           " as two right angle brackets in C++11"))
9210             inform (token->location,
9211                     "suggest parentheses around %<>>%> expression");
9212         }
9213
9214       new_prec = TOKEN_PRECEDENCE (token);
9215       if (new_prec != PREC_NOT_OPERATOR
9216           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9217         /* This is a fold-expression; handle it later.  */
9218         new_prec = PREC_NOT_OPERATOR;
9219
9220       /* Popping an entry off the stack means we completed a subexpression:
9221          - either we found a token which is not an operator (`>' where it is not
9222            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9223            will happen repeatedly;
9224          - or, we found an operator which has lower priority.  This is the case
9225            where the recursive descent *ascends*, as in `3 * 4 + 5' after
9226            parsing `3 * 4'.  */
9227       if (new_prec <= current.prec)
9228         {
9229           if (sp == stack)
9230             break;
9231           else
9232             goto pop;
9233         }
9234
9235      get_rhs:
9236       current.tree_type = binops_by_token[token->type].tree_type;
9237       current.loc = token->location;
9238
9239       /* We used the operator token.  */
9240       cp_lexer_consume_token (parser->lexer);
9241
9242       /* For "false && x" or "true || x", x will never be executed;
9243          disable warnings while evaluating it.  */
9244       if (current.tree_type == TRUTH_ANDIF_EXPR)
9245         c_inhibit_evaluation_warnings +=
9246           cp_fully_fold (current.lhs) == truthvalue_false_node;
9247       else if (current.tree_type == TRUTH_ORIF_EXPR)
9248         c_inhibit_evaluation_warnings +=
9249           cp_fully_fold (current.lhs) == truthvalue_true_node;
9250
9251       /* Extract another operand.  It may be the RHS of this expression
9252          or the LHS of a new, higher priority expression.  */
9253       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9254                   ? TRUTH_NOT_EXPR : ERROR_MARK);
9255       rhs = cp_parser_simple_cast_expression (parser);
9256
9257       /* Get another operator token.  Look up its precedence to avoid
9258          building a useless (immediately popped) stack entry for common
9259          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
9260       token = cp_lexer_peek_token (parser->lexer);
9261       lookahead_prec = TOKEN_PRECEDENCE (token);
9262       if (lookahead_prec != PREC_NOT_OPERATOR
9263           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9264         lookahead_prec = PREC_NOT_OPERATOR;
9265       if (lookahead_prec > new_prec)
9266         {
9267           /* ... and prepare to parse the RHS of the new, higher priority
9268              expression.  Since precedence levels on the stack are
9269              monotonically increasing, we do not have to care about
9270              stack overflows.  */
9271           *sp = current;
9272           ++sp;
9273           current.lhs = rhs;
9274           current.lhs_type = rhs_type;
9275           current.prec = new_prec;
9276           new_prec = lookahead_prec;
9277           goto get_rhs;
9278
9279          pop:
9280           lookahead_prec = new_prec;
9281           /* If the stack is not empty, we have parsed into LHS the right side
9282              (`4' in the example above) of an expression we had suspended.
9283              We can use the information on the stack to recover the LHS (`3')
9284              from the stack together with the tree code (`MULT_EXPR'), and
9285              the precedence of the higher level subexpression
9286              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
9287              which will be used to actually build the additive expression.  */
9288           rhs = current.lhs;
9289           rhs_type = current.lhs_type;
9290           --sp;
9291           current = *sp;
9292         }
9293
9294       /* Undo the disabling of warnings done above.  */
9295       if (current.tree_type == TRUTH_ANDIF_EXPR)
9296         c_inhibit_evaluation_warnings -=
9297           cp_fully_fold (current.lhs) == truthvalue_false_node;
9298       else if (current.tree_type == TRUTH_ORIF_EXPR)
9299         c_inhibit_evaluation_warnings -=
9300           cp_fully_fold (current.lhs) == truthvalue_true_node;
9301
9302       if (warn_logical_not_paren
9303           && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9304           && current.lhs_type == TRUTH_NOT_EXPR
9305           /* Avoid warning for !!x == y.  */
9306           && (TREE_CODE (current.lhs) != NE_EXPR
9307               || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9308           && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9309               || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9310                   /* Avoid warning for !b == y where b is boolean.  */
9311                   && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9312                       || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9313                           != BOOLEAN_TYPE))))
9314           /* Avoid warning for !!b == y where b is boolean.  */
9315           && (!DECL_P (current.lhs)
9316               || TREE_TYPE (current.lhs) == NULL_TREE
9317               || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9318         warn_logical_not_parentheses (current.loc, current.tree_type,
9319                                       current.lhs, maybe_constant_value (rhs));
9320
9321       overload = NULL;
9322
9323       location_t combined_loc = make_location (current.loc,
9324                                                current.lhs.get_start (),
9325                                                rhs.get_finish ());
9326
9327       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9328          ERROR_MARK for everything that is not a binary expression.
9329          This makes warn_about_parentheses miss some warnings that
9330          involve unary operators.  For unary expressions we should
9331          pass the correct tree_code unless the unary expression was
9332          surrounded by parentheses.
9333       */
9334       if (no_toplevel_fold_p
9335           && lookahead_prec <= current.prec
9336           && sp == stack)
9337         {
9338           if (current.lhs == error_mark_node || rhs == error_mark_node)
9339             current.lhs = error_mark_node;
9340           else
9341             {
9342               current.lhs
9343                 = build_min (current.tree_type,
9344                              TREE_CODE_CLASS (current.tree_type)
9345                              == tcc_comparison
9346                              ? boolean_type_node : TREE_TYPE (current.lhs),
9347                              current.lhs.get_value (), rhs.get_value ());
9348               SET_EXPR_LOCATION (current.lhs, combined_loc);
9349             }
9350         }
9351       else
9352         {
9353           current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9354                                            current.lhs, current.lhs_type,
9355                                            rhs, rhs_type, &overload,
9356                                            complain_flags (decltype_p));
9357           /* TODO: build_x_binary_op doesn't always honor the location.  */
9358           current.lhs.set_location (combined_loc);
9359         }
9360       current.lhs_type = current.tree_type;
9361
9362       /* If the binary operator required the use of an overloaded operator,
9363          then this expression cannot be an integral constant-expression.
9364          An overloaded operator can be used even if both operands are
9365          otherwise permissible in an integral constant-expression if at
9366          least one of the operands is of enumeration type.  */
9367
9368       if (overload
9369           && cp_parser_non_integral_constant_expression (parser,
9370                                                          NIC_OVERLOADED))
9371         return error_mark_node;
9372     }
9373
9374   return current.lhs;
9375 }
9376
9377 static cp_expr
9378 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9379                              bool no_toplevel_fold_p,
9380                              enum cp_parser_prec prec,
9381                              cp_id_kind * pidk)
9382 {
9383   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9384                                       /*decltype*/false, prec, pidk);
9385 }
9386
9387 /* Parse the `? expression : assignment-expression' part of a
9388    conditional-expression.  The LOGICAL_OR_EXPR is the
9389    logical-or-expression that started the conditional-expression.
9390    Returns a representation of the entire conditional-expression.
9391
9392    This routine is used by cp_parser_assignment_expression.
9393
9394      ? expression : assignment-expression
9395
9396    GNU Extensions:
9397
9398      ? : assignment-expression */
9399
9400 static tree
9401 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9402 {
9403   tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9404   cp_expr assignment_expr;
9405   struct cp_token *token;
9406   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9407
9408   /* Consume the `?' token.  */
9409   cp_lexer_consume_token (parser->lexer);
9410   token = cp_lexer_peek_token (parser->lexer);
9411   if (cp_parser_allow_gnu_extensions_p (parser)
9412       && token->type == CPP_COLON)
9413     {
9414       pedwarn (token->location, OPT_Wpedantic, 
9415                "ISO C++ does not allow ?: with omitted middle operand");
9416       /* Implicit true clause.  */
9417       expr = NULL_TREE;
9418       c_inhibit_evaluation_warnings +=
9419         folded_logical_or_expr == truthvalue_true_node;
9420       warn_for_omitted_condop (token->location, logical_or_expr);
9421     }
9422   else
9423     {
9424       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9425       parser->colon_corrects_to_scope_p = false;
9426       /* Parse the expression.  */
9427       c_inhibit_evaluation_warnings +=
9428         folded_logical_or_expr == truthvalue_false_node;
9429       expr = cp_parser_expression (parser);
9430       c_inhibit_evaluation_warnings +=
9431         ((folded_logical_or_expr == truthvalue_true_node)
9432          - (folded_logical_or_expr == truthvalue_false_node));
9433       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9434     }
9435
9436   /* The next token should be a `:'.  */
9437   cp_parser_require (parser, CPP_COLON, RT_COLON);
9438   /* Parse the assignment-expression.  */
9439   assignment_expr = cp_parser_assignment_expression (parser);
9440   c_inhibit_evaluation_warnings -=
9441     folded_logical_or_expr == truthvalue_true_node;
9442
9443   /* Make a location:
9444        LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9445        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9446      with the caret at the "?", ranging from the start of
9447      the logical_or_expr to the end of the assignment_expr.  */
9448   loc = make_location (loc,
9449                        logical_or_expr.get_start (),
9450                        assignment_expr.get_finish ());
9451
9452   /* Build the conditional-expression.  */
9453   return build_x_conditional_expr (loc, logical_or_expr,
9454                                    expr,
9455                                    assignment_expr,
9456                                    tf_warning_or_error);
9457 }
9458
9459 /* Parse an assignment-expression.
9460
9461    assignment-expression:
9462      conditional-expression
9463      logical-or-expression assignment-operator assignment_expression
9464      throw-expression
9465
9466    CAST_P is true if this expression is the target of a cast.
9467    DECLTYPE_P is true if this expression is the operand of decltype.
9468
9469    Returns a representation for the expression.  */
9470
9471 static cp_expr
9472 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9473                                  bool cast_p, bool decltype_p)
9474 {
9475   cp_expr expr;
9476
9477   /* If the next token is the `throw' keyword, then we're looking at
9478      a throw-expression.  */
9479   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9480     expr = cp_parser_throw_expression (parser);
9481   /* Otherwise, it must be that we are looking at a
9482      logical-or-expression.  */
9483   else
9484     {
9485       /* Parse the binary expressions (logical-or-expression).  */
9486       expr = cp_parser_binary_expression (parser, cast_p, false,
9487                                           decltype_p,
9488                                           PREC_NOT_OPERATOR, pidk);
9489       /* If the next token is a `?' then we're actually looking at a
9490          conditional-expression.  */
9491       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9492         return cp_parser_question_colon_clause (parser, expr);
9493       else
9494         {
9495           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9496
9497           /* If it's an assignment-operator, we're using the second
9498              production.  */
9499           enum tree_code assignment_operator
9500             = cp_parser_assignment_operator_opt (parser);
9501           if (assignment_operator != ERROR_MARK)
9502             {
9503               bool non_constant_p;
9504
9505               /* Parse the right-hand side of the assignment.  */
9506               cp_expr rhs = cp_parser_initializer_clause (parser,
9507                                                           &non_constant_p);
9508
9509               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9510                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9511
9512               /* An assignment may not appear in a
9513                  constant-expression.  */
9514               if (cp_parser_non_integral_constant_expression (parser,
9515                                                               NIC_ASSIGNMENT))
9516                 return error_mark_node;
9517               /* Build the assignment expression.  Its default
9518                  location:
9519                    LHS = RHS
9520                    ~~~~^~~~~
9521                  is the location of the '=' token as the
9522                  caret, ranging from the start of the lhs to the
9523                  end of the rhs.  */
9524               loc = make_location (loc,
9525                                    expr.get_start (),
9526                                    rhs.get_finish ());
9527               expr = build_x_modify_expr (loc, expr,
9528                                           assignment_operator,
9529                                           rhs,
9530                                           complain_flags (decltype_p));
9531               /* TODO: build_x_modify_expr doesn't honor the location,
9532                  so we must set it here.  */
9533               expr.set_location (loc);
9534             }
9535         }
9536     }
9537
9538   return expr;
9539 }
9540
9541 /* Parse an (optional) assignment-operator.
9542
9543    assignment-operator: one of
9544      = *= /= %= += -= >>= <<= &= ^= |=
9545
9546    GNU Extension:
9547
9548    assignment-operator: one of
9549      <?= >?=
9550
9551    If the next token is an assignment operator, the corresponding tree
9552    code is returned, and the token is consumed.  For example, for
9553    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
9554    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
9555    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
9556    operator, ERROR_MARK is returned.  */
9557
9558 static enum tree_code
9559 cp_parser_assignment_operator_opt (cp_parser* parser)
9560 {
9561   enum tree_code op;
9562   cp_token *token;
9563
9564   /* Peek at the next token.  */
9565   token = cp_lexer_peek_token (parser->lexer);
9566
9567   switch (token->type)
9568     {
9569     case CPP_EQ:
9570       op = NOP_EXPR;
9571       break;
9572
9573     case CPP_MULT_EQ:
9574       op = MULT_EXPR;
9575       break;
9576
9577     case CPP_DIV_EQ:
9578       op = TRUNC_DIV_EXPR;
9579       break;
9580
9581     case CPP_MOD_EQ:
9582       op = TRUNC_MOD_EXPR;
9583       break;
9584
9585     case CPP_PLUS_EQ:
9586       op = PLUS_EXPR;
9587       break;
9588
9589     case CPP_MINUS_EQ:
9590       op = MINUS_EXPR;
9591       break;
9592
9593     case CPP_RSHIFT_EQ:
9594       op = RSHIFT_EXPR;
9595       break;
9596
9597     case CPP_LSHIFT_EQ:
9598       op = LSHIFT_EXPR;
9599       break;
9600
9601     case CPP_AND_EQ:
9602       op = BIT_AND_EXPR;
9603       break;
9604
9605     case CPP_XOR_EQ:
9606       op = BIT_XOR_EXPR;
9607       break;
9608
9609     case CPP_OR_EQ:
9610       op = BIT_IOR_EXPR;
9611       break;
9612
9613     default:
9614       /* Nothing else is an assignment operator.  */
9615       op = ERROR_MARK;
9616     }
9617
9618   /* An operator followed by ... is a fold-expression, handled elsewhere.  */
9619   if (op != ERROR_MARK
9620       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9621     op = ERROR_MARK;
9622
9623   /* If it was an assignment operator, consume it.  */
9624   if (op != ERROR_MARK)
9625     cp_lexer_consume_token (parser->lexer);
9626
9627   return op;
9628 }
9629
9630 /* Parse an expression.
9631
9632    expression:
9633      assignment-expression
9634      expression , assignment-expression
9635
9636    CAST_P is true if this expression is the target of a cast.
9637    DECLTYPE_P is true if this expression is the immediate operand of decltype,
9638      except possibly parenthesized or on the RHS of a comma (N3276).
9639
9640    Returns a representation of the expression.  */
9641
9642 static cp_expr
9643 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9644                       bool cast_p, bool decltype_p)
9645 {
9646   cp_expr expression = NULL_TREE;
9647   location_t loc = UNKNOWN_LOCATION;
9648
9649   while (true)
9650     {
9651       cp_expr assignment_expression;
9652
9653       /* Parse the next assignment-expression.  */
9654       assignment_expression
9655         = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9656
9657       /* We don't create a temporary for a call that is the immediate operand
9658          of decltype or on the RHS of a comma.  But when we see a comma, we
9659          need to create a temporary for a call on the LHS.  */
9660       if (decltype_p && !processing_template_decl
9661           && TREE_CODE (assignment_expression) == CALL_EXPR
9662           && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9663           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9664         assignment_expression
9665           = build_cplus_new (TREE_TYPE (assignment_expression),
9666                              assignment_expression, tf_warning_or_error);
9667
9668       /* If this is the first assignment-expression, we can just
9669          save it away.  */
9670       if (!expression)
9671         expression = assignment_expression;
9672       else
9673         {
9674           /* Create a location with caret at the comma, ranging
9675              from the start of the LHS to the end of the RHS.  */
9676           loc = make_location (loc,
9677                                expression.get_start (),
9678                                assignment_expression.get_finish ());
9679           expression = build_x_compound_expr (loc, expression,
9680                                               assignment_expression,
9681                                               complain_flags (decltype_p));
9682           expression.set_location (loc);
9683         }
9684       /* If the next token is not a comma, or we're in a fold-expression, then
9685          we are done with the expression.  */
9686       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9687           || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9688         break;
9689       /* Consume the `,'.  */
9690       loc = cp_lexer_peek_token (parser->lexer)->location;
9691       cp_lexer_consume_token (parser->lexer);
9692       /* A comma operator cannot appear in a constant-expression.  */
9693       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9694         expression = error_mark_node;
9695     }
9696
9697   return expression;
9698 }
9699
9700 /* Parse a constant-expression.
9701
9702    constant-expression:
9703      conditional-expression
9704
9705   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9706   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
9707   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
9708   is false, NON_CONSTANT_P should be NULL.  If STRICT_P is true,
9709   only parse a conditional-expression, otherwise parse an
9710   assignment-expression.  See below for rationale.  */
9711
9712 static cp_expr
9713 cp_parser_constant_expression (cp_parser* parser,
9714                                bool allow_non_constant_p,
9715                                bool *non_constant_p,
9716                                bool strict_p)
9717 {
9718   bool saved_integral_constant_expression_p;
9719   bool saved_allow_non_integral_constant_expression_p;
9720   bool saved_non_integral_constant_expression_p;
9721   cp_expr expression;
9722
9723   /* It might seem that we could simply parse the
9724      conditional-expression, and then check to see if it were
9725      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
9726      one that the compiler can figure out is constant, possibly after
9727      doing some simplifications or optimizations.  The standard has a
9728      precise definition of constant-expression, and we must honor
9729      that, even though it is somewhat more restrictive.
9730
9731      For example:
9732
9733        int i[(2, 3)];
9734
9735      is not a legal declaration, because `(2, 3)' is not a
9736      constant-expression.  The `,' operator is forbidden in a
9737      constant-expression.  However, GCC's constant-folding machinery
9738      will fold this operation to an INTEGER_CST for `3'.  */
9739
9740   /* Save the old settings.  */
9741   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9742   saved_allow_non_integral_constant_expression_p
9743     = parser->allow_non_integral_constant_expression_p;
9744   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9745   /* We are now parsing a constant-expression.  */
9746   parser->integral_constant_expression_p = true;
9747   parser->allow_non_integral_constant_expression_p
9748     = (allow_non_constant_p || cxx_dialect >= cxx11);
9749   parser->non_integral_constant_expression_p = false;
9750   /* Although the grammar says "conditional-expression", when not STRICT_P,
9751      we parse an "assignment-expression", which also permits
9752      "throw-expression" and the use of assignment operators.  In the case
9753      that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9754      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
9755      actually essential that we look for an assignment-expression.
9756      For example, cp_parser_initializer_clauses uses this function to
9757      determine whether a particular assignment-expression is in fact
9758      constant.  */
9759   if (strict_p)
9760     {
9761       /* Parse the binary expressions (logical-or-expression).  */
9762       expression = cp_parser_binary_expression (parser, false, false, false,
9763                                                 PREC_NOT_OPERATOR, NULL);
9764       /* If the next token is a `?' then we're actually looking at
9765          a conditional-expression; otherwise we're done.  */
9766       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9767         expression = cp_parser_question_colon_clause (parser, expression);
9768     }
9769   else
9770     expression = cp_parser_assignment_expression (parser);
9771   /* Restore the old settings.  */
9772   parser->integral_constant_expression_p
9773     = saved_integral_constant_expression_p;
9774   parser->allow_non_integral_constant_expression_p
9775     = saved_allow_non_integral_constant_expression_p;
9776   if (cxx_dialect >= cxx11)
9777     {
9778       /* Require an rvalue constant expression here; that's what our
9779          callers expect.  Reference constant expressions are handled
9780          separately in e.g. cp_parser_template_argument.  */
9781       tree decay = expression;
9782       if (TREE_TYPE (expression)
9783           && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9784         decay = build_address (expression);
9785       bool is_const = potential_rvalue_constant_expression (decay);
9786       parser->non_integral_constant_expression_p = !is_const;
9787       if (!is_const && !allow_non_constant_p)
9788         require_potential_rvalue_constant_expression (decay);
9789     }
9790   if (allow_non_constant_p)
9791     *non_constant_p = parser->non_integral_constant_expression_p;
9792   parser->non_integral_constant_expression_p
9793     = saved_non_integral_constant_expression_p;
9794
9795   return expression;
9796 }
9797
9798 /* Parse __builtin_offsetof.
9799
9800    offsetof-expression:
9801      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9802
9803    offsetof-member-designator:
9804      id-expression
9805      | offsetof-member-designator "." id-expression
9806      | offsetof-member-designator "[" expression "]"
9807      | offsetof-member-designator "->" id-expression  */
9808
9809 static cp_expr
9810 cp_parser_builtin_offsetof (cp_parser *parser)
9811 {
9812   int save_ice_p, save_non_ice_p;
9813   tree type;
9814   cp_expr expr;
9815   cp_id_kind dummy;
9816   cp_token *token;
9817   location_t finish_loc;
9818
9819   /* We're about to accept non-integral-constant things, but will
9820      definitely yield an integral constant expression.  Save and
9821      restore these values around our local parsing.  */
9822   save_ice_p = parser->integral_constant_expression_p;
9823   save_non_ice_p = parser->non_integral_constant_expression_p;
9824
9825   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9826
9827   /* Consume the "__builtin_offsetof" token.  */
9828   cp_lexer_consume_token (parser->lexer);
9829   /* Consume the opening `('.  */
9830   matching_parens parens;
9831   parens.require_open (parser);
9832   /* Parse the type-id.  */
9833   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9834   {
9835     const char *saved_message = parser->type_definition_forbidden_message;
9836     parser->type_definition_forbidden_message
9837       = G_("types may not be defined within __builtin_offsetof");
9838     type = cp_parser_type_id (parser);
9839     parser->type_definition_forbidden_message = saved_message;
9840   }
9841   /* Look for the `,'.  */
9842   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9843   token = cp_lexer_peek_token (parser->lexer);
9844
9845   /* Build the (type *)null that begins the traditional offsetof macro.  */
9846   tree object_ptr
9847     = build_static_cast (build_pointer_type (type), null_pointer_node,
9848                          tf_warning_or_error);
9849
9850   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
9851   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9852                                                  true, &dummy, token->location);
9853   while (true)
9854     {
9855       token = cp_lexer_peek_token (parser->lexer);
9856       switch (token->type)
9857         {
9858         case CPP_OPEN_SQUARE:
9859           /* offsetof-member-designator "[" expression "]" */
9860           expr = cp_parser_postfix_open_square_expression (parser, expr,
9861                                                            true, false);
9862           break;
9863
9864         case CPP_DEREF:
9865           /* offsetof-member-designator "->" identifier */
9866           expr = grok_array_decl (token->location, expr,
9867                                   integer_zero_node, false);
9868           /* FALLTHRU */
9869
9870         case CPP_DOT:
9871           /* offsetof-member-designator "." identifier */
9872           cp_lexer_consume_token (parser->lexer);
9873           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9874                                                          expr, true, &dummy,
9875                                                          token->location);
9876           break;
9877
9878         case CPP_CLOSE_PAREN:
9879           /* Consume the ")" token.  */
9880           finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9881           cp_lexer_consume_token (parser->lexer);
9882           goto success;
9883
9884         default:
9885           /* Error.  We know the following require will fail, but
9886              that gives the proper error message.  */
9887           parens.require_close (parser);
9888           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9889           expr = error_mark_node;
9890           goto failure;
9891         }
9892     }
9893
9894  success:
9895   /* Make a location of the form:
9896        __builtin_offsetof (struct s, f)
9897        ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9898      with caret at the type-id, ranging from the start of the
9899      "_builtin_offsetof" token to the close paren.  */
9900   loc = make_location (loc, start_loc, finish_loc);
9901   /* The result will be an INTEGER_CST, so we need to explicitly
9902      preserve the location.  */
9903   expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9904
9905  failure:
9906   parser->integral_constant_expression_p = save_ice_p;
9907   parser->non_integral_constant_expression_p = save_non_ice_p;
9908
9909   expr = expr.maybe_add_location_wrapper ();
9910   return expr;
9911 }
9912
9913 /* Parse a trait expression.
9914
9915    Returns a representation of the expression, the underlying type
9916    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
9917
9918 static cp_expr
9919 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9920 {
9921   cp_trait_kind kind;
9922   tree type1, type2 = NULL_TREE;
9923   bool binary = false;
9924   bool variadic = false;
9925
9926   switch (keyword)
9927     {
9928     case RID_HAS_NOTHROW_ASSIGN:
9929       kind = CPTK_HAS_NOTHROW_ASSIGN;
9930       break;
9931     case RID_HAS_NOTHROW_CONSTRUCTOR:
9932       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9933       break;
9934     case RID_HAS_NOTHROW_COPY:
9935       kind = CPTK_HAS_NOTHROW_COPY;
9936       break;
9937     case RID_HAS_TRIVIAL_ASSIGN:
9938       kind = CPTK_HAS_TRIVIAL_ASSIGN;
9939       break;
9940     case RID_HAS_TRIVIAL_CONSTRUCTOR:
9941       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9942       break;
9943     case RID_HAS_TRIVIAL_COPY:
9944       kind = CPTK_HAS_TRIVIAL_COPY;
9945       break;
9946     case RID_HAS_TRIVIAL_DESTRUCTOR:
9947       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9948       break;
9949     case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9950       kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9951       break;
9952     case RID_HAS_VIRTUAL_DESTRUCTOR:
9953       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9954       break;
9955     case RID_IS_ABSTRACT:
9956       kind = CPTK_IS_ABSTRACT;
9957       break;
9958     case RID_IS_AGGREGATE:
9959       kind = CPTK_IS_AGGREGATE;
9960       break;
9961     case RID_IS_BASE_OF:
9962       kind = CPTK_IS_BASE_OF;
9963       binary = true;
9964       break;
9965     case RID_IS_CLASS:
9966       kind = CPTK_IS_CLASS;
9967       break;
9968     case RID_IS_EMPTY:
9969       kind = CPTK_IS_EMPTY;
9970       break;
9971     case RID_IS_ENUM:
9972       kind = CPTK_IS_ENUM;
9973       break;
9974     case RID_IS_FINAL:
9975       kind = CPTK_IS_FINAL;
9976       break;
9977     case RID_IS_LITERAL_TYPE:
9978       kind = CPTK_IS_LITERAL_TYPE;
9979       break;
9980     case RID_IS_POD:
9981       kind = CPTK_IS_POD;
9982       break;
9983     case RID_IS_POLYMORPHIC:
9984       kind = CPTK_IS_POLYMORPHIC;
9985       break;
9986     case RID_IS_SAME_AS:
9987       kind = CPTK_IS_SAME_AS;
9988       binary = true;
9989       break;
9990     case RID_IS_STD_LAYOUT:
9991       kind = CPTK_IS_STD_LAYOUT;
9992       break;
9993     case RID_IS_TRIVIAL:
9994       kind = CPTK_IS_TRIVIAL;
9995       break;
9996     case RID_IS_TRIVIALLY_ASSIGNABLE:
9997       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9998       binary = true;
9999       break;
10000     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10001       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10002       variadic = true;
10003       break;
10004     case RID_IS_TRIVIALLY_COPYABLE:
10005       kind = CPTK_IS_TRIVIALLY_COPYABLE;
10006       break;
10007     case RID_IS_UNION:
10008       kind = CPTK_IS_UNION;
10009       break;
10010     case RID_UNDERLYING_TYPE:
10011       kind = CPTK_UNDERLYING_TYPE;
10012       break;
10013     case RID_BASES:
10014       kind = CPTK_BASES;
10015       break;
10016     case RID_DIRECT_BASES:
10017       kind = CPTK_DIRECT_BASES;
10018       break;
10019     case RID_IS_ASSIGNABLE:
10020       kind = CPTK_IS_ASSIGNABLE;
10021       binary = true;
10022       break;
10023     case RID_IS_CONSTRUCTIBLE:
10024       kind = CPTK_IS_CONSTRUCTIBLE;
10025       variadic = true;
10026       break;
10027     default:
10028       gcc_unreachable ();
10029     }
10030
10031   /* Get location of initial token.  */
10032   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10033
10034   /* Consume the token.  */
10035   cp_lexer_consume_token (parser->lexer);
10036
10037   matching_parens parens;
10038   parens.require_open (parser);
10039
10040   {
10041     type_id_in_expr_sentinel s (parser);
10042     type1 = cp_parser_type_id (parser);
10043   }
10044
10045   if (type1 == error_mark_node)
10046     return error_mark_node;
10047
10048   if (binary)
10049     {
10050       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10051  
10052       {
10053         type_id_in_expr_sentinel s (parser);
10054         type2 = cp_parser_type_id (parser);
10055       }
10056
10057       if (type2 == error_mark_node)
10058         return error_mark_node;
10059     }
10060   else if (variadic)
10061     {
10062       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10063         {
10064           cp_lexer_consume_token (parser->lexer);
10065           tree elt = cp_parser_type_id (parser);
10066           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10067             {
10068               cp_lexer_consume_token (parser->lexer);
10069               elt = make_pack_expansion (elt);
10070             }
10071           if (elt == error_mark_node)
10072             return error_mark_node;
10073           type2 = tree_cons (NULL_TREE, elt, type2);
10074         }
10075     }
10076
10077   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10078   parens.require_close (parser);
10079
10080   /* Construct a location of the form:
10081        __is_trivially_copyable(_Tp)
10082        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10083      with start == caret, finishing at the close-paren.  */
10084   location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10085
10086   /* Complete the trait expression, which may mean either processing
10087      the trait expr now or saving it for template instantiation.  */
10088   switch (kind)
10089     {
10090     case CPTK_UNDERLYING_TYPE:
10091       return cp_expr (finish_underlying_type (type1), trait_loc);
10092     case CPTK_BASES:
10093       return cp_expr (finish_bases (type1, false), trait_loc);
10094     case CPTK_DIRECT_BASES:
10095       return cp_expr (finish_bases (type1, true), trait_loc);
10096     default:
10097       return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10098     }
10099 }
10100
10101 /* Parse a lambda expression.
10102
10103    lambda-expression:
10104      lambda-introducer lambda-declarator [opt] compound-statement
10105
10106    Returns a representation of the expression.  */
10107
10108 static cp_expr
10109 cp_parser_lambda_expression (cp_parser* parser)
10110 {
10111   tree lambda_expr = build_lambda_expr ();
10112   tree type;
10113   bool ok = true;
10114   cp_token *token = cp_lexer_peek_token (parser->lexer);
10115   cp_token_position start = 0;
10116
10117   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10118
10119   if (cp_unevaluated_operand)
10120     {
10121       if (!token->error_reported)
10122         {
10123           error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10124                     "lambda-expression in unevaluated context");
10125           token->error_reported = true;
10126         }
10127       ok = false;
10128     }
10129   else if (parser->in_template_argument_list_p)
10130     {
10131       if (!token->error_reported)
10132         {
10133           error_at (token->location, "lambda-expression in template-argument");
10134           token->error_reported = true;
10135         }
10136       ok = false;
10137     }
10138
10139   /* We may be in the middle of deferred access check.  Disable
10140      it now.  */
10141   push_deferring_access_checks (dk_no_deferred);
10142
10143   cp_parser_lambda_introducer (parser, lambda_expr);
10144
10145   type = begin_lambda_type (lambda_expr);
10146   if (type == error_mark_node)
10147     return error_mark_node;
10148
10149   record_lambda_scope (lambda_expr);
10150
10151   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
10152   determine_visibility (TYPE_NAME (type));
10153
10154   /* Now that we've started the type, add the capture fields for any
10155      explicit captures.  */
10156   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10157
10158   {
10159     /* Inside the class, surrounding template-parameter-lists do not apply.  */
10160     unsigned int saved_num_template_parameter_lists
10161         = parser->num_template_parameter_lists;
10162     unsigned char in_statement = parser->in_statement;
10163     bool in_switch_statement_p = parser->in_switch_statement_p;
10164     bool fully_implicit_function_template_p
10165         = parser->fully_implicit_function_template_p;
10166     tree implicit_template_parms = parser->implicit_template_parms;
10167     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10168     bool auto_is_implicit_function_template_parm_p
10169         = parser->auto_is_implicit_function_template_parm_p;
10170
10171     parser->num_template_parameter_lists = 0;
10172     parser->in_statement = 0;
10173     parser->in_switch_statement_p = false;
10174     parser->fully_implicit_function_template_p = false;
10175     parser->implicit_template_parms = 0;
10176     parser->implicit_template_scope = 0;
10177     parser->auto_is_implicit_function_template_parm_p = false;
10178
10179     /* By virtue of defining a local class, a lambda expression has access to
10180        the private variables of enclosing classes.  */
10181
10182     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10183
10184     if (ok && cp_parser_error_occurred (parser))
10185       ok = false;
10186
10187     if (ok)
10188       {
10189         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10190             && cp_parser_start_tentative_firewall (parser))
10191           start = token;
10192         cp_parser_lambda_body (parser, lambda_expr);
10193       }
10194     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10195       {
10196         if (cp_parser_skip_to_closing_brace (parser))
10197           cp_lexer_consume_token (parser->lexer);
10198       }
10199
10200     /* The capture list was built up in reverse order; fix that now.  */
10201     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10202       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10203
10204     if (ok)
10205       maybe_add_lambda_conv_op (type);
10206
10207     type = finish_struct (type, /*attributes=*/NULL_TREE);
10208
10209     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10210     parser->in_statement = in_statement;
10211     parser->in_switch_statement_p = in_switch_statement_p;
10212     parser->fully_implicit_function_template_p
10213         = fully_implicit_function_template_p;
10214     parser->implicit_template_parms = implicit_template_parms;
10215     parser->implicit_template_scope = implicit_template_scope;
10216     parser->auto_is_implicit_function_template_parm_p
10217         = auto_is_implicit_function_template_parm_p;
10218   }
10219
10220   /* This field is only used during parsing of the lambda.  */
10221   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10222
10223   /* This lambda shouldn't have any proxies left at this point.  */
10224   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10225   /* And now that we're done, push proxies for an enclosing lambda.  */
10226   insert_pending_capture_proxies ();
10227
10228   if (ok)
10229     lambda_expr = build_lambda_object (lambda_expr);
10230   else
10231     lambda_expr = error_mark_node;
10232
10233   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10234
10235   pop_deferring_access_checks ();
10236
10237   return lambda_expr;
10238 }
10239
10240 /* Parse the beginning of a lambda expression.
10241
10242    lambda-introducer:
10243      [ lambda-capture [opt] ]
10244
10245    LAMBDA_EXPR is the current representation of the lambda expression.  */
10246
10247 static void
10248 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10249 {
10250   /* Need commas after the first capture.  */
10251   bool first = true;
10252
10253   /* Eat the leading `['.  */
10254   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10255
10256   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
10257   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10258       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10259     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10260   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10261     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10262
10263   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10264     {
10265       cp_lexer_consume_token (parser->lexer);
10266       first = false;
10267     }
10268
10269   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10270     {
10271       cp_token* capture_token;
10272       tree capture_id;
10273       tree capture_init_expr;
10274       cp_id_kind idk = CP_ID_KIND_NONE;
10275       bool explicit_init_p = false;
10276
10277       enum capture_kind_type
10278       {
10279         BY_COPY,
10280         BY_REFERENCE
10281       };
10282       enum capture_kind_type capture_kind = BY_COPY;
10283
10284       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10285         {
10286           error ("expected end of capture-list");
10287           return;
10288         }
10289
10290       if (first)
10291         first = false;
10292       else
10293         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10294
10295       /* Possibly capture `this'.  */
10296       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10297         {
10298           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10299           if (cxx_dialect < cxx2a
10300               && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10301             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10302                      "with by-copy capture default");
10303           cp_lexer_consume_token (parser->lexer);
10304           add_capture (lambda_expr,
10305                        /*id=*/this_identifier,
10306                        /*initializer=*/finish_this_expr (),
10307                        /*by_reference_p=*/true,
10308                        explicit_init_p);
10309           continue;
10310         }
10311
10312       /* Possibly capture `*this'.  */
10313       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10314           && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10315         {
10316           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10317           if (cxx_dialect < cxx17)
10318             pedwarn (loc, 0, "%<*this%> capture only available with "
10319                              "-std=c++17 or -std=gnu++17");
10320           cp_lexer_consume_token (parser->lexer);
10321           cp_lexer_consume_token (parser->lexer);
10322           add_capture (lambda_expr,
10323                        /*id=*/this_identifier,
10324                        /*initializer=*/finish_this_expr (),
10325                        /*by_reference_p=*/false,
10326                        explicit_init_p);
10327           continue;
10328         }
10329
10330       /* Remember whether we want to capture as a reference or not.  */
10331       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10332         {
10333           capture_kind = BY_REFERENCE;
10334           cp_lexer_consume_token (parser->lexer);
10335         }
10336
10337       /* Get the identifier.  */
10338       capture_token = cp_lexer_peek_token (parser->lexer);
10339       capture_id = cp_parser_identifier (parser);
10340
10341       if (capture_id == error_mark_node)
10342         /* Would be nice to have a cp_parser_skip_to_closing_x for general
10343            delimiters, but I modified this to stop on unnested ']' as well.  It
10344            was already changed to stop on unnested '}', so the
10345            "closing_parenthesis" name is no more misleading with my change.  */
10346         {
10347           cp_parser_skip_to_closing_parenthesis (parser,
10348                                                  /*recovering=*/true,
10349                                                  /*or_comma=*/true,
10350                                                  /*consume_paren=*/true);
10351           break;
10352         }
10353
10354       /* Find the initializer for this capture.  */
10355       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10356           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10357           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10358         {
10359           bool direct, non_constant;
10360           /* An explicit initializer exists.  */
10361           if (cxx_dialect < cxx14)
10362             pedwarn (input_location, 0,
10363                      "lambda capture initializers "
10364                      "only available with -std=c++14 or -std=gnu++14");
10365           capture_init_expr = cp_parser_initializer (parser, &direct,
10366                                                      &non_constant, true);
10367           explicit_init_p = true;
10368           if (capture_init_expr == NULL_TREE)
10369             {
10370               error ("empty initializer for lambda init-capture");
10371               capture_init_expr = error_mark_node;
10372             }
10373         }
10374       else
10375         {
10376           const char* error_msg;
10377
10378           /* Turn the identifier into an id-expression.  */
10379           capture_init_expr
10380             = cp_parser_lookup_name_simple (parser, capture_id,
10381                                             capture_token->location);
10382
10383           if (capture_init_expr == error_mark_node)
10384             {
10385               unqualified_name_lookup_error (capture_id);
10386               continue;
10387             }
10388           else if (!VAR_P (capture_init_expr)
10389                    && TREE_CODE (capture_init_expr) != PARM_DECL)
10390             {
10391               error_at (capture_token->location,
10392                         "capture of non-variable %qE",
10393                         capture_init_expr);
10394               if (DECL_P (capture_init_expr))
10395                 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10396                         "%q#D declared here", capture_init_expr);
10397               continue;
10398             }
10399           if (VAR_P (capture_init_expr)
10400               && decl_storage_duration (capture_init_expr) != dk_auto)
10401             {
10402               if (pedwarn (capture_token->location, 0, "capture of variable "
10403                            "%qD with non-automatic storage duration",
10404                            capture_init_expr))
10405                 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10406                         "%q#D declared here", capture_init_expr);
10407               continue;
10408             }
10409
10410           capture_init_expr
10411             = finish_id_expression
10412                 (capture_id,
10413                  capture_init_expr,
10414                  parser->scope,
10415                  &idk,
10416                  /*integral_constant_expression_p=*/false,
10417                  /*allow_non_integral_constant_expression_p=*/false,
10418                  /*non_integral_constant_expression_p=*/NULL,
10419                  /*template_p=*/false,
10420                  /*done=*/true,
10421                  /*address_p=*/false,
10422                  /*template_arg_p=*/false,
10423                  &error_msg,
10424                  capture_token->location);
10425
10426           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10427             {
10428               cp_lexer_consume_token (parser->lexer);
10429               capture_init_expr = make_pack_expansion (capture_init_expr);
10430             }
10431         }
10432
10433       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10434           && !explicit_init_p)
10435         {
10436           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10437               && capture_kind == BY_COPY)
10438             pedwarn (capture_token->location, 0, "explicit by-copy capture "
10439                      "of %qD redundant with by-copy capture default",
10440                      capture_id);
10441           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10442               && capture_kind == BY_REFERENCE)
10443             pedwarn (capture_token->location, 0, "explicit by-reference "
10444                      "capture of %qD redundant with by-reference capture "
10445                      "default", capture_id);
10446         }
10447
10448       add_capture (lambda_expr,
10449                    capture_id,
10450                    capture_init_expr,
10451                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
10452                    explicit_init_p);
10453
10454       /* If there is any qualification still in effect, clear it
10455          now; we will be starting fresh with the next capture.  */
10456       parser->scope = NULL_TREE;
10457       parser->qualifying_scope = NULL_TREE;
10458       parser->object_scope = NULL_TREE;
10459     }
10460
10461   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10462 }
10463
10464 /* Parse the (optional) middle of a lambda expression.
10465
10466    lambda-declarator:
10467      < template-parameter-list [opt] >
10468      ( parameter-declaration-clause [opt] )
10469        attribute-specifier [opt]
10470        decl-specifier-seq [opt]
10471        exception-specification [opt]
10472        lambda-return-type-clause [opt]
10473
10474    LAMBDA_EXPR is the current representation of the lambda expression.  */
10475
10476 static bool
10477 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10478 {
10479   /* 5.1.1.4 of the standard says:
10480        If a lambda-expression does not include a lambda-declarator, it is as if
10481        the lambda-declarator were ().
10482      This means an empty parameter list, no attributes, and no exception
10483      specification.  */
10484   tree param_list = void_list_node;
10485   tree attributes = NULL_TREE;
10486   tree exception_spec = NULL_TREE;
10487   tree template_param_list = NULL_TREE;
10488   tree tx_qual = NULL_TREE;
10489   tree return_type = NULL_TREE;
10490   cp_decl_specifier_seq lambda_specs;
10491   clear_decl_specs (&lambda_specs);
10492
10493   /* The template-parameter-list is optional, but must begin with
10494      an opening angle if present.  */
10495   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10496     {
10497       if (cxx_dialect < cxx14)
10498         pedwarn (parser->lexer->next_token->location, 0,
10499                  "lambda templates are only available with "
10500                  "-std=c++14 or -std=gnu++14");
10501       else if (cxx_dialect < cxx2a)
10502         pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10503                  "lambda templates are only available with "
10504                  "-std=c++2a or -std=gnu++2a");
10505
10506       cp_lexer_consume_token (parser->lexer);
10507
10508       template_param_list = cp_parser_template_parameter_list (parser);
10509
10510       cp_parser_skip_to_end_of_template_parameter_list (parser);
10511
10512       /* We just processed one more parameter list.  */
10513       ++parser->num_template_parameter_lists;
10514     }
10515
10516   /* The parameter-declaration-clause is optional (unless
10517      template-parameter-list was given), but must begin with an
10518      opening parenthesis if present.  */
10519   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10520     {
10521       matching_parens parens;
10522       parens.consume_open (parser);
10523
10524       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10525
10526       /* Parse parameters.  */
10527       param_list = cp_parser_parameter_declaration_clause (parser);
10528
10529       /* Default arguments shall not be specified in the
10530          parameter-declaration-clause of a lambda-declarator.  */
10531       if (cxx_dialect < cxx14)
10532         for (tree t = param_list; t; t = TREE_CHAIN (t))
10533           if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10534             pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10535                      "default argument specified for lambda parameter");
10536
10537       parens.require_close (parser);
10538
10539       attributes = cp_parser_attributes_opt (parser);
10540
10541       /* In the decl-specifier-seq of the lambda-declarator, each
10542          decl-specifier shall either be mutable or constexpr.  */
10543       int declares_class_or_enum;
10544       if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10545         cp_parser_decl_specifier_seq (parser,
10546                                       CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10547                                       &lambda_specs, &declares_class_or_enum);
10548       if (lambda_specs.storage_class == sc_mutable)
10549         {
10550           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10551           if (lambda_specs.conflicting_specifiers_p)
10552             error_at (lambda_specs.locations[ds_storage_class],
10553                       "duplicate %<mutable%>");
10554         }
10555
10556       tx_qual = cp_parser_tx_qualifier_opt (parser);
10557
10558       /* Parse optional exception specification.  */
10559       exception_spec = cp_parser_exception_specification_opt (parser);
10560
10561       /* Parse optional trailing return type.  */
10562       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10563         {
10564           cp_lexer_consume_token (parser->lexer);
10565           return_type = cp_parser_trailing_type_id (parser);
10566         }
10567
10568       /* The function parameters must be in scope all the way until after the
10569          trailing-return-type in case of decltype.  */
10570       pop_bindings_and_leave_scope ();
10571     }
10572   else if (template_param_list != NULL_TREE) // generate diagnostic
10573     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10574
10575   /* Create the function call operator.
10576
10577      Messing with declarators like this is no uglier than building up the
10578      FUNCTION_DECL by hand, and this is less likely to get out of sync with
10579      other code.  */
10580   {
10581     cp_decl_specifier_seq return_type_specs;
10582     cp_declarator* declarator;
10583     tree fco;
10584     int quals;
10585     void *p;
10586
10587     clear_decl_specs (&return_type_specs);
10588     if (return_type)
10589       return_type_specs.type = return_type;
10590     else
10591       /* Maybe we will deduce the return type later.  */
10592       return_type_specs.type = make_auto ();
10593
10594     if (lambda_specs.locations[ds_constexpr])
10595       {
10596         if (cxx_dialect >= cxx17)
10597           return_type_specs.locations[ds_constexpr]
10598             = lambda_specs.locations[ds_constexpr];
10599         else
10600           error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10601                     "lambda only available with -std=c++17 or -std=gnu++17");
10602       }
10603
10604     p = obstack_alloc (&declarator_obstack, 0);
10605
10606     declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10607
10608     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10609              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10610     declarator = make_call_declarator (declarator, param_list, quals,
10611                                        VIRT_SPEC_UNSPECIFIED,
10612                                        REF_QUAL_NONE,
10613                                        tx_qual,
10614                                        exception_spec,
10615                                        /*late_return_type=*/NULL_TREE,
10616                                        /*requires_clause*/NULL_TREE);
10617     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10618
10619     fco = grokmethod (&return_type_specs,
10620                       declarator,
10621                       attributes);
10622     if (fco != error_mark_node)
10623       {
10624         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10625         DECL_ARTIFICIAL (fco) = 1;
10626         /* Give the object parameter a different name.  */
10627         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10628         DECL_LAMBDA_FUNCTION (fco) = 1;
10629         if (return_type)
10630           TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10631       }
10632     if (template_param_list)
10633       {
10634         fco = finish_member_template_decl (fco);
10635         finish_template_decl (template_param_list);
10636         --parser->num_template_parameter_lists;
10637       }
10638     else if (parser->fully_implicit_function_template_p)
10639       fco = finish_fully_implicit_template (parser, fco);
10640
10641     finish_member_declaration (fco);
10642
10643     obstack_free (&declarator_obstack, p);
10644
10645     return (fco != error_mark_node);
10646   }
10647 }
10648
10649 /* Parse the body of a lambda expression, which is simply
10650
10651    compound-statement
10652
10653    but which requires special handling.
10654    LAMBDA_EXPR is the current representation of the lambda expression.  */
10655
10656 static void
10657 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10658 {
10659   bool nested = (current_function_decl != NULL_TREE);
10660   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10661   bool in_function_body = parser->in_function_body;
10662
10663   if (nested)
10664     push_function_context ();
10665   else
10666     /* Still increment function_depth so that we don't GC in the
10667        middle of an expression.  */
10668     ++function_depth;
10669
10670   vec<tree> omp_privatization_save;
10671   save_omp_privatization_clauses (omp_privatization_save);
10672   /* Clear this in case we're in the middle of a default argument.  */
10673   parser->local_variables_forbidden_p = false;
10674   parser->in_function_body = true;
10675
10676   {
10677     local_specialization_stack s (lss_copy);
10678     tree fco = lambda_function (lambda_expr);
10679     tree body = start_lambda_function (fco, lambda_expr);
10680     matching_braces braces;
10681
10682     if (braces.require_open (parser))
10683       {
10684         tree compound_stmt = begin_compound_stmt (0);
10685
10686         /* Originally C++11 required us to peek for 'return expr'; and
10687            process it specially here to deduce the return type.  N3638
10688            removed the need for that.  */
10689
10690         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10691           cp_parser_label_declaration (parser);
10692         cp_parser_statement_seq_opt (parser, NULL_TREE);
10693         braces.require_close (parser);
10694
10695         finish_compound_stmt (compound_stmt);
10696       }
10697
10698     finish_lambda_function (body);
10699   }
10700
10701   restore_omp_privatization_clauses (omp_privatization_save);
10702   parser->local_variables_forbidden_p = local_variables_forbidden_p;
10703   parser->in_function_body = in_function_body;
10704   if (nested)
10705     pop_function_context();
10706   else
10707     --function_depth;
10708 }
10709
10710 /* Statements [gram.stmt.stmt]  */
10711
10712 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC.  */
10713
10714 static void
10715 add_debug_begin_stmt (location_t loc)
10716 {
10717   if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10718     return;
10719   if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10720     /* A concept is never expanded normally.  */
10721     return;
10722
10723   tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10724   SET_EXPR_LOCATION (stmt, loc);
10725   add_stmt (stmt);
10726 }
10727
10728 /* Parse a statement.
10729
10730    statement:
10731      labeled-statement
10732      expression-statement
10733      compound-statement
10734      selection-statement
10735      iteration-statement
10736      jump-statement
10737      declaration-statement
10738      try-block
10739
10740   C++11:
10741
10742   statement:
10743     labeled-statement
10744     attribute-specifier-seq (opt) expression-statement
10745     attribute-specifier-seq (opt) compound-statement
10746     attribute-specifier-seq (opt) selection-statement
10747     attribute-specifier-seq (opt) iteration-statement
10748     attribute-specifier-seq (opt) jump-statement
10749     declaration-statement
10750     attribute-specifier-seq (opt) try-block
10751
10752   init-statement:
10753     expression-statement
10754     simple-declaration
10755
10756   TM Extension:
10757
10758    statement:
10759      atomic-statement
10760
10761   IN_COMPOUND is true when the statement is nested inside a
10762   cp_parser_compound_statement; this matters for certain pragmas.
10763
10764   If IF_P is not NULL, *IF_P is set to indicate whether the statement
10765   is a (possibly labeled) if statement which is not enclosed in braces
10766   and has an else clause.  This is used to implement -Wparentheses.
10767
10768   CHAIN is a vector of if-else-if conditions.  */
10769
10770 static void
10771 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10772                      bool in_compound, bool *if_p, vec<tree> *chain,
10773                      location_t *loc_after_labels)
10774 {
10775   tree statement, std_attrs = NULL_TREE;
10776   cp_token *token;
10777   location_t statement_location, attrs_location;
10778
10779  restart:
10780   if (if_p != NULL)
10781     *if_p = false;
10782   /* There is no statement yet.  */
10783   statement = NULL_TREE;
10784
10785   saved_token_sentinel saved_tokens (parser->lexer);
10786   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10787   if (c_dialect_objc ())
10788     /* In obj-c++, seeing '[[' might be the either the beginning of
10789        c++11 attributes, or a nested objc-message-expression.  So
10790        let's parse the c++11 attributes tentatively.  */
10791     cp_parser_parse_tentatively (parser);
10792   std_attrs = cp_parser_std_attribute_spec_seq (parser);
10793   if (c_dialect_objc ())
10794     {
10795       if (!cp_parser_parse_definitely (parser))
10796         std_attrs = NULL_TREE;
10797     }
10798
10799   /* Peek at the next token.  */
10800   token = cp_lexer_peek_token (parser->lexer);
10801   /* Remember the location of the first token in the statement.  */
10802   statement_location = token->location;
10803   add_debug_begin_stmt (statement_location);
10804   /* If this is a keyword, then that will often determine what kind of
10805      statement we have.  */
10806   if (token->type == CPP_KEYWORD)
10807     {
10808       enum rid keyword = token->keyword;
10809
10810       switch (keyword)
10811         {
10812         case RID_CASE:
10813         case RID_DEFAULT:
10814           /* Looks like a labeled-statement with a case label.
10815              Parse the label, and then use tail recursion to parse
10816              the statement.  */
10817           cp_parser_label_for_labeled_statement (parser, std_attrs);
10818           in_compound = false;
10819           goto restart;
10820
10821         case RID_IF:
10822         case RID_SWITCH:
10823           statement = cp_parser_selection_statement (parser, if_p, chain);
10824           break;
10825
10826         case RID_WHILE:
10827         case RID_DO:
10828         case RID_FOR:
10829           statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10830           break;
10831
10832         case RID_BREAK:
10833         case RID_CONTINUE:
10834         case RID_RETURN:
10835         case RID_GOTO:
10836           statement = cp_parser_jump_statement (parser);
10837           break;
10838
10839           /* Objective-C++ exception-handling constructs.  */
10840         case RID_AT_TRY:
10841         case RID_AT_CATCH:
10842         case RID_AT_FINALLY:
10843         case RID_AT_SYNCHRONIZED:
10844         case RID_AT_THROW:
10845           statement = cp_parser_objc_statement (parser);
10846           break;
10847
10848         case RID_TRY:
10849           statement = cp_parser_try_block (parser);
10850           break;
10851
10852         case RID_NAMESPACE:
10853           /* This must be a namespace alias definition.  */
10854           cp_parser_declaration_statement (parser);
10855           return;
10856           
10857         case RID_TRANSACTION_ATOMIC:
10858         case RID_TRANSACTION_RELAXED:
10859         case RID_SYNCHRONIZED:
10860         case RID_ATOMIC_NOEXCEPT:
10861         case RID_ATOMIC_CANCEL:
10862           statement = cp_parser_transaction (parser, token);
10863           break;
10864         case RID_TRANSACTION_CANCEL:
10865           statement = cp_parser_transaction_cancel (parser);
10866           break;
10867
10868         default:
10869           /* It might be a keyword like `int' that can start a
10870              declaration-statement.  */
10871           break;
10872         }
10873     }
10874   else if (token->type == CPP_NAME)
10875     {
10876       /* If the next token is a `:', then we are looking at a
10877          labeled-statement.  */
10878       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10879       if (token->type == CPP_COLON)
10880         {
10881           /* Looks like a labeled-statement with an ordinary label.
10882              Parse the label, and then use tail recursion to parse
10883              the statement.  */
10884
10885           cp_parser_label_for_labeled_statement (parser, std_attrs);
10886           in_compound = false;
10887           goto restart;
10888         }
10889     }
10890   /* Anything that starts with a `{' must be a compound-statement.  */
10891   else if (token->type == CPP_OPEN_BRACE)
10892     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10893   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10894      a statement all its own.  */
10895   else if (token->type == CPP_PRAGMA)
10896     {
10897       /* Only certain OpenMP pragmas are attached to statements, and thus
10898          are considered statements themselves.  All others are not.  In
10899          the context of a compound, accept the pragma as a "statement" and
10900          return so that we can check for a close brace.  Otherwise we
10901          require a real statement and must go back and read one.  */
10902       if (in_compound)
10903         cp_parser_pragma (parser, pragma_compound, if_p);
10904       else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10905         goto restart;
10906       return;
10907     }
10908   else if (token->type == CPP_EOF)
10909     {
10910       cp_parser_error (parser, "expected statement");
10911       return;
10912     }
10913
10914   /* Everything else must be a declaration-statement or an
10915      expression-statement.  Try for the declaration-statement
10916      first, unless we are looking at a `;', in which case we know that
10917      we have an expression-statement.  */
10918   if (!statement)
10919     {
10920       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10921         {
10922           if (std_attrs != NULL_TREE)
10923             {
10924               /*  Attributes should be parsed as part of the the
10925                   declaration, so let's un-parse them.  */
10926               saved_tokens.rollback();
10927               std_attrs = NULL_TREE;
10928             }
10929
10930           cp_parser_parse_tentatively (parser);
10931           /* Try to parse the declaration-statement.  */
10932           cp_parser_declaration_statement (parser);
10933           /* If that worked, we're done.  */
10934           if (cp_parser_parse_definitely (parser))
10935             return;
10936         }
10937       /* All preceding labels have been parsed at this point.  */
10938       if (loc_after_labels != NULL)
10939         *loc_after_labels = statement_location;
10940
10941       /* Look for an expression-statement instead.  */
10942       statement = cp_parser_expression_statement (parser, in_statement_expr);
10943
10944       /* Handle [[fallthrough]];.  */
10945       if (attribute_fallthrough_p (std_attrs))
10946         {
10947           /* The next token after the fallthrough attribute is ';'.  */
10948           if (statement == NULL_TREE)
10949             {
10950               /* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
10951               statement = build_call_expr_internal_loc (statement_location,
10952                                                         IFN_FALLTHROUGH,
10953                                                         void_type_node, 0);
10954               finish_expr_stmt (statement);
10955             }
10956           else
10957             warning_at (statement_location, OPT_Wattributes,
10958                         "%<fallthrough%> attribute not followed by %<;%>");
10959           std_attrs = NULL_TREE;
10960         }
10961     }
10962
10963   /* Set the line number for the statement.  */
10964   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10965     SET_EXPR_LOCATION (statement, statement_location);
10966
10967   /* Allow "[[fallthrough]];", but warn otherwise.  */
10968   if (std_attrs != NULL_TREE)
10969     warning_at (attrs_location,
10970                 OPT_Wattributes,
10971                 "attributes at the beginning of statement are ignored");
10972 }
10973
10974 /* Append ATTR to attribute list ATTRS.  */
10975
10976 static tree
10977 attr_chainon (tree attrs, tree attr)
10978 {
10979   if (attrs == error_mark_node)
10980     return error_mark_node;
10981   if (attr == error_mark_node)
10982     return error_mark_node;
10983   return chainon (attrs, attr);
10984 }
10985
10986 /* Parse the label for a labeled-statement, i.e.
10987
10988    identifier :
10989    case constant-expression :
10990    default :
10991
10992    GNU Extension:
10993    case constant-expression ... constant-expression : statement
10994
10995    When a label is parsed without errors, the label is added to the
10996    parse tree by the finish_* functions, so this function doesn't
10997    have to return the label.  */
10998
10999 static void
11000 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11001 {
11002   cp_token *token;
11003   tree label = NULL_TREE;
11004   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11005
11006   /* The next token should be an identifier.  */
11007   token = cp_lexer_peek_token (parser->lexer);
11008   if (token->type != CPP_NAME
11009       && token->type != CPP_KEYWORD)
11010     {
11011       cp_parser_error (parser, "expected labeled-statement");
11012       return;
11013     }
11014
11015   /* Remember whether this case or a user-defined label is allowed to fall
11016      through to.  */
11017   bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11018
11019   parser->colon_corrects_to_scope_p = false;
11020   switch (token->keyword)
11021     {
11022     case RID_CASE:
11023       {
11024         tree expr, expr_hi;
11025         cp_token *ellipsis;
11026
11027         /* Consume the `case' token.  */
11028         cp_lexer_consume_token (parser->lexer);
11029         /* Parse the constant-expression.  */
11030         expr = cp_parser_constant_expression (parser);
11031         if (check_for_bare_parameter_packs (expr))
11032           expr = error_mark_node;
11033
11034         ellipsis = cp_lexer_peek_token (parser->lexer);
11035         if (ellipsis->type == CPP_ELLIPSIS)
11036           {
11037             /* Consume the `...' token.  */
11038             cp_lexer_consume_token (parser->lexer);
11039             expr_hi = cp_parser_constant_expression (parser);
11040             if (check_for_bare_parameter_packs (expr_hi))
11041               expr_hi = error_mark_node;
11042
11043             /* We don't need to emit warnings here, as the common code
11044                will do this for us.  */
11045           }
11046         else
11047           expr_hi = NULL_TREE;
11048
11049         if (parser->in_switch_statement_p)
11050           {
11051             tree l = finish_case_label (token->location, expr, expr_hi);
11052             if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11053               FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11054           }
11055         else
11056           error_at (token->location,
11057                     "case label %qE not within a switch statement",
11058                     expr);
11059       }
11060       break;
11061
11062     case RID_DEFAULT:
11063       /* Consume the `default' token.  */
11064       cp_lexer_consume_token (parser->lexer);
11065
11066       if (parser->in_switch_statement_p)
11067         {
11068           tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11069           if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11070             FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11071         }
11072       else
11073         error_at (token->location, "case label not within a switch statement");
11074       break;
11075
11076     default:
11077       /* Anything else must be an ordinary label.  */
11078       label = finish_label_stmt (cp_parser_identifier (parser));
11079       if (label && TREE_CODE (label) == LABEL_DECL)
11080         FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11081       break;
11082     }
11083
11084   /* Require the `:' token.  */
11085   cp_parser_require (parser, CPP_COLON, RT_COLON);
11086
11087   /* An ordinary label may optionally be followed by attributes.
11088      However, this is only permitted if the attributes are then
11089      followed by a semicolon.  This is because, for backward
11090      compatibility, when parsing
11091        lab: __attribute__ ((unused)) int i;
11092      we want the attribute to attach to "i", not "lab".  */
11093   if (label != NULL_TREE
11094       && cp_next_tokens_can_be_gnu_attribute_p (parser))
11095     {
11096       tree attrs;
11097       cp_parser_parse_tentatively (parser);
11098       attrs = cp_parser_gnu_attributes_opt (parser);
11099       if (attrs == NULL_TREE
11100           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11101         cp_parser_abort_tentative_parse (parser);
11102       else if (!cp_parser_parse_definitely (parser))
11103         ;
11104       else
11105         attributes = attr_chainon (attributes, attrs);
11106     }
11107
11108   if (attributes != NULL_TREE)
11109     cplus_decl_attributes (&label, attributes, 0);
11110
11111   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11112 }
11113
11114 /* Parse an expression-statement.
11115
11116    expression-statement:
11117      expression [opt] ;
11118
11119    Returns the new EXPR_STMT -- or NULL_TREE if the expression
11120    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11121    indicates whether this expression-statement is part of an
11122    expression statement.  */
11123
11124 static tree
11125 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11126 {
11127   tree statement = NULL_TREE;
11128   cp_token *token = cp_lexer_peek_token (parser->lexer);
11129   location_t loc = token->location;
11130
11131   /* There might be attribute fallthrough.  */
11132   tree attr = cp_parser_gnu_attributes_opt (parser);
11133
11134   /* If the next token is a ';', then there is no expression
11135      statement.  */
11136   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11137     {
11138       statement = cp_parser_expression (parser);
11139       if (statement == error_mark_node
11140           && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11141         {
11142           cp_parser_skip_to_end_of_block_or_statement (parser);
11143           return error_mark_node;
11144         }
11145     }
11146
11147   /* Handle [[fallthrough]];.  */
11148   if (attribute_fallthrough_p (attr))
11149     {
11150       /* The next token after the fallthrough attribute is ';'.  */
11151       if (statement == NULL_TREE)
11152         /* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
11153         statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11154                                                   void_type_node, 0);
11155       else
11156         warning_at (loc, OPT_Wattributes,
11157                     "%<fallthrough%> attribute not followed by %<;%>");
11158       attr = NULL_TREE;
11159     }
11160
11161   /* Allow "[[fallthrough]];", but warn otherwise.  */
11162   if (attr != NULL_TREE)
11163     warning_at (loc, OPT_Wattributes,
11164                 "attributes at the beginning of statement are ignored");
11165
11166   /* Give a helpful message for "A<T>::type t;" and the like.  */
11167   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11168       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11169     {
11170       if (TREE_CODE (statement) == SCOPE_REF)
11171         error_at (token->location, "need %<typename%> before %qE because "
11172                   "%qT is a dependent scope",
11173                   statement, TREE_OPERAND (statement, 0));
11174       else if (is_overloaded_fn (statement)
11175                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11176         {
11177           /* A::A a; */
11178           tree fn = get_first_fn (statement);
11179           error_at (token->location,
11180                     "%<%T::%D%> names the constructor, not the type",
11181                     DECL_CONTEXT (fn), DECL_NAME (fn));
11182         }
11183     }
11184
11185   /* Consume the final `;'.  */
11186   cp_parser_consume_semicolon_at_end_of_statement (parser);
11187
11188   if (in_statement_expr
11189       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11190     /* This is the final expression statement of a statement
11191        expression.  */
11192     statement = finish_stmt_expr_expr (statement, in_statement_expr);
11193   else if (statement)
11194     statement = finish_expr_stmt (statement);
11195
11196   return statement;
11197 }
11198
11199 /* Parse a compound-statement.
11200
11201    compound-statement:
11202      { statement-seq [opt] }
11203
11204    GNU extension:
11205
11206    compound-statement:
11207      { label-declaration-seq [opt] statement-seq [opt] }
11208
11209    label-declaration-seq:
11210      label-declaration
11211      label-declaration-seq label-declaration
11212
11213    Returns a tree representing the statement.  */
11214
11215 static tree
11216 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11217                               int bcs_flags, bool function_body)
11218 {
11219   tree compound_stmt;
11220   matching_braces braces;
11221
11222   /* Consume the `{'.  */
11223   if (!braces.require_open (parser))
11224     return error_mark_node;
11225   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11226       && !function_body && cxx_dialect < cxx14)
11227     pedwarn (input_location, OPT_Wpedantic,
11228              "compound-statement in %<constexpr%> function");
11229   /* Begin the compound-statement.  */
11230   compound_stmt = begin_compound_stmt (bcs_flags);
11231   /* If the next keyword is `__label__' we have a label declaration.  */
11232   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11233     cp_parser_label_declaration (parser);
11234   /* Parse an (optional) statement-seq.  */
11235   cp_parser_statement_seq_opt (parser, in_statement_expr);
11236   /* Finish the compound-statement.  */
11237   finish_compound_stmt (compound_stmt);
11238   /* Consume the `}'.  */
11239   braces.require_close (parser);
11240
11241   return compound_stmt;
11242 }
11243
11244 /* Parse an (optional) statement-seq.
11245
11246    statement-seq:
11247      statement
11248      statement-seq [opt] statement  */
11249
11250 static void
11251 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11252 {
11253   /* Scan statements until there aren't any more.  */
11254   while (true)
11255     {
11256       cp_token *token = cp_lexer_peek_token (parser->lexer);
11257
11258       /* If we are looking at a `}', then we have run out of
11259          statements; the same is true if we have reached the end
11260          of file, or have stumbled upon a stray '@end'.  */
11261       if (token->type == CPP_CLOSE_BRACE
11262           || token->type == CPP_EOF
11263           || token->type == CPP_PRAGMA_EOL
11264           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11265         break;
11266       
11267       /* If we are in a compound statement and find 'else' then
11268          something went wrong.  */
11269       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11270         {
11271           if (parser->in_statement & IN_IF_STMT) 
11272             break;
11273           else
11274             {
11275               token = cp_lexer_consume_token (parser->lexer);
11276               error_at (token->location, "%<else%> without a previous %<if%>");
11277             }
11278         }
11279
11280       /* Parse the statement.  */
11281       cp_parser_statement (parser, in_statement_expr, true, NULL);
11282     }
11283 }
11284
11285 /* Return true if we're looking at (init; cond), false otherwise.  */
11286
11287 static bool
11288 cp_parser_init_statement_p (cp_parser *parser)
11289 {
11290   /* Save tokens so that we can put them back.  */
11291   cp_lexer_save_tokens (parser->lexer);
11292
11293   /* Look for ';' that is not nested in () or {}.  */
11294   int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11295                                                      /*recovering=*/false,
11296                                                      CPP_SEMICOLON,
11297                                                      /*consume_paren=*/false);
11298
11299   /* Roll back the tokens we skipped.  */
11300   cp_lexer_rollback_tokens (parser->lexer);
11301
11302   return ret == -1;
11303 }
11304
11305 /* Parse a selection-statement.
11306
11307    selection-statement:
11308      if ( init-statement [opt] condition ) statement
11309      if ( init-statement [opt] condition ) statement else statement
11310      switch ( init-statement [opt] condition ) statement
11311
11312    Returns the new IF_STMT or SWITCH_STMT.
11313
11314    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11315    is a (possibly labeled) if statement which is not enclosed in
11316    braces and has an else clause.  This is used to implement
11317    -Wparentheses.
11318
11319    CHAIN is a vector of if-else-if conditions.  This is used to implement
11320    -Wduplicated-cond.  */
11321
11322 static tree
11323 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11324                                vec<tree> *chain)
11325 {
11326   cp_token *token;
11327   enum rid keyword;
11328   token_indent_info guard_tinfo;
11329
11330   if (if_p != NULL)
11331     *if_p = false;
11332
11333   /* Peek at the next token.  */
11334   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11335   guard_tinfo = get_token_indent_info (token);
11336
11337   /* See what kind of keyword it is.  */
11338   keyword = token->keyword;
11339   switch (keyword)
11340     {
11341     case RID_IF:
11342     case RID_SWITCH:
11343       {
11344         tree statement;
11345         tree condition;
11346
11347         bool cx = false;
11348         if (keyword == RID_IF
11349             && cp_lexer_next_token_is_keyword (parser->lexer,
11350                                                RID_CONSTEXPR))
11351           {
11352             cx = true;
11353             cp_token *tok = cp_lexer_consume_token (parser->lexer);
11354             if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11355               pedwarn (tok->location, 0, "%<if constexpr%> only available "
11356                        "with -std=c++17 or -std=gnu++17");
11357           }
11358
11359         /* Look for the `('.  */
11360         matching_parens parens;
11361         if (!parens.require_open (parser))
11362           {
11363             cp_parser_skip_to_end_of_statement (parser);
11364             return error_mark_node;
11365           }
11366
11367         /* Begin the selection-statement.  */
11368         if (keyword == RID_IF)
11369           {
11370             statement = begin_if_stmt ();
11371             IF_STMT_CONSTEXPR_P (statement) = cx;
11372           }
11373         else
11374           statement = begin_switch_stmt ();
11375
11376         /* Parse the optional init-statement.  */
11377         if (cp_parser_init_statement_p (parser))
11378           {
11379             tree decl;
11380             if (cxx_dialect < cxx17)
11381               pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11382                        "init-statement in selection statements only available "
11383                        "with -std=c++17 or -std=gnu++17");
11384             cp_parser_init_statement (parser, &decl);
11385           }
11386
11387         /* Parse the condition.  */
11388         condition = cp_parser_condition (parser);
11389         /* Look for the `)'.  */
11390         if (!parens.require_close (parser))
11391           cp_parser_skip_to_closing_parenthesis (parser, true, false,
11392                                                  /*consume_paren=*/true);
11393
11394         if (keyword == RID_IF)
11395           {
11396             bool nested_if;
11397             unsigned char in_statement;
11398
11399             /* Add the condition.  */
11400             condition = finish_if_stmt_cond (condition, statement);
11401
11402             if (warn_duplicated_cond)
11403               warn_duplicated_cond_add_or_warn (token->location, condition,
11404                                                 &chain);
11405
11406             /* Parse the then-clause.  */
11407             in_statement = parser->in_statement;
11408             parser->in_statement |= IN_IF_STMT;
11409
11410             /* Outside a template, the non-selected branch of a constexpr
11411                if is a 'discarded statement', i.e. unevaluated.  */
11412             bool was_discarded = in_discarded_stmt;
11413             bool discard_then = (cx && !processing_template_decl
11414                                  && integer_zerop (condition));
11415             if (discard_then)
11416               {
11417                 in_discarded_stmt = true;
11418                 ++c_inhibit_evaluation_warnings;
11419               }
11420
11421             cp_parser_implicitly_scoped_statement (parser, &nested_if,
11422                                                    guard_tinfo);
11423
11424             parser->in_statement = in_statement;
11425
11426             finish_then_clause (statement);
11427
11428             if (discard_then)
11429               {
11430                 THEN_CLAUSE (statement) = NULL_TREE;
11431                 in_discarded_stmt = was_discarded;
11432                 --c_inhibit_evaluation_warnings;
11433               }
11434
11435             /* If the next token is `else', parse the else-clause.  */
11436             if (cp_lexer_next_token_is_keyword (parser->lexer,
11437                                                 RID_ELSE))
11438               {
11439                 bool discard_else = (cx && !processing_template_decl
11440                                      && integer_nonzerop (condition));
11441                 if (discard_else)
11442                   {
11443                     in_discarded_stmt = true;
11444                     ++c_inhibit_evaluation_warnings;
11445                   }
11446
11447                 guard_tinfo
11448                   = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11449                 /* Consume the `else' keyword.  */
11450                 cp_lexer_consume_token (parser->lexer);
11451                 if (warn_duplicated_cond)
11452                   {
11453                     if (cp_lexer_next_token_is_keyword (parser->lexer,
11454                                                         RID_IF)
11455                         && chain == NULL)
11456                       {
11457                         /* We've got "if (COND) else if (COND2)".  Start
11458                            the condition chain and add COND as the first
11459                            element.  */
11460                         chain = new vec<tree> ();
11461                         if (!CONSTANT_CLASS_P (condition)
11462                             && !TREE_SIDE_EFFECTS (condition))
11463                         {
11464                           /* Wrap it in a NOP_EXPR so that we can set the
11465                              location of the condition.  */
11466                           tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11467                                            condition);
11468                           SET_EXPR_LOCATION (e, token->location);
11469                           chain->safe_push (e);
11470                         }
11471                       }
11472                     else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11473                                                               RID_IF))
11474                       {
11475                         /* This is if-else without subsequent if.  Zap the
11476                            condition chain; we would have already warned at
11477                            this point.  */
11478                         delete chain;
11479                         chain = NULL;
11480                       }
11481                   }
11482                 begin_else_clause (statement);
11483                 /* Parse the else-clause.  */
11484                 cp_parser_implicitly_scoped_statement (parser, NULL,
11485                                                        guard_tinfo, chain);
11486
11487                 finish_else_clause (statement);
11488
11489                 /* If we are currently parsing a then-clause, then
11490                    IF_P will not be NULL.  We set it to true to
11491                    indicate that this if statement has an else clause.
11492                    This may trigger the Wparentheses warning below
11493                    when we get back up to the parent if statement.  */
11494                 if (if_p != NULL)
11495                   *if_p = true;
11496
11497                 if (discard_else)
11498                   {
11499                     ELSE_CLAUSE (statement) = NULL_TREE;
11500                     in_discarded_stmt = was_discarded;
11501                     --c_inhibit_evaluation_warnings;
11502                   }
11503               }
11504             else
11505               {
11506                 /* This if statement does not have an else clause.  If
11507                    NESTED_IF is true, then the then-clause has an if
11508                    statement which does have an else clause.  We warn
11509                    about the potential ambiguity.  */
11510                 if (nested_if)
11511                   warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11512                               "suggest explicit braces to avoid ambiguous"
11513                               " %<else%>");
11514                 if (warn_duplicated_cond)
11515                   {
11516                     /* We don't need the condition chain anymore.  */
11517                     delete chain;
11518                     chain = NULL;
11519                   }
11520               }
11521
11522             /* Now we're all done with the if-statement.  */
11523             finish_if_stmt (statement);
11524           }
11525         else
11526           {
11527             bool in_switch_statement_p;
11528             unsigned char in_statement;
11529
11530             /* Add the condition.  */
11531             finish_switch_cond (condition, statement);
11532
11533             /* Parse the body of the switch-statement.  */
11534             in_switch_statement_p = parser->in_switch_statement_p;
11535             in_statement = parser->in_statement;
11536             parser->in_switch_statement_p = true;
11537             parser->in_statement |= IN_SWITCH_STMT;
11538             cp_parser_implicitly_scoped_statement (parser, if_p,
11539                                                    guard_tinfo);
11540             parser->in_switch_statement_p = in_switch_statement_p;
11541             parser->in_statement = in_statement;
11542
11543             /* Now we're all done with the switch-statement.  */
11544             finish_switch_stmt (statement);
11545           }
11546
11547         return statement;
11548       }
11549       break;
11550
11551     default:
11552       cp_parser_error (parser, "expected selection-statement");
11553       return error_mark_node;
11554     }
11555 }
11556
11557 /* Parse a condition.
11558
11559    condition:
11560      expression
11561      type-specifier-seq declarator = initializer-clause
11562      type-specifier-seq declarator braced-init-list
11563
11564    GNU Extension:
11565
11566    condition:
11567      type-specifier-seq declarator asm-specification [opt]
11568        attributes [opt] = assignment-expression
11569
11570    Returns the expression that should be tested.  */
11571
11572 static tree
11573 cp_parser_condition (cp_parser* parser)
11574 {
11575   cp_decl_specifier_seq type_specifiers;
11576   const char *saved_message;
11577   int declares_class_or_enum;
11578
11579   /* Try the declaration first.  */
11580   cp_parser_parse_tentatively (parser);
11581   /* New types are not allowed in the type-specifier-seq for a
11582      condition.  */
11583   saved_message = parser->type_definition_forbidden_message;
11584   parser->type_definition_forbidden_message
11585     = G_("types may not be defined in conditions");
11586   /* Parse the type-specifier-seq.  */
11587   cp_parser_decl_specifier_seq (parser,
11588                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11589                                 &type_specifiers,
11590                                 &declares_class_or_enum);
11591   /* Restore the saved message.  */
11592   parser->type_definition_forbidden_message = saved_message;
11593   /* If all is well, we might be looking at a declaration.  */
11594   if (!cp_parser_error_occurred (parser))
11595     {
11596       tree decl;
11597       tree asm_specification;
11598       tree attributes;
11599       cp_declarator *declarator;
11600       tree initializer = NULL_TREE;
11601
11602       /* Parse the declarator.  */
11603       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11604                                          /*ctor_dtor_or_conv_p=*/NULL,
11605                                          /*parenthesized_p=*/NULL,
11606                                          /*member_p=*/false,
11607                                          /*friend_p=*/false);
11608       /* Parse the attributes.  */
11609       attributes = cp_parser_attributes_opt (parser);
11610       /* Parse the asm-specification.  */
11611       asm_specification = cp_parser_asm_specification_opt (parser);
11612       /* If the next token is not an `=' or '{', then we might still be
11613          looking at an expression.  For example:
11614
11615            if (A(a).x)
11616
11617          looks like a decl-specifier-seq and a declarator -- but then
11618          there is no `=', so this is an expression.  */
11619       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11620           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11621         cp_parser_simulate_error (parser);
11622         
11623       /* If we did see an `=' or '{', then we are looking at a declaration
11624          for sure.  */
11625       if (cp_parser_parse_definitely (parser))
11626         {
11627           tree pushed_scope;
11628           bool non_constant_p;
11629           int flags = LOOKUP_ONLYCONVERTING;
11630
11631           /* Create the declaration.  */
11632           decl = start_decl (declarator, &type_specifiers,
11633                              /*initialized_p=*/true,
11634                              attributes, /*prefix_attributes=*/NULL_TREE,
11635                              &pushed_scope);
11636
11637           /* Parse the initializer.  */
11638           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11639             {
11640               initializer = cp_parser_braced_list (parser, &non_constant_p);
11641               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11642               flags = 0;
11643             }
11644           else
11645             {
11646               /* Consume the `='.  */
11647               cp_parser_require (parser, CPP_EQ, RT_EQ);
11648               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11649             }
11650           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11651             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11652
11653           /* Process the initializer.  */
11654           cp_finish_decl (decl,
11655                           initializer, !non_constant_p,
11656                           asm_specification,
11657                           flags);
11658
11659           if (pushed_scope)
11660             pop_scope (pushed_scope);
11661
11662           return convert_from_reference (decl);
11663         }
11664     }
11665   /* If we didn't even get past the declarator successfully, we are
11666      definitely not looking at a declaration.  */
11667   else
11668     cp_parser_abort_tentative_parse (parser);
11669
11670   /* Otherwise, we are looking at an expression.  */
11671   return cp_parser_expression (parser);
11672 }
11673
11674 /* Parses a for-statement or range-for-statement until the closing ')',
11675    not included. */
11676
11677 static tree
11678 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11679 {
11680   tree init, scope, decl;
11681   bool is_range_for;
11682
11683   /* Begin the for-statement.  */
11684   scope = begin_for_scope (&init);
11685
11686   /* Parse the initialization.  */
11687   is_range_for = cp_parser_init_statement (parser, &decl);
11688
11689   if (is_range_for)
11690     return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11691   else
11692     return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11693 }
11694
11695 static tree
11696 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11697                  unsigned short unroll)
11698 {
11699   /* Normal for loop */
11700   tree condition = NULL_TREE;
11701   tree expression = NULL_TREE;
11702   tree stmt;
11703
11704   stmt = begin_for_stmt (scope, init);
11705   /* The init-statement has already been parsed in
11706      cp_parser_init_statement, so no work is needed here.  */
11707   finish_init_stmt (stmt);
11708
11709   /* If there's a condition, process it.  */
11710   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11711     condition = cp_parser_condition (parser);
11712   else if (ivdep)
11713     {
11714       cp_parser_error (parser, "missing loop condition in loop with "
11715                        "%<GCC ivdep%> pragma");
11716       condition = error_mark_node;
11717     }
11718   else if (unroll)
11719     {
11720       cp_parser_error (parser, "missing loop condition in loop with "
11721                        "%<GCC unroll%> pragma");
11722       condition = error_mark_node;
11723     }
11724   finish_for_cond (condition, stmt, ivdep, unroll);
11725   /* Look for the `;'.  */
11726   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11727
11728   /* If there's an expression, process it.  */
11729   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11730     expression = cp_parser_expression (parser);
11731   finish_for_expr (expression, stmt);
11732
11733   return stmt;
11734 }
11735
11736 /* Tries to parse a range-based for-statement:
11737
11738   range-based-for:
11739     decl-specifier-seq declarator : expression
11740
11741   The decl-specifier-seq declarator and the `:' are already parsed by
11742   cp_parser_init_statement.  If processing_template_decl it returns a
11743   newly created RANGE_FOR_STMT; if not, it is converted to a
11744   regular FOR_STMT.  */
11745
11746 static tree
11747 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11748                      bool ivdep, unsigned short unroll)
11749 {
11750   tree stmt, range_expr;
11751   auto_vec <cxx_binding *, 16> bindings;
11752   auto_vec <tree, 16> names;
11753   tree decomp_first_name = NULL_TREE;
11754   unsigned int decomp_cnt = 0;
11755
11756   /* Get the range declaration momentarily out of the way so that
11757      the range expression doesn't clash with it. */
11758   if (range_decl != error_mark_node)
11759     {
11760       if (DECL_HAS_VALUE_EXPR_P (range_decl))
11761         {
11762           tree v = DECL_VALUE_EXPR (range_decl);
11763           /* For decomposition declaration get all of the corresponding
11764              declarations out of the way.  */
11765           if (TREE_CODE (v) == ARRAY_REF
11766               && VAR_P (TREE_OPERAND (v, 0))
11767               && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11768             {
11769               tree d = range_decl;
11770               range_decl = TREE_OPERAND (v, 0);
11771               decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11772               decomp_first_name = d;
11773               for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11774                 {
11775                   tree name = DECL_NAME (d);
11776                   names.safe_push (name);
11777                   bindings.safe_push (IDENTIFIER_BINDING (name));
11778                   IDENTIFIER_BINDING (name)
11779                     = IDENTIFIER_BINDING (name)->previous;
11780                 }
11781             }
11782         }
11783       if (names.is_empty ())
11784         {
11785           tree name = DECL_NAME (range_decl);
11786           names.safe_push (name);
11787           bindings.safe_push (IDENTIFIER_BINDING (name));
11788           IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11789         }
11790     }
11791
11792   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11793     {
11794       bool expr_non_constant_p;
11795       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11796     }
11797   else
11798     range_expr = cp_parser_expression (parser);
11799
11800   /* Put the range declaration(s) back into scope. */
11801   for (unsigned int i = 0; i < names.length (); i++)
11802     {
11803       cxx_binding *binding = bindings[i];
11804       binding->previous = IDENTIFIER_BINDING (names[i]);
11805       IDENTIFIER_BINDING (names[i]) = binding;
11806     }
11807
11808   /* If in template, STMT is converted to a normal for-statement
11809      at instantiation. If not, it is done just ahead. */
11810   if (processing_template_decl)
11811     {
11812       if (check_for_bare_parameter_packs (range_expr))
11813         range_expr = error_mark_node;
11814       stmt = begin_range_for_stmt (scope, init);
11815       if (ivdep)
11816         RANGE_FOR_IVDEP (stmt) = 1;
11817       if (unroll)
11818         RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11819       finish_range_for_decl (stmt, range_decl, range_expr);
11820       if (!type_dependent_expression_p (range_expr)
11821           /* do_auto_deduction doesn't mess with template init-lists.  */
11822           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11823         do_range_for_auto_deduction (range_decl, range_expr);
11824     }
11825   else
11826     {
11827       stmt = begin_for_stmt (scope, init);
11828       stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11829                                    decomp_first_name, decomp_cnt, ivdep,
11830                                    unroll);
11831     }
11832   return stmt;
11833 }
11834
11835 /* Subroutine of cp_convert_range_for: given the initializer expression,
11836    builds up the range temporary.  */
11837
11838 static tree
11839 build_range_temp (tree range_expr)
11840 {
11841   tree range_type, range_temp;
11842
11843   /* Find out the type deduced by the declaration
11844      `auto &&__range = range_expr'.  */
11845   range_type = cp_build_reference_type (make_auto (), true);
11846   range_type = do_auto_deduction (range_type, range_expr,
11847                                   type_uses_auto (range_type));
11848
11849   /* Create the __range variable.  */
11850   range_temp = build_decl (input_location, VAR_DECL,
11851                            get_identifier ("__for_range"), range_type);
11852   TREE_USED (range_temp) = 1;
11853   DECL_ARTIFICIAL (range_temp) = 1;
11854
11855   return range_temp;
11856 }
11857
11858 /* Used by cp_parser_range_for in template context: we aren't going to
11859    do a full conversion yet, but we still need to resolve auto in the
11860    type of the for-range-declaration if present.  This is basically
11861    a shortcut version of cp_convert_range_for.  */
11862
11863 static void
11864 do_range_for_auto_deduction (tree decl, tree range_expr)
11865 {
11866   tree auto_node = type_uses_auto (TREE_TYPE (decl));
11867   if (auto_node)
11868     {
11869       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11870       range_temp = convert_from_reference (build_range_temp (range_expr));
11871       iter_type = (cp_parser_perform_range_for_lookup
11872                    (range_temp, &begin_dummy, &end_dummy));
11873       if (iter_type)
11874         {
11875           iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11876                                   iter_type);
11877           iter_decl = build_x_indirect_ref (input_location, iter_decl,
11878                                             RO_UNARY_STAR,
11879                                             tf_warning_or_error);
11880           TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11881                                                 iter_decl, auto_node);
11882         }
11883     }
11884 }
11885
11886 /* Converts a range-based for-statement into a normal
11887    for-statement, as per the definition.
11888
11889       for (RANGE_DECL : RANGE_EXPR)
11890         BLOCK
11891
11892    should be equivalent to:
11893
11894       {
11895         auto &&__range = RANGE_EXPR;
11896         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11897               __begin != __end;
11898               ++__begin)
11899           {
11900               RANGE_DECL = *__begin;
11901               BLOCK
11902           }
11903       }
11904
11905    If RANGE_EXPR is an array:
11906         BEGIN_EXPR = __range
11907         END_EXPR = __range + ARRAY_SIZE(__range)
11908    Else if RANGE_EXPR has a member 'begin' or 'end':
11909         BEGIN_EXPR = __range.begin()
11910         END_EXPR = __range.end()
11911    Else:
11912         BEGIN_EXPR = begin(__range)
11913         END_EXPR = end(__range);
11914
11915    If __range has a member 'begin' but not 'end', or vice versa, we must
11916    still use the second alternative (it will surely fail, however).
11917    When calling begin()/end() in the third alternative we must use
11918    argument dependent lookup, but always considering 'std' as an associated
11919    namespace.  */
11920
11921 tree
11922 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11923                       tree decomp_first_name, unsigned int decomp_cnt,
11924                       bool ivdep, unsigned short unroll)
11925 {
11926   tree begin, end;
11927   tree iter_type, begin_expr, end_expr;
11928   tree condition, expression;
11929
11930   range_expr = mark_lvalue_use (range_expr);
11931
11932   if (range_decl == error_mark_node || range_expr == error_mark_node)
11933     /* If an error happened previously do nothing or else a lot of
11934        unhelpful errors would be issued.  */
11935     begin_expr = end_expr = iter_type = error_mark_node;
11936   else
11937     {
11938       tree range_temp;
11939
11940       if (VAR_P (range_expr)
11941           && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11942         /* Can't bind a reference to an array of runtime bound.  */
11943         range_temp = range_expr;
11944       else
11945         {
11946           range_temp = build_range_temp (range_expr);
11947           pushdecl (range_temp);
11948           cp_finish_decl (range_temp, range_expr,
11949                           /*is_constant_init*/false, NULL_TREE,
11950                           LOOKUP_ONLYCONVERTING);
11951           range_temp = convert_from_reference (range_temp);
11952         }
11953       iter_type = cp_parser_perform_range_for_lookup (range_temp,
11954                                                       &begin_expr, &end_expr);
11955     }
11956
11957   /* The new for initialization statement.  */
11958   begin = build_decl (input_location, VAR_DECL,
11959                       get_identifier ("__for_begin"), iter_type);
11960   TREE_USED (begin) = 1;
11961   DECL_ARTIFICIAL (begin) = 1;
11962   pushdecl (begin);
11963   cp_finish_decl (begin, begin_expr,
11964                   /*is_constant_init*/false, NULL_TREE,
11965                   LOOKUP_ONLYCONVERTING);
11966
11967   if (cxx_dialect >= cxx17)
11968     iter_type = cv_unqualified (TREE_TYPE (end_expr));
11969   end = build_decl (input_location, VAR_DECL,
11970                     get_identifier ("__for_end"), iter_type);
11971   TREE_USED (end) = 1;
11972   DECL_ARTIFICIAL (end) = 1;
11973   pushdecl (end);
11974   cp_finish_decl (end, end_expr,
11975                   /*is_constant_init*/false, NULL_TREE,
11976                   LOOKUP_ONLYCONVERTING);
11977
11978   finish_init_stmt (statement);
11979
11980   /* The new for condition.  */
11981   condition = build_x_binary_op (input_location, NE_EXPR,
11982                                  begin, ERROR_MARK,
11983                                  end, ERROR_MARK,
11984                                  NULL, tf_warning_or_error);
11985   finish_for_cond (condition, statement, ivdep, unroll);
11986
11987   /* The new increment expression.  */
11988   expression = finish_unary_op_expr (input_location,
11989                                      PREINCREMENT_EXPR, begin,
11990                                      tf_warning_or_error);
11991   finish_for_expr (expression, statement);
11992
11993   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11994     cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11995
11996   /* The declaration is initialized with *__begin inside the loop body.  */
11997   cp_finish_decl (range_decl,
11998                   build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11999                                         tf_warning_or_error),
12000                   /*is_constant_init*/false, NULL_TREE,
12001                   LOOKUP_ONLYCONVERTING);
12002   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12003     cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12004
12005   return statement;
12006 }
12007
12008 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12009    We need to solve both at the same time because the method used
12010    depends on the existence of members begin or end.
12011    Returns the type deduced for the iterator expression.  */
12012
12013 static tree
12014 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12015 {
12016   if (error_operand_p (range))
12017     {
12018       *begin = *end = error_mark_node;
12019       return error_mark_node;
12020     }
12021
12022   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12023     {
12024       error ("range-based %<for%> expression of type %qT "
12025              "has incomplete type", TREE_TYPE (range));
12026       *begin = *end = error_mark_node;
12027       return error_mark_node;
12028     }
12029   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12030     {
12031       /* If RANGE is an array, we will use pointer arithmetic.  */
12032       *begin = decay_conversion (range, tf_warning_or_error);
12033       *end = build_binary_op (input_location, PLUS_EXPR,
12034                               range,
12035                               array_type_nelts_top (TREE_TYPE (range)),
12036                               false);
12037       return TREE_TYPE (*begin);
12038     }
12039   else
12040     {
12041       /* If it is not an array, we must do a bit of magic.  */
12042       tree id_begin, id_end;
12043       tree member_begin, member_end;
12044
12045       *begin = *end = error_mark_node;
12046
12047       id_begin = get_identifier ("begin");
12048       id_end = get_identifier ("end");
12049       member_begin = lookup_member (TREE_TYPE (range), id_begin,
12050                                     /*protect=*/2, /*want_type=*/false,
12051                                     tf_warning_or_error);
12052       member_end = lookup_member (TREE_TYPE (range), id_end,
12053                                   /*protect=*/2, /*want_type=*/false,
12054                                   tf_warning_or_error);
12055
12056       if (member_begin != NULL_TREE && member_end != NULL_TREE)
12057         {
12058           /* Use the member functions.  */
12059           *begin = cp_parser_range_for_member_function (range, id_begin);
12060           *end = cp_parser_range_for_member_function (range, id_end);
12061         }
12062       else
12063         {
12064           /* Use global functions with ADL.  */
12065           vec<tree, va_gc> *vec;
12066           vec = make_tree_vector ();
12067
12068           vec_safe_push (vec, range);
12069
12070           member_begin = perform_koenig_lookup (id_begin, vec,
12071                                                 tf_warning_or_error);
12072           *begin = finish_call_expr (member_begin, &vec, false, true,
12073                                      tf_warning_or_error);
12074           member_end = perform_koenig_lookup (id_end, vec,
12075                                               tf_warning_or_error);
12076           *end = finish_call_expr (member_end, &vec, false, true,
12077                                    tf_warning_or_error);
12078
12079           release_tree_vector (vec);
12080         }
12081
12082       /* Last common checks.  */
12083       if (*begin == error_mark_node || *end == error_mark_node)
12084         {
12085           /* If one of the expressions is an error do no more checks.  */
12086           *begin = *end = error_mark_node;
12087           return error_mark_node;
12088         }
12089       else if (type_dependent_expression_p (*begin)
12090                || type_dependent_expression_p (*end))
12091         /* Can happen, when, eg, in a template context, Koenig lookup
12092            can't resolve begin/end (c++/58503).  */
12093         return NULL_TREE;
12094       else
12095         {
12096           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12097           /* The unqualified type of the __begin and __end temporaries should
12098              be the same, as required by the multiple auto declaration.  */
12099           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12100             {
12101               if (cxx_dialect >= cxx17
12102                   && (build_x_binary_op (input_location, NE_EXPR,
12103                                          *begin, ERROR_MARK,
12104                                          *end, ERROR_MARK,
12105                                          NULL, tf_none)
12106                       != error_mark_node))
12107                 /* P0184R0 allows __begin and __end to have different types,
12108                    but make sure they are comparable so we can give a better
12109                    diagnostic.  */;
12110               else
12111                 error ("inconsistent begin/end types in range-based %<for%> "
12112                        "statement: %qT and %qT",
12113                        TREE_TYPE (*begin), TREE_TYPE (*end));
12114             }
12115           return iter_type;
12116         }
12117     }
12118 }
12119
12120 /* Helper function for cp_parser_perform_range_for_lookup.
12121    Builds a tree for RANGE.IDENTIFIER().  */
12122
12123 static tree
12124 cp_parser_range_for_member_function (tree range, tree identifier)
12125 {
12126   tree member, res;
12127   vec<tree, va_gc> *vec;
12128
12129   member = finish_class_member_access_expr (range, identifier,
12130                                             false, tf_warning_or_error);
12131   if (member == error_mark_node)
12132     return error_mark_node;
12133
12134   vec = make_tree_vector ();
12135   res = finish_call_expr (member, &vec,
12136                           /*disallow_virtual=*/false,
12137                           /*koenig_p=*/false,
12138                           tf_warning_or_error);
12139   release_tree_vector (vec);
12140   return res;
12141 }
12142
12143 /* Parse an iteration-statement.
12144
12145    iteration-statement:
12146      while ( condition ) statement
12147      do statement while ( expression ) ;
12148      for ( init-statement condition [opt] ; expression [opt] )
12149        statement
12150
12151    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
12152
12153 static tree
12154 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12155                                unsigned short unroll)
12156 {
12157   cp_token *token;
12158   enum rid keyword;
12159   tree statement;
12160   unsigned char in_statement;
12161   token_indent_info guard_tinfo;
12162
12163   /* Peek at the next token.  */
12164   token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12165   if (!token)
12166     return error_mark_node;
12167
12168   guard_tinfo = get_token_indent_info (token);
12169
12170   /* Remember whether or not we are already within an iteration
12171      statement.  */
12172   in_statement = parser->in_statement;
12173
12174   /* See what kind of keyword it is.  */
12175   keyword = token->keyword;
12176   switch (keyword)
12177     {
12178     case RID_WHILE:
12179       {
12180         tree condition;
12181
12182         /* Begin the while-statement.  */
12183         statement = begin_while_stmt ();
12184         /* Look for the `('.  */
12185         matching_parens parens;
12186         parens.require_open (parser);
12187         /* Parse the condition.  */
12188         condition = cp_parser_condition (parser);
12189         finish_while_stmt_cond (condition, statement, ivdep, unroll);
12190         /* Look for the `)'.  */
12191         parens.require_close (parser);
12192         /* Parse the dependent statement.  */
12193         parser->in_statement = IN_ITERATION_STMT;
12194         bool prev = note_iteration_stmt_body_start ();
12195         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12196         note_iteration_stmt_body_end (prev);
12197         parser->in_statement = in_statement;
12198         /* We're done with the while-statement.  */
12199         finish_while_stmt (statement);
12200       }
12201       break;
12202
12203     case RID_DO:
12204       {
12205         tree expression;
12206
12207         /* Begin the do-statement.  */
12208         statement = begin_do_stmt ();
12209         /* Parse the body of the do-statement.  */
12210         parser->in_statement = IN_ITERATION_STMT;
12211         bool prev = note_iteration_stmt_body_start ();
12212         cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12213         note_iteration_stmt_body_end (prev);
12214         parser->in_statement = in_statement;
12215         finish_do_body (statement);
12216         /* Look for the `while' keyword.  */
12217         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12218         /* Look for the `('.  */
12219         matching_parens parens;
12220         parens.require_open (parser);
12221         /* Parse the expression.  */
12222         expression = cp_parser_expression (parser);
12223         /* We're done with the do-statement.  */
12224         finish_do_stmt (expression, statement, ivdep, unroll);
12225         /* Look for the `)'.  */
12226         parens.require_close (parser);
12227         /* Look for the `;'.  */
12228         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12229       }
12230       break;
12231
12232     case RID_FOR:
12233       {
12234         /* Look for the `('.  */
12235         matching_parens parens;
12236         parens.require_open (parser);
12237
12238         statement = cp_parser_for (parser, ivdep, unroll);
12239
12240         /* Look for the `)'.  */
12241         parens.require_close (parser);
12242
12243         /* Parse the body of the for-statement.  */
12244         parser->in_statement = IN_ITERATION_STMT;
12245         bool prev = note_iteration_stmt_body_start ();
12246         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12247         note_iteration_stmt_body_end (prev);
12248         parser->in_statement = in_statement;
12249
12250         /* We're done with the for-statement.  */
12251         finish_for_stmt (statement);
12252       }
12253       break;
12254
12255     default:
12256       cp_parser_error (parser, "expected iteration-statement");
12257       statement = error_mark_node;
12258       break;
12259     }
12260
12261   return statement;
12262 }
12263
12264 /* Parse a init-statement or the declarator of a range-based-for.
12265    Returns true if a range-based-for declaration is seen.
12266
12267    init-statement:
12268      expression-statement
12269      simple-declaration  */
12270
12271 static bool
12272 cp_parser_init_statement (cp_parser* parser, tree *decl)
12273 {
12274   /* If the next token is a `;', then we have an empty
12275      expression-statement.  Grammatically, this is also a
12276      simple-declaration, but an invalid one, because it does not
12277      declare anything.  Therefore, if we did not handle this case
12278      specially, we would issue an error message about an invalid
12279      declaration.  */
12280   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12281     {
12282       bool is_range_for = false;
12283       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12284
12285       /* A colon is used in range-based for.  */
12286       parser->colon_corrects_to_scope_p = false;
12287
12288       /* We're going to speculatively look for a declaration, falling back
12289          to an expression, if necessary.  */
12290       cp_parser_parse_tentatively (parser);
12291       /* Parse the declaration.  */
12292       cp_parser_simple_declaration (parser,
12293                                     /*function_definition_allowed_p=*/false,
12294                                     decl);
12295       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12296       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12297         {
12298           /* It is a range-for, consume the ':' */
12299           cp_lexer_consume_token (parser->lexer);
12300           is_range_for = true;
12301           if (cxx_dialect < cxx11)
12302             pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12303                      "range-based %<for%> loops only available with "
12304                      "-std=c++11 or -std=gnu++11");
12305         }
12306       else
12307           /* The ';' is not consumed yet because we told
12308              cp_parser_simple_declaration not to.  */
12309           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12310
12311       if (cp_parser_parse_definitely (parser))
12312         return is_range_for;
12313       /* If the tentative parse failed, then we shall need to look for an
12314          expression-statement.  */
12315     }
12316   /* If we are here, it is an expression-statement.  */
12317   cp_parser_expression_statement (parser, NULL_TREE);
12318   return false;
12319 }
12320
12321 /* Parse a jump-statement.
12322
12323    jump-statement:
12324      break ;
12325      continue ;
12326      return expression [opt] ;
12327      return braced-init-list ;
12328      goto identifier ;
12329
12330    GNU extension:
12331
12332    jump-statement:
12333      goto * expression ;
12334
12335    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
12336
12337 static tree
12338 cp_parser_jump_statement (cp_parser* parser)
12339 {
12340   tree statement = error_mark_node;
12341   cp_token *token;
12342   enum rid keyword;
12343   unsigned char in_statement;
12344
12345   /* Peek at the next token.  */
12346   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12347   if (!token)
12348     return error_mark_node;
12349
12350   /* See what kind of keyword it is.  */
12351   keyword = token->keyword;
12352   switch (keyword)
12353     {
12354     case RID_BREAK:
12355       in_statement = parser->in_statement & ~IN_IF_STMT;      
12356       switch (in_statement)
12357         {
12358         case 0:
12359           error_at (token->location, "break statement not within loop or switch");
12360           break;
12361         default:
12362           gcc_assert ((in_statement & IN_SWITCH_STMT)
12363                       || in_statement == IN_ITERATION_STMT);
12364           statement = finish_break_stmt ();
12365           if (in_statement == IN_ITERATION_STMT)
12366             break_maybe_infinite_loop ();
12367           break;
12368         case IN_OMP_BLOCK:
12369           error_at (token->location, "invalid exit from OpenMP structured block");
12370           break;
12371         case IN_OMP_FOR:
12372           error_at (token->location, "break statement used with OpenMP for loop");
12373           break;
12374         }
12375       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12376       break;
12377
12378     case RID_CONTINUE:
12379       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12380         {
12381         case 0:
12382           error_at (token->location, "continue statement not within a loop");
12383           break;
12384           /* Fall through.  */
12385         case IN_ITERATION_STMT:
12386         case IN_OMP_FOR:
12387           statement = finish_continue_stmt ();
12388           break;
12389         case IN_OMP_BLOCK:
12390           error_at (token->location, "invalid exit from OpenMP structured block");
12391           break;
12392         default:
12393           gcc_unreachable ();
12394         }
12395       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12396       break;
12397
12398     case RID_RETURN:
12399       {
12400         tree expr;
12401         bool expr_non_constant_p;
12402
12403         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12404           {
12405             cp_lexer_set_source_position (parser->lexer);
12406             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12407             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12408           }
12409         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12410           expr = cp_parser_expression (parser);
12411         else
12412           /* If the next token is a `;', then there is no
12413              expression.  */
12414           expr = NULL_TREE;
12415         /* Build the return-statement.  */
12416         if (current_function_auto_return_pattern && in_discarded_stmt)
12417           /* Don't deduce from a discarded return statement.  */;
12418         else
12419           statement = finish_return_stmt (expr);
12420         /* Look for the final `;'.  */
12421         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12422       }
12423       break;
12424
12425     case RID_GOTO:
12426       if (parser->in_function_body
12427           && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12428         {
12429           error ("%<goto%> in %<constexpr%> function");
12430           cp_function_chain->invalid_constexpr = true;
12431         }
12432
12433       /* Create the goto-statement.  */
12434       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12435         {
12436           /* Issue a warning about this use of a GNU extension.  */
12437           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12438           /* Consume the '*' token.  */
12439           cp_lexer_consume_token (parser->lexer);
12440           /* Parse the dependent expression.  */
12441           finish_goto_stmt (cp_parser_expression (parser));
12442         }
12443       else
12444         finish_goto_stmt (cp_parser_identifier (parser));
12445       /* Look for the final `;'.  */
12446       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12447       break;
12448
12449     default:
12450       cp_parser_error (parser, "expected jump-statement");
12451       break;
12452     }
12453
12454   return statement;
12455 }
12456
12457 /* Parse a declaration-statement.
12458
12459    declaration-statement:
12460      block-declaration  */
12461
12462 static void
12463 cp_parser_declaration_statement (cp_parser* parser)
12464 {
12465   void *p;
12466
12467   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12468   p = obstack_alloc (&declarator_obstack, 0);
12469
12470  /* Parse the block-declaration.  */
12471   cp_parser_block_declaration (parser, /*statement_p=*/true);
12472
12473   /* Free any declarators allocated.  */
12474   obstack_free (&declarator_obstack, p);
12475 }
12476
12477 /* Some dependent statements (like `if (cond) statement'), are
12478    implicitly in their own scope.  In other words, if the statement is
12479    a single statement (as opposed to a compound-statement), it is
12480    none-the-less treated as if it were enclosed in braces.  Any
12481    declarations appearing in the dependent statement are out of scope
12482    after control passes that point.  This function parses a statement,
12483    but ensures that is in its own scope, even if it is not a
12484    compound-statement.
12485
12486    If IF_P is not NULL, *IF_P is set to indicate whether the statement
12487    is a (possibly labeled) if statement which is not enclosed in
12488    braces and has an else clause.  This is used to implement
12489    -Wparentheses.
12490
12491    CHAIN is a vector of if-else-if conditions.  This is used to implement
12492    -Wduplicated-cond.
12493
12494    Returns the new statement.  */
12495
12496 static tree
12497 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12498                                        const token_indent_info &guard_tinfo,
12499                                        vec<tree> *chain)
12500 {
12501   tree statement;
12502   location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12503   location_t body_loc_after_labels = UNKNOWN_LOCATION;
12504   token_indent_info body_tinfo
12505     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12506
12507   if (if_p != NULL)
12508     *if_p = false;
12509
12510   /* Mark if () ; with a special NOP_EXPR.  */
12511   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12512     {
12513       cp_lexer_consume_token (parser->lexer);
12514       statement = add_stmt (build_empty_stmt (body_loc));
12515
12516       if (guard_tinfo.keyword == RID_IF
12517           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12518         warning_at (body_loc, OPT_Wempty_body,
12519                     "suggest braces around empty body in an %<if%> statement");
12520       else if (guard_tinfo.keyword == RID_ELSE)
12521         warning_at (body_loc, OPT_Wempty_body,
12522                     "suggest braces around empty body in an %<else%> statement");
12523     }
12524   /* if a compound is opened, we simply parse the statement directly.  */
12525   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12526     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12527   /* If the token is not a `{', then we must take special action.  */
12528   else
12529     {
12530       /* Create a compound-statement.  */
12531       statement = begin_compound_stmt (0);
12532       /* Parse the dependent-statement.  */
12533       cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12534                            &body_loc_after_labels);
12535       /* Finish the dummy compound-statement.  */
12536       finish_compound_stmt (statement);
12537     }
12538
12539   token_indent_info next_tinfo
12540     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12541   warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12542
12543   if (body_loc_after_labels != UNKNOWN_LOCATION
12544       && next_tinfo.type != CPP_SEMICOLON)
12545     warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12546                                     guard_tinfo.location, guard_tinfo.keyword);
12547
12548   /* Return the statement.  */
12549   return statement;
12550 }
12551
12552 /* For some dependent statements (like `while (cond) statement'), we
12553    have already created a scope.  Therefore, even if the dependent
12554    statement is a compound-statement, we do not want to create another
12555    scope.  */
12556
12557 static void
12558 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12559                                     const token_indent_info &guard_tinfo)
12560 {
12561   /* If the token is a `{', then we must take special action.  */
12562   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12563     {
12564       token_indent_info body_tinfo
12565         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12566       location_t loc_after_labels = UNKNOWN_LOCATION;
12567
12568       cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12569                            &loc_after_labels);
12570       token_indent_info next_tinfo
12571         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12572       warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12573
12574       if (loc_after_labels != UNKNOWN_LOCATION
12575           && next_tinfo.type != CPP_SEMICOLON)
12576         warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12577                                         guard_tinfo.location,
12578                                         guard_tinfo.keyword);
12579     }
12580   else
12581     {
12582       /* Avoid calling cp_parser_compound_statement, so that we
12583          don't create a new scope.  Do everything else by hand.  */
12584       matching_braces braces;
12585       braces.require_open (parser);
12586       /* If the next keyword is `__label__' we have a label declaration.  */
12587       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12588         cp_parser_label_declaration (parser);
12589       /* Parse an (optional) statement-seq.  */
12590       cp_parser_statement_seq_opt (parser, NULL_TREE);
12591       braces.require_close (parser);
12592     }
12593 }
12594
12595 /* Declarations [gram.dcl.dcl] */
12596
12597 /* Parse an optional declaration-sequence.
12598
12599    declaration-seq:
12600      declaration
12601      declaration-seq declaration  */
12602
12603 static void
12604 cp_parser_declaration_seq_opt (cp_parser* parser)
12605 {
12606   while (true)
12607     {
12608       cp_token *token;
12609
12610       token = cp_lexer_peek_token (parser->lexer);
12611
12612       if (token->type == CPP_CLOSE_BRACE
12613           || token->type == CPP_EOF
12614           || token->type == CPP_PRAGMA_EOL)
12615         break;
12616
12617       if (token->type == CPP_SEMICOLON)
12618         {
12619           /* A declaration consisting of a single semicolon is
12620              invalid.  Allow it unless we're being pedantic.  */
12621           cp_lexer_consume_token (parser->lexer);
12622           if (!in_system_header_at (input_location))
12623             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12624           continue;
12625         }
12626
12627       /* If we're entering or exiting a region that's implicitly
12628          extern "C", modify the lang context appropriately.  */
12629       if (!parser->implicit_extern_c && token->implicit_extern_c)
12630         {
12631           push_lang_context (lang_name_c);
12632           parser->implicit_extern_c = true;
12633         }
12634       else if (parser->implicit_extern_c && !token->implicit_extern_c)
12635         {
12636           pop_lang_context ();
12637           parser->implicit_extern_c = false;
12638         }
12639
12640       if (token->type == CPP_PRAGMA)
12641         {
12642           /* A top-level declaration can consist solely of a #pragma.
12643              A nested declaration cannot, so this is done here and not
12644              in cp_parser_declaration.  (A #pragma at block scope is
12645              handled in cp_parser_statement.)  */
12646           cp_parser_pragma (parser, pragma_external, NULL);
12647           continue;
12648         }
12649
12650       /* Parse the declaration itself.  */
12651       cp_parser_declaration (parser);
12652     }
12653 }
12654
12655 /* Parse a declaration.
12656
12657    declaration:
12658      block-declaration
12659      function-definition
12660      template-declaration
12661      explicit-instantiation
12662      explicit-specialization
12663      linkage-specification
12664      namespace-definition
12665
12666    C++17:
12667      deduction-guide
12668
12669    GNU extension:
12670
12671    declaration:
12672       __extension__ declaration */
12673
12674 static void
12675 cp_parser_declaration (cp_parser* parser)
12676 {
12677   cp_token token1;
12678   cp_token token2;
12679   int saved_pedantic;
12680   void *p;
12681   tree attributes = NULL_TREE;
12682
12683   /* Check for the `__extension__' keyword.  */
12684   if (cp_parser_extension_opt (parser, &saved_pedantic))
12685     {
12686       /* Parse the qualified declaration.  */
12687       cp_parser_declaration (parser);
12688       /* Restore the PEDANTIC flag.  */
12689       pedantic = saved_pedantic;
12690
12691       return;
12692     }
12693
12694   /* Try to figure out what kind of declaration is present.  */
12695   token1 = *cp_lexer_peek_token (parser->lexer);
12696
12697   if (token1.type != CPP_EOF)
12698     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12699   else
12700     {
12701       token2.type = CPP_EOF;
12702       token2.keyword = RID_MAX;
12703     }
12704
12705   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12706   p = obstack_alloc (&declarator_obstack, 0);
12707
12708   /* If the next token is `extern' and the following token is a string
12709      literal, then we have a linkage specification.  */
12710   if (token1.keyword == RID_EXTERN
12711       && cp_parser_is_pure_string_literal (&token2))
12712     cp_parser_linkage_specification (parser);
12713   /* If the next token is `template', then we have either a template
12714      declaration, an explicit instantiation, or an explicit
12715      specialization.  */
12716   else if (token1.keyword == RID_TEMPLATE)
12717     {
12718       /* `template <>' indicates a template specialization.  */
12719       if (token2.type == CPP_LESS
12720           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12721         cp_parser_explicit_specialization (parser);
12722       /* `template <' indicates a template declaration.  */
12723       else if (token2.type == CPP_LESS)
12724         cp_parser_template_declaration (parser, /*member_p=*/false);
12725       /* Anything else must be an explicit instantiation.  */
12726       else
12727         cp_parser_explicit_instantiation (parser);
12728     }
12729   /* If the next token is `export', then we have a template
12730      declaration.  */
12731   else if (token1.keyword == RID_EXPORT)
12732     cp_parser_template_declaration (parser, /*member_p=*/false);
12733   /* If the next token is `extern', 'static' or 'inline' and the one
12734      after that is `template', we have a GNU extended explicit
12735      instantiation directive.  */
12736   else if (cp_parser_allow_gnu_extensions_p (parser)
12737            && (token1.keyword == RID_EXTERN
12738                || token1.keyword == RID_STATIC
12739                || token1.keyword == RID_INLINE)
12740            && token2.keyword == RID_TEMPLATE)
12741     cp_parser_explicit_instantiation (parser);
12742   /* If the next token is `namespace', check for a named or unnamed
12743      namespace definition.  */
12744   else if (token1.keyword == RID_NAMESPACE
12745            && (/* A named namespace definition.  */
12746                (token2.type == CPP_NAME
12747                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12748                     != CPP_EQ))
12749                || (token2.type == CPP_OPEN_SQUARE
12750                    && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12751                    == CPP_OPEN_SQUARE)
12752                /* An unnamed namespace definition.  */
12753                || token2.type == CPP_OPEN_BRACE
12754                || token2.keyword == RID_ATTRIBUTE))
12755     cp_parser_namespace_definition (parser);
12756   /* An inline (associated) namespace definition.  */
12757   else if (token1.keyword == RID_INLINE
12758            && token2.keyword == RID_NAMESPACE)
12759     cp_parser_namespace_definition (parser);
12760   /* Objective-C++ declaration/definition.  */
12761   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12762     cp_parser_objc_declaration (parser, NULL_TREE);
12763   else if (c_dialect_objc ()
12764            && token1.keyword == RID_ATTRIBUTE
12765            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12766     cp_parser_objc_declaration (parser, attributes);
12767   /* At this point we may have a template declared by a concept
12768      introduction.  */
12769   else if (flag_concepts
12770            && cp_parser_template_declaration_after_export (parser,
12771                                                            /*member_p=*/false))
12772     /* We did.  */;
12773   else
12774     /* Try to parse a block-declaration, or a function-definition.  */
12775     cp_parser_block_declaration (parser, /*statement_p=*/false);
12776
12777   /* Free any declarators allocated.  */
12778   obstack_free (&declarator_obstack, p);
12779 }
12780
12781 /* Parse a block-declaration.
12782
12783    block-declaration:
12784      simple-declaration
12785      asm-definition
12786      namespace-alias-definition
12787      using-declaration
12788      using-directive
12789
12790    GNU Extension:
12791
12792    block-declaration:
12793      __extension__ block-declaration
12794
12795    C++0x Extension:
12796
12797    block-declaration:
12798      static_assert-declaration
12799
12800    If STATEMENT_P is TRUE, then this block-declaration is occurring as
12801    part of a declaration-statement.  */
12802
12803 static void
12804 cp_parser_block_declaration (cp_parser *parser,
12805                              bool      statement_p)
12806 {
12807   cp_token *token1;
12808   int saved_pedantic;
12809
12810   /* Check for the `__extension__' keyword.  */
12811   if (cp_parser_extension_opt (parser, &saved_pedantic))
12812     {
12813       /* Parse the qualified declaration.  */
12814       cp_parser_block_declaration (parser, statement_p);
12815       /* Restore the PEDANTIC flag.  */
12816       pedantic = saved_pedantic;
12817
12818       return;
12819     }
12820
12821   /* Peek at the next token to figure out which kind of declaration is
12822      present.  */
12823   token1 = cp_lexer_peek_token (parser->lexer);
12824
12825   /* If the next keyword is `asm', we have an asm-definition.  */
12826   if (token1->keyword == RID_ASM)
12827     {
12828       if (statement_p)
12829         cp_parser_commit_to_tentative_parse (parser);
12830       cp_parser_asm_definition (parser);
12831     }
12832   /* If the next keyword is `namespace', we have a
12833      namespace-alias-definition.  */
12834   else if (token1->keyword == RID_NAMESPACE)
12835     cp_parser_namespace_alias_definition (parser);
12836   /* If the next keyword is `using', we have a
12837      using-declaration, a using-directive, or an alias-declaration.  */
12838   else if (token1->keyword == RID_USING)
12839     {
12840       cp_token *token2;
12841
12842       if (statement_p)
12843         cp_parser_commit_to_tentative_parse (parser);
12844       /* If the token after `using' is `namespace', then we have a
12845          using-directive.  */
12846       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12847       if (token2->keyword == RID_NAMESPACE)
12848         cp_parser_using_directive (parser);
12849       /* If the second token after 'using' is '=', then we have an
12850          alias-declaration.  */
12851       else if (cxx_dialect >= cxx11
12852                && token2->type == CPP_NAME
12853                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12854                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12855         cp_parser_alias_declaration (parser);
12856       /* Otherwise, it's a using-declaration.  */
12857       else
12858         cp_parser_using_declaration (parser,
12859                                      /*access_declaration_p=*/false);
12860     }
12861   /* If the next keyword is `__label__' we have a misplaced label
12862      declaration.  */
12863   else if (token1->keyword == RID_LABEL)
12864     {
12865       cp_lexer_consume_token (parser->lexer);
12866       error_at (token1->location, "%<__label__%> not at the beginning of a block");
12867       cp_parser_skip_to_end_of_statement (parser);
12868       /* If the next token is now a `;', consume it.  */
12869       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12870         cp_lexer_consume_token (parser->lexer);
12871     }
12872   /* If the next token is `static_assert' we have a static assertion.  */
12873   else if (token1->keyword == RID_STATIC_ASSERT)
12874     cp_parser_static_assert (parser, /*member_p=*/false);
12875   /* Anything else must be a simple-declaration.  */
12876   else
12877     cp_parser_simple_declaration (parser, !statement_p,
12878                                   /*maybe_range_for_decl*/NULL);
12879 }
12880
12881 /* Parse a simple-declaration.
12882
12883    simple-declaration:
12884      decl-specifier-seq [opt] init-declarator-list [opt] ;
12885      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12886        brace-or-equal-initializer ;
12887
12888    init-declarator-list:
12889      init-declarator
12890      init-declarator-list , init-declarator
12891
12892    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12893    function-definition as a simple-declaration.
12894
12895    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12896    parsed declaration if it is an uninitialized single declarator not followed
12897    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12898    if present, will not be consumed.  */
12899
12900 static void
12901 cp_parser_simple_declaration (cp_parser* parser,
12902                               bool function_definition_allowed_p,
12903                               tree *maybe_range_for_decl)
12904 {
12905   cp_decl_specifier_seq decl_specifiers;
12906   int declares_class_or_enum;
12907   bool saw_declarator;
12908   location_t comma_loc = UNKNOWN_LOCATION;
12909   location_t init_loc = UNKNOWN_LOCATION;
12910
12911   if (maybe_range_for_decl)
12912     *maybe_range_for_decl = NULL_TREE;
12913
12914   /* Defer access checks until we know what is being declared; the
12915      checks for names appearing in the decl-specifier-seq should be
12916      done as if we were in the scope of the thing being declared.  */
12917   push_deferring_access_checks (dk_deferred);
12918
12919   /* Parse the decl-specifier-seq.  We have to keep track of whether
12920      or not the decl-specifier-seq declares a named class or
12921      enumeration type, since that is the only case in which the
12922      init-declarator-list is allowed to be empty.
12923
12924      [dcl.dcl]
12925
12926      In a simple-declaration, the optional init-declarator-list can be
12927      omitted only when declaring a class or enumeration, that is when
12928      the decl-specifier-seq contains either a class-specifier, an
12929      elaborated-type-specifier, or an enum-specifier.  */
12930   cp_parser_decl_specifier_seq (parser,
12931                                 CP_PARSER_FLAGS_OPTIONAL,
12932                                 &decl_specifiers,
12933                                 &declares_class_or_enum);
12934   /* We no longer need to defer access checks.  */
12935   stop_deferring_access_checks ();
12936
12937   /* In a block scope, a valid declaration must always have a
12938      decl-specifier-seq.  By not trying to parse declarators, we can
12939      resolve the declaration/expression ambiguity more quickly.  */
12940   if (!function_definition_allowed_p
12941       && !decl_specifiers.any_specifiers_p)
12942     {
12943       cp_parser_error (parser, "expected declaration");
12944       goto done;
12945     }
12946
12947   /* If the next two tokens are both identifiers, the code is
12948      erroneous. The usual cause of this situation is code like:
12949
12950        T t;
12951
12952      where "T" should name a type -- but does not.  */
12953   if (!decl_specifiers.any_type_specifiers_p
12954       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12955     {
12956       /* If parsing tentatively, we should commit; we really are
12957          looking at a declaration.  */
12958       cp_parser_commit_to_tentative_parse (parser);
12959       /* Give up.  */
12960       goto done;
12961     }
12962
12963   /* If we have seen at least one decl-specifier, and the next token
12964      is not a parenthesis, then we must be looking at a declaration.
12965      (After "int (" we might be looking at a functional cast.)  */
12966   if (decl_specifiers.any_specifiers_p
12967       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12968       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12969       && !cp_parser_error_occurred (parser))
12970     cp_parser_commit_to_tentative_parse (parser);
12971
12972   /* Look for C++17 decomposition declaration.  */
12973   for (size_t n = 1; ; n++)
12974     if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12975         || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12976       continue;
12977     else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12978              && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12979              && decl_specifiers.any_specifiers_p)
12980       {
12981         tree decl
12982           = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12983                                                  maybe_range_for_decl,
12984                                                  &init_loc);
12985
12986         /* The next token should be either a `,' or a `;'.  */
12987         cp_token *token = cp_lexer_peek_token (parser->lexer);
12988         /* If it's a `;', we are done.  */
12989         if (token->type == CPP_SEMICOLON)
12990           goto finish;
12991         else if (maybe_range_for_decl)
12992           {
12993             if (*maybe_range_for_decl == NULL_TREE)
12994               *maybe_range_for_decl = error_mark_node;
12995             goto finish;
12996           }
12997         /* Anything else is an error.  */
12998         else
12999           {
13000             /* If we have already issued an error message we don't need
13001                to issue another one.  */
13002             if ((decl != error_mark_node
13003                  && DECL_INITIAL (decl) != error_mark_node)
13004                 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13005               cp_parser_error (parser, "expected %<,%> or %<;%>");
13006             /* Skip tokens until we reach the end of the statement.  */
13007             cp_parser_skip_to_end_of_statement (parser);
13008             /* If the next token is now a `;', consume it.  */
13009             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13010               cp_lexer_consume_token (parser->lexer);
13011             goto done;
13012           }
13013       }
13014     else
13015       break;
13016
13017   tree last_type;
13018   bool auto_specifier_p;
13019   /* NULL_TREE if both variable and function declaration are allowed,
13020      error_mark_node if function declaration are not allowed and
13021      a FUNCTION_DECL that should be diagnosed if it is followed by
13022      variable declarations.  */
13023   tree auto_function_declaration;
13024
13025   last_type = NULL_TREE;
13026   auto_specifier_p
13027     = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13028   auto_function_declaration = NULL_TREE;
13029
13030   /* Keep going until we hit the `;' at the end of the simple
13031      declaration.  */
13032   saw_declarator = false;
13033   while (cp_lexer_next_token_is_not (parser->lexer,
13034                                      CPP_SEMICOLON))
13035     {
13036       cp_token *token;
13037       bool function_definition_p;
13038       tree decl;
13039       tree auto_result = NULL_TREE;
13040
13041       if (saw_declarator)
13042         {
13043           /* If we are processing next declarator, comma is expected */
13044           token = cp_lexer_peek_token (parser->lexer);
13045           gcc_assert (token->type == CPP_COMMA);
13046           cp_lexer_consume_token (parser->lexer);
13047           if (maybe_range_for_decl)
13048             {
13049               *maybe_range_for_decl = error_mark_node;
13050               if (comma_loc == UNKNOWN_LOCATION)
13051                 comma_loc = token->location;
13052             }
13053         }
13054       else
13055         saw_declarator = true;
13056
13057       /* Parse the init-declarator.  */
13058       decl = cp_parser_init_declarator (parser, &decl_specifiers,
13059                                         /*checks=*/NULL,
13060                                         function_definition_allowed_p,
13061                                         /*member_p=*/false,
13062                                         declares_class_or_enum,
13063                                         &function_definition_p,
13064                                         maybe_range_for_decl,
13065                                         &init_loc,
13066                                         &auto_result);
13067       /* If an error occurred while parsing tentatively, exit quickly.
13068          (That usually happens when in the body of a function; each
13069          statement is treated as a declaration-statement until proven
13070          otherwise.)  */
13071       if (cp_parser_error_occurred (parser))
13072         goto done;
13073
13074       if (auto_specifier_p && cxx_dialect >= cxx14)
13075         {
13076           /* If the init-declarator-list contains more than one
13077              init-declarator, they shall all form declarations of
13078              variables.  */
13079           if (auto_function_declaration == NULL_TREE)
13080             auto_function_declaration
13081               = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13082           else if (TREE_CODE (decl) == FUNCTION_DECL
13083                    || auto_function_declaration != error_mark_node)
13084             {
13085               error_at (decl_specifiers.locations[ds_type_spec],
13086                         "non-variable %qD in declaration with more than one "
13087                         "declarator with placeholder type",
13088                         TREE_CODE (decl) == FUNCTION_DECL
13089                         ? decl : auto_function_declaration);
13090               auto_function_declaration = error_mark_node;
13091             }
13092         }
13093
13094       if (auto_result
13095           && (!processing_template_decl || !type_uses_auto (auto_result)))
13096         {
13097           if (last_type
13098               && last_type != error_mark_node
13099               && !same_type_p (auto_result, last_type))
13100             {
13101               /* If the list of declarators contains more than one declarator,
13102                  the type of each declared variable is determined as described
13103                  above. If the type deduced for the template parameter U is not
13104                  the same in each deduction, the program is ill-formed.  */
13105               error_at (decl_specifiers.locations[ds_type_spec],
13106                         "inconsistent deduction for %qT: %qT and then %qT",
13107                         decl_specifiers.type, last_type, auto_result);
13108               last_type = error_mark_node;
13109             }
13110           else
13111             last_type = auto_result;
13112         }
13113
13114       /* Handle function definitions specially.  */
13115       if (function_definition_p)
13116         {
13117           /* If the next token is a `,', then we are probably
13118              processing something like:
13119
13120                void f() {}, *p;
13121
13122              which is erroneous.  */
13123           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13124             {
13125               cp_token *token = cp_lexer_peek_token (parser->lexer);
13126               error_at (token->location,
13127                         "mixing"
13128                         " declarations and function-definitions is forbidden");
13129             }
13130           /* Otherwise, we're done with the list of declarators.  */
13131           else
13132             {
13133               pop_deferring_access_checks ();
13134               return;
13135             }
13136         }
13137       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13138         *maybe_range_for_decl = decl;
13139       /* The next token should be either a `,' or a `;'.  */
13140       token = cp_lexer_peek_token (parser->lexer);
13141       /* If it's a `,', there are more declarators to come.  */
13142       if (token->type == CPP_COMMA)
13143         /* will be consumed next time around */;
13144       /* If it's a `;', we are done.  */
13145       else if (token->type == CPP_SEMICOLON)
13146         break;
13147       else if (maybe_range_for_decl)
13148         {
13149           if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13150             permerror (decl_specifiers.locations[ds_type_spec],
13151                        "types may not be defined in a for-range-declaration");
13152           break;
13153         }
13154       /* Anything else is an error.  */
13155       else
13156         {
13157           /* If we have already issued an error message we don't need
13158              to issue another one.  */
13159           if ((decl != error_mark_node
13160                && DECL_INITIAL (decl) != error_mark_node)
13161               || cp_parser_uncommitted_to_tentative_parse_p (parser))
13162             cp_parser_error (parser, "expected %<,%> or %<;%>");
13163           /* Skip tokens until we reach the end of the statement.  */
13164           cp_parser_skip_to_end_of_statement (parser);
13165           /* If the next token is now a `;', consume it.  */
13166           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13167             cp_lexer_consume_token (parser->lexer);
13168           goto done;
13169         }
13170       /* After the first time around, a function-definition is not
13171          allowed -- even if it was OK at first.  For example:
13172
13173            int i, f() {}
13174
13175          is not valid.  */
13176       function_definition_allowed_p = false;
13177     }
13178
13179   /* Issue an error message if no declarators are present, and the
13180      decl-specifier-seq does not itself declare a class or
13181      enumeration: [dcl.dcl]/3.  */
13182   if (!saw_declarator)
13183     {
13184       if (cp_parser_declares_only_class_p (parser))
13185         {
13186           if (!declares_class_or_enum
13187               && decl_specifiers.type
13188               && OVERLOAD_TYPE_P (decl_specifiers.type))
13189             /* Ensure an error is issued anyway when finish_decltype_type,
13190                called via cp_parser_decl_specifier_seq, returns a class or
13191                an enumeration (c++/51786).  */
13192             decl_specifiers.type = NULL_TREE;
13193           shadow_tag (&decl_specifiers);
13194         }
13195       /* Perform any deferred access checks.  */
13196       perform_deferred_access_checks (tf_warning_or_error);
13197     }
13198
13199   /* Consume the `;'.  */
13200  finish:
13201   if (!maybe_range_for_decl)
13202     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13203   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13204     {
13205       if (init_loc != UNKNOWN_LOCATION)
13206         error_at (init_loc, "initializer in range-based %<for%> loop");
13207       if (comma_loc != UNKNOWN_LOCATION)
13208         error_at (comma_loc,
13209                   "multiple declarations in range-based %<for%> loop");
13210     }
13211
13212  done:
13213   pop_deferring_access_checks ();
13214 }
13215
13216 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13217      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13218        initializer ;  */
13219
13220 static tree
13221 cp_parser_decomposition_declaration (cp_parser *parser,
13222                                      cp_decl_specifier_seq *decl_specifiers,
13223                                      tree *maybe_range_for_decl,
13224                                      location_t *init_loc)
13225 {
13226   cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13227   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13228   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13229
13230   /* Parse the identifier-list.  */
13231   auto_vec<cp_expr, 10> v;
13232   if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13233     while (true)
13234       {
13235         cp_expr e = cp_parser_identifier (parser);
13236         if (e.get_value () == error_mark_node)
13237           break;
13238         v.safe_push (e);
13239         if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13240           break;
13241         cp_lexer_consume_token (parser->lexer);
13242       }
13243
13244   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13245   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13246     {
13247       end_loc = UNKNOWN_LOCATION;
13248       cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13249                                                false);
13250       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13251         cp_lexer_consume_token (parser->lexer);
13252       else
13253         {
13254           cp_parser_skip_to_end_of_statement (parser);
13255           return error_mark_node;
13256         }
13257     }
13258
13259   if (cxx_dialect < cxx17)
13260     pedwarn (loc, 0, "structured bindings only available with "
13261                      "-std=c++17 or -std=gnu++17");
13262
13263   tree pushed_scope;
13264   cp_declarator *declarator = make_declarator (cdk_decomp);
13265   loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13266   declarator->id_loc = loc;
13267   if (ref_qual != REF_QUAL_NONE)
13268     declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13269                                             ref_qual == REF_QUAL_RVALUE,
13270                                             NULL_TREE);
13271   tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13272                           NULL_TREE, decl_specifiers->attributes,
13273                           &pushed_scope);
13274   tree orig_decl = decl;
13275
13276   unsigned int i;
13277   cp_expr e;
13278   cp_decl_specifier_seq decl_specs;
13279   clear_decl_specs (&decl_specs);
13280   decl_specs.type = make_auto ();
13281   tree prev = decl;
13282   FOR_EACH_VEC_ELT (v, i, e)
13283     {
13284       if (i == 0)
13285         declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13286       else
13287         declarator->u.id.unqualified_name = e.get_value ();
13288       declarator->id_loc = e.get_location ();
13289       tree elt_pushed_scope;
13290       tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13291                                NULL_TREE, NULL_TREE, &elt_pushed_scope);
13292       if (decl2 == error_mark_node)
13293         decl = error_mark_node;
13294       else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13295         {
13296           /* Ensure we've diagnosed redeclaration if we aren't creating
13297              a new VAR_DECL.  */
13298           gcc_assert (errorcount);
13299           decl = error_mark_node;
13300         }
13301       else
13302         prev = decl2;
13303       if (elt_pushed_scope)
13304         pop_scope (elt_pushed_scope);
13305     }
13306
13307   if (v.is_empty ())
13308     {
13309       error_at (loc, "empty structured binding declaration");
13310       decl = error_mark_node;
13311     }
13312
13313   if (maybe_range_for_decl == NULL
13314       || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13315     {
13316       bool non_constant_p = false, is_direct_init = false;
13317       *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13318       tree initializer = cp_parser_initializer (parser, &is_direct_init,
13319                                                 &non_constant_p);
13320       if (initializer == NULL_TREE
13321           || (TREE_CODE (initializer) == TREE_LIST
13322               && TREE_CHAIN (initializer))
13323           || (is_direct_init
13324               && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13325               && CONSTRUCTOR_NELTS (initializer) != 1))
13326         {
13327           error_at (loc, "invalid initializer for structured binding "
13328                     "declaration");
13329           initializer = error_mark_node;
13330         }
13331
13332       if (decl != error_mark_node)
13333         {
13334           cp_maybe_mangle_decomp (decl, prev, v.length ());
13335           cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13336                           is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13337           cp_finish_decomp (decl, prev, v.length ());
13338         }
13339     }
13340   else if (decl != error_mark_node)
13341     {
13342       *maybe_range_for_decl = prev;
13343       /* Ensure DECL_VALUE_EXPR is created for all the decls but
13344          the underlying DECL.  */
13345       cp_finish_decomp (decl, prev, v.length ());
13346     }
13347
13348   if (pushed_scope)
13349     pop_scope (pushed_scope);
13350
13351   if (decl == error_mark_node && DECL_P (orig_decl))
13352     {
13353       if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13354         SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13355     }
13356
13357   return decl;
13358 }
13359
13360 /* Parse a decl-specifier-seq.
13361
13362    decl-specifier-seq:
13363      decl-specifier-seq [opt] decl-specifier
13364      decl-specifier attribute-specifier-seq [opt] (C++11)
13365
13366    decl-specifier:
13367      storage-class-specifier
13368      type-specifier
13369      function-specifier
13370      friend
13371      typedef
13372
13373    GNU Extension:
13374
13375    decl-specifier:
13376      attributes
13377
13378    Concepts Extension:
13379
13380    decl-specifier:
13381      concept
13382
13383    Set *DECL_SPECS to a representation of the decl-specifier-seq.
13384
13385    The parser flags FLAGS is used to control type-specifier parsing.
13386
13387    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13388    flags:
13389
13390      1: one of the decl-specifiers is an elaborated-type-specifier
13391         (i.e., a type declaration)
13392      2: one of the decl-specifiers is an enum-specifier or a
13393         class-specifier (i.e., a type definition)
13394
13395    */
13396
13397 static void
13398 cp_parser_decl_specifier_seq (cp_parser* parser,
13399                               cp_parser_flags flags,
13400                               cp_decl_specifier_seq *decl_specs,
13401                               int* declares_class_or_enum)
13402 {
13403   bool constructor_possible_p = !parser->in_declarator_p;
13404   bool found_decl_spec = false;
13405   cp_token *start_token = NULL;
13406   cp_decl_spec ds;
13407
13408   /* Clear DECL_SPECS.  */
13409   clear_decl_specs (decl_specs);
13410
13411   /* Assume no class or enumeration type is declared.  */
13412   *declares_class_or_enum = 0;
13413
13414   /* Keep reading specifiers until there are no more to read.  */
13415   while (true)
13416     {
13417       bool constructor_p;
13418       cp_token *token;
13419       ds = ds_last;
13420
13421       /* Peek at the next token.  */
13422       token = cp_lexer_peek_token (parser->lexer);
13423
13424       /* Save the first token of the decl spec list for error
13425          reporting.  */
13426       if (!start_token)
13427         start_token = token;
13428       /* Handle attributes.  */
13429       if (cp_next_tokens_can_be_attribute_p (parser))
13430         {
13431           /* Parse the attributes.  */
13432           tree attrs = cp_parser_attributes_opt (parser);
13433
13434           /* In a sequence of declaration specifiers, c++11 attributes
13435              appertain to the type that precede them. In that case
13436              [dcl.spec]/1 says:
13437
13438                  The attribute-specifier-seq affects the type only for
13439                  the declaration it appears in, not other declarations
13440                  involving the same type.
13441
13442              But for now let's force the user to position the
13443              attribute either at the beginning of the declaration or
13444              after the declarator-id, which would clearly mean that it
13445              applies to the declarator.  */
13446           if (cxx11_attribute_p (attrs))
13447             {
13448               if (!found_decl_spec)
13449                 /* The c++11 attribute is at the beginning of the
13450                    declaration.  It appertains to the entity being
13451                    declared.  */;
13452               else
13453                 {
13454                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13455                     {
13456                       /*  This is an attribute following a
13457                           class-specifier.  */
13458                       if (decl_specs->type_definition_p)
13459                         warn_misplaced_attr_for_class_type (token->location,
13460                                                             decl_specs->type);
13461                       attrs = NULL_TREE;
13462                     }
13463                   else
13464                     {
13465                       decl_specs->std_attributes
13466                         = attr_chainon (decl_specs->std_attributes, attrs);
13467                       if (decl_specs->locations[ds_std_attribute] == 0)
13468                         decl_specs->locations[ds_std_attribute] = token->location;
13469                     }
13470                   continue;
13471                 }
13472             }
13473
13474           decl_specs->attributes
13475             = attr_chainon (decl_specs->attributes, attrs);
13476           if (decl_specs->locations[ds_attribute] == 0)
13477             decl_specs->locations[ds_attribute] = token->location;
13478           continue;
13479         }
13480       /* Assume we will find a decl-specifier keyword.  */
13481       found_decl_spec = true;
13482       /* If the next token is an appropriate keyword, we can simply
13483          add it to the list.  */
13484       switch (token->keyword)
13485         {
13486           /* decl-specifier:
13487                friend
13488                constexpr */
13489         case RID_FRIEND:
13490           if (!at_class_scope_p ())
13491             {
13492               gcc_rich_location richloc (token->location);
13493               richloc.add_fixit_remove ();
13494               error_at (&richloc, "%<friend%> used outside of class");
13495               cp_lexer_purge_token (parser->lexer);
13496             }
13497           else
13498             {
13499               ds = ds_friend;
13500               /* Consume the token.  */
13501               cp_lexer_consume_token (parser->lexer);
13502             }
13503           break;
13504
13505         case RID_CONSTEXPR:
13506           ds = ds_constexpr;
13507           cp_lexer_consume_token (parser->lexer);
13508           break;
13509
13510         case RID_CONCEPT:
13511           ds = ds_concept;
13512           cp_lexer_consume_token (parser->lexer);
13513           break;
13514
13515           /* function-specifier:
13516                inline
13517                virtual
13518                explicit  */
13519         case RID_INLINE:
13520         case RID_VIRTUAL:
13521         case RID_EXPLICIT:
13522           cp_parser_function_specifier_opt (parser, decl_specs);
13523           break;
13524
13525           /* decl-specifier:
13526                typedef  */
13527         case RID_TYPEDEF:
13528           ds = ds_typedef;
13529           /* Consume the token.  */
13530           cp_lexer_consume_token (parser->lexer);
13531           /* A constructor declarator cannot appear in a typedef.  */
13532           constructor_possible_p = false;
13533           /* The "typedef" keyword can only occur in a declaration; we
13534              may as well commit at this point.  */
13535           cp_parser_commit_to_tentative_parse (parser);
13536
13537           if (decl_specs->storage_class != sc_none)
13538             decl_specs->conflicting_specifiers_p = true;
13539           break;
13540
13541           /* storage-class-specifier:
13542                auto
13543                register
13544                static
13545                extern
13546                mutable
13547
13548              GNU Extension:
13549                thread  */
13550         case RID_AUTO:
13551           if (cxx_dialect == cxx98) 
13552             {
13553               /* Consume the token.  */
13554               cp_lexer_consume_token (parser->lexer);
13555
13556               /* Complain about `auto' as a storage specifier, if
13557                  we're complaining about C++0x compatibility.  */
13558               gcc_rich_location richloc (token->location);
13559               richloc.add_fixit_remove ();
13560               warning_at (&richloc, OPT_Wc__11_compat,
13561                           "%<auto%> changes meaning in C++11; "
13562                           "please remove it");
13563
13564               /* Set the storage class anyway.  */
13565               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13566                                            token);
13567             }
13568           else
13569             /* C++0x auto type-specifier.  */
13570             found_decl_spec = false;
13571           break;
13572
13573         case RID_REGISTER:
13574         case RID_STATIC:
13575         case RID_EXTERN:
13576         case RID_MUTABLE:
13577           /* Consume the token.  */
13578           cp_lexer_consume_token (parser->lexer);
13579           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13580                                        token);
13581           break;
13582         case RID_THREAD:
13583           /* Consume the token.  */
13584           ds = ds_thread;
13585           cp_lexer_consume_token (parser->lexer);
13586           break;
13587
13588         default:
13589           /* We did not yet find a decl-specifier yet.  */
13590           found_decl_spec = false;
13591           break;
13592         }
13593
13594       if (found_decl_spec
13595           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13596           && token->keyword != RID_CONSTEXPR)
13597         error ("decl-specifier invalid in condition");
13598
13599       if (found_decl_spec
13600           && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13601           && token->keyword != RID_MUTABLE
13602           && token->keyword != RID_CONSTEXPR)
13603         error_at (token->location, "%qD invalid in lambda",
13604                   ridpointers[token->keyword]);
13605
13606       if (ds != ds_last)
13607         set_and_check_decl_spec_loc (decl_specs, ds, token);
13608
13609       /* Constructors are a special case.  The `S' in `S()' is not a
13610          decl-specifier; it is the beginning of the declarator.  */
13611       constructor_p
13612         = (!found_decl_spec
13613            && constructor_possible_p
13614            && (cp_parser_constructor_declarator_p
13615                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13616
13617       /* If we don't have a DECL_SPEC yet, then we must be looking at
13618          a type-specifier.  */
13619       if (!found_decl_spec && !constructor_p)
13620         {
13621           int decl_spec_declares_class_or_enum;
13622           bool is_cv_qualifier;
13623           tree type_spec;
13624
13625           type_spec
13626             = cp_parser_type_specifier (parser, flags,
13627                                         decl_specs,
13628                                         /*is_declaration=*/true,
13629                                         &decl_spec_declares_class_or_enum,
13630                                         &is_cv_qualifier);
13631           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13632
13633           /* If this type-specifier referenced a user-defined type
13634              (a typedef, class-name, etc.), then we can't allow any
13635              more such type-specifiers henceforth.
13636
13637              [dcl.spec]
13638
13639              The longest sequence of decl-specifiers that could
13640              possibly be a type name is taken as the
13641              decl-specifier-seq of a declaration.  The sequence shall
13642              be self-consistent as described below.
13643
13644              [dcl.type]
13645
13646              As a general rule, at most one type-specifier is allowed
13647              in the complete decl-specifier-seq of a declaration.  The
13648              only exceptions are the following:
13649
13650              -- const or volatile can be combined with any other
13651                 type-specifier.
13652
13653              -- signed or unsigned can be combined with char, long,
13654                 short, or int.
13655
13656              -- ..
13657
13658              Example:
13659
13660                typedef char* Pc;
13661                void g (const int Pc);
13662
13663              Here, Pc is *not* part of the decl-specifier seq; it's
13664              the declarator.  Therefore, once we see a type-specifier
13665              (other than a cv-qualifier), we forbid any additional
13666              user-defined types.  We *do* still allow things like `int
13667              int' to be considered a decl-specifier-seq, and issue the
13668              error message later.  */
13669           if (type_spec && !is_cv_qualifier)
13670             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13671           /* A constructor declarator cannot follow a type-specifier.  */
13672           if (type_spec)
13673             {
13674               constructor_possible_p = false;
13675               found_decl_spec = true;
13676               if (!is_cv_qualifier)
13677                 decl_specs->any_type_specifiers_p = true;
13678             }
13679         }
13680
13681       /* If we still do not have a DECL_SPEC, then there are no more
13682          decl-specifiers.  */
13683       if (!found_decl_spec)
13684         break;
13685
13686       decl_specs->any_specifiers_p = true;
13687       /* After we see one decl-specifier, further decl-specifiers are
13688          always optional.  */
13689       flags |= CP_PARSER_FLAGS_OPTIONAL;
13690     }
13691
13692   /* Don't allow a friend specifier with a class definition.  */
13693   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13694       && (*declares_class_or_enum & 2))
13695     error_at (decl_specs->locations[ds_friend],
13696               "class definition may not be declared a friend");
13697 }
13698
13699 /* Parse an (optional) storage-class-specifier.
13700
13701    storage-class-specifier:
13702      auto
13703      register
13704      static
13705      extern
13706      mutable
13707
13708    GNU Extension:
13709
13710    storage-class-specifier:
13711      thread
13712
13713    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
13714
13715 static tree
13716 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13717 {
13718   switch (cp_lexer_peek_token (parser->lexer)->keyword)
13719     {
13720     case RID_AUTO:
13721       if (cxx_dialect != cxx98)
13722         return NULL_TREE;
13723       /* Fall through for C++98.  */
13724       gcc_fallthrough ();
13725
13726     case RID_REGISTER:
13727     case RID_STATIC:
13728     case RID_EXTERN:
13729     case RID_MUTABLE:
13730     case RID_THREAD:
13731       /* Consume the token.  */
13732       return cp_lexer_consume_token (parser->lexer)->u.value;
13733
13734     default:
13735       return NULL_TREE;
13736     }
13737 }
13738
13739 /* Parse an (optional) function-specifier.
13740
13741    function-specifier:
13742      inline
13743      virtual
13744      explicit
13745
13746    Returns an IDENTIFIER_NODE corresponding to the keyword used.
13747    Updates DECL_SPECS, if it is non-NULL.  */
13748
13749 static tree
13750 cp_parser_function_specifier_opt (cp_parser* parser,
13751                                   cp_decl_specifier_seq *decl_specs)
13752 {
13753   cp_token *token = cp_lexer_peek_token (parser->lexer);
13754   switch (token->keyword)
13755     {
13756     case RID_INLINE:
13757       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13758       break;
13759
13760     case RID_VIRTUAL:
13761       /* 14.5.2.3 [temp.mem]
13762
13763          A member function template shall not be virtual.  */
13764       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13765           && current_class_type)
13766         error_at (token->location, "templates may not be %<virtual%>");
13767       else
13768         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13769       break;
13770
13771     case RID_EXPLICIT:
13772       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13773       break;
13774
13775     default:
13776       return NULL_TREE;
13777     }
13778
13779   /* Consume the token.  */
13780   return cp_lexer_consume_token (parser->lexer)->u.value;
13781 }
13782
13783 /* Parse a linkage-specification.
13784
13785    linkage-specification:
13786      extern string-literal { declaration-seq [opt] }
13787      extern string-literal declaration  */
13788
13789 static void
13790 cp_parser_linkage_specification (cp_parser* parser)
13791 {
13792   tree linkage;
13793
13794   /* Look for the `extern' keyword.  */
13795   cp_token *extern_token
13796     = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13797
13798   /* Look for the string-literal.  */
13799   cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13800   linkage = cp_parser_string_literal (parser, false, false);
13801
13802   /* Transform the literal into an identifier.  If the literal is a
13803      wide-character string, or contains embedded NULs, then we can't
13804      handle it as the user wants.  */
13805   if (strlen (TREE_STRING_POINTER (linkage))
13806       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13807     {
13808       cp_parser_error (parser, "invalid linkage-specification");
13809       /* Assume C++ linkage.  */
13810       linkage = lang_name_cplusplus;
13811     }
13812   else
13813     linkage = get_identifier (TREE_STRING_POINTER (linkage));
13814
13815   /* We're now using the new linkage.  */
13816   push_lang_context (linkage);
13817
13818   /* Preserve the location of the the innermost linkage specification,
13819      tracking the locations of nested specifications via a local.  */
13820   location_t saved_location
13821     = parser->innermost_linkage_specification_location;
13822   /* Construct a location ranging from the start of the "extern" to
13823      the end of the string-literal, with the caret at the start, e.g.:
13824        extern "C" {
13825        ^~~~~~~~~~
13826   */
13827   parser->innermost_linkage_specification_location
13828     = make_location (extern_token->location,
13829                      extern_token->location,
13830                      get_finish (string_token->location));
13831
13832   /* If the next token is a `{', then we're using the first
13833      production.  */
13834   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13835     {
13836       cp_ensure_no_omp_declare_simd (parser);
13837       cp_ensure_no_oacc_routine (parser);
13838
13839       /* Consume the `{' token.  */
13840       matching_braces braces;
13841       braces.consume_open (parser)->location;
13842       /* Parse the declarations.  */
13843       cp_parser_declaration_seq_opt (parser);
13844       /* Look for the closing `}'.  */
13845       braces.require_close (parser);
13846     }
13847   /* Otherwise, there's just one declaration.  */
13848   else
13849     {
13850       bool saved_in_unbraced_linkage_specification_p;
13851
13852       saved_in_unbraced_linkage_specification_p
13853         = parser->in_unbraced_linkage_specification_p;
13854       parser->in_unbraced_linkage_specification_p = true;
13855       cp_parser_declaration (parser);
13856       parser->in_unbraced_linkage_specification_p
13857         = saved_in_unbraced_linkage_specification_p;
13858     }
13859
13860   /* We're done with the linkage-specification.  */
13861   pop_lang_context ();
13862
13863   /* Restore location of parent linkage specification, if any.  */
13864   parser->innermost_linkage_specification_location = saved_location;
13865 }
13866
13867 /* Parse a static_assert-declaration.
13868
13869    static_assert-declaration:
13870      static_assert ( constant-expression , string-literal ) ; 
13871      static_assert ( constant-expression ) ; (C++17)
13872
13873    If MEMBER_P, this static_assert is a class member.  */
13874
13875 static void 
13876 cp_parser_static_assert(cp_parser *parser, bool member_p)
13877 {
13878   cp_expr condition;
13879   location_t token_loc;
13880   tree message;
13881   bool dummy;
13882
13883   /* Peek at the `static_assert' token so we can keep track of exactly
13884      where the static assertion started.  */
13885   token_loc = cp_lexer_peek_token (parser->lexer)->location;
13886
13887   /* Look for the `static_assert' keyword.  */
13888   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
13889                                   RT_STATIC_ASSERT))
13890     return;
13891
13892   /*  We know we are in a static assertion; commit to any tentative
13893       parse.  */
13894   if (cp_parser_parsing_tentatively (parser))
13895     cp_parser_commit_to_tentative_parse (parser);
13896
13897   /* Parse the `(' starting the static assertion condition.  */
13898   matching_parens parens;
13899   parens.require_open (parser);
13900
13901   /* Parse the constant-expression.  Allow a non-constant expression
13902      here in order to give better diagnostics in finish_static_assert.  */
13903   condition = 
13904     cp_parser_constant_expression (parser,
13905                                    /*allow_non_constant_p=*/true,
13906                                    /*non_constant_p=*/&dummy);
13907
13908   if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13909     {
13910       if (cxx_dialect < cxx17)
13911         pedwarn (input_location, OPT_Wpedantic,
13912                  "static_assert without a message "
13913                  "only available with -std=c++17 or -std=gnu++17");
13914       /* Eat the ')'  */
13915       cp_lexer_consume_token (parser->lexer);
13916       message = build_string (1, "");
13917       TREE_TYPE (message) = char_array_type_node;
13918       fix_string_type (message);
13919     }
13920   else
13921     {
13922       /* Parse the separating `,'.  */
13923       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13924
13925       /* Parse the string-literal message.  */
13926       message = cp_parser_string_literal (parser, 
13927                                           /*translate=*/false,
13928                                           /*wide_ok=*/true);
13929
13930       /* A `)' completes the static assertion.  */
13931       if (!parens.require_close (parser))
13932         cp_parser_skip_to_closing_parenthesis (parser, 
13933                                                /*recovering=*/true, 
13934                                                /*or_comma=*/false,
13935                                                /*consume_paren=*/true);
13936     }
13937
13938   /* A semicolon terminates the declaration.  */
13939   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13940
13941   /* Get the location for the static assertion.  Use that of the
13942      condition if available, otherwise, use that of the "static_assert"
13943      token.  */
13944   location_t assert_loc = condition.get_location ();
13945   if (assert_loc == UNKNOWN_LOCATION)
13946     assert_loc = token_loc;
13947
13948   /* Complete the static assertion, which may mean either processing 
13949      the static assert now or saving it for template instantiation.  */
13950   finish_static_assert (condition, message, assert_loc, member_p);
13951 }
13952
13953 /* Parse the expression in decltype ( expression ).  */
13954
13955 static tree
13956 cp_parser_decltype_expr (cp_parser *parser,
13957                          bool &id_expression_or_member_access_p)
13958 {
13959   cp_token *id_expr_start_token;
13960   tree expr;
13961
13962   /* Since we're going to preserve any side-effects from this parse, set up a
13963      firewall to protect our callers from cp_parser_commit_to_tentative_parse
13964      in the expression.  */
13965   tentative_firewall firewall (parser);
13966
13967   /* First, try parsing an id-expression.  */
13968   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13969   cp_parser_parse_tentatively (parser);
13970   expr = cp_parser_id_expression (parser,
13971                                   /*template_keyword_p=*/false,
13972                                   /*check_dependency_p=*/true,
13973                                   /*template_p=*/NULL,
13974                                   /*declarator_p=*/false,
13975                                   /*optional_p=*/false);
13976
13977   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13978     {
13979       bool non_integral_constant_expression_p = false;
13980       tree id_expression = expr;
13981       cp_id_kind idk;
13982       const char *error_msg;
13983
13984       if (identifier_p (expr))
13985         /* Lookup the name we got back from the id-expression.  */
13986         expr = cp_parser_lookup_name_simple (parser, expr,
13987                                              id_expr_start_token->location);
13988
13989       if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13990         /* A template without args is not a complete id-expression.  */
13991         expr = error_mark_node;
13992
13993       if (expr
13994           && expr != error_mark_node
13995           && TREE_CODE (expr) != TYPE_DECL
13996           && (TREE_CODE (expr) != BIT_NOT_EXPR
13997               || !TYPE_P (TREE_OPERAND (expr, 0)))
13998           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13999         {
14000           /* Complete lookup of the id-expression.  */
14001           expr = (finish_id_expression
14002                   (id_expression, expr, parser->scope, &idk,
14003                    /*integral_constant_expression_p=*/false,
14004                    /*allow_non_integral_constant_expression_p=*/true,
14005                    &non_integral_constant_expression_p,
14006                    /*template_p=*/false,
14007                    /*done=*/true,
14008                    /*address_p=*/false,
14009                    /*template_arg_p=*/false,
14010                    &error_msg,
14011                    id_expr_start_token->location));
14012
14013           if (expr == error_mark_node)
14014             /* We found an id-expression, but it was something that we
14015                should not have found. This is an error, not something
14016                we can recover from, so note that we found an
14017                id-expression and we'll recover as gracefully as
14018                possible.  */
14019             id_expression_or_member_access_p = true;
14020         }
14021
14022       if (expr 
14023           && expr != error_mark_node
14024           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14025         /* We have an id-expression.  */
14026         id_expression_or_member_access_p = true;
14027     }
14028
14029   if (!id_expression_or_member_access_p)
14030     {
14031       /* Abort the id-expression parse.  */
14032       cp_parser_abort_tentative_parse (parser);
14033
14034       /* Parsing tentatively, again.  */
14035       cp_parser_parse_tentatively (parser);
14036
14037       /* Parse a class member access.  */
14038       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14039                                            /*cast_p=*/false, /*decltype*/true,
14040                                            /*member_access_only_p=*/true, NULL);
14041
14042       if (expr 
14043           && expr != error_mark_node
14044           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14045         /* We have an id-expression.  */
14046         id_expression_or_member_access_p = true;
14047     }
14048
14049   if (id_expression_or_member_access_p)
14050     /* We have parsed the complete id-expression or member access.  */
14051     cp_parser_parse_definitely (parser);
14052   else
14053     {
14054       /* Abort our attempt to parse an id-expression or member access
14055          expression.  */
14056       cp_parser_abort_tentative_parse (parser);
14057
14058       /* Commit to the tentative_firewall so we get syntax errors.  */
14059       cp_parser_commit_to_tentative_parse (parser);
14060
14061       /* Parse a full expression.  */
14062       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14063                                    /*decltype_p=*/true);
14064     }
14065
14066   return expr;
14067 }
14068
14069 /* Parse a `decltype' type. Returns the type.
14070
14071    simple-type-specifier:
14072      decltype ( expression )
14073    C++14 proposal:
14074      decltype ( auto )  */
14075
14076 static tree
14077 cp_parser_decltype (cp_parser *parser)
14078 {
14079   bool id_expression_or_member_access_p = false;
14080   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14081
14082   if (start_token->type == CPP_DECLTYPE)
14083     {
14084       /* Already parsed.  */
14085       cp_lexer_consume_token (parser->lexer);
14086       return saved_checks_value (start_token->u.tree_check_value);
14087     }
14088
14089   /* Look for the `decltype' token.  */
14090   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14091     return error_mark_node;
14092
14093   /* Parse the opening `('.  */
14094   matching_parens parens;
14095   if (!parens.require_open (parser))
14096     return error_mark_node;
14097
14098   push_deferring_access_checks (dk_deferred);
14099
14100   tree expr = NULL_TREE;
14101   
14102   if (cxx_dialect >= cxx14
14103       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14104     /* decltype (auto) */
14105     cp_lexer_consume_token (parser->lexer);
14106   else
14107     {
14108       /* decltype (expression)  */
14109
14110       /* Types cannot be defined in a `decltype' expression.  Save away the
14111          old message and set the new one.  */
14112       const char *saved_message = parser->type_definition_forbidden_message;
14113       parser->type_definition_forbidden_message
14114         = G_("types may not be defined in %<decltype%> expressions");
14115
14116       /* The restrictions on constant-expressions do not apply inside
14117          decltype expressions.  */
14118       bool saved_integral_constant_expression_p
14119         = parser->integral_constant_expression_p;
14120       bool saved_non_integral_constant_expression_p
14121         = parser->non_integral_constant_expression_p;
14122       parser->integral_constant_expression_p = false;
14123
14124       /* Within a parenthesized expression, a `>' token is always
14125          the greater-than operator.  */
14126       bool saved_greater_than_is_operator_p
14127         = parser->greater_than_is_operator_p;
14128       parser->greater_than_is_operator_p = true;
14129
14130       /* Do not actually evaluate the expression.  */
14131       ++cp_unevaluated_operand;
14132
14133       /* Do not warn about problems with the expression.  */
14134       ++c_inhibit_evaluation_warnings;
14135
14136       expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14137
14138       /* Go back to evaluating expressions.  */
14139       --cp_unevaluated_operand;
14140       --c_inhibit_evaluation_warnings;
14141
14142       /* The `>' token might be the end of a template-id or
14143          template-parameter-list now.  */
14144       parser->greater_than_is_operator_p
14145         = saved_greater_than_is_operator_p;
14146
14147       /* Restore the old message and the integral constant expression
14148          flags.  */
14149       parser->type_definition_forbidden_message = saved_message;
14150       parser->integral_constant_expression_p
14151         = saved_integral_constant_expression_p;
14152       parser->non_integral_constant_expression_p
14153         = saved_non_integral_constant_expression_p;
14154     }
14155
14156   /* Parse to the closing `)'.  */
14157   if (!parens.require_close (parser))
14158     {
14159       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14160                                              /*consume_paren=*/true);
14161       pop_deferring_access_checks ();
14162       return error_mark_node;
14163     }
14164
14165   if (!expr)
14166     {
14167       /* Build auto.  */
14168       expr = make_decltype_auto ();
14169       AUTO_IS_DECLTYPE (expr) = true;
14170     }
14171   else
14172     expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14173                                  tf_warning_or_error);
14174
14175   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14176      it again.  */
14177   start_token->type = CPP_DECLTYPE;
14178   start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14179   start_token->u.tree_check_value->value = expr;
14180   start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14181   start_token->keyword = RID_MAX;
14182   cp_lexer_purge_tokens_after (parser->lexer, start_token);
14183
14184   pop_to_parent_deferring_access_checks ();
14185   
14186   return expr;
14187 }
14188
14189 /* Special member functions [gram.special] */
14190
14191 /* Parse a conversion-function-id.
14192
14193    conversion-function-id:
14194      operator conversion-type-id
14195
14196    Returns an IDENTIFIER_NODE representing the operator.  */
14197
14198 static tree
14199 cp_parser_conversion_function_id (cp_parser* parser)
14200 {
14201   tree type;
14202   tree saved_scope;
14203   tree saved_qualifying_scope;
14204   tree saved_object_scope;
14205   tree pushed_scope = NULL_TREE;
14206
14207   /* Look for the `operator' token.  */
14208   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14209     return error_mark_node;
14210   /* When we parse the conversion-type-id, the current scope will be
14211      reset.  However, we need that information in able to look up the
14212      conversion function later, so we save it here.  */
14213   saved_scope = parser->scope;
14214   saved_qualifying_scope = parser->qualifying_scope;
14215   saved_object_scope = parser->object_scope;
14216   /* We must enter the scope of the class so that the names of
14217      entities declared within the class are available in the
14218      conversion-type-id.  For example, consider:
14219
14220        struct S {
14221          typedef int I;
14222          operator I();
14223        };
14224
14225        S::operator I() { ... }
14226
14227      In order to see that `I' is a type-name in the definition, we
14228      must be in the scope of `S'.  */
14229   if (saved_scope)
14230     pushed_scope = push_scope (saved_scope);
14231   /* Parse the conversion-type-id.  */
14232   type = cp_parser_conversion_type_id (parser);
14233   /* Leave the scope of the class, if any.  */
14234   if (pushed_scope)
14235     pop_scope (pushed_scope);
14236   /* Restore the saved scope.  */
14237   parser->scope = saved_scope;
14238   parser->qualifying_scope = saved_qualifying_scope;
14239   parser->object_scope = saved_object_scope;
14240   /* If the TYPE is invalid, indicate failure.  */
14241   if (type == error_mark_node)
14242     return error_mark_node;
14243   return make_conv_op_name (type);
14244 }
14245
14246 /* Parse a conversion-type-id:
14247
14248    conversion-type-id:
14249      type-specifier-seq conversion-declarator [opt]
14250
14251    Returns the TYPE specified.  */
14252
14253 static tree
14254 cp_parser_conversion_type_id (cp_parser* parser)
14255 {
14256   tree attributes;
14257   cp_decl_specifier_seq type_specifiers;
14258   cp_declarator *declarator;
14259   tree type_specified;
14260   const char *saved_message;
14261
14262   /* Parse the attributes.  */
14263   attributes = cp_parser_attributes_opt (parser);
14264
14265   saved_message = parser->type_definition_forbidden_message;
14266   parser->type_definition_forbidden_message
14267     = G_("types may not be defined in a conversion-type-id");
14268
14269   /* Parse the type-specifiers.  */
14270   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14271                                 /*is_trailing_return=*/false,
14272                                 &type_specifiers);
14273
14274   parser->type_definition_forbidden_message = saved_message;
14275
14276   /* If that didn't work, stop.  */
14277   if (type_specifiers.type == error_mark_node)
14278     return error_mark_node;
14279   /* Parse the conversion-declarator.  */
14280   declarator = cp_parser_conversion_declarator_opt (parser);
14281
14282   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
14283                                     /*initialized=*/0, &attributes);
14284   if (attributes)
14285     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14286
14287   /* Don't give this error when parsing tentatively.  This happens to
14288      work because we always parse this definitively once.  */
14289   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14290       && type_uses_auto (type_specified))
14291     {
14292       if (cxx_dialect < cxx14)
14293         {
14294           error ("invalid use of %<auto%> in conversion operator");
14295           return error_mark_node;
14296         }
14297       else if (template_parm_scope_p ())
14298         warning (0, "use of %<auto%> in member template "
14299                  "conversion operator can never be deduced");
14300     }
14301
14302   return type_specified;
14303 }
14304
14305 /* Parse an (optional) conversion-declarator.
14306
14307    conversion-declarator:
14308      ptr-operator conversion-declarator [opt]
14309
14310    */
14311
14312 static cp_declarator *
14313 cp_parser_conversion_declarator_opt (cp_parser* parser)
14314 {
14315   enum tree_code code;
14316   tree class_type, std_attributes = NULL_TREE;
14317   cp_cv_quals cv_quals;
14318
14319   /* We don't know if there's a ptr-operator next, or not.  */
14320   cp_parser_parse_tentatively (parser);
14321   /* Try the ptr-operator.  */
14322   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14323                                  &std_attributes);
14324   /* If it worked, look for more conversion-declarators.  */
14325   if (cp_parser_parse_definitely (parser))
14326     {
14327       cp_declarator *declarator;
14328
14329       /* Parse another optional declarator.  */
14330       declarator = cp_parser_conversion_declarator_opt (parser);
14331
14332       declarator = cp_parser_make_indirect_declarator
14333         (code, class_type, cv_quals, declarator, std_attributes);
14334
14335       return declarator;
14336    }
14337
14338   return NULL;
14339 }
14340
14341 /* Parse an (optional) ctor-initializer.
14342
14343    ctor-initializer:
14344      : mem-initializer-list  */
14345
14346 static void
14347 cp_parser_ctor_initializer_opt (cp_parser* parser)
14348 {
14349   /* If the next token is not a `:', then there is no
14350      ctor-initializer.  */
14351   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14352     {
14353       /* Do default initialization of any bases and members.  */
14354       if (DECL_CONSTRUCTOR_P (current_function_decl))
14355         finish_mem_initializers (NULL_TREE);
14356       return;
14357     }
14358
14359   /* Consume the `:' token.  */
14360   cp_lexer_consume_token (parser->lexer);
14361   /* And the mem-initializer-list.  */
14362   cp_parser_mem_initializer_list (parser);
14363 }
14364
14365 /* Parse a mem-initializer-list.
14366
14367    mem-initializer-list:
14368      mem-initializer ... [opt]
14369      mem-initializer ... [opt] , mem-initializer-list  */
14370
14371 static void
14372 cp_parser_mem_initializer_list (cp_parser* parser)
14373 {
14374   tree mem_initializer_list = NULL_TREE;
14375   tree target_ctor = error_mark_node;
14376   cp_token *token = cp_lexer_peek_token (parser->lexer);
14377
14378   /* Let the semantic analysis code know that we are starting the
14379      mem-initializer-list.  */
14380   if (!DECL_CONSTRUCTOR_P (current_function_decl))
14381     error_at (token->location,
14382               "only constructors take member initializers");
14383
14384   /* Loop through the list.  */
14385   while (true)
14386     {
14387       tree mem_initializer;
14388
14389       token = cp_lexer_peek_token (parser->lexer);
14390       /* Parse the mem-initializer.  */
14391       mem_initializer = cp_parser_mem_initializer (parser);
14392       /* If the next token is a `...', we're expanding member initializers. */
14393       bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14394       if (ellipsis
14395           || (mem_initializer != error_mark_node
14396               && check_for_bare_parameter_packs (TREE_PURPOSE
14397                                                  (mem_initializer))))
14398         {
14399           /* Consume the `...'. */
14400           if (ellipsis)
14401             cp_lexer_consume_token (parser->lexer);
14402
14403           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14404              can be expanded but members cannot. */
14405           if (mem_initializer != error_mark_node
14406               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14407             {
14408               error_at (token->location,
14409                         "cannot expand initializer for member %qD",
14410                         TREE_PURPOSE (mem_initializer));
14411               mem_initializer = error_mark_node;
14412             }
14413
14414           /* Construct the pack expansion type. */
14415           if (mem_initializer != error_mark_node)
14416             mem_initializer = make_pack_expansion (mem_initializer);
14417         }
14418       if (target_ctor != error_mark_node
14419           && mem_initializer != error_mark_node)
14420         {
14421           error ("mem-initializer for %qD follows constructor delegation",
14422                  TREE_PURPOSE (mem_initializer));
14423           mem_initializer = error_mark_node;
14424         }
14425       /* Look for a target constructor. */
14426       if (mem_initializer != error_mark_node
14427           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14428           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14429         {
14430           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14431           if (mem_initializer_list)
14432             {
14433               error ("constructor delegation follows mem-initializer for %qD",
14434                      TREE_PURPOSE (mem_initializer_list));
14435               mem_initializer = error_mark_node;
14436             }
14437           target_ctor = mem_initializer;
14438         }
14439       /* Add it to the list, unless it was erroneous.  */
14440       if (mem_initializer != error_mark_node)
14441         {
14442           TREE_CHAIN (mem_initializer) = mem_initializer_list;
14443           mem_initializer_list = mem_initializer;
14444         }
14445       /* If the next token is not a `,', we're done.  */
14446       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14447         break;
14448       /* Consume the `,' token.  */
14449       cp_lexer_consume_token (parser->lexer);
14450     }
14451
14452   /* Perform semantic analysis.  */
14453   if (DECL_CONSTRUCTOR_P (current_function_decl))
14454     finish_mem_initializers (mem_initializer_list);
14455 }
14456
14457 /* Parse a mem-initializer.
14458
14459    mem-initializer:
14460      mem-initializer-id ( expression-list [opt] )
14461      mem-initializer-id braced-init-list
14462
14463    GNU extension:
14464
14465    mem-initializer:
14466      ( expression-list [opt] )
14467
14468    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
14469    class) or FIELD_DECL (for a non-static data member) to initialize;
14470    the TREE_VALUE is the expression-list.  An empty initialization
14471    list is represented by void_list_node.  */
14472
14473 static tree
14474 cp_parser_mem_initializer (cp_parser* parser)
14475 {
14476   tree mem_initializer_id;
14477   tree expression_list;
14478   tree member;
14479   cp_token *token = cp_lexer_peek_token (parser->lexer);
14480
14481   /* Find out what is being initialized.  */
14482   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14483     {
14484       permerror (token->location,
14485                  "anachronistic old-style base class initializer");
14486       mem_initializer_id = NULL_TREE;
14487     }
14488   else
14489     {
14490       mem_initializer_id = cp_parser_mem_initializer_id (parser);
14491       if (mem_initializer_id == error_mark_node)
14492         return mem_initializer_id;
14493     }
14494   member = expand_member_init (mem_initializer_id);
14495   if (member && !DECL_P (member))
14496     in_base_initializer = 1;
14497
14498   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14499     {
14500       bool expr_non_constant_p;
14501       cp_lexer_set_source_position (parser->lexer);
14502       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14503       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14504       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14505       expression_list = build_tree_list (NULL_TREE, expression_list);
14506     }
14507   else
14508     {
14509       vec<tree, va_gc> *vec;
14510       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14511                                                      /*cast_p=*/false,
14512                                                      /*allow_expansion_p=*/true,
14513                                                      /*non_constant_p=*/NULL);
14514       if (vec == NULL)
14515         return error_mark_node;
14516       expression_list = build_tree_list_vec (vec);
14517       release_tree_vector (vec);
14518     }
14519
14520   if (expression_list == error_mark_node)
14521     return error_mark_node;
14522   if (!expression_list)
14523     expression_list = void_type_node;
14524
14525   in_base_initializer = 0;
14526
14527   return member ? build_tree_list (member, expression_list) : error_mark_node;
14528 }
14529
14530 /* Parse a mem-initializer-id.
14531
14532    mem-initializer-id:
14533      :: [opt] nested-name-specifier [opt] class-name
14534      decltype-specifier (C++11)
14535      identifier
14536
14537    Returns a TYPE indicating the class to be initialized for the first
14538    production (and the second in C++11).  Returns an IDENTIFIER_NODE
14539    indicating the data member to be initialized for the last production.  */
14540
14541 static tree
14542 cp_parser_mem_initializer_id (cp_parser* parser)
14543 {
14544   bool global_scope_p;
14545   bool nested_name_specifier_p;
14546   bool template_p = false;
14547   tree id;
14548
14549   cp_token *token = cp_lexer_peek_token (parser->lexer);
14550
14551   /* `typename' is not allowed in this context ([temp.res]).  */
14552   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14553     {
14554       error_at (token->location, 
14555                 "keyword %<typename%> not allowed in this context (a qualified "
14556                 "member initializer is implicitly a type)");
14557       cp_lexer_consume_token (parser->lexer);
14558     }
14559   /* Look for the optional `::' operator.  */
14560   global_scope_p
14561     = (cp_parser_global_scope_opt (parser,
14562                                    /*current_scope_valid_p=*/false)
14563        != NULL_TREE);
14564   /* Look for the optional nested-name-specifier.  The simplest way to
14565      implement:
14566
14567        [temp.res]
14568
14569        The keyword `typename' is not permitted in a base-specifier or
14570        mem-initializer; in these contexts a qualified name that
14571        depends on a template-parameter is implicitly assumed to be a
14572        type name.
14573
14574      is to assume that we have seen the `typename' keyword at this
14575      point.  */
14576   nested_name_specifier_p
14577     = (cp_parser_nested_name_specifier_opt (parser,
14578                                             /*typename_keyword_p=*/true,
14579                                             /*check_dependency_p=*/true,
14580                                             /*type_p=*/true,
14581                                             /*is_declaration=*/true)
14582        != NULL_TREE);
14583   if (nested_name_specifier_p)
14584     template_p = cp_parser_optional_template_keyword (parser);
14585   /* If there is a `::' operator or a nested-name-specifier, then we
14586      are definitely looking for a class-name.  */
14587   if (global_scope_p || nested_name_specifier_p)
14588     return cp_parser_class_name (parser,
14589                                  /*typename_keyword_p=*/true,
14590                                  /*template_keyword_p=*/template_p,
14591                                  typename_type,
14592                                  /*check_dependency_p=*/true,
14593                                  /*class_head_p=*/false,
14594                                  /*is_declaration=*/true);
14595   /* Otherwise, we could also be looking for an ordinary identifier.  */
14596   cp_parser_parse_tentatively (parser);
14597   if (cp_lexer_next_token_is_decltype (parser->lexer))
14598     /* Try a decltype-specifier.  */
14599     id = cp_parser_decltype (parser);
14600   else
14601     /* Otherwise, try a class-name.  */
14602     id = cp_parser_class_name (parser,
14603                                /*typename_keyword_p=*/true,
14604                                /*template_keyword_p=*/false,
14605                                none_type,
14606                                /*check_dependency_p=*/true,
14607                                /*class_head_p=*/false,
14608                                /*is_declaration=*/true);
14609   /* If we found one, we're done.  */
14610   if (cp_parser_parse_definitely (parser))
14611     return id;
14612   /* Otherwise, look for an ordinary identifier.  */
14613   return cp_parser_identifier (parser);
14614 }
14615
14616 /* Overloading [gram.over] */
14617
14618 /* Parse an operator-function-id.
14619
14620    operator-function-id:
14621      operator operator
14622
14623    Returns an IDENTIFIER_NODE for the operator which is a
14624    human-readable spelling of the identifier, e.g., `operator +'.  */
14625
14626 static cp_expr
14627 cp_parser_operator_function_id (cp_parser* parser)
14628 {
14629   /* Look for the `operator' keyword.  */
14630   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14631     return error_mark_node;
14632   /* And then the name of the operator itself.  */
14633   return cp_parser_operator (parser);
14634 }
14635
14636 /* Return an identifier node for a user-defined literal operator.
14637    The suffix identifier is chained to the operator name identifier.  */
14638
14639 tree
14640 cp_literal_operator_id (const char* name)
14641 {
14642   tree identifier;
14643   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14644                               + strlen (name) + 10);
14645   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14646   identifier = get_identifier (buffer);
14647
14648   return identifier;
14649 }
14650
14651 /* Parse an operator.
14652
14653    operator:
14654      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14655      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14656      || ++ -- , ->* -> () []
14657
14658    GNU Extensions:
14659
14660    operator:
14661      <? >? <?= >?=
14662
14663    Returns an IDENTIFIER_NODE for the operator which is a
14664    human-readable spelling of the identifier, e.g., `operator +'.  */
14665
14666 static cp_expr
14667 cp_parser_operator (cp_parser* parser)
14668 {
14669   tree id = NULL_TREE;
14670   cp_token *token;
14671   bool utf8 = false;
14672
14673   /* Peek at the next token.  */
14674   token = cp_lexer_peek_token (parser->lexer);
14675
14676   location_t start_loc = token->location;
14677
14678   /* Figure out which operator we have.  */
14679   enum tree_code op = ERROR_MARK;
14680   bool assop = false;
14681   bool consumed = false;
14682   switch (token->type)
14683     {
14684     case CPP_KEYWORD:
14685       {
14686         /* The keyword should be either `new' or `delete'.  */
14687         if (token->keyword == RID_NEW)
14688           op = NEW_EXPR;
14689         else if (token->keyword == RID_DELETE)
14690           op = DELETE_EXPR;
14691         else
14692           break;
14693
14694         /* Consume the `new' or `delete' token.  */
14695         location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14696
14697         /* Peek at the next token.  */
14698         token = cp_lexer_peek_token (parser->lexer);
14699         /* If it's a `[' token then this is the array variant of the
14700            operator.  */
14701         if (token->type == CPP_OPEN_SQUARE)
14702           {
14703             /* Consume the `[' token.  */
14704             cp_lexer_consume_token (parser->lexer);
14705             /* Look for the `]' token.  */
14706             if (cp_token *close_token
14707                 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14708               end_loc = close_token->location;
14709             op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14710           }
14711         start_loc = make_location (start_loc, start_loc, end_loc);
14712         consumed = true;
14713         break;
14714       }
14715
14716     case CPP_PLUS:
14717       op = PLUS_EXPR;
14718       break;
14719
14720     case CPP_MINUS:
14721       op = MINUS_EXPR;
14722       break;
14723
14724     case CPP_MULT:
14725       op = MULT_EXPR;
14726       break;
14727
14728     case CPP_DIV:
14729       op = TRUNC_DIV_EXPR;
14730       break;
14731
14732     case CPP_MOD:
14733       op = TRUNC_MOD_EXPR;
14734       break;
14735
14736     case CPP_XOR:
14737       op = BIT_XOR_EXPR;
14738       break;
14739
14740     case CPP_AND:
14741       op = BIT_AND_EXPR;
14742       break;
14743
14744     case CPP_OR:
14745       op = BIT_IOR_EXPR;
14746       break;
14747
14748     case CPP_COMPL:
14749       op = BIT_NOT_EXPR;
14750       break;
14751
14752     case CPP_NOT:
14753       op = TRUTH_NOT_EXPR;
14754       break;
14755
14756     case CPP_EQ:
14757       assop = true;
14758       op = NOP_EXPR;
14759       break;
14760
14761     case CPP_LESS:
14762       op = LT_EXPR;
14763       break;
14764
14765     case CPP_GREATER:
14766       op = GT_EXPR;
14767       break;
14768
14769     case CPP_PLUS_EQ:
14770       assop = true;
14771       op = PLUS_EXPR;
14772       break;
14773
14774     case CPP_MINUS_EQ:
14775       assop = true;
14776       op = MINUS_EXPR;
14777       break;
14778
14779     case CPP_MULT_EQ:
14780       assop = true;
14781       op = MULT_EXPR;
14782       break;
14783
14784     case CPP_DIV_EQ:
14785       assop = true;
14786       op = TRUNC_DIV_EXPR;
14787       break;
14788
14789     case CPP_MOD_EQ:
14790       assop = true;
14791       op = TRUNC_MOD_EXPR;
14792       break;
14793
14794     case CPP_XOR_EQ:
14795       assop = true;
14796       op = BIT_XOR_EXPR;
14797       break;
14798
14799     case CPP_AND_EQ:
14800       assop = true;
14801       op = BIT_AND_EXPR;
14802       break;
14803
14804     case CPP_OR_EQ:
14805       assop = true;
14806       op = BIT_IOR_EXPR;
14807       break;
14808
14809     case CPP_LSHIFT:
14810       op = LSHIFT_EXPR;
14811       break;
14812
14813     case CPP_RSHIFT:
14814       op = RSHIFT_EXPR;
14815       break;
14816
14817     case CPP_LSHIFT_EQ:
14818       assop = true;
14819       op = LSHIFT_EXPR;
14820       break;
14821
14822     case CPP_RSHIFT_EQ:
14823       assop = true;
14824       op = RSHIFT_EXPR;
14825       break;
14826
14827     case CPP_EQ_EQ:
14828       op = EQ_EXPR;
14829       break;
14830
14831     case CPP_NOT_EQ:
14832       op = NE_EXPR;
14833       break;
14834
14835     case CPP_LESS_EQ:
14836       op = LE_EXPR;
14837       break;
14838
14839     case CPP_GREATER_EQ:
14840       op = GE_EXPR;
14841       break;
14842
14843     case CPP_AND_AND:
14844       op = TRUTH_ANDIF_EXPR;
14845       break;
14846
14847     case CPP_OR_OR:
14848       op = TRUTH_ORIF_EXPR;
14849       break;
14850
14851     case CPP_PLUS_PLUS:
14852       op = POSTINCREMENT_EXPR;
14853       break;
14854
14855     case CPP_MINUS_MINUS:
14856       op = PREDECREMENT_EXPR;
14857       break;
14858
14859     case CPP_COMMA:
14860       op = COMPOUND_EXPR;
14861       break;
14862
14863     case CPP_DEREF_STAR:
14864       op = MEMBER_REF;
14865       break;
14866
14867     case CPP_DEREF:
14868       op = COMPONENT_REF;
14869       break;
14870
14871     case CPP_OPEN_PAREN:
14872       {
14873         /* Consume the `('.  */
14874         matching_parens parens;
14875         parens.consume_open (parser);
14876         /* Look for the matching `)'.  */
14877         parens.require_close (parser);
14878         op = CALL_EXPR;
14879         consumed = true;
14880         break;
14881       }
14882
14883     case CPP_OPEN_SQUARE:
14884       /* Consume the `['.  */
14885       cp_lexer_consume_token (parser->lexer);
14886       /* Look for the matching `]'.  */
14887       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14888       op = ARRAY_REF;
14889       consumed = true;
14890       break;
14891
14892     case CPP_UTF8STRING:
14893     case CPP_UTF8STRING_USERDEF:
14894       utf8 = true;
14895       /* FALLTHRU */
14896     case CPP_STRING:
14897     case CPP_WSTRING:
14898     case CPP_STRING16:
14899     case CPP_STRING32:
14900     case CPP_STRING_USERDEF:
14901     case CPP_WSTRING_USERDEF:
14902     case CPP_STRING16_USERDEF:
14903     case CPP_STRING32_USERDEF:
14904       {
14905         tree str, string_tree;
14906         int sz, len;
14907
14908         if (cxx_dialect == cxx98)
14909           maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14910
14911         /* Consume the string.  */
14912         str = cp_parser_string_literal (parser, /*translate=*/true,
14913                                       /*wide_ok=*/true, /*lookup_udlit=*/false);
14914         if (str == error_mark_node)
14915           return error_mark_node;
14916         else if (TREE_CODE (str) == USERDEF_LITERAL)
14917           {
14918             string_tree = USERDEF_LITERAL_VALUE (str);
14919             id = USERDEF_LITERAL_SUFFIX_ID (str);
14920           }
14921         else
14922           {
14923             string_tree = str;
14924             /* Look for the suffix identifier.  */
14925             token = cp_lexer_peek_token (parser->lexer);
14926             if (token->type == CPP_NAME)
14927               id = cp_parser_identifier (parser);
14928             else if (token->type == CPP_KEYWORD)
14929               {
14930                 error ("unexpected keyword;"
14931                        " remove space between quotes and suffix identifier");
14932                 return error_mark_node;
14933               }
14934             else
14935               {
14936                 error ("expected suffix identifier");
14937                 return error_mark_node;
14938               }
14939           }
14940         sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14941                                (TREE_TYPE (TREE_TYPE (string_tree))));
14942         len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14943         if (len != 0)
14944           {
14945             error ("expected empty string after %<operator%> keyword");
14946             return error_mark_node;
14947           }
14948         if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14949             != char_type_node)
14950           {
14951             error ("invalid encoding prefix in literal operator");
14952             return error_mark_node;
14953           }
14954         if (id != error_mark_node)
14955           {
14956             const char *name = IDENTIFIER_POINTER (id);
14957             id = cp_literal_operator_id (name);
14958           }
14959         return id;
14960       }
14961
14962     default:
14963       /* Anything else is an error.  */
14964       break;
14965     }
14966
14967   /* If we have selected an identifier, we need to consume the
14968      operator token.  */
14969   if (op != ERROR_MARK)
14970     {
14971       id = ovl_op_identifier (assop, op);
14972       if (!consumed)
14973         cp_lexer_consume_token (parser->lexer);
14974     }
14975   /* Otherwise, no valid operator name was present.  */
14976   else
14977     {
14978       cp_parser_error (parser, "expected operator");
14979       id = error_mark_node;
14980     }
14981
14982   return cp_expr (id, start_loc);
14983 }
14984
14985 /* Parse a template-declaration.
14986
14987    template-declaration:
14988      export [opt] template < template-parameter-list > declaration
14989
14990    If MEMBER_P is TRUE, this template-declaration occurs within a
14991    class-specifier.
14992
14993    The grammar rule given by the standard isn't correct.  What
14994    is really meant is:
14995
14996    template-declaration:
14997      export [opt] template-parameter-list-seq
14998        decl-specifier-seq [opt] init-declarator [opt] ;
14999      export [opt] template-parameter-list-seq
15000        function-definition
15001
15002    template-parameter-list-seq:
15003      template-parameter-list-seq [opt]
15004      template < template-parameter-list >
15005
15006    Concept Extensions:
15007
15008    template-parameter-list-seq:
15009      template < template-parameter-list > requires-clause [opt]
15010
15011    requires-clause:
15012      requires logical-or-expression  */
15013
15014 static void
15015 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15016 {
15017   /* Check for `export'.  */
15018   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15019     {
15020       /* Consume the `export' token.  */
15021       cp_lexer_consume_token (parser->lexer);
15022       /* Warn that we do not support `export'.  */
15023       warning (0, "keyword %<export%> not implemented, and will be ignored");
15024     }
15025
15026   cp_parser_template_declaration_after_export (parser, member_p);
15027 }
15028
15029 /* Parse a template-parameter-list.
15030
15031    template-parameter-list:
15032      template-parameter
15033      template-parameter-list , template-parameter
15034
15035    Returns a TREE_LIST.  Each node represents a template parameter.
15036    The nodes are connected via their TREE_CHAINs.  */
15037
15038 static tree
15039 cp_parser_template_parameter_list (cp_parser* parser)
15040 {
15041   tree parameter_list = NULL_TREE;
15042
15043   begin_template_parm_list ();
15044
15045   /* The loop below parses the template parms.  We first need to know
15046      the total number of template parms to be able to compute proper
15047      canonical types of each dependent type. So after the loop, when
15048      we know the total number of template parms,
15049      end_template_parm_list computes the proper canonical types and
15050      fixes up the dependent types accordingly.  */
15051   while (true)
15052     {
15053       tree parameter;
15054       bool is_non_type;
15055       bool is_parameter_pack;
15056       location_t parm_loc;
15057
15058       /* Parse the template-parameter.  */
15059       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15060       parameter = cp_parser_template_parameter (parser, 
15061                                                 &is_non_type,
15062                                                 &is_parameter_pack);
15063       /* Add it to the list.  */
15064       if (parameter != error_mark_node)
15065         parameter_list = process_template_parm (parameter_list,
15066                                                 parm_loc,
15067                                                 parameter,
15068                                                 is_non_type,
15069                                                 is_parameter_pack);
15070       else
15071        {
15072          tree err_parm = build_tree_list (parameter, parameter);
15073          parameter_list = chainon (parameter_list, err_parm);
15074        }
15075
15076       /* If the next token is not a `,', we're done.  */
15077       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15078         break;
15079       /* Otherwise, consume the `,' token.  */
15080       cp_lexer_consume_token (parser->lexer);
15081     }
15082
15083   return end_template_parm_list (parameter_list);
15084 }
15085
15086 /* Parse a introduction-list.
15087
15088    introduction-list:
15089      introduced-parameter
15090      introduction-list , introduced-parameter
15091
15092    introduced-parameter:
15093      ...[opt] identifier
15094
15095    Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
15096    then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
15097    WILDCARD_DECL will also have DECL_NAME set and token location in
15098    DECL_SOURCE_LOCATION.  */
15099
15100 static tree
15101 cp_parser_introduction_list (cp_parser *parser)
15102 {
15103   vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15104
15105   while (true)
15106     {
15107       bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15108       if (is_pack)
15109         cp_lexer_consume_token (parser->lexer);
15110
15111       /* Build placeholder. */
15112       tree parm = build_nt (WILDCARD_DECL);
15113       DECL_SOURCE_LOCATION (parm)
15114         = cp_lexer_peek_token (parser->lexer)->location;
15115       DECL_NAME (parm) = cp_parser_identifier (parser);
15116       WILDCARD_PACK_P (parm) = is_pack;
15117       vec_safe_push (introduction_vec, parm);
15118
15119       /* If the next token is not a `,', we're done.  */
15120       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15121         break;
15122       /* Otherwise, consume the `,' token.  */
15123       cp_lexer_consume_token (parser->lexer);
15124     }
15125
15126   /* Convert the vec into a TREE_VEC.  */
15127   tree introduction_list = make_tree_vec (introduction_vec->length ());
15128   unsigned int n;
15129   tree parm;
15130   FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15131     TREE_VEC_ELT (introduction_list, n) = parm;
15132
15133   release_tree_vector (introduction_vec);
15134   return introduction_list;
15135 }
15136
15137 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15138    is an abstract declarator. */
15139
15140 static inline cp_declarator*
15141 get_id_declarator (cp_declarator *declarator)
15142 {
15143   cp_declarator *d = declarator;
15144   while (d && d->kind != cdk_id)
15145     d = d->declarator;
15146   return d;
15147 }
15148
15149 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15150    is an abstract declarator. */
15151
15152 static inline tree
15153 get_unqualified_id (cp_declarator *declarator)
15154 {
15155   declarator = get_id_declarator (declarator);
15156   if (declarator)
15157     return declarator->u.id.unqualified_name;
15158   else
15159     return NULL_TREE;
15160 }
15161
15162 /* Returns true if DECL represents a constrained-parameter.  */
15163
15164 static inline bool
15165 is_constrained_parameter (tree decl)
15166 {
15167   return (decl
15168           && TREE_CODE (decl) == TYPE_DECL
15169           && CONSTRAINED_PARM_CONCEPT (decl)
15170           && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15171 }
15172
15173 /* Returns true if PARM declares a constrained-parameter. */
15174
15175 static inline bool
15176 is_constrained_parameter (cp_parameter_declarator *parm)
15177 {
15178   return is_constrained_parameter (parm->decl_specifiers.type);
15179 }
15180
15181 /* Check that the type parameter is only a declarator-id, and that its
15182    type is not cv-qualified. */
15183
15184 bool
15185 cp_parser_check_constrained_type_parm (cp_parser *parser,
15186                                        cp_parameter_declarator *parm)
15187 {
15188   if (!parm->declarator)
15189     return true;
15190
15191   if (parm->declarator->kind != cdk_id)
15192     {
15193       cp_parser_error (parser, "invalid constrained type parameter");
15194       return false;
15195     }
15196
15197   /* Don't allow cv-qualified type parameters.  */
15198   if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15199       || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15200     {
15201       cp_parser_error (parser, "cv-qualified type parameter");
15202       return false;
15203     }
15204
15205   return true;
15206 }
15207
15208 /* Finish parsing/processing a template type parameter and checking
15209    various restrictions. */
15210
15211 static inline tree
15212 cp_parser_constrained_type_template_parm (cp_parser *parser,
15213                                           tree id,
15214                                           cp_parameter_declarator* parmdecl)
15215 {
15216   if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15217     return finish_template_type_parm (class_type_node, id);
15218   else
15219     return error_mark_node;
15220 }
15221
15222 static tree
15223 finish_constrained_template_template_parm (tree proto, tree id)
15224 {
15225   /* FIXME: This should probably be copied, and we may need to adjust
15226      the template parameter depths.  */
15227   tree saved_parms = current_template_parms;
15228   begin_template_parm_list ();
15229   current_template_parms = DECL_TEMPLATE_PARMS (proto);
15230   end_template_parm_list ();
15231
15232   tree parm = finish_template_template_parm (class_type_node, id);
15233   current_template_parms = saved_parms;
15234
15235   return parm;
15236 }
15237
15238 /* Finish parsing/processing a template template parameter by borrowing
15239    the template parameter list from the prototype parameter.  */
15240
15241 static tree
15242 cp_parser_constrained_template_template_parm (cp_parser *parser,
15243                                               tree proto,
15244                                               tree id,
15245                                               cp_parameter_declarator *parmdecl)
15246 {
15247   if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15248     return error_mark_node;
15249   return finish_constrained_template_template_parm (proto, id);
15250 }
15251
15252 /* Create a new non-type template parameter from the given PARM
15253    declarator.  */
15254
15255 static tree
15256 constrained_non_type_template_parm (bool *is_non_type,
15257                                     cp_parameter_declarator *parm)
15258 {
15259   *is_non_type = true;
15260   cp_declarator *decl = parm->declarator;
15261   cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15262   specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15263   return grokdeclarator (decl, specs, TPARM, 0, NULL);
15264 }
15265
15266 /* Build a constrained template parameter based on the PARMDECL
15267    declarator. The type of PARMDECL is the constrained type, which
15268    refers to the prototype template parameter that ultimately
15269    specifies the type of the declared parameter. */
15270
15271 static tree
15272 finish_constrained_parameter (cp_parser *parser,
15273                               cp_parameter_declarator *parmdecl,
15274                               bool *is_non_type,
15275                               bool *is_parameter_pack)
15276 {
15277   tree decl = parmdecl->decl_specifiers.type;
15278   tree id = get_unqualified_id (parmdecl->declarator);
15279   tree def = parmdecl->default_argument;
15280   tree proto = DECL_INITIAL (decl);
15281
15282   /* A template parameter constrained by a variadic concept shall also
15283      be declared as a template parameter pack.  */
15284   bool is_variadic = template_parameter_pack_p (proto);
15285   if (is_variadic && !*is_parameter_pack)
15286     cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15287
15288   /* Build the parameter. Return an error if the declarator was invalid. */
15289   tree parm;
15290   if (TREE_CODE (proto) == TYPE_DECL)
15291     parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15292   else if (TREE_CODE (proto) == TEMPLATE_DECL)
15293     parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15294                                                          parmdecl);
15295   else
15296     parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15297   if (parm == error_mark_node)
15298     return error_mark_node;
15299
15300   /* Finish the parameter decl and create a node attaching the
15301      default argument and constraint.  */
15302   parm = build_tree_list (def, parm);
15303   TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15304
15305   return parm;
15306 }
15307
15308 /* Returns true if the parsed type actually represents the declaration
15309    of a type template-parameter.  */
15310
15311 static inline bool
15312 declares_constrained_type_template_parameter (tree type)
15313 {
15314   return (is_constrained_parameter (type)
15315           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15316 }
15317
15318
15319 /* Returns true if the parsed type actually represents the declaration of
15320    a template template-parameter.  */
15321
15322 static bool
15323 declares_constrained_template_template_parameter (tree type)
15324 {
15325   return (is_constrained_parameter (type)
15326           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15327 }
15328
15329 /* Parse a default argument for a type template-parameter.
15330    Note that diagnostics are handled in cp_parser_template_parameter.  */
15331
15332 static tree
15333 cp_parser_default_type_template_argument (cp_parser *parser)
15334 {
15335   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15336
15337   /* Consume the `=' token.  */
15338   cp_lexer_consume_token (parser->lexer);
15339
15340   cp_token *token = cp_lexer_peek_token (parser->lexer);
15341
15342   /* Parse the default-argument.  */
15343   push_deferring_access_checks (dk_no_deferred);
15344   tree default_argument = cp_parser_type_id (parser);
15345   pop_deferring_access_checks ();
15346
15347   if (flag_concepts && type_uses_auto (default_argument))
15348     {
15349       error_at (token->location,
15350                 "invalid use of %<auto%> in default template argument");
15351       return error_mark_node;
15352     }
15353
15354   return default_argument;
15355 }
15356
15357 /* Parse a default argument for a template template-parameter.  */
15358
15359 static tree
15360 cp_parser_default_template_template_argument (cp_parser *parser)
15361 {
15362   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15363
15364   bool is_template;
15365
15366   /* Consume the `='.  */
15367   cp_lexer_consume_token (parser->lexer);
15368   /* Parse the id-expression.  */
15369   push_deferring_access_checks (dk_no_deferred);
15370   /* save token before parsing the id-expression, for error
15371      reporting */
15372   const cp_token* token = cp_lexer_peek_token (parser->lexer);
15373   tree default_argument
15374     = cp_parser_id_expression (parser,
15375                                /*template_keyword_p=*/false,
15376                                /*check_dependency_p=*/true,
15377                                /*template_p=*/&is_template,
15378                                /*declarator_p=*/false,
15379                                /*optional_p=*/false);
15380   if (TREE_CODE (default_argument) == TYPE_DECL)
15381     /* If the id-expression was a template-id that refers to
15382        a template-class, we already have the declaration here,
15383        so no further lookup is needed.  */
15384     ;
15385   else
15386     /* Look up the name.  */
15387     default_argument
15388       = cp_parser_lookup_name (parser, default_argument,
15389                                none_type,
15390                                /*is_template=*/is_template,
15391                                /*is_namespace=*/false,
15392                                /*check_dependency=*/true,
15393                                /*ambiguous_decls=*/NULL,
15394                                token->location);
15395   /* See if the default argument is valid.  */
15396   default_argument = check_template_template_default_arg (default_argument);
15397   pop_deferring_access_checks ();
15398   return default_argument;
15399 }
15400
15401 /* Parse a template-parameter.
15402
15403    template-parameter:
15404      type-parameter
15405      parameter-declaration
15406
15407    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
15408    the parameter.  The TREE_PURPOSE is the default value, if any.
15409    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
15410    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
15411    set to true iff this parameter is a parameter pack. */
15412
15413 static tree
15414 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15415                               bool *is_parameter_pack)
15416 {
15417   cp_token *token;
15418   cp_parameter_declarator *parameter_declarator;
15419   tree parm;
15420
15421   /* Assume it is a type parameter or a template parameter.  */
15422   *is_non_type = false;
15423   /* Assume it not a parameter pack. */
15424   *is_parameter_pack = false;
15425   /* Peek at the next token.  */
15426   token = cp_lexer_peek_token (parser->lexer);
15427   /* If it is `template', we have a type-parameter.  */
15428   if (token->keyword == RID_TEMPLATE)
15429     return cp_parser_type_parameter (parser, is_parameter_pack);
15430   /* If it is `class' or `typename' we do not know yet whether it is a
15431      type parameter or a non-type parameter.  Consider:
15432
15433        template <typename T, typename T::X X> ...
15434
15435      or:
15436
15437        template <class C, class D*> ...
15438
15439      Here, the first parameter is a type parameter, and the second is
15440      a non-type parameter.  We can tell by looking at the token after
15441      the identifier -- if it is a `,', `=', or `>' then we have a type
15442      parameter.  */
15443   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15444     {
15445       /* Peek at the token after `class' or `typename'.  */
15446       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15447       /* If it's an ellipsis, we have a template type parameter
15448          pack. */
15449       if (token->type == CPP_ELLIPSIS)
15450         return cp_parser_type_parameter (parser, is_parameter_pack);
15451       /* If it's an identifier, skip it.  */
15452       if (token->type == CPP_NAME)
15453         token = cp_lexer_peek_nth_token (parser->lexer, 3);
15454       /* Now, see if the token looks like the end of a template
15455          parameter.  */
15456       if (token->type == CPP_COMMA
15457           || token->type == CPP_EQ
15458           || token->type == CPP_GREATER)
15459         return cp_parser_type_parameter (parser, is_parameter_pack);
15460     }
15461
15462   /* Otherwise, it is a non-type parameter or a constrained parameter.
15463
15464      [temp.param]
15465
15466      When parsing a default template-argument for a non-type
15467      template-parameter, the first non-nested `>' is taken as the end
15468      of the template parameter-list rather than a greater-than
15469      operator.  */
15470   parameter_declarator
15471      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15472                                         /*parenthesized_p=*/NULL);
15473
15474   if (!parameter_declarator)
15475     return error_mark_node;
15476
15477   /* If the parameter declaration is marked as a parameter pack, set
15478    *IS_PARAMETER_PACK to notify the caller.  */
15479   if (parameter_declarator->template_parameter_pack_p)
15480     *is_parameter_pack = true;
15481
15482   if (parameter_declarator->default_argument)
15483     {
15484       /* Can happen in some cases of erroneous input (c++/34892).  */
15485       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15486         /* Consume the `...' for better error recovery.  */
15487         cp_lexer_consume_token (parser->lexer);
15488     }
15489
15490   // The parameter may have been constrained.
15491   if (is_constrained_parameter (parameter_declarator))
15492     return finish_constrained_parameter (parser,
15493                                          parameter_declarator,
15494                                          is_non_type,
15495                                          is_parameter_pack);
15496
15497   // Now we're sure that the parameter is a non-type parameter.
15498   *is_non_type = true;
15499
15500   parm = grokdeclarator (parameter_declarator->declarator,
15501                          &parameter_declarator->decl_specifiers,
15502                          TPARM, /*initialized=*/0,
15503                          /*attrlist=*/NULL);
15504   if (parm == error_mark_node)
15505     return error_mark_node;
15506
15507   return build_tree_list (parameter_declarator->default_argument, parm);
15508 }
15509
15510 /* Parse a type-parameter.
15511
15512    type-parameter:
15513      class identifier [opt]
15514      class identifier [opt] = type-id
15515      typename identifier [opt]
15516      typename identifier [opt] = type-id
15517      template < template-parameter-list > class identifier [opt]
15518      template < template-parameter-list > class identifier [opt]
15519        = id-expression
15520
15521    GNU Extension (variadic templates):
15522
15523    type-parameter:
15524      class ... identifier [opt]
15525      typename ... identifier [opt]
15526
15527    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
15528    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
15529    the declaration of the parameter.
15530
15531    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15532
15533 static tree
15534 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15535 {
15536   cp_token *token;
15537   tree parameter;
15538
15539   /* Look for a keyword to tell us what kind of parameter this is.  */
15540   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15541   if (!token)
15542     return error_mark_node;
15543
15544   switch (token->keyword)
15545     {
15546     case RID_CLASS:
15547     case RID_TYPENAME:
15548       {
15549         tree identifier;
15550         tree default_argument;
15551
15552         /* If the next token is an ellipsis, we have a template
15553            argument pack. */
15554         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15555           {
15556             /* Consume the `...' token. */
15557             cp_lexer_consume_token (parser->lexer);
15558             maybe_warn_variadic_templates ();
15559
15560             *is_parameter_pack = true;
15561           }
15562
15563         /* If the next token is an identifier, then it names the
15564            parameter.  */
15565         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15566           identifier = cp_parser_identifier (parser);
15567         else
15568           identifier = NULL_TREE;
15569
15570         /* Create the parameter.  */
15571         parameter = finish_template_type_parm (class_type_node, identifier);
15572
15573         /* If the next token is an `=', we have a default argument.  */
15574         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15575           {
15576             default_argument
15577               = cp_parser_default_type_template_argument (parser);
15578
15579             /* Template parameter packs cannot have default
15580                arguments. */
15581             if (*is_parameter_pack)
15582               {
15583                 if (identifier)
15584                   error_at (token->location,
15585                             "template parameter pack %qD cannot have a "
15586                             "default argument", identifier);
15587                 else
15588                   error_at (token->location,
15589                             "template parameter packs cannot have "
15590                             "default arguments");
15591                 default_argument = NULL_TREE;
15592               }
15593             else if (check_for_bare_parameter_packs (default_argument))
15594               default_argument = error_mark_node;
15595           }
15596         else
15597           default_argument = NULL_TREE;
15598
15599         /* Create the combined representation of the parameter and the
15600            default argument.  */
15601         parameter = build_tree_list (default_argument, parameter);
15602       }
15603       break;
15604
15605     case RID_TEMPLATE:
15606       {
15607         tree identifier;
15608         tree default_argument;
15609
15610         /* Look for the `<'.  */
15611         cp_parser_require (parser, CPP_LESS, RT_LESS);
15612         /* Parse the template-parameter-list.  */
15613         cp_parser_template_parameter_list (parser);
15614         /* Look for the `>'.  */
15615         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15616
15617         // If template requirements are present, parse them.
15618         if (flag_concepts)
15619           {
15620             tree reqs = get_shorthand_constraints (current_template_parms);
15621             if (tree r = cp_parser_requires_clause_opt (parser))
15622               reqs = conjoin_constraints (reqs, normalize_expression (r));
15623             TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15624           }
15625
15626         /* Look for the `class' or 'typename' keywords.  */
15627         cp_parser_type_parameter_key (parser);
15628         /* If the next token is an ellipsis, we have a template
15629            argument pack. */
15630         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15631           {
15632             /* Consume the `...' token. */
15633             cp_lexer_consume_token (parser->lexer);
15634             maybe_warn_variadic_templates ();
15635
15636             *is_parameter_pack = true;
15637           }
15638         /* If the next token is an `=', then there is a
15639            default-argument.  If the next token is a `>', we are at
15640            the end of the parameter-list.  If the next token is a `,',
15641            then we are at the end of this parameter.  */
15642         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15643             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15644             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15645           {
15646             identifier = cp_parser_identifier (parser);
15647             /* Treat invalid names as if the parameter were nameless.  */
15648             if (identifier == error_mark_node)
15649               identifier = NULL_TREE;
15650           }
15651         else
15652           identifier = NULL_TREE;
15653
15654         /* Create the template parameter.  */
15655         parameter = finish_template_template_parm (class_type_node,
15656                                                    identifier);
15657
15658         /* If the next token is an `=', then there is a
15659            default-argument.  */
15660         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15661           {
15662             default_argument
15663               = cp_parser_default_template_template_argument (parser);
15664
15665             /* Template parameter packs cannot have default
15666                arguments. */
15667             if (*is_parameter_pack)
15668               {
15669                 if (identifier)
15670                   error_at (token->location,
15671                             "template parameter pack %qD cannot "
15672                             "have a default argument",
15673                             identifier);
15674                 else
15675                   error_at (token->location, "template parameter packs cannot "
15676                             "have default arguments");
15677                 default_argument = NULL_TREE;
15678               }
15679           }
15680         else
15681           default_argument = NULL_TREE;
15682
15683         /* Create the combined representation of the parameter and the
15684            default argument.  */
15685         parameter = build_tree_list (default_argument, parameter);
15686       }
15687       break;
15688
15689     default:
15690       gcc_unreachable ();
15691       break;
15692     }
15693
15694   return parameter;
15695 }
15696
15697 /* Parse a template-id.
15698
15699    template-id:
15700      template-name < template-argument-list [opt] >
15701
15702    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15703    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
15704    returned.  Otherwise, if the template-name names a function, or set
15705    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
15706    names a class, returns a TYPE_DECL for the specialization.
15707
15708    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15709    uninstantiated templates.  */
15710
15711 static tree
15712 cp_parser_template_id (cp_parser *parser,
15713                        bool template_keyword_p,
15714                        bool check_dependency_p,
15715                        enum tag_types tag_type,
15716                        bool is_declaration)
15717 {
15718   tree templ;
15719   tree arguments;
15720   tree template_id;
15721   cp_token_position start_of_id = 0;
15722   cp_token *next_token = NULL, *next_token_2 = NULL;
15723   bool is_identifier;
15724
15725   /* If the next token corresponds to a template-id, there is no need
15726      to reparse it.  */
15727   cp_token *token = cp_lexer_peek_token (parser->lexer);
15728   if (token->type == CPP_TEMPLATE_ID)
15729     {
15730       cp_lexer_consume_token (parser->lexer);
15731       return saved_checks_value (token->u.tree_check_value);
15732     }
15733
15734   /* Avoid performing name lookup if there is no possibility of
15735      finding a template-id.  */
15736   if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15737       || (token->type == CPP_NAME
15738           && !cp_parser_nth_token_starts_template_argument_list_p
15739                (parser, 2)))
15740     {
15741       cp_parser_error (parser, "expected template-id");
15742       return error_mark_node;
15743     }
15744
15745   /* Remember where the template-id starts.  */
15746   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15747     start_of_id = cp_lexer_token_position (parser->lexer, false);
15748
15749   push_deferring_access_checks (dk_deferred);
15750
15751   /* Parse the template-name.  */
15752   is_identifier = false;
15753   templ = cp_parser_template_name (parser, template_keyword_p,
15754                                    check_dependency_p,
15755                                    is_declaration,
15756                                    tag_type,
15757                                    &is_identifier);
15758   if (templ == error_mark_node || is_identifier)
15759     {
15760       pop_deferring_access_checks ();
15761       return templ;
15762     }
15763
15764   /* Since we're going to preserve any side-effects from this parse, set up a
15765      firewall to protect our callers from cp_parser_commit_to_tentative_parse
15766      in the template arguments.  */
15767   tentative_firewall firewall (parser);
15768
15769   /* If we find the sequence `[:' after a template-name, it's probably
15770      a digraph-typo for `< ::'. Substitute the tokens and check if we can
15771      parse correctly the argument list.  */
15772   if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15773        == CPP_OPEN_SQUARE)
15774       && next_token->flags & DIGRAPH
15775       && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15776           == CPP_COLON)
15777       && !(next_token_2->flags & PREV_WHITE))
15778     {
15779       cp_parser_parse_tentatively (parser);
15780       /* Change `:' into `::'.  */
15781       next_token_2->type = CPP_SCOPE;
15782       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15783          CPP_LESS.  */
15784       cp_lexer_consume_token (parser->lexer);
15785
15786       /* Parse the arguments.  */
15787       arguments = cp_parser_enclosed_template_argument_list (parser);
15788       if (!cp_parser_parse_definitely (parser))
15789         {
15790           /* If we couldn't parse an argument list, then we revert our changes
15791              and return simply an error. Maybe this is not a template-id
15792              after all.  */
15793           next_token_2->type = CPP_COLON;
15794           cp_parser_error (parser, "expected %<<%>");
15795           pop_deferring_access_checks ();
15796           return error_mark_node;
15797         }
15798       /* Otherwise, emit an error about the invalid digraph, but continue
15799          parsing because we got our argument list.  */
15800       if (permerror (next_token->location,
15801                      "%<<::%> cannot begin a template-argument list"))
15802         {
15803           static bool hint = false;
15804           inform (next_token->location,
15805                   "%<<:%> is an alternate spelling for %<[%>."
15806                   " Insert whitespace between %<<%> and %<::%>");
15807           if (!hint && !flag_permissive)
15808             {
15809               inform (next_token->location, "(if you use %<-fpermissive%> "
15810                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15811                       "accept your code)");
15812               hint = true;
15813             }
15814         }
15815     }
15816   else
15817     {
15818       /* Look for the `<' that starts the template-argument-list.  */
15819       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15820         {
15821           pop_deferring_access_checks ();
15822           return error_mark_node;
15823         }
15824       /* Parse the arguments.  */
15825       arguments = cp_parser_enclosed_template_argument_list (parser);
15826     }
15827
15828   /* Set the location to be of the form:
15829      template-name < template-argument-list [opt] >
15830      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15831      with caret == start at the start of the template-name,
15832      ranging until the closing '>'.  */
15833   location_t finish_loc
15834     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15835   location_t combined_loc
15836     = make_location (token->location, token->location, finish_loc);
15837
15838   /* Check for concepts autos where they don't belong.  We could
15839      identify types in some cases of idnetifier TEMPL, looking ahead
15840      for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15841      types.  We reject them in functions, but if what we have is an
15842      identifier, even with none_type we can't conclude it's NOT a
15843      type, we have to wait for template substitution.  */
15844   if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15845     template_id = error_mark_node;
15846   /* Build a representation of the specialization.  */
15847   else if (identifier_p (templ))
15848     template_id = build_min_nt_loc (combined_loc,
15849                                     TEMPLATE_ID_EXPR,
15850                                     templ, arguments);
15851   else if (DECL_TYPE_TEMPLATE_P (templ)
15852            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15853     {
15854       bool entering_scope;
15855       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15856          template (rather than some instantiation thereof) only if
15857          is not nested within some other construct.  For example, in
15858          "template <typename T> void f(T) { A<T>::", A<T> is just an
15859          instantiation of A.  */
15860       entering_scope = (template_parm_scope_p ()
15861                         && cp_lexer_next_token_is (parser->lexer,
15862                                                    CPP_SCOPE));
15863       template_id
15864         = finish_template_type (templ, arguments, entering_scope);
15865     }
15866   /* A template-like identifier may be a partial concept id. */
15867   else if (flag_concepts
15868            && (template_id = (cp_parser_maybe_partial_concept_id
15869                               (parser, templ, arguments))))
15870     return template_id;
15871   else if (variable_template_p (templ))
15872     {
15873       template_id = lookup_template_variable (templ, arguments);
15874       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15875         SET_EXPR_LOCATION (template_id, combined_loc);
15876     }
15877   else
15878     {
15879       /* If it's not a class-template or a template-template, it should be
15880          a function-template.  */
15881       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15882                    || TREE_CODE (templ) == OVERLOAD
15883                    || BASELINK_P (templ)));
15884
15885       template_id = lookup_template_function (templ, arguments);
15886       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15887         SET_EXPR_LOCATION (template_id, combined_loc);
15888     }
15889
15890   /* If parsing tentatively, replace the sequence of tokens that makes
15891      up the template-id with a CPP_TEMPLATE_ID token.  That way,
15892      should we re-parse the token stream, we will not have to repeat
15893      the effort required to do the parse, nor will we issue duplicate
15894      error messages about problems during instantiation of the
15895      template.  */
15896   if (start_of_id
15897       /* Don't do this if we had a parse error in a declarator; re-parsing
15898          might succeed if a name changes meaning (60361).  */
15899       && !(cp_parser_error_occurred (parser)
15900            && cp_parser_parsing_tentatively (parser)
15901            && parser->in_declarator_p))
15902     {
15903       /* Reset the contents of the START_OF_ID token.  */
15904       token->type = CPP_TEMPLATE_ID;
15905       token->location = combined_loc;
15906
15907       /* We must mark the lookup as kept, so we don't throw it away on
15908          the first parse.  */
15909       if (is_overloaded_fn (template_id))
15910         lookup_keep (get_fns (template_id), true);
15911
15912       /* Retrieve any deferred checks.  Do not pop this access checks yet
15913          so the memory will not be reclaimed during token replacing below.  */
15914       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15915       token->u.tree_check_value->value = template_id;
15916       token->u.tree_check_value->checks = get_deferred_access_checks ();
15917       token->keyword = RID_MAX;
15918
15919       /* Purge all subsequent tokens.  */
15920       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15921
15922       /* ??? Can we actually assume that, if template_id ==
15923          error_mark_node, we will have issued a diagnostic to the
15924          user, as opposed to simply marking the tentative parse as
15925          failed?  */
15926       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15927         error_at (token->location, "parse error in template argument list");
15928     }
15929
15930   pop_to_parent_deferring_access_checks ();
15931   return template_id;
15932 }
15933
15934 /* Parse a template-name.
15935
15936    template-name:
15937      identifier
15938
15939    The standard should actually say:
15940
15941    template-name:
15942      identifier
15943      operator-function-id
15944
15945    A defect report has been filed about this issue.
15946
15947    A conversion-function-id cannot be a template name because they cannot
15948    be part of a template-id. In fact, looking at this code:
15949
15950    a.operator K<int>()
15951
15952    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15953    It is impossible to call a templated conversion-function-id with an
15954    explicit argument list, since the only allowed template parameter is
15955    the type to which it is converting.
15956
15957    If TEMPLATE_KEYWORD_P is true, then we have just seen the
15958    `template' keyword, in a construction like:
15959
15960      T::template f<3>()
15961
15962    In that case `f' is taken to be a template-name, even though there
15963    is no way of knowing for sure.
15964
15965    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15966    name refers to a set of overloaded functions, at least one of which
15967    is a template, or an IDENTIFIER_NODE with the name of the template,
15968    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
15969    names are looked up inside uninstantiated templates.  */
15970
15971 static tree
15972 cp_parser_template_name (cp_parser* parser,
15973                          bool template_keyword_p,
15974                          bool check_dependency_p,
15975                          bool is_declaration,
15976                          enum tag_types tag_type,
15977                          bool *is_identifier)
15978 {
15979   tree identifier;
15980   tree decl;
15981   cp_token *token = cp_lexer_peek_token (parser->lexer);
15982
15983   /* If the next token is `operator', then we have either an
15984      operator-function-id or a conversion-function-id.  */
15985   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15986     {
15987       /* We don't know whether we're looking at an
15988          operator-function-id or a conversion-function-id.  */
15989       cp_parser_parse_tentatively (parser);
15990       /* Try an operator-function-id.  */
15991       identifier = cp_parser_operator_function_id (parser);
15992       /* If that didn't work, try a conversion-function-id.  */
15993       if (!cp_parser_parse_definitely (parser))
15994         {
15995           cp_parser_error (parser, "expected template-name");
15996           return error_mark_node;
15997         }
15998     }
15999   /* Look for the identifier.  */
16000   else
16001     identifier = cp_parser_identifier (parser);
16002
16003   /* If we didn't find an identifier, we don't have a template-id.  */
16004   if (identifier == error_mark_node)
16005     return error_mark_node;
16006
16007   /* If the name immediately followed the `template' keyword, then it
16008      is a template-name.  However, if the next token is not `<', then
16009      we do not treat it as a template-name, since it is not being used
16010      as part of a template-id.  This enables us to handle constructs
16011      like:
16012
16013        template <typename T> struct S { S(); };
16014        template <typename T> S<T>::S();
16015
16016      correctly.  We would treat `S' as a template -- if it were `S<T>'
16017      -- but we do not if there is no `<'.  */
16018
16019   if (processing_template_decl
16020       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16021     {
16022       /* In a declaration, in a dependent context, we pretend that the
16023          "template" keyword was present in order to improve error
16024          recovery.  For example, given:
16025
16026            template <typename T> void f(T::X<int>);
16027
16028          we want to treat "X<int>" as a template-id.  */
16029       if (is_declaration
16030           && !template_keyword_p
16031           && parser->scope && TYPE_P (parser->scope)
16032           && check_dependency_p
16033           && dependent_scope_p (parser->scope)
16034           /* Do not do this for dtors (or ctors), since they never
16035              need the template keyword before their name.  */
16036           && !constructor_name_p (identifier, parser->scope))
16037         {
16038           cp_token_position start = 0;
16039
16040           /* Explain what went wrong.  */
16041           error_at (token->location, "non-template %qD used as template",
16042                     identifier);
16043           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16044                   parser->scope, identifier);
16045           /* If parsing tentatively, find the location of the "<" token.  */
16046           if (cp_parser_simulate_error (parser))
16047             start = cp_lexer_token_position (parser->lexer, true);
16048           /* Parse the template arguments so that we can issue error
16049              messages about them.  */
16050           cp_lexer_consume_token (parser->lexer);
16051           cp_parser_enclosed_template_argument_list (parser);
16052           /* Skip tokens until we find a good place from which to
16053              continue parsing.  */
16054           cp_parser_skip_to_closing_parenthesis (parser,
16055                                                  /*recovering=*/true,
16056                                                  /*or_comma=*/true,
16057                                                  /*consume_paren=*/false);
16058           /* If parsing tentatively, permanently remove the
16059              template argument list.  That will prevent duplicate
16060              error messages from being issued about the missing
16061              "template" keyword.  */
16062           if (start)
16063             cp_lexer_purge_tokens_after (parser->lexer, start);
16064           if (is_identifier)
16065             *is_identifier = true;
16066           parser->context->object_type = NULL_TREE;
16067           return identifier;
16068         }
16069
16070       /* If the "template" keyword is present, then there is generally
16071          no point in doing name-lookup, so we just return IDENTIFIER.
16072          But, if the qualifying scope is non-dependent then we can
16073          (and must) do name-lookup normally.  */
16074       if (template_keyword_p)
16075         {
16076           tree scope = (parser->scope ? parser->scope
16077                         : parser->context->object_type);
16078           if (scope && TYPE_P (scope)
16079               && (!CLASS_TYPE_P (scope)
16080                   || (check_dependency_p && dependent_type_p (scope))))
16081             {
16082               /* We're optimizing away the call to cp_parser_lookup_name, but
16083                  we still need to do this.  */
16084               parser->context->object_type = NULL_TREE;
16085               return identifier;
16086             }
16087         }
16088     }
16089
16090   /* Look up the name.  */
16091   decl = cp_parser_lookup_name (parser, identifier,
16092                                 tag_type,
16093                                 /*is_template=*/true,
16094                                 /*is_namespace=*/false,
16095                                 check_dependency_p,
16096                                 /*ambiguous_decls=*/NULL,
16097                                 token->location);
16098
16099   decl = strip_using_decl (decl);
16100
16101   /* If DECL is a template, then the name was a template-name.  */
16102   if (TREE_CODE (decl) == TEMPLATE_DECL)
16103     {
16104       if (TREE_DEPRECATED (decl)
16105           && deprecated_state != DEPRECATED_SUPPRESS)
16106         warn_deprecated_use (decl, NULL_TREE);
16107     }
16108   else
16109     {
16110       /* The standard does not explicitly indicate whether a name that
16111          names a set of overloaded declarations, some of which are
16112          templates, is a template-name.  However, such a name should
16113          be a template-name; otherwise, there is no way to form a
16114          template-id for the overloaded templates.  */
16115       bool found = false;
16116
16117       for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16118            !found && iter; ++iter)
16119         if (TREE_CODE (*iter) == TEMPLATE_DECL)
16120           found = true;
16121
16122       if (!found)
16123         {
16124           /* The name does not name a template.  */
16125           cp_parser_error (parser, "expected template-name");
16126           return error_mark_node;
16127         }
16128     }
16129
16130   /* If DECL is dependent, and refers to a function, then just return
16131      its name; we will look it up again during template instantiation.  */
16132   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16133     {
16134       tree scope = ovl_scope (decl);
16135       if (TYPE_P (scope) && dependent_type_p (scope))
16136         return identifier;
16137     }
16138
16139   return decl;
16140 }
16141
16142 /* Parse a template-argument-list.
16143
16144    template-argument-list:
16145      template-argument ... [opt]
16146      template-argument-list , template-argument ... [opt]
16147
16148    Returns a TREE_VEC containing the arguments.  */
16149
16150 static tree
16151 cp_parser_template_argument_list (cp_parser* parser)
16152 {
16153   tree fixed_args[10];
16154   unsigned n_args = 0;
16155   unsigned alloced = 10;
16156   tree *arg_ary = fixed_args;
16157   tree vec;
16158   bool saved_in_template_argument_list_p;
16159   bool saved_ice_p;
16160   bool saved_non_ice_p;
16161
16162   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16163   parser->in_template_argument_list_p = true;
16164   /* Even if the template-id appears in an integral
16165      constant-expression, the contents of the argument list do
16166      not.  */
16167   saved_ice_p = parser->integral_constant_expression_p;
16168   parser->integral_constant_expression_p = false;
16169   saved_non_ice_p = parser->non_integral_constant_expression_p;
16170   parser->non_integral_constant_expression_p = false;
16171
16172   /* Parse the arguments.  */
16173   do
16174     {
16175       tree argument;
16176
16177       if (n_args)
16178         /* Consume the comma.  */
16179         cp_lexer_consume_token (parser->lexer);
16180
16181       /* Parse the template-argument.  */
16182       argument = cp_parser_template_argument (parser);
16183
16184       /* If the next token is an ellipsis, we're expanding a template
16185          argument pack. */
16186       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16187         {
16188           if (argument == error_mark_node)
16189             {
16190               cp_token *token = cp_lexer_peek_token (parser->lexer);
16191               error_at (token->location,
16192                         "expected parameter pack before %<...%>");
16193             }
16194           /* Consume the `...' token. */
16195           cp_lexer_consume_token (parser->lexer);
16196
16197           /* Make the argument into a TYPE_PACK_EXPANSION or
16198              EXPR_PACK_EXPANSION. */
16199           argument = make_pack_expansion (argument);
16200         }
16201
16202       if (n_args == alloced)
16203         {
16204           alloced *= 2;
16205
16206           if (arg_ary == fixed_args)
16207             {
16208               arg_ary = XNEWVEC (tree, alloced);
16209               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16210             }
16211           else
16212             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16213         }
16214       arg_ary[n_args++] = argument;
16215     }
16216   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16217
16218   vec = make_tree_vec (n_args);
16219
16220   while (n_args--)
16221     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16222
16223   if (arg_ary != fixed_args)
16224     free (arg_ary);
16225   parser->non_integral_constant_expression_p = saved_non_ice_p;
16226   parser->integral_constant_expression_p = saved_ice_p;
16227   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16228   if (CHECKING_P)
16229     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16230   return vec;
16231 }
16232
16233 /* Parse a template-argument.
16234
16235    template-argument:
16236      assignment-expression
16237      type-id
16238      id-expression
16239
16240    The representation is that of an assignment-expression, type-id, or
16241    id-expression -- except that the qualified id-expression is
16242    evaluated, so that the value returned is either a DECL or an
16243    OVERLOAD.
16244
16245    Although the standard says "assignment-expression", it forbids
16246    throw-expressions or assignments in the template argument.
16247    Therefore, we use "conditional-expression" instead.  */
16248
16249 static tree
16250 cp_parser_template_argument (cp_parser* parser)
16251 {
16252   tree argument;
16253   bool template_p;
16254   bool address_p;
16255   bool maybe_type_id = false;
16256   cp_token *token = NULL, *argument_start_token = NULL;
16257   location_t loc = 0;
16258   cp_id_kind idk;
16259
16260   /* There's really no way to know what we're looking at, so we just
16261      try each alternative in order.
16262
16263        [temp.arg]
16264
16265        In a template-argument, an ambiguity between a type-id and an
16266        expression is resolved to a type-id, regardless of the form of
16267        the corresponding template-parameter.
16268
16269      Therefore, we try a type-id first.  */
16270   cp_parser_parse_tentatively (parser);
16271   argument = cp_parser_template_type_arg (parser);
16272   /* If there was no error parsing the type-id but the next token is a
16273      '>>', our behavior depends on which dialect of C++ we're
16274      parsing. In C++98, we probably found a typo for '> >'. But there
16275      are type-id which are also valid expressions. For instance:
16276
16277      struct X { int operator >> (int); };
16278      template <int V> struct Foo {};
16279      Foo<X () >> 5> r;
16280
16281      Here 'X()' is a valid type-id of a function type, but the user just
16282      wanted to write the expression "X() >> 5". Thus, we remember that we
16283      found a valid type-id, but we still try to parse the argument as an
16284      expression to see what happens. 
16285
16286      In C++0x, the '>>' will be considered two separate '>'
16287      tokens.  */
16288   if (!cp_parser_error_occurred (parser)
16289       && cxx_dialect == cxx98
16290       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16291     {
16292       maybe_type_id = true;
16293       cp_parser_abort_tentative_parse (parser);
16294     }
16295   else
16296     {
16297       /* If the next token isn't a `,' or a `>', then this argument wasn't
16298       really finished. This means that the argument is not a valid
16299       type-id.  */
16300       if (!cp_parser_next_token_ends_template_argument_p (parser))
16301         cp_parser_error (parser, "expected template-argument");
16302       /* If that worked, we're done.  */
16303       if (cp_parser_parse_definitely (parser))
16304         return argument;
16305     }
16306   /* We're still not sure what the argument will be.  */
16307   cp_parser_parse_tentatively (parser);
16308   /* Try a template.  */
16309   argument_start_token = cp_lexer_peek_token (parser->lexer);
16310   argument = cp_parser_id_expression (parser,
16311                                       /*template_keyword_p=*/false,
16312                                       /*check_dependency_p=*/true,
16313                                       &template_p,
16314                                       /*declarator_p=*/false,
16315                                       /*optional_p=*/false);
16316   /* If the next token isn't a `,' or a `>', then this argument wasn't
16317      really finished.  */
16318   if (!cp_parser_next_token_ends_template_argument_p (parser))
16319     cp_parser_error (parser, "expected template-argument");
16320   if (!cp_parser_error_occurred (parser))
16321     {
16322       /* Figure out what is being referred to.  If the id-expression
16323          was for a class template specialization, then we will have a
16324          TYPE_DECL at this point.  There is no need to do name lookup
16325          at this point in that case.  */
16326       if (TREE_CODE (argument) != TYPE_DECL)
16327         argument = cp_parser_lookup_name (parser, argument,
16328                                           none_type,
16329                                           /*is_template=*/template_p,
16330                                           /*is_namespace=*/false,
16331                                           /*check_dependency=*/true,
16332                                           /*ambiguous_decls=*/NULL,
16333                                           argument_start_token->location);
16334       /* Handle a constrained-type-specifier for a non-type template
16335          parameter.  */
16336       if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16337         argument = decl;
16338       else if (TREE_CODE (argument) != TEMPLATE_DECL
16339                && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16340         cp_parser_error (parser, "expected template-name");
16341     }
16342   if (cp_parser_parse_definitely (parser))
16343     {
16344       if (TREE_DEPRECATED (argument))
16345         warn_deprecated_use (argument, NULL_TREE);
16346       return argument;
16347     }
16348   /* It must be a non-type argument.  In C++17 any constant-expression is
16349      allowed.  */
16350   if (cxx_dialect > cxx14)
16351     goto general_expr;
16352
16353   /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16354
16355      -- an integral constant-expression of integral or enumeration
16356         type; or
16357
16358      -- the name of a non-type template-parameter; or
16359
16360      -- the name of an object or function with external linkage...
16361
16362      -- the address of an object or function with external linkage...
16363
16364      -- a pointer to member...  */
16365   /* Look for a non-type template parameter.  */
16366   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16367     {
16368       cp_parser_parse_tentatively (parser);
16369       argument = cp_parser_primary_expression (parser,
16370                                                /*address_p=*/false,
16371                                                /*cast_p=*/false,
16372                                                /*template_arg_p=*/true,
16373                                                &idk);
16374       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16375           || !cp_parser_next_token_ends_template_argument_p (parser))
16376         cp_parser_simulate_error (parser);
16377       if (cp_parser_parse_definitely (parser))
16378         return argument;
16379     }
16380
16381   /* If the next token is "&", the argument must be the address of an
16382      object or function with external linkage.  */
16383   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16384   if (address_p)
16385     {
16386       loc = cp_lexer_peek_token (parser->lexer)->location;
16387       cp_lexer_consume_token (parser->lexer);
16388     }
16389   /* See if we might have an id-expression.  */
16390   token = cp_lexer_peek_token (parser->lexer);
16391   if (token->type == CPP_NAME
16392       || token->keyword == RID_OPERATOR
16393       || token->type == CPP_SCOPE
16394       || token->type == CPP_TEMPLATE_ID
16395       || token->type == CPP_NESTED_NAME_SPECIFIER)
16396     {
16397       cp_parser_parse_tentatively (parser);
16398       argument = cp_parser_primary_expression (parser,
16399                                                address_p,
16400                                                /*cast_p=*/false,
16401                                                /*template_arg_p=*/true,
16402                                                &idk);
16403       if (cp_parser_error_occurred (parser)
16404           || !cp_parser_next_token_ends_template_argument_p (parser))
16405         cp_parser_abort_tentative_parse (parser);
16406       else
16407         {
16408           tree probe;
16409
16410           if (INDIRECT_REF_P (argument))
16411             {
16412               /* Strip the dereference temporarily.  */
16413               gcc_assert (REFERENCE_REF_P (argument));
16414               argument = TREE_OPERAND (argument, 0);
16415             }
16416
16417           /* If we're in a template, we represent a qualified-id referring
16418              to a static data member as a SCOPE_REF even if the scope isn't
16419              dependent so that we can check access control later.  */
16420           probe = argument;
16421           if (TREE_CODE (probe) == SCOPE_REF)
16422             probe = TREE_OPERAND (probe, 1);
16423           if (VAR_P (probe))
16424             {
16425               /* A variable without external linkage might still be a
16426                  valid constant-expression, so no error is issued here
16427                  if the external-linkage check fails.  */
16428               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16429                 cp_parser_simulate_error (parser);
16430             }
16431           else if (is_overloaded_fn (argument))
16432             /* All overloaded functions are allowed; if the external
16433                linkage test does not pass, an error will be issued
16434                later.  */
16435             ;
16436           else if (address_p
16437                    && (TREE_CODE (argument) == OFFSET_REF
16438                        || TREE_CODE (argument) == SCOPE_REF))
16439             /* A pointer-to-member.  */
16440             ;
16441           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16442             ;
16443           else
16444             cp_parser_simulate_error (parser);
16445
16446           if (cp_parser_parse_definitely (parser))
16447             {
16448               if (address_p)
16449                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16450                                              tf_warning_or_error);
16451               else
16452                 argument = convert_from_reference (argument);
16453               return argument;
16454             }
16455         }
16456     }
16457   /* If the argument started with "&", there are no other valid
16458      alternatives at this point.  */
16459   if (address_p)
16460     {
16461       cp_parser_error (parser, "invalid non-type template argument");
16462       return error_mark_node;
16463     }
16464
16465  general_expr:
16466   /* If the argument wasn't successfully parsed as a type-id followed
16467      by '>>', the argument can only be a constant expression now.
16468      Otherwise, we try parsing the constant-expression tentatively,
16469      because the argument could really be a type-id.  */
16470   if (maybe_type_id)
16471     cp_parser_parse_tentatively (parser);
16472
16473   if (cxx_dialect <= cxx14)
16474     argument = cp_parser_constant_expression (parser);
16475   else
16476     {
16477       /* With C++17 generalized non-type template arguments we need to handle
16478          lvalue constant expressions, too.  */
16479       argument = cp_parser_assignment_expression (parser);
16480       require_potential_constant_expression (argument);
16481     }
16482
16483   if (!maybe_type_id)
16484     return argument;
16485   if (!cp_parser_next_token_ends_template_argument_p (parser))
16486     cp_parser_error (parser, "expected template-argument");
16487   if (cp_parser_parse_definitely (parser))
16488     return argument;
16489   /* We did our best to parse the argument as a non type-id, but that
16490      was the only alternative that matched (albeit with a '>' after
16491      it). We can assume it's just a typo from the user, and a
16492      diagnostic will then be issued.  */
16493   return cp_parser_template_type_arg (parser);
16494 }
16495
16496 /* Parse an explicit-instantiation.
16497
16498    explicit-instantiation:
16499      template declaration
16500
16501    Although the standard says `declaration', what it really means is:
16502
16503    explicit-instantiation:
16504      template decl-specifier-seq [opt] declarator [opt] ;
16505
16506    Things like `template int S<int>::i = 5, int S<double>::j;' are not
16507    supposed to be allowed.  A defect report has been filed about this
16508    issue.
16509
16510    GNU Extension:
16511
16512    explicit-instantiation:
16513      storage-class-specifier template
16514        decl-specifier-seq [opt] declarator [opt] ;
16515      function-specifier template
16516        decl-specifier-seq [opt] declarator [opt] ;  */
16517
16518 static void
16519 cp_parser_explicit_instantiation (cp_parser* parser)
16520 {
16521   int declares_class_or_enum;
16522   cp_decl_specifier_seq decl_specifiers;
16523   tree extension_specifier = NULL_TREE;
16524
16525   timevar_push (TV_TEMPLATE_INST);
16526
16527   /* Look for an (optional) storage-class-specifier or
16528      function-specifier.  */
16529   if (cp_parser_allow_gnu_extensions_p (parser))
16530     {
16531       extension_specifier
16532         = cp_parser_storage_class_specifier_opt (parser);
16533       if (!extension_specifier)
16534         extension_specifier
16535           = cp_parser_function_specifier_opt (parser,
16536                                               /*decl_specs=*/NULL);
16537     }
16538
16539   /* Look for the `template' keyword.  */
16540   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16541   /* Let the front end know that we are processing an explicit
16542      instantiation.  */
16543   begin_explicit_instantiation ();
16544   /* [temp.explicit] says that we are supposed to ignore access
16545      control while processing explicit instantiation directives.  */
16546   push_deferring_access_checks (dk_no_check);
16547   /* Parse a decl-specifier-seq.  */
16548   cp_parser_decl_specifier_seq (parser,
16549                                 CP_PARSER_FLAGS_OPTIONAL,
16550                                 &decl_specifiers,
16551                                 &declares_class_or_enum);
16552   /* If there was exactly one decl-specifier, and it declared a class,
16553      and there's no declarator, then we have an explicit type
16554      instantiation.  */
16555   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16556     {
16557       tree type;
16558
16559       type = check_tag_decl (&decl_specifiers,
16560                              /*explicit_type_instantiation_p=*/true);
16561       /* Turn access control back on for names used during
16562          template instantiation.  */
16563       pop_deferring_access_checks ();
16564       if (type)
16565         do_type_instantiation (type, extension_specifier,
16566                                /*complain=*/tf_error);
16567     }
16568   else
16569     {
16570       cp_declarator *declarator;
16571       tree decl;
16572
16573       /* Parse the declarator.  */
16574       declarator
16575         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16576                                 /*ctor_dtor_or_conv_p=*/NULL,
16577                                 /*parenthesized_p=*/NULL,
16578                                 /*member_p=*/false,
16579                                 /*friend_p=*/false);
16580       if (declares_class_or_enum & 2)
16581         cp_parser_check_for_definition_in_return_type (declarator,
16582                                                        decl_specifiers.type,
16583                                                        decl_specifiers.locations[ds_type_spec]);
16584       if (declarator != cp_error_declarator)
16585         {
16586           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16587             permerror (decl_specifiers.locations[ds_inline],
16588                        "explicit instantiation shall not use"
16589                        " %<inline%> specifier");
16590           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16591             permerror (decl_specifiers.locations[ds_constexpr],
16592                        "explicit instantiation shall not use"
16593                        " %<constexpr%> specifier");
16594
16595           decl = grokdeclarator (declarator, &decl_specifiers,
16596                                  NORMAL, 0, &decl_specifiers.attributes);
16597           /* Turn access control back on for names used during
16598              template instantiation.  */
16599           pop_deferring_access_checks ();
16600           /* Do the explicit instantiation.  */
16601           do_decl_instantiation (decl, extension_specifier);
16602         }
16603       else
16604         {
16605           pop_deferring_access_checks ();
16606           /* Skip the body of the explicit instantiation.  */
16607           cp_parser_skip_to_end_of_statement (parser);
16608         }
16609     }
16610   /* We're done with the instantiation.  */
16611   end_explicit_instantiation ();
16612
16613   cp_parser_consume_semicolon_at_end_of_statement (parser);
16614
16615   timevar_pop (TV_TEMPLATE_INST);
16616 }
16617
16618 /* Parse an explicit-specialization.
16619
16620    explicit-specialization:
16621      template < > declaration
16622
16623    Although the standard says `declaration', what it really means is:
16624
16625    explicit-specialization:
16626      template <> decl-specifier [opt] init-declarator [opt] ;
16627      template <> function-definition
16628      template <> explicit-specialization
16629      template <> template-declaration  */
16630
16631 static void
16632 cp_parser_explicit_specialization (cp_parser* parser)
16633 {
16634   bool need_lang_pop;
16635   cp_token *token = cp_lexer_peek_token (parser->lexer);
16636
16637   /* Look for the `template' keyword.  */
16638   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16639   /* Look for the `<'.  */
16640   cp_parser_require (parser, CPP_LESS, RT_LESS);
16641   /* Look for the `>'.  */
16642   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16643   /* We have processed another parameter list.  */
16644   ++parser->num_template_parameter_lists;
16645   /* [temp]
16646
16647      A template ... explicit specialization ... shall not have C
16648      linkage.  */
16649   if (current_lang_name == lang_name_c)
16650     {
16651       error_at (token->location, "template specialization with C linkage");
16652       maybe_show_extern_c_location ();
16653       /* Give it C++ linkage to avoid confusing other parts of the
16654          front end.  */
16655       push_lang_context (lang_name_cplusplus);
16656       need_lang_pop = true;
16657     }
16658   else
16659     need_lang_pop = false;
16660   /* Let the front end know that we are beginning a specialization.  */
16661   if (!begin_specialization ())
16662     {
16663       end_specialization ();
16664       return;
16665     }
16666
16667   /* If the next keyword is `template', we need to figure out whether
16668      or not we're looking a template-declaration.  */
16669   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16670     {
16671       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16672           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16673         cp_parser_template_declaration_after_export (parser,
16674                                                      /*member_p=*/false);
16675       else
16676         cp_parser_explicit_specialization (parser);
16677     }
16678   else
16679     /* Parse the dependent declaration.  */
16680     cp_parser_single_declaration (parser,
16681                                   /*checks=*/NULL,
16682                                   /*member_p=*/false,
16683                                   /*explicit_specialization_p=*/true,
16684                                   /*friend_p=*/NULL);
16685   /* We're done with the specialization.  */
16686   end_specialization ();
16687   /* For the erroneous case of a template with C linkage, we pushed an
16688      implicit C++ linkage scope; exit that scope now.  */
16689   if (need_lang_pop)
16690     pop_lang_context ();
16691   /* We're done with this parameter list.  */
16692   --parser->num_template_parameter_lists;
16693 }
16694
16695 /* Parse a type-specifier.
16696
16697    type-specifier:
16698      simple-type-specifier
16699      class-specifier
16700      enum-specifier
16701      elaborated-type-specifier
16702      cv-qualifier
16703
16704    GNU Extension:
16705
16706    type-specifier:
16707      __complex__
16708
16709    Returns a representation of the type-specifier.  For a
16710    class-specifier, enum-specifier, or elaborated-type-specifier, a
16711    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16712
16713    The parser flags FLAGS is used to control type-specifier parsing.
16714
16715    If IS_DECLARATION is TRUE, then this type-specifier is appearing
16716    in a decl-specifier-seq.
16717
16718    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16719    class-specifier, enum-specifier, or elaborated-type-specifier, then
16720    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
16721    if a type is declared; 2 if it is defined.  Otherwise, it is set to
16722    zero.
16723
16724    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16725    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
16726    is set to FALSE.  */
16727
16728 static tree
16729 cp_parser_type_specifier (cp_parser* parser,
16730                           cp_parser_flags flags,
16731                           cp_decl_specifier_seq *decl_specs,
16732                           bool is_declaration,
16733                           int* declares_class_or_enum,
16734                           bool* is_cv_qualifier)
16735 {
16736   tree type_spec = NULL_TREE;
16737   cp_token *token;
16738   enum rid keyword;
16739   cp_decl_spec ds = ds_last;
16740
16741   /* Assume this type-specifier does not declare a new type.  */
16742   if (declares_class_or_enum)
16743     *declares_class_or_enum = 0;
16744   /* And that it does not specify a cv-qualifier.  */
16745   if (is_cv_qualifier)
16746     *is_cv_qualifier = false;
16747   /* Peek at the next token.  */
16748   token = cp_lexer_peek_token (parser->lexer);
16749
16750   /* If we're looking at a keyword, we can use that to guide the
16751      production we choose.  */
16752   keyword = token->keyword;
16753   switch (keyword)
16754     {
16755     case RID_ENUM:
16756       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16757         goto elaborated_type_specifier;
16758
16759       /* Look for the enum-specifier.  */
16760       type_spec = cp_parser_enum_specifier (parser);
16761       /* If that worked, we're done.  */
16762       if (type_spec)
16763         {
16764           if (declares_class_or_enum)
16765             *declares_class_or_enum = 2;
16766           if (decl_specs)
16767             cp_parser_set_decl_spec_type (decl_specs,
16768                                           type_spec,
16769                                           token,
16770                                           /*type_definition_p=*/true);
16771           return type_spec;
16772         }
16773       else
16774         goto elaborated_type_specifier;
16775
16776       /* Any of these indicate either a class-specifier, or an
16777          elaborated-type-specifier.  */
16778     case RID_CLASS:
16779     case RID_STRUCT:
16780     case RID_UNION:
16781       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16782         goto elaborated_type_specifier;
16783
16784       /* Parse tentatively so that we can back up if we don't find a
16785          class-specifier.  */
16786       cp_parser_parse_tentatively (parser);
16787       /* Look for the class-specifier.  */
16788       type_spec = cp_parser_class_specifier (parser);
16789       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16790       /* If that worked, we're done.  */
16791       if (cp_parser_parse_definitely (parser))
16792         {
16793           if (declares_class_or_enum)
16794             *declares_class_or_enum = 2;
16795           if (decl_specs)
16796             cp_parser_set_decl_spec_type (decl_specs,
16797                                           type_spec,
16798                                           token,
16799                                           /*type_definition_p=*/true);
16800           return type_spec;
16801         }
16802
16803       /* Fall through.  */
16804     elaborated_type_specifier:
16805       /* We're declaring (not defining) a class or enum.  */
16806       if (declares_class_or_enum)
16807         *declares_class_or_enum = 1;
16808
16809       /* Fall through.  */
16810     case RID_TYPENAME:
16811       /* Look for an elaborated-type-specifier.  */
16812       type_spec
16813         = (cp_parser_elaborated_type_specifier
16814            (parser,
16815             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16816             is_declaration));
16817       if (decl_specs)
16818         cp_parser_set_decl_spec_type (decl_specs,
16819                                       type_spec,
16820                                       token,
16821                                       /*type_definition_p=*/false);
16822       return type_spec;
16823
16824     case RID_CONST:
16825       ds = ds_const;
16826       if (is_cv_qualifier)
16827         *is_cv_qualifier = true;
16828       break;
16829
16830     case RID_VOLATILE:
16831       ds = ds_volatile;
16832       if (is_cv_qualifier)
16833         *is_cv_qualifier = true;
16834       break;
16835
16836     case RID_RESTRICT:
16837       ds = ds_restrict;
16838       if (is_cv_qualifier)
16839         *is_cv_qualifier = true;
16840       break;
16841
16842     case RID_COMPLEX:
16843       /* The `__complex__' keyword is a GNU extension.  */
16844       ds = ds_complex;
16845       break;
16846
16847     default:
16848       break;
16849     }
16850
16851   /* Handle simple keywords.  */
16852   if (ds != ds_last)
16853     {
16854       if (decl_specs)
16855         {
16856           set_and_check_decl_spec_loc (decl_specs, ds, token);
16857           decl_specs->any_specifiers_p = true;
16858         }
16859       return cp_lexer_consume_token (parser->lexer)->u.value;
16860     }
16861
16862   /* If we do not already have a type-specifier, assume we are looking
16863      at a simple-type-specifier.  */
16864   type_spec = cp_parser_simple_type_specifier (parser,
16865                                                decl_specs,
16866                                                flags);
16867
16868   /* If we didn't find a type-specifier, and a type-specifier was not
16869      optional in this context, issue an error message.  */
16870   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16871     {
16872       cp_parser_error (parser, "expected type specifier");
16873       return error_mark_node;
16874     }
16875
16876   return type_spec;
16877 }
16878
16879 /* Parse a simple-type-specifier.
16880
16881    simple-type-specifier:
16882      :: [opt] nested-name-specifier [opt] type-name
16883      :: [opt] nested-name-specifier template template-id
16884      char
16885      wchar_t
16886      bool
16887      short
16888      int
16889      long
16890      signed
16891      unsigned
16892      float
16893      double
16894      void
16895
16896    C++11 Extension:
16897
16898    simple-type-specifier:
16899      auto
16900      decltype ( expression )   
16901      char16_t
16902      char32_t
16903      __underlying_type ( type-id )
16904
16905    C++17 extension:
16906
16907      nested-name-specifier(opt) template-name
16908
16909    GNU Extension:
16910
16911    simple-type-specifier:
16912      __int128
16913      __typeof__ unary-expression
16914      __typeof__ ( type-id )
16915      __typeof__ ( type-id ) { initializer-list , [opt] }
16916
16917    Concepts Extension:
16918
16919    simple-type-specifier:
16920      constrained-type-specifier
16921
16922    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
16923    appropriately updated.  */
16924
16925 static tree
16926 cp_parser_simple_type_specifier (cp_parser* parser,
16927                                  cp_decl_specifier_seq *decl_specs,
16928                                  cp_parser_flags flags)
16929 {
16930   tree type = NULL_TREE;
16931   cp_token *token;
16932   int idx;
16933
16934   /* Peek at the next token.  */
16935   token = cp_lexer_peek_token (parser->lexer);
16936
16937   /* If we're looking at a keyword, things are easy.  */
16938   switch (token->keyword)
16939     {
16940     case RID_CHAR:
16941       if (decl_specs)
16942         decl_specs->explicit_char_p = true;
16943       type = char_type_node;
16944       break;
16945     case RID_CHAR16:
16946       type = char16_type_node;
16947       break;
16948     case RID_CHAR32:
16949       type = char32_type_node;
16950       break;
16951     case RID_WCHAR:
16952       type = wchar_type_node;
16953       break;
16954     case RID_BOOL:
16955       type = boolean_type_node;
16956       break;
16957     case RID_SHORT:
16958       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16959       type = short_integer_type_node;
16960       break;
16961     case RID_INT:
16962       if (decl_specs)
16963         decl_specs->explicit_int_p = true;
16964       type = integer_type_node;
16965       break;
16966     case RID_INT_N_0:
16967     case RID_INT_N_1:
16968     case RID_INT_N_2:
16969     case RID_INT_N_3:
16970       idx = token->keyword - RID_INT_N_0;
16971       if (! int_n_enabled_p [idx])
16972         break;
16973       if (decl_specs)
16974         {
16975           decl_specs->explicit_intN_p = true;
16976           decl_specs->int_n_idx = idx;
16977         }
16978       type = int_n_trees [idx].signed_type;
16979       break;
16980     case RID_LONG:
16981       if (decl_specs)
16982         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16983       type = long_integer_type_node;
16984       break;
16985     case RID_SIGNED:
16986       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16987       type = integer_type_node;
16988       break;
16989     case RID_UNSIGNED:
16990       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16991       type = unsigned_type_node;
16992       break;
16993     case RID_FLOAT:
16994       type = float_type_node;
16995       break;
16996     case RID_DOUBLE:
16997       type = double_type_node;
16998       break;
16999     case RID_VOID:
17000       type = void_type_node;
17001       break;
17002
17003     case RID_AUTO:
17004       maybe_warn_cpp0x (CPP0X_AUTO);
17005       if (parser->auto_is_implicit_function_template_parm_p)
17006         {
17007           /* The 'auto' might be the placeholder return type for a function decl
17008              with trailing return type.  */
17009           bool have_trailing_return_fn_decl = false;
17010
17011           cp_parser_parse_tentatively (parser);
17012           cp_lexer_consume_token (parser->lexer);
17013           while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17014                  && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17015                  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17016                  && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17017             {
17018               if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17019                 {
17020                   cp_lexer_consume_token (parser->lexer);
17021                   cp_parser_skip_to_closing_parenthesis (parser,
17022                                                          /*recovering*/false,
17023                                                          /*or_comma*/false,
17024                                                          /*consume_paren*/true);
17025                   continue;
17026                 }
17027
17028               if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17029                 {
17030                   have_trailing_return_fn_decl = true;
17031                   break;
17032                 }
17033
17034               cp_lexer_consume_token (parser->lexer);
17035             }
17036           cp_parser_abort_tentative_parse (parser);
17037
17038           if (have_trailing_return_fn_decl)
17039             {
17040               type = make_auto ();
17041               break;
17042             }
17043
17044           if (cxx_dialect >= cxx14)
17045             {
17046               type = synthesize_implicit_template_parm (parser, NULL_TREE);
17047               type = TREE_TYPE (type);
17048             }
17049           else
17050             type = error_mark_node;
17051
17052           if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17053             {
17054               if (cxx_dialect < cxx14)
17055                 error_at (token->location,
17056                          "use of %<auto%> in lambda parameter declaration "
17057                          "only available with "
17058                          "-std=c++14 or -std=gnu++14");
17059             }
17060           else if (cxx_dialect < cxx14)
17061             error_at (token->location,
17062                      "use of %<auto%> in parameter declaration "
17063                      "only available with "
17064                      "-std=c++14 or -std=gnu++14");
17065           else if (!flag_concepts)
17066             pedwarn (token->location, 0,
17067                      "use of %<auto%> in parameter declaration "
17068                      "only available with -fconcepts");
17069         }
17070       else
17071         type = make_auto ();
17072       break;
17073
17074     case RID_DECLTYPE:
17075       /* Since DR 743, decltype can either be a simple-type-specifier by
17076          itself or begin a nested-name-specifier.  Parsing it will replace
17077          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17078          handling below decide what to do.  */
17079       cp_parser_decltype (parser);
17080       cp_lexer_set_token_position (parser->lexer, token);
17081       break;
17082
17083     case RID_TYPEOF:
17084       /* Consume the `typeof' token.  */
17085       cp_lexer_consume_token (parser->lexer);
17086       /* Parse the operand to `typeof'.  */
17087       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17088       /* If it is not already a TYPE, take its type.  */
17089       if (!TYPE_P (type))
17090         type = finish_typeof (type);
17091
17092       if (decl_specs)
17093         cp_parser_set_decl_spec_type (decl_specs, type,
17094                                       token,
17095                                       /*type_definition_p=*/false);
17096
17097       return type;
17098
17099     case RID_UNDERLYING_TYPE:
17100       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17101       if (decl_specs)
17102         cp_parser_set_decl_spec_type (decl_specs, type,
17103                                       token,
17104                                       /*type_definition_p=*/false);
17105
17106       return type;
17107
17108     case RID_BASES:
17109     case RID_DIRECT_BASES:
17110       type = cp_parser_trait_expr (parser, token->keyword);
17111       if (decl_specs)
17112        cp_parser_set_decl_spec_type (decl_specs, type,
17113                                      token,
17114                                      /*type_definition_p=*/false);
17115       return type;
17116     default:
17117       break;
17118     }
17119
17120   /* If token is an already-parsed decltype not followed by ::,
17121      it's a simple-type-specifier.  */
17122   if (token->type == CPP_DECLTYPE
17123       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17124     {
17125       type = saved_checks_value (token->u.tree_check_value);
17126       if (decl_specs)
17127         {
17128           cp_parser_set_decl_spec_type (decl_specs, type,
17129                                         token,
17130                                         /*type_definition_p=*/false);
17131           /* Remember that we are handling a decltype in order to
17132              implement the resolution of DR 1510 when the argument
17133              isn't instantiation dependent.  */
17134           decl_specs->decltype_p = true;
17135         }
17136       cp_lexer_consume_token (parser->lexer);
17137       return type;
17138     }
17139
17140   /* If the type-specifier was for a built-in type, we're done.  */
17141   if (type)
17142     {
17143       /* Record the type.  */
17144       if (decl_specs
17145           && (token->keyword != RID_SIGNED
17146               && token->keyword != RID_UNSIGNED
17147               && token->keyword != RID_SHORT
17148               && token->keyword != RID_LONG))
17149         cp_parser_set_decl_spec_type (decl_specs,
17150                                       type,
17151                                       token,
17152                                       /*type_definition_p=*/false);
17153       if (decl_specs)
17154         decl_specs->any_specifiers_p = true;
17155
17156       /* Consume the token.  */
17157       cp_lexer_consume_token (parser->lexer);
17158
17159       if (type == error_mark_node)
17160         return error_mark_node;
17161
17162       /* There is no valid C++ program where a non-template type is
17163          followed by a "<".  That usually indicates that the user thought
17164          that the type was a template.  */
17165       cp_parser_check_for_invalid_template_id (parser, type, none_type,
17166                                                token->location);
17167
17168       return TYPE_NAME (type);
17169     }
17170
17171   /* The type-specifier must be a user-defined type.  */
17172   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17173     {
17174       bool qualified_p;
17175       bool global_p;
17176
17177       /* Don't gobble tokens or issue error messages if this is an
17178          optional type-specifier.  */
17179       if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17180         cp_parser_parse_tentatively (parser);
17181
17182       token = cp_lexer_peek_token (parser->lexer);
17183
17184       /* Look for the optional `::' operator.  */
17185       global_p
17186         = (cp_parser_global_scope_opt (parser,
17187                                        /*current_scope_valid_p=*/false)
17188            != NULL_TREE);
17189       /* Look for the nested-name specifier.  */
17190       qualified_p
17191         = (cp_parser_nested_name_specifier_opt (parser,
17192                                                 /*typename_keyword_p=*/false,
17193                                                 /*check_dependency_p=*/true,
17194                                                 /*type_p=*/false,
17195                                                 /*is_declaration=*/false)
17196            != NULL_TREE);
17197       /* If we have seen a nested-name-specifier, and the next token
17198          is `template', then we are using the template-id production.  */
17199       if (parser->scope
17200           && cp_parser_optional_template_keyword (parser))
17201         {
17202           /* Look for the template-id.  */
17203           type = cp_parser_template_id (parser,
17204                                         /*template_keyword_p=*/true,
17205                                         /*check_dependency_p=*/true,
17206                                         none_type,
17207                                         /*is_declaration=*/false);
17208           /* If the template-id did not name a type, we are out of
17209              luck.  */
17210           if (TREE_CODE (type) != TYPE_DECL)
17211             {
17212               cp_parser_error (parser, "expected template-id for type");
17213               type = NULL_TREE;
17214             }
17215         }
17216       /* Otherwise, look for a type-name.  */
17217       else
17218         type = cp_parser_type_name (parser);
17219       /* Keep track of all name-lookups performed in class scopes.  */
17220       if (type
17221           && !global_p
17222           && !qualified_p
17223           && TREE_CODE (type) == TYPE_DECL
17224           && identifier_p (DECL_NAME (type)))
17225         maybe_note_name_used_in_class (DECL_NAME (type), type);
17226       /* If it didn't work out, we don't have a TYPE.  */
17227       if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17228           && !cp_parser_parse_definitely (parser))
17229         type = NULL_TREE;
17230       if (!type && cxx_dialect >= cxx17)
17231         {
17232           if (flags & CP_PARSER_FLAGS_OPTIONAL)
17233             cp_parser_parse_tentatively (parser);
17234
17235           cp_parser_global_scope_opt (parser,
17236                                       /*current_scope_valid_p=*/false);
17237           cp_parser_nested_name_specifier_opt (parser,
17238                                                /*typename_keyword_p=*/false,
17239                                                /*check_dependency_p=*/true,
17240                                                /*type_p=*/false,
17241                                                /*is_declaration=*/false);
17242           tree name = cp_parser_identifier (parser);
17243           if (name && TREE_CODE (name) == IDENTIFIER_NODE
17244               && parser->scope != error_mark_node)
17245             {
17246               tree tmpl = cp_parser_lookup_name (parser, name,
17247                                                  none_type,
17248                                                  /*is_template=*/false,
17249                                                  /*is_namespace=*/false,
17250                                                  /*check_dependency=*/true,
17251                                                  /*ambiguous_decls=*/NULL,
17252                                                  token->location);
17253               if (tmpl && tmpl != error_mark_node
17254                   && (DECL_CLASS_TEMPLATE_P (tmpl)
17255                       || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17256                 type = make_template_placeholder (tmpl);
17257               else
17258                 {
17259                   type = error_mark_node;
17260                   if (!cp_parser_simulate_error (parser))
17261                     cp_parser_name_lookup_error (parser, name, tmpl,
17262                                                  NLE_TYPE, token->location);
17263                 }
17264             }
17265           else
17266             type = error_mark_node;
17267
17268           if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17269               && !cp_parser_parse_definitely (parser))
17270             type = NULL_TREE;
17271         }
17272       if (type && decl_specs)
17273         cp_parser_set_decl_spec_type (decl_specs, type,
17274                                       token,
17275                                       /*type_definition_p=*/false);
17276     }
17277
17278   /* If we didn't get a type-name, issue an error message.  */
17279   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17280     {
17281       cp_parser_error (parser, "expected type-name");
17282       return error_mark_node;
17283     }
17284
17285   if (type && type != error_mark_node)
17286     {
17287       /* See if TYPE is an Objective-C type, and if so, parse and
17288          accept any protocol references following it.  Do this before
17289          the cp_parser_check_for_invalid_template_id() call, because
17290          Objective-C types can be followed by '<...>' which would
17291          enclose protocol names rather than template arguments, and so
17292          everything is fine.  */
17293       if (c_dialect_objc () && !parser->scope
17294           && (objc_is_id (type) || objc_is_class_name (type)))
17295         {
17296           tree protos = cp_parser_objc_protocol_refs_opt (parser);
17297           tree qual_type = objc_get_protocol_qualified_type (type, protos);
17298
17299           /* Clobber the "unqualified" type previously entered into
17300              DECL_SPECS with the new, improved protocol-qualified version.  */
17301           if (decl_specs)
17302             decl_specs->type = qual_type;
17303
17304           return qual_type;
17305         }
17306
17307       /* There is no valid C++ program where a non-template type is
17308          followed by a "<".  That usually indicates that the user
17309          thought that the type was a template.  */
17310       cp_parser_check_for_invalid_template_id (parser, type,
17311                                                none_type,
17312                                                token->location);
17313     }
17314
17315   return type;
17316 }
17317
17318 /* Parse a type-name.
17319
17320    type-name:
17321      class-name
17322      enum-name
17323      typedef-name
17324      simple-template-id [in c++0x]
17325
17326    enum-name:
17327      identifier
17328
17329    typedef-name:
17330      identifier
17331
17332   Concepts:
17333
17334    type-name:
17335      concept-name
17336      partial-concept-id
17337
17338    concept-name:
17339      identifier
17340
17341    Returns a TYPE_DECL for the type.  */
17342
17343 static tree
17344 cp_parser_type_name (cp_parser* parser)
17345 {
17346   return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17347 }
17348
17349 /* See above. */
17350 static tree
17351 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17352 {
17353   tree type_decl;
17354
17355   /* We can't know yet whether it is a class-name or not.  */
17356   cp_parser_parse_tentatively (parser);
17357   /* Try a class-name.  */
17358   type_decl = cp_parser_class_name (parser,
17359                                     typename_keyword_p,
17360                                     /*template_keyword_p=*/false,
17361                                     none_type,
17362                                     /*check_dependency_p=*/true,
17363                                     /*class_head_p=*/false,
17364                                     /*is_declaration=*/false);
17365   /* If it's not a class-name, keep looking.  */
17366   if (!cp_parser_parse_definitely (parser))
17367     {
17368       if (cxx_dialect < cxx11)
17369         /* It must be a typedef-name or an enum-name.  */
17370         return cp_parser_nonclass_name (parser);
17371
17372       cp_parser_parse_tentatively (parser);
17373       /* It is either a simple-template-id representing an
17374          instantiation of an alias template...  */
17375       type_decl = cp_parser_template_id (parser,
17376                                          /*template_keyword_p=*/false,
17377                                          /*check_dependency_p=*/true,
17378                                          none_type,
17379                                          /*is_declaration=*/false);
17380       /* Note that this must be an instantiation of an alias template
17381          because [temp.names]/6 says:
17382          
17383              A template-id that names an alias template specialization
17384              is a type-name.
17385
17386          Whereas [temp.names]/7 says:
17387          
17388              A simple-template-id that names a class template
17389              specialization is a class-name.
17390
17391          With concepts, this could also be a partial-concept-id that
17392          declares a non-type template parameter. */
17393       if (type_decl != NULL_TREE
17394           && TREE_CODE (type_decl) == TYPE_DECL
17395           && TYPE_DECL_ALIAS_P (type_decl))
17396         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17397       else if (is_constrained_parameter (type_decl))
17398         /* Don't do anything. */ ;
17399       else
17400         cp_parser_simulate_error (parser);
17401
17402       if (!cp_parser_parse_definitely (parser))
17403         /* ... Or a typedef-name or an enum-name.  */
17404         return cp_parser_nonclass_name (parser);
17405     }
17406
17407   return type_decl;
17408 }
17409
17410 /*  Check if DECL and ARGS can form a constrained-type-specifier.
17411     If ARGS is non-null, we try to form a concept check of the
17412     form DECL<?, ARGS> where ? is a wildcard that matches any
17413     kind of template argument. If ARGS is NULL, then we try to
17414     form a concept check of the form DECL<?>. */
17415
17416 static tree
17417 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17418                                             tree decl, tree args)
17419 {
17420   gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17421
17422   /* If we a constrained-type-specifier cannot be deduced. */
17423   if (parser->prevent_constrained_type_specifiers)
17424     return NULL_TREE;
17425
17426   /* A constrained type specifier can only be found in an
17427      overload set or as a reference to a template declaration.
17428
17429      FIXME: This might be masking a bug.  It's possible that
17430      that the deduction below is causing template specializations
17431      to be formed with the wildcard as an argument.  */
17432   if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17433     return NULL_TREE;
17434
17435   /* Try to build a call expression that evaluates the
17436      concept. This can fail if the overload set refers
17437      only to non-templates. */
17438   tree placeholder = build_nt (WILDCARD_DECL);
17439   tree check = build_concept_check (decl, placeholder, args);
17440   if (check == error_mark_node)
17441     return NULL_TREE;
17442
17443   /* Deduce the checked constraint and the prototype parameter.
17444
17445      FIXME: In certain cases, failure to deduce should be a
17446      diagnosable error.  */
17447   tree conc;
17448   tree proto;
17449   if (!deduce_constrained_parameter (check, conc, proto))
17450     return NULL_TREE;
17451
17452   /* In template parameter scope, this results in a constrained
17453      parameter. Return a descriptor of that parm. */
17454   if (processing_template_parmlist)
17455     return build_constrained_parameter (conc, proto, args);
17456
17457   /* In a parameter-declaration-clause, constrained-type
17458      specifiers result in invented template parameters.  */
17459   if (parser->auto_is_implicit_function_template_parm_p)
17460     {
17461       tree x = build_constrained_parameter (conc, proto, args);
17462       return synthesize_implicit_template_parm (parser, x);
17463     }
17464   else
17465     {
17466      /* Otherwise, we're in a context where the constrained
17467         type name is deduced and the constraint applies
17468         after deduction. */
17469       return make_constrained_auto (conc, args);
17470     }
17471
17472   return NULL_TREE;
17473 }
17474
17475 /* If DECL refers to a concept, return a TYPE_DECL representing
17476    the result of using the constrained type specifier in the
17477    current context.  DECL refers to a concept if
17478
17479   - it is an overload set containing a function concept taking a single
17480     type argument, or
17481
17482   - it is a variable concept taking a single type argument.  */
17483
17484 static tree
17485 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17486 {
17487   if (flag_concepts
17488       && (TREE_CODE (decl) == OVERLOAD
17489           || BASELINK_P (decl)
17490           || variable_concept_p (decl)))
17491     return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17492   else
17493     return NULL_TREE;
17494 }
17495
17496 /* Check if DECL and ARGS form a partial-concept-id.  If so,
17497    assign ID to the resulting constrained placeholder.
17498
17499    Returns true if the partial-concept-id designates a placeholder
17500    and false otherwise. Note that *id is set to NULL_TREE in
17501    this case. */
17502
17503 static tree
17504 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17505 {
17506   return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17507 }
17508
17509 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17510    or a concept-name.
17511
17512    enum-name:
17513      identifier
17514
17515    typedef-name:
17516      identifier
17517
17518    concept-name:
17519      identifier
17520
17521    Returns a TYPE_DECL for the type.  */
17522
17523 static tree
17524 cp_parser_nonclass_name (cp_parser* parser)
17525 {
17526   tree type_decl;
17527   tree identifier;
17528
17529   cp_token *token = cp_lexer_peek_token (parser->lexer);
17530   identifier = cp_parser_identifier (parser);
17531   if (identifier == error_mark_node)
17532     return error_mark_node;
17533
17534   /* Look up the type-name.  */
17535   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17536
17537   type_decl = strip_using_decl (type_decl);
17538   
17539   /* If we found an overload set, then it may refer to a concept-name. */
17540   if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17541     type_decl = decl;
17542
17543   if (TREE_CODE (type_decl) != TYPE_DECL
17544       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17545     {
17546       /* See if this is an Objective-C type.  */
17547       tree protos = cp_parser_objc_protocol_refs_opt (parser);
17548       tree type = objc_get_protocol_qualified_type (identifier, protos);
17549       if (type)
17550         type_decl = TYPE_NAME (type);
17551     }
17552
17553   /* Issue an error if we did not find a type-name.  */
17554   if (TREE_CODE (type_decl) != TYPE_DECL
17555       /* In Objective-C, we have the complication that class names are
17556          normally type names and start declarations (eg, the
17557          "NSObject" in "NSObject *object;"), but can be used in an
17558          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17559          is an expression.  So, a classname followed by a dot is not a
17560          valid type-name.  */
17561       || (objc_is_class_name (TREE_TYPE (type_decl))
17562           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17563     {
17564       if (!cp_parser_simulate_error (parser))
17565         cp_parser_name_lookup_error (parser, identifier, type_decl,
17566                                      NLE_TYPE, token->location);
17567       return error_mark_node;
17568     }
17569   /* Remember that the name was used in the definition of the
17570      current class so that we can check later to see if the
17571      meaning would have been different after the class was
17572      entirely defined.  */
17573   else if (type_decl != error_mark_node
17574            && !parser->scope)
17575     maybe_note_name_used_in_class (identifier, type_decl);
17576   
17577   return type_decl;
17578 }
17579
17580 /* Parse an elaborated-type-specifier.  Note that the grammar given
17581    here incorporates the resolution to DR68.
17582
17583    elaborated-type-specifier:
17584      class-key :: [opt] nested-name-specifier [opt] identifier
17585      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17586      enum-key :: [opt] nested-name-specifier [opt] identifier
17587      typename :: [opt] nested-name-specifier identifier
17588      typename :: [opt] nested-name-specifier template [opt]
17589        template-id
17590
17591    GNU extension:
17592
17593    elaborated-type-specifier:
17594      class-key attributes :: [opt] nested-name-specifier [opt] identifier
17595      class-key attributes :: [opt] nested-name-specifier [opt]
17596                template [opt] template-id
17597      enum attributes :: [opt] nested-name-specifier [opt] identifier
17598
17599    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17600    declared `friend'.  If IS_DECLARATION is TRUE, then this
17601    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17602    something is being declared.
17603
17604    Returns the TYPE specified.  */
17605
17606 static tree
17607 cp_parser_elaborated_type_specifier (cp_parser* parser,
17608                                      bool is_friend,
17609                                      bool is_declaration)
17610 {
17611   enum tag_types tag_type;
17612   tree identifier;
17613   tree type = NULL_TREE;
17614   tree attributes = NULL_TREE;
17615   tree globalscope;
17616   cp_token *token = NULL;
17617
17618   /* See if we're looking at the `enum' keyword.  */
17619   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17620     {
17621       /* Consume the `enum' token.  */
17622       cp_lexer_consume_token (parser->lexer);
17623       /* Remember that it's an enumeration type.  */
17624       tag_type = enum_type;
17625       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17626          enums) is used here.  */
17627       cp_token *token = cp_lexer_peek_token (parser->lexer);
17628       if (cp_parser_is_keyword (token, RID_CLASS)
17629           || cp_parser_is_keyword (token, RID_STRUCT))
17630         {
17631           gcc_rich_location richloc (token->location);
17632           richloc.add_range (input_location, false);
17633           richloc.add_fixit_remove ();
17634           pedwarn (&richloc, 0, "elaborated-type-specifier for "
17635                    "a scoped enum must not use the %qD keyword",
17636                    token->u.value);
17637           /* Consume the `struct' or `class' and parse it anyway.  */
17638           cp_lexer_consume_token (parser->lexer);
17639         }
17640       /* Parse the attributes.  */
17641       attributes = cp_parser_attributes_opt (parser);
17642     }
17643   /* Or, it might be `typename'.  */
17644   else if (cp_lexer_next_token_is_keyword (parser->lexer,
17645                                            RID_TYPENAME))
17646     {
17647       /* Consume the `typename' token.  */
17648       cp_lexer_consume_token (parser->lexer);
17649       /* Remember that it's a `typename' type.  */
17650       tag_type = typename_type;
17651     }
17652   /* Otherwise it must be a class-key.  */
17653   else
17654     {
17655       tag_type = cp_parser_class_key (parser);
17656       if (tag_type == none_type)
17657         return error_mark_node;
17658       /* Parse the attributes.  */
17659       attributes = cp_parser_attributes_opt (parser);
17660     }
17661
17662   /* Look for the `::' operator.  */
17663   globalscope =  cp_parser_global_scope_opt (parser,
17664                                              /*current_scope_valid_p=*/false);
17665   /* Look for the nested-name-specifier.  */
17666   tree nested_name_specifier;
17667   if (tag_type == typename_type && !globalscope)
17668     {
17669       nested_name_specifier
17670         = cp_parser_nested_name_specifier (parser,
17671                                            /*typename_keyword_p=*/true,
17672                                            /*check_dependency_p=*/true,
17673                                            /*type_p=*/true,
17674                                            is_declaration);
17675       if (!nested_name_specifier)
17676         return error_mark_node;
17677     }
17678   else
17679     /* Even though `typename' is not present, the proposed resolution
17680        to Core Issue 180 says that in `class A<T>::B', `B' should be
17681        considered a type-name, even if `A<T>' is dependent.  */
17682     nested_name_specifier
17683       = cp_parser_nested_name_specifier_opt (parser,
17684                                              /*typename_keyword_p=*/true,
17685                                              /*check_dependency_p=*/true,
17686                                              /*type_p=*/true,
17687                                              is_declaration);
17688  /* For everything but enumeration types, consider a template-id.
17689     For an enumeration type, consider only a plain identifier.  */
17690   if (tag_type != enum_type)
17691     {
17692       bool template_p = false;
17693       tree decl;
17694
17695       /* Allow the `template' keyword.  */
17696       template_p = cp_parser_optional_template_keyword (parser);
17697       /* If we didn't see `template', we don't know if there's a
17698          template-id or not.  */
17699       if (!template_p)
17700         cp_parser_parse_tentatively (parser);
17701       /* Parse the template-id.  */
17702       token = cp_lexer_peek_token (parser->lexer);
17703       decl = cp_parser_template_id (parser, template_p,
17704                                     /*check_dependency_p=*/true,
17705                                     tag_type,
17706                                     is_declaration);
17707       /* If we didn't find a template-id, look for an ordinary
17708          identifier.  */
17709       if (!template_p && !cp_parser_parse_definitely (parser))
17710         ;
17711       /* We can get here when cp_parser_template_id, called by
17712          cp_parser_class_name with tag_type == none_type, succeeds
17713          and caches a BASELINK.  Then, when called again here,
17714          instead of failing and returning an error_mark_node
17715          returns it (see template/typename17.C in C++11).
17716          ??? Could we diagnose this earlier?  */
17717       else if (tag_type == typename_type && BASELINK_P (decl))
17718         {
17719           cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17720           type = error_mark_node;
17721         }
17722       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17723          in effect, then we must assume that, upon instantiation, the
17724          template will correspond to a class.  */
17725       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17726                && tag_type == typename_type)
17727         type = make_typename_type (parser->scope, decl,
17728                                    typename_type,
17729                                    /*complain=*/tf_error);
17730       /* If the `typename' keyword is in effect and DECL is not a type
17731          decl, then type is non existent.   */
17732       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17733         ; 
17734       else if (TREE_CODE (decl) == TYPE_DECL)
17735         {
17736           type = check_elaborated_type_specifier (tag_type, decl,
17737                                                   /*allow_template_p=*/true);
17738
17739           /* If the next token is a semicolon, this must be a specialization,
17740              instantiation, or friend declaration.  Check the scope while we
17741              still know whether or not we had a nested-name-specifier.  */
17742           if (type != error_mark_node
17743               && !nested_name_specifier && !is_friend
17744               && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17745             check_unqualified_spec_or_inst (type, token->location);
17746         }
17747       else if (decl == error_mark_node)
17748         type = error_mark_node; 
17749     }
17750
17751   if (!type)
17752     {
17753       token = cp_lexer_peek_token (parser->lexer);
17754       identifier = cp_parser_identifier (parser);
17755
17756       if (identifier == error_mark_node)
17757         {
17758           parser->scope = NULL_TREE;
17759           return error_mark_node;
17760         }
17761
17762       /* For a `typename', we needn't call xref_tag.  */
17763       if (tag_type == typename_type
17764           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17765         return cp_parser_make_typename_type (parser, identifier,
17766                                              token->location);
17767
17768       /* Template parameter lists apply only if we are not within a
17769          function parameter list.  */
17770       bool template_parm_lists_apply
17771           = parser->num_template_parameter_lists;
17772       if (template_parm_lists_apply)
17773         for (cp_binding_level *s = current_binding_level;
17774              s && s->kind != sk_template_parms;
17775              s = s->level_chain)
17776           if (s->kind == sk_function_parms)
17777             template_parm_lists_apply = false;
17778
17779       /* Look up a qualified name in the usual way.  */
17780       if (parser->scope)
17781         {
17782           tree decl;
17783           tree ambiguous_decls;
17784
17785           decl = cp_parser_lookup_name (parser, identifier,
17786                                         tag_type,
17787                                         /*is_template=*/false,
17788                                         /*is_namespace=*/false,
17789                                         /*check_dependency=*/true,
17790                                         &ambiguous_decls,
17791                                         token->location);
17792
17793           /* If the lookup was ambiguous, an error will already have been
17794              issued.  */
17795           if (ambiguous_decls)
17796             return error_mark_node;
17797
17798           /* If we are parsing friend declaration, DECL may be a
17799              TEMPLATE_DECL tree node here.  However, we need to check
17800              whether this TEMPLATE_DECL results in valid code.  Consider
17801              the following example:
17802
17803                namespace N {
17804                  template <class T> class C {};
17805                }
17806                class X {
17807                  template <class T> friend class N::C; // #1, valid code
17808                };
17809                template <class T> class Y {
17810                  friend class N::C;                    // #2, invalid code
17811                };
17812
17813              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17814              name lookup of `N::C'.  We see that friend declaration must
17815              be template for the code to be valid.  Note that
17816              processing_template_decl does not work here since it is
17817              always 1 for the above two cases.  */
17818
17819           decl = (cp_parser_maybe_treat_template_as_class
17820                   (decl, /*tag_name_p=*/is_friend
17821                          && template_parm_lists_apply));
17822
17823           if (TREE_CODE (decl) != TYPE_DECL)
17824             {
17825               cp_parser_diagnose_invalid_type_name (parser,
17826                                                     identifier,
17827                                                     token->location);
17828               return error_mark_node;
17829             }
17830
17831           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17832             {
17833               bool allow_template = (template_parm_lists_apply
17834                                      || DECL_SELF_REFERENCE_P (decl));
17835               type = check_elaborated_type_specifier (tag_type, decl,
17836                                                       allow_template);
17837
17838               if (type == error_mark_node)
17839                 return error_mark_node;
17840             }
17841
17842           /* Forward declarations of nested types, such as
17843
17844                class C1::C2;
17845                class C1::C2::C3;
17846
17847              are invalid unless all components preceding the final '::'
17848              are complete.  If all enclosing types are complete, these
17849              declarations become merely pointless.
17850
17851              Invalid forward declarations of nested types are errors
17852              caught elsewhere in parsing.  Those that are pointless arrive
17853              here.  */
17854
17855           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17856               && !is_friend && !processing_explicit_instantiation)
17857             warning (0, "declaration %qD does not declare anything", decl);
17858
17859           type = TREE_TYPE (decl);
17860         }
17861       else
17862         {
17863           /* An elaborated-type-specifier sometimes introduces a new type and
17864              sometimes names an existing type.  Normally, the rule is that it
17865              introduces a new type only if there is not an existing type of
17866              the same name already in scope.  For example, given:
17867
17868                struct S {};
17869                void f() { struct S s; }
17870
17871              the `struct S' in the body of `f' is the same `struct S' as in
17872              the global scope; the existing definition is used.  However, if
17873              there were no global declaration, this would introduce a new
17874              local class named `S'.
17875
17876              An exception to this rule applies to the following code:
17877
17878                namespace N { struct S; }
17879
17880              Here, the elaborated-type-specifier names a new type
17881              unconditionally; even if there is already an `S' in the
17882              containing scope this declaration names a new type.
17883              This exception only applies if the elaborated-type-specifier
17884              forms the complete declaration:
17885
17886                [class.name]
17887
17888                A declaration consisting solely of `class-key identifier ;' is
17889                either a redeclaration of the name in the current scope or a
17890                forward declaration of the identifier as a class name.  It
17891                introduces the name into the current scope.
17892
17893              We are in this situation precisely when the next token is a `;'.
17894
17895              An exception to the exception is that a `friend' declaration does
17896              *not* name a new type; i.e., given:
17897
17898                struct S { friend struct T; };
17899
17900              `T' is not a new type in the scope of `S'.
17901
17902              Also, `new struct S' or `sizeof (struct S)' never results in the
17903              definition of a new type; a new type can only be declared in a
17904              declaration context.  */
17905
17906           tag_scope ts;
17907           bool template_p;
17908
17909           if (is_friend)
17910             /* Friends have special name lookup rules.  */
17911             ts = ts_within_enclosing_non_class;
17912           else if (is_declaration
17913                    && cp_lexer_next_token_is (parser->lexer,
17914                                               CPP_SEMICOLON))
17915             /* This is a `class-key identifier ;' */
17916             ts = ts_current;
17917           else
17918             ts = ts_global;
17919
17920           template_p =
17921             (template_parm_lists_apply
17922              && (cp_parser_next_token_starts_class_definition_p (parser)
17923                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17924           /* An unqualified name was used to reference this type, so
17925              there were no qualifying templates.  */
17926           if (template_parm_lists_apply
17927               && !cp_parser_check_template_parameters (parser,
17928                                                        /*num_templates=*/0,
17929                                                        /*template_id*/false,
17930                                                        token->location,
17931                                                        /*declarator=*/NULL))
17932             return error_mark_node;
17933           type = xref_tag (tag_type, identifier, ts, template_p);
17934         }
17935     }
17936
17937   if (type == error_mark_node)
17938     return error_mark_node;
17939
17940   /* Allow attributes on forward declarations of classes.  */
17941   if (attributes)
17942     {
17943       if (TREE_CODE (type) == TYPENAME_TYPE)
17944         warning (OPT_Wattributes,
17945                  "attributes ignored on uninstantiated type");
17946       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17947                && ! processing_explicit_instantiation)
17948         warning (OPT_Wattributes,
17949                  "attributes ignored on template instantiation");
17950       else if (is_declaration && cp_parser_declares_only_class_p (parser))
17951         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17952       else
17953         warning (OPT_Wattributes,
17954                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17955     }
17956
17957   if (tag_type != enum_type)
17958     {
17959       /* Indicate whether this class was declared as a `class' or as a
17960          `struct'.  */
17961       if (CLASS_TYPE_P (type))
17962         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17963       cp_parser_check_class_key (tag_type, type);
17964     }
17965
17966   /* A "<" cannot follow an elaborated type specifier.  If that
17967      happens, the user was probably trying to form a template-id.  */
17968   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17969                                            token->location);
17970
17971   return type;
17972 }
17973
17974 /* Parse an enum-specifier.
17975
17976    enum-specifier:
17977      enum-head { enumerator-list [opt] }
17978      enum-head { enumerator-list , } [C++0x]
17979
17980    enum-head:
17981      enum-key identifier [opt] enum-base [opt]
17982      enum-key nested-name-specifier identifier enum-base [opt]
17983
17984    enum-key:
17985      enum
17986      enum class   [C++0x]
17987      enum struct  [C++0x]
17988
17989    enum-base:   [C++0x]
17990      : type-specifier-seq
17991
17992    opaque-enum-specifier:
17993      enum-key identifier enum-base [opt] ;
17994
17995    GNU Extensions:
17996      enum-key attributes[opt] identifier [opt] enum-base [opt] 
17997        { enumerator-list [opt] }attributes[opt]
17998      enum-key attributes[opt] identifier [opt] enum-base [opt]
17999        { enumerator-list, }attributes[opt] [C++0x]
18000
18001    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18002    if the token stream isn't an enum-specifier after all.  */
18003
18004 static tree
18005 cp_parser_enum_specifier (cp_parser* parser)
18006 {
18007   tree identifier;
18008   tree type = NULL_TREE;
18009   tree prev_scope;
18010   tree nested_name_specifier = NULL_TREE;
18011   tree attributes;
18012   bool scoped_enum_p = false;
18013   bool has_underlying_type = false;
18014   bool nested_being_defined = false;
18015   bool new_value_list = false;
18016   bool is_new_type = false;
18017   bool is_unnamed = false;
18018   tree underlying_type = NULL_TREE;
18019   cp_token *type_start_token = NULL;
18020   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18021
18022   parser->colon_corrects_to_scope_p = false;
18023
18024   /* Parse tentatively so that we can back up if we don't find a
18025      enum-specifier.  */
18026   cp_parser_parse_tentatively (parser);
18027
18028   /* Caller guarantees that the current token is 'enum', an identifier
18029      possibly follows, and the token after that is an opening brace.
18030      If we don't have an identifier, fabricate an anonymous name for
18031      the enumeration being defined.  */
18032   cp_lexer_consume_token (parser->lexer);
18033
18034   /* Parse the "class" or "struct", which indicates a scoped
18035      enumeration type in C++0x.  */
18036   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18037       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18038     {
18039       if (cxx_dialect < cxx11)
18040         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18041
18042       /* Consume the `struct' or `class' token.  */
18043       cp_lexer_consume_token (parser->lexer);
18044
18045       scoped_enum_p = true;
18046     }
18047
18048   attributes = cp_parser_attributes_opt (parser);
18049
18050   /* Clear the qualification.  */
18051   parser->scope = NULL_TREE;
18052   parser->qualifying_scope = NULL_TREE;
18053   parser->object_scope = NULL_TREE;
18054
18055   /* Figure out in what scope the declaration is being placed.  */
18056   prev_scope = current_scope ();
18057
18058   type_start_token = cp_lexer_peek_token (parser->lexer);
18059
18060   push_deferring_access_checks (dk_no_check);
18061   nested_name_specifier
18062       = cp_parser_nested_name_specifier_opt (parser,
18063                                              /*typename_keyword_p=*/true,
18064                                              /*check_dependency_p=*/false,
18065                                              /*type_p=*/false,
18066                                              /*is_declaration=*/false);
18067
18068   if (nested_name_specifier)
18069     {
18070       tree name;
18071
18072       identifier = cp_parser_identifier (parser);
18073       name =  cp_parser_lookup_name (parser, identifier,
18074                                      enum_type,
18075                                      /*is_template=*/false,
18076                                      /*is_namespace=*/false,
18077                                      /*check_dependency=*/true,
18078                                      /*ambiguous_decls=*/NULL,
18079                                      input_location);
18080       if (name && name != error_mark_node)
18081         {
18082           type = TREE_TYPE (name);
18083           if (TREE_CODE (type) == TYPENAME_TYPE)
18084             {
18085               /* Are template enums allowed in ISO? */
18086               if (template_parm_scope_p ())
18087                 pedwarn (type_start_token->location, OPT_Wpedantic,
18088                          "%qD is an enumeration template", name);
18089               /* ignore a typename reference, for it will be solved by name
18090                  in start_enum.  */
18091               type = NULL_TREE;
18092             }
18093         }
18094       else if (nested_name_specifier == error_mark_node)
18095         /* We already issued an error.  */;
18096       else
18097         {
18098           error_at (type_start_token->location,
18099                     "%qD does not name an enumeration in %qT",
18100                     identifier, nested_name_specifier);
18101           nested_name_specifier = error_mark_node;
18102         }
18103     }
18104   else
18105     {
18106       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18107         identifier = cp_parser_identifier (parser);
18108       else
18109         {
18110           identifier = make_anon_name ();
18111           is_unnamed = true;
18112           if (scoped_enum_p)
18113             error_at (type_start_token->location,
18114                       "unnamed scoped enum is not allowed");
18115         }
18116     }
18117   pop_deferring_access_checks ();
18118
18119   /* Check for the `:' that denotes a specified underlying type in C++0x.
18120      Note that a ':' could also indicate a bitfield width, however.  */
18121   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18122     {
18123       cp_decl_specifier_seq type_specifiers;
18124
18125       /* Consume the `:'.  */
18126       cp_lexer_consume_token (parser->lexer);
18127
18128       /* Parse the type-specifier-seq.  */
18129       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18130                                     /*is_trailing_return=*/false,
18131                                     &type_specifiers);
18132
18133       /* At this point this is surely not elaborated type specifier.  */
18134       if (!cp_parser_parse_definitely (parser))
18135         return NULL_TREE;
18136
18137       if (cxx_dialect < cxx11)
18138         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18139
18140       has_underlying_type = true;
18141
18142       /* If that didn't work, stop.  */
18143       if (type_specifiers.type != error_mark_node)
18144         {
18145           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18146                                             /*initialized=*/0, NULL);
18147           if (underlying_type == error_mark_node
18148               || check_for_bare_parameter_packs (underlying_type))
18149             underlying_type = NULL_TREE;
18150         }
18151     }
18152
18153   /* Look for the `{' but don't consume it yet.  */
18154   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18155     {
18156       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18157         {
18158           cp_parser_error (parser, "expected %<{%>");
18159           if (has_underlying_type)
18160             {
18161               type = NULL_TREE;
18162               goto out;
18163             }
18164         }
18165       /* An opaque-enum-specifier must have a ';' here.  */
18166       if ((scoped_enum_p || underlying_type)
18167           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18168         {
18169           cp_parser_error (parser, "expected %<;%> or %<{%>");
18170           if (has_underlying_type)
18171             {
18172               type = NULL_TREE;
18173               goto out;
18174             }
18175         }
18176     }
18177
18178   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18179     return NULL_TREE;
18180
18181   if (nested_name_specifier)
18182     {
18183       if (CLASS_TYPE_P (nested_name_specifier))
18184         {
18185           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18186           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18187           push_scope (nested_name_specifier);
18188         }
18189       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18190         {
18191           push_nested_namespace (nested_name_specifier);
18192         }
18193     }
18194
18195   /* Issue an error message if type-definitions are forbidden here.  */
18196   if (!cp_parser_check_type_definition (parser))
18197     type = error_mark_node;
18198   else
18199     /* Create the new type.  We do this before consuming the opening
18200        brace so the enum will be recorded as being on the line of its
18201        tag (or the 'enum' keyword, if there is no tag).  */
18202     type = start_enum (identifier, type, underlying_type,
18203                        attributes, scoped_enum_p, &is_new_type);
18204
18205   /* If the next token is not '{' it is an opaque-enum-specifier or an
18206      elaborated-type-specifier.  */
18207   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18208     {
18209       timevar_push (TV_PARSE_ENUM);
18210       if (nested_name_specifier
18211           && nested_name_specifier != error_mark_node)
18212         {
18213           /* The following catches invalid code such as:
18214              enum class S<int>::E { A, B, C }; */
18215           if (!processing_specialization
18216               && CLASS_TYPE_P (nested_name_specifier)
18217               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18218             error_at (type_start_token->location, "cannot add an enumerator "
18219                       "list to a template instantiation");
18220
18221           if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18222             {
18223               error_at (type_start_token->location,
18224                         "%<%T::%E%> has not been declared",
18225                         TYPE_CONTEXT (nested_name_specifier),
18226                         nested_name_specifier);
18227               type = error_mark_node;
18228             }
18229           else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18230                    && !CLASS_TYPE_P (nested_name_specifier))
18231             {
18232               error_at (type_start_token->location, "nested name specifier "
18233                         "%qT for enum declaration does not name a class "
18234                         "or namespace", nested_name_specifier);
18235               type = error_mark_node;
18236             }
18237           /* If that scope does not contain the scope in which the
18238              class was originally declared, the program is invalid.  */
18239           else if (prev_scope && !is_ancestor (prev_scope,
18240                                                nested_name_specifier))
18241             {
18242               if (at_namespace_scope_p ())
18243                 error_at (type_start_token->location,
18244                           "declaration of %qD in namespace %qD which does not "
18245                           "enclose %qD",
18246                           type, prev_scope, nested_name_specifier);
18247               else
18248                 error_at (type_start_token->location,
18249                           "declaration of %qD in %qD which does not "
18250                           "enclose %qD",
18251                           type, prev_scope, nested_name_specifier);
18252               type = error_mark_node;
18253             }
18254           /* If that scope is the scope where the declaration is being placed
18255              the program is invalid.  */
18256           else if (CLASS_TYPE_P (nested_name_specifier)
18257                    && CLASS_TYPE_P (prev_scope)
18258                    && same_type_p (nested_name_specifier, prev_scope))
18259             {
18260               permerror (type_start_token->location,
18261                          "extra qualification not allowed");
18262               nested_name_specifier = NULL_TREE;
18263             }
18264         }
18265
18266       if (scoped_enum_p)
18267         begin_scope (sk_scoped_enum, type);
18268
18269       /* Consume the opening brace.  */
18270       matching_braces braces;
18271       braces.consume_open (parser);
18272
18273       if (type == error_mark_node)
18274         ; /* Nothing to add */
18275       else if (OPAQUE_ENUM_P (type)
18276                || (cxx_dialect > cxx98 && processing_specialization))
18277         {
18278           new_value_list = true;
18279           SET_OPAQUE_ENUM_P (type, false);
18280           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18281         }
18282       else
18283         {
18284           error_at (type_start_token->location,
18285                     "multiple definition of %q#T", type);
18286           inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18287                   "previous definition here");
18288           type = error_mark_node;
18289         }
18290
18291       if (type == error_mark_node)
18292         cp_parser_skip_to_end_of_block_or_statement (parser);
18293       /* If the next token is not '}', then there are some enumerators.  */
18294       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18295         {
18296           if (is_unnamed && !scoped_enum_p)
18297             pedwarn (type_start_token->location, OPT_Wpedantic,
18298                      "ISO C++ forbids empty unnamed enum");
18299         }
18300       else
18301         cp_parser_enumerator_list (parser, type);
18302
18303       /* Consume the final '}'.  */
18304       braces.require_close (parser);
18305
18306       if (scoped_enum_p)
18307         finish_scope ();
18308       timevar_pop (TV_PARSE_ENUM);
18309     }
18310   else
18311     {
18312       /* If a ';' follows, then it is an opaque-enum-specifier
18313         and additional restrictions apply.  */
18314       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18315         {
18316           if (is_unnamed)
18317             error_at (type_start_token->location,
18318                       "opaque-enum-specifier without name");
18319           else if (nested_name_specifier)
18320             error_at (type_start_token->location,
18321                       "opaque-enum-specifier must use a simple identifier");
18322         }
18323     }
18324
18325   /* Look for trailing attributes to apply to this enumeration, and
18326      apply them if appropriate.  */
18327   if (cp_parser_allow_gnu_extensions_p (parser))
18328     {
18329       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18330       cplus_decl_attributes (&type,
18331                              trailing_attr,
18332                              (int) ATTR_FLAG_TYPE_IN_PLACE);
18333     }
18334
18335   /* Finish up the enumeration.  */
18336   if (type != error_mark_node)
18337     {
18338       if (new_value_list)
18339         finish_enum_value_list (type);
18340       if (is_new_type)
18341         finish_enum (type);
18342     }
18343
18344   if (nested_name_specifier)
18345     {
18346       if (CLASS_TYPE_P (nested_name_specifier))
18347         {
18348           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18349           pop_scope (nested_name_specifier);
18350         }
18351       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18352         {
18353           pop_nested_namespace (nested_name_specifier);
18354         }
18355     }
18356  out:
18357   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18358   return type;
18359 }
18360
18361 /* Parse an enumerator-list.  The enumerators all have the indicated
18362    TYPE.
18363
18364    enumerator-list:
18365      enumerator-definition
18366      enumerator-list , enumerator-definition  */
18367
18368 static void
18369 cp_parser_enumerator_list (cp_parser* parser, tree type)
18370 {
18371   while (true)
18372     {
18373       /* Parse an enumerator-definition.  */
18374       cp_parser_enumerator_definition (parser, type);
18375
18376       /* If the next token is not a ',', we've reached the end of
18377          the list.  */
18378       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18379         break;
18380       /* Otherwise, consume the `,' and keep going.  */
18381       cp_lexer_consume_token (parser->lexer);
18382       /* If the next token is a `}', there is a trailing comma.  */
18383       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18384         {
18385           if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18386             pedwarn (input_location, OPT_Wpedantic,
18387                      "comma at end of enumerator list");
18388           break;
18389         }
18390     }
18391 }
18392
18393 /* Parse an enumerator-definition.  The enumerator has the indicated
18394    TYPE.
18395
18396    enumerator-definition:
18397      enumerator
18398      enumerator = constant-expression
18399
18400    enumerator:
18401      identifier
18402
18403    GNU Extensions:
18404
18405    enumerator-definition:
18406      enumerator attributes [opt]
18407      enumerator attributes [opt] = constant-expression  */
18408
18409 static void
18410 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18411 {
18412   tree identifier;
18413   tree value;
18414   location_t loc;
18415
18416   /* Save the input location because we are interested in the location
18417      of the identifier and not the location of the explicit value.  */
18418   loc = cp_lexer_peek_token (parser->lexer)->location;
18419
18420   /* Look for the identifier.  */
18421   identifier = cp_parser_identifier (parser);
18422   if (identifier == error_mark_node)
18423     return;
18424
18425   /* Parse any specified attributes.  */
18426   tree attrs = cp_parser_attributes_opt (parser);
18427
18428   /* If the next token is an '=', then there is an explicit value.  */
18429   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18430     {
18431       /* Consume the `=' token.  */
18432       cp_lexer_consume_token (parser->lexer);
18433       /* Parse the value.  */
18434       value = cp_parser_constant_expression (parser);
18435     }
18436   else
18437     value = NULL_TREE;
18438
18439   /* If we are processing a template, make sure the initializer of the
18440      enumerator doesn't contain any bare template parameter pack.  */
18441   if (check_for_bare_parameter_packs (value))
18442     value = error_mark_node;
18443
18444   /* Create the enumerator.  */
18445   build_enumerator (identifier, value, type, attrs, loc);
18446 }
18447
18448 /* Parse a namespace-name.
18449
18450    namespace-name:
18451      original-namespace-name
18452      namespace-alias
18453
18454    Returns the NAMESPACE_DECL for the namespace.  */
18455
18456 static tree
18457 cp_parser_namespace_name (cp_parser* parser)
18458 {
18459   tree identifier;
18460   tree namespace_decl;
18461
18462   cp_token *token = cp_lexer_peek_token (parser->lexer);
18463
18464   /* Get the name of the namespace.  */
18465   identifier = cp_parser_identifier (parser);
18466   if (identifier == error_mark_node)
18467     return error_mark_node;
18468
18469   /* Look up the identifier in the currently active scope.  Look only
18470      for namespaces, due to:
18471
18472        [basic.lookup.udir]
18473
18474        When looking up a namespace-name in a using-directive or alias
18475        definition, only namespace names are considered.
18476
18477      And:
18478
18479        [basic.lookup.qual]
18480
18481        During the lookup of a name preceding the :: scope resolution
18482        operator, object, function, and enumerator names are ignored.
18483
18484      (Note that cp_parser_qualifying_entity only calls this
18485      function if the token after the name is the scope resolution
18486      operator.)  */
18487   namespace_decl = cp_parser_lookup_name (parser, identifier,
18488                                           none_type,
18489                                           /*is_template=*/false,
18490                                           /*is_namespace=*/true,
18491                                           /*check_dependency=*/true,
18492                                           /*ambiguous_decls=*/NULL,
18493                                           token->location);
18494   /* If it's not a namespace, issue an error.  */
18495   if (namespace_decl == error_mark_node
18496       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18497     {
18498       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18499         {
18500           error_at (token->location, "%qD is not a namespace-name", identifier);
18501           if (namespace_decl == error_mark_node
18502               && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18503             suggest_alternative_in_explicit_scope (token->location, identifier,
18504                                                    parser->scope);
18505         }
18506       cp_parser_error (parser, "expected namespace-name");
18507       namespace_decl = error_mark_node;
18508     }
18509
18510   return namespace_decl;
18511 }
18512
18513 /* Parse a namespace-definition.
18514
18515    namespace-definition:
18516      named-namespace-definition
18517      unnamed-namespace-definition
18518
18519    named-namespace-definition:
18520      original-namespace-definition
18521      extension-namespace-definition
18522
18523    original-namespace-definition:
18524      namespace identifier { namespace-body }
18525
18526    extension-namespace-definition:
18527      namespace original-namespace-name { namespace-body }
18528
18529    unnamed-namespace-definition:
18530      namespace { namespace-body } */
18531
18532 static void
18533 cp_parser_namespace_definition (cp_parser* parser)
18534 {
18535   tree identifier;
18536   int nested_definition_count = 0;
18537
18538   cp_ensure_no_omp_declare_simd (parser);
18539   cp_ensure_no_oacc_routine (parser);
18540
18541   bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18542
18543   if (is_inline)
18544     {
18545       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18546       cp_lexer_consume_token (parser->lexer);
18547     }
18548
18549   /* Look for the `namespace' keyword.  */
18550   cp_token* token
18551     = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18552
18553   /* Parse any specified attributes before the identifier.  */
18554   tree attribs = cp_parser_attributes_opt (parser);
18555
18556   for (;;)
18557     {
18558       identifier = NULL_TREE;
18559       
18560       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18561         {
18562           identifier = cp_parser_identifier (parser);
18563
18564           /* Parse any attributes specified after the identifier.  */
18565           attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18566         }
18567
18568       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18569         break;
18570   
18571       if (!nested_definition_count && cxx_dialect < cxx17)
18572         pedwarn (input_location, OPT_Wpedantic,
18573                  "nested namespace definitions only available with "
18574                  "-std=c++17 or -std=gnu++17");
18575
18576       /* Nested namespace names can create new namespaces (unlike
18577          other qualified-ids).  */
18578       if (int count = identifier ? push_namespace (identifier) : 0)
18579         nested_definition_count += count;
18580       else
18581         cp_parser_error (parser, "nested namespace name required");
18582       cp_lexer_consume_token (parser->lexer);
18583     }
18584
18585   if (nested_definition_count && !identifier)
18586     cp_parser_error (parser, "namespace name required");
18587   
18588   if (nested_definition_count && attribs)
18589     error_at (token->location,
18590               "a nested namespace definition cannot have attributes");
18591   if (nested_definition_count && is_inline)
18592     error_at (token->location,
18593               "a nested namespace definition cannot be inline");
18594
18595   /* Start the namespace.  */
18596   nested_definition_count += push_namespace (identifier, is_inline);
18597
18598   bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18599
18600   warning  (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18601
18602   /* Look for the `{' to validate starting the namespace.  */
18603   matching_braces braces;
18604   if (braces.require_open (parser))
18605     {
18606       /* Parse the body of the namespace.  */
18607       cp_parser_namespace_body (parser);
18608
18609       /* Look for the final `}'.  */
18610       braces.require_close (parser);
18611     }
18612
18613   if (has_visibility)
18614     pop_visibility (1);
18615
18616   /* Pop the nested namespace definitions.  */
18617   while (nested_definition_count--)
18618     pop_namespace ();
18619 }
18620
18621 /* Parse a namespace-body.
18622
18623    namespace-body:
18624      declaration-seq [opt]  */
18625
18626 static void
18627 cp_parser_namespace_body (cp_parser* parser)
18628 {
18629   cp_parser_declaration_seq_opt (parser);
18630 }
18631
18632 /* Parse a namespace-alias-definition.
18633
18634    namespace-alias-definition:
18635      namespace identifier = qualified-namespace-specifier ;  */
18636
18637 static void
18638 cp_parser_namespace_alias_definition (cp_parser* parser)
18639 {
18640   tree identifier;
18641   tree namespace_specifier;
18642
18643   cp_token *token = cp_lexer_peek_token (parser->lexer);
18644
18645   /* Look for the `namespace' keyword.  */
18646   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18647   /* Look for the identifier.  */
18648   identifier = cp_parser_identifier (parser);
18649   if (identifier == error_mark_node)
18650     return;
18651   /* Look for the `=' token.  */
18652   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18653       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
18654     {
18655       error_at (token->location, "%<namespace%> definition is not allowed here");
18656       /* Skip the definition.  */
18657       cp_lexer_consume_token (parser->lexer);
18658       if (cp_parser_skip_to_closing_brace (parser))
18659         cp_lexer_consume_token (parser->lexer);
18660       return;
18661     }
18662   cp_parser_require (parser, CPP_EQ, RT_EQ);
18663   /* Look for the qualified-namespace-specifier.  */
18664   namespace_specifier
18665     = cp_parser_qualified_namespace_specifier (parser);
18666   /* Look for the `;' token.  */
18667   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18668
18669   /* Register the alias in the symbol table.  */
18670   do_namespace_alias (identifier, namespace_specifier);
18671 }
18672
18673 /* Parse a qualified-namespace-specifier.
18674
18675    qualified-namespace-specifier:
18676      :: [opt] nested-name-specifier [opt] namespace-name
18677
18678    Returns a NAMESPACE_DECL corresponding to the specified
18679    namespace.  */
18680
18681 static tree
18682 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18683 {
18684   /* Look for the optional `::'.  */
18685   cp_parser_global_scope_opt (parser,
18686                               /*current_scope_valid_p=*/false);
18687
18688   /* Look for the optional nested-name-specifier.  */
18689   cp_parser_nested_name_specifier_opt (parser,
18690                                        /*typename_keyword_p=*/false,
18691                                        /*check_dependency_p=*/true,
18692                                        /*type_p=*/false,
18693                                        /*is_declaration=*/true);
18694
18695   return cp_parser_namespace_name (parser);
18696 }
18697
18698 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18699    access declaration.
18700
18701    using-declaration:
18702      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18703      using :: unqualified-id ;  
18704
18705    access-declaration:
18706      qualified-id ;  
18707
18708    */
18709
18710 static bool
18711 cp_parser_using_declaration (cp_parser* parser, 
18712                              bool access_declaration_p)
18713 {
18714   cp_token *token;
18715   bool typename_p = false;
18716   bool global_scope_p;
18717   tree decl;
18718   tree identifier;
18719   tree qscope;
18720   int oldcount = errorcount;
18721   cp_token *diag_token = NULL;
18722
18723   if (access_declaration_p)
18724     {
18725       diag_token = cp_lexer_peek_token (parser->lexer);
18726       cp_parser_parse_tentatively (parser);
18727     }
18728   else
18729     {
18730       /* Look for the `using' keyword.  */
18731       cp_parser_require_keyword (parser, RID_USING, RT_USING);
18732       
18733  again:
18734       /* Peek at the next token.  */
18735       token = cp_lexer_peek_token (parser->lexer);
18736       /* See if it's `typename'.  */
18737       if (token->keyword == RID_TYPENAME)
18738         {
18739           /* Remember that we've seen it.  */
18740           typename_p = true;
18741           /* Consume the `typename' token.  */
18742           cp_lexer_consume_token (parser->lexer);
18743         }
18744     }
18745
18746   /* Look for the optional global scope qualification.  */
18747   global_scope_p
18748     = (cp_parser_global_scope_opt (parser,
18749                                    /*current_scope_valid_p=*/false)
18750        != NULL_TREE);
18751
18752   /* If we saw `typename', or didn't see `::', then there must be a
18753      nested-name-specifier present.  */
18754   if (typename_p || !global_scope_p)
18755     {
18756       qscope = cp_parser_nested_name_specifier (parser, typename_p,
18757                                                 /*check_dependency_p=*/true,
18758                                                 /*type_p=*/false,
18759                                                 /*is_declaration=*/true);
18760       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18761         {
18762           cp_parser_skip_to_end_of_block_or_statement (parser);
18763           return false;
18764         }
18765     }
18766   /* Otherwise, we could be in either of the two productions.  In that
18767      case, treat the nested-name-specifier as optional.  */
18768   else
18769     qscope = cp_parser_nested_name_specifier_opt (parser,
18770                                                   /*typename_keyword_p=*/false,
18771                                                   /*check_dependency_p=*/true,
18772                                                   /*type_p=*/false,
18773                                                   /*is_declaration=*/true);
18774   if (!qscope)
18775     qscope = global_namespace;
18776   else if (UNSCOPED_ENUM_P (qscope))
18777     qscope = CP_TYPE_CONTEXT (qscope);
18778
18779   if (access_declaration_p && cp_parser_error_occurred (parser))
18780     /* Something has already gone wrong; there's no need to parse
18781        further.  Since an error has occurred, the return value of
18782        cp_parser_parse_definitely will be false, as required.  */
18783     return cp_parser_parse_definitely (parser);
18784
18785   token = cp_lexer_peek_token (parser->lexer);
18786   /* Parse the unqualified-id.  */
18787   identifier = cp_parser_unqualified_id (parser,
18788                                          /*template_keyword_p=*/false,
18789                                          /*check_dependency_p=*/true,
18790                                          /*declarator_p=*/true,
18791                                          /*optional_p=*/false);
18792
18793   if (access_declaration_p)
18794     {
18795       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18796         cp_parser_simulate_error (parser);
18797       if (!cp_parser_parse_definitely (parser))
18798         return false;
18799     }
18800   else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18801     {
18802       cp_token *ell = cp_lexer_consume_token (parser->lexer);
18803       if (cxx_dialect < cxx17
18804           && !in_system_header_at (ell->location))
18805         pedwarn (ell->location, 0,
18806                  "pack expansion in using-declaration only available "
18807                  "with -std=c++17 or -std=gnu++17");
18808       qscope = make_pack_expansion (qscope);
18809     }
18810
18811   /* The function we call to handle a using-declaration is different
18812      depending on what scope we are in.  */
18813   if (qscope == error_mark_node || identifier == error_mark_node)
18814     ;
18815   else if (!identifier_p (identifier)
18816            && TREE_CODE (identifier) != BIT_NOT_EXPR)
18817     /* [namespace.udecl]
18818
18819        A using declaration shall not name a template-id.  */
18820     error_at (token->location,
18821               "a template-id may not appear in a using-declaration");
18822   else
18823     {
18824       if (at_class_scope_p ())
18825         {
18826           /* Create the USING_DECL.  */
18827           decl = do_class_using_decl (qscope, identifier);
18828
18829           if (decl && typename_p)
18830             USING_DECL_TYPENAME_P (decl) = 1;
18831
18832           if (check_for_bare_parameter_packs (decl))
18833             {
18834               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18835               return false;
18836             }
18837           else
18838             /* Add it to the list of members in this class.  */
18839             finish_member_declaration (decl);
18840         }
18841       else
18842         {
18843           decl = cp_parser_lookup_name_simple (parser,
18844                                                identifier,
18845                                                token->location);
18846           if (decl == error_mark_node)
18847             cp_parser_name_lookup_error (parser, identifier,
18848                                          decl, NLE_NULL,
18849                                          token->location);
18850           else if (check_for_bare_parameter_packs (decl))
18851             {
18852               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18853               return false;
18854             }
18855           else if (!at_namespace_scope_p ())
18856             finish_local_using_decl (decl, qscope, identifier);
18857           else
18858             finish_namespace_using_decl (decl, qscope, identifier);
18859         }
18860     }
18861
18862   if (!access_declaration_p
18863       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18864     {
18865       cp_token *comma = cp_lexer_consume_token (parser->lexer);
18866       if (cxx_dialect < cxx17)
18867         pedwarn (comma->location, 0,
18868                  "comma-separated list in using-declaration only available "
18869                  "with -std=c++17 or -std=gnu++17");
18870       goto again;
18871     }
18872
18873   /* Look for the final `;'.  */
18874   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18875
18876   if (access_declaration_p && errorcount == oldcount)
18877     warning_at (diag_token->location, OPT_Wdeprecated,
18878                 "access declarations are deprecated "
18879                 "in favour of using-declarations; "
18880                 "suggestion: add the %<using%> keyword");
18881
18882   return true;
18883 }
18884
18885 /* Parse an alias-declaration.
18886
18887    alias-declaration:
18888      using identifier attribute-specifier-seq [opt] = type-id  */
18889
18890 static tree
18891 cp_parser_alias_declaration (cp_parser* parser)
18892 {
18893   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18894   location_t id_location;
18895   cp_declarator *declarator;
18896   cp_decl_specifier_seq decl_specs;
18897   bool member_p;
18898   const char *saved_message = NULL;
18899
18900   /* Look for the `using' keyword.  */
18901   cp_token *using_token
18902     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18903   if (using_token == NULL)
18904     return error_mark_node;
18905
18906   id_location = cp_lexer_peek_token (parser->lexer)->location;
18907   id = cp_parser_identifier (parser);
18908   if (id == error_mark_node)
18909     return error_mark_node;
18910
18911   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18912   attributes = cp_parser_attributes_opt (parser);
18913   if (attributes == error_mark_node)
18914     return error_mark_node;
18915
18916   cp_parser_require (parser, CPP_EQ, RT_EQ);
18917
18918   if (cp_parser_error_occurred (parser))
18919     return error_mark_node;
18920
18921   cp_parser_commit_to_tentative_parse (parser);
18922
18923   /* Now we are going to parse the type-id of the declaration.  */
18924
18925   /*
18926     [dcl.type]/3 says:
18927
18928         "A type-specifier-seq shall not define a class or enumeration
18929          unless it appears in the type-id of an alias-declaration (7.1.3) that
18930          is not the declaration of a template-declaration."
18931
18932     In other words, if we currently are in an alias template, the
18933     type-id should not define a type.
18934
18935     So let's set parser->type_definition_forbidden_message in that
18936     case; cp_parser_check_type_definition (called by
18937     cp_parser_class_specifier) will then emit an error if a type is
18938     defined in the type-id.  */
18939   if (parser->num_template_parameter_lists)
18940     {
18941       saved_message = parser->type_definition_forbidden_message;
18942       parser->type_definition_forbidden_message =
18943         G_("types may not be defined in alias template declarations");
18944     }
18945
18946   type = cp_parser_type_id (parser);
18947
18948   /* Restore the error message if need be.  */
18949   if (parser->num_template_parameter_lists)
18950     parser->type_definition_forbidden_message = saved_message;
18951
18952   if (type == error_mark_node
18953       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18954     {
18955       cp_parser_skip_to_end_of_block_or_statement (parser);
18956       return error_mark_node;
18957     }
18958
18959   /* A typedef-name can also be introduced by an alias-declaration. The
18960      identifier following the using keyword becomes a typedef-name. It has
18961      the same semantics as if it were introduced by the typedef
18962      specifier. In particular, it does not define a new type and it shall
18963      not appear in the type-id.  */
18964
18965   clear_decl_specs (&decl_specs);
18966   decl_specs.type = type;
18967   if (attributes != NULL_TREE)
18968     {
18969       decl_specs.attributes = attributes;
18970       set_and_check_decl_spec_loc (&decl_specs,
18971                                    ds_attribute,
18972                                    attrs_token);
18973     }
18974   set_and_check_decl_spec_loc (&decl_specs,
18975                                ds_typedef,
18976                                using_token);
18977   set_and_check_decl_spec_loc (&decl_specs,
18978                                ds_alias,
18979                                using_token);
18980
18981   if (parser->num_template_parameter_lists
18982       && !cp_parser_check_template_parameters (parser,
18983                                                /*num_templates=*/0,
18984                                                /*template_id*/false,
18985                                                id_location,
18986                                                /*declarator=*/NULL))
18987     return error_mark_node;
18988
18989   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18990   declarator->id_loc = id_location;
18991
18992   member_p = at_class_scope_p ();
18993   if (member_p)
18994     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18995                       NULL_TREE, attributes);
18996   else
18997     decl = start_decl (declarator, &decl_specs, 0,
18998                        attributes, NULL_TREE, &pushed_scope);
18999   if (decl == error_mark_node)
19000     return decl;
19001
19002   // Attach constraints to the alias declaration.
19003   if (flag_concepts && current_template_parms)
19004     {
19005       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19006       tree constr = build_constraints (reqs, NULL_TREE);
19007       set_constraints (decl, constr);
19008     }
19009
19010   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19011
19012   if (pushed_scope)
19013     pop_scope (pushed_scope);
19014
19015   /* If decl is a template, return its TEMPLATE_DECL so that it gets
19016      added into the symbol table; otherwise, return the TYPE_DECL.  */
19017   if (DECL_LANG_SPECIFIC (decl)
19018       && DECL_TEMPLATE_INFO (decl)
19019       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19020     {
19021       decl = DECL_TI_TEMPLATE (decl);
19022       if (member_p)
19023         check_member_template (decl);
19024     }
19025
19026   return decl;
19027 }
19028
19029 /* Parse a using-directive.
19030
19031    using-directive:
19032      using namespace :: [opt] nested-name-specifier [opt]
19033        namespace-name ;  */
19034
19035 static void
19036 cp_parser_using_directive (cp_parser* parser)
19037 {
19038   tree namespace_decl;
19039   tree attribs;
19040
19041   /* Look for the `using' keyword.  */
19042   cp_parser_require_keyword (parser, RID_USING, RT_USING);
19043   /* And the `namespace' keyword.  */
19044   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19045   /* Look for the optional `::' operator.  */
19046   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19047   /* And the optional nested-name-specifier.  */
19048   cp_parser_nested_name_specifier_opt (parser,
19049                                        /*typename_keyword_p=*/false,
19050                                        /*check_dependency_p=*/true,
19051                                        /*type_p=*/false,
19052                                        /*is_declaration=*/true);
19053   /* Get the namespace being used.  */
19054   namespace_decl = cp_parser_namespace_name (parser);
19055   /* And any specified attributes.  */
19056   attribs = cp_parser_attributes_opt (parser);
19057
19058   /* Update the symbol table.  */
19059   if (namespace_bindings_p ())
19060     finish_namespace_using_directive (namespace_decl, attribs);
19061   else
19062     finish_local_using_directive (namespace_decl, attribs);
19063
19064   /* Look for the final `;'.  */
19065   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19066 }
19067
19068 /* Parse an asm-definition.
19069
19070   asm-qualifier:
19071     volatile
19072     inline
19073     goto
19074
19075   asm-qualifier-list:
19076     asm-qualifier
19077     asm-qualifier-list asm-qualifier
19078
19079    asm-definition:
19080      asm ( string-literal ) ;
19081
19082    GNU Extension:
19083
19084    asm-definition:
19085      asm asm-qualifier-list [opt] ( string-literal ) ;
19086      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19087      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19088                                     : asm-operand-list [opt] ) ;
19089      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19090                                     : asm-operand-list [opt]
19091                           : asm-clobber-list [opt] ) ;
19092      asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19093                                     : asm-clobber-list [opt]
19094                                     : asm-goto-list ) ;
19095
19096   The form with asm-goto-list is valid if and only if the asm-qualifier-list
19097   contains goto, and is the only allowed form in that case.  No duplicates are
19098   allowed in an asm-qualifier-list.  */
19099
19100 static void
19101 cp_parser_asm_definition (cp_parser* parser)
19102 {
19103   tree string;
19104   tree outputs = NULL_TREE;
19105   tree inputs = NULL_TREE;
19106   tree clobbers = NULL_TREE;
19107   tree labels = NULL_TREE;
19108   tree asm_stmt;
19109   bool extended_p = false;
19110   bool invalid_inputs_p = false;
19111   bool invalid_outputs_p = false;
19112   required_token missing = RT_NONE;
19113
19114   /* Look for the `asm' keyword.  */
19115   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19116
19117   if (parser->in_function_body
19118       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19119     {
19120       error ("%<asm%> in %<constexpr%> function");
19121       cp_function_chain->invalid_constexpr = true;
19122     }
19123
19124   /* Handle the asm-qualifier-list.  */
19125   location_t volatile_loc = UNKNOWN_LOCATION;
19126   location_t inline_loc = UNKNOWN_LOCATION;
19127   location_t goto_loc = UNKNOWN_LOCATION;
19128   location_t first_loc = UNKNOWN_LOCATION;
19129
19130   if (cp_parser_allow_gnu_extensions_p (parser))
19131     for (;;)
19132       {
19133         cp_token *token = cp_lexer_peek_token (parser->lexer);
19134         location_t loc = token->location;
19135         switch (cp_lexer_peek_token (parser->lexer)->keyword)
19136           {
19137           case RID_VOLATILE:
19138             if (volatile_loc)
19139               {
19140                 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19141                 inform (volatile_loc, "first seen here");
19142               }
19143             else
19144               volatile_loc = loc;
19145             cp_lexer_consume_token (parser->lexer);
19146             continue;
19147
19148           case RID_INLINE:
19149             if (inline_loc)
19150               {
19151                 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19152                 inform (inline_loc, "first seen here");
19153               }
19154             else
19155               inline_loc = loc;
19156             if (!first_loc)
19157               first_loc = loc;
19158             cp_lexer_consume_token (parser->lexer);
19159             continue;
19160
19161           case RID_GOTO:
19162             if (goto_loc)
19163               {
19164                 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19165                 inform (goto_loc, "first seen here");
19166               }
19167             else
19168               goto_loc = loc;
19169             if (!first_loc)
19170               first_loc = loc;
19171             cp_lexer_consume_token (parser->lexer);
19172             continue;
19173
19174           case RID_CONST:
19175           case RID_RESTRICT:
19176             error_at (loc, "%qT is not an asm qualifier", token->u.value);
19177             cp_lexer_consume_token (parser->lexer);
19178             continue;
19179
19180           default:
19181             break;
19182           }
19183         break;
19184       }
19185
19186   bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19187   bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19188   bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19189
19190   if (!parser->in_function_body && (inline_p || goto_p))
19191     {
19192       error_at (first_loc, "asm qualifier outside of function body");
19193       inline_p = goto_p = false;
19194     }
19195
19196   /* Look for the opening `('.  */
19197   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19198     return;
19199   /* Look for the string.  */
19200   string = cp_parser_string_literal (parser, false, false);
19201   if (string == error_mark_node)
19202     {
19203       cp_parser_skip_to_closing_parenthesis (parser, true, false,
19204                                              /*consume_paren=*/true);
19205       return;
19206     }
19207
19208   /* If we're allowing GNU extensions, check for the extended assembly
19209      syntax.  Unfortunately, the `:' tokens need not be separated by
19210      a space in C, and so, for compatibility, we tolerate that here
19211      too.  Doing that means that we have to treat the `::' operator as
19212      two `:' tokens.  */
19213   if (cp_parser_allow_gnu_extensions_p (parser)
19214       && parser->in_function_body
19215       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19216           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19217     {
19218       bool inputs_p = false;
19219       bool clobbers_p = false;
19220       bool labels_p = false;
19221
19222       /* The extended syntax was used.  */
19223       extended_p = true;
19224
19225       /* Look for outputs.  */
19226       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19227         {
19228           /* Consume the `:'.  */
19229           cp_lexer_consume_token (parser->lexer);
19230           /* Parse the output-operands.  */
19231           if (cp_lexer_next_token_is_not (parser->lexer,
19232                                           CPP_COLON)
19233               && cp_lexer_next_token_is_not (parser->lexer,
19234                                              CPP_SCOPE)
19235               && cp_lexer_next_token_is_not (parser->lexer,
19236                                              CPP_CLOSE_PAREN)
19237               && !goto_p)
19238             {
19239               outputs = cp_parser_asm_operand_list (parser);
19240               if (outputs == error_mark_node)
19241                 invalid_outputs_p = true;
19242             }
19243         }
19244       /* If the next token is `::', there are no outputs, and the
19245          next token is the beginning of the inputs.  */
19246       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19247         /* The inputs are coming next.  */
19248         inputs_p = true;
19249
19250       /* Look for inputs.  */
19251       if (inputs_p
19252           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19253         {
19254           /* Consume the `:' or `::'.  */
19255           cp_lexer_consume_token (parser->lexer);
19256           /* Parse the output-operands.  */
19257           if (cp_lexer_next_token_is_not (parser->lexer,
19258                                           CPP_COLON)
19259               && cp_lexer_next_token_is_not (parser->lexer,
19260                                              CPP_SCOPE)
19261               && cp_lexer_next_token_is_not (parser->lexer,
19262                                              CPP_CLOSE_PAREN))
19263             {
19264               inputs = cp_parser_asm_operand_list (parser);
19265               if (inputs == error_mark_node)
19266                 invalid_inputs_p = true;
19267             }
19268         }
19269       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19270         /* The clobbers are coming next.  */
19271         clobbers_p = true;
19272
19273       /* Look for clobbers.  */
19274       if (clobbers_p
19275           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19276         {
19277           clobbers_p = true;
19278           /* Consume the `:' or `::'.  */
19279           cp_lexer_consume_token (parser->lexer);
19280           /* Parse the clobbers.  */
19281           if (cp_lexer_next_token_is_not (parser->lexer,
19282                                           CPP_COLON)
19283               && cp_lexer_next_token_is_not (parser->lexer,
19284                                              CPP_CLOSE_PAREN))
19285             clobbers = cp_parser_asm_clobber_list (parser);
19286         }
19287       else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19288         /* The labels are coming next.  */
19289         labels_p = true;
19290
19291       /* Look for labels.  */
19292       if (labels_p
19293           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19294         {
19295           labels_p = true;
19296           /* Consume the `:' or `::'.  */
19297           cp_lexer_consume_token (parser->lexer);
19298           /* Parse the labels.  */
19299           labels = cp_parser_asm_label_list (parser);
19300         }
19301
19302       if (goto_p && !labels_p)
19303         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19304     }
19305   else if (goto_p)
19306     missing = RT_COLON_SCOPE;
19307
19308   /* Look for the closing `)'.  */
19309   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19310                           missing ? missing : RT_CLOSE_PAREN))
19311     cp_parser_skip_to_closing_parenthesis (parser, true, false,
19312                                            /*consume_paren=*/true);
19313   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19314
19315   if (!invalid_inputs_p && !invalid_outputs_p)
19316     {
19317       /* Create the ASM_EXPR.  */
19318       if (parser->in_function_body)
19319         {
19320           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19321                                       inputs, clobbers, labels, inline_p);
19322           /* If the extended syntax was not used, mark the ASM_EXPR.  */
19323           if (!extended_p)
19324             {
19325               tree temp = asm_stmt;
19326               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19327                 temp = TREE_OPERAND (temp, 0);
19328
19329               ASM_INPUT_P (temp) = 1;
19330             }
19331         }
19332       else
19333         symtab->finalize_toplevel_asm (string);
19334     }
19335 }
19336
19337 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19338    type that comes from the decl-specifier-seq.  */
19339
19340 static tree
19341 strip_declarator_types (tree type, cp_declarator *declarator)
19342 {
19343   for (cp_declarator *d = declarator; d;)
19344     switch (d->kind)
19345       {
19346       case cdk_id:
19347       case cdk_decomp:
19348       case cdk_error:
19349         d = NULL;
19350         break;
19351
19352       default:
19353         if (TYPE_PTRMEMFUNC_P (type))
19354           type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19355         type = TREE_TYPE (type);
19356         d = d->declarator;
19357         break;
19358       }
19359
19360   return type;
19361 }
19362
19363 /* Declarators [gram.dcl.decl] */
19364
19365 /* Parse an init-declarator.
19366
19367    init-declarator:
19368      declarator initializer [opt]
19369
19370    GNU Extension:
19371
19372    init-declarator:
19373      declarator asm-specification [opt] attributes [opt] initializer [opt]
19374
19375    function-definition:
19376      decl-specifier-seq [opt] declarator ctor-initializer [opt]
19377        function-body
19378      decl-specifier-seq [opt] declarator function-try-block
19379
19380    GNU Extension:
19381
19382    function-definition:
19383      __extension__ function-definition
19384
19385    TM Extension:
19386
19387    function-definition:
19388      decl-specifier-seq [opt] declarator function-transaction-block
19389
19390    The DECL_SPECIFIERS apply to this declarator.  Returns a
19391    representation of the entity declared.  If MEMBER_P is TRUE, then
19392    this declarator appears in a class scope.  The new DECL created by
19393    this declarator is returned.
19394
19395    The CHECKS are access checks that should be performed once we know
19396    what entity is being declared (and, therefore, what classes have
19397    befriended it).
19398
19399    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19400    for a function-definition here as well.  If the declarator is a
19401    declarator for a function-definition, *FUNCTION_DEFINITION_P will
19402    be TRUE upon return.  By that point, the function-definition will
19403    have been completely parsed.
19404
19405    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19406    is FALSE.
19407
19408    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19409    parsed declaration if it is an uninitialized single declarator not followed
19410    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19411    if present, will not be consumed.  If returned, this declarator will be
19412    created with SD_INITIALIZED but will not call cp_finish_decl.
19413
19414    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19415    and there is an initializer, the pointed location_t is set to the
19416    location of the '=' or `(', or '{' in C++11 token introducing the
19417    initializer.  */
19418
19419 static tree
19420 cp_parser_init_declarator (cp_parser* parser,
19421                            cp_decl_specifier_seq *decl_specifiers,
19422                            vec<deferred_access_check, va_gc> *checks,
19423                            bool function_definition_allowed_p,
19424                            bool member_p,
19425                            int declares_class_or_enum,
19426                            bool* function_definition_p,
19427                            tree* maybe_range_for_decl,
19428                            location_t* init_loc,
19429                            tree* auto_result)
19430 {
19431   cp_token *token = NULL, *asm_spec_start_token = NULL,
19432            *attributes_start_token = NULL;
19433   cp_declarator *declarator;
19434   tree prefix_attributes;
19435   tree attributes = NULL;
19436   tree asm_specification;
19437   tree initializer;
19438   tree decl = NULL_TREE;
19439   tree scope;
19440   int is_initialized;
19441   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
19442      initialized with "= ..", CPP_OPEN_PAREN if initialized with
19443      "(...)".  */
19444   enum cpp_ttype initialization_kind;
19445   bool is_direct_init = false;
19446   bool is_non_constant_init;
19447   int ctor_dtor_or_conv_p;
19448   bool friend_p = cp_parser_friend_p (decl_specifiers);
19449   tree pushed_scope = NULL_TREE;
19450   bool range_for_decl_p = false;
19451   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19452   location_t tmp_init_loc = UNKNOWN_LOCATION;
19453
19454   /* Gather the attributes that were provided with the
19455      decl-specifiers.  */
19456   prefix_attributes = decl_specifiers->attributes;
19457
19458   /* Assume that this is not the declarator for a function
19459      definition.  */
19460   if (function_definition_p)
19461     *function_definition_p = false;
19462
19463   /* Default arguments are only permitted for function parameters.  */
19464   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19465     parser->default_arg_ok_p = false;
19466
19467   /* Defer access checks while parsing the declarator; we cannot know
19468      what names are accessible until we know what is being
19469      declared.  */
19470   resume_deferring_access_checks ();
19471
19472   token = cp_lexer_peek_token (parser->lexer);
19473
19474   /* Parse the declarator.  */
19475   declarator
19476     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19477                             &ctor_dtor_or_conv_p,
19478                             /*parenthesized_p=*/NULL,
19479                             member_p, friend_p);
19480   /* Gather up the deferred checks.  */
19481   stop_deferring_access_checks ();
19482
19483   parser->default_arg_ok_p = saved_default_arg_ok_p;
19484
19485   /* If the DECLARATOR was erroneous, there's no need to go
19486      further.  */
19487   if (declarator == cp_error_declarator)
19488     return error_mark_node;
19489
19490   /* Check that the number of template-parameter-lists is OK.  */
19491   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19492                                                        token->location))
19493     return error_mark_node;
19494
19495   if (declares_class_or_enum & 2)
19496     cp_parser_check_for_definition_in_return_type (declarator,
19497                                                    decl_specifiers->type,
19498                                                    decl_specifiers->locations[ds_type_spec]);
19499
19500   /* Figure out what scope the entity declared by the DECLARATOR is
19501      located in.  `grokdeclarator' sometimes changes the scope, so
19502      we compute it now.  */
19503   scope = get_scope_of_declarator (declarator);
19504
19505   /* Perform any lookups in the declared type which were thought to be
19506      dependent, but are not in the scope of the declarator.  */
19507   decl_specifiers->type
19508     = maybe_update_decl_type (decl_specifiers->type, scope);
19509
19510   /* If we're allowing GNU extensions, look for an
19511      asm-specification.  */
19512   if (cp_parser_allow_gnu_extensions_p (parser))
19513     {
19514       /* Look for an asm-specification.  */
19515       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19516       asm_specification = cp_parser_asm_specification_opt (parser);
19517     }
19518   else
19519     asm_specification = NULL_TREE;
19520
19521   /* Look for attributes.  */
19522   attributes_start_token = cp_lexer_peek_token (parser->lexer);
19523   attributes = cp_parser_attributes_opt (parser);
19524
19525   /* Peek at the next token.  */
19526   token = cp_lexer_peek_token (parser->lexer);
19527
19528   bool bogus_implicit_tmpl = false;
19529
19530   if (function_declarator_p (declarator))
19531     {
19532       /* Handle C++17 deduction guides.  */
19533       if (!decl_specifiers->type
19534           && ctor_dtor_or_conv_p <= 0
19535           && cxx_dialect >= cxx17)
19536         {
19537           cp_declarator *id = get_id_declarator (declarator);
19538           tree name = id->u.id.unqualified_name;
19539           parser->scope = id->u.id.qualifying_scope;
19540           tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19541           if (tmpl
19542               && (DECL_CLASS_TEMPLATE_P (tmpl)
19543                   || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19544             {
19545               id->u.id.unqualified_name = dguide_name (tmpl);
19546               id->u.id.sfk = sfk_deduction_guide;
19547               ctor_dtor_or_conv_p = 1;
19548             }
19549         }
19550
19551       /* Check to see if the token indicates the start of a
19552          function-definition.  */
19553       if (cp_parser_token_starts_function_definition_p (token))
19554         {
19555           if (!function_definition_allowed_p)
19556             {
19557               /* If a function-definition should not appear here, issue an
19558                  error message.  */
19559               cp_parser_error (parser,
19560                                "a function-definition is not allowed here");
19561               return error_mark_node;
19562             }
19563
19564           location_t func_brace_location
19565             = cp_lexer_peek_token (parser->lexer)->location;
19566
19567           /* Neither attributes nor an asm-specification are allowed
19568              on a function-definition.  */
19569           if (asm_specification)
19570             error_at (asm_spec_start_token->location,
19571                       "an asm-specification is not allowed "
19572                       "on a function-definition");
19573           if (attributes)
19574             error_at (attributes_start_token->location,
19575                       "attributes are not allowed "
19576                       "on a function-definition");
19577           /* This is a function-definition.  */
19578           *function_definition_p = true;
19579
19580           /* Parse the function definition.  */
19581           if (member_p)
19582             decl = cp_parser_save_member_function_body (parser,
19583                                                         decl_specifiers,
19584                                                         declarator,
19585                                                         prefix_attributes);
19586           else
19587             decl =
19588               (cp_parser_function_definition_from_specifiers_and_declarator
19589                (parser, decl_specifiers, prefix_attributes, declarator));
19590
19591           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19592             {
19593               /* This is where the prologue starts...  */
19594               DECL_STRUCT_FUNCTION (decl)->function_start_locus
19595                 = func_brace_location;
19596             }
19597
19598           return decl;
19599         }
19600     }
19601   else if (parser->fully_implicit_function_template_p)
19602     {
19603       /* A non-template declaration involving a function parameter list
19604          containing an implicit template parameter will be made into a
19605          template.  If the resulting declaration is not going to be an
19606          actual function then finish the template scope here to prevent it.
19607          An error message will be issued once we have a decl to talk about.
19608
19609          FIXME probably we should do type deduction rather than create an
19610          implicit template, but the standard currently doesn't allow it. */
19611       bogus_implicit_tmpl = true;
19612       finish_fully_implicit_template (parser, NULL_TREE);
19613     }
19614
19615   /* [dcl.dcl]
19616
19617      Only in function declarations for constructors, destructors, type
19618      conversions, and deduction guides can the decl-specifier-seq be omitted.
19619
19620      We explicitly postpone this check past the point where we handle
19621      function-definitions because we tolerate function-definitions
19622      that are missing their return types in some modes.  */
19623   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19624     {
19625       cp_parser_error (parser,
19626                        "expected constructor, destructor, or type conversion");
19627       return error_mark_node;
19628     }
19629
19630   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
19631   if (token->type == CPP_EQ
19632       || token->type == CPP_OPEN_PAREN
19633       || token->type == CPP_OPEN_BRACE)
19634     {
19635       is_initialized = SD_INITIALIZED;
19636       initialization_kind = token->type;
19637       if (maybe_range_for_decl)
19638         *maybe_range_for_decl = error_mark_node;
19639       tmp_init_loc = token->location;
19640       if (init_loc && *init_loc == UNKNOWN_LOCATION)
19641         *init_loc = tmp_init_loc;
19642
19643       if (token->type == CPP_EQ
19644           && function_declarator_p (declarator))
19645         {
19646           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19647           if (t2->keyword == RID_DEFAULT)
19648             is_initialized = SD_DEFAULTED;
19649           else if (t2->keyword == RID_DELETE)
19650             is_initialized = SD_DELETED;
19651         }
19652     }
19653   else
19654     {
19655       /* If the init-declarator isn't initialized and isn't followed by a
19656          `,' or `;', it's not a valid init-declarator.  */
19657       if (token->type != CPP_COMMA
19658           && token->type != CPP_SEMICOLON)
19659         {
19660           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19661             range_for_decl_p = true;
19662           else
19663             {
19664               if (!maybe_range_for_decl)
19665                 cp_parser_error (parser, "expected initializer");
19666               return error_mark_node;
19667             }
19668         }
19669       is_initialized = SD_UNINITIALIZED;
19670       initialization_kind = CPP_EOF;
19671     }
19672
19673   /* Because start_decl has side-effects, we should only call it if we
19674      know we're going ahead.  By this point, we know that we cannot
19675      possibly be looking at any other construct.  */
19676   cp_parser_commit_to_tentative_parse (parser);
19677
19678   /* Enter the newly declared entry in the symbol table.  If we're
19679      processing a declaration in a class-specifier, we wait until
19680      after processing the initializer.  */
19681   if (!member_p)
19682     {
19683       if (parser->in_unbraced_linkage_specification_p)
19684         decl_specifiers->storage_class = sc_extern;
19685       decl = start_decl (declarator, decl_specifiers,
19686                          range_for_decl_p? SD_INITIALIZED : is_initialized,
19687                          attributes, prefix_attributes, &pushed_scope);
19688       cp_finalize_omp_declare_simd (parser, decl);
19689       cp_finalize_oacc_routine (parser, decl, false);
19690       /* Adjust location of decl if declarator->id_loc is more appropriate:
19691          set, and decl wasn't merged with another decl, in which case its
19692          location would be different from input_location, and more accurate.  */
19693       if (DECL_P (decl)
19694           && declarator->id_loc != UNKNOWN_LOCATION
19695           && DECL_SOURCE_LOCATION (decl) == input_location)
19696         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19697     }
19698   else if (scope)
19699     /* Enter the SCOPE.  That way unqualified names appearing in the
19700        initializer will be looked up in SCOPE.  */
19701     pushed_scope = push_scope (scope);
19702
19703   /* Perform deferred access control checks, now that we know in which
19704      SCOPE the declared entity resides.  */
19705   if (!member_p && decl)
19706     {
19707       tree saved_current_function_decl = NULL_TREE;
19708
19709       /* If the entity being declared is a function, pretend that we
19710          are in its scope.  If it is a `friend', it may have access to
19711          things that would not otherwise be accessible.  */
19712       if (TREE_CODE (decl) == FUNCTION_DECL)
19713         {
19714           saved_current_function_decl = current_function_decl;
19715           current_function_decl = decl;
19716         }
19717
19718       /* Perform access checks for template parameters.  */
19719       cp_parser_perform_template_parameter_access_checks (checks);
19720
19721       /* Perform the access control checks for the declarator and the
19722          decl-specifiers.  */
19723       perform_deferred_access_checks (tf_warning_or_error);
19724
19725       /* Restore the saved value.  */
19726       if (TREE_CODE (decl) == FUNCTION_DECL)
19727         current_function_decl = saved_current_function_decl;
19728     }
19729
19730   /* Parse the initializer.  */
19731   initializer = NULL_TREE;
19732   is_direct_init = false;
19733   is_non_constant_init = true;
19734   if (is_initialized)
19735     {
19736       if (function_declarator_p (declarator))
19737         {
19738            if (initialization_kind == CPP_EQ)
19739              initializer = cp_parser_pure_specifier (parser);
19740            else
19741              {
19742                /* If the declaration was erroneous, we don't really
19743                   know what the user intended, so just silently
19744                   consume the initializer.  */
19745                if (decl != error_mark_node)
19746                  error_at (tmp_init_loc, "initializer provided for function");
19747                cp_parser_skip_to_closing_parenthesis (parser,
19748                                                       /*recovering=*/true,
19749                                                       /*or_comma=*/false,
19750                                                       /*consume_paren=*/true);
19751              }
19752         }
19753       else
19754         {
19755           /* We want to record the extra mangling scope for in-class
19756              initializers of class members and initializers of static data
19757              member templates.  The former involves deferring
19758              parsing of the initializer until end of class as with default
19759              arguments.  So right here we only handle the latter.  */
19760           if (!member_p && processing_template_decl && decl != error_mark_node)
19761             start_lambda_scope (decl);
19762           initializer = cp_parser_initializer (parser,
19763                                                &is_direct_init,
19764                                                &is_non_constant_init);
19765           if (!member_p && processing_template_decl && decl != error_mark_node)
19766             finish_lambda_scope ();
19767           if (initializer == error_mark_node)
19768             cp_parser_skip_to_end_of_statement (parser);
19769         }
19770     }
19771
19772   /* The old parser allows attributes to appear after a parenthesized
19773      initializer.  Mark Mitchell proposed removing this functionality
19774      on the GCC mailing lists on 2002-08-13.  This parser accepts the
19775      attributes -- but ignores them.  Made a permerror in GCC 8.  */
19776   if (cp_parser_allow_gnu_extensions_p (parser)
19777       && initialization_kind == CPP_OPEN_PAREN
19778       && cp_parser_attributes_opt (parser)
19779       && permerror (input_location,
19780                     "attributes after parenthesized initializer ignored"))
19781     {
19782       static bool hint;
19783       if (flag_permissive && !hint)
19784         {
19785           hint = true;
19786           inform (input_location,
19787                   "this flexibility is deprecated and will be removed");
19788         }
19789     }
19790
19791   /* And now complain about a non-function implicit template.  */
19792   if (bogus_implicit_tmpl && decl != error_mark_node)
19793     error_at (DECL_SOURCE_LOCATION (decl),
19794               "non-function %qD declared as implicit template", decl);
19795
19796   /* For an in-class declaration, use `grokfield' to create the
19797      declaration.  */
19798   if (member_p)
19799     {
19800       if (pushed_scope)
19801         {
19802           pop_scope (pushed_scope);
19803           pushed_scope = NULL_TREE;
19804         }
19805       decl = grokfield (declarator, decl_specifiers,
19806                         initializer, !is_non_constant_init,
19807                         /*asmspec=*/NULL_TREE,
19808                         attr_chainon (attributes, prefix_attributes));
19809       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19810         cp_parser_save_default_args (parser, decl);
19811       cp_finalize_omp_declare_simd (parser, decl);
19812       cp_finalize_oacc_routine (parser, decl, false);
19813     }
19814
19815   /* Finish processing the declaration.  But, skip member
19816      declarations.  */
19817   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19818     {
19819       cp_finish_decl (decl,
19820                       initializer, !is_non_constant_init,
19821                       asm_specification,
19822                       /* If the initializer is in parentheses, then this is
19823                          a direct-initialization, which means that an
19824                          `explicit' constructor is OK.  Otherwise, an
19825                          `explicit' constructor cannot be used.  */
19826                       ((is_direct_init || !is_initialized)
19827                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19828     }
19829   else if ((cxx_dialect != cxx98) && friend_p
19830            && decl && TREE_CODE (decl) == FUNCTION_DECL)
19831     /* Core issue #226 (C++0x only): A default template-argument
19832        shall not be specified in a friend class template
19833        declaration. */
19834     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
19835                              /*is_partial=*/false, /*is_friend_decl=*/1);
19836
19837   if (!friend_p && pushed_scope)
19838     pop_scope (pushed_scope);
19839
19840   if (function_declarator_p (declarator)
19841       && parser->fully_implicit_function_template_p)
19842     {
19843       if (member_p)
19844         decl = finish_fully_implicit_template (parser, decl);
19845       else
19846         finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19847     }
19848
19849   if (auto_result && is_initialized && decl_specifiers->type
19850       && type_uses_auto (decl_specifiers->type))
19851     *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19852
19853   return decl;
19854 }
19855
19856 /* Parse a declarator.
19857
19858    declarator:
19859      direct-declarator
19860      ptr-operator declarator
19861
19862    abstract-declarator:
19863      ptr-operator abstract-declarator [opt]
19864      direct-abstract-declarator
19865
19866    GNU Extensions:
19867
19868    declarator:
19869      attributes [opt] direct-declarator
19870      attributes [opt] ptr-operator declarator
19871
19872    abstract-declarator:
19873      attributes [opt] ptr-operator abstract-declarator [opt]
19874      attributes [opt] direct-abstract-declarator
19875
19876    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19877    detect constructors, destructors, deduction guides, or conversion operators.
19878    It is set to -1 if the declarator is a name, and +1 if it is a
19879    function. Otherwise it is set to zero. Usually you just want to
19880    test for >0, but internally the negative value is used.
19881
19882    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19883    a decl-specifier-seq unless it declares a constructor, destructor,
19884    or conversion.  It might seem that we could check this condition in
19885    semantic analysis, rather than parsing, but that makes it difficult
19886    to handle something like `f()'.  We want to notice that there are
19887    no decl-specifiers, and therefore realize that this is an
19888    expression, not a declaration.)
19889
19890    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19891    the declarator is a direct-declarator of the form "(...)".
19892
19893    MEMBER_P is true iff this declarator is a member-declarator.
19894
19895    FRIEND_P is true iff this declarator is a friend.  */
19896
19897 static cp_declarator *
19898 cp_parser_declarator (cp_parser* parser,
19899                       cp_parser_declarator_kind dcl_kind,
19900                       int* ctor_dtor_or_conv_p,
19901                       bool* parenthesized_p,
19902                       bool member_p, bool friend_p)
19903 {
19904   cp_declarator *declarator;
19905   enum tree_code code;
19906   cp_cv_quals cv_quals;
19907   tree class_type;
19908   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19909
19910   /* Assume this is not a constructor, destructor, or type-conversion
19911      operator.  */
19912   if (ctor_dtor_or_conv_p)
19913     *ctor_dtor_or_conv_p = 0;
19914
19915   if (cp_parser_allow_gnu_extensions_p (parser))
19916     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19917
19918   /* Check for the ptr-operator production.  */
19919   cp_parser_parse_tentatively (parser);
19920   /* Parse the ptr-operator.  */
19921   code = cp_parser_ptr_operator (parser,
19922                                  &class_type,
19923                                  &cv_quals,
19924                                  &std_attributes);
19925
19926   /* If that worked, then we have a ptr-operator.  */
19927   if (cp_parser_parse_definitely (parser))
19928     {
19929       /* If a ptr-operator was found, then this declarator was not
19930          parenthesized.  */
19931       if (parenthesized_p)
19932         *parenthesized_p = true;
19933       /* The dependent declarator is optional if we are parsing an
19934          abstract-declarator.  */
19935       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19936         cp_parser_parse_tentatively (parser);
19937
19938       /* Parse the dependent declarator.  */
19939       declarator = cp_parser_declarator (parser, dcl_kind,
19940                                          /*ctor_dtor_or_conv_p=*/NULL,
19941                                          /*parenthesized_p=*/NULL,
19942                                          /*member_p=*/false,
19943                                          friend_p);
19944
19945       /* If we are parsing an abstract-declarator, we must handle the
19946          case where the dependent declarator is absent.  */
19947       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19948           && !cp_parser_parse_definitely (parser))
19949         declarator = NULL;
19950
19951       declarator = cp_parser_make_indirect_declarator
19952         (code, class_type, cv_quals, declarator, std_attributes);
19953     }
19954   /* Everything else is a direct-declarator.  */
19955   else
19956     {
19957       if (parenthesized_p)
19958         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19959                                                    CPP_OPEN_PAREN);
19960       declarator = cp_parser_direct_declarator (parser, dcl_kind,
19961                                                 ctor_dtor_or_conv_p,
19962                                                 member_p, friend_p);
19963     }
19964
19965   if (gnu_attributes && declarator && declarator != cp_error_declarator)
19966     declarator->attributes = gnu_attributes;
19967   return declarator;
19968 }
19969
19970 /* Parse a direct-declarator or direct-abstract-declarator.
19971
19972    direct-declarator:
19973      declarator-id
19974      direct-declarator ( parameter-declaration-clause )
19975        cv-qualifier-seq [opt]
19976        ref-qualifier [opt]
19977        exception-specification [opt]
19978      direct-declarator [ constant-expression [opt] ]
19979      ( declarator )
19980
19981    direct-abstract-declarator:
19982      direct-abstract-declarator [opt]
19983        ( parameter-declaration-clause )
19984        cv-qualifier-seq [opt]
19985        ref-qualifier [opt]
19986        exception-specification [opt]
19987      direct-abstract-declarator [opt] [ constant-expression [opt] ]
19988      ( abstract-declarator )
19989
19990    Returns a representation of the declarator.  DCL_KIND is
19991    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19992    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
19993    we are parsing a direct-declarator.  It is
19994    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19995    of ambiguity we prefer an abstract declarator, as per
19996    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19997    as for cp_parser_declarator.  */
19998
19999 static cp_declarator *
20000 cp_parser_direct_declarator (cp_parser* parser,
20001                              cp_parser_declarator_kind dcl_kind,
20002                              int* ctor_dtor_or_conv_p,
20003                              bool member_p, bool friend_p)
20004 {
20005   cp_token *token;
20006   cp_declarator *declarator = NULL;
20007   tree scope = NULL_TREE;
20008   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20009   bool saved_in_declarator_p = parser->in_declarator_p;
20010   bool first = true;
20011   tree pushed_scope = NULL_TREE;
20012   cp_token *open_paren = NULL, *close_paren = NULL;
20013
20014   while (true)
20015     {
20016       /* Peek at the next token.  */
20017       token = cp_lexer_peek_token (parser->lexer);
20018       if (token->type == CPP_OPEN_PAREN)
20019         {
20020           /* This is either a parameter-declaration-clause, or a
20021              parenthesized declarator. When we know we are parsing a
20022              named declarator, it must be a parenthesized declarator
20023              if FIRST is true. For instance, `(int)' is a
20024              parameter-declaration-clause, with an omitted
20025              direct-abstract-declarator. But `((*))', is a
20026              parenthesized abstract declarator. Finally, when T is a
20027              template parameter `(T)' is a
20028              parameter-declaration-clause, and not a parenthesized
20029              named declarator.
20030
20031              We first try and parse a parameter-declaration-clause,
20032              and then try a nested declarator (if FIRST is true).
20033
20034              It is not an error for it not to be a
20035              parameter-declaration-clause, even when FIRST is
20036              false. Consider,
20037
20038                int i (int);
20039                int i (3);
20040
20041              The first is the declaration of a function while the
20042              second is the definition of a variable, including its
20043              initializer.
20044
20045              Having seen only the parenthesis, we cannot know which of
20046              these two alternatives should be selected.  Even more
20047              complex are examples like:
20048
20049                int i (int (a));
20050                int i (int (3));
20051
20052              The former is a function-declaration; the latter is a
20053              variable initialization.
20054
20055              Thus again, we try a parameter-declaration-clause, and if
20056              that fails, we back out and return.  */
20057
20058           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20059             {
20060               tree params;
20061               bool is_declarator = false;
20062
20063               open_paren = NULL;
20064
20065               /* In a member-declarator, the only valid interpretation
20066                  of a parenthesis is the start of a
20067                  parameter-declaration-clause.  (It is invalid to
20068                  initialize a static data member with a parenthesized
20069                  initializer; only the "=" form of initialization is
20070                  permitted.)  */
20071               if (!member_p)
20072                 cp_parser_parse_tentatively (parser);
20073
20074               /* Consume the `('.  */
20075               matching_parens parens;
20076               parens.consume_open (parser);
20077               if (first)
20078                 {
20079                   /* If this is going to be an abstract declarator, we're
20080                      in a declarator and we can't have default args.  */
20081                   parser->default_arg_ok_p = false;
20082                   parser->in_declarator_p = true;
20083                 }
20084
20085               begin_scope (sk_function_parms, NULL_TREE);
20086
20087               /* Parse the parameter-declaration-clause.  */
20088               params = cp_parser_parameter_declaration_clause (parser);
20089
20090               /* Consume the `)'.  */
20091               parens.require_close (parser);
20092
20093               /* If all went well, parse the cv-qualifier-seq,
20094                  ref-qualifier and the exception-specification.  */
20095               if (member_p || cp_parser_parse_definitely (parser))
20096                 {
20097                   cp_cv_quals cv_quals;
20098                   cp_virt_specifiers virt_specifiers;
20099                   cp_ref_qualifier ref_qual;
20100                   tree exception_specification;
20101                   tree late_return;
20102                   tree attrs;
20103                   bool memfn = (member_p || (pushed_scope
20104                                              && CLASS_TYPE_P (pushed_scope)));
20105
20106                   is_declarator = true;
20107
20108                   if (ctor_dtor_or_conv_p)
20109                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20110                   first = false;
20111
20112                   /* Parse the cv-qualifier-seq.  */
20113                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20114                   /* Parse the ref-qualifier. */
20115                   ref_qual = cp_parser_ref_qualifier_opt (parser);
20116                   /* Parse the tx-qualifier.  */
20117                   tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20118                   /* And the exception-specification.  */
20119                   exception_specification
20120                     = cp_parser_exception_specification_opt (parser);
20121
20122                   attrs = cp_parser_std_attribute_spec_seq (parser);
20123
20124                   /* In here, we handle cases where attribute is used after
20125                      the function declaration.  For example:
20126                      void func (int x) __attribute__((vector(..)));  */
20127                   tree gnu_attrs = NULL_TREE;
20128                   tree requires_clause = NULL_TREE;
20129                   late_return = (cp_parser_late_return_type_opt
20130                                  (parser, declarator, requires_clause,
20131                                   memfn ? cv_quals : -1));
20132
20133                   /* Parse the virt-specifier-seq.  */
20134                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20135
20136                   /* Create the function-declarator.  */
20137                   declarator = make_call_declarator (declarator,
20138                                                      params,
20139                                                      cv_quals,
20140                                                      virt_specifiers,
20141                                                      ref_qual,
20142                                                      tx_qual,
20143                                                      exception_specification,
20144                                                      late_return,
20145                                                      requires_clause);
20146                   declarator->std_attributes = attrs;
20147                   declarator->attributes = gnu_attrs;
20148                   /* Any subsequent parameter lists are to do with
20149                      return type, so are not those of the declared
20150                      function.  */
20151                   parser->default_arg_ok_p = false;
20152                 }
20153
20154               /* Remove the function parms from scope.  */
20155               pop_bindings_and_leave_scope ();
20156
20157               if (is_declarator)
20158                 /* Repeat the main loop.  */
20159                 continue;
20160             }
20161
20162           /* If this is the first, we can try a parenthesized
20163              declarator.  */
20164           if (first)
20165             {
20166               bool saved_in_type_id_in_expr_p;
20167
20168               parser->default_arg_ok_p = saved_default_arg_ok_p;
20169               parser->in_declarator_p = saved_in_declarator_p;
20170
20171               open_paren = token;
20172               /* Consume the `('.  */
20173               matching_parens parens;
20174               parens.consume_open (parser);
20175               /* Parse the nested declarator.  */
20176               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20177               parser->in_type_id_in_expr_p = true;
20178               declarator
20179                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20180                                         /*parenthesized_p=*/NULL,
20181                                         member_p, friend_p);
20182               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20183               first = false;
20184               /* Expect a `)'.  */
20185               close_paren = cp_lexer_peek_token (parser->lexer);
20186               if (!parens.require_close (parser))
20187                 declarator = cp_error_declarator;
20188               if (declarator == cp_error_declarator)
20189                 break;
20190
20191               goto handle_declarator;
20192             }
20193           /* Otherwise, we must be done.  */
20194           else
20195             break;
20196         }
20197       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20198                && token->type == CPP_OPEN_SQUARE
20199                && !cp_next_tokens_can_be_attribute_p (parser))
20200         {
20201           /* Parse an array-declarator.  */
20202           tree bounds, attrs;
20203
20204           if (ctor_dtor_or_conv_p)
20205             *ctor_dtor_or_conv_p = 0;
20206
20207           open_paren = NULL;
20208           first = false;
20209           parser->default_arg_ok_p = false;
20210           parser->in_declarator_p = true;
20211           /* Consume the `['.  */
20212           cp_lexer_consume_token (parser->lexer);
20213           /* Peek at the next token.  */
20214           token = cp_lexer_peek_token (parser->lexer);
20215           /* If the next token is `]', then there is no
20216              constant-expression.  */
20217           if (token->type != CPP_CLOSE_SQUARE)
20218             {
20219               bool non_constant_p;
20220               bounds
20221                 = cp_parser_constant_expression (parser,
20222                                                  /*allow_non_constant=*/true,
20223                                                  &non_constant_p);
20224               if (!non_constant_p)
20225                 /* OK */;
20226               else if (error_operand_p (bounds))
20227                 /* Already gave an error.  */;
20228               else if (!parser->in_function_body
20229                        || current_binding_level->kind == sk_function_parms)
20230                 {
20231                   /* Normally, the array bound must be an integral constant
20232                      expression.  However, as an extension, we allow VLAs
20233                      in function scopes as long as they aren't part of a
20234                      parameter declaration.  */
20235                   cp_parser_error (parser,
20236                                    "array bound is not an integer constant");
20237                   bounds = error_mark_node;
20238                 }
20239               else if (processing_template_decl
20240                        && !type_dependent_expression_p (bounds))
20241                 {
20242                   /* Remember this wasn't a constant-expression.  */
20243                   bounds = build_nop (TREE_TYPE (bounds), bounds);
20244                   TREE_SIDE_EFFECTS (bounds) = 1;
20245                 }
20246             }
20247           else
20248             bounds = NULL_TREE;
20249           /* Look for the closing `]'.  */
20250           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20251             {
20252               declarator = cp_error_declarator;
20253               break;
20254             }
20255
20256           attrs = cp_parser_std_attribute_spec_seq (parser);
20257           declarator = make_array_declarator (declarator, bounds);
20258           declarator->std_attributes = attrs;
20259         }
20260       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20261         {
20262           {
20263             tree qualifying_scope;
20264             tree unqualified_name;
20265             tree attrs;
20266             special_function_kind sfk;
20267             bool abstract_ok;
20268             bool pack_expansion_p = false;
20269             cp_token *declarator_id_start_token;
20270
20271             /* Parse a declarator-id */
20272             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20273             if (abstract_ok)
20274               {
20275                 cp_parser_parse_tentatively (parser);
20276
20277                 /* If we see an ellipsis, we should be looking at a
20278                    parameter pack. */
20279                 if (token->type == CPP_ELLIPSIS)
20280                   {
20281                     /* Consume the `...' */
20282                     cp_lexer_consume_token (parser->lexer);
20283
20284                     pack_expansion_p = true;
20285                   }
20286               }
20287
20288             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20289             unqualified_name
20290               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20291             qualifying_scope = parser->scope;
20292             if (abstract_ok)
20293               {
20294                 bool okay = false;
20295
20296                 if (!unqualified_name && pack_expansion_p)
20297                   {
20298                     /* Check whether an error occurred. */
20299                     okay = !cp_parser_error_occurred (parser);
20300
20301                     /* We already consumed the ellipsis to mark a
20302                        parameter pack, but we have no way to report it,
20303                        so abort the tentative parse. We will be exiting
20304                        immediately anyway. */
20305                     cp_parser_abort_tentative_parse (parser);
20306                   }
20307                 else
20308                   okay = cp_parser_parse_definitely (parser);
20309
20310                 if (!okay)
20311                   unqualified_name = error_mark_node;
20312                 else if (unqualified_name
20313                          && (qualifying_scope
20314                              || (!identifier_p (unqualified_name))))
20315                   {
20316                     cp_parser_error (parser, "expected unqualified-id");
20317                     unqualified_name = error_mark_node;
20318                   }
20319               }
20320
20321             if (!unqualified_name)
20322               return NULL;
20323             if (unqualified_name == error_mark_node)
20324               {
20325                 declarator = cp_error_declarator;
20326                 pack_expansion_p = false;
20327                 declarator->parameter_pack_p = false;
20328                 break;
20329               }
20330
20331             attrs = cp_parser_std_attribute_spec_seq (parser);
20332
20333             if (qualifying_scope && at_namespace_scope_p ()
20334                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20335               {
20336                 /* In the declaration of a member of a template class
20337                    outside of the class itself, the SCOPE will sometimes
20338                    be a TYPENAME_TYPE.  For example, given:
20339
20340                    template <typename T>
20341                    int S<T>::R::i = 3;
20342
20343                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
20344                    this context, we must resolve S<T>::R to an ordinary
20345                    type, rather than a typename type.
20346
20347                    The reason we normally avoid resolving TYPENAME_TYPEs
20348                    is that a specialization of `S' might render
20349                    `S<T>::R' not a type.  However, if `S' is
20350                    specialized, then this `i' will not be used, so there
20351                    is no harm in resolving the types here.  */
20352                 tree type;
20353
20354                 /* Resolve the TYPENAME_TYPE.  */
20355                 type = resolve_typename_type (qualifying_scope,
20356                                               /*only_current_p=*/false);
20357                 /* If that failed, the declarator is invalid.  */
20358                 if (TREE_CODE (type) == TYPENAME_TYPE)
20359                   {
20360                     if (typedef_variant_p (type))
20361                       error_at (declarator_id_start_token->location,
20362                                 "cannot define member of dependent typedef "
20363                                 "%qT", type);
20364                     else
20365                       error_at (declarator_id_start_token->location,
20366                                 "%<%T::%E%> is not a type",
20367                                 TYPE_CONTEXT (qualifying_scope),
20368                                 TYPE_IDENTIFIER (qualifying_scope));
20369                   }
20370                 qualifying_scope = type;
20371               }
20372
20373             sfk = sfk_none;
20374
20375             if (unqualified_name)
20376               {
20377                 tree class_type;
20378
20379                 if (qualifying_scope
20380                     && CLASS_TYPE_P (qualifying_scope))
20381                   class_type = qualifying_scope;
20382                 else
20383                   class_type = current_class_type;
20384
20385                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20386                   {
20387                     tree name_type = TREE_TYPE (unqualified_name);
20388
20389                     if (!class_type || !same_type_p (name_type, class_type))
20390                       {
20391                         /* We do not attempt to print the declarator
20392                            here because we do not have enough
20393                            information about its original syntactic
20394                            form.  */
20395                         cp_parser_error (parser, "invalid declarator");
20396                         declarator = cp_error_declarator;
20397                         break;
20398                       }
20399                     else if (qualifying_scope
20400                              && CLASSTYPE_USE_TEMPLATE (name_type))
20401                       {
20402                         error_at (declarator_id_start_token->location,
20403                                   "invalid use of constructor as a template");
20404                         inform (declarator_id_start_token->location,
20405                                 "use %<%T::%D%> instead of %<%T::%D%> to "
20406                                 "name the constructor in a qualified name",
20407                                 class_type,
20408                                 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20409                                 class_type, name_type);
20410                         declarator = cp_error_declarator;
20411                         break;
20412                       }
20413                     unqualified_name = constructor_name (class_type);
20414                   }
20415
20416                 if (class_type)
20417                   {
20418                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20419                       sfk = sfk_destructor;
20420                     else if (identifier_p (unqualified_name)
20421                              && IDENTIFIER_CONV_OP_P (unqualified_name))
20422                       sfk = sfk_conversion;
20423                     else if (/* There's no way to declare a constructor
20424                                 for an unnamed type, even if the type
20425                                 got a name for linkage purposes.  */
20426                              !TYPE_WAS_UNNAMED (class_type)
20427                              /* Handle correctly (c++/19200):
20428
20429                                 struct S {
20430                                   struct T{};
20431                                   friend void S(T);
20432                                 };
20433
20434                                 and also:
20435
20436                                 namespace N {
20437                                   void S();
20438                                 }
20439
20440                                 struct S {
20441                                   friend void N::S();
20442                                 };  */
20443                              && (!friend_p || class_type == qualifying_scope)
20444                              && constructor_name_p (unqualified_name,
20445                                                     class_type))
20446                       sfk = sfk_constructor;
20447                     else if (is_overloaded_fn (unqualified_name)
20448                              && DECL_CONSTRUCTOR_P (get_first_fn
20449                                                     (unqualified_name)))
20450                       sfk = sfk_constructor;
20451
20452                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
20453                       *ctor_dtor_or_conv_p = -1;
20454                   }
20455               }
20456             declarator = make_id_declarator (qualifying_scope,
20457                                              unqualified_name,
20458                                              sfk);
20459             declarator->std_attributes = attrs;
20460             declarator->id_loc = token->location;
20461             declarator->parameter_pack_p = pack_expansion_p;
20462
20463             if (pack_expansion_p)
20464               maybe_warn_variadic_templates ();
20465           }
20466
20467         handle_declarator:;
20468           scope = get_scope_of_declarator (declarator);
20469           if (scope)
20470             {
20471               /* Any names that appear after the declarator-id for a
20472                  member are looked up in the containing scope.  */
20473               if (at_function_scope_p ())
20474                 {
20475                   /* But declarations with qualified-ids can't appear in a
20476                      function.  */
20477                   cp_parser_error (parser, "qualified-id in declaration");
20478                   declarator = cp_error_declarator;
20479                   break;
20480                 }
20481               pushed_scope = push_scope (scope);
20482             }
20483           parser->in_declarator_p = true;
20484           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20485               || (declarator && declarator->kind == cdk_id))
20486             /* Default args are only allowed on function
20487                declarations.  */
20488             parser->default_arg_ok_p = saved_default_arg_ok_p;
20489           else
20490             parser->default_arg_ok_p = false;
20491
20492           first = false;
20493         }
20494       /* We're done.  */
20495       else
20496         break;
20497     }
20498
20499   /* For an abstract declarator, we might wind up with nothing at this
20500      point.  That's an error; the declarator is not optional.  */
20501   if (!declarator)
20502     cp_parser_error (parser, "expected declarator");
20503   else if (open_paren)
20504     {
20505       /* Record overly parenthesized declarator so we can give a
20506          diagnostic about confusing decl/expr disambiguation.  */
20507       if (declarator->kind == cdk_array)
20508         {
20509           /* If the open and close parens are on different lines, this
20510              is probably a formatting thing, so ignore.  */
20511           expanded_location open = expand_location (open_paren->location);
20512           expanded_location close = expand_location (close_paren->location);
20513           if (open.line != close.line || open.file != close.file)
20514             open_paren = NULL;
20515         }
20516       if (open_paren)
20517         declarator->parenthesized = open_paren->location;
20518     }
20519
20520   /* If we entered a scope, we must exit it now.  */
20521   if (pushed_scope)
20522     pop_scope (pushed_scope);
20523
20524   parser->default_arg_ok_p = saved_default_arg_ok_p;
20525   parser->in_declarator_p = saved_in_declarator_p;
20526
20527   return declarator;
20528 }
20529
20530 /* Parse a ptr-operator.
20531
20532    ptr-operator:
20533      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20534      * cv-qualifier-seq [opt]
20535      &
20536      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20537      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20538
20539    GNU Extension:
20540
20541    ptr-operator:
20542      & cv-qualifier-seq [opt]
20543
20544    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20545    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20546    an rvalue reference. In the case of a pointer-to-member, *TYPE is
20547    filled in with the TYPE containing the member.  *CV_QUALS is
20548    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20549    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
20550    Note that the tree codes returned by this function have nothing
20551    to do with the types of trees that will be eventually be created
20552    to represent the pointer or reference type being parsed. They are
20553    just constants with suggestive names. */
20554 static enum tree_code
20555 cp_parser_ptr_operator (cp_parser* parser,
20556                         tree* type,
20557                         cp_cv_quals *cv_quals,
20558                         tree *attributes)
20559 {
20560   enum tree_code code = ERROR_MARK;
20561   cp_token *token;
20562   tree attrs = NULL_TREE;
20563
20564   /* Assume that it's not a pointer-to-member.  */
20565   *type = NULL_TREE;
20566   /* And that there are no cv-qualifiers.  */
20567   *cv_quals = TYPE_UNQUALIFIED;
20568
20569   /* Peek at the next token.  */
20570   token = cp_lexer_peek_token (parser->lexer);
20571
20572   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
20573   if (token->type == CPP_MULT)
20574     code = INDIRECT_REF;
20575   else if (token->type == CPP_AND)
20576     code = ADDR_EXPR;
20577   else if ((cxx_dialect != cxx98) &&
20578            token->type == CPP_AND_AND) /* C++0x only */
20579     code = NON_LVALUE_EXPR;
20580
20581   if (code != ERROR_MARK)
20582     {
20583       /* Consume the `*', `&' or `&&'.  */
20584       cp_lexer_consume_token (parser->lexer);
20585
20586       /* A `*' can be followed by a cv-qualifier-seq, and so can a
20587          `&', if we are allowing GNU extensions.  (The only qualifier
20588          that can legally appear after `&' is `restrict', but that is
20589          enforced during semantic analysis.  */
20590       if (code == INDIRECT_REF
20591           || cp_parser_allow_gnu_extensions_p (parser))
20592         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20593
20594       attrs = cp_parser_std_attribute_spec_seq (parser);
20595       if (attributes != NULL)
20596         *attributes = attrs;
20597     }
20598   else
20599     {
20600       /* Try the pointer-to-member case.  */
20601       cp_parser_parse_tentatively (parser);
20602       /* Look for the optional `::' operator.  */
20603       cp_parser_global_scope_opt (parser,
20604                                   /*current_scope_valid_p=*/false);
20605       /* Look for the nested-name specifier.  */
20606       token = cp_lexer_peek_token (parser->lexer);
20607       cp_parser_nested_name_specifier (parser,
20608                                        /*typename_keyword_p=*/false,
20609                                        /*check_dependency_p=*/true,
20610                                        /*type_p=*/false,
20611                                        /*is_declaration=*/false);
20612       /* If we found it, and the next token is a `*', then we are
20613          indeed looking at a pointer-to-member operator.  */
20614       if (!cp_parser_error_occurred (parser)
20615           && cp_parser_require (parser, CPP_MULT, RT_MULT))
20616         {
20617           /* Indicate that the `*' operator was used.  */
20618           code = INDIRECT_REF;
20619
20620           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20621             error_at (token->location, "%qD is a namespace", parser->scope);
20622           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20623             error_at (token->location, "cannot form pointer to member of "
20624                       "non-class %q#T", parser->scope);
20625           else
20626             {
20627               /* The type of which the member is a member is given by the
20628                  current SCOPE.  */
20629               *type = parser->scope;
20630               /* The next name will not be qualified.  */
20631               parser->scope = NULL_TREE;
20632               parser->qualifying_scope = NULL_TREE;
20633               parser->object_scope = NULL_TREE;
20634               /* Look for optional c++11 attributes.  */
20635               attrs = cp_parser_std_attribute_spec_seq (parser);
20636               if (attributes != NULL)
20637                 *attributes = attrs;
20638               /* Look for the optional cv-qualifier-seq.  */
20639               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20640             }
20641         }
20642       /* If that didn't work we don't have a ptr-operator.  */
20643       if (!cp_parser_parse_definitely (parser))
20644         cp_parser_error (parser, "expected ptr-operator");
20645     }
20646
20647   return code;
20648 }
20649
20650 /* Parse an (optional) cv-qualifier-seq.
20651
20652    cv-qualifier-seq:
20653      cv-qualifier cv-qualifier-seq [opt]
20654
20655    cv-qualifier:
20656      const
20657      volatile
20658
20659    GNU Extension:
20660
20661    cv-qualifier:
20662      __restrict__
20663
20664    Returns a bitmask representing the cv-qualifiers.  */
20665
20666 static cp_cv_quals
20667 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20668 {
20669   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20670
20671   while (true)
20672     {
20673       cp_token *token;
20674       cp_cv_quals cv_qualifier;
20675
20676       /* Peek at the next token.  */
20677       token = cp_lexer_peek_token (parser->lexer);
20678       /* See if it's a cv-qualifier.  */
20679       switch (token->keyword)
20680         {
20681         case RID_CONST:
20682           cv_qualifier = TYPE_QUAL_CONST;
20683           break;
20684
20685         case RID_VOLATILE:
20686           cv_qualifier = TYPE_QUAL_VOLATILE;
20687           break;
20688
20689         case RID_RESTRICT:
20690           cv_qualifier = TYPE_QUAL_RESTRICT;
20691           break;
20692
20693         default:
20694           cv_qualifier = TYPE_UNQUALIFIED;
20695           break;
20696         }
20697
20698       if (!cv_qualifier)
20699         break;
20700
20701       if (cv_quals & cv_qualifier)
20702         {
20703           gcc_rich_location richloc (token->location);
20704           richloc.add_fixit_remove ();
20705           error_at (&richloc, "duplicate cv-qualifier");
20706           cp_lexer_purge_token (parser->lexer);
20707         }
20708       else
20709         {
20710           cp_lexer_consume_token (parser->lexer);
20711           cv_quals |= cv_qualifier;
20712         }
20713     }
20714
20715   return cv_quals;
20716 }
20717
20718 /* Parse an (optional) ref-qualifier
20719
20720    ref-qualifier:
20721      &
20722      &&
20723
20724    Returns cp_ref_qualifier representing ref-qualifier. */
20725
20726 static cp_ref_qualifier
20727 cp_parser_ref_qualifier_opt (cp_parser* parser)
20728 {
20729   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20730
20731   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
20732   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20733     return ref_qual;
20734
20735   while (true)
20736     {
20737       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20738       cp_token *token = cp_lexer_peek_token (parser->lexer);
20739
20740       switch (token->type)
20741         {
20742         case CPP_AND:
20743           curr_ref_qual = REF_QUAL_LVALUE;
20744           break;
20745
20746         case CPP_AND_AND:
20747           curr_ref_qual = REF_QUAL_RVALUE;
20748           break;
20749
20750         default:
20751           curr_ref_qual = REF_QUAL_NONE;
20752           break;
20753         }
20754
20755       if (!curr_ref_qual)
20756         break;
20757       else if (ref_qual)
20758         {
20759           error_at (token->location, "multiple ref-qualifiers");
20760           cp_lexer_purge_token (parser->lexer);
20761         }
20762       else
20763         {
20764           ref_qual = curr_ref_qual;
20765           cp_lexer_consume_token (parser->lexer);
20766         }
20767     }
20768
20769   return ref_qual;
20770 }
20771
20772 /* Parse an optional tx-qualifier.
20773
20774    tx-qualifier:
20775      transaction_safe
20776      transaction_safe_dynamic  */
20777
20778 static tree
20779 cp_parser_tx_qualifier_opt (cp_parser *parser)
20780 {
20781   cp_token *token = cp_lexer_peek_token (parser->lexer);
20782   if (token->type == CPP_NAME)
20783     {
20784       tree name = token->u.value;
20785       const char *p = IDENTIFIER_POINTER (name);
20786       const int len = strlen ("transaction_safe");
20787       if (!strncmp (p, "transaction_safe", len))
20788         {
20789           p += len;
20790           if (*p == '\0'
20791               || !strcmp (p, "_dynamic"))
20792             {
20793               cp_lexer_consume_token (parser->lexer);
20794               if (!flag_tm)
20795                 {
20796                   error ("%qE requires %<-fgnu-tm%>", name);
20797                   return NULL_TREE;
20798                 }
20799               else
20800                 return name;
20801             }
20802         }
20803     }
20804   return NULL_TREE;
20805 }
20806
20807 /* Parse an (optional) virt-specifier-seq.
20808
20809    virt-specifier-seq:
20810      virt-specifier virt-specifier-seq [opt]
20811
20812    virt-specifier:
20813      override
20814      final
20815
20816    Returns a bitmask representing the virt-specifiers.  */
20817
20818 static cp_virt_specifiers
20819 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20820 {
20821   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20822
20823   while (true)
20824     {
20825       cp_token *token;
20826       cp_virt_specifiers virt_specifier;
20827
20828       /* Peek at the next token.  */
20829       token = cp_lexer_peek_token (parser->lexer);
20830       /* See if it's a virt-specifier-qualifier.  */
20831       if (token->type != CPP_NAME)
20832         break;
20833       if (id_equal (token->u.value, "override"))
20834         {
20835           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20836           virt_specifier = VIRT_SPEC_OVERRIDE;
20837         }
20838       else if (id_equal (token->u.value, "final"))
20839         {
20840           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20841           virt_specifier = VIRT_SPEC_FINAL;
20842         }
20843       else if (id_equal (token->u.value, "__final"))
20844         {
20845           virt_specifier = VIRT_SPEC_FINAL;
20846         }
20847       else
20848         break;
20849
20850       if (virt_specifiers & virt_specifier)
20851         {
20852           gcc_rich_location richloc (token->location);
20853           richloc.add_fixit_remove ();
20854           error_at (&richloc, "duplicate virt-specifier");
20855           cp_lexer_purge_token (parser->lexer);
20856         }
20857       else
20858         {
20859           cp_lexer_consume_token (parser->lexer);
20860           virt_specifiers |= virt_specifier;
20861         }
20862     }
20863   return virt_specifiers;
20864 }
20865
20866 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20867    is in scope even though it isn't real.  */
20868
20869 void
20870 inject_this_parameter (tree ctype, cp_cv_quals quals)
20871 {
20872   tree this_parm;
20873
20874   if (current_class_ptr)
20875     {
20876       /* We don't clear this between NSDMIs.  Is it already what we want?  */
20877       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20878       if (DECL_P (current_class_ptr)
20879           && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20880           && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20881           && cp_type_quals (type) == quals)
20882         return;
20883     }
20884
20885   this_parm = build_this_parm (NULL_TREE, ctype, quals);
20886   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
20887   current_class_ptr = NULL_TREE;
20888   current_class_ref
20889     = cp_build_fold_indirect_ref (this_parm);
20890   current_class_ptr = this_parm;
20891 }
20892
20893 /* Return true iff our current scope is a non-static data member
20894    initializer.  */
20895
20896 bool
20897 parsing_nsdmi (void)
20898 {
20899   /* We recognize NSDMI context by the context-less 'this' pointer set up
20900      by the function above.  */
20901   if (current_class_ptr
20902       && TREE_CODE (current_class_ptr) == PARM_DECL
20903       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20904     return true;
20905   return false;
20906 }
20907
20908 /* Parse a late-specified return type, if any.  This is not a separate
20909    non-terminal, but part of a function declarator, which looks like
20910
20911    -> trailing-type-specifier-seq abstract-declarator(opt)
20912
20913    Returns the type indicated by the type-id.
20914
20915    In addition to this, parse any queued up #pragma omp declare simd
20916    clauses, and #pragma acc routine clauses.
20917
20918    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20919    function.  */
20920
20921 static tree
20922 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20923                                 tree& requires_clause, cp_cv_quals quals)
20924 {
20925   cp_token *token;
20926   tree type = NULL_TREE;
20927   bool declare_simd_p = (parser->omp_declare_simd
20928                          && declarator
20929                          && declarator->kind == cdk_id);
20930
20931   bool oacc_routine_p = (parser->oacc_routine
20932                          && declarator
20933                          && declarator->kind == cdk_id);
20934
20935   /* Peek at the next token.  */
20936   token = cp_lexer_peek_token (parser->lexer);
20937   /* A late-specified return type is indicated by an initial '->'. */
20938   if (token->type != CPP_DEREF
20939       && token->keyword != RID_REQUIRES
20940       && !(token->type == CPP_NAME
20941            && token->u.value == ridpointers[RID_REQUIRES])
20942       && !(declare_simd_p || oacc_routine_p))
20943     return NULL_TREE;
20944
20945   tree save_ccp = current_class_ptr;
20946   tree save_ccr = current_class_ref;
20947   if (quals >= 0)
20948     {
20949       /* DR 1207: 'this' is in scope in the trailing return type.  */
20950       inject_this_parameter (current_class_type, quals);
20951     }
20952
20953   if (token->type == CPP_DEREF)
20954     {
20955       /* Consume the ->.  */
20956       cp_lexer_consume_token (parser->lexer);
20957
20958       type = cp_parser_trailing_type_id (parser);
20959     }
20960
20961   /* Function declarations may be followed by a trailing
20962      requires-clause.  */
20963   requires_clause = cp_parser_requires_clause_opt (parser);
20964
20965   if (declare_simd_p)
20966     declarator->attributes
20967       = cp_parser_late_parsing_omp_declare_simd (parser,
20968                                                  declarator->attributes);
20969   if (oacc_routine_p)
20970     declarator->attributes
20971       = cp_parser_late_parsing_oacc_routine (parser,
20972                                              declarator->attributes);
20973
20974   if (quals >= 0)
20975     {
20976       current_class_ptr = save_ccp;
20977       current_class_ref = save_ccr;
20978     }
20979
20980   return type;
20981 }
20982
20983 /* Parse a declarator-id.
20984
20985    declarator-id:
20986      id-expression
20987      :: [opt] nested-name-specifier [opt] type-name
20988
20989    In the `id-expression' case, the value returned is as for
20990    cp_parser_id_expression if the id-expression was an unqualified-id.
20991    If the id-expression was a qualified-id, then a SCOPE_REF is
20992    returned.  The first operand is the scope (either a NAMESPACE_DECL
20993    or TREE_TYPE), but the second is still just a representation of an
20994    unqualified-id.  */
20995
20996 static tree
20997 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20998 {
20999   tree id;
21000   /* The expression must be an id-expression.  Assume that qualified
21001      names are the names of types so that:
21002
21003        template <class T>
21004        int S<T>::R::i = 3;
21005
21006      will work; we must treat `S<T>::R' as the name of a type.
21007      Similarly, assume that qualified names are templates, where
21008      required, so that:
21009
21010        template <class T>
21011        int S<T>::R<T>::i = 3;
21012
21013      will work, too.  */
21014   id = cp_parser_id_expression (parser,
21015                                 /*template_keyword_p=*/false,
21016                                 /*check_dependency_p=*/false,
21017                                 /*template_p=*/NULL,
21018                                 /*declarator_p=*/true,
21019                                 optional_p);
21020   if (id && BASELINK_P (id))
21021     id = BASELINK_FUNCTIONS (id);
21022   return id;
21023 }
21024
21025 /* Parse a type-id.
21026
21027    type-id:
21028      type-specifier-seq abstract-declarator [opt]
21029
21030    Returns the TYPE specified.  */
21031
21032 static tree
21033 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21034                      bool is_trailing_return)
21035 {
21036   cp_decl_specifier_seq type_specifier_seq;
21037   cp_declarator *abstract_declarator;
21038
21039   /* Parse the type-specifier-seq.  */
21040   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21041                                 is_trailing_return,
21042                                 &type_specifier_seq);
21043   if (is_template_arg && type_specifier_seq.type
21044       && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21045       && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21046     /* A bare template name as a template argument is a template template
21047        argument, not a placeholder, so fail parsing it as a type argument.  */
21048     {
21049       gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21050       cp_parser_simulate_error (parser);
21051       return error_mark_node;
21052     }
21053   if (type_specifier_seq.type == error_mark_node)
21054     return error_mark_node;
21055
21056   /* There might or might not be an abstract declarator.  */
21057   cp_parser_parse_tentatively (parser);
21058   /* Look for the declarator.  */
21059   abstract_declarator
21060     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21061                             /*parenthesized_p=*/NULL,
21062                             /*member_p=*/false,
21063                             /*friend_p=*/false);
21064   /* Check to see if there really was a declarator.  */
21065   if (!cp_parser_parse_definitely (parser))
21066     abstract_declarator = NULL;
21067
21068   if (type_specifier_seq.type
21069       /* The concepts TS allows 'auto' as a type-id.  */
21070       && (!flag_concepts || parser->in_type_id_in_expr_p)
21071       /* None of the valid uses of 'auto' in C++14 involve the type-id
21072          nonterminal, but it is valid in a trailing-return-type.  */
21073       && !(cxx_dialect >= cxx14 && is_trailing_return))
21074     if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21075       {
21076         /* A type-id with type 'auto' is only ok if the abstract declarator
21077            is a function declarator with a late-specified return type.
21078
21079            A type-id with 'auto' is also valid in a trailing-return-type
21080            in a compound-requirement. */
21081         if (abstract_declarator
21082             && abstract_declarator->kind == cdk_function
21083             && abstract_declarator->u.function.late_return_type)
21084           /* OK */;
21085         else if (parser->in_result_type_constraint_p)
21086           /* OK */;
21087         else
21088           {
21089             location_t loc = type_specifier_seq.locations[ds_type_spec];
21090             if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21091               {
21092                 error_at (loc, "missing template arguments after %qT",
21093                           auto_node);
21094                 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21095                         tmpl);
21096               }
21097             else
21098               error_at (loc, "invalid use of %qT", auto_node);
21099             return error_mark_node;
21100           }
21101       }
21102   
21103   return groktypename (&type_specifier_seq, abstract_declarator,
21104                        is_template_arg);
21105 }
21106
21107 static tree
21108 cp_parser_type_id (cp_parser *parser)
21109 {
21110   return cp_parser_type_id_1 (parser, false, false);
21111 }
21112
21113 static tree
21114 cp_parser_template_type_arg (cp_parser *parser)
21115 {
21116   tree r;
21117   const char *saved_message = parser->type_definition_forbidden_message;
21118   parser->type_definition_forbidden_message
21119     = G_("types may not be defined in template arguments");
21120   r = cp_parser_type_id_1 (parser, true, false);
21121   parser->type_definition_forbidden_message = saved_message;
21122   if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21123     {
21124       error ("invalid use of %<auto%> in template argument");
21125       r = error_mark_node;
21126     }
21127   return r;
21128 }
21129
21130 static tree
21131 cp_parser_trailing_type_id (cp_parser *parser)
21132 {
21133   return cp_parser_type_id_1 (parser, false, true);
21134 }
21135
21136 /* Parse a type-specifier-seq.
21137
21138    type-specifier-seq:
21139      type-specifier type-specifier-seq [opt]
21140
21141    GNU extension:
21142
21143    type-specifier-seq:
21144      attributes type-specifier-seq [opt]
21145
21146    If IS_DECLARATION is true, we are at the start of a "condition" or
21147    exception-declaration, so we might be followed by a declarator-id.
21148
21149    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21150    i.e. we've just seen "->".
21151
21152    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
21153
21154 static void
21155 cp_parser_type_specifier_seq (cp_parser* parser,
21156                               bool is_declaration,
21157                               bool is_trailing_return,
21158                               cp_decl_specifier_seq *type_specifier_seq)
21159 {
21160   bool seen_type_specifier = false;
21161   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21162   cp_token *start_token = NULL;
21163
21164   /* Clear the TYPE_SPECIFIER_SEQ.  */
21165   clear_decl_specs (type_specifier_seq);
21166
21167   /* In the context of a trailing return type, enum E { } is an
21168      elaborated-type-specifier followed by a function-body, not an
21169      enum-specifier.  */
21170   if (is_trailing_return)
21171     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21172
21173   /* Parse the type-specifiers and attributes.  */
21174   while (true)
21175     {
21176       tree type_specifier;
21177       bool is_cv_qualifier;
21178
21179       /* Check for attributes first.  */
21180       if (cp_next_tokens_can_be_attribute_p (parser))
21181         {
21182           type_specifier_seq->attributes
21183             = attr_chainon (type_specifier_seq->attributes,
21184                             cp_parser_attributes_opt (parser));
21185           continue;
21186         }
21187
21188       /* record the token of the beginning of the type specifier seq,
21189          for error reporting purposes*/
21190      if (!start_token)
21191        start_token = cp_lexer_peek_token (parser->lexer);
21192
21193       /* Look for the type-specifier.  */
21194       type_specifier = cp_parser_type_specifier (parser,
21195                                                  flags,
21196                                                  type_specifier_seq,
21197                                                  /*is_declaration=*/false,
21198                                                  NULL,
21199                                                  &is_cv_qualifier);
21200       if (!type_specifier)
21201         {
21202           /* If the first type-specifier could not be found, this is not a
21203              type-specifier-seq at all.  */
21204           if (!seen_type_specifier)
21205             {
21206               /* Set in_declarator_p to avoid skipping to the semicolon.  */
21207               int in_decl = parser->in_declarator_p;
21208               parser->in_declarator_p = true;
21209
21210               if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21211                   || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21212                 cp_parser_error (parser, "expected type-specifier");
21213
21214               parser->in_declarator_p = in_decl;
21215
21216               type_specifier_seq->type = error_mark_node;
21217               return;
21218             }
21219           /* If subsequent type-specifiers could not be found, the
21220              type-specifier-seq is complete.  */
21221           break;
21222         }
21223
21224       seen_type_specifier = true;
21225       /* The standard says that a condition can be:
21226
21227             type-specifier-seq declarator = assignment-expression
21228
21229          However, given:
21230
21231            struct S {};
21232            if (int S = ...)
21233
21234          we should treat the "S" as a declarator, not as a
21235          type-specifier.  The standard doesn't say that explicitly for
21236          type-specifier-seq, but it does say that for
21237          decl-specifier-seq in an ordinary declaration.  Perhaps it
21238          would be clearer just to allow a decl-specifier-seq here, and
21239          then add a semantic restriction that if any decl-specifiers
21240          that are not type-specifiers appear, the program is invalid.  */
21241       if (is_declaration && !is_cv_qualifier)
21242         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21243     }
21244 }
21245
21246 /* Return whether the function currently being declared has an associated
21247    template parameter list.  */
21248
21249 static bool
21250 function_being_declared_is_template_p (cp_parser* parser)
21251 {
21252   if (!current_template_parms || processing_template_parmlist)
21253     return false;
21254
21255   if (parser->implicit_template_scope)
21256     return true;
21257
21258   if (at_class_scope_p ()
21259       && TYPE_BEING_DEFINED (current_class_type))
21260     return parser->num_template_parameter_lists != 0;
21261
21262   return ((int) parser->num_template_parameter_lists > template_class_depth
21263           (current_class_type));
21264 }
21265
21266 /* Parse a parameter-declaration-clause.
21267
21268    parameter-declaration-clause:
21269      parameter-declaration-list [opt] ... [opt]
21270      parameter-declaration-list , ...
21271
21272    Returns a representation for the parameter declarations.  A return
21273    value of NULL indicates a parameter-declaration-clause consisting
21274    only of an ellipsis.  */
21275
21276 static tree
21277 cp_parser_parameter_declaration_clause (cp_parser* parser)
21278 {
21279   tree parameters;
21280   cp_token *token;
21281   bool ellipsis_p;
21282   bool is_error;
21283
21284   temp_override<bool> cleanup
21285     (parser->auto_is_implicit_function_template_parm_p);
21286
21287   if (!processing_specialization
21288       && !processing_template_parmlist
21289       && !processing_explicit_instantiation
21290       /* default_arg_ok_p tracks whether this is a parameter-clause for an
21291          actual function or a random abstract declarator.  */
21292       && parser->default_arg_ok_p)
21293     if (!current_function_decl
21294         || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21295       parser->auto_is_implicit_function_template_parm_p = true;
21296
21297   /* Peek at the next token.  */
21298   token = cp_lexer_peek_token (parser->lexer);
21299   /* Check for trivial parameter-declaration-clauses.  */
21300   if (token->type == CPP_ELLIPSIS)
21301     {
21302       /* Consume the `...' token.  */
21303       cp_lexer_consume_token (parser->lexer);
21304       return NULL_TREE;
21305     }
21306   else if (token->type == CPP_CLOSE_PAREN)
21307     /* There are no parameters.  */
21308     {
21309 #ifndef NO_IMPLICIT_EXTERN_C
21310       if (in_system_header_at (input_location)
21311           && current_class_type == NULL
21312           && current_lang_name == lang_name_c)
21313         return NULL_TREE;
21314       else
21315 #endif
21316         return void_list_node;
21317     }
21318   /* Check for `(void)', too, which is a special case.  */
21319   else if (token->keyword == RID_VOID
21320            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21321                == CPP_CLOSE_PAREN))
21322     {
21323       /* Consume the `void' token.  */
21324       cp_lexer_consume_token (parser->lexer);
21325       /* There are no parameters.  */
21326       return void_list_node;
21327     }
21328
21329   /* Parse the parameter-declaration-list.  */
21330   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21331   /* If a parse error occurred while parsing the
21332      parameter-declaration-list, then the entire
21333      parameter-declaration-clause is erroneous.  */
21334   if (is_error)
21335     return NULL;
21336
21337   /* Peek at the next token.  */
21338   token = cp_lexer_peek_token (parser->lexer);
21339   /* If it's a `,', the clause should terminate with an ellipsis.  */
21340   if (token->type == CPP_COMMA)
21341     {
21342       /* Consume the `,'.  */
21343       cp_lexer_consume_token (parser->lexer);
21344       /* Expect an ellipsis.  */
21345       ellipsis_p
21346         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21347     }
21348   /* It might also be `...' if the optional trailing `,' was
21349      omitted.  */
21350   else if (token->type == CPP_ELLIPSIS)
21351     {
21352       /* Consume the `...' token.  */
21353       cp_lexer_consume_token (parser->lexer);
21354       /* And remember that we saw it.  */
21355       ellipsis_p = true;
21356     }
21357   else
21358     ellipsis_p = false;
21359
21360   /* Finish the parameter list.  */
21361   if (!ellipsis_p)
21362     parameters = chainon (parameters, void_list_node);
21363
21364   return parameters;
21365 }
21366
21367 /* Parse a parameter-declaration-list.
21368
21369    parameter-declaration-list:
21370      parameter-declaration
21371      parameter-declaration-list , parameter-declaration
21372
21373    Returns a representation of the parameter-declaration-list, as for
21374    cp_parser_parameter_declaration_clause.  However, the
21375    `void_list_node' is never appended to the list.  Upon return,
21376    *IS_ERROR will be true iff an error occurred.  */
21377
21378 static tree
21379 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21380 {
21381   tree parameters = NULL_TREE;
21382   tree *tail = &parameters;
21383   bool saved_in_unbraced_linkage_specification_p;
21384   int index = 0;
21385
21386   /* Assume all will go well.  */
21387   *is_error = false;
21388   /* The special considerations that apply to a function within an
21389      unbraced linkage specifications do not apply to the parameters
21390      to the function.  */
21391   saved_in_unbraced_linkage_specification_p
21392     = parser->in_unbraced_linkage_specification_p;
21393   parser->in_unbraced_linkage_specification_p = false;
21394
21395   /* Look for more parameters.  */
21396   while (true)
21397     {
21398       cp_parameter_declarator *parameter;
21399       tree decl = error_mark_node;
21400       bool parenthesized_p = false;
21401
21402       /* Parse the parameter.  */
21403       parameter
21404         = cp_parser_parameter_declaration (parser,
21405                                            /*template_parm_p=*/false,
21406                                            &parenthesized_p);
21407
21408       /* We don't know yet if the enclosing context is deprecated, so wait
21409          and warn in grokparms if appropriate.  */
21410       deprecated_state = DEPRECATED_SUPPRESS;
21411
21412       if (parameter)
21413         {
21414           decl = grokdeclarator (parameter->declarator,
21415                                  &parameter->decl_specifiers,
21416                                  PARM,
21417                                  parameter->default_argument != NULL_TREE,
21418                                  &parameter->decl_specifiers.attributes);
21419           if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21420             DECL_SOURCE_LOCATION (decl) = parameter->loc;
21421         }
21422
21423       deprecated_state = DEPRECATED_NORMAL;
21424
21425       /* If a parse error occurred parsing the parameter declaration,
21426          then the entire parameter-declaration-list is erroneous.  */
21427       if (decl == error_mark_node)
21428         {
21429           *is_error = true;
21430           parameters = error_mark_node;
21431           break;
21432         }
21433
21434       if (parameter->decl_specifiers.attributes)
21435         cplus_decl_attributes (&decl,
21436                                parameter->decl_specifiers.attributes,
21437                                0);
21438       if (DECL_NAME (decl))
21439         decl = pushdecl (decl);
21440
21441       if (decl != error_mark_node)
21442         {
21443           retrofit_lang_decl (decl);
21444           DECL_PARM_INDEX (decl) = ++index;
21445           DECL_PARM_LEVEL (decl) = function_parm_depth ();
21446         }
21447
21448       /* Add the new parameter to the list.  */
21449       *tail = build_tree_list (parameter->default_argument, decl);
21450       tail = &TREE_CHAIN (*tail);
21451
21452       /* Peek at the next token.  */
21453       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21454           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21455           /* These are for Objective-C++ */
21456           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21457           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21458         /* The parameter-declaration-list is complete.  */
21459         break;
21460       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21461         {
21462           cp_token *token;
21463
21464           /* Peek at the next token.  */
21465           token = cp_lexer_peek_nth_token (parser->lexer, 2);
21466           /* If it's an ellipsis, then the list is complete.  */
21467           if (token->type == CPP_ELLIPSIS)
21468             break;
21469           /* Otherwise, there must be more parameters.  Consume the
21470              `,'.  */
21471           cp_lexer_consume_token (parser->lexer);
21472           /* When parsing something like:
21473
21474                 int i(float f, double d)
21475
21476              we can tell after seeing the declaration for "f" that we
21477              are not looking at an initialization of a variable "i",
21478              but rather at the declaration of a function "i".
21479
21480              Due to the fact that the parsing of template arguments
21481              (as specified to a template-id) requires backtracking we
21482              cannot use this technique when inside a template argument
21483              list.  */
21484           if (!parser->in_template_argument_list_p
21485               && !parser->in_type_id_in_expr_p
21486               && cp_parser_uncommitted_to_tentative_parse_p (parser)
21487               /* However, a parameter-declaration of the form
21488                  "float(f)" (which is a valid declaration of a
21489                  parameter "f") can also be interpreted as an
21490                  expression (the conversion of "f" to "float").  */
21491               && !parenthesized_p)
21492             cp_parser_commit_to_tentative_parse (parser);
21493         }
21494       else
21495         {
21496           cp_parser_error (parser, "expected %<,%> or %<...%>");
21497           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21498             cp_parser_skip_to_closing_parenthesis (parser,
21499                                                    /*recovering=*/true,
21500                                                    /*or_comma=*/false,
21501                                                    /*consume_paren=*/false);
21502           break;
21503         }
21504     }
21505
21506   parser->in_unbraced_linkage_specification_p
21507     = saved_in_unbraced_linkage_specification_p;
21508
21509   /* Reset implicit_template_scope if we are about to leave the function
21510      parameter list that introduced it.  Note that for out-of-line member
21511      definitions, there will be one or more class scopes before we get to
21512      the template parameter scope.  */
21513
21514   if (cp_binding_level *its = parser->implicit_template_scope)
21515     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21516       {
21517         while (maybe_its->kind == sk_class)
21518           maybe_its = maybe_its->level_chain;
21519         if (maybe_its == its)
21520           {
21521             parser->implicit_template_parms = 0;
21522             parser->implicit_template_scope = 0;
21523           }
21524       }
21525
21526   return parameters;
21527 }
21528
21529 /* Parse a parameter declaration.
21530
21531    parameter-declaration:
21532      decl-specifier-seq ... [opt] declarator
21533      decl-specifier-seq declarator = assignment-expression
21534      decl-specifier-seq ... [opt] abstract-declarator [opt]
21535      decl-specifier-seq abstract-declarator [opt] = assignment-expression
21536
21537    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21538    declares a template parameter.  (In that case, a non-nested `>'
21539    token encountered during the parsing of the assignment-expression
21540    is not interpreted as a greater-than operator.)
21541
21542    Returns a representation of the parameter, or NULL if an error
21543    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21544    true iff the declarator is of the form "(p)".  */
21545
21546 static cp_parameter_declarator *
21547 cp_parser_parameter_declaration (cp_parser *parser,
21548                                  bool template_parm_p,
21549                                  bool *parenthesized_p)
21550 {
21551   int declares_class_or_enum;
21552   cp_decl_specifier_seq decl_specifiers;
21553   cp_declarator *declarator;
21554   tree default_argument;
21555   cp_token *token = NULL, *declarator_token_start = NULL;
21556   const char *saved_message;
21557   bool template_parameter_pack_p = false;
21558
21559   /* In a template parameter, `>' is not an operator.
21560
21561      [temp.param]
21562
21563      When parsing a default template-argument for a non-type
21564      template-parameter, the first non-nested `>' is taken as the end
21565      of the template parameter-list rather than a greater-than
21566      operator.  */
21567
21568   /* Type definitions may not appear in parameter types.  */
21569   saved_message = parser->type_definition_forbidden_message;
21570   parser->type_definition_forbidden_message
21571     = G_("types may not be defined in parameter types");
21572
21573   int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21574                            TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21575                                             (current_template_parms)) : 0);
21576
21577   /* Parse the declaration-specifiers.  */
21578   cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21579   cp_parser_decl_specifier_seq (parser,
21580                                 CP_PARSER_FLAGS_NONE,
21581                                 &decl_specifiers,
21582                                 &declares_class_or_enum);
21583
21584   /* Complain about missing 'typename' or other invalid type names.  */
21585   if (!decl_specifiers.any_type_specifiers_p
21586       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21587     decl_specifiers.type = error_mark_node;
21588
21589   /* If an error occurred, there's no reason to attempt to parse the
21590      rest of the declaration.  */
21591   if (cp_parser_error_occurred (parser))
21592     {
21593       parser->type_definition_forbidden_message = saved_message;
21594       return NULL;
21595     }
21596
21597   /* Peek at the next token.  */
21598   token = cp_lexer_peek_token (parser->lexer);
21599
21600   /* If the next token is a `)', `,', `=', `>', or `...', then there
21601      is no declarator. However, when variadic templates are enabled,
21602      there may be a declarator following `...'.  */
21603   if (token->type == CPP_CLOSE_PAREN
21604       || token->type == CPP_COMMA
21605       || token->type == CPP_EQ
21606       || token->type == CPP_GREATER)
21607     {
21608       declarator = NULL;
21609       if (parenthesized_p)
21610         *parenthesized_p = false;
21611     }
21612   /* Otherwise, there should be a declarator.  */
21613   else
21614     {
21615       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21616       parser->default_arg_ok_p = false;
21617
21618       /* After seeing a decl-specifier-seq, if the next token is not a
21619          "(", there is no possibility that the code is a valid
21620          expression.  Therefore, if parsing tentatively, we commit at
21621          this point.  */
21622       if (!parser->in_template_argument_list_p
21623           /* In an expression context, having seen:
21624
21625                (int((char ...
21626
21627              we cannot be sure whether we are looking at a
21628              function-type (taking a "char" as a parameter) or a cast
21629              of some object of type "char" to "int".  */
21630           && !parser->in_type_id_in_expr_p
21631           && cp_parser_uncommitted_to_tentative_parse_p (parser)
21632           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21633           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21634         cp_parser_commit_to_tentative_parse (parser);
21635       /* Parse the declarator.  */
21636       declarator_token_start = token;
21637       declarator = cp_parser_declarator (parser,
21638                                          CP_PARSER_DECLARATOR_EITHER,
21639                                          /*ctor_dtor_or_conv_p=*/NULL,
21640                                          parenthesized_p,
21641                                          /*member_p=*/false,
21642                                          /*friend_p=*/false);
21643       parser->default_arg_ok_p = saved_default_arg_ok_p;
21644       /* After the declarator, allow more attributes.  */
21645       decl_specifiers.attributes
21646         = attr_chainon (decl_specifiers.attributes,
21647                         cp_parser_attributes_opt (parser));
21648
21649       /* If the declarator is a template parameter pack, remember that and
21650          clear the flag in the declarator itself so we don't get errors
21651          from grokdeclarator.  */
21652       if (template_parm_p && declarator && declarator->parameter_pack_p)
21653         {
21654           declarator->parameter_pack_p = false;
21655           template_parameter_pack_p = true;
21656         }
21657     }
21658
21659   /* If the next token is an ellipsis, and we have not seen a declarator
21660      name, and if either the type of the declarator contains parameter
21661      packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21662      for, eg, abbreviated integral type names), then we actually have a
21663      parameter pack expansion expression. Otherwise, leave the ellipsis
21664      for a C-style variadic function. */
21665   token = cp_lexer_peek_token (parser->lexer);
21666
21667   /* If a function parameter pack was specified and an implicit template
21668      parameter was introduced during cp_parser_parameter_declaration,
21669      change any implicit parameters introduced into packs.  */
21670   if (parser->implicit_template_parms
21671       && ((token->type == CPP_ELLIPSIS
21672            && declarator_can_be_parameter_pack (declarator))
21673           || (declarator && declarator->parameter_pack_p)))
21674     {
21675       int latest_template_parm_idx = TREE_VEC_LENGTH
21676         (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21677
21678       if (latest_template_parm_idx != template_parm_idx)
21679         decl_specifiers.type = convert_generic_types_to_packs
21680           (decl_specifiers.type,
21681            template_parm_idx, latest_template_parm_idx);
21682     }
21683
21684   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21685     {
21686       tree type = decl_specifiers.type;
21687
21688       if (type && DECL_P (type))
21689         type = TREE_TYPE (type);
21690
21691       if (((type
21692             && TREE_CODE (type) != TYPE_PACK_EXPANSION
21693             && (template_parm_p || uses_parameter_packs (type)))
21694            || (!type && template_parm_p))
21695           && declarator_can_be_parameter_pack (declarator))
21696         {
21697           /* Consume the `...'. */
21698           cp_lexer_consume_token (parser->lexer);
21699           maybe_warn_variadic_templates ();
21700           
21701           /* Build a pack expansion type */
21702           if (template_parm_p)
21703             template_parameter_pack_p = true;
21704           else if (declarator)
21705             declarator->parameter_pack_p = true;
21706           else
21707             decl_specifiers.type = make_pack_expansion (type);
21708         }
21709     }
21710
21711   /* The restriction on defining new types applies only to the type
21712      of the parameter, not to the default argument.  */
21713   parser->type_definition_forbidden_message = saved_message;
21714
21715   /* If the next token is `=', then process a default argument.  */
21716   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21717     {
21718       tree type = decl_specifiers.type;
21719       token = cp_lexer_peek_token (parser->lexer);
21720       /* If we are defining a class, then the tokens that make up the
21721          default argument must be saved and processed later.  */
21722       if (!template_parm_p && at_class_scope_p ()
21723           && TYPE_BEING_DEFINED (current_class_type)
21724           && !LAMBDA_TYPE_P (current_class_type))
21725         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21726
21727       // A constrained-type-specifier may declare a type template-parameter.
21728       else if (declares_constrained_type_template_parameter (type))
21729         default_argument
21730           = cp_parser_default_type_template_argument (parser);
21731
21732       // A constrained-type-specifier may declare a template-template-parameter.
21733       else if (declares_constrained_template_template_parameter (type))
21734         default_argument
21735           = cp_parser_default_template_template_argument (parser);
21736
21737       /* Outside of a class definition, we can just parse the
21738          assignment-expression.  */
21739       else
21740         default_argument
21741           = cp_parser_default_argument (parser, template_parm_p);
21742
21743       if (!parser->default_arg_ok_p)
21744         {
21745           permerror (token->location,
21746                      "default arguments are only "
21747                      "permitted for function parameters");
21748         }
21749       else if ((declarator && declarator->parameter_pack_p)
21750                || template_parameter_pack_p
21751                || (decl_specifiers.type
21752                    && PACK_EXPANSION_P (decl_specifiers.type)))
21753         {
21754           /* Find the name of the parameter pack.  */     
21755           cp_declarator *id_declarator = declarator;
21756           while (id_declarator && id_declarator->kind != cdk_id)
21757             id_declarator = id_declarator->declarator;
21758           
21759           if (id_declarator && id_declarator->kind == cdk_id)
21760             error_at (declarator_token_start->location,
21761                       template_parm_p
21762                       ? G_("template parameter pack %qD "
21763                            "cannot have a default argument")
21764                       : G_("parameter pack %qD cannot have "
21765                            "a default argument"),
21766                       id_declarator->u.id.unqualified_name);
21767           else
21768             error_at (declarator_token_start->location,
21769                       template_parm_p
21770                       ? G_("template parameter pack cannot have "
21771                            "a default argument")
21772                       : G_("parameter pack cannot have a "
21773                            "default argument"));
21774
21775           default_argument = NULL_TREE;
21776         }
21777     }
21778   else
21779     default_argument = NULL_TREE;
21780
21781   /* Generate a location for the parameter, ranging from the start of the
21782      initial token to the end of the final token (using input_location for
21783      the latter, set up by cp_lexer_set_source_position_from_token when
21784      consuming tokens).
21785
21786      If we have a identifier, then use it for the caret location, e.g.
21787
21788        extern int callee (int one, int (*two)(int, int), float three);
21789                                    ~~~~~~^~~~~~~~~~~~~~
21790
21791      otherwise, reuse the start location for the caret location e.g.:
21792
21793        extern int callee (int one, int (*)(int, int), float three);
21794                                    ^~~~~~~~~~~~~~~~~
21795
21796   */
21797   location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21798                           ? declarator->id_loc
21799                           : decl_spec_token_start->location);
21800   location_t param_loc = make_location (caret_loc,
21801                                         decl_spec_token_start->location,
21802                                         input_location);
21803
21804   return make_parameter_declarator (&decl_specifiers,
21805                                     declarator,
21806                                     default_argument,
21807                                     param_loc,
21808                                     template_parameter_pack_p);
21809 }
21810
21811 /* Parse a default argument and return it.
21812
21813    TEMPLATE_PARM_P is true if this is a default argument for a
21814    non-type template parameter.  */
21815 static tree
21816 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21817 {
21818   tree default_argument = NULL_TREE;
21819   bool saved_greater_than_is_operator_p;
21820   bool saved_local_variables_forbidden_p;
21821   bool non_constant_p, is_direct_init;
21822
21823   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21824      set correctly.  */
21825   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21826   parser->greater_than_is_operator_p = !template_parm_p;
21827   /* Local variable names (and the `this' keyword) may not
21828      appear in a default argument.  */
21829   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21830   parser->local_variables_forbidden_p = true;
21831   /* Parse the assignment-expression.  */
21832   if (template_parm_p)
21833     push_deferring_access_checks (dk_no_deferred);
21834   tree saved_class_ptr = NULL_TREE;
21835   tree saved_class_ref = NULL_TREE;
21836   /* The "this" pointer is not valid in a default argument.  */
21837   if (cfun)
21838     {
21839       saved_class_ptr = current_class_ptr;
21840       cp_function_chain->x_current_class_ptr = NULL_TREE;
21841       saved_class_ref = current_class_ref;
21842       cp_function_chain->x_current_class_ref = NULL_TREE;
21843     }
21844   default_argument
21845     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21846   /* Restore the "this" pointer.  */
21847   if (cfun)
21848     {
21849       cp_function_chain->x_current_class_ptr = saved_class_ptr;
21850       cp_function_chain->x_current_class_ref = saved_class_ref;
21851     }
21852   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21853     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21854   if (template_parm_p)
21855     pop_deferring_access_checks ();
21856   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21857   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21858
21859   return default_argument;
21860 }
21861
21862 /* Parse a function-body.
21863
21864    function-body:
21865      compound_statement  */
21866
21867 static void
21868 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21869 {
21870   cp_parser_compound_statement (parser, NULL, (in_function_try_block
21871                                                ? BCS_TRY_BLOCK : BCS_NORMAL),
21872                                 true);
21873 }
21874
21875 /* Parse a ctor-initializer-opt followed by a function-body.  Return
21876    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
21877    is true we are parsing a function-try-block.  */
21878
21879 static void
21880 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21881                                                   bool in_function_try_block)
21882 {
21883   tree body, list;
21884   const bool check_body_p =
21885      DECL_CONSTRUCTOR_P (current_function_decl)
21886      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21887   tree last = NULL;
21888
21889   /* Begin the function body.  */
21890   body = begin_function_body ();
21891   /* Parse the optional ctor-initializer.  */
21892   cp_parser_ctor_initializer_opt (parser);
21893
21894   /* If we're parsing a constexpr constructor definition, we need
21895      to check that the constructor body is indeed empty.  However,
21896      before we get to cp_parser_function_body lot of junk has been
21897      generated, so we can't just check that we have an empty block.
21898      Rather we take a snapshot of the outermost block, and check whether
21899      cp_parser_function_body changed its state.  */
21900   if (check_body_p)
21901     {
21902       list = cur_stmt_list;
21903       if (STATEMENT_LIST_TAIL (list))
21904         last = STATEMENT_LIST_TAIL (list)->stmt;
21905     }
21906   /* Parse the function-body.  */
21907   cp_parser_function_body (parser, in_function_try_block);
21908   if (check_body_p)
21909     check_constexpr_ctor_body (last, list, /*complain=*/true);
21910   /* Finish the function body.  */
21911   finish_function_body (body);
21912 }
21913
21914 /* Parse an initializer.
21915
21916    initializer:
21917      = initializer-clause
21918      ( expression-list )
21919
21920    Returns an expression representing the initializer.  If no
21921    initializer is present, NULL_TREE is returned.
21922
21923    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21924    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
21925    set to TRUE if there is no initializer present.  If there is an
21926    initializer, and it is not a constant-expression, *NON_CONSTANT_P
21927    is set to true; otherwise it is set to false.  */
21928
21929 static tree
21930 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21931                        bool* non_constant_p, bool subexpression_p)
21932 {
21933   cp_token *token;
21934   tree init;
21935
21936   /* Peek at the next token.  */
21937   token = cp_lexer_peek_token (parser->lexer);
21938
21939   /* Let our caller know whether or not this initializer was
21940      parenthesized.  */
21941   *is_direct_init = (token->type != CPP_EQ);
21942   /* Assume that the initializer is constant.  */
21943   *non_constant_p = false;
21944
21945   if (token->type == CPP_EQ)
21946     {
21947       /* Consume the `='.  */
21948       cp_lexer_consume_token (parser->lexer);
21949       /* Parse the initializer-clause.  */
21950       init = cp_parser_initializer_clause (parser, non_constant_p);
21951     }
21952   else if (token->type == CPP_OPEN_PAREN)
21953     {
21954       vec<tree, va_gc> *vec;
21955       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21956                                                      /*cast_p=*/false,
21957                                                      /*allow_expansion_p=*/true,
21958                                                      non_constant_p);
21959       if (vec == NULL)
21960         return error_mark_node;
21961       init = build_tree_list_vec (vec);
21962       release_tree_vector (vec);
21963     }
21964   else if (token->type == CPP_OPEN_BRACE)
21965     {
21966       cp_lexer_set_source_position (parser->lexer);
21967       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21968       init = cp_parser_braced_list (parser, non_constant_p);
21969       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21970     }
21971   else
21972     {
21973       /* Anything else is an error.  */
21974       cp_parser_error (parser, "expected initializer");
21975       init = error_mark_node;
21976     }
21977
21978   if (!subexpression_p && check_for_bare_parameter_packs (init))
21979     init = error_mark_node;
21980
21981   return init;
21982 }
21983
21984 /* Parse an initializer-clause.
21985
21986    initializer-clause:
21987      assignment-expression
21988      braced-init-list
21989
21990    Returns an expression representing the initializer.
21991
21992    If the `assignment-expression' production is used the value
21993    returned is simply a representation for the expression.
21994
21995    Otherwise, calls cp_parser_braced_list.  */
21996
21997 static cp_expr
21998 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21999 {
22000   cp_expr initializer;
22001
22002   /* Assume the expression is constant.  */
22003   *non_constant_p = false;
22004
22005   /* If it is not a `{', then we are looking at an
22006      assignment-expression.  */
22007   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22008     {
22009       initializer
22010         = cp_parser_constant_expression (parser,
22011                                         /*allow_non_constant_p=*/true,
22012                                         non_constant_p);
22013     }
22014   else
22015     initializer = cp_parser_braced_list (parser, non_constant_p);
22016
22017   return initializer;
22018 }
22019
22020 /* Parse a brace-enclosed initializer list.
22021
22022    braced-init-list:
22023      { initializer-list , [opt] }
22024      { designated-initializer-list , [opt] }
22025      { }
22026
22027    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
22028    the elements of the initializer-list (or NULL, if the last
22029    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
22030    NULL_TREE.  There is no way to detect whether or not the optional
22031    trailing `,' was provided.  NON_CONSTANT_P is as for
22032    cp_parser_initializer.  */     
22033
22034 static cp_expr
22035 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22036 {
22037   tree initializer;
22038   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22039
22040   /* Consume the `{' token.  */
22041   matching_braces braces;
22042   braces.require_open (parser);
22043   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
22044   initializer = make_node (CONSTRUCTOR);
22045   /* If it's not a `}', then there is a non-trivial initializer.  */
22046   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22047     {
22048       /* Parse the initializer list.  */
22049       CONSTRUCTOR_ELTS (initializer)
22050         = cp_parser_initializer_list (parser, non_constant_p);
22051       /* A trailing `,' token is allowed.  */
22052       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22053         cp_lexer_consume_token (parser->lexer);
22054     }
22055   else
22056     *non_constant_p = false;
22057   /* Now, there should be a trailing `}'.  */
22058   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22059   braces.require_close (parser);
22060   TREE_TYPE (initializer) = init_list_type_node;
22061
22062   cp_expr result (initializer);
22063   /* Build a location of the form:
22064        { ... }
22065        ^~~~~~~
22066      with caret==start at the open brace, finish at the close brace.  */
22067   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22068   result.set_location (combined_loc);
22069   return result;
22070 }
22071
22072 /* Consume tokens up to, and including, the next non-nested closing `]'.
22073    Returns true iff we found a closing `]'.  */
22074
22075 static bool
22076 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22077 {
22078   unsigned square_depth = 0;
22079
22080   while (true)
22081     {
22082       cp_token * token = cp_lexer_peek_token (parser->lexer);
22083
22084       switch (token->type)
22085         {
22086         case CPP_EOF:
22087         case CPP_PRAGMA_EOL:
22088           /* If we've run out of tokens, then there is no closing `]'.  */
22089           return false;
22090
22091         case CPP_OPEN_SQUARE:
22092           ++square_depth;
22093           break;
22094
22095         case CPP_CLOSE_SQUARE:
22096           if (!square_depth--)
22097             {
22098               cp_lexer_consume_token (parser->lexer);
22099               return true;
22100             }
22101           break;
22102
22103         default:
22104           break;
22105         }
22106
22107       /* Consume the token.  */
22108       cp_lexer_consume_token (parser->lexer);
22109     }
22110 }
22111
22112 /* Return true if we are looking at an array-designator, false otherwise.  */
22113
22114 static bool
22115 cp_parser_array_designator_p (cp_parser *parser)
22116 {
22117   /* Consume the `['.  */
22118   cp_lexer_consume_token (parser->lexer);
22119
22120   cp_lexer_save_tokens (parser->lexer);
22121
22122   /* Skip tokens until the next token is a closing square bracket.
22123      If we find the closing `]', and the next token is a `=', then
22124      we are looking at an array designator.  */
22125   bool array_designator_p
22126     = (cp_parser_skip_to_closing_square_bracket (parser)
22127        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22128   
22129   /* Roll back the tokens we skipped.  */
22130   cp_lexer_rollback_tokens (parser->lexer);
22131
22132   return array_designator_p;
22133 }
22134
22135 /* Parse an initializer-list.
22136
22137    initializer-list:
22138      initializer-clause ... [opt]
22139      initializer-list , initializer-clause ... [opt]
22140
22141    C++2A Extension:
22142
22143    designated-initializer-list:
22144      designated-initializer-clause
22145      designated-initializer-list , designated-initializer-clause
22146
22147    designated-initializer-clause:
22148      designator brace-or-equal-initializer
22149
22150    designator:
22151      . identifier
22152
22153    GNU Extension:
22154
22155    initializer-list:
22156      designation initializer-clause ...[opt]
22157      initializer-list , designation initializer-clause ...[opt]
22158
22159    designation:
22160      . identifier =
22161      identifier :
22162      [ constant-expression ] =
22163
22164    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
22165    for the initializer.  If the INDEX of the elt is non-NULL, it is the
22166    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
22167    as for cp_parser_initializer.  */
22168
22169 static vec<constructor_elt, va_gc> *
22170 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22171 {
22172   vec<constructor_elt, va_gc> *v = NULL;
22173   bool first_p = true;
22174   tree first_designator = NULL_TREE;
22175
22176   /* Assume all of the expressions are constant.  */
22177   *non_constant_p = false;
22178
22179   /* Parse the rest of the list.  */
22180   while (true)
22181     {
22182       cp_token *token;
22183       tree designator;
22184       tree initializer;
22185       bool clause_non_constant_p;
22186       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22187
22188       /* Handle the C++2A syntax, '. id ='.  */
22189       if ((cxx_dialect >= cxx2a
22190            || cp_parser_allow_gnu_extensions_p (parser))
22191           && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22192           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22193           && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22194               || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22195                   == CPP_OPEN_BRACE)))
22196         {
22197           if (cxx_dialect < cxx2a)
22198             pedwarn (loc, OPT_Wpedantic,
22199                      "C++ designated initializers only available with "
22200                      "-std=c++2a or -std=gnu++2a");
22201           /* Consume the `.'.  */
22202           cp_lexer_consume_token (parser->lexer);
22203           /* Consume the identifier.  */
22204           designator = cp_lexer_consume_token (parser->lexer)->u.value;
22205           if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22206             /* Consume the `='.  */
22207             cp_lexer_consume_token (parser->lexer);
22208         }
22209       /* Also, if the next token is an identifier and the following one is a
22210          colon, we are looking at the GNU designated-initializer
22211          syntax.  */
22212       else if (cp_parser_allow_gnu_extensions_p (parser)
22213                && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22214                && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22215                    == CPP_COLON))
22216         {
22217           /* Warn the user that they are using an extension.  */
22218           pedwarn (loc, OPT_Wpedantic,
22219                    "ISO C++ does not allow GNU designated initializers");
22220           /* Consume the identifier.  */
22221           designator = cp_lexer_consume_token (parser->lexer)->u.value;
22222           /* Consume the `:'.  */
22223           cp_lexer_consume_token (parser->lexer);
22224         }
22225       /* Also handle C99 array designators, '[ const ] ='.  */
22226       else if (cp_parser_allow_gnu_extensions_p (parser)
22227                && !c_dialect_objc ()
22228                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22229         {
22230           /* In C++11, [ could start a lambda-introducer.  */
22231           bool non_const = false;
22232
22233           cp_parser_parse_tentatively (parser);
22234
22235           if (!cp_parser_array_designator_p (parser))
22236             {
22237               cp_parser_simulate_error (parser);
22238               designator = NULL_TREE;
22239             }
22240           else
22241             {
22242               designator = cp_parser_constant_expression (parser, true,
22243                                                           &non_const);
22244               cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22245               cp_parser_require (parser, CPP_EQ, RT_EQ);
22246             }
22247
22248           if (!cp_parser_parse_definitely (parser))
22249             designator = NULL_TREE;
22250           else if (non_const
22251                    && (!require_potential_rvalue_constant_expression
22252                        (designator)))
22253             designator = NULL_TREE;
22254           if (designator)
22255             /* Warn the user that they are using an extension.  */
22256             pedwarn (loc, OPT_Wpedantic,
22257                      "ISO C++ does not allow C99 designated initializers");
22258         }
22259       else
22260         designator = NULL_TREE;
22261
22262       if (first_p)
22263         {
22264           first_designator = designator;
22265           first_p = false;
22266         }
22267       else if (cxx_dialect >= cxx2a
22268                && first_designator != error_mark_node
22269                && (!first_designator != !designator))
22270         {
22271           error_at (loc, "either all initializer clauses should be designated "
22272                          "or none of them should be");
22273           first_designator = error_mark_node;
22274         }
22275       else if (cxx_dialect < cxx2a && !first_designator)
22276         first_designator = designator;
22277
22278       /* Parse the initializer.  */
22279       initializer = cp_parser_initializer_clause (parser,
22280                                                   &clause_non_constant_p);
22281       /* If any clause is non-constant, so is the entire initializer.  */
22282       if (clause_non_constant_p)
22283         *non_constant_p = true;
22284
22285       /* If we have an ellipsis, this is an initializer pack
22286          expansion.  */
22287       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22288         {
22289           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22290
22291           /* Consume the `...'.  */
22292           cp_lexer_consume_token (parser->lexer);
22293
22294           if (designator && cxx_dialect >= cxx2a)
22295             error_at (loc,
22296                       "%<...%> not allowed in designated initializer list");
22297
22298           /* Turn the initializer into an initializer expansion.  */
22299           initializer = make_pack_expansion (initializer);
22300         }
22301
22302       /* Add it to the vector.  */
22303       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22304
22305       /* If the next token is not a comma, we have reached the end of
22306          the list.  */
22307       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22308         break;
22309
22310       /* Peek at the next token.  */
22311       token = cp_lexer_peek_nth_token (parser->lexer, 2);
22312       /* If the next token is a `}', then we're still done.  An
22313          initializer-clause can have a trailing `,' after the
22314          initializer-list and before the closing `}'.  */
22315       if (token->type == CPP_CLOSE_BRACE)
22316         break;
22317
22318       /* Consume the `,' token.  */
22319       cp_lexer_consume_token (parser->lexer);
22320     }
22321
22322   /* The same identifier shall not appear in multiple designators
22323      of a designated-initializer-list.  */
22324   if (first_designator)
22325     {
22326       unsigned int i;
22327       tree designator, val;
22328       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22329         if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22330           {
22331             if (IDENTIFIER_MARKED (designator))
22332               {
22333                 error_at (EXPR_LOC_OR_LOC (val, input_location),
22334                           "%<.%s%> designator used multiple times in "
22335                           "the same initializer list",
22336                           IDENTIFIER_POINTER (designator));
22337                 (*v)[i].index = NULL_TREE;
22338               }
22339             else
22340               IDENTIFIER_MARKED (designator) = 1;
22341           }
22342       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22343         if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22344           IDENTIFIER_MARKED (designator) = 0;
22345     }
22346
22347   return v;
22348 }
22349
22350 /* Classes [gram.class] */
22351
22352 /* Parse a class-name.
22353
22354    class-name:
22355      identifier
22356      template-id
22357
22358    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22359    to indicate that names looked up in dependent types should be
22360    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
22361    keyword has been used to indicate that the name that appears next
22362    is a template.  TAG_TYPE indicates the explicit tag given before
22363    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
22364    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
22365    is the class being defined in a class-head.  If ENUM_OK is TRUE,
22366    enum-names are also accepted.
22367
22368    Returns the TYPE_DECL representing the class.  */
22369
22370 static tree
22371 cp_parser_class_name (cp_parser *parser,
22372                       bool typename_keyword_p,
22373                       bool template_keyword_p,
22374                       enum tag_types tag_type,
22375                       bool check_dependency_p,
22376                       bool class_head_p,
22377                       bool is_declaration,
22378                       bool enum_ok)
22379 {
22380   tree decl;
22381   tree scope;
22382   bool typename_p;
22383   cp_token *token;
22384   tree identifier = NULL_TREE;
22385
22386   /* All class-names start with an identifier.  */
22387   token = cp_lexer_peek_token (parser->lexer);
22388   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22389     {
22390       cp_parser_error (parser, "expected class-name");
22391       return error_mark_node;
22392     }
22393
22394   /* PARSER->SCOPE can be cleared when parsing the template-arguments
22395      to a template-id, so we save it here.  */
22396   scope = parser->scope;
22397   if (scope == error_mark_node)
22398     return error_mark_node;
22399
22400   /* Any name names a type if we're following the `typename' keyword
22401      in a qualified name where the enclosing scope is type-dependent.  */
22402   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22403                 && dependent_type_p (scope));
22404   /* Handle the common case (an identifier, but not a template-id)
22405      efficiently.  */
22406   if (token->type == CPP_NAME
22407       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22408     {
22409       cp_token *identifier_token;
22410       bool ambiguous_p;
22411
22412       /* Look for the identifier.  */
22413       identifier_token = cp_lexer_peek_token (parser->lexer);
22414       ambiguous_p = identifier_token->error_reported;
22415       identifier = cp_parser_identifier (parser);
22416       /* If the next token isn't an identifier, we are certainly not
22417          looking at a class-name.  */
22418       if (identifier == error_mark_node)
22419         decl = error_mark_node;
22420       /* If we know this is a type-name, there's no need to look it
22421          up.  */
22422       else if (typename_p)
22423         decl = identifier;
22424       else
22425         {
22426           tree ambiguous_decls;
22427           /* If we already know that this lookup is ambiguous, then
22428              we've already issued an error message; there's no reason
22429              to check again.  */
22430           if (ambiguous_p)
22431             {
22432               cp_parser_simulate_error (parser);
22433               return error_mark_node;
22434             }
22435           /* If the next token is a `::', then the name must be a type
22436              name.
22437
22438              [basic.lookup.qual]
22439
22440              During the lookup for a name preceding the :: scope
22441              resolution operator, object, function, and enumerator
22442              names are ignored.  */
22443           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22444             tag_type = scope_type;
22445           /* Look up the name.  */
22446           decl = cp_parser_lookup_name (parser, identifier,
22447                                         tag_type,
22448                                         /*is_template=*/false,
22449                                         /*is_namespace=*/false,
22450                                         check_dependency_p,
22451                                         &ambiguous_decls,
22452                                         identifier_token->location);
22453           if (ambiguous_decls)
22454             {
22455               if (cp_parser_parsing_tentatively (parser))
22456                 cp_parser_simulate_error (parser);
22457               return error_mark_node;
22458             }
22459         }
22460     }
22461   else
22462     {
22463       /* Try a template-id.  */
22464       decl = cp_parser_template_id (parser, template_keyword_p,
22465                                     check_dependency_p,
22466                                     tag_type,
22467                                     is_declaration);
22468       if (decl == error_mark_node)
22469         return error_mark_node;
22470     }
22471
22472   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22473
22474   /* If this is a typename, create a TYPENAME_TYPE.  */
22475   if (typename_p && decl != error_mark_node)
22476     {
22477       decl = make_typename_type (scope, decl, typename_type,
22478                                  /*complain=*/tf_error);
22479       if (decl != error_mark_node)
22480         decl = TYPE_NAME (decl);
22481     }
22482
22483   decl = strip_using_decl (decl);
22484
22485   /* Check to see that it is really the name of a class.  */
22486   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22487       && identifier_p (TREE_OPERAND (decl, 0))
22488       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22489     /* Situations like this:
22490
22491          template <typename T> struct A {
22492            typename T::template X<int>::I i;
22493          };
22494
22495        are problematic.  Is `T::template X<int>' a class-name?  The
22496        standard does not seem to be definitive, but there is no other
22497        valid interpretation of the following `::'.  Therefore, those
22498        names are considered class-names.  */
22499     {
22500       decl = make_typename_type (scope, decl, tag_type, tf_error);
22501       if (decl != error_mark_node)
22502         decl = TYPE_NAME (decl);
22503     }
22504   else if (TREE_CODE (decl) != TYPE_DECL
22505            || TREE_TYPE (decl) == error_mark_node
22506            || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22507                 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22508            /* In Objective-C 2.0, a classname followed by '.' starts a
22509               dot-syntax expression, and it's not a type-name.  */
22510            || (c_dialect_objc ()
22511                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
22512                && objc_is_class_name (decl)))
22513     decl = error_mark_node;
22514
22515   if (decl == error_mark_node)
22516     cp_parser_error (parser, "expected class-name");
22517   else if (identifier && !parser->scope)
22518     maybe_note_name_used_in_class (identifier, decl);
22519
22520   return decl;
22521 }
22522
22523 /* Parse a class-specifier.
22524
22525    class-specifier:
22526      class-head { member-specification [opt] }
22527
22528    Returns the TREE_TYPE representing the class.  */
22529
22530 static tree
22531 cp_parser_class_specifier_1 (cp_parser* parser)
22532 {
22533   tree type;
22534   tree attributes = NULL_TREE;
22535   bool nested_name_specifier_p;
22536   unsigned saved_num_template_parameter_lists;
22537   bool saved_in_function_body;
22538   unsigned char in_statement;
22539   bool in_switch_statement_p;
22540   bool saved_in_unbraced_linkage_specification_p;
22541   tree old_scope = NULL_TREE;
22542   tree scope = NULL_TREE;
22543   cp_token *closing_brace;
22544
22545   push_deferring_access_checks (dk_no_deferred);
22546
22547   /* Parse the class-head.  */
22548   type = cp_parser_class_head (parser,
22549                                &nested_name_specifier_p);
22550   /* If the class-head was a semantic disaster, skip the entire body
22551      of the class.  */
22552   if (!type)
22553     {
22554       cp_parser_skip_to_end_of_block_or_statement (parser);
22555       pop_deferring_access_checks ();
22556       return error_mark_node;
22557     }
22558
22559   /* Look for the `{'.  */
22560   matching_braces braces;
22561   if (!braces.require_open (parser))
22562     {
22563       pop_deferring_access_checks ();
22564       return error_mark_node;
22565     }
22566
22567   cp_ensure_no_omp_declare_simd (parser);
22568   cp_ensure_no_oacc_routine (parser);
22569
22570   /* Issue an error message if type-definitions are forbidden here.  */
22571   bool type_definition_ok_p = cp_parser_check_type_definition (parser);
22572   /* Remember that we are defining one more class.  */
22573   ++parser->num_classes_being_defined;
22574   /* Inside the class, surrounding template-parameter-lists do not
22575      apply.  */
22576   saved_num_template_parameter_lists
22577     = parser->num_template_parameter_lists;
22578   parser->num_template_parameter_lists = 0;
22579   /* We are not in a function body.  */
22580   saved_in_function_body = parser->in_function_body;
22581   parser->in_function_body = false;
22582   /* Or in a loop.  */
22583   in_statement = parser->in_statement;
22584   parser->in_statement = 0;
22585   /* Or in a switch.  */
22586   in_switch_statement_p = parser->in_switch_statement_p;
22587   parser->in_switch_statement_p = false;
22588   /* We are not immediately inside an extern "lang" block.  */
22589   saved_in_unbraced_linkage_specification_p
22590     = parser->in_unbraced_linkage_specification_p;
22591   parser->in_unbraced_linkage_specification_p = false;
22592
22593   // Associate constraints with the type.
22594   if (flag_concepts)
22595     type = associate_classtype_constraints (type);
22596
22597   /* Start the class.  */
22598   if (nested_name_specifier_p)
22599     {
22600       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22601       old_scope = push_inner_scope (scope);
22602     }
22603   type = begin_class_definition (type);
22604
22605   if (type == error_mark_node)
22606     /* If the type is erroneous, skip the entire body of the class.  */
22607     cp_parser_skip_to_closing_brace (parser);
22608   else
22609     /* Parse the member-specification.  */
22610     cp_parser_member_specification_opt (parser);
22611
22612   /* Look for the trailing `}'.  */
22613   closing_brace = braces.require_close (parser);
22614   /* Look for trailing attributes to apply to this class.  */
22615   if (cp_parser_allow_gnu_extensions_p (parser))
22616     attributes = cp_parser_gnu_attributes_opt (parser);
22617   if (type != error_mark_node)
22618     type = finish_struct (type, attributes);
22619   if (nested_name_specifier_p)
22620     pop_inner_scope (old_scope, scope);
22621
22622   /* We've finished a type definition.  Check for the common syntax
22623      error of forgetting a semicolon after the definition.  We need to
22624      be careful, as we can't just check for not-a-semicolon and be done
22625      with it; the user might have typed:
22626
22627      class X { } c = ...;
22628      class X { } *p = ...;
22629
22630      and so forth.  Instead, enumerate all the possible tokens that
22631      might follow this production; if we don't see one of them, then
22632      complain and silently insert the semicolon.  */
22633   {
22634     cp_token *token = cp_lexer_peek_token (parser->lexer);
22635     bool want_semicolon = true;
22636
22637     if (cp_next_tokens_can_be_std_attribute_p (parser))
22638       /* Don't try to parse c++11 attributes here.  As per the
22639          grammar, that should be a task for
22640          cp_parser_decl_specifier_seq.  */
22641       want_semicolon = false;
22642
22643     switch (token->type)
22644       {
22645       case CPP_NAME:
22646       case CPP_SEMICOLON:
22647       case CPP_MULT:
22648       case CPP_AND:
22649       case CPP_OPEN_PAREN:
22650       case CPP_CLOSE_PAREN:
22651       case CPP_COMMA:
22652         want_semicolon = false;
22653         break;
22654
22655         /* While it's legal for type qualifiers and storage class
22656            specifiers to follow type definitions in the grammar, only
22657            compiler testsuites contain code like that.  Assume that if
22658            we see such code, then what we're really seeing is a case
22659            like:
22660
22661            class X { }
22662            const <type> var = ...;
22663
22664            or
22665
22666            class Y { }
22667            static <type> func (...) ...
22668
22669            i.e. the qualifier or specifier applies to the next
22670            declaration.  To do so, however, we need to look ahead one
22671            more token to see if *that* token is a type specifier.
22672
22673            This code could be improved to handle:
22674
22675            class Z { }
22676            static const <type> var = ...;  */
22677       case CPP_KEYWORD:
22678         if (keyword_is_decl_specifier (token->keyword))
22679           {
22680             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22681
22682             /* Handling user-defined types here would be nice, but very
22683                tricky.  */
22684             want_semicolon
22685               = (lookahead->type == CPP_KEYWORD
22686                  && keyword_begins_type_specifier (lookahead->keyword));
22687           }
22688         break;
22689       default:
22690         break;
22691       }
22692
22693     /* If we don't have a type, then something is very wrong and we
22694        shouldn't try to do anything clever.  Likewise for not seeing the
22695        closing brace.  */
22696     if (closing_brace && TYPE_P (type) && want_semicolon)
22697       {
22698         /* Locate the closing brace.  */
22699         cp_token_position prev
22700           = cp_lexer_previous_token_position (parser->lexer);
22701         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22702         location_t loc = prev_token->location;
22703
22704         /* We want to suggest insertion of a ';' immediately *after* the
22705            closing brace, so, if we can, offset the location by 1 column.  */
22706         location_t next_loc = loc;
22707         if (!linemap_location_from_macro_expansion_p (line_table, loc))
22708           next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22709
22710         rich_location richloc (line_table, next_loc);
22711
22712         /* If we successfully offset the location, suggest the fix-it.  */
22713         if (next_loc != loc)
22714           richloc.add_fixit_insert_before (next_loc, ";");
22715
22716         if (CLASSTYPE_DECLARED_CLASS (type))
22717           error_at (&richloc,
22718                     "expected %<;%> after class definition");
22719         else if (TREE_CODE (type) == RECORD_TYPE)
22720           error_at (&richloc,
22721                     "expected %<;%> after struct definition");
22722         else if (TREE_CODE (type) == UNION_TYPE)
22723           error_at (&richloc,
22724                     "expected %<;%> after union definition");
22725         else
22726           gcc_unreachable ();
22727
22728         /* Unget one token and smash it to look as though we encountered
22729            a semicolon in the input stream.  */
22730         cp_lexer_set_token_position (parser->lexer, prev);
22731         token = cp_lexer_peek_token (parser->lexer);
22732         token->type = CPP_SEMICOLON;
22733         token->keyword = RID_MAX;
22734       }
22735   }
22736
22737   /* If this class is not itself within the scope of another class,
22738      then we need to parse the bodies of all of the queued function
22739      definitions.  Note that the queued functions defined in a class
22740      are not always processed immediately following the
22741      class-specifier for that class.  Consider:
22742
22743        struct A {
22744          struct B { void f() { sizeof (A); } };
22745        };
22746
22747      If `f' were processed before the processing of `A' were
22748      completed, there would be no way to compute the size of `A'.
22749      Note that the nesting we are interested in here is lexical --
22750      not the semantic nesting given by TYPE_CONTEXT.  In particular,
22751      for:
22752
22753        struct A { struct B; };
22754        struct A::B { void f() { } };
22755
22756      there is no need to delay the parsing of `A::B::f'.  */
22757   if (--parser->num_classes_being_defined == 0)
22758     {
22759       tree decl;
22760       tree class_type = NULL_TREE;
22761       tree pushed_scope = NULL_TREE;
22762       unsigned ix;
22763       cp_default_arg_entry *e;
22764       tree save_ccp, save_ccr;
22765
22766       if (!type_definition_ok_p || any_erroneous_template_args_p (type))
22767         {
22768           /* Skip default arguments, NSDMIs, etc, in order to improve
22769              error recovery (c++/71169, c++/71832).  */
22770           vec_safe_truncate (unparsed_funs_with_default_args, 0);
22771           vec_safe_truncate (unparsed_nsdmis, 0);
22772           vec_safe_truncate (unparsed_classes, 0);
22773           vec_safe_truncate (unparsed_funs_with_definitions, 0);
22774         }
22775
22776       /* In a first pass, parse default arguments to the functions.
22777          Then, in a second pass, parse the bodies of the functions.
22778          This two-phased approach handles cases like:
22779
22780             struct S {
22781               void f() { g(); }
22782               void g(int i = 3);
22783             };
22784
22785          */
22786       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22787         {
22788           decl = e->decl;
22789           /* If there are default arguments that have not yet been processed,
22790              take care of them now.  */
22791           if (class_type != e->class_type)
22792             {
22793               if (pushed_scope)
22794                 pop_scope (pushed_scope);
22795               class_type = e->class_type;
22796               pushed_scope = push_scope (class_type);
22797             }
22798           /* Make sure that any template parameters are in scope.  */
22799           maybe_begin_member_template_processing (decl);
22800           /* Parse the default argument expressions.  */
22801           cp_parser_late_parsing_default_args (parser, decl);
22802           /* Remove any template parameters from the symbol table.  */
22803           maybe_end_member_template_processing ();
22804         }
22805       vec_safe_truncate (unparsed_funs_with_default_args, 0);
22806       /* Now parse any NSDMIs.  */
22807       save_ccp = current_class_ptr;
22808       save_ccr = current_class_ref;
22809       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22810         {
22811           if (class_type != DECL_CONTEXT (decl))
22812             {
22813               if (pushed_scope)
22814                 pop_scope (pushed_scope);
22815               class_type = DECL_CONTEXT (decl);
22816               pushed_scope = push_scope (class_type);
22817             }
22818           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22819           cp_parser_late_parsing_nsdmi (parser, decl);
22820         }
22821       vec_safe_truncate (unparsed_nsdmis, 0);
22822       current_class_ptr = save_ccp;
22823       current_class_ref = save_ccr;
22824       if (pushed_scope)
22825         pop_scope (pushed_scope);
22826
22827       /* Now do some post-NSDMI bookkeeping.  */
22828       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22829         after_nsdmi_defaulted_late_checks (class_type);
22830       vec_safe_truncate (unparsed_classes, 0);
22831       after_nsdmi_defaulted_late_checks (type);
22832
22833       /* Now parse the body of the functions.  */
22834       if (flag_openmp)
22835         {
22836           /* OpenMP UDRs need to be parsed before all other functions.  */
22837           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22838             if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22839               cp_parser_late_parsing_for_member (parser, decl);
22840           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22841             if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22842               cp_parser_late_parsing_for_member (parser, decl);
22843         }
22844       else
22845         FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22846           cp_parser_late_parsing_for_member (parser, decl);
22847       vec_safe_truncate (unparsed_funs_with_definitions, 0);
22848     }
22849   else
22850     vec_safe_push (unparsed_classes, type);
22851
22852   /* Put back any saved access checks.  */
22853   pop_deferring_access_checks ();
22854
22855   /* Restore saved state.  */
22856   parser->in_switch_statement_p = in_switch_statement_p;
22857   parser->in_statement = in_statement;
22858   parser->in_function_body = saved_in_function_body;
22859   parser->num_template_parameter_lists
22860     = saved_num_template_parameter_lists;
22861   parser->in_unbraced_linkage_specification_p
22862     = saved_in_unbraced_linkage_specification_p;
22863
22864   return type;
22865 }
22866
22867 static tree
22868 cp_parser_class_specifier (cp_parser* parser)
22869 {
22870   tree ret;
22871   timevar_push (TV_PARSE_STRUCT);
22872   ret = cp_parser_class_specifier_1 (parser);
22873   timevar_pop (TV_PARSE_STRUCT);
22874   return ret;
22875 }
22876
22877 /* Parse a class-head.
22878
22879    class-head:
22880      class-key identifier [opt] base-clause [opt]
22881      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22882      class-key nested-name-specifier [opt] template-id
22883        base-clause [opt]
22884
22885    class-virt-specifier:
22886      final
22887
22888    GNU Extensions:
22889      class-key attributes identifier [opt] base-clause [opt]
22890      class-key attributes nested-name-specifier identifier base-clause [opt]
22891      class-key attributes nested-name-specifier [opt] template-id
22892        base-clause [opt]
22893
22894    Upon return BASES is initialized to the list of base classes (or
22895    NULL, if there are none) in the same form returned by
22896    cp_parser_base_clause.
22897
22898    Returns the TYPE of the indicated class.  Sets
22899    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22900    involving a nested-name-specifier was used, and FALSE otherwise.
22901
22902    Returns error_mark_node if this is not a class-head.
22903
22904    Returns NULL_TREE if the class-head is syntactically valid, but
22905    semantically invalid in a way that means we should skip the entire
22906    body of the class.  */
22907
22908 static tree
22909 cp_parser_class_head (cp_parser* parser,
22910                       bool* nested_name_specifier_p)
22911 {
22912   tree nested_name_specifier;
22913   enum tag_types class_key;
22914   tree id = NULL_TREE;
22915   tree type = NULL_TREE;
22916   tree attributes;
22917   tree bases;
22918   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22919   bool template_id_p = false;
22920   bool qualified_p = false;
22921   bool invalid_nested_name_p = false;
22922   bool invalid_explicit_specialization_p = false;
22923   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22924   tree pushed_scope = NULL_TREE;
22925   unsigned num_templates;
22926   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22927   /* Assume no nested-name-specifier will be present.  */
22928   *nested_name_specifier_p = false;
22929   /* Assume no template parameter lists will be used in defining the
22930      type.  */
22931   num_templates = 0;
22932   parser->colon_corrects_to_scope_p = false;
22933
22934   /* Look for the class-key.  */
22935   class_key = cp_parser_class_key (parser);
22936   if (class_key == none_type)
22937     return error_mark_node;
22938
22939   location_t class_head_start_location = input_location;
22940
22941   /* Parse the attributes.  */
22942   attributes = cp_parser_attributes_opt (parser);
22943
22944   /* If the next token is `::', that is invalid -- but sometimes
22945      people do try to write:
22946
22947        struct ::S {};
22948
22949      Handle this gracefully by accepting the extra qualifier, and then
22950      issuing an error about it later if this really is a
22951      class-head.  If it turns out just to be an elaborated type
22952      specifier, remain silent.  */
22953   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22954     qualified_p = true;
22955
22956   push_deferring_access_checks (dk_no_check);
22957
22958   /* Determine the name of the class.  Begin by looking for an
22959      optional nested-name-specifier.  */
22960   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22961   nested_name_specifier
22962     = cp_parser_nested_name_specifier_opt (parser,
22963                                            /*typename_keyword_p=*/false,
22964                                            /*check_dependency_p=*/false,
22965                                            /*type_p=*/true,
22966                                            /*is_declaration=*/false);
22967   /* If there was a nested-name-specifier, then there *must* be an
22968      identifier.  */
22969
22970   cp_token *bad_template_keyword = NULL;
22971
22972   if (nested_name_specifier)
22973     {
22974       type_start_token = cp_lexer_peek_token (parser->lexer);
22975       /* Although the grammar says `identifier', it really means
22976          `class-name' or `template-name'.  You are only allowed to
22977          define a class that has already been declared with this
22978          syntax.
22979
22980          The proposed resolution for Core Issue 180 says that wherever
22981          you see `class T::X' you should treat `X' as a type-name.
22982
22983          It is OK to define an inaccessible class; for example:
22984
22985            class A { class B; };
22986            class A::B {};
22987
22988          We do not know if we will see a class-name, or a
22989          template-name.  We look for a class-name first, in case the
22990          class-name is a template-id; if we looked for the
22991          template-name first we would stop after the template-name.  */
22992       cp_parser_parse_tentatively (parser);
22993       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22994         bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22995       type = cp_parser_class_name (parser,
22996                                    /*typename_keyword_p=*/false,
22997                                    /*template_keyword_p=*/false,
22998                                    class_type,
22999                                    /*check_dependency_p=*/false,
23000                                    /*class_head_p=*/true,
23001                                    /*is_declaration=*/false);
23002       /* If that didn't work, ignore the nested-name-specifier.  */
23003       if (!cp_parser_parse_definitely (parser))
23004         {
23005           invalid_nested_name_p = true;
23006           type_start_token = cp_lexer_peek_token (parser->lexer);
23007           id = cp_parser_identifier (parser);
23008           if (id == error_mark_node)
23009             id = NULL_TREE;
23010         }
23011       /* If we could not find a corresponding TYPE, treat this
23012          declaration like an unqualified declaration.  */
23013       if (type == error_mark_node)
23014         nested_name_specifier = NULL_TREE;
23015       /* Otherwise, count the number of templates used in TYPE and its
23016          containing scopes.  */
23017       else
23018         {
23019           tree scope;
23020
23021           for (scope = TREE_TYPE (type);
23022                scope && TREE_CODE (scope) != NAMESPACE_DECL;
23023                scope = get_containing_scope (scope))
23024             if (TYPE_P (scope)
23025                 && CLASS_TYPE_P (scope)
23026                 && CLASSTYPE_TEMPLATE_INFO (scope)
23027                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
23028                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
23029                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
23030               ++num_templates;
23031         }
23032     }
23033   /* Otherwise, the identifier is optional.  */
23034   else
23035     {
23036       /* We don't know whether what comes next is a template-id,
23037          an identifier, or nothing at all.  */
23038       cp_parser_parse_tentatively (parser);
23039       /* Check for a template-id.  */
23040       type_start_token = cp_lexer_peek_token (parser->lexer);
23041       id = cp_parser_template_id (parser,
23042                                   /*template_keyword_p=*/false,
23043                                   /*check_dependency_p=*/true,
23044                                   class_key,
23045                                   /*is_declaration=*/true);
23046       /* If that didn't work, it could still be an identifier.  */
23047       if (!cp_parser_parse_definitely (parser))
23048         {
23049           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23050             {
23051               type_start_token = cp_lexer_peek_token (parser->lexer);
23052               id = cp_parser_identifier (parser);
23053             }
23054           else
23055             id = NULL_TREE;
23056         }
23057       else
23058         {
23059           template_id_p = true;
23060           ++num_templates;
23061         }
23062     }
23063
23064   pop_deferring_access_checks ();
23065
23066   if (id)
23067     {
23068       cp_parser_check_for_invalid_template_id (parser, id,
23069                                                class_key,
23070                                                type_start_token->location);
23071     }
23072   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23073
23074   /* If it's not a `:' or a `{' then we can't really be looking at a
23075      class-head, since a class-head only appears as part of a
23076      class-specifier.  We have to detect this situation before calling
23077      xref_tag, since that has irreversible side-effects.  */
23078   if (!cp_parser_next_token_starts_class_definition_p (parser))
23079     {
23080       cp_parser_error (parser, "expected %<{%> or %<:%>");
23081       type = error_mark_node;
23082       goto out;
23083     }
23084
23085   /* At this point, we're going ahead with the class-specifier, even
23086      if some other problem occurs.  */
23087   cp_parser_commit_to_tentative_parse (parser);
23088   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23089     {
23090       cp_parser_error (parser,
23091                        "cannot specify %<override%> for a class");
23092       type = error_mark_node;
23093       goto out;
23094     }
23095   /* Issue the error about the overly-qualified name now.  */
23096   if (qualified_p)
23097     {
23098       cp_parser_error (parser,
23099                        "global qualification of class name is invalid");
23100       type = error_mark_node;
23101       goto out;
23102     }
23103   else if (invalid_nested_name_p)
23104     {
23105       cp_parser_error (parser,
23106                        "qualified name does not name a class");
23107       type = error_mark_node;
23108       goto out;
23109     }
23110   else if (nested_name_specifier)
23111     {
23112       tree scope;
23113
23114       if (bad_template_keyword)
23115         /* [temp.names]: in a qualified-id formed by a class-head-name, the
23116            keyword template shall not appear at the top level.  */
23117         pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23118                  "keyword %<template%> not allowed in class-head-name");
23119
23120       /* Reject typedef-names in class heads.  */
23121       if (!DECL_IMPLICIT_TYPEDEF_P (type))
23122         {
23123           error_at (type_start_token->location,
23124                     "invalid class name in declaration of %qD",
23125                     type);
23126           type = NULL_TREE;
23127           goto done;
23128         }
23129
23130       /* Figure out in what scope the declaration is being placed.  */
23131       scope = current_scope ();
23132       /* If that scope does not contain the scope in which the
23133          class was originally declared, the program is invalid.  */
23134       if (scope && !is_ancestor (scope, nested_name_specifier))
23135         {
23136           if (at_namespace_scope_p ())
23137             error_at (type_start_token->location,
23138                       "declaration of %qD in namespace %qD which does not "
23139                       "enclose %qD",
23140                       type, scope, nested_name_specifier);
23141           else
23142             error_at (type_start_token->location,
23143                       "declaration of %qD in %qD which does not enclose %qD",
23144                       type, scope, nested_name_specifier);
23145           type = NULL_TREE;
23146           goto done;
23147         }
23148       /* [dcl.meaning]
23149
23150          A declarator-id shall not be qualified except for the
23151          definition of a ... nested class outside of its class
23152          ... [or] the definition or explicit instantiation of a
23153          class member of a namespace outside of its namespace.  */
23154       if (scope == nested_name_specifier)
23155         {
23156           permerror (nested_name_specifier_token_start->location,
23157                      "extra qualification not allowed");
23158           nested_name_specifier = NULL_TREE;
23159           num_templates = 0;
23160         }
23161     }
23162   /* An explicit-specialization must be preceded by "template <>".  If
23163      it is not, try to recover gracefully.  */
23164   if (at_namespace_scope_p ()
23165       && parser->num_template_parameter_lists == 0
23166       && !processing_template_parmlist
23167       && template_id_p)
23168     {
23169       /* Build a location of this form:
23170            struct typename <ARGS>
23171            ^~~~~~~~~~~~~~~~~~~~~~
23172          with caret==start at the start token, and
23173          finishing at the end of the type.  */
23174       location_t reported_loc
23175         = make_location (class_head_start_location,
23176                          class_head_start_location,
23177                          get_finish (type_start_token->location));
23178       rich_location richloc (line_table, reported_loc);
23179       richloc.add_fixit_insert_before (class_head_start_location,
23180                                        "template <> ");
23181       error_at (&richloc,
23182                 "an explicit specialization must be preceded by"
23183                 " %<template <>%>");
23184       invalid_explicit_specialization_p = true;
23185       /* Take the same action that would have been taken by
23186          cp_parser_explicit_specialization.  */
23187       ++parser->num_template_parameter_lists;
23188       begin_specialization ();
23189     }
23190   /* There must be no "return" statements between this point and the
23191      end of this function; set "type "to the correct return value and
23192      use "goto done;" to return.  */
23193   /* Make sure that the right number of template parameters were
23194      present.  */
23195   if (!cp_parser_check_template_parameters (parser, num_templates,
23196                                             template_id_p,
23197                                             type_start_token->location,
23198                                             /*declarator=*/NULL))
23199     {
23200       /* If something went wrong, there is no point in even trying to
23201          process the class-definition.  */
23202       type = NULL_TREE;
23203       goto done;
23204     }
23205
23206   /* Look up the type.  */
23207   if (template_id_p)
23208     {
23209       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23210           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23211               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23212         {
23213           error_at (type_start_token->location,
23214                     "function template %qD redeclared as a class template", id);
23215           type = error_mark_node;
23216         }
23217       else
23218         {
23219           type = TREE_TYPE (id);
23220           type = maybe_process_partial_specialization (type);
23221
23222           /* Check the scope while we still know whether or not we had a
23223              nested-name-specifier.  */
23224           if (type != error_mark_node)
23225             check_unqualified_spec_or_inst (type, type_start_token->location);
23226         }
23227       if (nested_name_specifier)
23228         pushed_scope = push_scope (nested_name_specifier);
23229     }
23230   else if (nested_name_specifier)
23231     {
23232       tree class_type;
23233
23234       /* Given:
23235
23236             template <typename T> struct S { struct T };
23237             template <typename T> struct S<T>::T { };
23238
23239          we will get a TYPENAME_TYPE when processing the definition of
23240          `S::T'.  We need to resolve it to the actual type before we
23241          try to define it.  */
23242       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23243         {
23244           class_type = resolve_typename_type (TREE_TYPE (type),
23245                                               /*only_current_p=*/false);
23246           if (TREE_CODE (class_type) != TYPENAME_TYPE)
23247             type = TYPE_NAME (class_type);
23248           else
23249             {
23250               cp_parser_error (parser, "could not resolve typename type");
23251               type = error_mark_node;
23252             }
23253         }
23254
23255       if (maybe_process_partial_specialization (TREE_TYPE (type))
23256           == error_mark_node)
23257         {
23258           type = NULL_TREE;
23259           goto done;
23260         }
23261
23262       class_type = current_class_type;
23263       /* Enter the scope indicated by the nested-name-specifier.  */
23264       pushed_scope = push_scope (nested_name_specifier);
23265       /* Get the canonical version of this type.  */
23266       type = TYPE_MAIN_DECL (TREE_TYPE (type));
23267       /* Call push_template_decl if it seems like we should be defining a
23268          template either from the template headers or the type we're
23269          defining, so that we diagnose both extra and missing headers.  */
23270       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23271            || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23272           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23273         {
23274           type = push_template_decl (type);
23275           if (type == error_mark_node)
23276             {
23277               type = NULL_TREE;
23278               goto done;
23279             }
23280         }
23281
23282       type = TREE_TYPE (type);
23283       *nested_name_specifier_p = true;
23284     }
23285   else      /* The name is not a nested name.  */
23286     {
23287       /* If the class was unnamed, create a dummy name.  */
23288       if (!id)
23289         id = make_anon_name ();
23290       tag_scope tag_scope = (parser->in_type_id_in_expr_p
23291                              ? ts_within_enclosing_non_class
23292                              : ts_current);
23293       type = xref_tag (class_key, id, tag_scope,
23294                        parser->num_template_parameter_lists);
23295     }
23296
23297   /* Indicate whether this class was declared as a `class' or as a
23298      `struct'.  */
23299   if (TREE_CODE (type) == RECORD_TYPE)
23300     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23301   cp_parser_check_class_key (class_key, type);
23302
23303   /* If this type was already complete, and we see another definition,
23304      that's an error.  */
23305   if (type != error_mark_node && COMPLETE_TYPE_P (type))
23306     {
23307       error_at (type_start_token->location, "redefinition of %q#T",
23308                 type);
23309       inform (location_of (type), "previous definition of %q#T",
23310               type);
23311       type = NULL_TREE;
23312       goto done;
23313     }
23314   else if (type == error_mark_node)
23315     type = NULL_TREE;
23316
23317   if (type)
23318     {
23319       /* Apply attributes now, before any use of the class as a template
23320          argument in its base list.  */
23321       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23322       fixup_attribute_variants (type);
23323     }
23324
23325   /* We will have entered the scope containing the class; the names of
23326      base classes should be looked up in that context.  For example:
23327
23328        struct A { struct B {}; struct C; };
23329        struct A::C : B {};
23330
23331      is valid.  */
23332
23333   /* Get the list of base-classes, if there is one.  */
23334   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23335     {
23336       /* PR59482: enter the class scope so that base-specifiers are looked
23337          up correctly.  */
23338       if (type)
23339         pushclass (type);
23340       bases = cp_parser_base_clause (parser);
23341       /* PR59482: get out of the previously pushed class scope so that the
23342          subsequent pops pop the right thing.  */
23343       if (type)
23344         popclass ();
23345     }
23346   else
23347     bases = NULL_TREE;
23348
23349   /* If we're really defining a class, process the base classes.
23350      If they're invalid, fail.  */
23351   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23352     xref_basetypes (type, bases);
23353
23354  done:
23355   /* Leave the scope given by the nested-name-specifier.  We will
23356      enter the class scope itself while processing the members.  */
23357   if (pushed_scope)
23358     pop_scope (pushed_scope);
23359
23360   if (invalid_explicit_specialization_p)
23361     {
23362       end_specialization ();
23363       --parser->num_template_parameter_lists;
23364     }
23365
23366   if (type)
23367     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23368   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23369     CLASSTYPE_FINAL (type) = 1;
23370  out:
23371   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23372   return type;
23373 }
23374
23375 /* Parse a class-key.
23376
23377    class-key:
23378      class
23379      struct
23380      union
23381
23382    Returns the kind of class-key specified, or none_type to indicate
23383    error.  */
23384
23385 static enum tag_types
23386 cp_parser_class_key (cp_parser* parser)
23387 {
23388   cp_token *token;
23389   enum tag_types tag_type;
23390
23391   /* Look for the class-key.  */
23392   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23393   if (!token)
23394     return none_type;
23395
23396   /* Check to see if the TOKEN is a class-key.  */
23397   tag_type = cp_parser_token_is_class_key (token);
23398   if (!tag_type)
23399     cp_parser_error (parser, "expected class-key");
23400   return tag_type;
23401 }
23402
23403 /* Parse a type-parameter-key.
23404
23405    type-parameter-key:
23406      class
23407      typename
23408  */
23409
23410 static void
23411 cp_parser_type_parameter_key (cp_parser* parser)
23412 {
23413   /* Look for the type-parameter-key.  */
23414   enum tag_types tag_type = none_type;
23415   cp_token *token = cp_lexer_peek_token (parser->lexer);
23416   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23417     {
23418       cp_lexer_consume_token (parser->lexer);
23419       if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23420         /* typename is not allowed in a template template parameter
23421            by the standard until C++17.  */
23422         pedwarn (token->location, OPT_Wpedantic, 
23423                  "ISO C++ forbids typename key in template template parameter;"
23424                  " use -std=c++17 or -std=gnu++17");
23425     }
23426   else
23427     cp_parser_error (parser, "expected %<class%> or %<typename%>");
23428
23429   return;
23430 }
23431
23432 /* Parse an (optional) member-specification.
23433
23434    member-specification:
23435      member-declaration member-specification [opt]
23436      access-specifier : member-specification [opt]  */
23437
23438 static void
23439 cp_parser_member_specification_opt (cp_parser* parser)
23440 {
23441   while (true)
23442     {
23443       cp_token *token;
23444       enum rid keyword;
23445
23446       /* Peek at the next token.  */
23447       token = cp_lexer_peek_token (parser->lexer);
23448       /* If it's a `}', or EOF then we've seen all the members.  */
23449       if (token->type == CPP_CLOSE_BRACE
23450           || token->type == CPP_EOF
23451           || token->type == CPP_PRAGMA_EOL)
23452         break;
23453
23454       /* See if this token is a keyword.  */
23455       keyword = token->keyword;
23456       switch (keyword)
23457         {
23458         case RID_PUBLIC:
23459         case RID_PROTECTED:
23460         case RID_PRIVATE:
23461           /* Consume the access-specifier.  */
23462           cp_lexer_consume_token (parser->lexer);
23463           /* Remember which access-specifier is active.  */
23464           current_access_specifier = token->u.value;
23465           /* Look for the `:'.  */
23466           cp_parser_require (parser, CPP_COLON, RT_COLON);
23467           break;
23468
23469         default:
23470           /* Accept #pragmas at class scope.  */
23471           if (token->type == CPP_PRAGMA)
23472             {
23473               cp_parser_pragma (parser, pragma_member, NULL);
23474               break;
23475             }
23476
23477           /* Otherwise, the next construction must be a
23478              member-declaration.  */
23479           cp_parser_member_declaration (parser);
23480         }
23481     }
23482 }
23483
23484 /* Parse a member-declaration.
23485
23486    member-declaration:
23487      decl-specifier-seq [opt] member-declarator-list [opt] ;
23488      function-definition ; [opt]
23489      :: [opt] nested-name-specifier template [opt] unqualified-id ;
23490      using-declaration
23491      template-declaration
23492      alias-declaration
23493
23494    member-declarator-list:
23495      member-declarator
23496      member-declarator-list , member-declarator
23497
23498    member-declarator:
23499      declarator pure-specifier [opt]
23500      declarator constant-initializer [opt]
23501      identifier [opt] : constant-expression
23502
23503    GNU Extensions:
23504
23505    member-declaration:
23506      __extension__ member-declaration
23507
23508    member-declarator:
23509      declarator attributes [opt] pure-specifier [opt]
23510      declarator attributes [opt] constant-initializer [opt]
23511      identifier [opt] attributes [opt] : constant-expression  
23512
23513    C++0x Extensions:
23514
23515    member-declaration:
23516      static_assert-declaration  */
23517
23518 static void
23519 cp_parser_member_declaration (cp_parser* parser)
23520 {
23521   cp_decl_specifier_seq decl_specifiers;
23522   tree prefix_attributes;
23523   tree decl;
23524   int declares_class_or_enum;
23525   bool friend_p;
23526   cp_token *token = NULL;
23527   cp_token *decl_spec_token_start = NULL;
23528   cp_token *initializer_token_start = NULL;
23529   int saved_pedantic;
23530   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23531
23532   /* Check for the `__extension__' keyword.  */
23533   if (cp_parser_extension_opt (parser, &saved_pedantic))
23534     {
23535       /* Recurse.  */
23536       cp_parser_member_declaration (parser);
23537       /* Restore the old value of the PEDANTIC flag.  */
23538       pedantic = saved_pedantic;
23539
23540       return;
23541     }
23542
23543   /* Check for a template-declaration.  */
23544   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23545     {
23546       /* An explicit specialization here is an error condition, and we
23547          expect the specialization handler to detect and report this.  */
23548       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23549           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23550         cp_parser_explicit_specialization (parser);
23551       else
23552         cp_parser_template_declaration (parser, /*member_p=*/true);
23553
23554       return;
23555     }
23556   /* Check for a template introduction.  */
23557   else if (cp_parser_template_declaration_after_export (parser, true))
23558     return;
23559
23560   /* Check for a using-declaration.  */
23561   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23562     {
23563       if (cxx_dialect < cxx11)
23564         {
23565           /* Parse the using-declaration.  */
23566           cp_parser_using_declaration (parser,
23567                                        /*access_declaration_p=*/false);
23568           return;
23569         }
23570       else
23571         {
23572           tree decl;
23573           bool alias_decl_expected;
23574           cp_parser_parse_tentatively (parser);
23575           decl = cp_parser_alias_declaration (parser);
23576           /* Note that if we actually see the '=' token after the
23577              identifier, cp_parser_alias_declaration commits the
23578              tentative parse.  In that case, we really expect an
23579              alias-declaration.  Otherwise, we expect a using
23580              declaration.  */
23581           alias_decl_expected =
23582             !cp_parser_uncommitted_to_tentative_parse_p (parser);
23583           cp_parser_parse_definitely (parser);
23584
23585           if (alias_decl_expected)
23586             finish_member_declaration (decl);
23587           else
23588             cp_parser_using_declaration (parser,
23589                                          /*access_declaration_p=*/false);
23590           return;
23591         }
23592     }
23593
23594   /* Check for @defs.  */
23595   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23596     {
23597       tree ivar, member;
23598       tree ivar_chains = cp_parser_objc_defs_expression (parser);
23599       ivar = ivar_chains;
23600       while (ivar)
23601         {
23602           member = ivar;
23603           ivar = TREE_CHAIN (member);
23604           TREE_CHAIN (member) = NULL_TREE;
23605           finish_member_declaration (member);
23606         }
23607       return;
23608     }
23609
23610   /* If the next token is `static_assert' we have a static assertion.  */
23611   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23612     {
23613       cp_parser_static_assert (parser, /*member_p=*/true);
23614       return;
23615     }
23616
23617   parser->colon_corrects_to_scope_p = false;
23618
23619   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23620       goto out;
23621
23622   /* Parse the decl-specifier-seq.  */
23623   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23624   cp_parser_decl_specifier_seq (parser,
23625                                 CP_PARSER_FLAGS_OPTIONAL,
23626                                 &decl_specifiers,
23627                                 &declares_class_or_enum);
23628   /* Check for an invalid type-name.  */
23629   if (!decl_specifiers.any_type_specifiers_p
23630       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23631     goto out;
23632   /* If there is no declarator, then the decl-specifier-seq should
23633      specify a type.  */
23634   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23635     {
23636       /* If there was no decl-specifier-seq, and the next token is a
23637          `;', then we have something like:
23638
23639            struct S { ; };
23640
23641          [class.mem]
23642
23643          Each member-declaration shall declare at least one member
23644          name of the class.  */
23645       if (!decl_specifiers.any_specifiers_p)
23646         {
23647           cp_token *token = cp_lexer_peek_token (parser->lexer);
23648           if (!in_system_header_at (token->location))
23649             {
23650               gcc_rich_location richloc (token->location);
23651               richloc.add_fixit_remove ();
23652               pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23653             }
23654         }
23655       else
23656         {
23657           tree type;
23658
23659           /* See if this declaration is a friend.  */
23660           friend_p = cp_parser_friend_p (&decl_specifiers);
23661           /* If there were decl-specifiers, check to see if there was
23662              a class-declaration.  */
23663           type = check_tag_decl (&decl_specifiers,
23664                                  /*explicit_type_instantiation_p=*/false);
23665           /* Nested classes have already been added to the class, but
23666              a `friend' needs to be explicitly registered.  */
23667           if (friend_p)
23668             {
23669               /* If the `friend' keyword was present, the friend must
23670                  be introduced with a class-key.  */
23671                if (!declares_class_or_enum && cxx_dialect < cxx11)
23672                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23673                           "in C++03 a class-key must be used "
23674                           "when declaring a friend");
23675                /* In this case:
23676
23677                     template <typename T> struct A {
23678                       friend struct A<T>::B;
23679                     };
23680
23681                   A<T>::B will be represented by a TYPENAME_TYPE, and
23682                   therefore not recognized by check_tag_decl.  */
23683                if (!type)
23684                  {
23685                    type = decl_specifiers.type;
23686                    if (type && TREE_CODE (type) == TYPE_DECL)
23687                      type = TREE_TYPE (type);
23688                  }
23689                if (!type || !TYPE_P (type))
23690                  error_at (decl_spec_token_start->location,
23691                            "friend declaration does not name a class or "
23692                            "function");
23693                else
23694                  make_friend_class (current_class_type, type,
23695                                     /*complain=*/true);
23696             }
23697           /* If there is no TYPE, an error message will already have
23698              been issued.  */
23699           else if (!type || type == error_mark_node)
23700             ;
23701           /* An anonymous aggregate has to be handled specially; such
23702              a declaration really declares a data member (with a
23703              particular type), as opposed to a nested class.  */
23704           else if (ANON_AGGR_TYPE_P (type))
23705             {
23706               /* C++11 9.5/6.  */
23707               if (decl_specifiers.storage_class != sc_none)
23708                 error_at (decl_spec_token_start->location,
23709                           "a storage class on an anonymous aggregate "
23710                           "in class scope is not allowed");
23711
23712               /* Remove constructors and such from TYPE, now that we
23713                  know it is an anonymous aggregate.  */
23714               fixup_anonymous_aggr (type);
23715               /* And make the corresponding data member.  */
23716               decl = build_decl (decl_spec_token_start->location,
23717                                  FIELD_DECL, NULL_TREE, type);
23718               /* Add it to the class.  */
23719               finish_member_declaration (decl);
23720             }
23721           else
23722             cp_parser_check_access_in_redeclaration
23723                                               (TYPE_NAME (type),
23724                                                decl_spec_token_start->location);
23725         }
23726     }
23727   else
23728     {
23729       bool assume_semicolon = false;
23730
23731       /* Clear attributes from the decl_specifiers but keep them
23732          around as prefix attributes that apply them to the entity
23733          being declared.  */
23734       prefix_attributes = decl_specifiers.attributes;
23735       decl_specifiers.attributes = NULL_TREE;
23736
23737       /* See if these declarations will be friends.  */
23738       friend_p = cp_parser_friend_p (&decl_specifiers);
23739
23740       /* Keep going until we hit the `;' at the end of the
23741          declaration.  */
23742       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23743         {
23744           tree attributes = NULL_TREE;
23745           tree first_attribute;
23746           tree initializer;
23747           bool named_bitfld = false;
23748
23749           /* Peek at the next token.  */
23750           token = cp_lexer_peek_token (parser->lexer);
23751
23752           /* The following code wants to know early if it is a bit-field
23753              or some other declaration.  Attributes can appear before
23754              the `:' token.  Skip over them without consuming any tokens
23755              to peek if they are followed by `:'.  */
23756           if (cp_next_tokens_can_be_attribute_p (parser)
23757               || (token->type == CPP_NAME
23758                   && cp_nth_tokens_can_be_attribute_p (parser, 2)
23759                   && (named_bitfld = true)))
23760             {
23761               size_t n
23762                 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23763               token = cp_lexer_peek_nth_token (parser->lexer, n);
23764             }
23765
23766           /* Check for a bitfield declaration.  */
23767           if (token->type == CPP_COLON
23768               || (token->type == CPP_NAME
23769                   && token == cp_lexer_peek_token (parser->lexer)
23770                   && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23771                   && (named_bitfld = true)))
23772             {
23773               tree identifier;
23774               tree width;
23775               tree late_attributes = NULL_TREE;
23776
23777               if (named_bitfld)
23778                 identifier = cp_parser_identifier (parser);
23779               else
23780                 identifier = NULL_TREE;
23781
23782               /* Look for attributes that apply to the bitfield.  */
23783               attributes = cp_parser_attributes_opt (parser);
23784
23785               /* Consume the `:' token.  */
23786               cp_lexer_consume_token (parser->lexer);
23787
23788               /* Get the width of the bitfield.  */
23789               width = cp_parser_constant_expression (parser, false, NULL,
23790                                                      cxx_dialect >= cxx11);
23791
23792               /* In C++2A and as extension for C++11 and above we allow
23793                  default member initializers for bit-fields.  */
23794               initializer = NULL_TREE;
23795               if (cxx_dialect >= cxx11
23796                   && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23797                       || cp_lexer_next_token_is (parser->lexer,
23798                                                  CPP_OPEN_BRACE)))
23799                 {
23800                   location_t loc
23801                     = cp_lexer_peek_token (parser->lexer)->location;
23802                   if (cxx_dialect < cxx2a
23803                       && !in_system_header_at (loc)
23804                       && identifier != NULL_TREE)
23805                     pedwarn (loc, 0,
23806                              "default member initializers for bit-fields "
23807                              "only available with -std=c++2a or "
23808                              "-std=gnu++2a");
23809
23810                   initializer = cp_parser_save_nsdmi (parser);
23811                   if (identifier == NULL_TREE)
23812                     {
23813                       error_at (loc, "default member initializer for "
23814                                      "unnamed bit-field");
23815                       initializer = NULL_TREE;
23816                     }
23817                 }
23818               else
23819                 { 
23820                   /* Look for attributes that apply to the bitfield after
23821                      the `:' token and width.  This is where GCC used to
23822                      parse attributes in the past, pedwarn if there is
23823                      a std attribute.  */
23824                   if (cp_next_tokens_can_be_std_attribute_p (parser))
23825                     pedwarn (input_location, OPT_Wpedantic,
23826                              "ISO C++ allows bit-field attributes only "
23827                              "before the %<:%> token");
23828
23829                   late_attributes = cp_parser_attributes_opt (parser);
23830                 }
23831
23832               attributes = attr_chainon (attributes, late_attributes);
23833
23834               /* Remember which attributes are prefix attributes and
23835                  which are not.  */
23836               first_attribute = attributes;
23837               /* Combine the attributes.  */
23838               attributes = attr_chainon (prefix_attributes, attributes);
23839
23840               /* Create the bitfield declaration.  */
23841               decl = grokbitfield (identifier
23842                                    ? make_id_declarator (NULL_TREE,
23843                                                          identifier,
23844                                                          sfk_none)
23845                                    : NULL,
23846                                    &decl_specifiers,
23847                                    width, initializer,
23848                                    attributes);
23849             }
23850           else
23851             {
23852               cp_declarator *declarator;
23853               tree asm_specification;
23854               int ctor_dtor_or_conv_p;
23855
23856               /* Parse the declarator.  */
23857               declarator
23858                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23859                                         &ctor_dtor_or_conv_p,
23860                                         /*parenthesized_p=*/NULL,
23861                                         /*member_p=*/true,
23862                                         friend_p);
23863
23864               /* If something went wrong parsing the declarator, make sure
23865                  that we at least consume some tokens.  */
23866               if (declarator == cp_error_declarator)
23867                 {
23868                   /* Skip to the end of the statement.  */
23869                   cp_parser_skip_to_end_of_statement (parser);
23870                   /* If the next token is not a semicolon, that is
23871                      probably because we just skipped over the body of
23872                      a function.  So, we consume a semicolon if
23873                      present, but do not issue an error message if it
23874                      is not present.  */
23875                   if (cp_lexer_next_token_is (parser->lexer,
23876                                               CPP_SEMICOLON))
23877                     cp_lexer_consume_token (parser->lexer);
23878                   goto out;
23879                 }
23880
23881               if (declares_class_or_enum & 2)
23882                 cp_parser_check_for_definition_in_return_type
23883                                             (declarator, decl_specifiers.type,
23884                                              decl_specifiers.locations[ds_type_spec]);
23885
23886               /* Look for an asm-specification.  */
23887               asm_specification = cp_parser_asm_specification_opt (parser);
23888               /* Look for attributes that apply to the declaration.  */
23889               attributes = cp_parser_attributes_opt (parser);
23890               /* Remember which attributes are prefix attributes and
23891                  which are not.  */
23892               first_attribute = attributes;
23893               /* Combine the attributes.  */
23894               attributes = attr_chainon (prefix_attributes, attributes);
23895
23896               /* If it's an `=', then we have a constant-initializer or a
23897                  pure-specifier.  It is not correct to parse the
23898                  initializer before registering the member declaration
23899                  since the member declaration should be in scope while
23900                  its initializer is processed.  However, the rest of the
23901                  front end does not yet provide an interface that allows
23902                  us to handle this correctly.  */
23903               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23904                 {
23905                   /* In [class.mem]:
23906
23907                      A pure-specifier shall be used only in the declaration of
23908                      a virtual function.
23909
23910                      A member-declarator can contain a constant-initializer
23911                      only if it declares a static member of integral or
23912                      enumeration type.
23913
23914                      Therefore, if the DECLARATOR is for a function, we look
23915                      for a pure-specifier; otherwise, we look for a
23916                      constant-initializer.  When we call `grokfield', it will
23917                      perform more stringent semantics checks.  */
23918                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
23919                   if (function_declarator_p (declarator)
23920                       || (decl_specifiers.type
23921                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23922                           && declarator->kind == cdk_id
23923                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23924                               == FUNCTION_TYPE)))
23925                     initializer = cp_parser_pure_specifier (parser);
23926                   else if (decl_specifiers.storage_class != sc_static)
23927                     initializer = cp_parser_save_nsdmi (parser);
23928                   else if (cxx_dialect >= cxx11)
23929                     {
23930                       bool nonconst;
23931                       /* Don't require a constant rvalue in C++11, since we
23932                          might want a reference constant.  We'll enforce
23933                          constancy later.  */
23934                       cp_lexer_consume_token (parser->lexer);
23935                       /* Parse the initializer.  */
23936                       initializer = cp_parser_initializer_clause (parser,
23937                                                                   &nonconst);
23938                     }
23939                   else
23940                     /* Parse the initializer.  */
23941                     initializer = cp_parser_constant_initializer (parser);
23942                 }
23943               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23944                        && !function_declarator_p (declarator))
23945                 {
23946                   bool x;
23947                   if (decl_specifiers.storage_class != sc_static)
23948                     initializer = cp_parser_save_nsdmi (parser);
23949                   else
23950                     initializer = cp_parser_initializer (parser, &x, &x);
23951                 }
23952               /* Otherwise, there is no initializer.  */
23953               else
23954                 initializer = NULL_TREE;
23955
23956               /* See if we are probably looking at a function
23957                  definition.  We are certainly not looking at a
23958                  member-declarator.  Calling `grokfield' has
23959                  side-effects, so we must not do it unless we are sure
23960                  that we are looking at a member-declarator.  */
23961               if (cp_parser_token_starts_function_definition_p
23962                   (cp_lexer_peek_token (parser->lexer)))
23963                 {
23964                   /* The grammar does not allow a pure-specifier to be
23965                      used when a member function is defined.  (It is
23966                      possible that this fact is an oversight in the
23967                      standard, since a pure function may be defined
23968                      outside of the class-specifier.  */
23969                   if (initializer && initializer_token_start)
23970                     error_at (initializer_token_start->location,
23971                               "pure-specifier on function-definition");
23972                   decl = cp_parser_save_member_function_body (parser,
23973                                                               &decl_specifiers,
23974                                                               declarator,
23975                                                               attributes);
23976                   if (parser->fully_implicit_function_template_p)
23977                     decl = finish_fully_implicit_template (parser, decl);
23978                   /* If the member was not a friend, declare it here.  */
23979                   if (!friend_p)
23980                     finish_member_declaration (decl);
23981                   /* Peek at the next token.  */
23982                   token = cp_lexer_peek_token (parser->lexer);
23983                   /* If the next token is a semicolon, consume it.  */
23984                   if (token->type == CPP_SEMICOLON)
23985                     {
23986                       location_t semicolon_loc
23987                         = cp_lexer_consume_token (parser->lexer)->location;
23988                       gcc_rich_location richloc (semicolon_loc);
23989                       richloc.add_fixit_remove ();
23990                       warning_at (&richloc, OPT_Wextra_semi,
23991                                   "extra %<;%> after in-class "
23992                                   "function definition");
23993                     }
23994                   goto out;
23995                 }
23996               else
23997                 if (declarator->kind == cdk_function)
23998                   declarator->id_loc = token->location;
23999               /* Create the declaration.  */
24000               decl = grokfield (declarator, &decl_specifiers,
24001                                 initializer, /*init_const_expr_p=*/true,
24002                                 asm_specification, attributes);
24003               if (parser->fully_implicit_function_template_p)
24004                 {
24005                   if (friend_p)
24006                     finish_fully_implicit_template (parser, 0);
24007                   else
24008                     decl = finish_fully_implicit_template (parser, decl);
24009                 }
24010             }
24011
24012           cp_finalize_omp_declare_simd (parser, decl);
24013           cp_finalize_oacc_routine (parser, decl, false);
24014
24015           /* Reset PREFIX_ATTRIBUTES.  */
24016           if (attributes != error_mark_node)
24017             {
24018               while (attributes && TREE_CHAIN (attributes) != first_attribute)
24019                 attributes = TREE_CHAIN (attributes);
24020               if (attributes)
24021                 TREE_CHAIN (attributes) = NULL_TREE;
24022             }
24023
24024           /* If there is any qualification still in effect, clear it
24025              now; we will be starting fresh with the next declarator.  */
24026           parser->scope = NULL_TREE;
24027           parser->qualifying_scope = NULL_TREE;
24028           parser->object_scope = NULL_TREE;
24029           /* If it's a `,', then there are more declarators.  */
24030           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24031             {
24032               cp_lexer_consume_token (parser->lexer);
24033               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24034                 {
24035                   cp_token *token = cp_lexer_previous_token (parser->lexer);
24036                   gcc_rich_location richloc (token->location);
24037                   richloc.add_fixit_remove ();
24038                   error_at (&richloc, "stray %<,%> at end of "
24039                             "member declaration");
24040                 }
24041             }
24042           /* If the next token isn't a `;', then we have a parse error.  */
24043           else if (cp_lexer_next_token_is_not (parser->lexer,
24044                                                CPP_SEMICOLON))
24045             {
24046               /* The next token might be a ways away from where the
24047                  actual semicolon is missing.  Find the previous token
24048                  and use that for our error position.  */
24049               cp_token *token = cp_lexer_previous_token (parser->lexer);
24050               gcc_rich_location richloc (token->location);
24051               richloc.add_fixit_insert_after (";");
24052               error_at (&richloc, "expected %<;%> at end of "
24053                         "member declaration");
24054
24055               /* Assume that the user meant to provide a semicolon.  If
24056                  we were to cp_parser_skip_to_end_of_statement, we might
24057                  skip to a semicolon inside a member function definition
24058                  and issue nonsensical error messages.  */
24059               assume_semicolon = true;
24060             }
24061
24062           if (decl)
24063             {
24064               /* Add DECL to the list of members.  */
24065               if (!friend_p
24066                   /* Explicitly include, eg, NSDMIs, for better error
24067                      recovery (c++/58650).  */
24068                   || !DECL_DECLARES_FUNCTION_P (decl))
24069                 finish_member_declaration (decl);
24070
24071               if (TREE_CODE (decl) == FUNCTION_DECL)
24072                 cp_parser_save_default_args (parser, decl);
24073               else if (TREE_CODE (decl) == FIELD_DECL
24074                        && DECL_INITIAL (decl))
24075                 /* Add DECL to the queue of NSDMI to be parsed later.  */
24076                 vec_safe_push (unparsed_nsdmis, decl);
24077             }
24078
24079           if (assume_semicolon)
24080             goto out;
24081         }
24082     }
24083
24084   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24085  out:
24086   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24087 }
24088
24089 /* Parse a pure-specifier.
24090
24091    pure-specifier:
24092      = 0
24093
24094    Returns INTEGER_ZERO_NODE if a pure specifier is found.
24095    Otherwise, ERROR_MARK_NODE is returned.  */
24096
24097 static tree
24098 cp_parser_pure_specifier (cp_parser* parser)
24099 {
24100   cp_token *token;
24101
24102   /* Look for the `=' token.  */
24103   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24104     return error_mark_node;
24105   /* Look for the `0' token.  */
24106   token = cp_lexer_peek_token (parser->lexer);
24107
24108   if (token->type == CPP_EOF
24109       || token->type == CPP_PRAGMA_EOL)
24110     return error_mark_node;
24111
24112   cp_lexer_consume_token (parser->lexer);
24113
24114   /* Accept = default or = delete in c++0x mode.  */
24115   if (token->keyword == RID_DEFAULT
24116       || token->keyword == RID_DELETE)
24117     {
24118       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24119       return token->u.value;
24120     }
24121
24122   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
24123   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24124     {
24125       cp_parser_error (parser,
24126                        "invalid pure specifier (only %<= 0%> is allowed)");
24127       cp_parser_skip_to_end_of_statement (parser);
24128       return error_mark_node;
24129     }
24130   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24131     {
24132       error_at (token->location, "templates may not be %<virtual%>");
24133       return error_mark_node;
24134     }
24135
24136   return integer_zero_node;
24137 }
24138
24139 /* Parse a constant-initializer.
24140
24141    constant-initializer:
24142      = constant-expression
24143
24144    Returns a representation of the constant-expression.  */
24145
24146 static tree
24147 cp_parser_constant_initializer (cp_parser* parser)
24148 {
24149   /* Look for the `=' token.  */
24150   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24151     return error_mark_node;
24152
24153   /* It is invalid to write:
24154
24155        struct S { static const int i = { 7 }; };
24156
24157      */
24158   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24159     {
24160       cp_parser_error (parser,
24161                        "a brace-enclosed initializer is not allowed here");
24162       /* Consume the opening brace.  */
24163       matching_braces braces;
24164       braces.consume_open (parser);
24165       /* Skip the initializer.  */
24166       cp_parser_skip_to_closing_brace (parser);
24167       /* Look for the trailing `}'.  */
24168       braces.require_close (parser);
24169
24170       return error_mark_node;
24171     }
24172
24173   return cp_parser_constant_expression (parser);
24174 }
24175
24176 /* Derived classes [gram.class.derived] */
24177
24178 /* Parse a base-clause.
24179
24180    base-clause:
24181      : base-specifier-list
24182
24183    base-specifier-list:
24184      base-specifier ... [opt]
24185      base-specifier-list , base-specifier ... [opt]
24186
24187    Returns a TREE_LIST representing the base-classes, in the order in
24188    which they were declared.  The representation of each node is as
24189    described by cp_parser_base_specifier.
24190
24191    In the case that no bases are specified, this function will return
24192    NULL_TREE, not ERROR_MARK_NODE.  */
24193
24194 static tree
24195 cp_parser_base_clause (cp_parser* parser)
24196 {
24197   tree bases = NULL_TREE;
24198
24199   /* Look for the `:' that begins the list.  */
24200   cp_parser_require (parser, CPP_COLON, RT_COLON);
24201
24202   /* Scan the base-specifier-list.  */
24203   while (true)
24204     {
24205       cp_token *token;
24206       tree base;
24207       bool pack_expansion_p = false;
24208
24209       /* Look for the base-specifier.  */
24210       base = cp_parser_base_specifier (parser);
24211       /* Look for the (optional) ellipsis. */
24212       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24213         {
24214           /* Consume the `...'. */
24215           cp_lexer_consume_token (parser->lexer);
24216
24217           pack_expansion_p = true;
24218         }
24219
24220       /* Add BASE to the front of the list.  */
24221       if (base && base != error_mark_node)
24222         {
24223           if (pack_expansion_p)
24224             /* Make this a pack expansion type. */
24225             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24226
24227           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24228             {
24229               TREE_CHAIN (base) = bases;
24230               bases = base;
24231             }
24232         }
24233       /* Peek at the next token.  */
24234       token = cp_lexer_peek_token (parser->lexer);
24235       /* If it's not a comma, then the list is complete.  */
24236       if (token->type != CPP_COMMA)
24237         break;
24238       /* Consume the `,'.  */
24239       cp_lexer_consume_token (parser->lexer);
24240     }
24241
24242   /* PARSER->SCOPE may still be non-NULL at this point, if the last
24243      base class had a qualified name.  However, the next name that
24244      appears is certainly not qualified.  */
24245   parser->scope = NULL_TREE;
24246   parser->qualifying_scope = NULL_TREE;
24247   parser->object_scope = NULL_TREE;
24248
24249   return nreverse (bases);
24250 }
24251
24252 /* Parse a base-specifier.
24253
24254    base-specifier:
24255      :: [opt] nested-name-specifier [opt] class-name
24256      virtual access-specifier [opt] :: [opt] nested-name-specifier
24257        [opt] class-name
24258      access-specifier virtual [opt] :: [opt] nested-name-specifier
24259        [opt] class-name
24260
24261    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
24262    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24263    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
24264    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
24265
24266 static tree
24267 cp_parser_base_specifier (cp_parser* parser)
24268 {
24269   cp_token *token;
24270   bool done = false;
24271   bool virtual_p = false;
24272   bool duplicate_virtual_error_issued_p = false;
24273   bool duplicate_access_error_issued_p = false;
24274   bool class_scope_p, template_p;
24275   tree access = access_default_node;
24276   tree type;
24277
24278   /* Process the optional `virtual' and `access-specifier'.  */
24279   while (!done)
24280     {
24281       /* Peek at the next token.  */
24282       token = cp_lexer_peek_token (parser->lexer);
24283       /* Process `virtual'.  */
24284       switch (token->keyword)
24285         {
24286         case RID_VIRTUAL:
24287           /* If `virtual' appears more than once, issue an error.  */
24288           if (virtual_p && !duplicate_virtual_error_issued_p)
24289             {
24290               cp_parser_error (parser,
24291                                "%<virtual%> specified more than once in base-specifier");
24292               duplicate_virtual_error_issued_p = true;
24293             }
24294
24295           virtual_p = true;
24296
24297           /* Consume the `virtual' token.  */
24298           cp_lexer_consume_token (parser->lexer);
24299
24300           break;
24301
24302         case RID_PUBLIC:
24303         case RID_PROTECTED:
24304         case RID_PRIVATE:
24305           /* If more than one access specifier appears, issue an
24306              error.  */
24307           if (access != access_default_node
24308               && !duplicate_access_error_issued_p)
24309             {
24310               cp_parser_error (parser,
24311                                "more than one access specifier in base-specifier");
24312               duplicate_access_error_issued_p = true;
24313             }
24314
24315           access = ridpointers[(int) token->keyword];
24316
24317           /* Consume the access-specifier.  */
24318           cp_lexer_consume_token (parser->lexer);
24319
24320           break;
24321
24322         default:
24323           done = true;
24324           break;
24325         }
24326     }
24327   /* It is not uncommon to see programs mechanically, erroneously, use
24328      the 'typename' keyword to denote (dependent) qualified types
24329      as base classes.  */
24330   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24331     {
24332       token = cp_lexer_peek_token (parser->lexer);
24333       if (!processing_template_decl)
24334         error_at (token->location,
24335                   "keyword %<typename%> not allowed outside of templates");
24336       else
24337         error_at (token->location,
24338                   "keyword %<typename%> not allowed in this context "
24339                   "(the base class is implicitly a type)");
24340       cp_lexer_consume_token (parser->lexer);
24341     }
24342
24343   /* Look for the optional `::' operator.  */
24344   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24345   /* Look for the nested-name-specifier.  The simplest way to
24346      implement:
24347
24348        [temp.res]
24349
24350        The keyword `typename' is not permitted in a base-specifier or
24351        mem-initializer; in these contexts a qualified name that
24352        depends on a template-parameter is implicitly assumed to be a
24353        type name.
24354
24355      is to pretend that we have seen the `typename' keyword at this
24356      point.  */
24357   cp_parser_nested_name_specifier_opt (parser,
24358                                        /*typename_keyword_p=*/true,
24359                                        /*check_dependency_p=*/true,
24360                                        /*type_p=*/true,
24361                                        /*is_declaration=*/true);
24362   /* If the base class is given by a qualified name, assume that names
24363      we see are type names or templates, as appropriate.  */
24364   class_scope_p = (parser->scope && TYPE_P (parser->scope));
24365   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24366
24367   if (!parser->scope
24368       && cp_lexer_next_token_is_decltype (parser->lexer))
24369     /* DR 950 allows decltype as a base-specifier.  */
24370     type = cp_parser_decltype (parser);
24371   else
24372     {
24373       /* Otherwise, look for the class-name.  */
24374       type = cp_parser_class_name (parser,
24375                                    class_scope_p,
24376                                    template_p,
24377                                    typename_type,
24378                                    /*check_dependency_p=*/true,
24379                                    /*class_head_p=*/false,
24380                                    /*is_declaration=*/true);
24381       type = TREE_TYPE (type);
24382     }
24383
24384   if (type == error_mark_node)
24385     return error_mark_node;
24386
24387   return finish_base_specifier (type, access, virtual_p);
24388 }
24389
24390 /* Exception handling [gram.exception] */
24391
24392 /* Parse an (optional) noexcept-specification.
24393
24394    noexcept-specification:
24395      noexcept ( constant-expression ) [opt]
24396
24397    If no noexcept-specification is present, returns NULL_TREE.
24398    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24399    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24400    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
24401    Otherwise, returns a noexcept specification unless RETURN_COND is true,
24402    in which case a boolean condition is returned instead.  */
24403
24404 static tree
24405 cp_parser_noexcept_specification_opt (cp_parser* parser,
24406                                       bool require_constexpr,
24407                                       bool* consumed_expr,
24408                                       bool return_cond)
24409 {
24410   cp_token *token;
24411   const char *saved_message;
24412
24413   /* Peek at the next token.  */
24414   token = cp_lexer_peek_token (parser->lexer);
24415
24416   /* Is it a noexcept-specification?  */
24417   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24418     {
24419       tree expr;
24420       cp_lexer_consume_token (parser->lexer);
24421
24422       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24423         {
24424           matching_parens parens;
24425           parens.consume_open (parser);
24426
24427           if (require_constexpr)
24428             {
24429               /* Types may not be defined in an exception-specification.  */
24430               saved_message = parser->type_definition_forbidden_message;
24431               parser->type_definition_forbidden_message
24432               = G_("types may not be defined in an exception-specification");
24433
24434               expr = cp_parser_constant_expression (parser);
24435
24436               /* Restore the saved message.  */
24437               parser->type_definition_forbidden_message = saved_message;
24438             }
24439           else
24440             {
24441               expr = cp_parser_expression (parser);
24442               *consumed_expr = true;
24443             }
24444
24445           parens.require_close (parser);
24446         }
24447       else
24448         {
24449           expr = boolean_true_node;
24450           if (!require_constexpr)
24451             *consumed_expr = false;
24452         }
24453
24454       /* We cannot build a noexcept-spec right away because this will check
24455          that expr is a constexpr.  */
24456       if (!return_cond)
24457         return build_noexcept_spec (expr, tf_warning_or_error);
24458       else
24459         return expr;
24460     }
24461   else
24462     return NULL_TREE;
24463 }
24464
24465 /* Parse an (optional) exception-specification.
24466
24467    exception-specification:
24468      throw ( type-id-list [opt] )
24469
24470    Returns a TREE_LIST representing the exception-specification.  The
24471    TREE_VALUE of each node is a type.  */
24472
24473 static tree
24474 cp_parser_exception_specification_opt (cp_parser* parser)
24475 {
24476   cp_token *token;
24477   tree type_id_list;
24478   const char *saved_message;
24479
24480   /* Peek at the next token.  */
24481   token = cp_lexer_peek_token (parser->lexer);
24482
24483   /* Is it a noexcept-specification?  */
24484   type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24485                                                        false);
24486   if (type_id_list != NULL_TREE)
24487     return type_id_list;
24488
24489   /* If it's not `throw', then there's no exception-specification.  */
24490   if (!cp_parser_is_keyword (token, RID_THROW))
24491     return NULL_TREE;
24492
24493   location_t loc = token->location;
24494
24495   /* Consume the `throw'.  */
24496   cp_lexer_consume_token (parser->lexer);
24497
24498   /* Look for the `('.  */
24499   matching_parens parens;
24500   parens.require_open (parser);
24501
24502   /* Peek at the next token.  */
24503   token = cp_lexer_peek_token (parser->lexer);
24504   /* If it's not a `)', then there is a type-id-list.  */
24505   if (token->type != CPP_CLOSE_PAREN)
24506     {
24507       /* Types may not be defined in an exception-specification.  */
24508       saved_message = parser->type_definition_forbidden_message;
24509       parser->type_definition_forbidden_message
24510         = G_("types may not be defined in an exception-specification");
24511       /* Parse the type-id-list.  */
24512       type_id_list = cp_parser_type_id_list (parser);
24513       /* Restore the saved message.  */
24514       parser->type_definition_forbidden_message = saved_message;
24515
24516       if (cxx_dialect >= cxx17)
24517         {
24518           error_at (loc, "ISO C++17 does not allow dynamic exception "
24519                          "specifications");
24520           type_id_list = NULL_TREE;
24521         }
24522       else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24523         warning_at (loc, OPT_Wdeprecated,
24524                     "dynamic exception specifications are deprecated in "
24525                     "C++11");
24526     }
24527   /* In C++17, throw() is equivalent to noexcept (true).  throw()
24528      is deprecated in C++11 and above as well, but is still widely used,
24529      so don't warn about it yet.  */
24530   else if (cxx_dialect >= cxx17)
24531     type_id_list = noexcept_true_spec;
24532   else
24533     type_id_list = empty_except_spec;
24534
24535   /* Look for the `)'.  */
24536   parens.require_close (parser);
24537
24538   return type_id_list;
24539 }
24540
24541 /* Parse an (optional) type-id-list.
24542
24543    type-id-list:
24544      type-id ... [opt]
24545      type-id-list , type-id ... [opt]
24546
24547    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
24548    in the order that the types were presented.  */
24549
24550 static tree
24551 cp_parser_type_id_list (cp_parser* parser)
24552 {
24553   tree types = NULL_TREE;
24554
24555   while (true)
24556     {
24557       cp_token *token;
24558       tree type;
24559
24560       token = cp_lexer_peek_token (parser->lexer);
24561
24562       /* Get the next type-id.  */
24563       type = cp_parser_type_id (parser);
24564       /* Check for invalid 'auto'.  */
24565       if (flag_concepts && type_uses_auto (type))
24566         {
24567           error_at (token->location,
24568                     "invalid use of %<auto%> in exception-specification");
24569           type = error_mark_node;
24570         }
24571       /* Parse the optional ellipsis. */
24572       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24573         {
24574           /* Consume the `...'. */
24575           cp_lexer_consume_token (parser->lexer);
24576
24577           /* Turn the type into a pack expansion expression. */
24578           type = make_pack_expansion (type);
24579         }
24580       /* Add it to the list.  */
24581       types = add_exception_specifier (types, type, /*complain=*/1);
24582       /* Peek at the next token.  */
24583       token = cp_lexer_peek_token (parser->lexer);
24584       /* If it is not a `,', we are done.  */
24585       if (token->type != CPP_COMMA)
24586         break;
24587       /* Consume the `,'.  */
24588       cp_lexer_consume_token (parser->lexer);
24589     }
24590
24591   return nreverse (types);
24592 }
24593
24594 /* Parse a try-block.
24595
24596    try-block:
24597      try compound-statement handler-seq  */
24598
24599 static tree
24600 cp_parser_try_block (cp_parser* parser)
24601 {
24602   tree try_block;
24603
24604   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24605   if (parser->in_function_body
24606       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24607     error ("%<try%> in %<constexpr%> function");
24608
24609   try_block = begin_try_block ();
24610   cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24611   finish_try_block (try_block);
24612   cp_parser_handler_seq (parser);
24613   finish_handler_sequence (try_block);
24614
24615   return try_block;
24616 }
24617
24618 /* Parse a function-try-block.
24619
24620    function-try-block:
24621      try ctor-initializer [opt] function-body handler-seq  */
24622
24623 static void
24624 cp_parser_function_try_block (cp_parser* parser)
24625 {
24626   tree compound_stmt;
24627   tree try_block;
24628
24629   /* Look for the `try' keyword.  */
24630   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24631     return;
24632   /* Let the rest of the front end know where we are.  */
24633   try_block = begin_function_try_block (&compound_stmt);
24634   /* Parse the function-body.  */
24635   cp_parser_ctor_initializer_opt_and_function_body
24636     (parser, /*in_function_try_block=*/true);
24637   /* We're done with the `try' part.  */
24638   finish_function_try_block (try_block);
24639   /* Parse the handlers.  */
24640   cp_parser_handler_seq (parser);
24641   /* We're done with the handlers.  */
24642   finish_function_handler_sequence (try_block, compound_stmt);
24643 }
24644
24645 /* Parse a handler-seq.
24646
24647    handler-seq:
24648      handler handler-seq [opt]  */
24649
24650 static void
24651 cp_parser_handler_seq (cp_parser* parser)
24652 {
24653   while (true)
24654     {
24655       cp_token *token;
24656
24657       /* Parse the handler.  */
24658       cp_parser_handler (parser);
24659       /* Peek at the next token.  */
24660       token = cp_lexer_peek_token (parser->lexer);
24661       /* If it's not `catch' then there are no more handlers.  */
24662       if (!cp_parser_is_keyword (token, RID_CATCH))
24663         break;
24664     }
24665 }
24666
24667 /* Parse a handler.
24668
24669    handler:
24670      catch ( exception-declaration ) compound-statement  */
24671
24672 static void
24673 cp_parser_handler (cp_parser* parser)
24674 {
24675   tree handler;
24676   tree declaration;
24677
24678   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24679   handler = begin_handler ();
24680   matching_parens parens;
24681   parens.require_open (parser);
24682   declaration = cp_parser_exception_declaration (parser);
24683   finish_handler_parms (declaration, handler);
24684   parens.require_close (parser);
24685   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24686   finish_handler (handler);
24687 }
24688
24689 /* Parse an exception-declaration.
24690
24691    exception-declaration:
24692      type-specifier-seq declarator
24693      type-specifier-seq abstract-declarator
24694      type-specifier-seq
24695      ...
24696
24697    Returns a VAR_DECL for the declaration, or NULL_TREE if the
24698    ellipsis variant is used.  */
24699
24700 static tree
24701 cp_parser_exception_declaration (cp_parser* parser)
24702 {
24703   cp_decl_specifier_seq type_specifiers;
24704   cp_declarator *declarator;
24705   const char *saved_message;
24706
24707   /* If it's an ellipsis, it's easy to handle.  */
24708   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24709     {
24710       /* Consume the `...' token.  */
24711       cp_lexer_consume_token (parser->lexer);
24712       return NULL_TREE;
24713     }
24714
24715   /* Types may not be defined in exception-declarations.  */
24716   saved_message = parser->type_definition_forbidden_message;
24717   parser->type_definition_forbidden_message
24718     = G_("types may not be defined in exception-declarations");
24719
24720   /* Parse the type-specifier-seq.  */
24721   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24722                                 /*is_trailing_return=*/false,
24723                                 &type_specifiers);
24724   /* If it's a `)', then there is no declarator.  */
24725   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24726     declarator = NULL;
24727   else
24728     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24729                                        /*ctor_dtor_or_conv_p=*/NULL,
24730                                        /*parenthesized_p=*/NULL,
24731                                        /*member_p=*/false,
24732                                        /*friend_p=*/false);
24733
24734   /* Restore the saved message.  */
24735   parser->type_definition_forbidden_message = saved_message;
24736
24737   if (!type_specifiers.any_specifiers_p)
24738     return error_mark_node;
24739
24740   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24741 }
24742
24743 /* Parse a throw-expression.
24744
24745    throw-expression:
24746      throw assignment-expression [opt]
24747
24748    Returns a THROW_EXPR representing the throw-expression.  */
24749
24750 static tree
24751 cp_parser_throw_expression (cp_parser* parser)
24752 {
24753   tree expression;
24754   cp_token* token;
24755
24756   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24757   token = cp_lexer_peek_token (parser->lexer);
24758   /* Figure out whether or not there is an assignment-expression
24759      following the "throw" keyword.  */
24760   if (token->type == CPP_COMMA
24761       || token->type == CPP_SEMICOLON
24762       || token->type == CPP_CLOSE_PAREN
24763       || token->type == CPP_CLOSE_SQUARE
24764       || token->type == CPP_CLOSE_BRACE
24765       || token->type == CPP_COLON)
24766     expression = NULL_TREE;
24767   else
24768     expression = cp_parser_assignment_expression (parser);
24769
24770   return build_throw (expression);
24771 }
24772
24773 /* GNU Extensions */
24774
24775 /* Parse an (optional) asm-specification.
24776
24777    asm-specification:
24778      asm ( string-literal )
24779
24780    If the asm-specification is present, returns a STRING_CST
24781    corresponding to the string-literal.  Otherwise, returns
24782    NULL_TREE.  */
24783
24784 static tree
24785 cp_parser_asm_specification_opt (cp_parser* parser)
24786 {
24787   cp_token *token;
24788   tree asm_specification;
24789
24790   /* Peek at the next token.  */
24791   token = cp_lexer_peek_token (parser->lexer);
24792   /* If the next token isn't the `asm' keyword, then there's no
24793      asm-specification.  */
24794   if (!cp_parser_is_keyword (token, RID_ASM))
24795     return NULL_TREE;
24796
24797   /* Consume the `asm' token.  */
24798   cp_lexer_consume_token (parser->lexer);
24799   /* Look for the `('.  */
24800   matching_parens parens;
24801   parens.require_open (parser);
24802
24803   /* Look for the string-literal.  */
24804   asm_specification = cp_parser_string_literal (parser, false, false);
24805
24806   /* Look for the `)'.  */
24807   parens.require_close (parser);
24808
24809   return asm_specification;
24810 }
24811
24812 /* Parse an asm-operand-list.
24813
24814    asm-operand-list:
24815      asm-operand
24816      asm-operand-list , asm-operand
24817
24818    asm-operand:
24819      string-literal ( expression )
24820      [ string-literal ] string-literal ( expression )
24821
24822    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
24823    each node is the expression.  The TREE_PURPOSE is itself a
24824    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24825    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24826    is a STRING_CST for the string literal before the parenthesis. Returns
24827    ERROR_MARK_NODE if any of the operands are invalid.  */
24828
24829 static tree
24830 cp_parser_asm_operand_list (cp_parser* parser)
24831 {
24832   tree asm_operands = NULL_TREE;
24833   bool invalid_operands = false;
24834
24835   while (true)
24836     {
24837       tree string_literal;
24838       tree expression;
24839       tree name;
24840
24841       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24842         {
24843           /* Consume the `[' token.  */
24844           cp_lexer_consume_token (parser->lexer);
24845           /* Read the operand name.  */
24846           name = cp_parser_identifier (parser);
24847           if (name != error_mark_node)
24848             name = build_string (IDENTIFIER_LENGTH (name),
24849                                  IDENTIFIER_POINTER (name));
24850           /* Look for the closing `]'.  */
24851           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24852         }
24853       else
24854         name = NULL_TREE;
24855       /* Look for the string-literal.  */
24856       string_literal = cp_parser_string_literal (parser, false, false);
24857
24858       /* Look for the `('.  */
24859       matching_parens parens;
24860       parens.require_open (parser);
24861       /* Parse the expression.  */
24862       expression = cp_parser_expression (parser);
24863       /* Look for the `)'.  */
24864       parens.require_close (parser);
24865
24866       if (name == error_mark_node 
24867           || string_literal == error_mark_node 
24868           || expression == error_mark_node)
24869         invalid_operands = true;
24870
24871       /* Add this operand to the list.  */
24872       asm_operands = tree_cons (build_tree_list (name, string_literal),
24873                                 expression,
24874                                 asm_operands);
24875       /* If the next token is not a `,', there are no more
24876          operands.  */
24877       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24878         break;
24879       /* Consume the `,'.  */
24880       cp_lexer_consume_token (parser->lexer);
24881     }
24882
24883   return invalid_operands ? error_mark_node : nreverse (asm_operands);
24884 }
24885
24886 /* Parse an asm-clobber-list.
24887
24888    asm-clobber-list:
24889      string-literal
24890      asm-clobber-list , string-literal
24891
24892    Returns a TREE_LIST, indicating the clobbers in the order that they
24893    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
24894
24895 static tree
24896 cp_parser_asm_clobber_list (cp_parser* parser)
24897 {
24898   tree clobbers = NULL_TREE;
24899
24900   while (true)
24901     {
24902       tree string_literal;
24903
24904       /* Look for the string literal.  */
24905       string_literal = cp_parser_string_literal (parser, false, false);
24906       /* Add it to the list.  */
24907       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24908       /* If the next token is not a `,', then the list is
24909          complete.  */
24910       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24911         break;
24912       /* Consume the `,' token.  */
24913       cp_lexer_consume_token (parser->lexer);
24914     }
24915
24916   return clobbers;
24917 }
24918
24919 /* Parse an asm-label-list.
24920
24921    asm-label-list:
24922      identifier
24923      asm-label-list , identifier
24924
24925    Returns a TREE_LIST, indicating the labels in the order that they
24926    appeared.  The TREE_VALUE of each node is a label.  */
24927
24928 static tree
24929 cp_parser_asm_label_list (cp_parser* parser)
24930 {
24931   tree labels = NULL_TREE;
24932
24933   while (true)
24934     {
24935       tree identifier, label, name;
24936
24937       /* Look for the identifier.  */
24938       identifier = cp_parser_identifier (parser);
24939       if (!error_operand_p (identifier))
24940         {
24941           label = lookup_label (identifier);
24942           if (TREE_CODE (label) == LABEL_DECL)
24943             {
24944               TREE_USED (label) = 1;
24945               check_goto (label);
24946               name = build_string (IDENTIFIER_LENGTH (identifier),
24947                                    IDENTIFIER_POINTER (identifier));
24948               labels = tree_cons (name, label, labels);
24949             }
24950         }
24951       /* If the next token is not a `,', then the list is
24952          complete.  */
24953       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24954         break;
24955       /* Consume the `,' token.  */
24956       cp_lexer_consume_token (parser->lexer);
24957     }
24958
24959   return nreverse (labels);
24960 }
24961
24962 /* Return TRUE iff the next tokens in the stream are possibly the
24963    beginning of a GNU extension attribute. */
24964
24965 static bool
24966 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24967 {
24968   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24969 }
24970
24971 /* Return TRUE iff the next tokens in the stream are possibly the
24972    beginning of a standard C++-11 attribute specifier.  */
24973
24974 static bool
24975 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24976 {
24977   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24978 }
24979
24980 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24981    beginning of a standard C++-11 attribute specifier.  */
24982
24983 static bool
24984 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24985 {
24986   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24987
24988   return (cxx_dialect >= cxx11
24989           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24990               || (token->type == CPP_OPEN_SQUARE
24991                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24992                   && token->type == CPP_OPEN_SQUARE)));
24993 }
24994
24995 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24996    beginning of a GNU extension attribute.  */
24997
24998 static bool
24999 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25000 {
25001   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25002
25003   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25004 }
25005
25006 /* Return true iff the next tokens can be the beginning of either a
25007    GNU attribute list, or a standard C++11 attribute sequence.  */
25008
25009 static bool
25010 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25011 {
25012   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25013           || cp_next_tokens_can_be_std_attribute_p (parser));
25014 }
25015
25016 /* Return true iff the next Nth tokens can be the beginning of either
25017    a GNU attribute list, or a standard C++11 attribute sequence.  */
25018
25019 static bool
25020 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25021 {
25022   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25023           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25024 }
25025
25026 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25027    of GNU attributes, or return NULL.  */
25028
25029 static tree
25030 cp_parser_attributes_opt (cp_parser *parser)
25031 {
25032   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25033     return cp_parser_gnu_attributes_opt (parser);
25034   return cp_parser_std_attribute_spec_seq (parser);
25035 }
25036
25037 /* Parse an (optional) series of attributes.
25038
25039    attributes:
25040      attributes attribute
25041
25042    attribute:
25043      __attribute__ (( attribute-list [opt] ))
25044
25045    The return value is as for cp_parser_gnu_attribute_list.  */
25046
25047 static tree
25048 cp_parser_gnu_attributes_opt (cp_parser* parser)
25049 {
25050   tree attributes = NULL_TREE;
25051
25052   temp_override<bool> cleanup
25053     (parser->auto_is_implicit_function_template_parm_p, false);
25054
25055   while (true)
25056     {
25057       cp_token *token;
25058       tree attribute_list;
25059       bool ok = true;
25060
25061       /* Peek at the next token.  */
25062       token = cp_lexer_peek_token (parser->lexer);
25063       /* If it's not `__attribute__', then we're done.  */
25064       if (token->keyword != RID_ATTRIBUTE)
25065         break;
25066
25067       /* Consume the `__attribute__' keyword.  */
25068       cp_lexer_consume_token (parser->lexer);
25069       /* Look for the two `(' tokens.  */
25070       matching_parens outer_parens;
25071       outer_parens.require_open (parser);
25072       matching_parens inner_parens;
25073       inner_parens.require_open (parser);
25074
25075       /* Peek at the next token.  */
25076       token = cp_lexer_peek_token (parser->lexer);
25077       if (token->type != CPP_CLOSE_PAREN)
25078         /* Parse the attribute-list.  */
25079         attribute_list = cp_parser_gnu_attribute_list (parser);
25080       else
25081         /* If the next token is a `)', then there is no attribute
25082            list.  */
25083         attribute_list = NULL;
25084
25085       /* Look for the two `)' tokens.  */
25086       if (!inner_parens.require_close (parser))
25087         ok = false;
25088       if (!outer_parens.require_close (parser))
25089         ok = false;
25090       if (!ok)
25091         cp_parser_skip_to_end_of_statement (parser);
25092
25093       /* Add these new attributes to the list.  */
25094       attributes = attr_chainon (attributes, attribute_list);
25095     }
25096
25097   return attributes;
25098 }
25099
25100 /* Parse a GNU attribute-list.
25101
25102    attribute-list:
25103      attribute
25104      attribute-list , attribute
25105
25106    attribute:
25107      identifier
25108      identifier ( identifier )
25109      identifier ( identifier , expression-list )
25110      identifier ( expression-list )
25111
25112    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
25113    to an attribute.  The TREE_PURPOSE of each node is the identifier
25114    indicating which attribute is in use.  The TREE_VALUE represents
25115    the arguments, if any.  */
25116
25117 static tree
25118 cp_parser_gnu_attribute_list (cp_parser* parser)
25119 {
25120   tree attribute_list = NULL_TREE;
25121   bool save_translate_strings_p = parser->translate_strings_p;
25122
25123   parser->translate_strings_p = false;
25124   while (true)
25125     {
25126       cp_token *token;
25127       tree identifier;
25128       tree attribute;
25129
25130       /* Look for the identifier.  We also allow keywords here; for
25131          example `__attribute__ ((const))' is legal.  */
25132       token = cp_lexer_peek_token (parser->lexer);
25133       if (token->type == CPP_NAME
25134           || token->type == CPP_KEYWORD)
25135         {
25136           tree arguments = NULL_TREE;
25137
25138           /* Consume the token, but save it since we need it for the
25139              SIMD enabled function parsing.  */
25140           cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25141
25142           /* Save away the identifier that indicates which attribute
25143              this is.  */
25144           identifier = (token->type == CPP_KEYWORD) 
25145             /* For keywords, use the canonical spelling, not the
25146                parsed identifier.  */
25147             ? ridpointers[(int) token->keyword]
25148             : id_token->u.value;
25149
25150           identifier = canonicalize_attr_name (identifier);
25151           attribute = build_tree_list (identifier, NULL_TREE);
25152
25153           /* Peek at the next token.  */
25154           token = cp_lexer_peek_token (parser->lexer);
25155           /* If it's an `(', then parse the attribute arguments.  */
25156           if (token->type == CPP_OPEN_PAREN)
25157             {
25158               vec<tree, va_gc> *vec;
25159               int attr_flag = (attribute_takes_identifier_p (identifier)
25160                                ? id_attr : normal_attr);
25161               vec = cp_parser_parenthesized_expression_list 
25162                     (parser, attr_flag, /*cast_p=*/false, 
25163                     /*allow_expansion_p=*/false, 
25164                     /*non_constant_p=*/NULL);
25165               if (vec == NULL)
25166                 arguments = error_mark_node;
25167               else
25168                 {
25169                   arguments = build_tree_list_vec (vec);
25170                   release_tree_vector (vec);
25171                 }
25172               /* Save the arguments away.  */
25173               TREE_VALUE (attribute) = arguments;
25174             }
25175
25176           if (arguments != error_mark_node)
25177             {
25178               /* Add this attribute to the list.  */
25179               TREE_CHAIN (attribute) = attribute_list;
25180               attribute_list = attribute;
25181             }
25182
25183           token = cp_lexer_peek_token (parser->lexer);
25184         }
25185       /* Now, look for more attributes.  If the next token isn't a
25186          `,', we're done.  */
25187       if (token->type != CPP_COMMA)
25188         break;
25189
25190       /* Consume the comma and keep going.  */
25191       cp_lexer_consume_token (parser->lexer);
25192     }
25193   parser->translate_strings_p = save_translate_strings_p;
25194
25195   /* We built up the list in reverse order.  */
25196   return nreverse (attribute_list);
25197 }
25198
25199 /*  Parse a standard C++11 attribute.
25200
25201     The returned representation is a TREE_LIST which TREE_PURPOSE is
25202     the scoped name of the attribute, and the TREE_VALUE is its
25203     arguments list.
25204
25205     Note that the scoped name of the attribute is itself a TREE_LIST
25206     which TREE_PURPOSE is the namespace of the attribute, and
25207     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
25208     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25209     and which TREE_PURPOSE is directly the attribute name.
25210
25211     Clients of the attribute code should use get_attribute_namespace
25212     and get_attribute_name to get the actual namespace and name of
25213     attributes, regardless of their being GNU or C++11 attributes.
25214
25215     attribute:
25216       attribute-token attribute-argument-clause [opt]
25217
25218     attribute-token:
25219       identifier
25220       attribute-scoped-token
25221
25222     attribute-scoped-token:
25223       attribute-namespace :: identifier
25224
25225     attribute-namespace:
25226       identifier
25227
25228     attribute-argument-clause:
25229       ( balanced-token-seq )
25230
25231     balanced-token-seq:
25232       balanced-token [opt]
25233       balanced-token-seq balanced-token
25234
25235     balanced-token:
25236       ( balanced-token-seq )
25237       [ balanced-token-seq ]
25238       { balanced-token-seq }.  */
25239
25240 static tree
25241 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25242 {
25243   tree attribute, attr_id = NULL_TREE, arguments;
25244   cp_token *token;
25245
25246   temp_override<bool> cleanup
25247     (parser->auto_is_implicit_function_template_parm_p, false);
25248
25249   /* First, parse name of the attribute, a.k.a attribute-token.  */
25250
25251   token = cp_lexer_peek_token (parser->lexer);
25252   if (token->type == CPP_NAME)
25253     attr_id = token->u.value;
25254   else if (token->type == CPP_KEYWORD)
25255     attr_id = ridpointers[(int) token->keyword];
25256   else if (token->flags & NAMED_OP)
25257     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25258
25259   if (attr_id == NULL_TREE)
25260     return NULL_TREE;
25261
25262   cp_lexer_consume_token (parser->lexer);
25263
25264   token = cp_lexer_peek_token (parser->lexer);
25265   if (token->type == CPP_SCOPE)
25266     {
25267       /* We are seeing a scoped attribute token.  */
25268
25269       cp_lexer_consume_token (parser->lexer);
25270       if (attr_ns)
25271         error_at (token->location, "attribute using prefix used together "
25272                                    "with scoped attribute token");
25273       attr_ns = attr_id;
25274
25275       token = cp_lexer_consume_token (parser->lexer);
25276       if (token->type == CPP_NAME)
25277         attr_id = token->u.value;
25278       else if (token->type == CPP_KEYWORD)
25279         attr_id = ridpointers[(int) token->keyword];
25280       else if (token->flags & NAMED_OP)
25281         attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25282       else
25283         {
25284           error_at (token->location,
25285                     "expected an identifier for the attribute name");
25286           return error_mark_node;
25287         }
25288
25289       attr_ns = canonicalize_attr_name (attr_ns);
25290       attr_id = canonicalize_attr_name (attr_id);
25291       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25292                                    NULL_TREE);
25293       token = cp_lexer_peek_token (parser->lexer);
25294     }
25295   else if (attr_ns)
25296     {
25297       attr_ns = canonicalize_attr_name (attr_ns);
25298       attr_id = canonicalize_attr_name (attr_id);
25299       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25300                                    NULL_TREE);
25301     }
25302   else
25303     {
25304       attr_id = canonicalize_attr_name (attr_id);
25305       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25306                                    NULL_TREE);
25307       /* C++11 noreturn attribute is equivalent to GNU's.  */
25308       if (is_attribute_p ("noreturn", attr_id))
25309         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25310       /* C++14 deprecated attribute is equivalent to GNU's.  */
25311       else if (is_attribute_p ("deprecated", attr_id))
25312         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25313       /* C++17 fallthrough attribute is equivalent to GNU's.  */
25314       else if (is_attribute_p ("fallthrough", attr_id))
25315         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25316       /* Transactional Memory TS optimize_for_synchronized attribute is
25317          equivalent to GNU transaction_callable.  */
25318       else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25319         TREE_PURPOSE (attribute)
25320           = get_identifier ("transaction_callable");
25321       /* Transactional Memory attributes are GNU attributes.  */
25322       else if (tm_attr_to_mask (attr_id))
25323         TREE_PURPOSE (attribute) = attr_id;
25324     }
25325
25326   /* Now parse the optional argument clause of the attribute.  */
25327
25328   if (token->type != CPP_OPEN_PAREN)
25329     return attribute;
25330
25331   {
25332     vec<tree, va_gc> *vec;
25333     int attr_flag = normal_attr;
25334
25335     if (attr_ns == get_identifier ("gnu")
25336         && attribute_takes_identifier_p (attr_id))
25337       /* A GNU attribute that takes an identifier in parameter.  */
25338       attr_flag = id_attr;
25339
25340     vec = cp_parser_parenthesized_expression_list
25341       (parser, attr_flag, /*cast_p=*/false,
25342        /*allow_expansion_p=*/true,
25343        /*non_constant_p=*/NULL);
25344     if (vec == NULL)
25345       arguments = error_mark_node;
25346     else
25347       {
25348         arguments = build_tree_list_vec (vec);
25349         release_tree_vector (vec);
25350       }
25351
25352     if (arguments == error_mark_node)
25353       attribute = error_mark_node;
25354     else
25355       TREE_VALUE (attribute) = arguments;
25356   }
25357
25358   return attribute;
25359 }
25360
25361 /* Check that the attribute ATTRIBUTE appears at most once in the
25362    attribute-list ATTRIBUTES.  This is enforced for noreturn (7.6.3)
25363    and deprecated (7.6.5).  Note that carries_dependency (7.6.4)
25364    isn't implemented yet in GCC.  */
25365
25366 static void
25367 cp_parser_check_std_attribute (tree attributes, tree attribute)
25368 {
25369   if (attributes)
25370     {
25371       tree name = get_attribute_name (attribute);
25372       if (is_attribute_p ("noreturn", name)
25373           && lookup_attribute ("noreturn", attributes))
25374         error ("attribute %<noreturn%> can appear at most once "
25375                "in an attribute-list");
25376       else if (is_attribute_p ("deprecated", name)
25377                && lookup_attribute ("deprecated", attributes))
25378         error ("attribute %<deprecated%> can appear at most once "
25379                "in an attribute-list");
25380     }
25381 }
25382
25383 /* Parse a list of standard C++-11 attributes.
25384
25385    attribute-list:
25386      attribute [opt]
25387      attribute-list , attribute[opt]
25388      attribute ...
25389      attribute-list , attribute ...
25390 */
25391
25392 static tree
25393 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25394 {
25395   tree attributes = NULL_TREE, attribute = NULL_TREE;
25396   cp_token *token = NULL;
25397
25398   while (true)
25399     {
25400       attribute = cp_parser_std_attribute (parser, attr_ns);
25401       if (attribute == error_mark_node)
25402         break;
25403       if (attribute != NULL_TREE)
25404         {
25405           cp_parser_check_std_attribute (attributes, attribute);
25406           TREE_CHAIN (attribute) = attributes;
25407           attributes = attribute;
25408         }
25409       token = cp_lexer_peek_token (parser->lexer);
25410       if (token->type == CPP_ELLIPSIS)
25411         {
25412           cp_lexer_consume_token (parser->lexer);
25413           if (attribute == NULL_TREE)
25414             error_at (token->location,
25415                       "expected attribute before %<...%>");
25416           else
25417             {
25418               tree pack = make_pack_expansion (TREE_VALUE (attribute));
25419               if (pack == error_mark_node)
25420                 return error_mark_node;
25421               TREE_VALUE (attribute) = pack;
25422             }
25423           token = cp_lexer_peek_token (parser->lexer);
25424         }
25425       if (token->type != CPP_COMMA)
25426         break;
25427       cp_lexer_consume_token (parser->lexer);
25428     }
25429   attributes = nreverse (attributes);
25430   return attributes;
25431 }
25432
25433 /* Parse a standard C++-11 attribute specifier.
25434
25435    attribute-specifier:
25436      [ [ attribute-using-prefix [opt] attribute-list ] ]
25437      alignment-specifier
25438
25439    attribute-using-prefix:
25440      using attribute-namespace :
25441
25442    alignment-specifier:
25443      alignas ( type-id ... [opt] )
25444      alignas ( alignment-expression ... [opt] ).  */
25445
25446 static tree
25447 cp_parser_std_attribute_spec (cp_parser *parser)
25448 {
25449   tree attributes = NULL_TREE;
25450   cp_token *token = cp_lexer_peek_token (parser->lexer);
25451
25452   if (token->type == CPP_OPEN_SQUARE
25453       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25454     {
25455       tree attr_ns = NULL_TREE;
25456
25457       cp_lexer_consume_token (parser->lexer);
25458       cp_lexer_consume_token (parser->lexer);
25459
25460       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25461         {
25462           token = cp_lexer_peek_nth_token (parser->lexer, 2);
25463           if (token->type == CPP_NAME)
25464             attr_ns = token->u.value;
25465           else if (token->type == CPP_KEYWORD)
25466             attr_ns = ridpointers[(int) token->keyword];
25467           else if (token->flags & NAMED_OP)
25468             attr_ns = get_identifier (cpp_type2name (token->type,
25469                                                      token->flags));
25470           if (attr_ns
25471               && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25472             {
25473               if (cxx_dialect < cxx17
25474                   && !in_system_header_at (input_location))
25475                 pedwarn (input_location, 0,
25476                          "attribute using prefix only available "
25477                          "with -std=c++17 or -std=gnu++17");
25478
25479               cp_lexer_consume_token (parser->lexer);
25480               cp_lexer_consume_token (parser->lexer);
25481               cp_lexer_consume_token (parser->lexer);
25482             }
25483           else
25484             attr_ns = NULL_TREE;
25485         }
25486
25487       attributes = cp_parser_std_attribute_list (parser, attr_ns);
25488
25489       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25490           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25491         cp_parser_skip_to_end_of_statement (parser);
25492       else
25493         /* Warn about parsing c++11 attribute in non-c++11 mode, only
25494            when we are sure that we have actually parsed them.  */
25495         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25496     }
25497   else
25498     {
25499       tree alignas_expr;
25500
25501       /* Look for an alignment-specifier.  */
25502
25503       token = cp_lexer_peek_token (parser->lexer);
25504
25505       if (token->type != CPP_KEYWORD
25506           || token->keyword != RID_ALIGNAS)
25507         return NULL_TREE;
25508
25509       cp_lexer_consume_token (parser->lexer);
25510       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25511
25512       matching_parens parens;
25513       if (!parens.require_open (parser))
25514         return error_mark_node;
25515
25516       cp_parser_parse_tentatively (parser);
25517       alignas_expr = cp_parser_type_id (parser);
25518
25519       if (!cp_parser_parse_definitely (parser))
25520         {
25521           alignas_expr = cp_parser_assignment_expression (parser);
25522           if (alignas_expr == error_mark_node)
25523             cp_parser_skip_to_end_of_statement (parser);
25524           if (alignas_expr == NULL_TREE
25525               || alignas_expr == error_mark_node)
25526             return alignas_expr;
25527         }
25528
25529       alignas_expr = cxx_alignas_expr (alignas_expr);
25530       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25531
25532       /* Handle alignas (pack...).  */
25533       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25534         {
25535           cp_lexer_consume_token (parser->lexer);
25536           alignas_expr = make_pack_expansion (alignas_expr);
25537         }
25538
25539       /* Something went wrong, so don't build the attribute.  */
25540       if (alignas_expr == error_mark_node)
25541         return error_mark_node;
25542
25543       if (!parens.require_close (parser))
25544         return error_mark_node;
25545
25546       /* Build the C++-11 representation of an 'aligned'
25547          attribute.  */
25548       attributes =
25549         build_tree_list (build_tree_list (get_identifier ("gnu"),
25550                                           get_identifier ("aligned")),
25551                          alignas_expr);
25552     }
25553
25554   return attributes;
25555 }
25556
25557 /* Parse a standard C++-11 attribute-specifier-seq.
25558
25559    attribute-specifier-seq:
25560      attribute-specifier-seq [opt] attribute-specifier
25561  */
25562
25563 static tree
25564 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25565 {
25566   tree attr_specs = NULL_TREE;
25567   tree attr_last = NULL_TREE;
25568
25569   while (true)
25570     {
25571       tree attr_spec = cp_parser_std_attribute_spec (parser);
25572       if (attr_spec == NULL_TREE)
25573         break;
25574       if (attr_spec == error_mark_node)
25575         return error_mark_node;
25576
25577       if (attr_last)
25578         TREE_CHAIN (attr_last) = attr_spec;
25579       else
25580         attr_specs = attr_last = attr_spec;
25581       attr_last = tree_last (attr_last);
25582     }
25583
25584   return attr_specs;
25585 }
25586
25587 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25588    return index of the first token after balanced-token, or N on failure.  */
25589
25590 static size_t
25591 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25592 {
25593   size_t orig_n = n;
25594   int nparens = 0, nbraces = 0, nsquares = 0;
25595   do
25596     switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25597       {
25598       case CPP_EOF:
25599       case CPP_PRAGMA_EOL:
25600         /* Ran out of tokens.  */
25601         return orig_n;
25602       case CPP_OPEN_PAREN:
25603         ++nparens;
25604         break;
25605       case CPP_OPEN_BRACE:
25606         ++nbraces;
25607         break;
25608       case CPP_OPEN_SQUARE:
25609         ++nsquares;
25610         break;
25611       case CPP_CLOSE_PAREN:
25612         --nparens;
25613         break;
25614       case CPP_CLOSE_BRACE:
25615         --nbraces;
25616         break;
25617       case CPP_CLOSE_SQUARE:
25618         --nsquares;
25619         break;
25620       default:
25621         break;
25622       }
25623   while (nparens || nbraces || nsquares);
25624   return n;
25625 }
25626
25627 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25628    return index of the first token after the GNU attribute tokens, or N on
25629    failure.  */
25630
25631 static size_t
25632 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25633 {
25634   while (true)
25635     {
25636       if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25637           || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25638           || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25639         break;
25640
25641       size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25642       if (n2 == n + 2)
25643         break;
25644       if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25645         break;
25646       n = n2 + 1;
25647     }
25648   return n;
25649 }
25650
25651 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25652    next token), return index of the first token after the standard C++11
25653    attribute tokens, or N on failure.  */
25654
25655 static size_t
25656 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25657 {
25658   while (true)
25659     {
25660       if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25661           && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25662         {
25663           size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25664           if (n2 == n + 1)
25665             break;
25666           if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25667             break;
25668           n = n2 + 1;
25669         }
25670       else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25671                && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25672         {
25673           size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25674           if (n2 == n + 1)
25675             break;
25676           n = n2;
25677         }
25678       else
25679         break;
25680     }
25681   return n;
25682 }
25683
25684 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25685    as the next token), return index of the first token after the attribute
25686    tokens, or N on failure.  */
25687
25688 static size_t
25689 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25690 {
25691   if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25692     return cp_parser_skip_gnu_attributes_opt (parser, n);
25693   return cp_parser_skip_std_attribute_spec_seq (parser, n);
25694 }
25695
25696 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
25697    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
25698    current value of the PEDANTIC flag, regardless of whether or not
25699    the `__extension__' keyword is present.  The caller is responsible
25700    for restoring the value of the PEDANTIC flag.  */
25701
25702 static bool
25703 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25704 {
25705   /* Save the old value of the PEDANTIC flag.  */
25706   *saved_pedantic = pedantic;
25707
25708   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25709     {
25710       /* Consume the `__extension__' token.  */
25711       cp_lexer_consume_token (parser->lexer);
25712       /* We're not being pedantic while the `__extension__' keyword is
25713          in effect.  */
25714       pedantic = 0;
25715
25716       return true;
25717     }
25718
25719   return false;
25720 }
25721
25722 /* Parse a label declaration.
25723
25724    label-declaration:
25725      __label__ label-declarator-seq ;
25726
25727    label-declarator-seq:
25728      identifier , label-declarator-seq
25729      identifier  */
25730
25731 static void
25732 cp_parser_label_declaration (cp_parser* parser)
25733 {
25734   /* Look for the `__label__' keyword.  */
25735   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25736
25737   while (true)
25738     {
25739       tree identifier;
25740
25741       /* Look for an identifier.  */
25742       identifier = cp_parser_identifier (parser);
25743       /* If we failed, stop.  */
25744       if (identifier == error_mark_node)
25745         break;
25746       /* Declare it as a label.  */
25747       finish_label_decl (identifier);
25748       /* If the next token is a `;', stop.  */
25749       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25750         break;
25751       /* Look for the `,' separating the label declarations.  */
25752       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25753     }
25754
25755   /* Look for the final `;'.  */
25756   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25757 }
25758
25759 // -------------------------------------------------------------------------- //
25760 // Requires Clause
25761
25762 // Parse a requires clause.
25763 //
25764 //    requires-clause:
25765 //      'requires' logical-or-expression
25766 //
25767 // The required logical-or-expression must be a constant expression. Note
25768 // that we don't check that the expression is constepxr here. We defer until
25769 // we analyze constraints and then, we only check atomic constraints.
25770 static tree
25771 cp_parser_requires_clause (cp_parser *parser)
25772 {
25773   // Parse the requires clause so that it is not automatically folded.
25774   ++processing_template_decl;
25775   tree expr = cp_parser_binary_expression (parser, false, false,
25776                                            PREC_NOT_OPERATOR, NULL);
25777   if (check_for_bare_parameter_packs (expr))
25778     expr = error_mark_node;
25779   --processing_template_decl;
25780   return expr;
25781 }
25782
25783 // Optionally parse a requires clause:
25784 static tree
25785 cp_parser_requires_clause_opt (cp_parser *parser)
25786 {
25787   cp_token *tok = cp_lexer_peek_token (parser->lexer);
25788   if (tok->keyword != RID_REQUIRES)
25789     {
25790       if (!flag_concepts && tok->type == CPP_NAME
25791           && tok->u.value == ridpointers[RID_REQUIRES])
25792         {
25793           error_at (cp_lexer_peek_token (parser->lexer)->location,
25794                     "%<requires%> only available with -fconcepts");
25795           /* Parse and discard the requires-clause.  */
25796           cp_lexer_consume_token (parser->lexer);
25797           cp_parser_requires_clause (parser);
25798         }
25799       return NULL_TREE;
25800     }
25801   cp_lexer_consume_token (parser->lexer);
25802   return cp_parser_requires_clause (parser);
25803 }
25804
25805
25806 /*---------------------------------------------------------------------------
25807                            Requires expressions
25808 ---------------------------------------------------------------------------*/
25809
25810 /* Parse a requires expression
25811
25812    requirement-expression:
25813        'requires' requirement-parameter-list [opt] requirement-body */
25814 static tree
25815 cp_parser_requires_expression (cp_parser *parser)
25816 {
25817   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25818   location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25819
25820   /* A requires-expression shall appear only within a concept
25821      definition or a requires-clause.
25822
25823      TODO: Implement this diagnostic correctly. */
25824   if (!processing_template_decl)
25825     {
25826       error_at (loc, "a requires expression cannot appear outside a template");
25827       cp_parser_skip_to_end_of_statement (parser);
25828       return error_mark_node;
25829     }
25830
25831   tree parms, reqs;
25832   {
25833     /* Local parameters are delared as variables within the scope
25834        of the expression.  They are not visible past the end of
25835        the expression.  Expressions within the requires-expression
25836        are unevaluated.  */
25837     struct scope_sentinel
25838     {
25839       scope_sentinel ()
25840       {
25841         ++cp_unevaluated_operand;
25842         begin_scope (sk_block, NULL_TREE);
25843       }
25844
25845       ~scope_sentinel ()
25846       {
25847         pop_bindings_and_leave_scope ();
25848         --cp_unevaluated_operand;
25849       }
25850     } s;
25851
25852     /* Parse the optional parameter list. */
25853     if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25854       {
25855         parms = cp_parser_requirement_parameter_list (parser);
25856         if (parms == error_mark_node)
25857           return error_mark_node;
25858       }
25859     else
25860       parms = NULL_TREE;
25861
25862     /* Parse the requirement body. */
25863     reqs = cp_parser_requirement_body (parser);
25864     if (reqs == error_mark_node)
25865       return error_mark_node;
25866   }
25867
25868   /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25869      the parm chain.  */
25870   grokparms (parms, &parms);
25871   return finish_requires_expr (parms, reqs);
25872 }
25873
25874 /* Parse a parameterized requirement.
25875
25876    requirement-parameter-list:
25877        '(' parameter-declaration-clause ')' */
25878 static tree
25879 cp_parser_requirement_parameter_list (cp_parser *parser)
25880 {
25881   matching_parens parens;
25882   if (!parens.require_open (parser))
25883     return error_mark_node;
25884
25885   tree parms = cp_parser_parameter_declaration_clause (parser);
25886
25887   if (!parens.require_close (parser))
25888     return error_mark_node;
25889
25890   return parms;
25891 }
25892
25893 /* Parse the body of a requirement.
25894
25895    requirement-body:
25896        '{' requirement-list '}' */
25897 static tree
25898 cp_parser_requirement_body (cp_parser *parser)
25899 {
25900   matching_braces braces;
25901   if (!braces.require_open (parser))
25902     return error_mark_node;
25903
25904   tree reqs = cp_parser_requirement_list (parser);
25905
25906   if (!braces.require_close (parser))
25907     return error_mark_node;
25908
25909   return reqs;
25910 }
25911
25912 /* Parse a list of requirements.
25913
25914    requirement-list:
25915        requirement
25916        requirement-list ';' requirement[opt] */
25917 static tree
25918 cp_parser_requirement_list (cp_parser *parser)
25919 {
25920   tree result = NULL_TREE;
25921   while (true)
25922     {
25923       tree req = cp_parser_requirement (parser);
25924       if (req == error_mark_node)
25925         return error_mark_node;
25926
25927       result = tree_cons (NULL_TREE, req, result);
25928
25929       /* If we see a semi-colon, consume it. */
25930       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25931         cp_lexer_consume_token (parser->lexer);
25932
25933       /* Stop processing at the end of the list. */
25934       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25935         break;
25936     }
25937
25938   /* Reverse the order of requirements so they are analyzed in
25939      declaration order. */
25940   return nreverse (result);
25941 }
25942
25943 /* Parse a syntactic requirement or type requirement.
25944
25945      requirement:
25946        simple-requirement
25947        compound-requirement
25948        type-requirement
25949        nested-requirement */
25950 static tree
25951 cp_parser_requirement (cp_parser *parser)
25952 {
25953   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25954     return cp_parser_compound_requirement (parser);
25955   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25956     return cp_parser_type_requirement (parser);
25957   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25958     return cp_parser_nested_requirement (parser);
25959   else
25960     return cp_parser_simple_requirement (parser);
25961 }
25962
25963 /* Parse a simple requirement.
25964
25965      simple-requirement:
25966        expression ';' */
25967 static tree
25968 cp_parser_simple_requirement (cp_parser *parser)
25969 {
25970   tree expr = cp_parser_expression (parser, NULL, false, false);
25971   if (!expr || expr == error_mark_node)
25972     return error_mark_node;
25973
25974   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25975     return error_mark_node;
25976
25977   return finish_simple_requirement (expr);
25978 }
25979
25980 /* Parse a type requirement
25981
25982      type-requirement
25983          nested-name-specifier [opt] required-type-name ';'
25984
25985      required-type-name:
25986          type-name
25987          'template' [opt] simple-template-id  */
25988 static tree
25989 cp_parser_type_requirement (cp_parser *parser)
25990 {
25991   cp_lexer_consume_token (parser->lexer);
25992
25993   // Save the scope before parsing name specifiers.
25994   tree saved_scope = parser->scope;
25995   tree saved_object_scope = parser->object_scope;
25996   tree saved_qualifying_scope = parser->qualifying_scope;
25997   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25998   cp_parser_nested_name_specifier_opt (parser,
25999                                        /*typename_keyword_p=*/true,
26000                                        /*check_dependency_p=*/false,
26001                                        /*type_p=*/true,
26002                                        /*is_declaration=*/false);
26003
26004   tree type;
26005   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26006     {
26007       cp_lexer_consume_token (parser->lexer);
26008       type = cp_parser_template_id (parser,
26009                                     /*template_keyword_p=*/true,
26010                                     /*check_dependency=*/false,
26011                                     /*tag_type=*/none_type,
26012                                     /*is_declaration=*/false);
26013       type = make_typename_type (parser->scope, type, typename_type,
26014                                  /*complain=*/tf_error);
26015     }
26016   else
26017    type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26018
26019   if (TREE_CODE (type) == TYPE_DECL)
26020     type = TREE_TYPE (type);
26021
26022   parser->scope = saved_scope;
26023   parser->object_scope = saved_object_scope;
26024   parser->qualifying_scope = saved_qualifying_scope;
26025
26026   if (type == error_mark_node)
26027     cp_parser_skip_to_end_of_statement (parser);
26028
26029   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26030     return error_mark_node;
26031   if (type == error_mark_node)
26032     return error_mark_node;
26033
26034   return finish_type_requirement (type);
26035 }
26036
26037 /* Parse a compound requirement
26038
26039      compound-requirement:
26040          '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26041 static tree
26042 cp_parser_compound_requirement (cp_parser *parser)
26043 {
26044   /* Parse an expression enclosed in '{ }'s. */
26045   matching_braces braces;
26046   if (!braces.require_open (parser))
26047     return error_mark_node;
26048
26049   tree expr = cp_parser_expression (parser, NULL, false, false);
26050   if (!expr || expr == error_mark_node)
26051     return error_mark_node;
26052
26053   if (!braces.require_close (parser))
26054     return error_mark_node;
26055
26056   /* Parse the optional noexcept. */
26057   bool noexcept_p = false;
26058   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26059     {
26060       cp_lexer_consume_token (parser->lexer);
26061       noexcept_p = true;
26062     }
26063
26064   /* Parse the optional trailing return type. */
26065   tree type = NULL_TREE;
26066   if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26067     {
26068       cp_lexer_consume_token (parser->lexer);
26069       bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26070       parser->in_result_type_constraint_p = true;
26071       type = cp_parser_trailing_type_id (parser);
26072       parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26073       if (type == error_mark_node)
26074         return error_mark_node;
26075     }
26076
26077   return finish_compound_requirement (expr, type, noexcept_p);
26078 }
26079
26080 /* Parse a nested requirement. This is the same as a requires clause.
26081
26082    nested-requirement:
26083      requires-clause */
26084 static tree
26085 cp_parser_nested_requirement (cp_parser *parser)
26086 {
26087   cp_lexer_consume_token (parser->lexer);
26088   tree req = cp_parser_requires_clause (parser);
26089   if (req == error_mark_node)
26090     return error_mark_node;
26091   return finish_nested_requirement (req);
26092 }
26093
26094 /* Support Functions */
26095
26096 /* Return the appropriate prefer_type argument for lookup_name_real based on
26097    tag_type and template_mem_access.  */
26098
26099 static inline int
26100 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26101 {
26102   /* DR 141: When looking in the current enclosing context for a template-name
26103      after -> or ., only consider class templates.  */
26104   if (template_mem_access)
26105     return 2;
26106   switch (tag_type)
26107     {
26108     case none_type:  return 0;  // No preference.
26109     case scope_type: return 1;  // Type or namespace.
26110     default:         return 2;  // Type only.
26111     }
26112 }
26113
26114 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26115    NAME should have one of the representations used for an
26116    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26117    is returned.  If PARSER->SCOPE is a dependent type, then a
26118    SCOPE_REF is returned.
26119
26120    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26121    returned; the name was already resolved when the TEMPLATE_ID_EXPR
26122    was formed.  Abstractly, such entities should not be passed to this
26123    function, because they do not need to be looked up, but it is
26124    simpler to check for this special case here, rather than at the
26125    call-sites.
26126
26127    In cases not explicitly covered above, this function returns a
26128    DECL, OVERLOAD, or baselink representing the result of the lookup.
26129    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26130    is returned.
26131
26132    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26133    (e.g., "struct") that was used.  In that case bindings that do not
26134    refer to types are ignored.
26135
26136    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26137    ignored.
26138
26139    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26140    are ignored.
26141
26142    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26143    types.
26144
26145    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26146    TREE_LIST of candidates if name-lookup results in an ambiguity, and
26147    NULL_TREE otherwise.  */
26148
26149 static cp_expr
26150 cp_parser_lookup_name (cp_parser *parser, tree name,
26151                        enum tag_types tag_type,
26152                        bool is_template,
26153                        bool is_namespace,
26154                        bool check_dependency,
26155                        tree *ambiguous_decls,
26156                        location_t name_location)
26157 {
26158   tree decl;
26159   tree object_type = parser->context->object_type;
26160
26161   /* Assume that the lookup will be unambiguous.  */
26162   if (ambiguous_decls)
26163     *ambiguous_decls = NULL_TREE;
26164
26165   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26166      no longer valid.  Note that if we are parsing tentatively, and
26167      the parse fails, OBJECT_TYPE will be automatically restored.  */
26168   parser->context->object_type = NULL_TREE;
26169
26170   if (name == error_mark_node)
26171     return error_mark_node;
26172
26173   /* A template-id has already been resolved; there is no lookup to
26174      do.  */
26175   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26176     return name;
26177   if (BASELINK_P (name))
26178     {
26179       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26180                   == TEMPLATE_ID_EXPR);
26181       return name;
26182     }
26183
26184   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
26185      it should already have been checked to make sure that the name
26186      used matches the type being destroyed.  */
26187   if (TREE_CODE (name) == BIT_NOT_EXPR)
26188     {
26189       tree type;
26190
26191       /* Figure out to which type this destructor applies.  */
26192       if (parser->scope)
26193         type = parser->scope;
26194       else if (object_type)
26195         type = object_type;
26196       else
26197         type = current_class_type;
26198       /* If that's not a class type, there is no destructor.  */
26199       if (!type || !CLASS_TYPE_P (type))
26200         return error_mark_node;
26201
26202       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26203         lazily_declare_fn (sfk_destructor, type);
26204
26205       if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26206         return dtor;
26207
26208       return error_mark_node;
26209     }
26210
26211   /* By this point, the NAME should be an ordinary identifier.  If
26212      the id-expression was a qualified name, the qualifying scope is
26213      stored in PARSER->SCOPE at this point.  */
26214   gcc_assert (identifier_p (name));
26215
26216   /* Perform the lookup.  */
26217   if (parser->scope)
26218     {
26219       bool dependent_p;
26220
26221       if (parser->scope == error_mark_node)
26222         return error_mark_node;
26223
26224       /* If the SCOPE is dependent, the lookup must be deferred until
26225          the template is instantiated -- unless we are explicitly
26226          looking up names in uninstantiated templates.  Even then, we
26227          cannot look up the name if the scope is not a class type; it
26228          might, for example, be a template type parameter.  */
26229       dependent_p = (TYPE_P (parser->scope)
26230                      && dependent_scope_p (parser->scope));
26231       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26232           && dependent_p)
26233         /* Defer lookup.  */
26234         decl = error_mark_node;
26235       else
26236         {
26237           tree pushed_scope = NULL_TREE;
26238
26239           /* If PARSER->SCOPE is a dependent type, then it must be a
26240              class type, and we must not be checking dependencies;
26241              otherwise, we would have processed this lookup above.  So
26242              that PARSER->SCOPE is not considered a dependent base by
26243              lookup_member, we must enter the scope here.  */
26244           if (dependent_p)
26245             pushed_scope = push_scope (parser->scope);
26246
26247           /* If the PARSER->SCOPE is a template specialization, it
26248              may be instantiated during name lookup.  In that case,
26249              errors may be issued.  Even if we rollback the current
26250              tentative parse, those errors are valid.  */
26251           decl = lookup_qualified_name (parser->scope, name,
26252                                         prefer_type_arg (tag_type),
26253                                         /*complain=*/true);
26254
26255           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26256              lookup result and the nested-name-specifier nominates a class C:
26257                * if the name specified after the nested-name-specifier, when
26258                looked up in C, is the injected-class-name of C (Clause 9), or
26259                * if the name specified after the nested-name-specifier is the
26260                same as the identifier or the simple-template-id's template-
26261                name in the last component of the nested-name-specifier,
26262              the name is instead considered to name the constructor of
26263              class C. [ Note: for example, the constructor is not an
26264              acceptable lookup result in an elaborated-type-specifier so
26265              the constructor would not be used in place of the
26266              injected-class-name. --end note ] Such a constructor name
26267              shall be used only in the declarator-id of a declaration that
26268              names a constructor or in a using-declaration.  */
26269           if (tag_type == none_type
26270               && DECL_SELF_REFERENCE_P (decl)
26271               && same_type_p (DECL_CONTEXT (decl), parser->scope))
26272             decl = lookup_qualified_name (parser->scope, ctor_identifier,
26273                                           prefer_type_arg (tag_type),
26274                                           /*complain=*/true);
26275
26276           /* If we have a single function from a using decl, pull it out.  */
26277           if (TREE_CODE (decl) == OVERLOAD
26278               && !really_overloaded_fn (decl))
26279             decl = OVL_FUNCTION (decl);
26280
26281           if (pushed_scope)
26282             pop_scope (pushed_scope);
26283         }
26284
26285       /* If the scope is a dependent type and either we deferred lookup or
26286          we did lookup but didn't find the name, rememeber the name.  */
26287       if (decl == error_mark_node && TYPE_P (parser->scope)
26288           && dependent_type_p (parser->scope))
26289         {
26290           if (tag_type)
26291             {
26292               tree type;
26293
26294               /* The resolution to Core Issue 180 says that `struct
26295                  A::B' should be considered a type-name, even if `A'
26296                  is dependent.  */
26297               type = make_typename_type (parser->scope, name, tag_type,
26298                                          /*complain=*/tf_error);
26299               if (type != error_mark_node)
26300                 decl = TYPE_NAME (type);
26301             }
26302           else if (is_template
26303                    && (cp_parser_next_token_ends_template_argument_p (parser)
26304                        || cp_lexer_next_token_is (parser->lexer,
26305                                                   CPP_CLOSE_PAREN)))
26306             decl = make_unbound_class_template (parser->scope,
26307                                                 name, NULL_TREE,
26308                                                 /*complain=*/tf_error);
26309           else
26310             decl = build_qualified_name (/*type=*/NULL_TREE,
26311                                          parser->scope, name,
26312                                          is_template);
26313         }
26314       parser->qualifying_scope = parser->scope;
26315       parser->object_scope = NULL_TREE;
26316     }
26317   else if (object_type)
26318     {
26319       /* Look up the name in the scope of the OBJECT_TYPE, unless the
26320          OBJECT_TYPE is not a class.  */
26321       if (CLASS_TYPE_P (object_type))
26322         /* If the OBJECT_TYPE is a template specialization, it may
26323            be instantiated during name lookup.  In that case, errors
26324            may be issued.  Even if we rollback the current tentative
26325            parse, those errors are valid.  */
26326         decl = lookup_member (object_type,
26327                               name,
26328                               /*protect=*/0,
26329                               prefer_type_arg (tag_type),
26330                               tf_warning_or_error);
26331       else
26332         decl = NULL_TREE;
26333
26334       if (!decl)
26335         /* Look it up in the enclosing context.  DR 141: When looking for a
26336            template-name after -> or ., only consider class templates.  */
26337         decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26338                                  /*nonclass=*/0,
26339                                  /*block_p=*/true, is_namespace, 0);
26340       if (object_type == unknown_type_node)
26341         /* The object is type-dependent, so we can't look anything up; we used
26342            this to get the DR 141 behavior.  */
26343         object_type = NULL_TREE;
26344       parser->object_scope = object_type;
26345       parser->qualifying_scope = NULL_TREE;
26346     }
26347   else
26348     {
26349       decl = lookup_name_real (name, prefer_type_arg (tag_type),
26350                                /*nonclass=*/0,
26351                                /*block_p=*/true, is_namespace, 0);
26352       parser->qualifying_scope = NULL_TREE;
26353       parser->object_scope = NULL_TREE;
26354     }
26355
26356   /* If the lookup failed, let our caller know.  */
26357   if (!decl || decl == error_mark_node)
26358     return error_mark_node;
26359
26360   /* Pull out the template from an injected-class-name (or multiple).  */
26361   if (is_template)
26362     decl = maybe_get_template_decl_from_type_decl (decl);
26363
26364   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
26365   if (TREE_CODE (decl) == TREE_LIST)
26366     {
26367       if (ambiguous_decls)
26368         *ambiguous_decls = decl;
26369       /* The error message we have to print is too complicated for
26370          cp_parser_error, so we incorporate its actions directly.  */
26371       if (!cp_parser_simulate_error (parser))
26372         {
26373           error_at (name_location, "reference to %qD is ambiguous",
26374                     name);
26375           print_candidates (decl);
26376         }
26377       return error_mark_node;
26378     }
26379
26380   gcc_assert (DECL_P (decl)
26381               || TREE_CODE (decl) == OVERLOAD
26382               || TREE_CODE (decl) == SCOPE_REF
26383               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26384               || BASELINK_P (decl));
26385
26386   /* If we have resolved the name of a member declaration, check to
26387      see if the declaration is accessible.  When the name resolves to
26388      set of overloaded functions, accessibility is checked when
26389      overload resolution is done.
26390
26391      During an explicit instantiation, access is not checked at all,
26392      as per [temp.explicit].  */
26393   if (DECL_P (decl))
26394     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26395
26396   maybe_record_typedef_use (decl);
26397
26398   return cp_expr (decl, name_location);
26399 }
26400
26401 /* Like cp_parser_lookup_name, but for use in the typical case where
26402    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26403    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
26404
26405 static tree
26406 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26407 {
26408   return cp_parser_lookup_name (parser, name,
26409                                 none_type,
26410                                 /*is_template=*/false,
26411                                 /*is_namespace=*/false,
26412                                 /*check_dependency=*/true,
26413                                 /*ambiguous_decls=*/NULL,
26414                                 location);
26415 }
26416
26417 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26418    the current context, return the TYPE_DECL.  If TAG_NAME_P is
26419    true, the DECL indicates the class being defined in a class-head,
26420    or declared in an elaborated-type-specifier.
26421
26422    Otherwise, return DECL.  */
26423
26424 static tree
26425 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26426 {
26427   /* If the TEMPLATE_DECL is being declared as part of a class-head,
26428      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26429
26430        struct A {
26431          template <typename T> struct B;
26432        };
26433
26434        template <typename T> struct A::B {};
26435
26436      Similarly, in an elaborated-type-specifier:
26437
26438        namespace N { struct X{}; }
26439
26440        struct A {
26441          template <typename T> friend struct N::X;
26442        };
26443
26444      However, if the DECL refers to a class type, and we are in
26445      the scope of the class, then the name lookup automatically
26446      finds the TYPE_DECL created by build_self_reference rather
26447      than a TEMPLATE_DECL.  For example, in:
26448
26449        template <class T> struct S {
26450          S s;
26451        };
26452
26453      there is no need to handle such case.  */
26454
26455   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26456     return DECL_TEMPLATE_RESULT (decl);
26457
26458   return decl;
26459 }
26460
26461 /* If too many, or too few, template-parameter lists apply to the
26462    declarator, issue an error message.  Returns TRUE if all went well,
26463    and FALSE otherwise.  */
26464
26465 static bool
26466 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26467                                                 cp_declarator *declarator,
26468                                                 location_t declarator_location)
26469 {
26470   switch (declarator->kind)
26471     {
26472     case cdk_id:
26473       {
26474         unsigned num_templates = 0;
26475         tree scope = declarator->u.id.qualifying_scope;
26476         bool template_id_p = false;
26477
26478         if (scope)
26479           num_templates = num_template_headers_for_class (scope);
26480         else if (TREE_CODE (declarator->u.id.unqualified_name)
26481                  == TEMPLATE_ID_EXPR)
26482           {
26483             /* If the DECLARATOR has the form `X<y>' then it uses one
26484                additional level of template parameters.  */
26485             ++num_templates;
26486             template_id_p = true;
26487           }
26488
26489         return cp_parser_check_template_parameters 
26490           (parser, num_templates, template_id_p, declarator_location,
26491            declarator);
26492       }
26493
26494     case cdk_function:
26495     case cdk_array:
26496     case cdk_pointer:
26497     case cdk_reference:
26498     case cdk_ptrmem:
26499       return (cp_parser_check_declarator_template_parameters
26500               (parser, declarator->declarator, declarator_location));
26501
26502     case cdk_decomp:
26503     case cdk_error:
26504       return true;
26505
26506     default:
26507       gcc_unreachable ();
26508     }
26509   return false;
26510 }
26511
26512 /* NUM_TEMPLATES were used in the current declaration.  If that is
26513    invalid, return FALSE and issue an error messages.  Otherwise,
26514    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
26515    declarator and we can print more accurate diagnostics.  */
26516
26517 static bool
26518 cp_parser_check_template_parameters (cp_parser* parser,
26519                                      unsigned num_templates,
26520                                      bool template_id_p,
26521                                      location_t location,
26522                                      cp_declarator *declarator)
26523 {
26524   /* If there are the same number of template classes and parameter
26525      lists, that's OK.  */
26526   if (parser->num_template_parameter_lists == num_templates)
26527     return true;
26528   /* If there are more, but only one more, and the name ends in an identifier,
26529      then we are declaring a primary template.  That's OK too.  */
26530   if (!template_id_p
26531       && parser->num_template_parameter_lists == num_templates + 1)
26532     return true;
26533   /* If there are more template classes than parameter lists, we have
26534      something like:
26535
26536        template <class T> void S<T>::R<T>::f ();  */
26537   if (parser->num_template_parameter_lists < num_templates)
26538     {
26539       if (declarator && !current_function_decl)
26540         error_at (location, "specializing member %<%T::%E%> "
26541                   "requires %<template<>%> syntax", 
26542                   declarator->u.id.qualifying_scope,
26543                   declarator->u.id.unqualified_name);
26544       else if (declarator)
26545         error_at (location, "invalid declaration of %<%T::%E%>",
26546                   declarator->u.id.qualifying_scope,
26547                   declarator->u.id.unqualified_name);
26548       else 
26549         error_at (location, "too few template-parameter-lists");
26550       return false;
26551     }
26552   /* Otherwise, there are too many template parameter lists.  We have
26553      something like:
26554
26555      template <class T> template <class U> void S::f();  */
26556   error_at (location, "too many template-parameter-lists");
26557   return false;
26558 }
26559
26560 /* Parse an optional `::' token indicating that the following name is
26561    from the global namespace.  If so, PARSER->SCOPE is set to the
26562    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26563    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26564    Returns the new value of PARSER->SCOPE, if the `::' token is
26565    present, and NULL_TREE otherwise.  */
26566
26567 static tree
26568 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26569 {
26570   cp_token *token;
26571
26572   /* Peek at the next token.  */
26573   token = cp_lexer_peek_token (parser->lexer);
26574   /* If we're looking at a `::' token then we're starting from the
26575      global namespace, not our current location.  */
26576   if (token->type == CPP_SCOPE)
26577     {
26578       /* Consume the `::' token.  */
26579       cp_lexer_consume_token (parser->lexer);
26580       /* Set the SCOPE so that we know where to start the lookup.  */
26581       parser->scope = global_namespace;
26582       parser->qualifying_scope = global_namespace;
26583       parser->object_scope = NULL_TREE;
26584
26585       return parser->scope;
26586     }
26587   else if (!current_scope_valid_p)
26588     {
26589       parser->scope = NULL_TREE;
26590       parser->qualifying_scope = NULL_TREE;
26591       parser->object_scope = NULL_TREE;
26592     }
26593
26594   return NULL_TREE;
26595 }
26596
26597 /* Returns TRUE if the upcoming token sequence is the start of a
26598    constructor declarator or C++17 deduction guide.  If FRIEND_P is true, the
26599    declarator is preceded by the `friend' specifier.  */
26600
26601 static bool
26602 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26603 {
26604   bool constructor_p;
26605   bool outside_class_specifier_p;
26606   tree nested_name_specifier;
26607   cp_token *next_token;
26608
26609   /* The common case is that this is not a constructor declarator, so
26610      try to avoid doing lots of work if at all possible.  It's not
26611      valid declare a constructor at function scope.  */
26612   if (parser->in_function_body)
26613     return false;
26614   /* And only certain tokens can begin a constructor declarator.  */
26615   next_token = cp_lexer_peek_token (parser->lexer);
26616   if (next_token->type != CPP_NAME
26617       && next_token->type != CPP_SCOPE
26618       && next_token->type != CPP_NESTED_NAME_SPECIFIER
26619       && next_token->type != CPP_TEMPLATE_ID)
26620     return false;
26621
26622   /* Parse tentatively; we are going to roll back all of the tokens
26623      consumed here.  */
26624   cp_parser_parse_tentatively (parser);
26625   /* Assume that we are looking at a constructor declarator.  */
26626   constructor_p = true;
26627
26628   /* Look for the optional `::' operator.  */
26629   cp_parser_global_scope_opt (parser,
26630                               /*current_scope_valid_p=*/false);
26631   /* Look for the nested-name-specifier.  */
26632   nested_name_specifier
26633     = (cp_parser_nested_name_specifier_opt (parser,
26634                                             /*typename_keyword_p=*/false,
26635                                             /*check_dependency_p=*/false,
26636                                             /*type_p=*/false,
26637                                             /*is_declaration=*/false));
26638
26639   outside_class_specifier_p = (!at_class_scope_p ()
26640                                || !TYPE_BEING_DEFINED (current_class_type)
26641                                || friend_p);
26642
26643   /* Outside of a class-specifier, there must be a
26644      nested-name-specifier.  Except in C++17 mode, where we
26645      might be declaring a guiding declaration.  */
26646   if (!nested_name_specifier && outside_class_specifier_p
26647       && cxx_dialect < cxx17)
26648     constructor_p = false;
26649   else if (nested_name_specifier == error_mark_node)
26650     constructor_p = false;
26651
26652   /* If we have a class scope, this is easy; DR 147 says that S::S always
26653      names the constructor, and no other qualified name could.  */
26654   if (constructor_p && nested_name_specifier
26655       && CLASS_TYPE_P (nested_name_specifier))
26656     {
26657       tree id = cp_parser_unqualified_id (parser,
26658                                           /*template_keyword_p=*/false,
26659                                           /*check_dependency_p=*/false,
26660                                           /*declarator_p=*/true,
26661                                           /*optional_p=*/false);
26662       if (is_overloaded_fn (id))
26663         id = DECL_NAME (get_first_fn (id));
26664       if (!constructor_name_p (id, nested_name_specifier))
26665         constructor_p = false;
26666     }
26667   /* If we still think that this might be a constructor-declarator,
26668      look for a class-name.  */
26669   else if (constructor_p)
26670     {
26671       /* If we have:
26672
26673            template <typename T> struct S {
26674              S();
26675            };
26676
26677          we must recognize that the nested `S' names a class.  */
26678       if (cxx_dialect >= cxx17)
26679         cp_parser_parse_tentatively (parser);
26680
26681       tree type_decl;
26682       type_decl = cp_parser_class_name (parser,
26683                                         /*typename_keyword_p=*/false,
26684                                         /*template_keyword_p=*/false,
26685                                         none_type,
26686                                         /*check_dependency_p=*/false,
26687                                         /*class_head_p=*/false,
26688                                         /*is_declaration=*/false);
26689
26690       if (cxx_dialect >= cxx17
26691           && !cp_parser_parse_definitely (parser))
26692         {
26693           type_decl = NULL_TREE;
26694           tree tmpl = cp_parser_template_name (parser,
26695                                                /*template_keyword*/false,
26696                                                /*check_dependency_p*/false,
26697                                                /*is_declaration*/false,
26698                                                none_type,
26699                                                /*is_identifier*/NULL);
26700           if (DECL_CLASS_TEMPLATE_P (tmpl)
26701               || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26702             /* It's a deduction guide, return true.  */;
26703           else
26704             cp_parser_simulate_error (parser);
26705         }
26706
26707       /* If there was no class-name, then this is not a constructor.
26708          Otherwise, if we are in a class-specifier and we aren't
26709          handling a friend declaration, check that its type matches
26710          current_class_type (c++/38313).  Note: error_mark_node
26711          is left alone for error recovery purposes.  */
26712       constructor_p = (!cp_parser_error_occurred (parser)
26713                        && (outside_class_specifier_p
26714                            || type_decl == NULL_TREE
26715                            || type_decl == error_mark_node
26716                            || same_type_p (current_class_type,
26717                                            TREE_TYPE (type_decl))));
26718
26719       /* If we're still considering a constructor, we have to see a `(',
26720          to begin the parameter-declaration-clause, followed by either a
26721          `)', an `...', or a decl-specifier.  We need to check for a
26722          type-specifier to avoid being fooled into thinking that:
26723
26724            S (f) (int);
26725
26726          is a constructor.  (It is actually a function named `f' that
26727          takes one parameter (of type `int') and returns a value of type
26728          `S'.  */
26729       if (constructor_p
26730           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26731         constructor_p = false;
26732
26733       if (constructor_p
26734           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26735           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26736           /* A parameter declaration begins with a decl-specifier,
26737              which is either the "attribute" keyword, a storage class
26738              specifier, or (usually) a type-specifier.  */
26739           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26740         {
26741           tree type;
26742           tree pushed_scope = NULL_TREE;
26743           unsigned saved_num_template_parameter_lists;
26744
26745           /* Names appearing in the type-specifier should be looked up
26746              in the scope of the class.  */
26747           if (current_class_type)
26748             type = NULL_TREE;
26749           else if (type_decl)
26750             {
26751               type = TREE_TYPE (type_decl);
26752               if (TREE_CODE (type) == TYPENAME_TYPE)
26753                 {
26754                   type = resolve_typename_type (type,
26755                                                 /*only_current_p=*/false);
26756                   if (TREE_CODE (type) == TYPENAME_TYPE)
26757                     {
26758                       cp_parser_abort_tentative_parse (parser);
26759                       return false;
26760                     }
26761                 }
26762               pushed_scope = push_scope (type);
26763             }
26764
26765           /* Inside the constructor parameter list, surrounding
26766              template-parameter-lists do not apply.  */
26767           saved_num_template_parameter_lists
26768             = parser->num_template_parameter_lists;
26769           parser->num_template_parameter_lists = 0;
26770
26771           /* Look for the type-specifier.  */
26772           cp_parser_type_specifier (parser,
26773                                     CP_PARSER_FLAGS_NONE,
26774                                     /*decl_specs=*/NULL,
26775                                     /*is_declarator=*/true,
26776                                     /*declares_class_or_enum=*/NULL,
26777                                     /*is_cv_qualifier=*/NULL);
26778
26779           parser->num_template_parameter_lists
26780             = saved_num_template_parameter_lists;
26781
26782           /* Leave the scope of the class.  */
26783           if (pushed_scope)
26784             pop_scope (pushed_scope);
26785
26786           constructor_p = !cp_parser_error_occurred (parser);
26787         }
26788     }
26789
26790   /* We did not really want to consume any tokens.  */
26791   cp_parser_abort_tentative_parse (parser);
26792
26793   return constructor_p;
26794 }
26795
26796 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26797    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
26798    they must be performed once we are in the scope of the function.
26799
26800    Returns the function defined.  */
26801
26802 static tree
26803 cp_parser_function_definition_from_specifiers_and_declarator
26804   (cp_parser* parser,
26805    cp_decl_specifier_seq *decl_specifiers,
26806    tree attributes,
26807    const cp_declarator *declarator)
26808 {
26809   tree fn;
26810   bool success_p;
26811
26812   /* Begin the function-definition.  */
26813   success_p = start_function (decl_specifiers, declarator, attributes);
26814
26815   /* The things we're about to see are not directly qualified by any
26816      template headers we've seen thus far.  */
26817   reset_specialization ();
26818
26819   /* If there were names looked up in the decl-specifier-seq that we
26820      did not check, check them now.  We must wait until we are in the
26821      scope of the function to perform the checks, since the function
26822      might be a friend.  */
26823   perform_deferred_access_checks (tf_warning_or_error);
26824
26825   if (success_p)
26826     {
26827       cp_finalize_omp_declare_simd (parser, current_function_decl);
26828       parser->omp_declare_simd = NULL;
26829       cp_finalize_oacc_routine (parser, current_function_decl, true);
26830       parser->oacc_routine = NULL;
26831     }
26832
26833   if (!success_p)
26834     {
26835       /* Skip the entire function.  */
26836       cp_parser_skip_to_end_of_block_or_statement (parser);
26837       fn = error_mark_node;
26838     }
26839   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26840     {
26841       /* Seen already, skip it.  An error message has already been output.  */
26842       cp_parser_skip_to_end_of_block_or_statement (parser);
26843       fn = current_function_decl;
26844       current_function_decl = NULL_TREE;
26845       /* If this is a function from a class, pop the nested class.  */
26846       if (current_class_name)
26847         pop_nested_class ();
26848     }
26849   else
26850     {
26851       timevar_id_t tv;
26852       if (DECL_DECLARED_INLINE_P (current_function_decl))
26853         tv = TV_PARSE_INLINE;
26854       else
26855         tv = TV_PARSE_FUNC;
26856       timevar_push (tv);
26857       fn = cp_parser_function_definition_after_declarator (parser,
26858                                                          /*inline_p=*/false);
26859       timevar_pop (tv);
26860     }
26861
26862   return fn;
26863 }
26864
26865 /* Parse the part of a function-definition that follows the
26866    declarator.  INLINE_P is TRUE iff this function is an inline
26867    function defined within a class-specifier.
26868
26869    Returns the function defined.  */
26870
26871 static tree
26872 cp_parser_function_definition_after_declarator (cp_parser* parser,
26873                                                 bool inline_p)
26874 {
26875   tree fn;
26876   bool saved_in_unbraced_linkage_specification_p;
26877   bool saved_in_function_body;
26878   unsigned saved_num_template_parameter_lists;
26879   cp_token *token;
26880   bool fully_implicit_function_template_p
26881     = parser->fully_implicit_function_template_p;
26882   parser->fully_implicit_function_template_p = false;
26883   tree implicit_template_parms
26884     = parser->implicit_template_parms;
26885   parser->implicit_template_parms = 0;
26886   cp_binding_level* implicit_template_scope
26887     = parser->implicit_template_scope;
26888   parser->implicit_template_scope = 0;
26889
26890   saved_in_function_body = parser->in_function_body;
26891   parser->in_function_body = true;
26892   /* If the next token is `return', then the code may be trying to
26893      make use of the "named return value" extension that G++ used to
26894      support.  */
26895   token = cp_lexer_peek_token (parser->lexer);
26896   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26897     {
26898       /* Consume the `return' keyword.  */
26899       cp_lexer_consume_token (parser->lexer);
26900       /* Look for the identifier that indicates what value is to be
26901          returned.  */
26902       cp_parser_identifier (parser);
26903       /* Issue an error message.  */
26904       error_at (token->location,
26905                 "named return values are no longer supported");
26906       /* Skip tokens until we reach the start of the function body.  */
26907       while (true)
26908         {
26909           cp_token *token = cp_lexer_peek_token (parser->lexer);
26910           if (token->type == CPP_OPEN_BRACE
26911               || token->type == CPP_EOF
26912               || token->type == CPP_PRAGMA_EOL)
26913             break;
26914           cp_lexer_consume_token (parser->lexer);
26915         }
26916     }
26917   /* The `extern' in `extern "C" void f () { ... }' does not apply to
26918      anything declared inside `f'.  */
26919   saved_in_unbraced_linkage_specification_p
26920     = parser->in_unbraced_linkage_specification_p;
26921   parser->in_unbraced_linkage_specification_p = false;
26922   /* Inside the function, surrounding template-parameter-lists do not
26923      apply.  */
26924   saved_num_template_parameter_lists
26925     = parser->num_template_parameter_lists;
26926   parser->num_template_parameter_lists = 0;
26927
26928   /* If the next token is `try', `__transaction_atomic', or
26929      `__transaction_relaxed`, then we are looking at either function-try-block
26930      or function-transaction-block.  Note that all of these include the
26931      function-body.  */
26932   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26933     cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26934   else if (cp_lexer_next_token_is_keyword (parser->lexer,
26935       RID_TRANSACTION_RELAXED))
26936     cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26937   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26938     cp_parser_function_try_block (parser);
26939   else
26940     cp_parser_ctor_initializer_opt_and_function_body
26941       (parser, /*in_function_try_block=*/false);
26942
26943   /* Finish the function.  */
26944   fn = finish_function (inline_p);
26945   /* Generate code for it, if necessary.  */
26946   expand_or_defer_fn (fn);
26947   /* Restore the saved values.  */
26948   parser->in_unbraced_linkage_specification_p
26949     = saved_in_unbraced_linkage_specification_p;
26950   parser->num_template_parameter_lists
26951     = saved_num_template_parameter_lists;
26952   parser->in_function_body = saved_in_function_body;
26953
26954   parser->fully_implicit_function_template_p
26955     = fully_implicit_function_template_p;
26956   parser->implicit_template_parms
26957     = implicit_template_parms;
26958   parser->implicit_template_scope
26959     = implicit_template_scope;
26960
26961   if (parser->fully_implicit_function_template_p)
26962     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26963
26964   return fn;
26965 }
26966
26967 /* Parse a template-declaration body (following argument list).  */
26968
26969 static void
26970 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26971                                                  tree parameter_list,
26972                                                  bool member_p)
26973 {
26974   tree decl = NULL_TREE;
26975   bool friend_p = false;
26976
26977   /* We just processed one more parameter list.  */
26978   ++parser->num_template_parameter_lists;
26979
26980   /* Get the deferred access checks from the parameter list.  These
26981      will be checked once we know what is being declared, as for a
26982      member template the checks must be performed in the scope of the
26983      class containing the member.  */
26984   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26985
26986   /* Tentatively parse for a new template parameter list, which can either be
26987      the template keyword or a template introduction.  */
26988   if (cp_parser_template_declaration_after_export (parser, member_p))
26989     /* OK */;
26990   else if (cxx_dialect >= cxx11
26991            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26992     decl = cp_parser_alias_declaration (parser);
26993   else
26994     {
26995       /* There are no access checks when parsing a template, as we do not
26996          know if a specialization will be a friend.  */
26997       push_deferring_access_checks (dk_no_check);
26998       cp_token *token = cp_lexer_peek_token (parser->lexer);
26999       decl = cp_parser_single_declaration (parser,
27000                                            checks,
27001                                            member_p,
27002                                            /*explicit_specialization_p=*/false,
27003                                            &friend_p);
27004       pop_deferring_access_checks ();
27005
27006       /* If this is a member template declaration, let the front
27007          end know.  */
27008       if (member_p && !friend_p && decl)
27009         {
27010           if (TREE_CODE (decl) == TYPE_DECL)
27011             cp_parser_check_access_in_redeclaration (decl, token->location);
27012
27013           decl = finish_member_template_decl (decl);
27014         }
27015       else if (friend_p && decl
27016                && DECL_DECLARES_TYPE_P (decl))
27017         make_friend_class (current_class_type, TREE_TYPE (decl),
27018                            /*complain=*/true);
27019     }
27020   /* We are done with the current parameter list.  */
27021   --parser->num_template_parameter_lists;
27022
27023   pop_deferring_access_checks ();
27024
27025   /* Finish up.  */
27026   finish_template_decl (parameter_list);
27027
27028   /* Check the template arguments for a literal operator template.  */
27029   if (decl
27030       && DECL_DECLARES_FUNCTION_P (decl)
27031       && UDLIT_OPER_P (DECL_NAME (decl)))
27032     {
27033       bool ok = true;
27034       if (parameter_list == NULL_TREE)
27035         ok = false;
27036       else
27037         {
27038           int num_parms = TREE_VEC_LENGTH (parameter_list);
27039           if (num_parms == 1)
27040             {
27041               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27042               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27043               if (TREE_TYPE (parm) != char_type_node
27044                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27045                 ok = false;
27046             }
27047           else if (num_parms == 2 && cxx_dialect >= cxx14)
27048             {
27049               tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27050               tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27051               tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27052               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27053               if (parm == error_mark_node
27054                   || TREE_TYPE (parm) != TREE_TYPE (type)
27055                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27056                 ok = false;
27057             }
27058           else
27059             ok = false;
27060         }
27061       if (!ok)
27062         {
27063           if (cxx_dialect >= cxx14)
27064             error ("literal operator template %qD has invalid parameter list."
27065                    "  Expected non-type template argument pack <char...>"
27066                    " or <typename CharT, CharT...>",
27067                    decl);
27068           else
27069             error ("literal operator template %qD has invalid parameter list."
27070                    "  Expected non-type template argument pack <char...>",
27071                    decl);
27072         }
27073     }
27074
27075   /* Register member declarations.  */
27076   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27077     finish_member_declaration (decl);
27078   /* If DECL is a function template, we must return to parse it later.
27079      (Even though there is no definition, there might be default
27080      arguments that need handling.)  */
27081   if (member_p && decl
27082       && DECL_DECLARES_FUNCTION_P (decl))
27083     vec_safe_push (unparsed_funs_with_definitions, decl);
27084 }
27085
27086 /* Parse a template introduction header for a template-declaration.  Returns
27087    false if tentative parse fails.  */
27088
27089 static bool
27090 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27091 {
27092   cp_parser_parse_tentatively (parser);
27093
27094   tree saved_scope = parser->scope;
27095   tree saved_object_scope = parser->object_scope;
27096   tree saved_qualifying_scope = parser->qualifying_scope;
27097
27098   /* Look for the optional `::' operator.  */
27099   cp_parser_global_scope_opt (parser,
27100                               /*current_scope_valid_p=*/false);
27101   /* Look for the nested-name-specifier.  */
27102   cp_parser_nested_name_specifier_opt (parser,
27103                                        /*typename_keyword_p=*/false,
27104                                        /*check_dependency_p=*/true,
27105                                        /*type_p=*/false,
27106                                        /*is_declaration=*/false);
27107
27108   cp_token *token = cp_lexer_peek_token (parser->lexer);
27109   tree concept_name = cp_parser_identifier (parser);
27110
27111   /* Look up the concept for which we will be matching
27112      template parameters.  */
27113   tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27114                                                  token->location);
27115   parser->scope = saved_scope;
27116   parser->object_scope = saved_object_scope;
27117   parser->qualifying_scope = saved_qualifying_scope;
27118
27119   if (concept_name == error_mark_node)
27120     cp_parser_simulate_error (parser);
27121
27122   /* Look for opening brace for introduction.  */
27123   matching_braces braces;
27124   braces.require_open (parser);
27125
27126   if (!cp_parser_parse_definitely (parser))
27127     return false;
27128
27129   push_deferring_access_checks (dk_deferred);
27130
27131   /* Build vector of placeholder parameters and grab
27132      matching identifiers.  */
27133   tree introduction_list = cp_parser_introduction_list (parser);
27134
27135   /* The introduction-list shall not be empty.  */
27136   int nargs = TREE_VEC_LENGTH (introduction_list);
27137   if (nargs == 0)
27138     {
27139       error ("empty introduction-list");
27140       return true;
27141     }
27142
27143   /* Look for closing brace for introduction.  */
27144   if (!braces.require_close (parser))
27145     return true;
27146
27147   if (tmpl_decl == error_mark_node)
27148     {
27149       cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27150                                    token->location);
27151       return true;
27152     }
27153
27154   /* Build and associate the constraint.  */
27155   tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27156   if (parms && parms != error_mark_node)
27157     {
27158       cp_parser_template_declaration_after_parameters (parser, parms,
27159                                                        member_p);
27160       return true;
27161     }
27162
27163   error_at (token->location, "no matching concept for template-introduction");
27164   return true;
27165 }
27166
27167 /* Parse a normal template-declaration following the template keyword.  */
27168
27169 static void
27170 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27171 {
27172   tree parameter_list;
27173   bool need_lang_pop;
27174   location_t location = input_location;
27175
27176   /* Look for the `<' token.  */
27177   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27178     return;
27179   if (at_class_scope_p () && current_function_decl)
27180     {
27181       /* 14.5.2.2 [temp.mem]
27182
27183          A local class shall not have member templates.  */
27184       error_at (location,
27185                 "invalid declaration of member template in local class");
27186       cp_parser_skip_to_end_of_block_or_statement (parser);
27187       return;
27188     }
27189   /* [temp]
27190
27191      A template ... shall not have C linkage.  */
27192   if (current_lang_name == lang_name_c)
27193     {
27194       error_at (location, "template with C linkage");
27195       maybe_show_extern_c_location ();
27196       /* Give it C++ linkage to avoid confusing other parts of the
27197          front end.  */
27198       push_lang_context (lang_name_cplusplus);
27199       need_lang_pop = true;
27200     }
27201   else
27202     need_lang_pop = false;
27203
27204   /* We cannot perform access checks on the template parameter
27205      declarations until we know what is being declared, just as we
27206      cannot check the decl-specifier list.  */
27207   push_deferring_access_checks (dk_deferred);
27208
27209   /* If the next token is `>', then we have an invalid
27210      specialization.  Rather than complain about an invalid template
27211      parameter, issue an error message here.  */
27212   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27213     {
27214       cp_parser_error (parser, "invalid explicit specialization");
27215       begin_specialization ();
27216       parameter_list = NULL_TREE;
27217     }
27218   else
27219     {
27220       /* Parse the template parameters.  */
27221       parameter_list = cp_parser_template_parameter_list (parser);
27222     }
27223
27224   /* Look for the `>'.  */
27225   cp_parser_skip_to_end_of_template_parameter_list (parser);
27226
27227   /* Manage template requirements */
27228   if (flag_concepts)
27229   {
27230     tree reqs = get_shorthand_constraints (current_template_parms);
27231     if (tree r = cp_parser_requires_clause_opt (parser))
27232       reqs = conjoin_constraints (reqs, normalize_expression (r));
27233     TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27234   }
27235
27236   cp_parser_template_declaration_after_parameters (parser, parameter_list,
27237                                                    member_p);
27238
27239   /* For the erroneous case of a template with C linkage, we pushed an
27240      implicit C++ linkage scope; exit that scope now.  */
27241   if (need_lang_pop)
27242     pop_lang_context ();
27243 }
27244
27245 /* Parse a template-declaration, assuming that the `export' (and
27246    `extern') keywords, if present, has already been scanned.  MEMBER_P
27247    is as for cp_parser_template_declaration.  */
27248
27249 static bool
27250 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27251 {
27252   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27253     {
27254       cp_lexer_consume_token (parser->lexer);
27255       cp_parser_explicit_template_declaration (parser, member_p);
27256       return true;
27257     }
27258   else if (flag_concepts)
27259     return cp_parser_template_introduction (parser, member_p);
27260
27261   return false;
27262 }
27263
27264 /* Perform the deferred access checks from a template-parameter-list.
27265    CHECKS is a TREE_LIST of access checks, as returned by
27266    get_deferred_access_checks.  */
27267
27268 static void
27269 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27270 {
27271   ++processing_template_parmlist;
27272   perform_access_checks (checks, tf_warning_or_error);
27273   --processing_template_parmlist;
27274 }
27275
27276 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27277    `function-definition' sequence that follows a template header.
27278    If MEMBER_P is true, this declaration appears in a class scope.
27279
27280    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
27281    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
27282
27283 static tree
27284 cp_parser_single_declaration (cp_parser* parser,
27285                               vec<deferred_access_check, va_gc> *checks,
27286                               bool member_p,
27287                               bool explicit_specialization_p,
27288                               bool* friend_p)
27289 {
27290   int declares_class_or_enum;
27291   tree decl = NULL_TREE;
27292   cp_decl_specifier_seq decl_specifiers;
27293   bool function_definition_p = false;
27294   cp_token *decl_spec_token_start;
27295
27296   /* This function is only used when processing a template
27297      declaration.  */
27298   gcc_assert (innermost_scope_kind () == sk_template_parms
27299               || innermost_scope_kind () == sk_template_spec);
27300
27301   /* Defer access checks until we know what is being declared.  */
27302   push_deferring_access_checks (dk_deferred);
27303
27304   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27305      alternative.  */
27306   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27307   cp_parser_decl_specifier_seq (parser,
27308                                 CP_PARSER_FLAGS_OPTIONAL,
27309                                 &decl_specifiers,
27310                                 &declares_class_or_enum);
27311   if (friend_p)
27312     *friend_p = cp_parser_friend_p (&decl_specifiers);
27313
27314   /* There are no template typedefs.  */
27315   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27316     {
27317       error_at (decl_spec_token_start->location,
27318                 "template declaration of %<typedef%>");
27319       decl = error_mark_node;
27320     }
27321
27322   /* Gather up the access checks that occurred the
27323      decl-specifier-seq.  */
27324   stop_deferring_access_checks ();
27325
27326   /* Check for the declaration of a template class.  */
27327   if (declares_class_or_enum)
27328     {
27329       if (cp_parser_declares_only_class_p (parser)
27330           || (declares_class_or_enum & 2))
27331         {
27332           // If this is a declaration, but not a definition, associate
27333           // any constraints with the type declaration. Constraints
27334           // are associated with definitions in cp_parser_class_specifier.
27335           if (declares_class_or_enum == 1)
27336             associate_classtype_constraints (decl_specifiers.type);
27337
27338           decl = shadow_tag (&decl_specifiers);
27339
27340           /* In this case:
27341
27342                struct C {
27343                  friend template <typename T> struct A<T>::B;
27344                };
27345
27346              A<T>::B will be represented by a TYPENAME_TYPE, and
27347              therefore not recognized by shadow_tag.  */
27348           if (friend_p && *friend_p
27349               && !decl
27350               && decl_specifiers.type
27351               && TYPE_P (decl_specifiers.type))
27352             decl = decl_specifiers.type;
27353
27354           if (decl && decl != error_mark_node)
27355             decl = TYPE_NAME (decl);
27356           else
27357             decl = error_mark_node;
27358
27359           /* Perform access checks for template parameters.  */
27360           cp_parser_perform_template_parameter_access_checks (checks);
27361
27362           /* Give a helpful diagnostic for
27363                template <class T> struct A { } a;
27364              if we aren't already recovering from an error.  */
27365           if (!cp_parser_declares_only_class_p (parser)
27366               && !seen_error ())
27367             {
27368               error_at (cp_lexer_peek_token (parser->lexer)->location,
27369                         "a class template declaration must not declare "
27370                         "anything else");
27371               cp_parser_skip_to_end_of_block_or_statement (parser);
27372               goto out;
27373             }
27374         }
27375     }
27376
27377   /* Complain about missing 'typename' or other invalid type names.  */
27378   if (!decl_specifiers.any_type_specifiers_p
27379       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27380     {
27381       /* cp_parser_parse_and_diagnose_invalid_type_name calls
27382          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27383          the rest of this declaration.  */
27384       decl = error_mark_node;
27385       goto out;
27386     }
27387
27388   /* If it's not a template class, try for a template function.  If
27389      the next token is a `;', then this declaration does not declare
27390      anything.  But, if there were errors in the decl-specifiers, then
27391      the error might well have come from an attempted class-specifier.
27392      In that case, there's no need to warn about a missing declarator.  */
27393   if (!decl
27394       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27395           || decl_specifiers.type != error_mark_node))
27396     {
27397       decl = cp_parser_init_declarator (parser,
27398                                         &decl_specifiers,
27399                                         checks,
27400                                         /*function_definition_allowed_p=*/true,
27401                                         member_p,
27402                                         declares_class_or_enum,
27403                                         &function_definition_p,
27404                                         NULL, NULL, NULL);
27405
27406     /* 7.1.1-1 [dcl.stc]
27407
27408        A storage-class-specifier shall not be specified in an explicit
27409        specialization...  */
27410     if (decl
27411         && explicit_specialization_p
27412         && decl_specifiers.storage_class != sc_none)
27413       {
27414         error_at (decl_spec_token_start->location,
27415                   "explicit template specialization cannot have a storage class");
27416         decl = error_mark_node;
27417       }
27418
27419     if (decl && VAR_P (decl))
27420       check_template_variable (decl);
27421     }
27422
27423   /* Look for a trailing `;' after the declaration.  */
27424   if (!function_definition_p
27425       && (decl == error_mark_node
27426           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27427     cp_parser_skip_to_end_of_block_or_statement (parser);
27428
27429  out:
27430   pop_deferring_access_checks ();
27431
27432   /* Clear any current qualification; whatever comes next is the start
27433      of something new.  */
27434   parser->scope = NULL_TREE;
27435   parser->qualifying_scope = NULL_TREE;
27436   parser->object_scope = NULL_TREE;
27437
27438   return decl;
27439 }
27440
27441 /* Parse a cast-expression that is not the operand of a unary "&".  */
27442
27443 static cp_expr
27444 cp_parser_simple_cast_expression (cp_parser *parser)
27445 {
27446   return cp_parser_cast_expression (parser, /*address_p=*/false,
27447                                     /*cast_p=*/false, /*decltype*/false, NULL);
27448 }
27449
27450 /* Parse a functional cast to TYPE.  Returns an expression
27451    representing the cast.  */
27452
27453 static cp_expr
27454 cp_parser_functional_cast (cp_parser* parser, tree type)
27455 {
27456   vec<tree, va_gc> *vec;
27457   tree expression_list;
27458   cp_expr cast;
27459   bool nonconst_p;
27460
27461   location_t start_loc = input_location;
27462
27463   if (!type)
27464     type = error_mark_node;
27465
27466   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27467     {
27468       cp_lexer_set_source_position (parser->lexer);
27469       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27470       expression_list = cp_parser_braced_list (parser, &nonconst_p);
27471       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27472       if (TREE_CODE (type) == TYPE_DECL)
27473         type = TREE_TYPE (type);
27474
27475       cast = finish_compound_literal (type, expression_list,
27476                                       tf_warning_or_error, fcl_functional);
27477       /* Create a location of the form:
27478             type_name{i, f}
27479             ^~~~~~~~~~~~~~~
27480          with caret == start at the start of the type name,
27481          finishing at the closing brace.  */
27482       location_t finish_loc
27483         = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27484       location_t combined_loc = make_location (start_loc, start_loc,
27485                                                finish_loc);
27486       cast.set_location (combined_loc);
27487       return cast;
27488    }
27489
27490
27491   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27492                                                  /*cast_p=*/true,
27493                                                  /*allow_expansion_p=*/true,
27494                                                  /*non_constant_p=*/NULL);
27495   if (vec == NULL)
27496     expression_list = error_mark_node;
27497   else
27498     {
27499       expression_list = build_tree_list_vec (vec);
27500       release_tree_vector (vec);
27501     }
27502
27503   cast = build_functional_cast (type, expression_list,
27504                                 tf_warning_or_error);
27505   /* [expr.const]/1: In an integral constant expression "only type
27506      conversions to integral or enumeration type can be used".  */
27507   if (TREE_CODE (type) == TYPE_DECL)
27508     type = TREE_TYPE (type);
27509   if (cast != error_mark_node
27510       && !cast_valid_in_integral_constant_expression_p (type)
27511       && cp_parser_non_integral_constant_expression (parser,
27512                                                      NIC_CONSTRUCTOR))
27513     return error_mark_node;
27514
27515   /* Create a location of the form:
27516        float(i)
27517        ^~~~~~~~
27518      with caret == start at the start of the type name,
27519      finishing at the closing paren.  */
27520   location_t finish_loc
27521     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27522   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27523   cast.set_location (combined_loc);
27524   return cast;
27525 }
27526
27527 /* Save the tokens that make up the body of a member function defined
27528    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
27529    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
27530    specifiers applied to the declaration.  Returns the FUNCTION_DECL
27531    for the member function.  */
27532
27533 static tree
27534 cp_parser_save_member_function_body (cp_parser* parser,
27535                                      cp_decl_specifier_seq *decl_specifiers,
27536                                      cp_declarator *declarator,
27537                                      tree attributes)
27538 {
27539   cp_token *first;
27540   cp_token *last;
27541   tree fn;
27542   bool function_try_block = false;
27543
27544   /* Create the FUNCTION_DECL.  */
27545   fn = grokmethod (decl_specifiers, declarator, attributes);
27546   cp_finalize_omp_declare_simd (parser, fn);
27547   cp_finalize_oacc_routine (parser, fn, true);
27548   /* If something went badly wrong, bail out now.  */
27549   if (fn == error_mark_node)
27550     {
27551       /* If there's a function-body, skip it.  */
27552       if (cp_parser_token_starts_function_definition_p
27553           (cp_lexer_peek_token (parser->lexer)))
27554         cp_parser_skip_to_end_of_block_or_statement (parser);
27555       return error_mark_node;
27556     }
27557
27558   /* Remember it, if there default args to post process.  */
27559   cp_parser_save_default_args (parser, fn);
27560
27561   /* Save away the tokens that make up the body of the
27562      function.  */
27563   first = parser->lexer->next_token;
27564
27565   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27566     cp_lexer_consume_token (parser->lexer);
27567   else if (cp_lexer_next_token_is_keyword (parser->lexer,
27568                                            RID_TRANSACTION_ATOMIC))
27569     {
27570       cp_lexer_consume_token (parser->lexer);
27571       /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
27572       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27573           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27574           && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27575               || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27576           && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27577           && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27578         {
27579           cp_lexer_consume_token (parser->lexer);
27580           cp_lexer_consume_token (parser->lexer);
27581           cp_lexer_consume_token (parser->lexer);
27582           cp_lexer_consume_token (parser->lexer);
27583           cp_lexer_consume_token (parser->lexer);
27584         }
27585       else
27586         while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27587                && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27588           {
27589             cp_lexer_consume_token (parser->lexer);
27590             if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27591               break;
27592           }
27593     }
27594
27595   /* Handle function try blocks.  */
27596   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27597     {
27598       cp_lexer_consume_token (parser->lexer);
27599       function_try_block = true;
27600     }
27601   /* We can have braced-init-list mem-initializers before the fn body.  */
27602   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27603     {
27604       cp_lexer_consume_token (parser->lexer);
27605       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27606         {
27607           /* cache_group will stop after an un-nested { } pair, too.  */
27608           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27609             break;
27610
27611           /* variadic mem-inits have ... after the ')'.  */
27612           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27613             cp_lexer_consume_token (parser->lexer);
27614         }
27615     }
27616   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27617   /* Handle function try blocks.  */
27618   if (function_try_block)
27619     while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27620       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27621   last = parser->lexer->next_token;
27622
27623   /* Save away the inline definition; we will process it when the
27624      class is complete.  */
27625   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27626   DECL_PENDING_INLINE_P (fn) = 1;
27627
27628   /* We need to know that this was defined in the class, so that
27629      friend templates are handled correctly.  */
27630   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27631
27632   /* Add FN to the queue of functions to be parsed later.  */
27633   vec_safe_push (unparsed_funs_with_definitions, fn);
27634
27635   return fn;
27636 }
27637
27638 /* Save the tokens that make up the in-class initializer for a non-static
27639    data member.  Returns a DEFAULT_ARG.  */
27640
27641 static tree
27642 cp_parser_save_nsdmi (cp_parser* parser)
27643 {
27644   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27645 }
27646
27647 /* Parse a template-argument-list, as well as the trailing ">" (but
27648    not the opening "<").  See cp_parser_template_argument_list for the
27649    return value.  */
27650
27651 static tree
27652 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27653 {
27654   tree arguments;
27655   tree saved_scope;
27656   tree saved_qualifying_scope;
27657   tree saved_object_scope;
27658   bool saved_greater_than_is_operator_p;
27659   int saved_unevaluated_operand;
27660   int saved_inhibit_evaluation_warnings;
27661
27662   /* [temp.names]
27663
27664      When parsing a template-id, the first non-nested `>' is taken as
27665      the end of the template-argument-list rather than a greater-than
27666      operator.  */
27667   saved_greater_than_is_operator_p
27668     = parser->greater_than_is_operator_p;
27669   parser->greater_than_is_operator_p = false;
27670   /* Parsing the argument list may modify SCOPE, so we save it
27671      here.  */
27672   saved_scope = parser->scope;
27673   saved_qualifying_scope = parser->qualifying_scope;
27674   saved_object_scope = parser->object_scope;
27675   /* We need to evaluate the template arguments, even though this
27676      template-id may be nested within a "sizeof".  */
27677   saved_unevaluated_operand = cp_unevaluated_operand;
27678   cp_unevaluated_operand = 0;
27679   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27680   c_inhibit_evaluation_warnings = 0;
27681   /* Parse the template-argument-list itself.  */
27682   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27683       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27684     arguments = NULL_TREE;
27685   else
27686     arguments = cp_parser_template_argument_list (parser);
27687   /* Look for the `>' that ends the template-argument-list. If we find
27688      a '>>' instead, it's probably just a typo.  */
27689   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27690     {
27691       if (cxx_dialect != cxx98)
27692         {
27693           /* In C++0x, a `>>' in a template argument list or cast
27694              expression is considered to be two separate `>'
27695              tokens. So, change the current token to a `>', but don't
27696              consume it: it will be consumed later when the outer
27697              template argument list (or cast expression) is parsed.
27698              Note that this replacement of `>' for `>>' is necessary
27699              even if we are parsing tentatively: in the tentative
27700              case, after calling
27701              cp_parser_enclosed_template_argument_list we will always
27702              throw away all of the template arguments and the first
27703              closing `>', either because the template argument list
27704              was erroneous or because we are replacing those tokens
27705              with a CPP_TEMPLATE_ID token.  The second `>' (which will
27706              not have been thrown away) is needed either to close an
27707              outer template argument list or to complete a new-style
27708              cast.  */
27709           cp_token *token = cp_lexer_peek_token (parser->lexer);
27710           token->type = CPP_GREATER;
27711         }
27712       else if (!saved_greater_than_is_operator_p)
27713         {
27714           /* If we're in a nested template argument list, the '>>' has
27715             to be a typo for '> >'. We emit the error message, but we
27716             continue parsing and we push a '>' as next token, so that
27717             the argument list will be parsed correctly.  Note that the
27718             global source location is still on the token before the
27719             '>>', so we need to say explicitly where we want it.  */
27720           cp_token *token = cp_lexer_peek_token (parser->lexer);
27721           gcc_rich_location richloc (token->location);
27722           richloc.add_fixit_replace ("> >");
27723           error_at (&richloc, "%<>>%> should be %<> >%> "
27724                     "within a nested template argument list");
27725
27726           token->type = CPP_GREATER;
27727         }
27728       else
27729         {
27730           /* If this is not a nested template argument list, the '>>'
27731             is a typo for '>'. Emit an error message and continue.
27732             Same deal about the token location, but here we can get it
27733             right by consuming the '>>' before issuing the diagnostic.  */
27734           cp_token *token = cp_lexer_consume_token (parser->lexer);
27735           error_at (token->location,
27736                     "spurious %<>>%>, use %<>%> to terminate "
27737                     "a template argument list");
27738         }
27739     }
27740   else
27741     cp_parser_skip_to_end_of_template_parameter_list (parser);
27742   /* The `>' token might be a greater-than operator again now.  */
27743   parser->greater_than_is_operator_p
27744     = saved_greater_than_is_operator_p;
27745   /* Restore the SAVED_SCOPE.  */
27746   parser->scope = saved_scope;
27747   parser->qualifying_scope = saved_qualifying_scope;
27748   parser->object_scope = saved_object_scope;
27749   cp_unevaluated_operand = saved_unevaluated_operand;
27750   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27751
27752   return arguments;
27753 }
27754
27755 /* MEMBER_FUNCTION is a member function, or a friend.  If default
27756    arguments, or the body of the function have not yet been parsed,
27757    parse them now.  */
27758
27759 static void
27760 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27761 {
27762   timevar_push (TV_PARSE_INMETH);
27763   /* If this member is a template, get the underlying
27764      FUNCTION_DECL.  */
27765   if (DECL_FUNCTION_TEMPLATE_P (member_function))
27766     member_function = DECL_TEMPLATE_RESULT (member_function);
27767
27768   /* There should not be any class definitions in progress at this
27769      point; the bodies of members are only parsed outside of all class
27770      definitions.  */
27771   gcc_assert (parser->num_classes_being_defined == 0);
27772   /* While we're parsing the member functions we might encounter more
27773      classes.  We want to handle them right away, but we don't want
27774      them getting mixed up with functions that are currently in the
27775      queue.  */
27776   push_unparsed_function_queues (parser);
27777
27778   /* Make sure that any template parameters are in scope.  */
27779   maybe_begin_member_template_processing (member_function);
27780
27781   /* If the body of the function has not yet been parsed, parse it
27782      now.  */
27783   if (DECL_PENDING_INLINE_P (member_function))
27784     {
27785       tree function_scope;
27786       cp_token_cache *tokens;
27787
27788       /* The function is no longer pending; we are processing it.  */
27789       tokens = DECL_PENDING_INLINE_INFO (member_function);
27790       DECL_PENDING_INLINE_INFO (member_function) = NULL;
27791       DECL_PENDING_INLINE_P (member_function) = 0;
27792
27793       /* If this is a local class, enter the scope of the containing
27794          function.  */
27795       function_scope = current_function_decl;
27796       if (function_scope)
27797         push_function_context ();
27798
27799       /* Push the body of the function onto the lexer stack.  */
27800       cp_parser_push_lexer_for_tokens (parser, tokens);
27801
27802       /* Let the front end know that we going to be defining this
27803          function.  */
27804       start_preparsed_function (member_function, NULL_TREE,
27805                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
27806
27807       /* Don't do access checking if it is a templated function.  */
27808       if (processing_template_decl)
27809         push_deferring_access_checks (dk_no_check);
27810
27811       /* #pragma omp declare reduction needs special parsing.  */
27812       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27813         {
27814           parser->lexer->in_pragma = true;
27815           cp_parser_omp_declare_reduction_exprs (member_function, parser);
27816           finish_function (/*inline_p=*/true);
27817           cp_check_omp_declare_reduction (member_function);
27818         }
27819       else
27820         /* Now, parse the body of the function.  */
27821         cp_parser_function_definition_after_declarator (parser,
27822                                                         /*inline_p=*/true);
27823
27824       if (processing_template_decl)
27825         pop_deferring_access_checks ();
27826
27827       /* Leave the scope of the containing function.  */
27828       if (function_scope)
27829         pop_function_context ();
27830       cp_parser_pop_lexer (parser);
27831     }
27832
27833   /* Remove any template parameters from the symbol table.  */
27834   maybe_end_member_template_processing ();
27835
27836   /* Restore the queue.  */
27837   pop_unparsed_function_queues (parser);
27838   timevar_pop (TV_PARSE_INMETH);
27839 }
27840
27841 /* If DECL contains any default args, remember it on the unparsed
27842    functions queue.  */
27843
27844 static void
27845 cp_parser_save_default_args (cp_parser* parser, tree decl)
27846 {
27847   tree probe;
27848
27849   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27850        probe;
27851        probe = TREE_CHAIN (probe))
27852     if (TREE_PURPOSE (probe))
27853       {
27854         cp_default_arg_entry entry = {current_class_type, decl};
27855         vec_safe_push (unparsed_funs_with_default_args, entry);
27856         break;
27857       }
27858 }
27859
27860 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27861    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
27862    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
27863    from the parameter-type-list.  */
27864
27865 static tree
27866 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27867                                       tree default_arg, tree parmtype)
27868 {
27869   cp_token_cache *tokens;
27870   tree parsed_arg;
27871   bool dummy;
27872
27873   if (default_arg == error_mark_node)
27874     return error_mark_node;
27875
27876   /* Push the saved tokens for the default argument onto the parser's
27877      lexer stack.  */
27878   tokens = DEFARG_TOKENS (default_arg);
27879   cp_parser_push_lexer_for_tokens (parser, tokens);
27880
27881   start_lambda_scope (decl);
27882
27883   /* Parse the default argument.  */
27884   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27885   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27886     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27887
27888   finish_lambda_scope ();
27889
27890   if (parsed_arg == error_mark_node)
27891     cp_parser_skip_to_end_of_statement (parser);
27892
27893   if (!processing_template_decl)
27894     {
27895       /* In a non-template class, check conversions now.  In a template,
27896          we'll wait and instantiate these as needed.  */
27897       if (TREE_CODE (decl) == PARM_DECL)
27898         parsed_arg = check_default_argument (parmtype, parsed_arg,
27899                                              tf_warning_or_error);
27900       else if (maybe_reject_flexarray_init (decl, parsed_arg))
27901         parsed_arg = error_mark_node;
27902       else
27903         parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27904     }
27905
27906   /* If the token stream has not been completely used up, then
27907      there was extra junk after the end of the default
27908      argument.  */
27909   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27910     {
27911       if (TREE_CODE (decl) == PARM_DECL)
27912         cp_parser_error (parser, "expected %<,%>");
27913       else
27914         cp_parser_error (parser, "expected %<;%>");
27915     }
27916
27917   /* Revert to the main lexer.  */
27918   cp_parser_pop_lexer (parser);
27919
27920   return parsed_arg;
27921 }
27922
27923 /* FIELD is a non-static data member with an initializer which we saved for
27924    later; parse it now.  */
27925
27926 static void
27927 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27928 {
27929   tree def;
27930
27931   maybe_begin_member_template_processing (field);
27932
27933   push_unparsed_function_queues (parser);
27934   def = cp_parser_late_parse_one_default_arg (parser, field,
27935                                               DECL_INITIAL (field),
27936                                               NULL_TREE);
27937   pop_unparsed_function_queues (parser);
27938
27939   maybe_end_member_template_processing ();
27940
27941   DECL_INITIAL (field) = def;
27942 }
27943
27944 /* FN is a FUNCTION_DECL which may contains a parameter with an
27945    unparsed DEFAULT_ARG.  Parse the default args now.  This function
27946    assumes that the current scope is the scope in which the default
27947    argument should be processed.  */
27948
27949 static void
27950 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27951 {
27952   bool saved_local_variables_forbidden_p;
27953   tree parm, parmdecl;
27954
27955   /* While we're parsing the default args, we might (due to the
27956      statement expression extension) encounter more classes.  We want
27957      to handle them right away, but we don't want them getting mixed
27958      up with default args that are currently in the queue.  */
27959   push_unparsed_function_queues (parser);
27960
27961   /* Local variable names (and the `this' keyword) may not appear
27962      in a default argument.  */
27963   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27964   parser->local_variables_forbidden_p = true;
27965
27966   push_defarg_context (fn);
27967
27968   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27969          parmdecl = DECL_ARGUMENTS (fn);
27970        parm && parm != void_list_node;
27971        parm = TREE_CHAIN (parm),
27972          parmdecl = DECL_CHAIN (parmdecl))
27973     {
27974       tree default_arg = TREE_PURPOSE (parm);
27975       tree parsed_arg;
27976       vec<tree, va_gc> *insts;
27977       tree copy;
27978       unsigned ix;
27979
27980       if (!default_arg)
27981         continue;
27982
27983       if (TREE_CODE (default_arg) != DEFAULT_ARG)
27984         /* This can happen for a friend declaration for a function
27985            already declared with default arguments.  */
27986         continue;
27987
27988       parsed_arg
27989         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27990                                                 default_arg,
27991                                                 TREE_VALUE (parm));
27992       TREE_PURPOSE (parm) = parsed_arg;
27993
27994       /* Update any instantiations we've already created.  */
27995       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27996            vec_safe_iterate (insts, ix, &copy); ix++)
27997         TREE_PURPOSE (copy) = parsed_arg;
27998     }
27999
28000   pop_defarg_context ();
28001
28002   /* Make sure no default arg is missing.  */
28003   check_default_args (fn);
28004
28005   /* Restore the state of local_variables_forbidden_p.  */
28006   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28007
28008   /* Restore the queue.  */
28009   pop_unparsed_function_queues (parser);
28010 }
28011
28012 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28013
28014      sizeof ... ( identifier )
28015
28016    where the 'sizeof' token has already been consumed.  */
28017
28018 static tree
28019 cp_parser_sizeof_pack (cp_parser *parser)
28020 {
28021   /* Consume the `...'.  */
28022   cp_lexer_consume_token (parser->lexer);
28023   maybe_warn_variadic_templates ();
28024
28025   matching_parens parens;
28026   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28027   if (paren)
28028     parens.consume_open (parser);
28029   else
28030     permerror (cp_lexer_peek_token (parser->lexer)->location,
28031                "%<sizeof...%> argument must be surrounded by parentheses");
28032
28033   cp_token *token = cp_lexer_peek_token (parser->lexer);
28034   tree name = cp_parser_identifier (parser);
28035   if (name == error_mark_node)
28036     return error_mark_node;
28037   /* The name is not qualified.  */
28038   parser->scope = NULL_TREE;
28039   parser->qualifying_scope = NULL_TREE;
28040   parser->object_scope = NULL_TREE;
28041   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28042   if (expr == error_mark_node)
28043     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28044                                  token->location);
28045   if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28046     expr = TREE_TYPE (expr);
28047   else if (TREE_CODE (expr) == CONST_DECL)
28048     expr = DECL_INITIAL (expr);
28049   expr = make_pack_expansion (expr);
28050   PACK_EXPANSION_SIZEOF_P (expr) = true;
28051
28052   if (paren)
28053     parens.require_close (parser);
28054
28055   return expr;
28056 }
28057
28058 /* Parse the operand of `sizeof' (or a similar operator).  Returns
28059    either a TYPE or an expression, depending on the form of the
28060    input.  The KEYWORD indicates which kind of expression we have
28061    encountered.  */
28062
28063 static tree
28064 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28065 {
28066   tree expr = NULL_TREE;
28067   const char *saved_message;
28068   char *tmp;
28069   bool saved_integral_constant_expression_p;
28070   bool saved_non_integral_constant_expression_p;
28071
28072   /* If it's a `...', then we are computing the length of a parameter
28073      pack.  */
28074   if (keyword == RID_SIZEOF
28075       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28076     return cp_parser_sizeof_pack (parser);
28077
28078   /* Types cannot be defined in a `sizeof' expression.  Save away the
28079      old message.  */
28080   saved_message = parser->type_definition_forbidden_message;
28081   /* And create the new one.  */
28082   tmp = concat ("types may not be defined in %<",
28083                 IDENTIFIER_POINTER (ridpointers[keyword]),
28084                 "%> expressions", NULL);
28085   parser->type_definition_forbidden_message = tmp;
28086
28087   /* The restrictions on constant-expressions do not apply inside
28088      sizeof expressions.  */
28089   saved_integral_constant_expression_p
28090     = parser->integral_constant_expression_p;
28091   saved_non_integral_constant_expression_p
28092     = parser->non_integral_constant_expression_p;
28093   parser->integral_constant_expression_p = false;
28094
28095   /* Do not actually evaluate the expression.  */
28096   ++cp_unevaluated_operand;
28097   ++c_inhibit_evaluation_warnings;
28098   /* If it's a `(', then we might be looking at the type-id
28099      construction.  */
28100   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28101     {
28102       tree type = NULL_TREE;
28103
28104       /* We can't be sure yet whether we're looking at a type-id or an
28105          expression.  */
28106       cp_parser_parse_tentatively (parser);
28107
28108       matching_parens parens;
28109       parens.consume_open (parser);
28110
28111       /* Note: as a GNU Extension, compound literals are considered
28112          postfix-expressions as they are in C99, so they are valid
28113          arguments to sizeof.  See comment in cp_parser_cast_expression
28114          for details.  */
28115       if (cp_parser_compound_literal_p (parser))
28116         cp_parser_simulate_error (parser);
28117       else
28118         {
28119           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28120           parser->in_type_id_in_expr_p = true;
28121           /* Look for the type-id.  */
28122           type = cp_parser_type_id (parser);
28123           /* Look for the closing `)'.  */
28124           parens.require_close (parser);
28125           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28126         }
28127
28128       /* If all went well, then we're done.  */
28129       if (cp_parser_parse_definitely (parser))
28130         {
28131           cp_decl_specifier_seq decl_specs;
28132
28133           /* Build a trivial decl-specifier-seq.  */
28134           clear_decl_specs (&decl_specs);
28135           decl_specs.type = type;
28136
28137           /* Call grokdeclarator to figure out what type this is.  */
28138           expr = grokdeclarator (NULL,
28139                                  &decl_specs,
28140                                  TYPENAME,
28141                                  /*initialized=*/0,
28142                                  /*attrlist=*/NULL);
28143         }
28144     }
28145
28146   /* If the type-id production did not work out, then we must be
28147      looking at the unary-expression production.  */
28148   if (!expr)
28149     expr = cp_parser_unary_expression (parser);
28150
28151   /* Go back to evaluating expressions.  */
28152   --cp_unevaluated_operand;
28153   --c_inhibit_evaluation_warnings;
28154
28155   /* Free the message we created.  */
28156   free (tmp);
28157   /* And restore the old one.  */
28158   parser->type_definition_forbidden_message = saved_message;
28159   parser->integral_constant_expression_p
28160     = saved_integral_constant_expression_p;
28161   parser->non_integral_constant_expression_p
28162     = saved_non_integral_constant_expression_p;
28163
28164   return expr;
28165 }
28166
28167 /* If the current declaration has no declarator, return true.  */
28168
28169 static bool
28170 cp_parser_declares_only_class_p (cp_parser *parser)
28171 {
28172   /* If the next token is a `;' or a `,' then there is no
28173      declarator.  */
28174   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28175           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28176 }
28177
28178 /* Update the DECL_SPECS to reflect the storage class indicated by
28179    KEYWORD.  */
28180
28181 static void
28182 cp_parser_set_storage_class (cp_parser *parser,
28183                              cp_decl_specifier_seq *decl_specs,
28184                              enum rid keyword,
28185                              cp_token *token)
28186 {
28187   cp_storage_class storage_class;
28188
28189   if (parser->in_unbraced_linkage_specification_p)
28190     {
28191       error_at (token->location, "invalid use of %qD in linkage specification",
28192                 ridpointers[keyword]);
28193       return;
28194     }
28195   else if (decl_specs->storage_class != sc_none)
28196     {
28197       decl_specs->conflicting_specifiers_p = true;
28198       return;
28199     }
28200
28201   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28202       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28203       && decl_specs->gnu_thread_keyword_p)
28204     {
28205       pedwarn (decl_specs->locations[ds_thread], 0,
28206                 "%<__thread%> before %qD", ridpointers[keyword]);
28207     }
28208
28209   switch (keyword)
28210     {
28211     case RID_AUTO:
28212       storage_class = sc_auto;
28213       break;
28214     case RID_REGISTER:
28215       storage_class = sc_register;
28216       break;
28217     case RID_STATIC:
28218       storage_class = sc_static;
28219       break;
28220     case RID_EXTERN:
28221       storage_class = sc_extern;
28222       break;
28223     case RID_MUTABLE:
28224       storage_class = sc_mutable;
28225       break;
28226     default:
28227       gcc_unreachable ();
28228     }
28229   decl_specs->storage_class = storage_class;
28230   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28231
28232   /* A storage class specifier cannot be applied alongside a typedef 
28233      specifier. If there is a typedef specifier present then set 
28234      conflicting_specifiers_p which will trigger an error later
28235      on in grokdeclarator. */
28236   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28237     decl_specs->conflicting_specifiers_p = true;
28238 }
28239
28240 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
28241    is true, the type is a class or enum definition.  */
28242
28243 static void
28244 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28245                               tree type_spec,
28246                               cp_token *token,
28247                               bool type_definition_p)
28248 {
28249   decl_specs->any_specifiers_p = true;
28250
28251   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28252      (with, for example, in "typedef int wchar_t;") we remember that
28253      this is what happened.  In system headers, we ignore these
28254      declarations so that G++ can work with system headers that are not
28255      C++-safe.  */
28256   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28257       && !type_definition_p
28258       && (type_spec == boolean_type_node
28259           || type_spec == char16_type_node
28260           || type_spec == char32_type_node
28261           || type_spec == wchar_type_node)
28262       && (decl_specs->type
28263           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28264           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28265           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28266           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28267     {
28268       decl_specs->redefined_builtin_type = type_spec;
28269       set_and_check_decl_spec_loc (decl_specs,
28270                                    ds_redefined_builtin_type_spec,
28271                                    token);
28272       if (!decl_specs->type)
28273         {
28274           decl_specs->type = type_spec;
28275           decl_specs->type_definition_p = false;
28276           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28277         }
28278     }
28279   else if (decl_specs->type)
28280     decl_specs->multiple_types_p = true;
28281   else
28282     {
28283       decl_specs->type = type_spec;
28284       decl_specs->type_definition_p = type_definition_p;
28285       decl_specs->redefined_builtin_type = NULL_TREE;
28286       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28287     }
28288 }
28289
28290 /* True iff TOKEN is the GNU keyword __thread.  */
28291
28292 static bool
28293 token_is__thread (cp_token *token)
28294 {
28295   gcc_assert (token->keyword == RID_THREAD);
28296   return id_equal (token->u.value, "__thread");
28297 }
28298
28299 /* Set the location for a declarator specifier and check if it is
28300    duplicated.
28301
28302    DECL_SPECS is the sequence of declarator specifiers onto which to
28303    set the location.
28304
28305    DS is the single declarator specifier to set which location  is to
28306    be set onto the existing sequence of declarators.
28307
28308    LOCATION is the location for the declarator specifier to
28309    consider.  */
28310
28311 static void
28312 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28313                              cp_decl_spec ds, cp_token *token)
28314 {
28315   gcc_assert (ds < ds_last);
28316
28317   if (decl_specs == NULL)
28318     return;
28319
28320   source_location location = token->location;
28321
28322   if (decl_specs->locations[ds] == 0)
28323     {
28324       decl_specs->locations[ds] = location;
28325       if (ds == ds_thread)
28326         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28327     }
28328   else
28329     {
28330       if (ds == ds_long)
28331         {
28332           if (decl_specs->locations[ds_long_long] != 0)
28333             error_at (location,
28334                       "%<long long long%> is too long for GCC");
28335           else
28336             {
28337               decl_specs->locations[ds_long_long] = location;
28338               pedwarn_cxx98 (location,
28339                              OPT_Wlong_long, 
28340                              "ISO C++ 1998 does not support %<long long%>");
28341             }
28342         }
28343       else if (ds == ds_thread)
28344         {
28345           bool gnu = token_is__thread (token);
28346           if (gnu != decl_specs->gnu_thread_keyword_p)
28347             error_at (location,
28348                       "both %<__thread%> and %<thread_local%> specified");
28349           else
28350             {
28351               gcc_rich_location richloc (location);
28352               richloc.add_fixit_remove ();
28353               error_at (&richloc, "duplicate %qD", token->u.value);
28354             }
28355         }
28356       else
28357         {
28358           static const char *const decl_spec_names[] = {
28359             "signed",
28360             "unsigned",
28361             "short",
28362             "long",
28363             "const",
28364             "volatile",
28365             "restrict",
28366             "inline",
28367             "virtual",
28368             "explicit",
28369             "friend",
28370             "typedef",
28371             "using",
28372             "constexpr",
28373             "__complex"
28374           };
28375           gcc_rich_location richloc (location);
28376           richloc.add_fixit_remove ();
28377           error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28378         }
28379     }
28380 }
28381
28382 /* Return true iff the declarator specifier DS is present in the
28383    sequence of declarator specifiers DECL_SPECS.  */
28384
28385 bool
28386 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28387                           cp_decl_spec ds)
28388 {
28389   gcc_assert (ds < ds_last);
28390
28391   if (decl_specs == NULL)
28392     return false;
28393
28394   return decl_specs->locations[ds] != 0;
28395 }
28396
28397 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28398    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
28399
28400 static bool
28401 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28402 {
28403   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28404 }
28405
28406 /* Issue an error message indicating that TOKEN_DESC was expected.
28407    If KEYWORD is true, it indicated this function is called by
28408    cp_parser_require_keword and the required token can only be
28409    a indicated keyword.
28410
28411    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28412    within any error as the location of an "opening" token matching
28413    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28414    RT_CLOSE_PAREN).  */
28415
28416 static void
28417 cp_parser_required_error (cp_parser *parser,
28418                           required_token token_desc,
28419                           bool keyword,
28420                           location_t matching_location)
28421 {
28422   if (cp_parser_simulate_error (parser))
28423     return;
28424
28425   const char *gmsgid = NULL;
28426   switch (token_desc)
28427     {
28428       case RT_NEW:
28429         gmsgid = G_("expected %<new%>");
28430         break;
28431       case RT_DELETE:
28432         gmsgid = G_("expected %<delete%>");
28433         break;
28434       case RT_RETURN:
28435         gmsgid = G_("expected %<return%>");
28436         break;
28437       case RT_WHILE:
28438         gmsgid = G_("expected %<while%>");
28439         break;
28440       case RT_EXTERN:
28441         gmsgid = G_("expected %<extern%>");
28442         break;
28443       case RT_STATIC_ASSERT:
28444         gmsgid = G_("expected %<static_assert%>");
28445         break;
28446       case RT_DECLTYPE:
28447         gmsgid = G_("expected %<decltype%>");
28448         break;
28449       case RT_OPERATOR:
28450         gmsgid = G_("expected %<operator%>");
28451         break;
28452       case RT_CLASS:
28453         gmsgid = G_("expected %<class%>");
28454         break;
28455       case RT_TEMPLATE:
28456         gmsgid = G_("expected %<template%>");
28457         break;
28458       case RT_NAMESPACE:
28459         gmsgid = G_("expected %<namespace%>");
28460         break;
28461       case RT_USING:
28462         gmsgid = G_("expected %<using%>");
28463         break;
28464       case RT_ASM:
28465         gmsgid = G_("expected %<asm%>");
28466         break;
28467       case RT_TRY:
28468         gmsgid = G_("expected %<try%>");
28469         break;
28470       case RT_CATCH:
28471         gmsgid = G_("expected %<catch%>");
28472         break;
28473       case RT_THROW:
28474         gmsgid = G_("expected %<throw%>");
28475         break;
28476       case RT_LABEL:
28477         gmsgid = G_("expected %<__label__%>");
28478         break;
28479       case RT_AT_TRY:
28480         gmsgid = G_("expected %<@try%>");
28481         break;
28482       case RT_AT_SYNCHRONIZED:
28483         gmsgid = G_("expected %<@synchronized%>");
28484         break;
28485       case RT_AT_THROW:
28486         gmsgid = G_("expected %<@throw%>");
28487         break;
28488       case RT_TRANSACTION_ATOMIC:
28489         gmsgid = G_("expected %<__transaction_atomic%>");
28490         break;
28491       case RT_TRANSACTION_RELAXED:
28492         gmsgid = G_("expected %<__transaction_relaxed%>");
28493         break;
28494       default:
28495         break;
28496     }
28497
28498   if (!gmsgid && !keyword)
28499     {
28500       switch (token_desc)
28501         {
28502           case RT_SEMICOLON:
28503             gmsgid = G_("expected %<;%>");
28504             break;
28505           case RT_OPEN_PAREN:
28506             gmsgid = G_("expected %<(%>");
28507             break;
28508           case RT_CLOSE_BRACE:
28509             gmsgid = G_("expected %<}%>");
28510             break;
28511           case RT_OPEN_BRACE:
28512             gmsgid = G_("expected %<{%>");
28513             break;
28514           case RT_CLOSE_SQUARE:
28515             gmsgid = G_("expected %<]%>");
28516             break;
28517           case RT_OPEN_SQUARE:
28518             gmsgid = G_("expected %<[%>");
28519             break;
28520           case RT_COMMA:
28521             gmsgid = G_("expected %<,%>");
28522             break;
28523           case RT_SCOPE:
28524             gmsgid = G_("expected %<::%>");
28525             break;
28526           case RT_LESS:
28527             gmsgid = G_("expected %<<%>");
28528             break;
28529           case RT_GREATER:
28530             gmsgid = G_("expected %<>%>");
28531             break;
28532           case RT_EQ:
28533             gmsgid = G_("expected %<=%>");
28534             break;
28535           case RT_ELLIPSIS:
28536             gmsgid = G_("expected %<...%>");
28537             break;
28538           case RT_MULT:
28539             gmsgid = G_("expected %<*%>");
28540             break;
28541           case RT_COMPL:
28542             gmsgid = G_("expected %<~%>");
28543             break;
28544           case RT_COLON:
28545             gmsgid = G_("expected %<:%>");
28546             break;
28547           case RT_COLON_SCOPE:
28548             gmsgid = G_("expected %<:%> or %<::%>");
28549             break;
28550           case RT_CLOSE_PAREN:
28551             gmsgid = G_("expected %<)%>");
28552             break;
28553           case RT_COMMA_CLOSE_PAREN:
28554             gmsgid = G_("expected %<,%> or %<)%>");
28555             break;
28556           case RT_PRAGMA_EOL:
28557             gmsgid = G_("expected end of line");
28558             break;
28559           case RT_NAME:
28560             gmsgid = G_("expected identifier");
28561             break;
28562           case RT_SELECT:
28563             gmsgid = G_("expected selection-statement");
28564             break;
28565           case RT_ITERATION:
28566             gmsgid = G_("expected iteration-statement");
28567             break;
28568           case RT_JUMP:
28569             gmsgid = G_("expected jump-statement");
28570             break;
28571           case RT_CLASS_KEY:
28572             gmsgid = G_("expected class-key");
28573             break;
28574           case RT_CLASS_TYPENAME_TEMPLATE:
28575             gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28576             break;
28577           default:
28578             gcc_unreachable ();
28579         }
28580     }
28581
28582   if (gmsgid)
28583     cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28584 }
28585
28586
28587 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
28588    issue an error message indicating that TOKEN_DESC was expected.
28589
28590    Returns the token consumed, if the token had the appropriate type.
28591    Otherwise, returns NULL.
28592
28593    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28594    within any error as the location of an "opening" token matching
28595    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28596    RT_CLOSE_PAREN).  */
28597
28598 static cp_token *
28599 cp_parser_require (cp_parser* parser,
28600                    enum cpp_ttype type,
28601                    required_token token_desc,
28602                    location_t matching_location)
28603 {
28604   if (cp_lexer_next_token_is (parser->lexer, type))
28605     return cp_lexer_consume_token (parser->lexer);
28606   else
28607     {
28608       /* Output the MESSAGE -- unless we're parsing tentatively.  */
28609       if (!cp_parser_simulate_error (parser))
28610         cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28611                                   matching_location);
28612       return NULL;
28613     }
28614 }
28615
28616 /* An error message is produced if the next token is not '>'.
28617    All further tokens are skipped until the desired token is
28618    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
28619
28620 static void
28621 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28622 {
28623   /* Current level of '< ... >'.  */
28624   unsigned level = 0;
28625   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
28626   unsigned nesting_depth = 0;
28627
28628   /* Are we ready, yet?  If not, issue error message.  */
28629   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28630     return;
28631
28632   /* Skip tokens until the desired token is found.  */
28633   while (true)
28634     {
28635       /* Peek at the next token.  */
28636       switch (cp_lexer_peek_token (parser->lexer)->type)
28637         {
28638         case CPP_LESS:
28639           if (!nesting_depth)
28640             ++level;
28641           break;
28642
28643         case CPP_RSHIFT:
28644           if (cxx_dialect == cxx98)
28645             /* C++0x views the `>>' operator as two `>' tokens, but
28646                C++98 does not. */
28647             break;
28648           else if (!nesting_depth && level-- == 0)
28649             {
28650               /* We've hit a `>>' where the first `>' closes the
28651                  template argument list, and the second `>' is
28652                  spurious.  Just consume the `>>' and stop; we've
28653                  already produced at least one error.  */
28654               cp_lexer_consume_token (parser->lexer);
28655               return;
28656             }
28657           /* Fall through for C++0x, so we handle the second `>' in
28658              the `>>'.  */
28659           gcc_fallthrough ();
28660
28661         case CPP_GREATER:
28662           if (!nesting_depth && level-- == 0)
28663             {
28664               /* We've reached the token we want, consume it and stop.  */
28665               cp_lexer_consume_token (parser->lexer);
28666               return;
28667             }
28668           break;
28669
28670         case CPP_OPEN_PAREN:
28671         case CPP_OPEN_SQUARE:
28672           ++nesting_depth;
28673           break;
28674
28675         case CPP_CLOSE_PAREN:
28676         case CPP_CLOSE_SQUARE:
28677           if (nesting_depth-- == 0)
28678             return;
28679           break;
28680
28681         case CPP_EOF:
28682         case CPP_PRAGMA_EOL:
28683         case CPP_SEMICOLON:
28684         case CPP_OPEN_BRACE:
28685         case CPP_CLOSE_BRACE:
28686           /* The '>' was probably forgotten, don't look further.  */
28687           return;
28688
28689         default:
28690           break;
28691         }
28692
28693       /* Consume this token.  */
28694       cp_lexer_consume_token (parser->lexer);
28695     }
28696 }
28697
28698 /* If the next token is the indicated keyword, consume it.  Otherwise,
28699    issue an error message indicating that TOKEN_DESC was expected.
28700
28701    Returns the token consumed, if the token had the appropriate type.
28702    Otherwise, returns NULL.  */
28703
28704 static cp_token *
28705 cp_parser_require_keyword (cp_parser* parser,
28706                            enum rid keyword,
28707                            required_token token_desc)
28708 {
28709   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28710
28711   if (token && token->keyword != keyword)
28712     {
28713       cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28714                                 UNKNOWN_LOCATION);
28715       return NULL;
28716     }
28717
28718   return token;
28719 }
28720
28721 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28722    function-definition.  */
28723
28724 static bool
28725 cp_parser_token_starts_function_definition_p (cp_token* token)
28726 {
28727   return (/* An ordinary function-body begins with an `{'.  */
28728           token->type == CPP_OPEN_BRACE
28729           /* A ctor-initializer begins with a `:'.  */
28730           || token->type == CPP_COLON
28731           /* A function-try-block begins with `try'.  */
28732           || token->keyword == RID_TRY
28733           /* A function-transaction-block begins with `__transaction_atomic'
28734              or `__transaction_relaxed'.  */
28735           || token->keyword == RID_TRANSACTION_ATOMIC
28736           || token->keyword == RID_TRANSACTION_RELAXED
28737           /* The named return value extension begins with `return'.  */
28738           || token->keyword == RID_RETURN);
28739 }
28740
28741 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28742    definition.  */
28743
28744 static bool
28745 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28746 {
28747   cp_token *token;
28748
28749   token = cp_lexer_peek_token (parser->lexer);
28750   return (token->type == CPP_OPEN_BRACE
28751           || (token->type == CPP_COLON
28752               && !parser->colon_doesnt_start_class_def_p));
28753 }
28754
28755 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28756    C++0x) ending a template-argument.  */
28757
28758 static bool
28759 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28760 {
28761   cp_token *token;
28762
28763   token = cp_lexer_peek_token (parser->lexer);
28764   return (token->type == CPP_COMMA 
28765           || token->type == CPP_GREATER
28766           || token->type == CPP_ELLIPSIS
28767           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28768 }
28769
28770 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28771    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
28772
28773 static bool
28774 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28775                                                      size_t n)
28776 {
28777   cp_token *token;
28778
28779   token = cp_lexer_peek_nth_token (parser->lexer, n);
28780   if (token->type == CPP_LESS)
28781     return true;
28782   /* Check for the sequence `<::' in the original code. It would be lexed as
28783      `[:', where `[' is a digraph, and there is no whitespace before
28784      `:'.  */
28785   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28786     {
28787       cp_token *token2;
28788       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28789       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28790         return true;
28791     }
28792   return false;
28793 }
28794
28795 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28796    or none_type otherwise.  */
28797
28798 static enum tag_types
28799 cp_parser_token_is_class_key (cp_token* token)
28800 {
28801   switch (token->keyword)
28802     {
28803     case RID_CLASS:
28804       return class_type;
28805     case RID_STRUCT:
28806       return record_type;
28807     case RID_UNION:
28808       return union_type;
28809
28810     default:
28811       return none_type;
28812     }
28813 }
28814
28815 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28816    or none_type otherwise or if the token is null.  */
28817
28818 static enum tag_types
28819 cp_parser_token_is_type_parameter_key (cp_token* token)
28820 {
28821   if (!token)
28822     return none_type;
28823
28824   switch (token->keyword)
28825     {
28826     case RID_CLASS:
28827       return class_type;
28828     case RID_TYPENAME:
28829       return typename_type;
28830
28831     default:
28832       return none_type;
28833     }
28834 }
28835
28836 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
28837
28838 static void
28839 cp_parser_check_class_key (enum tag_types class_key, tree type)
28840 {
28841   if (type == error_mark_node)
28842     return;
28843   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28844     {
28845       if (permerror (input_location, "%qs tag used in naming %q#T",
28846                      class_key == union_type ? "union"
28847                      : class_key == record_type ? "struct" : "class",
28848                      type))
28849         inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28850                 "%q#T was previously declared here", type);
28851     }
28852 }
28853
28854 /* Issue an error message if DECL is redeclared with different
28855    access than its original declaration [class.access.spec/3].
28856    This applies to nested classes, nested class templates and
28857    enumerations [class.mem/1].  */
28858
28859 static void
28860 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28861 {
28862   if (!decl
28863       || (!CLASS_TYPE_P (TREE_TYPE (decl))
28864           && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28865     return;
28866
28867   if ((TREE_PRIVATE (decl)
28868        != (current_access_specifier == access_private_node))
28869       || (TREE_PROTECTED (decl)
28870           != (current_access_specifier == access_protected_node)))
28871     error_at (location, "%qD redeclared with different access", decl);
28872 }
28873
28874 /* Look for the `template' keyword, as a syntactic disambiguator.
28875    Return TRUE iff it is present, in which case it will be
28876    consumed.  */
28877
28878 static bool
28879 cp_parser_optional_template_keyword (cp_parser *parser)
28880 {
28881   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28882     {
28883       /* In C++98 the `template' keyword can only be used within templates;
28884          outside templates the parser can always figure out what is a
28885          template and what is not.  In C++11,  per the resolution of DR 468,
28886          `template' is allowed in cases where it is not strictly necessary.  */
28887       if (!processing_template_decl
28888           && pedantic && cxx_dialect == cxx98)
28889         {
28890           cp_token *token = cp_lexer_peek_token (parser->lexer);
28891           pedwarn (token->location, OPT_Wpedantic,
28892                    "in C++98 %<template%> (as a disambiguator) is only "
28893                    "allowed within templates");
28894           /* If this part of the token stream is rescanned, the same
28895              error message would be generated.  So, we purge the token
28896              from the stream.  */
28897           cp_lexer_purge_token (parser->lexer);
28898           return false;
28899         }
28900       else
28901         {
28902           /* Consume the `template' keyword.  */
28903           cp_lexer_consume_token (parser->lexer);
28904           return true;
28905         }
28906     }
28907   return false;
28908 }
28909
28910 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
28911    set PARSER->SCOPE, and perform other related actions.  */
28912
28913 static void
28914 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28915 {
28916   struct tree_check *check_value;
28917
28918   /* Get the stored value.  */
28919   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28920   /* Set the scope from the stored value.  */
28921   parser->scope = saved_checks_value (check_value);
28922   parser->qualifying_scope = check_value->qualifying_scope;
28923   parser->object_scope = NULL_TREE;
28924 }
28925
28926 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
28927    encounter the end of a block before what we were looking for.  */
28928
28929 static bool
28930 cp_parser_cache_group (cp_parser *parser,
28931                        enum cpp_ttype end,
28932                        unsigned depth)
28933 {
28934   while (true)
28935     {
28936       cp_token *token = cp_lexer_peek_token (parser->lexer);
28937
28938       /* Abort a parenthesized expression if we encounter a semicolon.  */
28939       if ((end == CPP_CLOSE_PAREN || depth == 0)
28940           && token->type == CPP_SEMICOLON)
28941         return true;
28942       /* If we've reached the end of the file, stop.  */
28943       if (token->type == CPP_EOF
28944           || (end != CPP_PRAGMA_EOL
28945               && token->type == CPP_PRAGMA_EOL))
28946         return true;
28947       if (token->type == CPP_CLOSE_BRACE && depth == 0)
28948         /* We've hit the end of an enclosing block, so there's been some
28949            kind of syntax error.  */
28950         return true;
28951
28952       /* Consume the token.  */
28953       cp_lexer_consume_token (parser->lexer);
28954       /* See if it starts a new group.  */
28955       if (token->type == CPP_OPEN_BRACE)
28956         {
28957           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28958           /* In theory this should probably check end == '}', but
28959              cp_parser_save_member_function_body needs it to exit
28960              after either '}' or ')' when called with ')'.  */
28961           if (depth == 0)
28962             return false;
28963         }
28964       else if (token->type == CPP_OPEN_PAREN)
28965         {
28966           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28967           if (depth == 0 && end == CPP_CLOSE_PAREN)
28968             return false;
28969         }
28970       else if (token->type == CPP_PRAGMA)
28971         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28972       else if (token->type == end)
28973         return false;
28974     }
28975 }
28976
28977 /* Like above, for caching a default argument or NSDMI.  Both of these are
28978    terminated by a non-nested comma, but it can be unclear whether or not a
28979    comma is nested in a template argument list unless we do more parsing.
28980    In order to handle this ambiguity, when we encounter a ',' after a '<'
28981    we try to parse what follows as a parameter-declaration-list (in the
28982    case of a default argument) or a member-declarator (in the case of an
28983    NSDMI).  If that succeeds, then we stop caching.  */
28984
28985 static tree
28986 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28987 {
28988   unsigned depth = 0;
28989   int maybe_template_id = 0;
28990   cp_token *first_token;
28991   cp_token *token;
28992   tree default_argument;
28993
28994   /* Add tokens until we have processed the entire default
28995      argument.  We add the range [first_token, token).  */
28996   first_token = cp_lexer_peek_token (parser->lexer);
28997   if (first_token->type == CPP_OPEN_BRACE)
28998     {
28999       /* For list-initialization, this is straightforward.  */
29000       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29001       token = cp_lexer_peek_token (parser->lexer);
29002     }
29003   else while (true)
29004     {
29005       bool done = false;
29006
29007       /* Peek at the next token.  */
29008       token = cp_lexer_peek_token (parser->lexer);
29009       /* What we do depends on what token we have.  */
29010       switch (token->type)
29011         {
29012           /* In valid code, a default argument must be
29013              immediately followed by a `,' `)', or `...'.  */
29014         case CPP_COMMA:
29015           if (depth == 0 && maybe_template_id)
29016             {
29017               /* If we've seen a '<', we might be in a
29018                  template-argument-list.  Until Core issue 325 is
29019                  resolved, we don't know how this situation ought
29020                  to be handled, so try to DTRT.  We check whether
29021                  what comes after the comma is a valid parameter
29022                  declaration list.  If it is, then the comma ends
29023                  the default argument; otherwise the default
29024                  argument continues.  */
29025               bool error = false;
29026               cp_token *peek;
29027
29028               /* Set ITALP so cp_parser_parameter_declaration_list
29029                  doesn't decide to commit to this parse.  */
29030               bool saved_italp = parser->in_template_argument_list_p;
29031               parser->in_template_argument_list_p = true;
29032
29033               cp_parser_parse_tentatively (parser);
29034
29035               if (nsdmi)
29036                 {
29037                   /* Parse declarators until we reach a non-comma or
29038                      somthing that cannot be an initializer.
29039                      Just checking whether we're looking at a single
29040                      declarator is insufficient.  Consider:
29041                        int var = tuple<T,U>::x;
29042                      The template parameter 'U' looks exactly like a
29043                      declarator.  */
29044                   do
29045                     {
29046                       int ctor_dtor_or_conv_p;
29047                       cp_lexer_consume_token (parser->lexer);
29048                       cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29049                                             &ctor_dtor_or_conv_p,
29050                                             /*parenthesized_p=*/NULL,
29051                                             /*member_p=*/true,
29052                                             /*friend_p=*/false);
29053                       peek = cp_lexer_peek_token (parser->lexer);
29054                       if (cp_parser_error_occurred (parser))
29055                         break;
29056                     }
29057                   while (peek->type == CPP_COMMA);
29058                   /* If we met an '=' or ';' then the original comma
29059                      was the end of the NSDMI.  Otherwise assume
29060                      we're still in the NSDMI.  */
29061                   error = (peek->type != CPP_EQ
29062                            && peek->type != CPP_SEMICOLON);
29063                 }
29064               else
29065                 {
29066                   cp_lexer_consume_token (parser->lexer);
29067                   begin_scope (sk_function_parms, NULL_TREE);
29068                   cp_parser_parameter_declaration_list (parser, &error);
29069                   pop_bindings_and_leave_scope ();
29070                 }
29071               if (!cp_parser_error_occurred (parser) && !error)
29072                 done = true;
29073               cp_parser_abort_tentative_parse (parser);
29074
29075               parser->in_template_argument_list_p = saved_italp;
29076               break;
29077             }
29078           /* FALLTHRU */
29079         case CPP_CLOSE_PAREN:
29080         case CPP_ELLIPSIS:
29081           /* If we run into a non-nested `;', `}', or `]',
29082              then the code is invalid -- but the default
29083              argument is certainly over.  */
29084         case CPP_SEMICOLON:
29085         case CPP_CLOSE_BRACE:
29086         case CPP_CLOSE_SQUARE:
29087           if (depth == 0
29088               /* Handle correctly int n = sizeof ... ( p );  */
29089               && token->type != CPP_ELLIPSIS)
29090             done = true;
29091           /* Update DEPTH, if necessary.  */
29092           else if (token->type == CPP_CLOSE_PAREN
29093                    || token->type == CPP_CLOSE_BRACE
29094                    || token->type == CPP_CLOSE_SQUARE)
29095             --depth;
29096           break;
29097
29098         case CPP_OPEN_PAREN:
29099         case CPP_OPEN_SQUARE:
29100         case CPP_OPEN_BRACE:
29101           ++depth;
29102           break;
29103
29104         case CPP_LESS:
29105           if (depth == 0)
29106             /* This might be the comparison operator, or it might
29107                start a template argument list.  */
29108             ++maybe_template_id;
29109           break;
29110
29111         case CPP_RSHIFT:
29112           if (cxx_dialect == cxx98)
29113             break;
29114           /* Fall through for C++0x, which treats the `>>'
29115              operator like two `>' tokens in certain
29116              cases.  */
29117           gcc_fallthrough ();
29118
29119         case CPP_GREATER:
29120           if (depth == 0)
29121             {
29122               /* This might be an operator, or it might close a
29123                  template argument list.  But if a previous '<'
29124                  started a template argument list, this will have
29125                  closed it, so we can't be in one anymore.  */
29126               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29127               if (maybe_template_id < 0)
29128                 maybe_template_id = 0;
29129             }
29130           break;
29131
29132           /* If we run out of tokens, issue an error message.  */
29133         case CPP_EOF:
29134         case CPP_PRAGMA_EOL:
29135           error_at (token->location, "file ends in default argument");
29136           return error_mark_node;
29137
29138         case CPP_NAME:
29139         case CPP_SCOPE:
29140           /* In these cases, we should look for template-ids.
29141              For example, if the default argument is
29142              `X<int, double>()', we need to do name lookup to
29143              figure out whether or not `X' is a template; if
29144              so, the `,' does not end the default argument.
29145
29146              That is not yet done.  */
29147           break;
29148
29149         default:
29150           break;
29151         }
29152
29153       /* If we've reached the end, stop.  */
29154       if (done)
29155         break;
29156
29157       /* Add the token to the token block.  */
29158       token = cp_lexer_consume_token (parser->lexer);
29159     }
29160
29161   /* Create a DEFAULT_ARG to represent the unparsed default
29162      argument.  */
29163   default_argument = make_node (DEFAULT_ARG);
29164   DEFARG_TOKENS (default_argument)
29165     = cp_token_cache_new (first_token, token);
29166   DEFARG_INSTANTIATIONS (default_argument) = NULL;
29167
29168   return default_argument;
29169 }
29170
29171 /* A location to use for diagnostics about an unparsed DEFAULT_ARG.  */
29172
29173 location_t
29174 defarg_location (tree default_argument)
29175 {
29176   cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29177   location_t start = tokens->first->location;
29178   location_t end = tokens->last->location;
29179   return make_location (start, start, end);
29180 }
29181
29182 /* Begin parsing tentatively.  We always save tokens while parsing
29183    tentatively so that if the tentative parsing fails we can restore the
29184    tokens.  */
29185
29186 static void
29187 cp_parser_parse_tentatively (cp_parser* parser)
29188 {
29189   /* Enter a new parsing context.  */
29190   parser->context = cp_parser_context_new (parser->context);
29191   /* Begin saving tokens.  */
29192   cp_lexer_save_tokens (parser->lexer);
29193   /* In order to avoid repetitive access control error messages,
29194      access checks are queued up until we are no longer parsing
29195      tentatively.  */
29196   push_deferring_access_checks (dk_deferred);
29197 }
29198
29199 /* Commit to the currently active tentative parse.  */
29200
29201 static void
29202 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29203 {
29204   cp_parser_context *context;
29205   cp_lexer *lexer;
29206
29207   /* Mark all of the levels as committed.  */
29208   lexer = parser->lexer;
29209   for (context = parser->context; context->next; context = context->next)
29210     {
29211       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29212         break;
29213       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29214       while (!cp_lexer_saving_tokens (lexer))
29215         lexer = lexer->next;
29216       cp_lexer_commit_tokens (lexer);
29217     }
29218 }
29219
29220 /* Commit to the topmost currently active tentative parse.
29221
29222    Note that this function shouldn't be called when there are
29223    irreversible side-effects while in a tentative state.  For
29224    example, we shouldn't create a permanent entry in the symbol
29225    table, or issue an error message that might not apply if the
29226    tentative parse is aborted.  */
29227
29228 static void
29229 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29230 {
29231   cp_parser_context *context = parser->context;
29232   cp_lexer *lexer = parser->lexer;
29233
29234   if (context)
29235     {
29236       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29237         return;
29238       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29239
29240       while (!cp_lexer_saving_tokens (lexer))
29241         lexer = lexer->next;
29242       cp_lexer_commit_tokens (lexer);
29243     }
29244 }
29245
29246 /* Abort the currently active tentative parse.  All consumed tokens
29247    will be rolled back, and no diagnostics will be issued.  */
29248
29249 static void
29250 cp_parser_abort_tentative_parse (cp_parser* parser)
29251 {
29252   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29253               || errorcount > 0);
29254   cp_parser_simulate_error (parser);
29255   /* Now, pretend that we want to see if the construct was
29256      successfully parsed.  */
29257   cp_parser_parse_definitely (parser);
29258 }
29259
29260 /* Stop parsing tentatively.  If a parse error has occurred, restore the
29261    token stream.  Otherwise, commit to the tokens we have consumed.
29262    Returns true if no error occurred; false otherwise.  */
29263
29264 static bool
29265 cp_parser_parse_definitely (cp_parser* parser)
29266 {
29267   bool error_occurred;
29268   cp_parser_context *context;
29269
29270   /* Remember whether or not an error occurred, since we are about to
29271      destroy that information.  */
29272   error_occurred = cp_parser_error_occurred (parser);
29273   /* Remove the topmost context from the stack.  */
29274   context = parser->context;
29275   parser->context = context->next;
29276   /* If no parse errors occurred, commit to the tentative parse.  */
29277   if (!error_occurred)
29278     {
29279       /* Commit to the tokens read tentatively, unless that was
29280          already done.  */
29281       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29282         cp_lexer_commit_tokens (parser->lexer);
29283
29284       pop_to_parent_deferring_access_checks ();
29285     }
29286   /* Otherwise, if errors occurred, roll back our state so that things
29287      are just as they were before we began the tentative parse.  */
29288   else
29289     {
29290       cp_lexer_rollback_tokens (parser->lexer);
29291       pop_deferring_access_checks ();
29292     }
29293   /* Add the context to the front of the free list.  */
29294   context->next = cp_parser_context_free_list;
29295   cp_parser_context_free_list = context;
29296
29297   return !error_occurred;
29298 }
29299
29300 /* Returns true if we are parsing tentatively and are not committed to
29301    this tentative parse.  */
29302
29303 static bool
29304 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29305 {
29306   return (cp_parser_parsing_tentatively (parser)
29307           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29308 }
29309
29310 /* Returns nonzero iff an error has occurred during the most recent
29311    tentative parse.  */
29312
29313 static bool
29314 cp_parser_error_occurred (cp_parser* parser)
29315 {
29316   return (cp_parser_parsing_tentatively (parser)
29317           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29318 }
29319
29320 /* Returns nonzero if GNU extensions are allowed.  */
29321
29322 static bool
29323 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29324 {
29325   return parser->allow_gnu_extensions_p;
29326 }
29327 \f
29328 /* Objective-C++ Productions */
29329
29330
29331 /* Parse an Objective-C expression, which feeds into a primary-expression
29332    above.
29333
29334    objc-expression:
29335      objc-message-expression
29336      objc-string-literal
29337      objc-encode-expression
29338      objc-protocol-expression
29339      objc-selector-expression
29340
29341   Returns a tree representation of the expression.  */
29342
29343 static cp_expr
29344 cp_parser_objc_expression (cp_parser* parser)
29345 {
29346   /* Try to figure out what kind of declaration is present.  */
29347   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29348
29349   switch (kwd->type)
29350     {
29351     case CPP_OPEN_SQUARE:
29352       return cp_parser_objc_message_expression (parser);
29353
29354     case CPP_OBJC_STRING:
29355       kwd = cp_lexer_consume_token (parser->lexer);
29356       return objc_build_string_object (kwd->u.value);
29357
29358     case CPP_KEYWORD:
29359       switch (kwd->keyword)
29360         {
29361         case RID_AT_ENCODE:
29362           return cp_parser_objc_encode_expression (parser);
29363
29364         case RID_AT_PROTOCOL:
29365           return cp_parser_objc_protocol_expression (parser);
29366
29367         case RID_AT_SELECTOR:
29368           return cp_parser_objc_selector_expression (parser);
29369
29370         default:
29371           break;
29372         }
29373       /* FALLTHRU */
29374     default:
29375       error_at (kwd->location,
29376                 "misplaced %<@%D%> Objective-C++ construct",
29377                 kwd->u.value);
29378       cp_parser_skip_to_end_of_block_or_statement (parser);
29379     }
29380
29381   return error_mark_node;
29382 }
29383
29384 /* Parse an Objective-C message expression.
29385
29386    objc-message-expression:
29387      [ objc-message-receiver objc-message-args ]
29388
29389    Returns a representation of an Objective-C message.  */
29390
29391 static tree
29392 cp_parser_objc_message_expression (cp_parser* parser)
29393 {
29394   tree receiver, messageargs;
29395
29396   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29397   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
29398   receiver = cp_parser_objc_message_receiver (parser);
29399   messageargs = cp_parser_objc_message_args (parser);
29400   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29401   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29402
29403   tree result = objc_build_message_expr (receiver, messageargs);
29404
29405   /* Construct a location e.g.
29406        [self func1:5]
29407        ^~~~~~~~~~~~~~
29408      ranging from the '[' to the ']', with the caret at the start.  */
29409   location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29410   protected_set_expr_location (result, combined_loc);
29411
29412   return result;
29413 }
29414
29415 /* Parse an objc-message-receiver.
29416
29417    objc-message-receiver:
29418      expression
29419      simple-type-specifier
29420
29421   Returns a representation of the type or expression.  */
29422
29423 static tree
29424 cp_parser_objc_message_receiver (cp_parser* parser)
29425 {
29426   tree rcv;
29427
29428   /* An Objective-C message receiver may be either (1) a type
29429      or (2) an expression.  */
29430   cp_parser_parse_tentatively (parser);
29431   rcv = cp_parser_expression (parser);
29432
29433   /* If that worked out, fine.  */
29434   if (cp_parser_parse_definitely (parser))
29435     return rcv;
29436
29437   cp_parser_parse_tentatively (parser);
29438   rcv = cp_parser_simple_type_specifier (parser,
29439                                          /*decl_specs=*/NULL,
29440                                          CP_PARSER_FLAGS_NONE);
29441
29442   if (cp_parser_parse_definitely (parser))
29443     return objc_get_class_reference (rcv);
29444   
29445   cp_parser_error (parser, "objective-c++ message receiver expected");
29446   return error_mark_node;
29447 }
29448
29449 /* Parse the arguments and selectors comprising an Objective-C message.
29450
29451    objc-message-args:
29452      objc-selector
29453      objc-selector-args
29454      objc-selector-args , objc-comma-args
29455
29456    objc-selector-args:
29457      objc-selector [opt] : assignment-expression
29458      objc-selector-args objc-selector [opt] : assignment-expression
29459
29460    objc-comma-args:
29461      assignment-expression
29462      objc-comma-args , assignment-expression
29463
29464    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29465    selector arguments and TREE_VALUE containing a list of comma
29466    arguments.  */
29467
29468 static tree
29469 cp_parser_objc_message_args (cp_parser* parser)
29470 {
29471   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29472   bool maybe_unary_selector_p = true;
29473   cp_token *token = cp_lexer_peek_token (parser->lexer);
29474
29475   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29476     {
29477       tree selector = NULL_TREE, arg;
29478
29479       if (token->type != CPP_COLON)
29480         selector = cp_parser_objc_selector (parser);
29481
29482       /* Detect if we have a unary selector.  */
29483       if (maybe_unary_selector_p
29484           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29485         return build_tree_list (selector, NULL_TREE);
29486
29487       maybe_unary_selector_p = false;
29488       cp_parser_require (parser, CPP_COLON, RT_COLON);
29489       arg = cp_parser_assignment_expression (parser);
29490
29491       sel_args
29492         = chainon (sel_args,
29493                    build_tree_list (selector, arg));
29494
29495       token = cp_lexer_peek_token (parser->lexer);
29496     }
29497
29498   /* Handle non-selector arguments, if any. */
29499   while (token->type == CPP_COMMA)
29500     {
29501       tree arg;
29502
29503       cp_lexer_consume_token (parser->lexer);
29504       arg = cp_parser_assignment_expression (parser);
29505
29506       addl_args
29507         = chainon (addl_args,
29508                    build_tree_list (NULL_TREE, arg));
29509
29510       token = cp_lexer_peek_token (parser->lexer);
29511     }
29512
29513   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29514     {
29515       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29516       return build_tree_list (error_mark_node, error_mark_node);
29517     }
29518
29519   return build_tree_list (sel_args, addl_args);
29520 }
29521
29522 /* Parse an Objective-C encode expression.
29523
29524    objc-encode-expression:
29525      @encode objc-typename
29526
29527    Returns an encoded representation of the type argument.  */
29528
29529 static cp_expr
29530 cp_parser_objc_encode_expression (cp_parser* parser)
29531 {
29532   tree type;
29533   cp_token *token;
29534   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29535
29536   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
29537   matching_parens parens;
29538   parens.require_open (parser);
29539   token = cp_lexer_peek_token (parser->lexer);
29540   type = complete_type (cp_parser_type_id (parser));
29541   parens.require_close (parser);
29542
29543   if (!type)
29544     {
29545       error_at (token->location, 
29546                 "%<@encode%> must specify a type as an argument");
29547       return error_mark_node;
29548     }
29549
29550   /* This happens if we find @encode(T) (where T is a template
29551      typename or something dependent on a template typename) when
29552      parsing a template.  In that case, we can't compile it
29553      immediately, but we rather create an AT_ENCODE_EXPR which will
29554      need to be instantiated when the template is used.
29555   */
29556   if (dependent_type_p (type))
29557     {
29558       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29559       TREE_READONLY (value) = 1;
29560       return value;
29561     }
29562
29563
29564   /* Build a location of the form:
29565        @encode(int)
29566        ^~~~~~~~~~~~
29567      with caret==start at the @ token, finishing at the close paren.  */
29568   location_t combined_loc
29569     = make_location (start_loc, start_loc,
29570                      cp_lexer_previous_token (parser->lexer)->location);
29571
29572   return cp_expr (objc_build_encode_expr (type), combined_loc);
29573 }
29574
29575 /* Parse an Objective-C @defs expression.  */
29576
29577 static tree
29578 cp_parser_objc_defs_expression (cp_parser *parser)
29579 {
29580   tree name;
29581
29582   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
29583   matching_parens parens;
29584   parens.require_open (parser);
29585   name = cp_parser_identifier (parser);
29586   parens.require_close (parser);
29587
29588   return objc_get_class_ivars (name);
29589 }
29590
29591 /* Parse an Objective-C protocol expression.
29592
29593   objc-protocol-expression:
29594     @protocol ( identifier )
29595
29596   Returns a representation of the protocol expression.  */
29597
29598 static tree
29599 cp_parser_objc_protocol_expression (cp_parser* parser)
29600 {
29601   tree proto;
29602   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29603
29604   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
29605   matching_parens parens;
29606   parens.require_open (parser);
29607   proto = cp_parser_identifier (parser);
29608   parens.require_close (parser);
29609
29610   /* Build a location of the form:
29611        @protocol(prot)
29612        ^~~~~~~~~~~~~~~
29613      with caret==start at the @ token, finishing at the close paren.  */
29614   location_t combined_loc
29615     = make_location (start_loc, start_loc,
29616                      cp_lexer_previous_token (parser->lexer)->location);
29617   tree result = objc_build_protocol_expr (proto);
29618   protected_set_expr_location (result, combined_loc);
29619   return result;
29620 }
29621
29622 /* Parse an Objective-C selector expression.
29623
29624    objc-selector-expression:
29625      @selector ( objc-method-signature )
29626
29627    objc-method-signature:
29628      objc-selector
29629      objc-selector-seq
29630
29631    objc-selector-seq:
29632      objc-selector :
29633      objc-selector-seq objc-selector :
29634
29635   Returns a representation of the method selector.  */
29636
29637 static tree
29638 cp_parser_objc_selector_expression (cp_parser* parser)
29639 {
29640   tree sel_seq = NULL_TREE;
29641   bool maybe_unary_selector_p = true;
29642   cp_token *token;
29643   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29644
29645   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
29646   matching_parens parens;
29647   parens.require_open (parser);
29648   token = cp_lexer_peek_token (parser->lexer);
29649
29650   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29651          || token->type == CPP_SCOPE)
29652     {
29653       tree selector = NULL_TREE;
29654
29655       if (token->type != CPP_COLON
29656           || token->type == CPP_SCOPE)
29657         selector = cp_parser_objc_selector (parser);
29658
29659       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29660           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29661         {
29662           /* Detect if we have a unary selector.  */
29663           if (maybe_unary_selector_p)
29664             {
29665               sel_seq = selector;
29666               goto finish_selector;
29667             }
29668           else
29669             {
29670               cp_parser_error (parser, "expected %<:%>");
29671             }
29672         }
29673       maybe_unary_selector_p = false;
29674       token = cp_lexer_consume_token (parser->lexer);
29675
29676       if (token->type == CPP_SCOPE)
29677         {
29678           sel_seq
29679             = chainon (sel_seq,
29680                        build_tree_list (selector, NULL_TREE));
29681           sel_seq
29682             = chainon (sel_seq,
29683                        build_tree_list (NULL_TREE, NULL_TREE));
29684         }
29685       else
29686         sel_seq
29687           = chainon (sel_seq,
29688                      build_tree_list (selector, NULL_TREE));
29689
29690       token = cp_lexer_peek_token (parser->lexer);
29691     }
29692
29693  finish_selector:
29694   parens.require_close (parser);
29695
29696
29697   /* Build a location of the form:
29698        @selector(func)
29699        ^~~~~~~~~~~~~~~
29700      with caret==start at the @ token, finishing at the close paren.  */
29701   location_t combined_loc
29702     = make_location (loc, loc,
29703                      cp_lexer_previous_token (parser->lexer)->location);
29704   tree result = objc_build_selector_expr (combined_loc, sel_seq);
29705   /* TODO: objc_build_selector_expr doesn't always honor the location.  */
29706   protected_set_expr_location (result, combined_loc);
29707   return result;
29708 }
29709
29710 /* Parse a list of identifiers.
29711
29712    objc-identifier-list:
29713      identifier
29714      objc-identifier-list , identifier
29715
29716    Returns a TREE_LIST of identifier nodes.  */
29717
29718 static tree
29719 cp_parser_objc_identifier_list (cp_parser* parser)
29720 {
29721   tree identifier;
29722   tree list;
29723   cp_token *sep;
29724
29725   identifier = cp_parser_identifier (parser);
29726   if (identifier == error_mark_node)
29727     return error_mark_node;      
29728
29729   list = build_tree_list (NULL_TREE, identifier);
29730   sep = cp_lexer_peek_token (parser->lexer);
29731
29732   while (sep->type == CPP_COMMA)
29733     {
29734       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
29735       identifier = cp_parser_identifier (parser);
29736       if (identifier == error_mark_node)
29737         return list;
29738
29739       list = chainon (list, build_tree_list (NULL_TREE,
29740                                              identifier));
29741       sep = cp_lexer_peek_token (parser->lexer);
29742     }
29743   
29744   return list;
29745 }
29746
29747 /* Parse an Objective-C alias declaration.
29748
29749    objc-alias-declaration:
29750      @compatibility_alias identifier identifier ;
29751
29752    This function registers the alias mapping with the Objective-C front end.
29753    It returns nothing.  */
29754
29755 static void
29756 cp_parser_objc_alias_declaration (cp_parser* parser)
29757 {
29758   tree alias, orig;
29759
29760   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
29761   alias = cp_parser_identifier (parser);
29762   orig = cp_parser_identifier (parser);
29763   objc_declare_alias (alias, orig);
29764   cp_parser_consume_semicolon_at_end_of_statement (parser);
29765 }
29766
29767 /* Parse an Objective-C class forward-declaration.
29768
29769    objc-class-declaration:
29770      @class objc-identifier-list ;
29771
29772    The function registers the forward declarations with the Objective-C
29773    front end.  It returns nothing.  */
29774
29775 static void
29776 cp_parser_objc_class_declaration (cp_parser* parser)
29777 {
29778   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
29779   while (true)
29780     {
29781       tree id;
29782       
29783       id = cp_parser_identifier (parser);
29784       if (id == error_mark_node)
29785         break;
29786       
29787       objc_declare_class (id);
29788
29789       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29790         cp_lexer_consume_token (parser->lexer);
29791       else
29792         break;
29793     }
29794   cp_parser_consume_semicolon_at_end_of_statement (parser);
29795 }
29796
29797 /* Parse a list of Objective-C protocol references.
29798
29799    objc-protocol-refs-opt:
29800      objc-protocol-refs [opt]
29801
29802    objc-protocol-refs:
29803      < objc-identifier-list >
29804
29805    Returns a TREE_LIST of identifiers, if any.  */
29806
29807 static tree
29808 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29809 {
29810   tree protorefs = NULL_TREE;
29811
29812   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29813     {
29814       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
29815       protorefs = cp_parser_objc_identifier_list (parser);
29816       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29817     }
29818
29819   return protorefs;
29820 }
29821
29822 /* Parse a Objective-C visibility specification.  */
29823
29824 static void
29825 cp_parser_objc_visibility_spec (cp_parser* parser)
29826 {
29827   cp_token *vis = cp_lexer_peek_token (parser->lexer);
29828
29829   switch (vis->keyword)
29830     {
29831     case RID_AT_PRIVATE:
29832       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29833       break;
29834     case RID_AT_PROTECTED:
29835       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29836       break;
29837     case RID_AT_PUBLIC:
29838       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29839       break;
29840     case RID_AT_PACKAGE:
29841       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29842       break;
29843     default:
29844       return;
29845     }
29846
29847   /* Eat '@private'/'@protected'/'@public'.  */
29848   cp_lexer_consume_token (parser->lexer);
29849 }
29850
29851 /* Parse an Objective-C method type.  Return 'true' if it is a class
29852    (+) method, and 'false' if it is an instance (-) method.  */
29853
29854 static inline bool
29855 cp_parser_objc_method_type (cp_parser* parser)
29856 {
29857   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29858     return true;
29859   else
29860     return false;
29861 }
29862
29863 /* Parse an Objective-C protocol qualifier.  */
29864
29865 static tree
29866 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29867 {
29868   tree quals = NULL_TREE, node;
29869   cp_token *token = cp_lexer_peek_token (parser->lexer);
29870
29871   node = token->u.value;
29872
29873   while (node && identifier_p (node)
29874          && (node == ridpointers [(int) RID_IN]
29875              || node == ridpointers [(int) RID_OUT]
29876              || node == ridpointers [(int) RID_INOUT]
29877              || node == ridpointers [(int) RID_BYCOPY]
29878              || node == ridpointers [(int) RID_BYREF]
29879              || node == ridpointers [(int) RID_ONEWAY]))
29880     {
29881       quals = tree_cons (NULL_TREE, node, quals);
29882       cp_lexer_consume_token (parser->lexer);
29883       token = cp_lexer_peek_token (parser->lexer);
29884       node = token->u.value;
29885     }
29886
29887   return quals;
29888 }
29889
29890 /* Parse an Objective-C typename.  */
29891
29892 static tree
29893 cp_parser_objc_typename (cp_parser* parser)
29894 {
29895   tree type_name = NULL_TREE;
29896
29897   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29898     {
29899       tree proto_quals, cp_type = NULL_TREE;
29900
29901       matching_parens parens;
29902       parens.consume_open (parser); /* Eat '('.  */
29903       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29904
29905       /* An ObjC type name may consist of just protocol qualifiers, in which
29906          case the type shall default to 'id'.  */
29907       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29908         {
29909           cp_type = cp_parser_type_id (parser);
29910           
29911           /* If the type could not be parsed, an error has already
29912              been produced.  For error recovery, behave as if it had
29913              not been specified, which will use the default type
29914              'id'.  */
29915           if (cp_type == error_mark_node)
29916             {
29917               cp_type = NULL_TREE;
29918               /* We need to skip to the closing parenthesis as
29919                  cp_parser_type_id() does not seem to do it for
29920                  us.  */
29921               cp_parser_skip_to_closing_parenthesis (parser,
29922                                                      /*recovering=*/true,
29923                                                      /*or_comma=*/false,
29924                                                      /*consume_paren=*/false);
29925             }
29926         }
29927
29928       parens.require_close (parser);
29929       type_name = build_tree_list (proto_quals, cp_type);
29930     }
29931
29932   return type_name;
29933 }
29934
29935 /* Check to see if TYPE refers to an Objective-C selector name.  */
29936
29937 static bool
29938 cp_parser_objc_selector_p (enum cpp_ttype type)
29939 {
29940   return (type == CPP_NAME || type == CPP_KEYWORD
29941           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29942           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29943           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29944           || type == CPP_XOR || type == CPP_XOR_EQ);
29945 }
29946
29947 /* Parse an Objective-C selector.  */
29948
29949 static tree
29950 cp_parser_objc_selector (cp_parser* parser)
29951 {
29952   cp_token *token = cp_lexer_consume_token (parser->lexer);
29953
29954   if (!cp_parser_objc_selector_p (token->type))
29955     {
29956       error_at (token->location, "invalid Objective-C++ selector name");
29957       return error_mark_node;
29958     }
29959
29960   /* C++ operator names are allowed to appear in ObjC selectors.  */
29961   switch (token->type)
29962     {
29963     case CPP_AND_AND: return get_identifier ("and");
29964     case CPP_AND_EQ: return get_identifier ("and_eq");
29965     case CPP_AND: return get_identifier ("bitand");
29966     case CPP_OR: return get_identifier ("bitor");
29967     case CPP_COMPL: return get_identifier ("compl");
29968     case CPP_NOT: return get_identifier ("not");
29969     case CPP_NOT_EQ: return get_identifier ("not_eq");
29970     case CPP_OR_OR: return get_identifier ("or");
29971     case CPP_OR_EQ: return get_identifier ("or_eq");
29972     case CPP_XOR: return get_identifier ("xor");
29973     case CPP_XOR_EQ: return get_identifier ("xor_eq");
29974     default: return token->u.value;
29975     }
29976 }
29977
29978 /* Parse an Objective-C params list.  */
29979
29980 static tree
29981 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29982 {
29983   tree params = NULL_TREE;
29984   bool maybe_unary_selector_p = true;
29985   cp_token *token = cp_lexer_peek_token (parser->lexer);
29986
29987   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29988     {
29989       tree selector = NULL_TREE, type_name, identifier;
29990       tree parm_attr = NULL_TREE;
29991
29992       if (token->keyword == RID_ATTRIBUTE)
29993         break;
29994
29995       if (token->type != CPP_COLON)
29996         selector = cp_parser_objc_selector (parser);
29997
29998       /* Detect if we have a unary selector.  */
29999       if (maybe_unary_selector_p
30000           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30001         {
30002           params = selector; /* Might be followed by attributes.  */
30003           break;
30004         }
30005
30006       maybe_unary_selector_p = false;
30007       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30008         {
30009           /* Something went quite wrong.  There should be a colon
30010              here, but there is not.  Stop parsing parameters.  */
30011           break;
30012         }
30013       type_name = cp_parser_objc_typename (parser);
30014       /* New ObjC allows attributes on parameters too.  */
30015       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30016         parm_attr = cp_parser_attributes_opt (parser);
30017       identifier = cp_parser_identifier (parser);
30018
30019       params
30020         = chainon (params,
30021                    objc_build_keyword_decl (selector,
30022                                             type_name,
30023                                             identifier,
30024                                             parm_attr));
30025
30026       token = cp_lexer_peek_token (parser->lexer);
30027     }
30028
30029   if (params == NULL_TREE)
30030     {
30031       cp_parser_error (parser, "objective-c++ method declaration is expected");
30032       return error_mark_node;
30033     }
30034
30035   /* We allow tail attributes for the method.  */
30036   if (token->keyword == RID_ATTRIBUTE)
30037     {
30038       *attributes = cp_parser_attributes_opt (parser);
30039       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30040           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30041         return params;
30042       cp_parser_error (parser, 
30043                        "method attributes must be specified at the end");
30044       return error_mark_node;
30045     }
30046
30047   if (params == NULL_TREE)
30048     {
30049       cp_parser_error (parser, "objective-c++ method declaration is expected");
30050       return error_mark_node;
30051     }
30052   return params;
30053 }
30054
30055 /* Parse the non-keyword Objective-C params.  */
30056
30057 static tree
30058 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
30059                                        tree* attributes)
30060 {
30061   tree params = make_node (TREE_LIST);
30062   cp_token *token = cp_lexer_peek_token (parser->lexer);
30063   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
30064
30065   while (token->type == CPP_COMMA)
30066     {
30067       cp_parameter_declarator *parmdecl;
30068       tree parm;
30069
30070       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
30071       token = cp_lexer_peek_token (parser->lexer);
30072
30073       if (token->type == CPP_ELLIPSIS)
30074         {
30075           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
30076           *ellipsisp = true;
30077           token = cp_lexer_peek_token (parser->lexer);
30078           break;
30079         }
30080
30081       /* TODO: parse attributes for tail parameters.  */
30082       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30083       parm = grokdeclarator (parmdecl->declarator,
30084                              &parmdecl->decl_specifiers,
30085                              PARM, /*initialized=*/0,
30086                              /*attrlist=*/NULL);
30087
30088       chainon (params, build_tree_list (NULL_TREE, parm));
30089       token = cp_lexer_peek_token (parser->lexer);
30090     }
30091
30092   /* We allow tail attributes for the method.  */
30093   if (token->keyword == RID_ATTRIBUTE)
30094     {
30095       if (*attributes == NULL_TREE)
30096         {
30097           *attributes = cp_parser_attributes_opt (parser);
30098           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30099               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30100             return params;
30101         }
30102       else        
30103         /* We have an error, but parse the attributes, so that we can 
30104            carry on.  */
30105         *attributes = cp_parser_attributes_opt (parser);
30106
30107       cp_parser_error (parser, 
30108                        "method attributes must be specified at the end");
30109       return error_mark_node;
30110     }
30111
30112   return params;
30113 }
30114
30115 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
30116
30117 static void
30118 cp_parser_objc_interstitial_code (cp_parser* parser)
30119 {
30120   cp_token *token = cp_lexer_peek_token (parser->lexer);
30121
30122   /* If the next token is `extern' and the following token is a string
30123      literal, then we have a linkage specification.  */
30124   if (token->keyword == RID_EXTERN
30125       && cp_parser_is_pure_string_literal
30126          (cp_lexer_peek_nth_token (parser->lexer, 2)))
30127     cp_parser_linkage_specification (parser);
30128   /* Handle #pragma, if any.  */
30129   else if (token->type == CPP_PRAGMA)
30130     cp_parser_pragma (parser, pragma_objc_icode, NULL);
30131   /* Allow stray semicolons.  */
30132   else if (token->type == CPP_SEMICOLON)
30133     cp_lexer_consume_token (parser->lexer);
30134   /* Mark methods as optional or required, when building protocols.  */
30135   else if (token->keyword == RID_AT_OPTIONAL)
30136     {
30137       cp_lexer_consume_token (parser->lexer);
30138       objc_set_method_opt (true);
30139     }
30140   else if (token->keyword == RID_AT_REQUIRED)
30141     {
30142       cp_lexer_consume_token (parser->lexer);
30143       objc_set_method_opt (false);
30144     }
30145   else if (token->keyword == RID_NAMESPACE)
30146     cp_parser_namespace_definition (parser);
30147   /* Other stray characters must generate errors.  */
30148   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30149     {
30150       cp_lexer_consume_token (parser->lexer);
30151       error ("stray %qs between Objective-C++ methods",
30152              token->type == CPP_OPEN_BRACE ? "{" : "}");
30153     }
30154   /* Finally, try to parse a block-declaration, or a function-definition.  */
30155   else
30156     cp_parser_block_declaration (parser, /*statement_p=*/false);
30157 }
30158
30159 /* Parse a method signature.  */
30160
30161 static tree
30162 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30163 {
30164   tree rettype, kwdparms, optparms;
30165   bool ellipsis = false;
30166   bool is_class_method;
30167
30168   is_class_method = cp_parser_objc_method_type (parser);
30169   rettype = cp_parser_objc_typename (parser);
30170   *attributes = NULL_TREE;
30171   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30172   if (kwdparms == error_mark_node)
30173     return error_mark_node;
30174   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30175   if (optparms == error_mark_node)
30176     return error_mark_node;
30177
30178   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30179 }
30180
30181 static bool
30182 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30183 {
30184   tree tattr;  
30185   cp_lexer_save_tokens (parser->lexer);
30186   tattr = cp_parser_attributes_opt (parser);
30187   gcc_assert (tattr) ;
30188   
30189   /* If the attributes are followed by a method introducer, this is not allowed.
30190      Dump the attributes and flag the situation.  */
30191   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30192       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30193     return true;
30194
30195   /* Otherwise, the attributes introduce some interstitial code, possibly so
30196      rewind to allow that check.  */
30197   cp_lexer_rollback_tokens (parser->lexer);
30198   return false;  
30199 }
30200
30201 /* Parse an Objective-C method prototype list.  */
30202
30203 static void
30204 cp_parser_objc_method_prototype_list (cp_parser* parser)
30205 {
30206   cp_token *token = cp_lexer_peek_token (parser->lexer);
30207
30208   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30209     {
30210       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30211         {
30212           tree attributes, sig;
30213           bool is_class_method;
30214           if (token->type == CPP_PLUS)
30215             is_class_method = true;
30216           else
30217             is_class_method = false;
30218           sig = cp_parser_objc_method_signature (parser, &attributes);
30219           if (sig == error_mark_node)
30220             {
30221               cp_parser_skip_to_end_of_block_or_statement (parser);
30222               token = cp_lexer_peek_token (parser->lexer);
30223               continue;
30224             }
30225           objc_add_method_declaration (is_class_method, sig, attributes);
30226           cp_parser_consume_semicolon_at_end_of_statement (parser);
30227         }
30228       else if (token->keyword == RID_AT_PROPERTY)
30229         cp_parser_objc_at_property_declaration (parser);
30230       else if (token->keyword == RID_ATTRIBUTE 
30231                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30232         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
30233                     OPT_Wattributes, 
30234                     "prefix attributes are ignored for methods");
30235       else
30236         /* Allow for interspersed non-ObjC++ code.  */
30237         cp_parser_objc_interstitial_code (parser);
30238
30239       token = cp_lexer_peek_token (parser->lexer);
30240     }
30241
30242   if (token->type != CPP_EOF)
30243     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
30244   else
30245     cp_parser_error (parser, "expected %<@end%>");
30246
30247   objc_finish_interface ();
30248 }
30249
30250 /* Parse an Objective-C method definition list.  */
30251
30252 static void
30253 cp_parser_objc_method_definition_list (cp_parser* parser)
30254 {
30255   cp_token *token = cp_lexer_peek_token (parser->lexer);
30256
30257   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30258     {
30259       tree meth;
30260
30261       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30262         {
30263           cp_token *ptk;
30264           tree sig, attribute;
30265           bool is_class_method;
30266           if (token->type == CPP_PLUS)
30267             is_class_method = true;
30268           else
30269             is_class_method = false;
30270           push_deferring_access_checks (dk_deferred);
30271           sig = cp_parser_objc_method_signature (parser, &attribute);
30272           if (sig == error_mark_node)
30273             {
30274               cp_parser_skip_to_end_of_block_or_statement (parser);
30275               token = cp_lexer_peek_token (parser->lexer);
30276               continue;
30277             }
30278           objc_start_method_definition (is_class_method, sig, attribute,
30279                                         NULL_TREE);
30280
30281           /* For historical reasons, we accept an optional semicolon.  */
30282           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30283             cp_lexer_consume_token (parser->lexer);
30284
30285           ptk = cp_lexer_peek_token (parser->lexer);
30286           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
30287                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30288             {
30289               perform_deferred_access_checks (tf_warning_or_error);
30290               stop_deferring_access_checks ();
30291               meth = cp_parser_function_definition_after_declarator (parser,
30292                                                                      false);
30293               pop_deferring_access_checks ();
30294               objc_finish_method_definition (meth);
30295             }
30296         }
30297       /* The following case will be removed once @synthesize is
30298          completely implemented.  */
30299       else if (token->keyword == RID_AT_PROPERTY)
30300         cp_parser_objc_at_property_declaration (parser);
30301       else if (token->keyword == RID_AT_SYNTHESIZE)
30302         cp_parser_objc_at_synthesize_declaration (parser);
30303       else if (token->keyword == RID_AT_DYNAMIC)
30304         cp_parser_objc_at_dynamic_declaration (parser);
30305       else if (token->keyword == RID_ATTRIBUTE 
30306                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30307         warning_at (token->location, OPT_Wattributes,
30308                     "prefix attributes are ignored for methods");
30309       else
30310         /* Allow for interspersed non-ObjC++ code.  */
30311         cp_parser_objc_interstitial_code (parser);
30312
30313       token = cp_lexer_peek_token (parser->lexer);
30314     }
30315
30316   if (token->type != CPP_EOF)
30317     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
30318   else
30319     cp_parser_error (parser, "expected %<@end%>");
30320
30321   objc_finish_implementation ();
30322 }
30323
30324 /* Parse Objective-C ivars.  */
30325
30326 static void
30327 cp_parser_objc_class_ivars (cp_parser* parser)
30328 {
30329   cp_token *token = cp_lexer_peek_token (parser->lexer);
30330
30331   if (token->type != CPP_OPEN_BRACE)
30332     return;     /* No ivars specified.  */
30333
30334   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
30335   token = cp_lexer_peek_token (parser->lexer);
30336
30337   while (token->type != CPP_CLOSE_BRACE 
30338         && token->keyword != RID_AT_END && token->type != CPP_EOF)
30339     {
30340       cp_decl_specifier_seq declspecs;
30341       int decl_class_or_enum_p;
30342       tree prefix_attributes;
30343
30344       cp_parser_objc_visibility_spec (parser);
30345
30346       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30347         break;
30348
30349       cp_parser_decl_specifier_seq (parser,
30350                                     CP_PARSER_FLAGS_OPTIONAL,
30351                                     &declspecs,
30352                                     &decl_class_or_enum_p);
30353
30354       /* auto, register, static, extern, mutable.  */
30355       if (declspecs.storage_class != sc_none)
30356         {
30357           cp_parser_error (parser, "invalid type for instance variable");         
30358           declspecs.storage_class = sc_none;
30359         }
30360
30361       /* thread_local.  */
30362       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30363         {
30364           cp_parser_error (parser, "invalid type for instance variable");
30365           declspecs.locations[ds_thread] = 0;
30366         }
30367       
30368       /* typedef.  */
30369       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30370         {
30371           cp_parser_error (parser, "invalid type for instance variable");
30372           declspecs.locations[ds_typedef] = 0;
30373         }
30374
30375       prefix_attributes = declspecs.attributes;
30376       declspecs.attributes = NULL_TREE;
30377
30378       /* Keep going until we hit the `;' at the end of the
30379          declaration.  */
30380       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30381         {
30382           tree width = NULL_TREE, attributes, first_attribute, decl;
30383           cp_declarator *declarator = NULL;
30384           int ctor_dtor_or_conv_p;
30385
30386           /* Check for a (possibly unnamed) bitfield declaration.  */
30387           token = cp_lexer_peek_token (parser->lexer);
30388           if (token->type == CPP_COLON)
30389             goto eat_colon;
30390
30391           if (token->type == CPP_NAME
30392               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30393                   == CPP_COLON))
30394             {
30395               /* Get the name of the bitfield.  */
30396               declarator = make_id_declarator (NULL_TREE,
30397                                                cp_parser_identifier (parser),
30398                                                sfk_none);
30399
30400              eat_colon:
30401               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
30402               /* Get the width of the bitfield.  */
30403               width
30404                 = cp_parser_constant_expression (parser);
30405             }
30406           else
30407             {
30408               /* Parse the declarator.  */
30409               declarator
30410                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30411                                         &ctor_dtor_or_conv_p,
30412                                         /*parenthesized_p=*/NULL,
30413                                         /*member_p=*/false,
30414                                         /*friend_p=*/false);
30415             }
30416
30417           /* Look for attributes that apply to the ivar.  */
30418           attributes = cp_parser_attributes_opt (parser);
30419           /* Remember which attributes are prefix attributes and
30420              which are not.  */
30421           first_attribute = attributes;
30422           /* Combine the attributes.  */
30423           attributes = attr_chainon (prefix_attributes, attributes);
30424
30425           if (width)
30426             /* Create the bitfield declaration.  */
30427             decl = grokbitfield (declarator, &declspecs,
30428                                  width, NULL_TREE, attributes);
30429           else
30430             decl = grokfield (declarator, &declspecs,
30431                               NULL_TREE, /*init_const_expr_p=*/false,
30432                               NULL_TREE, attributes);
30433
30434           /* Add the instance variable.  */
30435           if (decl != error_mark_node && decl != NULL_TREE)
30436             objc_add_instance_variable (decl);
30437
30438           /* Reset PREFIX_ATTRIBUTES.  */
30439           if (attributes != error_mark_node)
30440             {
30441               while (attributes && TREE_CHAIN (attributes) != first_attribute)
30442                 attributes = TREE_CHAIN (attributes);
30443               if (attributes)
30444                 TREE_CHAIN (attributes) = NULL_TREE;
30445             }
30446
30447           token = cp_lexer_peek_token (parser->lexer);
30448
30449           if (token->type == CPP_COMMA)
30450             {
30451               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
30452               continue;
30453             }
30454           break;
30455         }
30456
30457       cp_parser_consume_semicolon_at_end_of_statement (parser);
30458       token = cp_lexer_peek_token (parser->lexer);
30459     }
30460
30461   if (token->keyword == RID_AT_END)
30462     cp_parser_error (parser, "expected %<}%>");
30463
30464   /* Do not consume the RID_AT_END, so it will be read again as terminating
30465      the @interface of @implementation.  */ 
30466   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30467     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
30468     
30469   /* For historical reasons, we accept an optional semicolon.  */
30470   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30471     cp_lexer_consume_token (parser->lexer);
30472 }
30473
30474 /* Parse an Objective-C protocol declaration.  */
30475
30476 static void
30477 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30478 {
30479   tree proto, protorefs;
30480   cp_token *tok;
30481
30482   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
30483   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30484     {
30485       tok = cp_lexer_peek_token (parser->lexer);
30486       error_at (tok->location, "identifier expected after %<@protocol%>");
30487       cp_parser_consume_semicolon_at_end_of_statement (parser);
30488       return;
30489     }
30490
30491   /* See if we have a forward declaration or a definition.  */
30492   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30493
30494   /* Try a forward declaration first.  */
30495   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30496     {
30497       while (true)
30498         {
30499           tree id;
30500           
30501           id = cp_parser_identifier (parser);
30502           if (id == error_mark_node)
30503             break;
30504           
30505           objc_declare_protocol (id, attributes);
30506           
30507           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30508             cp_lexer_consume_token (parser->lexer);
30509           else
30510             break;
30511         }
30512       cp_parser_consume_semicolon_at_end_of_statement (parser);
30513     }
30514
30515   /* Ok, we got a full-fledged definition (or at least should).  */
30516   else
30517     {
30518       proto = cp_parser_identifier (parser);
30519       protorefs = cp_parser_objc_protocol_refs_opt (parser);
30520       objc_start_protocol (proto, protorefs, attributes);
30521       cp_parser_objc_method_prototype_list (parser);
30522     }
30523 }
30524
30525 /* Parse an Objective-C superclass or category.  */
30526
30527 static void
30528 cp_parser_objc_superclass_or_category (cp_parser *parser, 
30529                                        bool iface_p,
30530                                        tree *super,
30531                                        tree *categ, bool *is_class_extension)
30532 {
30533   cp_token *next = cp_lexer_peek_token (parser->lexer);
30534
30535   *super = *categ = NULL_TREE;
30536   *is_class_extension = false;
30537   if (next->type == CPP_COLON)
30538     {
30539       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
30540       *super = cp_parser_identifier (parser);
30541     }
30542   else if (next->type == CPP_OPEN_PAREN)
30543     {
30544       matching_parens parens;
30545       parens.consume_open (parser);  /* Eat '('.  */
30546
30547       /* If there is no category name, and this is an @interface, we
30548          have a class extension.  */
30549       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30550         {
30551           *categ = NULL_TREE;
30552           *is_class_extension = true;
30553         }
30554       else
30555         *categ = cp_parser_identifier (parser);
30556
30557       parens.require_close (parser);
30558     }
30559 }
30560
30561 /* Parse an Objective-C class interface.  */
30562
30563 static void
30564 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30565 {
30566   tree name, super, categ, protos;
30567   bool is_class_extension;
30568
30569   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
30570   name = cp_parser_identifier (parser);
30571   if (name == error_mark_node)
30572     {
30573       /* It's hard to recover because even if valid @interface stuff
30574          is to follow, we can't compile it (or validate it) if we
30575          don't even know which class it refers to.  Let's assume this
30576          was a stray '@interface' token in the stream and skip it.
30577       */
30578       return;
30579     }
30580   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30581                                          &is_class_extension);
30582   protos = cp_parser_objc_protocol_refs_opt (parser);
30583
30584   /* We have either a class or a category on our hands.  */
30585   if (categ || is_class_extension)
30586     objc_start_category_interface (name, categ, protos, attributes);
30587   else
30588     {
30589       objc_start_class_interface (name, super, protos, attributes);
30590       /* Handle instance variable declarations, if any.  */
30591       cp_parser_objc_class_ivars (parser);
30592       objc_continue_interface ();
30593     }
30594
30595   cp_parser_objc_method_prototype_list (parser);
30596 }
30597
30598 /* Parse an Objective-C class implementation.  */
30599
30600 static void
30601 cp_parser_objc_class_implementation (cp_parser* parser)
30602 {
30603   tree name, super, categ;
30604   bool is_class_extension;
30605
30606   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
30607   name = cp_parser_identifier (parser);
30608   if (name == error_mark_node)
30609     {
30610       /* It's hard to recover because even if valid @implementation
30611          stuff is to follow, we can't compile it (or validate it) if
30612          we don't even know which class it refers to.  Let's assume
30613          this was a stray '@implementation' token in the stream and
30614          skip it.
30615       */
30616       return;
30617     }
30618   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30619                                          &is_class_extension);
30620
30621   /* We have either a class or a category on our hands.  */
30622   if (categ)
30623     objc_start_category_implementation (name, categ);
30624   else
30625     {
30626       objc_start_class_implementation (name, super);
30627       /* Handle instance variable declarations, if any.  */
30628       cp_parser_objc_class_ivars (parser);
30629       objc_continue_implementation ();
30630     }
30631
30632   cp_parser_objc_method_definition_list (parser);
30633 }
30634
30635 /* Consume the @end token and finish off the implementation.  */
30636
30637 static void
30638 cp_parser_objc_end_implementation (cp_parser* parser)
30639 {
30640   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
30641   objc_finish_implementation ();
30642 }
30643
30644 /* Parse an Objective-C declaration.  */
30645
30646 static void
30647 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30648 {
30649   /* Try to figure out what kind of declaration is present.  */
30650   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30651
30652   if (attributes)
30653     switch (kwd->keyword)
30654       {
30655         case RID_AT_ALIAS:
30656         case RID_AT_CLASS:
30657         case RID_AT_END:
30658           error_at (kwd->location, "attributes may not be specified before"
30659                     " the %<@%D%> Objective-C++ keyword",
30660                     kwd->u.value);
30661           attributes = NULL;
30662           break;
30663         case RID_AT_IMPLEMENTATION:
30664           warning_at (kwd->location, OPT_Wattributes,
30665                       "prefix attributes are ignored before %<@%D%>",
30666                       kwd->u.value);
30667           attributes = NULL;
30668         default:
30669           break;
30670       }
30671
30672   switch (kwd->keyword)
30673     {
30674     case RID_AT_ALIAS:
30675       cp_parser_objc_alias_declaration (parser);
30676       break;
30677     case RID_AT_CLASS:
30678       cp_parser_objc_class_declaration (parser);
30679       break;
30680     case RID_AT_PROTOCOL:
30681       cp_parser_objc_protocol_declaration (parser, attributes);
30682       break;
30683     case RID_AT_INTERFACE:
30684       cp_parser_objc_class_interface (parser, attributes);
30685       break;
30686     case RID_AT_IMPLEMENTATION:
30687       cp_parser_objc_class_implementation (parser);
30688       break;
30689     case RID_AT_END:
30690       cp_parser_objc_end_implementation (parser);
30691       break;
30692     default:
30693       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30694                 kwd->u.value);
30695       cp_parser_skip_to_end_of_block_or_statement (parser);
30696     }
30697 }
30698
30699 /* Parse an Objective-C try-catch-finally statement.
30700
30701    objc-try-catch-finally-stmt:
30702      @try compound-statement objc-catch-clause-seq [opt]
30703        objc-finally-clause [opt]
30704
30705    objc-catch-clause-seq:
30706      objc-catch-clause objc-catch-clause-seq [opt]
30707
30708    objc-catch-clause:
30709      @catch ( objc-exception-declaration ) compound-statement
30710
30711    objc-finally-clause:
30712      @finally compound-statement
30713
30714    objc-exception-declaration:
30715      parameter-declaration
30716      '...'
30717
30718    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30719
30720    Returns NULL_TREE.
30721
30722    PS: This function is identical to c_parser_objc_try_catch_finally_statement
30723    for C.  Keep them in sync.  */   
30724
30725 static tree
30726 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30727 {
30728   location_t location;
30729   tree stmt;
30730
30731   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30732   location = cp_lexer_peek_token (parser->lexer)->location;
30733   objc_maybe_warn_exceptions (location);
30734   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30735      node, lest it get absorbed into the surrounding block.  */
30736   stmt = push_stmt_list ();
30737   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30738   objc_begin_try_stmt (location, pop_stmt_list (stmt));
30739
30740   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30741     {
30742       cp_parameter_declarator *parm;
30743       tree parameter_declaration = error_mark_node;
30744       bool seen_open_paren = false;
30745       matching_parens parens;
30746
30747       cp_lexer_consume_token (parser->lexer);
30748       if (parens.require_open (parser))
30749         seen_open_paren = true;
30750       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30751         {
30752           /* We have "@catch (...)" (where the '...' are literally
30753              what is in the code).  Skip the '...'.
30754              parameter_declaration is set to NULL_TREE, and
30755              objc_being_catch_clauses() knows that that means
30756              '...'.  */
30757           cp_lexer_consume_token (parser->lexer);
30758           parameter_declaration = NULL_TREE;
30759         }
30760       else
30761         {
30762           /* We have "@catch (NSException *exception)" or something
30763              like that.  Parse the parameter declaration.  */
30764           parm = cp_parser_parameter_declaration (parser, false, NULL);
30765           if (parm == NULL)
30766             parameter_declaration = error_mark_node;
30767           else
30768             parameter_declaration = grokdeclarator (parm->declarator,
30769                                                     &parm->decl_specifiers,
30770                                                     PARM, /*initialized=*/0,
30771                                                     /*attrlist=*/NULL);
30772         }
30773       if (seen_open_paren)
30774         parens.require_close (parser);
30775       else
30776         {
30777           /* If there was no open parenthesis, we are recovering from
30778              an error, and we are trying to figure out what mistake
30779              the user has made.  */
30780
30781           /* If there is an immediate closing parenthesis, the user
30782              probably forgot the opening one (ie, they typed "@catch
30783              NSException *e)".  Parse the closing parenthesis and keep
30784              going.  */
30785           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30786             cp_lexer_consume_token (parser->lexer);
30787           
30788           /* If these is no immediate closing parenthesis, the user
30789              probably doesn't know that parenthesis are required at
30790              all (ie, they typed "@catch NSException *e").  So, just
30791              forget about the closing parenthesis and keep going.  */
30792         }
30793       objc_begin_catch_clause (parameter_declaration);
30794       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30795       objc_finish_catch_clause ();
30796     }
30797   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30798     {
30799       cp_lexer_consume_token (parser->lexer);
30800       location = cp_lexer_peek_token (parser->lexer)->location;
30801       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30802          node, lest it get absorbed into the surrounding block.  */
30803       stmt = push_stmt_list ();
30804       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30805       objc_build_finally_clause (location, pop_stmt_list (stmt));
30806     }
30807
30808   return objc_finish_try_stmt ();
30809 }
30810
30811 /* Parse an Objective-C synchronized statement.
30812
30813    objc-synchronized-stmt:
30814      @synchronized ( expression ) compound-statement
30815
30816    Returns NULL_TREE.  */
30817
30818 static tree
30819 cp_parser_objc_synchronized_statement (cp_parser *parser)
30820 {
30821   location_t location;
30822   tree lock, stmt;
30823
30824   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30825
30826   location = cp_lexer_peek_token (parser->lexer)->location;
30827   objc_maybe_warn_exceptions (location);
30828   matching_parens parens;
30829   parens.require_open (parser);
30830   lock = cp_parser_expression (parser);
30831   parens.require_close (parser);
30832
30833   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30834      node, lest it get absorbed into the surrounding block.  */
30835   stmt = push_stmt_list ();
30836   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30837
30838   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30839 }
30840
30841 /* Parse an Objective-C throw statement.
30842
30843    objc-throw-stmt:
30844      @throw assignment-expression [opt] ;
30845
30846    Returns a constructed '@throw' statement.  */
30847
30848 static tree
30849 cp_parser_objc_throw_statement (cp_parser *parser)
30850 {
30851   tree expr = NULL_TREE;
30852   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30853
30854   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30855
30856   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30857     expr = cp_parser_expression (parser);
30858
30859   cp_parser_consume_semicolon_at_end_of_statement (parser);
30860
30861   return objc_build_throw_stmt (loc, expr);
30862 }
30863
30864 /* Parse an Objective-C statement.  */
30865
30866 static tree
30867 cp_parser_objc_statement (cp_parser * parser)
30868 {
30869   /* Try to figure out what kind of declaration is present.  */
30870   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30871
30872   switch (kwd->keyword)
30873     {
30874     case RID_AT_TRY:
30875       return cp_parser_objc_try_catch_finally_statement (parser);
30876     case RID_AT_SYNCHRONIZED:
30877       return cp_parser_objc_synchronized_statement (parser);
30878     case RID_AT_THROW:
30879       return cp_parser_objc_throw_statement (parser);
30880     default:
30881       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30882                kwd->u.value);
30883       cp_parser_skip_to_end_of_block_or_statement (parser);
30884     }
30885
30886   return error_mark_node;
30887 }
30888
30889 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
30890    look ahead to see if an objc keyword follows the attributes.  This
30891    is to detect the use of prefix attributes on ObjC @interface and 
30892    @protocol.  */
30893
30894 static bool
30895 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30896 {
30897   cp_lexer_save_tokens (parser->lexer);
30898   *attrib = cp_parser_attributes_opt (parser);
30899   gcc_assert (*attrib);
30900   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30901     {
30902       cp_lexer_commit_tokens (parser->lexer);
30903       return true;
30904     }
30905   cp_lexer_rollback_tokens (parser->lexer);
30906   return false;  
30907 }
30908
30909 /* This routine is a minimal replacement for
30910    c_parser_struct_declaration () used when parsing the list of
30911    types/names or ObjC++ properties.  For example, when parsing the
30912    code
30913
30914    @property (readonly) int a, b, c;
30915
30916    this function is responsible for parsing "int a, int b, int c" and
30917    returning the declarations as CHAIN of DECLs.
30918
30919    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
30920    similar parsing.  */
30921 static tree
30922 cp_parser_objc_struct_declaration (cp_parser *parser)
30923 {
30924   tree decls = NULL_TREE;
30925   cp_decl_specifier_seq declspecs;
30926   int decl_class_or_enum_p;
30927   tree prefix_attributes;
30928
30929   cp_parser_decl_specifier_seq (parser,
30930                                 CP_PARSER_FLAGS_NONE,
30931                                 &declspecs,
30932                                 &decl_class_or_enum_p);
30933
30934   if (declspecs.type == error_mark_node)
30935     return error_mark_node;
30936
30937   /* auto, register, static, extern, mutable.  */
30938   if (declspecs.storage_class != sc_none)
30939     {
30940       cp_parser_error (parser, "invalid type for property");
30941       declspecs.storage_class = sc_none;
30942     }
30943   
30944   /* thread_local.  */
30945   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30946     {
30947       cp_parser_error (parser, "invalid type for property");
30948       declspecs.locations[ds_thread] = 0;
30949     }
30950   
30951   /* typedef.  */
30952   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30953     {
30954       cp_parser_error (parser, "invalid type for property");
30955       declspecs.locations[ds_typedef] = 0;
30956     }
30957
30958   prefix_attributes = declspecs.attributes;
30959   declspecs.attributes = NULL_TREE;
30960
30961   /* Keep going until we hit the `;' at the end of the declaration. */
30962   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30963     {
30964       tree attributes, first_attribute, decl;
30965       cp_declarator *declarator;
30966       cp_token *token;
30967
30968       /* Parse the declarator.  */
30969       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30970                                          NULL, NULL, false, false);
30971
30972       /* Look for attributes that apply to the ivar.  */
30973       attributes = cp_parser_attributes_opt (parser);
30974       /* Remember which attributes are prefix attributes and
30975          which are not.  */
30976       first_attribute = attributes;
30977       /* Combine the attributes.  */
30978       attributes = attr_chainon (prefix_attributes, attributes);
30979
30980       decl = grokfield (declarator, &declspecs,
30981                         NULL_TREE, /*init_const_expr_p=*/false,
30982                         NULL_TREE, attributes);
30983
30984       if (decl == error_mark_node || decl == NULL_TREE)
30985         return error_mark_node;
30986       
30987       /* Reset PREFIX_ATTRIBUTES.  */
30988       if (attributes != error_mark_node)
30989         {
30990           while (attributes && TREE_CHAIN (attributes) != first_attribute)
30991             attributes = TREE_CHAIN (attributes);
30992           if (attributes)
30993             TREE_CHAIN (attributes) = NULL_TREE;
30994         }
30995
30996       DECL_CHAIN (decl) = decls;
30997       decls = decl;
30998
30999       token = cp_lexer_peek_token (parser->lexer);
31000       if (token->type == CPP_COMMA)
31001         {
31002           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
31003           continue;
31004         }
31005       else
31006         break;
31007     }
31008   return decls;
31009 }
31010
31011 /* Parse an Objective-C @property declaration.  The syntax is:
31012
31013    objc-property-declaration:
31014      '@property' objc-property-attributes[opt] struct-declaration ;
31015
31016    objc-property-attributes:
31017     '(' objc-property-attribute-list ')'
31018
31019    objc-property-attribute-list:
31020      objc-property-attribute
31021      objc-property-attribute-list, objc-property-attribute
31022
31023    objc-property-attribute
31024      'getter' = identifier
31025      'setter' = identifier
31026      'readonly'
31027      'readwrite'
31028      'assign'
31029      'retain'
31030      'copy'
31031      'nonatomic'
31032
31033   For example:
31034     @property NSString *name;
31035     @property (readonly) id object;
31036     @property (retain, nonatomic, getter=getTheName) id name;
31037     @property int a, b, c;
31038
31039    PS: This function is identical to
31040    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
31041 static void 
31042 cp_parser_objc_at_property_declaration (cp_parser *parser)
31043 {
31044   /* The following variables hold the attributes of the properties as
31045      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
31046      seen.  When we see an attribute, we set them to 'true' (if they
31047      are boolean properties) or to the identifier (if they have an
31048      argument, ie, for getter and setter).  Note that here we only
31049      parse the list of attributes, check the syntax and accumulate the
31050      attributes that we find.  objc_add_property_declaration() will
31051      then process the information.  */
31052   bool property_assign = false;
31053   bool property_copy = false;
31054   tree property_getter_ident = NULL_TREE;
31055   bool property_nonatomic = false;
31056   bool property_readonly = false;
31057   bool property_readwrite = false;
31058   bool property_retain = false;
31059   tree property_setter_ident = NULL_TREE;
31060
31061   /* 'properties' is the list of properties that we read.  Usually a
31062      single one, but maybe more (eg, in "@property int a, b, c;" there
31063      are three).  */
31064   tree properties;
31065   location_t loc;
31066
31067   loc = cp_lexer_peek_token (parser->lexer)->location;
31068
31069   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
31070
31071   /* Parse the optional attribute list...  */
31072   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31073     {
31074       /* Eat the '('.  */
31075       matching_parens parens;
31076       parens.consume_open (parser);
31077
31078       while (true)
31079         {
31080           bool syntax_error = false;
31081           cp_token *token = cp_lexer_peek_token (parser->lexer);
31082           enum rid keyword;
31083
31084           if (token->type != CPP_NAME)
31085             {
31086               cp_parser_error (parser, "expected identifier");
31087               break;
31088             }
31089           keyword = C_RID_CODE (token->u.value);
31090           cp_lexer_consume_token (parser->lexer);
31091           switch (keyword)
31092             {
31093             case RID_ASSIGN:    property_assign = true;    break;
31094             case RID_COPY:      property_copy = true;      break;
31095             case RID_NONATOMIC: property_nonatomic = true; break;
31096             case RID_READONLY:  property_readonly = true;  break;
31097             case RID_READWRITE: property_readwrite = true; break;
31098             case RID_RETAIN:    property_retain = true;    break;
31099
31100             case RID_GETTER:
31101             case RID_SETTER:
31102               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31103                 {
31104                   if (keyword == RID_GETTER)
31105                     cp_parser_error (parser,
31106                                      "missing %<=%> (after %<getter%> attribute)");
31107                   else
31108                     cp_parser_error (parser,
31109                                      "missing %<=%> (after %<setter%> attribute)");
31110                   syntax_error = true;
31111                   break;
31112                 }
31113               cp_lexer_consume_token (parser->lexer); /* eat the = */
31114               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31115                 {
31116                   cp_parser_error (parser, "expected identifier");
31117                   syntax_error = true;
31118                   break;
31119                 }
31120               if (keyword == RID_SETTER)
31121                 {
31122                   if (property_setter_ident != NULL_TREE)
31123                     {
31124                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31125                       cp_lexer_consume_token (parser->lexer);
31126                     }
31127                   else
31128                     property_setter_ident = cp_parser_objc_selector (parser);
31129                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31130                     cp_parser_error (parser, "setter name must terminate with %<:%>");
31131                   else
31132                     cp_lexer_consume_token (parser->lexer);
31133                 }
31134               else
31135                 {
31136                   if (property_getter_ident != NULL_TREE)
31137                     {
31138                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31139                       cp_lexer_consume_token (parser->lexer);
31140                     }
31141                   else
31142                     property_getter_ident = cp_parser_objc_selector (parser);
31143                 }
31144               break;
31145             default:
31146               cp_parser_error (parser, "unknown property attribute");
31147               syntax_error = true;
31148               break;
31149             }
31150
31151           if (syntax_error)
31152             break;
31153
31154           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31155             cp_lexer_consume_token (parser->lexer);
31156           else
31157             break;
31158         }
31159
31160       /* FIXME: "@property (setter, assign);" will generate a spurious
31161          "error: expected â€˜)’ before â€˜,’ token".  This is because
31162          cp_parser_require, unlike the C counterpart, will produce an
31163          error even if we are in error recovery.  */
31164       if (!parens.require_close (parser))
31165         {
31166           cp_parser_skip_to_closing_parenthesis (parser,
31167                                                  /*recovering=*/true,
31168                                                  /*or_comma=*/false,
31169                                                  /*consume_paren=*/true);
31170         }
31171     }
31172
31173   /* ... and the property declaration(s).  */
31174   properties = cp_parser_objc_struct_declaration (parser);
31175
31176   if (properties == error_mark_node)
31177     {
31178       cp_parser_skip_to_end_of_statement (parser);
31179       /* If the next token is now a `;', consume it.  */
31180       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31181         cp_lexer_consume_token (parser->lexer);
31182       return;
31183     }
31184
31185   if (properties == NULL_TREE)
31186     cp_parser_error (parser, "expected identifier");
31187   else
31188     {
31189       /* Comma-separated properties are chained together in
31190          reverse order; add them one by one.  */
31191       properties = nreverse (properties);
31192       
31193       for (; properties; properties = TREE_CHAIN (properties))
31194         objc_add_property_declaration (loc, copy_node (properties),
31195                                        property_readonly, property_readwrite,
31196                                        property_assign, property_retain,
31197                                        property_copy, property_nonatomic,
31198                                        property_getter_ident, property_setter_ident);
31199     }
31200   
31201   cp_parser_consume_semicolon_at_end_of_statement (parser);
31202 }
31203
31204 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
31205
31206    objc-synthesize-declaration:
31207      @synthesize objc-synthesize-identifier-list ;
31208
31209    objc-synthesize-identifier-list:
31210      objc-synthesize-identifier
31211      objc-synthesize-identifier-list, objc-synthesize-identifier
31212
31213    objc-synthesize-identifier
31214      identifier
31215      identifier = identifier
31216
31217   For example:
31218     @synthesize MyProperty;
31219     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31220
31221   PS: This function is identical to c_parser_objc_at_synthesize_declaration
31222   for C.  Keep them in sync.
31223 */
31224 static void 
31225 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31226 {
31227   tree list = NULL_TREE;
31228   location_t loc;
31229   loc = cp_lexer_peek_token (parser->lexer)->location;
31230
31231   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
31232   while (true)
31233     {
31234       tree property, ivar;
31235       property = cp_parser_identifier (parser);
31236       if (property == error_mark_node)
31237         {
31238           cp_parser_consume_semicolon_at_end_of_statement (parser);
31239           return;
31240         }
31241       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31242         {
31243           cp_lexer_consume_token (parser->lexer);
31244           ivar = cp_parser_identifier (parser);
31245           if (ivar == error_mark_node)
31246             {
31247               cp_parser_consume_semicolon_at_end_of_statement (parser);
31248               return;
31249             }
31250         }
31251       else
31252         ivar = NULL_TREE;
31253       list = chainon (list, build_tree_list (ivar, property));
31254       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31255         cp_lexer_consume_token (parser->lexer);
31256       else
31257         break;
31258     }
31259   cp_parser_consume_semicolon_at_end_of_statement (parser);
31260   objc_add_synthesize_declaration (loc, list);
31261 }
31262
31263 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
31264
31265    objc-dynamic-declaration:
31266      @dynamic identifier-list ;
31267
31268    For example:
31269      @dynamic MyProperty;
31270      @dynamic MyProperty, AnotherProperty;
31271
31272   PS: This function is identical to c_parser_objc_at_dynamic_declaration
31273   for C.  Keep them in sync.
31274 */
31275 static void 
31276 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31277 {
31278   tree list = NULL_TREE;
31279   location_t loc;
31280   loc = cp_lexer_peek_token (parser->lexer)->location;
31281
31282   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
31283   while (true)
31284     {
31285       tree property;
31286       property = cp_parser_identifier (parser);
31287       if (property == error_mark_node)
31288         {
31289           cp_parser_consume_semicolon_at_end_of_statement (parser);
31290           return;
31291         }
31292       list = chainon (list, build_tree_list (NULL, property));
31293       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31294         cp_lexer_consume_token (parser->lexer);
31295       else
31296         break;
31297     }
31298   cp_parser_consume_semicolon_at_end_of_statement (parser);
31299   objc_add_dynamic_declaration (loc, list);
31300 }
31301
31302 \f
31303 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
31304
31305 /* Returns name of the next clause.
31306    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31307    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
31308    returned and the token is consumed.  */
31309
31310 static pragma_omp_clause
31311 cp_parser_omp_clause_name (cp_parser *parser)
31312 {
31313   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31314
31315   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31316     result = PRAGMA_OACC_CLAUSE_AUTO;
31317   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31318     result = PRAGMA_OMP_CLAUSE_IF;
31319   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31320     result = PRAGMA_OMP_CLAUSE_DEFAULT;
31321   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31322     result = PRAGMA_OACC_CLAUSE_DELETE;
31323   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31324     result = PRAGMA_OMP_CLAUSE_PRIVATE;
31325   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31326     result = PRAGMA_OMP_CLAUSE_FOR;
31327   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31328     {
31329       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31330       const char *p = IDENTIFIER_POINTER (id);
31331
31332       switch (p[0])
31333         {
31334         case 'a':
31335           if (!strcmp ("aligned", p))
31336             result = PRAGMA_OMP_CLAUSE_ALIGNED;
31337           else if (!strcmp ("async", p))
31338             result = PRAGMA_OACC_CLAUSE_ASYNC;
31339           break;
31340         case 'c':
31341           if (!strcmp ("collapse", p))
31342             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31343           else if (!strcmp ("copy", p))
31344             result = PRAGMA_OACC_CLAUSE_COPY;
31345           else if (!strcmp ("copyin", p))
31346             result = PRAGMA_OMP_CLAUSE_COPYIN;
31347           else if (!strcmp ("copyout", p))
31348             result = PRAGMA_OACC_CLAUSE_COPYOUT;
31349           else if (!strcmp ("copyprivate", p))
31350             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31351           else if (!strcmp ("create", p))
31352             result = PRAGMA_OACC_CLAUSE_CREATE;
31353           break;
31354         case 'd':
31355           if (!strcmp ("defaultmap", p))
31356             result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31357           else if (!strcmp ("depend", p))
31358             result = PRAGMA_OMP_CLAUSE_DEPEND;
31359           else if (!strcmp ("device", p))
31360             result = PRAGMA_OMP_CLAUSE_DEVICE;
31361           else if (!strcmp ("deviceptr", p))
31362             result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31363           else if (!strcmp ("device_resident", p))
31364             result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31365           else if (!strcmp ("dist_schedule", p))
31366             result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31367           break;
31368         case 'f':
31369           if (!strcmp ("final", p))
31370             result = PRAGMA_OMP_CLAUSE_FINAL;
31371           else if (!strcmp ("firstprivate", p))
31372             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31373           else if (!strcmp ("from", p))
31374             result = PRAGMA_OMP_CLAUSE_FROM;
31375           break;
31376         case 'g':
31377           if (!strcmp ("gang", p))
31378             result = PRAGMA_OACC_CLAUSE_GANG;
31379           else if (!strcmp ("grainsize", p))
31380             result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31381           break;
31382         case 'h':
31383           if (!strcmp ("hint", p))
31384             result = PRAGMA_OMP_CLAUSE_HINT;
31385           else if (!strcmp ("host", p))
31386             result = PRAGMA_OACC_CLAUSE_HOST;
31387           break;
31388         case 'i':
31389           if (!strcmp ("inbranch", p))
31390             result = PRAGMA_OMP_CLAUSE_INBRANCH;
31391           else if (!strcmp ("independent", p))
31392             result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31393           else if (!strcmp ("is_device_ptr", p))
31394             result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31395           break;
31396         case 'l':
31397           if (!strcmp ("lastprivate", p))
31398             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31399           else if (!strcmp ("linear", p))
31400             result = PRAGMA_OMP_CLAUSE_LINEAR;
31401           else if (!strcmp ("link", p))
31402             result = PRAGMA_OMP_CLAUSE_LINK;
31403           break;
31404         case 'm':
31405           if (!strcmp ("map", p))
31406             result = PRAGMA_OMP_CLAUSE_MAP;
31407           else if (!strcmp ("mergeable", p))
31408             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31409           break;
31410         case 'n':
31411           if (!strcmp ("nogroup", p))
31412             result = PRAGMA_OMP_CLAUSE_NOGROUP;
31413           else if (!strcmp ("notinbranch", p))
31414             result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31415           else if (!strcmp ("nowait", p))
31416             result = PRAGMA_OMP_CLAUSE_NOWAIT;
31417           else if (!strcmp ("num_gangs", p))
31418             result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31419           else if (!strcmp ("num_tasks", p))
31420             result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31421           else if (!strcmp ("num_teams", p))
31422             result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31423           else if (!strcmp ("num_threads", p))
31424             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31425           else if (!strcmp ("num_workers", p))
31426             result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31427           break;
31428         case 'o':
31429           if (!strcmp ("ordered", p))
31430             result = PRAGMA_OMP_CLAUSE_ORDERED;
31431           break;
31432         case 'p':
31433           if (!strcmp ("parallel", p))
31434             result = PRAGMA_OMP_CLAUSE_PARALLEL;
31435           else if (!strcmp ("present", p))
31436             result = PRAGMA_OACC_CLAUSE_PRESENT;
31437           else if (!strcmp ("present_or_copy", p)
31438                    || !strcmp ("pcopy", p))
31439             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31440           else if (!strcmp ("present_or_copyin", p)
31441                    || !strcmp ("pcopyin", p))
31442             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31443           else if (!strcmp ("present_or_copyout", p)
31444                    || !strcmp ("pcopyout", p))
31445             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31446           else if (!strcmp ("present_or_create", p)
31447                    || !strcmp ("pcreate", p))
31448             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31449           else if (!strcmp ("priority", p))
31450             result = PRAGMA_OMP_CLAUSE_PRIORITY;
31451           else if (!strcmp ("proc_bind", p))
31452             result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31453           break;
31454         case 'r':
31455           if (!strcmp ("reduction", p))
31456             result = PRAGMA_OMP_CLAUSE_REDUCTION;
31457           break;
31458         case 's':
31459           if (!strcmp ("safelen", p))
31460             result = PRAGMA_OMP_CLAUSE_SAFELEN;
31461           else if (!strcmp ("schedule", p))
31462             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31463           else if (!strcmp ("sections", p))
31464             result = PRAGMA_OMP_CLAUSE_SECTIONS;
31465           else if (!strcmp ("self", p))
31466             result = PRAGMA_OACC_CLAUSE_SELF;
31467           else if (!strcmp ("seq", p))
31468             result = PRAGMA_OACC_CLAUSE_SEQ;
31469           else if (!strcmp ("shared", p))
31470             result = PRAGMA_OMP_CLAUSE_SHARED;
31471           else if (!strcmp ("simd", p))
31472             result = PRAGMA_OMP_CLAUSE_SIMD;
31473           else if (!strcmp ("simdlen", p))
31474             result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31475           break;
31476         case 't':
31477           if (!strcmp ("taskgroup", p))
31478             result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31479           else if (!strcmp ("thread_limit", p))
31480             result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31481           else if (!strcmp ("threads", p))
31482             result = PRAGMA_OMP_CLAUSE_THREADS;
31483           else if (!strcmp ("tile", p))
31484             result = PRAGMA_OACC_CLAUSE_TILE;
31485           else if (!strcmp ("to", p))
31486             result = PRAGMA_OMP_CLAUSE_TO;
31487           break;
31488         case 'u':
31489           if (!strcmp ("uniform", p))
31490             result = PRAGMA_OMP_CLAUSE_UNIFORM;
31491           else if (!strcmp ("untied", p))
31492             result = PRAGMA_OMP_CLAUSE_UNTIED;
31493           else if (!strcmp ("use_device", p))
31494             result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31495           else if (!strcmp ("use_device_ptr", p))
31496             result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31497           break;
31498         case 'v':
31499           if (!strcmp ("vector", p))
31500             result = PRAGMA_OACC_CLAUSE_VECTOR;
31501           else if (!strcmp ("vector_length", p))
31502             result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31503           break;
31504         case 'w':
31505           if (!strcmp ("wait", p))
31506             result = PRAGMA_OACC_CLAUSE_WAIT;
31507           else if (!strcmp ("worker", p))
31508             result = PRAGMA_OACC_CLAUSE_WORKER;
31509           break;
31510         }
31511     }
31512
31513   if (result != PRAGMA_OMP_CLAUSE_NONE)
31514     cp_lexer_consume_token (parser->lexer);
31515
31516   return result;
31517 }
31518
31519 /* Validate that a clause of the given type does not already exist.  */
31520
31521 static void
31522 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31523                            const char *name, location_t location)
31524 {
31525   tree c;
31526
31527   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31528     if (OMP_CLAUSE_CODE (c) == code)
31529       {
31530         error_at (location, "too many %qs clauses", name);
31531         break;
31532       }
31533 }
31534
31535 /* OpenMP 2.5:
31536    variable-list:
31537      identifier
31538      variable-list , identifier
31539
31540    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31541    colon).  An opening parenthesis will have been consumed by the caller.
31542
31543    If KIND is nonzero, create the appropriate node and install the decl
31544    in OMP_CLAUSE_DECL and add the node to the head of the list.
31545
31546    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31547    return the list created.
31548
31549    COLON can be NULL if only closing parenthesis should end the list,
31550    or pointer to bool which will receive false if the list is terminated
31551    by closing parenthesis or true if the list is terminated by colon.  */
31552
31553 static tree
31554 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31555                                 tree list, bool *colon)
31556 {
31557   cp_token *token;
31558   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31559   if (colon)
31560     {
31561       parser->colon_corrects_to_scope_p = false;
31562       *colon = false;
31563     }
31564   while (1)
31565     {
31566       tree name, decl;
31567
31568       token = cp_lexer_peek_token (parser->lexer);
31569       if (kind != 0
31570           && current_class_ptr
31571           && cp_parser_is_keyword (token, RID_THIS))
31572         {
31573           decl = finish_this_expr ();
31574           if (TREE_CODE (decl) == NON_LVALUE_EXPR
31575               || CONVERT_EXPR_P (decl))
31576             decl = TREE_OPERAND (decl, 0);
31577           cp_lexer_consume_token (parser->lexer);
31578         }
31579       else
31580         {
31581           name = cp_parser_id_expression (parser, /*template_p=*/false,
31582                                           /*check_dependency_p=*/true,
31583                                           /*template_p=*/NULL,
31584                                           /*declarator_p=*/false,
31585                                           /*optional_p=*/false);
31586           if (name == error_mark_node)
31587             goto skip_comma;
31588
31589           if (identifier_p (name))
31590             decl = cp_parser_lookup_name_simple (parser, name, token->location);
31591           else
31592             decl = name;
31593           if (decl == error_mark_node)
31594             cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31595                                          token->location);
31596         }
31597       if (decl == error_mark_node)
31598         ;
31599       else if (kind != 0)
31600         {
31601           switch (kind)
31602             {
31603             case OMP_CLAUSE__CACHE_:
31604               /* The OpenACC cache directive explicitly only allows "array
31605                  elements or subarrays".  */
31606               if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31607                 {
31608                   error_at (token->location, "expected %<[%>");
31609                   decl = error_mark_node;
31610                   break;
31611                 }
31612               /* FALLTHROUGH.  */
31613             case OMP_CLAUSE_MAP:
31614             case OMP_CLAUSE_FROM:
31615             case OMP_CLAUSE_TO:
31616               while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31617                 {
31618                   location_t loc
31619                     = cp_lexer_peek_token (parser->lexer)->location;
31620                   cp_id_kind idk = CP_ID_KIND_NONE;
31621                   cp_lexer_consume_token (parser->lexer);
31622                   decl = convert_from_reference (decl);
31623                   decl
31624                     = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31625                                                               decl, false,
31626                                                               &idk, loc);
31627                 }
31628               /* FALLTHROUGH.  */
31629             case OMP_CLAUSE_DEPEND:
31630             case OMP_CLAUSE_REDUCTION:
31631               while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31632                 {
31633                   tree low_bound = NULL_TREE, length = NULL_TREE;
31634
31635                   parser->colon_corrects_to_scope_p = false;
31636                   cp_lexer_consume_token (parser->lexer);
31637                   if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31638                     low_bound = cp_parser_expression (parser);
31639                   if (!colon)
31640                     parser->colon_corrects_to_scope_p
31641                       = saved_colon_corrects_to_scope_p;
31642                   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31643                     length = integer_one_node;
31644                   else
31645                     {
31646                       /* Look for `:'.  */
31647                       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31648                         goto skip_comma;
31649                       if (!cp_lexer_next_token_is (parser->lexer,
31650                                                    CPP_CLOSE_SQUARE))
31651                         length = cp_parser_expression (parser);
31652                     }
31653                   /* Look for the closing `]'.  */
31654                   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31655                                           RT_CLOSE_SQUARE))
31656                     goto skip_comma;
31657
31658                   decl = tree_cons (low_bound, length, decl);
31659                 }
31660               break;
31661             default:
31662               break;
31663             }
31664
31665           tree u = build_omp_clause (token->location, kind);
31666           OMP_CLAUSE_DECL (u) = decl;
31667           OMP_CLAUSE_CHAIN (u) = list;
31668           list = u;
31669         }
31670       else
31671         list = tree_cons (decl, NULL_TREE, list);
31672
31673     get_comma:
31674       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31675         break;
31676       cp_lexer_consume_token (parser->lexer);
31677     }
31678
31679   if (colon)
31680     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31681
31682   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31683     {
31684       *colon = true;
31685       cp_parser_require (parser, CPP_COLON, RT_COLON);
31686       return list;
31687     }
31688
31689   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31690     {
31691       int ending;
31692
31693       /* Try to resync to an unnested comma.  Copied from
31694          cp_parser_parenthesized_expression_list.  */
31695     skip_comma:
31696       if (colon)
31697         parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31698       ending = cp_parser_skip_to_closing_parenthesis (parser,
31699                                                       /*recovering=*/true,
31700                                                       /*or_comma=*/true,
31701                                                       /*consume_paren=*/true);
31702       if (ending < 0)
31703         goto get_comma;
31704     }
31705
31706   return list;
31707 }
31708
31709 /* Similarly, but expect leading and trailing parenthesis.  This is a very
31710    common case for omp clauses.  */
31711
31712 static tree
31713 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31714 {
31715   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31716     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31717   return list;
31718 }
31719
31720 /* OpenACC 2.0:
31721    copy ( variable-list )
31722    copyin ( variable-list )
31723    copyout ( variable-list )
31724    create ( variable-list )
31725    delete ( variable-list )
31726    present ( variable-list )
31727    present_or_copy ( variable-list )
31728      pcopy ( variable-list )
31729    present_or_copyin ( variable-list )
31730      pcopyin ( variable-list )
31731    present_or_copyout ( variable-list )
31732      pcopyout ( variable-list )
31733    present_or_create ( variable-list )
31734      pcreate ( variable-list ) */
31735
31736 static tree
31737 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31738                             tree list)
31739 {
31740   enum gomp_map_kind kind;
31741   switch (c_kind)
31742     {
31743     case PRAGMA_OACC_CLAUSE_COPY:
31744       kind = GOMP_MAP_FORCE_TOFROM;
31745       break;
31746     case PRAGMA_OACC_CLAUSE_COPYIN:
31747       kind = GOMP_MAP_FORCE_TO;
31748       break;
31749     case PRAGMA_OACC_CLAUSE_COPYOUT:
31750       kind = GOMP_MAP_FORCE_FROM;
31751       break;
31752     case PRAGMA_OACC_CLAUSE_CREATE:
31753       kind = GOMP_MAP_FORCE_ALLOC;
31754       break;
31755     case PRAGMA_OACC_CLAUSE_DELETE:
31756       kind = GOMP_MAP_DELETE;
31757       break;
31758     case PRAGMA_OACC_CLAUSE_DEVICE:
31759       kind = GOMP_MAP_FORCE_TO;
31760       break;
31761     case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31762       kind = GOMP_MAP_DEVICE_RESIDENT;
31763       break;
31764     case PRAGMA_OACC_CLAUSE_HOST:
31765     case PRAGMA_OACC_CLAUSE_SELF:
31766       kind = GOMP_MAP_FORCE_FROM;
31767       break;
31768     case PRAGMA_OACC_CLAUSE_LINK:
31769       kind = GOMP_MAP_LINK;
31770       break;
31771     case PRAGMA_OACC_CLAUSE_PRESENT:
31772       kind = GOMP_MAP_FORCE_PRESENT;
31773       break;
31774     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31775       kind = GOMP_MAP_TOFROM;
31776       break;
31777     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31778       kind = GOMP_MAP_TO;
31779       break;
31780     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31781       kind = GOMP_MAP_FROM;
31782       break;
31783     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31784       kind = GOMP_MAP_ALLOC;
31785       break;
31786     default:
31787       gcc_unreachable ();
31788     }
31789   tree nl, c;
31790   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31791
31792   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31793     OMP_CLAUSE_SET_MAP_KIND (c, kind);
31794
31795   return nl;
31796 }
31797
31798 /* OpenACC 2.0:
31799    deviceptr ( variable-list ) */
31800
31801 static tree
31802 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31803 {
31804   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31805   tree vars, t;
31806
31807   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31808      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31809      variable-list must only allow for pointer variables.  */
31810   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31811   for (t = vars; t; t = TREE_CHAIN (t))
31812     {
31813       tree v = TREE_PURPOSE (t);
31814       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31815       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31816       OMP_CLAUSE_DECL (u) = v;
31817       OMP_CLAUSE_CHAIN (u) = list;
31818       list = u;
31819     }
31820
31821   return list;
31822 }
31823
31824 /* OpenACC 2.0:
31825    auto
31826    independent
31827    nohost
31828    seq */
31829
31830 static tree
31831 cp_parser_oacc_simple_clause (cp_parser * /* parser  */,
31832                               enum omp_clause_code code,
31833                               tree list, location_t location)
31834 {
31835   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31836   tree c = build_omp_clause (location, code);
31837   OMP_CLAUSE_CHAIN (c) = list;
31838   return c;
31839 }
31840
31841  /* OpenACC:
31842    num_gangs ( expression )
31843    num_workers ( expression )
31844    vector_length ( expression )  */
31845
31846 static tree
31847 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31848                                   const char *str, tree list)
31849 {
31850   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31851
31852   matching_parens parens;
31853   if (!parens.require_open (parser))
31854     return list;
31855
31856   tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31857
31858   if (t == error_mark_node
31859       || !parens.require_close (parser))
31860     {
31861       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31862                                              /*or_comma=*/false,
31863                                              /*consume_paren=*/true);
31864       return list;
31865     }
31866
31867   check_no_duplicate_clause (list, code, str, loc);
31868
31869   tree c = build_omp_clause (loc, code);
31870   OMP_CLAUSE_OPERAND (c, 0) = t;
31871   OMP_CLAUSE_CHAIN (c) = list;
31872   return c;
31873 }
31874
31875 /* OpenACC:
31876
31877     gang [( gang-arg-list )]
31878     worker [( [num:] int-expr )]
31879     vector [( [length:] int-expr )]
31880
31881   where gang-arg is one of:
31882
31883     [num:] int-expr
31884     static: size-expr
31885
31886   and size-expr may be:
31887
31888     *
31889     int-expr
31890 */
31891
31892 static tree
31893 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31894                              const char *str, tree list)
31895 {
31896   const char *id = "num";
31897   cp_lexer *lexer = parser->lexer;
31898   tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31899   location_t loc = cp_lexer_peek_token (lexer)->location;
31900
31901   if (kind == OMP_CLAUSE_VECTOR)
31902     id = "length";
31903
31904   if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31905     {
31906       matching_parens parens;
31907       parens.consume_open (parser);
31908
31909       do
31910         {
31911           cp_token *next = cp_lexer_peek_token (lexer);
31912           int idx = 0;
31913
31914           /* Gang static argument.  */
31915           if (kind == OMP_CLAUSE_GANG
31916               && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31917             {
31918               cp_lexer_consume_token (lexer);
31919
31920               if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31921                 goto cleanup_error;
31922
31923               idx = 1;
31924               if (ops[idx] != NULL)
31925                 {
31926                   cp_parser_error (parser, "too many %<static%> arguments");
31927                   goto cleanup_error;
31928                 }
31929
31930               /* Check for the '*' argument.  */
31931               if (cp_lexer_next_token_is (lexer, CPP_MULT)
31932                   && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31933                       || cp_lexer_nth_token_is (parser->lexer, 2,
31934                                                 CPP_CLOSE_PAREN)))
31935                 {
31936                   cp_lexer_consume_token (lexer);
31937                   ops[idx] = integer_minus_one_node;
31938
31939                   if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31940                     {
31941                       cp_lexer_consume_token (lexer);
31942                       continue;
31943                     }
31944                   else break;
31945                 }
31946             }
31947           /* Worker num: argument and vector length: arguments.  */
31948           else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31949                    && id_equal (next->u.value, id)
31950                    && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31951             {
31952               cp_lexer_consume_token (lexer);  /* id  */
31953               cp_lexer_consume_token (lexer);  /* ':'  */
31954             }
31955
31956           /* Now collect the actual argument.  */
31957           if (ops[idx] != NULL_TREE)
31958             {
31959               cp_parser_error (parser, "unexpected argument");
31960               goto cleanup_error;
31961             }
31962
31963           tree expr = cp_parser_assignment_expression (parser, NULL, false,
31964                                                        false);
31965           if (expr == error_mark_node)
31966             goto cleanup_error;
31967
31968           mark_exp_read (expr);
31969           ops[idx] = expr;
31970
31971           if (kind == OMP_CLAUSE_GANG
31972               && cp_lexer_next_token_is (lexer, CPP_COMMA))
31973             {
31974               cp_lexer_consume_token (lexer);
31975               continue;
31976             }
31977           break;
31978         }
31979       while (1);
31980
31981       if (!parens.require_close (parser))
31982         goto cleanup_error;
31983     }
31984
31985   check_no_duplicate_clause (list, kind, str, loc);
31986
31987   c = build_omp_clause (loc, kind);
31988
31989   if (ops[1])
31990     OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31991
31992   OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31993   OMP_CLAUSE_CHAIN (c) = list;
31994
31995   return c;
31996
31997  cleanup_error:
31998   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31999   return list;
32000 }
32001
32002 /* OpenACC 2.0:
32003    tile ( size-expr-list ) */
32004
32005 static tree
32006 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32007 {
32008   tree c, expr = error_mark_node;
32009   tree tile = NULL_TREE;
32010
32011   /* Collapse and tile are mutually exclusive.  (The spec doesn't say
32012      so, but the spec authors never considered such a case and have
32013      differing opinions on what it might mean, including 'not
32014      allowed'.)  */
32015   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32016   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32017                              clause_loc);
32018
32019   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32020     return list;
32021
32022   do
32023     {
32024       if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32025         return list;
32026       
32027       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32028           && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32029               || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32030         {
32031           cp_lexer_consume_token (parser->lexer);
32032           expr = integer_zero_node;
32033         }
32034       else
32035         expr = cp_parser_constant_expression (parser);
32036
32037       tile = tree_cons (NULL_TREE, expr, tile);
32038     }
32039   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32040
32041   /* Consume the trailing ')'.  */
32042   cp_lexer_consume_token (parser->lexer);
32043
32044   c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32045   tile = nreverse (tile);
32046   OMP_CLAUSE_TILE_LIST (c) = tile;
32047   OMP_CLAUSE_CHAIN (c) = list;
32048   return c;
32049 }
32050
32051 /* OpenACC 2.0
32052    Parse wait clause or directive parameters.  */
32053
32054 static tree
32055 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32056 {
32057   vec<tree, va_gc> *args;
32058   tree t, args_tree;
32059
32060   args = cp_parser_parenthesized_expression_list (parser, non_attr,
32061                                                   /*cast_p=*/false,
32062                                                   /*allow_expansion_p=*/true,
32063                                                   /*non_constant_p=*/NULL);
32064
32065   if (args == NULL || args->length () == 0)
32066     {
32067       cp_parser_error (parser, "expected integer expression before ')'");
32068       if (args != NULL)
32069         release_tree_vector (args);
32070       return list;
32071     }
32072
32073   args_tree = build_tree_list_vec (args);
32074
32075   release_tree_vector (args);
32076
32077   for (t = args_tree; t; t = TREE_CHAIN (t))
32078     {
32079       tree targ = TREE_VALUE (t);
32080
32081       if (targ != error_mark_node)
32082         {
32083           if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32084             error ("%<wait%> expression must be integral");
32085           else
32086             {
32087               tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32088
32089               targ = mark_rvalue_use (targ);
32090               OMP_CLAUSE_DECL (c) = targ;
32091               OMP_CLAUSE_CHAIN (c) = list;
32092               list = c;
32093             }
32094         }
32095     }
32096
32097   return list;
32098 }
32099
32100 /* OpenACC:
32101    wait ( int-expr-list ) */
32102
32103 static tree
32104 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32105 {
32106   location_t location = cp_lexer_peek_token (parser->lexer)->location;
32107
32108   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32109     return list;
32110
32111   list = cp_parser_oacc_wait_list (parser, location, list);
32112
32113   return list;
32114 }
32115
32116 /* OpenMP 3.0:
32117    collapse ( constant-expression ) */
32118
32119 static tree
32120 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32121 {
32122   tree c, num;
32123   location_t loc;
32124   HOST_WIDE_INT n;
32125
32126   loc = cp_lexer_peek_token (parser->lexer)->location;
32127   matching_parens parens;
32128   if (!parens.require_open (parser))
32129     return list;
32130
32131   num = cp_parser_constant_expression (parser);
32132
32133   if (!parens.require_close (parser))
32134     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32135                                            /*or_comma=*/false,
32136                                            /*consume_paren=*/true);
32137
32138   if (num == error_mark_node)
32139     return list;
32140   num = fold_non_dependent_expr (num);
32141   if (!tree_fits_shwi_p (num)
32142       || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32143       || (n = tree_to_shwi (num)) <= 0
32144       || (int) n != n)
32145     {
32146       error_at (loc, "collapse argument needs positive constant integer expression");
32147       return list;
32148     }
32149
32150   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32151   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32152   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32153   OMP_CLAUSE_CHAIN (c) = list;
32154   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32155
32156   return c;
32157 }
32158
32159 /* OpenMP 2.5:
32160    default ( none | shared )
32161
32162    OpenACC:
32163    default ( none | present ) */
32164
32165 static tree
32166 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32167                               location_t location, bool is_oacc)
32168 {
32169   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32170   tree c;
32171
32172   matching_parens parens;
32173   if (!parens.require_open (parser))
32174     return list;
32175   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32176     {
32177       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32178       const char *p = IDENTIFIER_POINTER (id);
32179
32180       switch (p[0])
32181         {
32182         case 'n':
32183           if (strcmp ("none", p) != 0)
32184             goto invalid_kind;
32185           kind = OMP_CLAUSE_DEFAULT_NONE;
32186           break;
32187
32188         case 'p':
32189           if (strcmp ("present", p) != 0 || !is_oacc)
32190             goto invalid_kind;
32191           kind = OMP_CLAUSE_DEFAULT_PRESENT;
32192           break;
32193
32194         case 's':
32195           if (strcmp ("shared", p) != 0 || is_oacc)
32196             goto invalid_kind;
32197           kind = OMP_CLAUSE_DEFAULT_SHARED;
32198           break;
32199
32200         default:
32201           goto invalid_kind;
32202         }
32203
32204       cp_lexer_consume_token (parser->lexer);
32205     }
32206   else
32207     {
32208     invalid_kind:
32209       if (is_oacc)
32210         cp_parser_error (parser, "expected %<none%> or %<present%>");
32211       else
32212         cp_parser_error (parser, "expected %<none%> or %<shared%>");
32213     }
32214
32215   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32216       || !parens.require_close (parser))
32217     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32218                                            /*or_comma=*/false,
32219                                            /*consume_paren=*/true);
32220
32221   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32222     return list;
32223
32224   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32225   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32226   OMP_CLAUSE_CHAIN (c) = list;
32227   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32228
32229   return c;
32230 }
32231
32232 /* OpenMP 3.1:
32233    final ( expression ) */
32234
32235 static tree
32236 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32237 {
32238   tree t, c;
32239
32240   matching_parens parens;
32241   if (!parens.require_open (parser))
32242     return list;
32243
32244   t = cp_parser_condition (parser);
32245
32246   if (t == error_mark_node
32247       || !parens.require_close (parser))
32248     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32249                                            /*or_comma=*/false,
32250                                            /*consume_paren=*/true);
32251
32252   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32253
32254   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32255   OMP_CLAUSE_FINAL_EXPR (c) = t;
32256   OMP_CLAUSE_CHAIN (c) = list;
32257
32258   return c;
32259 }
32260
32261 /* OpenMP 2.5:
32262    if ( expression )
32263
32264    OpenMP 4.5:
32265    if ( directive-name-modifier : expression )
32266
32267    directive-name-modifier:
32268      parallel | task | taskloop | target data | target | target update
32269      | target enter data | target exit data  */
32270
32271 static tree
32272 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32273                          bool is_omp)
32274 {
32275   tree t, c;
32276   enum tree_code if_modifier = ERROR_MARK;
32277
32278   matching_parens parens;
32279   if (!parens.require_open (parser))
32280     return list;
32281
32282   if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32283     {
32284       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32285       const char *p = IDENTIFIER_POINTER (id);
32286       int n = 2;
32287
32288       if (strcmp ("parallel", p) == 0)
32289         if_modifier = OMP_PARALLEL;
32290       else if (strcmp ("task", p) == 0)
32291         if_modifier = OMP_TASK;
32292       else if (strcmp ("taskloop", p) == 0)
32293         if_modifier = OMP_TASKLOOP;
32294       else if (strcmp ("target", p) == 0)
32295         {
32296           if_modifier = OMP_TARGET;
32297           if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32298             {
32299               id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32300               p = IDENTIFIER_POINTER (id);
32301               if (strcmp ("data", p) == 0)
32302                 if_modifier = OMP_TARGET_DATA;
32303               else if (strcmp ("update", p) == 0)
32304                 if_modifier = OMP_TARGET_UPDATE;
32305               else if (strcmp ("enter", p) == 0)
32306                 if_modifier = OMP_TARGET_ENTER_DATA;
32307               else if (strcmp ("exit", p) == 0)
32308                 if_modifier = OMP_TARGET_EXIT_DATA;
32309               if (if_modifier != OMP_TARGET)
32310                 n = 3;
32311               else
32312                 {
32313                   location_t loc
32314                     = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32315                   error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32316                                  "or %<exit%>");
32317                   if_modifier = ERROR_MARK;
32318                 }
32319               if (if_modifier == OMP_TARGET_ENTER_DATA
32320                   || if_modifier == OMP_TARGET_EXIT_DATA)
32321                 {
32322                   if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32323                     {
32324                       id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32325                       p = IDENTIFIER_POINTER (id);
32326                       if (strcmp ("data", p) == 0)
32327                         n = 4;
32328                     }
32329                   if (n != 4)
32330                     {
32331                       location_t loc
32332                         = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32333                       error_at (loc, "expected %<data%>");
32334                       if_modifier = ERROR_MARK;
32335                     }
32336                 }
32337             }
32338         }
32339       if (if_modifier != ERROR_MARK)
32340         {
32341           if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32342             {
32343               while (n-- > 0)
32344                 cp_lexer_consume_token (parser->lexer);
32345             }
32346           else
32347             {
32348               if (n > 2)
32349                 {
32350                   location_t loc
32351                     = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32352                   error_at (loc, "expected %<:%>");
32353                 }
32354               if_modifier = ERROR_MARK;
32355             }
32356         }
32357     }
32358
32359   t = cp_parser_condition (parser);
32360
32361   if (t == error_mark_node
32362       || !parens.require_close (parser))
32363     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32364                                            /*or_comma=*/false,
32365                                            /*consume_paren=*/true);
32366
32367   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32368     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32369       {
32370         if (if_modifier != ERROR_MARK
32371             && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32372           {
32373             const char *p = NULL;
32374             switch (if_modifier)
32375               {
32376               case OMP_PARALLEL: p = "parallel"; break;
32377               case OMP_TASK: p = "task"; break;
32378               case OMP_TASKLOOP: p = "taskloop"; break;
32379               case OMP_TARGET_DATA: p = "target data"; break;
32380               case OMP_TARGET: p = "target"; break;
32381               case OMP_TARGET_UPDATE: p = "target update"; break;
32382               case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32383               case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32384               default: gcc_unreachable ();
32385               }
32386             error_at (location, "too many %<if%> clauses with %qs modifier",
32387                       p);
32388             return list;
32389           }
32390         else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32391           {
32392             if (!is_omp)
32393               error_at (location, "too many %<if%> clauses");
32394             else
32395               error_at (location, "too many %<if%> clauses without modifier");
32396             return list;
32397           }
32398         else if (if_modifier == ERROR_MARK
32399                  || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32400           {
32401             error_at (location, "if any %<if%> clause has modifier, then all "
32402                                 "%<if%> clauses have to use modifier");
32403             return list;
32404           }
32405       }
32406
32407   c = build_omp_clause (location, OMP_CLAUSE_IF);
32408   OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32409   OMP_CLAUSE_IF_EXPR (c) = t;
32410   OMP_CLAUSE_CHAIN (c) = list;
32411
32412   return c;
32413 }
32414
32415 /* OpenMP 3.1:
32416    mergeable */
32417
32418 static tree
32419 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32420                                 tree list, location_t location)
32421 {
32422   tree c;
32423
32424   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32425                              location);
32426
32427   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32428   OMP_CLAUSE_CHAIN (c) = list;
32429   return c;
32430 }
32431
32432 /* OpenMP 2.5:
32433    nowait */
32434
32435 static tree
32436 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32437                              tree list, location_t location)
32438 {
32439   tree c;
32440
32441   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32442
32443   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32444   OMP_CLAUSE_CHAIN (c) = list;
32445   return c;
32446 }
32447
32448 /* OpenMP 2.5:
32449    num_threads ( expression ) */
32450
32451 static tree
32452 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32453                                   location_t location)
32454 {
32455   tree t, c;
32456
32457   matching_parens parens;
32458   if (!parens.require_open (parser))
32459     return list;
32460
32461   t = cp_parser_expression (parser);
32462
32463   if (t == error_mark_node
32464       || !parens.require_close (parser))
32465     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32466                                            /*or_comma=*/false,
32467                                            /*consume_paren=*/true);
32468
32469   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32470                              "num_threads", location);
32471
32472   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32473   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32474   OMP_CLAUSE_CHAIN (c) = list;
32475
32476   return c;
32477 }
32478
32479 /* OpenMP 4.5:
32480    num_tasks ( expression ) */
32481
32482 static tree
32483 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32484                                 location_t location)
32485 {
32486   tree t, c;
32487
32488   matching_parens parens;
32489   if (!parens.require_open (parser))
32490     return list;
32491
32492   t = cp_parser_expression (parser);
32493
32494   if (t == error_mark_node
32495       || !parens.require_close (parser))
32496     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32497                                            /*or_comma=*/false,
32498                                            /*consume_paren=*/true);
32499
32500   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32501                              "num_tasks", location);
32502
32503   c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32504   OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32505   OMP_CLAUSE_CHAIN (c) = list;
32506
32507   return c;
32508 }
32509
32510 /* OpenMP 4.5:
32511    grainsize ( expression ) */
32512
32513 static tree
32514 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32515                                 location_t location)
32516 {
32517   tree t, c;
32518
32519   matching_parens parens;
32520   if (!parens.require_open (parser))
32521     return list;
32522
32523   t = cp_parser_expression (parser);
32524
32525   if (t == error_mark_node
32526       || !parens.require_close (parser))
32527     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32528                                            /*or_comma=*/false,
32529                                            /*consume_paren=*/true);
32530
32531   check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32532                              "grainsize", location);
32533
32534   c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32535   OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32536   OMP_CLAUSE_CHAIN (c) = list;
32537
32538   return c;
32539 }
32540
32541 /* OpenMP 4.5:
32542    priority ( expression ) */
32543
32544 static tree
32545 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32546                                location_t location)
32547 {
32548   tree t, c;
32549
32550   matching_parens parens;
32551   if (!parens.require_open (parser))
32552     return list;
32553
32554   t = cp_parser_expression (parser);
32555
32556   if (t == error_mark_node
32557       || !parens.require_close (parser))
32558     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32559                                            /*or_comma=*/false,
32560                                            /*consume_paren=*/true);
32561
32562   check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32563                              "priority", location);
32564
32565   c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32566   OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32567   OMP_CLAUSE_CHAIN (c) = list;
32568
32569   return c;
32570 }
32571
32572 /* OpenMP 4.5:
32573    hint ( expression ) */
32574
32575 static tree
32576 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32577                            location_t location)
32578 {
32579   tree t, c;
32580
32581   matching_parens parens;
32582   if (!parens.require_open (parser))
32583     return list;
32584
32585   t = cp_parser_expression (parser);
32586
32587   if (t == error_mark_node
32588       || !parens.require_close (parser))
32589     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32590                                            /*or_comma=*/false,
32591                                            /*consume_paren=*/true);
32592
32593   check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32594
32595   c = build_omp_clause (location, OMP_CLAUSE_HINT);
32596   OMP_CLAUSE_HINT_EXPR (c) = t;
32597   OMP_CLAUSE_CHAIN (c) = list;
32598
32599   return c;
32600 }
32601
32602 /* OpenMP 4.5:
32603    defaultmap ( tofrom : scalar ) */
32604
32605 static tree
32606 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32607                                  location_t location)
32608 {
32609   tree c, id;
32610   const char *p;
32611
32612   matching_parens parens;
32613   if (!parens.require_open (parser))
32614     return list;
32615
32616   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32617     {
32618       cp_parser_error (parser, "expected %<tofrom%>");
32619       goto out_err;
32620     }
32621   id = cp_lexer_peek_token (parser->lexer)->u.value;
32622   p = IDENTIFIER_POINTER (id);
32623   if (strcmp (p, "tofrom") != 0)
32624     {
32625       cp_parser_error (parser, "expected %<tofrom%>");
32626       goto out_err;
32627     }
32628   cp_lexer_consume_token (parser->lexer);
32629   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32630     goto out_err;
32631
32632   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32633     {
32634       cp_parser_error (parser, "expected %<scalar%>");
32635       goto out_err;
32636     }
32637   id = cp_lexer_peek_token (parser->lexer)->u.value;
32638   p = IDENTIFIER_POINTER (id);
32639   if (strcmp (p, "scalar") != 0)
32640     {
32641       cp_parser_error (parser, "expected %<scalar%>");
32642       goto out_err;
32643     }
32644   cp_lexer_consume_token (parser->lexer);
32645   if (!parens.require_close (parser))
32646     goto out_err;
32647
32648   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32649                              location);
32650
32651   c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32652   OMP_CLAUSE_CHAIN (c) = list;
32653   return c;
32654
32655  out_err:
32656   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32657                                          /*or_comma=*/false,
32658                                          /*consume_paren=*/true);
32659   return list;
32660 }
32661
32662 /* OpenMP 2.5:
32663    ordered
32664
32665    OpenMP 4.5:
32666    ordered ( constant-expression ) */
32667
32668 static tree
32669 cp_parser_omp_clause_ordered (cp_parser *parser,
32670                               tree list, location_t location)
32671 {
32672   tree c, num = NULL_TREE;
32673   HOST_WIDE_INT n;
32674
32675   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32676                              "ordered", location);
32677
32678   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32679     {
32680       matching_parens parens;
32681       parens.consume_open (parser);
32682
32683       num = cp_parser_constant_expression (parser);
32684
32685       if (!parens.require_close (parser))
32686         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32687                                                /*or_comma=*/false,
32688                                                /*consume_paren=*/true);
32689
32690       if (num == error_mark_node)
32691         return list;
32692       num = fold_non_dependent_expr (num);
32693       if (!tree_fits_shwi_p (num)
32694           || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32695           || (n = tree_to_shwi (num)) <= 0
32696           || (int) n != n)
32697         {
32698           error_at (location,
32699                     "ordered argument needs positive constant integer "
32700                     "expression");
32701           return list;
32702         }
32703     }
32704
32705   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32706   OMP_CLAUSE_ORDERED_EXPR (c) = num;
32707   OMP_CLAUSE_CHAIN (c) = list;
32708   return c;
32709 }
32710
32711 /* OpenMP 2.5:
32712    reduction ( reduction-operator : variable-list )
32713
32714    reduction-operator:
32715      One of: + * - & ^ | && ||
32716
32717    OpenMP 3.1:
32718
32719    reduction-operator:
32720      One of: + * - & ^ | && || min max
32721
32722    OpenMP 4.0:
32723
32724    reduction-operator:
32725      One of: + * - & ^ | && ||
32726      id-expression  */
32727
32728 static tree
32729 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32730 {
32731   enum tree_code code = ERROR_MARK;
32732   tree nlist, c, id = NULL_TREE;
32733
32734   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32735     return list;
32736
32737   switch (cp_lexer_peek_token (parser->lexer)->type)
32738     {
32739     case CPP_PLUS: code = PLUS_EXPR; break;
32740     case CPP_MULT: code = MULT_EXPR; break;
32741     case CPP_MINUS: code = MINUS_EXPR; break;
32742     case CPP_AND: code = BIT_AND_EXPR; break;
32743     case CPP_XOR: code = BIT_XOR_EXPR; break;
32744     case CPP_OR: code = BIT_IOR_EXPR; break;
32745     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32746     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32747     default: break;
32748     }
32749
32750   if (code != ERROR_MARK)
32751     cp_lexer_consume_token (parser->lexer);
32752   else
32753     {
32754       bool saved_colon_corrects_to_scope_p;
32755       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32756       parser->colon_corrects_to_scope_p = false;
32757       id = cp_parser_id_expression (parser, /*template_p=*/false,
32758                                     /*check_dependency_p=*/true,
32759                                     /*template_p=*/NULL,
32760                                     /*declarator_p=*/false,
32761                                     /*optional_p=*/false);
32762       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32763       if (identifier_p (id))
32764         {
32765           const char *p = IDENTIFIER_POINTER (id);
32766
32767           if (strcmp (p, "min") == 0)
32768             code = MIN_EXPR;
32769           else if (strcmp (p, "max") == 0)
32770             code = MAX_EXPR;
32771           else if (id == ovl_op_identifier (false, PLUS_EXPR))
32772             code = PLUS_EXPR;
32773           else if (id == ovl_op_identifier (false, MULT_EXPR))
32774             code = MULT_EXPR;
32775           else if (id == ovl_op_identifier (false, MINUS_EXPR))
32776             code = MINUS_EXPR;
32777           else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32778             code = BIT_AND_EXPR;
32779           else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32780             code = BIT_IOR_EXPR;
32781           else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32782             code = BIT_XOR_EXPR;
32783           else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32784             code = TRUTH_ANDIF_EXPR;
32785           else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32786             code = TRUTH_ORIF_EXPR;
32787           id = omp_reduction_id (code, id, NULL_TREE);
32788           tree scope = parser->scope;
32789           if (scope)
32790             id = build_qualified_name (NULL_TREE, scope, id, false);
32791           parser->scope = NULL_TREE;
32792           parser->qualifying_scope = NULL_TREE;
32793           parser->object_scope = NULL_TREE;
32794         }
32795       else
32796         {
32797           error ("invalid reduction-identifier");
32798          resync_fail:
32799           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32800                                                  /*or_comma=*/false,
32801                                                  /*consume_paren=*/true);
32802           return list;
32803         }
32804     }
32805
32806   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32807     goto resync_fail;
32808
32809   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32810                                           NULL);
32811   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32812     {
32813       OMP_CLAUSE_REDUCTION_CODE (c) = code;
32814       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32815     }
32816
32817   return nlist;
32818 }
32819
32820 /* OpenMP 2.5:
32821    schedule ( schedule-kind )
32822    schedule ( schedule-kind , expression )
32823
32824    schedule-kind:
32825      static | dynamic | guided | runtime | auto
32826
32827    OpenMP 4.5:
32828    schedule ( schedule-modifier : schedule-kind )
32829    schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32830
32831    schedule-modifier:
32832      simd
32833      monotonic
32834      nonmonotonic  */
32835
32836 static tree
32837 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32838 {
32839   tree c, t;
32840   int modifiers = 0, nmodifiers = 0;
32841
32842   matching_parens parens;
32843   if (!parens.require_open (parser))
32844     return list;
32845
32846   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32847
32848   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32849     {
32850       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32851       const char *p = IDENTIFIER_POINTER (id);
32852       if (strcmp ("simd", p) == 0)
32853         OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32854       else if (strcmp ("monotonic", p) == 0)
32855         modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32856       else if (strcmp ("nonmonotonic", p) == 0)
32857         modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32858       else
32859         break;
32860       cp_lexer_consume_token (parser->lexer);
32861       if (nmodifiers++ == 0
32862           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32863         cp_lexer_consume_token (parser->lexer);
32864       else
32865         {
32866           cp_parser_require (parser, CPP_COLON, RT_COLON);
32867           break;
32868         }
32869     }
32870
32871   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32872     {
32873       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32874       const char *p = IDENTIFIER_POINTER (id);
32875
32876       switch (p[0])
32877         {
32878         case 'd':
32879           if (strcmp ("dynamic", p) != 0)
32880             goto invalid_kind;
32881           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32882           break;
32883
32884         case 'g':
32885           if (strcmp ("guided", p) != 0)
32886             goto invalid_kind;
32887           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32888           break;
32889
32890         case 'r':
32891           if (strcmp ("runtime", p) != 0)
32892             goto invalid_kind;
32893           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32894           break;
32895
32896         default:
32897           goto invalid_kind;
32898         }
32899     }
32900   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32901     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32902   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32903     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32904   else
32905     goto invalid_kind;
32906   cp_lexer_consume_token (parser->lexer);
32907
32908   if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32909                     | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32910       == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32911           | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32912     {
32913       error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32914                           "specified");
32915       modifiers = 0;
32916     }
32917
32918   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32919     {
32920       cp_token *token;
32921       cp_lexer_consume_token (parser->lexer);
32922
32923       token = cp_lexer_peek_token (parser->lexer);
32924       t = cp_parser_assignment_expression (parser);
32925
32926       if (t == error_mark_node)
32927         goto resync_fail;
32928       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32929         error_at (token->location, "schedule %<runtime%> does not take "
32930                   "a %<chunk_size%> parameter");
32931       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32932         error_at (token->location, "schedule %<auto%> does not take "
32933                   "a %<chunk_size%> parameter");
32934       else
32935         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32936
32937       if (!parens.require_close (parser))
32938         goto resync_fail;
32939     }
32940   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32941     goto resync_fail;
32942
32943   OMP_CLAUSE_SCHEDULE_KIND (c)
32944     = (enum omp_clause_schedule_kind)
32945       (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32946
32947   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32948   OMP_CLAUSE_CHAIN (c) = list;
32949   return c;
32950
32951  invalid_kind:
32952   cp_parser_error (parser, "invalid schedule kind");
32953  resync_fail:
32954   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32955                                          /*or_comma=*/false,
32956                                          /*consume_paren=*/true);
32957   return list;
32958 }
32959
32960 /* OpenMP 3.0:
32961    untied */
32962
32963 static tree
32964 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32965                              tree list, location_t location)
32966 {
32967   tree c;
32968
32969   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32970
32971   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32972   OMP_CLAUSE_CHAIN (c) = list;
32973   return c;
32974 }
32975
32976 /* OpenMP 4.0:
32977    inbranch
32978    notinbranch */
32979
32980 static tree
32981 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32982                              tree list, location_t location)
32983 {
32984   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32985   tree c = build_omp_clause (location, code);
32986   OMP_CLAUSE_CHAIN (c) = list;
32987   return c;
32988 }
32989
32990 /* OpenMP 4.0:
32991    parallel
32992    for
32993    sections
32994    taskgroup */
32995
32996 static tree
32997 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32998                                  enum omp_clause_code code,
32999                                  tree list, location_t location)
33000 {
33001   tree c = build_omp_clause (location, code);
33002   OMP_CLAUSE_CHAIN (c) = list;
33003   return c;
33004 }
33005
33006 /* OpenMP 4.5:
33007    nogroup */
33008
33009 static tree
33010 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33011                               tree list, location_t location)
33012 {
33013   check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33014   tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33015   OMP_CLAUSE_CHAIN (c) = list;
33016   return c;
33017 }
33018
33019 /* OpenMP 4.5:
33020    simd
33021    threads */
33022
33023 static tree
33024 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33025                                   enum omp_clause_code code,
33026                                   tree list, location_t location)
33027 {
33028   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33029   tree c = build_omp_clause (location, code);
33030   OMP_CLAUSE_CHAIN (c) = list;
33031   return c;
33032 }
33033
33034 /* OpenMP 4.0:
33035    num_teams ( expression ) */
33036
33037 static tree
33038 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33039                                 location_t location)
33040 {
33041   tree t, c;
33042
33043   matching_parens parens;
33044   if (!parens.require_open (parser))
33045     return list;
33046
33047   t = cp_parser_expression (parser);
33048
33049   if (t == error_mark_node
33050       || !parens.require_close (parser))
33051     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33052                                            /*or_comma=*/false,
33053                                            /*consume_paren=*/true);
33054
33055   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33056                              "num_teams", location);
33057
33058   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33059   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33060   OMP_CLAUSE_CHAIN (c) = list;
33061
33062   return c;
33063 }
33064
33065 /* OpenMP 4.0:
33066    thread_limit ( expression ) */
33067
33068 static tree
33069 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33070                                    location_t location)
33071 {
33072   tree t, c;
33073
33074   matching_parens parens;
33075   if (!parens.require_open (parser))
33076     return list;
33077
33078   t = cp_parser_expression (parser);
33079
33080   if (t == error_mark_node
33081       || !parens.require_close (parser))
33082     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33083                                            /*or_comma=*/false,
33084                                            /*consume_paren=*/true);
33085
33086   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33087                              "thread_limit", location);
33088
33089   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33090   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33091   OMP_CLAUSE_CHAIN (c) = list;
33092
33093   return c;
33094 }
33095
33096 /* OpenMP 4.0:
33097    aligned ( variable-list )
33098    aligned ( variable-list : constant-expression )  */
33099
33100 static tree
33101 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33102 {
33103   tree nlist, c, alignment = NULL_TREE;
33104   bool colon;
33105
33106   matching_parens parens;
33107   if (!parens.require_open (parser))
33108     return list;
33109
33110   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33111                                           &colon);
33112
33113   if (colon)
33114     {
33115       alignment = cp_parser_constant_expression (parser);
33116
33117       if (!parens.require_close (parser))
33118         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33119                                                /*or_comma=*/false,
33120                                                /*consume_paren=*/true);
33121
33122       if (alignment == error_mark_node)
33123         alignment = NULL_TREE;
33124     }
33125
33126   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33127     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33128
33129   return nlist;
33130 }
33131
33132 /* OpenMP 4.0:
33133    linear ( variable-list )
33134    linear ( variable-list : expression )
33135
33136    OpenMP 4.5:
33137    linear ( modifier ( variable-list ) )
33138    linear ( modifier ( variable-list ) : expression ) */
33139
33140 static tree
33141 cp_parser_omp_clause_linear (cp_parser *parser, tree list, 
33142                              bool declare_simd)
33143 {
33144   tree nlist, c, step = integer_one_node;
33145   bool colon;
33146   enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33147
33148   matching_parens parens;
33149   if (!parens.require_open (parser))
33150     return list;
33151
33152   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33153     {
33154       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33155       const char *p = IDENTIFIER_POINTER (id);
33156
33157       if (strcmp ("ref", p) == 0)
33158         kind = OMP_CLAUSE_LINEAR_REF;
33159       else if (strcmp ("val", p) == 0)
33160         kind = OMP_CLAUSE_LINEAR_VAL;
33161       else if (strcmp ("uval", p) == 0)
33162         kind = OMP_CLAUSE_LINEAR_UVAL;
33163       if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33164         cp_lexer_consume_token (parser->lexer);
33165       else
33166         kind = OMP_CLAUSE_LINEAR_DEFAULT;
33167     }
33168
33169   if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33170     nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33171                                             &colon);
33172   else
33173     {
33174       nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33175       colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33176       if (colon)
33177         cp_parser_require (parser, CPP_COLON, RT_COLON);
33178       else if (!parens.require_close (parser))
33179         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33180                                                /*or_comma=*/false,
33181                                                /*consume_paren=*/true);
33182     }
33183
33184   if (colon)
33185     {
33186       step = NULL_TREE;
33187       if (declare_simd
33188           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33189           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33190         {
33191           cp_token *token = cp_lexer_peek_token (parser->lexer);
33192           cp_parser_parse_tentatively (parser);
33193           step = cp_parser_id_expression (parser, /*template_p=*/false,
33194                                           /*check_dependency_p=*/true,
33195                                           /*template_p=*/NULL,
33196                                           /*declarator_p=*/false,
33197                                           /*optional_p=*/false);
33198           if (step != error_mark_node)
33199             step = cp_parser_lookup_name_simple (parser, step, token->location);
33200           if (step == error_mark_node)
33201             {
33202               step = NULL_TREE;
33203               cp_parser_abort_tentative_parse (parser);
33204             }
33205           else if (!cp_parser_parse_definitely (parser))
33206             step = NULL_TREE;
33207         }
33208       if (!step)
33209         step = cp_parser_expression (parser);
33210
33211       if (!parens.require_close (parser))
33212         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33213                                                /*or_comma=*/false,
33214                                                /*consume_paren=*/true);
33215
33216       if (step == error_mark_node)
33217         return list;
33218     }
33219
33220   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33221     {
33222       OMP_CLAUSE_LINEAR_STEP (c) = step;
33223       OMP_CLAUSE_LINEAR_KIND (c) = kind;
33224     }
33225
33226   return nlist;
33227 }
33228
33229 /* OpenMP 4.0:
33230    safelen ( constant-expression )  */
33231
33232 static tree
33233 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33234                               location_t location)
33235 {
33236   tree t, c;
33237
33238   matching_parens parens;
33239   if (!parens.require_open (parser))
33240     return list;
33241
33242   t = cp_parser_constant_expression (parser);
33243
33244   if (t == error_mark_node
33245       || !parens.require_close (parser))
33246     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33247                                            /*or_comma=*/false,
33248                                            /*consume_paren=*/true);
33249
33250   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33251
33252   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33253   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33254   OMP_CLAUSE_CHAIN (c) = list;
33255
33256   return c;
33257 }
33258
33259 /* OpenMP 4.0:
33260    simdlen ( constant-expression )  */
33261
33262 static tree
33263 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33264                               location_t location)
33265 {
33266   tree t, c;
33267
33268   matching_parens parens;
33269   if (!parens.require_open (parser))
33270     return list;
33271
33272   t = cp_parser_constant_expression (parser);
33273
33274   if (t == error_mark_node
33275       || !parens.require_close (parser))
33276     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33277                                            /*or_comma=*/false,
33278                                            /*consume_paren=*/true);
33279
33280   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33281
33282   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33283   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33284   OMP_CLAUSE_CHAIN (c) = list;
33285
33286   return c;
33287 }
33288
33289 /* OpenMP 4.5:
33290    vec:
33291      identifier [+/- integer]
33292      vec , identifier [+/- integer]
33293 */
33294
33295 static tree
33296 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33297                                   tree list)
33298 {
33299   tree vec = NULL;
33300
33301   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33302     {
33303       cp_parser_error (parser, "expected identifier");
33304       return list;
33305     }
33306
33307   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33308     {
33309       location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33310       tree t, identifier = cp_parser_identifier (parser);
33311       tree addend = NULL;
33312
33313       if (identifier == error_mark_node)
33314         t = error_mark_node;
33315       else
33316         {
33317           t = cp_parser_lookup_name_simple
33318                 (parser, identifier,
33319                  cp_lexer_peek_token (parser->lexer)->location);
33320           if (t == error_mark_node)
33321             cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33322                                          id_loc);
33323         }
33324
33325       bool neg = false;
33326       if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33327         neg = true;
33328       else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33329         {
33330           addend = integer_zero_node;
33331           goto add_to_vector;
33332         }
33333       cp_lexer_consume_token (parser->lexer);
33334
33335       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33336         {
33337           cp_parser_error (parser, "expected integer");
33338           return list;
33339         }
33340
33341       addend = cp_lexer_peek_token (parser->lexer)->u.value;
33342       if (TREE_CODE (addend) != INTEGER_CST)
33343         {
33344           cp_parser_error (parser, "expected integer");
33345           return list;
33346         }
33347       cp_lexer_consume_token (parser->lexer);
33348
33349     add_to_vector:
33350       if (t != error_mark_node)
33351         {
33352           vec = tree_cons (addend, t, vec);
33353           if (neg)
33354             OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33355         }
33356
33357       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33358         break;
33359
33360       cp_lexer_consume_token (parser->lexer);
33361     }
33362
33363   if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33364     {
33365       tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33366       OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33367       OMP_CLAUSE_DECL (u) = nreverse (vec);
33368       OMP_CLAUSE_CHAIN (u) = list;
33369       return u;
33370     }
33371   return list;
33372 }
33373
33374 /* OpenMP 4.0:
33375    depend ( depend-kind : variable-list )
33376
33377    depend-kind:
33378      in | out | inout
33379
33380    OpenMP 4.5:
33381    depend ( source )
33382
33383    depend ( sink : vec ) */
33384
33385 static tree
33386 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33387 {
33388   tree nlist, c;
33389   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33390
33391   matching_parens parens;
33392   if (!parens.require_open (parser))
33393     return list;
33394
33395   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33396     {
33397       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33398       const char *p = IDENTIFIER_POINTER (id);
33399
33400       if (strcmp ("in", p) == 0)
33401         kind = OMP_CLAUSE_DEPEND_IN;
33402       else if (strcmp ("inout", p) == 0)
33403         kind = OMP_CLAUSE_DEPEND_INOUT;
33404       else if (strcmp ("out", p) == 0)
33405         kind = OMP_CLAUSE_DEPEND_OUT;
33406       else if (strcmp ("source", p) == 0)
33407         kind = OMP_CLAUSE_DEPEND_SOURCE;
33408       else if (strcmp ("sink", p) == 0)
33409         kind = OMP_CLAUSE_DEPEND_SINK;
33410       else
33411         goto invalid_kind;
33412     }
33413   else
33414     goto invalid_kind;
33415
33416   cp_lexer_consume_token (parser->lexer);
33417
33418   if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33419     {
33420       c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33421       OMP_CLAUSE_DEPEND_KIND (c) = kind;
33422       OMP_CLAUSE_DECL (c) = NULL_TREE;
33423       OMP_CLAUSE_CHAIN (c) = list;
33424       if (!parens.require_close (parser))
33425         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33426                                                /*or_comma=*/false,
33427                                                /*consume_paren=*/true);
33428       return c;
33429     }
33430
33431   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33432     goto resync_fail;
33433
33434   if (kind == OMP_CLAUSE_DEPEND_SINK)
33435     nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33436   else
33437     {
33438       nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33439                                               list, NULL);
33440
33441       for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33442         OMP_CLAUSE_DEPEND_KIND (c) = kind;
33443     }
33444   return nlist;
33445
33446  invalid_kind:
33447   cp_parser_error (parser, "invalid depend kind");
33448  resync_fail:
33449   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33450                                          /*or_comma=*/false,
33451                                          /*consume_paren=*/true);
33452   return list;
33453 }
33454
33455 /* OpenMP 4.0:
33456    map ( map-kind : variable-list )
33457    map ( variable-list )
33458
33459    map-kind:
33460      alloc | to | from | tofrom
33461
33462    OpenMP 4.5:
33463    map-kind:
33464      alloc | to | from | tofrom | release | delete
33465
33466    map ( always [,] map-kind: variable-list ) */
33467
33468 static tree
33469 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33470 {
33471   tree nlist, c;
33472   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33473   bool always = false;
33474
33475   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33476     return list;
33477
33478   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33479     {
33480       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33481       const char *p = IDENTIFIER_POINTER (id);
33482
33483       if (strcmp ("always", p) == 0)
33484         {
33485           int nth = 2;
33486           if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33487             nth++;
33488           if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33489                || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33490                    == RID_DELETE))
33491               && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33492                   == CPP_COLON))
33493             {
33494               always = true;
33495               cp_lexer_consume_token (parser->lexer);
33496               if (nth == 3)
33497                 cp_lexer_consume_token (parser->lexer);
33498             }
33499         }
33500     }
33501
33502   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33503       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33504     {
33505       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33506       const char *p = IDENTIFIER_POINTER (id);
33507
33508       if (strcmp ("alloc", p) == 0)
33509         kind = GOMP_MAP_ALLOC;
33510       else if (strcmp ("to", p) == 0)
33511         kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33512       else if (strcmp ("from", p) == 0)
33513         kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33514       else if (strcmp ("tofrom", p) == 0)
33515         kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33516       else if (strcmp ("release", p) == 0)
33517         kind = GOMP_MAP_RELEASE;
33518       else
33519         {
33520           cp_parser_error (parser, "invalid map kind");
33521           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33522                                                  /*or_comma=*/false,
33523                                                  /*consume_paren=*/true);
33524           return list;
33525         }
33526       cp_lexer_consume_token (parser->lexer);
33527       cp_lexer_consume_token (parser->lexer);
33528     }
33529   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33530            && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33531     {
33532       kind = GOMP_MAP_DELETE;
33533       cp_lexer_consume_token (parser->lexer);
33534       cp_lexer_consume_token (parser->lexer);
33535     }
33536
33537   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33538                                           NULL);
33539
33540   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33541     OMP_CLAUSE_SET_MAP_KIND (c, kind);
33542
33543   return nlist;
33544 }
33545
33546 /* OpenMP 4.0:
33547    device ( expression ) */
33548
33549 static tree
33550 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33551                              location_t location)
33552 {
33553   tree t, c;
33554
33555   matching_parens parens;
33556   if (!parens.require_open (parser))
33557     return list;
33558
33559   t = cp_parser_expression (parser);
33560
33561   if (t == error_mark_node
33562       || !parens.require_close (parser))
33563     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33564                                            /*or_comma=*/false,
33565                                            /*consume_paren=*/true);
33566
33567   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33568                              "device", location);
33569
33570   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33571   OMP_CLAUSE_DEVICE_ID (c) = t;
33572   OMP_CLAUSE_CHAIN (c) = list;
33573
33574   return c;
33575 }
33576
33577 /* OpenMP 4.0:
33578    dist_schedule ( static )
33579    dist_schedule ( static , expression )  */
33580
33581 static tree
33582 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33583                                     location_t location)
33584 {
33585   tree c, t;
33586
33587   matching_parens parens;
33588   if (!parens.require_open (parser))
33589     return list;
33590
33591   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33592
33593   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33594     goto invalid_kind;
33595   cp_lexer_consume_token (parser->lexer);
33596
33597   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33598     {
33599       cp_lexer_consume_token (parser->lexer);
33600
33601       t = cp_parser_assignment_expression (parser);
33602
33603       if (t == error_mark_node)
33604         goto resync_fail;
33605       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33606
33607       if (!parens.require_close (parser))
33608         goto resync_fail;
33609     }
33610   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33611     goto resync_fail;
33612
33613   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33614                              location);
33615   OMP_CLAUSE_CHAIN (c) = list;
33616   return c;
33617
33618  invalid_kind:
33619   cp_parser_error (parser, "invalid dist_schedule kind");
33620  resync_fail:
33621   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33622                                          /*or_comma=*/false,
33623                                          /*consume_paren=*/true);
33624   return list;
33625 }
33626
33627 /* OpenMP 4.0:
33628    proc_bind ( proc-bind-kind )
33629
33630    proc-bind-kind:
33631      master | close | spread  */
33632
33633 static tree
33634 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33635                                 location_t location)
33636 {
33637   tree c;
33638   enum omp_clause_proc_bind_kind kind;
33639
33640   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33641     return list;
33642
33643   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33644     {
33645       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33646       const char *p = IDENTIFIER_POINTER (id);
33647
33648       if (strcmp ("master", p) == 0)
33649         kind = OMP_CLAUSE_PROC_BIND_MASTER;
33650       else if (strcmp ("close", p) == 0)
33651         kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33652       else if (strcmp ("spread", p) == 0)
33653         kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33654       else
33655         goto invalid_kind;
33656     }
33657   else
33658     goto invalid_kind;
33659
33660   cp_lexer_consume_token (parser->lexer);
33661   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33662     goto resync_fail;
33663
33664   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33665   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33666                              location);
33667   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33668   OMP_CLAUSE_CHAIN (c) = list;
33669   return c;
33670
33671  invalid_kind:
33672   cp_parser_error (parser, "invalid depend kind");
33673  resync_fail:
33674   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33675                                          /*or_comma=*/false,
33676                                          /*consume_paren=*/true);
33677   return list;
33678 }
33679
33680 /* OpenACC:
33681    async [( int-expr )] */
33682
33683 static tree
33684 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33685 {
33686   tree c, t;
33687   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33688
33689   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33690
33691   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33692     {
33693       matching_parens parens;
33694       parens.consume_open (parser);
33695
33696       t = cp_parser_expression (parser);
33697       if (t == error_mark_node
33698           || !parens.require_close (parser))
33699         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33700                                                 /*or_comma=*/false,
33701                                                 /*consume_paren=*/true);
33702     }
33703
33704   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33705
33706   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33707   OMP_CLAUSE_ASYNC_EXPR (c) = t;
33708   OMP_CLAUSE_CHAIN (c) = list;
33709   list = c;
33710
33711   return list;
33712 }
33713
33714 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
33715    is a bitmask in MASK.  Return the list of clauses found.  */
33716
33717 static tree
33718 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33719                            const char *where, cp_token *pragma_tok,
33720                            bool finish_p = true)
33721 {
33722   tree clauses = NULL;
33723   bool first = true;
33724
33725   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33726     {
33727       location_t here;
33728       pragma_omp_clause c_kind;
33729       omp_clause_code code;
33730       const char *c_name;
33731       tree prev = clauses;
33732
33733       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33734         cp_lexer_consume_token (parser->lexer);
33735
33736       here = cp_lexer_peek_token (parser->lexer)->location;
33737       c_kind = cp_parser_omp_clause_name (parser);
33738
33739       switch (c_kind)
33740         {
33741         case PRAGMA_OACC_CLAUSE_ASYNC:
33742           clauses = cp_parser_oacc_clause_async (parser, clauses);
33743           c_name = "async";
33744           break;
33745         case PRAGMA_OACC_CLAUSE_AUTO:
33746           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33747                                                  clauses, here);
33748           c_name = "auto";
33749           break;
33750         case PRAGMA_OACC_CLAUSE_COLLAPSE:
33751           clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33752           c_name = "collapse";
33753           break;
33754         case PRAGMA_OACC_CLAUSE_COPY:
33755           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33756           c_name = "copy";
33757           break;
33758         case PRAGMA_OACC_CLAUSE_COPYIN:
33759           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33760           c_name = "copyin";
33761           break;
33762         case PRAGMA_OACC_CLAUSE_COPYOUT:
33763           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33764           c_name = "copyout";
33765           break;
33766         case PRAGMA_OACC_CLAUSE_CREATE:
33767           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33768           c_name = "create";
33769           break;
33770         case PRAGMA_OACC_CLAUSE_DELETE:
33771           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33772           c_name = "delete";
33773           break;
33774         case PRAGMA_OMP_CLAUSE_DEFAULT:
33775           clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33776           c_name = "default";
33777           break;
33778         case PRAGMA_OACC_CLAUSE_DEVICE:
33779           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33780           c_name = "device";
33781           break;
33782         case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33783           clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33784           c_name = "deviceptr";
33785           break;
33786         case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33787           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33788           c_name = "device_resident";
33789           break;
33790         case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33791           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33792                                             clauses);
33793           c_name = "firstprivate";
33794           break;
33795         case PRAGMA_OACC_CLAUSE_GANG:
33796           c_name = "gang";
33797           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33798                                                  c_name, clauses);
33799           break;
33800         case PRAGMA_OACC_CLAUSE_HOST:
33801           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33802           c_name = "host";
33803           break;
33804         case PRAGMA_OACC_CLAUSE_IF:
33805           clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33806           c_name = "if";
33807           break;
33808         case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33809           clauses = cp_parser_oacc_simple_clause (parser,
33810                                                   OMP_CLAUSE_INDEPENDENT,
33811                                                   clauses, here);
33812           c_name = "independent";
33813           break;
33814         case PRAGMA_OACC_CLAUSE_LINK:
33815           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33816           c_name = "link";
33817           break;
33818         case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33819           code = OMP_CLAUSE_NUM_GANGS;
33820           c_name = "num_gangs";
33821           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33822                                                       clauses);
33823           break;
33824         case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33825           c_name = "num_workers";
33826           code = OMP_CLAUSE_NUM_WORKERS;
33827           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33828                                                       clauses);
33829           break;
33830         case PRAGMA_OACC_CLAUSE_PRESENT:
33831           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33832           c_name = "present";
33833           break;
33834         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33835           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33836           c_name = "present_or_copy";
33837           break;
33838         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33839           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33840           c_name = "present_or_copyin";
33841           break;
33842         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33843           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33844           c_name = "present_or_copyout";
33845           break;
33846         case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33847           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33848           c_name = "present_or_create";
33849           break;
33850         case PRAGMA_OACC_CLAUSE_PRIVATE:
33851           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33852                                             clauses);
33853           c_name = "private";
33854           break;
33855         case PRAGMA_OACC_CLAUSE_REDUCTION:
33856           clauses = cp_parser_omp_clause_reduction (parser, clauses);
33857           c_name = "reduction";
33858           break;
33859         case PRAGMA_OACC_CLAUSE_SELF:
33860           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33861           c_name = "self";
33862           break;
33863         case PRAGMA_OACC_CLAUSE_SEQ:
33864           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33865                                                  clauses, here);
33866           c_name = "seq";
33867           break;
33868         case PRAGMA_OACC_CLAUSE_TILE:
33869           clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33870           c_name = "tile";
33871           break;
33872         case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33873           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33874                                             clauses);
33875           c_name = "use_device";
33876           break;
33877         case PRAGMA_OACC_CLAUSE_VECTOR:
33878           c_name = "vector";
33879           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33880                                                  c_name, clauses);
33881           break;
33882         case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33883           c_name = "vector_length";
33884           code = OMP_CLAUSE_VECTOR_LENGTH;
33885           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33886                                                       clauses);
33887           break;
33888         case PRAGMA_OACC_CLAUSE_WAIT:
33889           clauses = cp_parser_oacc_clause_wait (parser, clauses);
33890           c_name = "wait";
33891           break;
33892         case PRAGMA_OACC_CLAUSE_WORKER:
33893           c_name = "worker";
33894           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33895                                                  c_name, clauses);
33896           break;
33897         default:
33898           cp_parser_error (parser, "expected %<#pragma acc%> clause");
33899           goto saw_error;
33900         }
33901
33902       first = false;
33903
33904       if (((mask >> c_kind) & 1) == 0)
33905         {
33906           /* Remove the invalid clause(s) from the list to avoid
33907              confusing the rest of the compiler.  */
33908           clauses = prev;
33909           error_at (here, "%qs is not valid for %qs", c_name, where);
33910         }
33911     }
33912
33913  saw_error:
33914   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33915
33916   if (finish_p)
33917     return finish_omp_clauses (clauses, C_ORT_ACC);
33918
33919   return clauses;
33920 }
33921
33922 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
33923    is a bitmask in MASK.  Return the list of clauses found; the result
33924    of clause default goes in *pdefault.  */
33925
33926 static tree
33927 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33928                            const char *where, cp_token *pragma_tok,
33929                            bool finish_p = true)
33930 {
33931   tree clauses = NULL;
33932   bool first = true;
33933   cp_token *token = NULL;
33934
33935   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33936     {
33937       pragma_omp_clause c_kind;
33938       const char *c_name;
33939       tree prev = clauses;
33940
33941       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33942         cp_lexer_consume_token (parser->lexer);
33943
33944       token = cp_lexer_peek_token (parser->lexer);
33945       c_kind = cp_parser_omp_clause_name (parser);
33946
33947       switch (c_kind)
33948         {
33949         case PRAGMA_OMP_CLAUSE_COLLAPSE:
33950           clauses = cp_parser_omp_clause_collapse (parser, clauses,
33951                                                    token->location);
33952           c_name = "collapse";
33953           break;
33954         case PRAGMA_OMP_CLAUSE_COPYIN:
33955           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33956           c_name = "copyin";
33957           break;
33958         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33959           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33960                                             clauses);
33961           c_name = "copyprivate";
33962           break;
33963         case PRAGMA_OMP_CLAUSE_DEFAULT:
33964           clauses = cp_parser_omp_clause_default (parser, clauses,
33965                                                   token->location, false);
33966           c_name = "default";
33967           break;
33968         case PRAGMA_OMP_CLAUSE_FINAL:
33969           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33970           c_name = "final";
33971           break;
33972         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33973           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33974                                             clauses);
33975           c_name = "firstprivate";
33976           break;
33977         case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33978           clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33979                                                     token->location);
33980           c_name = "grainsize";
33981           break;
33982         case PRAGMA_OMP_CLAUSE_HINT:
33983           clauses = cp_parser_omp_clause_hint (parser, clauses,
33984                                                token->location);
33985           c_name = "hint";
33986           break;
33987         case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33988           clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33989                                                      token->location);
33990           c_name = "defaultmap";
33991           break;
33992         case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33993           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33994                                             clauses);
33995           c_name = "use_device_ptr";
33996           break;
33997         case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33998           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33999                                             clauses);
34000           c_name = "is_device_ptr";
34001           break;
34002         case PRAGMA_OMP_CLAUSE_IF:
34003           clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34004                                              true);
34005           c_name = "if";
34006           break;
34007         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34008           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34009                                             clauses);
34010           c_name = "lastprivate";
34011           break;
34012         case PRAGMA_OMP_CLAUSE_MERGEABLE:
34013           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34014                                                     token->location);
34015           c_name = "mergeable";
34016           break;
34017         case PRAGMA_OMP_CLAUSE_NOWAIT:
34018           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34019           c_name = "nowait";
34020           break;
34021         case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34022           clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34023                                                     token->location);
34024           c_name = "num_tasks";
34025           break;
34026         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34027           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34028                                                       token->location);
34029           c_name = "num_threads";
34030           break;
34031         case PRAGMA_OMP_CLAUSE_ORDERED:
34032           clauses = cp_parser_omp_clause_ordered (parser, clauses,
34033                                                   token->location);
34034           c_name = "ordered";
34035           break;
34036         case PRAGMA_OMP_CLAUSE_PRIORITY:
34037           clauses = cp_parser_omp_clause_priority (parser, clauses,
34038                                                    token->location);
34039           c_name = "priority";
34040           break;
34041         case PRAGMA_OMP_CLAUSE_PRIVATE:
34042           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34043                                             clauses);
34044           c_name = "private";
34045           break;
34046         case PRAGMA_OMP_CLAUSE_REDUCTION:
34047           clauses = cp_parser_omp_clause_reduction (parser, clauses);
34048           c_name = "reduction";
34049           break;
34050         case PRAGMA_OMP_CLAUSE_SCHEDULE:
34051           clauses = cp_parser_omp_clause_schedule (parser, clauses,
34052                                                    token->location);
34053           c_name = "schedule";
34054           break;
34055         case PRAGMA_OMP_CLAUSE_SHARED:
34056           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34057                                             clauses);
34058           c_name = "shared";
34059           break;
34060         case PRAGMA_OMP_CLAUSE_UNTIED:
34061           clauses = cp_parser_omp_clause_untied (parser, clauses,
34062                                                  token->location);
34063           c_name = "untied";
34064           break;
34065         case PRAGMA_OMP_CLAUSE_INBRANCH:
34066           clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34067                                                  clauses, token->location);
34068           c_name = "inbranch";
34069           break;
34070         case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34071           clauses = cp_parser_omp_clause_branch (parser,
34072                                                  OMP_CLAUSE_NOTINBRANCH,
34073                                                  clauses, token->location);
34074           c_name = "notinbranch";
34075           break;
34076         case PRAGMA_OMP_CLAUSE_PARALLEL:
34077           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34078                                                      clauses, token->location);
34079           c_name = "parallel";
34080           if (!first)
34081             {
34082              clause_not_first:
34083               error_at (token->location, "%qs must be the first clause of %qs",
34084                         c_name, where);
34085               clauses = prev;
34086             }
34087           break;
34088         case PRAGMA_OMP_CLAUSE_FOR:
34089           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34090                                                      clauses, token->location);
34091           c_name = "for";
34092           if (!first)
34093             goto clause_not_first;
34094           break;
34095         case PRAGMA_OMP_CLAUSE_SECTIONS:
34096           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34097                                                      clauses, token->location);
34098           c_name = "sections";
34099           if (!first)
34100             goto clause_not_first;
34101           break;
34102         case PRAGMA_OMP_CLAUSE_TASKGROUP:
34103           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34104                                                      clauses, token->location);
34105           c_name = "taskgroup";
34106           if (!first)
34107             goto clause_not_first;
34108           break;
34109         case PRAGMA_OMP_CLAUSE_LINK:
34110           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34111           c_name = "to";
34112           break;
34113         case PRAGMA_OMP_CLAUSE_TO:
34114           if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34115             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34116                                               clauses);
34117           else
34118             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34119           c_name = "to";
34120           break;
34121         case PRAGMA_OMP_CLAUSE_FROM:
34122           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34123           c_name = "from";
34124           break;
34125         case PRAGMA_OMP_CLAUSE_UNIFORM:
34126           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34127                                             clauses);
34128           c_name = "uniform";
34129           break;
34130         case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34131           clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34132                                                     token->location);
34133           c_name = "num_teams";
34134           break;
34135         case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34136           clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34137                                                        token->location);
34138           c_name = "thread_limit";
34139           break;
34140         case PRAGMA_OMP_CLAUSE_ALIGNED:
34141           clauses = cp_parser_omp_clause_aligned (parser, clauses);
34142           c_name = "aligned";
34143           break;
34144         case PRAGMA_OMP_CLAUSE_LINEAR:
34145           {
34146             bool declare_simd = false;
34147             if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34148               declare_simd = true;
34149             clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34150           }
34151           c_name = "linear";
34152           break;
34153         case PRAGMA_OMP_CLAUSE_DEPEND:
34154           clauses = cp_parser_omp_clause_depend (parser, clauses,
34155                                                  token->location);
34156           c_name = "depend";
34157           break;
34158         case PRAGMA_OMP_CLAUSE_MAP:
34159           clauses = cp_parser_omp_clause_map (parser, clauses);
34160           c_name = "map";
34161           break;
34162         case PRAGMA_OMP_CLAUSE_DEVICE:
34163           clauses = cp_parser_omp_clause_device (parser, clauses,
34164                                                  token->location);
34165           c_name = "device";
34166           break;
34167         case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34168           clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34169                                                         token->location);
34170           c_name = "dist_schedule";
34171           break;
34172         case PRAGMA_OMP_CLAUSE_PROC_BIND:
34173           clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34174                                                     token->location);
34175           c_name = "proc_bind";
34176           break;
34177         case PRAGMA_OMP_CLAUSE_SAFELEN:
34178           clauses = cp_parser_omp_clause_safelen (parser, clauses,
34179                                                   token->location);
34180           c_name = "safelen";
34181           break;
34182         case PRAGMA_OMP_CLAUSE_SIMDLEN:
34183           clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34184                                                   token->location);
34185           c_name = "simdlen";
34186           break;
34187         case PRAGMA_OMP_CLAUSE_NOGROUP:
34188           clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34189                                                   token->location);
34190           c_name = "nogroup";
34191           break;
34192         case PRAGMA_OMP_CLAUSE_THREADS:
34193           clauses
34194             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34195                                                 clauses, token->location);
34196           c_name = "threads";
34197           break;
34198         case PRAGMA_OMP_CLAUSE_SIMD:
34199           clauses
34200             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34201                                                 clauses, token->location);
34202           c_name = "simd";
34203           break;
34204         default:
34205           cp_parser_error (parser, "expected %<#pragma omp%> clause");
34206           goto saw_error;
34207         }
34208
34209       first = false;
34210
34211       if (((mask >> c_kind) & 1) == 0)
34212         {
34213           /* Remove the invalid clause(s) from the list to avoid
34214              confusing the rest of the compiler.  */
34215           clauses = prev;
34216           error_at (token->location, "%qs is not valid for %qs", c_name, where);
34217         }
34218     }
34219  saw_error:
34220   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34221   if (finish_p)
34222     {
34223       if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34224         return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34225       else
34226         return finish_omp_clauses (clauses, C_ORT_OMP);
34227     }
34228   return clauses;
34229 }
34230
34231 /* OpenMP 2.5:
34232    structured-block:
34233      statement
34234
34235    In practice, we're also interested in adding the statement to an
34236    outer node.  So it is convenient if we work around the fact that
34237    cp_parser_statement calls add_stmt.  */
34238
34239 static unsigned
34240 cp_parser_begin_omp_structured_block (cp_parser *parser)
34241 {
34242   unsigned save = parser->in_statement;
34243
34244   /* Only move the values to IN_OMP_BLOCK if they weren't false.
34245      This preserves the "not within loop or switch" style error messages
34246      for nonsense cases like
34247         void foo() {
34248         #pragma omp single
34249           break;
34250         }
34251   */
34252   if (parser->in_statement)
34253     parser->in_statement = IN_OMP_BLOCK;
34254
34255   return save;
34256 }
34257
34258 static void
34259 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34260 {
34261   parser->in_statement = save;
34262 }
34263
34264 static tree
34265 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34266 {
34267   tree stmt = begin_omp_structured_block ();
34268   unsigned int save = cp_parser_begin_omp_structured_block (parser);
34269
34270   cp_parser_statement (parser, NULL_TREE, false, if_p);
34271
34272   cp_parser_end_omp_structured_block (parser, save);
34273   return finish_omp_structured_block (stmt);
34274 }
34275
34276 /* OpenMP 2.5:
34277    # pragma omp atomic new-line
34278      expression-stmt
34279
34280    expression-stmt:
34281      x binop= expr | x++ | ++x | x-- | --x
34282    binop:
34283      +, *, -, /, &, ^, |, <<, >>
34284
34285   where x is an lvalue expression with scalar type.
34286
34287    OpenMP 3.1:
34288    # pragma omp atomic new-line
34289      update-stmt
34290
34291    # pragma omp atomic read new-line
34292      read-stmt
34293
34294    # pragma omp atomic write new-line
34295      write-stmt
34296
34297    # pragma omp atomic update new-line
34298      update-stmt
34299
34300    # pragma omp atomic capture new-line
34301      capture-stmt
34302
34303    # pragma omp atomic capture new-line
34304      capture-block
34305
34306    read-stmt:
34307      v = x
34308    write-stmt:
34309      x = expr
34310    update-stmt:
34311      expression-stmt | x = x binop expr
34312    capture-stmt:
34313      v = expression-stmt
34314    capture-block:
34315      { v = x; update-stmt; } | { update-stmt; v = x; }
34316
34317    OpenMP 4.0:
34318    update-stmt:
34319      expression-stmt | x = x binop expr | x = expr binop x
34320    capture-stmt:
34321      v = update-stmt
34322    capture-block:
34323      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34324
34325   where x and v are lvalue expressions with scalar type.  */
34326
34327 static void
34328 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34329 {
34330   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34331   tree rhs1 = NULL_TREE, orig_lhs;
34332   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34333   bool structured_block = false;
34334   bool seq_cst = false;
34335
34336   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34337     {
34338       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34339       const char *p = IDENTIFIER_POINTER (id);
34340
34341       if (!strcmp (p, "seq_cst"))
34342         {
34343           seq_cst = true;
34344           cp_lexer_consume_token (parser->lexer);
34345           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34346               && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34347             cp_lexer_consume_token (parser->lexer);
34348         }
34349     }
34350   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34351     {
34352       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34353       const char *p = IDENTIFIER_POINTER (id);
34354
34355       if (!strcmp (p, "read"))
34356         code = OMP_ATOMIC_READ;
34357       else if (!strcmp (p, "write"))
34358         code = NOP_EXPR;
34359       else if (!strcmp (p, "update"))
34360         code = OMP_ATOMIC;
34361       else if (!strcmp (p, "capture"))
34362         code = OMP_ATOMIC_CAPTURE_NEW;
34363       else
34364         p = NULL;
34365       if (p)
34366         cp_lexer_consume_token (parser->lexer);
34367     }
34368   if (!seq_cst)
34369     {
34370       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34371           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34372         cp_lexer_consume_token (parser->lexer);
34373
34374       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34375         {
34376           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34377           const char *p = IDENTIFIER_POINTER (id);
34378
34379           if (!strcmp (p, "seq_cst"))
34380             {
34381               seq_cst = true;
34382               cp_lexer_consume_token (parser->lexer);
34383             }
34384         }
34385     }
34386   cp_parser_require_pragma_eol (parser, pragma_tok);
34387
34388   switch (code)
34389     {
34390     case OMP_ATOMIC_READ:
34391     case NOP_EXPR: /* atomic write */
34392       v = cp_parser_unary_expression (parser);
34393       if (v == error_mark_node)
34394         goto saw_error;
34395       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34396         goto saw_error;
34397       if (code == NOP_EXPR)
34398         lhs = cp_parser_expression (parser);
34399       else
34400         lhs = cp_parser_unary_expression (parser);
34401       if (lhs == error_mark_node)
34402         goto saw_error;
34403       if (code == NOP_EXPR)
34404         {
34405           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34406              opcode.  */
34407           code = OMP_ATOMIC;
34408           rhs = lhs;
34409           lhs = v;
34410           v = NULL_TREE;
34411         }
34412       goto done;
34413     case OMP_ATOMIC_CAPTURE_NEW:
34414       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34415         {
34416           cp_lexer_consume_token (parser->lexer);
34417           structured_block = true;
34418         }
34419       else
34420         {
34421           v = cp_parser_unary_expression (parser);
34422           if (v == error_mark_node)
34423             goto saw_error;
34424           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34425             goto saw_error;
34426         }
34427     default:
34428       break;
34429     }
34430
34431 restart:
34432   lhs = cp_parser_unary_expression (parser);
34433   orig_lhs = lhs;
34434   switch (TREE_CODE (lhs))
34435     {
34436     case ERROR_MARK:
34437       goto saw_error;
34438
34439     case POSTINCREMENT_EXPR:
34440       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34441         code = OMP_ATOMIC_CAPTURE_OLD;
34442       /* FALLTHROUGH */
34443     case PREINCREMENT_EXPR:
34444       lhs = TREE_OPERAND (lhs, 0);
34445       opcode = PLUS_EXPR;
34446       rhs = integer_one_node;
34447       break;
34448
34449     case POSTDECREMENT_EXPR:
34450       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34451         code = OMP_ATOMIC_CAPTURE_OLD;
34452       /* FALLTHROUGH */
34453     case PREDECREMENT_EXPR:
34454       lhs = TREE_OPERAND (lhs, 0);
34455       opcode = MINUS_EXPR;
34456       rhs = integer_one_node;
34457       break;
34458
34459     case COMPOUND_EXPR:
34460       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34461          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34462          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34463          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34464          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34465                                              (TREE_OPERAND (lhs, 1), 0), 0)))
34466             == BOOLEAN_TYPE)
34467        /* Undo effects of boolean_increment for post {in,de}crement.  */
34468        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34469       /* FALLTHRU */
34470     case MODIFY_EXPR:
34471       if (TREE_CODE (lhs) == MODIFY_EXPR
34472          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34473         {
34474           /* Undo effects of boolean_increment.  */
34475           if (integer_onep (TREE_OPERAND (lhs, 1)))
34476             {
34477               /* This is pre or post increment.  */
34478               rhs = TREE_OPERAND (lhs, 1);
34479               lhs = TREE_OPERAND (lhs, 0);
34480               opcode = NOP_EXPR;
34481               if (code == OMP_ATOMIC_CAPTURE_NEW
34482                   && !structured_block
34483                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34484                 code = OMP_ATOMIC_CAPTURE_OLD;
34485               break;
34486             }
34487         }
34488       /* FALLTHRU */
34489     default:
34490       switch (cp_lexer_peek_token (parser->lexer)->type)
34491         {
34492         case CPP_MULT_EQ:
34493           opcode = MULT_EXPR;
34494           break;
34495         case CPP_DIV_EQ:
34496           opcode = TRUNC_DIV_EXPR;
34497           break;
34498         case CPP_PLUS_EQ:
34499           opcode = PLUS_EXPR;
34500           break;
34501         case CPP_MINUS_EQ:
34502           opcode = MINUS_EXPR;
34503           break;
34504         case CPP_LSHIFT_EQ:
34505           opcode = LSHIFT_EXPR;
34506           break;
34507         case CPP_RSHIFT_EQ:
34508           opcode = RSHIFT_EXPR;
34509           break;
34510         case CPP_AND_EQ:
34511           opcode = BIT_AND_EXPR;
34512           break;
34513         case CPP_OR_EQ:
34514           opcode = BIT_IOR_EXPR;
34515           break;
34516         case CPP_XOR_EQ:
34517           opcode = BIT_XOR_EXPR;
34518           break;
34519         case CPP_EQ:
34520           enum cp_parser_prec oprec;
34521           cp_token *token;
34522           cp_lexer_consume_token (parser->lexer);
34523           cp_parser_parse_tentatively (parser);
34524           rhs1 = cp_parser_simple_cast_expression (parser);
34525           if (rhs1 == error_mark_node)
34526             {
34527               cp_parser_abort_tentative_parse (parser);
34528               cp_parser_simple_cast_expression (parser);
34529               goto saw_error;
34530             }
34531           token = cp_lexer_peek_token (parser->lexer);
34532           if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34533             {
34534               cp_parser_abort_tentative_parse (parser);
34535               cp_parser_parse_tentatively (parser);
34536               rhs = cp_parser_binary_expression (parser, false, true,
34537                                                  PREC_NOT_OPERATOR, NULL);
34538               if (rhs == error_mark_node)
34539                 {
34540                   cp_parser_abort_tentative_parse (parser);
34541                   cp_parser_binary_expression (parser, false, true,
34542                                                PREC_NOT_OPERATOR, NULL);
34543                   goto saw_error;
34544                 }
34545               switch (TREE_CODE (rhs))
34546                 {
34547                 case MULT_EXPR:
34548                 case TRUNC_DIV_EXPR:
34549                 case RDIV_EXPR:
34550                 case PLUS_EXPR:
34551                 case MINUS_EXPR:
34552                 case LSHIFT_EXPR:
34553                 case RSHIFT_EXPR:
34554                 case BIT_AND_EXPR:
34555                 case BIT_IOR_EXPR:
34556                 case BIT_XOR_EXPR:
34557                   if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34558                     {
34559                       if (cp_parser_parse_definitely (parser))
34560                         {
34561                           opcode = TREE_CODE (rhs);
34562                           rhs1 = TREE_OPERAND (rhs, 0);
34563                           rhs = TREE_OPERAND (rhs, 1);
34564                           goto stmt_done;
34565                         }
34566                       else
34567                         goto saw_error;
34568                     }
34569                   break;
34570                 default:
34571                   break;
34572                 }
34573               cp_parser_abort_tentative_parse (parser);
34574               if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34575                 {
34576                   rhs = cp_parser_expression (parser);
34577                   if (rhs == error_mark_node)
34578                     goto saw_error;
34579                   opcode = NOP_EXPR;
34580                   rhs1 = NULL_TREE;
34581                   goto stmt_done;
34582                 }
34583               cp_parser_error (parser,
34584                                "invalid form of %<#pragma omp atomic%>");
34585               goto saw_error;
34586             }
34587           if (!cp_parser_parse_definitely (parser))
34588             goto saw_error;
34589           switch (token->type)
34590             {
34591             case CPP_SEMICOLON:
34592               if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34593                 {
34594                   code = OMP_ATOMIC_CAPTURE_OLD;
34595                   v = lhs;
34596                   lhs = NULL_TREE;
34597                   lhs1 = rhs1;
34598                   rhs1 = NULL_TREE;
34599                   cp_lexer_consume_token (parser->lexer);
34600                   goto restart;
34601                 }
34602               else if (structured_block)
34603                 {
34604                   opcode = NOP_EXPR;
34605                   rhs = rhs1;
34606                   rhs1 = NULL_TREE;
34607                   goto stmt_done;
34608                 }
34609               cp_parser_error (parser,
34610                                "invalid form of %<#pragma omp atomic%>");
34611               goto saw_error;
34612             case CPP_MULT:
34613               opcode = MULT_EXPR;
34614               break;
34615             case CPP_DIV:
34616               opcode = TRUNC_DIV_EXPR;
34617               break;
34618             case CPP_PLUS:
34619               opcode = PLUS_EXPR;
34620               break;
34621             case CPP_MINUS:
34622               opcode = MINUS_EXPR;
34623               break;
34624             case CPP_LSHIFT:
34625               opcode = LSHIFT_EXPR;
34626               break;
34627             case CPP_RSHIFT:
34628               opcode = RSHIFT_EXPR;
34629               break;
34630             case CPP_AND:
34631               opcode = BIT_AND_EXPR;
34632               break;
34633             case CPP_OR:
34634               opcode = BIT_IOR_EXPR;
34635               break;
34636             case CPP_XOR:
34637               opcode = BIT_XOR_EXPR;
34638               break;
34639             default:
34640               cp_parser_error (parser,
34641                                "invalid operator for %<#pragma omp atomic%>");
34642               goto saw_error;
34643             }
34644           oprec = TOKEN_PRECEDENCE (token);
34645           gcc_assert (oprec != PREC_NOT_OPERATOR);
34646           if (commutative_tree_code (opcode))
34647             oprec = (enum cp_parser_prec) (oprec - 1);
34648           cp_lexer_consume_token (parser->lexer);
34649           rhs = cp_parser_binary_expression (parser, false, false,
34650                                              oprec, NULL);
34651           if (rhs == error_mark_node)
34652             goto saw_error;
34653           goto stmt_done;
34654           /* FALLTHROUGH */
34655         default:
34656           cp_parser_error (parser,
34657                            "invalid operator for %<#pragma omp atomic%>");
34658           goto saw_error;
34659         }
34660       cp_lexer_consume_token (parser->lexer);
34661
34662       rhs = cp_parser_expression (parser);
34663       if (rhs == error_mark_node)
34664         goto saw_error;
34665       break;
34666     }
34667 stmt_done:
34668   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34669     {
34670       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34671         goto saw_error;
34672       v = cp_parser_unary_expression (parser);
34673       if (v == error_mark_node)
34674         goto saw_error;
34675       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34676         goto saw_error;
34677       lhs1 = cp_parser_unary_expression (parser);
34678       if (lhs1 == error_mark_node)
34679         goto saw_error;
34680     }
34681   if (structured_block)
34682     {
34683       cp_parser_consume_semicolon_at_end_of_statement (parser);
34684       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34685     }
34686 done:
34687   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34688   if (!structured_block)
34689     cp_parser_consume_semicolon_at_end_of_statement (parser);
34690   return;
34691
34692  saw_error:
34693   cp_parser_skip_to_end_of_block_or_statement (parser);
34694   if (structured_block)
34695     {
34696       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34697         cp_lexer_consume_token (parser->lexer);
34698       else if (code == OMP_ATOMIC_CAPTURE_NEW)
34699         {
34700           cp_parser_skip_to_end_of_block_or_statement (parser);
34701           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34702             cp_lexer_consume_token (parser->lexer);
34703         }
34704     }
34705 }
34706
34707
34708 /* OpenMP 2.5:
34709    # pragma omp barrier new-line  */
34710
34711 static void
34712 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34713 {
34714   cp_parser_require_pragma_eol (parser, pragma_tok);
34715   finish_omp_barrier ();
34716 }
34717
34718 /* OpenMP 2.5:
34719    # pragma omp critical [(name)] new-line
34720      structured-block
34721
34722    OpenMP 4.5:
34723    # pragma omp critical [(name) [hint(expression)]] new-line
34724      structured-block  */
34725
34726 #define OMP_CRITICAL_CLAUSE_MASK                \
34727         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34728
34729 static tree
34730 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34731 {
34732   tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34733
34734   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34735     {
34736       matching_parens parens;
34737       parens.consume_open (parser);
34738
34739       name = cp_parser_identifier (parser);
34740
34741       if (name == error_mark_node
34742           || !parens.require_close (parser))
34743         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34744                                                /*or_comma=*/false,
34745                                                /*consume_paren=*/true);
34746       if (name == error_mark_node)
34747         name = NULL;
34748
34749       clauses = cp_parser_omp_all_clauses (parser,
34750                                            OMP_CRITICAL_CLAUSE_MASK,
34751                                            "#pragma omp critical", pragma_tok);
34752     }
34753   else
34754     cp_parser_require_pragma_eol (parser, pragma_tok);
34755
34756   stmt = cp_parser_omp_structured_block (parser, if_p);
34757   return c_finish_omp_critical (input_location, stmt, name, clauses);
34758 }
34759
34760 /* OpenMP 2.5:
34761    # pragma omp flush flush-vars[opt] new-line
34762
34763    flush-vars:
34764      ( variable-list ) */
34765
34766 static void
34767 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34768 {
34769   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34770     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34771   cp_parser_require_pragma_eol (parser, pragma_tok);
34772
34773   finish_omp_flush ();
34774 }
34775
34776 /* Helper function, to parse omp for increment expression.  */
34777
34778 static tree
34779 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34780 {
34781   tree cond = cp_parser_binary_expression (parser, false, true,
34782                                            PREC_NOT_OPERATOR, NULL);
34783   if (cond == error_mark_node
34784       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34785     {
34786       cp_parser_skip_to_end_of_statement (parser);
34787       return error_mark_node;
34788     }
34789
34790   switch (TREE_CODE (cond))
34791     {
34792     case GT_EXPR:
34793     case GE_EXPR:
34794     case LT_EXPR:
34795     case LE_EXPR:
34796       break;
34797     case NE_EXPR:
34798       /* Fall through: OpenMP disallows NE_EXPR.  */
34799       gcc_fallthrough ();
34800     default:
34801       return error_mark_node;
34802     }
34803
34804   /* If decl is an iterator, preserve LHS and RHS of the relational
34805      expr until finish_omp_for.  */
34806   if (decl
34807       && (type_dependent_expression_p (decl)
34808           || CLASS_TYPE_P (TREE_TYPE (decl))))
34809     return cond;
34810
34811   return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34812                             TREE_CODE (cond),
34813                             TREE_OPERAND (cond, 0), ERROR_MARK,
34814                             TREE_OPERAND (cond, 1), ERROR_MARK,
34815                             /*overload=*/NULL, tf_warning_or_error);
34816 }
34817
34818 /* Helper function, to parse omp for increment expression.  */
34819
34820 static tree
34821 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34822 {
34823   cp_token *token = cp_lexer_peek_token (parser->lexer);
34824   enum tree_code op;
34825   tree lhs, rhs;
34826   cp_id_kind idk;
34827   bool decl_first;
34828
34829   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34830     {
34831       op = (token->type == CPP_PLUS_PLUS
34832             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34833       cp_lexer_consume_token (parser->lexer);
34834       lhs = cp_parser_simple_cast_expression (parser);
34835       if (lhs != decl
34836           && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34837         return error_mark_node;
34838       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34839     }
34840
34841   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34842   if (lhs != decl
34843       && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34844     return error_mark_node;
34845
34846   token = cp_lexer_peek_token (parser->lexer);
34847   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34848     {
34849       op = (token->type == CPP_PLUS_PLUS
34850             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34851       cp_lexer_consume_token (parser->lexer);
34852       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34853     }
34854
34855   op = cp_parser_assignment_operator_opt (parser);
34856   if (op == ERROR_MARK)
34857     return error_mark_node;
34858
34859   if (op != NOP_EXPR)
34860     {
34861       rhs = cp_parser_assignment_expression (parser);
34862       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34863       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34864     }
34865
34866   lhs = cp_parser_binary_expression (parser, false, false,
34867                                      PREC_ADDITIVE_EXPRESSION, NULL);
34868   token = cp_lexer_peek_token (parser->lexer);
34869   decl_first = (lhs == decl
34870                 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34871   if (decl_first)
34872     lhs = NULL_TREE;
34873   if (token->type != CPP_PLUS
34874       && token->type != CPP_MINUS)
34875     return error_mark_node;
34876
34877   do
34878     {
34879       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34880       cp_lexer_consume_token (parser->lexer);
34881       rhs = cp_parser_binary_expression (parser, false, false,
34882                                          PREC_ADDITIVE_EXPRESSION, NULL);
34883       token = cp_lexer_peek_token (parser->lexer);
34884       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34885         {
34886           if (lhs == NULL_TREE)
34887             {
34888               if (op == PLUS_EXPR)
34889                 lhs = rhs;
34890               else
34891                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34892                                         tf_warning_or_error);
34893             }
34894           else
34895             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34896                                      ERROR_MARK, NULL, tf_warning_or_error);
34897         }
34898     }
34899   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34900
34901   if (!decl_first)
34902     {
34903       if ((rhs != decl
34904            && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34905           || op == MINUS_EXPR)
34906         return error_mark_node;
34907       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34908     }
34909   else
34910     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34911
34912   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34913 }
34914
34915 /* Parse the initialization statement of an OpenMP for loop.
34916
34917    Return true if the resulting construct should have an
34918    OMP_CLAUSE_PRIVATE added to it.  */
34919
34920 static tree
34921 cp_parser_omp_for_loop_init (cp_parser *parser,
34922                              tree &this_pre_body,
34923                              vec<tree, va_gc> *&for_block,
34924                              tree &init,
34925                              tree &orig_init,
34926                              tree &decl,
34927                              tree &real_decl)
34928 {
34929   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34930     return NULL_TREE;
34931
34932   tree add_private_clause = NULL_TREE;
34933
34934   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34935
34936      init-expr:
34937      var = lb
34938      integer-type var = lb
34939      random-access-iterator-type var = lb
34940      pointer-type var = lb
34941   */
34942   cp_decl_specifier_seq type_specifiers;
34943
34944   /* First, try to parse as an initialized declaration.  See
34945      cp_parser_condition, from whence the bulk of this is copied.  */
34946
34947   cp_parser_parse_tentatively (parser);
34948   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34949                                 /*is_trailing_return=*/false,
34950                                 &type_specifiers);
34951   if (cp_parser_parse_definitely (parser))
34952     {
34953       /* If parsing a type specifier seq succeeded, then this
34954          MUST be a initialized declaration.  */
34955       tree asm_specification, attributes;
34956       cp_declarator *declarator;
34957
34958       declarator = cp_parser_declarator (parser,
34959                                          CP_PARSER_DECLARATOR_NAMED,
34960                                          /*ctor_dtor_or_conv_p=*/NULL,
34961                                          /*parenthesized_p=*/NULL,
34962                                          /*member_p=*/false,
34963                                          /*friend_p=*/false);
34964       attributes = cp_parser_attributes_opt (parser);
34965       asm_specification = cp_parser_asm_specification_opt (parser);
34966
34967       if (declarator == cp_error_declarator) 
34968         cp_parser_skip_to_end_of_statement (parser);
34969
34970       else 
34971         {
34972           tree pushed_scope, auto_node;
34973
34974           decl = start_decl (declarator, &type_specifiers,
34975                              SD_INITIALIZED, attributes,
34976                              /*prefix_attributes=*/NULL_TREE,
34977                              &pushed_scope);
34978
34979           auto_node = type_uses_auto (TREE_TYPE (decl));
34980           if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34981             {
34982               if (cp_lexer_next_token_is (parser->lexer, 
34983                                           CPP_OPEN_PAREN))
34984                 error ("parenthesized initialization is not allowed in "
34985                        "OpenMP %<for%> loop");
34986               else
34987                 /* Trigger an error.  */
34988                 cp_parser_require (parser, CPP_EQ, RT_EQ);
34989
34990               init = error_mark_node;
34991               cp_parser_skip_to_end_of_statement (parser);
34992             }
34993           else if (CLASS_TYPE_P (TREE_TYPE (decl))
34994                    || type_dependent_expression_p (decl)
34995                    || auto_node)
34996             {
34997               bool is_direct_init, is_non_constant_init;
34998
34999               init = cp_parser_initializer (parser,
35000                                             &is_direct_init,
35001                                             &is_non_constant_init);
35002
35003               if (auto_node)
35004                 {
35005                   TREE_TYPE (decl)
35006                     = do_auto_deduction (TREE_TYPE (decl), init,
35007                                          auto_node);
35008
35009                   if (!CLASS_TYPE_P (TREE_TYPE (decl))
35010                       && !type_dependent_expression_p (decl))
35011                     goto non_class;
35012                 }
35013                       
35014               cp_finish_decl (decl, init, !is_non_constant_init,
35015                               asm_specification,
35016                               LOOKUP_ONLYCONVERTING);
35017               orig_init = init;
35018               if (CLASS_TYPE_P (TREE_TYPE (decl)))
35019                 {
35020                   vec_safe_push (for_block, this_pre_body);
35021                   init = NULL_TREE;
35022                 }
35023               else
35024                 {
35025                   init = pop_stmt_list (this_pre_body);
35026                   if (init && TREE_CODE (init) == STATEMENT_LIST)
35027                     {
35028                       tree_stmt_iterator i = tsi_start (init);
35029                       /* Move lambda DECL_EXPRs to FOR_BLOCK.  */
35030                       while (!tsi_end_p (i))
35031                         {
35032                           tree t = tsi_stmt (i);
35033                           if (TREE_CODE (t) == DECL_EXPR
35034                               && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35035                             {
35036                               tsi_delink (&i);
35037                               vec_safe_push (for_block, t);
35038                               continue;
35039                             }
35040                           break;
35041                         }
35042                       if (tsi_one_before_end_p (i))
35043                         {
35044                           tree t = tsi_stmt (i);
35045                           tsi_delink (&i);
35046                           free_stmt_list (init);
35047                           init = t;
35048                         }
35049                     }
35050                 }
35051               this_pre_body = NULL_TREE;
35052             }
35053           else
35054             {
35055               /* Consume '='.  */
35056               cp_lexer_consume_token (parser->lexer);
35057               init = cp_parser_assignment_expression (parser);
35058
35059             non_class:
35060               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
35061                 init = error_mark_node;
35062               else
35063                 cp_finish_decl (decl, NULL_TREE,
35064                                 /*init_const_expr_p=*/false,
35065                                 asm_specification,
35066                                 LOOKUP_ONLYCONVERTING);
35067             }
35068
35069           if (pushed_scope)
35070             pop_scope (pushed_scope);
35071         }
35072     }
35073   else 
35074     {
35075       cp_id_kind idk;
35076       /* If parsing a type specifier sequence failed, then
35077          this MUST be a simple expression.  */
35078       cp_parser_parse_tentatively (parser);
35079       decl = cp_parser_primary_expression (parser, false, false,
35080                                            false, &idk);
35081       cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35082       if (!cp_parser_error_occurred (parser)
35083           && decl
35084           && (TREE_CODE (decl) == COMPONENT_REF
35085               || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35086         {
35087           cp_parser_abort_tentative_parse (parser);
35088           cp_parser_parse_tentatively (parser);
35089           cp_token *token = cp_lexer_peek_token (parser->lexer);
35090           tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35091                                                /*check_dependency_p=*/true,
35092                                                /*template_p=*/NULL,
35093                                                /*declarator_p=*/false,
35094                                                /*optional_p=*/false);
35095           if (name != error_mark_node
35096               && last_tok == cp_lexer_peek_token (parser->lexer))
35097             {
35098               decl = cp_parser_lookup_name_simple (parser, name,
35099                                                    token->location);
35100               if (TREE_CODE (decl) == FIELD_DECL)
35101                 add_private_clause = omp_privatize_field (decl, false);
35102             }
35103           cp_parser_abort_tentative_parse (parser);
35104           cp_parser_parse_tentatively (parser);
35105           decl = cp_parser_primary_expression (parser, false, false,
35106                                                false, &idk);
35107         }
35108       if (!cp_parser_error_occurred (parser)
35109           && decl
35110           && DECL_P (decl)
35111           && CLASS_TYPE_P (TREE_TYPE (decl)))
35112         {
35113           tree rhs;
35114
35115           cp_parser_parse_definitely (parser);
35116           cp_parser_require (parser, CPP_EQ, RT_EQ);
35117           rhs = cp_parser_assignment_expression (parser);
35118           orig_init = rhs;
35119           finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35120                                                  decl, NOP_EXPR,
35121                                                  rhs,
35122                                                  tf_warning_or_error));
35123           if (!add_private_clause)
35124             add_private_clause = decl;
35125         }
35126       else
35127         {
35128           decl = NULL;
35129           cp_parser_abort_tentative_parse (parser);
35130           init = cp_parser_expression (parser);
35131           if (init)
35132             {
35133               if (TREE_CODE (init) == MODIFY_EXPR
35134                   || TREE_CODE (init) == MODOP_EXPR)
35135                 real_decl = TREE_OPERAND (init, 0);
35136             }
35137         }
35138     }
35139   return add_private_clause;
35140 }
35141
35142 /* Parse the restricted form of the for statement allowed by OpenMP.  */
35143
35144 static tree
35145 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35146                         tree *cclauses, bool *if_p)
35147 {
35148   tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35149   tree real_decl, initv, condv, incrv, declv;
35150   tree this_pre_body, cl, ordered_cl = NULL_TREE;
35151   location_t loc_first;
35152   bool collapse_err = false;
35153   int i, collapse = 1, ordered = 0, count, nbraces = 0;
35154   vec<tree, va_gc> *for_block = make_tree_vector ();
35155   auto_vec<tree, 4> orig_inits;
35156   bool tiling = false;
35157
35158   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35159     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35160       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35161     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35162       {
35163         tiling = true;
35164         collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35165       }
35166     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35167              && OMP_CLAUSE_ORDERED_EXPR (cl))
35168       {
35169         ordered_cl = cl;
35170         ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35171       }
35172
35173   if (ordered && ordered < collapse)
35174     {
35175       error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35176                 "%<ordered%> clause parameter is less than %<collapse%>");
35177       OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35178         = build_int_cst (NULL_TREE, collapse);
35179       ordered = collapse;
35180     }
35181   if (ordered)
35182     {
35183       for (tree *pc = &clauses; *pc; )
35184         if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35185           {
35186             error_at (OMP_CLAUSE_LOCATION (*pc),
35187                       "%<linear%> clause may not be specified together "
35188                       "with %<ordered%> clause with a parameter");
35189             *pc = OMP_CLAUSE_CHAIN (*pc);
35190           }
35191         else
35192           pc = &OMP_CLAUSE_CHAIN (*pc);
35193     }
35194
35195   gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35196   count = ordered ? ordered : collapse;
35197
35198   declv = make_tree_vec (count);
35199   initv = make_tree_vec (count);
35200   condv = make_tree_vec (count);
35201   incrv = make_tree_vec (count);
35202
35203   loc_first = cp_lexer_peek_token (parser->lexer)->location;
35204
35205   for (i = 0; i < count; i++)
35206     {
35207       int bracecount = 0;
35208       tree add_private_clause = NULL_TREE;
35209       location_t loc;
35210
35211       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35212         {
35213           if (!collapse_err)
35214             cp_parser_error (parser, "for statement expected");
35215           return NULL;
35216         }
35217       loc = cp_lexer_consume_token (parser->lexer)->location;
35218
35219       matching_parens parens;
35220       if (!parens.require_open (parser))
35221         return NULL;
35222
35223       init = orig_init = decl = real_decl = NULL;
35224       this_pre_body = push_stmt_list ();
35225
35226       add_private_clause
35227         = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35228                                        init, orig_init, decl, real_decl);
35229
35230       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35231       if (this_pre_body)
35232         {
35233           this_pre_body = pop_stmt_list (this_pre_body);
35234           if (pre_body)
35235             {
35236               tree t = pre_body;
35237               pre_body = push_stmt_list ();
35238               add_stmt (t);
35239               add_stmt (this_pre_body);
35240               pre_body = pop_stmt_list (pre_body);
35241             }
35242           else
35243             pre_body = this_pre_body;
35244         }
35245
35246       if (decl)
35247         real_decl = decl;
35248       if (cclauses != NULL
35249           && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35250           && real_decl != NULL_TREE)
35251         {
35252           tree *c;
35253           for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35254             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35255                 && OMP_CLAUSE_DECL (*c) == real_decl)
35256               {
35257                 error_at (loc, "iteration variable %qD"
35258                           " should not be firstprivate", real_decl);
35259                 *c = OMP_CLAUSE_CHAIN (*c);
35260               }
35261             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35262                      && OMP_CLAUSE_DECL (*c) == real_decl)
35263               {
35264                 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
35265                 tree l = *c;
35266                 *c = OMP_CLAUSE_CHAIN (*c);
35267                 if (code == OMP_SIMD)
35268                   {
35269                     OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35270                     cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35271                   }
35272                 else
35273                   {
35274                     OMP_CLAUSE_CHAIN (l) = clauses;
35275                     clauses = l;
35276                   }
35277                 add_private_clause = NULL_TREE;
35278               }
35279             else
35280               {
35281                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35282                     && OMP_CLAUSE_DECL (*c) == real_decl)
35283                   add_private_clause = NULL_TREE;
35284                 c = &OMP_CLAUSE_CHAIN (*c);
35285               }
35286         }
35287
35288       if (add_private_clause)
35289         {
35290           tree c;
35291           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35292             {
35293               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35294                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35295                   && OMP_CLAUSE_DECL (c) == decl)
35296                 break;
35297               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35298                        && OMP_CLAUSE_DECL (c) == decl)
35299                 error_at (loc, "iteration variable %qD "
35300                           "should not be firstprivate",
35301                           decl);
35302               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35303                        && OMP_CLAUSE_DECL (c) == decl)
35304                 error_at (loc, "iteration variable %qD should not be reduction",
35305                           decl);
35306             }
35307           if (c == NULL)
35308             {
35309               if (code != OMP_SIMD)
35310                 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35311               else if (collapse == 1)
35312                 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35313               else
35314                 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35315               OMP_CLAUSE_DECL (c) = add_private_clause;
35316               c = finish_omp_clauses (c, C_ORT_OMP);
35317               if (c)
35318                 {
35319                   OMP_CLAUSE_CHAIN (c) = clauses;
35320                   clauses = c;
35321                   /* For linear, signal that we need to fill up
35322                      the so far unknown linear step.  */
35323                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35324                     OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35325                 }
35326             }
35327         }
35328
35329       cond = NULL;
35330       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35331         cond = cp_parser_omp_for_cond (parser, decl);
35332       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35333
35334       incr = NULL;
35335       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35336         {
35337           /* If decl is an iterator, preserve the operator on decl
35338              until finish_omp_for.  */
35339           if (real_decl
35340               && ((processing_template_decl
35341                    && (TREE_TYPE (real_decl) == NULL_TREE
35342                        || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35343                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35344             incr = cp_parser_omp_for_incr (parser, real_decl);
35345           else
35346             incr = cp_parser_expression (parser);
35347           if (!EXPR_HAS_LOCATION (incr))
35348             protected_set_expr_location (incr, input_location);
35349         }
35350
35351       if (!parens.require_close (parser))
35352         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35353                                                /*or_comma=*/false,
35354                                                /*consume_paren=*/true);
35355
35356       TREE_VEC_ELT (declv, i) = decl;
35357       TREE_VEC_ELT (initv, i) = init;
35358       TREE_VEC_ELT (condv, i) = cond;
35359       TREE_VEC_ELT (incrv, i) = incr;
35360       if (orig_init)
35361         {
35362           orig_inits.safe_grow_cleared (i + 1);
35363           orig_inits[i] = orig_init;
35364         }
35365
35366       if (i == count - 1)
35367         break;
35368
35369       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35370          in between the collapsed for loops to be still considered perfectly
35371          nested.  Hopefully the final version clarifies this.
35372          For now handle (multiple) {'s and empty statements.  */
35373       cp_parser_parse_tentatively (parser);
35374       for (;;)
35375         {
35376           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35377             break;
35378           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35379             {
35380               cp_lexer_consume_token (parser->lexer);
35381               bracecount++;
35382             }
35383           else if (bracecount
35384                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35385             cp_lexer_consume_token (parser->lexer);
35386           else
35387             {
35388               loc = cp_lexer_peek_token (parser->lexer)->location;
35389               error_at (loc, "not enough for loops to collapse");
35390               collapse_err = true;
35391               cp_parser_abort_tentative_parse (parser);
35392               declv = NULL_TREE;
35393               break;
35394             }
35395         }
35396
35397       if (declv)
35398         {
35399           cp_parser_parse_definitely (parser);
35400           nbraces += bracecount;
35401         }
35402     }
35403
35404   if (nbraces)
35405     if_p = NULL;
35406
35407   /* Note that we saved the original contents of this flag when we entered
35408      the structured block, and so we don't need to re-save it here.  */
35409   parser->in_statement = IN_OMP_FOR;
35410
35411   /* Note that the grammar doesn't call for a structured block here,
35412      though the loop as a whole is a structured block.  */
35413   body = push_stmt_list ();
35414   cp_parser_statement (parser, NULL_TREE, false, if_p);
35415   body = pop_stmt_list (body);
35416
35417   if (declv == NULL_TREE)
35418     ret = NULL_TREE;
35419   else
35420     ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35421                           body, pre_body, &orig_inits, clauses);
35422
35423   while (nbraces)
35424     {
35425       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35426         {
35427           cp_lexer_consume_token (parser->lexer);
35428           nbraces--;
35429         }
35430       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35431         cp_lexer_consume_token (parser->lexer);
35432       else
35433         {
35434           if (!collapse_err)
35435             {
35436               error_at (cp_lexer_peek_token (parser->lexer)->location,
35437                         "collapsed loops not perfectly nested");
35438             }
35439           collapse_err = true;
35440           cp_parser_statement_seq_opt (parser, NULL);
35441           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35442             break;
35443         }
35444     }
35445
35446   while (!for_block->is_empty ())
35447     {
35448       tree t = for_block->pop ();
35449       if (TREE_CODE (t) == STATEMENT_LIST)
35450         add_stmt (pop_stmt_list (t));
35451       else
35452         add_stmt (t);
35453     }
35454   release_tree_vector (for_block);
35455
35456   return ret;
35457 }
35458
35459 /* Helper function for OpenMP parsing, split clauses and call
35460    finish_omp_clauses on each of the set of clauses afterwards.  */
35461
35462 static void
35463 cp_omp_split_clauses (location_t loc, enum tree_code code,
35464                       omp_clause_mask mask, tree clauses, tree *cclauses)
35465 {
35466   int i;
35467   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35468   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35469     if (cclauses[i])
35470       cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35471 }
35472
35473 /* OpenMP 4.0:
35474    #pragma omp simd simd-clause[optseq] new-line
35475      for-loop  */
35476
35477 #define OMP_SIMD_CLAUSE_MASK                                    \
35478         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)      \
35479         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
35480         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
35481         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
35482         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35483         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
35484         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
35485         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35486
35487 static tree
35488 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35489                     char *p_name, omp_clause_mask mask, tree *cclauses,
35490                     bool *if_p)
35491 {
35492   tree clauses, sb, ret;
35493   unsigned int save;
35494   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35495
35496   strcat (p_name, " simd");
35497   mask |= OMP_SIMD_CLAUSE_MASK;
35498
35499   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35500                                        cclauses == NULL);
35501   if (cclauses)
35502     {
35503       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35504       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35505       tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35506                                 OMP_CLAUSE_ORDERED);
35507       if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35508         {
35509           error_at (OMP_CLAUSE_LOCATION (c),
35510                     "%<ordered%> clause with parameter may not be specified "
35511                     "on %qs construct", p_name);
35512           OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35513         }
35514     }
35515
35516   sb = begin_omp_structured_block ();
35517   save = cp_parser_begin_omp_structured_block (parser);
35518
35519   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35520
35521   cp_parser_end_omp_structured_block (parser, save);
35522   add_stmt (finish_omp_structured_block (sb));
35523
35524   return ret;
35525 }
35526
35527 /* OpenMP 2.5:
35528    #pragma omp for for-clause[optseq] new-line
35529      for-loop
35530
35531    OpenMP 4.0:
35532    #pragma omp for simd for-simd-clause[optseq] new-line
35533      for-loop  */
35534
35535 #define OMP_FOR_CLAUSE_MASK                                     \
35536         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35537         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35538         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
35539         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
35540         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
35541         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)      \
35542         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)     \
35543         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
35544         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35545
35546 static tree
35547 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35548                    char *p_name, omp_clause_mask mask, tree *cclauses,
35549                    bool *if_p)
35550 {
35551   tree clauses, sb, ret;
35552   unsigned int save;
35553   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35554
35555   strcat (p_name, " for");
35556   mask |= OMP_FOR_CLAUSE_MASK;
35557   /* parallel for{, simd} disallows nowait clause, but for
35558      target {teams distribute ,}parallel for{, simd} it should be accepted.  */
35559   if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35560     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35561   /* Composite distribute parallel for{, simd} disallows ordered clause.  */
35562   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35563     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35564
35565   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35566     {
35567       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35568       const char *p = IDENTIFIER_POINTER (id);
35569
35570       if (strcmp (p, "simd") == 0)
35571         {
35572           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35573           if (cclauses == NULL)
35574             cclauses = cclauses_buf;
35575
35576           cp_lexer_consume_token (parser->lexer);
35577           if (!flag_openmp)  /* flag_openmp_simd  */
35578             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35579                                        cclauses, if_p);
35580           sb = begin_omp_structured_block ();
35581           save = cp_parser_begin_omp_structured_block (parser);
35582           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35583                                     cclauses, if_p);
35584           cp_parser_end_omp_structured_block (parser, save);
35585           tree body = finish_omp_structured_block (sb);
35586           if (ret == NULL)
35587             return ret;
35588           ret = make_node (OMP_FOR);
35589           TREE_TYPE (ret) = void_type_node;
35590           OMP_FOR_BODY (ret) = body;
35591           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35592           SET_EXPR_LOCATION (ret, loc);
35593           add_stmt (ret);
35594           return ret;
35595         }
35596     }
35597   if (!flag_openmp)  /* flag_openmp_simd  */
35598     {
35599       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35600       return NULL_TREE;
35601     }
35602
35603   /* Composite distribute parallel for disallows linear clause.  */
35604   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35605     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35606
35607   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35608                                        cclauses == NULL);
35609   if (cclauses)
35610     {
35611       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35612       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35613     }
35614
35615   sb = begin_omp_structured_block ();
35616   save = cp_parser_begin_omp_structured_block (parser);
35617
35618   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35619
35620   cp_parser_end_omp_structured_block (parser, save);
35621   add_stmt (finish_omp_structured_block (sb));
35622
35623   return ret;
35624 }
35625
35626 /* OpenMP 2.5:
35627    # pragma omp master new-line
35628      structured-block  */
35629
35630 static tree
35631 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35632 {
35633   cp_parser_require_pragma_eol (parser, pragma_tok);
35634   return c_finish_omp_master (input_location,
35635                               cp_parser_omp_structured_block (parser, if_p));
35636 }
35637
35638 /* OpenMP 2.5:
35639    # pragma omp ordered new-line
35640      structured-block
35641
35642    OpenMP 4.5:
35643    # pragma omp ordered ordered-clauses new-line
35644      structured-block  */
35645
35646 #define OMP_ORDERED_CLAUSE_MASK                                 \
35647         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)      \
35648         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35649
35650 #define OMP_ORDERED_DEPEND_CLAUSE_MASK                          \
35651         (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35652
35653 static bool
35654 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35655                        enum pragma_context context, bool *if_p)
35656 {
35657   location_t loc = pragma_tok->location;
35658
35659   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35660     {
35661       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35662       const char *p = IDENTIFIER_POINTER (id);
35663
35664       if (strcmp (p, "depend") == 0)
35665         {
35666           if (!flag_openmp)     /* flag_openmp_simd */
35667             {
35668               cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35669               return false;
35670             }
35671           if (context == pragma_stmt)
35672             {
35673               error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35674                         "%<depend%> clause may only be used in compound "
35675                         "statements");
35676               cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35677               return false;
35678             }
35679           tree clauses
35680             = cp_parser_omp_all_clauses (parser,
35681                                          OMP_ORDERED_DEPEND_CLAUSE_MASK,
35682                                          "#pragma omp ordered", pragma_tok);
35683           c_finish_omp_ordered (loc, clauses, NULL_TREE);
35684           return false;
35685         }
35686     }
35687
35688   tree clauses
35689     = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35690                                  "#pragma omp ordered", pragma_tok);
35691
35692   if (!flag_openmp     /* flag_openmp_simd  */
35693       && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35694     return false;
35695
35696   c_finish_omp_ordered (loc, clauses,
35697                         cp_parser_omp_structured_block (parser, if_p));
35698   return true;
35699 }
35700
35701 /* OpenMP 2.5:
35702
35703    section-scope:
35704      { section-sequence }
35705
35706    section-sequence:
35707      section-directive[opt] structured-block
35708      section-sequence section-directive structured-block  */
35709
35710 static tree
35711 cp_parser_omp_sections_scope (cp_parser *parser)
35712 {
35713   tree stmt, substmt;
35714   bool error_suppress = false;
35715   cp_token *tok;
35716
35717   matching_braces braces;
35718   if (!braces.require_open (parser))
35719     return NULL_TREE;
35720
35721   stmt = push_stmt_list ();
35722
35723   if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35724       != PRAGMA_OMP_SECTION)
35725     {
35726       substmt = cp_parser_omp_structured_block (parser, NULL);
35727       substmt = build1 (OMP_SECTION, void_type_node, substmt);
35728       add_stmt (substmt);
35729     }
35730
35731   while (1)
35732     {
35733       tok = cp_lexer_peek_token (parser->lexer);
35734       if (tok->type == CPP_CLOSE_BRACE)
35735         break;
35736       if (tok->type == CPP_EOF)
35737         break;
35738
35739       if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35740         {
35741           cp_lexer_consume_token (parser->lexer);
35742           cp_parser_require_pragma_eol (parser, tok);
35743           error_suppress = false;
35744         }
35745       else if (!error_suppress)
35746         {
35747           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35748           error_suppress = true;
35749         }
35750
35751       substmt = cp_parser_omp_structured_block (parser, NULL);
35752       substmt = build1 (OMP_SECTION, void_type_node, substmt);
35753       add_stmt (substmt);
35754     }
35755   braces.require_close (parser);
35756
35757   substmt = pop_stmt_list (stmt);
35758
35759   stmt = make_node (OMP_SECTIONS);
35760   TREE_TYPE (stmt) = void_type_node;
35761   OMP_SECTIONS_BODY (stmt) = substmt;
35762
35763   add_stmt (stmt);
35764   return stmt;
35765 }
35766
35767 /* OpenMP 2.5:
35768    # pragma omp sections sections-clause[optseq] newline
35769      sections-scope  */
35770
35771 #define OMP_SECTIONS_CLAUSE_MASK                                \
35772         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35773         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35774         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
35775         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
35776         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35777
35778 static tree
35779 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35780                         char *p_name, omp_clause_mask mask, tree *cclauses)
35781 {
35782   tree clauses, ret;
35783   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35784
35785   strcat (p_name, " sections");
35786   mask |= OMP_SECTIONS_CLAUSE_MASK;
35787   if (cclauses)
35788     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35789
35790   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35791                                        cclauses == NULL);
35792   if (cclauses)
35793     {
35794       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35795       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35796     }
35797
35798   ret = cp_parser_omp_sections_scope (parser);
35799   if (ret)
35800     OMP_SECTIONS_CLAUSES (ret) = clauses;
35801
35802   return ret;
35803 }
35804
35805 /* OpenMP 2.5:
35806    # pragma omp parallel parallel-clause[optseq] new-line
35807      structured-block
35808    # pragma omp parallel for parallel-for-clause[optseq] new-line
35809      structured-block
35810    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35811      structured-block
35812
35813    OpenMP 4.0:
35814    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35815      structured-block */
35816
35817 #define OMP_PARALLEL_CLAUSE_MASK                                \
35818         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
35819         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35820         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35821         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
35822         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
35823         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)       \
35824         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
35825         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)  \
35826         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35827
35828 static tree
35829 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35830                         char *p_name, omp_clause_mask mask, tree *cclauses,
35831                         bool *if_p)
35832 {
35833   tree stmt, clauses, block;
35834   unsigned int save;
35835   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35836
35837   strcat (p_name, " parallel");
35838   mask |= OMP_PARALLEL_CLAUSE_MASK;
35839   /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
35840   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35841       && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35842     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35843
35844   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35845     {
35846       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35847       if (cclauses == NULL)
35848         cclauses = cclauses_buf;
35849
35850       cp_lexer_consume_token (parser->lexer);
35851       if (!flag_openmp)  /* flag_openmp_simd  */
35852         return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35853                                   if_p);
35854       block = begin_omp_parallel ();
35855       save = cp_parser_begin_omp_structured_block (parser);
35856       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35857                                     if_p);
35858       cp_parser_end_omp_structured_block (parser, save);
35859       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35860                                   block);
35861       if (ret == NULL_TREE)
35862         return ret;
35863       OMP_PARALLEL_COMBINED (stmt) = 1;
35864       return stmt;
35865     }
35866   /* When combined with distribute, parallel has to be followed by for.
35867      #pragma omp target parallel is allowed though.  */
35868   else if (cclauses
35869            && (mask & (OMP_CLAUSE_MASK_1
35870                        << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35871     {
35872       error_at (loc, "expected %<for%> after %qs", p_name);
35873       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35874       return NULL_TREE;
35875     }
35876   else if (!flag_openmp)  /* flag_openmp_simd  */
35877     {
35878       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35879       return NULL_TREE;
35880     }
35881   else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35882     {
35883       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35884       const char *p = IDENTIFIER_POINTER (id);
35885       if (strcmp (p, "sections") == 0)
35886         {
35887           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35888           cclauses = cclauses_buf;
35889
35890           cp_lexer_consume_token (parser->lexer);
35891           block = begin_omp_parallel ();
35892           save = cp_parser_begin_omp_structured_block (parser);
35893           cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35894           cp_parser_end_omp_structured_block (parser, save);
35895           stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35896                                       block);
35897           OMP_PARALLEL_COMBINED (stmt) = 1;
35898           return stmt;
35899         }
35900     }
35901
35902   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35903                                        cclauses == NULL);
35904   if (cclauses)
35905     {
35906       cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35907       clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35908     }
35909
35910   block = begin_omp_parallel ();
35911   save = cp_parser_begin_omp_structured_block (parser);
35912   cp_parser_statement (parser, NULL_TREE, false, if_p);
35913   cp_parser_end_omp_structured_block (parser, save);
35914   stmt = finish_omp_parallel (clauses, block);
35915   return stmt;
35916 }
35917
35918 /* OpenMP 2.5:
35919    # pragma omp single single-clause[optseq] new-line
35920      structured-block  */
35921
35922 #define OMP_SINGLE_CLAUSE_MASK                                  \
35923         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35924         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35925         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)  \
35926         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35927
35928 static tree
35929 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35930 {
35931   tree stmt = make_node (OMP_SINGLE);
35932   TREE_TYPE (stmt) = void_type_node;
35933
35934   OMP_SINGLE_CLAUSES (stmt)
35935     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35936                                  "#pragma omp single", pragma_tok);
35937   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35938
35939   return add_stmt (stmt);
35940 }
35941
35942 /* OpenMP 3.0:
35943    # pragma omp task task-clause[optseq] new-line
35944      structured-block  */
35945
35946 #define OMP_TASK_CLAUSE_MASK                                    \
35947         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
35948         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
35949         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
35950         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35951         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35952         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
35953         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
35954         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
35955         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
35956         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35957
35958 static tree
35959 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35960 {
35961   tree clauses, block;
35962   unsigned int save;
35963
35964   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35965                                        "#pragma omp task", pragma_tok);
35966   block = begin_omp_task ();
35967   save = cp_parser_begin_omp_structured_block (parser);
35968   cp_parser_statement (parser, NULL_TREE, false, if_p);
35969   cp_parser_end_omp_structured_block (parser, save);
35970   return finish_omp_task (clauses, block);
35971 }
35972
35973 /* OpenMP 3.0:
35974    # pragma omp taskwait new-line  */
35975
35976 static void
35977 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35978 {
35979   cp_parser_require_pragma_eol (parser, pragma_tok);
35980   finish_omp_taskwait ();
35981 }
35982
35983 /* OpenMP 3.1:
35984    # pragma omp taskyield new-line  */
35985
35986 static void
35987 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35988 {
35989   cp_parser_require_pragma_eol (parser, pragma_tok);
35990   finish_omp_taskyield ();
35991 }
35992
35993 /* OpenMP 4.0:
35994    # pragma omp taskgroup new-line
35995      structured-block  */
35996
35997 static tree
35998 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35999 {
36000   cp_parser_require_pragma_eol (parser, pragma_tok);
36001   return c_finish_omp_taskgroup (input_location,
36002                                  cp_parser_omp_structured_block (parser,
36003                                                                  if_p));
36004 }
36005
36006
36007 /* OpenMP 2.5:
36008    # pragma omp threadprivate (variable-list) */
36009
36010 static void
36011 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36012 {
36013   tree vars;
36014
36015   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36016   cp_parser_require_pragma_eol (parser, pragma_tok);
36017
36018   finish_omp_threadprivate (vars);
36019 }
36020
36021 /* OpenMP 4.0:
36022    # pragma omp cancel cancel-clause[optseq] new-line  */
36023
36024 #define OMP_CANCEL_CLAUSE_MASK                                  \
36025         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
36026         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
36027         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
36028         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)    \
36029         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36030
36031 static void
36032 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36033 {
36034   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36035                                             "#pragma omp cancel", pragma_tok);
36036   finish_omp_cancel (clauses);
36037 }
36038
36039 /* OpenMP 4.0:
36040    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
36041
36042 #define OMP_CANCELLATION_POINT_CLAUSE_MASK                      \
36043         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
36044         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
36045         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
36046         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36047
36048 static void
36049 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36050                                   enum pragma_context context)
36051 {
36052   tree clauses;
36053   bool point_seen = false;
36054
36055   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36056     {
36057       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36058       const char *p = IDENTIFIER_POINTER (id);
36059
36060       if (strcmp (p, "point") == 0)
36061         {
36062           cp_lexer_consume_token (parser->lexer);
36063           point_seen = true;
36064         }
36065     }
36066   if (!point_seen)
36067     {
36068       cp_parser_error (parser, "expected %<point%>");
36069       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36070       return;
36071     }
36072
36073   if (context != pragma_compound)
36074     {
36075       if (context == pragma_stmt)
36076         error_at (pragma_tok->location,
36077                   "%<#pragma %s%> may only be used in compound statements",
36078                   "omp cancellation point");
36079       else
36080         cp_parser_error (parser, "expected declaration specifiers");
36081       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36082       return;
36083     }
36084
36085   clauses = cp_parser_omp_all_clauses (parser,
36086                                        OMP_CANCELLATION_POINT_CLAUSE_MASK,
36087                                        "#pragma omp cancellation point",
36088                                        pragma_tok);
36089   finish_omp_cancellation_point (clauses);
36090 }
36091
36092 /* OpenMP 4.0:
36093    #pragma omp distribute distribute-clause[optseq] new-line
36094      for-loop  */
36095
36096 #define OMP_DISTRIBUTE_CLAUSE_MASK                              \
36097         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
36098         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36099         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
36100         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36101         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36102
36103 static tree
36104 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36105                           char *p_name, omp_clause_mask mask, tree *cclauses,
36106                           bool *if_p)
36107 {
36108   tree clauses, sb, ret;
36109   unsigned int save;
36110   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36111
36112   strcat (p_name, " distribute");
36113   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36114
36115   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36116     {
36117       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36118       const char *p = IDENTIFIER_POINTER (id);
36119       bool simd = false;
36120       bool parallel = false;
36121
36122       if (strcmp (p, "simd") == 0)
36123         simd = true;
36124       else
36125         parallel = strcmp (p, "parallel") == 0;
36126       if (parallel || simd)
36127         {
36128           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36129           if (cclauses == NULL)
36130             cclauses = cclauses_buf;
36131           cp_lexer_consume_token (parser->lexer);
36132           if (!flag_openmp)  /* flag_openmp_simd  */
36133             {
36134               if (simd)
36135                 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36136                                            cclauses, if_p);
36137               else
36138                 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36139                                                cclauses, if_p);
36140             }
36141           sb = begin_omp_structured_block ();
36142           save = cp_parser_begin_omp_structured_block (parser);
36143           if (simd)
36144             ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36145                                       cclauses, if_p);
36146           else
36147             ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36148                                           cclauses, if_p);
36149           cp_parser_end_omp_structured_block (parser, save);
36150           tree body = finish_omp_structured_block (sb);
36151           if (ret == NULL)
36152             return ret;
36153           ret = make_node (OMP_DISTRIBUTE);
36154           TREE_TYPE (ret) = void_type_node;
36155           OMP_FOR_BODY (ret) = body;
36156           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36157           SET_EXPR_LOCATION (ret, loc);
36158           add_stmt (ret);
36159           return ret;
36160         }
36161     }
36162   if (!flag_openmp)  /* flag_openmp_simd  */
36163     {
36164       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36165       return NULL_TREE;
36166     }
36167
36168   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36169                                        cclauses == NULL);
36170   if (cclauses)
36171     {
36172       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36173       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36174     }
36175
36176   sb = begin_omp_structured_block ();
36177   save = cp_parser_begin_omp_structured_block (parser);
36178
36179   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36180
36181   cp_parser_end_omp_structured_block (parser, save);
36182   add_stmt (finish_omp_structured_block (sb));
36183
36184   return ret;
36185 }
36186
36187 /* OpenMP 4.0:
36188    # pragma omp teams teams-clause[optseq] new-line
36189      structured-block  */
36190
36191 #define OMP_TEAMS_CLAUSE_MASK                                   \
36192         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
36193         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36194         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
36195         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
36196         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)    \
36197         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36198         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36199
36200 static tree
36201 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36202                      char *p_name, omp_clause_mask mask, tree *cclauses,
36203                      bool *if_p)
36204 {
36205   tree clauses, sb, ret;
36206   unsigned int save;
36207   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36208
36209   strcat (p_name, " teams");
36210   mask |= OMP_TEAMS_CLAUSE_MASK;
36211
36212   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36213     {
36214       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36215       const char *p = IDENTIFIER_POINTER (id);
36216       if (strcmp (p, "distribute") == 0)
36217         {
36218           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36219           if (cclauses == NULL)
36220             cclauses = cclauses_buf;
36221
36222           cp_lexer_consume_token (parser->lexer);
36223           if (!flag_openmp)  /* flag_openmp_simd  */
36224             return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36225                                              cclauses, if_p);
36226           sb = begin_omp_structured_block ();
36227           save = cp_parser_begin_omp_structured_block (parser);
36228           ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36229                                           cclauses, if_p);
36230           cp_parser_end_omp_structured_block (parser, save);
36231           tree body = finish_omp_structured_block (sb);
36232           if (ret == NULL)
36233             return ret;
36234           clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36235           ret = make_node (OMP_TEAMS);
36236           TREE_TYPE (ret) = void_type_node;
36237           OMP_TEAMS_CLAUSES (ret) = clauses;
36238           OMP_TEAMS_BODY (ret) = body;
36239           OMP_TEAMS_COMBINED (ret) = 1;
36240           SET_EXPR_LOCATION (ret, loc);
36241           return add_stmt (ret);
36242         }
36243     }
36244   if (!flag_openmp)  /* flag_openmp_simd  */
36245     {
36246       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36247       return NULL_TREE;
36248     }
36249
36250   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36251                                        cclauses == NULL);
36252   if (cclauses)
36253     {
36254       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36255       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36256     }
36257
36258   tree stmt = make_node (OMP_TEAMS);
36259   TREE_TYPE (stmt) = void_type_node;
36260   OMP_TEAMS_CLAUSES (stmt) = clauses;
36261   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36262   SET_EXPR_LOCATION (stmt, loc);
36263
36264   return add_stmt (stmt);
36265 }
36266
36267 /* OpenMP 4.0:
36268    # pragma omp target data target-data-clause[optseq] new-line
36269      structured-block  */
36270
36271 #define OMP_TARGET_DATA_CLAUSE_MASK                             \
36272         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36273         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
36274         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36275         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36276
36277 static tree
36278 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36279 {
36280   tree clauses
36281     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36282                                  "#pragma omp target data", pragma_tok);
36283   int map_seen = 0;
36284   for (tree *pc = &clauses; *pc;)
36285     {
36286       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36287         switch (OMP_CLAUSE_MAP_KIND (*pc))
36288           {
36289           case GOMP_MAP_TO:
36290           case GOMP_MAP_ALWAYS_TO:
36291           case GOMP_MAP_FROM:
36292           case GOMP_MAP_ALWAYS_FROM:
36293           case GOMP_MAP_TOFROM:
36294           case GOMP_MAP_ALWAYS_TOFROM:
36295           case GOMP_MAP_ALLOC:
36296             map_seen = 3;
36297             break;
36298           case GOMP_MAP_FIRSTPRIVATE_POINTER:
36299           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36300           case GOMP_MAP_ALWAYS_POINTER:
36301             break;
36302           default:
36303             map_seen |= 1;
36304             error_at (OMP_CLAUSE_LOCATION (*pc),
36305                       "%<#pragma omp target data%> with map-type other "
36306                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36307                       "on %<map%> clause");
36308             *pc = OMP_CLAUSE_CHAIN (*pc);
36309             continue;
36310           }
36311       pc = &OMP_CLAUSE_CHAIN (*pc);
36312     }
36313
36314   if (map_seen != 3)
36315     {
36316       if (map_seen == 0)
36317         error_at (pragma_tok->location,
36318                   "%<#pragma omp target data%> must contain at least "
36319                   "one %<map%> clause");
36320       return NULL_TREE;
36321     }
36322
36323   tree stmt = make_node (OMP_TARGET_DATA);
36324   TREE_TYPE (stmt) = void_type_node;
36325   OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36326
36327   keep_next_level (true);
36328   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36329
36330   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36331   return add_stmt (stmt);
36332 }
36333
36334 /* OpenMP 4.5:
36335    # pragma omp target enter data target-enter-data-clause[optseq] new-line
36336      structured-block  */
36337
36338 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK                       \
36339         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36340         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
36341         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36342         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
36343         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36344
36345 static tree
36346 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36347                                  enum pragma_context context)
36348 {
36349   bool data_seen = false;
36350   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36351     {
36352       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36353       const char *p = IDENTIFIER_POINTER (id);
36354
36355       if (strcmp (p, "data") == 0)
36356         {
36357           cp_lexer_consume_token (parser->lexer);
36358           data_seen = true;
36359         }
36360     }
36361   if (!data_seen)
36362     {
36363       cp_parser_error (parser, "expected %<data%>");
36364       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36365       return NULL_TREE;
36366     }
36367
36368   if (context == pragma_stmt)
36369     {
36370       error_at (pragma_tok->location,
36371                 "%<#pragma %s%> may only be used in compound statements",
36372                 "omp target enter data");
36373       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36374       return NULL_TREE;
36375     }
36376
36377   tree clauses
36378     = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36379                                  "#pragma omp target enter data", pragma_tok);
36380   int map_seen = 0;
36381   for (tree *pc = &clauses; *pc;)
36382     {
36383       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36384         switch (OMP_CLAUSE_MAP_KIND (*pc))
36385           {
36386           case GOMP_MAP_TO:
36387           case GOMP_MAP_ALWAYS_TO:
36388           case GOMP_MAP_ALLOC:
36389             map_seen = 3;
36390             break;
36391           case GOMP_MAP_FIRSTPRIVATE_POINTER:
36392           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36393           case GOMP_MAP_ALWAYS_POINTER:
36394             break;
36395           default:
36396             map_seen |= 1;
36397             error_at (OMP_CLAUSE_LOCATION (*pc),
36398                       "%<#pragma omp target enter data%> with map-type other "
36399                       "than %<to%> or %<alloc%> on %<map%> clause");
36400             *pc = OMP_CLAUSE_CHAIN (*pc);
36401             continue;
36402           }
36403       pc = &OMP_CLAUSE_CHAIN (*pc);
36404     }
36405
36406   if (map_seen != 3)
36407     {
36408       if (map_seen == 0)
36409         error_at (pragma_tok->location,
36410                   "%<#pragma omp target enter data%> must contain at least "
36411                   "one %<map%> clause");
36412       return NULL_TREE;
36413     }
36414
36415   tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36416   TREE_TYPE (stmt) = void_type_node;
36417   OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36418   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36419   return add_stmt (stmt);
36420 }
36421
36422 /* OpenMP 4.5:
36423    # pragma omp target exit data target-enter-data-clause[optseq] new-line
36424      structured-block  */
36425
36426 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK                        \
36427         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36428         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
36429         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36430         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
36431         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36432
36433 static tree
36434 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36435                                 enum pragma_context context)
36436 {
36437   bool data_seen = false;
36438   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36439     {
36440       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36441       const char *p = IDENTIFIER_POINTER (id);
36442
36443       if (strcmp (p, "data") == 0)
36444         {
36445           cp_lexer_consume_token (parser->lexer);
36446           data_seen = true;
36447         }
36448     }
36449   if (!data_seen)
36450     {
36451       cp_parser_error (parser, "expected %<data%>");
36452       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36453       return NULL_TREE;
36454     }
36455
36456   if (context == pragma_stmt)
36457     {
36458       error_at (pragma_tok->location,
36459                 "%<#pragma %s%> may only be used in compound statements",
36460                 "omp target exit data");
36461       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36462       return NULL_TREE;
36463     }
36464
36465   tree clauses
36466     = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36467                                  "#pragma omp target exit data", pragma_tok);
36468   int map_seen = 0;
36469   for (tree *pc = &clauses; *pc;)
36470     {
36471       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36472         switch (OMP_CLAUSE_MAP_KIND (*pc))
36473           {
36474           case GOMP_MAP_FROM:
36475           case GOMP_MAP_ALWAYS_FROM:
36476           case GOMP_MAP_RELEASE:
36477           case GOMP_MAP_DELETE:
36478             map_seen = 3;
36479             break;
36480           case GOMP_MAP_FIRSTPRIVATE_POINTER:
36481           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36482           case GOMP_MAP_ALWAYS_POINTER:
36483             break;
36484           default:
36485             map_seen |= 1;
36486             error_at (OMP_CLAUSE_LOCATION (*pc),
36487                       "%<#pragma omp target exit data%> with map-type other "
36488                       "than %<from%>, %<release%> or %<delete%> on %<map%>"
36489                       " clause");
36490             *pc = OMP_CLAUSE_CHAIN (*pc);
36491             continue;
36492           }
36493       pc = &OMP_CLAUSE_CHAIN (*pc);
36494     }
36495
36496   if (map_seen != 3)
36497     {
36498       if (map_seen == 0)
36499         error_at (pragma_tok->location,
36500                   "%<#pragma omp target exit data%> must contain at least "
36501                   "one %<map%> clause");
36502       return NULL_TREE;
36503     }
36504
36505   tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36506   TREE_TYPE (stmt) = void_type_node;
36507   OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36508   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36509   return add_stmt (stmt);
36510 }
36511
36512 /* OpenMP 4.0:
36513    # pragma omp target update target-update-clause[optseq] new-line */
36514
36515 #define OMP_TARGET_UPDATE_CLAUSE_MASK                           \
36516         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)         \
36517         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
36518         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36519         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36520         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
36521         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36522
36523 static bool
36524 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36525                              enum pragma_context context)
36526 {
36527   if (context == pragma_stmt)
36528     {
36529       error_at (pragma_tok->location,
36530                 "%<#pragma %s%> may only be used in compound statements",
36531                 "omp target update");
36532       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36533       return false;
36534     }
36535
36536   tree clauses
36537     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36538                                  "#pragma omp target update", pragma_tok);
36539   if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36540       && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36541     {
36542       error_at (pragma_tok->location,
36543                 "%<#pragma omp target update%> must contain at least one "
36544                 "%<from%> or %<to%> clauses");
36545       return false;
36546     }
36547
36548   tree stmt = make_node (OMP_TARGET_UPDATE);
36549   TREE_TYPE (stmt) = void_type_node;
36550   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36551   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36552   add_stmt (stmt);
36553   return false;
36554 }
36555
36556 /* OpenMP 4.0:
36557    # pragma omp target target-clause[optseq] new-line
36558      structured-block  */
36559
36560 #define OMP_TARGET_CLAUSE_MASK                                  \
36561         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36562         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
36563         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36564         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
36565         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
36566         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
36567         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36568         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)   \
36569         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36570
36571 static bool
36572 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36573                       enum pragma_context context, bool *if_p)
36574 {
36575   tree *pc = NULL, stmt;
36576
36577   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36578     {
36579       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36580       const char *p = IDENTIFIER_POINTER (id);
36581       enum tree_code ccode = ERROR_MARK;
36582
36583       if (strcmp (p, "teams") == 0)
36584         ccode = OMP_TEAMS;
36585       else if (strcmp (p, "parallel") == 0)
36586         ccode = OMP_PARALLEL;
36587       else if (strcmp (p, "simd") == 0)
36588         ccode = OMP_SIMD;
36589       if (ccode != ERROR_MARK)
36590         {
36591           tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36592           char p_name[sizeof ("#pragma omp target teams distribute "
36593                               "parallel for simd")];
36594
36595           cp_lexer_consume_token (parser->lexer);
36596           strcpy (p_name, "#pragma omp target");
36597           if (!flag_openmp)  /* flag_openmp_simd  */
36598             {
36599               tree stmt;
36600               switch (ccode)
36601                 {
36602                 case OMP_TEAMS:
36603                   stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36604                                               OMP_TARGET_CLAUSE_MASK,
36605                                               cclauses, if_p);
36606                   break;
36607                 case OMP_PARALLEL:
36608                   stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36609                                                  OMP_TARGET_CLAUSE_MASK,
36610                                                  cclauses, if_p);
36611                   break;
36612                 case OMP_SIMD:
36613                   stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36614                                              OMP_TARGET_CLAUSE_MASK,
36615                                              cclauses, if_p);
36616                   break;
36617                 default:
36618                   gcc_unreachable ();
36619                 }
36620               return stmt != NULL_TREE;
36621             }
36622           keep_next_level (true);
36623           tree sb = begin_omp_structured_block (), ret;
36624           unsigned save = cp_parser_begin_omp_structured_block (parser);
36625           switch (ccode)
36626             {
36627             case OMP_TEAMS:
36628               ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36629                                          OMP_TARGET_CLAUSE_MASK, cclauses,
36630                                          if_p);
36631               break;
36632             case OMP_PARALLEL:
36633               ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36634                                             OMP_TARGET_CLAUSE_MASK, cclauses,
36635                                             if_p);
36636               break;
36637             case OMP_SIMD:
36638               ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36639                                         OMP_TARGET_CLAUSE_MASK, cclauses,
36640                                         if_p);
36641               break;
36642             default:
36643               gcc_unreachable ();
36644             }
36645           cp_parser_end_omp_structured_block (parser, save);
36646           tree body = finish_omp_structured_block (sb);
36647           if (ret == NULL_TREE)
36648             return false;
36649           if (ccode == OMP_TEAMS && !processing_template_decl)
36650             {
36651               /* For combined target teams, ensure the num_teams and
36652                  thread_limit clause expressions are evaluated on the host,
36653                  before entering the target construct.  */
36654               tree c;
36655               for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36656                    c; c = OMP_CLAUSE_CHAIN (c))
36657                 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36658                      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36659                     && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36660                   {
36661                     tree expr = OMP_CLAUSE_OPERAND (c, 0);
36662                     expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36663                     if (expr == error_mark_node)
36664                       continue;
36665                     tree tmp = TARGET_EXPR_SLOT (expr);
36666                     add_stmt (expr);
36667                     OMP_CLAUSE_OPERAND (c, 0) = expr;
36668                     tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36669                                                 OMP_CLAUSE_FIRSTPRIVATE);
36670                     OMP_CLAUSE_DECL (tc) = tmp;
36671                     OMP_CLAUSE_CHAIN (tc)
36672                       = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36673                     cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36674                   }
36675             }
36676           tree stmt = make_node (OMP_TARGET);
36677           TREE_TYPE (stmt) = void_type_node;
36678           OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36679           OMP_TARGET_BODY (stmt) = body;
36680           OMP_TARGET_COMBINED (stmt) = 1;
36681           SET_EXPR_LOCATION (stmt, pragma_tok->location);
36682           add_stmt (stmt);
36683           pc = &OMP_TARGET_CLAUSES (stmt);
36684           goto check_clauses;
36685         }
36686       else if (!flag_openmp)  /* flag_openmp_simd  */
36687         {
36688           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36689           return false;
36690         }
36691       else if (strcmp (p, "data") == 0)
36692         {
36693           cp_lexer_consume_token (parser->lexer);
36694           cp_parser_omp_target_data (parser, pragma_tok, if_p);
36695           return true;
36696         }
36697       else if (strcmp (p, "enter") == 0)
36698         {
36699           cp_lexer_consume_token (parser->lexer);
36700           cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36701           return false;
36702         }
36703       else if (strcmp (p, "exit") == 0)
36704         {
36705           cp_lexer_consume_token (parser->lexer);
36706           cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36707           return false;
36708         }
36709       else if (strcmp (p, "update") == 0)
36710         {
36711           cp_lexer_consume_token (parser->lexer);
36712           return cp_parser_omp_target_update (parser, pragma_tok, context);
36713         }
36714     }
36715   if (!flag_openmp)  /* flag_openmp_simd  */
36716     {
36717       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36718       return false;
36719     }
36720
36721   stmt = make_node (OMP_TARGET);
36722   TREE_TYPE (stmt) = void_type_node;
36723
36724   OMP_TARGET_CLAUSES (stmt)
36725     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36726                                  "#pragma omp target", pragma_tok);
36727   pc = &OMP_TARGET_CLAUSES (stmt);
36728   keep_next_level (true);
36729   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36730
36731   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36732   add_stmt (stmt);
36733
36734 check_clauses:
36735   while (*pc)
36736     {
36737       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36738         switch (OMP_CLAUSE_MAP_KIND (*pc))
36739           {
36740           case GOMP_MAP_TO:
36741           case GOMP_MAP_ALWAYS_TO:
36742           case GOMP_MAP_FROM:
36743           case GOMP_MAP_ALWAYS_FROM:
36744           case GOMP_MAP_TOFROM:
36745           case GOMP_MAP_ALWAYS_TOFROM:
36746           case GOMP_MAP_ALLOC:
36747           case GOMP_MAP_FIRSTPRIVATE_POINTER:
36748           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36749           case GOMP_MAP_ALWAYS_POINTER:
36750             break;
36751           default:
36752             error_at (OMP_CLAUSE_LOCATION (*pc),
36753                       "%<#pragma omp target%> with map-type other "
36754                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36755                       "on %<map%> clause");
36756             *pc = OMP_CLAUSE_CHAIN (*pc);
36757             continue;
36758           }
36759       pc = &OMP_CLAUSE_CHAIN (*pc);
36760     }
36761   return true;
36762 }
36763
36764 /* OpenACC 2.0:
36765    # pragma acc cache (variable-list) new-line
36766 */
36767
36768 static tree
36769 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36770 {
36771   tree stmt, clauses;
36772
36773   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36774   clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36775
36776   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36777
36778   stmt = make_node (OACC_CACHE);
36779   TREE_TYPE (stmt) = void_type_node;
36780   OACC_CACHE_CLAUSES (stmt) = clauses;
36781   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36782   add_stmt (stmt);
36783
36784   return stmt;
36785 }
36786
36787 /* OpenACC 2.0:
36788    # pragma acc data oacc-data-clause[optseq] new-line
36789      structured-block  */
36790
36791 #define OACC_DATA_CLAUSE_MASK                                           \
36792         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
36793         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
36794         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
36795         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
36796         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
36797         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
36798         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
36799         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
36800         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
36801         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
36802         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36803
36804 static tree
36805 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36806 {
36807   tree stmt, clauses, block;
36808   unsigned int save;
36809
36810   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36811                                         "#pragma acc data", pragma_tok);
36812
36813   block = begin_omp_parallel ();
36814   save = cp_parser_begin_omp_structured_block (parser);
36815   cp_parser_statement (parser, NULL_TREE, false, if_p);
36816   cp_parser_end_omp_structured_block (parser, save);
36817   stmt = finish_oacc_data (clauses, block);
36818   return stmt;
36819 }
36820
36821 /* OpenACC 2.0:
36822   # pragma acc host_data <clauses> new-line
36823   structured-block  */
36824
36825 #define OACC_HOST_DATA_CLAUSE_MASK                                      \
36826   ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36827
36828 static tree
36829 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36830 {
36831   tree stmt, clauses, block;
36832   unsigned int save;
36833
36834   clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36835                                         "#pragma acc host_data", pragma_tok);
36836
36837   block = begin_omp_parallel ();
36838   save = cp_parser_begin_omp_structured_block (parser);
36839   cp_parser_statement (parser, NULL_TREE, false, if_p);
36840   cp_parser_end_omp_structured_block (parser, save);
36841   stmt = finish_oacc_host_data (clauses, block);
36842   return stmt;
36843 }
36844
36845 /* OpenACC 2.0:
36846    # pragma acc declare oacc-data-clause[optseq] new-line
36847 */
36848
36849 #define OACC_DECLARE_CLAUSE_MASK                                        \
36850         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
36851         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
36852         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
36853         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
36854         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
36855         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)     \
36856         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)                \
36857         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
36858         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
36859         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
36860         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
36861         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36862
36863 static tree
36864 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36865 {
36866   tree clauses, stmt;
36867   bool error = false;
36868
36869   clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36870                                         "#pragma acc declare", pragma_tok, true);
36871
36872
36873   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36874     {
36875       error_at (pragma_tok->location,
36876                 "no valid clauses specified in %<#pragma acc declare%>");
36877       return NULL_TREE;
36878     }
36879
36880   for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36881     {
36882       location_t loc = OMP_CLAUSE_LOCATION (t);
36883       tree decl = OMP_CLAUSE_DECL (t);
36884       if (!DECL_P (decl))
36885         {
36886           error_at (loc, "array section in %<#pragma acc declare%>");
36887           error = true;
36888           continue;
36889         }
36890       gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36891       switch (OMP_CLAUSE_MAP_KIND (t))
36892         {
36893         case GOMP_MAP_FIRSTPRIVATE_POINTER:
36894         case GOMP_MAP_FORCE_ALLOC:
36895         case GOMP_MAP_FORCE_TO:
36896         case GOMP_MAP_FORCE_DEVICEPTR:
36897         case GOMP_MAP_DEVICE_RESIDENT:
36898           break;
36899
36900         case GOMP_MAP_LINK:
36901           if (!global_bindings_p ()
36902               && (TREE_STATIC (decl)
36903                || !DECL_EXTERNAL (decl)))
36904             {
36905               error_at (loc,
36906                         "%qD must be a global variable in "
36907                         "%<#pragma acc declare link%>",
36908                         decl);
36909               error = true;
36910               continue;
36911             }
36912           break;
36913
36914         default:
36915           if (global_bindings_p ())
36916             {
36917               error_at (loc, "invalid OpenACC clause at file scope");
36918               error = true;
36919               continue;
36920             }
36921           if (DECL_EXTERNAL (decl))
36922             {
36923               error_at (loc,
36924                         "invalid use of %<extern%> variable %qD "
36925                         "in %<#pragma acc declare%>", decl);
36926               error = true;
36927               continue;
36928             }
36929           else if (TREE_PUBLIC (decl))
36930             {
36931               error_at (loc,
36932                         "invalid use of %<global%> variable %qD "
36933                         "in %<#pragma acc declare%>", decl);
36934               error = true;
36935               continue;
36936             }
36937           break;
36938         }
36939
36940       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36941           || lookup_attribute ("omp declare target link",
36942                                DECL_ATTRIBUTES (decl)))
36943         {
36944           error_at (loc, "variable %qD used more than once with "
36945                     "%<#pragma acc declare%>", decl);
36946           error = true;
36947           continue;
36948         }
36949
36950       if (!error)
36951         {
36952           tree id;
36953
36954           if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36955             id = get_identifier ("omp declare target link");
36956           else
36957             id = get_identifier ("omp declare target");
36958
36959           DECL_ATTRIBUTES (decl)
36960             = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36961           if (global_bindings_p ())
36962             {
36963               symtab_node *node = symtab_node::get (decl);
36964               if (node != NULL)
36965                 {
36966                   node->offloadable = 1;
36967                   if (ENABLE_OFFLOADING)
36968                     {
36969                       g->have_offload = true;
36970                       if (is_a <varpool_node *> (node))
36971                         vec_safe_push (offload_vars, decl);
36972                     }
36973                 }
36974             }
36975         }
36976     }
36977
36978   if (error || global_bindings_p ())
36979     return NULL_TREE;
36980
36981   stmt = make_node (OACC_DECLARE);
36982   TREE_TYPE (stmt) = void_type_node;
36983   OACC_DECLARE_CLAUSES (stmt) = clauses;
36984   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36985
36986   add_stmt (stmt);
36987
36988   return NULL_TREE;
36989 }
36990
36991 /* OpenACC 2.0:
36992    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36993
36994    or
36995
36996    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36997
36998    LOC is the location of the #pragma token.
36999 */
37000
37001 #define OACC_ENTER_DATA_CLAUSE_MASK                                     \
37002         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
37003         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
37004         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
37005         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
37006         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
37007         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
37008         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37009
37010 #define OACC_EXIT_DATA_CLAUSE_MASK                                      \
37011         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
37012         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
37013         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
37014         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE)              \
37015         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37016
37017 static tree
37018 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37019                                 bool enter)
37020 {
37021   location_t loc = pragma_tok->location;
37022   tree stmt, clauses;
37023   const char *p = "";
37024
37025   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37026     p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37027
37028   if (strcmp (p, "data") != 0)
37029     {
37030       error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37031                 enter ? "enter" : "exit");
37032       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37033       return NULL_TREE;
37034     }
37035
37036   cp_lexer_consume_token (parser->lexer);
37037
37038   if (enter)
37039     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37040                                          "#pragma acc enter data", pragma_tok);
37041   else
37042     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37043                                          "#pragma acc exit data", pragma_tok);
37044
37045   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37046     {
37047       error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37048                 enter ? "enter" : "exit");
37049       return NULL_TREE;
37050     }
37051
37052   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37053   TREE_TYPE (stmt) = void_type_node;
37054   OMP_STANDALONE_CLAUSES (stmt) = clauses;
37055   SET_EXPR_LOCATION (stmt, pragma_tok->location);
37056   add_stmt (stmt);
37057   return stmt;
37058 }
37059
37060 /* OpenACC 2.0:
37061    # pragma acc loop oacc-loop-clause[optseq] new-line
37062      structured-block  */
37063
37064 #define OACC_LOOP_CLAUSE_MASK                                           \
37065         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)            \
37066         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
37067         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
37068         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
37069         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
37070         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
37071         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)                \
37072         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)         \
37073         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)                 \
37074         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37075
37076 static tree
37077 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37078                      omp_clause_mask mask, tree *cclauses, bool *if_p)
37079 {
37080   bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37081
37082   strcat (p_name, " loop");
37083   mask |= OACC_LOOP_CLAUSE_MASK;
37084
37085   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37086                                              cclauses == NULL);
37087   if (cclauses)
37088     {
37089       clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37090       if (*cclauses)
37091         *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37092       if (clauses)
37093         clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37094     }
37095
37096   tree block = begin_omp_structured_block ();
37097   int save = cp_parser_begin_omp_structured_block (parser);
37098   tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37099   cp_parser_end_omp_structured_block (parser, save);
37100   add_stmt (finish_omp_structured_block (block));
37101
37102   return stmt;
37103 }
37104
37105 /* OpenACC 2.0:
37106    # pragma acc kernels oacc-kernels-clause[optseq] new-line
37107      structured-block
37108
37109    or
37110
37111    # pragma acc parallel oacc-parallel-clause[optseq] new-line
37112      structured-block
37113 */
37114
37115 #define OACC_KERNELS_CLAUSE_MASK                                        \
37116         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
37117         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
37118         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
37119         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
37120         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
37121         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
37122         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
37123         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
37124         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
37125         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
37126         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
37127         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
37128         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
37129         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
37130         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
37131         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
37132         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37133
37134 #define OACC_PARALLEL_CLAUSE_MASK                                       \
37135         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
37136         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
37137         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
37138         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
37139         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
37140         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
37141         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
37142         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)        \
37143         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
37144         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
37145         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
37146         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
37147         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
37148         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
37149         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
37150         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
37151         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
37152         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
37153         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
37154         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37155
37156 static tree
37157 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37158                                  char *p_name, bool *if_p)
37159 {
37160   omp_clause_mask mask;
37161   enum tree_code code;
37162   switch (cp_parser_pragma_kind (pragma_tok))
37163     {
37164     case PRAGMA_OACC_KERNELS:
37165       strcat (p_name, " kernels");
37166       mask = OACC_KERNELS_CLAUSE_MASK;
37167       code = OACC_KERNELS;
37168       break;
37169     case PRAGMA_OACC_PARALLEL:
37170       strcat (p_name, " parallel");
37171       mask = OACC_PARALLEL_CLAUSE_MASK;
37172       code = OACC_PARALLEL;
37173       break;
37174     default:
37175       gcc_unreachable ();
37176     }
37177
37178   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37179     {
37180       const char *p
37181         = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37182       if (strcmp (p, "loop") == 0)
37183         {
37184           cp_lexer_consume_token (parser->lexer);
37185           tree block = begin_omp_parallel ();
37186           tree clauses;
37187           cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37188                                if_p);
37189           return finish_omp_construct (code, block, clauses);
37190         }
37191     }
37192
37193   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37194
37195   tree block = begin_omp_parallel ();
37196   unsigned int save = cp_parser_begin_omp_structured_block (parser);
37197   cp_parser_statement (parser, NULL_TREE, false, if_p);
37198   cp_parser_end_omp_structured_block (parser, save);
37199   return finish_omp_construct (code, block, clauses);
37200 }
37201
37202 /* OpenACC 2.0:
37203    # pragma acc update oacc-update-clause[optseq] new-line
37204 */
37205
37206 #define OACC_UPDATE_CLAUSE_MASK                                         \
37207         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
37208         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)              \
37209         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)                \
37210         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
37211         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)                \
37212         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37213
37214 static tree
37215 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37216 {
37217   tree stmt, clauses;
37218
37219   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37220                                          "#pragma acc update", pragma_tok);
37221
37222   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37223     {
37224       error_at (pragma_tok->location,
37225                 "%<#pragma acc update%> must contain at least one "
37226                 "%<device%> or %<host%> or %<self%> clause");
37227       return NULL_TREE;
37228     }
37229
37230   stmt = make_node (OACC_UPDATE);
37231   TREE_TYPE (stmt) = void_type_node;
37232   OACC_UPDATE_CLAUSES (stmt) = clauses;
37233   SET_EXPR_LOCATION (stmt, pragma_tok->location);
37234   add_stmt (stmt);
37235   return stmt;
37236 }
37237
37238 /* OpenACC 2.0:
37239    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37240
37241    LOC is the location of the #pragma token.
37242 */
37243
37244 #define OACC_WAIT_CLAUSE_MASK                                   \
37245         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37246
37247 static tree
37248 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37249 {
37250   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37251   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37252
37253   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37254     list = cp_parser_oacc_wait_list (parser, loc, list);
37255
37256   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37257                                         "#pragma acc wait", pragma_tok);
37258
37259   stmt = c_finish_oacc_wait (loc, list, clauses);
37260   stmt = finish_expr_stmt (stmt);
37261
37262   return stmt;
37263 }
37264
37265 /* OpenMP 4.0:
37266    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
37267
37268 #define OMP_DECLARE_SIMD_CLAUSE_MASK                            \
37269         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
37270         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
37271         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
37272         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)      \
37273         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)     \
37274         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37275
37276 static void
37277 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37278                             enum pragma_context context)
37279 {
37280   bool first_p = parser->omp_declare_simd == NULL;
37281   cp_omp_declare_simd_data data;
37282   if (first_p)
37283     {
37284       data.error_seen = false;
37285       data.fndecl_seen = false;
37286       data.tokens = vNULL;
37287       data.clauses = NULL_TREE;
37288       /* It is safe to take the address of a local variable; it will only be
37289          used while this scope is live.  */
37290       parser->omp_declare_simd = &data;
37291     }
37292
37293   /* Store away all pragma tokens.  */
37294   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37295          && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37296     cp_lexer_consume_token (parser->lexer);
37297   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37298     parser->omp_declare_simd->error_seen = true;
37299   cp_parser_require_pragma_eol (parser, pragma_tok);
37300   struct cp_token_cache *cp
37301     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37302   parser->omp_declare_simd->tokens.safe_push (cp);
37303
37304   if (first_p)
37305     {
37306       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37307         cp_parser_pragma (parser, context, NULL);
37308       switch (context)
37309         {
37310         case pragma_external:
37311           cp_parser_declaration (parser);
37312           break;
37313         case pragma_member:
37314           cp_parser_member_declaration (parser);
37315           break;
37316         case pragma_objc_icode:
37317           cp_parser_block_declaration (parser, /*statement_p=*/false);
37318           break;
37319         default:
37320           cp_parser_declaration_statement (parser);
37321           break;
37322         }
37323       if (parser->omp_declare_simd
37324           && !parser->omp_declare_simd->error_seen
37325           && !parser->omp_declare_simd->fndecl_seen)
37326         error_at (pragma_tok->location,
37327                   "%<#pragma omp declare simd%> not immediately followed by "
37328                   "function declaration or definition");
37329       data.tokens.release ();
37330       parser->omp_declare_simd = NULL;
37331     }
37332 }
37333
37334 /* Finalize #pragma omp declare simd clauses after direct declarator has
37335    been parsed, and put that into "omp declare simd" attribute.  */
37336
37337 static tree
37338 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37339 {
37340   struct cp_token_cache *ce;
37341   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37342   int i;
37343
37344   if (!data->error_seen && data->fndecl_seen)
37345     {
37346       error ("%<#pragma omp declare simd%> not immediately followed by "
37347              "a single function declaration or definition");
37348       data->error_seen = true;
37349     }
37350   if (data->error_seen)
37351     return attrs;
37352
37353   FOR_EACH_VEC_ELT (data->tokens, i, ce)
37354     {
37355       tree c, cl;
37356
37357       cp_parser_push_lexer_for_tokens (parser, ce);
37358       parser->lexer->in_pragma = true;
37359       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37360       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37361       cp_lexer_consume_token (parser->lexer);
37362       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37363                                       "#pragma omp declare simd", pragma_tok);
37364       cp_parser_pop_lexer (parser);
37365       if (cl)
37366         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37367       c = build_tree_list (get_identifier ("omp declare simd"), cl);
37368       TREE_CHAIN (c) = attrs;
37369       if (processing_template_decl)
37370         ATTR_IS_DEPENDENT (c) = 1;
37371       attrs = c;
37372     }
37373
37374   data->fndecl_seen = true;
37375   return attrs;
37376 }
37377
37378
37379 /* OpenMP 4.0:
37380    # pragma omp declare target new-line
37381    declarations and definitions
37382    # pragma omp end declare target new-line
37383
37384    OpenMP 4.5:
37385    # pragma omp declare target ( extended-list ) new-line
37386
37387    # pragma omp declare target declare-target-clauses[seq] new-line  */
37388
37389 #define OMP_DECLARE_TARGET_CLAUSE_MASK                          \
37390         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
37391         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37392
37393 static void
37394 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37395 {
37396   tree clauses = NULL_TREE;
37397   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37398     clauses
37399       = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37400                                    "#pragma omp declare target", pragma_tok);
37401   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37402     {
37403       clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37404                                         clauses);
37405       clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37406       cp_parser_require_pragma_eol (parser, pragma_tok);
37407     }
37408   else
37409     {
37410       cp_parser_require_pragma_eol (parser, pragma_tok);
37411       scope_chain->omp_declare_target_attribute++;
37412       return;
37413     }
37414   if (scope_chain->omp_declare_target_attribute)
37415     error_at (pragma_tok->location,
37416               "%<#pragma omp declare target%> with clauses in between "
37417               "%<#pragma omp declare target%> without clauses and "
37418               "%<#pragma omp end declare target%>");
37419   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37420     {
37421       tree t = OMP_CLAUSE_DECL (c), id;
37422       tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37423       tree at2 = lookup_attribute ("omp declare target link",
37424                                    DECL_ATTRIBUTES (t));
37425       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37426         {
37427           id = get_identifier ("omp declare target link");
37428           std::swap (at1, at2);
37429         }
37430       else
37431         id = get_identifier ("omp declare target");
37432       if (at2)
37433         {
37434           error_at (OMP_CLAUSE_LOCATION (c),
37435                     "%qD specified both in declare target %<link%> and %<to%>"
37436                     " clauses", t);
37437           continue;
37438         }
37439       if (!at1)
37440         {
37441           DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37442           if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37443             continue;
37444
37445           symtab_node *node = symtab_node::get (t);
37446           if (node != NULL)
37447             {
37448               node->offloadable = 1;
37449               if (ENABLE_OFFLOADING)
37450                 {
37451                   g->have_offload = true;
37452                   if (is_a <varpool_node *> (node))
37453                     vec_safe_push (offload_vars, t);
37454                 }
37455             }
37456         }
37457     }
37458 }
37459
37460 static void
37461 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37462 {
37463   const char *p = "";
37464   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37465     {
37466       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37467       p = IDENTIFIER_POINTER (id);
37468     }
37469   if (strcmp (p, "declare") == 0)
37470     {
37471       cp_lexer_consume_token (parser->lexer);
37472       p = "";
37473       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37474         {
37475           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37476           p = IDENTIFIER_POINTER (id);
37477         }
37478       if (strcmp (p, "target") == 0)
37479         cp_lexer_consume_token (parser->lexer);
37480       else
37481         {
37482           cp_parser_error (parser, "expected %<target%>");
37483           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37484           return;
37485         }
37486     }
37487   else
37488     {
37489       cp_parser_error (parser, "expected %<declare%>");
37490       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37491       return;
37492     }
37493   cp_parser_require_pragma_eol (parser, pragma_tok);
37494   if (!scope_chain->omp_declare_target_attribute)
37495     error_at (pragma_tok->location,
37496               "%<#pragma omp end declare target%> without corresponding "
37497               "%<#pragma omp declare target%>");
37498   else
37499     scope_chain->omp_declare_target_attribute--;
37500 }
37501
37502 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
37503    expression and optional initializer clause of
37504    #pragma omp declare reduction.  We store the expression(s) as
37505    either 3, 6 or 7 special statements inside of the artificial function's
37506    body.  The first two statements are DECL_EXPRs for the artificial
37507    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37508    expression that uses those variables.
37509    If there was any INITIALIZER clause, this is followed by further statements,
37510    the fourth and fifth statements are DECL_EXPRs for the artificial
37511    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
37512    constructor variant (first token after open paren is not omp_priv),
37513    then the sixth statement is a statement with the function call expression
37514    that uses the OMP_PRIV and optionally OMP_ORIG variable.
37515    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37516    to initialize the OMP_PRIV artificial variable and there is seventh
37517    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
37518
37519 static bool
37520 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37521 {
37522   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37523   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37524   type = TREE_TYPE (type);
37525   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37526   DECL_ARTIFICIAL (omp_out) = 1;
37527   pushdecl (omp_out);
37528   add_decl_expr (omp_out);
37529   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37530   DECL_ARTIFICIAL (omp_in) = 1;
37531   pushdecl (omp_in);
37532   add_decl_expr (omp_in);
37533   tree combiner;
37534   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37535
37536   keep_next_level (true);
37537   tree block = begin_omp_structured_block ();
37538   combiner = cp_parser_expression (parser);
37539   finish_expr_stmt (combiner);
37540   block = finish_omp_structured_block (block);
37541   add_stmt (block);
37542
37543   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37544     return false;
37545
37546   const char *p = "";
37547   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37548     {
37549       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37550       p = IDENTIFIER_POINTER (id);
37551     }
37552
37553   if (strcmp (p, "initializer") == 0)
37554     {
37555       cp_lexer_consume_token (parser->lexer);
37556       matching_parens parens;
37557       if (!parens.require_open (parser))
37558         return false;
37559
37560       p = "";
37561       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37562         {
37563           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37564           p = IDENTIFIER_POINTER (id);
37565         }
37566
37567       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37568       DECL_ARTIFICIAL (omp_priv) = 1;
37569       pushdecl (omp_priv);
37570       add_decl_expr (omp_priv);
37571       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37572       DECL_ARTIFICIAL (omp_orig) = 1;
37573       pushdecl (omp_orig);
37574       add_decl_expr (omp_orig);
37575
37576       keep_next_level (true);
37577       block = begin_omp_structured_block ();
37578
37579       bool ctor = false;
37580       if (strcmp (p, "omp_priv") == 0)
37581         {
37582           bool is_direct_init, is_non_constant_init;
37583           ctor = true;
37584           cp_lexer_consume_token (parser->lexer);
37585           /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
37586           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37587               || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37588                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37589                      == CPP_CLOSE_PAREN
37590                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37591                      == CPP_CLOSE_PAREN))
37592             {
37593               finish_omp_structured_block (block);
37594               error ("invalid initializer clause");
37595               return false;
37596             }
37597           initializer = cp_parser_initializer (parser, &is_direct_init,
37598                                                &is_non_constant_init);
37599           cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37600                           NULL_TREE, LOOKUP_ONLYCONVERTING);
37601         }
37602       else
37603         {
37604           cp_parser_parse_tentatively (parser);
37605           tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37606                                                   /*check_dependency_p=*/true,
37607                                                   /*template_p=*/NULL,
37608                                                   /*declarator_p=*/false,
37609                                                   /*optional_p=*/false);
37610           vec<tree, va_gc> *args;
37611           if (fn_name == error_mark_node
37612               || cp_parser_error_occurred (parser)
37613               || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37614               || ((args = cp_parser_parenthesized_expression_list
37615                                 (parser, non_attr, /*cast_p=*/false,
37616                                  /*allow_expansion_p=*/true,
37617                                  /*non_constant_p=*/NULL)),
37618                   cp_parser_error_occurred (parser)))
37619             {
37620               finish_omp_structured_block (block);
37621               cp_parser_abort_tentative_parse (parser);
37622               cp_parser_error (parser, "expected id-expression (arguments)");
37623               return false;
37624             }
37625           unsigned int i;
37626           tree arg;
37627           FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37628             if (arg == omp_priv
37629                 || (TREE_CODE (arg) == ADDR_EXPR
37630                     && TREE_OPERAND (arg, 0) == omp_priv))
37631               break;
37632           cp_parser_abort_tentative_parse (parser);
37633           if (arg == NULL_TREE)
37634             error ("one of the initializer call arguments should be %<omp_priv%>"
37635                    " or %<&omp_priv%>");
37636           initializer = cp_parser_postfix_expression (parser, false, false, false,
37637                                                       false, NULL);
37638           finish_expr_stmt (initializer);
37639         }
37640
37641       block = finish_omp_structured_block (block);
37642       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37643       add_stmt (block);
37644
37645       if (ctor)
37646         add_decl_expr (omp_orig);
37647
37648       if (!parens.require_close (parser))
37649         return false;
37650     }
37651
37652   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37653     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37654                               UNKNOWN_LOCATION);
37655
37656   return true;
37657 }
37658
37659 /* OpenMP 4.0
37660    #pragma omp declare reduction (reduction-id : typename-list : expression) \
37661       initializer-clause[opt] new-line
37662
37663    initializer-clause:
37664       initializer (omp_priv initializer)
37665       initializer (function-name (argument-list))  */
37666
37667 static void
37668 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37669                                  enum pragma_context)
37670 {
37671   auto_vec<tree> types;
37672   enum tree_code reduc_code = ERROR_MARK;
37673   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37674   unsigned int i;
37675   cp_token *first_token;
37676   cp_token_cache *cp;
37677   int errs;
37678   void *p;
37679     
37680   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
37681   p = obstack_alloc (&declarator_obstack, 0);
37682
37683   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37684     goto fail;
37685
37686   switch (cp_lexer_peek_token (parser->lexer)->type)
37687     {
37688     case CPP_PLUS:
37689       reduc_code = PLUS_EXPR;
37690       break;
37691     case CPP_MULT:
37692       reduc_code = MULT_EXPR;
37693       break;
37694     case CPP_MINUS:
37695       reduc_code = MINUS_EXPR;
37696       break;
37697     case CPP_AND:
37698       reduc_code = BIT_AND_EXPR;
37699       break;
37700     case CPP_XOR:
37701       reduc_code = BIT_XOR_EXPR;
37702       break;
37703     case CPP_OR:
37704       reduc_code = BIT_IOR_EXPR;
37705       break;
37706     case CPP_AND_AND:
37707       reduc_code = TRUTH_ANDIF_EXPR;
37708       break;
37709     case CPP_OR_OR:
37710       reduc_code = TRUTH_ORIF_EXPR;
37711       break;
37712     case CPP_NAME:
37713       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37714       break;
37715     default:
37716       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37717                                "%<|%>, %<&&%>, %<||%> or identifier");
37718       goto fail;
37719     }
37720
37721   if (reduc_code != ERROR_MARK)
37722     cp_lexer_consume_token (parser->lexer);
37723
37724   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37725   if (reduc_id == error_mark_node)
37726     goto fail;
37727
37728   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37729     goto fail;
37730
37731   /* Types may not be defined in declare reduction type list.  */
37732   const char *saved_message;
37733   saved_message = parser->type_definition_forbidden_message;
37734   parser->type_definition_forbidden_message
37735     = G_("types may not be defined in declare reduction type list");
37736   bool saved_colon_corrects_to_scope_p;
37737   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37738   parser->colon_corrects_to_scope_p = false;
37739   bool saved_colon_doesnt_start_class_def_p;
37740   saved_colon_doesnt_start_class_def_p
37741     = parser->colon_doesnt_start_class_def_p;
37742   parser->colon_doesnt_start_class_def_p = true;
37743
37744   while (true)
37745     {
37746       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37747       type = cp_parser_type_id (parser);
37748       if (type == error_mark_node)
37749         ;
37750       else if (ARITHMETIC_TYPE_P (type)
37751                && (orig_reduc_id == NULL_TREE
37752                    || (TREE_CODE (type) != COMPLEX_TYPE
37753                        && (id_equal (orig_reduc_id, "min")
37754                            || id_equal (orig_reduc_id, "max")))))
37755         error_at (loc, "predeclared arithmetic type %qT in "
37756                        "%<#pragma omp declare reduction%>", type);
37757       else if (TREE_CODE (type) == FUNCTION_TYPE
37758                || TREE_CODE (type) == METHOD_TYPE
37759                || TREE_CODE (type) == ARRAY_TYPE)
37760         error_at (loc, "function or array type %qT in "
37761                        "%<#pragma omp declare reduction%>", type);
37762       else if (TREE_CODE (type) == REFERENCE_TYPE)
37763         error_at (loc, "reference type %qT in "
37764                        "%<#pragma omp declare reduction%>", type);
37765       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37766         error_at (loc, "const, volatile or __restrict qualified type %qT in "
37767                        "%<#pragma omp declare reduction%>", type);
37768       else
37769         types.safe_push (type);
37770
37771       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37772         cp_lexer_consume_token (parser->lexer);
37773       else
37774         break;
37775     }
37776
37777   /* Restore the saved message.  */
37778   parser->type_definition_forbidden_message = saved_message;
37779   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37780   parser->colon_doesnt_start_class_def_p
37781     = saved_colon_doesnt_start_class_def_p;
37782
37783   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37784       || types.is_empty ())
37785     {
37786      fail:
37787       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37788       goto done;
37789     }
37790
37791   first_token = cp_lexer_peek_token (parser->lexer);
37792   cp = NULL;
37793   errs = errorcount;
37794   FOR_EACH_VEC_ELT (types, i, type)
37795     {
37796       tree fntype
37797         = build_function_type_list (void_type_node,
37798                                     cp_build_reference_type (type, false),
37799                                     NULL_TREE);
37800       tree this_reduc_id = reduc_id;
37801       if (!dependent_type_p (type))
37802         this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37803       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37804       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37805       DECL_ARTIFICIAL (fndecl) = 1;
37806       DECL_EXTERNAL (fndecl) = 1;
37807       DECL_DECLARED_INLINE_P (fndecl) = 1;
37808       DECL_IGNORED_P (fndecl) = 1;
37809       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37810       SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37811       DECL_ATTRIBUTES (fndecl)
37812         = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37813                      DECL_ATTRIBUTES (fndecl));
37814       if (processing_template_decl)
37815         fndecl = push_template_decl (fndecl);
37816       bool block_scope = false;
37817       tree block = NULL_TREE;
37818       if (current_function_decl)
37819         {
37820           block_scope = true;
37821           DECL_CONTEXT (fndecl) = global_namespace;
37822           if (!processing_template_decl)
37823             pushdecl (fndecl);
37824         }
37825       else if (current_class_type)
37826         {
37827           if (cp == NULL)
37828             {
37829               while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37830                      && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37831                 cp_lexer_consume_token (parser->lexer);
37832               if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37833                 goto fail;
37834               cp = cp_token_cache_new (first_token,
37835                                        cp_lexer_peek_nth_token (parser->lexer,
37836                                                                 2));
37837             }
37838           DECL_STATIC_FUNCTION_P (fndecl) = 1;
37839           finish_member_declaration (fndecl);
37840           DECL_PENDING_INLINE_INFO (fndecl) = cp;
37841           DECL_PENDING_INLINE_P (fndecl) = 1;
37842           vec_safe_push (unparsed_funs_with_definitions, fndecl);
37843           continue;
37844         }
37845       else
37846         {
37847           DECL_CONTEXT (fndecl) = current_namespace;
37848           pushdecl (fndecl);
37849         }
37850       if (!block_scope)
37851         start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37852       else
37853         block = begin_omp_structured_block ();
37854       if (cp)
37855         {
37856           cp_parser_push_lexer_for_tokens (parser, cp);
37857           parser->lexer->in_pragma = true;
37858         }
37859       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37860         {
37861           if (!block_scope)
37862             finish_function (/*inline_p=*/false);
37863           else
37864             DECL_CONTEXT (fndecl) = current_function_decl;
37865           if (cp)
37866             cp_parser_pop_lexer (parser);
37867           goto fail;
37868         }
37869       if (cp)
37870         cp_parser_pop_lexer (parser);
37871       if (!block_scope)
37872         finish_function (/*inline_p=*/false);
37873       else
37874         {
37875           DECL_CONTEXT (fndecl) = current_function_decl;
37876           block = finish_omp_structured_block (block);
37877           if (TREE_CODE (block) == BIND_EXPR)
37878             DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37879           else if (TREE_CODE (block) == STATEMENT_LIST)
37880             DECL_SAVED_TREE (fndecl) = block;
37881           if (processing_template_decl)
37882             add_decl_expr (fndecl);
37883         }
37884       cp_check_omp_declare_reduction (fndecl);
37885       if (cp == NULL && types.length () > 1)
37886         cp = cp_token_cache_new (first_token,
37887                                  cp_lexer_peek_nth_token (parser->lexer, 2));
37888       if (errs != errorcount)
37889         break;
37890     }
37891
37892   cp_parser_require_pragma_eol (parser, pragma_tok);
37893
37894  done:
37895   /* Free any declarators allocated.  */
37896   obstack_free (&declarator_obstack, p);
37897 }
37898
37899 /* OpenMP 4.0
37900    #pragma omp declare simd declare-simd-clauses[optseq] new-line
37901    #pragma omp declare reduction (reduction-id : typename-list : expression) \
37902       initializer-clause[opt] new-line
37903    #pragma omp declare target new-line  */
37904
37905 static bool
37906 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37907                        enum pragma_context context)
37908 {
37909   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37910     {
37911       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37912       const char *p = IDENTIFIER_POINTER (id);
37913
37914       if (strcmp (p, "simd") == 0)
37915         {
37916           cp_lexer_consume_token (parser->lexer);
37917           cp_parser_omp_declare_simd (parser, pragma_tok,
37918                                       context);
37919           return true;
37920         }
37921       cp_ensure_no_omp_declare_simd (parser);
37922       if (strcmp (p, "reduction") == 0)
37923         {
37924           cp_lexer_consume_token (parser->lexer);
37925           cp_parser_omp_declare_reduction (parser, pragma_tok,
37926                                            context);
37927           return false;
37928         }
37929       if (!flag_openmp)  /* flag_openmp_simd  */
37930         {
37931           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37932           return false;
37933         }
37934       if (strcmp (p, "target") == 0)
37935         {
37936           cp_lexer_consume_token (parser->lexer);
37937           cp_parser_omp_declare_target (parser, pragma_tok);
37938           return false;
37939         }
37940     }
37941   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37942                            "or %<target%>");
37943   cp_parser_require_pragma_eol (parser, pragma_tok);
37944   return false;
37945 }
37946
37947 /* OpenMP 4.5:
37948    #pragma omp taskloop taskloop-clause[optseq] new-line
37949      for-loop
37950
37951    #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37952      for-loop  */
37953
37954 #define OMP_TASKLOOP_CLAUSE_MASK                                \
37955         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
37956         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
37957         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37958         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
37959         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
37960         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)    \
37961         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)    \
37962         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)     \
37963         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
37964         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
37965         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
37966         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
37967         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)      \
37968         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37969
37970 static tree
37971 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37972                         char *p_name, omp_clause_mask mask, tree *cclauses,
37973                         bool *if_p)
37974 {
37975   tree clauses, sb, ret;
37976   unsigned int save;
37977   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37978
37979   strcat (p_name, " taskloop");
37980   mask |= OMP_TASKLOOP_CLAUSE_MASK;
37981
37982   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37983     {
37984       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37985       const char *p = IDENTIFIER_POINTER (id);
37986
37987       if (strcmp (p, "simd") == 0)
37988         {
37989           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37990           if (cclauses == NULL)
37991             cclauses = cclauses_buf;
37992
37993           cp_lexer_consume_token (parser->lexer);
37994           if (!flag_openmp)  /* flag_openmp_simd  */
37995             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37996                                        cclauses, if_p);
37997           sb = begin_omp_structured_block ();
37998           save = cp_parser_begin_omp_structured_block (parser);
37999           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38000                                     cclauses, if_p);
38001           cp_parser_end_omp_structured_block (parser, save);
38002           tree body = finish_omp_structured_block (sb);
38003           if (ret == NULL)
38004             return ret;
38005           ret = make_node (OMP_TASKLOOP);
38006           TREE_TYPE (ret) = void_type_node;
38007           OMP_FOR_BODY (ret) = body;
38008           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38009           SET_EXPR_LOCATION (ret, loc);
38010           add_stmt (ret);
38011           return ret;
38012         }
38013     }
38014   if (!flag_openmp)  /* flag_openmp_simd  */
38015     {
38016       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38017       return NULL_TREE;
38018     }
38019
38020   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38021                                        cclauses == NULL);
38022   if (cclauses)
38023     {
38024       cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38025       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38026     }
38027
38028   sb = begin_omp_structured_block ();
38029   save = cp_parser_begin_omp_structured_block (parser);
38030
38031   ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38032                                 if_p);
38033
38034   cp_parser_end_omp_structured_block (parser, save);
38035   add_stmt (finish_omp_structured_block (sb));
38036
38037   return ret;
38038 }
38039
38040
38041 /* OpenACC 2.0:
38042    # pragma acc routine oacc-routine-clause[optseq] new-line
38043      function-definition
38044
38045    # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38046 */
38047
38048 #define OACC_ROUTINE_CLAUSE_MASK                                        \
38049         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
38050         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
38051         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
38052         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38053
38054
38055 /* Parse the OpenACC routine pragma.  This has an optional '( name )'
38056    component, which must resolve to a declared namespace-scope
38057    function.  The clauses are either processed directly (for a named
38058    function), or defered until the immediatley following declaration
38059    is parsed.  */
38060
38061 static void
38062 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38063                         enum pragma_context context)
38064 {
38065   gcc_checking_assert (context == pragma_external);
38066   /* The checking for "another pragma following this one" in the "no optional
38067      '( name )'" case makes sure that we dont re-enter.  */
38068   gcc_checking_assert (parser->oacc_routine == NULL);
38069
38070   cp_oacc_routine_data data;
38071   data.error_seen = false;
38072   data.fndecl_seen = false;
38073   data.tokens = vNULL;
38074   data.clauses = NULL_TREE;
38075   data.loc = pragma_tok->location;
38076   /* It is safe to take the address of a local variable; it will only be
38077      used while this scope is live.  */
38078   parser->oacc_routine = &data;
38079
38080   /* Look for optional '( name )'.  */
38081   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38082     {
38083       matching_parens parens;
38084       parens.consume_open (parser); /* '(' */
38085
38086       /* We parse the name as an id-expression.  If it resolves to
38087          anything other than a non-overloaded function at namespace
38088          scope, it's an error.  */
38089       location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38090       tree name = cp_parser_id_expression (parser,
38091                                            /*template_keyword_p=*/false,
38092                                            /*check_dependency_p=*/false,
38093                                            /*template_p=*/NULL,
38094                                            /*declarator_p=*/false,
38095                                            /*optional_p=*/false);
38096       tree decl = (identifier_p (name)
38097                    ? cp_parser_lookup_name_simple (parser, name, name_loc)
38098                    : name);
38099       if (name != error_mark_node && decl == error_mark_node)
38100         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38101
38102       if (decl == error_mark_node
38103           || !parens.require_close (parser))
38104         {
38105           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38106           parser->oacc_routine = NULL;
38107           return;
38108         }
38109
38110       data.clauses
38111         = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38112                                       "#pragma acc routine",
38113                                       cp_lexer_peek_token (parser->lexer));
38114
38115       if (decl && is_overloaded_fn (decl)
38116           && (TREE_CODE (decl) != FUNCTION_DECL
38117               || DECL_FUNCTION_TEMPLATE_P  (decl)))
38118         {
38119           error_at (name_loc,
38120                     "%<#pragma acc routine%> names a set of overloads");
38121           parser->oacc_routine = NULL;
38122           return;
38123         }
38124
38125       /* Perhaps we should use the same rule as declarations in different
38126          namespaces?  */
38127       if (!DECL_NAMESPACE_SCOPE_P (decl))
38128         {
38129           error_at (name_loc,
38130                     "%qD does not refer to a namespace scope function", decl);
38131           parser->oacc_routine = NULL;
38132           return;
38133         }
38134
38135       if (TREE_CODE (decl) != FUNCTION_DECL)
38136         {
38137           error_at (name_loc, "%qD does not refer to a function", decl);
38138           parser->oacc_routine = NULL;
38139           return;
38140         }
38141
38142       cp_finalize_oacc_routine (parser, decl, false);
38143       parser->oacc_routine = NULL;
38144     }
38145   else /* No optional '( name )'.  */
38146     {
38147       /* Store away all pragma tokens.  */
38148       while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38149              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38150         cp_lexer_consume_token (parser->lexer);
38151       if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38152         parser->oacc_routine->error_seen = true;
38153       cp_parser_require_pragma_eol (parser, pragma_tok);
38154       struct cp_token_cache *cp
38155         = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38156       parser->oacc_routine->tokens.safe_push (cp);
38157
38158       /* Emit a helpful diagnostic if there's another pragma following this
38159          one.  */
38160       if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38161         {
38162           cp_ensure_no_oacc_routine (parser);
38163           data.tokens.release ();
38164           /* ..., and then just keep going.  */
38165           return;
38166         }
38167
38168       /* We only have to consider the pragma_external case here.  */
38169       cp_parser_declaration (parser);
38170       if (parser->oacc_routine
38171           && !parser->oacc_routine->fndecl_seen)
38172         cp_ensure_no_oacc_routine (parser);
38173       else
38174         parser->oacc_routine = NULL;
38175       data.tokens.release ();
38176     }
38177 }
38178
38179 /* Finalize #pragma acc routine clauses after direct declarator has
38180    been parsed.  */
38181
38182 static tree
38183 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38184 {
38185   struct cp_token_cache *ce;
38186   cp_oacc_routine_data *data = parser->oacc_routine;
38187
38188   if (!data->error_seen && data->fndecl_seen)
38189     {
38190       error_at (data->loc,
38191                 "%<#pragma acc routine%> not immediately followed by "
38192                 "a single function declaration or definition");
38193       data->error_seen = true;
38194     }
38195   if (data->error_seen)
38196     return attrs;
38197
38198   gcc_checking_assert (data->tokens.length () == 1);
38199   ce = data->tokens[0];
38200
38201   cp_parser_push_lexer_for_tokens (parser, ce);
38202   parser->lexer->in_pragma = true;
38203   gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38204
38205   cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38206   gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38207   parser->oacc_routine->clauses
38208     = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38209                                   "#pragma acc routine", pragma_tok);
38210   cp_parser_pop_lexer (parser);
38211   /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38212      fndecl_seen.  */
38213
38214   return attrs;
38215 }
38216
38217 /* Apply any saved OpenACC routine clauses to a just-parsed
38218    declaration.  */
38219
38220 static void
38221 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38222 {
38223   if (__builtin_expect (parser->oacc_routine != NULL, 0))
38224     {
38225       /* Keep going if we're in error reporting mode.  */
38226       if (parser->oacc_routine->error_seen
38227           || fndecl == error_mark_node)
38228         return;
38229
38230       if (parser->oacc_routine->fndecl_seen)
38231         {
38232           error_at (parser->oacc_routine->loc,
38233                     "%<#pragma acc routine%> not immediately followed by"
38234                     " a single function declaration or definition");
38235           parser->oacc_routine = NULL;
38236           return;
38237         }
38238       if (TREE_CODE (fndecl) != FUNCTION_DECL)
38239         {
38240           cp_ensure_no_oacc_routine (parser);
38241           return;
38242         }
38243
38244       if (oacc_get_fn_attrib (fndecl))
38245         {
38246           error_at (parser->oacc_routine->loc,
38247                     "%<#pragma acc routine%> already applied to %qD", fndecl);
38248           parser->oacc_routine = NULL;
38249           return;
38250         }
38251
38252       if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38253         {
38254           error_at (parser->oacc_routine->loc,
38255                     TREE_USED (fndecl)
38256                     ? G_("%<#pragma acc routine%> must be applied before use")
38257                     : G_("%<#pragma acc routine%> must be applied before "
38258                          "definition"));
38259           parser->oacc_routine = NULL;
38260           return;
38261         }
38262
38263       /* Process the routine's dimension clauses.  */
38264       tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38265       oacc_replace_fn_attrib (fndecl, dims);
38266
38267       /* Add an "omp declare target" attribute.  */
38268       DECL_ATTRIBUTES (fndecl)
38269         = tree_cons (get_identifier ("omp declare target"),
38270                      NULL_TREE, DECL_ATTRIBUTES (fndecl));
38271
38272       /* Don't unset parser->oacc_routine here: we may still need it to
38273          diagnose wrong usage.  But, remember that we've used this "#pragma acc
38274          routine".  */
38275       parser->oacc_routine->fndecl_seen = true;
38276     }
38277 }
38278
38279 /* Main entry point to OpenMP statement pragmas.  */
38280
38281 static void
38282 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38283 {
38284   tree stmt;
38285   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38286   omp_clause_mask mask (0);
38287
38288   switch (cp_parser_pragma_kind (pragma_tok))
38289     {
38290     case PRAGMA_OACC_ATOMIC:
38291       cp_parser_omp_atomic (parser, pragma_tok);
38292       return;
38293     case PRAGMA_OACC_CACHE:
38294       stmt = cp_parser_oacc_cache (parser, pragma_tok);
38295       break;
38296     case PRAGMA_OACC_DATA:
38297       stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38298       break;
38299     case PRAGMA_OACC_ENTER_DATA:
38300       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38301       break;
38302     case PRAGMA_OACC_EXIT_DATA:
38303       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38304       break;
38305     case PRAGMA_OACC_HOST_DATA:
38306       stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38307       break;
38308     case PRAGMA_OACC_KERNELS:
38309     case PRAGMA_OACC_PARALLEL:
38310       strcpy (p_name, "#pragma acc");
38311       stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38312                                               if_p);
38313       break;
38314     case PRAGMA_OACC_LOOP:
38315       strcpy (p_name, "#pragma acc");
38316       stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38317                                   if_p);
38318       break;
38319     case PRAGMA_OACC_UPDATE:
38320       stmt = cp_parser_oacc_update (parser, pragma_tok);
38321       break;
38322     case PRAGMA_OACC_WAIT:
38323       stmt = cp_parser_oacc_wait (parser, pragma_tok);
38324       break;
38325     case PRAGMA_OMP_ATOMIC:
38326       cp_parser_omp_atomic (parser, pragma_tok);
38327       return;
38328     case PRAGMA_OMP_CRITICAL:
38329       stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38330       break;
38331     case PRAGMA_OMP_DISTRIBUTE:
38332       strcpy (p_name, "#pragma omp");
38333       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38334                                        if_p);
38335       break;
38336     case PRAGMA_OMP_FOR:
38337       strcpy (p_name, "#pragma omp");
38338       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38339                                 if_p);
38340       break;
38341     case PRAGMA_OMP_MASTER:
38342       stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38343       break;
38344     case PRAGMA_OMP_PARALLEL:
38345       strcpy (p_name, "#pragma omp");
38346       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38347                                      if_p);
38348       break;
38349     case PRAGMA_OMP_SECTIONS:
38350       strcpy (p_name, "#pragma omp");
38351       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38352       break;
38353     case PRAGMA_OMP_SIMD:
38354       strcpy (p_name, "#pragma omp");
38355       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38356                                  if_p);
38357       break;
38358     case PRAGMA_OMP_SINGLE:
38359       stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38360       break;
38361     case PRAGMA_OMP_TASK:
38362       stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38363       break;
38364     case PRAGMA_OMP_TASKGROUP:
38365       stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38366       break;
38367     case PRAGMA_OMP_TASKLOOP:
38368       strcpy (p_name, "#pragma omp");
38369       stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38370                                      if_p);
38371       break;
38372     case PRAGMA_OMP_TEAMS:
38373       strcpy (p_name, "#pragma omp");
38374       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38375                                   if_p);
38376       break;
38377     default:
38378       gcc_unreachable ();
38379     }
38380
38381   protected_set_expr_location (stmt, pragma_tok->location);
38382 }
38383 \f
38384 /* Transactional Memory parsing routines.  */
38385
38386 /* Parse a transaction attribute.
38387
38388    txn-attribute:
38389         attribute
38390         [ [ identifier ] ]
38391
38392    We use this instead of cp_parser_attributes_opt for transactions to avoid
38393    the pedwarn in C++98 mode.  */
38394
38395 static tree
38396 cp_parser_txn_attribute_opt (cp_parser *parser)
38397 {
38398   cp_token *token;
38399   tree attr_name, attr = NULL;
38400
38401   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38402     return cp_parser_attributes_opt (parser);
38403
38404   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38405     return NULL_TREE;
38406   cp_lexer_consume_token (parser->lexer);
38407   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38408     goto error1;
38409
38410   token = cp_lexer_peek_token (parser->lexer);
38411   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38412     {
38413       token = cp_lexer_consume_token (parser->lexer);
38414
38415       attr_name = (token->type == CPP_KEYWORD
38416                    /* For keywords, use the canonical spelling,
38417                       not the parsed identifier.  */
38418                    ? ridpointers[(int) token->keyword]
38419                    : token->u.value);
38420       attr = build_tree_list (attr_name, NULL_TREE);
38421     }
38422   else
38423     cp_parser_error (parser, "expected identifier");
38424
38425   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38426  error1:
38427   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38428   return attr;
38429 }
38430
38431 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38432
38433    transaction-statement:
38434      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38435        compound-statement
38436      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38437 */
38438
38439 static tree
38440 cp_parser_transaction (cp_parser *parser, cp_token *token)
38441 {
38442   unsigned char old_in = parser->in_transaction;
38443   unsigned char this_in = 1, new_in;
38444   enum rid keyword = token->keyword;
38445   tree stmt, attrs, noex;
38446
38447   cp_lexer_consume_token (parser->lexer);
38448
38449   if (keyword == RID_TRANSACTION_RELAXED
38450       || keyword == RID_SYNCHRONIZED)
38451     this_in |= TM_STMT_ATTR_RELAXED;
38452   else
38453     {
38454       attrs = cp_parser_txn_attribute_opt (parser);
38455       if (attrs)
38456         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38457     }
38458
38459   /* Parse a noexcept specification.  */
38460   if (keyword == RID_ATOMIC_NOEXCEPT)
38461     noex = boolean_true_node;
38462   else if (keyword == RID_ATOMIC_CANCEL)
38463     {
38464       /* cancel-and-throw is unimplemented.  */
38465       sorry ("atomic_cancel");
38466       noex = NULL_TREE;
38467     }
38468   else
38469     noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38470
38471   /* Keep track if we're in the lexical scope of an outer transaction.  */
38472   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38473
38474   stmt = begin_transaction_stmt (token->location, NULL, this_in);
38475
38476   parser->in_transaction = new_in;
38477   cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38478   parser->in_transaction = old_in;
38479
38480   finish_transaction_stmt (stmt, NULL, this_in, noex);
38481
38482   return stmt;
38483 }
38484
38485 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38486
38487    transaction-expression:
38488      __transaction_atomic txn-noexcept-spec[opt] ( expression )
38489      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38490 */
38491
38492 static tree
38493 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38494 {
38495   unsigned char old_in = parser->in_transaction;
38496   unsigned char this_in = 1;
38497   cp_token *token;
38498   tree expr, noex;
38499   bool noex_expr;
38500   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38501
38502   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38503       || keyword == RID_TRANSACTION_RELAXED);
38504
38505   if (!flag_tm)
38506     error_at (loc,
38507               keyword == RID_TRANSACTION_RELAXED
38508               ? G_("%<__transaction_relaxed%> without transactional memory "
38509                   "support enabled")
38510               : G_("%<__transaction_atomic%> without transactional memory "
38511                    "support enabled"));
38512
38513   token = cp_parser_require_keyword (parser, keyword,
38514       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38515           : RT_TRANSACTION_RELAXED));
38516   gcc_assert (token != NULL);
38517
38518   if (keyword == RID_TRANSACTION_RELAXED)
38519     this_in |= TM_STMT_ATTR_RELAXED;
38520
38521   /* Set this early.  This might mean that we allow transaction_cancel in
38522      an expression that we find out later actually has to be a constexpr.
38523      However, we expect that cxx_constant_value will be able to deal with
38524      this; also, if the noexcept has no constexpr, then what we parse next
38525      really is a transaction's body.  */
38526   parser->in_transaction = this_in;
38527
38528   /* Parse a noexcept specification.  */
38529   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38530                                                true);
38531
38532   if (!noex || !noex_expr
38533       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38534     {
38535       matching_parens parens;
38536       parens.require_open (parser);
38537
38538       expr = cp_parser_expression (parser);
38539       expr = finish_parenthesized_expr (expr);
38540
38541       parens.require_close (parser);
38542     }
38543   else
38544     {
38545       /* The only expression that is available got parsed for the noexcept
38546          already.  noexcept is true then.  */
38547       expr = noex;
38548       noex = boolean_true_node;
38549     }
38550
38551   expr = build_transaction_expr (token->location, expr, this_in, noex);
38552   parser->in_transaction = old_in;
38553
38554   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38555     return error_mark_node;
38556
38557   return (flag_tm ? expr : error_mark_node);
38558 }
38559
38560 /* Parse a function-transaction-block.
38561
38562    function-transaction-block:
38563      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38564          function-body
38565      __transaction_atomic txn-attribute[opt] function-try-block
38566      __transaction_relaxed ctor-initializer[opt] function-body
38567      __transaction_relaxed function-try-block
38568 */
38569
38570 static void
38571 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38572 {
38573   unsigned char old_in = parser->in_transaction;
38574   unsigned char new_in = 1;
38575   tree compound_stmt, stmt, attrs;
38576   cp_token *token;
38577
38578   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38579       || keyword == RID_TRANSACTION_RELAXED);
38580   token = cp_parser_require_keyword (parser, keyword,
38581       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38582           : RT_TRANSACTION_RELAXED));
38583   gcc_assert (token != NULL);
38584
38585   if (keyword == RID_TRANSACTION_RELAXED)
38586     new_in |= TM_STMT_ATTR_RELAXED;
38587   else
38588     {
38589       attrs = cp_parser_txn_attribute_opt (parser);
38590       if (attrs)
38591         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38592     }
38593
38594   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38595
38596   parser->in_transaction = new_in;
38597
38598   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38599     cp_parser_function_try_block (parser);
38600   else
38601     cp_parser_ctor_initializer_opt_and_function_body
38602       (parser, /*in_function_try_block=*/false);
38603
38604   parser->in_transaction = old_in;
38605
38606   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38607 }
38608
38609 /* Parse a __transaction_cancel statement.
38610
38611    cancel-statement:
38612      __transaction_cancel txn-attribute[opt] ;
38613      __transaction_cancel txn-attribute[opt] throw-expression ;
38614
38615    ??? Cancel and throw is not yet implemented.  */
38616
38617 static tree
38618 cp_parser_transaction_cancel (cp_parser *parser)
38619 {
38620   cp_token *token;
38621   bool is_outer = false;
38622   tree stmt, attrs;
38623
38624   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38625                                      RT_TRANSACTION_CANCEL);
38626   gcc_assert (token != NULL);
38627
38628   attrs = cp_parser_txn_attribute_opt (parser);
38629   if (attrs)
38630     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38631
38632   /* ??? Parse cancel-and-throw here.  */
38633
38634   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38635
38636   if (!flag_tm)
38637     {
38638       error_at (token->location, "%<__transaction_cancel%> without "
38639                 "transactional memory support enabled");
38640       return error_mark_node;
38641     }
38642   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38643     {
38644       error_at (token->location, "%<__transaction_cancel%> within a "
38645                 "%<__transaction_relaxed%>");
38646       return error_mark_node;
38647     }
38648   else if (is_outer)
38649     {
38650       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38651           && !is_tm_may_cancel_outer (current_function_decl))
38652         {
38653           error_at (token->location, "outer %<__transaction_cancel%> not "
38654                     "within outer %<__transaction_atomic%>");
38655           error_at (token->location,
38656                     "  or a %<transaction_may_cancel_outer%> function");
38657           return error_mark_node;
38658         }
38659     }
38660   else if (parser->in_transaction == 0)
38661     {
38662       error_at (token->location, "%<__transaction_cancel%> not within "
38663                 "%<__transaction_atomic%>");
38664       return error_mark_node;
38665     }
38666
38667   stmt = build_tm_abort_call (token->location, is_outer);
38668   add_stmt (stmt);
38669
38670   return stmt;
38671 }
38672 \f
38673 /* The parser.  */
38674
38675 static GTY (()) cp_parser *the_parser;
38676
38677 \f
38678 /* Special handling for the first token or line in the file.  The first
38679    thing in the file might be #pragma GCC pch_preprocess, which loads a
38680    PCH file, which is a GC collection point.  So we need to handle this
38681    first pragma without benefit of an existing lexer structure.
38682
38683    Always returns one token to the caller in *FIRST_TOKEN.  This is
38684    either the true first token of the file, or the first token after
38685    the initial pragma.  */
38686
38687 static void
38688 cp_parser_initial_pragma (cp_token *first_token)
38689 {
38690   tree name = NULL;
38691
38692   cp_lexer_get_preprocessor_token (NULL, first_token);
38693   if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38694     return;
38695
38696   cp_lexer_get_preprocessor_token (NULL, first_token);
38697   if (first_token->type == CPP_STRING)
38698     {
38699       name = first_token->u.value;
38700
38701       cp_lexer_get_preprocessor_token (NULL, first_token);
38702       if (first_token->type != CPP_PRAGMA_EOL)
38703         error_at (first_token->location,
38704                   "junk at end of %<#pragma GCC pch_preprocess%>");
38705     }
38706   else
38707     error_at (first_token->location, "expected string literal");
38708
38709   /* Skip to the end of the pragma.  */
38710   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38711     cp_lexer_get_preprocessor_token (NULL, first_token);
38712
38713   /* Now actually load the PCH file.  */
38714   if (name)
38715     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38716
38717   /* Read one more token to return to our caller.  We have to do this
38718      after reading the PCH file in, since its pointers have to be
38719      live.  */
38720   cp_lexer_get_preprocessor_token (NULL, first_token);
38721 }
38722
38723 /* Parse a pragma GCC ivdep.  */
38724
38725 static bool
38726 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38727 {
38728   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38729   return true;
38730 }
38731
38732 /* Parse a pragma GCC unroll.  */
38733
38734 static unsigned short
38735 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38736 {
38737   location_t location = cp_lexer_peek_token (parser->lexer)->location;
38738   tree expr = cp_parser_constant_expression (parser);
38739   unsigned short unroll;
38740   expr = maybe_constant_value (expr);
38741   HOST_WIDE_INT lunroll = 0;
38742   if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38743       || TREE_CODE (expr) != INTEGER_CST
38744       || (lunroll = tree_to_shwi (expr)) < 0
38745       || lunroll >= USHRT_MAX)
38746     {
38747       error_at (location, "%<#pragma GCC unroll%> requires an"
38748                 " assignment-expression that evaluates to a non-negative"
38749                 " integral constant less than %u", USHRT_MAX);
38750       unroll = 0;
38751     }
38752   else
38753     {
38754       unroll = (unsigned short)lunroll;
38755       if (unroll == 0)
38756         unroll = 1;
38757     }
38758   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38759   return unroll;
38760 }
38761
38762 /* Normal parsing of a pragma token.  Here we can (and must) use the
38763    regular lexer.  */
38764
38765 static bool
38766 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38767 {
38768   cp_token *pragma_tok;
38769   unsigned int id;
38770   tree stmt;
38771   bool ret;
38772
38773   pragma_tok = cp_lexer_consume_token (parser->lexer);
38774   gcc_assert (pragma_tok->type == CPP_PRAGMA);
38775   parser->lexer->in_pragma = true;
38776
38777   id = cp_parser_pragma_kind (pragma_tok);
38778   if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38779     cp_ensure_no_omp_declare_simd (parser);
38780   switch (id)
38781     {
38782     case PRAGMA_GCC_PCH_PREPROCESS:
38783       error_at (pragma_tok->location,
38784                 "%<#pragma GCC pch_preprocess%> must be first");
38785       break;
38786
38787     case PRAGMA_OMP_BARRIER:
38788       switch (context)
38789         {
38790         case pragma_compound:
38791           cp_parser_omp_barrier (parser, pragma_tok);
38792           return false;
38793         case pragma_stmt:
38794           error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38795                     "used in compound statements", "omp barrier");
38796           break;
38797         default:
38798           goto bad_stmt;
38799         }
38800       break;
38801
38802     case PRAGMA_OMP_FLUSH:
38803       switch (context)
38804         {
38805         case pragma_compound:
38806           cp_parser_omp_flush (parser, pragma_tok);
38807           return false;
38808         case pragma_stmt:
38809           error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38810                     "used in compound statements", "omp flush");
38811           break;
38812         default:
38813           goto bad_stmt;
38814         }
38815       break;
38816
38817     case PRAGMA_OMP_TASKWAIT:
38818       switch (context)
38819         {
38820         case pragma_compound:
38821           cp_parser_omp_taskwait (parser, pragma_tok);
38822           return false;
38823         case pragma_stmt:
38824           error_at (pragma_tok->location,
38825                     "%<#pragma %s%> may only be used in compound statements",
38826                     "omp taskwait");
38827           break;
38828         default:
38829           goto bad_stmt;
38830         }
38831       break;
38832
38833     case PRAGMA_OMP_TASKYIELD:
38834       switch (context)
38835         {
38836         case pragma_compound:
38837           cp_parser_omp_taskyield (parser, pragma_tok);
38838           return false;
38839         case pragma_stmt:
38840           error_at (pragma_tok->location,
38841                     "%<#pragma %s%> may only be used in compound statements",
38842                     "omp taskyield");
38843           break;
38844         default:
38845           goto bad_stmt;
38846         }
38847       break;
38848
38849     case PRAGMA_OMP_CANCEL:
38850       switch (context)
38851         {
38852         case pragma_compound:
38853           cp_parser_omp_cancel (parser, pragma_tok);
38854           return false;
38855         case pragma_stmt:
38856           error_at (pragma_tok->location,
38857                     "%<#pragma %s%> may only be used in compound statements",
38858                     "omp cancel");
38859           break;
38860         default:
38861           goto bad_stmt;
38862         }
38863       break;
38864
38865     case PRAGMA_OMP_CANCELLATION_POINT:
38866       cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38867       return false;
38868
38869     case PRAGMA_OMP_THREADPRIVATE:
38870       cp_parser_omp_threadprivate (parser, pragma_tok);
38871       return false;
38872
38873     case PRAGMA_OMP_DECLARE:
38874       return cp_parser_omp_declare (parser, pragma_tok, context);
38875
38876     case PRAGMA_OACC_DECLARE:
38877       cp_parser_oacc_declare (parser, pragma_tok);
38878       return false;
38879
38880     case PRAGMA_OACC_ENTER_DATA:
38881       if (context == pragma_stmt)
38882         {
38883           error_at (pragma_tok->location,
38884                     "%<#pragma %s%> may only be used in compound statements",
38885                     "acc enter data");
38886           break;
38887         }
38888       else if (context != pragma_compound)
38889         goto bad_stmt;
38890       cp_parser_omp_construct (parser, pragma_tok, if_p);
38891       return true;
38892
38893     case PRAGMA_OACC_EXIT_DATA:
38894       if (context == pragma_stmt)
38895         {
38896           error_at (pragma_tok->location,
38897                     "%<#pragma %s%> may only be used in compound statements",
38898                     "acc exit data");
38899           break;
38900         }
38901       else if (context != pragma_compound)
38902         goto bad_stmt;
38903       cp_parser_omp_construct (parser, pragma_tok, if_p);
38904       return true;
38905
38906     case PRAGMA_OACC_ROUTINE:
38907       if (context != pragma_external)
38908         {
38909           error_at (pragma_tok->location,
38910                     "%<#pragma acc routine%> must be at file scope");
38911           break;
38912         }
38913       cp_parser_oacc_routine (parser, pragma_tok, context);
38914       return false;
38915
38916     case PRAGMA_OACC_UPDATE:
38917       if (context == pragma_stmt)
38918         {
38919           error_at (pragma_tok->location,
38920                     "%<#pragma %s%> may only be used in compound statements",
38921                     "acc update");
38922           break;
38923         }
38924       else if (context != pragma_compound)
38925         goto bad_stmt;
38926       cp_parser_omp_construct (parser, pragma_tok, if_p);
38927       return true;
38928
38929     case PRAGMA_OACC_WAIT:
38930       if (context == pragma_stmt)
38931         {
38932           error_at (pragma_tok->location,
38933                     "%<#pragma %s%> may only be used in compound statements",
38934                     "acc wait");
38935           break;
38936         }
38937       else if (context != pragma_compound)
38938         goto bad_stmt;
38939       cp_parser_omp_construct (parser, pragma_tok, if_p);
38940       return true;
38941
38942     case PRAGMA_OACC_ATOMIC:
38943     case PRAGMA_OACC_CACHE:
38944     case PRAGMA_OACC_DATA:
38945     case PRAGMA_OACC_HOST_DATA:
38946     case PRAGMA_OACC_KERNELS:
38947     case PRAGMA_OACC_PARALLEL:
38948     case PRAGMA_OACC_LOOP:
38949     case PRAGMA_OMP_ATOMIC:
38950     case PRAGMA_OMP_CRITICAL:
38951     case PRAGMA_OMP_DISTRIBUTE:
38952     case PRAGMA_OMP_FOR:
38953     case PRAGMA_OMP_MASTER:
38954     case PRAGMA_OMP_PARALLEL:
38955     case PRAGMA_OMP_SECTIONS:
38956     case PRAGMA_OMP_SIMD:
38957     case PRAGMA_OMP_SINGLE:
38958     case PRAGMA_OMP_TASK:
38959     case PRAGMA_OMP_TASKGROUP:
38960     case PRAGMA_OMP_TASKLOOP:
38961     case PRAGMA_OMP_TEAMS:
38962       if (context != pragma_stmt && context != pragma_compound)
38963         goto bad_stmt;
38964       stmt = push_omp_privatization_clauses (false);
38965       cp_parser_omp_construct (parser, pragma_tok, if_p);
38966       pop_omp_privatization_clauses (stmt);
38967       return true;
38968
38969     case PRAGMA_OMP_ORDERED:
38970       if (context != pragma_stmt && context != pragma_compound)
38971         goto bad_stmt;
38972       stmt = push_omp_privatization_clauses (false);
38973       ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38974       pop_omp_privatization_clauses (stmt);
38975       return ret;
38976
38977     case PRAGMA_OMP_TARGET:
38978       if (context != pragma_stmt && context != pragma_compound)
38979         goto bad_stmt;
38980       stmt = push_omp_privatization_clauses (false);
38981       ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38982       pop_omp_privatization_clauses (stmt);
38983       return ret;
38984
38985     case PRAGMA_OMP_END_DECLARE_TARGET:
38986       cp_parser_omp_end_declare_target (parser, pragma_tok);
38987       return false;
38988
38989     case PRAGMA_OMP_SECTION:
38990       error_at (pragma_tok->location, 
38991                 "%<#pragma omp section%> may only be used in "
38992                 "%<#pragma omp sections%> construct");
38993       break;
38994
38995     case PRAGMA_IVDEP:
38996       {
38997         if (context == pragma_external)
38998           {
38999             error_at (pragma_tok->location,
39000                       "%<#pragma GCC ivdep%> must be inside a function");
39001             break;
39002           }
39003         const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39004         unsigned short unroll;
39005         cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39006         if (tok->type == CPP_PRAGMA
39007             && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39008           {
39009             tok = cp_lexer_consume_token (parser->lexer);
39010             unroll = cp_parser_pragma_unroll (parser, tok);
39011             tok = cp_lexer_peek_token (the_parser->lexer);
39012           }
39013         else
39014           unroll = 0;
39015         if (tok->type != CPP_KEYWORD
39016             || (tok->keyword != RID_FOR
39017                 && tok->keyword != RID_WHILE
39018                 && tok->keyword != RID_DO))
39019           {
39020             cp_parser_error (parser, "for, while or do statement expected");
39021             return false;
39022           }
39023         cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39024         return true;
39025       }
39026
39027     case PRAGMA_UNROLL:
39028       {
39029         if (context == pragma_external)
39030           {
39031             error_at (pragma_tok->location,
39032                       "%<#pragma GCC unroll%> must be inside a function");
39033             break;
39034           }
39035         const unsigned short unroll
39036           = cp_parser_pragma_unroll (parser, pragma_tok);
39037         bool ivdep;
39038         cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39039         if (tok->type == CPP_PRAGMA
39040             && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39041           {
39042             tok = cp_lexer_consume_token (parser->lexer);
39043             ivdep = cp_parser_pragma_ivdep (parser, tok);
39044             tok = cp_lexer_peek_token (the_parser->lexer);
39045           }
39046         else
39047           ivdep = false;
39048         if (tok->type != CPP_KEYWORD
39049             || (tok->keyword != RID_FOR
39050                 && tok->keyword != RID_WHILE
39051                 && tok->keyword != RID_DO))
39052           {
39053             cp_parser_error (parser, "for, while or do statement expected");
39054             return false;
39055           }
39056         cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39057         return true;
39058       }
39059
39060     default:
39061       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39062       c_invoke_pragma_handler (id);
39063       break;
39064
39065     bad_stmt:
39066       cp_parser_error (parser, "expected declaration specifiers");
39067       break;
39068     }
39069
39070   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39071   return false;
39072 }
39073
39074 /* The interface the pragma parsers have to the lexer.  */
39075
39076 enum cpp_ttype
39077 pragma_lex (tree *value, location_t *loc)
39078 {
39079   cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39080   enum cpp_ttype ret = tok->type;
39081
39082   *value = tok->u.value;
39083   if (loc)
39084     *loc = tok->location;
39085
39086   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39087     ret = CPP_EOF;
39088   else if (ret == CPP_STRING)
39089     *value = cp_parser_string_literal (the_parser, false, false);
39090   else
39091     {
39092       if (ret == CPP_KEYWORD)
39093         ret = CPP_NAME;
39094       cp_lexer_consume_token (the_parser->lexer);
39095     }
39096
39097   return ret;
39098 }
39099
39100 \f
39101 /* External interface.  */
39102
39103 /* Parse one entire translation unit.  */
39104
39105 void
39106 c_parse_file (void)
39107 {
39108   static bool already_called = false;
39109
39110   if (already_called)
39111     fatal_error (input_location,
39112                  "inter-module optimizations not implemented for C++");
39113   already_called = true;
39114
39115   the_parser = cp_parser_new ();
39116   push_deferring_access_checks (flag_access_control
39117                                 ? dk_no_deferred : dk_no_check);
39118   cp_parser_translation_unit (the_parser);
39119   the_parser = NULL;
39120 }
39121
39122 /* Create an identifier for a generic parameter type (a synthesized
39123    template parameter implied by `auto' or a concept identifier). */
39124
39125 static GTY(()) int generic_parm_count;
39126 static tree
39127 make_generic_type_name ()
39128 {
39129   char buf[32];
39130   sprintf (buf, "auto:%d", ++generic_parm_count);
39131   return get_identifier (buf);
39132 }
39133
39134 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39135    (creating a new template parameter list if necessary).  Returns the newly
39136    created template type parm.  */
39137
39138 static tree
39139 synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
39140 {
39141   gcc_assert (current_binding_level->kind == sk_function_parms);
39142
39143    /* Before committing to modifying any scope, if we're in an
39144       implicit template scope, and we're trying to synthesize a
39145       constrained parameter, try to find a previous parameter with
39146       the same name.  This is the same-type rule for abbreviated
39147       function templates.
39148
39149       NOTE: We can generate implicit parameters when tentatively
39150       parsing a nested name specifier, only to reject that parse
39151       later. However, matching the same template-id as part of a
39152       direct-declarator should generate an identical template
39153       parameter, so this rule will merge them. */
39154   if (parser->implicit_template_scope && constr)
39155     {
39156       tree t = parser->implicit_template_parms;
39157       while (t)
39158         {
39159           if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39160             {
39161               tree d = TREE_VALUE (t);
39162               if (TREE_CODE (d) == PARM_DECL)
39163                 /* Return the TEMPLATE_PARM_INDEX.  */
39164                 d = DECL_INITIAL (d);
39165               return d;
39166             }
39167           t = TREE_CHAIN (t);
39168         }
39169     }
39170
39171   /* We are either continuing a function template that already contains implicit
39172      template parameters, creating a new fully-implicit function template, or
39173      extending an existing explicit function template with implicit template
39174      parameters.  */
39175
39176   cp_binding_level *const entry_scope = current_binding_level;
39177
39178   bool become_template = false;
39179   cp_binding_level *parent_scope = 0;
39180
39181   if (parser->implicit_template_scope)
39182     {
39183       gcc_assert (parser->implicit_template_parms);
39184
39185       current_binding_level = parser->implicit_template_scope;
39186     }
39187   else
39188     {
39189       /* Roll back to the existing template parameter scope (in the case of
39190          extending an explicit function template) or introduce a new template
39191          parameter scope ahead of the function parameter scope (or class scope
39192          in the case of out-of-line member definitions).  The function scope is
39193          added back after template parameter synthesis below.  */
39194
39195       cp_binding_level *scope = entry_scope;
39196
39197       while (scope->kind == sk_function_parms)
39198         {
39199           parent_scope = scope;
39200           scope = scope->level_chain;
39201         }
39202       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39203         {
39204           /* If not defining a class, then any class scope is a scope level in
39205              an out-of-line member definition.  In this case simply wind back
39206              beyond the first such scope to inject the template parameter list.
39207              Otherwise wind back to the class being defined.  The latter can
39208              occur in class member friend declarations such as:
39209
39210                class A {
39211                  void foo (auto);
39212                };
39213                class B {
39214                  friend void A::foo (auto);
39215                };
39216
39217             The template parameter list synthesized for the friend declaration
39218             must be injected in the scope of 'B'.  This can also occur in
39219             erroneous cases such as:
39220
39221                struct A {
39222                  struct B {
39223                    void foo (auto);
39224                  };
39225                  void B::foo (auto) {}
39226                };
39227
39228             Here the attempted definition of 'B::foo' within 'A' is ill-formed
39229             but, nevertheless, the template parameter list synthesized for the
39230             declarator should be injected into the scope of 'A' as if the
39231             ill-formed template was specified explicitly.  */
39232
39233           while (scope->kind == sk_class && !scope->defining_class_p)
39234             {
39235               parent_scope = scope;
39236               scope = scope->level_chain;
39237             }
39238         }
39239
39240       current_binding_level = scope;
39241
39242       if (scope->kind != sk_template_parms
39243           || !function_being_declared_is_template_p (parser))
39244         {
39245           /* Introduce a new template parameter list for implicit template
39246              parameters.  */
39247
39248           become_template = true;
39249
39250           parser->implicit_template_scope
39251               = begin_scope (sk_template_parms, NULL);
39252
39253           ++processing_template_decl;
39254
39255           parser->fully_implicit_function_template_p = true;
39256           ++parser->num_template_parameter_lists;
39257         }
39258       else
39259         {
39260           /* Synthesize implicit template parameters at the end of the explicit
39261              template parameter list.  */
39262
39263           gcc_assert (current_template_parms);
39264
39265           parser->implicit_template_scope = scope;
39266
39267           tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39268           parser->implicit_template_parms
39269             = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39270         }
39271     }
39272
39273   /* Synthesize a new template parameter and track the current template
39274      parameter chain with implicit_template_parms.  */
39275
39276   tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39277   tree synth_id = make_generic_type_name ();
39278   tree synth_tmpl_parm;
39279   bool non_type = false;
39280
39281   if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39282     synth_tmpl_parm
39283       = finish_template_type_parm (class_type_node, synth_id);
39284   else if (TREE_CODE (proto) == TEMPLATE_DECL)
39285     synth_tmpl_parm
39286       = finish_constrained_template_template_parm (proto, synth_id);
39287   else
39288     {
39289       synth_tmpl_parm = copy_decl (proto);
39290       DECL_NAME (synth_tmpl_parm) = synth_id;
39291       non_type = true;
39292     }
39293
39294   // Attach the constraint to the parm before processing.
39295   tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39296   TREE_TYPE (node) = constr;
39297   tree new_parm
39298     = process_template_parm (parser->implicit_template_parms,
39299                              input_location,
39300                              node,
39301                              /*non_type=*/non_type,
39302                              /*param_pack=*/false);
39303
39304   // Chain the new parameter to the list of implicit parameters.
39305   if (parser->implicit_template_parms)
39306     parser->implicit_template_parms
39307       = TREE_CHAIN (parser->implicit_template_parms);
39308   else
39309     parser->implicit_template_parms = new_parm;
39310
39311   tree new_decl = get_local_decls ();
39312   if (non_type)
39313     /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
39314     new_decl = DECL_INITIAL (new_decl);
39315
39316   /* If creating a fully implicit function template, start the new implicit
39317      template parameter list with this synthesized type, otherwise grow the
39318      current template parameter list.  */
39319
39320   if (become_template)
39321     {
39322       parent_scope->level_chain = current_binding_level;
39323
39324       tree new_parms = make_tree_vec (1);
39325       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39326       current_template_parms = tree_cons (size_int (processing_template_decl),
39327                                           new_parms, current_template_parms);
39328     }
39329   else
39330     {
39331       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39332       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39333       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39334       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39335     }
39336
39337   // If the new parameter was constrained, we need to add that to the
39338   // constraints in the template parameter list.
39339   if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39340     {
39341       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39342       reqs = conjoin_constraints (reqs, req);
39343       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39344     }
39345
39346   current_binding_level = entry_scope;
39347
39348   return new_decl;
39349 }
39350
39351 /* Finish the declaration of a fully implicit function template.  Such a
39352    template has no explicit template parameter list so has not been through the
39353    normal template head and tail processing.  synthesize_implicit_template_parm
39354    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
39355    provided if the declaration is a class member such that its template
39356    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
39357    form is returned.  Otherwise NULL_TREE is returned. */
39358
39359 static tree
39360 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39361 {
39362   gcc_assert (parser->fully_implicit_function_template_p);
39363
39364   if (member_decl_opt && member_decl_opt != error_mark_node
39365       && DECL_VIRTUAL_P (member_decl_opt))
39366     {
39367       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39368                 "implicit templates may not be %<virtual%>");
39369       DECL_VIRTUAL_P (member_decl_opt) = false;
39370     }
39371
39372   if (member_decl_opt)
39373     member_decl_opt = finish_member_template_decl (member_decl_opt);
39374   end_template_decl ();
39375
39376   parser->fully_implicit_function_template_p = false;
39377   parser->implicit_template_parms = 0;
39378   parser->implicit_template_scope = 0;
39379   --parser->num_template_parameter_lists;
39380
39381   return member_decl_opt;
39382 }
39383
39384 /* Like finish_fully_implicit_template, but to be used in error
39385    recovery, rearranging scopes so that we restore the state we had
39386    before synthesize_implicit_template_parm inserted the implement
39387    template parms scope.  */
39388
39389 static void
39390 abort_fully_implicit_template (cp_parser *parser)
39391 {
39392   cp_binding_level *return_to_scope = current_binding_level;
39393
39394   if (parser->implicit_template_scope
39395       && return_to_scope != parser->implicit_template_scope)
39396     {
39397       cp_binding_level *child = return_to_scope;
39398       for (cp_binding_level *scope = child->level_chain;
39399            scope != parser->implicit_template_scope;
39400            scope = child->level_chain)
39401         child = scope;
39402       child->level_chain = parser->implicit_template_scope->level_chain;
39403       parser->implicit_template_scope->level_chain = return_to_scope;
39404       current_binding_level = parser->implicit_template_scope;
39405     }
39406   else
39407     return_to_scope = return_to_scope->level_chain;
39408
39409   finish_fully_implicit_template (parser, NULL);
39410
39411   gcc_assert (current_binding_level == return_to_scope);
39412 }
39413
39414 /* Helper function for diagnostics that have complained about things
39415    being used with 'extern "C"' linkage.
39416
39417    Attempt to issue a note showing where the 'extern "C"' linkage began.  */
39418
39419 void
39420 maybe_show_extern_c_location (void)
39421 {
39422   if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39423     inform (the_parser->innermost_linkage_specification_location,
39424             "%<extern \"C\"%> linkage started here");
39425 }
39426
39427 #include "gt-cp-parser.h"