1/* -*- C++ -*- Parser.
2   Copyright (C) 2000-2022 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
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3.  If not see
19<http://www.gnu.org/licenses/>.  */
20
21#include "config.h"
22#define INCLUDE_MEMORY
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 "cp-name-hint.h"
47#include "memmodel.h"
48#include "c-family/known-headers.h"
49
50
51/* The lexer.  */
52
53/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
54   and c-lex.cc) and the C++ parser.  */
55
56/* The various kinds of non integral constant we encounter. */
57enum non_integral_constant {
58  NIC_NONE,
59  /* floating-point literal */
60  NIC_FLOAT,
61  /* %<this%> */
62  NIC_THIS,
63  /* %<__FUNCTION__%> */
64  NIC_FUNC_NAME,
65  /* %<__PRETTY_FUNCTION__%> */
66  NIC_PRETTY_FUNC,
67  /* %<__func__%> */
68  NIC_C99_FUNC,
69  /* "%<va_arg%> */
70  NIC_VA_ARG,
71  /* a cast */
72  NIC_CAST,
73  /* %<typeid%> operator */
74  NIC_TYPEID,
75  /* non-constant compound literals */
76  NIC_NCC,
77  /* a function call */
78  NIC_FUNC_CALL,
79  /* an increment */
80  NIC_INC,
81  /* an decrement */
82  NIC_DEC,
83  /* an array reference */
84  NIC_ARRAY_REF,
85  /* %<->%> */
86  NIC_ARROW,
87  /* %<.%> */
88  NIC_POINT,
89  /* the address of a label */
90  NIC_ADDR_LABEL,
91  /* %<*%> */
92  NIC_STAR,
93  /* %<&%> */
94  NIC_ADDR,
95  /* %<++%> */
96  NIC_PREINCREMENT,
97  /* %<--%> */
98  NIC_PREDECREMENT,
99  /* %<new%> */
100  NIC_NEW,
101  /* %<delete%> */
102  NIC_DEL,
103  /* calls to overloaded operators */
104  NIC_OVERLOADED,
105  /* an assignment */
106  NIC_ASSIGNMENT,
107  /* a comma operator */
108  NIC_COMMA,
109  /* a call to a constructor */
110  NIC_CONSTRUCTOR,
111  /* a transaction expression */
112  NIC_TRANSACTION
113};
114
115/* The various kinds of errors about name-lookup failing. */
116enum name_lookup_error {
117  /* NULL */
118  NLE_NULL,
119  /* is not a type */
120  NLE_TYPE,
121  /* is not a class or namespace */
122  NLE_CXX98,
123  /* is not a class, namespace, or enumeration */
124  NLE_NOT_CXX98
125};
126
127/* The various kinds of required token */
128enum required_token {
129  RT_NONE,
130  RT_SEMICOLON,  /* ';' */
131  RT_OPEN_PAREN, /* '(' */
132  RT_CLOSE_BRACE, /* '}' */
133  RT_OPEN_BRACE,  /* '{' */
134  RT_CLOSE_SQUARE, /* ']' */
135  RT_OPEN_SQUARE,  /* '[' */
136  RT_COMMA, /* ',' */
137  RT_SCOPE, /* '::' */
138  RT_LESS, /* '<' */
139  RT_GREATER, /* '>' */
140  RT_EQ, /* '=' */
141  RT_ELLIPSIS, /* '...' */
142  RT_MULT, /* '*' */
143  RT_COMPL, /* '~' */
144  RT_COLON, /* ':' */
145  RT_COLON_SCOPE, /* ':' or '::' */
146  RT_CLOSE_PAREN, /* ')' */
147  RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
148  RT_PRAGMA_EOL, /* end of line */
149  RT_NAME, /* identifier */
150
151  /* The type is CPP_KEYWORD */
152  RT_NEW, /* new */
153  RT_DELETE, /* delete */
154  RT_RETURN, /* return */
155  RT_WHILE, /* while */
156  RT_EXTERN, /* extern */
157  RT_STATIC_ASSERT, /* static_assert */
158  RT_DECLTYPE, /* decltype */
159  RT_OPERATOR, /* operator */
160  RT_CLASS, /* class */
161  RT_TEMPLATE, /* template */
162  RT_NAMESPACE, /* namespace */
163  RT_USING, /* using */
164  RT_ASM, /* asm */
165  RT_TRY, /* try */
166  RT_CATCH, /* catch */
167  RT_THROW, /* throw */
168  RT_AUTO, /* auto */
169  RT_LABEL, /* __label__ */
170  RT_AT_TRY, /* @try */
171  RT_AT_SYNCHRONIZED, /* @synchronized */
172  RT_AT_THROW, /* @throw */
173
174  RT_SELECT,  /* selection-statement */
175  RT_ITERATION, /* iteration-statement */
176  RT_JUMP, /* jump-statement */
177  RT_CLASS_KEY, /* class-key */
178  RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179  RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180  RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181  RT_TRANSACTION_CANCEL, /* __transaction_cancel */
182
183  RT_CO_YIELD /* co_yield */
184};
185
186/* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187   reverting it on destruction.  */
188
189class type_id_in_expr_sentinel
190{
191  cp_parser *parser;
192  bool saved;
193public:
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
204static cp_lexer *cp_lexer_new_main
205  (void);
206static cp_lexer *cp_lexer_new_from_tokens
207  (cp_token_cache *tokens);
208static void cp_lexer_destroy
209  (cp_lexer *);
210static int cp_lexer_saving_tokens
211  (const cp_lexer *);
212static cp_token *cp_lexer_token_at
213  (cp_lexer *, cp_token_position);
214static void cp_lexer_get_preprocessor_token
215  (unsigned, cp_token *);
216static inline cp_token *cp_lexer_peek_token
217  (cp_lexer *);
218static cp_token *cp_lexer_peek_nth_token
219  (cp_lexer *, size_t);
220static inline bool cp_lexer_next_token_is
221  (cp_lexer *, enum cpp_ttype);
222static bool cp_lexer_next_token_is_not
223  (cp_lexer *, enum cpp_ttype);
224static bool cp_lexer_next_token_is_keyword
225  (cp_lexer *, enum rid);
226static cp_token *cp_lexer_consume_token
227  (cp_lexer *);
228static void cp_lexer_purge_token
229  (cp_lexer *);
230static void cp_lexer_purge_tokens_after
231  (cp_lexer *, cp_token_position);
232static void cp_lexer_save_tokens
233  (cp_lexer *);
234static void cp_lexer_commit_tokens
235  (cp_lexer *);
236static void cp_lexer_rollback_tokens
237  (cp_lexer *);
238static void cp_lexer_print_token
239  (FILE *, cp_token *);
240static inline bool cp_lexer_debugging_p
241  (cp_lexer *);
242static void cp_lexer_start_debugging
243  (cp_lexer *) ATTRIBUTE_UNUSED;
244static void cp_lexer_stop_debugging
245  (cp_lexer *) ATTRIBUTE_UNUSED;
246
247static cp_token_cache *cp_token_cache_new
248  (cp_token *, cp_token *);
249static tree cp_parser_late_noexcept_specifier
250  (cp_parser *, tree);
251static void noexcept_override_late_checks
252  (tree, tree);
253
254static void cp_parser_initial_pragma
255  (cp_token *);
256
257static bool cp_parser_omp_declare_reduction_exprs
258  (tree, cp_parser *);
259static void cp_finalize_oacc_routine
260  (cp_parser *, tree, bool);
261
262/* Manifest constants.  */
263#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264#define CP_SAVED_TOKEN_STACK 5
265
266/* Variables.  */
267
268/* The stream to which debugging output should be written.  */
269static FILE *cp_lexer_debug_stream;
270
271/* Nonzero if we are parsing an unevaluated operand: an operand to
272   sizeof, typeof, or alignof.  */
273int cp_unevaluated_operand;
274
275/* Dump up to NUM tokens in BUFFER to FILE starting with token
276   START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
277   first token in BUFFER.  If NUM is 0, dump all the tokens.  If
278   CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279   highlighted by surrounding it in [[ ]].  */
280
281static void
282cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283		      cp_token *start_token, unsigned num,
284		      cp_token *curr_token)
285{
286  unsigned i, nprinted;
287  cp_token *token;
288  bool do_print;
289
290  fprintf (file, "%u tokens\n", vec_safe_length (buffer));
291
292  if (buffer == NULL)
293    return;
294
295  if (num == 0)
296    num = buffer->length ();
297
298  if (start_token == NULL)
299    start_token = buffer->address ();
300
301  if (start_token > buffer->address ())
302    {
303      cp_lexer_print_token (file, &(*buffer)[0]);
304      fprintf (file, " ... ");
305    }
306
307  do_print = false;
308  nprinted = 0;
309  for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
310    {
311      if (token == start_token)
312	do_print = true;
313
314      if (!do_print)
315	continue;
316
317      nprinted++;
318      if (token == curr_token)
319	fprintf (file, "[[");
320
321      cp_lexer_print_token (file, token);
322
323      if (token == curr_token)
324	fprintf (file, "]]");
325
326      switch (token->type)
327	{
328	  case CPP_SEMICOLON:
329	  case CPP_OPEN_BRACE:
330	  case CPP_CLOSE_BRACE:
331	  case CPP_EOF:
332	    fputc ('\n', file);
333	    break;
334
335	  default:
336	    fputc (' ', file);
337	}
338    }
339
340  if (i == num && i < buffer->length ())
341    {
342      fprintf (file, " ... ");
343      cp_lexer_print_token (file, &buffer->last ());
344    }
345
346  fprintf (file, "\n");
347}
348
349
350/* Dump all tokens in BUFFER to stderr.  */
351
352void
353cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
354{
355  cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356}
357
358DEBUG_FUNCTION void
359debug (vec<cp_token, va_gc> &ref)
360{
361  cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362}
363
364DEBUG_FUNCTION void
365debug (vec<cp_token, va_gc> *ptr)
366{
367  if (ptr)
368    debug (*ptr);
369  else
370    fprintf (stderr, "<nil>\n");
371}
372
373
374/* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
375   description for T.  */
376
377static void
378cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
379{
380  if (t)
381    {
382      fprintf (file, "%s: ", desc);
383      print_node_brief (file, "", t, 0);
384    }
385}
386
387
388/* Dump parser context C to FILE.  */
389
390static void
391cp_debug_print_context (FILE *file, cp_parser_context *c)
392{
393  const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394  fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395  print_node_brief (file, "", c->object_type, 0);
396  fprintf (file, "}\n");
397}
398
399
400/* Print the stack of parsing contexts to FILE starting with FIRST.  */
401
402static void
403cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
404{
405  unsigned i;
406  cp_parser_context *c;
407
408  fprintf (file, "Parsing context stack:\n");
409  for (i = 0, c = first; c; c = c->next, i++)
410    {
411      fprintf (file, "\t#%u: ", i);
412      cp_debug_print_context (file, c);
413    }
414}
415
416
417/* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
418
419static void
420cp_debug_print_flag (FILE *file, const char *desc, bool flag)
421{
422  if (flag)
423    fprintf (file, "%s: true\n", desc);
424}
425
426
427/* Print an unparsed function entry UF to FILE.  */
428
429static void
430cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
431{
432  unsigned i;
433  cp_default_arg_entry *default_arg_fn;
434  tree fn;
435
436  fprintf (file, "\tFunctions with default args:\n");
437  for (i = 0;
438       vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
439       i++)
440    {
441      fprintf (file, "\t\tClass type: ");
442      print_node_brief (file, "", default_arg_fn->class_type, 0);
443      fprintf (file, "\t\tDeclaration: ");
444      print_node_brief (file, "", default_arg_fn->decl, 0);
445      fprintf (file, "\n");
446    }
447
448  fprintf (file, "\n\tFunctions with definitions that require "
449	   "post-processing\n\t\t");
450  for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
451    {
452      print_node_brief (file, "", fn, 0);
453      fprintf (file, " ");
454    }
455  fprintf (file, "\n");
456
457  fprintf (file, "\n\tNon-static data members with initializers that require "
458           "post-processing\n\t\t");
459  for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
460    {
461      print_node_brief (file, "", fn, 0);
462      fprintf (file, " ");
463    }
464  fprintf (file, "\n");
465}
466
467
468/* Print the stack of unparsed member functions S to FILE.  */
469
470static void
471cp_debug_print_unparsed_queues (FILE *file,
472				vec<cp_unparsed_functions_entry, va_gc> *s)
473{
474  unsigned i;
475  cp_unparsed_functions_entry *uf;
476
477  fprintf (file, "Unparsed functions\n");
478  for (i = 0; vec_safe_iterate (s, i, &uf); i++)
479    {
480      fprintf (file, "#%u:\n", i);
481      cp_debug_print_unparsed_function (file, uf);
482    }
483}
484
485
486/* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487   the given PARSER.  If FILE is NULL, the output is printed on stderr. */
488
489static void
490cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
491{
492  cp_token *next_token, *first_token, *start_token;
493
494  if (file == NULL)
495    file = stderr;
496
497  next_token = parser->lexer->next_token;
498  first_token = parser->lexer->buffer->address ();
499  start_token = (next_token > first_token + window_size / 2)
500		? next_token - window_size / 2
501		: first_token;
502  cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503			next_token);
504}
505
506
507/* Dump debugging information for the given PARSER.  If FILE is NULL,
508   the output is printed on stderr.  */
509
510void
511cp_debug_parser (FILE *file, cp_parser *parser)
512{
513  const size_t window_size = 20;
514  cp_token *token;
515  expanded_location eloc;
516
517  if (file == NULL)
518    file = stderr;
519
520  fprintf (file, "Parser state\n\n");
521  fprintf (file, "Number of tokens: %u\n",
522	   vec_safe_length (parser->lexer->buffer));
523  cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524  cp_debug_print_tree_if_set (file, "Object scope",
525				     parser->object_scope);
526  cp_debug_print_tree_if_set (file, "Qualifying scope",
527				     parser->qualifying_scope);
528  cp_debug_print_context_stack (file, parser->context);
529  cp_debug_print_flag (file, "Allow GNU extensions",
530			      parser->allow_gnu_extensions_p);
531  cp_debug_print_flag (file, "'>' token is greater-than",
532			      parser->greater_than_is_operator_p);
533  cp_debug_print_flag (file, "Default args allowed in current "
534			      "parameter list", parser->default_arg_ok_p);
535  cp_debug_print_flag (file, "Parsing integral constant-expression",
536			      parser->integral_constant_expression_p);
537  cp_debug_print_flag (file, "Allow non-constant expression in current "
538			      "constant-expression",
539			      parser->allow_non_integral_constant_expression_p);
540  cp_debug_print_flag (file, "Seen non-constant expression",
541			      parser->non_integral_constant_expression_p);
542  cp_debug_print_flag (file, "Local names forbidden in current context",
543			      (parser->local_variables_forbidden_p
544			       & LOCAL_VARS_FORBIDDEN));
545  cp_debug_print_flag (file, "'this' forbidden in current context",
546			      (parser->local_variables_forbidden_p
547			       & THIS_FORBIDDEN));
548  cp_debug_print_flag (file, "In unbraced linkage specification",
549			      parser->in_unbraced_linkage_specification_p);
550  cp_debug_print_flag (file, "Parsing a declarator",
551			      parser->in_declarator_p);
552  cp_debug_print_flag (file, "In template argument list",
553			      parser->in_template_argument_list_p);
554  cp_debug_print_flag (file, "Parsing an iteration statement",
555			      parser->in_statement & IN_ITERATION_STMT);
556  cp_debug_print_flag (file, "Parsing a switch statement",
557			      parser->in_statement & IN_SWITCH_STMT);
558  cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559			      parser->in_statement & IN_OMP_BLOCK);
560  cp_debug_print_flag (file, "Parsing an OpenMP loop",
561			      parser->in_statement & IN_OMP_FOR);
562  cp_debug_print_flag (file, "Parsing an if statement",
563			      parser->in_statement & IN_IF_STMT);
564  cp_debug_print_flag (file, "Parsing a type-id in an expression "
565			      "context", parser->in_type_id_in_expr_p);
566  cp_debug_print_flag (file, "String expressions should be translated "
567			      "to execution character set",
568			      parser->translate_strings_p);
569  cp_debug_print_flag (file, "Parsing function body outside of a "
570			      "local class", parser->in_function_body);
571  cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
572			      parser->colon_corrects_to_scope_p);
573  cp_debug_print_flag (file, "Colon doesn't start a class definition",
574			      parser->colon_doesnt_start_class_def_p);
575  cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
576			      parser->objective_c_message_context_p);
577  if (parser->type_definition_forbidden_message)
578    fprintf (file, "Error message for forbidden type definitions: %s %s\n",
579	     parser->type_definition_forbidden_message,
580	     parser->type_definition_forbidden_message_arg
581	     ? parser->type_definition_forbidden_message_arg : "<none>");
582  cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583  fprintf (file, "Number of class definitions in progress: %u\n",
584	   parser->num_classes_being_defined);
585  fprintf (file, "Number of template parameter lists for the current "
586	   "declaration: %u\n", parser->num_template_parameter_lists);
587  cp_debug_parser_tokens (file, parser, window_size);
588  token = parser->lexer->next_token;
589  fprintf (file, "Next token to parse:\n");
590  fprintf (file, "\tToken:  ");
591  cp_lexer_print_token (file, token);
592  eloc = expand_location (token->location);
593  fprintf (file, "\n\tFile:   %s\n", eloc.file);
594  fprintf (file, "\tLine:   %d\n", eloc.line);
595  fprintf (file, "\tColumn: %d\n", eloc.column);
596}
597
598DEBUG_FUNCTION void
599debug (cp_parser &ref)
600{
601  cp_debug_parser (stderr, &ref);
602}
603
604DEBUG_FUNCTION void
605debug (cp_parser *ptr)
606{
607  if (ptr)
608    debug (*ptr);
609  else
610    fprintf (stderr, "<nil>\n");
611}
612
613/* Allocate memory for a new lexer object and return it.  */
614
615static cp_lexer *
616cp_lexer_alloc (void)
617{
618  /* Allocate the memory.  */
619  cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
620
621  /* Initially we are not debugging.  */
622  lexer->debugging_p = false;
623
624  lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
625
626  /* Create the buffer.  */
627  vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
628
629  return lexer;
630}
631
632/* Create a new main C++ lexer, the lexer that gets tokens from the
633   preprocessor.  */
634
635static cp_lexer *
636cp_lexer_new_main (void)
637{
638  cp_token token;
639
640  /* It's possible that parsing the first pragma will load a PCH file,
641     which is a GC collection point.  So we have to do that before
642     allocating any memory.  */
643  cp_lexer_get_preprocessor_token (0, &token);
644  cp_parser_initial_pragma (&token);
645  c_common_no_more_pch ();
646
647  cp_lexer *lexer = cp_lexer_alloc ();
648  /* Put the first token in the buffer.  */
649  cp_token *tok = lexer->buffer->quick_push (token);
650
651  uintptr_t filter = 0;
652  if (modules_p ())
653    filter = module_token_cdtor (parse_in, filter);
654
655  /* Get the remaining tokens from the preprocessor.  */
656  while (tok->type != CPP_EOF)
657    {
658      if (filter)
659	/* Process the previous token.  */
660	module_token_lang (tok->type, tok->keyword, tok->u.value,
661			   tok->location, filter);
662      tok = vec_safe_push (lexer->buffer, cp_token ());
663      cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
664    }
665
666  lexer->next_token = lexer->buffer->address ();
667  lexer->last_token = lexer->next_token
668                      + lexer->buffer->length ()
669		      - 1;
670
671  if (lexer->buffer->length () != 1)
672    {
673      /* Set the EOF token's location to be the just after the previous
674         token's range.  That way 'at-eof' diagnostics point at something
675	 meaninful.  */
676      auto range = get_range_from_loc (line_table, tok[-1].location);
677      tok[0].location
678	= linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
679    }
680
681  if (filter)
682    module_token_cdtor (parse_in, filter);
683
684  /* Subsequent preprocessor diagnostics should use compiler
685     diagnostic functions to get the compiler source location.  */
686  done_lexing = true;
687
688  maybe_check_all_macros (parse_in);
689
690  gcc_assert (!lexer->next_token->purged_p);
691  return lexer;
692}
693
694/* Create a new lexer whose token stream is primed with the tokens in
695   CACHE.  When these tokens are exhausted, no new tokens will be read.  */
696
697static cp_lexer *
698cp_lexer_new_from_tokens (cp_token_cache *cache)
699{
700  cp_token *first = cache->first;
701  cp_token *last = cache->last;
702  cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
703
704  /* We do not own the buffer.  */
705  lexer->buffer = NULL;
706
707  /* Insert an EOF token.  */
708  lexer->saved_type = last->type;
709  lexer->saved_keyword = last->keyword;
710  last->type = CPP_EOF;
711  last->keyword = RID_MAX;
712
713  lexer->next_token = first;
714  lexer->last_token = last;
715
716  lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
717
718  /* Initially we are not debugging.  */
719  lexer->debugging_p = false;
720
721  gcc_assert (!lexer->next_token->purged_p
722	      && !lexer->last_token->purged_p);
723  return lexer;
724}
725
726/* Frees all resources associated with LEXER.  */
727
728static void
729cp_lexer_destroy (cp_lexer *lexer)
730{
731  if (lexer->buffer)
732    vec_free (lexer->buffer);
733  else
734    {
735      /* Restore the token we overwrite with EOF.  */
736      lexer->last_token->type = lexer->saved_type;
737      lexer->last_token->keyword = lexer->saved_keyword;
738    }
739  lexer->saved_tokens.release ();
740  ggc_free (lexer);
741}
742
743/* This needs to be set to TRUE before the lexer-debugging infrastructure can
744   be used.  The point of this flag is to help the compiler to fold away calls
745   to cp_lexer_debugging_p within this source file at compile time, when the
746   lexer is not being debugged.  */
747
748#define LEXER_DEBUGGING_ENABLED_P false
749
750/* Returns nonzero if debugging information should be output.  */
751
752static inline bool
753cp_lexer_debugging_p (cp_lexer *lexer)
754{
755  if (!LEXER_DEBUGGING_ENABLED_P)
756    return false;
757
758  return lexer->debugging_p;
759}
760
761
762static inline cp_token_position
763cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
764{
765  return lexer->next_token - previous_p;
766}
767
768static inline cp_token *
769cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
770{
771  return pos;
772}
773
774static inline void
775cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
776{
777  lexer->next_token = cp_lexer_token_at (lexer, pos);
778}
779
780static inline cp_token_position
781cp_lexer_previous_token_position (cp_lexer *lexer)
782{
783  return cp_lexer_token_position (lexer, true);
784}
785
786static inline cp_token *
787cp_lexer_previous_token (cp_lexer *lexer)
788{
789  cp_token_position tp = cp_lexer_previous_token_position (lexer);
790
791  /* Skip past purged tokens.  */
792  while (tp->purged_p)
793    {
794      gcc_assert (tp != vec_safe_address (lexer->buffer));
795      tp--;
796    }
797
798  return cp_lexer_token_at (lexer, tp);
799}
800
801/* Same as above, but return NULL when the lexer doesn't own the token
802   buffer or if the next_token is at the start of the token
803   vector or if all previous tokens are purged.  */
804
805static cp_token *
806cp_lexer_safe_previous_token (cp_lexer *lexer)
807{
808  if (lexer->buffer
809      && lexer->next_token != lexer->buffer->address ())
810    {
811      cp_token_position tp = cp_lexer_previous_token_position (lexer);
812
813      /* Skip past purged tokens.  */
814      while (tp->purged_p)
815	{
816	  if (tp == lexer->buffer->address ())
817	    return NULL;
818	  tp--;
819	}
820      return cp_lexer_token_at (lexer, tp);
821    }
822
823  return NULL;
824}
825
826/* Overload for make_location, taking the lexer to mean the location of the
827   previous token.  */
828
829static inline location_t
830make_location (location_t caret, location_t start, cp_lexer *lexer)
831{
832  cp_token *t = cp_lexer_previous_token (lexer);
833  return make_location (caret, start, t->location);
834}
835
836/* Overload for make_location taking tokens instead of locations.  */
837
838static inline location_t
839make_location (cp_token *caret, cp_token *start, cp_token *end)
840{
841  return make_location (caret->location, start->location, end->location);
842}
843
844/* nonzero if we are presently saving tokens.  */
845
846static inline int
847cp_lexer_saving_tokens (const cp_lexer* lexer)
848{
849  return lexer->saved_tokens.length () != 0;
850}
851
852/* Store the next token from the preprocessor in *TOKEN.  Return true
853   if we reach EOF.  If LEXER is NULL, assume we are handling an
854   initial #pragma pch_preprocess, and thus want the lexer to return
855   processed strings.  */
856
857static void
858cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
859{
860  static int is_extern_c = 0;
861
862   /* Get a new token from the preprocessor.  */
863  token->type
864    = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
865			flags);
866  token->keyword = RID_MAX;
867  token->purged_p = false;
868  token->error_reported = false;
869  token->tree_check_p = false;
870  /* Usually never see a zero, but just in case ... */
871  token->main_source_p = line_table->depth <= 1;
872
873  /* On some systems, some header files are surrounded by an
874     implicit extern "C" block.  Set a flag in the token if it
875     comes from such a header.  */
876  is_extern_c += pending_lang_change;
877  pending_lang_change = 0;
878  token->implicit_extern_c = is_extern_c > 0;
879
880  /* Check to see if this token is a keyword.  */
881  if (token->type == CPP_NAME)
882    {
883      if (IDENTIFIER_KEYWORD_P (token->u.value))
884	{
885	  /* Mark this token as a keyword.  */
886	  token->type = CPP_KEYWORD;
887	  /* Record which keyword.  */
888	  token->keyword = C_RID_CODE (token->u.value);
889	}
890      else
891	{
892          if (warn_cxx11_compat
893              && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
894              && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
895            {
896              /* Warn about the C++0x keyword (but still treat it as
897                 an identifier).  */
898	      warning_at (token->location, OPT_Wc__11_compat,
899			  "identifier %qE is a keyword in C++11",
900			  token->u.value);
901
902              /* Clear out the C_RID_CODE so we don't warn about this
903                 particular identifier-turned-keyword again.  */
904              C_SET_RID_CODE (token->u.value, RID_MAX);
905            }
906	  if (warn_cxx20_compat
907	      && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
908	      && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
909	    {
910	      /* Warn about the C++20 keyword (but still treat it as
911		 an identifier).  */
912	      warning_at (token->location, OPT_Wc__20_compat,
913			  "identifier %qE is a keyword in C++20",
914			  token->u.value);
915
916	      /* Clear out the C_RID_CODE so we don't warn about this
917		 particular identifier-turned-keyword again.  */
918	      C_SET_RID_CODE (token->u.value, RID_MAX);
919	    }
920
921	  token->keyword = RID_MAX;
922	}
923    }
924  else if (token->type == CPP_AT_NAME)
925    {
926      /* This only happens in Objective-C++; it must be a keyword.  */
927      token->type = CPP_KEYWORD;
928      switch (C_RID_CODE (token->u.value))
929	{
930	  /* Replace 'class' with '@class', 'private' with '@private',
931	     etc.  This prevents confusion with the C++ keyword
932	     'class', and makes the tokens consistent with other
933	     Objective-C 'AT' keywords.  For example '@class' is
934	     reported as RID_AT_CLASS which is consistent with
935	     '@synchronized', which is reported as
936	     RID_AT_SYNCHRONIZED.
937	  */
938	case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
939	case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
940	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
941	case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
942	case RID_THROW:     token->keyword = RID_AT_THROW; break;
943	case RID_TRY:       token->keyword = RID_AT_TRY; break;
944	case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
945	case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
946	default:            token->keyword = C_RID_CODE (token->u.value);
947	}
948    }
949}
950
951/* Update the globals input_location and the input file stack from TOKEN.  */
952static inline void
953cp_lexer_set_source_position_from_token (cp_token *token)
954{
955  input_location = token->location;
956}
957
958/* Update the globals input_location and the input file stack from LEXER.  */
959static inline void
960cp_lexer_set_source_position (cp_lexer *lexer)
961{
962  cp_token *token = cp_lexer_peek_token (lexer);
963  cp_lexer_set_source_position_from_token (token);
964}
965
966/* Return a pointer to the next token in the token stream, but do not
967   consume it.  */
968
969static inline cp_token *
970cp_lexer_peek_token (cp_lexer *lexer)
971{
972  if (cp_lexer_debugging_p (lexer))
973    {
974      fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
975      cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
976      putc ('\n', cp_lexer_debug_stream);
977    }
978  return lexer->next_token;
979}
980
981/* Return true if the next token has the indicated TYPE.  */
982
983static inline bool
984cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
985{
986  return cp_lexer_peek_token (lexer)->type == type;
987}
988
989/* Return true if the next token does not have the indicated TYPE.  */
990
991static inline bool
992cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
993{
994  return !cp_lexer_next_token_is (lexer, type);
995}
996
997/* Return true if the next token is the indicated KEYWORD.  */
998
999static inline bool
1000cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1001{
1002  return cp_lexer_peek_token (lexer)->keyword == keyword;
1003}
1004
1005static inline bool
1006cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1007{
1008  return cp_lexer_peek_nth_token (lexer, n)->type == type;
1009}
1010
1011static inline bool
1012cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1013{
1014  return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1015}
1016
1017/* Return true if KEYWORD can start a decl-specifier.  */
1018
1019bool
1020cp_keyword_starts_decl_specifier_p (enum rid keyword)
1021{
1022  switch (keyword)
1023    {
1024      /* auto specifier: storage-class-specifier in C++,
1025         simple-type-specifier in C++0x.  */
1026    case RID_AUTO:
1027      /* Storage classes.  */
1028    case RID_REGISTER:
1029    case RID_STATIC:
1030    case RID_EXTERN:
1031    case RID_MUTABLE:
1032    case RID_THREAD:
1033      /* Elaborated type specifiers.  */
1034    case RID_ENUM:
1035    case RID_CLASS:
1036    case RID_STRUCT:
1037    case RID_UNION:
1038    case RID_TYPENAME:
1039      /* Simple type specifiers.  */
1040    case RID_CHAR:
1041    case RID_CHAR8:
1042    case RID_CHAR16:
1043    case RID_CHAR32:
1044    case RID_WCHAR:
1045    case RID_BOOL:
1046    case RID_SHORT:
1047    case RID_INT:
1048    case RID_LONG:
1049    case RID_SIGNED:
1050    case RID_UNSIGNED:
1051    case RID_FLOAT:
1052    case RID_DOUBLE:
1053    case RID_VOID:
1054      /* CV qualifiers.  */
1055    case RID_CONST:
1056    case RID_VOLATILE:
1057      /* Function specifiers.  */
1058    case RID_EXPLICIT:
1059    case RID_VIRTUAL:
1060      /* friend/typdef/inline specifiers.  */
1061    case RID_FRIEND:
1062    case RID_TYPEDEF:
1063    case RID_INLINE:
1064      /* GNU extensions.  */
1065    case RID_TYPEOF:
1066      /* C++11 extensions.  */
1067    case RID_DECLTYPE:
1068    case RID_UNDERLYING_TYPE:
1069    case RID_CONSTEXPR:
1070      /* C++20 extensions.  */
1071    case RID_CONSTINIT:
1072    case RID_CONSTEVAL:
1073      return true;
1074
1075    default:
1076      if (keyword >= RID_FIRST_INT_N
1077	  && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1078	  && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1079	return true;
1080      return false;
1081    }
1082}
1083
1084/* Return true if the next token is a keyword for a decl-specifier.  */
1085
1086static bool
1087cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1088{
1089  cp_token *token;
1090
1091  token = cp_lexer_peek_token (lexer);
1092  return cp_keyword_starts_decl_specifier_p (token->keyword);
1093}
1094
1095/* Returns TRUE iff the token T begins a decltype type.  */
1096
1097static bool
1098token_is_decltype (cp_token *t)
1099{
1100  return (t->keyword == RID_DECLTYPE
1101	  || t->type == CPP_DECLTYPE);
1102}
1103
1104/* Returns TRUE iff the next token begins a decltype type.  */
1105
1106static bool
1107cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1108{
1109  cp_token *t = cp_lexer_peek_token (lexer);
1110  return token_is_decltype (t);
1111}
1112
1113/* Called when processing a token with tree_check_value; perform or defer the
1114   associated checks and return the value.  */
1115
1116static tree
1117saved_checks_value (struct tree_check *check_value)
1118{
1119  /* Perform any access checks that were deferred.  */
1120  vec<deferred_access_check, va_gc> *checks;
1121  deferred_access_check *chk;
1122  checks = check_value->checks;
1123  if (checks)
1124    {
1125      int i;
1126      FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1127	perform_or_defer_access_check (chk->binfo,
1128				       chk->decl,
1129				       chk->diag_decl, tf_warning_or_error);
1130    }
1131  /* Return the stored value.  */
1132  return check_value->value;
1133}
1134
1135/* Return a pointer to the Nth token in the token stream.  If N is 1,
1136   then this is precisely equivalent to cp_lexer_peek_token (except
1137   that it is not inline).  One would like to disallow that case, but
1138   there is one case (cp_parser_nth_token_starts_template_id) where
1139   the caller passes a variable for N and it might be 1.  */
1140
1141static cp_token *
1142cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1143{
1144  cp_token *token;
1145
1146  /* N is 1-based, not zero-based.  */
1147  gcc_assert (n > 0);
1148
1149  if (cp_lexer_debugging_p (lexer))
1150    fprintf (cp_lexer_debug_stream,
1151	     "cp_lexer: peeking ahead %ld at token: ", (long)n);
1152
1153  --n;
1154  token = lexer->next_token;
1155  while (n && token->type != CPP_EOF)
1156    {
1157      ++token;
1158      if (!token->purged_p)
1159	--n;
1160    }
1161
1162  if (cp_lexer_debugging_p (lexer))
1163    {
1164      cp_lexer_print_token (cp_lexer_debug_stream, token);
1165      putc ('\n', cp_lexer_debug_stream);
1166    }
1167
1168  return token;
1169}
1170
1171/* Return the next token, and advance the lexer's next_token pointer
1172   to point to the next non-purged token.  */
1173
1174static cp_token *
1175cp_lexer_consume_token (cp_lexer* lexer)
1176{
1177  cp_token *token = lexer->next_token;
1178
1179  do
1180    {
1181      gcc_assert (token->type != CPP_EOF);
1182      lexer->next_token++;
1183    }
1184  while (lexer->next_token->purged_p);
1185
1186  cp_lexer_set_source_position_from_token (token);
1187
1188  /* Provide debugging output.  */
1189  if (cp_lexer_debugging_p (lexer))
1190    {
1191      fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1192      cp_lexer_print_token (cp_lexer_debug_stream, token);
1193      putc ('\n', cp_lexer_debug_stream);
1194    }
1195
1196  return token;
1197}
1198
1199/* Permanently remove the next token from the token stream, and
1200   advance the next_token pointer to refer to the next non-purged
1201   token.  */
1202
1203static void
1204cp_lexer_purge_token (cp_lexer *lexer)
1205{
1206  cp_token *tok = lexer->next_token;
1207
1208  gcc_assert (tok->type != CPP_EOF);
1209  tok->purged_p = true;
1210  tok->location = UNKNOWN_LOCATION;
1211  tok->u.value = NULL_TREE;
1212  tok->keyword = RID_MAX;
1213
1214  do
1215    tok++;
1216  while (tok->purged_p);
1217  lexer->next_token = tok;
1218}
1219
1220/* Permanently remove all tokens after TOK, up to, but not
1221   including, the token that will be returned next by
1222   cp_lexer_peek_token.  */
1223
1224static void
1225cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1226{
1227  cp_token *peek = lexer->next_token;
1228
1229  gcc_assert (tok < peek);
1230
1231  for (tok++; tok != peek; tok++)
1232    {
1233      tok->purged_p = true;
1234      tok->location = UNKNOWN_LOCATION;
1235      tok->u.value = NULL_TREE;
1236      tok->keyword = RID_MAX;
1237    }
1238}
1239
1240/* Begin saving tokens.  All tokens consumed after this point will be
1241   preserved.  */
1242
1243static void
1244cp_lexer_save_tokens (cp_lexer* lexer)
1245{
1246  /* Provide debugging output.  */
1247  if (cp_lexer_debugging_p (lexer))
1248    fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1249
1250  lexer->saved_tokens.safe_push (lexer->next_token);
1251}
1252
1253/* Commit to the portion of the token stream most recently saved.  */
1254
1255static void
1256cp_lexer_commit_tokens (cp_lexer* lexer)
1257{
1258  /* Provide debugging output.  */
1259  if (cp_lexer_debugging_p (lexer))
1260    fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1261
1262  lexer->saved_tokens.pop ();
1263}
1264
1265/* Return all tokens saved since the last call to cp_lexer_save_tokens
1266   to the token stream.  Stop saving tokens.  */
1267
1268static void
1269cp_lexer_rollback_tokens (cp_lexer* lexer)
1270{
1271  /* Provide debugging output.  */
1272  if (cp_lexer_debugging_p (lexer))
1273    fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1274
1275  lexer->next_token = lexer->saved_tokens.pop ();
1276}
1277
1278/* Determines what saved_token_sentinel does when going out of scope.  */
1279
1280enum saved_token_sentinel_mode {
1281  STS_COMMIT,
1282  STS_ROLLBACK,
1283  STS_DONOTHING
1284};
1285
1286/* RAII wrapper around the above functions, with sanity checking (the token
1287   stream should be the same at the point of instantiation as it is at the
1288   point of destruction).
1289
1290   Creating a variable saves tokens.  MODE determines what happens when the
1291   object is destroyed.  STS_COMMIT commits tokens (default),
1292   STS_ROLLBACK rolls-back and STS_DONOTHING does nothing.  Calling
1293   rollback() will immediately roll-back tokens and set MODE to
1294   STS_DONOTHING.  */
1295
1296struct saved_token_sentinel
1297{
1298  cp_lexer *lexer;
1299  unsigned len;
1300  saved_token_sentinel_mode mode;
1301  saved_token_sentinel (cp_lexer *_lexer,
1302			saved_token_sentinel_mode _mode = STS_COMMIT)
1303    : lexer (_lexer), mode (_mode)
1304  {
1305    len = lexer->saved_tokens.length ();
1306    cp_lexer_save_tokens (lexer);
1307  }
1308  void rollback ()
1309  {
1310    cp_lexer_rollback_tokens (lexer);
1311    cp_lexer_set_source_position_from_token
1312      (cp_lexer_previous_token (lexer));
1313    mode = STS_DONOTHING;
1314  }
1315  ~saved_token_sentinel ()
1316  {
1317    if (mode == STS_COMMIT)
1318      cp_lexer_commit_tokens (lexer);
1319    else if (mode == STS_ROLLBACK)
1320      rollback ();
1321
1322    gcc_assert (lexer->saved_tokens.length () == len);
1323  }
1324};
1325
1326/* Print a representation of the TOKEN on the STREAM.  */
1327
1328static void
1329cp_lexer_print_token (FILE * stream, cp_token *token)
1330{
1331  /* We don't use cpp_type2name here because the parser defines
1332     a few tokens of its own.  */
1333  static const char *const token_names[] = {
1334    /* cpplib-defined token types */
1335#define OP(e, s) #e,
1336#define TK(e, s) #e,
1337    TTYPE_TABLE
1338#undef OP
1339#undef TK
1340    /* C++ parser token types - see "Manifest constants", above.  */
1341    "KEYWORD",
1342    "TEMPLATE_ID",
1343    "NESTED_NAME_SPECIFIER",
1344  };
1345
1346  /* For some tokens, print the associated data.  */
1347  switch (token->type)
1348    {
1349    case CPP_KEYWORD:
1350      /* Some keywords have a value that is not an IDENTIFIER_NODE.
1351	 For example, `struct' is mapped to an INTEGER_CST.  */
1352      if (!identifier_p (token->u.value))
1353	break;
1354      /* fall through */
1355    case CPP_NAME:
1356      fputs (IDENTIFIER_POINTER (token->u.value), stream);
1357      break;
1358
1359    case CPP_STRING:
1360    case CPP_STRING16:
1361    case CPP_STRING32:
1362    case CPP_WSTRING:
1363    case CPP_UTF8STRING:
1364      fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1365      break;
1366
1367    case CPP_NUMBER:
1368      print_generic_expr (stream, token->u.value);
1369      break;
1370
1371    default:
1372      /* If we have a name for the token, print it out.  Otherwise, we
1373	 simply give the numeric code.  */
1374      if (token->type < ARRAY_SIZE(token_names))
1375	fputs (token_names[token->type], stream);
1376      else
1377	fprintf (stream, "[%d]", token->type);
1378      break;
1379    }
1380}
1381
1382DEBUG_FUNCTION void
1383debug (cp_token &ref)
1384{
1385  cp_lexer_print_token (stderr, &ref);
1386  fprintf (stderr, "\n");
1387}
1388
1389DEBUG_FUNCTION void
1390debug (cp_token *ptr)
1391{
1392  if (ptr)
1393    debug (*ptr);
1394  else
1395    fprintf (stderr, "<nil>\n");
1396}
1397
1398
1399/* Start emitting debugging information.  */
1400
1401static void
1402cp_lexer_start_debugging (cp_lexer* lexer)
1403{
1404  if (!LEXER_DEBUGGING_ENABLED_P)
1405    fatal_error (input_location,
1406		 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1407
1408  lexer->debugging_p = true;
1409  cp_lexer_debug_stream = stderr;
1410}
1411
1412/* Stop emitting debugging information.  */
1413
1414static void
1415cp_lexer_stop_debugging (cp_lexer* lexer)
1416{
1417  if (!LEXER_DEBUGGING_ENABLED_P)
1418    fatal_error (input_location,
1419		 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1420
1421  lexer->debugging_p = false;
1422  cp_lexer_debug_stream = NULL;
1423}
1424
1425/* Create a new cp_token_cache, representing a range of tokens.  */
1426
1427static cp_token_cache *
1428cp_token_cache_new (cp_token *first, cp_token *last)
1429{
1430  cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1431  cache->first = first;
1432  cache->last = last;
1433  return cache;
1434}
1435
1436/* Diagnose if #pragma omp declare simd isn't followed immediately
1437   by function declaration or definition.  */
1438
1439static inline void
1440cp_ensure_no_omp_declare_simd (cp_parser *parser)
1441{
1442  if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1443    {
1444      error ("%<#pragma omp declare %s%> not immediately followed by "
1445	     "function declaration or definition",
1446	     parser->omp_declare_simd->variant_p ? "variant" : "simd");
1447      parser->omp_declare_simd = NULL;
1448    }
1449}
1450
1451/* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1452   and put that into "omp declare simd" attribute.  */
1453
1454static inline void
1455cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1456{
1457  if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1458    {
1459      if (fndecl == error_mark_node)
1460	{
1461	  parser->omp_declare_simd = NULL;
1462	  return;
1463	}
1464      if (TREE_CODE (fndecl) != FUNCTION_DECL)
1465	{
1466	  cp_ensure_no_omp_declare_simd (parser);
1467	  return;
1468	}
1469    }
1470}
1471
1472/* Similarly, but for use in declaration parsing functions
1473   which call cp_parser_handle_directive_omp_attributes.  */
1474
1475static inline void
1476cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1477{
1478  if (parser->omp_declare_simd != data)
1479    return;
1480
1481  if (!parser->omp_declare_simd->error_seen
1482      && !parser->omp_declare_simd->fndecl_seen)
1483    error_at (parser->omp_declare_simd->loc,
1484	      "%<declare %s%> directive not immediately followed by "
1485	      "function declaration or definition",
1486	      parser->omp_declare_simd->variant_p ? "variant" : "simd");
1487  parser->omp_declare_simd = NULL;
1488}
1489
1490/* Diagnose if #pragma acc routine isn't followed immediately by function
1491   declaration or definition.  */
1492
1493static inline void
1494cp_ensure_no_oacc_routine (cp_parser *parser)
1495{
1496  if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1497    {
1498      error_at (parser->oacc_routine->loc,
1499		"%<#pragma acc routine%> not immediately followed by "
1500		"function declaration or definition");
1501      parser->oacc_routine = NULL;
1502    }
1503}
1504
1505/* Decl-specifiers.  */
1506
1507/* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1508
1509static void
1510clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1511{
1512  memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1513}
1514
1515/* Declarators.  */
1516
1517/* Nothing other than the parser should be creating declarators;
1518   declarators are a semi-syntactic representation of C++ entities.
1519   Other parts of the front end that need to create entities (like
1520   VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1521
1522static cp_declarator *make_call_declarator
1523  (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1524   tree, tree, tree, tree, location_t);
1525static cp_declarator *make_array_declarator
1526  (cp_declarator *, tree);
1527static cp_declarator *make_pointer_declarator
1528  (cp_cv_quals, cp_declarator *, tree);
1529static cp_declarator *make_reference_declarator
1530  (cp_cv_quals, cp_declarator *, bool, tree);
1531static cp_declarator *make_ptrmem_declarator
1532  (cp_cv_quals, tree, cp_declarator *, tree);
1533
1534/* An erroneous declarator.  */
1535static cp_declarator *cp_error_declarator;
1536
1537/* The obstack on which declarators and related data structures are
1538   allocated.  */
1539static struct obstack declarator_obstack;
1540
1541/* Alloc BYTES from the declarator memory pool.  */
1542
1543static inline void *
1544alloc_declarator (size_t bytes)
1545{
1546  return obstack_alloc (&declarator_obstack, bytes);
1547}
1548
1549/* Allocate a declarator of the indicated KIND.  Clear fields that are
1550   common to all declarators.  */
1551
1552static cp_declarator *
1553make_declarator (cp_declarator_kind kind)
1554{
1555  cp_declarator *declarator;
1556
1557  declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1558  declarator->kind = kind;
1559  declarator->parenthesized = UNKNOWN_LOCATION;
1560  declarator->attributes = NULL_TREE;
1561  declarator->std_attributes = NULL_TREE;
1562  declarator->declarator = NULL;
1563  declarator->parameter_pack_p = false;
1564  declarator->id_loc = UNKNOWN_LOCATION;
1565  declarator->init_loc = UNKNOWN_LOCATION;
1566
1567  return declarator;
1568}
1569
1570/* Make a declarator for a generalized identifier.  If
1571   QUALIFYING_SCOPE is non-NULL, the identifier is
1572   QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1573   UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1574   is, if any.   */
1575
1576static cp_declarator *
1577make_id_declarator (tree qualifying_scope, tree unqualified_name,
1578		    special_function_kind sfk, location_t id_location)
1579{
1580  cp_declarator *declarator;
1581
1582  /* It is valid to write:
1583
1584       class C { void f(); };
1585       typedef C D;
1586       void D::f();
1587
1588     The standard is not clear about whether `typedef const C D' is
1589     legal; as of 2002-09-15 the committee is considering that
1590     question.  EDG 3.0 allows that syntax.  Therefore, we do as
1591     well.  */
1592  if (qualifying_scope && TYPE_P (qualifying_scope))
1593    qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1594
1595  gcc_assert (identifier_p (unqualified_name)
1596	      || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1597	      || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1598
1599  declarator = make_declarator (cdk_id);
1600  declarator->u.id.qualifying_scope = qualifying_scope;
1601  declarator->u.id.unqualified_name = unqualified_name;
1602  declarator->u.id.sfk = sfk;
1603  declarator->id_loc = id_location;
1604
1605  return declarator;
1606}
1607
1608/* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1609   of modifiers such as const or volatile to apply to the pointer
1610   type, represented as identifiers.  ATTRIBUTES represent the attributes that
1611   appertain to the pointer or reference.  */
1612
1613cp_declarator *
1614make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1615			 tree attributes)
1616{
1617  cp_declarator *declarator;
1618
1619  declarator = make_declarator (cdk_pointer);
1620  declarator->declarator = target;
1621  declarator->u.pointer.qualifiers = cv_qualifiers;
1622  declarator->u.pointer.class_type = NULL_TREE;
1623  if (target)
1624    {
1625      declarator->id_loc = target->id_loc;
1626      declarator->parameter_pack_p = target->parameter_pack_p;
1627      target->parameter_pack_p = false;
1628    }
1629  else
1630    declarator->parameter_pack_p = false;
1631
1632  declarator->std_attributes = attributes;
1633
1634  return declarator;
1635}
1636
1637/* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1638   represent the attributes that appertain to the pointer or
1639   reference.  */
1640
1641cp_declarator *
1642make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1643			   bool rvalue_ref, tree attributes)
1644{
1645  cp_declarator *declarator;
1646
1647  declarator = make_declarator (cdk_reference);
1648  declarator->declarator = target;
1649  declarator->u.reference.qualifiers = cv_qualifiers;
1650  declarator->u.reference.rvalue_ref = rvalue_ref;
1651  if (target)
1652    {
1653      declarator->id_loc = target->id_loc;
1654      declarator->parameter_pack_p = target->parameter_pack_p;
1655      target->parameter_pack_p = false;
1656    }
1657  else
1658    declarator->parameter_pack_p = false;
1659
1660  declarator->std_attributes = attributes;
1661
1662  return declarator;
1663}
1664
1665/* Like make_pointer_declarator -- but for a pointer to a non-static
1666   member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1667   appertain to the pointer or reference.  */
1668
1669cp_declarator *
1670make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1671			cp_declarator *pointee,
1672			tree attributes)
1673{
1674  cp_declarator *declarator;
1675
1676  declarator = make_declarator (cdk_ptrmem);
1677  declarator->declarator = pointee;
1678  declarator->u.pointer.qualifiers = cv_qualifiers;
1679  declarator->u.pointer.class_type = class_type;
1680
1681  if (pointee)
1682    {
1683      declarator->parameter_pack_p = pointee->parameter_pack_p;
1684      pointee->parameter_pack_p = false;
1685    }
1686  else
1687    declarator->parameter_pack_p = false;
1688
1689  declarator->std_attributes = attributes;
1690
1691  return declarator;
1692}
1693
1694/* Make a declarator for the function given by TARGET, with the
1695   indicated PARMS.  The CV_QUALIFIERS apply to the function, as in
1696   "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1697   indicates what exceptions can be thrown.  */
1698
1699cp_declarator *
1700make_call_declarator (cp_declarator *target,
1701		      tree parms,
1702		      cp_cv_quals cv_qualifiers,
1703		      cp_virt_specifiers virt_specifiers,
1704		      cp_ref_qualifier ref_qualifier,
1705		      tree tx_qualifier,
1706		      tree exception_specification,
1707		      tree late_return_type,
1708		      tree requires_clause,
1709		      location_t parens_loc)
1710{
1711  cp_declarator *declarator;
1712
1713  declarator = make_declarator (cdk_function);
1714  declarator->declarator = target;
1715  declarator->u.function.parameters = parms;
1716  declarator->u.function.qualifiers = cv_qualifiers;
1717  declarator->u.function.virt_specifiers = virt_specifiers;
1718  declarator->u.function.ref_qualifier = ref_qualifier;
1719  declarator->u.function.tx_qualifier = tx_qualifier;
1720  declarator->u.function.exception_specification = exception_specification;
1721  declarator->u.function.late_return_type = late_return_type;
1722  declarator->u.function.requires_clause = requires_clause;
1723  declarator->u.function.parens_loc = parens_loc;
1724  if (target)
1725    {
1726      declarator->id_loc = target->id_loc;
1727      declarator->parameter_pack_p = target->parameter_pack_p;
1728      target->parameter_pack_p = false;
1729    }
1730  else
1731    declarator->parameter_pack_p = false;
1732
1733  return declarator;
1734}
1735
1736/* Make a declarator for an array of BOUNDS elements, each of which is
1737   defined by ELEMENT.  */
1738
1739cp_declarator *
1740make_array_declarator (cp_declarator *element, tree bounds)
1741{
1742  cp_declarator *declarator;
1743
1744  declarator = make_declarator (cdk_array);
1745  declarator->declarator = element;
1746  declarator->u.array.bounds = bounds;
1747  if (element)
1748    {
1749      declarator->id_loc = element->id_loc;
1750      declarator->parameter_pack_p = element->parameter_pack_p;
1751      element->parameter_pack_p = false;
1752    }
1753  else
1754    declarator->parameter_pack_p = false;
1755
1756  return declarator;
1757}
1758
1759/* Determine whether the declarator we've seen so far can be a
1760   parameter pack, when followed by an ellipsis.  */
1761static bool
1762declarator_can_be_parameter_pack (cp_declarator *declarator)
1763{
1764  if (declarator && declarator->parameter_pack_p)
1765    /* We already saw an ellipsis.  */
1766    return false;
1767
1768  /* Search for a declarator name, or any other declarator that goes
1769     after the point where the ellipsis could appear in a parameter
1770     pack. If we find any of these, then this declarator cannot be
1771     made into a parameter pack.  */
1772  bool found = false;
1773  while (declarator && !found)
1774    {
1775      switch ((int)declarator->kind)
1776	{
1777	case cdk_id:
1778	case cdk_array:
1779	case cdk_decomp:
1780	  found = true;
1781	  break;
1782
1783	case cdk_error:
1784	  return true;
1785
1786	default:
1787	  declarator = declarator->declarator;
1788	  break;
1789	}
1790    }
1791
1792  return !found;
1793}
1794
1795cp_parameter_declarator *no_parameters;
1796
1797/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1798   DECLARATOR and DEFAULT_ARGUMENT.  */
1799
1800cp_parameter_declarator *
1801make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1802			   cp_declarator *declarator,
1803			   tree default_argument,
1804			   location_t loc,
1805			   bool template_parameter_pack_p = false)
1806{
1807  cp_parameter_declarator *parameter;
1808
1809  parameter = ((cp_parameter_declarator *)
1810	       alloc_declarator (sizeof (cp_parameter_declarator)));
1811  parameter->next = NULL;
1812  if (decl_specifiers)
1813    parameter->decl_specifiers = *decl_specifiers;
1814  else
1815    clear_decl_specs (&parameter->decl_specifiers);
1816  parameter->declarator = declarator;
1817  parameter->default_argument = default_argument;
1818  parameter->template_parameter_pack_p = template_parameter_pack_p;
1819  parameter->loc = loc;
1820
1821  return parameter;
1822}
1823
1824/* Returns true iff DECLARATOR  is a declaration for a function.  */
1825
1826static bool
1827function_declarator_p (const cp_declarator *declarator)
1828{
1829  while (declarator)
1830    {
1831      if (declarator->kind == cdk_function
1832	  && declarator->declarator->kind == cdk_id)
1833	return true;
1834      if (declarator->kind == cdk_id
1835	  || declarator->kind == cdk_decomp
1836	  || declarator->kind == cdk_error)
1837	return false;
1838      declarator = declarator->declarator;
1839    }
1840  return false;
1841}
1842
1843/* The parser.  */
1844
1845/* Overview
1846   --------
1847
1848   A cp_parser parses the token stream as specified by the C++
1849   grammar.  Its job is purely parsing, not semantic analysis.  For
1850   example, the parser breaks the token stream into declarators,
1851   expressions, statements, and other similar syntactic constructs.
1852   It does not check that the types of the expressions on either side
1853   of an assignment-statement are compatible, or that a function is
1854   not declared with a parameter of type `void'.
1855
1856   The parser invokes routines elsewhere in the compiler to perform
1857   semantic analysis and to build up the abstract syntax tree for the
1858   code processed.
1859
1860   The parser (and the template instantiation code, which is, in a
1861   way, a close relative of parsing) are the only parts of the
1862   compiler that should be calling push_scope and pop_scope, or
1863   related functions.  The parser (and template instantiation code)
1864   keeps track of what scope is presently active; everything else
1865   should simply honor that.  (The code that generates static
1866   initializers may also need to set the scope, in order to check
1867   access control correctly when emitting the initializers.)
1868
1869   Methodology
1870   -----------
1871
1872   The parser is of the standard recursive-descent variety.  Upcoming
1873   tokens in the token stream are examined in order to determine which
1874   production to use when parsing a non-terminal.  Some C++ constructs
1875   require arbitrary look ahead to disambiguate.  For example, it is
1876   impossible, in the general case, to tell whether a statement is an
1877   expression or declaration without scanning the entire statement.
1878   Therefore, the parser is capable of "parsing tentatively."  When the
1879   parser is not sure what construct comes next, it enters this mode.
1880   Then, while we attempt to parse the construct, the parser queues up
1881   error messages, rather than issuing them immediately, and saves the
1882   tokens it consumes.  If the construct is parsed successfully, the
1883   parser "commits", i.e., it issues any queued error messages and
1884   the tokens that were being preserved are permanently discarded.
1885   If, however, the construct is not parsed successfully, the parser
1886   rolls back its state completely so that it can resume parsing using
1887   a different alternative.
1888
1889   Future Improvements
1890   -------------------
1891
1892   The performance of the parser could probably be improved substantially.
1893   We could often eliminate the need to parse tentatively by looking ahead
1894   a little bit.  In some places, this approach might not entirely eliminate
1895   the need to parse tentatively, but it might still speed up the average
1896   case.  */
1897
1898/* Flags that are passed to some parsing functions.  These values can
1899   be bitwise-ored together.  */
1900
1901enum
1902{
1903  /* No flags.  */
1904  CP_PARSER_FLAGS_NONE = 0x0,
1905  /* The construct is optional.  If it is not present, then no error
1906     should be issued.  */
1907  CP_PARSER_FLAGS_OPTIONAL = 0x1,
1908  /* When parsing a type-specifier, treat user-defined type-names
1909     as non-type identifiers.  */
1910  CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1911  /* When parsing a type-specifier, do not try to parse a class-specifier
1912     or enum-specifier.  */
1913  CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1914  /* When parsing a decl-specifier-seq, only allow type-specifier or
1915     constexpr.  */
1916  CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1917  /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1918     for C++20 consteval.  */
1919  CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1920  /* When parsing a decl-specifier-seq, allow missing typename.  */
1921  CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1922  /* When parsing of the noexcept-specifier should be delayed.  */
1923  CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1924  /* When parsing a consteval declarator.  */
1925  CP_PARSER_FLAGS_CONSTEVAL = 0x80
1926};
1927
1928/* This type is used for parameters and variables which hold
1929   combinations of the above flags.  */
1930typedef int cp_parser_flags;
1931
1932/* The different kinds of declarators we want to parse.  */
1933
1934enum cp_parser_declarator_kind
1935{
1936  /* We want an abstract declarator.  */
1937  CP_PARSER_DECLARATOR_ABSTRACT,
1938  /* We want a named declarator.  */
1939  CP_PARSER_DECLARATOR_NAMED,
1940  /* We don't mind, but the name must be an unqualified-id.  */
1941  CP_PARSER_DECLARATOR_EITHER
1942};
1943
1944/* The precedence values used to parse binary expressions.  The minimum value
1945   of PREC must be 1, because zero is reserved to quickly discriminate
1946   binary operators from other tokens.  */
1947
1948enum cp_parser_prec
1949{
1950  PREC_NOT_OPERATOR,
1951  PREC_LOGICAL_OR_EXPRESSION,
1952  PREC_LOGICAL_AND_EXPRESSION,
1953  PREC_INCLUSIVE_OR_EXPRESSION,
1954  PREC_EXCLUSIVE_OR_EXPRESSION,
1955  PREC_AND_EXPRESSION,
1956  PREC_EQUALITY_EXPRESSION,
1957  PREC_RELATIONAL_EXPRESSION,
1958  PREC_SPACESHIP_EXPRESSION,
1959  PREC_SHIFT_EXPRESSION,
1960  PREC_ADDITIVE_EXPRESSION,
1961  PREC_MULTIPLICATIVE_EXPRESSION,
1962  PREC_PM_EXPRESSION,
1963  NUM_PREC_VALUES = PREC_PM_EXPRESSION
1964};
1965
1966/* A mapping from a token type to a corresponding tree node type, with a
1967   precedence value.  */
1968
1969struct cp_parser_binary_operations_map_node
1970{
1971  /* The token type.  */
1972  enum cpp_ttype token_type;
1973  /* The corresponding tree code.  */
1974  enum tree_code tree_type;
1975  /* The precedence of this operator.  */
1976  enum cp_parser_prec prec;
1977};
1978
1979struct cp_parser_expression_stack_entry
1980{
1981  /* Left hand side of the binary operation we are currently
1982     parsing.  */
1983  cp_expr lhs;
1984  /* Original tree code for left hand side, if it was a binary
1985     expression itself (used for -Wparentheses).  */
1986  enum tree_code lhs_type;
1987  /* Tree code for the binary operation we are parsing.  */
1988  enum tree_code tree_type;
1989  /* Precedence of the binary operation we are parsing.  */
1990  enum cp_parser_prec prec;
1991  /* Location of the binary operation we are parsing.  */
1992  location_t loc;
1993};
1994
1995/* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1996   entries because precedence levels on the stack are monotonically
1997   increasing.  */
1998typedef struct cp_parser_expression_stack_entry
1999  cp_parser_expression_stack[NUM_PREC_VALUES];
2000
2001/* Prototypes.  */
2002
2003/* Constructors and destructors.  */
2004
2005static cp_parser_context *cp_parser_context_new
2006  (cp_parser_context *);
2007
2008/* Class variables.  */
2009
2010static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2011
2012/* The operator-precedence table used by cp_parser_binary_expression.
2013   Transformed into an associative array (binops_by_token) by
2014   cp_parser_new.  */
2015
2016static const cp_parser_binary_operations_map_node binops[] = {
2017  { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2018  { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2019
2020  { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2021  { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2022  { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2023
2024  { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2025  { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2026
2027  { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2028  { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2029
2030  { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2031
2032  { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2033  { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2034  { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2035  { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2036
2037  { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2038  { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2039
2040  { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2041
2042  { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2043
2044  { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2045
2046  { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2047
2048  { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2049};
2050
2051/* The same as binops, but initialized by cp_parser_new so that
2052   binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
2053   for speed.  */
2054static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2055
2056/* Constructors and destructors.  */
2057
2058/* Construct a new context.  The context below this one on the stack
2059   is given by NEXT.  */
2060
2061static cp_parser_context *
2062cp_parser_context_new (cp_parser_context* next)
2063{
2064  cp_parser_context *context;
2065
2066  /* Allocate the storage.  */
2067  if (cp_parser_context_free_list != NULL)
2068    {
2069      /* Pull the first entry from the free list.  */
2070      context = cp_parser_context_free_list;
2071      cp_parser_context_free_list = context->next;
2072      memset (context, 0, sizeof (*context));
2073    }
2074  else
2075    context = ggc_cleared_alloc<cp_parser_context> ();
2076
2077  /* No errors have occurred yet in this context.  */
2078  context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2079  /* If this is not the bottommost context, copy information that we
2080     need from the previous context.  */
2081  if (next)
2082    {
2083      /* If, in the NEXT context, we are parsing an `x->' or `x.'
2084	 expression, then we are parsing one in this context, too.  */
2085      context->object_type = next->object_type;
2086      /* Thread the stack.  */
2087      context->next = next;
2088    }
2089
2090  return context;
2091}
2092
2093/* Managing the unparsed function queues.  */
2094
2095#define unparsed_funs_with_default_args \
2096  parser->unparsed_queues->last ().funs_with_default_args
2097#define unparsed_funs_with_definitions \
2098  parser->unparsed_queues->last ().funs_with_definitions
2099#define unparsed_nsdmis \
2100  parser->unparsed_queues->last ().nsdmis
2101#define unparsed_noexcepts \
2102  parser->unparsed_queues->last ().noexcepts
2103
2104static void
2105push_unparsed_function_queues (cp_parser *parser)
2106{
2107  cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2108  vec_safe_push (parser->unparsed_queues, e);
2109}
2110
2111static void
2112pop_unparsed_function_queues (cp_parser *parser)
2113{
2114  release_tree_vector (unparsed_funs_with_definitions);
2115  parser->unparsed_queues->pop ();
2116}
2117
2118/* Prototypes.  */
2119
2120/* Constructors and destructors.  */
2121
2122static cp_parser *cp_parser_new
2123  (cp_lexer *);
2124
2125/* Routines to parse various constructs.
2126
2127   Those that return `tree' will return the error_mark_node (rather
2128   than NULL_TREE) if a parse error occurs, unless otherwise noted.
2129   Sometimes, they will return an ordinary node if error-recovery was
2130   attempted, even though a parse error occurred.  So, to check
2131   whether or not a parse error occurred, you should always use
2132   cp_parser_error_occurred.  If the construct is optional (indicated
2133   either by an `_opt' in the name of the function that does the
2134   parsing or via a FLAGS parameter), then NULL_TREE is returned if
2135   the construct is not present.  */
2136
2137/* Lexical conventions [gram.lex]  */
2138
2139static cp_expr cp_parser_identifier
2140  (cp_parser *);
2141static cp_expr cp_parser_string_literal
2142  (cp_parser *, bool, bool, bool);
2143static cp_expr cp_parser_userdef_char_literal
2144  (cp_parser *);
2145static tree cp_parser_userdef_string_literal
2146  (tree);
2147static cp_expr cp_parser_userdef_numeric_literal
2148  (cp_parser *);
2149
2150/* Basic concepts [gram.basic]  */
2151
2152static void cp_parser_translation_unit (cp_parser *);
2153
2154/* Expressions [gram.expr]  */
2155
2156static cp_expr cp_parser_primary_expression
2157  (cp_parser *, bool, bool, bool, cp_id_kind *);
2158static cp_expr cp_parser_id_expression
2159  (cp_parser *, bool, bool, bool *, bool, bool);
2160static cp_expr cp_parser_unqualified_id
2161  (cp_parser *, bool, bool, bool, bool);
2162static tree cp_parser_nested_name_specifier_opt
2163  (cp_parser *, bool, bool, bool, bool, bool = false);
2164static tree cp_parser_nested_name_specifier
2165  (cp_parser *, bool, bool, bool, bool);
2166static tree cp_parser_qualifying_entity
2167  (cp_parser *, bool, bool, bool, bool, bool);
2168static cp_expr cp_parser_postfix_expression
2169  (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2170static tree cp_parser_postfix_open_square_expression
2171  (cp_parser *, tree, bool, bool);
2172static tree cp_parser_postfix_dot_deref_expression
2173  (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2174static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2175  (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2176   bool = false);
2177/* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
2178enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2179static void cp_parser_pseudo_destructor_name
2180  (cp_parser *, tree, tree *, tree *);
2181static cp_expr cp_parser_unary_expression
2182  (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2183static enum tree_code cp_parser_unary_operator
2184  (cp_token *);
2185static tree cp_parser_has_attribute_expression
2186  (cp_parser *);
2187static tree cp_parser_new_expression
2188  (cp_parser *);
2189static vec<tree, va_gc> *cp_parser_new_placement
2190  (cp_parser *);
2191static tree cp_parser_new_type_id
2192  (cp_parser *, tree *);
2193static cp_declarator *cp_parser_new_declarator_opt
2194  (cp_parser *);
2195static cp_declarator *cp_parser_direct_new_declarator
2196  (cp_parser *);
2197static vec<tree, va_gc> *cp_parser_new_initializer
2198  (cp_parser *);
2199static tree cp_parser_delete_expression
2200  (cp_parser *);
2201static cp_expr cp_parser_cast_expression
2202  (cp_parser *, bool, bool, bool, cp_id_kind *);
2203static cp_expr cp_parser_binary_expression
2204  (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2205static tree cp_parser_question_colon_clause
2206  (cp_parser *, cp_expr);
2207static cp_expr cp_parser_assignment_expression
2208  (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2209static enum tree_code cp_parser_assignment_operator_opt
2210  (cp_parser *);
2211static cp_expr cp_parser_expression
2212  (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2213static cp_expr cp_parser_constant_expression
2214  (cp_parser *, int = 0, bool * = NULL, bool = false);
2215static cp_expr cp_parser_builtin_offsetof
2216  (cp_parser *);
2217static cp_expr cp_parser_lambda_expression
2218  (cp_parser *);
2219static void cp_parser_lambda_introducer
2220  (cp_parser *, tree);
2221static bool cp_parser_lambda_declarator_opt
2222  (cp_parser *, tree);
2223static void cp_parser_lambda_body
2224  (cp_parser *, tree);
2225
2226/* Statements [gram.stmt.stmt]  */
2227
2228static void cp_parser_statement
2229  (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2230static void cp_parser_label_for_labeled_statement
2231(cp_parser *, tree);
2232static tree cp_parser_expression_statement
2233  (cp_parser *, tree);
2234static tree cp_parser_compound_statement
2235  (cp_parser *, tree, int, bool);
2236static void cp_parser_statement_seq_opt
2237  (cp_parser *, tree);
2238static tree cp_parser_selection_statement
2239  (cp_parser *, bool *, vec<tree> *);
2240static tree cp_parser_condition
2241  (cp_parser *);
2242static tree cp_parser_iteration_statement
2243  (cp_parser *, bool *, bool, unsigned short);
2244static bool cp_parser_init_statement
2245  (cp_parser *, tree *decl);
2246static tree cp_parser_for
2247  (cp_parser *, bool, unsigned short);
2248static tree cp_parser_c_for
2249  (cp_parser *, tree, tree, bool, unsigned short);
2250static tree cp_parser_range_for
2251  (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2252static void do_range_for_auto_deduction
2253  (tree, tree);
2254static tree cp_parser_perform_range_for_lookup
2255  (tree, tree *, tree *);
2256static tree cp_parser_range_for_member_function
2257  (tree, tree);
2258static tree cp_parser_jump_statement
2259  (cp_parser *);
2260static void cp_parser_declaration_statement
2261  (cp_parser *);
2262
2263static tree cp_parser_implicitly_scoped_statement
2264  (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2265static void cp_parser_already_scoped_statement
2266  (cp_parser *, bool *, const token_indent_info &);
2267
2268/* State of module-declaration parsing.  */
2269enum module_parse
2270{
2271  MP_NOT_MODULE,	/* Not a module.  */
2272
2273  _MP_UNUSED,
2274
2275  MP_FIRST,	/* First declaration of TU.  */
2276  MP_GLOBAL,	/* Global Module Fragment.  */
2277
2278  MP_PURVIEW_IMPORTS,   /* Imports of a module.  */
2279  MP_PURVIEW,	/* Purview of a named module.  */
2280
2281  MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment.  */
2282  MP_PRIVATE,   /* Private Module Fragment.  */
2283};
2284
2285static module_parse cp_parser_module_declaration
2286  (cp_parser *parser, module_parse, bool exporting);
2287static void cp_parser_import_declaration
2288  (cp_parser *parser, module_parse, bool exporting);
2289
2290/* Declarations [gram.dcl.dcl] */
2291
2292static void cp_parser_declaration_seq_opt
2293  (cp_parser *);
2294static void cp_parser_declaration
2295  (cp_parser *, tree);
2296static void cp_parser_toplevel_declaration
2297  (cp_parser *);
2298static void cp_parser_block_declaration
2299  (cp_parser *, bool);
2300static void cp_parser_simple_declaration
2301  (cp_parser *, bool, tree *);
2302static void cp_parser_decl_specifier_seq
2303  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2304static tree cp_parser_storage_class_specifier_opt
2305  (cp_parser *);
2306static tree cp_parser_function_specifier_opt
2307  (cp_parser *, cp_decl_specifier_seq *);
2308static tree cp_parser_type_specifier
2309  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2310   int *, bool *);
2311static tree cp_parser_simple_type_specifier
2312  (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2313static tree cp_parser_placeholder_type_specifier
2314  (cp_parser *, location_t, tree, bool);
2315static tree cp_parser_type_name
2316  (cp_parser *, bool);
2317static tree cp_parser_nonclass_name
2318  (cp_parser* parser);
2319static tree cp_parser_elaborated_type_specifier
2320  (cp_parser *, bool, bool);
2321static tree cp_parser_enum_specifier
2322  (cp_parser *);
2323static void cp_parser_enumerator_list
2324  (cp_parser *, tree);
2325static void cp_parser_enumerator_definition
2326  (cp_parser *, tree);
2327static tree cp_parser_namespace_name
2328  (cp_parser *);
2329static void cp_parser_namespace_definition
2330  (cp_parser *);
2331static void cp_parser_namespace_body
2332  (cp_parser *);
2333static tree cp_parser_qualified_namespace_specifier
2334  (cp_parser *);
2335static void cp_parser_namespace_alias_definition
2336  (cp_parser *);
2337static bool cp_parser_using_declaration
2338  (cp_parser *, bool);
2339static void cp_parser_using_directive
2340  (cp_parser *);
2341static void cp_parser_using_enum
2342  (cp_parser *);
2343static tree cp_parser_alias_declaration
2344  (cp_parser *);
2345static void cp_parser_asm_definition
2346  (cp_parser *);
2347static void cp_parser_linkage_specification
2348  (cp_parser *, tree);
2349static void cp_parser_static_assert
2350  (cp_parser *, bool);
2351static tree cp_parser_decltype
2352  (cp_parser *);
2353static tree cp_parser_decomposition_declaration
2354  (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2355
2356/* Declarators [gram.dcl.decl] */
2357
2358static tree cp_parser_init_declarator
2359  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2360   vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2361   location_t *, tree *);
2362static cp_declarator *cp_parser_declarator
2363  (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2364   bool, bool, bool);
2365static cp_declarator *cp_parser_direct_declarator
2366  (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2367   bool);
2368static enum tree_code cp_parser_ptr_operator
2369  (cp_parser *, tree *, cp_cv_quals *, tree *);
2370static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2371  (cp_parser *);
2372static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2373  (cp_parser *);
2374static cp_ref_qualifier cp_parser_ref_qualifier_opt
2375  (cp_parser *);
2376static tree cp_parser_tx_qualifier_opt
2377  (cp_parser *);
2378static tree cp_parser_late_return_type_opt
2379  (cp_parser *, cp_declarator *, tree &);
2380static tree cp_parser_declarator_id
2381  (cp_parser *, bool);
2382static tree cp_parser_type_id
2383  (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2384static tree cp_parser_template_type_arg
2385  (cp_parser *);
2386static tree cp_parser_trailing_type_id (cp_parser *);
2387static tree cp_parser_type_id_1
2388  (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2389static void cp_parser_type_specifier_seq
2390  (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2391static tree cp_parser_parameter_declaration_clause
2392  (cp_parser *, cp_parser_flags);
2393static tree cp_parser_parameter_declaration_list
2394  (cp_parser *, cp_parser_flags);
2395static cp_parameter_declarator *cp_parser_parameter_declaration
2396  (cp_parser *, cp_parser_flags, bool, bool *);
2397static tree cp_parser_default_argument
2398  (cp_parser *, bool);
2399static void cp_parser_function_body
2400  (cp_parser *, bool);
2401static tree cp_parser_initializer
2402  (cp_parser *, bool *, bool *, bool = false);
2403static cp_expr cp_parser_initializer_clause
2404  (cp_parser *, bool *);
2405static cp_expr cp_parser_braced_list
2406  (cp_parser*, bool*);
2407static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2408  (cp_parser *, bool *, bool *);
2409
2410static void cp_parser_ctor_initializer_opt_and_function_body
2411  (cp_parser *, bool);
2412
2413static tree cp_parser_late_parsing_omp_declare_simd
2414  (cp_parser *, tree);
2415
2416static tree cp_parser_late_parsing_oacc_routine
2417  (cp_parser *, tree);
2418
2419static tree synthesize_implicit_template_parm
2420  (cp_parser *, tree);
2421static tree finish_fully_implicit_template
2422  (cp_parser *, tree);
2423static void abort_fully_implicit_template
2424  (cp_parser *);
2425
2426/* Classes [gram.class] */
2427
2428static tree cp_parser_class_name
2429  (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2430static tree cp_parser_class_specifier
2431  (cp_parser *);
2432static tree cp_parser_class_head
2433  (cp_parser *, bool *);
2434static enum tag_types cp_parser_class_key
2435  (cp_parser *);
2436static void cp_parser_type_parameter_key
2437  (cp_parser* parser);
2438static void cp_parser_member_specification_opt
2439  (cp_parser *);
2440static void cp_parser_member_declaration
2441  (cp_parser *);
2442static tree cp_parser_pure_specifier
2443  (cp_parser *);
2444static tree cp_parser_constant_initializer
2445  (cp_parser *);
2446
2447/* Derived classes [gram.class.derived] */
2448
2449static tree cp_parser_base_clause
2450  (cp_parser *);
2451static tree cp_parser_base_specifier
2452  (cp_parser *);
2453
2454/* Special member functions [gram.special] */
2455
2456static tree cp_parser_conversion_function_id
2457  (cp_parser *);
2458static tree cp_parser_conversion_type_id
2459  (cp_parser *);
2460static cp_declarator *cp_parser_conversion_declarator_opt
2461  (cp_parser *);
2462static void cp_parser_ctor_initializer_opt
2463  (cp_parser *);
2464static void cp_parser_mem_initializer_list
2465  (cp_parser *);
2466static tree cp_parser_mem_initializer
2467  (cp_parser *);
2468static tree cp_parser_mem_initializer_id
2469  (cp_parser *);
2470
2471/* Overloading [gram.over] */
2472
2473static cp_expr cp_parser_operator_function_id
2474  (cp_parser *);
2475static cp_expr cp_parser_operator
2476  (cp_parser *, location_t);
2477
2478/* Templates [gram.temp] */
2479
2480static void cp_parser_template_declaration
2481  (cp_parser *, bool);
2482static tree cp_parser_template_parameter_list
2483  (cp_parser *);
2484static tree cp_parser_template_parameter
2485  (cp_parser *, bool *, bool *);
2486static tree cp_parser_type_parameter
2487  (cp_parser *, bool *);
2488static tree cp_parser_template_id
2489  (cp_parser *, bool, bool, enum tag_types, bool);
2490static tree cp_parser_template_id_expr
2491  (cp_parser *, bool, bool, bool);
2492static tree cp_parser_template_name
2493  (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2494static tree cp_parser_template_argument_list
2495  (cp_parser *);
2496static tree cp_parser_template_argument
2497  (cp_parser *);
2498static void cp_parser_explicit_instantiation
2499  (cp_parser *);
2500static void cp_parser_explicit_specialization
2501  (cp_parser *);
2502
2503/* Exception handling [gram.except] */
2504
2505static tree cp_parser_try_block
2506  (cp_parser *);
2507static void cp_parser_function_try_block
2508  (cp_parser *);
2509static void cp_parser_handler_seq
2510  (cp_parser *);
2511static void cp_parser_handler
2512  (cp_parser *);
2513static tree cp_parser_exception_declaration
2514  (cp_parser *);
2515static tree cp_parser_throw_expression
2516  (cp_parser *);
2517static tree cp_parser_exception_specification_opt
2518  (cp_parser *, cp_parser_flags);
2519static tree cp_parser_type_id_list
2520  (cp_parser *);
2521static tree cp_parser_noexcept_specification_opt
2522  (cp_parser *, cp_parser_flags, bool, bool *, bool);
2523
2524/* GNU Extensions */
2525
2526static tree cp_parser_asm_specification_opt
2527  (cp_parser *);
2528static tree cp_parser_asm_operand_list
2529  (cp_parser *);
2530static tree cp_parser_asm_clobber_list
2531  (cp_parser *);
2532static tree cp_parser_asm_label_list
2533  (cp_parser *);
2534static bool cp_next_tokens_can_be_attribute_p
2535  (cp_parser *);
2536static bool cp_next_tokens_can_be_gnu_attribute_p
2537  (cp_parser *);
2538static bool cp_next_tokens_can_be_std_attribute_p
2539  (cp_parser *);
2540static bool cp_nth_tokens_can_be_std_attribute_p
2541  (cp_parser *, size_t);
2542static bool cp_nth_tokens_can_be_gnu_attribute_p
2543  (cp_parser *, size_t);
2544static bool cp_nth_tokens_can_be_attribute_p
2545  (cp_parser *, size_t);
2546static tree cp_parser_attributes_opt
2547  (cp_parser *);
2548static tree cp_parser_gnu_attributes_opt
2549  (cp_parser *);
2550static tree cp_parser_gnu_attribute_list
2551  (cp_parser *, bool = false);
2552static tree cp_parser_std_attribute
2553  (cp_parser *, tree);
2554static tree cp_parser_std_attribute_spec
2555  (cp_parser *);
2556static tree cp_parser_std_attribute_spec_seq
2557  (cp_parser *);
2558static size_t cp_parser_skip_std_attribute_spec_seq
2559  (cp_parser *, size_t);
2560static size_t cp_parser_skip_attributes_opt
2561  (cp_parser *, size_t);
2562static bool cp_parser_extension_opt
2563  (cp_parser *, int *);
2564static void cp_parser_label_declaration
2565  (cp_parser *);
2566
2567/* Concept Extensions */
2568
2569static tree cp_parser_concept_definition
2570  (cp_parser *);
2571static tree cp_parser_constraint_expression
2572  (cp_parser *);
2573static tree cp_parser_requires_clause_opt
2574  (cp_parser *, bool);
2575static tree cp_parser_requires_expression
2576  (cp_parser *);
2577static tree cp_parser_requirement_parameter_list
2578  (cp_parser *);
2579static tree cp_parser_requirement_body
2580  (cp_parser *);
2581static tree cp_parser_requirement_seq
2582  (cp_parser *);
2583static tree cp_parser_requirement
2584  (cp_parser *);
2585static tree cp_parser_simple_requirement
2586  (cp_parser *);
2587static tree cp_parser_compound_requirement
2588  (cp_parser *);
2589static tree cp_parser_type_requirement
2590  (cp_parser *);
2591static tree cp_parser_nested_requirement
2592  (cp_parser *);
2593
2594/* Transactional Memory Extensions */
2595
2596static tree cp_parser_transaction
2597  (cp_parser *, cp_token *);
2598static tree cp_parser_transaction_expression
2599  (cp_parser *, enum rid);
2600static void cp_parser_function_transaction
2601  (cp_parser *, enum rid);
2602static tree cp_parser_transaction_cancel
2603  (cp_parser *);
2604
2605/* Coroutine extensions.  */
2606
2607static tree cp_parser_yield_expression
2608  (cp_parser *);
2609
2610
2611enum pragma_context {
2612  pragma_external,
2613  pragma_member,
2614  pragma_objc_icode,
2615  pragma_stmt,
2616  pragma_compound
2617};
2618static bool cp_parser_pragma
2619  (cp_parser *, enum pragma_context, bool *);
2620
2621/* Objective-C++ Productions */
2622
2623static tree cp_parser_objc_message_receiver
2624  (cp_parser *);
2625static tree cp_parser_objc_message_args
2626  (cp_parser *);
2627static tree cp_parser_objc_message_expression
2628  (cp_parser *);
2629static cp_expr cp_parser_objc_encode_expression
2630  (cp_parser *);
2631static tree cp_parser_objc_defs_expression
2632  (cp_parser *);
2633static tree cp_parser_objc_protocol_expression
2634  (cp_parser *);
2635static tree cp_parser_objc_selector_expression
2636  (cp_parser *);
2637static cp_expr cp_parser_objc_expression
2638  (cp_parser *);
2639static bool cp_parser_objc_selector_p
2640  (enum cpp_ttype);
2641static tree cp_parser_objc_selector
2642  (cp_parser *);
2643static tree cp_parser_objc_protocol_refs_opt
2644  (cp_parser *);
2645static void cp_parser_objc_declaration
2646  (cp_parser *, tree);
2647static tree cp_parser_objc_statement
2648  (cp_parser *);
2649static bool cp_parser_objc_valid_prefix_attributes
2650  (cp_parser *, tree *);
2651static void cp_parser_objc_at_property_declaration
2652  (cp_parser *) ;
2653static void cp_parser_objc_at_synthesize_declaration
2654  (cp_parser *) ;
2655static void cp_parser_objc_at_dynamic_declaration
2656  (cp_parser *) ;
2657static tree cp_parser_objc_struct_declaration
2658  (cp_parser *) ;
2659
2660/* Utility Routines */
2661
2662static cp_expr cp_parser_lookup_name
2663  (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2664static tree cp_parser_lookup_name_simple
2665  (cp_parser *, tree, location_t);
2666static tree cp_parser_maybe_treat_template_as_class
2667  (tree, bool);
2668static bool cp_parser_check_declarator_template_parameters
2669  (cp_parser *, cp_declarator *, location_t);
2670static bool cp_parser_check_template_parameters
2671  (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2672static cp_expr cp_parser_simple_cast_expression
2673  (cp_parser *);
2674static tree cp_parser_global_scope_opt
2675  (cp_parser *, bool);
2676static bool cp_parser_constructor_declarator_p
2677  (cp_parser *, cp_parser_flags, bool);
2678static tree cp_parser_function_definition_from_specifiers_and_declarator
2679  (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2680static tree cp_parser_function_definition_after_declarator
2681  (cp_parser *, bool);
2682static bool cp_parser_template_declaration_after_export
2683  (cp_parser *, bool);
2684static void cp_parser_perform_template_parameter_access_checks
2685  (vec<deferred_access_check, va_gc> *);
2686static tree cp_parser_single_declaration
2687  (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2688static cp_expr cp_parser_functional_cast
2689  (cp_parser *, tree);
2690static tree cp_parser_save_member_function_body
2691  (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2692static tree cp_parser_save_nsdmi
2693  (cp_parser *);
2694static tree cp_parser_enclosed_template_argument_list
2695  (cp_parser *);
2696static void cp_parser_save_default_args
2697  (cp_parser *, tree);
2698static void cp_parser_late_parsing_for_member
2699  (cp_parser *, tree);
2700static tree cp_parser_late_parse_one_default_arg
2701  (cp_parser *, tree, tree, tree);
2702static void cp_parser_late_parsing_nsdmi
2703  (cp_parser *, tree);
2704static void cp_parser_late_parsing_default_args
2705  (cp_parser *, tree);
2706static tree cp_parser_sizeof_operand
2707  (cp_parser *, enum rid);
2708static cp_expr cp_parser_trait_expr
2709  (cp_parser *, enum rid);
2710static bool cp_parser_declares_only_class_p
2711  (cp_parser *);
2712static void cp_parser_set_storage_class
2713  (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2714static void cp_parser_set_decl_spec_type
2715  (cp_decl_specifier_seq *, tree, cp_token *, bool);
2716static void set_and_check_decl_spec_loc
2717  (cp_decl_specifier_seq *decl_specs,
2718   cp_decl_spec ds, cp_token *);
2719static bool cp_parser_friend_p
2720  (const cp_decl_specifier_seq *);
2721static void cp_parser_required_error
2722  (cp_parser *, required_token, bool, location_t);
2723static cp_token *cp_parser_require
2724  (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2725static cp_token *cp_parser_require_keyword
2726  (cp_parser *, enum rid, required_token);
2727static bool cp_parser_token_starts_function_definition_p
2728  (cp_token *);
2729static bool cp_parser_next_token_starts_class_definition_p
2730  (cp_parser *);
2731static bool cp_parser_next_token_ends_template_argument_p
2732  (cp_parser *);
2733static bool cp_parser_nth_token_starts_template_argument_list_p
2734  (cp_parser *, size_t);
2735static enum tag_types cp_parser_token_is_class_key
2736  (cp_token *);
2737static enum tag_types cp_parser_token_is_type_parameter_key
2738  (cp_token *);
2739static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2740static void cp_parser_check_class_key
2741(cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2742static void cp_parser_check_access_in_redeclaration
2743  (tree type, location_t location);
2744static bool cp_parser_optional_template_keyword
2745  (cp_parser *);
2746static void cp_parser_pre_parsed_nested_name_specifier
2747  (cp_parser *);
2748static bool cp_parser_cache_group
2749  (cp_parser *, enum cpp_ttype, unsigned);
2750static tree cp_parser_cache_defarg
2751  (cp_parser *parser, bool nsdmi);
2752static void cp_parser_parse_tentatively
2753  (cp_parser *);
2754static void cp_parser_commit_to_tentative_parse
2755  (cp_parser *);
2756static void cp_parser_commit_to_topmost_tentative_parse
2757  (cp_parser *);
2758static void cp_parser_abort_tentative_parse
2759  (cp_parser *);
2760static bool cp_parser_parse_definitely
2761  (cp_parser *);
2762static inline bool cp_parser_parsing_tentatively
2763  (cp_parser *);
2764static bool cp_parser_uncommitted_to_tentative_parse_p
2765  (cp_parser *);
2766static void cp_parser_error
2767  (cp_parser *, const char *);
2768static void cp_parser_name_lookup_error
2769  (cp_parser *, tree, tree, name_lookup_error, location_t);
2770static bool cp_parser_simulate_error
2771  (cp_parser *);
2772static bool cp_parser_check_type_definition
2773  (cp_parser *);
2774static void cp_parser_check_for_definition_in_return_type
2775  (cp_declarator *, tree, location_t type_location);
2776static void cp_parser_check_for_invalid_template_id
2777  (cp_parser *, tree, enum tag_types, location_t location);
2778static bool cp_parser_non_integral_constant_expression
2779  (cp_parser *, non_integral_constant);
2780static void cp_parser_diagnose_invalid_type_name
2781  (cp_parser *, tree, location_t);
2782static bool cp_parser_parse_and_diagnose_invalid_type_name
2783  (cp_parser *);
2784static int cp_parser_skip_to_closing_parenthesis
2785  (cp_parser *, bool, bool, bool);
2786static void cp_parser_skip_to_end_of_statement
2787  (cp_parser *);
2788static void cp_parser_consume_semicolon_at_end_of_statement
2789  (cp_parser *);
2790static void cp_parser_skip_to_end_of_block_or_statement
2791  (cp_parser *);
2792static bool cp_parser_skip_to_closing_brace
2793  (cp_parser *);
2794static bool cp_parser_skip_entire_template_parameter_list
2795  (cp_parser *);
2796static void cp_parser_require_end_of_template_parameter_list
2797  (cp_parser *);
2798static bool cp_parser_skip_to_end_of_template_parameter_list
2799  (cp_parser *);
2800static void cp_parser_skip_to_pragma_eol
2801  (cp_parser*, cp_token *);
2802static bool cp_parser_error_occurred
2803  (cp_parser *);
2804static bool cp_parser_allow_gnu_extensions_p
2805  (cp_parser *);
2806static bool cp_parser_is_pure_string_literal
2807  (cp_token *);
2808static bool cp_parser_is_string_literal
2809  (cp_token *);
2810static bool cp_parser_is_keyword
2811  (cp_token *, enum rid);
2812static tree cp_parser_make_typename_type
2813  (cp_parser *, tree, location_t location);
2814static cp_declarator * cp_parser_make_indirect_declarator
2815  (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2816static bool cp_parser_compound_literal_p
2817  (cp_parser *);
2818static bool cp_parser_array_designator_p
2819  (cp_parser *);
2820static bool cp_parser_init_statement_p
2821  (cp_parser *);
2822static bool cp_parser_skip_to_closing_square_bracket
2823  (cp_parser *);
2824static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2825
2826// -------------------------------------------------------------------------- //
2827// Unevaluated Operand Guard
2828//
2829// Implementation of an RAII helper for unevaluated operand parsing.
2830cp_unevaluated::cp_unevaluated ()
2831{
2832  ++cp_unevaluated_operand;
2833  ++c_inhibit_evaluation_warnings;
2834}
2835
2836cp_unevaluated::~cp_unevaluated ()
2837{
2838  --c_inhibit_evaluation_warnings;
2839  --cp_unevaluated_operand;
2840}
2841
2842// -------------------------------------------------------------------------- //
2843// Tentative Parsing
2844
2845/* Returns nonzero if we are parsing tentatively.  */
2846
2847static inline bool
2848cp_parser_parsing_tentatively (cp_parser* parser)
2849{
2850  return parser->context->next != NULL;
2851}
2852
2853/* Returns nonzero if TOKEN is a string literal.  */
2854
2855static bool
2856cp_parser_is_pure_string_literal (cp_token* token)
2857{
2858  return (token->type == CPP_STRING ||
2859	  token->type == CPP_STRING16 ||
2860	  token->type == CPP_STRING32 ||
2861	  token->type == CPP_WSTRING ||
2862	  token->type == CPP_UTF8STRING);
2863}
2864
2865/* Returns nonzero if TOKEN is a string literal
2866   of a user-defined string literal.  */
2867
2868static bool
2869cp_parser_is_string_literal (cp_token* token)
2870{
2871  return (cp_parser_is_pure_string_literal (token) ||
2872	  token->type == CPP_STRING_USERDEF ||
2873	  token->type == CPP_STRING16_USERDEF ||
2874	  token->type == CPP_STRING32_USERDEF ||
2875	  token->type == CPP_WSTRING_USERDEF ||
2876	  token->type == CPP_UTF8STRING_USERDEF);
2877}
2878
2879/* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2880
2881static bool
2882cp_parser_is_keyword (cp_token* token, enum rid keyword)
2883{
2884  return token->keyword == keyword;
2885}
2886
2887/* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2888   PRAGMA_NONE.  */
2889
2890static enum pragma_kind
2891cp_parser_pragma_kind (cp_token *token)
2892{
2893  if (token->type != CPP_PRAGMA)
2894    return PRAGMA_NONE;
2895  /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
2896  return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2897}
2898
2899/* Helper function for cp_parser_error.
2900   Having peeked a token of kind TOK1_KIND that might signify
2901   a conflict marker, peek successor tokens to determine
2902   if we actually do have a conflict marker.
2903   Specifically, we consider a run of 7 '<', '=' or '>' characters
2904   at the start of a line as a conflict marker.
2905   These come through the lexer as three pairs and a single,
2906   e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2907   If it returns true, *OUT_LOC is written to with the location/range
2908   of the marker.  */
2909
2910static bool
2911cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2912			       location_t *out_loc)
2913{
2914  cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2915  if (token2->type != tok1_kind)
2916    return false;
2917  cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2918  if (token3->type != tok1_kind)
2919    return false;
2920  cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2921  if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2922    return false;
2923
2924  /* It must be at the start of the line.  */
2925  location_t start_loc = cp_lexer_peek_token (lexer)->location;
2926  if (LOCATION_COLUMN (start_loc) != 1)
2927    return false;
2928
2929  /* We have a conflict marker.  Construct a location of the form:
2930       <<<<<<<
2931       ^~~~~~~
2932     with start == caret, finishing at the end of the marker.  */
2933  location_t finish_loc = get_finish (token4->location);
2934  *out_loc = make_location (start_loc, start_loc, finish_loc);
2935
2936  return true;
2937}
2938
2939/* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2940   RT_CLOSE_PAREN.  */
2941
2942static const char *
2943get_matching_symbol (required_token token_desc)
2944{
2945  switch (token_desc)
2946    {
2947    default:
2948      gcc_unreachable ();
2949      return "";
2950    case RT_CLOSE_BRACE:
2951      return "{";
2952    case RT_CLOSE_PAREN:
2953      return "(";
2954    }
2955}
2956
2957/* Attempt to convert TOKEN_DESC from a required_token to an
2958   enum cpp_ttype, returning CPP_EOF if there is no good conversion.  */
2959
2960static enum cpp_ttype
2961get_required_cpp_ttype (required_token token_desc)
2962{
2963  switch (token_desc)
2964    {
2965    case RT_SEMICOLON:
2966      return CPP_SEMICOLON;
2967    case RT_OPEN_PAREN:
2968      return CPP_OPEN_PAREN;
2969    case RT_CLOSE_BRACE:
2970      return CPP_CLOSE_BRACE;
2971    case RT_OPEN_BRACE:
2972      return CPP_OPEN_BRACE;
2973    case RT_CLOSE_SQUARE:
2974      return CPP_CLOSE_SQUARE;
2975    case RT_OPEN_SQUARE:
2976      return CPP_OPEN_SQUARE;
2977    case RT_COMMA:
2978      return CPP_COMMA;
2979    case RT_COLON:
2980      return CPP_COLON;
2981    case RT_CLOSE_PAREN:
2982      return CPP_CLOSE_PAREN;
2983
2984    default:
2985      /* Use CPP_EOF as a "no completions possible" code.  */
2986      return CPP_EOF;
2987    }
2988}
2989
2990
2991/* Subroutine of cp_parser_error and cp_parser_required_error.
2992
2993   Issue a diagnostic of the form
2994      FILE:LINE: MESSAGE before TOKEN
2995   where TOKEN is the next token in the input stream.  MESSAGE
2996   (specified by the caller) is usually of the form "expected
2997   OTHER-TOKEN".
2998
2999   This bypasses the check for tentative passing, and potentially
3000   adds material needed by cp_parser_required_error.
3001
3002   If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3003   suggesting insertion of the missing token.
3004
3005   Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3006   have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3007   location.  */
3008
3009static void
3010cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3011		   required_token missing_token_desc,
3012		   location_t matching_location)
3013{
3014  cp_token *token = cp_lexer_peek_token (parser->lexer);
3015  /* This diagnostic makes more sense if it is tagged to the line
3016     of the token we just peeked at.  */
3017  cp_lexer_set_source_position_from_token (token);
3018
3019  if (token->type == CPP_PRAGMA)
3020    {
3021      error_at (token->location,
3022		"%<#pragma%> is not allowed here");
3023      cp_parser_skip_to_pragma_eol (parser, token);
3024      return;
3025    }
3026
3027  /* If this is actually a conflict marker, report it as such.  */
3028  if (token->type == CPP_LSHIFT
3029      || token->type == CPP_RSHIFT
3030      || token->type == CPP_EQ_EQ)
3031    {
3032      location_t loc;
3033      if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3034	{
3035	  error_at (loc, "version control conflict marker in file");
3036	  expanded_location token_exploc = expand_location (token->location);
3037	  /* Consume tokens until the end of the source line.  */
3038	  for (;;)
3039	    {
3040	      cp_lexer_consume_token (parser->lexer);
3041	      cp_token *next = cp_lexer_peek_token (parser->lexer);
3042	      if (next->type == CPP_EOF)
3043		break;
3044	      if (next->location == UNKNOWN_LOCATION
3045		  || loc == UNKNOWN_LOCATION)
3046		break;
3047
3048	      expanded_location next_exploc = expand_location (next->location);
3049	      if (next_exploc.file != token_exploc.file)
3050		break;
3051	      if (next_exploc.line != token_exploc.line)
3052		break;
3053	    }
3054	  return;
3055	}
3056    }
3057
3058  auto_diagnostic_group d;
3059  gcc_rich_location richloc (input_location);
3060
3061  bool added_matching_location = false;
3062
3063  if (missing_token_desc != RT_NONE)
3064    if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3065      {
3066	/* Potentially supply a fix-it hint, suggesting to add the
3067	   missing token immediately after the *previous* token.
3068	   This may move the primary location within richloc.  */
3069	enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3070	location_t prev_token_loc = prev_token->location;
3071	maybe_suggest_missing_token_insertion (&richloc, ttype,
3072					       prev_token_loc);
3073
3074	/* If matching_location != UNKNOWN_LOCATION, highlight it.
3075	   Attempt to consolidate diagnostics by printing it as a
3076	   secondary range within the main diagnostic.  */
3077	if (matching_location != UNKNOWN_LOCATION)
3078	  added_matching_location
3079	    = richloc.add_location_if_nearby (matching_location);
3080      }
3081
3082  /* If we were parsing a string-literal and there is an unknown name
3083     token right after, then check to see if that could also have been
3084     a literal string by checking the name against a list of known
3085     standard string literal constants defined in header files. If
3086     there is one, then add that as an hint to the error message. */
3087  name_hint h;
3088  if (token->type == CPP_NAME)
3089    if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3090      if (cp_parser_is_string_literal (prev_token))
3091	{
3092	  tree name = token->u.value;
3093	  const char *token_name = IDENTIFIER_POINTER (name);
3094	  const char *header_hint
3095	    = get_cp_stdlib_header_for_string_macro_name (token_name);
3096	  if (header_hint != NULL)
3097	    h = name_hint (NULL, new suggest_missing_header (token->location,
3098							     token_name,
3099							     header_hint));
3100	}
3101
3102  /* Actually emit the error.  */
3103  c_parse_error (gmsgid,
3104		 /* Because c_parser_error does not understand
3105		    CPP_KEYWORD, keywords are treated like
3106		    identifiers.  */
3107		 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3108		 token->u.value, token->flags, &richloc);
3109
3110  if (missing_token_desc != RT_NONE)
3111    {
3112      /* If we weren't able to consolidate matching_location, then
3113	 print it as a secondary diagnostic.  */
3114      if (matching_location != UNKNOWN_LOCATION
3115	  && !added_matching_location)
3116	inform (matching_location, "to match this %qs",
3117		get_matching_symbol (missing_token_desc));
3118    }
3119}
3120
3121/* If not parsing tentatively, issue a diagnostic of the form
3122      FILE:LINE: MESSAGE before TOKEN
3123   where TOKEN is the next token in the input stream.  MESSAGE
3124   (specified by the caller) is usually of the form "expected
3125   OTHER-TOKEN".  */
3126
3127static void
3128cp_parser_error (cp_parser* parser, const char* gmsgid)
3129{
3130  if (!cp_parser_simulate_error (parser))
3131    cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3132}
3133
3134/* Issue an error about name-lookup failing.  NAME is the
3135   IDENTIFIER_NODE DECL is the result of
3136   the lookup (as returned from cp_parser_lookup_name).  DESIRED is
3137   the thing that we hoped to find.  */
3138
3139static void
3140cp_parser_name_lookup_error (cp_parser* parser,
3141			     tree name,
3142			     tree decl,
3143			     name_lookup_error desired,
3144			     location_t location)
3145{
3146  /* If name lookup completely failed, tell the user that NAME was not
3147     declared.  */
3148  if (decl == error_mark_node)
3149    {
3150      if (parser->scope && parser->scope != global_namespace)
3151	error_at (location, "%<%E::%E%> has not been declared",
3152		  parser->scope, name);
3153      else if (parser->scope == global_namespace)
3154	error_at (location, "%<::%E%> has not been declared", name);
3155      else if (parser->object_scope
3156	       && !CLASS_TYPE_P (parser->object_scope))
3157	error_at (location, "request for member %qE in non-class type %qT",
3158		  name, parser->object_scope);
3159      else if (parser->object_scope)
3160	error_at (location, "%<%T::%E%> has not been declared",
3161		  parser->object_scope, name);
3162      else
3163	error_at (location, "%qE has not been declared", name);
3164    }
3165  else if (parser->scope && parser->scope != global_namespace)
3166    {
3167      switch (desired)
3168	{
3169	  case NLE_TYPE:
3170	    error_at (location, "%<%E::%E%> is not a type",
3171	    			parser->scope, name);
3172	    break;
3173	  case NLE_CXX98:
3174	    error_at (location, "%<%E::%E%> is not a class or namespace",
3175	    			parser->scope, name);
3176	    break;
3177	  case NLE_NOT_CXX98:
3178	    error_at (location,
3179	    	      "%<%E::%E%> is not a class, namespace, or enumeration",
3180		      parser->scope, name);
3181	    break;
3182	  default:
3183	    gcc_unreachable ();
3184
3185	}
3186    }
3187  else if (parser->scope == global_namespace)
3188    {
3189      switch (desired)
3190	{
3191	  case NLE_TYPE:
3192	    error_at (location, "%<::%E%> is not a type", name);
3193	    break;
3194	  case NLE_CXX98:
3195	    error_at (location, "%<::%E%> is not a class or namespace", name);
3196	    break;
3197	  case NLE_NOT_CXX98:
3198	    error_at (location,
3199		      "%<::%E%> is not a class, namespace, or enumeration",
3200		      name);
3201	    break;
3202	  default:
3203	    gcc_unreachable ();
3204	}
3205    }
3206  else
3207    {
3208      switch (desired)
3209	{
3210	  case NLE_TYPE:
3211	    error_at (location, "%qE is not a type", name);
3212	    break;
3213	  case NLE_CXX98:
3214	    error_at (location, "%qE is not a class or namespace", name);
3215	    break;
3216	  case NLE_NOT_CXX98:
3217	    error_at (location,
3218		      "%qE is not a class, namespace, or enumeration", name);
3219	    break;
3220	  default:
3221	    gcc_unreachable ();
3222	}
3223    }
3224}
3225
3226/* If we are parsing tentatively, remember that an error has occurred
3227   during this tentative parse.  Returns true if the error was
3228   simulated; false if a message should be issued by the caller.  */
3229
3230static bool
3231cp_parser_simulate_error (cp_parser* parser)
3232{
3233  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3234    {
3235      parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3236      return true;
3237    }
3238  return false;
3239}
3240
3241/* This function is called when a type is defined.  If type
3242   definitions are forbidden at this point, an error message is
3243   issued.  */
3244
3245static bool
3246cp_parser_check_type_definition (cp_parser* parser)
3247{
3248  /* If types are forbidden here, issue a message.  */
3249  if (parser->type_definition_forbidden_message)
3250    {
3251      /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3252	 or %qs in the message need to be interpreted.  */
3253      error (parser->type_definition_forbidden_message,
3254	     parser->type_definition_forbidden_message_arg);
3255      return false;
3256    }
3257  return true;
3258}
3259
3260/* This function is called when the DECLARATOR is processed.  The TYPE
3261   was a type defined in the decl-specifiers.  If it is invalid to
3262   define a type in the decl-specifiers for DECLARATOR, an error is
3263   issued. TYPE_LOCATION is the location of TYPE and is used
3264   for error reporting.  */
3265
3266static void
3267cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3268					       tree type, location_t type_location)
3269{
3270  /* [dcl.fct] forbids type definitions in return types.
3271     Unfortunately, it's not easy to know whether or not we are
3272     processing a return type until after the fact.  */
3273  while (declarator
3274	 && (declarator->kind == cdk_pointer
3275	     || declarator->kind == cdk_reference
3276	     || declarator->kind == cdk_ptrmem))
3277    declarator = declarator->declarator;
3278  if (declarator
3279      && declarator->kind == cdk_function)
3280    {
3281      error_at (type_location,
3282		"new types may not be defined in a return type");
3283      inform (type_location,
3284	      "(perhaps a semicolon is missing after the definition of %qT)",
3285	      type);
3286    }
3287}
3288
3289/* A type-specifier (TYPE) has been parsed which cannot be followed by
3290   "<" in any valid C++ program.  If the next token is indeed "<",
3291   issue a message warning the user about what appears to be an
3292   invalid attempt to form a template-id. LOCATION is the location
3293   of the type-specifier (TYPE) */
3294
3295static void
3296cp_parser_check_for_invalid_template_id (cp_parser* parser,
3297					 tree type,
3298					 enum tag_types tag_type,
3299					 location_t location)
3300{
3301  cp_token_position start = 0;
3302
3303  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3304    {
3305      if (TREE_CODE (type) == TYPE_DECL)
3306	type = TREE_TYPE (type);
3307      if (TYPE_P (type) && !template_placeholder_p (type))
3308	error_at (location, "%qT is not a template", type);
3309      else if (identifier_p (type))
3310	{
3311	  if (tag_type != none_type)
3312	    error_at (location, "%qE is not a class template", type);
3313	  else
3314	    error_at (location, "%qE is not a template", type);
3315	}
3316      else
3317	error_at (location, "invalid template-id");
3318      /* Remember the location of the invalid "<".  */
3319      if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3320	start = cp_lexer_token_position (parser->lexer, true);
3321      /* Consume the "<".  */
3322      cp_lexer_consume_token (parser->lexer);
3323      /* Parse the template arguments.  */
3324      cp_parser_enclosed_template_argument_list (parser);
3325      /* Permanently remove the invalid template arguments so that
3326	 this error message is not issued again.  */
3327      if (start)
3328	cp_lexer_purge_tokens_after (parser->lexer, start);
3329    }
3330}
3331
3332/* If parsing an integral constant-expression, issue an error message
3333   about the fact that THING appeared and return true.  Otherwise,
3334   return false.  In either case, set
3335   PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
3336
3337static bool
3338cp_parser_non_integral_constant_expression (cp_parser  *parser,
3339					    non_integral_constant thing)
3340{
3341  parser->non_integral_constant_expression_p = true;
3342  if (parser->integral_constant_expression_p)
3343    {
3344      if (!parser->allow_non_integral_constant_expression_p)
3345	{
3346	  const char *msg = NULL;
3347	  switch (thing)
3348	    {
3349  	      case NIC_FLOAT:
3350		pedwarn (input_location, OPT_Wpedantic,
3351			 "ISO C++ forbids using a floating-point literal "
3352			 "in a constant-expression");
3353		return true;
3354	      case NIC_CAST:
3355		error ("a cast to a type other than an integral or "
3356		       "enumeration type cannot appear in a "
3357		       "constant-expression");
3358		return true;
3359	      case NIC_TYPEID:
3360		error ("%<typeid%> operator "
3361		       "cannot appear in a constant-expression");
3362		return true;
3363	      case NIC_NCC:
3364		error ("non-constant compound literals "
3365		       "cannot appear in a constant-expression");
3366		return true;
3367	      case NIC_FUNC_CALL:
3368		error ("a function call "
3369		       "cannot appear in a constant-expression");
3370		return true;
3371	      case NIC_INC:
3372		error ("an increment "
3373		       "cannot appear in a constant-expression");
3374		return true;
3375	      case NIC_DEC:
3376		error ("an decrement "
3377		       "cannot appear in a constant-expression");
3378		return true;
3379	      case NIC_ARRAY_REF:
3380		error ("an array reference "
3381		       "cannot appear in a constant-expression");
3382		return true;
3383	      case NIC_ADDR_LABEL:
3384		error ("the address of a label "
3385		       "cannot appear in a constant-expression");
3386		return true;
3387	      case NIC_OVERLOADED:
3388		error ("calls to overloaded operators "
3389		       "cannot appear in a constant-expression");
3390		return true;
3391	      case NIC_ASSIGNMENT:
3392		error ("an assignment cannot appear in a constant-expression");
3393		return true;
3394	      case NIC_COMMA:
3395		error ("a comma operator "
3396		       "cannot appear in a constant-expression");
3397		return true;
3398	      case NIC_CONSTRUCTOR:
3399		error ("a call to a constructor "
3400		       "cannot appear in a constant-expression");
3401		return true;
3402	      case NIC_TRANSACTION:
3403		error ("a transaction expression "
3404		       "cannot appear in a constant-expression");
3405		return true;
3406	      case NIC_THIS:
3407		msg = "this";
3408		break;
3409	      case NIC_FUNC_NAME:
3410		msg = "__FUNCTION__";
3411		break;
3412  	      case NIC_PRETTY_FUNC:
3413		msg = "__PRETTY_FUNCTION__";
3414		break;
3415	      case NIC_C99_FUNC:
3416		msg = "__func__";
3417		break;
3418	      case NIC_VA_ARG:
3419		msg = "va_arg";
3420		break;
3421	      case NIC_ARROW:
3422		msg = "->";
3423		break;
3424	      case NIC_POINT:
3425		msg = ".";
3426		break;
3427	      case NIC_STAR:
3428		msg = "*";
3429		break;
3430	      case NIC_ADDR:
3431		msg = "&";
3432		break;
3433	      case NIC_PREINCREMENT:
3434		msg = "++";
3435		break;
3436	      case NIC_PREDECREMENT:
3437		msg = "--";
3438		break;
3439	      case NIC_NEW:
3440		msg = "new";
3441		break;
3442	      case NIC_DEL:
3443		msg = "delete";
3444		break;
3445	      default:
3446		gcc_unreachable ();
3447	    }
3448	  if (msg)
3449	    error ("%qs cannot appear in a constant-expression", msg);
3450	  return true;
3451	}
3452    }
3453  return false;
3454}
3455
3456/* Emit a diagnostic for an invalid type name.  This function commits
3457   to the current active tentative parse, if any.  (Otherwise, the
3458   problematic construct might be encountered again later, resulting
3459   in duplicate error messages.) LOCATION is the location of ID.  */
3460
3461static void
3462cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3463				      location_t location)
3464{
3465  tree decl, ambiguous_decls;
3466  cp_parser_commit_to_tentative_parse (parser);
3467  /* Try to lookup the identifier.  */
3468  decl = cp_parser_lookup_name (parser, id, none_type,
3469				/*is_template=*/false,
3470				/*is_namespace=*/false,
3471				/*check_dependency=*/true,
3472				&ambiguous_decls, location);
3473  if (ambiguous_decls)
3474    /* If the lookup was ambiguous, an error will already have
3475       been issued.  */
3476    return;
3477  /* If the lookup found a template-name, it means that the user forgot
3478  to specify an argument list. Emit a useful error message.  */
3479  if (DECL_TYPE_TEMPLATE_P (decl))
3480    {
3481      auto_diagnostic_group d;
3482      error_at (location,
3483		"invalid use of template-name %qE without an argument list",
3484		decl);
3485      if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3486	inform (location, "class template argument deduction is only available "
3487		"with %<-std=c++17%> or %<-std=gnu++17%>");
3488      inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3489    }
3490  else if (TREE_CODE (id) == BIT_NOT_EXPR)
3491    error_at (location, "invalid use of destructor %qD as a type", id);
3492  else if (TREE_CODE (decl) == TYPE_DECL)
3493    /* Something like 'unsigned A a;'  */
3494    error_at (location, "invalid combination of multiple type-specifiers");
3495  else if (!parser->scope)
3496    {
3497      /* Issue an error message.  */
3498      auto_diagnostic_group d;
3499      name_hint hint;
3500      if (TREE_CODE (id) == IDENTIFIER_NODE)
3501	hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3502      if (const char *suggestion = hint.suggestion ())
3503	{
3504	  gcc_rich_location richloc (location);
3505	  richloc.add_fixit_replace (suggestion);
3506	  error_at (&richloc,
3507		    "%qE does not name a type; did you mean %qs?",
3508		    id, suggestion);
3509	}
3510      else
3511	error_at (location, "%qE does not name a type", id);
3512      /* If we're in a template class, it's possible that the user was
3513	 referring to a type from a base class.  For example:
3514
3515	   template <typename T> struct A { typedef T X; };
3516	   template <typename T> struct B : public A<T> { X x; };
3517
3518	 The user should have said "typename A<T>::X".  */
3519      if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3520	inform (location, "C++11 %<constexpr%> only available with "
3521		"%<-std=c++11%> or %<-std=gnu++11%>");
3522      else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3523	inform (location, "C++11 %<noexcept%> only available with "
3524		"%<-std=c++11%> or %<-std=gnu++11%>");
3525      else if (TREE_CODE (id) == IDENTIFIER_NODE
3526	       && (id_equal (id, "module") || id_equal (id, "import")))
3527	{
3528	  if (modules_p ())
3529	    inform (location, "%qE is not recognized as a module control-line",
3530		    id);
3531	  else if (cxx_dialect < cxx20)
3532	    inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3533		    id);
3534	  else
3535	    inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3536		    ", which is not yet enabled with %<-std=c++20%>", id);
3537	}
3538      else if (cxx_dialect < cxx11
3539	       && TREE_CODE (id) == IDENTIFIER_NODE
3540	       && id_equal (id, "thread_local"))
3541	inform (location, "C++11 %<thread_local%> only available with "
3542		"%<-std=c++11%> or %<-std=gnu++11%>");
3543      else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3544	inform (location, "C++20 %<constinit%> only available with "
3545		"%<-std=c++20%> or %<-std=gnu++20%>");
3546      else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3547	inform (location, "%<concept%> only available with %<-std=c++20%> or "
3548		"%<-fconcepts%>");
3549      else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3550	inform (location, "%<requires%> only available with %<-std=c++20%> or "
3551		"%<-fconcepts%>");
3552      else if (processing_template_decl && current_class_type
3553	       && TYPE_BINFO (current_class_type))
3554	{
3555	  for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3556	       b; b = TREE_CHAIN (b))
3557	    {
3558	      tree base_type = BINFO_TYPE (b);
3559	      if (CLASS_TYPE_P (base_type)
3560		  && dependent_type_p (base_type))
3561		{
3562		  /* Go from a particular instantiation of the
3563		     template (which will have an empty TYPE_FIELDs),
3564		     to the main version.  */
3565		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3566		  for (tree field = TYPE_FIELDS (base_type);
3567		       field; field = DECL_CHAIN (field))
3568		    if (TREE_CODE (field) == TYPE_DECL
3569			&& DECL_NAME (field) == id)
3570		      {
3571			inform (location,
3572				"(perhaps %<typename %T::%E%> was intended)",
3573				BINFO_TYPE (b), id);
3574			goto found;
3575		      }
3576		}
3577	    }
3578	found:;
3579	}
3580    }
3581  /* Here we diagnose qualified-ids where the scope is actually correct,
3582     but the identifier does not resolve to a valid type name.  */
3583  else if (parser->scope != error_mark_node)
3584    {
3585      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3586	{
3587	  auto_diagnostic_group d;
3588	  name_hint hint;
3589	  if (decl == error_mark_node)
3590	    hint = suggest_alternative_in_explicit_scope (location, id,
3591							  parser->scope);
3592	  const char *suggestion = hint.suggestion ();
3593	  gcc_rich_location richloc (location_of (id));
3594	  if (suggestion)
3595	    richloc.add_fixit_replace (suggestion);
3596	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3597	    {
3598	      if (suggestion)
3599		error_at (&richloc,
3600			  "%qE in namespace %qE does not name a template"
3601			  " type; did you mean %qs?",
3602			  id, parser->scope, suggestion);
3603	      else
3604		error_at (&richloc,
3605			  "%qE in namespace %qE does not name a template type",
3606			  id, parser->scope);
3607	    }
3608	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3609	    {
3610	      if (suggestion)
3611		error_at (&richloc,
3612			  "%qE in namespace %qE does not name a template"
3613			  " type; did you mean %qs?",
3614			  TREE_OPERAND (id, 0), parser->scope, suggestion);
3615	      else
3616		error_at (&richloc,
3617			  "%qE in namespace %qE does not name a template"
3618			  " type",
3619			  TREE_OPERAND (id, 0), parser->scope);
3620	    }
3621	  else
3622	    {
3623	      if (suggestion)
3624		error_at (&richloc,
3625			  "%qE in namespace %qE does not name a type"
3626			  "; did you mean %qs?",
3627			  id, parser->scope, suggestion);
3628	      else
3629		error_at (&richloc,
3630			  "%qE in namespace %qE does not name a type",
3631			  id, parser->scope);
3632	    }
3633	  if (DECL_P (decl))
3634	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3635	}
3636      else if (CLASS_TYPE_P (parser->scope)
3637	       && constructor_name_p (id, parser->scope))
3638	{
3639	  /* A<T>::A<T>() */
3640	  auto_diagnostic_group d;
3641	  error_at (location, "%<%T::%E%> names the constructor, not"
3642		    " the type", parser->scope, id);
3643	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3644	    error_at (location, "and %qT has no template constructors",
3645		      parser->scope);
3646	}
3647      else if (TYPE_P (parser->scope)
3648	       && dependent_scope_p (parser->scope))
3649	{
3650	  gcc_rich_location richloc (location);
3651	  richloc.add_fixit_insert_before ("typename ");
3652	  if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3653	    error_at (&richloc,
3654		      "need %<typename%> before %<%T::%D::%E%> because "
3655		      "%<%T::%D%> is a dependent scope",
3656		      TYPE_CONTEXT (parser->scope),
3657		      TYPENAME_TYPE_FULLNAME (parser->scope),
3658		      id,
3659		      TYPE_CONTEXT (parser->scope),
3660		      TYPENAME_TYPE_FULLNAME (parser->scope));
3661	  else
3662	    error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3663		      "%qT is a dependent scope",
3664		      parser->scope, id, parser->scope);
3665	}
3666      else if (TYPE_P (parser->scope))
3667	{
3668	  auto_diagnostic_group d;
3669	  if (!COMPLETE_TYPE_P (parser->scope))
3670	    cxx_incomplete_type_error (location_of (id), NULL_TREE,
3671				       parser->scope);
3672	  else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3673	    error_at (location_of (id),
3674		      "%qE in %q#T does not name a template type",
3675		      id, parser->scope);
3676	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3677	    error_at (location_of (id),
3678		      "%qE in %q#T does not name a template type",
3679		      TREE_OPERAND (id, 0), parser->scope);
3680	  else
3681	    error_at (location_of (id),
3682		      "%qE in %q#T does not name a type",
3683		      id, parser->scope);
3684	  if (DECL_P (decl))
3685	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3686	}
3687      else
3688	gcc_unreachable ();
3689    }
3690}
3691
3692/* Check for a common situation where a type-name should be present,
3693   but is not, and issue a sensible error message.  Returns true if an
3694   invalid type-name was detected.
3695
3696   The situation handled by this function are variable declarations of the
3697   form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3698   Usually, `ID' should name a type, but if we got here it means that it
3699   does not. We try to emit the best possible error message depending on
3700   how exactly the id-expression looks like.  */
3701
3702static bool
3703cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3704{
3705  tree id;
3706  cp_token *token = cp_lexer_peek_token (parser->lexer);
3707
3708  /* Avoid duplicate error about ambiguous lookup.  */
3709  if (token->type == CPP_NESTED_NAME_SPECIFIER)
3710    {
3711      cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3712      if (next->type == CPP_NAME && next->error_reported)
3713	goto out;
3714    }
3715
3716  cp_parser_parse_tentatively (parser);
3717  id = cp_parser_id_expression (parser,
3718				/*template_keyword_p=*/false,
3719				/*check_dependency_p=*/true,
3720				/*template_p=*/NULL,
3721				/*declarator_p=*/true,
3722				/*optional_p=*/false);
3723  /* If the next token is a (, this is a function with no explicit return
3724     type, i.e. constructor, destructor or conversion op.  */
3725  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3726      || TREE_CODE (id) == TYPE_DECL)
3727    {
3728      cp_parser_abort_tentative_parse (parser);
3729      return false;
3730    }
3731  if (!cp_parser_parse_definitely (parser))
3732    return false;
3733
3734  /* Emit a diagnostic for the invalid type.  */
3735  cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3736 out:
3737  /* If we aren't in the middle of a declarator (i.e. in a
3738     parameter-declaration-clause), skip to the end of the declaration;
3739     there's no point in trying to process it.  */
3740  if (!parser->in_declarator_p)
3741    cp_parser_skip_to_end_of_block_or_statement (parser);
3742  return true;
3743}
3744
3745/* Consume tokens up to, and including, the next non-nested closing `)'.
3746   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3747   are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3748   found an unnested token of that type.  */
3749
3750static int
3751cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3752					 bool recovering,
3753					 cpp_ttype or_ttype,
3754					 bool consume_paren)
3755{
3756  unsigned paren_depth = 0;
3757  unsigned brace_depth = 0;
3758  unsigned square_depth = 0;
3759  unsigned condop_depth = 0;
3760
3761  if (recovering && or_ttype == CPP_EOF
3762      && cp_parser_uncommitted_to_tentative_parse_p (parser))
3763    return 0;
3764
3765  while (true)
3766    {
3767      cp_token * token = cp_lexer_peek_token (parser->lexer);
3768
3769      /* Have we found what we're looking for before the closing paren?  */
3770      if (token->type == or_ttype && or_ttype != CPP_EOF
3771	  && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3772	return -1;
3773
3774      switch (token->type)
3775	{
3776	case CPP_PRAGMA_EOL:
3777	  if (!parser->lexer->in_pragma)
3778	    break;
3779	  /* FALLTHRU */
3780	case CPP_EOF:
3781	  /* If we've run out of tokens, then there is no closing `)'.  */
3782	  return 0;
3783
3784        /* This is good for lambda expression capture-lists.  */
3785        case CPP_OPEN_SQUARE:
3786          ++square_depth;
3787          break;
3788        case CPP_CLOSE_SQUARE:
3789          if (!square_depth--)
3790            return 0;
3791          break;
3792
3793	case CPP_SEMICOLON:
3794	  /* This matches the processing in skip_to_end_of_statement.  */
3795	  if (!brace_depth)
3796	    return 0;
3797	  break;
3798
3799	case CPP_OPEN_BRACE:
3800	  ++brace_depth;
3801	  break;
3802	case CPP_CLOSE_BRACE:
3803	  if (!brace_depth--)
3804	    return 0;
3805	  break;
3806
3807	case CPP_OPEN_PAREN:
3808	  if (!brace_depth)
3809	    ++paren_depth;
3810	  break;
3811
3812	case CPP_CLOSE_PAREN:
3813	  if (!brace_depth && !paren_depth--)
3814	    {
3815	      if (consume_paren)
3816		cp_lexer_consume_token (parser->lexer);
3817	      return 1;
3818	    }
3819	  break;
3820
3821	case CPP_QUERY:
3822	  if (!brace_depth && !paren_depth && !square_depth)
3823	    ++condop_depth;
3824	  break;
3825
3826	case CPP_COLON:
3827	  if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3828	    condop_depth--;
3829	  break;
3830
3831	case CPP_KEYWORD:
3832	  if (token->keyword != RID__EXPORT
3833	      && token->keyword != RID__MODULE
3834	      && token->keyword != RID__IMPORT)
3835	    break;
3836	  /* FALLTHROUGH  */
3837
3838	case CPP_PRAGMA:
3839	  /* We fell into a pragma.  Skip it, and continue. */
3840	  cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3841	  continue;
3842
3843	default:
3844	  break;
3845	}
3846
3847      /* Consume the token.  */
3848      cp_lexer_consume_token (parser->lexer);
3849    }
3850}
3851
3852/* Consume tokens up to, and including, the next non-nested closing `)'.
3853   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3854   are doing error recovery. Returns -1 if OR_COMMA is true and we
3855   found an unnested token of that type.  */
3856
3857static int
3858cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3859				       bool recovering,
3860				       bool or_comma,
3861				       bool consume_paren)
3862{
3863  cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3864  return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3865						  ttype, consume_paren);
3866}
3867
3868/* Consume tokens until we reach the end of the current statement.
3869   Normally, that will be just before consuming a `;'.  However, if a
3870   non-nested `}' comes first, then we stop before consuming that.  */
3871
3872static void
3873cp_parser_skip_to_end_of_statement (cp_parser* parser)
3874{
3875  unsigned nesting_depth = 0;
3876
3877  /* Unwind generic function template scope if necessary.  */
3878  if (parser->fully_implicit_function_template_p)
3879    abort_fully_implicit_template (parser);
3880
3881  while (true)
3882    {
3883      cp_token *token = cp_lexer_peek_token (parser->lexer);
3884
3885      switch (token->type)
3886	{
3887	case CPP_PRAGMA_EOL:
3888	  if (!parser->lexer->in_pragma)
3889	    break;
3890	  /* FALLTHRU */
3891	case CPP_EOF:
3892	  /* If we've run out of tokens, stop.  */
3893	  return;
3894
3895	case CPP_SEMICOLON:
3896	  /* If the next token is a `;', we have reached the end of the
3897	     statement.  */
3898	  if (!nesting_depth)
3899	    return;
3900	  break;
3901
3902	case CPP_CLOSE_BRACE:
3903	  /* If this is a non-nested '}', stop before consuming it.
3904	     That way, when confronted with something like:
3905
3906	       { 3 + }
3907
3908	     we stop before consuming the closing '}', even though we
3909	     have not yet reached a `;'.  */
3910	  if (nesting_depth == 0)
3911	    return;
3912
3913	  /* If it is the closing '}' for a block that we have
3914	     scanned, stop -- but only after consuming the token.
3915	     That way given:
3916
3917		void f g () { ... }
3918		typedef int I;
3919
3920	     we will stop after the body of the erroneously declared
3921	     function, but before consuming the following `typedef'
3922	     declaration.  */
3923	  if (--nesting_depth == 0)
3924	    {
3925	      cp_lexer_consume_token (parser->lexer);
3926	      return;
3927	    }
3928	  break;
3929
3930	case CPP_OPEN_BRACE:
3931	  ++nesting_depth;
3932	  break;
3933
3934	case CPP_KEYWORD:
3935	  if (token->keyword != RID__EXPORT
3936	      && token->keyword != RID__MODULE
3937	      && token->keyword != RID__IMPORT)
3938	    break;
3939	  /* FALLTHROUGH  */
3940
3941	case CPP_PRAGMA:
3942	  /* We fell into a pragma.  Skip it, and continue or return. */
3943	  cp_parser_skip_to_pragma_eol (parser, token);
3944	  if (!nesting_depth)
3945	    return;
3946	  continue;
3947
3948	default:
3949	  break;
3950	}
3951
3952      /* Consume the token.  */
3953      cp_lexer_consume_token (parser->lexer);
3954    }
3955}
3956
3957/* This function is called at the end of a statement or declaration.
3958   If the next token is a semicolon, it is consumed; otherwise, error
3959   recovery is attempted.  */
3960
3961static void
3962cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3963{
3964  /* Look for the trailing `;'.  */
3965  if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3966    {
3967      /* If there is additional (erroneous) input, skip to the end of
3968	 the statement.  */
3969      cp_parser_skip_to_end_of_statement (parser);
3970      /* If the next token is now a `;', consume it.  */
3971      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3972	cp_lexer_consume_token (parser->lexer);
3973    }
3974}
3975
3976/* Skip tokens until we have consumed an entire block, or until we
3977   have consumed a non-nested `;'.  */
3978
3979static void
3980cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3981{
3982  int nesting_depth = 0;
3983
3984  /* Unwind generic function template scope if necessary.  */
3985  if (parser->fully_implicit_function_template_p)
3986    abort_fully_implicit_template (parser);
3987
3988  while (nesting_depth >= 0)
3989    {
3990      cp_token *token = cp_lexer_peek_token (parser->lexer);
3991
3992      switch (token->type)
3993	{
3994	case CPP_PRAGMA_EOL:
3995	  if (!parser->lexer->in_pragma)
3996	    break;
3997	  /* FALLTHRU */
3998	case CPP_EOF:
3999	  /* If we've run out of tokens, stop.  */
4000	  return;
4001
4002	case CPP_SEMICOLON:
4003	  /* Stop if this is an unnested ';'. */
4004	  if (!nesting_depth)
4005	    nesting_depth = -1;
4006	  break;
4007
4008	case CPP_CLOSE_BRACE:
4009	  /* Stop if this is an unnested '}', or closes the outermost
4010	     nesting level.  */
4011	  nesting_depth--;
4012	  if (nesting_depth < 0)
4013	    return;
4014	  if (!nesting_depth)
4015	    nesting_depth = -1;
4016	  break;
4017
4018	case CPP_OPEN_BRACE:
4019	  /* Nest. */
4020	  nesting_depth++;
4021	  break;
4022
4023	case CPP_KEYWORD:
4024	  if (token->keyword != RID__EXPORT
4025	      && token->keyword != RID__MODULE
4026	      && token->keyword != RID__IMPORT)
4027	    break;
4028	  /* FALLTHROUGH  */
4029
4030	case CPP_PRAGMA:
4031	  /* Skip it, and continue or return. */
4032	  cp_parser_skip_to_pragma_eol (parser, token);
4033	  if (!nesting_depth)
4034	    return;
4035	  continue;
4036
4037	default:
4038	  break;
4039	}
4040
4041      /* Consume the token.  */
4042      cp_lexer_consume_token (parser->lexer);
4043    }
4044}
4045
4046/* Skip tokens until a non-nested closing curly brace is the next
4047   token, or there are no more tokens. Return true in the first case,
4048   false otherwise.  */
4049
4050static bool
4051cp_parser_skip_to_closing_brace (cp_parser *parser)
4052{
4053  unsigned nesting_depth = 0;
4054
4055  while (true)
4056    {
4057      cp_token *token = cp_lexer_peek_token (parser->lexer);
4058
4059      switch (token->type)
4060	{
4061	case CPP_PRAGMA_EOL:
4062	  if (!parser->lexer->in_pragma)
4063	    break;
4064	  /* FALLTHRU */
4065	case CPP_EOF:
4066	  /* If we've run out of tokens, stop.  */
4067	  return false;
4068
4069	case CPP_CLOSE_BRACE:
4070	  /* If the next token is a non-nested `}', then we have reached
4071	     the end of the current block.  */
4072	  if (nesting_depth-- == 0)
4073	    return true;
4074	  break;
4075
4076	case CPP_OPEN_BRACE:
4077	  /* If it the next token is a `{', then we are entering a new
4078	     block.  Consume the entire block.  */
4079	  ++nesting_depth;
4080	  break;
4081
4082	default:
4083	  break;
4084	}
4085
4086      /* Consume the token.  */
4087      cp_lexer_consume_token (parser->lexer);
4088    }
4089}
4090
4091/* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
4092   parameter is the PRAGMA token, allowing us to purge the entire pragma
4093   sequence.  PRAGMA_TOK can be NULL, if we're speculatively scanning
4094   forwards (not error recovery).  */
4095
4096static void
4097cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4098{
4099  cp_token *token;
4100
4101  do
4102    {
4103      /* The preprocessor makes sure that a PRAGMA_EOL token appears
4104         before an EOF token, even when the EOF is on the pragma line.
4105         We should never get here without being inside a deferred
4106         pragma.  */
4107      gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4108      token = cp_lexer_consume_token (parser->lexer);
4109    }
4110  while (token->type != CPP_PRAGMA_EOL);
4111
4112  if (pragma_tok)
4113    {
4114      parser->lexer->in_pragma = false;
4115      if (parser->lexer->in_omp_attribute_pragma
4116	  && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4117	{
4118	  parser->lexer = parser->lexer->next;
4119	  /* Put the current source position back where it was before this
4120	     lexer was pushed.  */
4121	  cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4122	}
4123    }
4124}
4125
4126/* Require pragma end of line, resyncing with it as necessary.  The
4127   arguments are as for cp_parser_skip_to_pragma_eol.  */
4128
4129static void
4130cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4131{
4132  parser->lexer->in_pragma = false;
4133  if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4134    cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4135  else if (parser->lexer->in_omp_attribute_pragma
4136	   && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4137    {
4138      parser->lexer = parser->lexer->next;
4139      /* Put the current source position back where it was before this
4140	 lexer was pushed.  */
4141      cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4142    }
4143}
4144
4145/* This is a simple wrapper around make_typename_type. When the id is
4146   an unresolved identifier node, we can provide a superior diagnostic
4147   using cp_parser_diagnose_invalid_type_name.  */
4148
4149static tree
4150cp_parser_make_typename_type (cp_parser *parser, tree id,
4151			      location_t id_location)
4152{
4153  tree result;
4154  if (identifier_p (id))
4155    {
4156      result = make_typename_type (parser->scope, id, typename_type,
4157				   /*complain=*/tf_none);
4158      if (result == error_mark_node)
4159	cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4160      return result;
4161    }
4162  return make_typename_type (parser->scope, id, typename_type, tf_error);
4163}
4164
4165/* This is a wrapper around the
4166   make_{pointer,ptrmem,reference}_declarator functions that decides
4167   which one to call based on the CODE and CLASS_TYPE arguments. The
4168   CODE argument should be one of the values returned by
4169   cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
4170   appertain to the pointer or reference.  */
4171
4172static cp_declarator *
4173cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4174				    cp_cv_quals cv_qualifiers,
4175				    cp_declarator *target,
4176				    tree attributes)
4177{
4178  if (code == ERROR_MARK || target == cp_error_declarator)
4179    return cp_error_declarator;
4180
4181  if (code == INDIRECT_REF)
4182    if (class_type == NULL_TREE)
4183      return make_pointer_declarator (cv_qualifiers, target, attributes);
4184    else
4185      return make_ptrmem_declarator (cv_qualifiers, class_type,
4186				     target, attributes);
4187  else if (code == ADDR_EXPR && class_type == NULL_TREE)
4188    return make_reference_declarator (cv_qualifiers, target,
4189				      false, attributes);
4190  else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4191    return make_reference_declarator (cv_qualifiers, target,
4192				      true, attributes);
4193  gcc_unreachable ();
4194}
4195
4196/* Create a new C++ parser.  */
4197
4198static cp_parser *
4199cp_parser_new (cp_lexer *lexer)
4200{
4201  /* Initialize the binops_by_token so that we can get the tree
4202     directly from the token.  */
4203  for (unsigned i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
4204    binops_by_token[binops[i].token_type] = binops[i];
4205
4206  cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4207  parser->lexer = lexer;
4208  parser->context = cp_parser_context_new (NULL);
4209
4210  /* For now, we always accept GNU extensions.  */
4211  parser->allow_gnu_extensions_p = 1;
4212
4213  /* The `>' token is a greater-than operator, not the end of a
4214     template-id.  */
4215  parser->greater_than_is_operator_p = true;
4216
4217  parser->default_arg_ok_p = true;
4218
4219  /* We are not parsing a constant-expression.  */
4220  parser->integral_constant_expression_p = false;
4221  parser->allow_non_integral_constant_expression_p = false;
4222  parser->non_integral_constant_expression_p = false;
4223
4224  /* Local variable names are not forbidden.  */
4225  parser->local_variables_forbidden_p = 0;
4226
4227  /* We are not processing an `extern "C"' declaration.  */
4228  parser->in_unbraced_linkage_specification_p = false;
4229
4230  /* We are not processing a declarator.  */
4231  parser->in_declarator_p = false;
4232
4233  /* We are not processing a template-argument-list.  */
4234  parser->in_template_argument_list_p = false;
4235
4236  /* We are not in an iteration statement.  */
4237  parser->in_statement = 0;
4238
4239  /* We are not in a switch statement.  */
4240  parser->in_switch_statement_p = false;
4241
4242  /* We are not parsing a type-id inside an expression.  */
4243  parser->in_type_id_in_expr_p = false;
4244
4245  /* String literals should be translated to the execution character set.  */
4246  parser->translate_strings_p = true;
4247
4248  /* We are not parsing a function body.  */
4249  parser->in_function_body = false;
4250
4251  /* We can correct until told otherwise.  */
4252  parser->colon_corrects_to_scope_p = true;
4253
4254  /* The unparsed function queue is empty.  */
4255  push_unparsed_function_queues (parser);
4256
4257  /* There are no classes being defined.  */
4258  parser->num_classes_being_defined = 0;
4259
4260  /* No template parameters apply.  */
4261  parser->num_template_parameter_lists = 0;
4262
4263  /* Special parsing data structures.  */
4264  parser->omp_declare_simd = NULL;
4265  parser->oacc_routine = NULL;
4266
4267  /* Not declaring an implicit function template.  */
4268  parser->auto_is_implicit_function_template_parm_p = false;
4269  parser->fully_implicit_function_template_p = false;
4270  parser->implicit_template_parms = 0;
4271  parser->implicit_template_scope = 0;
4272
4273  /* Allow constrained-type-specifiers. */
4274  parser->prevent_constrained_type_specifiers = 0;
4275
4276  /* We haven't yet seen an 'extern "C"'.  */
4277  parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4278
4279  return parser;
4280}
4281
4282/* Create a cp_lexer structure which will emit the tokens in CACHE
4283   and push it onto the parser's lexer stack.  This is used for delayed
4284   parsing of in-class method bodies and default arguments, and should
4285   not be confused with tentative parsing.  */
4286static void
4287cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4288{
4289  cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4290  lexer->next = parser->lexer;
4291  parser->lexer = lexer;
4292
4293  /* Move the current source position to that of the first token in the
4294     new lexer.  */
4295  cp_lexer_set_source_position_from_token (lexer->next_token);
4296}
4297
4298/* Pop the top lexer off the parser stack.  This is never used for the
4299   "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
4300static void
4301cp_parser_pop_lexer (cp_parser *parser)
4302{
4303  cp_lexer *lexer = parser->lexer;
4304  parser->lexer = lexer->next;
4305  cp_lexer_destroy (lexer);
4306
4307  /* Put the current source position back where it was before this
4308     lexer was pushed.  */
4309  cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4310}
4311
4312/* Lexical conventions [gram.lex]  */
4313
4314/* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
4315   identifier.  */
4316
4317static cp_expr
4318cp_parser_identifier (cp_parser* parser)
4319{
4320  cp_token *token;
4321
4322  /* Look for the identifier.  */
4323  token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4324  /* Return the value.  */
4325  if (token)
4326    return cp_expr (token->u.value, token->location);
4327  else
4328    return error_mark_node;
4329}
4330
4331/* Parse a sequence of adjacent string constants.  Returns a
4332   TREE_STRING representing the combined, nul-terminated string
4333   constant.  If TRANSLATE is true, translate the string to the
4334   execution character set.  If WIDE_OK is true, a wide string is
4335   invalid here.
4336
4337   C++98 [lex.string] says that if a narrow string literal token is
4338   adjacent to a wide string literal token, the behavior is undefined.
4339   However, C99 6.4.5p4 says that this results in a wide string literal.
4340   We follow C99 here, for consistency with the C front end.
4341
4342   This code is largely lifted from lex_string() in c-lex.cc.
4343
4344   FUTURE: ObjC++ will need to handle @-strings here.  */
4345static cp_expr
4346cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4347			  bool lookup_udlit = true)
4348{
4349  tree value;
4350  size_t count;
4351  struct obstack str_ob;
4352  struct obstack loc_ob;
4353  cpp_string str, istr, *strs;
4354  cp_token *tok;
4355  enum cpp_ttype type, curr_type;
4356  int have_suffix_p = 0;
4357  tree string_tree;
4358  tree suffix_id = NULL_TREE;
4359  bool curr_tok_is_userdef_p = false;
4360
4361  tok = cp_lexer_peek_token (parser->lexer);
4362  if (!cp_parser_is_string_literal (tok))
4363    {
4364      cp_parser_error (parser, "expected string-literal");
4365      return error_mark_node;
4366    }
4367
4368  location_t loc = tok->location;
4369
4370  if (cpp_userdef_string_p (tok->type))
4371    {
4372      string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4373      curr_type = cpp_userdef_string_remove_type (tok->type);
4374      curr_tok_is_userdef_p = true;
4375    }
4376  else
4377    {
4378      string_tree = tok->u.value;
4379      curr_type = tok->type;
4380    }
4381  type = curr_type;
4382
4383  /* Try to avoid the overhead of creating and destroying an obstack
4384     for the common case of just one string.  */
4385  if (!cp_parser_is_string_literal
4386      (cp_lexer_peek_nth_token (parser->lexer, 2)))
4387    {
4388      cp_lexer_consume_token (parser->lexer);
4389
4390      str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4391      str.len = TREE_STRING_LENGTH (string_tree);
4392      count = 1;
4393
4394      if (curr_tok_is_userdef_p)
4395	{
4396	  suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4397	  have_suffix_p = 1;
4398	  curr_type = cpp_userdef_string_remove_type (tok->type);
4399	}
4400      else
4401	curr_type = tok->type;
4402
4403      strs = &str;
4404    }
4405  else
4406    {
4407      location_t last_tok_loc = tok->location;
4408      gcc_obstack_init (&str_ob);
4409      gcc_obstack_init (&loc_ob);
4410      count = 0;
4411
4412      do
4413	{
4414	  cp_lexer_consume_token (parser->lexer);
4415	  count++;
4416	  str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4417	  str.len = TREE_STRING_LENGTH (string_tree);
4418
4419	  if (curr_tok_is_userdef_p)
4420	    {
4421	      tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4422	      if (have_suffix_p == 0)
4423		{
4424		  suffix_id = curr_suffix_id;
4425		  have_suffix_p = 1;
4426		}
4427	      else if (have_suffix_p == 1
4428		       && curr_suffix_id != suffix_id)
4429		{
4430		  error ("inconsistent user-defined literal suffixes"
4431			 " %qD and %qD in string literal",
4432			 suffix_id, curr_suffix_id);
4433		  have_suffix_p = -1;
4434		}
4435	      curr_type = cpp_userdef_string_remove_type (tok->type);
4436	    }
4437	  else
4438	    curr_type = tok->type;
4439
4440	  if (type != curr_type)
4441	    {
4442	      if (type == CPP_STRING)
4443		type = curr_type;
4444	      else if (curr_type != CPP_STRING)
4445		{
4446		  rich_location rich_loc (line_table, tok->location);
4447		  rich_loc.add_range (last_tok_loc);
4448		  error_at (&rich_loc,
4449			    "concatenation of string literals with "
4450			    "conflicting encoding prefixes");
4451		}
4452	    }
4453
4454	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
4455	  obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4456
4457	  last_tok_loc = tok->location;
4458
4459	  tok = cp_lexer_peek_token (parser->lexer);
4460	  if (cpp_userdef_string_p (tok->type))
4461	    {
4462	      string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4463	      curr_type = cpp_userdef_string_remove_type (tok->type);
4464	      curr_tok_is_userdef_p = true;
4465	    }
4466	  else
4467	    {
4468	      string_tree = tok->u.value;
4469	      curr_type = tok->type;
4470	      curr_tok_is_userdef_p = false;
4471	    }
4472	}
4473      while (cp_parser_is_string_literal (tok));
4474
4475      /* A string literal built by concatenation has its caret=start at
4476	 the start of the initial string, and its finish at the finish of
4477	 the final string literal.  */
4478      loc = make_location (loc, loc, get_finish (last_tok_loc));
4479
4480      strs = (cpp_string *) obstack_finish (&str_ob);
4481    }
4482
4483  if (type != CPP_STRING && !wide_ok)
4484    {
4485      cp_parser_error (parser, "a wide string is invalid in this context");
4486      type = CPP_STRING;
4487    }
4488
4489  if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4490      (parse_in, strs, count, &istr, type))
4491    {
4492      value = build_string (istr.len, (const char *)istr.text);
4493      free (CONST_CAST (unsigned char *, istr.text));
4494      if (count > 1)
4495	{
4496	  location_t *locs = (location_t *)obstack_finish (&loc_ob);
4497	  gcc_assert (g_string_concat_db);
4498	  g_string_concat_db->record_string_concatenation (count, locs);
4499	}
4500
4501      switch (type)
4502	{
4503	default:
4504	case CPP_STRING:
4505	  TREE_TYPE (value) = char_array_type_node;
4506	  break;
4507	case CPP_UTF8STRING:
4508	  if (flag_char8_t)
4509	    TREE_TYPE (value) = char8_array_type_node;
4510	  else
4511	    TREE_TYPE (value) = char_array_type_node;
4512	  break;
4513	case CPP_STRING16:
4514	  TREE_TYPE (value) = char16_array_type_node;
4515	  break;
4516	case CPP_STRING32:
4517	  TREE_TYPE (value) = char32_array_type_node;
4518	  break;
4519	case CPP_WSTRING:
4520	  TREE_TYPE (value) = wchar_array_type_node;
4521	  break;
4522	}
4523
4524      value = fix_string_type (value);
4525
4526      if (have_suffix_p)
4527	{
4528	  tree literal = build_userdef_literal (suffix_id, value,
4529						OT_NONE, NULL_TREE);
4530	  if (lookup_udlit)
4531	    value = cp_parser_userdef_string_literal (literal);
4532	  else
4533	    value = literal;
4534	}
4535    }
4536  else
4537    /* cpp_interpret_string has issued an error.  */
4538    value = error_mark_node;
4539
4540  if (count > 1)
4541    {
4542      obstack_free (&str_ob, 0);
4543      obstack_free (&loc_ob, 0);
4544    }
4545
4546  return cp_expr (value, loc);
4547}
4548
4549/* Look up a literal operator with the name and the exact arguments.  */
4550
4551static tree
4552lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4553{
4554  tree decl = lookup_name (name);
4555  if (!decl || !is_overloaded_fn (decl))
4556    return error_mark_node;
4557
4558  for (lkp_iterator iter (decl); iter; ++iter)
4559    {
4560      tree fn = *iter;
4561
4562      if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4563	{
4564	  unsigned int ix;
4565	  bool found = true;
4566
4567	  for (ix = 0;
4568	       found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4569	       ++ix, parmtypes = TREE_CHAIN (parmtypes))
4570	    {
4571	      tree tparm = TREE_VALUE (parmtypes);
4572	      tree targ = TREE_TYPE ((*args)[ix]);
4573	      bool ptr = TYPE_PTR_P (tparm);
4574	      bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4575	      if ((ptr || arr || !same_type_p (tparm, targ))
4576		  && (!ptr || !arr
4577		      || !same_type_p (TREE_TYPE (tparm),
4578				       TREE_TYPE (targ))))
4579		found = false;
4580	    }
4581
4582	  if (found
4583	      && ix == vec_safe_length (args)
4584	      /* May be this should be sufficient_parms_p instead,
4585		 depending on how exactly should user-defined literals
4586		 work in presence of default arguments on the literal
4587		 operator parameters.  */
4588	      && parmtypes == void_list_node)
4589	    return decl;
4590	}
4591    }
4592
4593  return error_mark_node;
4594}
4595
4596/* Parse a user-defined char constant.  Returns a call to a user-defined
4597   literal operator taking the character as an argument.  */
4598
4599static cp_expr
4600cp_parser_userdef_char_literal (cp_parser *parser)
4601{
4602  cp_token *token = cp_lexer_consume_token (parser->lexer);
4603  tree literal = token->u.value;
4604  tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4605  tree value = USERDEF_LITERAL_VALUE (literal);
4606  tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4607  tree decl, result;
4608
4609  /* Build up a call to the user-defined operator  */
4610  /* Lookup the name we got back from the id-expression.  */
4611  releasing_vec args;
4612  vec_safe_push (args, value);
4613  decl = lookup_literal_operator (name, args);
4614  if (!decl || decl == error_mark_node)
4615    {
4616      error ("unable to find character literal operator %qD with %qT argument",
4617	     name, TREE_TYPE (value));
4618      return error_mark_node;
4619    }
4620  result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4621  return result;
4622}
4623
4624/* A subroutine of cp_parser_userdef_numeric_literal to
4625   create a char... template parameter pack from a string node.  */
4626
4627static tree
4628make_char_string_pack (tree value)
4629{
4630  tree charvec;
4631  tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4632  const unsigned char *str
4633    = (const unsigned char *) TREE_STRING_POINTER (value);
4634  int i, len = TREE_STRING_LENGTH (value) - 1;
4635  tree argvec = make_tree_vec (1);
4636
4637  /* Fill in CHARVEC with all of the parameters.  */
4638  charvec = make_tree_vec (len);
4639  for (i = 0; i < len; ++i)
4640    {
4641      unsigned char s[3] = { '\'', str[i], '\'' };
4642      cpp_string in = { 3, s };
4643      cpp_string out = { 0, 0 };
4644      if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4645	return NULL_TREE;
4646      gcc_assert (out.len == 2);
4647      TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4648						 out.text[0]);
4649    }
4650
4651  /* Build the argument packs.  */
4652  SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4653
4654  TREE_VEC_ELT (argvec, 0) = argpack;
4655
4656  return argvec;
4657}
4658
4659/* A subroutine of cp_parser_userdef_numeric_literal to
4660   create a char... template parameter pack from a string node.  */
4661
4662static tree
4663make_string_pack (tree value)
4664{
4665  tree charvec;
4666  tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4667  const unsigned char *str
4668    = (const unsigned char *) TREE_STRING_POINTER (value);
4669  int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4670  int len = TREE_STRING_LENGTH (value) / sz - 1;
4671  tree argvec = make_tree_vec (2);
4672
4673  tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4674  str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4675
4676  /* First template parm is character type.  */
4677  TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4678
4679  /* Fill in CHARVEC with all of the parameters.  */
4680  charvec = make_tree_vec (len);
4681  for (int i = 0; i < len; ++i)
4682    TREE_VEC_ELT (charvec, i)
4683      = double_int_to_tree (str_char_type_node,
4684			    double_int::from_buffer (str + i * sz, sz));
4685
4686  /* Build the argument packs.  */
4687  SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4688
4689  TREE_VEC_ELT (argvec, 1) = argpack;
4690
4691  return argvec;
4692}
4693
4694/* Parse a user-defined numeric constant.  returns a call to a user-defined
4695   literal operator.  */
4696
4697static cp_expr
4698cp_parser_userdef_numeric_literal (cp_parser *parser)
4699{
4700  cp_token *token = cp_lexer_consume_token (parser->lexer);
4701  tree literal = token->u.value;
4702  tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4703  tree value = USERDEF_LITERAL_VALUE (literal);
4704  int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4705  tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4706  tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4707  tree decl, result;
4708
4709  /* Look for a literal operator taking the exact type of numeric argument
4710     as the literal value.  */
4711  releasing_vec args;
4712  vec_safe_push (args, value);
4713  decl = lookup_literal_operator (name, args);
4714  if (decl && decl != error_mark_node)
4715    {
4716      result = finish_call_expr (decl, &args, false, true,
4717				 tf_warning_or_error);
4718
4719      if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4720	{
4721	  warning_at (token->location, OPT_Woverflow,
4722		      "integer literal exceeds range of %qT type",
4723		      long_long_unsigned_type_node);
4724	}
4725      else
4726	{
4727	  if (overflow > 0)
4728	    warning_at (token->location, OPT_Woverflow,
4729			"floating literal exceeds range of %qT type",
4730			long_double_type_node);
4731	  else if (overflow < 0)
4732	    warning_at (token->location, OPT_Woverflow,
4733			"floating literal truncated to zero");
4734	}
4735
4736      return result;
4737    }
4738
4739  /* If the numeric argument didn't work, look for a raw literal
4740     operator taking a const char* argument consisting of the number
4741     in string format.  */
4742  args->truncate (0);
4743  vec_safe_push (args, num_string);
4744  decl = lookup_literal_operator (name, args);
4745  if (decl && decl != error_mark_node)
4746    {
4747      result = finish_call_expr (decl, &args, false, true,
4748				 tf_warning_or_error);
4749      return result;
4750    }
4751
4752  /* If the raw literal didn't work, look for a non-type template
4753     function with parameter pack char....  Call the function with
4754     template parameter characters representing the number.  */
4755  args->truncate (0);
4756  decl = lookup_literal_operator (name, args);
4757  if (decl && decl != error_mark_node)
4758    {
4759      tree tmpl_args = make_char_string_pack (num_string);
4760      if (tmpl_args == NULL_TREE)
4761	{
4762	  error ("failed to translate literal to execution character set %qT",
4763		 num_string);
4764	  return error_mark_node;
4765	}
4766      decl = lookup_template_function (decl, tmpl_args);
4767      result = finish_call_expr (decl, &args, false, true,
4768				 tf_warning_or_error);
4769      return result;
4770    }
4771
4772  /* In C++14 the standard library defines complex number suffixes that
4773     conflict with GNU extensions.  Prefer them if <complex> is #included.  */
4774  bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4775  bool i14 = (cxx_dialect > cxx11
4776	      && (id_equal (suffix_id, "i")
4777		  || id_equal (suffix_id, "if")
4778		  || id_equal (suffix_id, "il")));
4779  diagnostic_t kind = DK_ERROR;
4780  int opt = 0;
4781
4782  if (i14 && ext)
4783    {
4784      tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4785					  LOOK_want::NORMAL, false);
4786      if (cxlit == error_mark_node)
4787	{
4788	  /* No <complex>, so pedwarn and use GNU semantics.  */
4789	  kind = DK_PEDWARN;
4790	  opt = OPT_Wpedantic;
4791	}
4792    }
4793
4794  bool complained
4795    = emit_diagnostic (kind, input_location, opt,
4796		       "unable to find numeric literal operator %qD", name);
4797
4798  if (!complained)
4799    /* Don't inform either.  */;
4800  else if (i14)
4801    {
4802      inform (token->location, "add %<using namespace std::complex_literals%> "
4803	      "(from %<<complex>%>) to enable the C++14 user-defined literal "
4804	      "suffixes");
4805      if (ext)
4806	inform (token->location, "or use %<j%> instead of %<i%> for the "
4807		"GNU built-in suffix");
4808    }
4809  else if (!ext)
4810    inform (token->location, "use %<-fext-numeric-literals%> "
4811	    "to enable more built-in suffixes");
4812
4813  if (kind == DK_ERROR)
4814    value = error_mark_node;
4815  else
4816    {
4817      /* Use the built-in semantics.  */
4818      tree type;
4819      if (id_equal (suffix_id, "i"))
4820	{
4821	  if (TREE_CODE (value) == INTEGER_CST)
4822	    type = integer_type_node;
4823	  else
4824	    type = double_type_node;
4825	}
4826      else if (id_equal (suffix_id, "if"))
4827	type = float_type_node;
4828      else /* if (id_equal (suffix_id, "il")) */
4829	type = long_double_type_node;
4830
4831      value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
4832			   build_zero_cst (type), fold_convert (type, value));
4833    }
4834
4835  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4836    /* Avoid repeated diagnostics.  */
4837    token->u.value = value;
4838  return value;
4839}
4840
4841/* Parse a user-defined string constant.  Returns a call to a user-defined
4842   literal operator taking a character pointer and the length of the string
4843   as arguments.  */
4844
4845static tree
4846cp_parser_userdef_string_literal (tree literal)
4847{
4848  tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4849  tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4850  tree value = USERDEF_LITERAL_VALUE (literal);
4851  int len = TREE_STRING_LENGTH (value)
4852	/ TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4853  tree decl;
4854
4855  /* Build up a call to the user-defined operator.  */
4856  /* Lookup the name we got back from the id-expression.  */
4857  releasing_vec args;
4858  vec_safe_push (args, value);
4859  vec_safe_push (args, build_int_cst (size_type_node, len));
4860  decl = lookup_literal_operator (name, args);
4861
4862  if (decl && decl != error_mark_node)
4863    return finish_call_expr (decl, &args, false, true,
4864			     tf_warning_or_error);
4865
4866  /* Look for a suitable template function, either (C++20) with a single
4867     parameter of class type, or (N3599) with typename parameter CharT and
4868     parameter pack CharT...  */
4869  args->truncate (0);
4870  decl = lookup_literal_operator (name, args);
4871  if (decl && decl != error_mark_node)
4872    {
4873      /* Use resolve_nondeduced_context to try to choose one form of template
4874	 or the other.  */
4875      tree tmpl_args = make_tree_vec (1);
4876      TREE_VEC_ELT (tmpl_args, 0) = value;
4877      decl = lookup_template_function (decl, tmpl_args);
4878      tree res = resolve_nondeduced_context (decl, tf_none);
4879      if (DECL_P (res))
4880	decl = res;
4881      else
4882	{
4883	  TREE_OPERAND (decl, 1) = make_string_pack (value);
4884	  res = resolve_nondeduced_context (decl, tf_none);
4885	  if (DECL_P (res))
4886	    decl = res;
4887	}
4888      if (!DECL_P (decl) && cxx_dialect > cxx17)
4889	TREE_OPERAND (decl, 1) = tmpl_args;
4890      return finish_call_expr (decl, &args, false, true,
4891			       tf_warning_or_error);
4892    }
4893
4894  error ("unable to find string literal operator %qD with %qT, %qT arguments",
4895	 name, TREE_TYPE (value), size_type_node);
4896  return error_mark_node;
4897}
4898
4899
4900/* Basic concepts [gram.basic]  */
4901
4902/* Parse a translation-unit.
4903
4904   translation-unit:
4905     declaration-seq [opt]  */
4906
4907static void
4908cp_parser_translation_unit (cp_parser* parser)
4909{
4910  gcc_checking_assert (!cp_error_declarator);
4911
4912  /* Create the declarator obstack.  */
4913  gcc_obstack_init (&declarator_obstack);
4914  /* Create the error declarator.  */
4915  cp_error_declarator = make_declarator (cdk_error);
4916  /* Create the empty parameter list.  */
4917  no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4918					     UNKNOWN_LOCATION);
4919  /* Remember where the base of the declarator obstack lies.  */
4920  void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4921
4922  push_deferring_access_checks (flag_access_control
4923				? dk_no_deferred : dk_no_check);
4924
4925  module_parse mp_state = MP_NOT_MODULE;
4926  if (modules_p () && !header_module_p ())
4927    mp_state = MP_FIRST;
4928
4929  bool implicit_extern_c = false;
4930
4931  /* Parse until EOF.  */
4932  for (;;)
4933    {
4934      cp_token *token = cp_lexer_peek_token (parser->lexer);
4935
4936      /* If we're entering or exiting a region that's implicitly
4937	 extern "C", modify the lang context appropriately.  This is
4938	 so horrible.  Please die.   */
4939      if (implicit_extern_c
4940	  != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4941	{
4942	  implicit_extern_c = !implicit_extern_c;
4943	  if (implicit_extern_c)
4944	    push_lang_context (lang_name_c);
4945	  else
4946	    pop_lang_context ();
4947	}
4948
4949      if (token->type == CPP_EOF)
4950	break;
4951
4952      if (modules_p ())
4953	{
4954	  /* Top-level module declarations are ok, and change the
4955	     portion of file we're in.  Top-level import declarations
4956	     are significant for the import portions.  */
4957
4958	  cp_token *next = token;
4959	  bool exporting = token->keyword == RID__EXPORT;
4960	  if (exporting)
4961	    {
4962	      cp_lexer_consume_token (parser->lexer);
4963	      next = cp_lexer_peek_token (parser->lexer);
4964	    }
4965	  if (next->keyword == RID__MODULE)
4966	    {
4967	      mp_state
4968		= cp_parser_module_declaration (parser, mp_state, exporting);
4969	      continue;
4970	    }
4971	  else if (next->keyword == RID__IMPORT)
4972	    {
4973	      if (mp_state == MP_FIRST)
4974		mp_state = MP_NOT_MODULE;
4975	      cp_parser_import_declaration (parser, mp_state, exporting);
4976	      continue;
4977	    }
4978	  else
4979	    gcc_checking_assert (!exporting);
4980
4981	  if (mp_state == MP_GLOBAL && token->main_source_p)
4982	    {
4983	      static bool warned = false;
4984	      if (!warned)
4985		{
4986		  warned = true;
4987		  error_at (token->location,
4988			    "global module fragment contents must be"
4989			    " from preprocessor inclusion");
4990		}
4991	    }
4992	}
4993
4994      /* This relies on the ordering of module_parse values.  */
4995      if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
4996	/* We're no longer in the import portion of a named module.  */
4997	mp_state = module_parse (mp_state + 1);
4998      else if (mp_state == MP_FIRST)
4999	mp_state = MP_NOT_MODULE;
5000
5001      if (token->type == CPP_CLOSE_BRACE)
5002	{
5003	  cp_parser_error (parser, "expected declaration");
5004	  cp_lexer_consume_token (parser->lexer);
5005	  /* If the next token is now a `;', consume it.  */
5006	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5007	    cp_lexer_consume_token (parser->lexer);
5008	}
5009      else
5010	cp_parser_toplevel_declaration (parser);
5011    }
5012
5013  /* Get rid of the token array; we don't need it any more.  */
5014  cp_lexer_destroy (parser->lexer);
5015  parser->lexer = NULL;
5016
5017  /* The EOF should have reset this. */
5018  gcc_checking_assert (!implicit_extern_c);
5019
5020  /* Make sure the declarator obstack was fully cleaned up.  */
5021  gcc_assert (obstack_next_free (&declarator_obstack)
5022	      == declarator_obstack_base);
5023}
5024
5025/* Return the appropriate tsubst flags for parsing, possibly in N3276
5026   decltype context.  */
5027
5028static inline tsubst_flags_t
5029complain_flags (bool decltype_p)
5030{
5031  tsubst_flags_t complain = tf_warning_or_error;
5032  if (decltype_p)
5033    complain |= tf_decltype;
5034  return complain;
5035}
5036
5037/* We're about to parse a collection of statements.  If we're currently
5038   parsing tentatively, set up a firewall so that any nested
5039   cp_parser_commit_to_tentative_parse won't affect the current context.  */
5040
5041static cp_token_position
5042cp_parser_start_tentative_firewall (cp_parser *parser)
5043{
5044  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5045    return 0;
5046
5047  cp_parser_parse_tentatively (parser);
5048  cp_parser_commit_to_topmost_tentative_parse (parser);
5049  return cp_lexer_token_position (parser->lexer, false);
5050}
5051
5052/* We've finished parsing the collection of statements.  Wrap up the
5053   firewall and replace the relevant tokens with the parsed form.  */
5054
5055static void
5056cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5057				  tree expr)
5058{
5059  if (!start)
5060    return;
5061
5062  /* Finish the firewall level.  */
5063  cp_parser_parse_definitely (parser);
5064  /* And remember the result of the parse for when we try again.  */
5065  cp_token *token = cp_lexer_token_at (parser->lexer, start);
5066  token->type = CPP_PREPARSED_EXPR;
5067  token->u.value = expr;
5068  token->keyword = RID_MAX;
5069  cp_lexer_purge_tokens_after (parser->lexer, start);
5070}
5071
5072/* Like the above functions, but let the user modify the tokens.  Used by
5073   CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5074   later parses, so it makes sense to localize the effects of
5075   cp_parser_commit_to_tentative_parse.  */
5076
5077struct tentative_firewall
5078{
5079  cp_parser *parser;
5080  bool set;
5081
5082  tentative_firewall (cp_parser *p): parser(p)
5083  {
5084    /* If we're currently parsing tentatively, start a committed level as a
5085       firewall and then an inner tentative parse.  */
5086    if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5087      {
5088	cp_parser_parse_tentatively (parser);
5089	cp_parser_commit_to_topmost_tentative_parse (parser);
5090	cp_parser_parse_tentatively (parser);
5091      }
5092  }
5093
5094  ~tentative_firewall()
5095  {
5096    if (set)
5097      {
5098	/* Finish the inner tentative parse and the firewall, propagating any
5099	   uncommitted error state to the outer tentative parse.  */
5100	bool err = cp_parser_error_occurred (parser);
5101	cp_parser_parse_definitely (parser);
5102	cp_parser_parse_definitely (parser);
5103	if (err)
5104	  cp_parser_simulate_error (parser);
5105      }
5106  }
5107};
5108
5109/* Some tokens naturally come in pairs e.g.'(' and ')'.
5110   This class is for tracking such a matching pair of symbols.
5111   In particular, it tracks the location of the first token,
5112   so that if the second token is missing, we can highlight the
5113   location of the first token when notifying the user about the
5114   problem.  */
5115
5116template <typename traits_t>
5117class token_pair
5118{
5119 public:
5120  /* token_pair's ctor.  */
5121  token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5122
5123  /* If the next token is the opening symbol for this pair, consume it and
5124     return true.
5125     Otherwise, issue an error and return false.
5126     In either case, record the location of the opening token.  */
5127
5128  bool require_open (cp_parser *parser)
5129  {
5130    m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5131    return cp_parser_require (parser, traits_t::open_token_type,
5132			      traits_t::required_token_open);
5133  }
5134
5135  /* Consume the next token from PARSER, recording its location as
5136     that of the opening token within the pair.  */
5137
5138  cp_token * consume_open (cp_parser *parser)
5139  {
5140    cp_token *tok = cp_lexer_consume_token (parser->lexer);
5141    gcc_assert (tok->type == traits_t::open_token_type);
5142    m_open_loc = tok->location;
5143    return tok;
5144  }
5145
5146  /* If the next token is the closing symbol for this pair, consume it
5147     and return it.
5148     Otherwise, issue an error, highlighting the location of the
5149     corresponding opening token, and return NULL.  */
5150
5151  cp_token *require_close (cp_parser *parser) const
5152  {
5153    return cp_parser_require (parser, traits_t::close_token_type,
5154			      traits_t::required_token_close,
5155			      m_open_loc);
5156  }
5157
5158  location_t open_location () const { return m_open_loc; }
5159
5160 private:
5161  location_t m_open_loc;
5162};
5163
5164/* Traits for token_pair<T> for tracking matching pairs of parentheses.  */
5165
5166struct matching_paren_traits
5167{
5168  static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5169  static const enum required_token required_token_open  = RT_OPEN_PAREN;
5170  static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5171  static const enum required_token required_token_close = RT_CLOSE_PAREN;
5172};
5173
5174/* "matching_parens" is a token_pair<T> class for tracking matching
5175   pairs of parentheses.  */
5176
5177typedef token_pair<matching_paren_traits> matching_parens;
5178
5179/* Traits for token_pair<T> for tracking matching pairs of braces.  */
5180
5181struct matching_brace_traits
5182{
5183  static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5184  static const enum required_token required_token_open = RT_OPEN_BRACE;
5185  static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5186  static const enum required_token required_token_close = RT_CLOSE_BRACE;
5187};
5188
5189/* "matching_braces" is a token_pair<T> class for tracking matching
5190   pairs of braces.  */
5191
5192typedef token_pair<matching_brace_traits> matching_braces;
5193
5194
5195/* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5196   enclosing parentheses.  */
5197
5198static cp_expr
5199cp_parser_statement_expr (cp_parser *parser)
5200{
5201  cp_token_position start = cp_parser_start_tentative_firewall (parser);
5202
5203  /* Consume the '('.  */
5204  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5205  matching_parens parens;
5206  parens.consume_open (parser);
5207  /* Start the statement-expression.  */
5208  tree expr = begin_stmt_expr ();
5209  /* Parse the compound-statement.  */
5210  cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
5211  /* Finish up.  */
5212  expr = finish_stmt_expr (expr, false);
5213  /* Consume the ')'.  */
5214  location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5215  if (!parens.require_close (parser))
5216    cp_parser_skip_to_end_of_statement (parser);
5217
5218  cp_parser_end_tentative_firewall (parser, start, expr);
5219  location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5220  return cp_expr (expr, combined_loc);
5221}
5222
5223/* Expressions [gram.expr] */
5224
5225/* Parse a fold-operator.
5226
5227    fold-operator:
5228        -  *  /  %  ^  &  |  =  <  >  <<  >>
5229      =  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=
5230      ==  !=  <=  >=  &&  ||  ,  .*  ->*
5231
5232   This returns the tree code corresponding to the matched operator
5233   as an int. When the current token matches a compound assignment
5234   operator, the resulting tree code is the negative value of the
5235   non-assignment operator. */
5236
5237static int
5238cp_parser_fold_operator (cp_token *token)
5239{
5240  switch (token->type)
5241    {
5242    case CPP_PLUS: return PLUS_EXPR;
5243    case CPP_MINUS: return MINUS_EXPR;
5244    case CPP_MULT: return MULT_EXPR;
5245    case CPP_DIV: return TRUNC_DIV_EXPR;
5246    case CPP_MOD: return TRUNC_MOD_EXPR;
5247    case CPP_XOR: return BIT_XOR_EXPR;
5248    case CPP_AND: return BIT_AND_EXPR;
5249    case CPP_OR: return BIT_IOR_EXPR;
5250    case CPP_LSHIFT: return LSHIFT_EXPR;
5251    case CPP_RSHIFT: return RSHIFT_EXPR;
5252
5253    case CPP_EQ: return -NOP_EXPR;
5254    case CPP_PLUS_EQ: return -PLUS_EXPR;
5255    case CPP_MINUS_EQ: return -MINUS_EXPR;
5256    case CPP_MULT_EQ: return -MULT_EXPR;
5257    case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5258    case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5259    case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5260    case CPP_AND_EQ: return -BIT_AND_EXPR;
5261    case CPP_OR_EQ: return -BIT_IOR_EXPR;
5262    case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5263    case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5264
5265    case CPP_EQ_EQ: return EQ_EXPR;
5266    case CPP_NOT_EQ: return NE_EXPR;
5267    case CPP_LESS: return LT_EXPR;
5268    case CPP_GREATER: return GT_EXPR;
5269    case CPP_LESS_EQ: return LE_EXPR;
5270    case CPP_GREATER_EQ: return GE_EXPR;
5271
5272    case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5273    case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5274
5275    case CPP_COMMA: return COMPOUND_EXPR;
5276
5277    case CPP_DOT_STAR: return DOTSTAR_EXPR;
5278    case CPP_DEREF_STAR: return MEMBER_REF;
5279
5280    default: return ERROR_MARK;
5281    }
5282}
5283
5284/* Returns true if CODE indicates a binary expression, which is not allowed in
5285   the LHS of a fold-expression.  More codes will need to be added to use this
5286   function in other contexts.  */
5287
5288static bool
5289is_binary_op (tree_code code)
5290{
5291  switch (code)
5292    {
5293    case PLUS_EXPR:
5294    case POINTER_PLUS_EXPR:
5295    case MINUS_EXPR:
5296    case MULT_EXPR:
5297    case TRUNC_DIV_EXPR:
5298    case TRUNC_MOD_EXPR:
5299    case BIT_XOR_EXPR:
5300    case BIT_AND_EXPR:
5301    case BIT_IOR_EXPR:
5302    case LSHIFT_EXPR:
5303    case RSHIFT_EXPR:
5304
5305    case MODOP_EXPR:
5306
5307    case EQ_EXPR:
5308    case NE_EXPR:
5309    case LE_EXPR:
5310    case GE_EXPR:
5311    case LT_EXPR:
5312    case GT_EXPR:
5313
5314    case TRUTH_ANDIF_EXPR:
5315    case TRUTH_ORIF_EXPR:
5316
5317    case COMPOUND_EXPR:
5318
5319    case DOTSTAR_EXPR:
5320    case MEMBER_REF:
5321      return true;
5322
5323    default:
5324      return false;
5325    }
5326}
5327
5328/* If the next token is a suitable fold operator, consume it and return as
5329   the function above.  */
5330
5331static int
5332cp_parser_fold_operator (cp_parser *parser)
5333{
5334  cp_token* token = cp_lexer_peek_token (parser->lexer);
5335  int code = cp_parser_fold_operator (token);
5336  if (code != ERROR_MARK)
5337    cp_lexer_consume_token (parser->lexer);
5338  return code;
5339}
5340
5341/* Parse a fold-expression.
5342
5343     fold-expression:
5344       ( ... folding-operator cast-expression)
5345       ( cast-expression folding-operator ... )
5346       ( cast-expression folding operator ... folding-operator cast-expression)
5347
5348   Note that the '(' and ')' are matched in primary expression. */
5349
5350static cp_expr
5351cp_parser_fold_expression (cp_parser *parser, tree expr1)
5352{
5353  cp_id_kind pidk;
5354
5355  // Left fold.
5356  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5357    {
5358      if (expr1)
5359	return error_mark_node;
5360      cp_lexer_consume_token (parser->lexer);
5361      int op = cp_parser_fold_operator (parser);
5362      if (op == ERROR_MARK)
5363        {
5364          cp_parser_error (parser, "expected binary operator");
5365          return error_mark_node;
5366        }
5367
5368      tree expr = cp_parser_cast_expression (parser, false, false,
5369					     false, &pidk);
5370      if (expr == error_mark_node)
5371        return error_mark_node;
5372      return finish_left_unary_fold_expr (expr, op);
5373    }
5374
5375  const cp_token* token = cp_lexer_peek_token (parser->lexer);
5376  int op = cp_parser_fold_operator (parser);
5377  if (op == ERROR_MARK)
5378    {
5379      cp_parser_error (parser, "expected binary operator");
5380      return error_mark_node;
5381    }
5382
5383  if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5384    {
5385      cp_parser_error (parser, "expected ...");
5386      return error_mark_node;
5387    }
5388  cp_lexer_consume_token (parser->lexer);
5389
5390  /* The operands of a fold-expression are cast-expressions, so binary or
5391     conditional expressions are not allowed.  We check this here to avoid
5392     tentative parsing.  */
5393  if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5394    /* OK, the expression was parenthesized.  */;
5395  else if (is_binary_op (TREE_CODE (expr1)))
5396    error_at (location_of (expr1),
5397	      "binary expression in operand of fold-expression");
5398  else if (TREE_CODE (expr1) == COND_EXPR
5399	   || (REFERENCE_REF_P (expr1)
5400	       && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5401    error_at (location_of (expr1),
5402	      "conditional expression in operand of fold-expression");
5403
5404  // Right fold.
5405  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5406    return finish_right_unary_fold_expr (expr1, op);
5407
5408  if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5409    {
5410      cp_parser_error (parser, "mismatched operator in fold-expression");
5411      return error_mark_node;
5412    }
5413  cp_lexer_consume_token (parser->lexer);
5414
5415  // Binary left or right fold.
5416  tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5417  if (expr2 == error_mark_node)
5418    return error_mark_node;
5419  return finish_binary_fold_expr (expr1, expr2, op);
5420}
5421
5422/* Parse a primary-expression.
5423
5424   primary-expression:
5425     literal
5426     this
5427     ( expression )
5428     id-expression
5429     lambda-expression (C++11)
5430
5431   GNU Extensions:
5432
5433   primary-expression:
5434     ( compound-statement )
5435     __builtin_va_arg ( assignment-expression , type-id )
5436     __builtin_offsetof ( type-id , offsetof-expression )
5437
5438   C++ Extensions:
5439     __has_nothrow_assign ( type-id )
5440     __has_nothrow_constructor ( type-id )
5441     __has_nothrow_copy ( type-id )
5442     __has_trivial_assign ( type-id )
5443     __has_trivial_constructor ( type-id )
5444     __has_trivial_copy ( type-id )
5445     __has_trivial_destructor ( type-id )
5446     __has_virtual_destructor ( type-id )
5447     __is_abstract ( type-id )
5448     __is_base_of ( type-id , type-id )
5449     __is_class ( type-id )
5450     __is_empty ( type-id )
5451     __is_enum ( type-id )
5452     __is_final ( type-id )
5453     __is_literal_type ( type-id )
5454     __is_pod ( type-id )
5455     __is_polymorphic ( type-id )
5456     __is_std_layout ( type-id )
5457     __is_trivial ( type-id )
5458     __is_union ( type-id )
5459
5460   Objective-C++ Extension:
5461
5462   primary-expression:
5463     objc-expression
5464
5465   literal:
5466     __null
5467
5468   ADDRESS_P is true iff this expression was immediately preceded by
5469   "&" and therefore might denote a pointer-to-member.  CAST_P is true
5470   iff this expression is the target of a cast.  TEMPLATE_ARG_P is
5471   true iff this expression is a template argument.
5472
5473   Returns a representation of the expression.  Upon return, *IDK
5474   indicates what kind of id-expression (if any) was present.  */
5475
5476static cp_expr
5477cp_parser_primary_expression (cp_parser *parser,
5478			      bool address_p,
5479			      bool cast_p,
5480			      bool template_arg_p,
5481			      bool decltype_p,
5482			      cp_id_kind *idk)
5483{
5484  cp_token *token = NULL;
5485
5486  /* Assume the primary expression is not an id-expression.  */
5487  *idk = CP_ID_KIND_NONE;
5488
5489  /* Peek at the next token.  */
5490  token = cp_lexer_peek_token (parser->lexer);
5491  switch ((int) token->type)
5492    {
5493      /* literal:
5494	   integer-literal
5495	   character-literal
5496	   floating-literal
5497	   string-literal
5498	   boolean-literal
5499	   pointer-literal
5500	   user-defined-literal  */
5501    case CPP_CHAR:
5502    case CPP_CHAR16:
5503    case CPP_CHAR32:
5504    case CPP_WCHAR:
5505    case CPP_UTF8CHAR:
5506    case CPP_NUMBER:
5507    case CPP_PREPARSED_EXPR:
5508      if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5509	return cp_parser_userdef_numeric_literal (parser);
5510      token = cp_lexer_consume_token (parser->lexer);
5511      if (TREE_CODE (token->u.value) == FIXED_CST)
5512	{
5513	  error_at (token->location,
5514		    "fixed-point types not supported in C++");
5515	  return error_mark_node;
5516	}
5517      /* Floating-point literals are only allowed in an integral
5518	 constant expression if they are cast to an integral or
5519	 enumeration type.  */
5520      if (TREE_CODE (token->u.value) == REAL_CST
5521	  && parser->integral_constant_expression_p
5522	  && pedantic)
5523	{
5524	  /* CAST_P will be set even in invalid code like "int(2.7 +
5525	     ...)".   Therefore, we have to check that the next token
5526	     is sure to end the cast.  */
5527	  if (cast_p)
5528	    {
5529	      cp_token *next_token;
5530
5531	      next_token = cp_lexer_peek_token (parser->lexer);
5532	      if (/* The comma at the end of an
5533		     enumerator-definition.  */
5534		  next_token->type != CPP_COMMA
5535		  /* The curly brace at the end of an enum-specifier.  */
5536		  && next_token->type != CPP_CLOSE_BRACE
5537		  /* The end of a statement.  */
5538		  && next_token->type != CPP_SEMICOLON
5539		  /* The end of the cast-expression.  */
5540		  && next_token->type != CPP_CLOSE_PAREN
5541		  /* The end of an array bound.  */
5542		  && next_token->type != CPP_CLOSE_SQUARE
5543		  /* The closing ">" in a template-argument-list.  */
5544		  && (next_token->type != CPP_GREATER
5545		      || parser->greater_than_is_operator_p)
5546		  /* C++0x only: A ">>" treated like two ">" tokens,
5547                     in a template-argument-list.  */
5548		  && (next_token->type != CPP_RSHIFT
5549                      || (cxx_dialect == cxx98)
5550		      || parser->greater_than_is_operator_p))
5551		cast_p = false;
5552	    }
5553
5554	  /* If we are within a cast, then the constraint that the
5555	     cast is to an integral or enumeration type will be
5556	     checked at that point.  If we are not within a cast, then
5557	     this code is invalid.  */
5558	  if (!cast_p)
5559	    cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5560	}
5561      return (cp_expr (token->u.value, token->location)
5562	      .maybe_add_location_wrapper ());
5563
5564    case CPP_CHAR_USERDEF:
5565    case CPP_CHAR16_USERDEF:
5566    case CPP_CHAR32_USERDEF:
5567    case CPP_WCHAR_USERDEF:
5568    case CPP_UTF8CHAR_USERDEF:
5569      return cp_parser_userdef_char_literal (parser);
5570
5571    case CPP_STRING:
5572    case CPP_STRING16:
5573    case CPP_STRING32:
5574    case CPP_WSTRING:
5575    case CPP_UTF8STRING:
5576    case CPP_STRING_USERDEF:
5577    case CPP_STRING16_USERDEF:
5578    case CPP_STRING32_USERDEF:
5579    case CPP_WSTRING_USERDEF:
5580    case CPP_UTF8STRING_USERDEF:
5581      /* ??? Should wide strings be allowed when parser->translate_strings_p
5582	 is false (i.e. in attributes)?  If not, we can kill the third
5583	 argument to cp_parser_string_literal.  */
5584      return (cp_parser_string_literal (parser,
5585					parser->translate_strings_p,
5586					true)
5587	      .maybe_add_location_wrapper ());
5588
5589    case CPP_OPEN_PAREN:
5590      /* If we see `( { ' then we are looking at the beginning of
5591	 a GNU statement-expression.  */
5592      if (cp_parser_allow_gnu_extensions_p (parser)
5593	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5594	{
5595	  /* Statement-expressions are not allowed by the standard.  */
5596	  pedwarn (token->location, OPT_Wpedantic,
5597		   "ISO C++ forbids braced-groups within expressions");
5598
5599	  /* And they're not allowed outside of a function-body; you
5600	     cannot, for example, write:
5601
5602	     int i = ({ int j = 3; j + 1; });
5603
5604	     at class or namespace scope.  */
5605	  if (!parser->in_function_body
5606	      || parser->in_template_argument_list_p)
5607	    {
5608	      error_at (token->location,
5609			"statement-expressions are not allowed outside "
5610			"functions nor in template-argument lists");
5611	      cp_parser_skip_to_end_of_block_or_statement (parser);
5612	      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5613		cp_lexer_consume_token (parser->lexer);
5614	      return error_mark_node;
5615	    }
5616	  else
5617	    return cp_parser_statement_expr (parser);
5618	}
5619      /* Otherwise it's a normal parenthesized expression.  */
5620      {
5621	cp_expr expr;
5622	bool saved_greater_than_is_operator_p;
5623
5624	location_t open_paren_loc = token->location;
5625
5626	/* Consume the `('.  */
5627	matching_parens parens;
5628	parens.consume_open (parser);
5629	/* Within a parenthesized expression, a `>' token is always
5630	   the greater-than operator.  */
5631	saved_greater_than_is_operator_p
5632	  = parser->greater_than_is_operator_p;
5633	parser->greater_than_is_operator_p = true;
5634
5635	if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5636	  /* Left fold expression. */
5637	  expr = NULL_TREE;
5638	else
5639	  /* Parse the parenthesized expression.  */
5640	  expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5641
5642	token = cp_lexer_peek_token (parser->lexer);
5643	if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5644	  {
5645	    expr = cp_parser_fold_expression (parser, expr);
5646	    if (expr != error_mark_node
5647		&& cxx_dialect < cxx17)
5648	      pedwarn (input_location, OPT_Wc__17_extensions,
5649		       "fold-expressions only available with %<-std=c++17%> "
5650		       "or %<-std=gnu++17%>");
5651	  }
5652	else
5653	  /* Let the front end know that this expression was
5654	     enclosed in parentheses. This matters in case, for
5655	     example, the expression is of the form `A::B', since
5656	     `&A::B' might be a pointer-to-member, but `&(A::B)' is
5657	     not.  */
5658	  expr = finish_parenthesized_expr (expr);
5659
5660	/* DR 705: Wrapping an unqualified name in parentheses
5661	   suppresses arg-dependent lookup.  We want to pass back
5662	   CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5663	   (c++/37862), but none of the others.  */
5664	if (*idk != CP_ID_KIND_QUALIFIED)
5665	  *idk = CP_ID_KIND_NONE;
5666
5667	/* The `>' token might be the end of a template-id or
5668	   template-parameter-list now.  */
5669	parser->greater_than_is_operator_p
5670	  = saved_greater_than_is_operator_p;
5671
5672	/* Consume the `)'.  */
5673	token = cp_lexer_peek_token (parser->lexer);
5674	location_t close_paren_loc = token->location;
5675	bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5676	expr.set_range (open_paren_loc, close_paren_loc);
5677	if (no_wparens)
5678	  suppress_warning (expr, OPT_Wparentheses);
5679	if (!parens.require_close (parser)
5680	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5681	  cp_parser_skip_to_end_of_statement (parser);
5682
5683	return expr;
5684      }
5685
5686    case CPP_OPEN_SQUARE:
5687      {
5688	if (c_dialect_objc ())
5689	  {
5690	    /* We might have an Objective-C++ message. */
5691	    cp_parser_parse_tentatively (parser);
5692	    tree msg = cp_parser_objc_message_expression (parser);
5693	    /* If that works out, we're done ... */
5694	    if (cp_parser_parse_definitely (parser))
5695	      return msg;
5696	    /* ... else, fall though to see if it's a lambda.  */
5697	  }
5698	cp_expr lam = cp_parser_lambda_expression (parser);
5699	/* Don't warn about a failed tentative parse.  */
5700	if (cp_parser_error_occurred (parser))
5701	  return error_mark_node;
5702	maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5703	return lam;
5704      }
5705
5706    case CPP_OBJC_STRING:
5707      if (c_dialect_objc ())
5708	/* We have an Objective-C++ string literal. */
5709        return cp_parser_objc_expression (parser);
5710      cp_parser_error (parser, "expected primary-expression");
5711      return error_mark_node;
5712
5713    case CPP_KEYWORD:
5714      switch (token->keyword)
5715	{
5716	  /* These two are the boolean literals.  */
5717	case RID_TRUE:
5718	  cp_lexer_consume_token (parser->lexer);
5719	  return cp_expr (boolean_true_node, token->location);
5720	case RID_FALSE:
5721	  cp_lexer_consume_token (parser->lexer);
5722	  return cp_expr (boolean_false_node, token->location);
5723
5724	  /* The `__null' literal.  */
5725	case RID_NULL:
5726	  cp_lexer_consume_token (parser->lexer);
5727	  return cp_expr (null_node, token->location);
5728
5729	  /* The `nullptr' literal.  */
5730	case RID_NULLPTR:
5731	  cp_lexer_consume_token (parser->lexer);
5732	  return cp_expr (nullptr_node, token->location);
5733
5734	  /* Recognize the `this' keyword.  */
5735	case RID_THIS:
5736	  cp_lexer_consume_token (parser->lexer);
5737	  if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5738	    {
5739	      error_at (token->location,
5740			"%<this%> may not be used in this context");
5741	      return error_mark_node;
5742	    }
5743	  /* Pointers cannot appear in constant-expressions.  */
5744	  if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5745	    return error_mark_node;
5746	  return cp_expr (finish_this_expr (), token->location);
5747
5748	  /* The `operator' keyword can be the beginning of an
5749	     id-expression.  */
5750	case RID_OPERATOR:
5751	  goto id_expression;
5752
5753	case RID_FUNCTION_NAME:
5754	case RID_PRETTY_FUNCTION_NAME:
5755	case RID_C99_FUNCTION_NAME:
5756	  {
5757	    non_integral_constant name;
5758
5759	    /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5760	       __func__ are the names of variables -- but they are
5761	       treated specially.  Therefore, they are handled here,
5762	       rather than relying on the generic id-expression logic
5763	       below.  Grammatically, these names are id-expressions.
5764
5765	       Consume the token.  */
5766	    token = cp_lexer_consume_token (parser->lexer);
5767
5768	    switch (token->keyword)
5769	      {
5770	      case RID_FUNCTION_NAME:
5771		name = NIC_FUNC_NAME;
5772		break;
5773	      case RID_PRETTY_FUNCTION_NAME:
5774		name = NIC_PRETTY_FUNC;
5775		break;
5776	      case RID_C99_FUNCTION_NAME:
5777		name = NIC_C99_FUNC;
5778		break;
5779	      default:
5780		gcc_unreachable ();
5781	      }
5782
5783	    if (cp_parser_non_integral_constant_expression (parser, name))
5784	      return error_mark_node;
5785
5786	    /* Look up the name.  */
5787	    return finish_fname (token->u.value);
5788	  }
5789
5790	case RID_VA_ARG:
5791	  {
5792	    tree expression;
5793	    tree type;
5794	    location_t type_location;
5795	    location_t start_loc
5796	      = cp_lexer_peek_token (parser->lexer)->location;
5797	    /* The `__builtin_va_arg' construct is used to handle
5798	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
5799	    cp_lexer_consume_token (parser->lexer);
5800	    /* Look for the opening `('.  */
5801	    matching_parens parens;
5802	    parens.require_open (parser);
5803	    /* Now, parse the assignment-expression.  */
5804	    expression = cp_parser_assignment_expression (parser);
5805	    /* Look for the `,'.  */
5806	    cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5807	    type_location = cp_lexer_peek_token (parser->lexer)->location;
5808	    /* Parse the type-id.  */
5809	    {
5810	      type_id_in_expr_sentinel s (parser);
5811	      type = cp_parser_type_id (parser);
5812	    }
5813	    /* Look for the closing `)'.  */
5814	    location_t finish_loc
5815	      = cp_lexer_peek_token (parser->lexer)->location;
5816	    parens.require_close (parser);
5817	    /* Using `va_arg' in a constant-expression is not
5818	       allowed.  */
5819	    if (cp_parser_non_integral_constant_expression (parser,
5820							    NIC_VA_ARG))
5821	      return error_mark_node;
5822	    /* Construct a location of the form:
5823		 __builtin_va_arg (v, int)
5824		 ~~~~~~~~~~~~~~~~~~~~~^~~~
5825	       with the caret at the type, ranging from the start of the
5826	       "__builtin_va_arg" token to the close paren.  */
5827	    location_t combined_loc
5828	      = make_location (type_location, start_loc, finish_loc);
5829	    return build_x_va_arg (combined_loc, expression, type);
5830	  }
5831
5832	case RID_OFFSETOF:
5833	  return cp_parser_builtin_offsetof (parser);
5834
5835	case RID_HAS_NOTHROW_ASSIGN:
5836	case RID_HAS_NOTHROW_CONSTRUCTOR:
5837	case RID_HAS_NOTHROW_COPY:
5838	case RID_HAS_TRIVIAL_ASSIGN:
5839	case RID_HAS_TRIVIAL_CONSTRUCTOR:
5840	case RID_HAS_TRIVIAL_COPY:
5841	case RID_HAS_TRIVIAL_DESTRUCTOR:
5842	case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5843	case RID_HAS_VIRTUAL_DESTRUCTOR:
5844	case RID_IS_ABSTRACT:
5845	case RID_IS_AGGREGATE:
5846	case RID_IS_BASE_OF:
5847	case RID_IS_CLASS:
5848	case RID_IS_EMPTY:
5849	case RID_IS_ENUM:
5850	case RID_IS_FINAL:
5851	case RID_IS_LAYOUT_COMPATIBLE:
5852	case RID_IS_LITERAL_TYPE:
5853	case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
5854	case RID_IS_POD:
5855	case RID_IS_POLYMORPHIC:
5856	case RID_IS_SAME_AS:
5857	case RID_IS_STD_LAYOUT:
5858	case RID_IS_TRIVIAL:
5859	case RID_IS_TRIVIALLY_ASSIGNABLE:
5860	case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5861	case RID_IS_TRIVIALLY_COPYABLE:
5862	case RID_IS_UNION:
5863	case RID_IS_ASSIGNABLE:
5864	case RID_IS_CONSTRUCTIBLE:
5865	case RID_IS_NOTHROW_ASSIGNABLE:
5866	case RID_IS_NOTHROW_CONSTRUCTIBLE:
5867	  return cp_parser_trait_expr (parser, token->keyword);
5868
5869	// C++ concepts
5870	case RID_REQUIRES:
5871	  return cp_parser_requires_expression (parser);
5872
5873	/* Objective-C++ expressions.  */
5874	case RID_AT_ENCODE:
5875	case RID_AT_PROTOCOL:
5876	case RID_AT_SELECTOR:
5877	  return cp_parser_objc_expression (parser);
5878
5879	case RID_TEMPLATE:
5880	  if (parser->in_function_body
5881	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5882	      	  == CPP_LESS))
5883	    {
5884	      error_at (token->location,
5885			"a template declaration cannot appear at block scope");
5886	      cp_parser_skip_to_end_of_block_or_statement (parser);
5887	      return error_mark_node;
5888	    }
5889	  /* FALLTHRU */
5890	default:
5891	  cp_parser_error (parser, "expected primary-expression");
5892	  return error_mark_node;
5893	}
5894
5895      /* An id-expression can start with either an identifier, a
5896	 `::' as the beginning of a qualified-id, or the "operator"
5897	 keyword.  */
5898    case CPP_NAME:
5899    case CPP_SCOPE:
5900    case CPP_TEMPLATE_ID:
5901    case CPP_NESTED_NAME_SPECIFIER:
5902      {
5903      id_expression:
5904	cp_expr id_expression;
5905	cp_expr decl;
5906	const char *error_msg;
5907	bool template_p;
5908	bool done;
5909	cp_token *id_expr_token;
5910
5911	/* Parse the id-expression.  */
5912	id_expression
5913	  = cp_parser_id_expression (parser,
5914				     /*template_keyword_p=*/false,
5915				     /*check_dependency_p=*/true,
5916				     &template_p,
5917				     /*declarator_p=*/false,
5918				     /*optional_p=*/false);
5919	if (id_expression == error_mark_node)
5920	  return error_mark_node;
5921	id_expr_token = token;
5922	token = cp_lexer_peek_token (parser->lexer);
5923	done = (token->type != CPP_OPEN_SQUARE
5924		&& token->type != CPP_OPEN_PAREN
5925		&& token->type != CPP_DOT
5926		&& token->type != CPP_DEREF
5927		&& token->type != CPP_PLUS_PLUS
5928		&& token->type != CPP_MINUS_MINUS);
5929	/* If we have a template-id, then no further lookup is
5930	   required.  If the template-id was for a template-class, we
5931	   will sometimes have a TYPE_DECL at this point.  */
5932	if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5933		 || TREE_CODE (id_expression) == TYPE_DECL)
5934	  decl = id_expression;
5935	/* Look up the name.  */
5936	else
5937	  {
5938	    tree ambiguous_decls;
5939
5940	    /* If we already know that this lookup is ambiguous, then
5941	       we've already issued an error message; there's no reason
5942	       to check again.  */
5943	    if (id_expr_token->type == CPP_NAME
5944		&& id_expr_token->error_reported)
5945	      {
5946		cp_parser_simulate_error (parser);
5947		return error_mark_node;
5948	      }
5949
5950	    decl = cp_parser_lookup_name (parser, id_expression,
5951					  none_type,
5952					  template_p,
5953					  /*is_namespace=*/false,
5954					  /*check_dependency=*/true,
5955					  &ambiguous_decls,
5956					  id_expression.get_location ());
5957	    /* If the lookup was ambiguous, an error will already have
5958	       been issued.  */
5959	    if (ambiguous_decls)
5960	      return error_mark_node;
5961
5962	    /* In Objective-C++, we may have an Objective-C 2.0
5963	       dot-syntax for classes here.  */
5964	    if (c_dialect_objc ()
5965		&& cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5966		&& TREE_CODE (decl) == TYPE_DECL
5967		&& objc_is_class_name (decl))
5968	      {
5969		tree component;
5970		cp_lexer_consume_token (parser->lexer);
5971		component = cp_parser_identifier (parser);
5972		if (component == error_mark_node)
5973		  return error_mark_node;
5974
5975		tree result = objc_build_class_component_ref (id_expression,
5976							      component);
5977		/* Build a location of the form:
5978		     expr.component
5979		     ~~~~~^~~~~~~~~
5980		   with caret at the start of the component name (at
5981		   input_location), ranging from the start of the id_expression
5982		   to the end of the component name.  */
5983		location_t combined_loc
5984		  = make_location (input_location, id_expression.get_start (),
5985				   get_finish (input_location));
5986		protected_set_expr_location (result, combined_loc);
5987		return result;
5988	      }
5989
5990	    /* In Objective-C++, an instance variable (ivar) may be preferred
5991	       to whatever cp_parser_lookup_name() found.
5992	       Call objc_lookup_ivar.  To avoid exposing cp_expr to the
5993	       rest of c-family, we have to do a little extra work to preserve
5994	       any location information in cp_expr "decl".  Given that
5995	       objc_lookup_ivar is implemented in "c-family" and "objc", we
5996	       have a trip through the pure "tree" type, rather than cp_expr.
5997	       Naively copying it back to "decl" would implicitly give the
5998	       new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5999	       store an EXPR_LOCATION.  Hence we only update "decl" (and
6000	       hence its location_t) if we get back a different tree node.  */
6001	    tree decl_tree = objc_lookup_ivar (decl.get_value (),
6002					       id_expression);
6003	    if (decl_tree != decl.get_value ())
6004	      decl = cp_expr (decl_tree);
6005
6006	    /* If name lookup gives us a SCOPE_REF, then the
6007	       qualifying scope was dependent.  */
6008	    if (TREE_CODE (decl) == SCOPE_REF)
6009	      {
6010		/* At this point, we do not know if DECL is a valid
6011		   integral constant expression.  We assume that it is
6012		   in fact such an expression, so that code like:
6013
6014		      template <int N> struct A {
6015			int a[B<N>::i];
6016		      };
6017
6018		   is accepted.  At template-instantiation time, we
6019		   will check that B<N>::i is actually a constant.  */
6020		return decl;
6021	      }
6022	    /* Check to see if DECL is a local variable in a context
6023	       where that is forbidden.  */
6024	    if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6025		&& local_variable_p (decl)
6026		/* DR 2082 permits local variables in unevaluated contexts
6027		   within a default argument.  */
6028		&& !cp_unevaluated_operand)
6029	      {
6030		const char *msg
6031		  = (TREE_CODE (decl) == PARM_DECL
6032		     ? _("parameter %qD may not appear in this context")
6033		     : _("local variable %qD may not appear in this context"));
6034		error_at (id_expression.get_location (), msg,
6035			  decl.get_value ());
6036		return error_mark_node;
6037	      }
6038	  }
6039
6040	decl = (finish_id_expression
6041		(id_expression, decl, parser->scope,
6042		 idk,
6043		 parser->integral_constant_expression_p,
6044		 parser->allow_non_integral_constant_expression_p,
6045		 &parser->non_integral_constant_expression_p,
6046		 template_p, done, address_p,
6047		 template_arg_p,
6048		 &error_msg,
6049		 id_expression.get_location ()));
6050	if (error_msg)
6051	  cp_parser_error (parser, error_msg);
6052	/* Build a location for an id-expression of the form:
6053	     ::ns::id
6054             ~~~~~~^~
6055	  or:
6056	     id
6057	     ^~
6058	   i.e. from the start of the first token to the end of the final
6059	   token, with the caret at the start of the unqualified-id.  */
6060	location_t caret_loc = get_pure_location (id_expression.get_location ());
6061	location_t start_loc = get_start (id_expr_token->location);
6062	location_t finish_loc = get_finish (id_expression.get_location ());
6063	location_t combined_loc
6064	  = make_location (caret_loc, start_loc, finish_loc);
6065
6066	decl.set_location (combined_loc);
6067	return decl;
6068      }
6069
6070      /* Anything else is an error.  */
6071    default:
6072      cp_parser_error (parser, "expected primary-expression");
6073      return error_mark_node;
6074    }
6075}
6076
6077static inline cp_expr
6078cp_parser_primary_expression (cp_parser *parser,
6079			      bool address_p,
6080			      bool cast_p,
6081			      bool template_arg_p,
6082			      cp_id_kind *idk)
6083{
6084  return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6085				       /*decltype*/false, idk);
6086}
6087
6088/* Parse an id-expression.
6089
6090   id-expression:
6091     unqualified-id
6092     qualified-id
6093
6094   qualified-id:
6095     :: [opt] nested-name-specifier template [opt] unqualified-id
6096     :: identifier
6097     :: operator-function-id
6098     :: template-id
6099
6100   Return a representation of the unqualified portion of the
6101   identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
6102   a `::' or nested-name-specifier.
6103
6104   Often, if the id-expression was a qualified-id, the caller will
6105   want to make a SCOPE_REF to represent the qualified-id.  This
6106   function does not do this in order to avoid wastefully creating
6107   SCOPE_REFs when they are not required.
6108
6109   If TEMPLATE_KEYWORD_P is true, then we have just seen the
6110   `template' keyword.
6111
6112   If CHECK_DEPENDENCY_P is false, then names are looked up inside
6113   uninstantiated templates.
6114
6115   If *TEMPLATE_P is non-NULL, it is set to true iff the
6116   `template' keyword is used to explicitly indicate that the entity
6117   named is a template.
6118
6119   If DECLARATOR_P is true, the id-expression is appearing as part of
6120   a declarator, rather than as part of an expression.  */
6121
6122static cp_expr
6123cp_parser_id_expression (cp_parser *parser,
6124			 bool template_keyword_p,
6125			 bool check_dependency_p,
6126			 bool *template_p,
6127			 bool declarator_p,
6128			 bool optional_p)
6129{
6130  bool global_scope_p;
6131  bool nested_name_specifier_p;
6132
6133  /* Assume the `template' keyword was not used.  */
6134  if (template_p)
6135    *template_p = template_keyword_p;
6136
6137  /* Look for the optional `::' operator.  */
6138  global_scope_p
6139    = (!template_keyword_p
6140       && (cp_parser_global_scope_opt (parser,
6141				       /*current_scope_valid_p=*/false)
6142	   != NULL_TREE));
6143
6144  /* Look for the optional nested-name-specifier.  */
6145  nested_name_specifier_p
6146    = (cp_parser_nested_name_specifier_opt (parser,
6147					    /*typename_keyword_p=*/false,
6148					    check_dependency_p,
6149					    /*type_p=*/false,
6150					    declarator_p,
6151					    template_keyword_p)
6152       != NULL_TREE);
6153
6154  cp_expr id = NULL_TREE;
6155  tree scope = parser->scope;
6156
6157  /* Peek at the next token.  */
6158  cp_token *token = cp_lexer_peek_token (parser->lexer);
6159
6160  /* If there is a nested-name-specifier, then we are looking at
6161     the first qualified-id production.  */
6162  if (nested_name_specifier_p)
6163    {
6164      tree saved_object_scope;
6165      tree saved_qualifying_scope;
6166
6167      /* See if the next token is the `template' keyword.  */
6168      if (!template_p)
6169	template_p = &template_keyword_p;
6170      *template_p = cp_parser_optional_template_keyword (parser);
6171      /* Name lookup we do during the processing of the
6172	 unqualified-id might obliterate SCOPE.  */
6173      saved_object_scope = parser->object_scope;
6174      saved_qualifying_scope = parser->qualifying_scope;
6175      /* Process the final unqualified-id.  */
6176      id = cp_parser_unqualified_id (parser, *template_p,
6177				     check_dependency_p,
6178				     declarator_p,
6179				     /*optional_p=*/false);
6180      /* Restore the SAVED_SCOPE for our caller.  */
6181      parser->scope = scope;
6182      parser->object_scope = saved_object_scope;
6183      parser->qualifying_scope = saved_qualifying_scope;
6184    }
6185  /* Otherwise, if we are in global scope, then we are looking at one
6186     of the other qualified-id productions.  */
6187  else if (global_scope_p)
6188    {
6189      /* If it's an identifier, and the next token is not a "<", then
6190	 we can avoid the template-id case.  This is an optimization
6191	 for this common case.  */
6192      if (token->type == CPP_NAME
6193	  && !cp_parser_nth_token_starts_template_argument_list_p
6194	       (parser, 2))
6195	return cp_parser_identifier (parser);
6196
6197      cp_parser_parse_tentatively (parser);
6198      /* Try a template-id.  */
6199      id = cp_parser_template_id_expr (parser,
6200				       /*template_keyword_p=*/false,
6201				       /*check_dependency_p=*/true,
6202				       declarator_p);
6203      /* If that worked, we're done.  */
6204      if (cp_parser_parse_definitely (parser))
6205	return id;
6206
6207      /* Peek at the next token.  (Changes in the token buffer may
6208	 have invalidated the pointer obtained above.)  */
6209      token = cp_lexer_peek_token (parser->lexer);
6210
6211      switch (token->type)
6212	{
6213	case CPP_NAME:
6214	  id = cp_parser_identifier (parser);
6215	  break;
6216
6217	case CPP_KEYWORD:
6218	  if (token->keyword == RID_OPERATOR)
6219	    {
6220	      id = cp_parser_operator_function_id (parser);
6221	      break;
6222	    }
6223	  /* Fall through.  */
6224
6225	default:
6226	  cp_parser_error (parser, "expected id-expression");
6227	  return error_mark_node;
6228	}
6229    }
6230  else
6231    {
6232      if (!scope)
6233	scope = parser->context->object_type;
6234      id = cp_parser_unqualified_id (parser, template_keyword_p,
6235				     /*check_dependency_p=*/true,
6236				     declarator_p,
6237				     optional_p);
6238    }
6239
6240  if (id && TREE_CODE (id) == IDENTIFIER_NODE
6241      && warn_missing_template_keyword
6242      && !template_keyword_p
6243      /* Don't warn if we're looking inside templates.  */
6244      && check_dependency_p
6245      /* In a template argument list a > could be closing
6246	 the enclosing targs.  */
6247      && !parser->in_template_argument_list_p
6248      && scope && dependentish_scope_p (scope)
6249      /* Don't confuse an ill-formed constructor declarator for a missing
6250	 template keyword in a return type.  */
6251      && !(declarator_p && constructor_name_p (id, scope))
6252      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6253      && warning_enabled_at (token->location,
6254			     OPT_Wmissing_template_keyword))
6255    {
6256      saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6257      if (cp_parser_skip_entire_template_parameter_list (parser)
6258	  /* An operator after the > suggests that the > ends a
6259	     template-id; a name or literal suggests that the > is an
6260	     operator.  */
6261	  && (cp_lexer_peek_token (parser->lexer)->type
6262	      <= CPP_LAST_PUNCTUATOR))
6263	warning_at (token->location, OPT_Wmissing_template_keyword,
6264		    "expected %qs keyword before dependent "
6265		    "template name", "template");
6266    }
6267
6268  return id;
6269}
6270
6271/* Parse an unqualified-id.
6272
6273   unqualified-id:
6274     identifier
6275     operator-function-id
6276     conversion-function-id
6277     ~ class-name
6278     template-id
6279
6280   If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6281   keyword, in a construct like `A::template ...'.
6282
6283   Returns a representation of unqualified-id.  For the `identifier'
6284   production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
6285   production a BIT_NOT_EXPR is returned; the operand of the
6286   BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
6287   other productions, see the documentation accompanying the
6288   corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
6289   names are looked up in uninstantiated templates.  If DECLARATOR_P
6290   is true, the unqualified-id is appearing as part of a declarator,
6291   rather than as part of an expression.  */
6292
6293static cp_expr
6294cp_parser_unqualified_id (cp_parser* parser,
6295			  bool template_keyword_p,
6296			  bool check_dependency_p,
6297			  bool declarator_p,
6298			  bool optional_p)
6299{
6300  cp_token *token;
6301
6302  /* Peek at the next token.  */
6303  token = cp_lexer_peek_token (parser->lexer);
6304
6305  switch ((int) token->type)
6306    {
6307    case CPP_NAME:
6308      {
6309	tree id;
6310
6311	/* We don't know yet whether or not this will be a
6312	   template-id.  */
6313	cp_parser_parse_tentatively (parser);
6314	/* Try a template-id.  */
6315	id = cp_parser_template_id_expr (parser, template_keyword_p,
6316					 check_dependency_p,
6317					 declarator_p);
6318	/* If it worked, we're done.  */
6319	if (cp_parser_parse_definitely (parser))
6320	  return id;
6321	/* Otherwise, it's an ordinary identifier.  */
6322	return cp_parser_identifier (parser);
6323      }
6324
6325    case CPP_TEMPLATE_ID:
6326      return cp_parser_template_id_expr (parser, template_keyword_p,
6327					 check_dependency_p,
6328					 declarator_p);
6329
6330    case CPP_COMPL:
6331      {
6332	tree type_decl;
6333	tree qualifying_scope;
6334	tree object_scope;
6335	tree scope;
6336	bool done;
6337	location_t tilde_loc = token->location;
6338
6339	/* Consume the `~' token.  */
6340	cp_lexer_consume_token (parser->lexer);
6341	/* Parse the class-name.  The standard, as written, seems to
6342	   say that:
6343
6344	     template <typename T> struct S { ~S (); };
6345	     template <typename T> S<T>::~S() {}
6346
6347	   is invalid, since `~' must be followed by a class-name, but
6348	   `S<T>' is dependent, and so not known to be a class.
6349	   That's not right; we need to look in uninstantiated
6350	   templates.  A further complication arises from:
6351
6352	     template <typename T> void f(T t) {
6353	       t.T::~T();
6354	     }
6355
6356	   Here, it is not possible to look up `T' in the scope of `T'
6357	   itself.  We must look in both the current scope, and the
6358	   scope of the containing complete expression.
6359
6360	   Yet another issue is:
6361
6362	     struct S {
6363	       int S;
6364	       ~S();
6365	     };
6366
6367	     S::~S() {}
6368
6369	   The standard does not seem to say that the `S' in `~S'
6370	   should refer to the type `S' and not the data member
6371	   `S::S'.  */
6372
6373	/* DR 244 says that we look up the name after the "~" in the
6374	   same scope as we looked up the qualifying name.  That idea
6375	   isn't fully worked out; it's more complicated than that.  */
6376	scope = parser->scope;
6377	object_scope = parser->object_scope;
6378	qualifying_scope = parser->qualifying_scope;
6379
6380	/* Check for invalid scopes.  */
6381	if (scope == error_mark_node)
6382	  {
6383	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6384	      cp_lexer_consume_token (parser->lexer);
6385	    return error_mark_node;
6386	  }
6387	if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6388	  {
6389	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6390	      error_at (token->location,
6391			"scope %qT before %<~%> is not a class-name",
6392			scope);
6393	    cp_parser_simulate_error (parser);
6394	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6395	      cp_lexer_consume_token (parser->lexer);
6396	    return error_mark_node;
6397	  }
6398	if (template_keyword_p)
6399	  {
6400	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6401	      error_at (tilde_loc, "%<template%> keyword not permitted in "
6402			"destructor name");
6403	    cp_parser_simulate_error (parser);
6404	    return error_mark_node;
6405	  }
6406
6407	gcc_assert (!scope || TYPE_P (scope));
6408
6409	token = cp_lexer_peek_token (parser->lexer);
6410
6411	/* Create a location with caret == start at the tilde,
6412	   finishing at the end of the peeked token, e.g:
6413	   ~token
6414	   ^~~~~~.  */
6415	location_t loc
6416	  = make_location (tilde_loc, tilde_loc, token->location);
6417
6418	/* If the name is of the form "X::~X" it's OK even if X is a
6419	   typedef.  */
6420
6421	if (scope
6422	    && token->type == CPP_NAME
6423	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6424		!= CPP_LESS)
6425	    && (token->u.value == TYPE_IDENTIFIER (scope)
6426		|| (CLASS_TYPE_P (scope)
6427		    && constructor_name_p (token->u.value, scope))))
6428	  {
6429	    cp_lexer_consume_token (parser->lexer);
6430	    return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6431	  }
6432
6433	/* ~auto means the destructor of whatever the object is.  */
6434	if (cp_parser_is_keyword (token, RID_AUTO))
6435	  {
6436	    if (cxx_dialect < cxx14)
6437	      pedwarn (loc, OPT_Wc__14_extensions,
6438		       "%<~auto%> only available with "
6439		       "%<-std=c++14%> or %<-std=gnu++14%>");
6440	    cp_lexer_consume_token (parser->lexer);
6441	    return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6442	  }
6443
6444	/* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6445	   declarator-id of a constructor or destructor.  */
6446	if (token->type == CPP_TEMPLATE_ID && declarator_p
6447	    && cxx_dialect >= cxx20)
6448	  {
6449	    if (!cp_parser_simulate_error (parser))
6450	      error_at (tilde_loc, "template-id not allowed for destructor");
6451	    return error_mark_node;
6452	  }
6453
6454	/* If there was an explicit qualification (S::~T), first look
6455	   in the scope given by the qualification (i.e., S).
6456
6457	   Note: in the calls to cp_parser_class_name below we pass
6458	   typename_type so that lookup finds the injected-class-name
6459	   rather than the constructor.  */
6460	done = false;
6461	type_decl = NULL_TREE;
6462	if (scope)
6463	  {
6464	    cp_parser_parse_tentatively (parser);
6465	    type_decl = cp_parser_class_name (parser,
6466					      /*typename_keyword_p=*/false,
6467					      /*template_keyword_p=*/false,
6468					      typename_type,
6469					      /*check_dependency=*/false,
6470					      /*class_head_p=*/false,
6471					      declarator_p);
6472	    if (cp_parser_parse_definitely (parser))
6473	      done = true;
6474	  }
6475	/* In "N::S::~S", look in "N" as well.  */
6476	if (!done && scope && qualifying_scope)
6477	  {
6478	    cp_parser_parse_tentatively (parser);
6479	    parser->scope = qualifying_scope;
6480	    parser->object_scope = NULL_TREE;
6481	    parser->qualifying_scope = NULL_TREE;
6482	    type_decl
6483	      = cp_parser_class_name (parser,
6484				      /*typename_keyword_p=*/false,
6485				      /*template_keyword_p=*/false,
6486				      typename_type,
6487				      /*check_dependency=*/false,
6488				      /*class_head_p=*/false,
6489				      declarator_p);
6490	    if (cp_parser_parse_definitely (parser))
6491	      done = true;
6492	  }
6493	/* In "p->S::~T", look in the scope given by "*p" as well.  */
6494	else if (!done && object_scope)
6495	  {
6496	    cp_parser_parse_tentatively (parser);
6497	    parser->scope = object_scope;
6498	    parser->object_scope = NULL_TREE;
6499	    parser->qualifying_scope = NULL_TREE;
6500	    type_decl
6501	      = cp_parser_class_name (parser,
6502				      /*typename_keyword_p=*/false,
6503				      /*template_keyword_p=*/false,
6504				      typename_type,
6505				      /*check_dependency=*/false,
6506				      /*class_head_p=*/false,
6507				      declarator_p);
6508	    if (cp_parser_parse_definitely (parser))
6509	      done = true;
6510	  }
6511	/* Look in the surrounding context.  */
6512	if (!done)
6513	  {
6514	    parser->scope = NULL_TREE;
6515	    parser->object_scope = NULL_TREE;
6516	    parser->qualifying_scope = NULL_TREE;
6517	    if (processing_template_decl)
6518	      cp_parser_parse_tentatively (parser);
6519	    type_decl
6520	      = cp_parser_class_name (parser,
6521				      /*typename_keyword_p=*/false,
6522				      /*template_keyword_p=*/false,
6523				      typename_type,
6524				      /*check_dependency=*/false,
6525				      /*class_head_p=*/false,
6526				      declarator_p);
6527	    if (processing_template_decl
6528		&& ! cp_parser_parse_definitely (parser))
6529	      {
6530		/* We couldn't find a type with this name.  If we're parsing
6531		   tentatively, fail and try something else.  */
6532		if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6533		  {
6534		    cp_parser_simulate_error (parser);
6535		    return error_mark_node;
6536		  }
6537		/* Otherwise, accept it and check for a match at instantiation
6538		   time.  */
6539		type_decl = cp_parser_identifier (parser);
6540		if (type_decl != error_mark_node)
6541		  type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6542		return type_decl;
6543	      }
6544	  }
6545	/* If an error occurred, assume that the name of the
6546	   destructor is the same as the name of the qualifying
6547	   class.  That allows us to keep parsing after running
6548	   into ill-formed destructor names.  */
6549	if (type_decl == error_mark_node && scope)
6550	  return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6551	else if (type_decl == error_mark_node)
6552	  return error_mark_node;
6553
6554	/* Check that destructor name and scope match.  */
6555	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6556	  {
6557	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6558	      error_at (loc,
6559			"declaration of %<~%T%> as member of %qT",
6560			type_decl, scope);
6561	    cp_parser_simulate_error (parser);
6562	    return error_mark_node;
6563	  }
6564
6565	/* [class.dtor]
6566
6567	   A typedef-name that names a class shall not be used as the
6568	   identifier in the declarator for a destructor declaration.  */
6569	if (declarator_p
6570	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6571	    && !DECL_SELF_REFERENCE_P (type_decl)
6572	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6573	  error_at (loc,
6574		    "typedef-name %qD used as destructor declarator",
6575		    type_decl);
6576
6577	return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6578      }
6579
6580    case CPP_KEYWORD:
6581      if (token->keyword == RID_OPERATOR)
6582	{
6583	  cp_expr id;
6584
6585	  /* This could be a template-id, so we try that first.  */
6586	  cp_parser_parse_tentatively (parser);
6587	  /* Try a template-id.  */
6588	  id = cp_parser_template_id_expr (parser, template_keyword_p,
6589					   /*check_dependency_p=*/true,
6590					   declarator_p);
6591	  /* If that worked, we're done.  */
6592	  if (cp_parser_parse_definitely (parser))
6593	    return id;
6594	  /* We still don't know whether we're looking at an
6595	     operator-function-id or a conversion-function-id.  */
6596	  cp_parser_parse_tentatively (parser);
6597	  /* Try an operator-function-id.  */
6598	  id = cp_parser_operator_function_id (parser);
6599	  /* If that didn't work, try a conversion-function-id.  */
6600	  if (!cp_parser_parse_definitely (parser))
6601	    id = cp_parser_conversion_function_id (parser);
6602
6603	  return id;
6604	}
6605      /* Fall through.  */
6606
6607    default:
6608      if (optional_p)
6609	return NULL_TREE;
6610      cp_parser_error (parser, "expected unqualified-id");
6611      return error_mark_node;
6612    }
6613}
6614
6615/* Check [temp.names]/5: A name prefixed by the keyword template shall
6616   be a template-id or the name shall refer to a class template or an
6617   alias template.  */
6618
6619static void
6620check_template_keyword_in_nested_name_spec (tree name)
6621{
6622  if (CLASS_TYPE_P (name)
6623      && ((CLASSTYPE_USE_TEMPLATE (name)
6624	   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6625	  || CLASSTYPE_IS_TEMPLATE (name)))
6626    return;
6627
6628  if (TREE_CODE (name) == TYPENAME_TYPE
6629      && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6630    return;
6631  /* Alias templates are also OK.  */
6632  else if (alias_template_specialization_p (name, nt_opaque))
6633    return;
6634
6635  permerror (input_location, TYPE_P (name)
6636	     ? G_("%qT is not a template")
6637	     : G_("%qD is not a template"),
6638	     name);
6639}
6640
6641/* Parse an (optional) nested-name-specifier.
6642
6643   nested-name-specifier: [C++98]
6644     class-or-namespace-name :: nested-name-specifier [opt]
6645     class-or-namespace-name :: template nested-name-specifier [opt]
6646
6647   nested-name-specifier: [C++0x]
6648     type-name ::
6649     namespace-name ::
6650     nested-name-specifier identifier ::
6651     nested-name-specifier template [opt] simple-template-id ::
6652
6653   PARSER->SCOPE should be set appropriately before this function is
6654   called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6655   effect.  TYPE_P is TRUE if we non-type bindings should be ignored
6656   in name lookups.
6657
6658   Sets PARSER->SCOPE to the class (TYPE) or namespace
6659   (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6660   it unchanged if there is no nested-name-specifier.  Returns the new
6661   scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6662
6663   If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6664
6665   If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6666   part of a declaration and/or decl-specifier.  */
6667
6668static tree
6669cp_parser_nested_name_specifier_opt (cp_parser *parser,
6670				     bool typename_keyword_p,
6671				     bool check_dependency_p,
6672				     bool type_p,
6673				     bool is_declaration,
6674				     bool template_keyword_p /* = false */)
6675{
6676  bool success = false;
6677  cp_token_position start = 0;
6678  cp_token *token;
6679
6680  /* Remember where the nested-name-specifier starts.  */
6681  if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6682      && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6683    {
6684      start = cp_lexer_token_position (parser->lexer, false);
6685      push_deferring_access_checks (dk_deferred);
6686    }
6687
6688  while (true)
6689    {
6690      tree new_scope;
6691      tree old_scope;
6692      tree saved_qualifying_scope;
6693
6694      /* Spot cases that cannot be the beginning of a
6695	 nested-name-specifier.  */
6696      token = cp_lexer_peek_token (parser->lexer);
6697
6698      /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6699	 the already parsed nested-name-specifier.  */
6700      if (token->type == CPP_NESTED_NAME_SPECIFIER)
6701	{
6702	  /* Grab the nested-name-specifier and continue the loop.  */
6703	  cp_parser_pre_parsed_nested_name_specifier (parser);
6704	  /* If we originally encountered this nested-name-specifier
6705	     with CHECK_DEPENDENCY_P set to true, we will not have
6706	     resolved TYPENAME_TYPEs, so we must do so here.  */
6707	  if (is_declaration
6708	      && !check_dependency_p
6709	      && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6710	    {
6711	      new_scope = resolve_typename_type (parser->scope,
6712						 /*only_current_p=*/false);
6713	      if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6714		parser->scope = new_scope;
6715	    }
6716	  success = true;
6717	  continue;
6718	}
6719
6720      /* Spot cases that cannot be the beginning of a
6721	 nested-name-specifier.  On the second and subsequent times
6722	 through the loop, we look for the `template' keyword.  */
6723      if (success && token->keyword == RID_TEMPLATE)
6724	;
6725      /* A template-id can start a nested-name-specifier.  */
6726      else if (token->type == CPP_TEMPLATE_ID)
6727	;
6728      /* DR 743: decltype can be used in a nested-name-specifier.  */
6729      else if (token_is_decltype (token))
6730	;
6731      else
6732	{
6733	  /* If the next token is not an identifier, then it is
6734	     definitely not a type-name or namespace-name.  */
6735	  if (token->type != CPP_NAME)
6736	    break;
6737	  /* If the following token is neither a `<' (to begin a
6738	     template-id), nor a `::', then we are not looking at a
6739	     nested-name-specifier.  */
6740	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
6741
6742	  if (token->type == CPP_COLON
6743	      && parser->colon_corrects_to_scope_p
6744	      && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6745	      /* name:name is a valid sequence in an Objective C message.  */
6746	      && !parser->objective_c_message_context_p)
6747	    {
6748	      gcc_rich_location richloc (token->location);
6749	      richloc.add_fixit_replace ("::");
6750	      error_at (&richloc,
6751			"found %<:%> in nested-name-specifier, "
6752			"expected %<::%>");
6753	      token->type = CPP_SCOPE;
6754	    }
6755
6756	  if (token->type != CPP_SCOPE
6757	      && !cp_parser_nth_token_starts_template_argument_list_p
6758		  (parser, 2))
6759	    break;
6760	}
6761
6762      /* The nested-name-specifier is optional, so we parse
6763	 tentatively.  */
6764      cp_parser_parse_tentatively (parser);
6765
6766      /* Look for the optional `template' keyword, if this isn't the
6767	 first time through the loop.  */
6768      if (success)
6769	{
6770	  template_keyword_p = cp_parser_optional_template_keyword (parser);
6771	  /* DR1710: "In a qualified-id used as the name in
6772	     a typename-specifier, elaborated-type-specifier, using-declaration,
6773	     or class-or-decltype, an optional keyword template appearing at
6774	     the top level is ignored."  */
6775	  if (!template_keyword_p
6776	      && typename_keyword_p
6777	      && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6778	    template_keyword_p = true;
6779	}
6780
6781      /* Save the old scope since the name lookup we are about to do
6782	 might destroy it.  */
6783      old_scope = parser->scope;
6784      saved_qualifying_scope = parser->qualifying_scope;
6785      /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6786	 look up names in "X<T>::I" in order to determine that "Y" is
6787	 a template.  So, if we have a typename at this point, we make
6788	 an effort to look through it.  */
6789      if (is_declaration
6790	  && !check_dependency_p
6791	  && !typename_keyword_p
6792	  && parser->scope
6793	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6794	parser->scope = resolve_typename_type (parser->scope,
6795					       /*only_current_p=*/false);
6796      /* Parse the qualifying entity.  */
6797      new_scope
6798	= cp_parser_qualifying_entity (parser,
6799                                       typename_keyword_p,
6800                                       template_keyword_p,
6801                                       check_dependency_p,
6802                                       type_p,
6803                                       is_declaration);
6804      /* Look for the `::' token.  */
6805      cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6806
6807      /* If we found what we wanted, we keep going; otherwise, we're
6808	 done.  */
6809      if (!cp_parser_parse_definitely (parser))
6810	{
6811	  bool error_p = false;
6812
6813	  /* Restore the OLD_SCOPE since it was valid before the
6814	     failed attempt at finding the last
6815	     class-or-namespace-name.  */
6816	  parser->scope = old_scope;
6817	  parser->qualifying_scope = saved_qualifying_scope;
6818
6819	  /* If the next token is a decltype, and the one after that is a
6820	     `::', then the decltype has failed to resolve to a class or
6821	     enumeration type.  Give this error even when parsing
6822	     tentatively since it can't possibly be valid--and we're going
6823	     to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6824	     won't get another chance.*/
6825	  if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6826	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6827		  == CPP_SCOPE))
6828	    {
6829	      token = cp_lexer_consume_token (parser->lexer);
6830	      tree dtype = token->u.tree_check_value->value;
6831	      if (dtype != error_mark_node)
6832		error_at (token->location, "%<decltype%> evaluates to %qT, "
6833			  "which is not a class or enumeration type",
6834			  dtype);
6835	      parser->scope = error_mark_node;
6836	      error_p = true;
6837	      /* As below.  */
6838	      success = true;
6839	      cp_lexer_consume_token (parser->lexer);
6840	    }
6841
6842	  if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6843	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6844	    {
6845	      /* If we have a non-type template-id followed by ::, it can't
6846		 possibly be valid.  */
6847	      token = cp_lexer_peek_token (parser->lexer);
6848	      tree tid = token->u.tree_check_value->value;
6849	      if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6850		  && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6851		{
6852		  tree tmpl = NULL_TREE;
6853		  if (is_overloaded_fn (tid))
6854		    {
6855		      tree fns = get_fns (tid);
6856		      if (OVL_SINGLE_P (fns))
6857			tmpl = OVL_FIRST (fns);
6858		      if (function_concept_p (fns))
6859			error_at (token->location, "concept-id %qD "
6860				  "in nested-name-specifier", tid);
6861		      else
6862			error_at (token->location, "function template-id "
6863				  "%qD in nested-name-specifier", tid);
6864		    }
6865		  else
6866		    {
6867		      tmpl = TREE_OPERAND (tid, 0);
6868		      if (variable_concept_p (tmpl)
6869			  || standard_concept_p (tmpl))
6870			error_at (token->location, "concept-id %qD "
6871				  "in nested-name-specifier", tid);
6872		      else
6873			{
6874			  /* Variable template.  */
6875			  gcc_assert (variable_template_p (tmpl));
6876			  error_at (token->location, "variable template-id "
6877				    "%qD in nested-name-specifier", tid);
6878			}
6879		    }
6880		  if (tmpl)
6881		    inform (DECL_SOURCE_LOCATION (tmpl),
6882			    "%qD declared here", tmpl);
6883
6884		  parser->scope = error_mark_node;
6885		  error_p = true;
6886		  /* As below.  */
6887		  success = true;
6888		  cp_lexer_consume_token (parser->lexer);
6889		  cp_lexer_consume_token (parser->lexer);
6890		}
6891	    }
6892
6893	  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6894	    break;
6895	  /* If the next token is an identifier, and the one after
6896	     that is a `::', then any valid interpretation would have
6897	     found a class-or-namespace-name.  */
6898	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6899		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6900		     == CPP_SCOPE)
6901		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6902		     != CPP_COMPL))
6903	    {
6904	      token = cp_lexer_consume_token (parser->lexer);
6905	      if (!error_p)
6906		{
6907		  if (!token->error_reported)
6908		    {
6909		      tree decl;
6910		      tree ambiguous_decls;
6911
6912		      decl = cp_parser_lookup_name (parser, token->u.value,
6913						    none_type,
6914						    /*is_template=*/false,
6915						    /*is_namespace=*/false,
6916						    /*check_dependency=*/true,
6917						    &ambiguous_decls,
6918						    token->location);
6919		      if (TREE_CODE (decl) == TEMPLATE_DECL)
6920			error_at (token->location,
6921				  "%qD used without template arguments",
6922				  decl);
6923		      else if (ambiguous_decls)
6924			{
6925			  // cp_parser_lookup_name has the same diagnostic,
6926			  // thus make sure to emit it at most once.
6927			  if (cp_parser_uncommitted_to_tentative_parse_p
6928			      (parser))
6929			    {
6930			      error_at (token->location,
6931					"reference to %qD is ambiguous",
6932					token->u.value);
6933			      print_candidates (ambiguous_decls);
6934			    }
6935			  decl = error_mark_node;
6936			}
6937		      else
6938                        {
6939                          if (cxx_dialect != cxx98)
6940                            cp_parser_name_lookup_error
6941                            (parser, token->u.value, decl, NLE_NOT_CXX98,
6942	  		     token->location);
6943			  else
6944			    cp_parser_name_lookup_error
6945			    (parser, token->u.value, decl, NLE_CXX98,
6946			     token->location);
6947                        }
6948		    }
6949		  parser->scope = error_mark_node;
6950		  error_p = true;
6951		  /* Treat this as a successful nested-name-specifier
6952		     due to:
6953
6954		     [basic.lookup.qual]
6955
6956		     If the name found is not a class-name (clause
6957		     _class_) or namespace-name (_namespace.def_), the
6958		     program is ill-formed.  */
6959		  success = true;
6960		}
6961	      cp_lexer_consume_token (parser->lexer);
6962	    }
6963	  break;
6964	}
6965      /* We've found one valid nested-name-specifier.  */
6966      success = true;
6967      /* Name lookup always gives us a DECL.  */
6968      if (TREE_CODE (new_scope) == TYPE_DECL)
6969	new_scope = TREE_TYPE (new_scope);
6970      /* Uses of "template" must be followed by actual templates.  */
6971      if (template_keyword_p)
6972	check_template_keyword_in_nested_name_spec (new_scope);
6973      /* If it is a class scope, try to complete it; we are about to
6974	 be looking up names inside the class.  */
6975      if (TYPE_P (new_scope)
6976	  /* Since checking types for dependency can be expensive,
6977	     avoid doing it if the type is already complete.  */
6978	  && !COMPLETE_TYPE_P (new_scope)
6979	  /* Do not try to complete dependent types.  */
6980	  && !dependent_type_p (new_scope))
6981	{
6982	  new_scope = complete_type (new_scope);
6983	  /* If it is a typedef to current class, use the current
6984	     class instead, as the typedef won't have any names inside
6985	     it yet.  */
6986	  if (!COMPLETE_TYPE_P (new_scope)
6987	      && currently_open_class (new_scope))
6988	    new_scope = TYPE_MAIN_VARIANT (new_scope);
6989	}
6990      /* Make sure we look in the right scope the next time through
6991	 the loop.  */
6992      parser->scope = new_scope;
6993    }
6994
6995  /* If parsing tentatively, replace the sequence of tokens that makes
6996     up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6997     token.  That way, should we re-parse the token stream, we will
6998     not have to repeat the effort required to do the parse, nor will
6999     we issue duplicate error messages.  */
7000  if (success && start)
7001    {
7002      cp_token *token;
7003
7004      token = cp_lexer_token_at (parser->lexer, start);
7005      /* Reset the contents of the START token.  */
7006      token->type = CPP_NESTED_NAME_SPECIFIER;
7007      /* Retrieve any deferred checks.  Do not pop this access checks yet
7008	 so the memory will not be reclaimed during token replacing below.  */
7009      token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7010      token->tree_check_p = true;
7011      token->u.tree_check_value->value = parser->scope;
7012      token->u.tree_check_value->checks = get_deferred_access_checks ();
7013      token->u.tree_check_value->qualifying_scope =
7014	parser->qualifying_scope;
7015      token->keyword = RID_MAX;
7016
7017      /* Purge all subsequent tokens.  */
7018      cp_lexer_purge_tokens_after (parser->lexer, start);
7019    }
7020
7021  if (start)
7022    pop_to_parent_deferring_access_checks ();
7023
7024  return success ? parser->scope : NULL_TREE;
7025}
7026
7027/* Parse a nested-name-specifier.  See
7028   cp_parser_nested_name_specifier_opt for details.  This function
7029   behaves identically, except that it will an issue an error if no
7030   nested-name-specifier is present.  */
7031
7032static tree
7033cp_parser_nested_name_specifier (cp_parser *parser,
7034				 bool typename_keyword_p,
7035				 bool check_dependency_p,
7036				 bool type_p,
7037				 bool is_declaration)
7038{
7039  tree scope;
7040
7041  /* Look for the nested-name-specifier.  */
7042  scope = cp_parser_nested_name_specifier_opt (parser,
7043					       typename_keyword_p,
7044					       check_dependency_p,
7045					       type_p,
7046					       is_declaration);
7047  /* If it was not present, issue an error message.  */
7048  if (!scope)
7049    {
7050      cp_parser_error (parser, "expected nested-name-specifier");
7051      parser->scope = NULL_TREE;
7052    }
7053
7054  return scope;
7055}
7056
7057/* Parse the qualifying entity in a nested-name-specifier. For C++98,
7058   this is either a class-name or a namespace-name (which corresponds
7059   to the class-or-namespace-name production in the grammar). For
7060   C++0x, it can also be a type-name that refers to an enumeration
7061   type or a simple-template-id.
7062
7063   TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7064   TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7065   CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7066   TYPE_P is TRUE iff the next name should be taken as a class-name,
7067   even the same name is declared to be another entity in the same
7068   scope.
7069
7070   Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7071   specified by the class-or-namespace-name.  If neither is found the
7072   ERROR_MARK_NODE is returned.  */
7073
7074static tree
7075cp_parser_qualifying_entity (cp_parser *parser,
7076			     bool typename_keyword_p,
7077			     bool template_keyword_p,
7078			     bool check_dependency_p,
7079			     bool type_p,
7080			     bool is_declaration)
7081{
7082  tree saved_scope;
7083  tree saved_qualifying_scope;
7084  tree saved_object_scope;
7085  tree scope;
7086  bool only_class_p;
7087  bool successful_parse_p;
7088
7089  /* DR 743: decltype can appear in a nested-name-specifier.  */
7090  if (cp_lexer_next_token_is_decltype (parser->lexer))
7091    {
7092      scope = cp_parser_decltype (parser);
7093      if (TREE_CODE (scope) != ENUMERAL_TYPE
7094	  && !MAYBE_CLASS_TYPE_P (scope))
7095	{
7096	  cp_parser_simulate_error (parser);
7097	  return error_mark_node;
7098	}
7099      if (TYPE_NAME (scope))
7100	scope = TYPE_NAME (scope);
7101      return scope;
7102    }
7103
7104  /* Before we try to parse the class-name, we must save away the
7105     current PARSER->SCOPE since cp_parser_class_name will destroy
7106     it.  */
7107  saved_scope = parser->scope;
7108  saved_qualifying_scope = parser->qualifying_scope;
7109  saved_object_scope = parser->object_scope;
7110  /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
7111     there is no need to look for a namespace-name.  */
7112  only_class_p = template_keyword_p
7113    || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7114  if (!only_class_p)
7115    cp_parser_parse_tentatively (parser);
7116  scope = cp_parser_class_name (parser,
7117				typename_keyword_p,
7118				template_keyword_p,
7119				type_p ? class_type : none_type,
7120				check_dependency_p,
7121				/*class_head_p=*/false,
7122				is_declaration,
7123				/*enum_ok=*/cxx_dialect > cxx98);
7124  successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7125  /* If that didn't work, try for a namespace-name.  */
7126  if (!only_class_p && !successful_parse_p)
7127    {
7128      /* Restore the saved scope.  */
7129      parser->scope = saved_scope;
7130      parser->qualifying_scope = saved_qualifying_scope;
7131      parser->object_scope = saved_object_scope;
7132      /* If we are not looking at an identifier followed by the scope
7133	 resolution operator, then this is not part of a
7134	 nested-name-specifier.  (Note that this function is only used
7135	 to parse the components of a nested-name-specifier.)  */
7136      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7137	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7138	return error_mark_node;
7139      scope = cp_parser_namespace_name (parser);
7140    }
7141
7142  return scope;
7143}
7144
7145/* Return true if we are looking at a compound-literal, false otherwise.  */
7146
7147static bool
7148cp_parser_compound_literal_p (cp_parser *parser)
7149{
7150  cp_lexer_save_tokens (parser->lexer);
7151
7152  /* Skip tokens until the next token is a closing parenthesis.
7153     If we find the closing `)', and the next token is a `{', then
7154     we are looking at a compound-literal.  */
7155  bool compound_literal_p
7156    = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7157					      /*consume_paren=*/true)
7158       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7159
7160  /* Roll back the tokens we skipped.  */
7161  cp_lexer_rollback_tokens (parser->lexer);
7162
7163  return compound_literal_p;
7164}
7165
7166/* Return true if EXPR is the integer constant zero or a complex constant
7167   of zero, without any folding, but ignoring location wrappers.  */
7168
7169bool
7170literal_integer_zerop (const_tree expr)
7171{
7172  return (location_wrapper_p (expr)
7173	  && integer_zerop (TREE_OPERAND (expr, 0)));
7174}
7175
7176/* Parse a postfix-expression.
7177
7178   postfix-expression:
7179     primary-expression
7180     postfix-expression [ expression ]
7181     postfix-expression ( expression-list [opt] )
7182     simple-type-specifier ( expression-list [opt] )
7183     typename :: [opt] nested-name-specifier identifier
7184       ( expression-list [opt] )
7185     typename :: [opt] nested-name-specifier template [opt] template-id
7186       ( expression-list [opt] )
7187     postfix-expression . template [opt] id-expression
7188     postfix-expression -> template [opt] id-expression
7189     postfix-expression . pseudo-destructor-name
7190     postfix-expression -> pseudo-destructor-name
7191     postfix-expression ++
7192     postfix-expression --
7193     dynamic_cast < type-id > ( expression )
7194     static_cast < type-id > ( expression )
7195     reinterpret_cast < type-id > ( expression )
7196     const_cast < type-id > ( expression )
7197     typeid ( expression )
7198     typeid ( type-id )
7199
7200   GNU Extension:
7201
7202   postfix-expression:
7203     ( type-id ) { initializer-list , [opt] }
7204
7205   This extension is a GNU version of the C99 compound-literal
7206   construct.  (The C99 grammar uses `type-name' instead of `type-id',
7207   but they are essentially the same concept.)
7208
7209   If ADDRESS_P is true, the postfix expression is the operand of the
7210   `&' operator.  CAST_P is true if this expression is the target of a
7211   cast.
7212
7213   If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7214   class member access expressions [expr.ref].
7215
7216   Returns a representation of the expression.  */
7217
7218static cp_expr
7219cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7220                              bool member_access_only_p, bool decltype_p,
7221			      cp_id_kind * pidk_return)
7222{
7223  cp_token *token;
7224  location_t loc;
7225  enum rid keyword;
7226  cp_id_kind idk = CP_ID_KIND_NONE;
7227  cp_expr postfix_expression = NULL_TREE;
7228  bool is_member_access = false;
7229
7230  /* Peek at the next token.  */
7231  token = cp_lexer_peek_token (parser->lexer);
7232  loc = token->location;
7233  location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7234
7235  /* Some of the productions are determined by keywords.  */
7236  keyword = token->keyword;
7237  switch (keyword)
7238    {
7239    case RID_DYNCAST:
7240    case RID_STATCAST:
7241    case RID_REINTCAST:
7242    case RID_CONSTCAST:
7243      {
7244	tree type;
7245	cp_expr expression;
7246	const char *saved_message;
7247	bool saved_in_type_id_in_expr_p;
7248
7249	/* All of these can be handled in the same way from the point
7250	   of view of parsing.  Begin by consuming the token
7251	   identifying the cast.  */
7252	cp_lexer_consume_token (parser->lexer);
7253
7254	/* New types cannot be defined in the cast.  */
7255	saved_message = parser->type_definition_forbidden_message;
7256	parser->type_definition_forbidden_message
7257	  = G_("types may not be defined in casts");
7258
7259	/* Look for the opening `<'.  */
7260	cp_parser_require (parser, CPP_LESS, RT_LESS);
7261	/* Parse the type to which we are casting.  */
7262	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7263	parser->in_type_id_in_expr_p = true;
7264	type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7265				  NULL);
7266	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7267	/* Look for the closing `>'.  */
7268	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7269	/* Restore the old message.  */
7270	parser->type_definition_forbidden_message = saved_message;
7271
7272	bool saved_greater_than_is_operator_p
7273	  = parser->greater_than_is_operator_p;
7274	parser->greater_than_is_operator_p = true;
7275
7276	/* And the expression which is being cast.  */
7277	matching_parens parens;
7278	parens.require_open (parser);
7279	expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7280	cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7281						   RT_CLOSE_PAREN);
7282	location_t end_loc = close_paren ?
7283	  close_paren->location : UNKNOWN_LOCATION;
7284
7285	parser->greater_than_is_operator_p
7286	  = saved_greater_than_is_operator_p;
7287
7288	/* Only type conversions to integral or enumeration types
7289	   can be used in constant-expressions.  */
7290	if (!cast_valid_in_integral_constant_expression_p (type)
7291	    && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7292	  {
7293	    postfix_expression = error_mark_node;
7294	    break;
7295	  }
7296
7297	/* Construct a location e.g. :
7298	     reinterpret_cast <int *> (expr)
7299	     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7300	   ranging from the start of the "*_cast" token to the final closing
7301	   paren, with the caret at the start.  */
7302	location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7303
7304	switch (keyword)
7305	  {
7306	  case RID_DYNCAST:
7307	    postfix_expression
7308	      = build_dynamic_cast (cp_cast_loc, type, expression,
7309				    tf_warning_or_error);
7310	    break;
7311	  case RID_STATCAST:
7312	    postfix_expression
7313	      = build_static_cast (cp_cast_loc, type, expression,
7314				   tf_warning_or_error);
7315	    break;
7316	  case RID_REINTCAST:
7317	    postfix_expression
7318	      = build_reinterpret_cast (cp_cast_loc, type, expression,
7319                                        tf_warning_or_error);
7320	    break;
7321	  case RID_CONSTCAST:
7322	    postfix_expression
7323	      = build_const_cast (cp_cast_loc, type, expression,
7324				  tf_warning_or_error);
7325	    break;
7326	  default:
7327	    gcc_unreachable ();
7328	  }
7329      }
7330      break;
7331
7332    case RID_TYPEID:
7333      {
7334	tree type;
7335	const char *saved_message;
7336	bool saved_in_type_id_in_expr_p;
7337
7338	/* Consume the `typeid' token.  */
7339	cp_lexer_consume_token (parser->lexer);
7340	/* Look for the `(' token.  */
7341	matching_parens parens;
7342	parens.require_open (parser);
7343	/* Types cannot be defined in a `typeid' expression.  */
7344	saved_message = parser->type_definition_forbidden_message;
7345	parser->type_definition_forbidden_message
7346	  = G_("types may not be defined in a %<typeid%> expression");
7347	/* We can't be sure yet whether we're looking at a type-id or an
7348	   expression.  */
7349	cp_parser_parse_tentatively (parser);
7350	/* Try a type-id first.  */
7351	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7352	parser->in_type_id_in_expr_p = true;
7353	type = cp_parser_type_id (parser);
7354	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7355	/* Look for the `)' token.  Otherwise, we can't be sure that
7356	   we're not looking at an expression: consider `typeid (int
7357	   (3))', for example.  */
7358	cp_token *close_paren = parens.require_close (parser);
7359	/* If all went well, simply lookup the type-id.  */
7360	if (cp_parser_parse_definitely (parser))
7361	  postfix_expression = get_typeid (type, tf_warning_or_error);
7362	/* Otherwise, fall back to the expression variant.  */
7363	else
7364	  {
7365	    tree expression;
7366
7367	    /* Look for an expression.  */
7368	    expression = cp_parser_expression (parser, & idk);
7369	    /* Compute its typeid.  */
7370	    postfix_expression = build_typeid (expression, tf_warning_or_error);
7371	    /* Look for the `)' token.  */
7372	    close_paren = parens.require_close (parser);
7373	  }
7374	/* Restore the saved message.  */
7375	parser->type_definition_forbidden_message = saved_message;
7376	/* `typeid' may not appear in an integral constant expression.  */
7377	if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7378	  postfix_expression = error_mark_node;
7379
7380	/* Construct a location e.g. :
7381	     typeid (expr)
7382	     ^~~~~~~~~~~~~
7383	   ranging from the start of the "typeid" token to the final closing
7384	   paren, with the caret at the start.  */
7385	if (close_paren)
7386	  {
7387	    location_t typeid_loc
7388	      = make_location (start_loc, start_loc, close_paren->location);
7389	    postfix_expression.set_location (typeid_loc);
7390	    postfix_expression.maybe_add_location_wrapper ();
7391	  }
7392      }
7393      break;
7394
7395    case RID_TYPENAME:
7396      {
7397	tree type;
7398	/* The syntax permitted here is the same permitted for an
7399	   elaborated-type-specifier.  */
7400        ++parser->prevent_constrained_type_specifiers;
7401	type = cp_parser_elaborated_type_specifier (parser,
7402						    /*is_friend=*/false,
7403						    /*is_declaration=*/false);
7404        --parser->prevent_constrained_type_specifiers;
7405	postfix_expression = cp_parser_functional_cast (parser, type);
7406      }
7407      break;
7408
7409    case RID_ADDRESSOF:
7410    case RID_BUILTIN_SHUFFLE:
7411    case RID_BUILTIN_SHUFFLEVECTOR:
7412    case RID_BUILTIN_LAUNDER:
7413    case RID_BUILTIN_ASSOC_BARRIER:
7414      {
7415	vec<tree, va_gc> *vec;
7416
7417	cp_lexer_consume_token (parser->lexer);
7418	vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7419		    /*cast_p=*/false, /*allow_expansion_p=*/true,
7420		    /*non_constant_p=*/NULL);
7421	if (vec == NULL)
7422	  {
7423	    postfix_expression = error_mark_node;
7424	    break;
7425	  }
7426
7427	for (tree p : *vec)
7428	  mark_exp_read (p);
7429
7430	switch (keyword)
7431	  {
7432	  case RID_ADDRESSOF:
7433	    if (vec->length () == 1)
7434	      postfix_expression
7435		= cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7436	    else
7437	      {
7438		error_at (loc, "wrong number of arguments to "
7439			       "%<__builtin_addressof%>");
7440		postfix_expression = error_mark_node;
7441	      }
7442	    break;
7443
7444	  case RID_BUILTIN_LAUNDER:
7445	    if (vec->length () == 1)
7446	      postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7447							   tf_warning_or_error);
7448	    else
7449	      {
7450		error_at (loc, "wrong number of arguments to "
7451			       "%<__builtin_launder%>");
7452		postfix_expression = error_mark_node;
7453	      }
7454	    break;
7455
7456	  case RID_BUILTIN_ASSOC_BARRIER:
7457	    if (vec->length () == 1)
7458	      postfix_expression = build1_loc (loc, PAREN_EXPR,
7459					       TREE_TYPE ((*vec)[0]),
7460					       (*vec)[0]);
7461	    else
7462	      {
7463		error_at (loc, "wrong number of arguments to "
7464			       "%<__builtin_assoc_barrier%>");
7465		postfix_expression = error_mark_node;
7466	      }
7467	    break;
7468
7469	  case RID_BUILTIN_SHUFFLE:
7470	    if (vec->length () == 2)
7471	      postfix_expression
7472		= build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7473					 (*vec)[1], tf_warning_or_error);
7474	    else if (vec->length () == 3)
7475	      postfix_expression
7476		= build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7477					 (*vec)[2], tf_warning_or_error);
7478	    else
7479	      {
7480		error_at (loc, "wrong number of arguments to "
7481			       "%<__builtin_shuffle%>");
7482		postfix_expression = error_mark_node;
7483	      }
7484	    break;
7485
7486	  case RID_BUILTIN_SHUFFLEVECTOR:
7487	    if (vec->length () < 3)
7488	      {
7489		error_at (loc, "wrong number of arguments to "
7490			       "%<__builtin_shufflevector%>");
7491		postfix_expression = error_mark_node;
7492	      }
7493	    else
7494	      {
7495		postfix_expression
7496		  = build_x_shufflevector (loc, vec, tf_warning_or_error);
7497	      }
7498	    break;
7499
7500	  default:
7501	    gcc_unreachable ();
7502	  }
7503	break;
7504      }
7505
7506    case RID_BUILTIN_CONVERTVECTOR:
7507      {
7508	tree expression;
7509	tree type;
7510	/* Consume the `__builtin_convertvector' token.  */
7511	cp_lexer_consume_token (parser->lexer);
7512	/* Look for the opening `('.  */
7513	matching_parens parens;
7514	parens.require_open (parser);
7515	/* Now, parse the assignment-expression.  */
7516	expression = cp_parser_assignment_expression (parser);
7517	/* Look for the `,'.  */
7518	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7519	location_t type_location
7520	  = cp_lexer_peek_token (parser->lexer)->location;
7521	/* Parse the type-id.  */
7522	{
7523	  type_id_in_expr_sentinel s (parser);
7524	  type = cp_parser_type_id (parser);
7525	}
7526	/* Look for the closing `)'.  */
7527	parens.require_close (parser);
7528	postfix_expression
7529	  = cp_build_vec_convert (expression, type_location, type,
7530				  tf_warning_or_error);
7531	break;
7532      }
7533
7534    case RID_BUILTIN_BIT_CAST:
7535      {
7536	tree expression;
7537	tree type;
7538	/* Consume the `__builtin_bit_cast' token.  */
7539	cp_lexer_consume_token (parser->lexer);
7540	/* Look for the opening `('.  */
7541	matching_parens parens;
7542	parens.require_open (parser);
7543	location_t type_location
7544	  = cp_lexer_peek_token (parser->lexer)->location;
7545	/* Parse the type-id.  */
7546	{
7547	  type_id_in_expr_sentinel s (parser);
7548	  type = cp_parser_type_id (parser);
7549	}
7550	/* Look for the `,'.  */
7551	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7552	/* Now, parse the assignment-expression.  */
7553	expression = cp_parser_assignment_expression (parser);
7554	/* Look for the closing `)'.  */
7555	parens.require_close (parser);
7556	postfix_expression
7557	  = cp_build_bit_cast (type_location, type, expression,
7558			       tf_warning_or_error);
7559	break;
7560      }
7561
7562    default:
7563      {
7564	tree type;
7565
7566	/* If the next thing is a simple-type-specifier, we may be
7567	   looking at a functional cast.  We could also be looking at
7568	   an id-expression.  So, we try the functional cast, and if
7569	   that doesn't work we fall back to the primary-expression.  */
7570	cp_parser_parse_tentatively (parser);
7571	/* Look for the simple-type-specifier.  */
7572        ++parser->prevent_constrained_type_specifiers;
7573	type = cp_parser_simple_type_specifier (parser,
7574						/*decl_specs=*/NULL,
7575						CP_PARSER_FLAGS_NONE);
7576        --parser->prevent_constrained_type_specifiers;
7577	/* Parse the cast itself.  */
7578	if (!cp_parser_error_occurred (parser))
7579	  postfix_expression
7580	    = cp_parser_functional_cast (parser, type);
7581	/* If that worked, we're done.  */
7582	if (cp_parser_parse_definitely (parser))
7583	  break;
7584
7585	/* If the functional-cast didn't work out, try a
7586	   compound-literal.  */
7587	if (cp_parser_allow_gnu_extensions_p (parser)
7588	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7589	  {
7590	    cp_expr initializer = NULL_TREE;
7591
7592	    cp_parser_parse_tentatively (parser);
7593
7594	    matching_parens parens;
7595	    parens.consume_open (parser);
7596
7597	    /* Avoid calling cp_parser_type_id pointlessly, see comment
7598	       in cp_parser_cast_expression about c++/29234.  */
7599	    if (!cp_parser_compound_literal_p (parser))
7600	      cp_parser_simulate_error (parser);
7601	    else
7602	      {
7603		/* Parse the type.  */
7604		bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7605		parser->in_type_id_in_expr_p = true;
7606		type = cp_parser_type_id (parser);
7607		parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7608		parens.require_close (parser);
7609	      }
7610
7611	    /* If things aren't going well, there's no need to
7612	       keep going.  */
7613	    if (!cp_parser_error_occurred (parser))
7614	      {
7615		bool non_constant_p;
7616		/* Parse the brace-enclosed initializer list.  */
7617		initializer = cp_parser_braced_list (parser,
7618						     &non_constant_p);
7619	      }
7620	    /* If that worked, we're definitely looking at a
7621	       compound-literal expression.  */
7622	    if (cp_parser_parse_definitely (parser))
7623	      {
7624		/* Warn the user that a compound literal is not
7625		   allowed in standard C++.  */
7626		pedwarn (input_location, OPT_Wpedantic,
7627			 "ISO C++ forbids compound-literals");
7628		/* For simplicity, we disallow compound literals in
7629		   constant-expressions.  We could
7630		   allow compound literals of integer type, whose
7631		   initializer was a constant, in constant
7632		   expressions.  Permitting that usage, as a further
7633		   extension, would not change the meaning of any
7634		   currently accepted programs.  (Of course, as
7635		   compound literals are not part of ISO C++, the
7636		   standard has nothing to say.)  */
7637		if (cp_parser_non_integral_constant_expression (parser,
7638								NIC_NCC))
7639		  {
7640		    postfix_expression = error_mark_node;
7641		    break;
7642		  }
7643		/* Form the representation of the compound-literal.  */
7644		postfix_expression
7645		  = finish_compound_literal (type, initializer,
7646					     tf_warning_or_error, fcl_c99);
7647		postfix_expression.set_location (initializer.get_location ());
7648		break;
7649	      }
7650	  }
7651
7652	/* It must be a primary-expression.  */
7653	postfix_expression
7654	  = cp_parser_primary_expression (parser, address_p, cast_p,
7655					  /*template_arg_p=*/false,
7656					  decltype_p,
7657					  &idk);
7658      }
7659      break;
7660    }
7661
7662  /* Note that we don't need to worry about calling build_cplus_new on a
7663     class-valued CALL_EXPR in decltype when it isn't the end of the
7664     postfix-expression; unary_complex_lvalue will take care of that for
7665     all these cases.  */
7666
7667  /* Keep looping until the postfix-expression is complete.  */
7668  while (true)
7669    {
7670      if (idk == CP_ID_KIND_UNQUALIFIED
7671	  && identifier_p (postfix_expression)
7672	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7673	/* It is not a Koenig lookup function call.  */
7674	postfix_expression
7675	  = unqualified_name_lookup_error (postfix_expression);
7676
7677      /* Peek at the next token.  */
7678      token = cp_lexer_peek_token (parser->lexer);
7679
7680      switch (token->type)
7681	{
7682	case CPP_OPEN_SQUARE:
7683	  if (cp_next_tokens_can_be_std_attribute_p (parser))
7684	    {
7685	      cp_parser_error (parser,
7686			       "two consecutive %<[%> shall "
7687			       "only introduce an attribute");
7688	      return error_mark_node;
7689	    }
7690	  postfix_expression
7691	    = cp_parser_postfix_open_square_expression (parser,
7692							postfix_expression,
7693							false,
7694							decltype_p);
7695	  postfix_expression.set_range (start_loc,
7696					postfix_expression.get_location ());
7697
7698	  idk = CP_ID_KIND_NONE;
7699          is_member_access = false;
7700	  break;
7701
7702	case CPP_OPEN_PAREN:
7703	  /* postfix-expression ( expression-list [opt] ) */
7704	  {
7705	    bool koenig_p;
7706	    bool is_builtin_constant_p;
7707	    bool saved_integral_constant_expression_p = false;
7708	    bool saved_non_integral_constant_expression_p = false;
7709	    tsubst_flags_t complain = complain_flags (decltype_p);
7710	    vec<tree, va_gc> *args;
7711	    location_t close_paren_loc = UNKNOWN_LOCATION;
7712	    location_t combined_loc = UNKNOWN_LOCATION;
7713
7714            is_member_access = false;
7715
7716	    tree stripped_expression
7717	      = tree_strip_any_location_wrapper (postfix_expression);
7718	    is_builtin_constant_p
7719	      = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7720	    if (is_builtin_constant_p)
7721	      {
7722		/* The whole point of __builtin_constant_p is to allow
7723		   non-constant expressions to appear as arguments.  */
7724		saved_integral_constant_expression_p
7725		  = parser->integral_constant_expression_p;
7726		saved_non_integral_constant_expression_p
7727		  = parser->non_integral_constant_expression_p;
7728		parser->integral_constant_expression_p = false;
7729	      }
7730	    args = (cp_parser_parenthesized_expression_list
7731		    (parser, non_attr,
7732		     /*cast_p=*/false, /*allow_expansion_p=*/true,
7733		     /*non_constant_p=*/NULL,
7734		     /*close_paren_loc=*/&close_paren_loc,
7735		     /*wrap_locations_p=*/true));
7736	    if (is_builtin_constant_p)
7737	      {
7738		parser->integral_constant_expression_p
7739		  = saved_integral_constant_expression_p;
7740		parser->non_integral_constant_expression_p
7741		  = saved_non_integral_constant_expression_p;
7742	      }
7743
7744	    if (args == NULL)
7745	      {
7746		postfix_expression = error_mark_node;
7747		break;
7748	      }
7749
7750	    /* Function calls are not permitted in
7751	       constant-expressions.  */
7752	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
7753		&& cp_parser_non_integral_constant_expression (parser,
7754							       NIC_FUNC_CALL))
7755	      {
7756		postfix_expression = error_mark_node;
7757		release_tree_vector (args);
7758		break;
7759	      }
7760
7761	    koenig_p = false;
7762	    if (idk == CP_ID_KIND_UNQUALIFIED
7763		|| idk == CP_ID_KIND_TEMPLATE_ID)
7764	      {
7765		if (identifier_p (postfix_expression)
7766		    /* In C++20, we may need to perform ADL for a template
7767		       name.  */
7768		    || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7769			&& identifier_p (TREE_OPERAND (postfix_expression, 0))))
7770		  {
7771		    if (!args->is_empty ())
7772		      {
7773			koenig_p = true;
7774			if (!any_type_dependent_arguments_p (args))
7775			  postfix_expression
7776			    = perform_koenig_lookup (postfix_expression, args,
7777						     complain);
7778		      }
7779		    else
7780		      postfix_expression
7781			= unqualified_fn_lookup_error (postfix_expression);
7782		  }
7783		/* We do not perform argument-dependent lookup if
7784		   normal lookup finds a non-function, in accordance
7785		   with the expected resolution of DR 218.  */
7786		else if (!args->is_empty ()
7787			 && is_overloaded_fn (postfix_expression))
7788		  {
7789		    /* Do not do argument dependent lookup if regular
7790		       lookup finds a member function or a block-scope
7791		       function declaration.  [basic.lookup.argdep]/3  */
7792		    bool do_adl_p = true;
7793		    tree fns = get_fns (postfix_expression);
7794		    for (lkp_iterator iter (fns); iter; ++iter)
7795		      {
7796			tree fn = STRIP_TEMPLATE (*iter);
7797			if ((TREE_CODE (fn) == USING_DECL
7798			     && DECL_DEPENDENT_P (fn))
7799			    || DECL_FUNCTION_MEMBER_P (fn)
7800			    || DECL_LOCAL_DECL_P (fn))
7801			  {
7802			    do_adl_p = false;
7803			    break;
7804			  }
7805		      }
7806
7807		    if (do_adl_p)
7808		      {
7809			koenig_p = true;
7810			if (!any_type_dependent_arguments_p (args))
7811			  postfix_expression
7812			    = perform_koenig_lookup (postfix_expression, args,
7813						     complain);
7814		      }
7815		  }
7816	      }
7817
7818	    /* Temporarily set input_location to the combined location
7819	       with call expression range, as e.g. build_out_target_exprs
7820	       called from convert_default_arg relies on input_location,
7821	       so updating it only when the call is fully built results
7822	       in inconsistencies between location handling in templates
7823	       and outside of templates.  */
7824	    if (close_paren_loc != UNKNOWN_LOCATION)
7825	      combined_loc = make_location (token->location, start_loc,
7826					    close_paren_loc);
7827	    iloc_sentinel ils (combined_loc);
7828
7829	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7830	      {
7831		tree instance = TREE_OPERAND (postfix_expression, 0);
7832		tree fn = TREE_OPERAND (postfix_expression, 1);
7833
7834		if (processing_template_decl
7835		    && (type_dependent_object_expression_p (instance)
7836			|| (!BASELINK_P (fn)
7837			    && TREE_CODE (fn) != FIELD_DECL)
7838			|| type_dependent_expression_p (fn)
7839			|| any_type_dependent_arguments_p (args)))
7840		  {
7841		    maybe_generic_this_capture (instance, fn);
7842		    postfix_expression
7843		      = build_min_nt_call_vec (postfix_expression, args);
7844		  }
7845		else if (BASELINK_P (fn))
7846		  {
7847		  postfix_expression
7848		    = (build_new_method_call
7849		       (instance, fn, &args, NULL_TREE,
7850			(idk == CP_ID_KIND_QUALIFIED
7851			 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7852			 : LOOKUP_NORMAL),
7853			/*fn_p=*/NULL,
7854			complain));
7855		  }
7856		else
7857		  postfix_expression
7858		    = finish_call_expr (postfix_expression, &args,
7859					/*disallow_virtual=*/false,
7860					/*koenig_p=*/false,
7861					complain);
7862	      }
7863	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
7864		     || TREE_CODE (postfix_expression) == MEMBER_REF
7865		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7866	      postfix_expression = (build_offset_ref_call_from_tree
7867				    (postfix_expression, &args,
7868				     complain));
7869	    else if (idk == CP_ID_KIND_QUALIFIED)
7870	      /* A call to a static class member, or a namespace-scope
7871		 function.  */
7872	      postfix_expression
7873		= finish_call_expr (postfix_expression, &args,
7874				    /*disallow_virtual=*/true,
7875				    koenig_p,
7876				    complain);
7877	    else
7878	      /* All other function calls.  */
7879	      postfix_expression
7880		= finish_call_expr (postfix_expression, &args,
7881				    /*disallow_virtual=*/false,
7882				    koenig_p,
7883				    complain);
7884
7885	    if (close_paren_loc != UNKNOWN_LOCATION)
7886	      postfix_expression.set_location (combined_loc);
7887
7888	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
7889	    idk = CP_ID_KIND_NONE;
7890
7891	    release_tree_vector (args);
7892	  }
7893	  break;
7894
7895	case CPP_DOT:
7896	case CPP_DEREF:
7897	  /* postfix-expression . template [opt] id-expression
7898	     postfix-expression . pseudo-destructor-name
7899	     postfix-expression -> template [opt] id-expression
7900	     postfix-expression -> pseudo-destructor-name */
7901
7902	  /* Consume the `.' or `->' operator.  */
7903	  cp_lexer_consume_token (parser->lexer);
7904
7905	  postfix_expression
7906	    = cp_parser_postfix_dot_deref_expression (parser, token->type,
7907						      postfix_expression,
7908						      false, &idk, loc);
7909
7910          is_member_access = true;
7911	  break;
7912
7913	case CPP_PLUS_PLUS:
7914	  /* postfix-expression ++  */
7915	  /* Consume the `++' token.  */
7916	  cp_lexer_consume_token (parser->lexer);
7917	  /* Generate a representation for the complete expression.  */
7918	  postfix_expression
7919	    = finish_increment_expr (postfix_expression,
7920				     POSTINCREMENT_EXPR);
7921	  /* Increments may not appear in constant-expressions.  */
7922	  if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7923	    postfix_expression = error_mark_node;
7924	  idk = CP_ID_KIND_NONE;
7925          is_member_access = false;
7926	  break;
7927
7928	case CPP_MINUS_MINUS:
7929	  /* postfix-expression -- */
7930	  /* Consume the `--' token.  */
7931	  cp_lexer_consume_token (parser->lexer);
7932	  /* Generate a representation for the complete expression.  */
7933	  postfix_expression
7934	    = finish_increment_expr (postfix_expression,
7935				     POSTDECREMENT_EXPR);
7936	  /* Decrements may not appear in constant-expressions.  */
7937	  if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7938	    postfix_expression = error_mark_node;
7939	  idk = CP_ID_KIND_NONE;
7940          is_member_access = false;
7941	  break;
7942
7943	default:
7944	  if (pidk_return != NULL)
7945	    * pidk_return = idk;
7946          if (member_access_only_p)
7947            return is_member_access
7948              ? postfix_expression
7949              : cp_expr (error_mark_node);
7950          else
7951            return postfix_expression;
7952	}
7953    }
7954}
7955
7956/* Helper function for cp_parser_parenthesized_expression_list and
7957   cp_parser_postfix_open_square_expression.  Parse a single element
7958   of parenthesized expression list.  */
7959
7960static cp_expr
7961cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
7962					     bool allow_expansion_p,
7963					     bool *non_constant_p)
7964{
7965  cp_expr expr (NULL_TREE);
7966  bool expr_non_constant_p;
7967
7968  /* Parse the next assignment-expression.  */
7969  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7970    {
7971      /* A braced-init-list.  */
7972      cp_lexer_set_source_position (parser->lexer);
7973      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7974      expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7975      if (non_constant_p && expr_non_constant_p)
7976	*non_constant_p = true;
7977    }
7978  else if (non_constant_p)
7979    {
7980      expr = cp_parser_constant_expression (parser,
7981					    /*allow_non_constant_p=*/true,
7982					    &expr_non_constant_p);
7983      if (expr_non_constant_p)
7984	*non_constant_p = true;
7985    }
7986  else
7987    expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
7988
7989  /* If we have an ellipsis, then this is an expression expansion.  */
7990  if (allow_expansion_p
7991      && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7992    {
7993      /* Consume the `...'.  */
7994      cp_lexer_consume_token (parser->lexer);
7995
7996      /* Build the argument pack.  */
7997      expr = make_pack_expansion (expr);
7998    }
7999  return expr;
8000}
8001
8002/* A subroutine of cp_parser_postfix_expression that also gets hijacked
8003   by cp_parser_builtin_offsetof.  We're looking for
8004
8005     postfix-expression [ expression ]
8006     postfix-expression [ braced-init-list ] (C++11)
8007     postfix-expression [ expression-list[opt] ] (C++23)
8008
8009   FOR_OFFSETOF is set if we're being called in that context, which
8010   changes how we deal with integer constant expressions.  */
8011
8012static tree
8013cp_parser_postfix_open_square_expression (cp_parser *parser,
8014					  tree postfix_expression,
8015					  bool for_offsetof,
8016					  bool decltype_p)
8017{
8018  tree index = NULL_TREE;
8019  releasing_vec expression_list = NULL;
8020  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8021  bool saved_greater_than_is_operator_p;
8022
8023  /* Consume the `[' token.  */
8024  cp_lexer_consume_token (parser->lexer);
8025
8026  saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8027  parser->greater_than_is_operator_p = true;
8028
8029  /* Parse the index expression.  */
8030  /* ??? For offsetof, there is a question of what to allow here.  If
8031     offsetof is not being used in an integral constant expression context,
8032     then we *could* get the right answer by computing the value at runtime.
8033     If we are in an integral constant expression context, then we might
8034     could accept any constant expression; hard to say without analysis.
8035     Rather than open the barn door too wide right away, allow only integer
8036     constant expressions here.  */
8037  if (for_offsetof)
8038    index = cp_parser_constant_expression (parser);
8039  else
8040    {
8041      if (cxx_dialect >= cxx23
8042	  && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8043	*&expression_list = make_tree_vector ();
8044      else if (cxx_dialect >= cxx23)
8045	{
8046	  while (true)
8047	    {
8048	      cp_expr expr
8049		= cp_parser_parenthesized_expression_list_elt (parser,
8050							       /*cast_p=*/
8051							       false,
8052							       /*allow_exp_p=*/
8053							       true,
8054							       /*non_cst_p=*/
8055							       NULL);
8056
8057	      if (expr == error_mark_node)
8058		index = error_mark_node;
8059	      else if (expression_list.get () == NULL
8060		       && !PACK_EXPANSION_P (expr.get_value ()))
8061		index = expr.get_value ();
8062	      else
8063		vec_safe_push (expression_list, expr.get_value ());
8064
8065	      /* If the next token isn't a `,', then we are done.  */
8066	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8067		break;
8068
8069	      if (expression_list.get () == NULL && index != error_mark_node)
8070		{
8071		  *&expression_list = make_tree_vector_single (index);
8072		  index = NULL_TREE;
8073		}
8074
8075	      /* Otherwise, consume the `,' and keep going.  */
8076	      cp_lexer_consume_token (parser->lexer);
8077	    }
8078	  if (expression_list.get () && index == error_mark_node)
8079	    expression_list.release ();
8080	}
8081      else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8082	{
8083	  bool expr_nonconst_p;
8084	  cp_lexer_set_source_position (parser->lexer);
8085	  maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8086	  index = cp_parser_braced_list (parser, &expr_nonconst_p);
8087	}
8088      else
8089	index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8090				      /*decltype_p=*/false,
8091				      /*warn_comma_p=*/warn_comma_subscript);
8092    }
8093
8094  parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8095
8096  /* Look for the closing `]'.  */
8097  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8098
8099  /* Build the ARRAY_REF.  */
8100  postfix_expression = grok_array_decl (loc, postfix_expression,
8101					index, &expression_list,
8102					tf_warning_or_error
8103					| (decltype_p ? tf_decltype : 0));
8104
8105  /* When not doing offsetof, array references are not permitted in
8106     constant-expressions.  */
8107  if (!for_offsetof
8108      && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8109    postfix_expression = error_mark_node;
8110
8111  return postfix_expression;
8112}
8113
8114/* A subroutine of cp_parser_postfix_dot_deref_expression.  Handle dot
8115   dereference of incomplete type, returns true if error_mark_node should
8116   be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8117   and *DEPENDENT_P.  */
8118
8119bool
8120cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8121				bool *dependent_p)
8122{
8123  /* In a template, be permissive by treating an object expression
8124     of incomplete type as dependent (after a pedwarn).  */
8125  diagnostic_t kind = (processing_template_decl
8126		       && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8127
8128  switch (TREE_CODE (*postfix_expression))
8129    {
8130    case CAST_EXPR:
8131    case REINTERPRET_CAST_EXPR:
8132    case CONST_CAST_EXPR:
8133    case STATIC_CAST_EXPR:
8134    case DYNAMIC_CAST_EXPR:
8135    case IMPLICIT_CONV_EXPR:
8136    case VIEW_CONVERT_EXPR:
8137    case NON_LVALUE_EXPR:
8138      kind = DK_ERROR;
8139      break;
8140    case OVERLOAD:
8141      /* Don't emit any diagnostic for OVERLOADs.  */
8142      kind = DK_IGNORED;
8143      break;
8144    default:
8145      /* Avoid clobbering e.g. DECLs.  */
8146      if (!EXPR_P (*postfix_expression))
8147	kind = DK_ERROR;
8148      break;
8149    }
8150
8151  if (kind == DK_IGNORED)
8152    return false;
8153
8154  location_t exploc = location_of (*postfix_expression);
8155  cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8156  if (!MAYBE_CLASS_TYPE_P (*scope))
8157    return true;
8158  if (kind == DK_ERROR)
8159    *scope = *postfix_expression = error_mark_node;
8160  else if (processing_template_decl)
8161    {
8162      *dependent_p = true;
8163      *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8164    }
8165  return false;
8166}
8167
8168/* A subroutine of cp_parser_postfix_expression that also gets hijacked
8169   by cp_parser_builtin_offsetof.  We're looking for
8170
8171     postfix-expression . template [opt] id-expression
8172     postfix-expression . pseudo-destructor-name
8173     postfix-expression -> template [opt] id-expression
8174     postfix-expression -> pseudo-destructor-name
8175
8176   FOR_OFFSETOF is set if we're being called in that context.  That sorta
8177   limits what of the above we'll actually accept, but nevermind.
8178   TOKEN_TYPE is the "." or "->" token, which will already have been
8179   removed from the stream.  */
8180
8181static tree
8182cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8183					enum cpp_ttype token_type,
8184					cp_expr postfix_expression,
8185					bool for_offsetof, cp_id_kind *idk,
8186					location_t location)
8187{
8188  tree name;
8189  bool dependent_p;
8190  bool pseudo_destructor_p;
8191  tree scope = NULL_TREE;
8192  location_t start_loc = postfix_expression.get_start ();
8193
8194  /* If this is a `->' operator, dereference the pointer.  */
8195  if (token_type == CPP_DEREF)
8196    postfix_expression = build_x_arrow (location, postfix_expression,
8197					tf_warning_or_error);
8198  /* Check to see whether or not the expression is type-dependent and
8199     not the current instantiation.  */
8200  dependent_p = type_dependent_object_expression_p (postfix_expression);
8201  /* The identifier following the `->' or `.' is not qualified.  */
8202  parser->scope = NULL_TREE;
8203  parser->qualifying_scope = NULL_TREE;
8204  parser->object_scope = NULL_TREE;
8205  *idk = CP_ID_KIND_NONE;
8206
8207  /* Enter the scope corresponding to the type of the object
8208     given by the POSTFIX_EXPRESSION.  */
8209  if (!dependent_p)
8210    {
8211      scope = TREE_TYPE (postfix_expression);
8212      /* According to the standard, no expression should ever have
8213	 reference type.  Unfortunately, we do not currently match
8214	 the standard in this respect in that our internal representation
8215	 of an expression may have reference type even when the standard
8216	 says it does not.  Therefore, we have to manually obtain the
8217	 underlying type here.  */
8218      scope = non_reference (scope);
8219      /* The type of the POSTFIX_EXPRESSION must be complete.  */
8220      /* Unlike the object expression in other contexts, *this is not
8221	 required to be of complete type for purposes of class member
8222	 access (5.2.5) outside the member function body.  */
8223      if (postfix_expression != current_class_ref
8224	  && scope != error_mark_node
8225	  && !currently_open_class (scope))
8226	{
8227	  scope = complete_type (scope);
8228	  if (!COMPLETE_TYPE_P (scope)
8229	      && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8230						 &dependent_p))
8231	    return error_mark_node;
8232	}
8233
8234      if (!dependent_p)
8235	{
8236	  /* Let the name lookup machinery know that we are processing a
8237	     class member access expression.  */
8238	  parser->context->object_type = scope;
8239	  /* If something went wrong, we want to be able to discern that case,
8240	     as opposed to the case where there was no SCOPE due to the type
8241	     of expression being dependent.  */
8242	  if (!scope)
8243	    scope = error_mark_node;
8244	  /* If the SCOPE was erroneous, make the various semantic analysis
8245	     functions exit quickly -- and without issuing additional error
8246	     messages.  */
8247	  if (scope == error_mark_node)
8248	    postfix_expression = error_mark_node;
8249	}
8250    }
8251
8252  if (dependent_p)
8253    {
8254      tree type = TREE_TYPE (postfix_expression);
8255      /* If we don't have a (type-dependent) object of class type, use
8256	 typeof to figure out the type of the object.  */
8257      if (type == NULL_TREE || is_auto (type))
8258	type = finish_typeof (postfix_expression);
8259      parser->context->object_type = type;
8260    }
8261
8262  /* Assume this expression is not a pseudo-destructor access.  */
8263  pseudo_destructor_p = false;
8264
8265  /* If the SCOPE is a scalar type, then, if this is a valid program,
8266     we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
8267     is type dependent, it can be pseudo-destructor-name or something else.
8268     Try to parse it as pseudo-destructor-name first.  */
8269  if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8270    {
8271      tree s;
8272      tree type;
8273
8274      cp_parser_parse_tentatively (parser);
8275      /* Parse the pseudo-destructor-name.  */
8276      s = NULL_TREE;
8277      cp_parser_pseudo_destructor_name (parser, postfix_expression,
8278					&s, &type);
8279      if (dependent_p
8280	  && (cp_parser_error_occurred (parser)
8281	      || !SCALAR_TYPE_P (type)))
8282	cp_parser_abort_tentative_parse (parser);
8283      else if (cp_parser_parse_definitely (parser))
8284	{
8285	  pseudo_destructor_p = true;
8286	  postfix_expression
8287	    = finish_pseudo_destructor_expr (postfix_expression,
8288					     s, type, location);
8289	}
8290    }
8291
8292  if (!pseudo_destructor_p)
8293    {
8294      /* If the SCOPE is not a scalar type, we are looking at an
8295	 ordinary class member access expression, rather than a
8296	 pseudo-destructor-name.  */
8297      bool template_p;
8298      cp_token *token = cp_lexer_peek_token (parser->lexer);
8299      /* Parse the id-expression.  */
8300      name = (cp_parser_id_expression
8301	      (parser,
8302	       cp_parser_optional_template_keyword (parser),
8303	       /*check_dependency_p=*/true,
8304	       &template_p,
8305	       /*declarator_p=*/false,
8306	       /*optional_p=*/false));
8307      /* In general, build a SCOPE_REF if the member name is qualified.
8308	 However, if the name was not dependent and has already been
8309	 resolved; there is no need to build the SCOPE_REF.  For example;
8310
8311	     struct X { void f(); };
8312	     template <typename T> void f(T* t) { t->X::f(); }
8313
8314	 Even though "t" is dependent, "X::f" is not and has been resolved
8315	 to a BASELINK; there is no need to include scope information.  */
8316
8317      /* But we do need to remember that there was an explicit scope for
8318	 virtual function calls.  */
8319      if (parser->scope)
8320	*idk = CP_ID_KIND_QUALIFIED;
8321
8322      /* If the name is a template-id that names a type, we will get a
8323	 TYPE_DECL here.  That is invalid code.  */
8324      if (TREE_CODE (name) == TYPE_DECL)
8325	{
8326	  error_at (token->location, "invalid use of %qD", name);
8327	  postfix_expression = error_mark_node;
8328	}
8329      else
8330	{
8331	  if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8332	    {
8333	      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8334		{
8335		  error_at (token->location, "%<%D::%D%> is not a class member",
8336			    parser->scope, name);
8337		  postfix_expression = error_mark_node;
8338		}
8339	      else
8340		name = build_qualified_name (/*type=*/NULL_TREE,
8341					     parser->scope,
8342					     name,
8343					     template_p);
8344	      parser->scope = NULL_TREE;
8345	      parser->qualifying_scope = NULL_TREE;
8346	      parser->object_scope = NULL_TREE;
8347	    }
8348	  if (parser->scope && name && BASELINK_P (name))
8349	    adjust_result_of_qualified_name_lookup
8350	      (name, parser->scope, scope);
8351	  postfix_expression
8352	    = finish_class_member_access_expr (postfix_expression, name,
8353					       template_p,
8354					       tf_warning_or_error);
8355	  /* Build a location e.g.:
8356	       ptr->access_expr
8357	       ~~~^~~~~~~~~~~~~
8358	     where the caret is at the deref token, ranging from
8359	     the start of postfix_expression to the end of the access expr.  */
8360	  location_t combined_loc
8361	    = make_location (input_location, start_loc, parser->lexer);
8362	  protected_set_expr_location (postfix_expression, combined_loc);
8363	}
8364    }
8365
8366  /* We no longer need to look up names in the scope of the object on
8367     the left-hand side of the `.' or `->' operator.  */
8368  parser->context->object_type = NULL_TREE;
8369
8370  /* Outside of offsetof, these operators may not appear in
8371     constant-expressions.  */
8372  if (!for_offsetof
8373      && (cp_parser_non_integral_constant_expression
8374	  (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8375    postfix_expression = error_mark_node;
8376
8377  return postfix_expression;
8378}
8379
8380/* Parse a parenthesized expression-list.
8381
8382   expression-list:
8383     assignment-expression
8384     expression-list, assignment-expression
8385
8386   attribute-list:
8387     expression-list
8388     identifier
8389     identifier, expression-list
8390
8391   CAST_P is true if this expression is the target of a cast.
8392
8393   ALLOW_EXPANSION_P is true if this expression allows expansion of an
8394   argument pack.
8395
8396   WRAP_LOCATIONS_P is true if expressions within this list for which
8397   CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8398   their source locations.
8399
8400   Returns a vector of trees.  Each element is a representation of an
8401   assignment-expression.  NULL is returned if the ( and or ) are
8402   missing.  An empty, but allocated, vector is returned on no
8403   expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
8404   if we are parsing an attribute list for an attribute that wants a
8405   plain identifier argument, normal_attr for an attribute that wants
8406   an expression, or non_attr if we aren't parsing an attribute list.  If
8407   NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8408   not all of the expressions in the list were constant.
8409   If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8410   will be written to with the location of the closing parenthesis.  If
8411   an error occurs, it may or may not be written to.  */
8412
8413static vec<tree, va_gc> *
8414cp_parser_parenthesized_expression_list (cp_parser* parser,
8415					 int is_attribute_list,
8416					 bool cast_p,
8417                                         bool allow_expansion_p,
8418					 bool *non_constant_p,
8419					 location_t *close_paren_loc,
8420					 bool wrap_locations_p)
8421{
8422  vec<tree, va_gc> *expression_list;
8423  tree identifier = NULL_TREE;
8424  bool saved_greater_than_is_operator_p;
8425
8426  /* Assume all the expressions will be constant.  */
8427  if (non_constant_p)
8428    *non_constant_p = false;
8429
8430  matching_parens parens;
8431  if (!parens.require_open (parser))
8432    return NULL;
8433
8434  expression_list = make_tree_vector ();
8435
8436  /* Within a parenthesized expression, a `>' token is always
8437     the greater-than operator.  */
8438  saved_greater_than_is_operator_p
8439    = parser->greater_than_is_operator_p;
8440  parser->greater_than_is_operator_p = true;
8441
8442  cp_expr expr (NULL_TREE);
8443
8444  /* Consume expressions until there are no more.  */
8445  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8446    while (true)
8447      {
8448	/* At the beginning of attribute lists, check to see if the
8449	   next token is an identifier.  */
8450	if (is_attribute_list == id_attr
8451	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8452	  {
8453	    cp_token *token;
8454
8455	    /* Consume the identifier.  */
8456	    token = cp_lexer_consume_token (parser->lexer);
8457	    /* Save the identifier.  */
8458	    identifier = token->u.value;
8459	  }
8460	else
8461	  {
8462	    expr
8463	      = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8464							     allow_expansion_p,
8465							     non_constant_p);
8466
8467	    if (wrap_locations_p)
8468	      expr.maybe_add_location_wrapper ();
8469
8470	     /* Add it to the list.  We add error_mark_node
8471		expressions to the list, so that we can still tell if
8472		the correct form for a parenthesized expression-list
8473		is found. That gives better errors.  */
8474	    vec_safe_push (expression_list, expr.get_value ());
8475
8476	    if (expr == error_mark_node)
8477	      goto skip_comma;
8478	  }
8479
8480	/* After the first item, attribute lists look the same as
8481	   expression lists.  */
8482	is_attribute_list = non_attr;
8483
8484      get_comma:;
8485	/* If the next token isn't a `,', then we are done.  */
8486	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8487	  break;
8488
8489	/* Otherwise, consume the `,' and keep going.  */
8490	cp_lexer_consume_token (parser->lexer);
8491      }
8492
8493  if (close_paren_loc)
8494    *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8495
8496  if (!parens.require_close (parser))
8497    {
8498      int ending;
8499
8500    skip_comma:;
8501      /* We try and resync to an unnested comma, as that will give the
8502	 user better diagnostics.  */
8503      ending = cp_parser_skip_to_closing_parenthesis (parser,
8504						      /*recovering=*/true,
8505						      /*or_comma=*/true,
8506						      /*consume_paren=*/true);
8507      if (ending < 0)
8508	goto get_comma;
8509      if (!ending)
8510	{
8511	  parser->greater_than_is_operator_p
8512	    = saved_greater_than_is_operator_p;
8513	  return NULL;
8514	}
8515    }
8516
8517  parser->greater_than_is_operator_p
8518    = saved_greater_than_is_operator_p;
8519
8520  if (identifier)
8521    vec_safe_insert (expression_list, 0, identifier);
8522
8523  return expression_list;
8524}
8525
8526/* Parse a pseudo-destructor-name.
8527
8528   pseudo-destructor-name:
8529     :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8530     :: [opt] nested-name-specifier template template-id :: ~ type-name
8531     :: [opt] nested-name-specifier [opt] ~ type-name
8532
8533   If either of the first two productions is used, sets *SCOPE to the
8534   TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
8535   NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
8536   or ERROR_MARK_NODE if the parse fails.  */
8537
8538static void
8539cp_parser_pseudo_destructor_name (cp_parser* parser,
8540				  tree object,
8541				  tree* scope,
8542				  tree* type)
8543{
8544  bool nested_name_specifier_p;
8545
8546  /* Handle ~auto.  */
8547  if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8548      && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8549      && !type_dependent_expression_p (object))
8550    {
8551      if (cxx_dialect < cxx14)
8552	pedwarn (input_location, OPT_Wc__14_extensions,
8553		 "%<~auto%> only available with "
8554		 "%<-std=c++14%> or %<-std=gnu++14%>");
8555      cp_lexer_consume_token (parser->lexer);
8556      cp_lexer_consume_token (parser->lexer);
8557      *scope = NULL_TREE;
8558      *type = TREE_TYPE (object);
8559      return;
8560    }
8561
8562  /* Assume that things will not work out.  */
8563  *type = error_mark_node;
8564
8565  /* Look for the optional `::' operator.  */
8566  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8567  /* Look for the optional nested-name-specifier.  */
8568  nested_name_specifier_p
8569    = (cp_parser_nested_name_specifier_opt (parser,
8570					    /*typename_keyword_p=*/false,
8571					    /*check_dependency_p=*/true,
8572					    /*type_p=*/false,
8573					    /*is_declaration=*/false)
8574       != NULL_TREE);
8575  /* Now, if we saw a nested-name-specifier, we might be doing the
8576     second production.  */
8577  if (nested_name_specifier_p
8578      && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8579    {
8580      /* Consume the `template' keyword.  */
8581      cp_lexer_consume_token (parser->lexer);
8582      /* Parse the template-id.  */
8583      cp_parser_template_id (parser,
8584			     /*template_keyword_p=*/true,
8585			     /*check_dependency_p=*/false,
8586			     class_type,
8587			     /*is_declaration=*/true);
8588      /* Look for the `::' token.  */
8589      cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8590    }
8591  /* If the next token is not a `~', then there might be some
8592     additional qualification.  */
8593  else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8594    {
8595      /* At this point, we're looking for "type-name :: ~".  The type-name
8596	 must not be a class-name, since this is a pseudo-destructor.  So,
8597	 it must be either an enum-name, or a typedef-name -- both of which
8598	 are just identifiers.  So, we peek ahead to check that the "::"
8599	 and "~" tokens are present; if they are not, then we can avoid
8600	 calling type_name.  */
8601      if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8602	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8603	  || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8604	{
8605	  cp_parser_error (parser, "non-scalar type");
8606	  return;
8607	}
8608
8609      /* Look for the type-name.  */
8610      *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8611      if (*scope == error_mark_node)
8612	return;
8613
8614      /* Look for the `::' token.  */
8615      cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8616    }
8617  else
8618    *scope = NULL_TREE;
8619
8620  /* Look for the `~'.  */
8621  cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8622
8623  /* Once we see the ~, this has to be a pseudo-destructor.  */
8624  if (!processing_template_decl && !cp_parser_error_occurred (parser))
8625    cp_parser_commit_to_topmost_tentative_parse (parser);
8626
8627  /* Look for the type-name again.  We are not responsible for
8628     checking that it matches the first type-name.  */
8629  *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8630}
8631
8632/* Parse a unary-expression.
8633
8634   unary-expression:
8635     postfix-expression
8636     ++ cast-expression
8637     -- cast-expression
8638     await-expression
8639     unary-operator cast-expression
8640     sizeof unary-expression
8641     sizeof ( type-id )
8642     alignof ( type-id )  [C++0x]
8643     new-expression
8644     delete-expression
8645
8646   GNU Extensions:
8647
8648   unary-expression:
8649     __extension__ cast-expression
8650     __alignof__ unary-expression
8651     __alignof__ ( type-id )
8652     alignof unary-expression  [C++0x]
8653     __real__ cast-expression
8654     __imag__ cast-expression
8655     && identifier
8656     sizeof ( type-id ) { initializer-list , [opt] }
8657     alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8658     __alignof__ ( type-id ) { initializer-list , [opt] }
8659
8660   ADDRESS_P is true iff the unary-expression is appearing as the
8661   operand of the `&' operator.   CAST_P is true if this expression is
8662   the target of a cast.
8663
8664   Returns a representation of the expression.  */
8665
8666static cp_expr
8667cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8668			    bool address_p, bool cast_p, bool decltype_p)
8669{
8670  cp_token *token;
8671  enum tree_code unary_operator;
8672
8673  /* Peek at the next token.  */
8674  token = cp_lexer_peek_token (parser->lexer);
8675  /* Some keywords give away the kind of expression.  */
8676  if (token->type == CPP_KEYWORD)
8677    {
8678      enum rid keyword = token->keyword;
8679
8680      switch (keyword)
8681	{
8682	case RID_ALIGNOF:
8683	case RID_SIZEOF:
8684	  {
8685	    tree operand, ret;
8686	    enum tree_code op;
8687	    location_t start_loc = token->location;
8688
8689	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8690	    bool std_alignof = id_equal (token->u.value, "alignof");
8691
8692	    /* Consume the token.  */
8693	    cp_lexer_consume_token (parser->lexer);
8694	    /* Parse the operand.  */
8695	    operand = cp_parser_sizeof_operand (parser, keyword);
8696
8697	    /* Construct a location e.g. :
8698              alignof (expr)
8699              ^~~~~~~~~~~~~~
8700              with start == caret at the start of the "alignof"/"sizeof"
8701              token, with the endpoint at the final closing paren.  */
8702	    location_t compound_loc
8703	      = make_location (start_loc, start_loc, parser->lexer);
8704
8705	    if (TYPE_P (operand))
8706	      ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8707						std_alignof, true);
8708	    else
8709	      {
8710		/* ISO C++ defines alignof only with types, not with
8711		   expressions. So pedwarn if alignof is used with a non-
8712		   type expression. However, __alignof__ is ok.  */
8713		if (std_alignof)
8714		  pedwarn (token->location, OPT_Wpedantic,
8715			   "ISO C++ does not allow %<alignof%> "
8716			   "with a non-type");
8717
8718		ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8719						  std_alignof, true);
8720	      }
8721	    /* For SIZEOF_EXPR, just issue diagnostics, but keep
8722	       SIZEOF_EXPR with the original operand.  */
8723	    if (op == SIZEOF_EXPR && ret != error_mark_node)
8724	      {
8725		if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8726		  {
8727		    if (!processing_template_decl && TYPE_P (operand))
8728		      {
8729			ret = build_min (SIZEOF_EXPR, size_type_node,
8730					 build1 (NOP_EXPR, operand,
8731						 error_mark_node));
8732			SIZEOF_EXPR_TYPE_P (ret) = 1;
8733		      }
8734		    else
8735		      ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8736		    TREE_SIDE_EFFECTS (ret) = 0;
8737		    TREE_READONLY (ret) = 1;
8738		    SET_EXPR_LOCATION (ret, compound_loc);
8739		  }
8740	      }
8741
8742	    cp_expr ret_expr (ret, compound_loc);
8743	    ret_expr = ret_expr.maybe_add_location_wrapper ();
8744	    return ret_expr;
8745	  }
8746
8747	case RID_BUILTIN_HAS_ATTRIBUTE:
8748	  return cp_parser_has_attribute_expression (parser);
8749
8750	case RID_NEW:
8751	  return cp_parser_new_expression (parser);
8752
8753	case RID_DELETE:
8754	  return cp_parser_delete_expression (parser);
8755
8756	case RID_EXTENSION:
8757	  {
8758	    /* The saved value of the PEDANTIC flag.  */
8759	    int saved_pedantic;
8760	    tree expr;
8761
8762	    /* Save away the PEDANTIC flag.  */
8763	    cp_parser_extension_opt (parser, &saved_pedantic);
8764	    /* Parse the cast-expression.  */
8765	    expr = cp_parser_simple_cast_expression (parser);
8766	    /* Restore the PEDANTIC flag.  */
8767	    pedantic = saved_pedantic;
8768
8769	    return expr;
8770	  }
8771
8772	case RID_REALPART:
8773	case RID_IMAGPART:
8774	  {
8775	    tree expression;
8776
8777	    /* Consume the `__real__' or `__imag__' token.  */
8778	    cp_lexer_consume_token (parser->lexer);
8779	    /* Parse the cast-expression.  */
8780	    expression = cp_parser_simple_cast_expression (parser);
8781	    /* Create the complete representation.  */
8782	    return build_x_unary_op (token->location,
8783				     (keyword == RID_REALPART
8784				      ? REALPART_EXPR : IMAGPART_EXPR),
8785				     expression, NULL_TREE,
8786                                     tf_warning_or_error);
8787	  }
8788	  break;
8789
8790	case RID_TRANSACTION_ATOMIC:
8791	case RID_TRANSACTION_RELAXED:
8792	  return cp_parser_transaction_expression (parser, keyword);
8793
8794	case RID_NOEXCEPT:
8795	  {
8796	    tree expr;
8797	    const char *saved_message;
8798	    bool saved_integral_constant_expression_p;
8799	    bool saved_non_integral_constant_expression_p;
8800	    bool saved_greater_than_is_operator_p;
8801
8802	    location_t start_loc = token->location;
8803
8804	    cp_lexer_consume_token (parser->lexer);
8805	    matching_parens parens;
8806	    parens.require_open (parser);
8807
8808	    saved_message = parser->type_definition_forbidden_message;
8809	    parser->type_definition_forbidden_message
8810	      = G_("types may not be defined in %<noexcept%> expressions");
8811
8812	    saved_integral_constant_expression_p
8813	      = parser->integral_constant_expression_p;
8814	    saved_non_integral_constant_expression_p
8815	      = parser->non_integral_constant_expression_p;
8816	    parser->integral_constant_expression_p = false;
8817
8818	    saved_greater_than_is_operator_p
8819	      = parser->greater_than_is_operator_p;
8820	    parser->greater_than_is_operator_p = true;
8821
8822	    ++cp_unevaluated_operand;
8823	    ++c_inhibit_evaluation_warnings;
8824	    ++cp_noexcept_operand;
8825	    expr = cp_parser_expression (parser);
8826	    --cp_noexcept_operand;
8827	    --c_inhibit_evaluation_warnings;
8828	    --cp_unevaluated_operand;
8829
8830	    parser->greater_than_is_operator_p
8831	      = saved_greater_than_is_operator_p;
8832
8833	    parser->integral_constant_expression_p
8834	      = saved_integral_constant_expression_p;
8835	    parser->non_integral_constant_expression_p
8836	      = saved_non_integral_constant_expression_p;
8837
8838	    parser->type_definition_forbidden_message = saved_message;
8839
8840	    parens.require_close (parser);
8841
8842	    /* Construct a location of the form:
8843	       noexcept (expr)
8844	       ^~~~~~~~~~~~~~~
8845	       with start == caret, finishing at the close-paren.  */
8846	    location_t noexcept_loc
8847	      = make_location (start_loc, start_loc, parser->lexer);
8848
8849	    return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8850			    noexcept_loc);
8851	  }
8852
8853	case RID_CO_AWAIT:
8854	  {
8855	    tree expr;
8856	    location_t kw_loc = token->location;
8857
8858	    /* Consume the `co_await' token.  */
8859	    cp_lexer_consume_token (parser->lexer);
8860	    /* Parse its cast-expression.  */
8861	    expr = cp_parser_simple_cast_expression (parser);
8862	    if (expr == error_mark_node)
8863	      return error_mark_node;
8864
8865	    /* Handle [expr.await].  */
8866	    return cp_expr (finish_co_await_expr (kw_loc, expr));
8867	  }
8868
8869	default:
8870	  break;
8871	}
8872    }
8873
8874  /* Look for the `:: new' and `:: delete', which also signal the
8875     beginning of a new-expression, or delete-expression,
8876     respectively.  If the next token is `::', then it might be one of
8877     these.  */
8878  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8879    {
8880      enum rid keyword;
8881
8882      /* See if the token after the `::' is one of the keywords in
8883	 which we're interested.  */
8884      keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8885      /* If it's `new', we have a new-expression.  */
8886      if (keyword == RID_NEW)
8887	return cp_parser_new_expression (parser);
8888      /* Similarly, for `delete'.  */
8889      else if (keyword == RID_DELETE)
8890	return cp_parser_delete_expression (parser);
8891    }
8892
8893  /* Look for a unary operator.  */
8894  unary_operator = cp_parser_unary_operator (token);
8895  /* The `++' and `--' operators can be handled similarly, even though
8896     they are not technically unary-operators in the grammar.  */
8897  if (unary_operator == ERROR_MARK)
8898    {
8899      if (token->type == CPP_PLUS_PLUS)
8900	unary_operator = PREINCREMENT_EXPR;
8901      else if (token->type == CPP_MINUS_MINUS)
8902	unary_operator = PREDECREMENT_EXPR;
8903      /* Handle the GNU address-of-label extension.  */
8904      else if (cp_parser_allow_gnu_extensions_p (parser)
8905	       && token->type == CPP_AND_AND)
8906	{
8907	  tree identifier;
8908	  tree expression;
8909	  location_t start_loc = token->location;
8910
8911	  /* Consume the '&&' token.  */
8912	  cp_lexer_consume_token (parser->lexer);
8913	  /* Look for the identifier.  */
8914	  identifier = cp_parser_identifier (parser);
8915	  /* Construct a location of the form:
8916	       &&label
8917	       ^~~~~~~
8918	     with caret==start at the "&&", finish at the end of the label.  */
8919	  location_t combined_loc
8920	    = make_location (start_loc, start_loc, parser->lexer);
8921	  /* Create an expression representing the address.  */
8922	  expression = finish_label_address_expr (identifier, combined_loc);
8923	  if (cp_parser_non_integral_constant_expression (parser,
8924							  NIC_ADDR_LABEL))
8925	    expression = error_mark_node;
8926	  return expression;
8927	}
8928    }
8929  if (unary_operator != ERROR_MARK)
8930    {
8931      cp_expr cast_expression;
8932      cp_expr expression = error_mark_node;
8933      non_integral_constant non_constant_p = NIC_NONE;
8934      location_t loc = token->location;
8935      tsubst_flags_t complain = complain_flags (decltype_p);
8936
8937      /* Consume the operator token.  */
8938      token = cp_lexer_consume_token (parser->lexer);
8939      enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8940
8941      /* Parse the cast-expression.  */
8942      cast_expression
8943	= cp_parser_cast_expression (parser,
8944				     unary_operator == ADDR_EXPR,
8945				     /*cast_p=*/false,
8946				     /*decltype*/false,
8947				     pidk);
8948
8949      /* Make a location:
8950	    OP_TOKEN  CAST_EXPRESSION
8951	    ^~~~~~~~~~~~~~~~~~~~~~~~~
8952	 with start==caret at the operator token, and
8953	 extending to the end of the cast_expression.  */
8954      loc = make_location (loc, loc, cast_expression.get_finish ());
8955
8956      /* Now, build an appropriate representation.  */
8957      switch (unary_operator)
8958	{
8959	case INDIRECT_REF:
8960	  non_constant_p = NIC_STAR;
8961	  expression = build_x_indirect_ref (loc, cast_expression,
8962					     RO_UNARY_STAR, NULL_TREE,
8963                                             complain);
8964          /* TODO: build_x_indirect_ref does not always honor the
8965             location, so ensure it is set.  */
8966          expression.set_location (loc);
8967	  break;
8968
8969	case ADDR_EXPR:
8970	   non_constant_p = NIC_ADDR;
8971	  /* Fall through.  */
8972	case BIT_NOT_EXPR:
8973	  expression = build_x_unary_op (loc, unary_operator,
8974					 cast_expression,
8975					 NULL_TREE, complain);
8976          /* TODO: build_x_unary_op does not always honor the location,
8977             so ensure it is set.  */
8978          expression.set_location (loc);
8979	  break;
8980
8981	case PREINCREMENT_EXPR:
8982	case PREDECREMENT_EXPR:
8983	  non_constant_p = unary_operator == PREINCREMENT_EXPR
8984			   ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8985	  /* Fall through.  */
8986	case NEGATE_EXPR:
8987	  /* Immediately fold negation of a constant, unless the constant is 0
8988	     (since -0 == 0) or it would overflow.  */
8989	  if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8990	    {
8991	      tree stripped_expr
8992		= tree_strip_any_location_wrapper (cast_expression);
8993	      if (CONSTANT_CLASS_P (stripped_expr)
8994		  && !integer_zerop (stripped_expr)
8995		  && !TREE_OVERFLOW (stripped_expr))
8996		{
8997		  tree folded = fold_build1 (unary_operator,
8998					     TREE_TYPE (stripped_expr),
8999					     stripped_expr);
9000		  if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9001		    {
9002		      expression = maybe_wrap_with_location (folded, loc);
9003		      break;
9004		    }
9005		}
9006	    }
9007	  /* Fall through.  */
9008	case UNARY_PLUS_EXPR:
9009	case TRUTH_NOT_EXPR:
9010	  expression = finish_unary_op_expr (loc, unary_operator,
9011					     cast_expression, complain);
9012	  break;
9013
9014	default:
9015	  gcc_unreachable ();
9016	}
9017
9018      if (non_constant_p != NIC_NONE
9019	  && cp_parser_non_integral_constant_expression (parser,
9020							 non_constant_p))
9021	expression = error_mark_node;
9022
9023      return expression;
9024    }
9025
9026  return cp_parser_postfix_expression (parser, address_p, cast_p,
9027                                       /*member_access_only_p=*/false,
9028				       decltype_p,
9029				       pidk);
9030}
9031
9032/* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
9033   unary-operator, the corresponding tree code is returned.  */
9034
9035static enum tree_code
9036cp_parser_unary_operator (cp_token* token)
9037{
9038  switch (token->type)
9039    {
9040    case CPP_MULT:
9041      return INDIRECT_REF;
9042
9043    case CPP_AND:
9044      return ADDR_EXPR;
9045
9046    case CPP_PLUS:
9047      return UNARY_PLUS_EXPR;
9048
9049    case CPP_MINUS:
9050      return NEGATE_EXPR;
9051
9052    case CPP_NOT:
9053      return TRUTH_NOT_EXPR;
9054
9055    case CPP_COMPL:
9056      return BIT_NOT_EXPR;
9057
9058    default:
9059      return ERROR_MARK;
9060    }
9061}
9062
9063/* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9064   Returns a representation of the expression.  */
9065
9066static tree
9067cp_parser_has_attribute_expression (cp_parser *parser)
9068{
9069  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9070
9071  /* Consume the __builtin_has_attribute token.  */
9072  cp_lexer_consume_token (parser->lexer);
9073
9074  matching_parens parens;
9075  if (!parens.require_open (parser))
9076    return error_mark_node;
9077
9078  /* Types cannot be defined in a `sizeof' expression.  Save away the
9079     old message.  */
9080  const char *saved_message = parser->type_definition_forbidden_message;
9081  const char *saved_message_arg
9082    = parser->type_definition_forbidden_message_arg;
9083  parser->type_definition_forbidden_message
9084    = G_("types may not be defined in %qs expressions");
9085  parser->type_definition_forbidden_message_arg
9086    = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9087
9088  /* The restrictions on constant-expressions do not apply inside
9089     sizeof expressions.  */
9090  bool saved_integral_constant_expression_p
9091    = parser->integral_constant_expression_p;
9092  bool saved_non_integral_constant_expression_p
9093    = parser->non_integral_constant_expression_p;
9094  parser->integral_constant_expression_p = false;
9095
9096  /* Do not actually evaluate the expression.  */
9097  ++cp_unevaluated_operand;
9098  ++c_inhibit_evaluation_warnings;
9099
9100  tree oper = NULL_TREE;
9101
9102  /* We can't be sure yet whether we're looking at a type-id or an
9103     expression.  */
9104  cp_parser_parse_tentatively (parser);
9105
9106  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9107  parser->in_type_id_in_expr_p = true;
9108  /* Look for the type-id.  */
9109  oper = cp_parser_type_id (parser);
9110  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9111
9112  cp_parser_parse_definitely (parser);
9113
9114  /* If the type-id production did not work out, then we must be
9115     looking at an expression.  */
9116  if (!oper || oper == error_mark_node)
9117    oper = cp_parser_assignment_expression (parser);
9118
9119  STRIP_ANY_LOCATION_WRAPPER (oper);
9120
9121  /* Go back to evaluating expressions.  */
9122  --cp_unevaluated_operand;
9123  --c_inhibit_evaluation_warnings;
9124
9125  /* And restore the old one.  */
9126  parser->type_definition_forbidden_message = saved_message;
9127  parser->type_definition_forbidden_message_arg = saved_message_arg;
9128  parser->integral_constant_expression_p
9129    = saved_integral_constant_expression_p;
9130  parser->non_integral_constant_expression_p
9131    = saved_non_integral_constant_expression_p;
9132
9133  /* Consume the comma if it's there.  */
9134  if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9135    {
9136      cp_parser_skip_to_closing_parenthesis (parser, false, false,
9137					     /*consume_paren=*/true);
9138      return error_mark_node;
9139    }
9140
9141  /* Parse the attribute specification.  */
9142  bool ret = false;
9143  location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9144  if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9145    {
9146      if (oper == error_mark_node)
9147	/* Nothing.  */;
9148      else if (processing_template_decl && uses_template_parms (oper))
9149	sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9150		  "not supported yet");
9151      else
9152	{
9153	  /* Fold constant expressions used in attributes first.  */
9154	  cp_check_const_attributes (attr);
9155
9156	  /* Finally, see if OPER has been declared with ATTR.  */
9157	  ret = has_attribute (atloc, oper, attr, default_conversion);
9158	}
9159
9160      parens.require_close (parser);
9161    }
9162  else
9163    {
9164      error_at (atloc, "expected identifier");
9165      cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9166    }
9167
9168  /* Construct a location e.g. :
9169     __builtin_has_attribute (oper, attr)
9170     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9171     with start == caret at the start of the built-in token,
9172     and with the endpoint at the final closing paren.  */
9173  location_t compound_loc
9174    = make_location (start_loc, start_loc, parser->lexer);
9175
9176  cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9177  ret_expr.set_location (compound_loc);
9178  ret_expr = ret_expr.maybe_add_location_wrapper ();
9179  return ret_expr;
9180}
9181
9182/* Parse a new-expression.
9183
9184   new-expression:
9185     :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9186     :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9187
9188   Returns a representation of the expression.  */
9189
9190static tree
9191cp_parser_new_expression (cp_parser* parser)
9192{
9193  bool global_scope_p;
9194  vec<tree, va_gc> *placement;
9195  tree type;
9196  vec<tree, va_gc> *initializer;
9197  tree nelts = NULL_TREE;
9198  tree ret;
9199
9200  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9201
9202  /* Look for the optional `::' operator.  */
9203  global_scope_p
9204    = (cp_parser_global_scope_opt (parser,
9205				   /*current_scope_valid_p=*/false)
9206       != NULL_TREE);
9207  /* Look for the `new' operator.  */
9208  cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9209  /* There's no easy way to tell a new-placement from the
9210     `( type-id )' construct.  */
9211  cp_parser_parse_tentatively (parser);
9212  /* Look for a new-placement.  */
9213  placement = cp_parser_new_placement (parser);
9214  /* If that didn't work out, there's no new-placement.  */
9215  if (!cp_parser_parse_definitely (parser))
9216    {
9217      if (placement != NULL)
9218	release_tree_vector (placement);
9219      placement = NULL;
9220    }
9221
9222  /* If the next token is a `(', then we have a parenthesized
9223     type-id.  */
9224  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9225    {
9226      cp_token *token;
9227      const char *saved_message = parser->type_definition_forbidden_message;
9228
9229      /* Consume the `('.  */
9230      matching_parens parens;
9231      parens.consume_open (parser);
9232
9233      /* Parse the type-id.  */
9234      parser->type_definition_forbidden_message
9235	= G_("types may not be defined in a new-expression");
9236      {
9237	type_id_in_expr_sentinel s (parser);
9238	type = cp_parser_type_id (parser);
9239      }
9240      parser->type_definition_forbidden_message = saved_message;
9241
9242      /* Look for the closing `)'.  */
9243      parens.require_close (parser);
9244      token = cp_lexer_peek_token (parser->lexer);
9245      /* There should not be a direct-new-declarator in this production,
9246	 but GCC used to allowed this, so we check and emit a sensible error
9247	 message for this case.  */
9248      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9249	{
9250	  error_at (token->location,
9251		    "array bound forbidden after parenthesized type-id");
9252	  inform (token->location,
9253		  "try removing the parentheses around the type-id");
9254	  cp_parser_direct_new_declarator (parser);
9255	}
9256    }
9257  /* Otherwise, there must be a new-type-id.  */
9258  else
9259    type = cp_parser_new_type_id (parser, &nelts);
9260
9261  /* If the next token is a `(' or '{', then we have a new-initializer.  */
9262  cp_token *token = cp_lexer_peek_token (parser->lexer);
9263  if (token->type == CPP_OPEN_PAREN
9264      || token->type == CPP_OPEN_BRACE)
9265    initializer = cp_parser_new_initializer (parser);
9266  else
9267    initializer = NULL;
9268
9269  /* A new-expression may not appear in an integral constant
9270     expression.  */
9271  if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9272    ret = error_mark_node;
9273  /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9274     of a new-type-id or type-id of a new-expression, the new-expression shall
9275     contain a new-initializer of the form ( assignment-expression )".
9276     Additionally, consistently with the spirit of DR 1467, we want to accept
9277     'new auto { 2 }' too.  */
9278  else if ((ret = type_uses_auto (type))
9279	   && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9280	   && (vec_safe_length (initializer) != 1
9281	       || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9282		   && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9283    {
9284      error_at (token->location,
9285		"initialization of new-expression for type %<auto%> "
9286		"requires exactly one element");
9287      ret = error_mark_node;
9288    }
9289  else
9290    {
9291      /* Construct a location e.g.:
9292           ptr = new int[100]
9293                 ^~~~~~~~~~~~
9294         with caret == start at the start of the "new" token, and the end
9295         at the end of the final token we consumed.  */
9296      location_t combined_loc = make_location (start_loc, start_loc,
9297					       parser->lexer);
9298      /* Create a representation of the new-expression.  */
9299      ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9300		       global_scope_p, tf_warning_or_error);
9301    }
9302
9303  if (placement != NULL)
9304    release_tree_vector (placement);
9305  if (initializer != NULL)
9306    release_tree_vector (initializer);
9307
9308  return ret;
9309}
9310
9311/* Parse a new-placement.
9312
9313   new-placement:
9314     ( expression-list )
9315
9316   Returns the same representation as for an expression-list.  */
9317
9318static vec<tree, va_gc> *
9319cp_parser_new_placement (cp_parser* parser)
9320{
9321  vec<tree, va_gc> *expression_list;
9322
9323  /* Parse the expression-list.  */
9324  expression_list = (cp_parser_parenthesized_expression_list
9325		     (parser, non_attr, /*cast_p=*/false,
9326		      /*allow_expansion_p=*/true,
9327		      /*non_constant_p=*/NULL));
9328
9329  if (expression_list && expression_list->is_empty ())
9330    error ("expected expression-list or type-id");
9331
9332  return expression_list;
9333}
9334
9335/* Parse a new-type-id.
9336
9337   new-type-id:
9338     type-specifier-seq new-declarator [opt]
9339
9340   Returns the TYPE allocated.  If the new-type-id indicates an array
9341   type, *NELTS is set to the number of elements in the last array
9342   bound; the TYPE will not include the last array bound.  */
9343
9344static tree
9345cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9346{
9347  cp_decl_specifier_seq type_specifier_seq;
9348  cp_declarator *new_declarator;
9349  cp_declarator *declarator;
9350  cp_declarator *outer_declarator;
9351  const char *saved_message;
9352
9353  /* The type-specifier sequence must not contain type definitions.
9354     (It cannot contain declarations of new types either, but if they
9355     are not definitions we will catch that because they are not
9356     complete.)  */
9357  saved_message = parser->type_definition_forbidden_message;
9358  parser->type_definition_forbidden_message
9359    = G_("types may not be defined in a new-type-id");
9360  /* Parse the type-specifier-seq.  */
9361  cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9362				/*is_declaration=*/false,
9363				/*is_trailing_return=*/false,
9364				&type_specifier_seq);
9365  /* Restore the old message.  */
9366  parser->type_definition_forbidden_message = saved_message;
9367
9368  if (type_specifier_seq.type == error_mark_node)
9369    return error_mark_node;
9370
9371  /* Parse the new-declarator.  */
9372  new_declarator = cp_parser_new_declarator_opt (parser);
9373
9374  /* Determine the number of elements in the last array dimension, if
9375     any.  */
9376  *nelts = NULL_TREE;
9377  /* Skip down to the last array dimension.  */
9378  declarator = new_declarator;
9379  outer_declarator = NULL;
9380  while (declarator && (declarator->kind == cdk_pointer
9381			|| declarator->kind == cdk_ptrmem))
9382    {
9383      outer_declarator = declarator;
9384      declarator = declarator->declarator;
9385    }
9386  while (declarator
9387	 && declarator->kind == cdk_array
9388	 && declarator->declarator
9389	 && declarator->declarator->kind == cdk_array)
9390    {
9391      outer_declarator = declarator;
9392      declarator = declarator->declarator;
9393    }
9394
9395  if (declarator && declarator->kind == cdk_array)
9396    {
9397      *nelts = declarator->u.array.bounds;
9398      if (*nelts == error_mark_node)
9399	*nelts = integer_one_node;
9400
9401      if (*nelts == NULL_TREE)
9402	/* Leave [] in the declarator.  */;
9403      else if (outer_declarator)
9404	outer_declarator->declarator = declarator->declarator;
9405      else
9406	new_declarator = NULL;
9407    }
9408
9409  return groktypename (&type_specifier_seq, new_declarator, false);
9410}
9411
9412/* Parse an (optional) new-declarator.
9413
9414   new-declarator:
9415     ptr-operator new-declarator [opt]
9416     direct-new-declarator
9417
9418   Returns the declarator.  */
9419
9420static cp_declarator *
9421cp_parser_new_declarator_opt (cp_parser* parser)
9422{
9423  enum tree_code code;
9424  tree type, std_attributes = NULL_TREE;
9425  cp_cv_quals cv_quals;
9426
9427  /* We don't know if there's a ptr-operator next, or not.  */
9428  cp_parser_parse_tentatively (parser);
9429  /* Look for a ptr-operator.  */
9430  code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9431  /* If that worked, look for more new-declarators.  */
9432  if (cp_parser_parse_definitely (parser))
9433    {
9434      cp_declarator *declarator;
9435
9436      /* Parse another optional declarator.  */
9437      declarator = cp_parser_new_declarator_opt (parser);
9438
9439      declarator = cp_parser_make_indirect_declarator
9440	(code, type, cv_quals, declarator, std_attributes);
9441
9442      return declarator;
9443    }
9444
9445  /* If the next token is a `[', there is a direct-new-declarator.  */
9446  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9447    return cp_parser_direct_new_declarator (parser);
9448
9449  return NULL;
9450}
9451
9452/* Parse a direct-new-declarator.
9453
9454   direct-new-declarator:
9455     [ expression ]
9456     direct-new-declarator [constant-expression]
9457
9458   */
9459
9460static cp_declarator *
9461cp_parser_direct_new_declarator (cp_parser* parser)
9462{
9463  cp_declarator *declarator = NULL;
9464  bool first_p = true;
9465
9466  while (true)
9467    {
9468      tree expression;
9469      cp_token *token;
9470
9471      /* Look for the opening `['.  */
9472      cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9473
9474      token = cp_lexer_peek_token (parser->lexer);
9475      if (token->type == CPP_CLOSE_SQUARE && first_p)
9476	expression = NULL_TREE;
9477      else
9478	expression = cp_parser_expression (parser);
9479      /* The standard requires that the expression have integral
9480	 type.  DR 74 adds enumeration types.  We believe that the
9481	 real intent is that these expressions be handled like the
9482	 expression in a `switch' condition, which also allows
9483	 classes with a single conversion to integral or
9484	 enumeration type.  */
9485      if (expression && !processing_template_decl)
9486	{
9487	  expression
9488	    = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9489					  expression,
9490					  /*complain=*/true);
9491	  if (!expression)
9492	    {
9493	      error_at (token->location,
9494			"expression in new-declarator must have integral "
9495			"or enumeration type");
9496	      expression = error_mark_node;
9497	    }
9498	}
9499
9500      /* Look for the closing `]'.  */
9501      cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9502
9503      /* Add this bound to the declarator.  */
9504      declarator = make_array_declarator (declarator, expression);
9505
9506      /* If the next token is not a `[', then there are no more
9507	 bounds.  */
9508      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9509	break;
9510      first_p = false;
9511    }
9512
9513  return declarator;
9514}
9515
9516/* Parse a new-initializer.
9517
9518   new-initializer:
9519     ( expression-list [opt] )
9520     braced-init-list
9521
9522   Returns a representation of the expression-list.  */
9523
9524static vec<tree, va_gc> *
9525cp_parser_new_initializer (cp_parser* parser)
9526{
9527  vec<tree, va_gc> *expression_list;
9528
9529  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9530    {
9531      tree t;
9532      bool expr_non_constant_p;
9533      cp_lexer_set_source_position (parser->lexer);
9534      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9535      t = cp_parser_braced_list (parser, &expr_non_constant_p);
9536      CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9537      expression_list = make_tree_vector_single (t);
9538    }
9539  else
9540    expression_list = (cp_parser_parenthesized_expression_list
9541		       (parser, non_attr, /*cast_p=*/false,
9542			/*allow_expansion_p=*/true,
9543			/*non_constant_p=*/NULL));
9544
9545  return expression_list;
9546}
9547
9548/* Parse a delete-expression.
9549
9550   delete-expression:
9551     :: [opt] delete cast-expression
9552     :: [opt] delete [ ] cast-expression
9553
9554   Returns a representation of the expression.  */
9555
9556static tree
9557cp_parser_delete_expression (cp_parser* parser)
9558{
9559  bool global_scope_p;
9560  bool array_p;
9561  tree expression;
9562  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9563
9564  /* Look for the optional `::' operator.  */
9565  global_scope_p
9566    = (cp_parser_global_scope_opt (parser,
9567				   /*current_scope_valid_p=*/false)
9568       != NULL_TREE);
9569  /* Look for the `delete' keyword.  */
9570  cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9571  /* See if the array syntax is in use.  */
9572  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9573    {
9574      /* Consume the `[' token.  */
9575      cp_lexer_consume_token (parser->lexer);
9576      /* Look for the `]' token.  */
9577      cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9578      /* Remember that this is the `[]' construct.  */
9579      array_p = true;
9580    }
9581  else
9582    array_p = false;
9583
9584  /* Parse the cast-expression.  */
9585  expression = cp_parser_simple_cast_expression (parser);
9586
9587  /* A delete-expression may not appear in an integral constant
9588     expression.  */
9589  if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9590    return error_mark_node;
9591
9592  /* Construct a location e.g.:
9593       delete [ ] ptr
9594       ^~~~~~~~~~~~~~
9595     with caret == start at the start of the "delete" token, and
9596     the end at the end of the final token we consumed.  */
9597  location_t combined_loc = make_location (start_loc, start_loc,
9598					   parser->lexer);
9599  expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9600			      global_scope_p, tf_warning_or_error);
9601
9602  return expression;
9603}
9604
9605/* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9606   neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9607   0 otherwise.  */
9608
9609static int
9610cp_parser_tokens_start_cast_expression (cp_parser *parser)
9611{
9612  cp_token *token = cp_lexer_peek_token (parser->lexer);
9613  switch (token->type)
9614    {
9615    case CPP_COMMA:
9616    case CPP_SEMICOLON:
9617    case CPP_QUERY:
9618    case CPP_COLON:
9619    case CPP_CLOSE_SQUARE:
9620    case CPP_CLOSE_PAREN:
9621    case CPP_CLOSE_BRACE:
9622    case CPP_OPEN_BRACE:
9623    case CPP_DOT:
9624    case CPP_DOT_STAR:
9625    case CPP_DEREF:
9626    case CPP_DEREF_STAR:
9627    case CPP_DIV:
9628    case CPP_MOD:
9629    case CPP_LSHIFT:
9630    case CPP_RSHIFT:
9631    case CPP_LESS:
9632    case CPP_GREATER:
9633    case CPP_LESS_EQ:
9634    case CPP_GREATER_EQ:
9635    case CPP_EQ_EQ:
9636    case CPP_NOT_EQ:
9637    case CPP_EQ:
9638    case CPP_MULT_EQ:
9639    case CPP_DIV_EQ:
9640    case CPP_MOD_EQ:
9641    case CPP_PLUS_EQ:
9642    case CPP_MINUS_EQ:
9643    case CPP_RSHIFT_EQ:
9644    case CPP_LSHIFT_EQ:
9645    case CPP_AND_EQ:
9646    case CPP_XOR_EQ:
9647    case CPP_OR_EQ:
9648    case CPP_XOR:
9649    case CPP_OR:
9650    case CPP_OR_OR:
9651    case CPP_EOF:
9652    case CPP_ELLIPSIS:
9653      return 0;
9654
9655    case CPP_OPEN_PAREN:
9656      /* In ((type ()) () the last () isn't a valid cast-expression,
9657	 so the whole must be parsed as postfix-expression.  */
9658      return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9659	     != CPP_CLOSE_PAREN;
9660
9661    case CPP_OPEN_SQUARE:
9662      /* '[' may start a primary-expression in obj-c++ and in C++11,
9663	 as a lambda-expression, eg, '(void)[]{}'.  */
9664      if (cxx_dialect >= cxx11)
9665	return -1;
9666      return c_dialect_objc ();
9667
9668    case CPP_PLUS_PLUS:
9669    case CPP_MINUS_MINUS:
9670      /* '++' and '--' may or may not start a cast-expression:
9671
9672	 struct T { void operator++(int); };
9673	 void f() { (T())++; }
9674
9675	 vs
9676
9677	 int a;
9678	 (int)++a;  */
9679      return -1;
9680
9681    default:
9682      return 1;
9683    }
9684}
9685
9686/* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9687   in the order: const_cast, static_cast, reinterpret_cast.
9688
9689   Don't suggest dynamic_cast.
9690
9691   Return the first legal cast kind found, or NULL otherwise.  */
9692
9693static const char *
9694get_cast_suggestion (tree dst_type, tree orig_expr)
9695{
9696  tree trial;
9697
9698  /* Reuse the parser logic by attempting to build the various kinds of
9699     cast, with "complain" disabled.
9700     Identify the first such cast that is valid.  */
9701
9702  /* Don't attempt to run such logic within template processing.  */
9703  if (processing_template_decl)
9704    return NULL;
9705
9706  /* First try const_cast.  */
9707  trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9708  if (trial != error_mark_node)
9709    return "const_cast";
9710
9711  /* If that fails, try static_cast.  */
9712  trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9713  if (trial != error_mark_node)
9714    return "static_cast";
9715
9716  /* Finally, try reinterpret_cast.  */
9717  trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9718				  tf_none);
9719  if (trial != error_mark_node)
9720    return "reinterpret_cast";
9721
9722  /* No such cast possible.  */
9723  return NULL;
9724}
9725
9726/* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9727   suggesting how to convert a C-style cast of the form:
9728
9729     (DST_TYPE)ORIG_EXPR
9730
9731   to a C++-style cast.
9732
9733   The primary range of RICHLOC is asssumed to be that of the original
9734   expression.  OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9735   of the parens in the C-style cast.  */
9736
9737static void
9738maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9739		      location_t close_paren_loc, tree orig_expr,
9740		      tree dst_type)
9741{
9742  /* This function is non-trivial, so bail out now if the warning isn't
9743     going to be emitted.  */
9744  if (!warn_old_style_cast)
9745    return;
9746
9747  /* Try to find a legal C++ cast, trying them in order:
9748     const_cast, static_cast, reinterpret_cast.  */
9749  const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9750  if (!cast_suggestion)
9751    return;
9752
9753  /* Replace the open paren with "CAST_SUGGESTION<".  */
9754  pretty_printer pp;
9755  pp_string (&pp, cast_suggestion);
9756  pp_less (&pp);
9757  rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9758
9759  /* Replace the close paren with "> (".  */
9760  rich_loc->add_fixit_replace (close_paren_loc, "> (");
9761
9762  /* Add a closing paren after the expr (the primary range of RICH_LOC).  */
9763  rich_loc->add_fixit_insert_after (")");
9764}
9765
9766
9767/* Parse a cast-expression.
9768
9769   cast-expression:
9770     unary-expression
9771     ( type-id ) cast-expression
9772
9773   ADDRESS_P is true iff the unary-expression is appearing as the
9774   operand of the `&' operator.   CAST_P is true if this expression is
9775   the target of a cast.
9776
9777   Returns a representation of the expression.  */
9778
9779static cp_expr
9780cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9781			   bool decltype_p, cp_id_kind * pidk)
9782{
9783  /* If it's a `(', then we might be looking at a cast.  */
9784  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9785    {
9786      tree type = NULL_TREE;
9787      cp_expr expr (NULL_TREE);
9788      int cast_expression = 0;
9789      const char *saved_message;
9790
9791      /* There's no way to know yet whether or not this is a cast.
9792	 For example, `(int (3))' is a unary-expression, while `(int)
9793	 3' is a cast.  So, we resort to parsing tentatively.  */
9794      cp_parser_parse_tentatively (parser);
9795      /* Types may not be defined in a cast.  */
9796      saved_message = parser->type_definition_forbidden_message;
9797      parser->type_definition_forbidden_message
9798	= G_("types may not be defined in casts");
9799      /* Consume the `('.  */
9800      matching_parens parens;
9801      cp_token *open_paren = parens.consume_open (parser);
9802      location_t open_paren_loc = open_paren->location;
9803      location_t close_paren_loc = UNKNOWN_LOCATION;
9804
9805      /* A very tricky bit is that `(struct S) { 3 }' is a
9806	 compound-literal (which we permit in C++ as an extension).
9807	 But, that construct is not a cast-expression -- it is a
9808	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
9809	 is legal; if the compound-literal were a cast-expression,
9810	 you'd need an extra set of parentheses.)  But, if we parse
9811	 the type-id, and it happens to be a class-specifier, then we
9812	 will commit to the parse at that point, because we cannot
9813	 undo the action that is done when creating a new class.  So,
9814	 then we cannot back up and do a postfix-expression.
9815
9816	 Another tricky case is the following (c++/29234):
9817
9818         struct S { void operator () (); };
9819
9820         void foo ()
9821         {
9822           ( S()() );
9823         }
9824
9825	 As a type-id we parse the parenthesized S()() as a function
9826	 returning a function, groktypename complains and we cannot
9827	 back up in this case either.
9828
9829	 Therefore, we scan ahead to the closing `)', and check to see
9830	 if the tokens after the `)' can start a cast-expression.  Otherwise
9831	 we are dealing with an unary-expression, a postfix-expression
9832	 or something else.
9833
9834	 Yet another tricky case, in C++11, is the following (c++/54891):
9835
9836	 (void)[]{};
9837
9838         The issue is that usually, besides the case of lambda-expressions,
9839	 the parenthesized type-id cannot be followed by '[', and, eg, we
9840	 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9841	 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9842	 we don't commit, we try a cast-expression, then an unary-expression.
9843
9844	 Save tokens so that we can put them back.  */
9845      cp_lexer_save_tokens (parser->lexer);
9846
9847      /* We may be looking at a cast-expression.  */
9848      if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9849						 /*consume_paren=*/true))
9850	cast_expression
9851	  = cp_parser_tokens_start_cast_expression (parser);
9852
9853      /* Roll back the tokens we skipped.  */
9854      cp_lexer_rollback_tokens (parser->lexer);
9855      /* If we aren't looking at a cast-expression, simulate an error so
9856	 that the call to cp_parser_error_occurred below returns true.  */
9857      if (!cast_expression)
9858	cp_parser_simulate_error (parser);
9859      else
9860	{
9861	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9862	  parser->in_type_id_in_expr_p = true;
9863	  /* Look for the type-id.  */
9864	  type = cp_parser_type_id (parser);
9865	  /* Look for the closing `)'.  */
9866	  cp_token *close_paren = parens.require_close (parser);
9867	  if (close_paren)
9868	    close_paren_loc = close_paren->location;
9869	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9870	}
9871
9872      /* Restore the saved message.  */
9873      parser->type_definition_forbidden_message = saved_message;
9874
9875      /* At this point this can only be either a cast or a
9876	 parenthesized ctor such as `(T ())' that looks like a cast to
9877	 function returning T.  */
9878      if (!cp_parser_error_occurred (parser))
9879	{
9880	  /* Only commit if the cast-expression doesn't start with
9881	     '++', '--', or '[' in C++11.  */
9882	  if (cast_expression > 0)
9883	    cp_parser_commit_to_topmost_tentative_parse (parser);
9884
9885	  expr = cp_parser_cast_expression (parser,
9886					    /*address_p=*/false,
9887					    /*cast_p=*/true,
9888					    /*decltype_p=*/false,
9889					    pidk);
9890
9891	  if (cp_parser_parse_definitely (parser))
9892	    {
9893	      /* Warn about old-style casts, if so requested.  */
9894	      if (warn_old_style_cast
9895		  && !in_system_header_at (input_location)
9896		  && !VOID_TYPE_P (type)
9897		  && current_lang_name != lang_name_c)
9898		{
9899		  gcc_rich_location rich_loc (input_location);
9900		  maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9901					expr, type);
9902		  warning_at (&rich_loc, OPT_Wold_style_cast,
9903			      "use of old-style cast to %q#T", type);
9904		}
9905
9906	      /* Only type conversions to integral or enumeration types
9907		 can be used in constant-expressions.  */
9908	      if (!cast_valid_in_integral_constant_expression_p (type)
9909		  && cp_parser_non_integral_constant_expression (parser,
9910								 NIC_CAST))
9911		return error_mark_node;
9912
9913	      /* Perform the cast.  */
9914	      /* Make a location:
9915		   (TYPE) EXPR
9916		   ^~~~~~~~~~~
9917		 with start==caret at the open paren, extending to the
9918		 end of "expr".  */
9919	      location_t cast_loc = make_location (open_paren_loc,
9920						   open_paren_loc,
9921						   expr.get_finish ());
9922	      expr = build_c_cast (cast_loc, type, expr);
9923	      return expr;
9924	    }
9925	}
9926      else
9927        cp_parser_abort_tentative_parse (parser);
9928    }
9929
9930  /* If we get here, then it's not a cast, so it must be a
9931     unary-expression.  */
9932  return cp_parser_unary_expression (parser, pidk, address_p,
9933				     cast_p, decltype_p);
9934}
9935
9936/* Parse a binary expression of the general form:
9937
9938   pm-expression:
9939     cast-expression
9940     pm-expression .* cast-expression
9941     pm-expression ->* cast-expression
9942
9943   multiplicative-expression:
9944     pm-expression
9945     multiplicative-expression * pm-expression
9946     multiplicative-expression / pm-expression
9947     multiplicative-expression % pm-expression
9948
9949   additive-expression:
9950     multiplicative-expression
9951     additive-expression + multiplicative-expression
9952     additive-expression - multiplicative-expression
9953
9954   shift-expression:
9955     additive-expression
9956     shift-expression << additive-expression
9957     shift-expression >> additive-expression
9958
9959   relational-expression:
9960     shift-expression
9961     relational-expression < shift-expression
9962     relational-expression > shift-expression
9963     relational-expression <= shift-expression
9964     relational-expression >= shift-expression
9965
9966  GNU Extension:
9967
9968   relational-expression:
9969     relational-expression <? shift-expression
9970     relational-expression >? shift-expression
9971
9972   equality-expression:
9973     relational-expression
9974     equality-expression == relational-expression
9975     equality-expression != relational-expression
9976
9977   and-expression:
9978     equality-expression
9979     and-expression & equality-expression
9980
9981   exclusive-or-expression:
9982     and-expression
9983     exclusive-or-expression ^ and-expression
9984
9985   inclusive-or-expression:
9986     exclusive-or-expression
9987     inclusive-or-expression | exclusive-or-expression
9988
9989   logical-and-expression:
9990     inclusive-or-expression
9991     logical-and-expression && inclusive-or-expression
9992
9993   logical-or-expression:
9994     logical-and-expression
9995     logical-or-expression || logical-and-expression
9996
9997   All these are implemented with a single function like:
9998
9999   binary-expression:
10000     simple-cast-expression
10001     binary-expression <token> binary-expression
10002
10003   CAST_P is true if this expression is the target of a cast.
10004
10005   The binops_by_token map is used to get the tree codes for each <token> type.
10006   binary-expressions are associated according to a precedence table.  */
10007
10008#define TOKEN_PRECEDENCE(token)				     \
10009(((token->type == CPP_GREATER				     \
10010   || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10011  && !parser->greater_than_is_operator_p)		     \
10012 ? PREC_NOT_OPERATOR					     \
10013 : binops_by_token[token->type].prec)
10014
10015static cp_expr
10016cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10017			     bool no_toplevel_fold_p,
10018			     bool decltype_p,
10019			     enum cp_parser_prec prec,
10020			     cp_id_kind * pidk)
10021{
10022  cp_parser_expression_stack stack;
10023  cp_parser_expression_stack_entry *sp = &stack[0];
10024  cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10025  cp_parser_expression_stack_entry current;
10026  cp_expr rhs;
10027  cp_token *token;
10028  enum tree_code rhs_type;
10029  enum cp_parser_prec new_prec, lookahead_prec;
10030  tree overload;
10031
10032  /* Parse the first expression.  */
10033  current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10034		      ? TRUTH_NOT_EXPR : ERROR_MARK);
10035  current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10036					   cast_p, decltype_p, pidk);
10037  current.prec = prec;
10038
10039  if (cp_parser_error_occurred (parser))
10040    return error_mark_node;
10041
10042  for (;;)
10043    {
10044      /* Get an operator token.  */
10045      token = cp_lexer_peek_token (parser->lexer);
10046
10047      if (warn_cxx11_compat
10048          && token->type == CPP_RSHIFT
10049          && !parser->greater_than_is_operator_p)
10050        {
10051          if (warning_at (token->location, OPT_Wc__11_compat,
10052			  "%<>>%> operator is treated"
10053			  " as two right angle brackets in C++11"))
10054	    inform (token->location,
10055		    "suggest parentheses around %<>>%> expression");
10056        }
10057
10058      new_prec = TOKEN_PRECEDENCE (token);
10059      if (new_prec != PREC_NOT_OPERATOR
10060	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10061	/* This is a fold-expression; handle it later.  */
10062	new_prec = PREC_NOT_OPERATOR;
10063
10064      /* Popping an entry off the stack means we completed a subexpression:
10065	 - either we found a token which is not an operator (`>' where it is not
10066	   an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10067	   will happen repeatedly;
10068	 - or, we found an operator which has lower priority.  This is the case
10069	   where the recursive descent *ascends*, as in `3 * 4 + 5' after
10070	   parsing `3 * 4'.  */
10071      if (new_prec <= current.prec)
10072	{
10073	  if (sp == stack)
10074	    break;
10075	  else
10076	    goto pop;
10077	}
10078
10079     get_rhs:
10080      current.tree_type = binops_by_token[token->type].tree_type;
10081      current.loc = token->location;
10082
10083      /* We used the operator token.  */
10084      cp_lexer_consume_token (parser->lexer);
10085
10086      /* For "false && x" or "true || x", x will never be executed;
10087	 disable warnings while evaluating it.  */
10088      if ((current.tree_type == TRUTH_ANDIF_EXPR
10089	   && cp_fully_fold (current.lhs) == truthvalue_false_node)
10090	  || (current.tree_type == TRUTH_ORIF_EXPR
10091	      && cp_fully_fold (current.lhs) == truthvalue_true_node))
10092	{
10093	  disable_warnings_sp = sp;
10094	  ++c_inhibit_evaluation_warnings;
10095	}
10096
10097      /* Extract another operand.  It may be the RHS of this expression
10098	 or the LHS of a new, higher priority expression.  */
10099      rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10100		  ? TRUTH_NOT_EXPR : ERROR_MARK);
10101      rhs = cp_parser_simple_cast_expression (parser);
10102
10103      /* Get another operator token.  Look up its precedence to avoid
10104	 building a useless (immediately popped) stack entry for common
10105	 cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
10106      token = cp_lexer_peek_token (parser->lexer);
10107      lookahead_prec = TOKEN_PRECEDENCE (token);
10108      if (lookahead_prec != PREC_NOT_OPERATOR
10109	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10110	lookahead_prec = PREC_NOT_OPERATOR;
10111      if (lookahead_prec > new_prec)
10112	{
10113	  /* ... and prepare to parse the RHS of the new, higher priority
10114	     expression.  Since precedence levels on the stack are
10115	     monotonically increasing, we do not have to care about
10116	     stack overflows.  */
10117	  *sp = current;
10118	  ++sp;
10119	  current.lhs = rhs;
10120	  current.lhs_type = rhs_type;
10121	  current.prec = new_prec;
10122	  new_prec = lookahead_prec;
10123	  goto get_rhs;
10124
10125	 pop:
10126	  lookahead_prec = new_prec;
10127	  /* If the stack is not empty, we have parsed into LHS the right side
10128	     (`4' in the example above) of an expression we had suspended.
10129	     We can use the information on the stack to recover the LHS (`3')
10130	     from the stack together with the tree code (`MULT_EXPR'), and
10131	     the precedence of the higher level subexpression
10132	     (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
10133	     which will be used to actually build the additive expression.  */
10134	  rhs = current.lhs;
10135	  rhs_type = current.lhs_type;
10136	  --sp;
10137	  current = *sp;
10138	}
10139
10140      /* Undo the disabling of warnings done above.  */
10141      if (sp == disable_warnings_sp)
10142	{
10143	  disable_warnings_sp = NULL;
10144	  --c_inhibit_evaluation_warnings;
10145	}
10146
10147      if (warn_logical_not_paren
10148	  && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10149	  && current.lhs_type == TRUTH_NOT_EXPR
10150	  /* Avoid warning for !!x == y.  */
10151	  && (TREE_CODE (current.lhs) != NE_EXPR
10152	      || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10153	  && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10154	      || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10155		  /* Avoid warning for !b == y where b is boolean.  */
10156		  && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10157		      || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10158			  != BOOLEAN_TYPE))))
10159	  /* Avoid warning for !!b == y where b is boolean.  */
10160	  && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10161		|| (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10162		    && DECL_P (tree_strip_any_location_wrapper
10163					    (TREE_OPERAND (current.lhs, 0)))))
10164	      || TREE_TYPE (current.lhs) == NULL_TREE
10165	      || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10166	warn_logical_not_parentheses (current.loc, current.tree_type,
10167				      current.lhs, maybe_constant_value (rhs));
10168
10169      overload = NULL;
10170
10171      location_t combined_loc = make_location (current.loc,
10172					       current.lhs.get_start (),
10173					       rhs.get_finish ());
10174
10175      /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10176	 ERROR_MARK for everything that is not a binary expression.
10177	 This makes warn_about_parentheses miss some warnings that
10178	 involve unary operators.  For unary expressions we should
10179	 pass the correct tree_code unless the unary expression was
10180	 surrounded by parentheses.
10181      */
10182      if (no_toplevel_fold_p
10183	  && lookahead_prec <= current.prec
10184	  && sp == stack)
10185	{
10186	  if (current.lhs == error_mark_node || rhs == error_mark_node)
10187	    current.lhs = error_mark_node;
10188	  else
10189	    {
10190	      current.lhs.maybe_add_location_wrapper ();
10191	      rhs.maybe_add_location_wrapper ();
10192	      current.lhs
10193		= build_min (current.tree_type,
10194			     TREE_CODE_CLASS (current.tree_type)
10195			     == tcc_comparison
10196			     ? boolean_type_node : TREE_TYPE (current.lhs),
10197			     current.lhs.get_value (), rhs.get_value ());
10198	      SET_EXPR_LOCATION (current.lhs, combined_loc);
10199	    }
10200	}
10201      else
10202        {
10203	  op_location_t op_loc (current.loc, combined_loc);
10204	  current.lhs = build_x_binary_op (op_loc, current.tree_type,
10205                                           current.lhs, current.lhs_type,
10206					   rhs, rhs_type, NULL_TREE, &overload,
10207                                           complain_flags (decltype_p));
10208          /* TODO: build_x_binary_op doesn't always honor the location.  */
10209          current.lhs.set_location (combined_loc);
10210        }
10211      current.lhs_type = current.tree_type;
10212
10213      /* If the binary operator required the use of an overloaded operator,
10214	 then this expression cannot be an integral constant-expression.
10215	 An overloaded operator can be used even if both operands are
10216	 otherwise permissible in an integral constant-expression if at
10217	 least one of the operands is of enumeration type.  */
10218
10219      if (overload
10220	  && cp_parser_non_integral_constant_expression (parser,
10221							 NIC_OVERLOADED))
10222	return error_mark_node;
10223    }
10224
10225  return current.lhs;
10226}
10227
10228static cp_expr
10229cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10230			     bool no_toplevel_fold_p,
10231			     enum cp_parser_prec prec,
10232			     cp_id_kind * pidk)
10233{
10234  return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10235				      /*decltype*/false, prec, pidk);
10236}
10237
10238/* Parse the `? expression : assignment-expression' part of a
10239   conditional-expression.  The LOGICAL_OR_EXPR is the
10240   logical-or-expression that started the conditional-expression.
10241   Returns a representation of the entire conditional-expression.
10242
10243   This routine is used by cp_parser_assignment_expression.
10244
10245     ? expression : assignment-expression
10246
10247   GNU Extensions:
10248
10249     ? : assignment-expression */
10250
10251static tree
10252cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10253{
10254  tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10255  cp_expr assignment_expr;
10256  struct cp_token *token;
10257  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10258
10259  /* Consume the `?' token.  */
10260  cp_lexer_consume_token (parser->lexer);
10261  token = cp_lexer_peek_token (parser->lexer);
10262  if (cp_parser_allow_gnu_extensions_p (parser)
10263      && token->type == CPP_COLON)
10264    {
10265      pedwarn (token->location, OPT_Wpedantic,
10266	       "ISO C++ does not allow %<?:%> with omitted middle operand");
10267      /* Implicit true clause.  */
10268      expr = NULL_TREE;
10269      c_inhibit_evaluation_warnings +=
10270	folded_logical_or_expr == truthvalue_true_node;
10271      warn_for_omitted_condop (token->location, logical_or_expr);
10272    }
10273  else
10274    {
10275      bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10276      parser->colon_corrects_to_scope_p = false;
10277      /* Parse the expression.  */
10278      c_inhibit_evaluation_warnings +=
10279	folded_logical_or_expr == truthvalue_false_node;
10280      expr = cp_parser_expression (parser);
10281      c_inhibit_evaluation_warnings +=
10282	((folded_logical_or_expr == truthvalue_true_node)
10283	 - (folded_logical_or_expr == truthvalue_false_node));
10284      parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10285    }
10286
10287  /* The next token should be a `:'.  */
10288  cp_parser_require (parser, CPP_COLON, RT_COLON);
10289  /* Parse the assignment-expression.  */
10290  assignment_expr = cp_parser_assignment_expression (parser);
10291  c_inhibit_evaluation_warnings -=
10292    folded_logical_or_expr == truthvalue_true_node;
10293
10294  /* Make a location:
10295       LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10296       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10297     with the caret at the "?", ranging from the start of
10298     the logical_or_expr to the end of the assignment_expr.  */
10299  loc = make_location (loc,
10300		       logical_or_expr.get_start (),
10301		       assignment_expr.get_finish ());
10302
10303  /* Build the conditional-expression.  */
10304  return build_x_conditional_expr (loc, logical_or_expr,
10305				   expr,
10306				   assignment_expr,
10307                                   tf_warning_or_error);
10308}
10309
10310/* Parse an assignment-expression.
10311
10312   assignment-expression:
10313     conditional-expression
10314     logical-or-expression assignment-operator assignment_expression
10315     throw-expression
10316     yield-expression
10317
10318   CAST_P is true if this expression is the target of a cast.
10319   DECLTYPE_P is true if this expression is the operand of decltype.
10320
10321   Returns a representation for the expression.  */
10322
10323static cp_expr
10324cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10325				 bool cast_p, bool decltype_p)
10326{
10327  cp_expr expr;
10328
10329  /* If the next token is the `throw' keyword, then we're looking at
10330     a throw-expression.  */
10331  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10332    expr = cp_parser_throw_expression (parser);
10333  /* If the next token is the `co_yield' keyword, then we're looking at
10334     a yield-expression.  */
10335  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10336    expr = cp_parser_yield_expression (parser);
10337  /* Otherwise, it must be that we are looking at a
10338     logical-or-expression.  */
10339  else
10340    {
10341      /* Parse the binary expressions (logical-or-expression).  */
10342      expr = cp_parser_binary_expression (parser, cast_p, false,
10343					  decltype_p,
10344					  PREC_NOT_OPERATOR, pidk);
10345      /* If the next token is a `?' then we're actually looking at a
10346	 conditional-expression.  */
10347      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10348	return cp_parser_question_colon_clause (parser, expr);
10349      else
10350	{
10351	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10352
10353	  /* If it's an assignment-operator, we're using the second
10354	     production.  */
10355	  enum tree_code assignment_operator
10356	    = cp_parser_assignment_operator_opt (parser);
10357	  if (assignment_operator != ERROR_MARK)
10358	    {
10359	      bool non_constant_p;
10360
10361	      /* Parse the right-hand side of the assignment.  */
10362	      cp_expr rhs = cp_parser_initializer_clause (parser,
10363							  &non_constant_p);
10364
10365	      if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10366		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10367
10368	      /* An assignment may not appear in a
10369		 constant-expression.  */
10370	      if (cp_parser_non_integral_constant_expression (parser,
10371							      NIC_ASSIGNMENT))
10372		return error_mark_node;
10373	      /* Build the assignment expression.  Its default
10374		 location:
10375		   LHS = RHS
10376		   ~~~~^~~~~
10377		 is the location of the '=' token as the
10378		 caret, ranging from the start of the lhs to the
10379		 end of the rhs.  */
10380	      loc = make_location (loc,
10381				   expr.get_start (),
10382				   rhs.get_finish ());
10383	      expr = build_x_modify_expr (loc, expr,
10384					  assignment_operator,
10385					  rhs, NULL_TREE,
10386					  complain_flags (decltype_p));
10387              /* TODO: build_x_modify_expr doesn't honor the location,
10388                 so we must set it here.  */
10389              expr.set_location (loc);
10390	    }
10391	}
10392    }
10393
10394  return expr;
10395}
10396
10397/* Parse an (optional) assignment-operator.
10398
10399   assignment-operator: one of
10400     = *= /= %= += -= >>= <<= &= ^= |=
10401
10402   GNU Extension:
10403
10404   assignment-operator: one of
10405     <?= >?=
10406
10407   If the next token is an assignment operator, the corresponding tree
10408   code is returned, and the token is consumed.  For example, for
10409   `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
10410   NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
10411   TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
10412   operator, ERROR_MARK is returned.  */
10413
10414static enum tree_code
10415cp_parser_assignment_operator_opt (cp_parser* parser)
10416{
10417  enum tree_code op;
10418  cp_token *token;
10419
10420  /* Peek at the next token.  */
10421  token = cp_lexer_peek_token (parser->lexer);
10422
10423  switch (token->type)
10424    {
10425    case CPP_EQ:
10426      op = NOP_EXPR;
10427      break;
10428
10429    case CPP_MULT_EQ:
10430      op = MULT_EXPR;
10431      break;
10432
10433    case CPP_DIV_EQ:
10434      op = TRUNC_DIV_EXPR;
10435      break;
10436
10437    case CPP_MOD_EQ:
10438      op = TRUNC_MOD_EXPR;
10439      break;
10440
10441    case CPP_PLUS_EQ:
10442      op = PLUS_EXPR;
10443      break;
10444
10445    case CPP_MINUS_EQ:
10446      op = MINUS_EXPR;
10447      break;
10448
10449    case CPP_RSHIFT_EQ:
10450      op = RSHIFT_EXPR;
10451      break;
10452
10453    case CPP_LSHIFT_EQ:
10454      op = LSHIFT_EXPR;
10455      break;
10456
10457    case CPP_AND_EQ:
10458      op = BIT_AND_EXPR;
10459      break;
10460
10461    case CPP_XOR_EQ:
10462      op = BIT_XOR_EXPR;
10463      break;
10464
10465    case CPP_OR_EQ:
10466      op = BIT_IOR_EXPR;
10467      break;
10468
10469    default:
10470      /* Nothing else is an assignment operator.  */
10471      op = ERROR_MARK;
10472    }
10473
10474  /* An operator followed by ... is a fold-expression, handled elsewhere.  */
10475  if (op != ERROR_MARK
10476      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10477    op = ERROR_MARK;
10478
10479  /* If it was an assignment operator, consume it.  */
10480  if (op != ERROR_MARK)
10481    cp_lexer_consume_token (parser->lexer);
10482
10483  return op;
10484}
10485
10486/* Parse an expression.
10487
10488   expression:
10489     assignment-expression
10490     expression , assignment-expression
10491
10492   CAST_P is true if this expression is the target of a cast.
10493   DECLTYPE_P is true if this expression is the immediate operand of decltype,
10494     except possibly parenthesized or on the RHS of a comma (N3276).
10495   WARN_COMMA_P is true if a comma should be diagnosed.
10496
10497   Returns a representation of the expression.  */
10498
10499static cp_expr
10500cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10501		      bool cast_p, bool decltype_p, bool warn_comma_p)
10502{
10503  cp_expr expression = NULL_TREE;
10504  location_t loc = UNKNOWN_LOCATION;
10505
10506  while (true)
10507    {
10508      cp_expr assignment_expression;
10509
10510      /* Parse the next assignment-expression.  */
10511      assignment_expression
10512	= cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10513
10514      /* We don't create a temporary for a call that is the immediate operand
10515	 of decltype or on the RHS of a comma.  But when we see a comma, we
10516	 need to create a temporary for a call on the LHS.  */
10517      if (decltype_p && !processing_template_decl
10518	  && TREE_CODE (assignment_expression) == CALL_EXPR
10519	  && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10520	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10521	assignment_expression
10522	  = build_cplus_new (TREE_TYPE (assignment_expression),
10523			     assignment_expression, tf_warning_or_error);
10524
10525      /* If this is the first assignment-expression, we can just
10526	 save it away.  */
10527      if (!expression)
10528	expression = assignment_expression;
10529      else
10530	{
10531	  /* Create a location with caret at the comma, ranging
10532	     from the start of the LHS to the end of the RHS.  */
10533	  loc = make_location (loc,
10534			       expression.get_start (),
10535			       assignment_expression.get_finish ());
10536	  expression = build_x_compound_expr (loc, expression,
10537					      assignment_expression, NULL_TREE,
10538					      complain_flags (decltype_p));
10539	  expression.set_location (loc);
10540	}
10541      /* If the next token is not a comma, or we're in a fold-expression, then
10542	 we are done with the expression.  */
10543      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10544	  || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10545	break;
10546      /* Consume the `,'.  */
10547      loc = cp_lexer_peek_token (parser->lexer)->location;
10548      if (warn_comma_p)
10549	{
10550	  /* [depr.comma.subscript]: A comma expression appearing as
10551	     the expr-or-braced-init-list of a subscripting expression
10552	     is deprecated.  A parenthesized comma expression is not
10553	     deprecated.  */
10554	  warning_at (loc, OPT_Wcomma_subscript,
10555		      "top-level comma expression in array subscript "
10556		      "is deprecated");
10557	  warn_comma_p = false;
10558	}
10559      cp_lexer_consume_token (parser->lexer);
10560      /* A comma operator cannot appear in a constant-expression.  */
10561      if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10562	expression = error_mark_node;
10563    }
10564
10565  return expression;
10566}
10567
10568/* Parse a constant-expression.
10569
10570   constant-expression:
10571     conditional-expression
10572
10573  If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10574  accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
10575  constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
10576  is false, NON_CONSTANT_P should be NULL.  If ALLOW_NON_CONSTANT_P is
10577  greater than 1, this isn't really a constant-expression, only a
10578  potentially constant-evaluated expression.  If STRICT_P is true,
10579  only parse a conditional-expression, otherwise parse an
10580  assignment-expression.  See below for rationale.  */
10581
10582static cp_expr
10583cp_parser_constant_expression (cp_parser* parser,
10584			       int allow_non_constant_p,
10585			       bool *non_constant_p,
10586			       bool strict_p)
10587{
10588  bool saved_integral_constant_expression_p;
10589  bool saved_allow_non_integral_constant_expression_p;
10590  bool saved_non_integral_constant_expression_p;
10591  cp_expr expression;
10592
10593  /* It might seem that we could simply parse the
10594     conditional-expression, and then check to see if it were
10595     TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
10596     one that the compiler can figure out is constant, possibly after
10597     doing some simplifications or optimizations.  The standard has a
10598     precise definition of constant-expression, and we must honor
10599     that, even though it is somewhat more restrictive.
10600
10601     For example:
10602
10603       int i[(2, 3)];
10604
10605     is not a legal declaration, because `(2, 3)' is not a
10606     constant-expression.  The `,' operator is forbidden in a
10607     constant-expression.  However, GCC's constant-folding machinery
10608     will fold this operation to an INTEGER_CST for `3'.  */
10609
10610  /* Save the old settings.  */
10611  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10612  saved_allow_non_integral_constant_expression_p
10613    = parser->allow_non_integral_constant_expression_p;
10614  saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10615  /* We are now parsing a constant-expression.  */
10616  parser->integral_constant_expression_p = true;
10617  parser->allow_non_integral_constant_expression_p
10618    = (allow_non_constant_p || cxx_dialect >= cxx11);
10619  parser->non_integral_constant_expression_p = false;
10620
10621  /* A manifestly constant-evaluated expression is evaluated even in an
10622     unevaluated operand.  */
10623  cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10624
10625  /* Although the grammar says "conditional-expression", when not STRICT_P,
10626     we parse an "assignment-expression", which also permits
10627     "throw-expression" and the use of assignment operators.  In the case
10628     that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10629     otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
10630     actually essential that we look for an assignment-expression.
10631     For example, cp_parser_initializer_clauses uses this function to
10632     determine whether a particular assignment-expression is in fact
10633     constant.  */
10634  if (strict_p)
10635    {
10636      /* Parse the binary expressions (logical-or-expression).  */
10637      expression = cp_parser_binary_expression (parser, false, false, false,
10638						PREC_NOT_OPERATOR, NULL);
10639      /* If the next token is a `?' then we're actually looking at
10640	 a conditional-expression; otherwise we're done.  */
10641      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10642	expression = cp_parser_question_colon_clause (parser, expression);
10643    }
10644  else
10645    expression = cp_parser_assignment_expression (parser);
10646  /* Restore the old settings.  */
10647  parser->integral_constant_expression_p
10648    = saved_integral_constant_expression_p;
10649  parser->allow_non_integral_constant_expression_p
10650    = saved_allow_non_integral_constant_expression_p;
10651  if (cxx_dialect >= cxx11)
10652    {
10653      /* Require an rvalue constant expression here; that's what our
10654	 callers expect.  Reference constant expressions are handled
10655	 separately in e.g. cp_parser_template_argument.  */
10656      tree decay = expression;
10657      if (TREE_TYPE (expression)
10658	  && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10659	decay = build_address (expression);
10660      bool is_const = is_rvalue_constant_expression (decay);
10661      parser->non_integral_constant_expression_p = !is_const;
10662      if (!is_const && !allow_non_constant_p)
10663	require_rvalue_constant_expression (decay);
10664    }
10665  if (allow_non_constant_p)
10666    *non_constant_p = parser->non_integral_constant_expression_p;
10667  parser->non_integral_constant_expression_p
10668    = saved_non_integral_constant_expression_p;
10669
10670  return expression;
10671}
10672
10673/* Parse __builtin_offsetof.
10674
10675   offsetof-expression:
10676     "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10677
10678   offsetof-member-designator:
10679     id-expression
10680     | offsetof-member-designator "." id-expression
10681     | offsetof-member-designator "[" expression "]"
10682     | offsetof-member-designator "->" id-expression  */
10683
10684static cp_expr
10685cp_parser_builtin_offsetof (cp_parser *parser)
10686{
10687  int save_ice_p, save_non_ice_p;
10688  tree type;
10689  cp_expr expr;
10690  cp_id_kind dummy;
10691  cp_token *token;
10692  location_t finish_loc;
10693
10694  /* We're about to accept non-integral-constant things, but will
10695     definitely yield an integral constant expression.  Save and
10696     restore these values around our local parsing.  */
10697  save_ice_p = parser->integral_constant_expression_p;
10698  save_non_ice_p = parser->non_integral_constant_expression_p;
10699
10700  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10701
10702  /* Consume the "__builtin_offsetof" token.  */
10703  cp_lexer_consume_token (parser->lexer);
10704  /* Consume the opening `('.  */
10705  matching_parens parens;
10706  parens.require_open (parser);
10707  /* Parse the type-id.  */
10708  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10709  {
10710    const char *saved_message = parser->type_definition_forbidden_message;
10711    parser->type_definition_forbidden_message
10712      = G_("types may not be defined within %<__builtin_offsetof%>");
10713    type = cp_parser_type_id (parser);
10714    parser->type_definition_forbidden_message = saved_message;
10715  }
10716  /* Look for the `,'.  */
10717  cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10718  token = cp_lexer_peek_token (parser->lexer);
10719
10720  /* Build the (type *)null that begins the traditional offsetof macro.  */
10721  tree object_ptr
10722    = build_static_cast (input_location, build_pointer_type (type),
10723			 null_pointer_node, tf_warning_or_error);
10724
10725  /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
10726  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10727						 true, &dummy, token->location);
10728  while (true)
10729    {
10730      token = cp_lexer_peek_token (parser->lexer);
10731      switch (token->type)
10732	{
10733	case CPP_OPEN_SQUARE:
10734	  /* offsetof-member-designator "[" expression "]" */
10735	  expr = cp_parser_postfix_open_square_expression (parser, expr,
10736							   true, false);
10737	  break;
10738
10739	case CPP_DEREF:
10740	  /* offsetof-member-designator "->" identifier */
10741	  expr = grok_array_decl (token->location, expr, integer_zero_node,
10742				  NULL, tf_warning_or_error);
10743	  /* FALLTHRU */
10744
10745	case CPP_DOT:
10746	  /* offsetof-member-designator "." identifier */
10747	  cp_lexer_consume_token (parser->lexer);
10748	  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10749							 expr, true, &dummy,
10750							 token->location);
10751	  break;
10752
10753	case CPP_CLOSE_PAREN:
10754	  /* Consume the ")" token.  */
10755	  finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10756	  cp_lexer_consume_token (parser->lexer);
10757	  goto success;
10758
10759	default:
10760	  /* Error.  We know the following require will fail, but
10761	     that gives the proper error message.  */
10762	  parens.require_close (parser);
10763	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10764	  expr = error_mark_node;
10765	  goto failure;
10766	}
10767    }
10768
10769 success:
10770  /* Make a location of the form:
10771       __builtin_offsetof (struct s, f)
10772       ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10773     with caret at the type-id, ranging from the start of the
10774     "_builtin_offsetof" token to the close paren.  */
10775  loc = make_location (loc, start_loc, finish_loc);
10776  /* The result will be an INTEGER_CST, so we need to explicitly
10777     preserve the location.  */
10778  expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10779
10780 failure:
10781  parser->integral_constant_expression_p = save_ice_p;
10782  parser->non_integral_constant_expression_p = save_non_ice_p;
10783
10784  expr = expr.maybe_add_location_wrapper ();
10785  return expr;
10786}
10787
10788/* Parse a trait expression.
10789
10790   Returns a representation of the expression, the underlying type
10791   of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
10792
10793static cp_expr
10794cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10795{
10796  cp_trait_kind kind;
10797  tree type1, type2 = NULL_TREE;
10798  bool binary = false;
10799  bool variadic = false;
10800
10801  switch (keyword)
10802    {
10803    case RID_HAS_NOTHROW_ASSIGN:
10804      kind = CPTK_HAS_NOTHROW_ASSIGN;
10805      break;
10806    case RID_HAS_NOTHROW_CONSTRUCTOR:
10807      kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10808      break;
10809    case RID_HAS_NOTHROW_COPY:
10810      kind = CPTK_HAS_NOTHROW_COPY;
10811      break;
10812    case RID_HAS_TRIVIAL_ASSIGN:
10813      kind = CPTK_HAS_TRIVIAL_ASSIGN;
10814      break;
10815    case RID_HAS_TRIVIAL_CONSTRUCTOR:
10816      kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10817      break;
10818    case RID_HAS_TRIVIAL_COPY:
10819      kind = CPTK_HAS_TRIVIAL_COPY;
10820      break;
10821    case RID_HAS_TRIVIAL_DESTRUCTOR:
10822      kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10823      break;
10824    case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10825      kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10826      break;
10827    case RID_HAS_VIRTUAL_DESTRUCTOR:
10828      kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10829      break;
10830    case RID_IS_ABSTRACT:
10831      kind = CPTK_IS_ABSTRACT;
10832      break;
10833    case RID_IS_AGGREGATE:
10834      kind = CPTK_IS_AGGREGATE;
10835      break;
10836    case RID_IS_BASE_OF:
10837      kind = CPTK_IS_BASE_OF;
10838      binary = true;
10839      break;
10840    case RID_IS_CLASS:
10841      kind = CPTK_IS_CLASS;
10842      break;
10843    case RID_IS_EMPTY:
10844      kind = CPTK_IS_EMPTY;
10845      break;
10846    case RID_IS_ENUM:
10847      kind = CPTK_IS_ENUM;
10848      break;
10849    case RID_IS_FINAL:
10850      kind = CPTK_IS_FINAL;
10851      break;
10852    case RID_IS_LAYOUT_COMPATIBLE:
10853      kind = CPTK_IS_LAYOUT_COMPATIBLE;
10854      binary = true;
10855      break;
10856    case RID_IS_LITERAL_TYPE:
10857      kind = CPTK_IS_LITERAL_TYPE;
10858      break;
10859    case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
10860      kind = CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF;
10861      binary = true;
10862      break;
10863    case RID_IS_POD:
10864      kind = CPTK_IS_POD;
10865      break;
10866    case RID_IS_POLYMORPHIC:
10867      kind = CPTK_IS_POLYMORPHIC;
10868      break;
10869    case RID_IS_SAME_AS:
10870      kind = CPTK_IS_SAME_AS;
10871      binary = true;
10872      break;
10873    case RID_IS_STD_LAYOUT:
10874      kind = CPTK_IS_STD_LAYOUT;
10875      break;
10876    case RID_IS_TRIVIAL:
10877      kind = CPTK_IS_TRIVIAL;
10878      break;
10879    case RID_IS_TRIVIALLY_ASSIGNABLE:
10880      kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10881      binary = true;
10882      break;
10883    case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10884      kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10885      variadic = true;
10886      break;
10887    case RID_IS_TRIVIALLY_COPYABLE:
10888      kind = CPTK_IS_TRIVIALLY_COPYABLE;
10889      break;
10890    case RID_IS_UNION:
10891      kind = CPTK_IS_UNION;
10892      break;
10893    case RID_UNDERLYING_TYPE:
10894      kind = CPTK_UNDERLYING_TYPE;
10895      break;
10896    case RID_BASES:
10897      kind = CPTK_BASES;
10898      break;
10899    case RID_DIRECT_BASES:
10900      kind = CPTK_DIRECT_BASES;
10901      break;
10902    case RID_IS_ASSIGNABLE:
10903      kind = CPTK_IS_ASSIGNABLE;
10904      binary = true;
10905      break;
10906    case RID_IS_CONSTRUCTIBLE:
10907      kind = CPTK_IS_CONSTRUCTIBLE;
10908      variadic = true;
10909      break;
10910    case RID_IS_NOTHROW_ASSIGNABLE:
10911      kind = CPTK_IS_NOTHROW_ASSIGNABLE;
10912      binary = true;
10913      break;
10914    case RID_IS_NOTHROW_CONSTRUCTIBLE:
10915      kind = CPTK_IS_NOTHROW_CONSTRUCTIBLE;
10916      variadic = true;
10917      break;
10918    default:
10919      gcc_unreachable ();
10920    }
10921
10922  /* Get location of initial token.  */
10923  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10924
10925  /* Consume the token.  */
10926  cp_lexer_consume_token (parser->lexer);
10927
10928  matching_parens parens;
10929  parens.require_open (parser);
10930
10931  {
10932    type_id_in_expr_sentinel s (parser);
10933    type1 = cp_parser_type_id (parser);
10934  }
10935
10936  if (type1 == error_mark_node)
10937    return error_mark_node;
10938
10939  if (binary)
10940    {
10941      cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10942
10943      {
10944	type_id_in_expr_sentinel s (parser);
10945	type2 = cp_parser_type_id (parser);
10946      }
10947
10948      if (type2 == error_mark_node)
10949	return error_mark_node;
10950    }
10951  else if (variadic)
10952    {
10953      while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10954	{
10955	  cp_lexer_consume_token (parser->lexer);
10956	  tree elt = cp_parser_type_id (parser);
10957	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10958	    {
10959	      cp_lexer_consume_token (parser->lexer);
10960	      elt = make_pack_expansion (elt);
10961	    }
10962	  if (elt == error_mark_node)
10963	    return error_mark_node;
10964	  type2 = tree_cons (NULL_TREE, elt, type2);
10965	}
10966      type2 = nreverse (type2);
10967    }
10968
10969  location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10970  parens.require_close (parser);
10971
10972  /* Construct a location of the form:
10973       __is_trivially_copyable(_Tp)
10974       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10975     with start == caret, finishing at the close-paren.  */
10976  location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10977
10978  /* Complete the trait expression, which may mean either processing
10979     the trait expr now or saving it for template instantiation.  */
10980  switch (kind)
10981    {
10982    case CPTK_UNDERLYING_TYPE:
10983      return cp_expr (finish_underlying_type (type1), trait_loc);
10984    case CPTK_BASES:
10985      return cp_expr (finish_bases (type1, false), trait_loc);
10986    case CPTK_DIRECT_BASES:
10987      return cp_expr (finish_bases (type1, true), trait_loc);
10988    default:
10989      return finish_trait_expr (trait_loc, kind, type1, type2);
10990    }
10991}
10992
10993/* Parse a lambda expression.
10994
10995   lambda-expression:
10996     lambda-introducer lambda-declarator [opt] compound-statement
10997     lambda-introducer < template-parameter-list > requires-clause [opt]
10998       lambda-declarator [opt] compound-statement
10999
11000   Returns a representation of the expression.  */
11001
11002static cp_expr
11003cp_parser_lambda_expression (cp_parser* parser)
11004{
11005  tree lambda_expr = build_lambda_expr ();
11006  tree type;
11007  bool ok = true;
11008  cp_token *token = cp_lexer_peek_token (parser->lexer);
11009  cp_token_position start = 0;
11010
11011  LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11012
11013  if (cxx_dialect >= cxx20)
11014    {
11015      /* C++20 allows lambdas in unevaluated context, but one in the type of a
11016	 non-type parameter is nonsensical.
11017
11018	 Distinguish a lambda in the parameter type from a lambda in the
11019	 default argument by looking at local_variables_forbidden_p, which is
11020	 only set in default arguments.  */
11021      if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11022	{
11023	  error_at (token->location,
11024		    "lambda-expression in template parameter type");
11025	  token->error_reported = true;
11026	  ok = false;
11027	}
11028    }
11029  else if (cp_unevaluated_operand)
11030    {
11031      if (!token->error_reported)
11032	{
11033	  error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11034		    "lambda-expression in unevaluated context"
11035		    " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11036	  token->error_reported = true;
11037	}
11038      ok = false;
11039    }
11040  else if (parser->in_template_argument_list_p || processing_template_parmlist)
11041    {
11042      if (!token->error_reported)
11043	{
11044	  error_at (token->location, "lambda-expression in template-argument"
11045		    " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11046	  token->error_reported = true;
11047	}
11048      ok = false;
11049    }
11050
11051  /* We may be in the middle of deferred access check.  Disable
11052     it now.  */
11053  push_deferring_access_checks (dk_no_deferred);
11054
11055  cp_parser_lambda_introducer (parser, lambda_expr);
11056  if (cp_parser_error_occurred (parser))
11057    return error_mark_node;
11058
11059  type = begin_lambda_type (lambda_expr);
11060  if (type == error_mark_node)
11061    return error_mark_node;
11062
11063  record_lambda_scope (lambda_expr);
11064
11065  /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
11066  determine_visibility (TYPE_NAME (type));
11067
11068  /* Now that we've started the type, add the capture fields for any
11069     explicit captures.  */
11070  register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11071
11072  {
11073    /* Inside the class, surrounding template-parameter-lists do not apply.  */
11074    unsigned int saved_num_template_parameter_lists
11075        = parser->num_template_parameter_lists;
11076    unsigned char in_statement = parser->in_statement;
11077    bool in_switch_statement_p = parser->in_switch_statement_p;
11078    bool fully_implicit_function_template_p
11079        = parser->fully_implicit_function_template_p;
11080    tree implicit_template_parms = parser->implicit_template_parms;
11081    cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11082    bool auto_is_implicit_function_template_parm_p
11083        = parser->auto_is_implicit_function_template_parm_p;
11084
11085    parser->num_template_parameter_lists = 0;
11086    parser->in_statement = 0;
11087    parser->in_switch_statement_p = false;
11088    parser->fully_implicit_function_template_p = false;
11089    parser->implicit_template_parms = 0;
11090    parser->implicit_template_scope = 0;
11091    parser->auto_is_implicit_function_template_parm_p = false;
11092
11093    /* The body of a lambda in a discarded statement is not discarded.  */
11094    bool discarded = in_discarded_stmt;
11095    in_discarded_stmt = 0;
11096
11097    /* Similarly the body of a lambda in immediate function context is not
11098       in immediate function context.  */
11099    bool save_in_consteval_if_p = in_consteval_if_p;
11100    in_consteval_if_p = false;
11101
11102    /* By virtue of defining a local class, a lambda expression has access to
11103       the private variables of enclosing classes.  */
11104
11105    if (cp_parser_start_tentative_firewall (parser))
11106      start = token;
11107
11108    ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11109
11110    if (ok && cp_parser_error_occurred (parser))
11111      ok = false;
11112
11113    if (ok)
11114      {
11115	cp_parser_lambda_body (parser, lambda_expr);
11116      }
11117    else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11118      {
11119	if (cp_parser_skip_to_closing_brace (parser))
11120	  cp_lexer_consume_token (parser->lexer);
11121      }
11122
11123    /* The capture list was built up in reverse order; fix that now.  */
11124    LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11125      = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11126
11127    if (ok)
11128      maybe_add_lambda_conv_op (type);
11129
11130    finish_struct (type, /*attributes=*/NULL_TREE);
11131
11132    in_consteval_if_p = save_in_consteval_if_p;
11133    in_discarded_stmt = discarded;
11134
11135    parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11136    parser->in_statement = in_statement;
11137    parser->in_switch_statement_p = in_switch_statement_p;
11138    parser->fully_implicit_function_template_p
11139	= fully_implicit_function_template_p;
11140    parser->implicit_template_parms = implicit_template_parms;
11141    parser->implicit_template_scope = implicit_template_scope;
11142    parser->auto_is_implicit_function_template_parm_p
11143	= auto_is_implicit_function_template_parm_p;
11144  }
11145
11146  /* This field is only used during parsing of the lambda.  */
11147  LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11148
11149  /* This lambda shouldn't have any proxies left at this point.  */
11150  gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11151  /* And now that we're done, push proxies for an enclosing lambda.  */
11152  insert_pending_capture_proxies ();
11153
11154  /* Update the lambda expression to a range.  */
11155  LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11156						      token->location,
11157						      parser->lexer);
11158
11159  if (ok)
11160    lambda_expr = build_lambda_object (lambda_expr);
11161  else
11162    lambda_expr = error_mark_node;
11163
11164  cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11165
11166  pop_deferring_access_checks ();
11167
11168  return lambda_expr;
11169}
11170
11171/* Parse the beginning of a lambda expression.
11172
11173   lambda-introducer:
11174     [ lambda-capture [opt] ]
11175
11176   LAMBDA_EXPR is the current representation of the lambda expression.  */
11177
11178static void
11179cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11180{
11181  /* Need commas after the first capture.  */
11182  bool first = true;
11183
11184  /* Eat the leading `['.  */
11185  cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11186
11187  /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
11188  if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11189      && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11190      && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11191      && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11192    LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11193  else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11194    LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11195
11196  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11197    {
11198      cp_lexer_consume_token (parser->lexer);
11199      first = false;
11200
11201      if (!(at_function_scope_p () || parsing_nsdmi ()))
11202	error ("non-local lambda expression cannot have a capture-default");
11203    }
11204
11205  hash_set<tree, true> ids;
11206  tree first_capture_id = NULL_TREE;
11207  while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11208    {
11209      cp_token* capture_token;
11210      tree capture_id;
11211      tree capture_init_expr;
11212      cp_id_kind idk = CP_ID_KIND_NONE;
11213      bool explicit_init_p = false;
11214
11215      enum capture_kind_type
11216      {
11217	BY_COPY,
11218	BY_REFERENCE
11219      };
11220      enum capture_kind_type capture_kind = BY_COPY;
11221
11222      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11223	{
11224	  error ("expected end of capture-list");
11225	  return;
11226	}
11227
11228      if (first)
11229	first = false;
11230      else
11231	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11232
11233      /* Possibly capture `this'.  */
11234      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11235	{
11236	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11237	  if (cxx_dialect < cxx20 && pedantic
11238	      && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11239	    pedwarn (loc, OPT_Wc__20_extensions,
11240		     "explicit by-copy capture of %<this%> "
11241		     "with by-copy capture default only available with "
11242		     "%<-std=c++20%> or %<-std=gnu++20%>");
11243	  cp_lexer_consume_token (parser->lexer);
11244	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11245	    pedwarn (input_location, 0,
11246		     "already captured %qD in lambda expression",
11247		     this_identifier);
11248	  else
11249	    add_capture (lambda_expr, /*id=*/this_identifier,
11250			 /*initializer=*/finish_this_expr (),
11251			 /*by_reference_p=*/true, explicit_init_p);
11252	  continue;
11253	}
11254
11255      /* Possibly capture `*this'.  */
11256      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11257	  && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11258	{
11259	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11260	  if (cxx_dialect < cxx17)
11261	    pedwarn (loc, OPT_Wc__17_extensions,
11262		     "%<*this%> capture only available with "
11263		     "%<-std=c++17%> or %<-std=gnu++17%>");
11264	  cp_lexer_consume_token (parser->lexer);
11265	  cp_lexer_consume_token (parser->lexer);
11266	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11267	    pedwarn (input_location, 0,
11268		     "already captured %qD in lambda expression",
11269		     this_identifier);
11270	  else
11271	    add_capture (lambda_expr, /*id=*/this_identifier,
11272			 /*initializer=*/finish_this_expr (),
11273			 /*by_reference_p=*/false, explicit_init_p);
11274	  continue;
11275	}
11276
11277      /* But reject `&this'.  */
11278      if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11279	  && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11280	{
11281	  error_at (cp_lexer_peek_token (parser->lexer)->location,
11282		    "%<this%> cannot be captured by reference");
11283	  cp_lexer_consume_token (parser->lexer);
11284	  cp_lexer_consume_token (parser->lexer);
11285	  continue;
11286	}
11287
11288      /* Remember whether we want to capture as a reference or not.  */
11289      if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11290	{
11291	  capture_kind = BY_REFERENCE;
11292	  cp_lexer_consume_token (parser->lexer);
11293	}
11294
11295      bool init_pack_expansion = false;
11296      location_t ellipsis_loc = UNKNOWN_LOCATION;
11297      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11298	{
11299	  ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11300	  if (cxx_dialect < cxx20)
11301	    pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11302		     "pack init-capture only available with "
11303		     "%<-std=c++20%> or %<-std=gnu++20%>");
11304	  cp_lexer_consume_token (parser->lexer);
11305	  init_pack_expansion = true;
11306	}
11307
11308      /* Early C++20 drafts had ...& instead of &...; be forgiving.  */
11309      if (init_pack_expansion && capture_kind != BY_REFERENCE
11310	  && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11311	{
11312	  pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11313		   0, "%<&%> should come before %<...%>");
11314	  capture_kind = BY_REFERENCE;
11315	  cp_lexer_consume_token (parser->lexer);
11316	}
11317
11318      /* Get the identifier.  */
11319      capture_token = cp_lexer_peek_token (parser->lexer);
11320      capture_id = cp_parser_identifier (parser);
11321
11322      if (capture_id == error_mark_node)
11323	/* Would be nice to have a cp_parser_skip_to_closing_x for general
11324           delimiters, but I modified this to stop on unnested ']' as well.  It
11325           was already changed to stop on unnested '}', so the
11326           "closing_parenthesis" name is no more misleading with my change.  */
11327	{
11328	  cp_parser_skip_to_closing_parenthesis (parser,
11329						 /*recovering=*/true,
11330						 /*or_comma=*/true,
11331						 /*consume_paren=*/true);
11332	  break;
11333	}
11334
11335      /* Find the initializer for this capture.  */
11336      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11337	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11338	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11339	{
11340	  bool direct, non_constant;
11341	  /* An explicit initializer exists.  */
11342	  if (cxx_dialect < cxx14)
11343	    pedwarn (input_location, OPT_Wc__14_extensions,
11344		     "lambda capture initializers "
11345		     "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11346	  capture_init_expr = cp_parser_initializer (parser, &direct,
11347						     &non_constant, true);
11348	  explicit_init_p = true;
11349	  if (capture_init_expr == NULL_TREE)
11350	    {
11351	      error ("empty initializer for lambda init-capture");
11352	      capture_init_expr = error_mark_node;
11353	    }
11354	  if (init_pack_expansion)
11355	    capture_init_expr = make_pack_expansion (capture_init_expr);
11356	}
11357      else
11358	{
11359	  const char* error_msg;
11360
11361	  /* Turn the identifier into an id-expression.  */
11362	  capture_init_expr
11363	    = cp_parser_lookup_name_simple (parser, capture_id,
11364					    capture_token->location);
11365
11366	  if (capture_init_expr == error_mark_node)
11367	    {
11368	      unqualified_name_lookup_error (capture_id);
11369	      continue;
11370	    }
11371	  else if (!VAR_P (capture_init_expr)
11372		   && TREE_CODE (capture_init_expr) != PARM_DECL)
11373	    {
11374	      error_at (capture_token->location,
11375			"capture of non-variable %qE",
11376			capture_init_expr);
11377	      if (DECL_P (capture_init_expr))
11378		inform (DECL_SOURCE_LOCATION (capture_init_expr),
11379			"%q#D declared here", capture_init_expr);
11380	      continue;
11381	    }
11382	  if (VAR_P (capture_init_expr)
11383	      && decl_storage_duration (capture_init_expr) != dk_auto)
11384	    {
11385	      if (pedwarn (capture_token->location, 0, "capture of variable "
11386			   "%qD with non-automatic storage duration",
11387			   capture_init_expr))
11388		inform (DECL_SOURCE_LOCATION (capture_init_expr),
11389			"%q#D declared here", capture_init_expr);
11390	      continue;
11391	    }
11392
11393	  capture_init_expr
11394            = finish_id_expression
11395                (capture_id,
11396		 capture_init_expr,
11397                 parser->scope,
11398                 &idk,
11399                 /*integral_constant_expression_p=*/false,
11400                 /*allow_non_integral_constant_expression_p=*/false,
11401                 /*non_integral_constant_expression_p=*/NULL,
11402                 /*template_p=*/false,
11403                 /*done=*/true,
11404                 /*address_p=*/false,
11405                 /*template_arg_p=*/false,
11406                 &error_msg,
11407                 capture_token->location);
11408
11409	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11410	    {
11411	      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11412	      cp_lexer_consume_token (parser->lexer);
11413	      capture_init_expr = make_pack_expansion (capture_init_expr);
11414	      if (init_pack_expansion)
11415		{
11416		  /* If what follows is an initializer, the second '...' is
11417		     invalid.  But for cases like [...xs...], the first one
11418		     is invalid.  */
11419		  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11420		      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11421		      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11422		    ellipsis_loc = loc;
11423		  error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11424		  continue;
11425		}
11426	    }
11427	}
11428
11429      if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11430	  && !explicit_init_p)
11431	{
11432	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11433	      && capture_kind == BY_COPY)
11434	    pedwarn (capture_token->location, 0, "explicit by-copy capture "
11435		     "of %qD redundant with by-copy capture default",
11436		     capture_id);
11437	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11438	      && capture_kind == BY_REFERENCE)
11439	    pedwarn (capture_token->location, 0, "explicit by-reference "
11440		     "capture of %qD redundant with by-reference capture "
11441		     "default", capture_id);
11442	}
11443
11444      /* Check for duplicates.
11445	 Optimize for the zero or one explicit captures cases and only create
11446	 the hash_set after adding second capture.  */
11447      bool found = false;
11448      if (!ids.is_empty ())
11449	found = ids.add (capture_id);
11450      else if (first_capture_id == NULL_TREE)
11451	first_capture_id = capture_id;
11452      else if (capture_id == first_capture_id)
11453	found = true;
11454      else
11455	{
11456	  ids.add (first_capture_id);
11457	  ids.add (capture_id);
11458	}
11459      if (found)
11460	pedwarn (input_location, 0,
11461		 "already captured %qD in lambda expression", capture_id);
11462      else
11463	add_capture (lambda_expr, capture_id, capture_init_expr,
11464		     /*by_reference_p=*/capture_kind == BY_REFERENCE,
11465		     explicit_init_p);
11466
11467      /* If there is any qualification still in effect, clear it
11468	 now; we will be starting fresh with the next capture.  */
11469      parser->scope = NULL_TREE;
11470      parser->qualifying_scope = NULL_TREE;
11471      parser->object_scope = NULL_TREE;
11472    }
11473
11474  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11475}
11476
11477/* Parse the (optional) middle of a lambda expression.
11478
11479   lambda-declarator:
11480     ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11481     lambda-specifiers (C++23)
11482
11483   lambda-specifiers:
11484     decl-specifier-seq [opt] noexcept-specifier [opt]
11485       attribute-specifier-seq [opt] trailing-return-type [opt]
11486
11487   LAMBDA_EXPR is the current representation of the lambda expression.  */
11488
11489static bool
11490cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11491{
11492  /* 5.1.1.4 of the standard says:
11493       If a lambda-expression does not include a lambda-declarator, it is as if
11494       the lambda-declarator were ().
11495     This means an empty parameter list, no attributes, and no exception
11496     specification.  */
11497  tree param_list = void_list_node;
11498  tree std_attrs = NULL_TREE;
11499  tree gnu_attrs = NULL_TREE;
11500  tree exception_spec = NULL_TREE;
11501  tree template_param_list = NULL_TREE;
11502  tree tx_qual = NULL_TREE;
11503  tree return_type = NULL_TREE;
11504  tree trailing_requires_clause = NULL_TREE;
11505  bool has_param_list = false;
11506  location_t omitted_parms_loc = UNKNOWN_LOCATION;
11507  cp_decl_specifier_seq lambda_specs;
11508  clear_decl_specs (&lambda_specs);
11509  /* A lambda op() is const unless explicitly 'mutable'.  */
11510  cp_cv_quals quals = TYPE_QUAL_CONST;
11511
11512  /* The template-parameter-list is optional, but must begin with
11513     an opening angle if present.  */
11514  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11515    {
11516      if (cxx_dialect < cxx14)
11517	pedwarn (parser->lexer->next_token->location, OPT_Wc__14_extensions,
11518		 "lambda templates are only available with "
11519		 "%<-std=c++14%> or %<-std=gnu++14%>");
11520      else if (pedantic && cxx_dialect < cxx20)
11521	pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11522		 "lambda templates are only available with "
11523		 "%<-std=c++20%> or %<-std=gnu++20%>");
11524
11525      cp_lexer_consume_token (parser->lexer);
11526
11527      template_param_list = cp_parser_template_parameter_list (parser);
11528      cp_parser_require_end_of_template_parameter_list (parser);
11529
11530      /* We may have a constrained generic lambda; parse the requires-clause
11531	 immediately after the template-parameter-list and combine with any
11532	 shorthand constraints present.  */
11533      tree dreqs = cp_parser_requires_clause_opt (parser, true);
11534      if (flag_concepts)
11535	{
11536	  tree reqs = get_shorthand_constraints (current_template_parms);
11537	  if (dreqs)
11538	    reqs = combine_constraint_expressions (reqs, dreqs);
11539	  TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11540	}
11541
11542      /* We just processed one more parameter list.  */
11543      ++parser->num_template_parameter_lists;
11544    }
11545
11546  /* Committee discussion supports allowing attributes here.  */
11547  lambda_specs.attributes = cp_parser_attributes_opt (parser);
11548
11549  /* The parameter-declaration-clause is optional (unless
11550     template-parameter-list was given), but must begin with an
11551     opening parenthesis if present.  */
11552  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11553    {
11554      bool is_consteval = false;
11555      /* For C++20, before parsing the parameter list check if there is
11556	 a consteval specifier in the corresponding decl-specifier-seq.  */
11557      if (cxx_dialect >= cxx20)
11558	{
11559	  for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11560	       cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11561	    {
11562	      if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11563		  == RID_CONSTEVAL)
11564		{
11565		  is_consteval = true;
11566		  break;
11567		}
11568	    }
11569	}
11570
11571      matching_parens parens;
11572      parens.consume_open (parser);
11573
11574      begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11575
11576      if (is_consteval)
11577	current_binding_level->immediate_fn_ctx_p = true;
11578
11579      /* Parse parameters.  */
11580      param_list
11581	= cp_parser_parameter_declaration_clause
11582	    (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11583
11584      /* Default arguments shall not be specified in the
11585	 parameter-declaration-clause of a lambda-declarator.  */
11586      if (pedantic && cxx_dialect < cxx14)
11587	for (tree t = param_list; t; t = TREE_CHAIN (t))
11588	  if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11589	    pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11590		     OPT_Wc__14_extensions,
11591		     "default argument specified for lambda parameter");
11592
11593      parens.require_close (parser);
11594      has_param_list = true;
11595    }
11596  else if (cxx_dialect < cxx23)
11597    omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11598
11599  /* In the decl-specifier-seq of the lambda-declarator, each
11600     decl-specifier shall either be mutable or constexpr.  */
11601  int declares_class_or_enum;
11602  if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11603    cp_parser_decl_specifier_seq (parser,
11604				  CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11605				  &lambda_specs, &declares_class_or_enum);
11606
11607  if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11608    {
11609      pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11610	       "parameter declaration before lambda declaration "
11611	       "specifiers only optional with %<-std=c++2b%> or "
11612	       "%<-std=gnu++2b%>");
11613      omitted_parms_loc = UNKNOWN_LOCATION;
11614    }
11615
11616  if (lambda_specs.storage_class == sc_mutable)
11617    {
11618      LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11619      quals = TYPE_UNQUALIFIED;
11620      if (lambda_specs.conflicting_specifiers_p)
11621	error_at (lambda_specs.locations[ds_storage_class],
11622		  "duplicate %<mutable%>");
11623    }
11624
11625  tx_qual = cp_parser_tx_qualifier_opt (parser);
11626  if (omitted_parms_loc && tx_qual)
11627    {
11628      pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11629	       "parameter declaration before lambda transaction "
11630	       "qualifier only optional with %<-std=c++2b%> or "
11631	       "%<-std=gnu++2b%>");
11632      omitted_parms_loc = UNKNOWN_LOCATION;
11633    }
11634
11635  /* Parse optional exception specification.  */
11636  exception_spec
11637    = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11638
11639  if (omitted_parms_loc && exception_spec)
11640    {
11641      pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11642	       "parameter declaration before lambda exception "
11643	       "specification only optional with %<-std=c++2b%> or "
11644	       "%<-std=gnu++2b%>");
11645      omitted_parms_loc = UNKNOWN_LOCATION;
11646    }
11647
11648  /* GCC 8 accepted attributes here, and this is the place for standard C++11
11649     attributes that appertain to the function type.  */
11650  if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11651    gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11652  else
11653    std_attrs = cp_parser_std_attribute_spec_seq (parser);
11654
11655  /* Parse optional trailing return type.  */
11656  if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11657    {
11658      if (omitted_parms_loc)
11659	pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11660		 "parameter declaration before lambda trailing "
11661		 "return type only optional with %<-std=c++2b%> or "
11662		 "%<-std=gnu++2b%>");
11663      cp_lexer_consume_token (parser->lexer);
11664      return_type = cp_parser_trailing_type_id (parser);
11665    }
11666
11667  /* Also allow GNU attributes at the very end of the declaration, the usual
11668     place for GNU attributes.  */
11669  if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11670    gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11671
11672  if (has_param_list)
11673    {
11674      /* Parse optional trailing requires clause.  */
11675      trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11676
11677      /* The function parameters must be in scope all the way until after the
11678         trailing-return-type in case of decltype.  */
11679      pop_bindings_and_leave_scope ();
11680    }
11681
11682  /* Create the function call operator.
11683
11684     Messing with declarators like this is no uglier than building up the
11685     FUNCTION_DECL by hand, and this is less likely to get out of sync with
11686     other code.  */
11687  {
11688    cp_decl_specifier_seq return_type_specs;
11689    cp_declarator* declarator;
11690    tree fco;
11691    void *p;
11692
11693    clear_decl_specs (&return_type_specs);
11694    return_type_specs.type = make_auto ();
11695
11696    if (lambda_specs.locations[ds_constexpr])
11697      {
11698	if (cxx_dialect >= cxx17)
11699	  return_type_specs.locations[ds_constexpr]
11700	    = lambda_specs.locations[ds_constexpr];
11701	else
11702	  error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11703		    "lambda only available with %<-std=c++17%> or "
11704		    "%<-std=gnu++17%>");
11705      }
11706    if (lambda_specs.locations[ds_consteval])
11707      return_type_specs.locations[ds_consteval]
11708	= lambda_specs.locations[ds_consteval];
11709
11710    p = obstack_alloc (&declarator_obstack, 0);
11711
11712    declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11713				     LAMBDA_EXPR_LOCATION (lambda_expr));
11714
11715    declarator = make_call_declarator (declarator, param_list, quals,
11716				       VIRT_SPEC_UNSPECIFIED,
11717                                       REF_QUAL_NONE,
11718				       tx_qual,
11719				       exception_spec,
11720                                       return_type,
11721				       trailing_requires_clause,
11722				       UNKNOWN_LOCATION);
11723    declarator->std_attributes = std_attrs;
11724
11725    fco = grokmethod (&return_type_specs,
11726		      declarator,
11727		      chainon (gnu_attrs, lambda_specs.attributes));
11728    if (fco != error_mark_node)
11729      {
11730	DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11731	DECL_ARTIFICIAL (fco) = 1;
11732	/* Give the object parameter a different name.  */
11733	DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11734	DECL_SET_LAMBDA_FUNCTION (fco, true);
11735      }
11736    if (template_param_list)
11737      {
11738	fco = finish_member_template_decl (fco);
11739	finish_template_decl (template_param_list);
11740	--parser->num_template_parameter_lists;
11741      }
11742    else if (parser->fully_implicit_function_template_p)
11743      fco = finish_fully_implicit_template (parser, fco);
11744
11745    finish_member_declaration (fco);
11746
11747    obstack_free (&declarator_obstack, p);
11748
11749    return (fco != error_mark_node);
11750  }
11751}
11752
11753/* Parse the body of a lambda expression, which is simply
11754
11755   compound-statement
11756
11757   but which requires special handling.
11758   LAMBDA_EXPR is the current representation of the lambda expression.  */
11759
11760static void
11761cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11762{
11763  bool nested = (current_function_decl != NULL_TREE);
11764  unsigned char local_variables_forbidden_p
11765    = parser->local_variables_forbidden_p;
11766  bool in_function_body = parser->in_function_body;
11767
11768  /* The body of a lambda-expression is not a subexpression of the enclosing
11769     expression.  */
11770  cp_evaluated ev;
11771
11772  if (nested)
11773    push_function_context ();
11774  else
11775    /* Still increment function_depth so that we don't GC in the
11776       middle of an expression.  */
11777    ++function_depth;
11778
11779  auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11780  auto ord = make_temp_override (parser->oacc_routine, NULL);
11781  auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11782  vec<tree> omp_privatization_save;
11783  save_omp_privatization_clauses (omp_privatization_save);
11784  /* Clear this in case we're in the middle of a default argument.  */
11785  parser->local_variables_forbidden_p = 0;
11786  parser->in_function_body = true;
11787
11788  {
11789    local_specialization_stack s (lss_copy);
11790    tree fco = lambda_function (lambda_expr);
11791    tree body = start_lambda_function (fco, lambda_expr);
11792
11793    /* Originally C++11 required us to peek for 'return expr'; and
11794       process it specially here to deduce the return type.  N3638
11795       removed the need for that.  */
11796    cp_parser_function_body (parser, false);
11797
11798    finish_lambda_function (body);
11799  }
11800
11801  restore_omp_privatization_clauses (omp_privatization_save);
11802  parser->local_variables_forbidden_p = local_variables_forbidden_p;
11803  parser->in_function_body = in_function_body;
11804  if (nested)
11805    pop_function_context();
11806  else
11807    --function_depth;
11808}
11809
11810/* Statements [gram.stmt.stmt]  */
11811
11812/* Build and add a DEBUG_BEGIN_STMT statement with location LOC.  */
11813
11814static void
11815add_debug_begin_stmt (location_t loc)
11816{
11817  if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11818    return;
11819  if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11820    /* A concept is never expanded normally.  */
11821    return;
11822
11823  tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11824  SET_EXPR_LOCATION (stmt, loc);
11825  add_stmt (stmt);
11826}
11827
11828struct cp_omp_attribute_data
11829{
11830  cp_token_cache *tokens;
11831  const c_omp_directive *dir;
11832  c_omp_directive_kind kind;
11833};
11834
11835/* Handle omp::directive and omp::sequence attributes in ATTRS
11836   (if any) at the start of a statement or in attribute-declaration.  */
11837
11838static tree
11839cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
11840{
11841  if (!flag_openmp && !flag_openmp_simd)
11842    return attrs;
11843
11844  auto_vec<cp_omp_attribute_data, 16> vec;
11845  int cnt = 0;
11846  int tokens = 0;
11847  bool bad = false;
11848  for (tree *pa = &attrs; *pa; )
11849    if (get_attribute_namespace (*pa) == omp_identifier
11850	&& is_attribute_p ("directive", get_attribute_name (*pa)))
11851      {
11852	cnt++;
11853	for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
11854	  {
11855	    tree d = TREE_VALUE (a);
11856	    gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
11857	    cp_token *first = DEFPARSE_TOKENS (d)->first;
11858	    cp_token *last = DEFPARSE_TOKENS (d)->last;
11859	    if (parser->omp_attrs_forbidden_p)
11860	      {
11861		error_at (first->location,
11862			  "mixing OpenMP directives with attribute and pragma "
11863			  "syntax on the same statement");
11864		parser->omp_attrs_forbidden_p = false;
11865		bad = true;
11866	      }
11867	    const char *directive[3] = {};
11868	    for (int i = 0; i < 3; i++)
11869	      {
11870		tree id = NULL_TREE;
11871		if (first + i == last)
11872		  break;
11873		if (first[i].type == CPP_NAME)
11874		  id = first[i].u.value;
11875		else if (first[i].type == CPP_KEYWORD)
11876		  id = ridpointers[(int) first[i].keyword];
11877		else
11878		  break;
11879		directive[i] = IDENTIFIER_POINTER (id);
11880	      }
11881	    const c_omp_directive *dir = NULL;
11882	    if (directive[0])
11883	      dir = c_omp_categorize_directive (directive[0], directive[1],
11884						directive[2]);
11885	    if (dir == NULL)
11886	      {
11887		error_at (first->location,
11888			  "unknown OpenMP directive name in %<omp::directive%>"
11889			  " attribute argument");
11890		continue;
11891	      }
11892	    c_omp_directive_kind kind = dir->kind;
11893	    if (dir->id == PRAGMA_OMP_ORDERED)
11894	      {
11895		/* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
11896		   depend clause.  */
11897		if (directive[1] && strcmp (directive[1], "depend") == 0)
11898		  kind = C_OMP_DIR_STANDALONE;
11899		else if (first + 2 < last
11900			 && first[1].type == CPP_COMMA
11901			 && first[2].type == CPP_NAME
11902			 && strcmp (IDENTIFIER_POINTER (first[2].u.value),
11903				    "depend") == 0)
11904		  kind = C_OMP_DIR_STANDALONE;
11905	      }
11906	    else if (dir->id == PRAGMA_OMP_ERROR)
11907	      {
11908		/* error with at(execution) clause is C_OMP_DIR_STANDALONE.  */
11909		int paren_depth = 0;
11910		for (int i = 1; first + i < last; i++)
11911		  if (first[i].type == CPP_OPEN_PAREN)
11912		    paren_depth++;
11913		  else if (first[i].type == CPP_CLOSE_PAREN)
11914		    paren_depth--;
11915		  else if (paren_depth == 0
11916			   && first + i + 2 < last
11917			   && first[i].type == CPP_NAME
11918			   && first[i + 1].type == CPP_OPEN_PAREN
11919			   && first[i + 2].type == CPP_NAME
11920			   && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
11921				       "at")
11922			   && !strcmp (IDENTIFIER_POINTER (first[i
11923								 + 2].u.value),
11924				       "execution"))
11925		    {
11926		      kind = C_OMP_DIR_STANDALONE;
11927		      break;
11928		    }
11929	      }
11930	    cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
11931	    vec.safe_push (v);
11932	    if (flag_openmp || dir->simd)
11933	      tokens += (last - first) + 1;
11934	  }
11935	cp_omp_attribute_data v = {};
11936	vec.safe_push (v);
11937	*pa = TREE_CHAIN (*pa);
11938      }
11939    else
11940      pa = &TREE_CHAIN (*pa);
11941
11942  if (bad)
11943    return attrs;
11944
11945  unsigned int i;
11946  cp_omp_attribute_data *v;
11947  cp_omp_attribute_data *construct_seen = nullptr;
11948  cp_omp_attribute_data *standalone_seen = nullptr;
11949  cp_omp_attribute_data *prev_standalone_seen = nullptr;
11950  FOR_EACH_VEC_ELT (vec, i, v)
11951    if (v->tokens)
11952      {
11953	if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
11954	  construct_seen = v;
11955	else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
11956	  standalone_seen = v;
11957      }
11958    else
11959      {
11960	if (standalone_seen && !prev_standalone_seen)
11961	  {
11962	    prev_standalone_seen = standalone_seen;
11963	    standalone_seen = nullptr;
11964	  }
11965      }
11966
11967  if (cnt > 1 && construct_seen)
11968    {
11969      error_at (construct_seen->tokens->first->location,
11970		"OpenMP construct among %<omp::directive%> attributes"
11971		" requires all %<omp::directive%> attributes on the"
11972		" same statement to be in the same %<omp::sequence%>");
11973      return attrs;
11974    }
11975  if (cnt > 1 && standalone_seen && prev_standalone_seen)
11976    {
11977      error_at (standalone_seen->tokens->first->location,
11978		"multiple OpenMP standalone directives among"
11979		" %<omp::directive%> attributes must be all within the"
11980		" same %<omp::sequence%>");
11981      return attrs;
11982    }
11983
11984  if (prev_standalone_seen)
11985    standalone_seen = prev_standalone_seen;
11986  if (standalone_seen
11987      && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11988    {
11989      error_at (standalone_seen->tokens->first->location,
11990		"standalone OpenMP directives in %<omp::directive%> attribute"
11991		" can only appear on an empty statement");
11992      return attrs;
11993    }
11994  if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
11995    {
11996      cp_token *token = cp_lexer_peek_token (parser->lexer);
11997      enum pragma_kind kind = cp_parser_pragma_kind (token);
11998      if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
11999	{
12000	  error_at (token->location,
12001		    "mixing OpenMP directives with attribute and pragma "
12002		    "syntax on the same statement");
12003	  return attrs;
12004	}
12005    }
12006
12007  if (!tokens)
12008    return attrs;
12009  tokens++;
12010  cp_lexer *lexer = cp_lexer_alloc ();
12011  lexer->debugging_p = parser->lexer->debugging_p;
12012  vec_safe_reserve (lexer->buffer, tokens, true);
12013  FOR_EACH_VEC_ELT (vec, i, v)
12014    {
12015      if (!v->tokens)
12016	continue;
12017      if (!flag_openmp && !v->dir->simd)
12018	continue;
12019      cp_token *first = v->tokens->first;
12020      cp_token *last = v->tokens->last;
12021      cp_token tok = {};
12022      tok.type = CPP_PRAGMA;
12023      tok.keyword = RID_MAX;
12024      tok.u.value = build_int_cst (NULL, v->dir->id);
12025      tok.location = first->location;
12026      lexer->buffer->quick_push (tok);
12027      while (++first < last)
12028	lexer->buffer->quick_push (*first);
12029      tok = {};
12030      tok.type = CPP_PRAGMA_EOL;
12031      tok.keyword = RID_MAX;
12032      tok.location = last->location;
12033      lexer->buffer->quick_push (tok);
12034    }
12035  cp_token tok = {};
12036  tok.type = CPP_EOF;
12037  tok.keyword = RID_MAX;
12038  tok.location = lexer->buffer->last ().location;
12039  lexer->buffer->quick_push (tok);
12040  lexer->next = parser->lexer;
12041  lexer->next_token = lexer->buffer->address ();
12042  lexer->last_token = lexer->next_token
12043		      + lexer->buffer->length ()
12044		      - 1;
12045  lexer->in_omp_attribute_pragma = true;
12046  parser->lexer = lexer;
12047  /* Move the current source position to that of the first token in the
12048     new lexer.  */
12049  cp_lexer_set_source_position_from_token (lexer->next_token);
12050  return attrs;
12051}
12052
12053/* Handle omp::directive and omp::sequence attributes in *PATTRS
12054   (if any) at the start or after declaration-id of a declaration.  */
12055
12056static void
12057cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12058					   cp_omp_declare_simd_data *data,
12059					   bool start)
12060{
12061  if (!flag_openmp && !flag_openmp_simd)
12062    return;
12063
12064  int cnt = 0;
12065  bool bad = false;
12066  bool variant_p = false;
12067  location_t loc = UNKNOWN_LOCATION;
12068  for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12069    if (get_attribute_namespace (pa) == omp_identifier
12070	&& is_attribute_p ("directive", get_attribute_name (pa)))
12071      {
12072	for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12073	  {
12074	    tree d = TREE_VALUE (a);
12075	    gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12076	    cp_token *first = DEFPARSE_TOKENS (d)->first;
12077	    cp_token *last = DEFPARSE_TOKENS (d)->last;
12078	    const char *directive[3] = {};
12079	    for (int i = 0; i < 3; i++)
12080	      {
12081		tree id = NULL_TREE;
12082		if (first + i == last)
12083		  break;
12084		if (first[i].type == CPP_NAME)
12085		  id = first[i].u.value;
12086		else if (first[i].type == CPP_KEYWORD)
12087		  id = ridpointers[(int) first[i].keyword];
12088		else
12089		  break;
12090		directive[i] = IDENTIFIER_POINTER (id);
12091	      }
12092	    const c_omp_directive *dir = NULL;
12093	    if (directive[0])
12094	      dir = c_omp_categorize_directive (directive[0], directive[1],
12095						directive[2]);
12096	    if (dir == NULL)
12097	      continue;
12098	    if (dir->id == PRAGMA_OMP_DECLARE
12099		&& (strcmp (directive[1], "simd") == 0
12100		    || strcmp (directive[1], "variant") == 0))
12101	      {
12102		if (cnt++ == 0)
12103		  {
12104		    variant_p = strcmp (directive[1], "variant") == 0;
12105		    loc = first->location;
12106		  }
12107		if (start && parser->omp_declare_simd && !bad)
12108		  {
12109		    error_at (first->location,
12110			      "mixing OpenMP directives with attribute and "
12111			      "pragma syntax on the same declaration");
12112		    bad = true;
12113		  }
12114	      }
12115	  }
12116      }
12117
12118  if (bad)
12119    {
12120      for (tree *pa = pattrs; *pa; )
12121	if (get_attribute_namespace (*pa) == omp_identifier
12122	    && is_attribute_p ("directive", get_attribute_name (*pa)))
12123	  *pa = TREE_CHAIN (*pa);
12124	else
12125	  pa = &TREE_CHAIN (*pa);
12126      return;
12127    }
12128  if (cnt == 0)
12129    return;
12130
12131  if (parser->omp_declare_simd == NULL)
12132    {
12133      data->error_seen = false;
12134      data->fndecl_seen = false;
12135      data->variant_p = variant_p;
12136      data->loc = loc;
12137      data->tokens = vNULL;
12138      data->attribs[0] = NULL;
12139      data->attribs[1] = NULL;
12140      parser->omp_declare_simd = data;
12141    }
12142  parser->omp_declare_simd->attribs[!start] = pattrs;
12143}
12144
12145/* Parse a statement.
12146
12147   statement:
12148     labeled-statement
12149     expression-statement
12150     compound-statement
12151     selection-statement
12152     iteration-statement
12153     jump-statement
12154     declaration-statement
12155     try-block
12156
12157  C++11:
12158
12159  statement:
12160    labeled-statement
12161    attribute-specifier-seq (opt) expression-statement
12162    attribute-specifier-seq (opt) compound-statement
12163    attribute-specifier-seq (opt) selection-statement
12164    attribute-specifier-seq (opt) iteration-statement
12165    attribute-specifier-seq (opt) jump-statement
12166    declaration-statement
12167    attribute-specifier-seq (opt) try-block
12168
12169  init-statement:
12170    expression-statement
12171    simple-declaration
12172    alias-declaration
12173
12174  TM Extension:
12175
12176   statement:
12177     atomic-statement
12178
12179  IN_COMPOUND is true when the statement is nested inside a
12180  cp_parser_compound_statement; this matters for certain pragmas.
12181
12182  If IF_P is not NULL, *IF_P is set to indicate whether the statement
12183  is a (possibly labeled) if statement which is not enclosed in braces
12184  and has an else clause.  This is used to implement -Wparentheses.
12185
12186  CHAIN is a vector of if-else-if conditions.  */
12187
12188static void
12189cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12190		     bool in_compound, bool *if_p, vec<tree> *chain,
12191		     location_t *loc_after_labels)
12192{
12193  tree statement, std_attrs = NULL_TREE;
12194  cp_token *token;
12195  location_t statement_location, attrs_loc;
12196  bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12197  bool has_std_attrs;
12198
12199 restart:
12200  if (if_p != NULL)
12201    *if_p = false;
12202  /* There is no statement yet.  */
12203  statement = NULL_TREE;
12204
12205  saved_token_sentinel saved_tokens (parser->lexer);
12206  token = cp_lexer_peek_token (parser->lexer);
12207  attrs_loc = token->location;
12208  if (c_dialect_objc ())
12209    /* In obj-c++, seeing '[[' might be the either the beginning of
12210       c++11 attributes, or a nested objc-message-expression.  So
12211       let's parse the c++11 attributes tentatively.  */
12212    cp_parser_parse_tentatively (parser);
12213  std_attrs = cp_parser_std_attribute_spec_seq (parser);
12214  if (std_attrs)
12215    attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12216  if (c_dialect_objc ())
12217    {
12218      if (!cp_parser_parse_definitely (parser))
12219	std_attrs = NULL_TREE;
12220    }
12221  has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12222
12223  /* Peek at the next token.  */
12224  token = cp_lexer_peek_token (parser->lexer);
12225  bool omp_attrs_forbidden_p;
12226  omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12227
12228  if (std_attrs && (flag_openmp || flag_openmp_simd))
12229    {
12230      bool handle_omp_attribs = false;
12231      if (token->type == CPP_KEYWORD)
12232	switch (token->keyword)
12233	  {
12234	  case RID_IF:
12235	  case RID_SWITCH:
12236	  case RID_WHILE:
12237	  case RID_DO:
12238	  case RID_FOR:
12239	  case RID_BREAK:
12240	  case RID_CONTINUE:
12241	  case RID_RETURN:
12242	  case RID_CO_RETURN:
12243	  case RID_GOTO:
12244	  case RID_AT_TRY:
12245	  case RID_AT_CATCH:
12246	  case RID_AT_FINALLY:
12247	  case RID_AT_SYNCHRONIZED:
12248	  case RID_AT_THROW:
12249	  case RID_TRY:
12250	  case RID_TRANSACTION_ATOMIC:
12251	  case RID_TRANSACTION_RELAXED:
12252	  case RID_SYNCHRONIZED:
12253	  case RID_ATOMIC_NOEXCEPT:
12254	  case RID_ATOMIC_CANCEL:
12255	  case RID_TRANSACTION_CANCEL:
12256	    handle_omp_attribs = true;
12257	    break;
12258	  default:
12259	    break;
12260	  }
12261      else if (token->type == CPP_SEMICOLON
12262	       || token->type == CPP_OPEN_BRACE
12263	       || token->type == CPP_PRAGMA)
12264	handle_omp_attribs = true;
12265      if (handle_omp_attribs)
12266	{
12267	  std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12268								 std_attrs);
12269	  token = cp_lexer_peek_token (parser->lexer);
12270	}
12271    }
12272  parser->omp_attrs_forbidden_p = false;
12273
12274  /* Remember the location of the first token in the statement.  */
12275  cp_token *statement_token = token;
12276  statement_location = token->location;
12277  add_debug_begin_stmt (statement_location);
12278  /* If this is a keyword, then that will often determine what kind of
12279     statement we have.  */
12280  if (token->type == CPP_KEYWORD)
12281    {
12282      enum rid keyword = token->keyword;
12283
12284      switch (keyword)
12285	{
12286	case RID_CASE:
12287	case RID_DEFAULT:
12288	  /* Looks like a labeled-statement with a case label.
12289	     Parse the label, and then use tail recursion to parse
12290	     the statement.  */
12291	  cp_parser_label_for_labeled_statement (parser, std_attrs);
12292	  in_compound = false;
12293	  in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12294	  goto restart;
12295
12296	case RID_IF:
12297	case RID_SWITCH:
12298	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12299	  statement = cp_parser_selection_statement (parser, if_p, chain);
12300	  break;
12301
12302	case RID_WHILE:
12303	case RID_DO:
12304	case RID_FOR:
12305	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12306	  statement = cp_parser_iteration_statement (parser, if_p, false, 0);
12307	  break;
12308
12309	case RID_BREAK:
12310	case RID_CONTINUE:
12311	case RID_RETURN:
12312	case RID_CO_RETURN:
12313	case RID_GOTO:
12314	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12315	  statement = cp_parser_jump_statement (parser);
12316	  break;
12317
12318	  /* Objective-C++ exception-handling constructs.  */
12319	case RID_AT_TRY:
12320	case RID_AT_CATCH:
12321	case RID_AT_FINALLY:
12322	case RID_AT_SYNCHRONIZED:
12323	case RID_AT_THROW:
12324	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12325	  statement = cp_parser_objc_statement (parser);
12326	  break;
12327
12328	case RID_TRY:
12329	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12330	  statement = cp_parser_try_block (parser);
12331	  break;
12332
12333	case RID_NAMESPACE:
12334	  /* This must be a namespace alias definition.  */
12335	  if (has_std_attrs)
12336	    {
12337	      /* Attributes should be parsed as part of the
12338		 declaration, so let's un-parse them.  */
12339	      saved_tokens.rollback();
12340	      std_attrs = NULL_TREE;
12341	    }
12342	  cp_parser_declaration_statement (parser);
12343	  return;
12344
12345	case RID_TRANSACTION_ATOMIC:
12346	case RID_TRANSACTION_RELAXED:
12347	case RID_SYNCHRONIZED:
12348	case RID_ATOMIC_NOEXCEPT:
12349	case RID_ATOMIC_CANCEL:
12350	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12351	  statement = cp_parser_transaction (parser, token);
12352	  break;
12353	case RID_TRANSACTION_CANCEL:
12354	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12355	  statement = cp_parser_transaction_cancel (parser);
12356	  break;
12357
12358	default:
12359	  /* It might be a keyword like `int' that can start a
12360	     declaration-statement.  */
12361	  break;
12362	}
12363    }
12364  else if (token->type == CPP_NAME)
12365    {
12366      /* If the next token is a `:', then we are looking at a
12367	 labeled-statement.  */
12368      token = cp_lexer_peek_nth_token (parser->lexer, 2);
12369      if (token->type == CPP_COLON)
12370	{
12371	  /* Looks like a labeled-statement with an ordinary label.
12372	     Parse the label, and then use tail recursion to parse
12373	     the statement.  */
12374
12375	  cp_parser_label_for_labeled_statement (parser, std_attrs);
12376	  in_compound = false;
12377	  in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12378	  goto restart;
12379	}
12380    }
12381  /* Anything that starts with a `{' must be a compound-statement.  */
12382  else if (token->type == CPP_OPEN_BRACE)
12383    {
12384      std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12385      statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12386    }
12387  /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12388     a statement all its own.  */
12389  else if (token->type == CPP_PRAGMA)
12390    {
12391     do_pragma:;
12392      cp_lexer *lexer = parser->lexer;
12393      bool do_restart = false;
12394      /* Only certain OpenMP pragmas are attached to statements, and thus
12395	 are considered statements themselves.  All others are not.  In
12396	 the context of a compound, accept the pragma as a "statement" and
12397	 return so that we can check for a close brace.  Otherwise we
12398	 require a real statement and must go back and read one.  */
12399      if (in_compound)
12400	cp_parser_pragma (parser, pragma_compound, if_p);
12401      else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12402	do_restart = true;
12403      if (parser->lexer != lexer
12404	  && lexer->in_omp_attribute_pragma
12405	  && (!in_omp_attribute_pragma || lexer->orphan_p))
12406	{
12407	  if (saved_tokens.lexer == lexer)
12408	    {
12409	      if (saved_tokens.mode == STS_COMMIT)
12410		cp_lexer_commit_tokens (lexer);
12411	      gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12412	      saved_tokens.lexer = parser->lexer;
12413	      saved_tokens.mode = STS_DONOTHING;
12414	      saved_tokens.len = parser->lexer->saved_tokens.length ();
12415	    }
12416	  cp_lexer_destroy (lexer);
12417	  lexer = parser->lexer;
12418	}
12419      if (do_restart)
12420	goto restart;
12421      if (parser->lexer == lexer
12422	  && lexer->in_omp_attribute_pragma
12423	  && !in_omp_attribute_pragma)
12424	parser->lexer->orphan_p = true;
12425      return;
12426    }
12427  else if (token->type == CPP_EOF)
12428    {
12429      cp_parser_error (parser, "expected statement");
12430      return;
12431    }
12432
12433  /* Everything else must be a declaration-statement or an
12434     expression-statement.  Try for the declaration-statement
12435     first, unless we are looking at a `;', in which case we know that
12436     we have an expression-statement.  */
12437  if (!statement)
12438    {
12439      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12440	{
12441	  if (has_std_attrs)
12442	    /* Attributes should be parsed as part of the declaration,
12443	       so let's un-parse them.  */
12444	    saved_tokens.rollback();
12445
12446	  parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12447	  cp_parser_parse_tentatively (parser);
12448	  /* Try to parse the declaration-statement.  */
12449	  cp_parser_declaration_statement (parser);
12450	  parser->omp_attrs_forbidden_p = false;
12451	  /* If that worked, we're done.  */
12452	  if (cp_parser_parse_definitely (parser))
12453	    return;
12454	  /* It didn't work, restore the post-attribute position.  */
12455	  if (has_std_attrs)
12456	    {
12457	      cp_lexer_set_token_position (parser->lexer, statement_token);
12458	      if (flag_openmp || flag_openmp_simd)
12459		{
12460		  size_t i = 1;
12461		  bool handle_omp_attribs = true;
12462		  while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12463			 == RID_EXTENSION)
12464		    i++;
12465		  switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12466		    {
12467		    case RID_ASM:
12468		    case RID_NAMESPACE:
12469		    case RID_USING:
12470		    case RID_LABEL:
12471		    case RID_STATIC_ASSERT:
12472		      /* Don't handle OpenMP attribs on keywords that
12473			 always start a declaration statement but don't
12474			 accept attribute before it and therefore
12475			 the tentative cp_parser_declaration_statement
12476			 fails to parse because of that.  */
12477		      handle_omp_attribs = false;
12478		      break;
12479		    default:
12480		      break;
12481		    }
12482
12483		  if (handle_omp_attribs)
12484		    {
12485		      parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12486		      std_attrs
12487			= cp_parser_handle_statement_omp_attributes
12488					(parser, std_attrs);
12489		      parser->omp_attrs_forbidden_p = false;
12490		      token = cp_lexer_peek_token (parser->lexer);
12491		      if (token->type == CPP_PRAGMA)
12492			goto do_pragma;
12493		    }
12494		}
12495	    }
12496	}
12497      /* All preceding labels have been parsed at this point.  */
12498      if (loc_after_labels != NULL)
12499	*loc_after_labels = statement_location;
12500
12501      std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12502
12503      /* Look for an expression-statement instead.  */
12504      statement = cp_parser_expression_statement (parser, in_statement_expr);
12505
12506      /* Handle [[fallthrough]];.  */
12507      if (attribute_fallthrough_p (std_attrs))
12508	{
12509	  /* The next token after the fallthrough attribute is ';'.  */
12510	  if (statement == NULL_TREE)
12511	    {
12512	      /* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
12513	      statement = build_call_expr_internal_loc (statement_location,
12514							IFN_FALLTHROUGH,
12515							void_type_node, 0);
12516	      finish_expr_stmt (statement);
12517	    }
12518	  else
12519	    warning_at (statement_location, OPT_Wattributes,
12520			"%<fallthrough%> attribute not followed by %<;%>");
12521	  std_attrs = NULL_TREE;
12522	}
12523    }
12524
12525  /* Set the line number for the statement.  */
12526  if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12527    SET_EXPR_LOCATION (statement, statement_location);
12528
12529  /* Allow "[[fallthrough]];", but warn otherwise.  */
12530  if (std_attrs != NULL_TREE)
12531    warning_at (attrs_loc,
12532		OPT_Wattributes,
12533		"attributes at the beginning of statement are ignored");
12534}
12535
12536/* Append ATTR to attribute list ATTRS.  */
12537
12538static tree
12539attr_chainon (tree attrs, tree attr)
12540{
12541  if (attrs == error_mark_node)
12542    return error_mark_node;
12543  if (attr == error_mark_node)
12544    return error_mark_node;
12545  return chainon (attrs, attr);
12546}
12547
12548/* Parse the label for a labeled-statement, i.e.
12549
12550   identifier :
12551   case constant-expression :
12552   default :
12553
12554   GNU Extension:
12555   case constant-expression ... constant-expression : statement
12556
12557   When a label is parsed without errors, the label is added to the
12558   parse tree by the finish_* functions, so this function doesn't
12559   have to return the label.  */
12560
12561static void
12562cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12563{
12564  cp_token *token;
12565  tree label = NULL_TREE;
12566  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12567
12568  /* The next token should be an identifier.  */
12569  token = cp_lexer_peek_token (parser->lexer);
12570  if (token->type != CPP_NAME
12571      && token->type != CPP_KEYWORD)
12572    {
12573      cp_parser_error (parser, "expected labeled-statement");
12574      return;
12575    }
12576
12577  /* Remember whether this case or a user-defined label is allowed to fall
12578     through to.  */
12579  bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12580
12581  parser->colon_corrects_to_scope_p = false;
12582  switch (token->keyword)
12583    {
12584    case RID_CASE:
12585      {
12586	tree expr, expr_hi;
12587	cp_token *ellipsis;
12588
12589	/* Consume the `case' token.  */
12590	cp_lexer_consume_token (parser->lexer);
12591	/* Parse the constant-expression.  */
12592	expr = cp_parser_constant_expression (parser);
12593	if (check_for_bare_parameter_packs (expr))
12594	  expr = error_mark_node;
12595
12596	ellipsis = cp_lexer_peek_token (parser->lexer);
12597	if (ellipsis->type == CPP_ELLIPSIS)
12598	  {
12599	    /* Consume the `...' token.  */
12600	    cp_lexer_consume_token (parser->lexer);
12601	    expr_hi = cp_parser_constant_expression (parser);
12602	    if (check_for_bare_parameter_packs (expr_hi))
12603	      expr_hi = error_mark_node;
12604
12605	    /* We don't need to emit warnings here, as the common code
12606	       will do this for us.  */
12607	  }
12608	else
12609	  expr_hi = NULL_TREE;
12610
12611	if (parser->in_switch_statement_p)
12612	  {
12613	    tree l = finish_case_label (token->location, expr, expr_hi);
12614	    if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12615	      {
12616		label = CASE_LABEL (l);
12617		FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12618	      }
12619	  }
12620	else
12621	  error_at (token->location,
12622		    "case label %qE not within a switch statement",
12623		    expr);
12624      }
12625      break;
12626
12627    case RID_DEFAULT:
12628      /* Consume the `default' token.  */
12629      cp_lexer_consume_token (parser->lexer);
12630
12631      if (parser->in_switch_statement_p)
12632	{
12633	  tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12634	  if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12635	      {
12636		label = CASE_LABEL (l);
12637		FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12638	      }
12639	}
12640      else
12641	error_at (token->location, "case label not within a switch statement");
12642      break;
12643
12644    default:
12645      /* Anything else must be an ordinary label.  */
12646      label = finish_label_stmt (cp_parser_identifier (parser));
12647      if (label && TREE_CODE (label) == LABEL_DECL)
12648	FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12649      break;
12650    }
12651
12652  /* Require the `:' token.  */
12653  cp_parser_require (parser, CPP_COLON, RT_COLON);
12654
12655  /* An ordinary label may optionally be followed by attributes.
12656     However, this is only permitted if the attributes are then
12657     followed by a semicolon.  This is because, for backward
12658     compatibility, when parsing
12659       lab: __attribute__ ((unused)) int i;
12660     we want the attribute to attach to "i", not "lab".  */
12661  if (label != NULL_TREE
12662      && cp_next_tokens_can_be_gnu_attribute_p (parser))
12663    {
12664      tree attrs;
12665      cp_parser_parse_tentatively (parser);
12666      attrs = cp_parser_gnu_attributes_opt (parser);
12667      if (attrs == NULL_TREE
12668	  /* And fallthrough always binds to the expression-statement.  */
12669	  || attribute_fallthrough_p (attrs)
12670	  || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12671	cp_parser_abort_tentative_parse (parser);
12672      else if (!cp_parser_parse_definitely (parser))
12673	;
12674      else
12675	attributes = attr_chainon (attributes, attrs);
12676    }
12677
12678  if (attributes != NULL_TREE)
12679    cplus_decl_attributes (&label, attributes, 0);
12680
12681  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12682}
12683
12684/* Parse an expression-statement.
12685
12686   expression-statement:
12687     expression [opt] ;
12688
12689   Returns the new EXPR_STMT -- or NULL_TREE if the expression
12690   statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12691   indicates whether this expression-statement is part of an
12692   expression statement.  */
12693
12694static tree
12695cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12696{
12697  tree statement = NULL_TREE;
12698  cp_token *token = cp_lexer_peek_token (parser->lexer);
12699  location_t loc = token->location;
12700
12701  /* There might be attribute fallthrough.  */
12702  tree attr = cp_parser_gnu_attributes_opt (parser);
12703
12704  /* If the next token is a ';', then there is no expression
12705     statement.  */
12706  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12707    {
12708      statement = cp_parser_expression (parser);
12709      if (statement == error_mark_node
12710	  && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12711	{
12712	  cp_parser_skip_to_end_of_block_or_statement (parser);
12713	  return error_mark_node;
12714	}
12715    }
12716
12717  /* Handle [[fallthrough]];.  */
12718  if (attribute_fallthrough_p (attr))
12719    {
12720      /* The next token after the fallthrough attribute is ';'.  */
12721      if (statement == NULL_TREE)
12722	/* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
12723	statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12724						  void_type_node, 0);
12725      else
12726	warning_at (loc, OPT_Wattributes,
12727		    "%<fallthrough%> attribute not followed by %<;%>");
12728      attr = NULL_TREE;
12729    }
12730
12731  /* Allow "[[fallthrough]];", but warn otherwise.  */
12732  if (attr != NULL_TREE)
12733    warning_at (loc, OPT_Wattributes,
12734		"attributes at the beginning of statement are ignored");
12735
12736  /* Give a helpful message for "A<T>::type t;" and the like.  */
12737  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12738      && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12739    {
12740      if (TREE_CODE (statement) == SCOPE_REF)
12741	error_at (token->location, "need %<typename%> before %qE because "
12742		  "%qT is a dependent scope",
12743		  statement, TREE_OPERAND (statement, 0));
12744      else if (is_overloaded_fn (statement)
12745	       && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12746	{
12747	  /* A::A a; */
12748	  tree fn = get_first_fn (statement);
12749	  error_at (token->location,
12750		    "%<%T::%D%> names the constructor, not the type",
12751		    DECL_CONTEXT (fn), DECL_NAME (fn));
12752	}
12753    }
12754
12755  /* Consume the final `;'.  */
12756  cp_parser_consume_semicolon_at_end_of_statement (parser);
12757
12758  if (in_statement_expr
12759      && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12760    /* This is the final expression statement of a statement
12761       expression.  */
12762    statement = finish_stmt_expr_expr (statement, in_statement_expr);
12763  else if (statement)
12764    statement = finish_expr_stmt (statement);
12765
12766  return statement;
12767}
12768
12769/* Parse a compound-statement.
12770
12771   compound-statement:
12772     { statement-seq [opt] }
12773
12774   GNU extension:
12775
12776   compound-statement:
12777     { label-declaration-seq [opt] statement-seq [opt] }
12778
12779   label-declaration-seq:
12780     label-declaration
12781     label-declaration-seq label-declaration
12782
12783   Returns a tree representing the statement.  */
12784
12785static tree
12786cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
12787			      int bcs_flags, bool function_body)
12788{
12789  tree compound_stmt;
12790  matching_braces braces;
12791
12792  /* Consume the `{'.  */
12793  if (!braces.require_open (parser))
12794    return error_mark_node;
12795  if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
12796      && !function_body && cxx_dialect < cxx14)
12797    pedwarn (input_location, OPT_Wpedantic,
12798	     "compound-statement in %<constexpr%> function");
12799  /* Begin the compound-statement.  */
12800  compound_stmt = begin_compound_stmt (bcs_flags);
12801  /* If the next keyword is `__label__' we have a label declaration.  */
12802  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12803    cp_parser_label_declaration (parser);
12804  /* Parse an (optional) statement-seq.  */
12805  cp_parser_statement_seq_opt (parser, in_statement_expr);
12806
12807  /* Consume the `}'.  */
12808  braces.require_close (parser);
12809
12810  /* Finish the compound-statement.  */
12811  finish_compound_stmt (compound_stmt);
12812
12813  return compound_stmt;
12814}
12815
12816/* Parse an (optional) statement-seq.
12817
12818   statement-seq:
12819     statement
12820     statement-seq [opt] statement  */
12821
12822static void
12823cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
12824{
12825  /* Scan statements until there aren't any more.  */
12826  while (true)
12827    {
12828      cp_token *token = cp_lexer_peek_token (parser->lexer);
12829
12830      /* If we are looking at a `}', then we have run out of
12831	 statements; the same is true if we have reached the end
12832	 of file, or have stumbled upon a stray '@end'.  */
12833      if (token->type == CPP_CLOSE_BRACE
12834	  || token->type == CPP_EOF
12835	  || token->type == CPP_PRAGMA_EOL
12836	  || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
12837	break;
12838
12839      /* If we are in a compound statement and find 'else' then
12840	 something went wrong.  */
12841      else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
12842	{
12843	  if (parser->in_statement & IN_IF_STMT)
12844	    break;
12845	  else
12846	    {
12847	      token = cp_lexer_consume_token (parser->lexer);
12848	      error_at (token->location, "%<else%> without a previous %<if%>");
12849	    }
12850	}
12851
12852      /* Parse the statement.  */
12853      cp_parser_statement (parser, in_statement_expr, true, NULL);
12854    }
12855}
12856
12857/* Return true if this is the C++20 version of range-based-for with
12858   init-statement.  */
12859
12860static bool
12861cp_parser_range_based_for_with_init_p (cp_parser *parser)
12862{
12863  bool r = false;
12864
12865  /* Save tokens so that we can put them back.  */
12866  cp_lexer_save_tokens (parser->lexer);
12867
12868  /* There has to be an unnested ; followed by an unnested :.  */
12869  if (cp_parser_skip_to_closing_parenthesis_1 (parser,
12870					       /*recovering=*/false,
12871					       CPP_SEMICOLON,
12872					       /*consume_paren=*/false) != -1)
12873    goto out;
12874
12875  /* We found the semicolon, eat it now.  */
12876  cp_lexer_consume_token (parser->lexer);
12877
12878  /* Now look for ':' that is not nested in () or {}.  */
12879  r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
12880						/*recovering=*/false,
12881						CPP_COLON,
12882						/*consume_paren=*/false) == -1);
12883
12884out:
12885  /* Roll back the tokens we skipped.  */
12886  cp_lexer_rollback_tokens (parser->lexer);
12887
12888  return r;
12889}
12890
12891/* Return true if we're looking at (init; cond), false otherwise.  */
12892
12893static bool
12894cp_parser_init_statement_p (cp_parser *parser)
12895{
12896  /* Save tokens so that we can put them back.  */
12897  cp_lexer_save_tokens (parser->lexer);
12898
12899  /* Look for ';' that is not nested in () or {}.  */
12900  int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
12901						     /*recovering=*/false,
12902						     CPP_SEMICOLON,
12903						     /*consume_paren=*/false);
12904
12905  /* Roll back the tokens we skipped.  */
12906  cp_lexer_rollback_tokens (parser->lexer);
12907
12908  return ret == -1;
12909}
12910
12911/* Parse a selection-statement.
12912
12913   selection-statement:
12914     if ( init-statement [opt] condition ) statement
12915     if ( init-statement [opt] condition ) statement else statement
12916     switch ( init-statement [opt] condition ) statement
12917
12918   Returns the new IF_STMT or SWITCH_STMT.
12919
12920   If IF_P is not NULL, *IF_P is set to indicate whether the statement
12921   is a (possibly labeled) if statement which is not enclosed in
12922   braces and has an else clause.  This is used to implement
12923   -Wparentheses.
12924
12925   CHAIN is a vector of if-else-if conditions.  This is used to implement
12926   -Wduplicated-cond.  */
12927
12928static tree
12929cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12930			       vec<tree> *chain)
12931{
12932  cp_token *token;
12933  enum rid keyword;
12934  token_indent_info guard_tinfo;
12935
12936  if (if_p != NULL)
12937    *if_p = false;
12938
12939  /* Peek at the next token.  */
12940  token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12941  guard_tinfo = get_token_indent_info (token);
12942
12943  /* See what kind of keyword it is.  */
12944  keyword = token->keyword;
12945  switch (keyword)
12946    {
12947    case RID_IF:
12948    case RID_SWITCH:
12949      {
12950	tree statement;
12951	tree condition;
12952
12953	bool cx = false;
12954	if (keyword == RID_IF
12955	    && cp_lexer_next_token_is_keyword (parser->lexer,
12956					       RID_CONSTEXPR))
12957	  {
12958	    cx = true;
12959	    cp_token *tok = cp_lexer_consume_token (parser->lexer);
12960	    if (cxx_dialect < cxx17)
12961	      pedwarn (tok->location, OPT_Wc__17_extensions,
12962		       "%<if constexpr%> only available with "
12963		       "%<-std=c++17%> or %<-std=gnu++17%>");
12964	  }
12965	int ce = 0;
12966	if (keyword == RID_IF && !cx)
12967	  {
12968	    if (cp_lexer_next_token_is_keyword (parser->lexer,
12969						RID_CONSTEVAL))
12970	      ce = 1;
12971	    else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
12972		     && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
12973						       RID_CONSTEVAL))
12974	      {
12975		ce = -1;
12976		cp_lexer_consume_token (parser->lexer);
12977	      }
12978	  }
12979	if (ce)
12980	  {
12981	    cp_token *tok = cp_lexer_consume_token (parser->lexer);
12982	    if (cxx_dialect < cxx23)
12983	      pedwarn (tok->location, OPT_Wc__23_extensions,
12984		       "%<if consteval%> only available with "
12985		       "%<-std=c++2b%> or %<-std=gnu++2b%>");
12986
12987	    bool save_in_consteval_if_p = in_consteval_if_p;
12988	    statement = begin_if_stmt ();
12989	    IF_STMT_CONSTEVAL_P (statement) = true;
12990	    condition = finish_if_stmt_cond (boolean_false_node, statement);
12991
12992	    gcc_rich_location richloc (tok->location);
12993	    bool non_compound_stmt_p = false;
12994	    if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12995	      {
12996		non_compound_stmt_p = true;
12997		richloc.add_fixit_insert_after (tok->location, "{");
12998	      }
12999
13000	    in_consteval_if_p |= ce > 0;
13001	    cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13002
13003	    if (non_compound_stmt_p)
13004	      {
13005		location_t before_loc
13006		  = cp_lexer_peek_token (parser->lexer)->location;
13007		richloc.add_fixit_insert_before (before_loc, "}");
13008		error_at (&richloc,
13009			  "%<if consteval%> requires compound statement");
13010		non_compound_stmt_p = false;
13011	      }
13012
13013	    finish_then_clause (statement);
13014
13015	    /* If the next token is `else', parse the else-clause.  */
13016	    if (cp_lexer_next_token_is_keyword (parser->lexer,
13017						RID_ELSE))
13018	      {
13019		cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13020		gcc_rich_location else_richloc (else_tok->location);
13021		guard_tinfo = get_token_indent_info (else_tok);
13022		/* Consume the `else' keyword.  */
13023		cp_lexer_consume_token (parser->lexer);
13024
13025		begin_else_clause (statement);
13026
13027		if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13028		  {
13029		    non_compound_stmt_p = true;
13030		    else_richloc.add_fixit_insert_after (else_tok->location,
13031							 "{");
13032		  }
13033
13034		in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13035		cp_parser_implicitly_scoped_statement (parser, NULL,
13036						       guard_tinfo);
13037
13038		if (non_compound_stmt_p)
13039		  {
13040		    location_t before_loc
13041		      = cp_lexer_peek_token (parser->lexer)->location;
13042		    else_richloc.add_fixit_insert_before (before_loc, "}");
13043		    error_at (&else_richloc,
13044			      "%<if consteval%> requires compound statement");
13045		  }
13046
13047		finish_else_clause (statement);
13048	      }
13049
13050	    in_consteval_if_p = save_in_consteval_if_p;
13051	    if (ce < 0)
13052	      {
13053		std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13054		if (THEN_CLAUSE (statement) == NULL_TREE)
13055		  THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13056	      }
13057
13058	    finish_if_stmt (statement);
13059	    return statement;
13060	  }
13061
13062	/* Look for the `('.  */
13063	matching_parens parens;
13064	if (!parens.require_open (parser))
13065	  {
13066	    cp_parser_skip_to_end_of_statement (parser);
13067	    return error_mark_node;
13068	  }
13069
13070	/* Begin the selection-statement.  */
13071	if (keyword == RID_IF)
13072	  {
13073	    statement = begin_if_stmt ();
13074	    IF_STMT_CONSTEXPR_P (statement) = cx;
13075	  }
13076	else
13077	  statement = begin_switch_stmt ();
13078
13079	/* Parse the optional init-statement.  */
13080	if (cp_parser_init_statement_p (parser))
13081	  {
13082	    tree decl;
13083	    if (cxx_dialect < cxx17)
13084	      pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13085		       OPT_Wc__17_extensions,
13086		       "init-statement in selection statements only available "
13087		       "with %<-std=c++17%> or %<-std=gnu++17%>");
13088	    if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13089	      /* A non-empty init-statement can have arbitrary side
13090		 effects.  */
13091	      vec_free (chain);
13092	    cp_parser_init_statement (parser, &decl);
13093	  }
13094
13095	/* Parse the condition.  */
13096	condition = cp_parser_condition (parser);
13097	/* Look for the `)'.  */
13098	if (!parens.require_close (parser))
13099	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
13100						 /*consume_paren=*/true);
13101
13102	if (keyword == RID_IF)
13103	  {
13104	    bool nested_if;
13105	    unsigned char in_statement;
13106
13107	    /* Add the condition.  */
13108	    condition = finish_if_stmt_cond (condition, statement);
13109
13110	    if (warn_duplicated_cond)
13111	      warn_duplicated_cond_add_or_warn (token->location, condition,
13112						&chain);
13113
13114	    /* Parse the then-clause.  */
13115	    in_statement = parser->in_statement;
13116	    parser->in_statement |= IN_IF_STMT;
13117
13118	    /* Outside a template, the non-selected branch of a constexpr
13119	       if is a 'discarded statement', i.e. unevaluated.  */
13120	    bool was_discarded = in_discarded_stmt;
13121	    bool discard_then = (cx && !processing_template_decl
13122				 && integer_zerop (condition));
13123	    if (discard_then)
13124	      {
13125		in_discarded_stmt = true;
13126		++c_inhibit_evaluation_warnings;
13127	      }
13128
13129	    cp_parser_implicitly_scoped_statement (parser, &nested_if,
13130						   guard_tinfo);
13131
13132	    parser->in_statement = in_statement;
13133
13134	    finish_then_clause (statement);
13135
13136	    if (discard_then)
13137	      {
13138		THEN_CLAUSE (statement) = NULL_TREE;
13139		in_discarded_stmt = was_discarded;
13140		--c_inhibit_evaluation_warnings;
13141	      }
13142
13143	    /* If the next token is `else', parse the else-clause.  */
13144	    if (cp_lexer_next_token_is_keyword (parser->lexer,
13145						RID_ELSE))
13146	      {
13147		bool discard_else = (cx && !processing_template_decl
13148				     && integer_nonzerop (condition));
13149		if (discard_else)
13150		  {
13151		    in_discarded_stmt = true;
13152		    ++c_inhibit_evaluation_warnings;
13153		  }
13154
13155		guard_tinfo
13156		  = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13157		/* Consume the `else' keyword.  */
13158		cp_lexer_consume_token (parser->lexer);
13159		if (warn_duplicated_cond)
13160		  {
13161		    if (cp_lexer_next_token_is_keyword (parser->lexer,
13162							RID_IF)
13163			&& chain == NULL)
13164		      {
13165			/* We've got "if (COND) else if (COND2)".  Start
13166			   the condition chain and add COND as the first
13167			   element.  */
13168			chain = new vec<tree> ();
13169			if (!CONSTANT_CLASS_P (condition)
13170			    && !TREE_SIDE_EFFECTS (condition))
13171			{
13172			  /* Wrap it in a NOP_EXPR so that we can set the
13173			     location of the condition.  */
13174			  tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13175					   condition);
13176			  SET_EXPR_LOCATION (e, token->location);
13177			  chain->safe_push (e);
13178			}
13179		      }
13180		    else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13181							      RID_IF))
13182		      /* This is if-else without subsequent if.  Zap the
13183			 condition chain; we would have already warned at
13184			 this point.  */
13185		      vec_free (chain);
13186		  }
13187		begin_else_clause (statement);
13188		/* Parse the else-clause.  */
13189		cp_parser_implicitly_scoped_statement (parser, NULL,
13190						       guard_tinfo, chain);
13191
13192		finish_else_clause (statement);
13193
13194		/* If we are currently parsing a then-clause, then
13195		   IF_P will not be NULL.  We set it to true to
13196		   indicate that this if statement has an else clause.
13197		   This may trigger the Wparentheses warning below
13198		   when we get back up to the parent if statement.  */
13199		if (if_p != NULL)
13200		  *if_p = true;
13201
13202		if (discard_else)
13203		  {
13204		    ELSE_CLAUSE (statement) = NULL_TREE;
13205		    in_discarded_stmt = was_discarded;
13206		    --c_inhibit_evaluation_warnings;
13207		  }
13208	      }
13209	    else
13210	      {
13211		/* This if statement does not have an else clause.  If
13212		   NESTED_IF is true, then the then-clause has an if
13213		   statement which does have an else clause.  We warn
13214		   about the potential ambiguity.  */
13215		if (nested_if)
13216		  warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13217			      "suggest explicit braces to avoid ambiguous"
13218			      " %<else%>");
13219		if (warn_duplicated_cond)
13220		  /* We don't need the condition chain anymore.  */
13221		  vec_free (chain);
13222	      }
13223
13224	    /* Now we're all done with the if-statement.  */
13225	    finish_if_stmt (statement);
13226	  }
13227	else
13228	  {
13229	    bool in_switch_statement_p;
13230	    unsigned char in_statement;
13231
13232	    /* Add the condition.  */
13233	    finish_switch_cond (condition, statement);
13234
13235	    /* Parse the body of the switch-statement.  */
13236	    in_switch_statement_p = parser->in_switch_statement_p;
13237	    in_statement = parser->in_statement;
13238	    parser->in_switch_statement_p = true;
13239	    parser->in_statement |= IN_SWITCH_STMT;
13240	    cp_parser_implicitly_scoped_statement (parser, if_p,
13241						   guard_tinfo);
13242	    parser->in_switch_statement_p = in_switch_statement_p;
13243	    parser->in_statement = in_statement;
13244
13245	    /* Now we're all done with the switch-statement.  */
13246	    finish_switch_stmt (statement);
13247	  }
13248
13249	return statement;
13250      }
13251      break;
13252
13253    default:
13254      cp_parser_error (parser, "expected selection-statement");
13255      return error_mark_node;
13256    }
13257}
13258
13259/* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13260   If we have seen at least one decl-specifier, and the next token is not
13261   a parenthesis (after "int (" we might be looking at a functional cast)
13262   neither we are dealing with a concept-check expression then we must be
13263   looking at a declaration.  */
13264
13265static void
13266cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13267				       cp_decl_specifier_seq *decl_specs)
13268{
13269  if (decl_specs->any_specifiers_p
13270      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13271      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13272      && !cp_parser_error_occurred (parser)
13273      && !(decl_specs->type
13274	   && TREE_CODE (decl_specs->type) == TYPE_DECL
13275	   && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13276    cp_parser_commit_to_tentative_parse (parser);
13277}
13278
13279/* Helper function for cp_parser_condition.  Enforces [stmt.stmt]/2:
13280   The declarator shall not specify a function or an array.  Returns
13281   TRUE if the declarator is valid, FALSE otherwise.  */
13282
13283static bool
13284cp_parser_check_condition_declarator (cp_parser* parser,
13285                                     cp_declarator *declarator,
13286                                     location_t loc)
13287{
13288  if (declarator == cp_error_declarator
13289      || function_declarator_p (declarator)
13290      || declarator->kind == cdk_array)
13291    {
13292      if (declarator == cp_error_declarator)
13293	/* Already complained.  */;
13294      else if (declarator->kind == cdk_array)
13295       error_at (loc, "condition declares an array");
13296      else
13297       error_at (loc, "condition declares a function");
13298      if (parser->fully_implicit_function_template_p)
13299       abort_fully_implicit_template (parser);
13300      cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13301                                            /*or_comma=*/false,
13302                                            /*consume_paren=*/false);
13303      return false;
13304    }
13305  else
13306    return true;
13307}
13308
13309/* Parse a condition.
13310
13311   condition:
13312     expression
13313     type-specifier-seq declarator = initializer-clause
13314     type-specifier-seq declarator braced-init-list
13315
13316   GNU Extension:
13317
13318   condition:
13319     type-specifier-seq declarator asm-specification [opt]
13320       attributes [opt] = assignment-expression
13321
13322   Returns the expression that should be tested.  */
13323
13324static tree
13325cp_parser_condition (cp_parser* parser)
13326{
13327  cp_decl_specifier_seq type_specifiers;
13328  const char *saved_message;
13329  int declares_class_or_enum;
13330
13331  /* Try the declaration first.  */
13332  cp_parser_parse_tentatively (parser);
13333  /* New types are not allowed in the type-specifier-seq for a
13334     condition.  */
13335  saved_message = parser->type_definition_forbidden_message;
13336  parser->type_definition_forbidden_message
13337    = G_("types may not be defined in conditions");
13338  /* Parse the type-specifier-seq.  */
13339  cp_parser_decl_specifier_seq (parser,
13340				CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13341				&type_specifiers,
13342				&declares_class_or_enum);
13343  /* Restore the saved message.  */
13344  parser->type_definition_forbidden_message = saved_message;
13345
13346  /* Gather the attributes that were provided with the
13347     decl-specifiers.  */
13348  tree prefix_attributes = type_specifiers.attributes;
13349
13350  cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13351
13352  /* If all is well, we might be looking at a declaration.  */
13353  if (!cp_parser_error_occurred (parser))
13354    {
13355      tree decl;
13356      tree asm_specification;
13357      tree attributes;
13358      cp_declarator *declarator;
13359      tree initializer = NULL_TREE;
13360      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13361
13362      /* Parse the declarator.  */
13363      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13364					 CP_PARSER_FLAGS_NONE,
13365					 /*ctor_dtor_or_conv_p=*/NULL,
13366					 /*parenthesized_p=*/NULL,
13367					 /*member_p=*/false,
13368					 /*friend_p=*/false,
13369					 /*static_p=*/false);
13370      /* Parse the attributes.  */
13371      attributes = cp_parser_attributes_opt (parser);
13372      /* Parse the asm-specification.  */
13373      asm_specification = cp_parser_asm_specification_opt (parser);
13374      /* If the next token is not an `=' or '{', then we might still be
13375	 looking at an expression.  For example:
13376
13377	   if (A(a).x)
13378
13379	 looks like a decl-specifier-seq and a declarator -- but then
13380	 there is no `=', so this is an expression.  */
13381      if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13382	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13383	cp_parser_simulate_error (parser);
13384
13385      /* If we did see an `=' or '{', then we are looking at a declaration
13386	 for sure.  */
13387      if (cp_parser_parse_definitely (parser))
13388	{
13389	  tree pushed_scope;
13390	  bool non_constant_p = false;
13391	  int flags = LOOKUP_ONLYCONVERTING;
13392
13393	  if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13394	    return error_mark_node;
13395
13396	  /* Create the declaration.  */
13397	  decl = start_decl (declarator, &type_specifiers,
13398			     /*initialized_p=*/true,
13399			     attributes, prefix_attributes,
13400			     &pushed_scope);
13401
13402	  declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13403	  /* Parse the initializer.  */
13404	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13405	    {
13406	      initializer = cp_parser_braced_list (parser, &non_constant_p);
13407	      CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13408	      flags = 0;
13409	    }
13410	  else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13411	    {
13412	      /* Consume the `='.  */
13413	      cp_lexer_consume_token (parser->lexer);
13414	      initializer = cp_parser_initializer_clause (parser,
13415							  &non_constant_p);
13416	    }
13417	  else
13418	    {
13419	      cp_parser_error (parser, "expected initializer");
13420	      initializer = error_mark_node;
13421	    }
13422	  if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13423	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13424
13425	  /* Process the initializer.  */
13426	  cp_finish_decl (decl,
13427			  initializer, !non_constant_p,
13428			  asm_specification,
13429			  flags);
13430
13431	  if (pushed_scope)
13432	    pop_scope (pushed_scope);
13433
13434	  return convert_from_reference (decl);
13435	}
13436    }
13437  /* If we didn't even get past the declarator successfully, we are
13438     definitely not looking at a declaration.  */
13439  else
13440    cp_parser_abort_tentative_parse (parser);
13441
13442  /* Otherwise, we are looking at an expression.  */
13443  return cp_parser_expression (parser);
13444}
13445
13446/* Parses a for-statement or range-for-statement until the closing ')',
13447   not included. */
13448
13449static tree
13450cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
13451{
13452  tree init, scope, decl;
13453  bool is_range_for;
13454
13455  /* Begin the for-statement.  */
13456  scope = begin_for_scope (&init);
13457
13458  /* Maybe parse the optional init-statement in a range-based for loop.  */
13459  if (cp_parser_range_based_for_with_init_p (parser)
13460      /* Checked for diagnostic purposes only.  */
13461      && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13462    {
13463      tree dummy;
13464      cp_parser_init_statement (parser, &dummy);
13465      if (cxx_dialect < cxx20)
13466	{
13467	  pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13468		   OPT_Wc__20_extensions,
13469		   "range-based %<for%> loops with initializer only "
13470		   "available with %<-std=c++20%> or %<-std=gnu++20%>");
13471	  decl = error_mark_node;
13472	}
13473    }
13474
13475  /* Parse the initialization.  */
13476  is_range_for = cp_parser_init_statement (parser, &decl);
13477
13478  if (is_range_for)
13479    return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13480				false);
13481  else
13482    return cp_parser_c_for (parser, scope, init, ivdep, unroll);
13483}
13484
13485static tree
13486cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13487		 unsigned short unroll)
13488{
13489  /* Normal for loop */
13490  tree condition = NULL_TREE;
13491  tree expression = NULL_TREE;
13492  tree stmt;
13493
13494  stmt = begin_for_stmt (scope, init);
13495  /* The init-statement has already been parsed in
13496     cp_parser_init_statement, so no work is needed here.  */
13497  finish_init_stmt (stmt);
13498
13499  /* If there's a condition, process it.  */
13500  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13501    condition = cp_parser_condition (parser);
13502  else if (ivdep)
13503    {
13504      cp_parser_error (parser, "missing loop condition in loop with "
13505		       "%<GCC ivdep%> pragma");
13506      condition = error_mark_node;
13507    }
13508  else if (unroll)
13509    {
13510      cp_parser_error (parser, "missing loop condition in loop with "
13511		       "%<GCC unroll%> pragma");
13512      condition = error_mark_node;
13513    }
13514  finish_for_cond (condition, stmt, ivdep, unroll);
13515  /* Look for the `;'.  */
13516  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13517
13518  /* If there's an expression, process it.  */
13519  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13520    expression = cp_parser_expression (parser);
13521  finish_for_expr (expression, stmt);
13522
13523  return stmt;
13524}
13525
13526/* Tries to parse a range-based for-statement:
13527
13528  range-based-for:
13529    decl-specifier-seq declarator : expression
13530
13531  The decl-specifier-seq declarator and the `:' are already parsed by
13532  cp_parser_init_statement.  If processing_template_decl it returns a
13533  newly created RANGE_FOR_STMT; if not, it is converted to a
13534  regular FOR_STMT.  */
13535
13536static tree
13537cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13538		     bool ivdep, unsigned short unroll, bool is_omp)
13539{
13540  tree stmt, range_expr;
13541  auto_vec <cxx_binding *, 16> bindings;
13542  auto_vec <tree, 16> names;
13543  tree decomp_first_name = NULL_TREE;
13544  unsigned int decomp_cnt = 0;
13545
13546  /* Get the range declaration momentarily out of the way so that
13547     the range expression doesn't clash with it. */
13548  if (range_decl != error_mark_node)
13549    {
13550      if (DECL_HAS_VALUE_EXPR_P (range_decl))
13551	{
13552	  tree v = DECL_VALUE_EXPR (range_decl);
13553	  /* For decomposition declaration get all of the corresponding
13554	     declarations out of the way.  */
13555	  if (TREE_CODE (v) == ARRAY_REF
13556	      && VAR_P (TREE_OPERAND (v, 0))
13557	      && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13558	    {
13559	      tree d = range_decl;
13560	      range_decl = TREE_OPERAND (v, 0);
13561	      decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13562	      decomp_first_name = d;
13563	      for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
13564		{
13565		  tree name = DECL_NAME (d);
13566		  names.safe_push (name);
13567		  bindings.safe_push (IDENTIFIER_BINDING (name));
13568		  IDENTIFIER_BINDING (name)
13569		    = IDENTIFIER_BINDING (name)->previous;
13570		}
13571	    }
13572	}
13573      if (names.is_empty ())
13574	{
13575	  tree name = DECL_NAME (range_decl);
13576	  names.safe_push (name);
13577	  bindings.safe_push (IDENTIFIER_BINDING (name));
13578	  IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13579	}
13580    }
13581
13582  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13583    {
13584      bool expr_non_constant_p;
13585      range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13586    }
13587  else
13588    range_expr = cp_parser_expression (parser);
13589
13590  /* Put the range declaration(s) back into scope. */
13591  for (unsigned int i = 0; i < names.length (); i++)
13592    {
13593      cxx_binding *binding = bindings[i];
13594      binding->previous = IDENTIFIER_BINDING (names[i]);
13595      IDENTIFIER_BINDING (names[i]) = binding;
13596    }
13597
13598  /* finish_omp_for has its own code for the following, so just
13599     return the range_expr instead.  */
13600  if (is_omp)
13601    return range_expr;
13602
13603  /* If in template, STMT is converted to a normal for-statement
13604     at instantiation. If not, it is done just ahead. */
13605  if (processing_template_decl)
13606    {
13607      if (check_for_bare_parameter_packs (range_expr))
13608	range_expr = error_mark_node;
13609      stmt = begin_range_for_stmt (scope, init);
13610      if (ivdep)
13611	RANGE_FOR_IVDEP (stmt) = 1;
13612      if (unroll)
13613	RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13614      finish_range_for_decl (stmt, range_decl, range_expr);
13615      if (!type_dependent_expression_p (range_expr)
13616	  /* do_auto_deduction doesn't mess with template init-lists.  */
13617	  && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13618	do_range_for_auto_deduction (range_decl, range_expr);
13619    }
13620  else
13621    {
13622      stmt = begin_for_stmt (scope, init);
13623      stmt = cp_convert_range_for (stmt, range_decl, range_expr,
13624				   decomp_first_name, decomp_cnt, ivdep,
13625				   unroll);
13626    }
13627  return stmt;
13628}
13629
13630/* Subroutine of cp_convert_range_for: given the initializer expression,
13631   builds up the range temporary.  */
13632
13633static tree
13634build_range_temp (tree range_expr)
13635{
13636  /* Find out the type deduced by the declaration
13637     `auto &&__range = range_expr'.  */
13638  tree auto_node = make_auto ();
13639  tree range_type = cp_build_reference_type (auto_node, true);
13640  range_type = do_auto_deduction (range_type, range_expr, auto_node);
13641
13642  /* Create the __range variable.  */
13643  tree range_temp = build_decl (input_location, VAR_DECL,
13644				for_range__identifier, range_type);
13645  TREE_USED (range_temp) = 1;
13646  DECL_ARTIFICIAL (range_temp) = 1;
13647
13648  return range_temp;
13649}
13650
13651/* Used by cp_parser_range_for in template context: we aren't going to
13652   do a full conversion yet, but we still need to resolve auto in the
13653   type of the for-range-declaration if present.  This is basically
13654   a shortcut version of cp_convert_range_for.  */
13655
13656static void
13657do_range_for_auto_deduction (tree decl, tree range_expr)
13658{
13659  tree auto_node = type_uses_auto (TREE_TYPE (decl));
13660  if (auto_node)
13661    {
13662      tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
13663      range_temp = convert_from_reference (build_range_temp (range_expr));
13664      iter_type = (cp_parser_perform_range_for_lookup
13665		   (range_temp, &begin_dummy, &end_dummy));
13666      if (iter_type)
13667	{
13668	  iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
13669				  iter_type);
13670	  iter_decl = build_x_indirect_ref (input_location, iter_decl,
13671					    RO_UNARY_STAR, NULL_TREE,
13672					    tf_warning_or_error);
13673	  TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
13674						iter_decl, auto_node,
13675						tf_warning_or_error,
13676						adc_variable_type);
13677	}
13678    }
13679}
13680
13681/* Warns when the loop variable should be changed to a reference type to
13682   avoid unnecessary copying.  I.e., from
13683
13684     for (const auto x : range)
13685
13686   where range returns a reference, to
13687
13688     for (const auto &x : range)
13689
13690   if this version doesn't make a copy.
13691
13692  This function also warns when the loop variable is initialized with
13693  a value of a different type resulting in a copy:
13694
13695     int arr[10];
13696     for (const double &x : arr)
13697
13698   DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13699   This function is never called when processing_template_decl is on.  */
13700
13701static void
13702warn_for_range_copy (tree decl, tree expr)
13703{
13704  if (!warn_range_loop_construct
13705      || decl == error_mark_node)
13706    return;
13707
13708  location_t loc = DECL_SOURCE_LOCATION (decl);
13709  tree type = TREE_TYPE (decl);
13710
13711  if (from_macro_expansion_at (loc))
13712    return;
13713
13714  if (TYPE_REF_P (type))
13715    {
13716      if (glvalue_p (expr) && !ref_conv_binds_directly_p (type, expr))
13717	{
13718	  auto_diagnostic_group d;
13719	  if (warning_at (loc, OPT_Wrange_loop_construct,
13720			  "loop variable %qD of type %qT binds to a temporary "
13721			  "constructed from type %qT", decl, type,
13722			  TREE_TYPE (expr)))
13723	    {
13724	      tree ref = cp_build_qualified_type (TREE_TYPE (expr),
13725						  TYPE_QUAL_CONST);
13726	      ref = cp_build_reference_type (ref, /*rval*/false);
13727	      inform (loc, "use non-reference type %qT to make the copy "
13728		      "explicit or %qT to prevent copying",
13729		      non_reference (type), ref);
13730	    }
13731	}
13732      return;
13733    }
13734  else if (!CP_TYPE_CONST_P (type))
13735    return;
13736
13737  /* Since small trivially copyable types are cheap to copy, we suppress the
13738     warning for them.  64B is a common size of a cache line.  */
13739  if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
13740      || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
13741	  && trivially_copyable_p (type)))
13742    return;
13743
13744  tree rtype = cp_build_reference_type (type, /*rval*/false);
13745  /* If we could initialize the reference directly, it wouldn't involve any
13746     copies.  */
13747  if (!ref_conv_binds_directly_p (rtype, expr))
13748    return;
13749
13750  auto_diagnostic_group d;
13751  if (warning_at (loc, OPT_Wrange_loop_construct,
13752		  "loop variable %qD creates a copy from type %qT",
13753		  decl, type))
13754    {
13755      gcc_rich_location richloc (loc);
13756      richloc.add_fixit_insert_before ("&");
13757      inform (&richloc, "use reference type to prevent copying");
13758    }
13759}
13760
13761/* Converts a range-based for-statement into a normal
13762   for-statement, as per the definition.
13763
13764      for (RANGE_DECL : RANGE_EXPR)
13765	BLOCK
13766
13767   should be equivalent to:
13768
13769      {
13770	auto &&__range = RANGE_EXPR;
13771	for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13772	      __begin != __end;
13773	      ++__begin)
13774	  {
13775	      RANGE_DECL = *__begin;
13776	      BLOCK
13777	  }
13778      }
13779
13780   If RANGE_EXPR is an array:
13781	BEGIN_EXPR = __range
13782	END_EXPR = __range + ARRAY_SIZE(__range)
13783   Else if RANGE_EXPR has a member 'begin' or 'end':
13784	BEGIN_EXPR = __range.begin()
13785	END_EXPR = __range.end()
13786   Else:
13787	BEGIN_EXPR = begin(__range)
13788	END_EXPR = end(__range);
13789
13790   If __range has a member 'begin' but not 'end', or vice versa, we must
13791   still use the second alternative (it will surely fail, however).
13792   When calling begin()/end() in the third alternative we must use
13793   argument dependent lookup, but always considering 'std' as an associated
13794   namespace.  */
13795
13796tree
13797cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
13798		      tree decomp_first_name, unsigned int decomp_cnt,
13799		      bool ivdep, unsigned short unroll)
13800{
13801  tree begin, end;
13802  tree iter_type, begin_expr, end_expr;
13803  tree condition, expression;
13804
13805  range_expr = mark_lvalue_use (range_expr);
13806
13807  if (range_decl == error_mark_node || range_expr == error_mark_node)
13808    /* If an error happened previously do nothing or else a lot of
13809       unhelpful errors would be issued.  */
13810    begin_expr = end_expr = iter_type = error_mark_node;
13811  else
13812    {
13813      tree range_temp;
13814
13815      if (VAR_P (range_expr)
13816	  && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
13817	/* Can't bind a reference to an array of runtime bound.  */
13818	range_temp = range_expr;
13819      else
13820	{
13821	  range_temp = build_range_temp (range_expr);
13822	  pushdecl (range_temp);
13823	  cp_finish_decl (range_temp, range_expr,
13824			  /*is_constant_init*/false, NULL_TREE,
13825			  LOOKUP_ONLYCONVERTING);
13826	  range_temp = convert_from_reference (range_temp);
13827	}
13828      iter_type = cp_parser_perform_range_for_lookup (range_temp,
13829						      &begin_expr, &end_expr);
13830    }
13831
13832  /* The new for initialization statement.  */
13833  begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
13834		      iter_type);
13835  TREE_USED (begin) = 1;
13836  DECL_ARTIFICIAL (begin) = 1;
13837  pushdecl (begin);
13838  cp_finish_decl (begin, begin_expr,
13839		  /*is_constant_init*/false, NULL_TREE,
13840		  LOOKUP_ONLYCONVERTING);
13841
13842  if (cxx_dialect >= cxx17)
13843    iter_type = cv_unqualified (TREE_TYPE (end_expr));
13844  end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
13845  TREE_USED (end) = 1;
13846  DECL_ARTIFICIAL (end) = 1;
13847  pushdecl (end);
13848  cp_finish_decl (end, end_expr,
13849		  /*is_constant_init*/false, NULL_TREE,
13850		  LOOKUP_ONLYCONVERTING);
13851
13852  finish_init_stmt (statement);
13853
13854  /* The new for condition.  */
13855  condition = build_x_binary_op (input_location, NE_EXPR,
13856				 begin, ERROR_MARK,
13857				 end, ERROR_MARK,
13858				 NULL_TREE, NULL, tf_warning_or_error);
13859  finish_for_cond (condition, statement, ivdep, unroll);
13860
13861  /* The new increment expression.  */
13862  expression = finish_unary_op_expr (input_location,
13863				     PREINCREMENT_EXPR, begin,
13864				     tf_warning_or_error);
13865  finish_for_expr (expression, statement);
13866
13867  if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13868    cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
13869
13870  /* The declaration is initialized with *__begin inside the loop body.  */
13871  tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
13872					   NULL_TREE, tf_warning_or_error);
13873  cp_finish_decl (range_decl, deref_begin,
13874		  /*is_constant_init*/false, NULL_TREE,
13875		  LOOKUP_ONLYCONVERTING);
13876  if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13877    cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
13878
13879  warn_for_range_copy (range_decl, deref_begin);
13880
13881  return statement;
13882}
13883
13884/* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13885   We need to solve both at the same time because the method used
13886   depends on the existence of members begin or end.
13887   Returns the type deduced for the iterator expression.  */
13888
13889static tree
13890cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
13891{
13892  if (error_operand_p (range))
13893    {
13894      *begin = *end = error_mark_node;
13895      return error_mark_node;
13896    }
13897
13898  if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
13899    {
13900      error ("range-based %<for%> expression of type %qT "
13901	     "has incomplete type", TREE_TYPE (range));
13902      *begin = *end = error_mark_node;
13903      return error_mark_node;
13904    }
13905  if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
13906    {
13907      /* If RANGE is an array, we will use pointer arithmetic.  */
13908      *begin = decay_conversion (range, tf_warning_or_error);
13909      *end = build_binary_op (input_location, PLUS_EXPR,
13910			      range,
13911			      array_type_nelts_top (TREE_TYPE (range)),
13912			      false);
13913      return TREE_TYPE (*begin);
13914    }
13915  else
13916    {
13917      /* If it is not an array, we must do a bit of magic.  */
13918      tree id_begin, id_end;
13919      tree member_begin, member_end;
13920
13921      *begin = *end = error_mark_node;
13922
13923      id_begin = get_identifier ("begin");
13924      id_end = get_identifier ("end");
13925      member_begin = lookup_member (TREE_TYPE (range), id_begin,
13926				    /*protect=*/2, /*want_type=*/false,
13927				    tf_warning_or_error);
13928      member_end = lookup_member (TREE_TYPE (range), id_end,
13929				  /*protect=*/2, /*want_type=*/false,
13930				  tf_warning_or_error);
13931
13932      if (member_begin != NULL_TREE && member_end != NULL_TREE)
13933	{
13934	  /* Use the member functions.  */
13935	  *begin = cp_parser_range_for_member_function (range, id_begin);
13936	  *end = cp_parser_range_for_member_function (range, id_end);
13937	}
13938      else
13939	{
13940	  /* Use global functions with ADL.  */
13941	  releasing_vec vec;
13942
13943	  vec_safe_push (vec, range);
13944
13945	  member_begin = perform_koenig_lookup (id_begin, vec,
13946						tf_warning_or_error);
13947	  *begin = finish_call_expr (member_begin, &vec, false, true,
13948				     tf_warning_or_error);
13949	  member_end = perform_koenig_lookup (id_end, vec,
13950					      tf_warning_or_error);
13951	  *end = finish_call_expr (member_end, &vec, false, true,
13952				   tf_warning_or_error);
13953	}
13954
13955      /* Last common checks.  */
13956      if (*begin == error_mark_node || *end == error_mark_node)
13957	{
13958	  /* If one of the expressions is an error do no more checks.  */
13959	  *begin = *end = error_mark_node;
13960	  return error_mark_node;
13961	}
13962      else if (type_dependent_expression_p (*begin)
13963	       || type_dependent_expression_p (*end))
13964	/* Can happen, when, eg, in a template context, Koenig lookup
13965	   can't resolve begin/end (c++/58503).  */
13966	return NULL_TREE;
13967      else
13968	{
13969	  tree iter_type = cv_unqualified (TREE_TYPE (*begin));
13970	  /* The unqualified type of the __begin and __end temporaries should
13971	     be the same, as required by the multiple auto declaration.  */
13972	  if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
13973	    {
13974	      if (cxx_dialect >= cxx17
13975		  && (build_x_binary_op (input_location, NE_EXPR,
13976					 *begin, ERROR_MARK,
13977					 *end, ERROR_MARK,
13978					 NULL_TREE, NULL, tf_none)
13979		      != error_mark_node))
13980		/* P0184R0 allows __begin and __end to have different types,
13981		   but make sure they are comparable so we can give a better
13982		   diagnostic.  */;
13983	      else
13984		error ("inconsistent begin/end types in range-based %<for%> "
13985		       "statement: %qT and %qT",
13986		       TREE_TYPE (*begin), TREE_TYPE (*end));
13987	    }
13988	  return iter_type;
13989	}
13990    }
13991}
13992
13993/* Helper function for cp_parser_perform_range_for_lookup.
13994   Builds a tree for RANGE.IDENTIFIER().  */
13995
13996static tree
13997cp_parser_range_for_member_function (tree range, tree identifier)
13998{
13999  tree member, res;
14000
14001  member = finish_class_member_access_expr (range, identifier,
14002					    false, tf_warning_or_error);
14003  if (member == error_mark_node)
14004    return error_mark_node;
14005
14006  releasing_vec vec;
14007  res = finish_call_expr (member, &vec,
14008			  /*disallow_virtual=*/false,
14009			  /*koenig_p=*/false,
14010			  tf_warning_or_error);
14011  return res;
14012}
14013
14014/* Parse an iteration-statement.
14015
14016   iteration-statement:
14017     while ( condition ) statement
14018     do statement while ( expression ) ;
14019     for ( init-statement condition [opt] ; expression [opt] )
14020       statement
14021
14022   Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
14023
14024static tree
14025cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14026			       unsigned short unroll)
14027{
14028  cp_token *token;
14029  enum rid keyword;
14030  tree statement;
14031  unsigned char in_statement;
14032  token_indent_info guard_tinfo;
14033
14034  /* Peek at the next token.  */
14035  token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14036  if (!token)
14037    return error_mark_node;
14038
14039  guard_tinfo = get_token_indent_info (token);
14040
14041  /* Remember whether or not we are already within an iteration
14042     statement.  */
14043  in_statement = parser->in_statement;
14044
14045  /* See what kind of keyword it is.  */
14046  keyword = token->keyword;
14047  switch (keyword)
14048    {
14049    case RID_WHILE:
14050      {
14051	tree condition;
14052
14053	/* Begin the while-statement.  */
14054	statement = begin_while_stmt ();
14055	/* Look for the `('.  */
14056	matching_parens parens;
14057	parens.require_open (parser);
14058	/* Parse the condition.  */
14059	condition = cp_parser_condition (parser);
14060	finish_while_stmt_cond (condition, statement, ivdep, unroll);
14061	/* Look for the `)'.  */
14062	parens.require_close (parser);
14063	/* Parse the dependent statement.  */
14064	parser->in_statement = IN_ITERATION_STMT;
14065	bool prev = note_iteration_stmt_body_start ();
14066	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14067	note_iteration_stmt_body_end (prev);
14068	parser->in_statement = in_statement;
14069	/* We're done with the while-statement.  */
14070	finish_while_stmt (statement);
14071      }
14072      break;
14073
14074    case RID_DO:
14075      {
14076	tree expression;
14077
14078	/* Begin the do-statement.  */
14079	statement = begin_do_stmt ();
14080	/* Parse the body of the do-statement.  */
14081	parser->in_statement = IN_ITERATION_STMT;
14082	bool prev = note_iteration_stmt_body_start ();
14083	cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14084	note_iteration_stmt_body_end (prev);
14085	parser->in_statement = in_statement;
14086	finish_do_body (statement);
14087	/* Look for the `while' keyword.  */
14088	cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14089	/* Look for the `('.  */
14090	matching_parens parens;
14091	parens.require_open (parser);
14092	/* Parse the expression.  */
14093	expression = cp_parser_expression (parser);
14094	/* We're done with the do-statement.  */
14095	finish_do_stmt (expression, statement, ivdep, unroll);
14096	/* Look for the `)'.  */
14097	parens.require_close (parser);
14098	/* Look for the `;'.  */
14099	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14100      }
14101      break;
14102
14103    case RID_FOR:
14104      {
14105	/* Look for the `('.  */
14106	matching_parens parens;
14107	parens.require_open (parser);
14108
14109	statement = cp_parser_for (parser, ivdep, unroll);
14110
14111	/* Look for the `)'.  */
14112	parens.require_close (parser);
14113
14114	/* Parse the body of the for-statement.  */
14115	parser->in_statement = IN_ITERATION_STMT;
14116	bool prev = note_iteration_stmt_body_start ();
14117	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14118	note_iteration_stmt_body_end (prev);
14119	parser->in_statement = in_statement;
14120
14121	/* We're done with the for-statement.  */
14122	finish_for_stmt (statement);
14123      }
14124      break;
14125
14126    default:
14127      cp_parser_error (parser, "expected iteration-statement");
14128      statement = error_mark_node;
14129      break;
14130    }
14131
14132  return statement;
14133}
14134
14135/* Parse an init-statement or the declarator of a range-based-for.
14136   Returns true if a range-based-for declaration is seen.
14137
14138   init-statement:
14139     expression-statement
14140     simple-declaration
14141     alias-declaration  */
14142
14143static bool
14144cp_parser_init_statement (cp_parser *parser, tree *decl)
14145{
14146  /* If the next token is a `;', then we have an empty
14147     expression-statement.  Grammatically, this is also a
14148     simple-declaration, but an invalid one, because it does not
14149     declare anything.  Therefore, if we did not handle this case
14150     specially, we would issue an error message about an invalid
14151     declaration.  */
14152  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14153    {
14154      bool is_range_for = false;
14155      bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14156
14157      /* A colon is used in range-based for.  */
14158      parser->colon_corrects_to_scope_p = false;
14159
14160      /* We're going to speculatively look for a declaration, falling back
14161	 to an expression, if necessary.  */
14162      cp_parser_parse_tentatively (parser);
14163      bool expect_semicolon_p = true;
14164      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14165	{
14166	  cp_parser_alias_declaration (parser);
14167	  expect_semicolon_p = false;
14168	  if (cxx_dialect < cxx23
14169	      && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14170	    pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14171		     OPT_Wc__23_extensions,
14172		     "alias-declaration in init-statement only "
14173		     "available with %<-std=c++23%> or %<-std=gnu++23%>");
14174	}
14175      else
14176	/* Parse the declaration.  */
14177	cp_parser_simple_declaration (parser,
14178				      /*function_definition_allowed_p=*/false,
14179				      decl);
14180      parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14181      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14182	{
14183	  /* It is a range-for, consume the ':'.  */
14184	  cp_lexer_consume_token (parser->lexer);
14185	  is_range_for = true;
14186	  if (cxx_dialect < cxx11)
14187	    pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14188		     OPT_Wc__11_extensions,
14189		     "range-based %<for%> loops only available with "
14190		     "%<-std=c++11%> or %<-std=gnu++11%>");
14191	}
14192      else if (expect_semicolon_p)
14193	/* The ';' is not consumed yet because we told
14194	   cp_parser_simple_declaration not to.  */
14195	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14196
14197      if (cp_parser_parse_definitely (parser))
14198	return is_range_for;
14199      /* If the tentative parse failed, then we shall need to look for an
14200	 expression-statement.  */
14201    }
14202  /* If we are here, it is an expression-statement.  */
14203  cp_parser_expression_statement (parser, NULL_TREE);
14204  return false;
14205}
14206
14207/* Parse a jump-statement.
14208
14209   jump-statement:
14210     break ;
14211     continue ;
14212     return expression [opt] ;
14213     return braced-init-list ;
14214     coroutine-return-statement;
14215     goto identifier ;
14216
14217   GNU extension:
14218
14219   jump-statement:
14220     goto * expression ;
14221
14222   Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
14223
14224static tree
14225cp_parser_jump_statement (cp_parser* parser)
14226{
14227  tree statement = error_mark_node;
14228  cp_token *token;
14229  enum rid keyword;
14230  unsigned char in_statement;
14231
14232  /* Peek at the next token.  */
14233  token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14234  if (!token)
14235    return error_mark_node;
14236
14237  /* See what kind of keyword it is.  */
14238  keyword = token->keyword;
14239  switch (keyword)
14240    {
14241    case RID_BREAK:
14242      in_statement = parser->in_statement & ~IN_IF_STMT;
14243      switch (in_statement)
14244	{
14245	case 0:
14246	  error_at (token->location, "break statement not within loop or switch");
14247	  break;
14248	default:
14249	  gcc_assert ((in_statement & IN_SWITCH_STMT)
14250		      || in_statement == IN_ITERATION_STMT);
14251	  statement = finish_break_stmt ();
14252	  if (in_statement == IN_ITERATION_STMT)
14253	    break_maybe_infinite_loop ();
14254	  break;
14255	case IN_OMP_BLOCK:
14256	  error_at (token->location, "invalid exit from OpenMP structured block");
14257	  break;
14258	case IN_OMP_FOR:
14259	  error_at (token->location, "break statement used with OpenMP for loop");
14260	  break;
14261	}
14262      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14263      break;
14264
14265    case RID_CONTINUE:
14266      switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14267	{
14268	case 0:
14269	  error_at (token->location, "continue statement not within a loop");
14270	  break;
14271	  /* Fall through.  */
14272	case IN_ITERATION_STMT:
14273	case IN_OMP_FOR:
14274	  statement = finish_continue_stmt ();
14275	  break;
14276	case IN_OMP_BLOCK:
14277	  error_at (token->location, "invalid exit from OpenMP structured block");
14278	  break;
14279	default:
14280	  gcc_unreachable ();
14281	}
14282      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14283      break;
14284
14285    case RID_CO_RETURN:
14286    case RID_RETURN:
14287      {
14288	tree expr;
14289	bool expr_non_constant_p;
14290
14291	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14292	  {
14293	    cp_lexer_set_source_position (parser->lexer);
14294	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14295	    expr = cp_parser_braced_list (parser, &expr_non_constant_p);
14296	  }
14297	else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14298	  expr = cp_parser_expression (parser);
14299	else
14300	  /* If the next token is a `;', then there is no
14301	     expression.  */
14302	  expr = NULL_TREE;
14303	/* Build the return-statement, check co-return first, since type
14304	   deduction is not valid there.  */
14305	if (keyword == RID_CO_RETURN)
14306	  statement = finish_co_return_stmt (token->location, expr);
14307	else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14308	  /* Don't deduce from a discarded return statement.  */;
14309	else
14310	  statement = finish_return_stmt (expr);
14311	/* Look for the final `;'.  */
14312	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14313      }
14314      break;
14315
14316    case RID_GOTO:
14317      if (parser->in_function_body
14318	  && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14319	  && cxx_dialect < cxx23)
14320	{
14321	  error ("%<goto%> in %<constexpr%> function only available with "
14322		 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14323	  cp_function_chain->invalid_constexpr = true;
14324	}
14325
14326      /* Create the goto-statement.  */
14327      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14328	{
14329	  /* Issue a warning about this use of a GNU extension.  */
14330	  pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14331	  /* Consume the '*' token.  */
14332	  cp_lexer_consume_token (parser->lexer);
14333	  /* Parse the dependent expression.  */
14334	  finish_goto_stmt (cp_parser_expression (parser));
14335	}
14336      else
14337	finish_goto_stmt (cp_parser_identifier (parser));
14338      /* Look for the final `;'.  */
14339      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14340      break;
14341
14342    default:
14343      cp_parser_error (parser, "expected jump-statement");
14344      break;
14345    }
14346
14347  return statement;
14348}
14349
14350/* Parse a declaration-statement.
14351
14352   declaration-statement:
14353     block-declaration  */
14354
14355static void
14356cp_parser_declaration_statement (cp_parser* parser)
14357{
14358  void *p;
14359
14360  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
14361  p = obstack_alloc (&declarator_obstack, 0);
14362
14363 /* Parse the block-declaration.  */
14364  cp_parser_block_declaration (parser, /*statement_p=*/true);
14365
14366  /* Free any declarators allocated.  */
14367  obstack_free (&declarator_obstack, p);
14368}
14369
14370/* Some dependent statements (like `if (cond) statement'), are
14371   implicitly in their own scope.  In other words, if the statement is
14372   a single statement (as opposed to a compound-statement), it is
14373   none-the-less treated as if it were enclosed in braces.  Any
14374   declarations appearing in the dependent statement are out of scope
14375   after control passes that point.  This function parses a statement,
14376   but ensures that is in its own scope, even if it is not a
14377   compound-statement.
14378
14379   If IF_P is not NULL, *IF_P is set to indicate whether the statement
14380   is a (possibly labeled) if statement which is not enclosed in
14381   braces and has an else clause.  This is used to implement
14382   -Wparentheses.
14383
14384   CHAIN is a vector of if-else-if conditions.  This is used to implement
14385   -Wduplicated-cond.
14386
14387   Returns the new statement.  */
14388
14389static tree
14390cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14391				       const token_indent_info &guard_tinfo,
14392				       vec<tree> *chain)
14393{
14394  tree statement;
14395  location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14396  location_t body_loc_after_labels = UNKNOWN_LOCATION;
14397  token_indent_info body_tinfo
14398    = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14399
14400  if (if_p != NULL)
14401    *if_p = false;
14402
14403  /* Mark if () ; with a special NOP_EXPR.  */
14404  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14405    {
14406      cp_lexer_consume_token (parser->lexer);
14407      statement = add_stmt (build_empty_stmt (body_loc));
14408
14409      if (guard_tinfo.keyword == RID_IF
14410	  && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14411	warning_at (body_loc, OPT_Wempty_body,
14412		    "suggest braces around empty body in an %<if%> statement");
14413      else if (guard_tinfo.keyword == RID_ELSE)
14414	warning_at (body_loc, OPT_Wempty_body,
14415		    "suggest braces around empty body in an %<else%> statement");
14416    }
14417  /* if a compound is opened, we simply parse the statement directly.  */
14418  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14419    statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14420  /* If the token is not a `{', then we must take special action.  */
14421  else
14422    {
14423      /* Create a compound-statement.  */
14424      statement = begin_compound_stmt (0);
14425      /* Parse the dependent-statement.  */
14426      cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14427			   &body_loc_after_labels);
14428      /* Finish the dummy compound-statement.  */
14429      finish_compound_stmt (statement);
14430    }
14431
14432  token_indent_info next_tinfo
14433    = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14434  warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14435
14436  if (body_loc_after_labels != UNKNOWN_LOCATION
14437      && next_tinfo.type != CPP_SEMICOLON)
14438    warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14439				    guard_tinfo.location, guard_tinfo.keyword);
14440
14441  /* Return the statement.  */
14442  return statement;
14443}
14444
14445/* For some dependent statements (like `while (cond) statement'), we
14446   have already created a scope.  Therefore, even if the dependent
14447   statement is a compound-statement, we do not want to create another
14448   scope.  */
14449
14450static void
14451cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14452				    const token_indent_info &guard_tinfo)
14453{
14454  /* If the token is a `{', then we must take special action.  */
14455  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14456    {
14457      token_indent_info body_tinfo
14458	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14459      location_t loc_after_labels = UNKNOWN_LOCATION;
14460
14461      cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14462			   &loc_after_labels);
14463      token_indent_info next_tinfo
14464	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14465      warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14466
14467      if (loc_after_labels != UNKNOWN_LOCATION
14468	  && next_tinfo.type != CPP_SEMICOLON)
14469	warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14470					guard_tinfo.location,
14471					guard_tinfo.keyword);
14472    }
14473  else
14474    {
14475      /* Avoid calling cp_parser_compound_statement, so that we
14476	 don't create a new scope.  Do everything else by hand.  */
14477      matching_braces braces;
14478      braces.require_open (parser);
14479      /* If the next keyword is `__label__' we have a label declaration.  */
14480      while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14481	cp_parser_label_declaration (parser);
14482      /* Parse an (optional) statement-seq.  */
14483      cp_parser_statement_seq_opt (parser, NULL_TREE);
14484      braces.require_close (parser);
14485    }
14486}
14487
14488/* Modules */
14489
14490/* Parse a module-name,
14491   identifier
14492   module-name . identifier
14493   header-name
14494
14495   Returns a pointer to module object, NULL.   */
14496
14497static module_state *
14498cp_parser_module_name (cp_parser *parser)
14499{
14500  cp_token *token = cp_lexer_peek_token (parser->lexer);
14501  if (token->type == CPP_HEADER_NAME)
14502    {
14503      cp_lexer_consume_token (parser->lexer);
14504
14505      return get_module (token->u.value);
14506    }
14507
14508  module_state *parent = NULL;
14509  bool partitioned = false;
14510  if (token->type == CPP_COLON && named_module_p ())
14511    {
14512      partitioned = true;
14513      cp_lexer_consume_token (parser->lexer);
14514    }
14515
14516  for (;;)
14517    {
14518      if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14519	{
14520	  cp_parser_error (parser, "expected module-name");
14521	  break;
14522	}
14523
14524      tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14525      parent = get_module (name, parent, partitioned);
14526      token = cp_lexer_peek_token (parser->lexer);
14527      if (!partitioned && token->type == CPP_COLON)
14528	partitioned = true;
14529      else if (token->type != CPP_DOT)
14530	break;
14531
14532      cp_lexer_consume_token (parser->lexer);
14533   }
14534
14535  return parent;
14536}
14537
14538/* Named module-declaration
14539     __module ; PRAGMA_EOL
14540     __module private ; PRAGMA_EOL (unimplemented)
14541     [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14542*/
14543
14544static module_parse
14545cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14546			      bool exporting)
14547{
14548  /* We're a pseudo pragma.  */
14549  parser->lexer->in_pragma = true;
14550  cp_token *token = cp_lexer_consume_token (parser->lexer);
14551
14552  if (flag_header_unit)
14553    {
14554      error_at (token->location,
14555		"module-declaration not permitted in header-unit");
14556      goto skip_eol;
14557    }
14558  else if (mp_state == MP_FIRST && !exporting
14559      && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14560    {
14561      /* Start global module fragment.  */
14562      cp_lexer_consume_token (parser->lexer);
14563      module_kind |= MK_GLOBAL;
14564      mp_state = MP_GLOBAL;
14565      cp_parser_require_pragma_eol (parser, token);
14566    }
14567  else if (!exporting
14568	   && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14569	   && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14570	   && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14571    {
14572      cp_lexer_consume_token (parser->lexer);
14573      cp_lexer_consume_token (parser->lexer);
14574      cp_lexer_consume_token (parser->lexer);
14575      cp_parser_require_pragma_eol (parser, token);
14576
14577      if (!(mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14578	  || !module_interface_p () || module_partition_p ())
14579	error_at (token->location,
14580		  "private module fragment only permitted in purview"
14581		  " of module interface or partition");
14582      else
14583	{
14584	  mp_state = MP_PRIVATE_IMPORTS;
14585	  sorry_at (token->location, "private module fragment");
14586	}
14587    }
14588  else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14589    {
14590      /* Neither the first declaration, nor in a GMF.  */
14591      error_at (token->location, "module-declaration only permitted as first"
14592		" declaration, or ending a global module fragment");
14593    skip_eol:
14594      cp_parser_skip_to_pragma_eol (parser, token);
14595    }
14596  else
14597    {
14598      module_state *mod = cp_parser_module_name (parser);
14599      tree attrs = cp_parser_attributes_opt (parser);
14600
14601      mp_state = MP_PURVIEW_IMPORTS;
14602      if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14603	goto skip_eol;
14604
14605      declare_module (mod, token->location, exporting, attrs, parse_in);
14606      cp_parser_require_pragma_eol (parser, token);
14607    }
14608
14609  return mp_state;
14610}
14611
14612/* Import-declaration
14613   [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14614
14615static void
14616cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14617			      bool exporting)
14618{
14619  /* We're a pseudo pragma.  */
14620  parser->lexer->in_pragma = true;
14621  cp_token *token = cp_lexer_consume_token (parser->lexer);
14622
14623  if (mp_state != MP_PURVIEW_IMPORTS
14624      && mp_state != MP_PRIVATE_IMPORTS
14625      && module_purview_p ()
14626      && !global_purview_p ())
14627    {
14628      error_at (token->location, "post-module-declaration"
14629		" imports must be contiguous");
14630    note_lexer:
14631      inform (token->location, "perhaps insert a line break, or other"
14632	      " disambiguation, to prevent this being considered a"
14633	      " module control-line");
14634    skip_eol:
14635      cp_parser_skip_to_pragma_eol (parser, token);
14636    }
14637  else if (current_scope () != global_namespace)
14638    {
14639      error_at (token->location, "import-declaration must be at global scope");
14640      goto note_lexer;
14641    }
14642  else
14643    {
14644      module_state *mod = cp_parser_module_name (parser);
14645      tree attrs = cp_parser_attributes_opt (parser);
14646
14647      if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14648	goto skip_eol;
14649      cp_parser_require_pragma_eol (parser, token);
14650
14651      if (parser->in_unbraced_linkage_specification_p)
14652	error_at (token->location, "import cannot appear directly in"
14653		  " a linkage-specification");
14654
14655      /* Module-purview imports must not be from source inclusion
14656	 [cpp.import]/7  */
14657      if (attrs && module_purview_p () && !global_purview_p ()
14658	  && private_lookup_attribute ("__translated",
14659				       strlen ("__translated"), attrs))
14660	error_at (token->location, "post-module-declaration imports"
14661		  " must not be include-translated");
14662      else if ((mp_state == MP_PURVIEW_IMPORTS
14663		|| mp_state == MP_PRIVATE_IMPORTS)
14664	       && !token->main_source_p)
14665	error_at (token->location, "post-module-declaration imports"
14666		  " must not be from header inclusion");
14667
14668      import_module (mod, token->location, exporting, attrs, parse_in);
14669    }
14670}
14671
14672/*  export-declaration.
14673
14674    export declaration
14675    export { declaration-seq-opt }  */
14676
14677static void
14678cp_parser_module_export (cp_parser *parser)
14679{
14680  gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
14681  cp_token *token = cp_lexer_consume_token (parser->lexer);
14682
14683  if (!module_interface_p ())
14684    error_at (token->location,
14685	      "%qE may only occur after a module interface declaration",
14686	      token->u.value);
14687
14688  bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
14689
14690  unsigned mk = module_kind;
14691  if (module_exporting_p ())
14692    error_at (token->location,
14693	      "%qE may only occur once in an export declaration",
14694	      token->u.value);
14695  module_kind |= MK_EXPORTING;
14696
14697  if (braced)
14698    {
14699      cp_ensure_no_omp_declare_simd (parser);
14700      cp_ensure_no_oacc_routine (parser);
14701
14702      cp_lexer_consume_token (parser->lexer);
14703      cp_parser_declaration_seq_opt (parser);
14704      cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14705    }
14706  else
14707    {
14708      /* Explicitly check if the next tokens might be a
14709         module-directive line, so we can give a clearer error message
14710         about why the directive will be rejected.  */
14711      if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
14712	  || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
14713	  || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
14714	error_at (token->location, "%<export%> not part of following"
14715		  " module-directive");
14716      cp_parser_declaration (parser, NULL_TREE);
14717    }
14718
14719  module_kind = mk;
14720}
14721
14722/* Declarations [gram.dcl.dcl] */
14723
14724/* Parse an optional declaration-sequence.  TOP_LEVEL is true, if this
14725   is the top-level declaration sequence.  That affects whether we
14726   deal with module-preamble.
14727
14728   declaration-seq:
14729     declaration
14730     declaration-seq declaration  */
14731
14732static void
14733cp_parser_declaration_seq_opt (cp_parser* parser)
14734{
14735  while (true)
14736    {
14737      cp_token *token = cp_lexer_peek_token (parser->lexer);
14738
14739      if (token->type == CPP_CLOSE_BRACE
14740	  || token->type == CPP_EOF)
14741	break;
14742      else
14743	cp_parser_toplevel_declaration (parser);
14744    }
14745}
14746
14747/* Parse a declaration.
14748
14749   declaration:
14750     block-declaration
14751     function-definition
14752     template-declaration
14753     explicit-instantiation
14754     explicit-specialization
14755     linkage-specification
14756     namespace-definition
14757
14758   C++17:
14759     deduction-guide
14760
14761   modules:
14762     (all these are only allowed at the outermost level, check
14763   	that semantically, for better diagnostics)
14764     module-declaration
14765     module-export-declaration
14766     module-import-declaration
14767     export-declaration
14768
14769   GNU extension:
14770
14771   declaration:
14772      __extension__ declaration */
14773
14774static void
14775cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
14776{
14777  int saved_pedantic;
14778
14779  /* Check for the `__extension__' keyword.  */
14780  if (cp_parser_extension_opt (parser, &saved_pedantic))
14781    {
14782      /* Parse the qualified declaration.  */
14783      cp_parser_declaration (parser, prefix_attrs);
14784      /* Restore the PEDANTIC flag.  */
14785      pedantic = saved_pedantic;
14786
14787      return;
14788    }
14789
14790  /* Try to figure out what kind of declaration is present.  */
14791  cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14792  cp_token *token2 = (token1->type == CPP_EOF
14793		      ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14794
14795  if (token1->type == CPP_SEMICOLON)
14796    {
14797      cp_lexer_consume_token (parser->lexer);
14798      /* A declaration consisting of a single semicolon is invalid
14799       * before C++11.  Allow it unless we're being pedantic.  */
14800      if (cxx_dialect < cxx11)
14801	pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
14802      return;
14803    }
14804  else if (cp_lexer_nth_token_is (parser->lexer,
14805				  cp_parser_skip_std_attribute_spec_seq (parser,
14806									 1),
14807				  CPP_SEMICOLON))
14808    {
14809      location_t attrs_loc = token1->location;
14810      tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
14811
14812      if (std_attrs && (flag_openmp || flag_openmp_simd))
14813	{
14814	  gcc_assert (!parser->lexer->in_omp_attribute_pragma);
14815	  std_attrs = cp_parser_handle_statement_omp_attributes (parser,
14816								 std_attrs);
14817	  if (parser->lexer->in_omp_attribute_pragma)
14818	    {
14819	      cp_lexer *lexer = parser->lexer;
14820	      while (parser->lexer->in_omp_attribute_pragma)
14821		{
14822		  gcc_assert (cp_lexer_next_token_is (parser->lexer,
14823						      CPP_PRAGMA));
14824		  cp_parser_pragma (parser, pragma_external, NULL);
14825		}
14826	      cp_lexer_destroy (lexer);
14827	    }
14828	}
14829
14830      if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
14831	warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
14832		    OPT_Wattributes, "attribute ignored");
14833      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14834	cp_lexer_consume_token (parser->lexer);
14835      return;
14836    }
14837
14838  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
14839  void *p = obstack_alloc (&declarator_obstack, 0);
14840
14841  tree attributes = NULL_TREE;
14842
14843  /* Conditionally, allow attributes to precede a linkage specification.  */
14844  if (token1->keyword == RID_ATTRIBUTE)
14845    {
14846      cp_lexer_save_tokens (parser->lexer);
14847      attributes = cp_parser_attributes_opt (parser);
14848      cp_token *t1 = cp_lexer_peek_token (parser->lexer);
14849      cp_token *t2 = (t1->type == CPP_EOF
14850		      ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14851      if (t1->keyword == RID_EXTERN
14852	  && cp_parser_is_pure_string_literal (t2))
14853	{
14854	  cp_lexer_commit_tokens (parser->lexer);
14855	  /* We might have already been here.  */
14856	  if (!c_dialect_objc ())
14857	    {
14858	      location_t where = get_finish (t2->location);
14859	      warning_at (token1->location, OPT_Wattributes, "attributes are"
14860			  " not permitted in this position");
14861	      where = linemap_position_for_loc_and_offset (line_table,
14862							   where, 1);
14863	      inform (where, "attributes may be inserted here");
14864	      attributes = NULL_TREE;
14865	    }
14866	  token1 = t1;
14867	  token2 = t2;
14868	}
14869      else
14870	{
14871	  cp_lexer_rollback_tokens (parser->lexer);
14872	  attributes = NULL_TREE;
14873	}
14874    }
14875  /* If we already had some attributes, and we've added more, then prepend.
14876     Otherwise attributes just contains any that we just read.  */
14877  if (prefix_attrs)
14878    {
14879      if (attributes)
14880	TREE_CHAIN (prefix_attrs) = attributes;
14881      attributes = prefix_attrs;
14882    }
14883
14884  /* If the next token is `extern' and the following token is a string
14885     literal, then we have a linkage specification.  */
14886  if (token1->keyword == RID_EXTERN
14887      && cp_parser_is_pure_string_literal (token2))
14888    cp_parser_linkage_specification (parser, attributes);
14889  /* If the next token is `template', then we have either a template
14890     declaration, an explicit instantiation, or an explicit
14891     specialization.  */
14892  else if (token1->keyword == RID_TEMPLATE)
14893    {
14894      /* `template <>' indicates a template specialization.  */
14895      if (token2->type == CPP_LESS
14896	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14897	cp_parser_explicit_specialization (parser);
14898      /* `template <' indicates a template declaration.  */
14899      else if (token2->type == CPP_LESS)
14900	cp_parser_template_declaration (parser, /*member_p=*/false);
14901      /* Anything else must be an explicit instantiation.  */
14902      else
14903	cp_parser_explicit_instantiation (parser);
14904    }
14905  /* If the next token is `export', it's new-style modules or
14906     old-style template.  */
14907  else if (token1->keyword == RID_EXPORT)
14908    {
14909      if (!modules_p ())
14910	cp_parser_template_declaration (parser, /*member_p=*/false);
14911      else
14912	cp_parser_module_export (parser);
14913    }
14914  else if (token1->keyword == RID__EXPORT
14915	   || token1->keyword == RID__IMPORT
14916	   || token1->keyword == RID__MODULE)
14917    {
14918      bool exporting = token1->keyword == RID__EXPORT;
14919      cp_token *next = exporting ? token2 : token1;
14920      if (exporting)
14921	cp_lexer_consume_token (parser->lexer);
14922      if (next->keyword == RID__MODULE)
14923	cp_parser_module_declaration (parser, MP_NOT_MODULE, exporting);
14924      else
14925	cp_parser_import_declaration (parser, MP_NOT_MODULE, exporting);
14926    }
14927  /* If the next token is `extern', 'static' or 'inline' and the one
14928     after that is `template', we have a GNU extended explicit
14929     instantiation directive.  */
14930  else if (cp_parser_allow_gnu_extensions_p (parser)
14931	   && token2->keyword == RID_TEMPLATE
14932	   && (token1->keyword == RID_EXTERN
14933	       || token1->keyword == RID_STATIC
14934	       || token1->keyword == RID_INLINE))
14935    cp_parser_explicit_instantiation (parser);
14936  /* If the next token is `namespace', check for a named or unnamed
14937     namespace definition.  */
14938  else if (token1->keyword == RID_NAMESPACE
14939	   && (/* A named namespace definition.  */
14940	       (token2->type == CPP_NAME
14941		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
14942		    != CPP_EQ))
14943               || (token2->type == CPP_OPEN_SQUARE
14944                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
14945                   == CPP_OPEN_SQUARE)
14946	       /* An unnamed namespace definition.  */
14947	       || token2->type == CPP_OPEN_BRACE
14948	       || token2->keyword == RID_ATTRIBUTE))
14949    cp_parser_namespace_definition (parser);
14950  /* An inline (associated) namespace definition.  */
14951  else if (token2->keyword == RID_NAMESPACE
14952	   && token1->keyword == RID_INLINE)
14953    cp_parser_namespace_definition (parser);
14954  /* Objective-C++ declaration/definition.  */
14955  else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
14956    cp_parser_objc_declaration (parser, attributes);
14957  else if (c_dialect_objc ()
14958	   && token1->keyword == RID_ATTRIBUTE
14959	   && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
14960    cp_parser_objc_declaration (parser, attributes);
14961  /* At this point we may have a template declared by a concept
14962     introduction.  */
14963  else if (flag_concepts
14964	   && cp_parser_template_declaration_after_export (parser,
14965							   /*member_p=*/false))
14966    /* We did.  */;
14967  else
14968    /* Try to parse a block-declaration, or a function-definition.  */
14969    cp_parser_block_declaration (parser, /*statement_p=*/false);
14970
14971  /* Free any declarators allocated.  */
14972  obstack_free (&declarator_obstack, p);
14973}
14974
14975/* Parse a namespace-scope declaration.  */
14976
14977static void
14978cp_parser_toplevel_declaration (cp_parser* parser)
14979{
14980  cp_token *token = cp_lexer_peek_token (parser->lexer);
14981
14982  if (token->type == CPP_PRAGMA)
14983    /* A top-level declaration can consist solely of a #pragma.  A
14984       nested declaration cannot, so this is done here and not in
14985       cp_parser_declaration.  (A #pragma at block scope is
14986       handled in cp_parser_statement.)  */
14987    cp_parser_pragma (parser, pragma_external, NULL);
14988  else
14989    /* Parse the declaration itself.  */
14990    cp_parser_declaration (parser, NULL_TREE);
14991}
14992
14993/* Parse a block-declaration.
14994
14995   block-declaration:
14996     simple-declaration
14997     asm-definition
14998     namespace-alias-definition
14999     using-declaration
15000     using-directive
15001
15002   GNU Extension:
15003
15004   block-declaration:
15005     __extension__ block-declaration
15006
15007   C++0x Extension:
15008
15009   block-declaration:
15010     static_assert-declaration
15011
15012   If STATEMENT_P is TRUE, then this block-declaration is occurring as
15013   part of a declaration-statement.  */
15014
15015static void
15016cp_parser_block_declaration (cp_parser *parser,
15017			     bool      statement_p)
15018{
15019  int saved_pedantic;
15020
15021  /* Check for the `__extension__' keyword.  */
15022  if (cp_parser_extension_opt (parser, &saved_pedantic))
15023    {
15024      /* Parse the qualified declaration.  */
15025      cp_parser_block_declaration (parser, statement_p);
15026      /* Restore the PEDANTIC flag.  */
15027      pedantic = saved_pedantic;
15028
15029      return;
15030    }
15031
15032  /* Peek at the next token to figure out which kind of declaration is
15033     present.  */
15034  cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15035  size_t attr_idx;
15036
15037  /* If the next keyword is `asm', we have an asm-definition.  */
15038  if (token1->keyword == RID_ASM)
15039    {
15040      if (statement_p)
15041	cp_parser_commit_to_tentative_parse (parser);
15042      cp_parser_asm_definition (parser);
15043    }
15044  /* If the next keyword is `namespace', we have a
15045     namespace-alias-definition.  */
15046  else if (token1->keyword == RID_NAMESPACE)
15047    cp_parser_namespace_alias_definition (parser);
15048  /* If the next keyword is `using', we have a
15049     using-declaration, a using-directive, or an alias-declaration.  */
15050  else if (token1->keyword == RID_USING)
15051    {
15052      cp_token *token2;
15053
15054      if (statement_p)
15055	cp_parser_commit_to_tentative_parse (parser);
15056      /* If the token after `using' is `namespace', then we have a
15057	 using-directive.  */
15058      token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15059      if (token2->keyword == RID_NAMESPACE)
15060	cp_parser_using_directive (parser);
15061      else if (token2->keyword == RID_ENUM)
15062	cp_parser_using_enum (parser);
15063      /* If the second token after 'using' is '=', then we have an
15064	 alias-declaration.  */
15065      else if (cxx_dialect >= cxx11
15066	       && token2->type == CPP_NAME
15067	       && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15068		   || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15069	cp_parser_alias_declaration (parser);
15070      /* Otherwise, it's a using-declaration.  */
15071      else
15072	cp_parser_using_declaration (parser,
15073				     /*access_declaration_p=*/false);
15074    }
15075  /* If the next keyword is `__label__' we have a misplaced label
15076     declaration.  */
15077  else if (token1->keyword == RID_LABEL)
15078    {
15079      cp_lexer_consume_token (parser->lexer);
15080      error_at (token1->location, "%<__label__%> not at the beginning of a block");
15081      cp_parser_skip_to_end_of_statement (parser);
15082      /* If the next token is now a `;', consume it.  */
15083      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15084	cp_lexer_consume_token (parser->lexer);
15085    }
15086  /* If the next token is `static_assert' we have a static assertion.  */
15087  else if (token1->keyword == RID_STATIC_ASSERT)
15088    cp_parser_static_assert (parser, /*member_p=*/false);
15089  /* If the next tokens after attributes is `using namespace', then we have
15090     a using-directive.  */
15091  else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15092	   && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15093					     RID_USING)
15094	   && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15095					     RID_NAMESPACE))
15096    {
15097      if (statement_p)
15098	cp_parser_commit_to_tentative_parse (parser);
15099      cp_parser_using_directive (parser);
15100    }
15101  /* Anything else must be a simple-declaration.  */
15102  else
15103    cp_parser_simple_declaration (parser, !statement_p,
15104				  /*maybe_range_for_decl*/NULL);
15105}
15106
15107/* Parse a simple-declaration.
15108
15109   simple-declaration:
15110     decl-specifier-seq [opt] init-declarator-list [opt] ;
15111     decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15112       brace-or-equal-initializer ;
15113
15114   init-declarator-list:
15115     init-declarator
15116     init-declarator-list , init-declarator
15117
15118   If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15119   function-definition as a simple-declaration.
15120
15121   If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15122   parsed declaration if it is an uninitialized single declarator not followed
15123   by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15124   if present, will not be consumed.  */
15125
15126static void
15127cp_parser_simple_declaration (cp_parser* parser,
15128			      bool function_definition_allowed_p,
15129			      tree *maybe_range_for_decl)
15130{
15131  cp_decl_specifier_seq decl_specifiers;
15132  int declares_class_or_enum;
15133  bool saw_declarator;
15134  location_t comma_loc = UNKNOWN_LOCATION;
15135  location_t init_loc = UNKNOWN_LOCATION;
15136
15137  if (maybe_range_for_decl)
15138    *maybe_range_for_decl = NULL_TREE;
15139
15140  /* Defer access checks until we know what is being declared; the
15141     checks for names appearing in the decl-specifier-seq should be
15142     done as if we were in the scope of the thing being declared.  */
15143  push_deferring_access_checks (dk_deferred);
15144
15145  /* Parse the decl-specifier-seq.  We have to keep track of whether
15146     or not the decl-specifier-seq declares a named class or
15147     enumeration type, since that is the only case in which the
15148     init-declarator-list is allowed to be empty.
15149
15150     [dcl.dcl]
15151
15152     In a simple-declaration, the optional init-declarator-list can be
15153     omitted only when declaring a class or enumeration, that is when
15154     the decl-specifier-seq contains either a class-specifier, an
15155     elaborated-type-specifier, or an enum-specifier.  */
15156  cp_parser_decl_specifier_seq (parser,
15157				CP_PARSER_FLAGS_OPTIONAL,
15158				&decl_specifiers,
15159				&declares_class_or_enum);
15160  /* We no longer need to defer access checks.  */
15161  stop_deferring_access_checks ();
15162
15163  cp_omp_declare_simd_data odsd;
15164  if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15165    cp_parser_handle_directive_omp_attributes (parser,
15166					       &decl_specifiers.attributes,
15167					       &odsd, true);
15168
15169  /* In a block scope, a valid declaration must always have a
15170     decl-specifier-seq.  By not trying to parse declarators, we can
15171     resolve the declaration/expression ambiguity more quickly.  */
15172  if (!function_definition_allowed_p
15173      && !decl_specifiers.any_specifiers_p)
15174    {
15175      cp_parser_error (parser, "expected declaration");
15176      goto done;
15177    }
15178
15179  /* If the next two tokens are both identifiers, the code is
15180     erroneous. The usual cause of this situation is code like:
15181
15182       T t;
15183
15184     where "T" should name a type -- but does not.  */
15185  if (!decl_specifiers.any_type_specifiers_p
15186      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15187    {
15188      /* If parsing tentatively, we should commit; we really are
15189	 looking at a declaration.  */
15190      cp_parser_commit_to_tentative_parse (parser);
15191      /* Give up.  */
15192      goto done;
15193    }
15194
15195  cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15196
15197  /* Look for C++17 decomposition declaration.  */
15198  for (size_t n = 1; ; n++)
15199    if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15200	|| cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15201      continue;
15202    else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15203	     && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15204	     && decl_specifiers.any_specifiers_p)
15205      {
15206	tree decl
15207	  = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15208						 maybe_range_for_decl,
15209						 &init_loc);
15210
15211	/* The next token should be either a `,' or a `;'.  */
15212	cp_token *token = cp_lexer_peek_token (parser->lexer);
15213	/* If it's a `;', we are done.  */
15214	if (token->type == CPP_SEMICOLON)
15215	  goto finish;
15216	else if (maybe_range_for_decl)
15217	  {
15218	    if (*maybe_range_for_decl == NULL_TREE)
15219	      *maybe_range_for_decl = error_mark_node;
15220	    goto finish;
15221	  }
15222	/* Anything else is an error.  */
15223	else
15224	  {
15225	    /* If we have already issued an error message we don't need
15226	       to issue another one.  */
15227	    if ((decl != error_mark_node
15228		 && DECL_INITIAL (decl) != error_mark_node)
15229		|| cp_parser_uncommitted_to_tentative_parse_p (parser))
15230	      cp_parser_error (parser, "expected %<;%>");
15231	    /* Skip tokens until we reach the end of the statement.  */
15232	    cp_parser_skip_to_end_of_statement (parser);
15233	    /* If the next token is now a `;', consume it.  */
15234	    if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15235	      cp_lexer_consume_token (parser->lexer);
15236	    goto done;
15237	  }
15238      }
15239    else
15240      break;
15241
15242  tree last_type;
15243  bool auto_specifier_p;
15244  /* NULL_TREE if both variable and function declaration are allowed,
15245     error_mark_node if function declaration are not allowed and
15246     a FUNCTION_DECL that should be diagnosed if it is followed by
15247     variable declarations.  */
15248  tree auto_function_declaration;
15249
15250  last_type = NULL_TREE;
15251  auto_specifier_p
15252    = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15253  auto_function_declaration = NULL_TREE;
15254
15255  /* Keep going until we hit the `;' at the end of the simple
15256     declaration.  */
15257  saw_declarator = false;
15258  while (cp_lexer_next_token_is_not (parser->lexer,
15259				     CPP_SEMICOLON))
15260    {
15261      cp_token *token;
15262      bool function_definition_p;
15263      tree decl;
15264      tree auto_result = NULL_TREE;
15265
15266      if (saw_declarator)
15267	{
15268	  /* If we are processing next declarator, comma is expected */
15269	  token = cp_lexer_peek_token (parser->lexer);
15270	  gcc_assert (token->type == CPP_COMMA);
15271	  cp_lexer_consume_token (parser->lexer);
15272	  if (maybe_range_for_decl)
15273	    {
15274	      *maybe_range_for_decl = error_mark_node;
15275	      if (comma_loc == UNKNOWN_LOCATION)
15276		comma_loc = token->location;
15277	    }
15278	}
15279      else
15280	saw_declarator = true;
15281
15282      /* Parse the init-declarator.  */
15283      decl = cp_parser_init_declarator (parser,
15284					CP_PARSER_FLAGS_NONE,
15285					&decl_specifiers,
15286					/*checks=*/NULL,
15287					function_definition_allowed_p,
15288					/*member_p=*/false,
15289					declares_class_or_enum,
15290					&function_definition_p,
15291					maybe_range_for_decl,
15292					&init_loc,
15293					&auto_result);
15294      /* If an error occurred while parsing tentatively, exit quickly.
15295	 (That usually happens when in the body of a function; each
15296	 statement is treated as a declaration-statement until proven
15297	 otherwise.)  */
15298      if (cp_parser_error_occurred (parser))
15299	goto done;
15300
15301      if (auto_specifier_p && cxx_dialect >= cxx14)
15302	{
15303	  /* If the init-declarator-list contains more than one
15304	     init-declarator, they shall all form declarations of
15305	     variables.  */
15306	  if (auto_function_declaration == NULL_TREE)
15307	    auto_function_declaration
15308	      = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15309	  else if (TREE_CODE (decl) == FUNCTION_DECL
15310		   || auto_function_declaration != error_mark_node)
15311	    {
15312	      error_at (decl_specifiers.locations[ds_type_spec],
15313			"non-variable %qD in declaration with more than one "
15314			"declarator with placeholder type",
15315			TREE_CODE (decl) == FUNCTION_DECL
15316			? decl : auto_function_declaration);
15317	      auto_function_declaration = error_mark_node;
15318	    }
15319	}
15320
15321      if (auto_result
15322	  && (!processing_template_decl || !type_uses_auto (auto_result)))
15323	{
15324	  if (last_type
15325	      && last_type != error_mark_node
15326	      && !same_type_p (auto_result, last_type))
15327	    {
15328	      /* If the list of declarators contains more than one declarator,
15329		 the type of each declared variable is determined as described
15330		 above. If the type deduced for the template parameter U is not
15331		 the same in each deduction, the program is ill-formed.  */
15332	      error_at (decl_specifiers.locations[ds_type_spec],
15333			"inconsistent deduction for %qT: %qT and then %qT",
15334			decl_specifiers.type, last_type, auto_result);
15335	      last_type = error_mark_node;
15336	    }
15337	  else
15338	    last_type = auto_result;
15339	}
15340
15341      /* Handle function definitions specially.  */
15342      if (function_definition_p)
15343	{
15344	  /* If the next token is a `,', then we are probably
15345	     processing something like:
15346
15347	       void f() {}, *p;
15348
15349	     which is erroneous.  */
15350	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15351	    {
15352	      cp_token *token = cp_lexer_peek_token (parser->lexer);
15353	      error_at (token->location,
15354			"mixing"
15355			" declarations and function-definitions is forbidden");
15356	    }
15357	  /* Otherwise, we're done with the list of declarators.  */
15358	  else
15359	    {
15360	      pop_deferring_access_checks ();
15361	      cp_finalize_omp_declare_simd (parser, &odsd);
15362	      return;
15363	    }
15364	}
15365      if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15366	*maybe_range_for_decl = decl;
15367      /* The next token should be either a `,' or a `;'.  */
15368      token = cp_lexer_peek_token (parser->lexer);
15369      /* If it's a `,', there are more declarators to come.  */
15370      if (token->type == CPP_COMMA)
15371	/* will be consumed next time around */;
15372      /* If it's a `;', we are done.  */
15373      else if (token->type == CPP_SEMICOLON)
15374	break;
15375      else if (maybe_range_for_decl)
15376	{
15377	  if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15378	    permerror (decl_specifiers.locations[ds_type_spec],
15379		       "types may not be defined in a for-range-declaration");
15380	  break;
15381	}
15382      /* Anything else is an error.  */
15383      else
15384	{
15385	  /* If we have already issued an error message we don't need
15386	     to issue another one.  */
15387	  if ((decl != error_mark_node
15388	       && DECL_INITIAL (decl) != error_mark_node)
15389	      || cp_parser_uncommitted_to_tentative_parse_p (parser))
15390	    cp_parser_error (parser, "expected %<,%> or %<;%>");
15391	  /* Skip tokens until we reach the end of the statement.  */
15392	  cp_parser_skip_to_end_of_statement (parser);
15393	  /* If the next token is now a `;', consume it.  */
15394	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15395	    cp_lexer_consume_token (parser->lexer);
15396	  goto done;
15397	}
15398      /* After the first time around, a function-definition is not
15399	 allowed -- even if it was OK at first.  For example:
15400
15401	   int i, f() {}
15402
15403	 is not valid.  */
15404      function_definition_allowed_p = false;
15405    }
15406
15407  /* Issue an error message if no declarators are present, and the
15408     decl-specifier-seq does not itself declare a class or
15409     enumeration: [dcl.dcl]/3.  */
15410  if (!saw_declarator)
15411    {
15412      if (cp_parser_declares_only_class_p (parser))
15413	{
15414	  if (!declares_class_or_enum
15415	      && decl_specifiers.type
15416	      && OVERLOAD_TYPE_P (decl_specifiers.type))
15417	    /* Ensure an error is issued anyway when finish_decltype_type,
15418	       called via cp_parser_decl_specifier_seq, returns a class or
15419	       an enumeration (c++/51786).  */
15420	    decl_specifiers.type = NULL_TREE;
15421	  shadow_tag (&decl_specifiers);
15422	}
15423      /* Perform any deferred access checks.  */
15424      perform_deferred_access_checks (tf_warning_or_error);
15425    }
15426
15427  /* Consume the `;'.  */
15428 finish:
15429  if (!maybe_range_for_decl)
15430    cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15431  else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15432    {
15433      if (init_loc != UNKNOWN_LOCATION)
15434	error_at (init_loc, "initializer in range-based %<for%> loop");
15435      if (comma_loc != UNKNOWN_LOCATION)
15436	error_at (comma_loc,
15437		  "multiple declarations in range-based %<for%> loop");
15438    }
15439
15440 done:
15441  pop_deferring_access_checks ();
15442  cp_finalize_omp_declare_simd (parser, &odsd);
15443}
15444
15445/* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15446     decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15447       initializer ;  */
15448
15449static tree
15450cp_parser_decomposition_declaration (cp_parser *parser,
15451				     cp_decl_specifier_seq *decl_specifiers,
15452				     tree *maybe_range_for_decl,
15453				     location_t *init_loc)
15454{
15455  cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15456  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15457  cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15458
15459  /* Parse the identifier-list.  */
15460  auto_vec<cp_expr, 10> v;
15461  if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15462    while (true)
15463      {
15464	cp_expr e = cp_parser_identifier (parser);
15465	if (e.get_value () == error_mark_node)
15466	  break;
15467	v.safe_push (e);
15468	if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15469	  break;
15470	cp_lexer_consume_token (parser->lexer);
15471      }
15472
15473  location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15474  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15475    {
15476      end_loc = UNKNOWN_LOCATION;
15477      cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15478					       false);
15479      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15480	cp_lexer_consume_token (parser->lexer);
15481      else
15482	{
15483	  cp_parser_skip_to_end_of_statement (parser);
15484	  return error_mark_node;
15485	}
15486    }
15487
15488  if (cxx_dialect < cxx17)
15489    pedwarn (loc, OPT_Wc__17_extensions,
15490	     "structured bindings only available with "
15491	     "%<-std=c++17%> or %<-std=gnu++17%>");
15492
15493  tree pushed_scope;
15494  cp_declarator *declarator = make_declarator (cdk_decomp);
15495  loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15496  declarator->id_loc = loc;
15497  if (ref_qual != REF_QUAL_NONE)
15498    declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15499					    ref_qual == REF_QUAL_RVALUE,
15500					    NULL_TREE);
15501  tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15502			  NULL_TREE, decl_specifiers->attributes,
15503			  &pushed_scope);
15504  tree orig_decl = decl;
15505
15506  unsigned int i;
15507  cp_expr e;
15508  cp_decl_specifier_seq decl_specs;
15509  clear_decl_specs (&decl_specs);
15510  decl_specs.type = make_auto ();
15511  tree prev = decl;
15512  FOR_EACH_VEC_ELT (v, i, e)
15513    {
15514      if (i == 0)
15515	declarator = make_id_declarator (NULL_TREE, e.get_value (),
15516					 sfk_none, e.get_location ());
15517      else
15518	{
15519	  declarator->u.id.unqualified_name = e.get_value ();
15520	  declarator->id_loc = e.get_location ();
15521	}
15522      tree elt_pushed_scope;
15523      tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15524			       NULL_TREE, NULL_TREE, &elt_pushed_scope);
15525      if (decl2 == error_mark_node)
15526	decl = error_mark_node;
15527      else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15528	{
15529	  /* Ensure we've diagnosed redeclaration if we aren't creating
15530	     a new VAR_DECL.  */
15531	  gcc_assert (errorcount);
15532	  decl = error_mark_node;
15533	}
15534      else
15535	prev = decl2;
15536      if (elt_pushed_scope)
15537	pop_scope (elt_pushed_scope);
15538    }
15539
15540  if (v.is_empty ())
15541    {
15542      error_at (loc, "empty structured binding declaration");
15543      decl = error_mark_node;
15544    }
15545
15546  if (maybe_range_for_decl == NULL
15547      || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15548    {
15549      bool non_constant_p = false, is_direct_init = false;
15550      *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15551      tree initializer = cp_parser_initializer (parser, &is_direct_init,
15552						&non_constant_p);
15553      if (initializer == NULL_TREE
15554	  || (TREE_CODE (initializer) == TREE_LIST
15555	      && TREE_CHAIN (initializer))
15556	  || (is_direct_init
15557	      && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15558	      && CONSTRUCTOR_NELTS (initializer) != 1))
15559	{
15560	  error_at (loc, "invalid initializer for structured binding "
15561		    "declaration");
15562	  initializer = error_mark_node;
15563	}
15564
15565      if (decl != error_mark_node)
15566	{
15567	  cp_maybe_mangle_decomp (decl, prev, v.length ());
15568	  cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15569			  (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15570	  cp_finish_decomp (decl, prev, v.length ());
15571	}
15572    }
15573  else if (decl != error_mark_node)
15574    {
15575      *maybe_range_for_decl = prev;
15576      /* Ensure DECL_VALUE_EXPR is created for all the decls but
15577	 the underlying DECL.  */
15578      cp_finish_decomp (decl, prev, v.length ());
15579    }
15580
15581  if (pushed_scope)
15582    pop_scope (pushed_scope);
15583
15584  if (decl == error_mark_node && DECL_P (orig_decl))
15585    {
15586      if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15587	SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15588    }
15589
15590  return decl;
15591}
15592
15593/* Parse a decl-specifier-seq.
15594
15595   decl-specifier-seq:
15596     decl-specifier-seq [opt] decl-specifier
15597     decl-specifier attribute-specifier-seq [opt] (C++11)
15598
15599   decl-specifier:
15600     storage-class-specifier
15601     type-specifier
15602     function-specifier
15603     friend
15604     typedef
15605
15606   GNU Extension:
15607
15608   decl-specifier:
15609     attributes
15610
15611   Concepts Extension:
15612
15613   decl-specifier:
15614     concept
15615
15616   Set *DECL_SPECS to a representation of the decl-specifier-seq.
15617
15618   The parser flags FLAGS is used to control type-specifier parsing.
15619
15620   *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15621   flags:
15622
15623     1: one of the decl-specifiers is an elaborated-type-specifier
15624	(i.e., a type declaration)
15625     2: one of the decl-specifiers is an enum-specifier or a
15626	class-specifier (i.e., a type definition)
15627
15628   */
15629
15630static void
15631cp_parser_decl_specifier_seq (cp_parser* parser,
15632			      cp_parser_flags flags,
15633			      cp_decl_specifier_seq *decl_specs,
15634			      int* declares_class_or_enum)
15635{
15636  bool constructor_possible_p = !parser->in_declarator_p;
15637  bool found_decl_spec = false;
15638  cp_token *start_token = NULL;
15639  cp_decl_spec ds;
15640
15641  /* Clear DECL_SPECS.  */
15642  clear_decl_specs (decl_specs);
15643
15644  /* Assume no class or enumeration type is declared.  */
15645  *declares_class_or_enum = 0;
15646
15647  /* Keep reading specifiers until there are no more to read.  */
15648  while (true)
15649    {
15650      bool constructor_p;
15651      cp_token *token;
15652      ds = ds_last;
15653
15654      /* Peek at the next token.  */
15655      token = cp_lexer_peek_token (parser->lexer);
15656
15657      /* Save the first token of the decl spec list for error
15658         reporting.  */
15659      if (!start_token)
15660	start_token = token;
15661      /* Handle attributes.  */
15662      if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
15663	  && cp_next_tokens_can_be_attribute_p (parser))
15664	{
15665	  /* Parse the attributes.  */
15666	  tree attrs = cp_parser_attributes_opt (parser);
15667
15668	  /* In a sequence of declaration specifiers, c++11 attributes
15669	     appertain to the type that precede them. In that case
15670	     [dcl.spec]/1 says:
15671
15672	         The attribute-specifier-seq affects the type only for
15673		 the declaration it appears in, not other declarations
15674		 involving the same type.
15675
15676             But for now let's force the user to position the
15677             attribute either at the beginning of the declaration or
15678             after the declarator-id, which would clearly mean that it
15679             applies to the declarator.  */
15680	  if (cxx11_attribute_p (attrs))
15681	    {
15682	      if (!found_decl_spec)
15683		/* The c++11 attribute is at the beginning of the
15684		   declaration.  It appertains to the entity being
15685		   declared.  */;
15686	      else
15687		{
15688		  if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
15689		    {
15690		      /*  This is an attribute following a
15691			  class-specifier.  */
15692		      if (decl_specs->type_definition_p)
15693			warn_misplaced_attr_for_class_type (token->location,
15694							    decl_specs->type);
15695		      attrs = NULL_TREE;
15696		    }
15697		  else
15698		    {
15699		      decl_specs->std_attributes
15700			= attr_chainon (decl_specs->std_attributes, attrs);
15701		      if (decl_specs->locations[ds_std_attribute] == 0)
15702			decl_specs->locations[ds_std_attribute] = token->location;
15703		    }
15704		  continue;
15705		}
15706	    }
15707
15708	  decl_specs->attributes
15709	    = attr_chainon (decl_specs->attributes, attrs);
15710	  if (decl_specs->locations[ds_attribute] == 0)
15711	    decl_specs->locations[ds_attribute] = token->location;
15712	  continue;
15713	}
15714      /* Assume we will find a decl-specifier keyword.  */
15715      found_decl_spec = true;
15716      /* If the next token is an appropriate keyword, we can simply
15717	 add it to the list.  */
15718      switch (token->keyword)
15719	{
15720	  /* decl-specifier:
15721	       friend
15722	       constexpr
15723	       constinit */
15724	case RID_FRIEND:
15725	  if (!at_class_scope_p ())
15726	    {
15727	      gcc_rich_location richloc (token->location);
15728	      richloc.add_fixit_remove ();
15729	      error_at (&richloc, "%<friend%> used outside of class");
15730	      cp_lexer_purge_token (parser->lexer);
15731	    }
15732	  else
15733	    {
15734	      ds = ds_friend;
15735	      /* Consume the token.  */
15736	      cp_lexer_consume_token (parser->lexer);
15737	    }
15738	  break;
15739
15740        case RID_CONSTEXPR:
15741	  ds = ds_constexpr;
15742          cp_lexer_consume_token (parser->lexer);
15743          break;
15744
15745	case RID_CONSTINIT:
15746	  ds = ds_constinit;
15747	  cp_lexer_consume_token (parser->lexer);
15748	  break;
15749
15750	case RID_CONSTEVAL:
15751	  ds = ds_consteval;
15752	  cp_lexer_consume_token (parser->lexer);
15753	  break;
15754
15755        case RID_CONCEPT:
15756          ds = ds_concept;
15757          cp_lexer_consume_token (parser->lexer);
15758
15759	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15760	    break;
15761
15762          /* Warn for concept as a decl-specifier. We'll rewrite these as
15763             concept declarations later.  */
15764          if (!flag_concepts_ts)
15765            {
15766	      cp_token *next = cp_lexer_peek_token (parser->lexer);
15767	      if (next->keyword == RID_BOOL)
15768		pedwarn (next->location, 0, "the %<bool%> keyword is not "
15769			 "allowed in a C++20 concept definition");
15770	      else
15771		pedwarn (token->location, 0, "C++20 concept definition syntax "
15772			 "is %<concept <name> = <expr>%>");
15773            }
15774
15775	  /* In C++20 a concept definition is just 'concept name = expr;'
15776	     Support that syntax as a TS extension by pretending we've seen
15777	     the 'bool' specifier.  */
15778	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15779	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
15780	      && !decl_specs->any_type_specifiers_p)
15781	    {
15782	      cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
15783					    token, /*type_definition*/false);
15784	      decl_specs->any_type_specifiers_p = true;
15785	    }
15786          break;
15787
15788	  /* function-specifier:
15789	       inline
15790	       virtual
15791	       explicit  */
15792	case RID_INLINE:
15793	case RID_VIRTUAL:
15794	case RID_EXPLICIT:
15795	  cp_parser_function_specifier_opt (parser, decl_specs);
15796	  break;
15797
15798	  /* decl-specifier:
15799	       typedef  */
15800	case RID_TYPEDEF:
15801	  ds = ds_typedef;
15802	  /* Consume the token.  */
15803	  cp_lexer_consume_token (parser->lexer);
15804
15805	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15806	    break;
15807
15808	  /* A constructor declarator cannot appear in a typedef.  */
15809	  constructor_possible_p = false;
15810	  /* The "typedef" keyword can only occur in a declaration; we
15811	     may as well commit at this point.  */
15812	  cp_parser_commit_to_tentative_parse (parser);
15813
15814          if (decl_specs->storage_class != sc_none)
15815            decl_specs->conflicting_specifiers_p = true;
15816	  break;
15817
15818	  /* storage-class-specifier:
15819	       auto
15820	       register
15821	       static
15822	       extern
15823	       mutable
15824
15825	     GNU Extension:
15826	       thread  */
15827	case RID_AUTO:
15828          if (cxx_dialect == cxx98)
15829            {
15830	      /* Consume the token.  */
15831	      cp_lexer_consume_token (parser->lexer);
15832
15833	      /* Complain about `auto' as a storage specifier, if
15834		 we're complaining about C++0x compatibility.  */
15835	      gcc_rich_location richloc (token->location);
15836	      richloc.add_fixit_remove ();
15837	      warning_at (&richloc, OPT_Wc__11_compat,
15838			  "%<auto%> changes meaning in C++11; "
15839			  "please remove it");
15840
15841              /* Set the storage class anyway.  */
15842              cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
15843					   token);
15844            }
15845          else
15846	    /* C++0x auto type-specifier.  */
15847	    found_decl_spec = false;
15848          break;
15849
15850	case RID_REGISTER:
15851	case RID_STATIC:
15852	case RID_EXTERN:
15853	case RID_MUTABLE:
15854	  /* Consume the token.  */
15855	  cp_lexer_consume_token (parser->lexer);
15856          cp_parser_set_storage_class (parser, decl_specs, token->keyword,
15857				       token);
15858	  break;
15859	case RID_THREAD:
15860	  /* Consume the token.  */
15861	  ds = ds_thread;
15862	  cp_lexer_consume_token (parser->lexer);
15863	  break;
15864
15865	default:
15866	  /* We did not yet find a decl-specifier yet.  */
15867	  found_decl_spec = false;
15868	  break;
15869	}
15870
15871      if (found_decl_spec
15872	  && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
15873	  && token->keyword != RID_CONSTEXPR)
15874	error ("%qD invalid in condition", ridpointers[token->keyword]);
15875
15876      if (found_decl_spec
15877	  && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15878	  && token->keyword != RID_MUTABLE
15879	  && token->keyword != RID_CONSTEXPR
15880	  && token->keyword != RID_CONSTEVAL)
15881	error_at (token->location, "%qD invalid in lambda",
15882		  ridpointers[token->keyword]);
15883
15884      if (ds != ds_last)
15885	set_and_check_decl_spec_loc (decl_specs, ds, token);
15886
15887      /* Constructors are a special case.  The `S' in `S()' is not a
15888	 decl-specifier; it is the beginning of the declarator.  */
15889      constructor_p
15890	= (!found_decl_spec
15891	   && constructor_possible_p
15892	   && (cp_parser_constructor_declarator_p
15893	       (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
15894							 ds_friend))));
15895
15896      /* If we don't have a DECL_SPEC yet, then we must be looking at
15897	 a type-specifier.  */
15898      if (!found_decl_spec && !constructor_p)
15899	{
15900	  int decl_spec_declares_class_or_enum;
15901	  bool is_cv_qualifier;
15902	  tree type_spec;
15903
15904	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15905	    flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15906
15907	  type_spec
15908	    = cp_parser_type_specifier (parser, flags,
15909					decl_specs,
15910					/*is_declaration=*/true,
15911					&decl_spec_declares_class_or_enum,
15912					&is_cv_qualifier);
15913	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
15914
15915	  /* If this type-specifier referenced a user-defined type
15916	     (a typedef, class-name, etc.), then we can't allow any
15917	     more such type-specifiers henceforth.
15918
15919	     [dcl.spec]
15920
15921	     The longest sequence of decl-specifiers that could
15922	     possibly be a type name is taken as the
15923	     decl-specifier-seq of a declaration.  The sequence shall
15924	     be self-consistent as described below.
15925
15926	     [dcl.type]
15927
15928	     As a general rule, at most one type-specifier is allowed
15929	     in the complete decl-specifier-seq of a declaration.  The
15930	     only exceptions are the following:
15931
15932	     -- const or volatile can be combined with any other
15933		type-specifier.
15934
15935	     -- signed or unsigned can be combined with char, long,
15936		short, or int.
15937
15938	     -- ..
15939
15940	     Example:
15941
15942	       typedef char* Pc;
15943	       void g (const int Pc);
15944
15945	     Here, Pc is *not* part of the decl-specifier seq; it's
15946	     the declarator.  Therefore, once we see a type-specifier
15947	     (other than a cv-qualifier), we forbid any additional
15948	     user-defined types.  We *do* still allow things like `int
15949	     int' to be considered a decl-specifier-seq, and issue the
15950	     error message later.  */
15951	  if (type_spec && !is_cv_qualifier)
15952	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15953	  /* A constructor declarator cannot follow a type-specifier.  */
15954	  if (type_spec)
15955	    {
15956	      constructor_possible_p = false;
15957	      found_decl_spec = true;
15958	      if (!is_cv_qualifier)
15959		decl_specs->any_type_specifiers_p = true;
15960
15961	      if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
15962		error_at (token->location, "type-specifier invalid in lambda");
15963	    }
15964	}
15965
15966      /* If we still do not have a DECL_SPEC, then there are no more
15967	 decl-specifiers.  */
15968      if (!found_decl_spec)
15969	break;
15970
15971      if (decl_specs->std_attributes)
15972	{
15973	  error_at (decl_specs->locations[ds_std_attribute],
15974		    "standard attributes in middle of decl-specifiers");
15975	  inform (decl_specs->locations[ds_std_attribute],
15976		  "standard attributes must precede the decl-specifiers to "
15977		  "apply to the declaration, or follow them to apply to "
15978		  "the type");
15979	}
15980
15981      decl_specs->any_specifiers_p = true;
15982      /* After we see one decl-specifier, further decl-specifiers are
15983	 always optional.  */
15984      flags |= CP_PARSER_FLAGS_OPTIONAL;
15985    }
15986
15987  /* Don't allow a friend specifier with a class definition.  */
15988  if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
15989      && (*declares_class_or_enum & 2))
15990    error_at (decl_specs->locations[ds_friend],
15991	      "class definition may not be declared a friend");
15992}
15993
15994/* Parse an (optional) storage-class-specifier.
15995
15996   storage-class-specifier:
15997     auto
15998     register
15999     static
16000     extern
16001     mutable
16002
16003   GNU Extension:
16004
16005   storage-class-specifier:
16006     thread
16007
16008   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
16009
16010static tree
16011cp_parser_storage_class_specifier_opt (cp_parser* parser)
16012{
16013  switch (cp_lexer_peek_token (parser->lexer)->keyword)
16014    {
16015    case RID_AUTO:
16016      if (cxx_dialect != cxx98)
16017        return NULL_TREE;
16018      /* Fall through for C++98.  */
16019      gcc_fallthrough ();
16020
16021    case RID_REGISTER:
16022    case RID_STATIC:
16023    case RID_EXTERN:
16024    case RID_MUTABLE:
16025    case RID_THREAD:
16026      /* Consume the token.  */
16027      return cp_lexer_consume_token (parser->lexer)->u.value;
16028
16029    default:
16030      return NULL_TREE;
16031    }
16032}
16033
16034/* Parse an (optional) function-specifier.
16035
16036   function-specifier:
16037     inline
16038     virtual
16039     explicit
16040
16041   C++20 Extension:
16042     explicit(constant-expression)
16043
16044   Returns an IDENTIFIER_NODE corresponding to the keyword used.
16045   Updates DECL_SPECS, if it is non-NULL.  */
16046
16047static tree
16048cp_parser_function_specifier_opt (cp_parser* parser,
16049				  cp_decl_specifier_seq *decl_specs)
16050{
16051  cp_token *token = cp_lexer_peek_token (parser->lexer);
16052  switch (token->keyword)
16053    {
16054    case RID_INLINE:
16055      set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16056      break;
16057
16058    case RID_VIRTUAL:
16059      /* 14.5.2.3 [temp.mem]
16060
16061	 A member function template shall not be virtual.  */
16062      if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16063	  && current_class_type)
16064	error_at (token->location, "templates may not be %<virtual%>");
16065      else
16066	set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16067      break;
16068
16069    case RID_EXPLICIT:
16070      {
16071	tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16072	/* If we see '(', it's C++20 explicit(bool).  */
16073	tree expr;
16074	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16075	  {
16076	    matching_parens parens;
16077	    parens.consume_open (parser);
16078
16079	    /* New types are not allowed in an explicit-specifier.  */
16080	    const char *saved_message
16081	      = parser->type_definition_forbidden_message;
16082	    parser->type_definition_forbidden_message
16083	      = G_("types may not be defined in explicit-specifier");
16084
16085	    if (cxx_dialect < cxx20)
16086	      pedwarn (token->location, OPT_Wc__20_extensions,
16087		       "%<explicit(bool)%> only available with %<-std=c++20%> "
16088		       "or %<-std=gnu++20%>");
16089
16090	    /* Parse the constant-expression.  */
16091	    expr = cp_parser_constant_expression (parser);
16092
16093	    /* Restore the saved message.  */
16094	    parser->type_definition_forbidden_message = saved_message;
16095	    parens.require_close (parser);
16096	  }
16097	else
16098	  /* The explicit-specifier explicit without a constant-expression is
16099	     equivalent to the explicit-specifier explicit(true).  */
16100	  expr = boolean_true_node;
16101
16102	/* [dcl.fct.spec]
16103	   "the constant-expression, if supplied, shall be a contextually
16104	   converted constant expression of type bool."  */
16105	expr = build_explicit_specifier (expr, tf_warning_or_error);
16106	/* We could evaluate it -- mark the decl as appropriate.  */
16107	if (expr == boolean_true_node)
16108	  set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16109	else if (expr == boolean_false_node)
16110	  /* Don't mark the decl as explicit.  */;
16111	else if (decl_specs)
16112	  /* The expression was value-dependent.  Remember it so that we can
16113	     substitute it later.  */
16114	  decl_specs->explicit_specifier = expr;
16115	return id;
16116      }
16117
16118    default:
16119      return NULL_TREE;
16120    }
16121
16122  /* Consume the token.  */
16123  return cp_lexer_consume_token (parser->lexer)->u.value;
16124}
16125
16126/* Parse a linkage-specification.
16127
16128   linkage-specification:
16129     extern string-literal { declaration-seq [opt] }
16130     extern string-literal declaration  */
16131
16132static void
16133cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16134{
16135  tree linkage;
16136
16137  /* Look for the `extern' keyword.  */
16138  cp_token *extern_token
16139    = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16140
16141  /* Look for the string-literal.  */
16142  cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16143  linkage = cp_parser_string_literal (parser, false, false);
16144
16145  /* Transform the literal into an identifier.  If the literal is a
16146     wide-character string, or contains embedded NULs, then we can't
16147     handle it as the user wants.  */
16148  if (linkage == error_mark_node
16149      || strlen (TREE_STRING_POINTER (linkage))
16150	 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16151    {
16152      cp_parser_error (parser, "invalid linkage-specification");
16153      /* Assume C++ linkage.  */
16154      linkage = lang_name_cplusplus;
16155    }
16156  else
16157    linkage = get_identifier (TREE_STRING_POINTER (linkage));
16158
16159  /* We're now using the new linkage.  */
16160  push_lang_context (linkage);
16161
16162  /* Preserve the location of the innermost linkage specification,
16163     tracking the locations of nested specifications via a local.  */
16164  location_t saved_location
16165    = parser->innermost_linkage_specification_location;
16166  /* Construct a location ranging from the start of the "extern" to
16167     the end of the string-literal, with the caret at the start, e.g.:
16168       extern "C" {
16169       ^~~~~~~~~~
16170  */
16171  parser->innermost_linkage_specification_location
16172    = make_location (extern_token->location,
16173		     extern_token->location,
16174		     get_finish (string_token->location));
16175
16176  /* If the next token is a `{', then we're using the first
16177     production.  */
16178  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16179    {
16180      cp_ensure_no_omp_declare_simd (parser);
16181      cp_ensure_no_oacc_routine (parser);
16182
16183      /* Consume the `{' token.  */
16184      matching_braces braces;
16185      braces.consume_open (parser);
16186      /* Parse the declarations.  */
16187      cp_parser_declaration_seq_opt (parser);
16188      /* Look for the closing `}'.  */
16189      braces.require_close (parser);
16190    }
16191  /* Otherwise, there's just one declaration.  */
16192  else
16193    {
16194      bool saved_in_unbraced_linkage_specification_p;
16195
16196      saved_in_unbraced_linkage_specification_p
16197	= parser->in_unbraced_linkage_specification_p;
16198      parser->in_unbraced_linkage_specification_p = true;
16199      cp_parser_declaration (parser, prefix_attr);
16200      parser->in_unbraced_linkage_specification_p
16201	= saved_in_unbraced_linkage_specification_p;
16202    }
16203
16204  /* We're done with the linkage-specification.  */
16205  pop_lang_context ();
16206
16207  /* Restore location of parent linkage specification, if any.  */
16208  parser->innermost_linkage_specification_location = saved_location;
16209}
16210
16211/* Parse a static_assert-declaration.
16212
16213   static_assert-declaration:
16214     static_assert ( constant-expression , string-literal ) ;
16215     static_assert ( constant-expression ) ; (C++17)
16216
16217   If MEMBER_P, this static_assert is a class member.  */
16218
16219static void
16220cp_parser_static_assert(cp_parser *parser, bool member_p)
16221{
16222  cp_expr condition;
16223  location_t token_loc;
16224  tree message;
16225  bool dummy;
16226
16227  /* Peek at the `static_assert' token so we can keep track of exactly
16228     where the static assertion started.  */
16229  token_loc = cp_lexer_peek_token (parser->lexer)->location;
16230
16231  /* Look for the `static_assert' keyword.  */
16232  if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16233                                  RT_STATIC_ASSERT))
16234    return;
16235
16236  /*  We know we are in a static assertion; commit to any tentative
16237      parse.  */
16238  if (cp_parser_parsing_tentatively (parser))
16239    cp_parser_commit_to_tentative_parse (parser);
16240
16241  /* Parse the `(' starting the static assertion condition.  */
16242  matching_parens parens;
16243  parens.require_open (parser);
16244
16245  /* Parse the constant-expression.  Allow a non-constant expression
16246     here in order to give better diagnostics in finish_static_assert.  */
16247  condition =
16248    cp_parser_constant_expression (parser,
16249                                   /*allow_non_constant_p=*/true,
16250                                   /*non_constant_p=*/&dummy);
16251
16252  if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16253    {
16254      if (pedantic && cxx_dialect < cxx17)
16255	pedwarn (input_location, OPT_Wc__17_extensions,
16256		 "%<static_assert%> without a message "
16257		 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16258      /* Eat the ')'  */
16259      cp_lexer_consume_token (parser->lexer);
16260      message = build_string (1, "");
16261      TREE_TYPE (message) = char_array_type_node;
16262      fix_string_type (message);
16263    }
16264  else
16265    {
16266      /* Parse the separating `,'.  */
16267      cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16268
16269      /* Parse the string-literal message.  */
16270      message = cp_parser_string_literal (parser,
16271                                	  /*translate=*/false,
16272                                	  /*wide_ok=*/true);
16273
16274      /* A `)' completes the static assertion.  */
16275      if (!parens.require_close (parser))
16276	cp_parser_skip_to_closing_parenthesis (parser,
16277                                               /*recovering=*/true,
16278                                               /*or_comma=*/false,
16279					       /*consume_paren=*/true);
16280    }
16281
16282  /* A semicolon terminates the declaration.  */
16283  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16284
16285  /* Get the location for the static assertion.  Use that of the
16286     condition if available, otherwise, use that of the "static_assert"
16287     token.  */
16288  location_t assert_loc = condition.get_location ();
16289  if (assert_loc == UNKNOWN_LOCATION)
16290    assert_loc = token_loc;
16291
16292  /* Complete the static assertion, which may mean either processing
16293     the static assert now or saving it for template instantiation.  */
16294  finish_static_assert (condition, message, assert_loc, member_p,
16295			/*show_expr_p=*/false);
16296}
16297
16298/* Parse the expression in decltype ( expression ).  */
16299
16300static tree
16301cp_parser_decltype_expr (cp_parser *parser,
16302			 bool &id_expression_or_member_access_p)
16303{
16304  cp_token *id_expr_start_token;
16305  tree expr;
16306
16307  /* First, try parsing an id-expression.  */
16308  id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16309  cp_parser_parse_tentatively (parser);
16310  expr = cp_parser_id_expression (parser,
16311                                  /*template_keyword_p=*/false,
16312                                  /*check_dependency_p=*/true,
16313                                  /*template_p=*/NULL,
16314                                  /*declarator_p=*/false,
16315                                  /*optional_p=*/false);
16316
16317  if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16318    {
16319      bool non_integral_constant_expression_p = false;
16320      tree id_expression = expr;
16321      cp_id_kind idk;
16322      const char *error_msg;
16323
16324      if (identifier_p (expr))
16325	/* Lookup the name we got back from the id-expression.  */
16326	expr = cp_parser_lookup_name_simple (parser, expr,
16327					     id_expr_start_token->location);
16328
16329      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
16330	/* A template without args is not a complete id-expression.  */
16331	expr = error_mark_node;
16332
16333      if (expr
16334          && expr != error_mark_node
16335          && TREE_CODE (expr) != TYPE_DECL
16336	  && (TREE_CODE (expr) != BIT_NOT_EXPR
16337	      || !TYPE_P (TREE_OPERAND (expr, 0)))
16338          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16339        {
16340          /* Complete lookup of the id-expression.  */
16341          expr = (finish_id_expression
16342                  (id_expression, expr, parser->scope, &idk,
16343                   /*integral_constant_expression_p=*/false,
16344                   /*allow_non_integral_constant_expression_p=*/true,
16345                   &non_integral_constant_expression_p,
16346                   /*template_p=*/false,
16347                   /*done=*/true,
16348                   /*address_p=*/false,
16349                   /*template_arg_p=*/false,
16350                   &error_msg,
16351		   id_expr_start_token->location));
16352
16353          if (expr == error_mark_node)
16354            /* We found an id-expression, but it was something that we
16355               should not have found. This is an error, not something
16356               we can recover from, so note that we found an
16357               id-expression and we'll recover as gracefully as
16358               possible.  */
16359            id_expression_or_member_access_p = true;
16360        }
16361
16362      if (expr
16363          && expr != error_mark_node
16364          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16365        /* We have an id-expression.  */
16366        id_expression_or_member_access_p = true;
16367    }
16368
16369  if (!id_expression_or_member_access_p)
16370    {
16371      /* Abort the id-expression parse.  */
16372      cp_parser_abort_tentative_parse (parser);
16373
16374      /* Parsing tentatively, again.  */
16375      cp_parser_parse_tentatively (parser);
16376
16377      /* Parse a class member access.  */
16378      expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16379                                           /*cast_p=*/false, /*decltype*/true,
16380                                           /*member_access_only_p=*/true, NULL);
16381
16382      if (expr
16383          && expr != error_mark_node
16384          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16385        /* We have an id-expression.  */
16386        id_expression_or_member_access_p = true;
16387    }
16388
16389  if (id_expression_or_member_access_p)
16390    /* We have parsed the complete id-expression or member access.  */
16391    cp_parser_parse_definitely (parser);
16392  else
16393    {
16394      /* Abort our attempt to parse an id-expression or member access
16395         expression.  */
16396      cp_parser_abort_tentative_parse (parser);
16397
16398      /* Parse a full expression.  */
16399      expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16400				   /*decltype_p=*/true);
16401    }
16402
16403  return expr;
16404}
16405
16406/* Parse a `decltype' type.  Returns the type.
16407
16408   decltype-specifier:
16409     decltype ( expression )
16410   C++14:
16411     decltype ( auto )  */
16412
16413static tree
16414cp_parser_decltype (cp_parser *parser)
16415{
16416  bool id_expression_or_member_access_p = false;
16417  cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16418
16419  if (start_token->type == CPP_DECLTYPE)
16420    {
16421      /* Already parsed.  */
16422      cp_lexer_consume_token (parser->lexer);
16423      return saved_checks_value (start_token->u.tree_check_value);
16424    }
16425
16426  /* Look for the `decltype' token.  */
16427  if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16428    return error_mark_node;
16429
16430  /* Parse the opening `('.  */
16431  matching_parens parens;
16432  if (!parens.require_open (parser))
16433    return error_mark_node;
16434
16435  /* Since we're going to preserve any side-effects from this parse, set up a
16436     firewall to protect our callers from cp_parser_commit_to_tentative_parse
16437     in the expression.  */
16438  tentative_firewall firewall (parser);
16439
16440  /* If in_declarator_p, a reparse as an expression might succeed (60361).
16441     Otherwise, commit now for better diagnostics.  */
16442  if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16443      && !parser->in_declarator_p)
16444    cp_parser_commit_to_topmost_tentative_parse (parser);
16445
16446  push_deferring_access_checks (dk_deferred);
16447
16448  tree expr = NULL_TREE;
16449
16450  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16451      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16452    {
16453      /* decltype (auto) */
16454      cp_lexer_consume_token (parser->lexer);
16455      if (cxx_dialect < cxx14)
16456	{
16457	  error_at (start_token->location,
16458		    "%<decltype(auto)%> type specifier only available with "
16459		    "%<-std=c++14%> or %<-std=gnu++14%>");
16460	  expr = error_mark_node;
16461	}
16462    }
16463  else
16464    {
16465      /* decltype (expression)  */
16466
16467      /* Types cannot be defined in a `decltype' expression.  Save away the
16468	 old message and set the new one.  */
16469      const char *saved_message = parser->type_definition_forbidden_message;
16470      parser->type_definition_forbidden_message
16471	= G_("types may not be defined in %<decltype%> expressions");
16472
16473      /* The restrictions on constant-expressions do not apply inside
16474	 decltype expressions.  */
16475      bool saved_integral_constant_expression_p
16476	= parser->integral_constant_expression_p;
16477      bool saved_non_integral_constant_expression_p
16478	= parser->non_integral_constant_expression_p;
16479      parser->integral_constant_expression_p = false;
16480
16481      /* Within a parenthesized expression, a `>' token is always
16482	 the greater-than operator.  */
16483      bool saved_greater_than_is_operator_p
16484	= parser->greater_than_is_operator_p;
16485      parser->greater_than_is_operator_p = true;
16486
16487      /* Don't synthesize an implicit template type parameter here.  This
16488	 could happen with C++23 code like
16489
16490	   void f(decltype(new auto{0}));
16491
16492	 where we want to deduce the auto right away so that the parameter
16493	 is of type 'int *'.  */
16494      auto cleanup = make_temp_override
16495	(parser->auto_is_implicit_function_template_parm_p, false);
16496
16497      /* Do not actually evaluate the expression.  */
16498      ++cp_unevaluated_operand;
16499
16500      /* Do not warn about problems with the expression.  */
16501      ++c_inhibit_evaluation_warnings;
16502
16503      expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16504      STRIP_ANY_LOCATION_WRAPPER (expr);
16505
16506      /* Go back to evaluating expressions.  */
16507      --cp_unevaluated_operand;
16508      --c_inhibit_evaluation_warnings;
16509
16510      /* The `>' token might be the end of a template-id or
16511	 template-parameter-list now.  */
16512      parser->greater_than_is_operator_p
16513	= saved_greater_than_is_operator_p;
16514
16515      /* Restore the old message and the integral constant expression
16516	 flags.  */
16517      parser->type_definition_forbidden_message = saved_message;
16518      parser->integral_constant_expression_p
16519	= saved_integral_constant_expression_p;
16520      parser->non_integral_constant_expression_p
16521	= saved_non_integral_constant_expression_p;
16522    }
16523
16524  /* Parse to the closing `)'.  */
16525  if (expr == error_mark_node || !parens.require_close (parser))
16526    {
16527      cp_parser_skip_to_closing_parenthesis (parser, true, false,
16528					     /*consume_paren=*/true);
16529      expr = error_mark_node;
16530    }
16531
16532  /* If we got a parse error while tentative, bail out now.  */
16533  if (cp_parser_error_occurred (parser))
16534    {
16535      pop_deferring_access_checks ();
16536      return error_mark_node;
16537    }
16538
16539  if (!expr)
16540    /* Build auto.  */
16541    expr = make_decltype_auto ();
16542  else
16543    expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16544				 tf_warning_or_error);
16545
16546  /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16547     it again.  */
16548  start_token->type = CPP_DECLTYPE;
16549  start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16550  start_token->tree_check_p = true;
16551  start_token->u.tree_check_value->value = expr;
16552  start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16553  start_token->keyword = RID_MAX;
16554
16555  location_t loc = start_token->location;
16556  loc = make_location (loc, loc, parser->lexer);
16557  start_token->location = loc;
16558
16559  cp_lexer_purge_tokens_after (parser->lexer, start_token);
16560
16561  pop_to_parent_deferring_access_checks ();
16562
16563  return expr;
16564}
16565
16566/* Special member functions [gram.special] */
16567
16568/* Parse a conversion-function-id.
16569
16570   conversion-function-id:
16571     operator conversion-type-id
16572
16573   Returns an IDENTIFIER_NODE representing the operator.  */
16574
16575static tree
16576cp_parser_conversion_function_id (cp_parser* parser)
16577{
16578  tree type;
16579  tree saved_scope;
16580  tree saved_qualifying_scope;
16581  tree saved_object_scope;
16582  tree pushed_scope = NULL_TREE;
16583
16584  /* Look for the `operator' token.  */
16585  if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16586    return error_mark_node;
16587  /* When we parse the conversion-type-id, the current scope will be
16588     reset.  However, we need that information in able to look up the
16589     conversion function later, so we save it here.  */
16590  saved_scope = parser->scope;
16591  saved_qualifying_scope = parser->qualifying_scope;
16592  saved_object_scope = parser->object_scope;
16593  /* We must enter the scope of the class so that the names of
16594     entities declared within the class are available in the
16595     conversion-type-id.  For example, consider:
16596
16597       struct S {
16598	 typedef int I;
16599	 operator I();
16600       };
16601
16602       S::operator I() { ... }
16603
16604     In order to see that `I' is a type-name in the definition, we
16605     must be in the scope of `S'.  */
16606  if (saved_scope)
16607    pushed_scope = push_scope (saved_scope);
16608  /* Parse the conversion-type-id.  */
16609  type = cp_parser_conversion_type_id (parser);
16610  /* Leave the scope of the class, if any.  */
16611  if (pushed_scope)
16612    pop_scope (pushed_scope);
16613  /* Restore the saved scope.  */
16614  parser->scope = saved_scope;
16615  parser->qualifying_scope = saved_qualifying_scope;
16616  parser->object_scope = saved_object_scope;
16617  /* If the TYPE is invalid, indicate failure.  */
16618  if (type == error_mark_node)
16619    return error_mark_node;
16620  return make_conv_op_name (type);
16621}
16622
16623/* Parse a conversion-type-id:
16624
16625   conversion-type-id:
16626     type-specifier-seq conversion-declarator [opt]
16627
16628   Returns the TYPE specified.  */
16629
16630static tree
16631cp_parser_conversion_type_id (cp_parser* parser)
16632{
16633  tree attributes;
16634  cp_decl_specifier_seq type_specifiers;
16635  cp_declarator *declarator;
16636  tree type_specified;
16637  const char *saved_message;
16638
16639  /* Parse the attributes.  */
16640  attributes = cp_parser_attributes_opt (parser);
16641
16642  saved_message = parser->type_definition_forbidden_message;
16643  parser->type_definition_forbidden_message
16644    = G_("types may not be defined in a conversion-type-id");
16645
16646  /* Parse the type-specifiers.  DR 2413 clarifies that `typename' is
16647     optional in conversion-type-id.  */
16648  cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16649				/*is_declaration=*/false,
16650				/*is_trailing_return=*/false,
16651				&type_specifiers);
16652
16653  parser->type_definition_forbidden_message = saved_message;
16654
16655  /* If that didn't work, stop.  */
16656  if (type_specifiers.type == error_mark_node)
16657    return error_mark_node;
16658  /* Parse the conversion-declarator.  */
16659  declarator = cp_parser_conversion_declarator_opt (parser);
16660
16661  type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
16662				    /*initialized=*/0, &attributes);
16663  if (attributes)
16664    cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
16665
16666  /* Don't give this error when parsing tentatively.  This happens to
16667     work because we always parse this definitively once.  */
16668  if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
16669      && type_uses_auto (type_specified))
16670    {
16671      if (cxx_dialect < cxx14)
16672	{
16673	  error ("invalid use of %<auto%> in conversion operator");
16674	  return error_mark_node;
16675	}
16676      else if (template_parm_scope_p ())
16677	warning (0, "use of %<auto%> in member template "
16678		 "conversion operator can never be deduced");
16679    }
16680
16681  return type_specified;
16682}
16683
16684/* Parse an (optional) conversion-declarator.
16685
16686   conversion-declarator:
16687     ptr-operator conversion-declarator [opt]
16688
16689   */
16690
16691static cp_declarator *
16692cp_parser_conversion_declarator_opt (cp_parser* parser)
16693{
16694  enum tree_code code;
16695  tree class_type, std_attributes = NULL_TREE;
16696  cp_cv_quals cv_quals;
16697
16698  /* We don't know if there's a ptr-operator next, or not.  */
16699  cp_parser_parse_tentatively (parser);
16700  /* Try the ptr-operator.  */
16701  code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
16702				 &std_attributes);
16703  /* If it worked, look for more conversion-declarators.  */
16704  if (cp_parser_parse_definitely (parser))
16705    {
16706      cp_declarator *declarator;
16707
16708      /* Parse another optional declarator.  */
16709      declarator = cp_parser_conversion_declarator_opt (parser);
16710
16711      declarator = cp_parser_make_indirect_declarator
16712	(code, class_type, cv_quals, declarator, std_attributes);
16713
16714      return declarator;
16715   }
16716
16717  return NULL;
16718}
16719
16720/* Parse an (optional) ctor-initializer.
16721
16722   ctor-initializer:
16723     : mem-initializer-list  */
16724
16725static void
16726cp_parser_ctor_initializer_opt (cp_parser* parser)
16727{
16728  /* If the next token is not a `:', then there is no
16729     ctor-initializer.  */
16730  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16731    {
16732      /* Do default initialization of any bases and members.  */
16733      if (DECL_CONSTRUCTOR_P (current_function_decl))
16734	finish_mem_initializers (NULL_TREE);
16735      return;
16736    }
16737
16738  /* Consume the `:' token.  */
16739  cp_lexer_consume_token (parser->lexer);
16740  /* And the mem-initializer-list.  */
16741  cp_parser_mem_initializer_list (parser);
16742}
16743
16744/* Parse a mem-initializer-list.
16745
16746   mem-initializer-list:
16747     mem-initializer ... [opt]
16748     mem-initializer ... [opt] , mem-initializer-list  */
16749
16750static void
16751cp_parser_mem_initializer_list (cp_parser* parser)
16752{
16753  tree mem_initializer_list = NULL_TREE;
16754  tree target_ctor = error_mark_node;
16755  cp_token *token = cp_lexer_peek_token (parser->lexer);
16756
16757  /* Let the semantic analysis code know that we are starting the
16758     mem-initializer-list.  */
16759  if (!DECL_CONSTRUCTOR_P (current_function_decl))
16760    error_at (token->location,
16761	      "only constructors take member initializers");
16762
16763  /* Loop through the list.  */
16764  while (true)
16765    {
16766      tree mem_initializer;
16767
16768      token = cp_lexer_peek_token (parser->lexer);
16769      /* Parse the mem-initializer.  */
16770      mem_initializer = cp_parser_mem_initializer (parser);
16771      /* If the next token is a `...', we're expanding member initializers. */
16772      bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16773      if (ellipsis
16774	  || (mem_initializer != error_mark_node
16775	      && check_for_bare_parameter_packs (TREE_PURPOSE
16776						 (mem_initializer))))
16777        {
16778          /* Consume the `...'. */
16779	  if (ellipsis)
16780	    cp_lexer_consume_token (parser->lexer);
16781
16782          /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16783             can be expanded but members cannot. */
16784          if (mem_initializer != error_mark_node
16785              && !TYPE_P (TREE_PURPOSE (mem_initializer)))
16786            {
16787              error_at (token->location,
16788			"cannot expand initializer for member %qD",
16789			TREE_PURPOSE (mem_initializer));
16790              mem_initializer = error_mark_node;
16791            }
16792
16793          /* Construct the pack expansion type. */
16794          if (mem_initializer != error_mark_node)
16795            mem_initializer = make_pack_expansion (mem_initializer);
16796        }
16797      if (target_ctor != error_mark_node
16798	  && mem_initializer != error_mark_node)
16799	{
16800	  error ("mem-initializer for %qD follows constructor delegation",
16801		 TREE_PURPOSE (mem_initializer));
16802	  mem_initializer = error_mark_node;
16803	}
16804      /* Look for a target constructor. */
16805      if (mem_initializer != error_mark_node
16806	  && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
16807	  && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
16808	{
16809	  maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
16810	  if (mem_initializer_list)
16811	    {
16812	      error ("constructor delegation follows mem-initializer for %qD",
16813		     TREE_PURPOSE (mem_initializer_list));
16814	      mem_initializer = error_mark_node;
16815	    }
16816	  target_ctor = mem_initializer;
16817	}
16818      /* Add it to the list, unless it was erroneous.  */
16819      if (mem_initializer != error_mark_node)
16820	{
16821	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
16822	  mem_initializer_list = mem_initializer;
16823	}
16824      /* If the next token is not a `,', we're done.  */
16825      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16826	break;
16827      /* Consume the `,' token.  */
16828      cp_lexer_consume_token (parser->lexer);
16829    }
16830
16831  /* Perform semantic analysis.  */
16832  if (DECL_CONSTRUCTOR_P (current_function_decl))
16833    finish_mem_initializers (mem_initializer_list);
16834}
16835
16836/* Parse a mem-initializer.
16837
16838   mem-initializer:
16839     mem-initializer-id ( expression-list [opt] )
16840     mem-initializer-id braced-init-list
16841
16842   GNU extension:
16843
16844   mem-initializer:
16845     ( expression-list [opt] )
16846
16847   Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
16848   class) or FIELD_DECL (for a non-static data member) to initialize;
16849   the TREE_VALUE is the expression-list.  An empty initialization
16850   list is represented by void_list_node.  */
16851
16852static tree
16853cp_parser_mem_initializer (cp_parser* parser)
16854{
16855  tree mem_initializer_id;
16856  tree expression_list;
16857  tree member;
16858  cp_token *token = cp_lexer_peek_token (parser->lexer);
16859
16860  /* Find out what is being initialized.  */
16861  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16862    {
16863      permerror (token->location,
16864		 "anachronistic old-style base class initializer");
16865      mem_initializer_id = NULL_TREE;
16866    }
16867  else
16868    {
16869      mem_initializer_id = cp_parser_mem_initializer_id (parser);
16870      if (mem_initializer_id == error_mark_node)
16871	return mem_initializer_id;
16872    }
16873  member = expand_member_init (mem_initializer_id);
16874  if (member && !DECL_P (member))
16875    in_base_initializer = 1;
16876
16877  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16878    {
16879      bool expr_non_constant_p;
16880      cp_lexer_set_source_position (parser->lexer);
16881      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16882      expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
16883      CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
16884      expression_list = build_tree_list (NULL_TREE, expression_list);
16885    }
16886  else
16887    {
16888      vec<tree, va_gc> *vec;
16889      vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16890						     /*cast_p=*/false,
16891						     /*allow_expansion_p=*/true,
16892						     /*non_constant_p=*/NULL,
16893						     /*close_paren_loc=*/NULL,
16894						     /*wrap_locations_p=*/true);
16895      if (vec == NULL)
16896	return error_mark_node;
16897      expression_list = build_tree_list_vec (vec);
16898      release_tree_vector (vec);
16899    }
16900
16901  if (expression_list == error_mark_node)
16902    return error_mark_node;
16903  if (!expression_list)
16904    expression_list = void_type_node;
16905
16906  in_base_initializer = 0;
16907
16908  if (!member)
16909    return error_mark_node;
16910  tree node = build_tree_list (member, expression_list);
16911
16912  /* We can't attach the source location of this initializer directly to
16913     the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
16914     within the TREE_TYPE of the list node.  */
16915  location_t loc
16916    = make_location (token->location, token->location, parser->lexer);
16917  tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
16918  SET_EXPR_LOCATION (dummy, loc);
16919  TREE_TYPE (node) = dummy;
16920
16921  return node;
16922}
16923
16924/* Parse a mem-initializer-id.
16925
16926   mem-initializer-id:
16927     :: [opt] nested-name-specifier [opt] class-name
16928     decltype-specifier (C++11)
16929     identifier
16930
16931   Returns a TYPE indicating the class to be initialized for the first
16932   production (and the second in C++11).  Returns an IDENTIFIER_NODE
16933   indicating the data member to be initialized for the last production.  */
16934
16935static tree
16936cp_parser_mem_initializer_id (cp_parser* parser)
16937{
16938  bool global_scope_p;
16939  bool nested_name_specifier_p;
16940  bool template_p = false;
16941  tree id;
16942
16943  cp_token *token = cp_lexer_peek_token (parser->lexer);
16944
16945  /* `typename' is not allowed in this context ([temp.res]).  */
16946  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16947    {
16948      error_at (token->location,
16949		"keyword %<typename%> not allowed in this context (a qualified "
16950		"member initializer is implicitly a type)");
16951      cp_lexer_consume_token (parser->lexer);
16952    }
16953  /* Look for the optional `::' operator.  */
16954  global_scope_p
16955    = (cp_parser_global_scope_opt (parser,
16956				   /*current_scope_valid_p=*/false)
16957       != NULL_TREE);
16958  /* Look for the optional nested-name-specifier.  The simplest way to
16959     implement:
16960
16961       [temp.res]
16962
16963       The keyword `typename' is not permitted in a base-specifier or
16964       mem-initializer; in these contexts a qualified name that
16965       depends on a template-parameter is implicitly assumed to be a
16966       type name.
16967
16968     is to assume that we have seen the `typename' keyword at this
16969     point.  */
16970  nested_name_specifier_p
16971    = (cp_parser_nested_name_specifier_opt (parser,
16972					    /*typename_keyword_p=*/true,
16973					    /*check_dependency_p=*/true,
16974					    /*type_p=*/true,
16975					    /*is_declaration=*/true)
16976       != NULL_TREE);
16977  if (nested_name_specifier_p)
16978    template_p = cp_parser_optional_template_keyword (parser);
16979  /* If there is a `::' operator or a nested-name-specifier, then we
16980     are definitely looking for a class-name.  */
16981  if (global_scope_p || nested_name_specifier_p)
16982    return cp_parser_class_name (parser,
16983				 /*typename_keyword_p=*/true,
16984				 /*template_keyword_p=*/template_p,
16985				 typename_type,
16986				 /*check_dependency_p=*/true,
16987				 /*class_head_p=*/false,
16988				 /*is_declaration=*/true);
16989  /* Otherwise, we could also be looking for an ordinary identifier.  */
16990  cp_parser_parse_tentatively (parser);
16991  if (cp_lexer_next_token_is_decltype (parser->lexer))
16992    /* Try a decltype-specifier.  */
16993    id = cp_parser_decltype (parser);
16994  else
16995    /* Otherwise, try a class-name.  */
16996    id = cp_parser_class_name (parser,
16997			       /*typename_keyword_p=*/true,
16998			       /*template_keyword_p=*/false,
16999			       none_type,
17000			       /*check_dependency_p=*/true,
17001			       /*class_head_p=*/false,
17002			       /*is_declaration=*/true);
17003  /* If we found one, we're done.  */
17004  if (cp_parser_parse_definitely (parser))
17005    return id;
17006  /* Otherwise, look for an ordinary identifier.  */
17007  return cp_parser_identifier (parser);
17008}
17009
17010/* Overloading [gram.over] */
17011
17012/* Parse an operator-function-id.
17013
17014   operator-function-id:
17015     operator operator
17016
17017   Returns an IDENTIFIER_NODE for the operator which is a
17018   human-readable spelling of the identifier, e.g., `operator +'.  */
17019
17020static cp_expr
17021cp_parser_operator_function_id (cp_parser* parser)
17022{
17023  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17024  /* Look for the `operator' keyword.  */
17025  if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17026    return error_mark_node;
17027  /* And then the name of the operator itself.  */
17028  return cp_parser_operator (parser, start_loc);
17029}
17030
17031/* Return an identifier node for a user-defined literal operator.
17032   The suffix identifier is chained to the operator name identifier.  */
17033
17034tree
17035cp_literal_operator_id (const char* name)
17036{
17037  tree identifier;
17038  char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17039			      + strlen (name) + 10);
17040  sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17041  identifier = get_identifier (buffer);
17042  XDELETEVEC (buffer);
17043
17044  return identifier;
17045}
17046
17047/* Parse an operator.
17048
17049   operator:
17050     new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17051     += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17052     || ++ -- , ->* -> () []
17053
17054   GNU Extensions:
17055
17056   operator:
17057     <? >? <?= >?=
17058
17059   Returns an IDENTIFIER_NODE for the operator which is a
17060   human-readable spelling of the identifier, e.g., `operator +'.  */
17061
17062static cp_expr
17063cp_parser_operator (cp_parser* parser, location_t start_loc)
17064{
17065  tree id = NULL_TREE;
17066  cp_token *token;
17067  bool utf8 = false;
17068
17069  /* Peek at the next token.  */
17070  token = cp_lexer_peek_token (parser->lexer);
17071
17072  location_t end_loc = token->location;
17073
17074  /* Figure out which operator we have.  */
17075  enum tree_code op = ERROR_MARK;
17076  bool assop = false;
17077  bool consumed = false;
17078  switch (token->type)
17079    {
17080    case CPP_KEYWORD:
17081      {
17082	/* The keyword should be either `new', `delete' or `co_await'.  */
17083	if (token->keyword == RID_NEW)
17084	  op = NEW_EXPR;
17085	else if (token->keyword == RID_DELETE)
17086	  op = DELETE_EXPR;
17087	else if (token->keyword == RID_CO_AWAIT)
17088	  op = CO_AWAIT_EXPR;
17089	else
17090	  break;
17091
17092	/* Consume the `new', `delete' or co_await token.  */
17093	end_loc = cp_lexer_consume_token (parser->lexer)->location;
17094
17095	/* Peek at the next token.  */
17096	token = cp_lexer_peek_token (parser->lexer);
17097	/* If it's a `[' token then this is the array variant of the
17098	   operator.  */
17099	if (token->type == CPP_OPEN_SQUARE
17100	    && op != CO_AWAIT_EXPR)
17101	  {
17102	    /* Consume the `[' token.  */
17103	    cp_lexer_consume_token (parser->lexer);
17104	    /* Look for the `]' token.  */
17105	    if (cp_token *close_token
17106		= cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17107	      end_loc = close_token->location;
17108	    op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17109	  }
17110	consumed = true;
17111	break;
17112      }
17113
17114    case CPP_PLUS:
17115      op = PLUS_EXPR;
17116      break;
17117
17118    case CPP_MINUS:
17119      op = MINUS_EXPR;
17120      break;
17121
17122    case CPP_MULT:
17123      op = MULT_EXPR;
17124      break;
17125
17126    case CPP_DIV:
17127      op = TRUNC_DIV_EXPR;
17128      break;
17129
17130    case CPP_MOD:
17131      op = TRUNC_MOD_EXPR;
17132      break;
17133
17134    case CPP_XOR:
17135      op = BIT_XOR_EXPR;
17136      break;
17137
17138    case CPP_AND:
17139      op = BIT_AND_EXPR;
17140      break;
17141
17142    case CPP_OR:
17143      op = BIT_IOR_EXPR;
17144      break;
17145
17146    case CPP_COMPL:
17147      op = BIT_NOT_EXPR;
17148      break;
17149
17150    case CPP_NOT:
17151      op = TRUTH_NOT_EXPR;
17152      break;
17153
17154    case CPP_EQ:
17155      assop = true;
17156      op = NOP_EXPR;
17157      break;
17158
17159    case CPP_LESS:
17160      op = LT_EXPR;
17161      break;
17162
17163    case CPP_GREATER:
17164      op = GT_EXPR;
17165      break;
17166
17167    case CPP_PLUS_EQ:
17168      assop = true;
17169      op = PLUS_EXPR;
17170      break;
17171
17172    case CPP_MINUS_EQ:
17173      assop = true;
17174      op = MINUS_EXPR;
17175      break;
17176
17177    case CPP_MULT_EQ:
17178      assop = true;
17179      op = MULT_EXPR;
17180      break;
17181
17182    case CPP_DIV_EQ:
17183      assop = true;
17184      op = TRUNC_DIV_EXPR;
17185      break;
17186
17187    case CPP_MOD_EQ:
17188      assop = true;
17189      op = TRUNC_MOD_EXPR;
17190      break;
17191
17192    case CPP_XOR_EQ:
17193      assop = true;
17194      op = BIT_XOR_EXPR;
17195      break;
17196
17197    case CPP_AND_EQ:
17198      assop = true;
17199      op = BIT_AND_EXPR;
17200      break;
17201
17202    case CPP_OR_EQ:
17203      assop = true;
17204      op = BIT_IOR_EXPR;
17205      break;
17206
17207    case CPP_LSHIFT:
17208      op = LSHIFT_EXPR;
17209      break;
17210
17211    case CPP_RSHIFT:
17212      op = RSHIFT_EXPR;
17213      break;
17214
17215    case CPP_LSHIFT_EQ:
17216      assop = true;
17217      op = LSHIFT_EXPR;
17218      break;
17219
17220    case CPP_RSHIFT_EQ:
17221      assop = true;
17222      op = RSHIFT_EXPR;
17223      break;
17224
17225    case CPP_EQ_EQ:
17226      op = EQ_EXPR;
17227      break;
17228
17229    case CPP_NOT_EQ:
17230      op = NE_EXPR;
17231      break;
17232
17233    case CPP_LESS_EQ:
17234      op = LE_EXPR;
17235      break;
17236
17237    case CPP_GREATER_EQ:
17238      op = GE_EXPR;
17239      break;
17240
17241    case CPP_SPACESHIP:
17242      op = SPACESHIP_EXPR;
17243      break;
17244
17245    case CPP_AND_AND:
17246      op = TRUTH_ANDIF_EXPR;
17247      break;
17248
17249    case CPP_OR_OR:
17250      op = TRUTH_ORIF_EXPR;
17251      break;
17252
17253    case CPP_PLUS_PLUS:
17254      op = POSTINCREMENT_EXPR;
17255      break;
17256
17257    case CPP_MINUS_MINUS:
17258      op = PREDECREMENT_EXPR;
17259      break;
17260
17261    case CPP_COMMA:
17262      op = COMPOUND_EXPR;
17263      break;
17264
17265    case CPP_DEREF_STAR:
17266      op = MEMBER_REF;
17267      break;
17268
17269    case CPP_DEREF:
17270      op = COMPONENT_REF;
17271      break;
17272
17273    case CPP_QUERY:
17274      op = COND_EXPR;
17275      /* Consume the `?'.  */
17276      cp_lexer_consume_token (parser->lexer);
17277      /* Look for the matching `:'.  */
17278      cp_parser_require (parser, CPP_COLON, RT_COLON);
17279      consumed = true;
17280      break;
17281
17282    case CPP_OPEN_PAREN:
17283      {
17284        /* Consume the `('.  */
17285        matching_parens parens;
17286        parens.consume_open (parser);
17287        /* Look for the matching `)'.  */
17288        token = parens.require_close (parser);
17289        if (token)
17290	  end_loc = token->location;
17291	op = CALL_EXPR;
17292	consumed = true;
17293	break;
17294      }
17295
17296    case CPP_OPEN_SQUARE:
17297      /* Consume the `['.  */
17298      cp_lexer_consume_token (parser->lexer);
17299      /* Look for the matching `]'.  */
17300      token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17301      if (token)
17302	end_loc = token->location;
17303      op = ARRAY_REF;
17304      consumed = true;
17305      break;
17306
17307    case CPP_UTF8STRING:
17308    case CPP_UTF8STRING_USERDEF:
17309      utf8 = true;
17310      /* FALLTHRU */
17311    case CPP_STRING:
17312    case CPP_WSTRING:
17313    case CPP_STRING16:
17314    case CPP_STRING32:
17315    case CPP_STRING_USERDEF:
17316    case CPP_WSTRING_USERDEF:
17317    case CPP_STRING16_USERDEF:
17318    case CPP_STRING32_USERDEF:
17319      {
17320	cp_expr str;
17321	tree string_tree;
17322	int sz, len;
17323
17324	if (cxx_dialect == cxx98)
17325	  maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17326
17327	/* Consume the string.  */
17328	str = cp_parser_string_literal (parser, /*translate=*/true,
17329				      /*wide_ok=*/true, /*lookup_udlit=*/false);
17330	if (str == error_mark_node)
17331	  return error_mark_node;
17332	else if (TREE_CODE (str) == USERDEF_LITERAL)
17333	  {
17334	    string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17335	    id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17336	    end_loc = str.get_location ();
17337	  }
17338	else
17339	  {
17340	    string_tree = str;
17341	    /* Look for the suffix identifier.  */
17342	    token = cp_lexer_peek_token (parser->lexer);
17343	    if (token->type == CPP_NAME)
17344	      {
17345		id = cp_parser_identifier (parser);
17346		end_loc = token->location;
17347	      }
17348	    else if (token->type == CPP_KEYWORD)
17349	      {
17350		error ("unexpected keyword;"
17351		       " remove space between quotes and suffix identifier");
17352		return error_mark_node;
17353	      }
17354	    else
17355	      {
17356		error ("expected suffix identifier");
17357		return error_mark_node;
17358	      }
17359	  }
17360	sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17361			       (TREE_TYPE (TREE_TYPE (string_tree))));
17362	len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17363	if (len != 0)
17364	  {
17365	    error ("expected empty string after %<operator%> keyword");
17366	    return error_mark_node;
17367	  }
17368	if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17369	    != char_type_node)
17370	  {
17371	    error ("invalid encoding prefix in literal operator");
17372	    return error_mark_node;
17373	  }
17374	if (id != error_mark_node)
17375	  {
17376	    const char *name = IDENTIFIER_POINTER (id);
17377	    id = cp_literal_operator_id (name);
17378	  }
17379	/* Generate a location of the form:
17380	     "" _suffix_identifier
17381	     ^~~~~~~~~~~~~~~~~~~~~
17382	   with caret == start at the start token, finish at the end of the
17383	   suffix identifier.  */
17384	location_t combined_loc
17385	  = make_location (start_loc, start_loc, parser->lexer);
17386	return cp_expr (id, combined_loc);
17387      }
17388
17389    default:
17390      /* Anything else is an error.  */
17391      break;
17392    }
17393
17394  /* If we have selected an identifier, we need to consume the
17395     operator token.  */
17396  if (op != ERROR_MARK)
17397    {
17398      id = ovl_op_identifier (assop, op);
17399      if (!consumed)
17400	cp_lexer_consume_token (parser->lexer);
17401    }
17402  /* Otherwise, no valid operator name was present.  */
17403  else
17404    {
17405      cp_parser_error (parser, "expected operator");
17406      id = error_mark_node;
17407    }
17408
17409  start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17410  return cp_expr (id, start_loc);
17411}
17412
17413/* Parse a template-declaration.
17414
17415   template-declaration:
17416     export [opt] template < template-parameter-list > declaration
17417
17418   If MEMBER_P is TRUE, this template-declaration occurs within a
17419   class-specifier.
17420
17421   The grammar rule given by the standard isn't correct.  What
17422   is really meant is:
17423
17424   template-declaration:
17425     export [opt] template-parameter-list-seq
17426       decl-specifier-seq [opt] init-declarator [opt] ;
17427     export [opt] template-parameter-list-seq
17428       function-definition
17429
17430   template-parameter-list-seq:
17431     template-parameter-list-seq [opt]
17432     template < template-parameter-list >
17433
17434   Concept Extensions:
17435
17436   template-parameter-list-seq:
17437     template < template-parameter-list > requires-clause [opt]
17438
17439   requires-clause:
17440     requires logical-or-expression  */
17441
17442static void
17443cp_parser_template_declaration (cp_parser* parser, bool member_p)
17444{
17445  /* Check for `export'.  */
17446  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17447    {
17448      /* Consume the `export' token.  */
17449      cp_lexer_consume_token (parser->lexer);
17450      /* Warn that this use of export is deprecated.  */
17451      if (cxx_dialect < cxx11)
17452	warning (0, "keyword %<export%> not implemented, and will be ignored");
17453      else if (cxx_dialect < cxx20)
17454	warning (0, "keyword %<export%> is deprecated, and is ignored");
17455      else
17456	warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17457    }
17458
17459  cp_parser_template_declaration_after_export (parser, member_p);
17460}
17461
17462/* Parse a template-parameter-list.
17463
17464   template-parameter-list:
17465     template-parameter
17466     template-parameter-list , template-parameter
17467
17468   Returns a TREE_LIST.  Each node represents a template parameter.
17469   The nodes are connected via their TREE_CHAINs.  */
17470
17471static tree
17472cp_parser_template_parameter_list (cp_parser* parser)
17473{
17474  tree parameter_list = NULL_TREE;
17475
17476  /* Don't create wrapper nodes within a template-parameter-list,
17477     since we don't want to have different types based on the
17478     spelling location of constants and decls within them.  */
17479  auto_suppress_location_wrappers sentinel;
17480
17481  begin_template_parm_list ();
17482
17483  /* The loop below parses the template parms.  We first need to know
17484     the total number of template parms to be able to compute proper
17485     canonical types of each dependent type. So after the loop, when
17486     we know the total number of template parms,
17487     end_template_parm_list computes the proper canonical types and
17488     fixes up the dependent types accordingly.  */
17489  while (true)
17490    {
17491      tree parameter;
17492      bool is_non_type;
17493      bool is_parameter_pack;
17494      location_t parm_loc;
17495
17496      /* Parse the template-parameter.  */
17497      parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17498      parameter = cp_parser_template_parameter (parser,
17499                                                &is_non_type,
17500                                                &is_parameter_pack);
17501      /* Add it to the list.  */
17502      if (parameter != error_mark_node)
17503	parameter_list = process_template_parm (parameter_list,
17504						parm_loc,
17505						parameter,
17506						is_non_type,
17507						is_parameter_pack);
17508      else
17509       {
17510         tree err_parm = build_tree_list (parameter, parameter);
17511         parameter_list = chainon (parameter_list, err_parm);
17512       }
17513
17514      /* If the next token is not a `,', we're done.  */
17515      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17516	break;
17517      /* Otherwise, consume the `,' token.  */
17518      cp_lexer_consume_token (parser->lexer);
17519    }
17520
17521  return end_template_parm_list (parameter_list);
17522}
17523
17524/* Parse a introduction-list.
17525
17526   introduction-list:
17527     introduced-parameter
17528     introduction-list , introduced-parameter
17529
17530   introduced-parameter:
17531     ...[opt] identifier
17532
17533   Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
17534   then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
17535   WILDCARD_DECL will also have DECL_NAME set and token location in
17536   DECL_SOURCE_LOCATION.  */
17537
17538static tree
17539cp_parser_introduction_list (cp_parser *parser)
17540{
17541  vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17542
17543  while (true)
17544    {
17545      bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17546      if (is_pack)
17547	cp_lexer_consume_token (parser->lexer);
17548
17549      tree identifier = cp_parser_identifier (parser);
17550      if (identifier == error_mark_node)
17551	break;
17552
17553      /* Build placeholder. */
17554      tree parm = build_nt (WILDCARD_DECL);
17555      DECL_SOURCE_LOCATION (parm)
17556	= cp_lexer_peek_token (parser->lexer)->location;
17557      DECL_NAME (parm) = identifier;
17558      WILDCARD_PACK_P (parm) = is_pack;
17559      vec_safe_push (introduction_vec, parm);
17560
17561      /* If the next token is not a `,', we're done.  */
17562      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17563	break;
17564      /* Otherwise, consume the `,' token.  */
17565      cp_lexer_consume_token (parser->lexer);
17566    }
17567
17568  /* Convert the vec into a TREE_VEC.  */
17569  tree introduction_list = make_tree_vec (introduction_vec->length ());
17570  unsigned int n;
17571  tree parm;
17572  FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17573    TREE_VEC_ELT (introduction_list, n) = parm;
17574
17575  release_tree_vector (introduction_vec);
17576  return introduction_list;
17577}
17578
17579/* Given a declarator, get the declarator-id part, or NULL_TREE if this
17580   is an abstract declarator. */
17581
17582static inline cp_declarator*
17583get_id_declarator (cp_declarator *declarator)
17584{
17585  cp_declarator *d = declarator;
17586  while (d && d->kind != cdk_id)
17587    d = d->declarator;
17588  return d;
17589}
17590
17591/* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17592   is an abstract declarator. */
17593
17594static inline tree
17595get_unqualified_id (cp_declarator *declarator)
17596{
17597  declarator = get_id_declarator (declarator);
17598  if (declarator)
17599    return declarator->u.id.unqualified_name;
17600  else
17601    return NULL_TREE;
17602}
17603
17604/* Returns true if TYPE would declare a constrained constrained-parameter.  */
17605
17606static inline bool
17607is_constrained_parameter (tree type)
17608{
17609  return (type
17610          && TREE_CODE (type) == TYPE_DECL
17611          && CONSTRAINED_PARM_CONCEPT (type)
17612          && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
17613}
17614
17615/* Returns true if PARM declares a constrained-parameter. */
17616
17617static inline bool
17618is_constrained_parameter (cp_parameter_declarator *parm)
17619{
17620  return is_constrained_parameter (parm->decl_specifiers.type);
17621}
17622
17623/* Check that the type parameter is only a declarator-id, and that its
17624   type is not cv-qualified. */
17625
17626bool
17627cp_parser_check_constrained_type_parm (cp_parser *parser,
17628				       cp_parameter_declarator *parm)
17629{
17630  if (!parm->declarator)
17631    return true;
17632
17633  if (parm->declarator->kind != cdk_id)
17634    {
17635      cp_parser_error (parser, "invalid constrained type parameter");
17636      return false;
17637    }
17638
17639  /* Don't allow cv-qualified type parameters.  */
17640  if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
17641      || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
17642    {
17643      cp_parser_error (parser, "cv-qualified type parameter");
17644      return false;
17645    }
17646
17647  return true;
17648}
17649
17650/* Finish parsing/processing a template type parameter and checking
17651   various restrictions. */
17652
17653static inline tree
17654cp_parser_constrained_type_template_parm (cp_parser *parser,
17655                                          tree id,
17656                                          cp_parameter_declarator* parmdecl)
17657{
17658  if (cp_parser_check_constrained_type_parm (parser, parmdecl))
17659    return finish_template_type_parm (class_type_node, id);
17660  else
17661    return error_mark_node;
17662}
17663
17664static tree
17665finish_constrained_template_template_parm (tree proto, tree id)
17666{
17667  /* FIXME: This should probably be copied, and we may need to adjust
17668     the template parameter depths.  */
17669  tree saved_parms = current_template_parms;
17670  begin_template_parm_list ();
17671  current_template_parms = DECL_TEMPLATE_PARMS (proto);
17672  end_template_parm_list ();
17673
17674  tree parm = finish_template_template_parm (class_type_node, id);
17675  current_template_parms = saved_parms;
17676
17677  return parm;
17678}
17679
17680/* Finish parsing/processing a template template parameter by borrowing
17681   the template parameter list from the prototype parameter.  */
17682
17683static tree
17684cp_parser_constrained_template_template_parm (cp_parser *parser,
17685                                              tree proto,
17686                                              tree id,
17687                                              cp_parameter_declarator *parmdecl)
17688{
17689  if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
17690    return error_mark_node;
17691  return finish_constrained_template_template_parm (proto, id);
17692}
17693
17694/* Create a new non-type template parameter from the given PARM
17695   declarator.  */
17696
17697static tree
17698cp_parser_constrained_non_type_template_parm (bool *is_non_type,
17699					      cp_parameter_declarator *parm)
17700{
17701  *is_non_type = true;
17702  cp_declarator *decl = parm->declarator;
17703  cp_decl_specifier_seq *specs = &parm->decl_specifiers;
17704  specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
17705  return grokdeclarator (decl, specs, TPARM, 0, NULL);
17706}
17707
17708/* Build a constrained template parameter based on the PARMDECL
17709   declarator. The type of PARMDECL is the constrained type, which
17710   refers to the prototype template parameter that ultimately
17711   specifies the type of the declared parameter. */
17712
17713static tree
17714finish_constrained_parameter (cp_parser *parser,
17715                              cp_parameter_declarator *parmdecl,
17716                              bool *is_non_type)
17717{
17718  tree decl = parmdecl->decl_specifiers.type;
17719  tree id = get_unqualified_id (parmdecl->declarator);
17720  tree def = parmdecl->default_argument;
17721  tree proto = DECL_INITIAL (decl);
17722
17723  /* Build the parameter. Return an error if the declarator was invalid. */
17724  tree parm;
17725  if (TREE_CODE (proto) == TYPE_DECL)
17726    parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
17727  else if (TREE_CODE (proto) == TEMPLATE_DECL)
17728    parm = cp_parser_constrained_template_template_parm (parser, proto, id,
17729							 parmdecl);
17730  else
17731    parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
17732  if (parm == error_mark_node)
17733    return error_mark_node;
17734
17735  /* Finish the parameter decl and create a node attaching the
17736     default argument and constraint.  */
17737  parm = build_tree_list (def, parm);
17738  TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
17739
17740  return parm;
17741}
17742
17743/* Returns true if the parsed type actually represents the declaration
17744   of a type template-parameter.  */
17745
17746static bool
17747declares_constrained_type_template_parameter (tree type)
17748{
17749  return (is_constrained_parameter (type)
17750	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
17751}
17752
17753/* Returns true if the parsed type actually represents the declaration of
17754   a template template-parameter.  */
17755
17756static bool
17757declares_constrained_template_template_parameter (tree type)
17758{
17759  return (is_constrained_parameter (type)
17760	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
17761}
17762
17763/* Parse a default argument for a type template-parameter.
17764   Note that diagnostics are handled in cp_parser_template_parameter.  */
17765
17766static tree
17767cp_parser_default_type_template_argument (cp_parser *parser)
17768{
17769  gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17770
17771  /* Consume the `=' token.  */
17772  cp_lexer_consume_token (parser->lexer);
17773
17774  cp_token *token = cp_lexer_peek_token (parser->lexer);
17775
17776  /* Tell cp_parser_lambda_expression this is a default argument.  */
17777  auto lvf = make_temp_override (parser->local_variables_forbidden_p);
17778  parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
17779
17780  /* Parse the default-argument.  */
17781  push_deferring_access_checks (dk_no_deferred);
17782  tree default_argument = cp_parser_type_id (parser,
17783					     CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17784					     NULL);
17785  pop_deferring_access_checks ();
17786
17787  if (flag_concepts && type_uses_auto (default_argument))
17788    {
17789      error_at (token->location,
17790		"invalid use of %<auto%> in default template argument");
17791      return error_mark_node;
17792    }
17793
17794  return default_argument;
17795}
17796
17797/* Parse a default argument for a template template-parameter.  */
17798
17799static tree
17800cp_parser_default_template_template_argument (cp_parser *parser)
17801{
17802  gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17803
17804  bool is_template;
17805
17806  /* Consume the `='.  */
17807  cp_lexer_consume_token (parser->lexer);
17808  /* Parse the id-expression.  */
17809  push_deferring_access_checks (dk_no_deferred);
17810  /* save token before parsing the id-expression, for error
17811     reporting */
17812  const cp_token* token = cp_lexer_peek_token (parser->lexer);
17813  tree default_argument
17814    = cp_parser_id_expression (parser,
17815                               /*template_keyword_p=*/false,
17816                               /*check_dependency_p=*/true,
17817                               /*template_p=*/&is_template,
17818                               /*declarator_p=*/false,
17819                               /*optional_p=*/false);
17820  if (TREE_CODE (default_argument) == TYPE_DECL)
17821    /* If the id-expression was a template-id that refers to
17822       a template-class, we already have the declaration here,
17823       so no further lookup is needed.  */
17824    ;
17825  else
17826    /* Look up the name.  */
17827    default_argument
17828      = cp_parser_lookup_name (parser, default_argument,
17829                               none_type,
17830                               /*is_template=*/is_template,
17831                               /*is_namespace=*/false,
17832                               /*check_dependency=*/true,
17833                               /*ambiguous_decls=*/NULL,
17834                               token->location);
17835  /* See if the default argument is valid.  */
17836  default_argument = check_template_template_default_arg (default_argument);
17837  pop_deferring_access_checks ();
17838  return default_argument;
17839}
17840
17841/* Parse a template-parameter.
17842
17843   template-parameter:
17844     type-parameter
17845     parameter-declaration
17846
17847   If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
17848   the parameter.  The TREE_PURPOSE is the default value, if any.
17849   Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
17850   iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
17851   set to true iff this parameter is a parameter pack. */
17852
17853static tree
17854cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
17855                              bool *is_parameter_pack)
17856{
17857  cp_token *token;
17858  cp_parameter_declarator *parameter_declarator;
17859  tree parm;
17860
17861  /* Assume it is a type parameter or a template parameter.  */
17862  *is_non_type = false;
17863  /* Assume it not a parameter pack. */
17864  *is_parameter_pack = false;
17865  /* Peek at the next token.  */
17866  token = cp_lexer_peek_token (parser->lexer);
17867  /* If it is `template', we have a type-parameter.  */
17868  if (token->keyword == RID_TEMPLATE)
17869    return cp_parser_type_parameter (parser, is_parameter_pack);
17870  /* If it is `class' or `typename' we do not know yet whether it is a
17871     type parameter or a non-type parameter.  Consider:
17872
17873       template <typename T, typename T::X X> ...
17874
17875     or:
17876
17877       template <class C, class D*> ...
17878
17879     Here, the first parameter is a type parameter, and the second is
17880     a non-type parameter.  We can tell by looking at the token after
17881     the identifier -- if it is a `,', `=', or `>' then we have a type
17882     parameter.  */
17883  if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
17884    {
17885      /* Peek at the token after `class' or `typename'.  */
17886      token = cp_lexer_peek_nth_token (parser->lexer, 2);
17887      /* If it's an ellipsis, we have a template type parameter
17888         pack. */
17889      if (token->type == CPP_ELLIPSIS)
17890        return cp_parser_type_parameter (parser, is_parameter_pack);
17891      /* If it's an identifier, skip it.  */
17892      if (token->type == CPP_NAME)
17893	token = cp_lexer_peek_nth_token (parser->lexer, 3);
17894      /* Now, see if the token looks like the end of a template
17895	 parameter.  */
17896      if (token->type == CPP_COMMA
17897	  || token->type == CPP_EQ
17898	  || token->type == CPP_GREATER)
17899	return cp_parser_type_parameter (parser, is_parameter_pack);
17900    }
17901
17902  /* Otherwise, it is a non-type parameter or a constrained parameter.
17903
17904     [temp.param]
17905
17906     When parsing a default template-argument for a non-type
17907     template-parameter, the first non-nested `>' is taken as the end
17908     of the template parameter-list rather than a greater-than
17909     operator.  */
17910  parameter_declarator
17911     = cp_parser_parameter_declaration (parser,
17912					CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17913					/*template_parm_p=*/true,
17914					/*parenthesized_p=*/NULL);
17915
17916  if (!parameter_declarator)
17917    return error_mark_node;
17918
17919  /* If the parameter declaration is marked as a parameter pack, set
17920   *IS_PARAMETER_PACK to notify the caller.  */
17921  if (parameter_declarator->template_parameter_pack_p)
17922    *is_parameter_pack = true;
17923
17924  if (parameter_declarator->default_argument)
17925    {
17926      /* Can happen in some cases of erroneous input (c++/34892).  */
17927      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17928	/* Consume the `...' for better error recovery.  */
17929	cp_lexer_consume_token (parser->lexer);
17930    }
17931
17932  /* The parameter may have been constrained type parameter.  */
17933  if (is_constrained_parameter (parameter_declarator))
17934    return finish_constrained_parameter (parser,
17935                                         parameter_declarator,
17936                                         is_non_type);
17937
17938  // Now we're sure that the parameter is a non-type parameter.
17939  *is_non_type = true;
17940
17941  parm = grokdeclarator (parameter_declarator->declarator,
17942			 &parameter_declarator->decl_specifiers,
17943			 TPARM, /*initialized=*/0,
17944			 /*attrlist=*/NULL);
17945  if (parm == error_mark_node)
17946    return error_mark_node;
17947
17948  return build_tree_list (parameter_declarator->default_argument, parm);
17949}
17950
17951/* Parse a type-parameter.
17952
17953   type-parameter:
17954     class identifier [opt]
17955     class identifier [opt] = type-id
17956     typename identifier [opt]
17957     typename identifier [opt] = type-id
17958     template < template-parameter-list > class identifier [opt]
17959     template < template-parameter-list > class identifier [opt]
17960       = id-expression
17961
17962   GNU Extension (variadic templates):
17963
17964   type-parameter:
17965     class ... identifier [opt]
17966     typename ... identifier [opt]
17967
17968   Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
17969   TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
17970   the declaration of the parameter.
17971
17972   Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
17973
17974static tree
17975cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
17976{
17977  cp_token *token;
17978  tree parameter;
17979
17980  /* Look for a keyword to tell us what kind of parameter this is.  */
17981  token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
17982  if (!token)
17983    return error_mark_node;
17984
17985  switch (token->keyword)
17986    {
17987    case RID_CLASS:
17988    case RID_TYPENAME:
17989      {
17990	tree identifier;
17991	tree default_argument;
17992
17993        /* If the next token is an ellipsis, we have a template
17994           argument pack. */
17995        if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17996          {
17997            /* Consume the `...' token. */
17998            cp_lexer_consume_token (parser->lexer);
17999            maybe_warn_variadic_templates ();
18000
18001            *is_parameter_pack = true;
18002          }
18003
18004	/* If the next token is an identifier, then it names the
18005	   parameter.  */
18006	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18007	  identifier = cp_parser_identifier (parser);
18008	else
18009	  identifier = NULL_TREE;
18010
18011	/* Create the parameter.  */
18012	parameter = finish_template_type_parm (class_type_node, identifier);
18013
18014	/* If the next token is an `=', we have a default argument.  */
18015	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18016	  {
18017	    default_argument
18018	      = cp_parser_default_type_template_argument (parser);
18019
18020            /* Template parameter packs cannot have default
18021               arguments. */
18022            if (*is_parameter_pack)
18023              {
18024                if (identifier)
18025                  error_at (token->location,
18026			    "template parameter pack %qD cannot have a "
18027			    "default argument", identifier);
18028                else
18029                  error_at (token->location,
18030			    "template parameter packs cannot have "
18031			    "default arguments");
18032                default_argument = NULL_TREE;
18033              }
18034	    else if (check_for_bare_parameter_packs (default_argument))
18035	      default_argument = error_mark_node;
18036	  }
18037	else
18038	  default_argument = NULL_TREE;
18039
18040	/* Create the combined representation of the parameter and the
18041	   default argument.  */
18042	parameter = build_tree_list (default_argument, parameter);
18043      }
18044      break;
18045
18046    case RID_TEMPLATE:
18047      {
18048	tree identifier;
18049	tree default_argument;
18050
18051	/* Look for the `<'.  */
18052	cp_parser_require (parser, CPP_LESS, RT_LESS);
18053	/* Parse the template-parameter-list.  */
18054	cp_parser_template_parameter_list (parser);
18055	/* Look for the `>'.  */
18056	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18057
18058	/* If template requirements are present, parse them.  */
18059	if (flag_concepts)
18060          {
18061	    tree reqs = get_shorthand_constraints (current_template_parms);
18062	    if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18063              reqs = combine_constraint_expressions (reqs, dreqs);
18064	    TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18065          }
18066
18067	/* Look for the `class' or 'typename' keywords.  */
18068	cp_parser_type_parameter_key (parser);
18069        /* If the next token is an ellipsis, we have a template
18070           argument pack. */
18071        if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18072          {
18073            /* Consume the `...' token. */
18074            cp_lexer_consume_token (parser->lexer);
18075            maybe_warn_variadic_templates ();
18076
18077            *is_parameter_pack = true;
18078          }
18079	/* If the next token is an `=', then there is a
18080	   default-argument.  If the next token is a `>', we are at
18081	   the end of the parameter-list.  If the next token is a `,',
18082	   then we are at the end of this parameter.  */
18083	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18084	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18085	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18086	  {
18087	    identifier = cp_parser_identifier (parser);
18088	    /* Treat invalid names as if the parameter were nameless.  */
18089	    if (identifier == error_mark_node)
18090	      identifier = NULL_TREE;
18091	  }
18092	else
18093	  identifier = NULL_TREE;
18094
18095	/* Create the template parameter.  */
18096	parameter = finish_template_template_parm (class_type_node,
18097						   identifier);
18098
18099	/* If the next token is an `=', then there is a
18100	   default-argument.  */
18101	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18102	  {
18103	    default_argument
18104	      = cp_parser_default_template_template_argument (parser);
18105
18106            /* Template parameter packs cannot have default
18107               arguments. */
18108            if (*is_parameter_pack)
18109              {
18110                if (identifier)
18111                  error_at (token->location,
18112			    "template parameter pack %qD cannot "
18113			    "have a default argument",
18114			    identifier);
18115                else
18116                  error_at (token->location, "template parameter packs cannot "
18117			    "have default arguments");
18118                default_argument = NULL_TREE;
18119              }
18120	  }
18121	else
18122	  default_argument = NULL_TREE;
18123
18124	/* Create the combined representation of the parameter and the
18125	   default argument.  */
18126	parameter = build_tree_list (default_argument, parameter);
18127      }
18128      break;
18129
18130    default:
18131      gcc_unreachable ();
18132      break;
18133    }
18134
18135  return parameter;
18136}
18137
18138/* Parse a template-id.
18139
18140   template-id:
18141     template-name < template-argument-list [opt] >
18142
18143   If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18144   `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
18145   returned.  Otherwise, if the template-name names a function, or set
18146   of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
18147   names a class, returns a TYPE_DECL for the specialization.
18148
18149   If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18150   uninstantiated templates.  */
18151
18152static tree
18153cp_parser_template_id (cp_parser *parser,
18154		       bool template_keyword_p,
18155		       bool check_dependency_p,
18156		       enum tag_types tag_type,
18157		       bool is_declaration)
18158{
18159  tree templ;
18160  tree arguments;
18161  tree template_id;
18162  cp_token_position start_of_id = 0;
18163  cp_token *next_token = NULL, *next_token_2 = NULL;
18164  bool is_identifier;
18165
18166  /* If the next token corresponds to a template-id, there is no need
18167     to reparse it.  */
18168  cp_token *token = cp_lexer_peek_token (parser->lexer);
18169
18170  if (token->type == CPP_TEMPLATE_ID)
18171    {
18172      cp_lexer_consume_token (parser->lexer);
18173      return saved_checks_value (token->u.tree_check_value);
18174    }
18175
18176  /* Avoid performing name lookup if there is no possibility of
18177     finding a template-id.  */
18178  if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18179      || (token->type == CPP_NAME
18180	  && !cp_parser_nth_token_starts_template_argument_list_p
18181	       (parser, 2)))
18182    {
18183      cp_parser_error (parser, "expected template-id");
18184      return error_mark_node;
18185    }
18186
18187  /* Remember where the template-id starts.  */
18188  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18189    start_of_id = cp_lexer_token_position (parser->lexer, false);
18190
18191  push_deferring_access_checks (dk_deferred);
18192
18193  /* Parse the template-name.  */
18194  is_identifier = false;
18195  templ = cp_parser_template_name (parser, template_keyword_p,
18196				   check_dependency_p,
18197				   is_declaration,
18198				   tag_type,
18199				   &is_identifier);
18200
18201  /* Push any access checks inside the firewall we're about to create.  */
18202  vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18203  pop_deferring_access_checks ();
18204  if (templ == error_mark_node || is_identifier)
18205    return templ;
18206
18207  /* Since we're going to preserve any side-effects from this parse, set up a
18208     firewall to protect our callers from cp_parser_commit_to_tentative_parse
18209     in the template arguments.  */
18210  tentative_firewall firewall (parser);
18211  reopen_deferring_access_checks (checks);
18212
18213  /* If we find the sequence `[:' after a template-name, it's probably
18214     a digraph-typo for `< ::'. Substitute the tokens and check if we can
18215     parse correctly the argument list.  */
18216  if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18217       == CPP_OPEN_SQUARE)
18218      && next_token->flags & DIGRAPH
18219      && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18220	  == CPP_COLON)
18221      && !(next_token_2->flags & PREV_WHITE))
18222    {
18223      cp_parser_parse_tentatively (parser);
18224      /* Change `:' into `::'.  */
18225      next_token_2->type = CPP_SCOPE;
18226      /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18227	 CPP_LESS.  */
18228      cp_lexer_consume_token (parser->lexer);
18229
18230      /* Parse the arguments.  */
18231      arguments = cp_parser_enclosed_template_argument_list (parser);
18232      if (!cp_parser_parse_definitely (parser))
18233	{
18234	  /* If we couldn't parse an argument list, then we revert our changes
18235	     and return simply an error. Maybe this is not a template-id
18236	     after all.  */
18237	  next_token_2->type = CPP_COLON;
18238	  cp_parser_error (parser, "expected %<<%>");
18239	  pop_deferring_access_checks ();
18240	  return error_mark_node;
18241	}
18242      /* Otherwise, emit an error about the invalid digraph, but continue
18243	 parsing because we got our argument list.  */
18244      if (permerror (next_token->location,
18245		     "%<<::%> cannot begin a template-argument list"))
18246	{
18247	  static bool hint = false;
18248	  inform (next_token->location,
18249		  "%<<:%> is an alternate spelling for %<[%>."
18250		  " Insert whitespace between %<<%> and %<::%>");
18251	  if (!hint && !flag_permissive)
18252	    {
18253	      inform (next_token->location, "(if you use %<-fpermissive%> "
18254		      "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18255		      "accept your code)");
18256	      hint = true;
18257	    }
18258	}
18259    }
18260  else
18261    {
18262      /* Look for the `<' that starts the template-argument-list.  */
18263      if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18264	{
18265	  pop_deferring_access_checks ();
18266	  return error_mark_node;
18267	}
18268      /* Parse the arguments.  */
18269      arguments = cp_parser_enclosed_template_argument_list (parser);
18270
18271      if ((cxx_dialect > cxx17)
18272	  && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18273	  && !template_keyword_p
18274	  && (cp_parser_error_occurred (parser)
18275	      || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18276	{
18277	  /* This didn't go well.  */
18278	  if (TREE_CODE (templ) == FUNCTION_DECL)
18279	    {
18280	      /* C++20 says that "function-name < a;" is now ill-formed.  */
18281	      if (cp_parser_error_occurred (parser))
18282		{
18283		  error_at (token->location, "invalid template-argument-list");
18284		  inform (token->location, "function name as the left hand "
18285			  "operand of %<<%> is ill-formed in C++20; wrap the "
18286			  "function name in %<()%>");
18287		}
18288	      else
18289		/* We expect "f<targs>" to be followed by "(args)".  */
18290		error_at (cp_lexer_peek_token (parser->lexer)->location,
18291			  "expected %<(%> after template-argument-list");
18292	      if (start_of_id)
18293		/* Purge all subsequent tokens.  */
18294		cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18295	    }
18296	  else
18297	    cp_parser_simulate_error (parser);
18298	  pop_deferring_access_checks ();
18299	  return error_mark_node;
18300	}
18301    }
18302
18303  /* Set the location to be of the form:
18304     template-name < template-argument-list [opt] >
18305     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18306     with caret == start at the start of the template-name,
18307     ranging until the closing '>'.  */
18308  location_t combined_loc
18309    = make_location (token->location, token->location, parser->lexer);
18310
18311  /* Check for concepts autos where they don't belong.  We could
18312     identify types in some cases of identifier TEMPL, looking ahead
18313     for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18314     types.  We reject them in functions, but if what we have is an
18315     identifier, even with none_type we can't conclude it's NOT a
18316     type, we have to wait for template substitution.  */
18317  if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18318    template_id = error_mark_node;
18319  /* Build a representation of the specialization.  */
18320  else if (identifier_p (templ))
18321    template_id = build_min_nt_loc (combined_loc,
18322				    TEMPLATE_ID_EXPR,
18323				    templ, arguments);
18324  else if (DECL_TYPE_TEMPLATE_P (templ)
18325	   || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18326    {
18327      /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18328	 template (rather than some instantiation thereof) only if
18329	 is not nested within some other construct.  For example, in
18330	 "template <typename T> void f(T) { A<T>::", A<T> is just an
18331	 instantiation of A.  */
18332      bool entering_scope
18333	= (template_parm_scope_p ()
18334	   && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18335      template_id
18336	= finish_template_type (templ, arguments, entering_scope);
18337    }
18338  else if (concept_definition_p (templ))
18339    {
18340      /* The caller will decide whether this is a concept check or type
18341	 constraint.  */
18342      template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18343				boolean_type_node, templ, arguments);
18344    }
18345  else if (variable_template_p (templ))
18346    {
18347      template_id = lookup_template_variable (templ, arguments);
18348      if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18349	SET_EXPR_LOCATION (template_id, combined_loc);
18350    }
18351  else if (TREE_CODE (templ) == TYPE_DECL
18352	   && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18353    {
18354      /* Some type template in dependent scope.  */
18355      tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18356      name = build_min_nt_loc (combined_loc,
18357			       TEMPLATE_ID_EXPR,
18358			       name, arguments);
18359      template_id = templ;
18360    }
18361  else
18362    {
18363      /* If it's not a class-template or a template-template, it should be
18364	 a function-template.  */
18365      gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18366
18367      template_id = lookup_template_function (templ, arguments);
18368      if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18369	SET_EXPR_LOCATION (template_id, combined_loc);
18370    }
18371
18372  /* If parsing tentatively, replace the sequence of tokens that makes
18373     up the template-id with a CPP_TEMPLATE_ID token.  That way,
18374     should we re-parse the token stream, we will not have to repeat
18375     the effort required to do the parse, nor will we issue duplicate
18376     error messages about problems during instantiation of the
18377     template.  */
18378  if (start_of_id
18379      /* Don't do this if we had a parse error in a declarator; re-parsing
18380	 might succeed if a name changes meaning (60361).  */
18381      && !(cp_parser_error_occurred (parser)
18382	   && cp_parser_parsing_tentatively (parser)
18383	   && parser->in_declarator_p))
18384    {
18385      /* Reset the contents of the START_OF_ID token.  */
18386      token->type = CPP_TEMPLATE_ID;
18387      token->location = combined_loc;
18388
18389      /* Retrieve any deferred checks.  Do not pop this access checks yet
18390	 so the memory will not be reclaimed during token replacing below.  */
18391      token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18392      token->tree_check_p = true;
18393      token->u.tree_check_value->value = template_id;
18394      token->u.tree_check_value->checks = get_deferred_access_checks ();
18395      token->keyword = RID_MAX;
18396
18397      /* Purge all subsequent tokens.  */
18398      cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18399
18400      /* ??? Can we actually assume that, if template_id ==
18401	 error_mark_node, we will have issued a diagnostic to the
18402	 user, as opposed to simply marking the tentative parse as
18403	 failed?  */
18404      if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18405	error_at (token->location, "parse error in template argument list");
18406    }
18407
18408  pop_to_parent_deferring_access_checks ();
18409  return template_id;
18410}
18411
18412/* Like cp_parser_template_id, called in non-type context.  */
18413
18414static tree
18415cp_parser_template_id_expr (cp_parser *parser,
18416			    bool template_keyword_p,
18417			    bool check_dependency_p,
18418			    bool is_declaration)
18419{
18420  tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18421				  none_type, is_declaration);
18422  if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18423      && concept_check_p (x))
18424    /* We didn't check the arguments in cp_parser_template_id; do that now.  */
18425    return build_concept_id (x);
18426  return x;
18427}
18428
18429/* Parse a template-name.
18430
18431   template-name:
18432     identifier
18433
18434   The standard should actually say:
18435
18436   template-name:
18437     identifier
18438     operator-function-id
18439
18440   A defect report has been filed about this issue.
18441
18442   A conversion-function-id cannot be a template name because they cannot
18443   be part of a template-id. In fact, looking at this code:
18444
18445   a.operator K<int>()
18446
18447   the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18448   It is impossible to call a templated conversion-function-id with an
18449   explicit argument list, since the only allowed template parameter is
18450   the type to which it is converting.
18451
18452   If TEMPLATE_KEYWORD_P is true, then we have just seen the
18453   `template' keyword, in a construction like:
18454
18455     T::template f<3>()
18456
18457   In that case `f' is taken to be a template-name, even though there
18458   is no way of knowing for sure.
18459
18460   Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18461   name refers to a set of overloaded functions, at least one of which
18462   is a template, or an IDENTIFIER_NODE with the name of the template,
18463   if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
18464   names are looked up inside uninstantiated templates.  */
18465
18466static tree
18467cp_parser_template_name (cp_parser* parser,
18468			 bool template_keyword_p,
18469			 bool check_dependency_p,
18470			 bool is_declaration,
18471			 enum tag_types tag_type,
18472			 bool *is_identifier)
18473{
18474  tree identifier;
18475  tree decl;
18476  cp_token *token = cp_lexer_peek_token (parser->lexer);
18477
18478  /* If the next token is `operator', then we have either an
18479     operator-function-id or a conversion-function-id.  */
18480  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18481    {
18482      /* We don't know whether we're looking at an
18483	 operator-function-id or a conversion-function-id.  */
18484      cp_parser_parse_tentatively (parser);
18485      /* Try an operator-function-id.  */
18486      identifier = cp_parser_operator_function_id (parser);
18487      /* If that didn't work, try a conversion-function-id.  */
18488      if (!cp_parser_parse_definitely (parser))
18489	{
18490	  cp_parser_error (parser, "expected template-name");
18491	  return error_mark_node;
18492	}
18493    }
18494  /* Look for the identifier.  */
18495  else
18496    identifier = cp_parser_identifier (parser);
18497
18498  /* If we didn't find an identifier, we don't have a template-id.  */
18499  if (identifier == error_mark_node)
18500    return error_mark_node;
18501
18502  /* If the name immediately followed the `template' keyword, then it
18503     is a template-name.  However, if the next token is not `<', then
18504     we do not treat it as a template-name, since it is not being used
18505     as part of a template-id.  This enables us to handle constructs
18506     like:
18507
18508       template <typename T> struct S { S(); };
18509       template <typename T> S<T>::S();
18510
18511     correctly.  We would treat `S' as a template -- if it were `S<T>'
18512     -- but we do not if there is no `<'.  */
18513
18514  if (processing_template_decl
18515      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18516    {
18517      /* In a declaration, in a dependent context, we pretend that the
18518	 "template" keyword was present in order to improve error
18519	 recovery.  For example, given:
18520
18521	   template <typename T> void f(T::X<int>);
18522
18523	 we want to treat "X<int>" as a template-id.  */
18524      if (is_declaration
18525	  && !template_keyword_p
18526	  && parser->scope && TYPE_P (parser->scope)
18527	  && check_dependency_p
18528	  && dependent_scope_p (parser->scope)
18529	  /* Do not do this for dtors (or ctors), since they never
18530	     need the template keyword before their name.  */
18531	  && !constructor_name_p (identifier, parser->scope))
18532	{
18533	  cp_token_position start = 0;
18534
18535	  /* Explain what went wrong.  */
18536	  error_at (token->location, "non-template %qD used as template",
18537		    identifier);
18538	  inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18539		  parser->scope, identifier);
18540	  /* If parsing tentatively, find the location of the "<" token.  */
18541	  if (cp_parser_simulate_error (parser))
18542	    start = cp_lexer_token_position (parser->lexer, true);
18543	  /* Parse the template arguments so that we can issue error
18544	     messages about them.  */
18545	  cp_lexer_consume_token (parser->lexer);
18546	  cp_parser_enclosed_template_argument_list (parser);
18547	  /* Skip tokens until we find a good place from which to
18548	     continue parsing.  */
18549	  cp_parser_skip_to_closing_parenthesis (parser,
18550						 /*recovering=*/true,
18551						 /*or_comma=*/true,
18552						 /*consume_paren=*/false);
18553	  /* If parsing tentatively, permanently remove the
18554	     template argument list.  That will prevent duplicate
18555	     error messages from being issued about the missing
18556	     "template" keyword.  */
18557	  if (start)
18558	    cp_lexer_purge_tokens_after (parser->lexer, start);
18559	  if (is_identifier)
18560	    *is_identifier = true;
18561	  parser->context->object_type = NULL_TREE;
18562	  return identifier;
18563	}
18564
18565      /* If the "template" keyword is present, then there is generally
18566	 no point in doing name-lookup, so we just return IDENTIFIER.
18567	 But, if the qualifying scope is non-dependent then we can
18568	 (and must) do name-lookup normally.  */
18569      if (template_keyword_p)
18570	{
18571	  tree scope = (parser->scope ? parser->scope
18572			: parser->context->object_type);
18573	  if (scope && TYPE_P (scope)
18574	      && (!CLASS_TYPE_P (scope)
18575		  || (check_dependency_p && dependent_scope_p (scope))))
18576	    {
18577	      /* We're optimizing away the call to cp_parser_lookup_name, but
18578		 we still need to do this.  */
18579	      parser->object_scope = parser->context->object_type;
18580	      parser->context->object_type = NULL_TREE;
18581	      return identifier;
18582	    }
18583	}
18584    }
18585
18586  /* cp_parser_lookup_name clears OBJECT_TYPE.  */
18587  tree scope = (parser->scope ? parser->scope
18588		: parser->context->object_type);
18589
18590  /* Look up the name.  */
18591  decl = cp_parser_lookup_name (parser, identifier,
18592				tag_type,
18593				/*is_template=*/true,
18594				/*is_namespace=*/false,
18595				check_dependency_p,
18596				/*ambiguous_decls=*/NULL,
18597				token->location);
18598
18599  decl = strip_using_decl (decl);
18600
18601  /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18602    template-argument-list if it follows a name that is not a
18603    conversion-function-id and
18604    - that follows the keyword template or a ~ after a nested-name-specifier or
18605    in a class member access expression, or
18606    - for which name lookup finds the injected-class-name of a class template
18607    or finds any declaration of a template, or
18608    - that is an unqualified name for which name lookup either finds one or
18609    more functions or finds nothing, or
18610    - that is a terminal name in a using-declarator (9.9), in a declarator-id
18611    (9.3.4), or in a type-only context other than a nested-name-specifier
18612    (13.8).  */
18613
18614  /* If DECL is a template, then the name was a template-name.  */
18615  if (TREE_CODE (decl) == TEMPLATE_DECL)
18616    {
18617      if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
18618	  && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
18619	{
18620	  tree d = DECL_TEMPLATE_RESULT (decl);
18621	  tree attr;
18622	  if (TREE_CODE (d) == TYPE_DECL)
18623	    attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
18624	  else
18625	    attr = DECL_ATTRIBUTES (d);
18626	  if (TREE_UNAVAILABLE (decl))
18627	    {
18628	      attr = lookup_attribute ("unavailable", attr);
18629	      error_unavailable_use (decl, attr);
18630	    }
18631	  else if (TREE_DEPRECATED (decl)
18632		   && deprecated_state != DEPRECATED_SUPPRESS)
18633	    {
18634	      attr = lookup_attribute ("deprecated", attr);
18635	      warn_deprecated_use (decl, attr);
18636	    }
18637	}
18638    }
18639  else
18640    {
18641      /* Look through an overload set for any templates.  */
18642      bool found = false;
18643
18644      for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
18645	   !found && iter; ++iter)
18646	if (TREE_CODE (*iter) == TEMPLATE_DECL)
18647	  found = true;
18648
18649      /* "an unqualified name for which name lookup either finds one or more
18650	 functions or finds nothing".  */
18651      if (!found
18652	  && (cxx_dialect > cxx17)
18653	  && !scope
18654	  && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
18655	  && tag_type == none_type)
18656	{
18657	  /* The "more functions" case.  Just use the OVERLOAD as normally.
18658	     We don't use is_overloaded_fn here to avoid considering
18659	     BASELINKs.  */
18660	  if (TREE_CODE (decl) == OVERLOAD
18661	      /* Name lookup found one function.  */
18662	      || TREE_CODE (decl) == FUNCTION_DECL
18663	      /* Name lookup found nothing.  */
18664	      || decl == error_mark_node)
18665	    found = true;
18666	}
18667
18668      /* "that follows the keyword template"..."in a type-only context" */
18669      if (!found && scope
18670	  && (template_keyword_p || tag_type != none_type)
18671	  && dependentish_scope_p (scope)
18672	  && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18673	found = true;
18674
18675      if (!found)
18676	{
18677	  /* The name does not name a template.  */
18678	  cp_parser_error (parser, "expected template-name");
18679	  return error_mark_node;
18680	}
18681      else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
18682	       || TREE_CODE (decl) == USING_DECL
18683	       /* cp_parser_template_id can only handle some TYPE_DECLs.  */
18684	       || (TREE_CODE (decl) == TYPE_DECL
18685		   && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
18686	/* Repeat the lookup at instantiation time.  */
18687	decl = identifier;
18688    }
18689
18690  return decl;
18691}
18692
18693/* Parse a template-argument-list.
18694
18695   template-argument-list:
18696     template-argument ... [opt]
18697     template-argument-list , template-argument ... [opt]
18698
18699   Returns a TREE_VEC containing the arguments.  */
18700
18701static tree
18702cp_parser_template_argument_list (cp_parser* parser)
18703{
18704  bool saved_in_template_argument_list_p;
18705  bool saved_ice_p;
18706  bool saved_non_ice_p;
18707
18708  /* Don't create location wrapper nodes within a template-argument-list.  */
18709  auto_suppress_location_wrappers sentinel;
18710
18711  saved_in_template_argument_list_p = parser->in_template_argument_list_p;
18712  parser->in_template_argument_list_p = true;
18713  /* Even if the template-id appears in an integral
18714     constant-expression, the contents of the argument list do
18715     not.  */
18716  saved_ice_p = parser->integral_constant_expression_p;
18717  parser->integral_constant_expression_p = false;
18718  saved_non_ice_p = parser->non_integral_constant_expression_p;
18719  parser->non_integral_constant_expression_p = false;
18720
18721  /* Parse the arguments.  */
18722  auto_vec<tree, 10> args;
18723  do
18724    {
18725      if (!args.is_empty ())
18726	/* Consume the comma.  */
18727	cp_lexer_consume_token (parser->lexer);
18728
18729      /* Parse the template-argument.  */
18730      tree argument = cp_parser_template_argument (parser);
18731
18732      /* If the next token is an ellipsis, we're expanding a template
18733         argument pack. */
18734      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18735        {
18736	  if (argument == error_mark_node)
18737	    {
18738	      cp_token *token = cp_lexer_peek_token (parser->lexer);
18739	      error_at (token->location,
18740			"expected parameter pack before %<...%>");
18741	    }
18742          /* Consume the `...' token. */
18743          cp_lexer_consume_token (parser->lexer);
18744
18745          /* Make the argument into a TYPE_PACK_EXPANSION or
18746             EXPR_PACK_EXPANSION. */
18747          argument = make_pack_expansion (argument);
18748        }
18749
18750      args.safe_push (argument);
18751    }
18752  while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18753
18754  int n_args = args.length ();
18755  tree vec = make_tree_vec (n_args);
18756
18757  for (int i = 0; i < n_args; i++)
18758    TREE_VEC_ELT (vec, i) = args[i];
18759
18760  parser->non_integral_constant_expression_p = saved_non_ice_p;
18761  parser->integral_constant_expression_p = saved_ice_p;
18762  parser->in_template_argument_list_p = saved_in_template_argument_list_p;
18763  if (CHECKING_P)
18764    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
18765  return vec;
18766}
18767
18768/* Parse a template-argument.
18769
18770   template-argument:
18771     assignment-expression
18772     type-id
18773     id-expression
18774
18775   The representation is that of an assignment-expression, type-id, or
18776   id-expression -- except that the qualified id-expression is
18777   evaluated, so that the value returned is either a DECL or an
18778   OVERLOAD.
18779
18780   Although the standard says "assignment-expression", it forbids
18781   throw-expressions or assignments in the template argument.
18782   Therefore, we use "conditional-expression" instead.  */
18783
18784static tree
18785cp_parser_template_argument (cp_parser* parser)
18786{
18787  tree argument;
18788  bool template_p;
18789  bool address_p;
18790  bool maybe_type_id = false;
18791  cp_token *token = NULL, *argument_start_token = NULL;
18792  location_t loc = 0;
18793  cp_id_kind idk;
18794
18795  /* There's really no way to know what we're looking at, so we just
18796     try each alternative in order.
18797
18798       [temp.arg]
18799
18800       In a template-argument, an ambiguity between a type-id and an
18801       expression is resolved to a type-id, regardless of the form of
18802       the corresponding template-parameter.
18803
18804     Therefore, we try a type-id first.  */
18805  cp_parser_parse_tentatively (parser);
18806  argument = cp_parser_template_type_arg (parser);
18807  /* If there was no error parsing the type-id but the next token is a
18808     '>>', our behavior depends on which dialect of C++ we're
18809     parsing. In C++98, we probably found a typo for '> >'. But there
18810     are type-id which are also valid expressions. For instance:
18811
18812     struct X { int operator >> (int); };
18813     template <int V> struct Foo {};
18814     Foo<X () >> 5> r;
18815
18816     Here 'X()' is a valid type-id of a function type, but the user just
18817     wanted to write the expression "X() >> 5". Thus, we remember that we
18818     found a valid type-id, but we still try to parse the argument as an
18819     expression to see what happens.
18820
18821     In C++0x, the '>>' will be considered two separate '>'
18822     tokens.  */
18823  if (!cp_parser_error_occurred (parser)
18824      && cxx_dialect == cxx98
18825      && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18826    {
18827      maybe_type_id = true;
18828      cp_parser_abort_tentative_parse (parser);
18829    }
18830  else
18831    {
18832      /* If the next token isn't a `,' or a `>', then this argument wasn't
18833      really finished. This means that the argument is not a valid
18834      type-id.  */
18835      if (!cp_parser_next_token_ends_template_argument_p (parser))
18836	cp_parser_error (parser, "expected template-argument");
18837      /* If that worked, we're done.  */
18838      if (cp_parser_parse_definitely (parser))
18839	return argument;
18840    }
18841  /* We're still not sure what the argument will be.  */
18842  cp_parser_parse_tentatively (parser);
18843  /* Try a template.  */
18844  argument_start_token = cp_lexer_peek_token (parser->lexer);
18845  argument = cp_parser_id_expression (parser,
18846				      /*template_keyword_p=*/false,
18847				      /*check_dependency_p=*/true,
18848				      &template_p,
18849				      /*declarator_p=*/false,
18850				      /*optional_p=*/false);
18851  /* If the next token isn't a `,' or a `>', then this argument wasn't
18852     really finished.  */
18853  if (!cp_parser_next_token_ends_template_argument_p (parser))
18854    cp_parser_error (parser, "expected template-argument");
18855  if (!cp_parser_error_occurred (parser))
18856    {
18857      /* Figure out what is being referred to.  If the id-expression
18858	 was for a class template specialization, then we will have a
18859	 TYPE_DECL at this point.  There is no need to do name lookup
18860	 at this point in that case.  */
18861      if (TREE_CODE (argument) != TYPE_DECL)
18862	argument = cp_parser_lookup_name (parser, argument,
18863					  none_type,
18864					  /*is_template=*/template_p,
18865					  /*is_namespace=*/false,
18866					  /*check_dependency=*/true,
18867					  /*ambiguous_decls=*/NULL,
18868					  argument_start_token->location);
18869      if (TREE_CODE (argument) != TEMPLATE_DECL
18870	       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
18871	cp_parser_error (parser, "expected template-name");
18872    }
18873  if (cp_parser_parse_definitely (parser))
18874    {
18875      if (TREE_UNAVAILABLE (argument))
18876	error_unavailable_use (argument, NULL_TREE);
18877      else if (TREE_DEPRECATED (argument))
18878	warn_deprecated_use (argument, NULL_TREE);
18879      return argument;
18880    }
18881  /* It must be a non-type argument.  In C++17 any constant-expression is
18882     allowed.  */
18883  if (cxx_dialect > cxx14)
18884    goto general_expr;
18885
18886  /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
18887
18888     -- an integral constant-expression of integral or enumeration
18889	type; or
18890
18891     -- the name of a non-type template-parameter; or
18892
18893     -- the name of an object or function with external linkage...
18894
18895     -- the address of an object or function with external linkage...
18896
18897     -- a pointer to member...  */
18898  /* Look for a non-type template parameter.  */
18899  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18900    {
18901      cp_parser_parse_tentatively (parser);
18902      argument = cp_parser_primary_expression (parser,
18903					       /*address_p=*/false,
18904					       /*cast_p=*/false,
18905					       /*template_arg_p=*/true,
18906					       &idk);
18907      if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
18908	  || !cp_parser_next_token_ends_template_argument_p (parser))
18909	cp_parser_simulate_error (parser);
18910      if (cp_parser_parse_definitely (parser))
18911	return argument;
18912    }
18913
18914  /* If the next token is "&", the argument must be the address of an
18915     object or function with external linkage.  */
18916  address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
18917  if (address_p)
18918    {
18919      loc = cp_lexer_peek_token (parser->lexer)->location;
18920      cp_lexer_consume_token (parser->lexer);
18921    }
18922  /* See if we might have an id-expression.  */
18923  token = cp_lexer_peek_token (parser->lexer);
18924  if (token->type == CPP_NAME
18925      || token->keyword == RID_OPERATOR
18926      || token->type == CPP_SCOPE
18927      || token->type == CPP_TEMPLATE_ID
18928      || token->type == CPP_NESTED_NAME_SPECIFIER)
18929    {
18930      cp_parser_parse_tentatively (parser);
18931      argument = cp_parser_primary_expression (parser,
18932					       address_p,
18933					       /*cast_p=*/false,
18934					       /*template_arg_p=*/true,
18935					       &idk);
18936      if (cp_parser_error_occurred (parser)
18937	  || !cp_parser_next_token_ends_template_argument_p (parser))
18938	cp_parser_abort_tentative_parse (parser);
18939      else
18940	{
18941	  tree probe;
18942
18943	  if (INDIRECT_REF_P (argument))
18944	    {
18945	      /* Strip the dereference temporarily.  */
18946	      gcc_assert (REFERENCE_REF_P (argument));
18947	      argument = TREE_OPERAND (argument, 0);
18948	    }
18949
18950	  /* If we're in a template, we represent a qualified-id referring
18951	     to a static data member as a SCOPE_REF even if the scope isn't
18952	     dependent so that we can check access control later.  */
18953	  probe = argument;
18954	  if (TREE_CODE (probe) == SCOPE_REF)
18955	    probe = TREE_OPERAND (probe, 1);
18956	  if (VAR_P (probe))
18957	    {
18958	      /* A variable without external linkage might still be a
18959		 valid constant-expression, so no error is issued here
18960		 if the external-linkage check fails.  */
18961	      if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
18962		cp_parser_simulate_error (parser);
18963	    }
18964	  else if (is_overloaded_fn (argument))
18965	    /* All overloaded functions are allowed; if the external
18966	       linkage test does not pass, an error will be issued
18967	       later.  */
18968	    ;
18969	  else if (address_p
18970		   && (TREE_CODE (argument) == OFFSET_REF
18971		       || TREE_CODE (argument) == SCOPE_REF))
18972	    /* A pointer-to-member.  */
18973	    ;
18974	  else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
18975	    ;
18976	  else
18977	    cp_parser_simulate_error (parser);
18978
18979	  if (cp_parser_parse_definitely (parser))
18980	    {
18981	      if (address_p)
18982		argument = build_x_unary_op (loc, ADDR_EXPR, argument,
18983					     NULL_TREE, tf_warning_or_error);
18984	      else
18985		argument = convert_from_reference (argument);
18986	      return argument;
18987	    }
18988	}
18989    }
18990  /* If the argument started with "&", there are no other valid
18991     alternatives at this point.  */
18992  if (address_p)
18993    {
18994      cp_parser_error (parser, "invalid non-type template argument");
18995      return error_mark_node;
18996    }
18997
18998 general_expr:
18999  /* If the argument wasn't successfully parsed as a type-id followed
19000     by '>>', the argument can only be a constant expression now.
19001     Otherwise, we try parsing the constant-expression tentatively,
19002     because the argument could really be a type-id.  */
19003  if (maybe_type_id)
19004    cp_parser_parse_tentatively (parser);
19005
19006  if (cxx_dialect <= cxx14)
19007    argument = cp_parser_constant_expression (parser);
19008  else
19009    {
19010      /* In C++20, we can encounter a braced-init-list.  */
19011      if (cxx_dialect >= cxx20
19012	  && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19013	{
19014	  bool expr_non_constant_p;
19015	  return cp_parser_braced_list (parser, &expr_non_constant_p);
19016	}
19017
19018      /* With C++17 generalized non-type template arguments we need to handle
19019	 lvalue constant expressions, too.  */
19020      argument = cp_parser_assignment_expression (parser);
19021      require_potential_constant_expression (argument);
19022    }
19023
19024  if (!maybe_type_id)
19025    return argument;
19026  if (!cp_parser_next_token_ends_template_argument_p (parser))
19027    cp_parser_error (parser, "expected template-argument");
19028  if (cp_parser_parse_definitely (parser))
19029    return argument;
19030  /* We did our best to parse the argument as a non type-id, but that
19031     was the only alternative that matched (albeit with a '>' after
19032     it). We can assume it's just a typo from the user, and a
19033     diagnostic will then be issued.  */
19034  return cp_parser_template_type_arg (parser);
19035}
19036
19037/* Parse an explicit-instantiation.
19038
19039   explicit-instantiation:
19040     template declaration
19041
19042   Although the standard says `declaration', what it really means is:
19043
19044   explicit-instantiation:
19045     template decl-specifier-seq [opt] declarator [opt] ;
19046
19047   Things like `template int S<int>::i = 5, int S<double>::j;' are not
19048   supposed to be allowed.  A defect report has been filed about this
19049   issue.
19050
19051   GNU Extension:
19052
19053   explicit-instantiation:
19054     storage-class-specifier template
19055       decl-specifier-seq [opt] declarator [opt] ;
19056     function-specifier template
19057       decl-specifier-seq [opt] declarator [opt] ;  */
19058
19059static void
19060cp_parser_explicit_instantiation (cp_parser* parser)
19061{
19062  int declares_class_or_enum;
19063  cp_decl_specifier_seq decl_specifiers;
19064  tree extension_specifier = NULL_TREE;
19065
19066  timevar_push (TV_TEMPLATE_INST);
19067
19068  /* Look for an (optional) storage-class-specifier or
19069     function-specifier.  */
19070  if (cp_parser_allow_gnu_extensions_p (parser))
19071    {
19072      extension_specifier
19073	= cp_parser_storage_class_specifier_opt (parser);
19074      if (!extension_specifier)
19075	extension_specifier
19076	  = cp_parser_function_specifier_opt (parser,
19077					      /*decl_specs=*/NULL);
19078    }
19079
19080  /* Look for the `template' keyword.  */
19081  cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19082  /* Let the front end know that we are processing an explicit
19083     instantiation.  */
19084  begin_explicit_instantiation ();
19085  /* [temp.explicit] says that we are supposed to ignore access
19086     control while processing explicit instantiation directives.  */
19087  push_deferring_access_checks (dk_no_check);
19088  /* Parse a decl-specifier-seq.  */
19089  cp_parser_decl_specifier_seq (parser,
19090				CP_PARSER_FLAGS_OPTIONAL,
19091				&decl_specifiers,
19092				&declares_class_or_enum);
19093
19094  cp_omp_declare_simd_data odsd;
19095  if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19096    cp_parser_handle_directive_omp_attributes (parser,
19097					       &decl_specifiers.attributes,
19098					       &odsd, true);
19099
19100  /* If there was exactly one decl-specifier, and it declared a class,
19101     and there's no declarator, then we have an explicit type
19102     instantiation.  */
19103  if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19104    {
19105      tree type = check_tag_decl (&decl_specifiers,
19106				  /*explicit_type_instantiation_p=*/true);
19107      /* Turn access control back on for names used during
19108	 template instantiation.  */
19109      pop_deferring_access_checks ();
19110      if (type)
19111	do_type_instantiation (type, extension_specifier,
19112			       /*complain=*/tf_error);
19113    }
19114  else
19115    {
19116      cp_declarator *declarator;
19117      tree decl;
19118
19119      /* Parse the declarator.  */
19120      declarator
19121	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19122				CP_PARSER_FLAGS_NONE,
19123				/*ctor_dtor_or_conv_p=*/NULL,
19124				/*parenthesized_p=*/NULL,
19125				/*member_p=*/false,
19126				/*friend_p=*/false,
19127				/*static_p=*/false);
19128      if (declares_class_or_enum & 2)
19129	cp_parser_check_for_definition_in_return_type (declarator,
19130						       decl_specifiers.type,
19131						       decl_specifiers.locations[ds_type_spec]);
19132      if (declarator != cp_error_declarator)
19133	{
19134	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19135	    permerror (decl_specifiers.locations[ds_inline],
19136		       "explicit instantiation shall not use"
19137		       " %<inline%> specifier");
19138	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19139	    permerror (decl_specifiers.locations[ds_constexpr],
19140		       "explicit instantiation shall not use"
19141		       " %<constexpr%> specifier");
19142	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19143	    permerror (decl_specifiers.locations[ds_consteval],
19144		       "explicit instantiation shall not use"
19145		       " %<consteval%> specifier");
19146
19147	  decl = grokdeclarator (declarator, &decl_specifiers,
19148				 NORMAL, 0, &decl_specifiers.attributes);
19149	  /* Turn access control back on for names used during
19150	     template instantiation.  */
19151	  pop_deferring_access_checks ();
19152	  /* Do the explicit instantiation.  */
19153	  do_decl_instantiation (decl, extension_specifier);
19154	}
19155      else
19156	{
19157	  pop_deferring_access_checks ();
19158	  /* Skip the body of the explicit instantiation.  */
19159	  cp_parser_skip_to_end_of_statement (parser);
19160	}
19161    }
19162  /* We're done with the instantiation.  */
19163  end_explicit_instantiation ();
19164
19165  cp_parser_consume_semicolon_at_end_of_statement (parser);
19166
19167  timevar_pop (TV_TEMPLATE_INST);
19168
19169  cp_finalize_omp_declare_simd (parser, &odsd);
19170}
19171
19172/* Parse an explicit-specialization.
19173
19174   explicit-specialization:
19175     template < > declaration
19176
19177   Although the standard says `declaration', what it really means is:
19178
19179   explicit-specialization:
19180     template <> decl-specifier [opt] init-declarator [opt] ;
19181     template <> function-definition
19182     template <> explicit-specialization
19183     template <> template-declaration  */
19184
19185static void
19186cp_parser_explicit_specialization (cp_parser* parser)
19187{
19188  cp_token *token = cp_lexer_peek_token (parser->lexer);
19189
19190  /* Look for the `template' keyword.  */
19191  cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19192  /* Look for the `<'.  */
19193  cp_parser_require (parser, CPP_LESS, RT_LESS);
19194  /* Look for the `>'.  */
19195  cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19196  /* We have processed another parameter list.  */
19197  ++parser->num_template_parameter_lists;
19198
19199  /* [temp]
19200
19201     A template ... explicit specialization ... shall not have C
19202     linkage.  */
19203  bool need_lang_pop = current_lang_name == lang_name_c;
19204  if (need_lang_pop)
19205    {
19206      error_at (token->location, "template specialization with C linkage");
19207      maybe_show_extern_c_location ();
19208
19209      /* Give it C++ linkage to avoid confusing other parts of the
19210	 front end.  */
19211      push_lang_context (lang_name_cplusplus);
19212    }
19213
19214  /* Let the front end know that we are beginning a specialization.  */
19215  if (begin_specialization ())
19216    {
19217      /* If the next keyword is `template', we need to figure out
19218	 whether or not we're looking a template-declaration.  */
19219      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19220	{
19221	  if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19222	      && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19223	    cp_parser_template_declaration_after_export (parser,
19224							 /*member_p=*/false);
19225	  else
19226	    cp_parser_explicit_specialization (parser);
19227	}
19228      else
19229	/* Parse the dependent declaration.  */
19230	cp_parser_single_declaration (parser,
19231				      /*checks=*/NULL,
19232				      /*member_p=*/false,
19233				      /*explicit_specialization_p=*/true,
19234				      /*friend_p=*/NULL);
19235    }
19236
19237  /* We're done with the specialization.  */
19238  end_specialization ();
19239
19240  /* For the erroneous case of a template with C linkage, we pushed an
19241     implicit C++ linkage scope; exit that scope now.  */
19242  if (need_lang_pop)
19243    pop_lang_context ();
19244
19245  /* We're done with this parameter list.  */
19246  --parser->num_template_parameter_lists;
19247}
19248
19249/* Preserve the attributes across a garbage collect (by making it a GC
19250   root), which can occur when parsing a member function.  */
19251
19252static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19253
19254/* Parse a type-specifier.
19255
19256   type-specifier:
19257     simple-type-specifier
19258     class-specifier
19259     enum-specifier
19260     elaborated-type-specifier
19261     cv-qualifier
19262
19263   GNU Extension:
19264
19265   type-specifier:
19266     __complex__
19267
19268   Returns a representation of the type-specifier.  For a
19269   class-specifier, enum-specifier, or elaborated-type-specifier, a
19270   TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19271
19272   The parser flags FLAGS is used to control type-specifier parsing.
19273
19274   If IS_DECLARATION is TRUE, then this type-specifier is appearing
19275   in a decl-specifier-seq.
19276
19277   If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19278   class-specifier, enum-specifier, or elaborated-type-specifier, then
19279   *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
19280   if a type is declared; 2 if it is defined.  Otherwise, it is set to
19281   zero.
19282
19283   If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19284   cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
19285   is set to FALSE.  */
19286
19287static tree
19288cp_parser_type_specifier (cp_parser* parser,
19289			  cp_parser_flags flags,
19290			  cp_decl_specifier_seq *decl_specs,
19291			  bool is_declaration,
19292			  int* declares_class_or_enum,
19293			  bool* is_cv_qualifier)
19294{
19295  tree type_spec = NULL_TREE;
19296  cp_token *token;
19297  enum rid keyword;
19298  cp_decl_spec ds = ds_last;
19299
19300  /* Assume this type-specifier does not declare a new type.  */
19301  if (declares_class_or_enum)
19302    *declares_class_or_enum = 0;
19303  /* And that it does not specify a cv-qualifier.  */
19304  if (is_cv_qualifier)
19305    *is_cv_qualifier = false;
19306  /* Peek at the next token.  */
19307  token = cp_lexer_peek_token (parser->lexer);
19308
19309  /* If we're looking at a keyword, we can use that to guide the
19310     production we choose.  */
19311  keyword = token->keyword;
19312  switch (keyword)
19313    {
19314    case RID_ENUM:
19315      if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19316	goto elaborated_type_specifier;
19317
19318      /* Look for the enum-specifier.  */
19319      type_spec = cp_parser_enum_specifier (parser);
19320      /* If that worked, we're done.  */
19321      if (type_spec)
19322	{
19323	  if (declares_class_or_enum)
19324	    *declares_class_or_enum = 2;
19325	  if (decl_specs)
19326	    cp_parser_set_decl_spec_type (decl_specs,
19327					  type_spec,
19328					  token,
19329					  /*type_definition_p=*/true);
19330	  return type_spec;
19331	}
19332      else
19333	goto elaborated_type_specifier;
19334
19335      /* Any of these indicate either a class-specifier, or an
19336	 elaborated-type-specifier.  */
19337    case RID_CLASS:
19338    case RID_STRUCT:
19339    case RID_UNION:
19340      if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19341	goto elaborated_type_specifier;
19342
19343      /* Parse tentatively so that we can back up if we don't find a
19344	 class-specifier.  */
19345      cp_parser_parse_tentatively (parser);
19346      if (decl_specs->attributes)
19347	vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19348      /* Look for the class-specifier.  */
19349      type_spec = cp_parser_class_specifier (parser);
19350      if (decl_specs->attributes)
19351	cp_parser_decl_specs_attrs->pop ();
19352      invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19353      /* If that worked, we're done.  */
19354      if (cp_parser_parse_definitely (parser))
19355	{
19356	  if (declares_class_or_enum)
19357	    *declares_class_or_enum = 2;
19358	  if (decl_specs)
19359	    cp_parser_set_decl_spec_type (decl_specs,
19360					  type_spec,
19361					  token,
19362					  /*type_definition_p=*/true);
19363	  return type_spec;
19364	}
19365
19366      /* Fall through.  */
19367    elaborated_type_specifier:
19368      /* We're declaring (not defining) a class or enum.  */
19369      if (declares_class_or_enum)
19370	*declares_class_or_enum = 1;
19371
19372      /* Fall through.  */
19373    case RID_TYPENAME:
19374      /* Look for an elaborated-type-specifier.  */
19375      type_spec
19376	= (cp_parser_elaborated_type_specifier
19377	   (parser,
19378	    decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19379	    is_declaration));
19380      if (decl_specs)
19381	cp_parser_set_decl_spec_type (decl_specs,
19382				      type_spec,
19383				      token,
19384				      /*type_definition_p=*/false);
19385      return type_spec;
19386
19387    case RID_CONST:
19388      ds = ds_const;
19389      if (is_cv_qualifier)
19390	*is_cv_qualifier = true;
19391      break;
19392
19393    case RID_VOLATILE:
19394      ds = ds_volatile;
19395      if (is_cv_qualifier)
19396	*is_cv_qualifier = true;
19397      break;
19398
19399    case RID_RESTRICT:
19400      ds = ds_restrict;
19401      if (is_cv_qualifier)
19402	*is_cv_qualifier = true;
19403      break;
19404
19405    case RID_COMPLEX:
19406      /* The `__complex__' keyword is a GNU extension.  */
19407      ds = ds_complex;
19408      break;
19409
19410    default:
19411      break;
19412    }
19413
19414  /* Handle simple keywords.  */
19415  if (ds != ds_last)
19416    {
19417      if (decl_specs)
19418	{
19419	  set_and_check_decl_spec_loc (decl_specs, ds, token);
19420	  decl_specs->any_specifiers_p = true;
19421	}
19422      return cp_lexer_consume_token (parser->lexer)->u.value;
19423    }
19424
19425  /* If we do not already have a type-specifier, assume we are looking
19426     at a simple-type-specifier.  */
19427  type_spec = cp_parser_simple_type_specifier (parser,
19428					       decl_specs,
19429					       flags);
19430
19431  /* If we didn't find a type-specifier, and a type-specifier was not
19432     optional in this context, issue an error message.  */
19433  if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19434    {
19435      cp_parser_error (parser, "expected type specifier");
19436      return error_mark_node;
19437    }
19438
19439  return type_spec;
19440}
19441
19442/* Parse a simple-type-specifier.
19443
19444   simple-type-specifier:
19445     :: [opt] nested-name-specifier [opt] type-name
19446     :: [opt] nested-name-specifier template template-id
19447     char
19448     wchar_t
19449     bool
19450     short
19451     int
19452     long
19453     signed
19454     unsigned
19455     float
19456     double
19457     void
19458
19459   C++11 Extension:
19460
19461   simple-type-specifier:
19462     auto
19463     decltype ( expression )
19464     char16_t
19465     char32_t
19466     __underlying_type ( type-id )
19467
19468   C++17 extension:
19469
19470     nested-name-specifier(opt) template-name
19471
19472   GNU Extension:
19473
19474   simple-type-specifier:
19475     __int128
19476     __typeof__ unary-expression
19477     __typeof__ ( type-id )
19478     __typeof__ ( type-id ) { initializer-list , [opt] }
19479
19480   Concepts Extension:
19481
19482   simple-type-specifier:
19483     constrained-type-specifier
19484
19485   Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
19486   appropriately updated.  */
19487
19488static tree
19489cp_parser_simple_type_specifier (cp_parser* parser,
19490				 cp_decl_specifier_seq *decl_specs,
19491				 cp_parser_flags flags)
19492{
19493  tree type = NULL_TREE;
19494  cp_token *token;
19495  int idx;
19496
19497  /* Peek at the next token.  */
19498  token = cp_lexer_peek_token (parser->lexer);
19499
19500  /* If we're looking at a keyword, things are easy.  */
19501  switch (token->keyword)
19502    {
19503    case RID_CHAR:
19504      if (decl_specs)
19505	decl_specs->explicit_char_p = true;
19506      type = char_type_node;
19507      break;
19508    case RID_CHAR8:
19509      type = char8_type_node;
19510      break;
19511    case RID_CHAR16:
19512      type = char16_type_node;
19513      break;
19514    case RID_CHAR32:
19515      type = char32_type_node;
19516      break;
19517    case RID_WCHAR:
19518      type = wchar_type_node;
19519      break;
19520    case RID_BOOL:
19521      type = boolean_type_node;
19522      break;
19523    case RID_SHORT:
19524      set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19525      type = short_integer_type_node;
19526      break;
19527    case RID_INT:
19528      if (decl_specs)
19529	decl_specs->explicit_int_p = true;
19530      type = integer_type_node;
19531      break;
19532    case RID_INT_N_0:
19533    case RID_INT_N_1:
19534    case RID_INT_N_2:
19535    case RID_INT_N_3:
19536      idx = token->keyword - RID_INT_N_0;
19537      if (! int_n_enabled_p [idx])
19538	break;
19539      if (decl_specs)
19540	{
19541	  decl_specs->explicit_intN_p = true;
19542	  decl_specs->int_n_idx = idx;
19543	  /* Check if the alternate "__intN__" form has been used instead of
19544	     "__intN".  */
19545	  if (startswith (IDENTIFIER_POINTER (token->u.value)
19546			  + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19547	    decl_specs->int_n_alt = true;
19548	}
19549      type = int_n_trees [idx].signed_type;
19550      break;
19551    case RID_LONG:
19552      if (decl_specs)
19553	set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19554      type = long_integer_type_node;
19555      break;
19556    case RID_SIGNED:
19557      set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19558      type = integer_type_node;
19559      break;
19560    case RID_UNSIGNED:
19561      set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19562      type = unsigned_type_node;
19563      break;
19564    case RID_FLOAT:
19565      type = float_type_node;
19566      break;
19567    case RID_DOUBLE:
19568      type = double_type_node;
19569      break;
19570    case RID_VOID:
19571      type = void_type_node;
19572      break;
19573
19574    case RID_AUTO:
19575      maybe_warn_cpp0x (CPP0X_AUTO);
19576      if (parser->auto_is_implicit_function_template_parm_p)
19577	{
19578	  /* The 'auto' might be the placeholder return type for a function decl
19579	     with trailing return type.  */
19580	  bool have_trailing_return_fn_decl = false;
19581
19582	  cp_parser_parse_tentatively (parser);
19583	  cp_lexer_consume_token (parser->lexer);
19584	  while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19585		 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19586		 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19587		 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19588	    {
19589	      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19590		{
19591		  cp_lexer_consume_token (parser->lexer);
19592		  cp_parser_skip_to_closing_parenthesis (parser,
19593							 /*recovering*/false,
19594							 /*or_comma*/false,
19595							 /*consume_paren*/true);
19596		  continue;
19597		}
19598
19599	      if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
19600		{
19601		  have_trailing_return_fn_decl = true;
19602		  break;
19603		}
19604
19605	      cp_lexer_consume_token (parser->lexer);
19606	    }
19607	  cp_parser_abort_tentative_parse (parser);
19608
19609	  if (have_trailing_return_fn_decl)
19610	    {
19611	      type = make_auto ();
19612	      break;
19613	    }
19614
19615	  if (cxx_dialect >= cxx14)
19616	    {
19617	      type = synthesize_implicit_template_parm (parser, NULL_TREE);
19618	      type = TREE_TYPE (type);
19619	    }
19620	  else
19621	    type = error_mark_node;
19622
19623	  if (current_class_type && LAMBDA_TYPE_P (current_class_type))
19624	    {
19625	      if (cxx_dialect < cxx14)
19626		error_at (token->location,
19627			 "use of %<auto%> in lambda parameter declaration "
19628			 "only available with "
19629			 "%<-std=c++14%> or %<-std=gnu++14%>");
19630	    }
19631	  else if (cxx_dialect < cxx14)
19632	    error_at (token->location,
19633		     "use of %<auto%> in parameter declaration "
19634		     "only available with "
19635		     "%<-std=c++14%> or %<-std=gnu++14%>");
19636	  else if (!flag_concepts)
19637	    pedwarn (token->location, 0,
19638		     "use of %<auto%> in parameter declaration "
19639		     "only available with %<-std=c++20%> or %<-fconcepts%>");
19640	}
19641      else
19642	type = make_auto ();
19643      break;
19644
19645    case RID_DECLTYPE:
19646      /* Since DR 743, decltype can either be a simple-type-specifier by
19647	 itself or begin a nested-name-specifier.  Parsing it will replace
19648	 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19649	 handling below decide what to do.  */
19650      cp_parser_decltype (parser);
19651      cp_lexer_set_token_position (parser->lexer, token);
19652      break;
19653
19654    case RID_TYPEOF:
19655      /* Consume the `typeof' token.  */
19656      cp_lexer_consume_token (parser->lexer);
19657      /* Parse the operand to `typeof'.  */
19658      type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
19659      /* If it is not already a TYPE, take its type.  */
19660      if (!TYPE_P (type))
19661	type = finish_typeof (type);
19662
19663      if (decl_specs)
19664	cp_parser_set_decl_spec_type (decl_specs, type,
19665				      token,
19666				      /*type_definition_p=*/false);
19667
19668      return type;
19669
19670    case RID_UNDERLYING_TYPE:
19671      type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
19672      if (decl_specs)
19673	cp_parser_set_decl_spec_type (decl_specs, type,
19674				      token,
19675				      /*type_definition_p=*/false);
19676
19677      return type;
19678
19679    case RID_BASES:
19680    case RID_DIRECT_BASES:
19681      type = cp_parser_trait_expr (parser, token->keyword);
19682      if (decl_specs)
19683       cp_parser_set_decl_spec_type (decl_specs, type,
19684                                     token,
19685                                     /*type_definition_p=*/false);
19686      return type;
19687    default:
19688      break;
19689    }
19690
19691  /* If token is an already-parsed decltype not followed by ::,
19692     it's a simple-type-specifier.  */
19693  if (token->type == CPP_DECLTYPE
19694      && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
19695    {
19696      type = saved_checks_value (token->u.tree_check_value);
19697      if (decl_specs)
19698	{
19699	  cp_parser_set_decl_spec_type (decl_specs, type,
19700					token,
19701					/*type_definition_p=*/false);
19702	  /* Remember that we are handling a decltype in order to
19703	     implement the resolution of DR 1510 when the argument
19704	     isn't instantiation dependent.  */
19705	  decl_specs->decltype_p = true;
19706	}
19707      cp_lexer_consume_token (parser->lexer);
19708      return type;
19709    }
19710
19711  /* If the type-specifier was for a built-in type, we're done.  */
19712  if (type)
19713    {
19714      /* Record the type.  */
19715      if (decl_specs
19716	  && (token->keyword != RID_SIGNED
19717	      && token->keyword != RID_UNSIGNED
19718	      && token->keyword != RID_SHORT
19719	      && token->keyword != RID_LONG))
19720	cp_parser_set_decl_spec_type (decl_specs,
19721				      type,
19722				      token,
19723				      /*type_definition_p=*/false);
19724      if (decl_specs)
19725	decl_specs->any_specifiers_p = true;
19726
19727      /* Consume the token.  */
19728      cp_lexer_consume_token (parser->lexer);
19729
19730      if (type == error_mark_node)
19731	return error_mark_node;
19732
19733      /* There is no valid C++ program where a non-template type is
19734	 followed by a "<".  That usually indicates that the user thought
19735	 that the type was a template.  */
19736      cp_parser_check_for_invalid_template_id (parser, type, none_type,
19737					       token->location);
19738
19739      return TYPE_NAME (type);
19740    }
19741
19742  /* The type-specifier must be a user-defined type.  */
19743  if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
19744    {
19745      bool qualified_p;
19746      bool global_p;
19747      const bool typename_p = (cxx_dialect >= cxx20
19748			       && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
19749
19750      /* Don't gobble tokens or issue error messages if this is an
19751	 optional type-specifier.  */
19752      if (flags & CP_PARSER_FLAGS_OPTIONAL)
19753	cp_parser_parse_tentatively (parser);
19754
19755      /* Remember current tentative parsing state -- if we know we need
19756	 a type, we can give better diagnostics here.  */
19757      bool tent = cp_parser_parsing_tentatively (parser);
19758
19759      token = cp_lexer_peek_token (parser->lexer);
19760
19761      /* Look for the optional `::' operator.  */
19762      global_p
19763	= (cp_parser_global_scope_opt (parser,
19764				       /*current_scope_valid_p=*/false)
19765	   != NULL_TREE);
19766      /* Look for the nested-name specifier.  */
19767      qualified_p
19768	= (cp_parser_nested_name_specifier_opt (parser,
19769						/*typename_keyword_p=*/false,
19770						/*check_dependency_p=*/true,
19771						/*type_p=*/false,
19772						/*is_declaration=*/false)
19773	   != NULL_TREE);
19774      /* If we have seen a nested-name-specifier, and the next token
19775	 is `template', then we are using the template-id production.  */
19776      if (parser->scope
19777	  && cp_parser_optional_template_keyword (parser))
19778	{
19779	  /* Look for the template-id.  */
19780	  type = cp_parser_template_id (parser,
19781					/*template_keyword_p=*/true,
19782					/*check_dependency_p=*/true,
19783					none_type,
19784					/*is_declaration=*/false);
19785	  /* If the template-id did not name a type, we are out of
19786	     luck.  */
19787	  if (TREE_CODE (type) != TYPE_DECL)
19788	    {
19789	      /* ...unless we pretend we have seen 'typename'.  */
19790	      if (typename_p)
19791		type = cp_parser_make_typename_type (parser, type,
19792						     token->location);
19793	      else
19794		{
19795		  cp_parser_error (parser, "expected template-id for type");
19796		  type = error_mark_node;
19797		}
19798	    }
19799	}
19800      /* DR 1812: A < following a qualified-id in a typename-specifier
19801	 could safely be assumed to begin a template argument list, so
19802	 the template keyword should be optional.  */
19803      else if (parser->scope
19804	       && qualified_p
19805	       && typename_p
19806	       && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
19807	{
19808	  cp_parser_parse_tentatively (parser);
19809
19810	  type = cp_parser_template_id (parser,
19811					/*template_keyword_p=*/true,
19812					/*check_dependency_p=*/true,
19813					none_type,
19814					/*is_declaration=*/false);
19815	  /* This is handled below, so back off.  */
19816	  if (type && concept_check_p (type))
19817	    cp_parser_simulate_error (parser);
19818
19819	  if (!cp_parser_parse_definitely (parser))
19820	    type = NULL_TREE;
19821	  else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
19822	    type = make_typename_type (parser->scope, type, typename_type,
19823				       /*complain=*/tf_error);
19824	  else if (TREE_CODE (type) != TYPE_DECL)
19825	    type = NULL_TREE;
19826	}
19827
19828      /* Otherwise, look for a type-name.  */
19829      if (!type)
19830	{
19831	  if (cxx_dialect >= cxx17)
19832	    cp_parser_parse_tentatively (parser);
19833
19834	  type = cp_parser_type_name (parser, (qualified_p && typename_p));
19835
19836	  if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
19837	    type = NULL_TREE;
19838	}
19839
19840      if (!type && flag_concepts && decl_specs)
19841	{
19842	  /* Try for a type-constraint with template arguments.  We check
19843	     decl_specs here to avoid trying this for a functional cast.  */
19844
19845	  cp_parser_parse_tentatively (parser);
19846
19847	  type = cp_parser_template_id (parser,
19848					/*template_keyword_p=*/false,
19849					/*check_dependency_p=*/true,
19850					none_type,
19851					/*is_declaration=*/false);
19852	  if (type && concept_check_p (type))
19853	    {
19854	      location_t loc = EXPR_LOCATION (type);
19855	      type = cp_parser_placeholder_type_specifier (parser, loc,
19856							   type, tent);
19857	      if (tent && type == error_mark_node)
19858		/* Perhaps it's a concept-check expression.  */
19859		cp_parser_simulate_error (parser);
19860	    }
19861	  else
19862	    cp_parser_simulate_error (parser);
19863
19864	  if (!cp_parser_parse_definitely (parser))
19865	    type = NULL_TREE;
19866	}
19867
19868      if (!type && cxx_dialect >= cxx17)
19869	{
19870	  /* Try class template argument deduction or type-constraint without
19871	     template arguments.  */
19872	  tree name = cp_parser_identifier (parser);
19873	  if (name && TREE_CODE (name) == IDENTIFIER_NODE
19874	      && parser->scope != error_mark_node)
19875	    {
19876	      location_t loc
19877		= cp_lexer_previous_token (parser->lexer)->location;
19878	      tree tmpl = cp_parser_lookup_name (parser, name,
19879						 none_type,
19880						 /*is_template=*/false,
19881						 /*is_namespace=*/false,
19882						 /*check_dependency=*/true,
19883						 /*ambiguous_decls=*/NULL,
19884						 token->location);
19885	      if (tmpl && tmpl != error_mark_node
19886		  && ctad_template_p (tmpl))
19887		type = make_template_placeholder (tmpl);
19888	      else if (flag_concepts && tmpl && concept_definition_p (tmpl))
19889		type = cp_parser_placeholder_type_specifier (parser, loc,
19890							     tmpl, tent);
19891	      else
19892		{
19893		  type = error_mark_node;
19894		  if (!cp_parser_simulate_error (parser))
19895		    cp_parser_name_lookup_error (parser, name, tmpl,
19896						 NLE_TYPE, token->location);
19897		}
19898	    }
19899	  else
19900	    type = error_mark_node;
19901	}
19902
19903      /* If it didn't work out, we don't have a TYPE.  */
19904      if ((flags & CP_PARSER_FLAGS_OPTIONAL)
19905	  && !cp_parser_parse_definitely (parser))
19906	type = NULL_TREE;
19907
19908      /* Keep track of all name-lookups performed in class scopes.  */
19909      if (type
19910	  && !global_p
19911	  && !qualified_p
19912	  && TREE_CODE (type) == TYPE_DECL
19913	  && identifier_p (DECL_NAME (type)))
19914	maybe_note_name_used_in_class (DECL_NAME (type), type);
19915
19916      if (type && decl_specs)
19917	cp_parser_set_decl_spec_type (decl_specs, type,
19918				      token,
19919				      /*type_definition_p=*/false);
19920    }
19921
19922  /* If we didn't get a type-name, issue an error message.  */
19923  if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19924    {
19925      cp_parser_error (parser, "expected type-name");
19926      return error_mark_node;
19927    }
19928
19929  if (type && type != error_mark_node)
19930    {
19931      /* See if TYPE is an Objective-C type, and if so, parse and
19932	 accept any protocol references following it.  Do this before
19933	 the cp_parser_check_for_invalid_template_id() call, because
19934	 Objective-C types can be followed by '<...>' which would
19935	 enclose protocol names rather than template arguments, and so
19936	 everything is fine.  */
19937      if (c_dialect_objc () && !parser->scope
19938	  && (objc_is_id (type) || objc_is_class_name (type)))
19939	{
19940	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
19941	  tree qual_type = objc_get_protocol_qualified_type (type, protos);
19942
19943	  /* Clobber the "unqualified" type previously entered into
19944	     DECL_SPECS with the new, improved protocol-qualified version.  */
19945	  if (decl_specs)
19946	    decl_specs->type = qual_type;
19947
19948	  return qual_type;
19949	}
19950
19951      /* There is no valid C++ program where a non-template type is
19952	 followed by a "<".  That usually indicates that the user
19953	 thought that the type was a template.  */
19954      cp_parser_check_for_invalid_template_id (parser, type,
19955					       none_type,
19956					       token->location);
19957    }
19958
19959  return type;
19960}
19961
19962/* Parse the remainder of a placholder-type-specifier.
19963
19964   placeholder-type-specifier:
19965     type-constraint_opt auto
19966     type-constraint_opt decltype(auto)
19967
19968  The raw form of the constraint is parsed in cp_parser_simple_type_specifier
19969  and passed as TMPL. This function converts TMPL to an actual type-constraint,
19970  parses the placeholder type, and performs some contextual syntactic analysis.
19971
19972  LOC provides the location of the template name.
19973
19974  TENTATIVE is true if the type-specifier parsing is tentative; in that case,
19975  don't give an error if TMPL isn't a valid type-constraint, as the template-id
19976  might actually be a concept-check,
19977
19978  Note that the Concepts TS allows the auto or decltype(auto) to be
19979  omitted in a constrained-type-specifier.  */
19980
19981static tree
19982cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
19983				      tree tmpl, bool tentative)
19984{
19985  if (tmpl == error_mark_node)
19986    return error_mark_node;
19987
19988  tree orig_tmpl = tmpl;
19989
19990  /* Get the arguments as written for subsequent analysis.  */
19991  tree args = NULL_TREE;
19992  if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
19993    {
19994      args = TREE_OPERAND (tmpl, 1);
19995      tmpl = TREE_OPERAND (tmpl, 0);
19996    }
19997  else
19998    /* A concept-name with no arguments can't be an expression.  */
19999    tentative = false;
20000
20001  tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20002
20003  /* Get the concept and prototype parameter for the constraint.  */
20004  tree_pair info = finish_type_constraints (tmpl, args, complain);
20005  tree con = info.first;
20006  tree proto = info.second;
20007  if (con == error_mark_node)
20008    return error_mark_node;
20009
20010  /* As per the standard, require auto or decltype(auto), except in some
20011     cases (template parameter lists, -fconcepts-ts enabled).  */
20012  cp_token *placeholder = NULL, *close_paren = NULL;
20013  if (cxx_dialect >= cxx20)
20014    {
20015      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20016	placeholder = cp_lexer_consume_token (parser->lexer);
20017      else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20018	{
20019	  placeholder = cp_lexer_consume_token (parser->lexer);
20020	  matching_parens parens;
20021	  parens.require_open (parser);
20022	  cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20023	  close_paren = parens.require_close (parser);
20024	}
20025    }
20026
20027  /* A type constraint constrains a contextually determined type or type
20028     parameter pack. However, the Concepts TS does allow concepts
20029     to introduce non-type and template template parameters.  */
20030  if (TREE_CODE (proto) != TYPE_DECL)
20031    {
20032      if (!flag_concepts_ts
20033	  || !processing_template_parmlist)
20034	{
20035	  if (!tentative)
20036	    {
20037	      error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20038	      inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20039	    }
20040	  return error_mark_node;
20041	}
20042    }
20043
20044  /* In a template parameter list, a type-parameter can be introduced
20045     by type-constraints alone.  */
20046  if (processing_template_parmlist && !placeholder)
20047    {
20048      /* In a default argument we may not be creating new parameters.  */
20049      if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20050	{
20051	  /* If this assert turns out to be false, do error() instead.  */
20052	  gcc_assert (tentative);
20053	  return error_mark_node;
20054	}
20055      return build_constrained_parameter (con, proto, args);
20056    }
20057
20058  /* Diagnose issues placeholder issues.  */
20059  if (!flag_concepts_ts
20060      && !parser->in_result_type_constraint_p
20061      && !placeholder)
20062    {
20063      if (tentative)
20064	/* Perhaps it's a concept-check expression (c++/91073).  */
20065	return error_mark_node;
20066
20067      tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20068      tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20069      error_at (input_location,
20070		"expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20071      /* Fall through. This is an error of omission.  */
20072    }
20073  else if (parser->in_result_type_constraint_p && placeholder)
20074    {
20075      /* A trailing return type only allows type-constraints.  */
20076      error_at (input_location,
20077		"unexpected placeholder in constrained result type");
20078    }
20079
20080  /* In a parameter-declaration-clause, a placeholder-type-specifier
20081     results in an invented template parameter.  */
20082  if (parser->auto_is_implicit_function_template_parm_p)
20083    {
20084      if (close_paren)
20085	{
20086	  location_t loc = make_location (placeholder->location,
20087					  placeholder->location,
20088					  close_paren->location);
20089	  error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20090	  return error_mark_node;
20091	}
20092      tree parm = build_constrained_parameter (con, proto, args);
20093      return synthesize_implicit_template_parm (parser, parm);
20094    }
20095
20096  /* Determine if the type should be deduced using template argument
20097     deduction or decltype deduction. Note that the latter is always
20098     used for type-constraints in trailing return types.  */
20099  bool decltype_p = placeholder
20100    ? placeholder->keyword == RID_DECLTYPE
20101    : parser->in_result_type_constraint_p;
20102
20103  /* Otherwise, this is the type of a variable or return type.  */
20104  if (decltype_p)
20105    return make_constrained_decltype_auto (con, args);
20106  else
20107    return make_constrained_auto (con, args);
20108}
20109
20110/* Parse a type-name.
20111
20112   type-name:
20113     class-name
20114     enum-name
20115     typedef-name
20116     simple-template-id [in c++0x]
20117
20118   enum-name:
20119     identifier
20120
20121   typedef-name:
20122     identifier
20123
20124  Concepts:
20125
20126   type-name:
20127     concept-name
20128     partial-concept-id
20129
20130   concept-name:
20131     identifier
20132
20133   Returns a TYPE_DECL for the type.  */
20134
20135static tree
20136cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20137{
20138  tree type_decl;
20139
20140  /* We can't know yet whether it is a class-name or not.  */
20141  cp_parser_parse_tentatively (parser);
20142  /* Try a class-name.  */
20143  type_decl = cp_parser_class_name (parser,
20144				    typename_keyword_p,
20145				    /*template_keyword_p=*/false,
20146				    none_type,
20147				    /*check_dependency_p=*/true,
20148				    /*class_head_p=*/false,
20149				    /*is_declaration=*/false);
20150  /* If it's not a class-name, keep looking.  */
20151  if (!cp_parser_parse_definitely (parser))
20152    {
20153      if (cxx_dialect < cxx11)
20154	/* It must be a typedef-name or an enum-name.  */
20155	return cp_parser_nonclass_name (parser);
20156
20157      cp_parser_parse_tentatively (parser);
20158      /* It is either a simple-template-id representing an
20159	 instantiation of an alias template...  */
20160      type_decl = cp_parser_template_id (parser,
20161					 /*template_keyword_p=*/false,
20162					 /*check_dependency_p=*/true,
20163					 none_type,
20164					 /*is_declaration=*/false);
20165      /* Note that this must be an instantiation of an alias template
20166	 because [temp.names]/6 says:
20167
20168	     A template-id that names an alias template specialization
20169	     is a type-name.
20170
20171	 Whereas [temp.names]/7 says:
20172
20173	     A simple-template-id that names a class template
20174	     specialization is a class-name.
20175
20176         With concepts, this could also be a partial-concept-id that
20177         declares a non-type template parameter. */
20178      if (type_decl != NULL_TREE
20179	  && TREE_CODE (type_decl) == TYPE_DECL
20180	  && TYPE_DECL_ALIAS_P (type_decl))
20181	gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20182      else
20183	cp_parser_simulate_error (parser);
20184
20185      if (!cp_parser_parse_definitely (parser))
20186	/* ... Or a typedef-name or an enum-name.  */
20187	return cp_parser_nonclass_name (parser);
20188    }
20189
20190  return type_decl;
20191}
20192
20193/* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20194   or a concept-name.
20195
20196   enum-name:
20197     identifier
20198
20199   typedef-name:
20200     identifier
20201
20202   concept-name:
20203     identifier
20204
20205   Returns a TYPE_DECL for the type.  */
20206
20207static tree
20208cp_parser_nonclass_name (cp_parser* parser)
20209{
20210  tree type_decl;
20211  tree identifier;
20212
20213  cp_token *token = cp_lexer_peek_token (parser->lexer);
20214  identifier = cp_parser_identifier (parser);
20215  if (identifier == error_mark_node)
20216    return error_mark_node;
20217
20218  /* Look up the type-name.  */
20219  type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20220
20221  type_decl = strip_using_decl (type_decl);
20222
20223  if (TREE_CODE (type_decl) != TYPE_DECL
20224      && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20225    {
20226      /* See if this is an Objective-C type.  */
20227      tree protos = cp_parser_objc_protocol_refs_opt (parser);
20228      tree type = objc_get_protocol_qualified_type (identifier, protos);
20229      if (type)
20230	type_decl = TYPE_NAME (type);
20231    }
20232
20233  /* Issue an error if we did not find a type-name.  */
20234  if (TREE_CODE (type_decl) != TYPE_DECL
20235      /* In Objective-C, we have the complication that class names are
20236	 normally type names and start declarations (eg, the
20237	 "NSObject" in "NSObject *object;"), but can be used in an
20238	 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20239	 is an expression.  So, a classname followed by a dot is not a
20240	 valid type-name.  */
20241      || (objc_is_class_name (TREE_TYPE (type_decl))
20242	  && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20243    {
20244      if (!cp_parser_simulate_error (parser))
20245	cp_parser_name_lookup_error (parser, identifier, type_decl,
20246				     NLE_TYPE, token->location);
20247      return error_mark_node;
20248    }
20249  /* Remember that the name was used in the definition of the
20250     current class so that we can check later to see if the
20251     meaning would have been different after the class was
20252     entirely defined.  */
20253  else if (type_decl != error_mark_node
20254	   && !parser->scope)
20255    maybe_note_name_used_in_class (identifier, type_decl);
20256
20257  return type_decl;
20258}
20259
20260/* Parse an elaborated-type-specifier.  Note that the grammar given
20261   here incorporates the resolution to DR68.
20262
20263   elaborated-type-specifier:
20264     class-key :: [opt] nested-name-specifier [opt] identifier
20265     class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20266     enum-key :: [opt] nested-name-specifier [opt] identifier
20267     typename :: [opt] nested-name-specifier identifier
20268     typename :: [opt] nested-name-specifier template [opt]
20269       template-id
20270
20271   GNU extension:
20272
20273   elaborated-type-specifier:
20274     class-key attributes :: [opt] nested-name-specifier [opt] identifier
20275     class-key attributes :: [opt] nested-name-specifier [opt]
20276	       template [opt] template-id
20277     enum attributes :: [opt] nested-name-specifier [opt] identifier
20278
20279   If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20280   declared `friend'.  If IS_DECLARATION is TRUE, then this
20281   elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20282   something is being declared.
20283
20284   Returns the TYPE specified.  */
20285
20286static tree
20287cp_parser_elaborated_type_specifier (cp_parser* parser,
20288				     bool is_friend,
20289				     bool is_declaration)
20290{
20291  enum tag_types tag_type;
20292  tree identifier;
20293  tree type = NULL_TREE;
20294  tree attributes = NULL_TREE;
20295  tree globalscope;
20296  cp_token *token = NULL;
20297
20298  /* For class and enum types the location of the class-key or enum-key.  */
20299  location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20300  /* For a scoped enum, the 'class' or 'struct' keyword id.  */
20301  rid scoped_key = RID_MAX;
20302
20303  /* See if we're looking at the `enum' keyword.  */
20304  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20305    {
20306      /* Consume the `enum' token.  */
20307      cp_lexer_consume_token (parser->lexer);
20308      /* Remember that it's an enumeration type.  */
20309      tag_type = enum_type;
20310      /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20311	 enums) is used here.  */
20312      cp_token *token = cp_lexer_peek_token (parser->lexer);
20313      if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20314	  || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20315	{
20316	  location_t loc = token->location;
20317	  gcc_rich_location richloc (loc);
20318	  richloc.add_range (input_location);
20319	  richloc.add_fixit_remove ();
20320	  pedwarn (&richloc, 0, "elaborated-type-specifier for "
20321		   "a scoped enum must not use the %qD keyword",
20322		   token->u.value);
20323	  /* Consume the `struct' or `class' and parse it anyway.  */
20324	  cp_lexer_consume_token (parser->lexer);
20325	  /* Create a combined location for the whole scoped-enum-key.  */
20326	  key_loc = make_location (key_loc, key_loc, loc);
20327	}
20328      else
20329	scoped_key = RID_MAX;
20330
20331      /* Parse the attributes.  */
20332      attributes = cp_parser_attributes_opt (parser);
20333    }
20334  /* Or, it might be `typename'.  */
20335  else if (cp_lexer_next_token_is_keyword (parser->lexer,
20336					   RID_TYPENAME))
20337    {
20338      /* Consume the `typename' token.  */
20339      cp_lexer_consume_token (parser->lexer);
20340      /* Remember that it's a `typename' type.  */
20341      tag_type = typename_type;
20342    }
20343  /* Otherwise it must be a class-key.  */
20344  else
20345    {
20346      key_loc = cp_lexer_peek_token (parser->lexer)->location;
20347      tag_type = cp_parser_class_key (parser);
20348      if (tag_type == none_type)
20349	return error_mark_node;
20350      /* Parse the attributes.  */
20351      attributes = cp_parser_attributes_opt (parser);
20352    }
20353
20354  /* Look for the `::' operator.  */
20355  globalscope =  cp_parser_global_scope_opt (parser,
20356					     /*current_scope_valid_p=*/false);
20357  /* Look for the nested-name-specifier.  */
20358  tree nested_name_specifier;
20359  if (tag_type == typename_type && !globalscope)
20360    {
20361      nested_name_specifier
20362	= cp_parser_nested_name_specifier (parser,
20363					   /*typename_keyword_p=*/true,
20364					   /*check_dependency_p=*/true,
20365					   /*type_p=*/true,
20366					   is_declaration);
20367      if (!nested_name_specifier)
20368	return error_mark_node;
20369    }
20370  else
20371    /* Even though `typename' is not present, the proposed resolution
20372       to Core Issue 180 says that in `class A<T>::B', `B' should be
20373       considered a type-name, even if `A<T>' is dependent.  */
20374    nested_name_specifier
20375      = cp_parser_nested_name_specifier_opt (parser,
20376					     /*typename_keyword_p=*/true,
20377					     /*check_dependency_p=*/true,
20378					     /*type_p=*/true,
20379					     is_declaration);
20380 /* For everything but enumeration types, consider a template-id.
20381    For an enumeration type, consider only a plain identifier.  */
20382  if (tag_type != enum_type)
20383    {
20384      bool template_p = false;
20385      tree decl;
20386
20387      /* Allow the `template' keyword.  */
20388      template_p = cp_parser_optional_template_keyword (parser);
20389      /* If we didn't see `template', we don't know if there's a
20390	 template-id or not.  */
20391      if (!template_p)
20392	cp_parser_parse_tentatively (parser);
20393      /* The `template' keyword must follow a nested-name-specifier.  */
20394      else if (!nested_name_specifier && !globalscope)
20395	{
20396	  cp_parser_error (parser, "%<template%> must follow a nested-"
20397			   "name-specifier");
20398	  return error_mark_node;
20399	}
20400
20401      /* Parse the template-id.  */
20402      token = cp_lexer_peek_token (parser->lexer);
20403      decl = cp_parser_template_id (parser, template_p,
20404				    /*check_dependency_p=*/true,
20405				    tag_type,
20406				    is_declaration);
20407      /* If we didn't find a template-id, look for an ordinary
20408	 identifier.  */
20409      if (!template_p && !cp_parser_parse_definitely (parser))
20410	;
20411      /* We can get here when cp_parser_template_id, called by
20412	 cp_parser_class_name with tag_type == none_type, succeeds
20413	 and caches a BASELINK.  Then, when called again here,
20414	 instead of failing and returning an error_mark_node
20415	 returns it (see template/typename17.C in C++11).
20416	 ??? Could we diagnose this earlier?  */
20417      else if (tag_type == typename_type && BASELINK_P (decl))
20418	{
20419	  cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20420	  type = error_mark_node;
20421	}
20422      /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20423	 in effect, then we must assume that, upon instantiation, the
20424	 template will correspond to a class.  */
20425      else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20426	       && tag_type == typename_type)
20427	type = make_typename_type (parser->scope, decl,
20428				   typename_type,
20429				   /*complain=*/tf_error);
20430      /* If the `typename' keyword is in effect and DECL is not a type
20431	 decl, then type is non existent.   */
20432      else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20433        ;
20434      else if (TREE_CODE (decl) == TYPE_DECL)
20435	{
20436	  type = check_elaborated_type_specifier (tag_type, decl,
20437						  /*allow_template_p=*/true);
20438
20439	  /* If the next token is a semicolon, this must be a specialization,
20440	     instantiation, or friend declaration.  Check the scope while we
20441	     still know whether or not we had a nested-name-specifier.  */
20442	  if (type != error_mark_node
20443	      && !nested_name_specifier && !is_friend
20444	      && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20445	    check_unqualified_spec_or_inst (type, token->location);
20446	}
20447      else if (decl == error_mark_node)
20448	type = error_mark_node;
20449    }
20450
20451  if (!type)
20452    {
20453      token = cp_lexer_peek_token (parser->lexer);
20454      identifier = cp_parser_identifier (parser);
20455
20456      if (identifier == error_mark_node)
20457	{
20458	  parser->scope = NULL_TREE;
20459	  return error_mark_node;
20460	}
20461
20462      /* For a `typename', we needn't call xref_tag.  */
20463      if (tag_type == typename_type
20464	  && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20465	return cp_parser_make_typename_type (parser, identifier,
20466					     token->location);
20467
20468      /* Template parameter lists apply only if we are not within a
20469	 function parameter list.  */
20470      bool template_parm_lists_apply
20471	  = parser->num_template_parameter_lists;
20472      if (template_parm_lists_apply)
20473	for (cp_binding_level *s = current_binding_level;
20474	     s && s->kind != sk_template_parms;
20475	     s = s->level_chain)
20476	  if (s->kind == sk_function_parms)
20477	    template_parm_lists_apply = false;
20478
20479      /* Look up a qualified name in the usual way.  */
20480      if (parser->scope)
20481	{
20482	  tree decl;
20483	  tree ambiguous_decls;
20484
20485	  decl = cp_parser_lookup_name (parser, identifier,
20486					tag_type,
20487					/*is_template=*/false,
20488					/*is_namespace=*/false,
20489					/*check_dependency=*/true,
20490					&ambiguous_decls,
20491					token->location);
20492
20493	  /* If the lookup was ambiguous, an error will already have been
20494	     issued.  */
20495	  if (ambiguous_decls)
20496	    return error_mark_node;
20497
20498	  /* If we are parsing friend declaration, DECL may be a
20499	     TEMPLATE_DECL tree node here.  However, we need to check
20500	     whether this TEMPLATE_DECL results in valid code.  Consider
20501	     the following example:
20502
20503	       namespace N {
20504		 template <class T> class C {};
20505	       }
20506	       class X {
20507		 template <class T> friend class N::C; // #1, valid code
20508	       };
20509	       template <class T> class Y {
20510		 friend class N::C;		       // #2, invalid code
20511	       };
20512
20513	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20514	     name lookup of `N::C'.  We see that friend declaration must
20515	     be template for the code to be valid.  Note that
20516	     processing_template_decl does not work here since it is
20517	     always 1 for the above two cases.  */
20518
20519	  decl = (cp_parser_maybe_treat_template_as_class
20520		  (decl, /*tag_name_p=*/is_friend
20521			 && template_parm_lists_apply));
20522
20523	  if (TREE_CODE (decl) != TYPE_DECL)
20524	    {
20525	      cp_parser_diagnose_invalid_type_name (parser,
20526						    identifier,
20527						    token->location);
20528	      return error_mark_node;
20529	    }
20530
20531	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20532            {
20533              bool allow_template = (template_parm_lists_apply
20534		                     || DECL_SELF_REFERENCE_P (decl));
20535              type = check_elaborated_type_specifier (tag_type, decl,
20536                                                      allow_template);
20537
20538              if (type == error_mark_node)
20539                return error_mark_node;
20540            }
20541
20542          /* Forward declarations of nested types, such as
20543
20544               class C1::C2;
20545               class C1::C2::C3;
20546
20547             are invalid unless all components preceding the final '::'
20548             are complete.  If all enclosing types are complete, these
20549             declarations become merely pointless.
20550
20551             Invalid forward declarations of nested types are errors
20552             caught elsewhere in parsing.  Those that are pointless arrive
20553             here.  */
20554
20555          if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20556	      && !is_friend && is_declaration
20557	      && !processing_explicit_instantiation)
20558            warning (0, "declaration %qD does not declare anything", decl);
20559
20560	  type = TREE_TYPE (decl);
20561	}
20562      else
20563	{
20564	  /* An elaborated-type-specifier sometimes introduces a new type and
20565	     sometimes names an existing type.  Normally, the rule is that it
20566	     introduces a new type only if there is not an existing type of
20567	     the same name already in scope.  For example, given:
20568
20569	       struct S {};
20570	       void f() { struct S s; }
20571
20572	     the `struct S' in the body of `f' is the same `struct S' as in
20573	     the global scope; the existing definition is used.  However, if
20574	     there were no global declaration, this would introduce a new
20575	     local class named `S'.
20576
20577	     An exception to this rule applies to the following code:
20578
20579	       namespace N { struct S; }
20580
20581	     Here, the elaborated-type-specifier names a new type
20582	     unconditionally; even if there is already an `S' in the
20583	     containing scope this declaration names a new type.
20584	     This exception only applies if the elaborated-type-specifier
20585	     forms the complete declaration:
20586
20587	       [class.name]
20588
20589	       A declaration consisting solely of `class-key identifier ;' is
20590	       either a redeclaration of the name in the current scope or a
20591	       forward declaration of the identifier as a class name.  It
20592	       introduces the name into the current scope.
20593
20594	     We are in this situation precisely when the next token is a `;'.
20595
20596	     An exception to the exception is that a `friend' declaration does
20597	     *not* name a new type; i.e., given:
20598
20599	       struct S { friend struct T; };
20600
20601	     `T' is not a new type in the scope of `S'.
20602
20603	     Also, `new struct S' or `sizeof (struct S)' never results in the
20604	     definition of a new type; a new type can only be declared in a
20605	     declaration context.  */
20606
20607	  TAG_how how;
20608
20609	  if (is_friend)
20610	    /* Friends have special name lookup rules.  */
20611	    how = TAG_how::HIDDEN_FRIEND;
20612	  else if (is_declaration
20613		   && cp_lexer_next_token_is (parser->lexer,
20614					      CPP_SEMICOLON))
20615	    /* This is a `class-key identifier ;' */
20616	    how = TAG_how::CURRENT_ONLY;
20617	  else
20618	    how = TAG_how::GLOBAL;
20619
20620	  bool template_p =
20621	    (template_parm_lists_apply
20622	     && (cp_parser_next_token_starts_class_definition_p (parser)
20623		 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20624	  /* An unqualified name was used to reference this type, so
20625	     there were no qualifying templates.  */
20626	  if (template_parm_lists_apply
20627	      && !cp_parser_check_template_parameters (parser,
20628						       /*num_templates=*/0,
20629						       /*template_id*/false,
20630						       token->location,
20631						       /*declarator=*/NULL))
20632	    return error_mark_node;
20633
20634	  type = xref_tag (tag_type, identifier, how, template_p);
20635	}
20636    }
20637
20638  if (type == error_mark_node)
20639    return error_mark_node;
20640
20641  /* Allow attributes on forward declarations of classes.  */
20642  if (attributes)
20643    {
20644      if (TREE_CODE (type) == TYPENAME_TYPE)
20645	warning (OPT_Wattributes,
20646		 "attributes ignored on uninstantiated type");
20647      else if (tag_type != enum_type
20648	       && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
20649	       && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
20650	       && ! processing_explicit_instantiation)
20651	warning (OPT_Wattributes,
20652		 "attributes ignored on template instantiation");
20653      else if (is_friend && cxx11_attribute_p (attributes))
20654	{
20655	  if (warning (OPT_Wattributes, "attribute ignored"))
20656	    inform (input_location, "an attribute that appertains to a friend "
20657		    "declaration that is not a definition is ignored");
20658	}
20659      else if (is_declaration && cp_parser_declares_only_class_p (parser))
20660	cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
20661      else
20662	warning (OPT_Wattributes,
20663		 "attributes ignored on elaborated-type-specifier that is "
20664		 "not a forward declaration");
20665    }
20666
20667  if (tag_type == enum_type)
20668    cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
20669  else
20670    {
20671      /* Diagnose class/struct/union mismatches.  IS_DECLARATION is false
20672	 for alias definition.  */
20673      bool decl_class = (is_declaration
20674			 && cp_parser_declares_only_class_p (parser));
20675      cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
20676				 decl_class);
20677
20678      /* Indicate whether this class was declared as a `class' or as a
20679	 `struct'.  */
20680      if (CLASS_TYPE_P (type) && !currently_open_class (type))
20681	CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
20682    }
20683
20684  /* A "<" cannot follow an elaborated type specifier.  If that
20685     happens, the user was probably trying to form a template-id.  */
20686  cp_parser_check_for_invalid_template_id (parser, type, tag_type,
20687					   token->location);
20688
20689  return type;
20690}
20691
20692/* Parse an enum-specifier.
20693
20694   enum-specifier:
20695     enum-head { enumerator-list [opt] }
20696     enum-head { enumerator-list , } [C++0x]
20697
20698   enum-head:
20699     enum-key identifier [opt] enum-base [opt]
20700     enum-key nested-name-specifier identifier enum-base [opt]
20701
20702   enum-key:
20703     enum
20704     enum class   [C++0x]
20705     enum struct  [C++0x]
20706
20707   enum-base:   [C++0x]
20708     : type-specifier-seq
20709
20710   opaque-enum-specifier:
20711     enum-key identifier enum-base [opt] ;
20712
20713   GNU Extensions:
20714     enum-key attributes[opt] identifier [opt] enum-base [opt]
20715       { enumerator-list [opt] }attributes[opt]
20716     enum-key attributes[opt] identifier [opt] enum-base [opt]
20717       { enumerator-list, }attributes[opt] [C++0x]
20718
20719   Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20720   if the token stream isn't an enum-specifier after all.  */
20721
20722static tree
20723cp_parser_enum_specifier (cp_parser* parser)
20724{
20725  tree identifier;
20726  tree type = NULL_TREE;
20727  tree prev_scope;
20728  tree nested_name_specifier = NULL_TREE;
20729  tree attributes;
20730  bool scoped_enum_p = false;
20731  bool has_underlying_type = false;
20732  bool nested_being_defined = false;
20733  bool new_value_list = false;
20734  bool is_new_type = false;
20735  bool is_unnamed = false;
20736  tree underlying_type = NULL_TREE;
20737  cp_token *type_start_token = NULL;
20738  auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
20739
20740  /* Parse tentatively so that we can back up if we don't find a
20741     enum-specifier.  */
20742  cp_parser_parse_tentatively (parser);
20743
20744  /* Caller guarantees that the current token is 'enum', an identifier
20745     possibly follows, and the token after that is an opening brace.
20746     If we don't have an identifier, fabricate an anonymous name for
20747     the enumeration being defined.  */
20748  cp_lexer_consume_token (parser->lexer);
20749
20750  /* Parse the "class" or "struct", which indicates a scoped
20751     enumeration type in C++0x.  */
20752  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
20753      || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
20754    {
20755      if (cxx_dialect < cxx11)
20756        maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20757
20758      /* Consume the `struct' or `class' token.  */
20759      cp_lexer_consume_token (parser->lexer);
20760
20761      scoped_enum_p = true;
20762    }
20763
20764  attributes = cp_parser_attributes_opt (parser);
20765
20766  /* Clear the qualification.  */
20767  parser->scope = NULL_TREE;
20768  parser->qualifying_scope = NULL_TREE;
20769  parser->object_scope = NULL_TREE;
20770
20771  /* Figure out in what scope the declaration is being placed.  */
20772  prev_scope = current_scope ();
20773
20774  type_start_token = cp_lexer_peek_token (parser->lexer);
20775
20776  push_deferring_access_checks (dk_no_check);
20777  nested_name_specifier
20778    = cp_parser_nested_name_specifier_opt (parser,
20779					   /*typename_keyword_p=*/true,
20780					   /*check_dependency_p=*/false,
20781					   /*type_p=*/false,
20782					   /*is_declaration=*/false);
20783
20784  if (nested_name_specifier)
20785    {
20786      tree name;
20787
20788      identifier = cp_parser_identifier (parser);
20789      name = cp_parser_lookup_name (parser, identifier,
20790				    enum_type,
20791				    /*is_template=*/false,
20792				    /*is_namespace=*/false,
20793				    /*check_dependency=*/true,
20794				    /*ambiguous_decls=*/NULL,
20795				    input_location);
20796      if (name && name != error_mark_node)
20797	{
20798	  type = TREE_TYPE (name);
20799	  if (TREE_CODE (type) == TYPENAME_TYPE)
20800	    {
20801	      /* Are template enums allowed in ISO? */
20802	      if (template_parm_scope_p ())
20803		pedwarn (type_start_token->location, OPT_Wpedantic,
20804			 "%qD is an enumeration template", name);
20805	      /* ignore a typename reference, for it will be solved by name
20806	         in start_enum.  */
20807	      type = NULL_TREE;
20808	    }
20809	}
20810      else if (nested_name_specifier == error_mark_node)
20811	/* We already issued an error.  */;
20812      else
20813	{
20814	  error_at (type_start_token->location,
20815		    "%qD does not name an enumeration in %qT",
20816		    identifier, nested_name_specifier);
20817	  nested_name_specifier = error_mark_node;
20818	}
20819    }
20820  else
20821    {
20822      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20823	identifier = cp_parser_identifier (parser);
20824      else
20825	{
20826	  identifier = make_anon_name ();
20827	  is_unnamed = true;
20828	  if (scoped_enum_p)
20829	    error_at (type_start_token->location,
20830		      "unnamed scoped enum is not allowed");
20831	}
20832    }
20833  pop_deferring_access_checks ();
20834
20835  /* Check for the `:' that denotes a specified underlying type in C++0x.
20836     Note that a ':' could also indicate a bitfield width, however.  */
20837  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20838    {
20839      cp_decl_specifier_seq type_specifiers;
20840
20841      /* Consume the `:'.  */
20842      cp_lexer_consume_token (parser->lexer);
20843
20844      auto tdf
20845	= make_temp_override (parser->type_definition_forbidden_message,
20846			      G_("types may not be defined in enum-base"));
20847
20848      /* Parse the type-specifier-seq.  */
20849      cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
20850				    /*is_declaration=*/false,
20851				    /*is_trailing_return=*/false,
20852                                    &type_specifiers);
20853
20854      /* At this point this is surely not elaborated type specifier.  */
20855      if (!cp_parser_parse_definitely (parser))
20856	return NULL_TREE;
20857
20858      if (cxx_dialect < cxx11)
20859        maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20860
20861      has_underlying_type = true;
20862
20863      /* If that didn't work, stop.  */
20864      if (type_specifiers.type != error_mark_node)
20865        {
20866          underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
20867                                            /*initialized=*/0, NULL);
20868          if (underlying_type == error_mark_node
20869	      || check_for_bare_parameter_packs (underlying_type))
20870            underlying_type = NULL_TREE;
20871        }
20872    }
20873
20874  /* Look for the `{' but don't consume it yet.  */
20875  if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20876    {
20877      if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
20878	{
20879	  if (has_underlying_type)
20880	    cp_parser_commit_to_tentative_parse (parser);
20881	  cp_parser_error (parser, "expected %<{%>");
20882	  if (has_underlying_type)
20883	    return error_mark_node;
20884	}
20885      /* An opaque-enum-specifier must have a ';' here.  */
20886      if ((scoped_enum_p || underlying_type)
20887	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20888	{
20889	  if (has_underlying_type)
20890	    cp_parser_commit_to_tentative_parse (parser);
20891	  cp_parser_error (parser, "expected %<;%> or %<{%>");
20892	  if (has_underlying_type)
20893	    return error_mark_node;
20894	}
20895    }
20896
20897  if (!has_underlying_type && !cp_parser_parse_definitely (parser))
20898    return NULL_TREE;
20899
20900  if (nested_name_specifier)
20901    {
20902      if (CLASS_TYPE_P (nested_name_specifier))
20903	{
20904	  nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
20905	  TYPE_BEING_DEFINED (nested_name_specifier) = 1;
20906	  push_scope (nested_name_specifier);
20907	}
20908      else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
20909	push_nested_namespace (nested_name_specifier);
20910    }
20911
20912  /* Issue an error message if type-definitions are forbidden here.  */
20913  if (!cp_parser_check_type_definition (parser))
20914    type = error_mark_node;
20915  else
20916    /* Create the new type.  We do this before consuming the opening
20917       brace so the enum will be recorded as being on the line of its
20918       tag (or the 'enum' keyword, if there is no tag).  */
20919    type = start_enum (identifier, type, underlying_type,
20920		       attributes, scoped_enum_p, &is_new_type);
20921
20922  /* If the next token is not '{' it is an opaque-enum-specifier or an
20923     elaborated-type-specifier.  */
20924  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20925    {
20926      timevar_push (TV_PARSE_ENUM);
20927      if (nested_name_specifier
20928	  && nested_name_specifier != error_mark_node)
20929	{
20930	  /* The following catches invalid code such as:
20931	     enum class S<int>::E { A, B, C }; */
20932	  if (!processing_specialization
20933	      && CLASS_TYPE_P (nested_name_specifier)
20934	      && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
20935	    error_at (type_start_token->location, "cannot add an enumerator "
20936		      "list to a template instantiation");
20937
20938	  if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
20939	    {
20940	      error_at (type_start_token->location,
20941			"%<%T::%E%> has not been declared",
20942			TYPE_CONTEXT (nested_name_specifier),
20943			nested_name_specifier);
20944	      type = error_mark_node;
20945	    }
20946	  else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
20947		   && !CLASS_TYPE_P (nested_name_specifier))
20948	    {
20949	      error_at (type_start_token->location, "nested name specifier "
20950			"%qT for enum declaration does not name a class "
20951			"or namespace", nested_name_specifier);
20952	      type = error_mark_node;
20953	    }
20954	  /* If that scope does not contain the scope in which the
20955	     class was originally declared, the program is invalid.  */
20956	  else if (prev_scope && !is_ancestor (prev_scope,
20957					       nested_name_specifier))
20958	    {
20959	      if (at_namespace_scope_p ())
20960		error_at (type_start_token->location,
20961			  "declaration of %qD in namespace %qD which does not "
20962			  "enclose %qD",
20963			  type, prev_scope, nested_name_specifier);
20964	      else
20965		error_at (type_start_token->location,
20966			  "declaration of %qD in %qD which does not "
20967			  "enclose %qD",
20968			  type, prev_scope, nested_name_specifier);
20969	      type = error_mark_node;
20970	    }
20971	  /* If that scope is the scope where the declaration is being placed
20972	     the program is invalid.  */
20973	  else if (CLASS_TYPE_P (nested_name_specifier)
20974		   && CLASS_TYPE_P (prev_scope)
20975		   && same_type_p (nested_name_specifier, prev_scope))
20976	    {
20977	      permerror (type_start_token->location,
20978			 "extra qualification not allowed");
20979	      nested_name_specifier = NULL_TREE;
20980	    }
20981	}
20982
20983      if (scoped_enum_p)
20984	begin_scope (sk_scoped_enum, type);
20985
20986      /* Consume the opening brace.  */
20987      matching_braces braces;
20988      braces.consume_open (parser);
20989
20990      if (type == error_mark_node)
20991	; /* Nothing to add */
20992      else if (OPAQUE_ENUM_P (type)
20993	       || (cxx_dialect > cxx98 && processing_specialization))
20994	{
20995	  new_value_list = true;
20996	  SET_OPAQUE_ENUM_P (type, false);
20997	  DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20998	}
20999      else
21000	{
21001	  error_at (type_start_token->location,
21002		    "multiple definition of %q#T", type);
21003	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21004		  "previous definition here");
21005	  type = error_mark_node;
21006	}
21007
21008      if (type == error_mark_node)
21009	cp_parser_skip_to_end_of_block_or_statement (parser);
21010      /* If the next token is not '}', then there are some enumerators.  */
21011      else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21012	{
21013	  if (is_unnamed && !scoped_enum_p
21014	      /* Don't warn for enum {} a; here.  */
21015	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21016	    pedwarn (type_start_token->location, OPT_Wpedantic,
21017		     "ISO C++ forbids empty unnamed enum");
21018	}
21019      else
21020	{
21021	  /* We've seen a '{' so we know we're in an enum-specifier.
21022	     Commit to any tentative parse to get syntax errors.  */
21023	  cp_parser_commit_to_tentative_parse (parser);
21024	  cp_parser_enumerator_list (parser, type);
21025	}
21026
21027      /* Consume the final '}'.  */
21028      braces.require_close (parser);
21029
21030      if (scoped_enum_p)
21031	finish_scope ();
21032      timevar_pop (TV_PARSE_ENUM);
21033    }
21034  else
21035    {
21036      /* If a ';' follows, then it is an opaque-enum-specifier
21037	and additional restrictions apply.  */
21038      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21039	{
21040	  if (is_unnamed)
21041	    error_at (type_start_token->location,
21042		      "opaque-enum-specifier without name");
21043	  else if (nested_name_specifier)
21044	    error_at (type_start_token->location,
21045		      "opaque-enum-specifier must use a simple identifier");
21046	}
21047    }
21048
21049  /* Look for trailing attributes to apply to this enumeration, and
21050     apply them if appropriate.  */
21051  if (cp_parser_allow_gnu_extensions_p (parser))
21052    {
21053      tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21054      cplus_decl_attributes (&type,
21055			     trailing_attr,
21056			     (int) ATTR_FLAG_TYPE_IN_PLACE);
21057    }
21058
21059  /* Finish up the enumeration.  */
21060  if (type != error_mark_node)
21061    {
21062      if (new_value_list)
21063	finish_enum_value_list (type);
21064      if (is_new_type)
21065	finish_enum (type);
21066    }
21067
21068  if (nested_name_specifier)
21069    {
21070      if (CLASS_TYPE_P (nested_name_specifier))
21071	{
21072	  TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21073	  pop_scope (nested_name_specifier);
21074	}
21075      else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21076	pop_nested_namespace (nested_name_specifier);
21077    }
21078  return type;
21079}
21080
21081/* Parse an enumerator-list.  The enumerators all have the indicated
21082   TYPE.
21083
21084   enumerator-list:
21085     enumerator-definition
21086     enumerator-list , enumerator-definition  */
21087
21088static void
21089cp_parser_enumerator_list (cp_parser* parser, tree type)
21090{
21091  while (true)
21092    {
21093      /* Parse an enumerator-definition.  */
21094      cp_parser_enumerator_definition (parser, type);
21095
21096      /* If the next token is not a ',', we've reached the end of
21097	 the list.  */
21098      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21099	break;
21100      /* Otherwise, consume the `,' and keep going.  */
21101      cp_lexer_consume_token (parser->lexer);
21102      /* If the next token is a `}', there is a trailing comma.  */
21103      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21104	{
21105	  if (cxx_dialect < cxx11)
21106	    pedwarn (input_location, OPT_Wpedantic,
21107                     "comma at end of enumerator list");
21108	  break;
21109	}
21110    }
21111}
21112
21113/* Parse an enumerator-definition.  The enumerator has the indicated
21114   TYPE.
21115
21116   enumerator-definition:
21117     enumerator
21118     enumerator = constant-expression
21119
21120   enumerator:
21121     identifier
21122
21123   GNU Extensions:
21124
21125   enumerator-definition:
21126     enumerator attributes [opt]
21127     enumerator attributes [opt] = constant-expression  */
21128
21129static void
21130cp_parser_enumerator_definition (cp_parser* parser, tree type)
21131{
21132  tree identifier;
21133  tree value;
21134  location_t loc;
21135
21136  /* Save the input location because we are interested in the location
21137     of the identifier and not the location of the explicit value.  */
21138  loc = cp_lexer_peek_token (parser->lexer)->location;
21139
21140  /* Look for the identifier.  */
21141  identifier = cp_parser_identifier (parser);
21142  if (identifier == error_mark_node)
21143    return;
21144
21145  /* Parse any specified attributes.  */
21146  tree attrs = cp_parser_attributes_opt (parser);
21147
21148  /* If the next token is an '=', then there is an explicit value.  */
21149  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21150    {
21151      /* Consume the `=' token.  */
21152      cp_lexer_consume_token (parser->lexer);
21153      /* Parse the value.  */
21154      value = cp_parser_constant_expression (parser);
21155    }
21156  else
21157    value = NULL_TREE;
21158
21159  /* If we are processing a template, make sure the initializer of the
21160     enumerator doesn't contain any bare template parameter pack.  */
21161  if (current_lambda_expr ())
21162    {
21163      /* In a lambda it should work, but doesn't currently.  */
21164      if (uses_parameter_packs (value))
21165	{
21166	  sorry ("unexpanded parameter pack in enumerator in lambda");
21167	  value = error_mark_node;
21168	}
21169    }
21170  else if (check_for_bare_parameter_packs (value))
21171    value = error_mark_node;
21172
21173  /* Create the enumerator.  */
21174  build_enumerator (identifier, value, type, attrs, loc);
21175}
21176
21177/* Parse a namespace-name.
21178
21179   namespace-name:
21180     original-namespace-name
21181     namespace-alias
21182
21183   Returns the NAMESPACE_DECL for the namespace.  */
21184
21185static tree
21186cp_parser_namespace_name (cp_parser* parser)
21187{
21188  tree identifier;
21189  tree namespace_decl;
21190
21191  cp_token *token = cp_lexer_peek_token (parser->lexer);
21192
21193  /* Get the name of the namespace.  */
21194  identifier = cp_parser_identifier (parser);
21195  if (identifier == error_mark_node)
21196    return error_mark_node;
21197
21198  /* Look up the identifier in the currently active scope.  Look only
21199     for namespaces, due to:
21200
21201       [basic.lookup.udir]
21202
21203       When looking up a namespace-name in a using-directive or alias
21204       definition, only namespace names are considered.
21205
21206     And:
21207
21208       [basic.lookup.qual]
21209
21210       During the lookup of a name preceding the :: scope resolution
21211       operator, object, function, and enumerator names are ignored.
21212
21213     (Note that cp_parser_qualifying_entity only calls this
21214     function if the token after the name is the scope resolution
21215     operator.)  */
21216  namespace_decl = cp_parser_lookup_name (parser, identifier,
21217					  none_type,
21218					  /*is_template=*/false,
21219					  /*is_namespace=*/true,
21220					  /*check_dependency=*/true,
21221					  /*ambiguous_decls=*/NULL,
21222					  token->location);
21223  /* If it's not a namespace, issue an error.  */
21224  if (namespace_decl == error_mark_node
21225      || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21226    {
21227      if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21228	{
21229	  auto_diagnostic_group d;
21230	  name_hint hint;
21231	  if (namespace_decl == error_mark_node
21232	      && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21233	    hint = suggest_alternative_in_explicit_scope (token->location,
21234							  identifier,
21235							  parser->scope);
21236	  if (const char *suggestion = hint.suggestion ())
21237	    {
21238	      gcc_rich_location richloc (token->location);
21239	      richloc.add_fixit_replace (suggestion);
21240	      error_at (&richloc,
21241			"%qD is not a namespace-name; did you mean %qs?",
21242			identifier, suggestion);
21243	    }
21244	  else
21245	    error_at (token->location, "%qD is not a namespace-name",
21246		      identifier);
21247	}
21248      else
21249	cp_parser_error (parser, "expected namespace-name");
21250      namespace_decl = error_mark_node;
21251    }
21252
21253  return namespace_decl;
21254}
21255
21256/* Parse a namespace-definition.
21257
21258   namespace-definition:
21259     named-namespace-definition
21260     unnamed-namespace-definition
21261
21262   named-namespace-definition:
21263     original-namespace-definition
21264     extension-namespace-definition
21265
21266   original-namespace-definition:
21267     namespace identifier { namespace-body }
21268
21269   extension-namespace-definition:
21270     namespace original-namespace-name { namespace-body }
21271
21272   unnamed-namespace-definition:
21273     namespace { namespace-body } */
21274
21275static void
21276cp_parser_namespace_definition (cp_parser* parser)
21277{
21278  tree identifier;
21279  int nested_definition_count = 0;
21280
21281  cp_ensure_no_omp_declare_simd (parser);
21282  cp_ensure_no_oacc_routine (parser);
21283
21284  bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21285  const bool topmost_inline_p = is_inline;
21286
21287  if (is_inline)
21288    {
21289      maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21290      cp_lexer_consume_token (parser->lexer);
21291    }
21292
21293  /* Look for the `namespace' keyword.  */
21294  cp_token* token
21295    = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21296
21297  /* Parse any specified attributes before the identifier.  */
21298  tree attribs = cp_parser_attributes_opt (parser);
21299
21300  for (;;)
21301    {
21302      identifier = NULL_TREE;
21303
21304      bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21305							     RID_INLINE);
21306      if (nested_inline_p && nested_definition_count != 0)
21307	{
21308	  if (pedantic && cxx_dialect < cxx20)
21309	    pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21310		     OPT_Wc__20_extensions, "nested inline namespace "
21311		     "definitions only available with %<-std=c++20%> or "
21312		     "%<-std=gnu++20%>");
21313	  cp_lexer_consume_token (parser->lexer);
21314	}
21315
21316      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21317	{
21318	  identifier = cp_parser_identifier (parser);
21319
21320	  if (cp_next_tokens_can_be_std_attribute_p (parser))
21321	    pedwarn (input_location, OPT_Wpedantic,
21322		     "standard attributes on namespaces must precede "
21323		     "the namespace name");
21324
21325	  /* Parse any attributes specified after the identifier.  */
21326	  attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21327	}
21328
21329      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21330	{
21331	  /* Don't forget that the innermost namespace might have been
21332	     marked as inline.  Use |= because we cannot overwrite
21333	     IS_INLINE in case the outermost namespace is inline, but
21334	     there are no nested inlines.  */
21335	  is_inline |= nested_inline_p;
21336	  break;
21337	}
21338
21339      if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21340        pedwarn (input_location, OPT_Wc__17_extensions,
21341		 "nested namespace definitions only available with "
21342		 "%<-std=c++17%> or %<-std=gnu++17%>");
21343
21344      /* Nested namespace names can create new namespaces (unlike
21345	 other qualified-ids).  */
21346      if (int count = (identifier
21347		       ? push_namespace (identifier, nested_inline_p)
21348		       : 0))
21349	nested_definition_count += count;
21350      else
21351	cp_parser_error (parser, "nested namespace name required");
21352      cp_lexer_consume_token (parser->lexer);
21353    }
21354
21355  if (nested_definition_count && !identifier)
21356    cp_parser_error (parser, "namespace name required");
21357
21358  if (nested_definition_count && attribs)
21359    error_at (token->location,
21360	      "a nested namespace definition cannot have attributes");
21361  if (nested_definition_count && topmost_inline_p)
21362    error_at (token->location,
21363	      "a nested namespace definition cannot be inline");
21364
21365  /* Start the namespace.  */
21366  nested_definition_count += push_namespace (identifier, is_inline);
21367
21368  bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21369
21370  warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21371
21372  /* Look for the `{' to validate starting the namespace.  */
21373  matching_braces braces;
21374  if (braces.require_open (parser))
21375    {
21376      /* Parse the body of the namespace.  */
21377      cp_parser_namespace_body (parser);
21378
21379      /* Look for the final `}'.  */
21380      braces.require_close (parser);
21381    }
21382
21383  if (has_visibility)
21384    pop_visibility (1);
21385
21386  /* Pop the nested namespace definitions.  */
21387  while (nested_definition_count--)
21388    pop_namespace ();
21389}
21390
21391/* Parse a namespace-body.
21392
21393   namespace-body:
21394     declaration-seq [opt]  */
21395
21396static void
21397cp_parser_namespace_body (cp_parser* parser)
21398{
21399  cp_parser_declaration_seq_opt (parser);
21400}
21401
21402/* Parse a namespace-alias-definition.
21403
21404   namespace-alias-definition:
21405     namespace identifier = qualified-namespace-specifier ;  */
21406
21407static void
21408cp_parser_namespace_alias_definition (cp_parser* parser)
21409{
21410  tree identifier;
21411  tree namespace_specifier;
21412
21413  cp_token *token = cp_lexer_peek_token (parser->lexer);
21414
21415  /* Look for the `namespace' keyword.  */
21416  cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21417  /* Look for the identifier.  */
21418  identifier = cp_parser_identifier (parser);
21419  if (identifier == error_mark_node)
21420    return;
21421  /* Look for the `=' token.  */
21422  if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21423      && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21424    {
21425      error_at (token->location, "%<namespace%> definition is not allowed here");
21426      /* Skip the definition.  */
21427      cp_lexer_consume_token (parser->lexer);
21428      if (cp_parser_skip_to_closing_brace (parser))
21429	cp_lexer_consume_token (parser->lexer);
21430      return;
21431    }
21432  cp_parser_require (parser, CPP_EQ, RT_EQ);
21433  /* Look for the qualified-namespace-specifier.  */
21434  namespace_specifier
21435    = cp_parser_qualified_namespace_specifier (parser);
21436  cp_warn_deprecated_use_scopes (namespace_specifier);
21437  /* Look for the `;' token.  */
21438  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21439
21440  /* Register the alias in the symbol table.  */
21441  do_namespace_alias (identifier, namespace_specifier);
21442}
21443
21444/* Parse a qualified-namespace-specifier.
21445
21446   qualified-namespace-specifier:
21447     :: [opt] nested-name-specifier [opt] namespace-name
21448
21449   Returns a NAMESPACE_DECL corresponding to the specified
21450   namespace.  */
21451
21452static tree
21453cp_parser_qualified_namespace_specifier (cp_parser* parser)
21454{
21455  /* Look for the optional `::'.  */
21456  cp_parser_global_scope_opt (parser,
21457			      /*current_scope_valid_p=*/false);
21458
21459  /* Look for the optional nested-name-specifier.  */
21460  cp_parser_nested_name_specifier_opt (parser,
21461				       /*typename_keyword_p=*/false,
21462				       /*check_dependency_p=*/true,
21463				       /*type_p=*/false,
21464				       /*is_declaration=*/true);
21465
21466  return cp_parser_namespace_name (parser);
21467}
21468
21469/* Subroutine of cp_parser_using_declaration.  */
21470
21471static tree
21472finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21473{
21474  tree decl = NULL_TREE;
21475  if (at_class_scope_p ())
21476    {
21477      /* Create the USING_DECL.  */
21478      decl = do_class_using_decl (qscope, identifier);
21479
21480      if (check_for_bare_parameter_packs (decl))
21481	return error_mark_node;
21482
21483      if (decl && typename_p)
21484	USING_DECL_TYPENAME_P (decl) = 1;
21485
21486      /* Add it to the list of members in this class.  */
21487      finish_member_declaration (decl);
21488    }
21489  else
21490    finish_nonmember_using_decl (qscope, identifier);
21491  return decl;
21492}
21493
21494/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21495   access declaration.
21496
21497   using-declaration:
21498     using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21499     using :: unqualified-id ;
21500
21501   access-declaration:
21502     qualified-id ;
21503
21504   */
21505
21506static bool
21507cp_parser_using_declaration (cp_parser* parser,
21508			     bool access_declaration_p)
21509{
21510  cp_token *token;
21511  bool typename_p = false;
21512  bool global_scope_p;
21513  tree identifier;
21514  tree qscope;
21515  int oldcount = errorcount;
21516  cp_token *diag_token = NULL;
21517
21518  if (access_declaration_p)
21519    {
21520      diag_token = cp_lexer_peek_token (parser->lexer);
21521      cp_parser_parse_tentatively (parser);
21522    }
21523  else
21524    {
21525      /* Look for the `using' keyword.  */
21526      cp_parser_require_keyword (parser, RID_USING, RT_USING);
21527
21528 again:
21529      /* Peek at the next token.  */
21530      token = cp_lexer_peek_token (parser->lexer);
21531      /* See if it's `typename'.  */
21532      if (token->keyword == RID_TYPENAME)
21533	{
21534	  /* Remember that we've seen it.  */
21535	  typename_p = true;
21536	  /* Consume the `typename' token.  */
21537	  cp_lexer_consume_token (parser->lexer);
21538	}
21539    }
21540
21541  /* Look for the optional global scope qualification.  */
21542  global_scope_p
21543    = (cp_parser_global_scope_opt (parser,
21544				   /*current_scope_valid_p=*/false)
21545       != NULL_TREE);
21546
21547  /* If we saw `typename', or didn't see `::', then there must be a
21548     nested-name-specifier present.  */
21549  if (typename_p || !global_scope_p)
21550    {
21551      qscope = cp_parser_nested_name_specifier (parser, typename_p,
21552						/*check_dependency_p=*/true,
21553						/*type_p=*/false,
21554						/*is_declaration=*/true);
21555      if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21556	{
21557	  cp_parser_skip_to_end_of_block_or_statement (parser);
21558	  return false;
21559	}
21560    }
21561  /* Otherwise, we could be in either of the two productions.  In that
21562     case, treat the nested-name-specifier as optional.  */
21563  else
21564    qscope = cp_parser_nested_name_specifier_opt (parser,
21565						  /*typename_keyword_p=*/false,
21566						  /*check_dependency_p=*/true,
21567						  /*type_p=*/false,
21568						  /*is_declaration=*/true);
21569  if (!qscope)
21570    qscope = global_namespace;
21571
21572  cp_warn_deprecated_use_scopes (qscope);
21573
21574  if (access_declaration_p && cp_parser_error_occurred (parser))
21575    /* Something has already gone wrong; there's no need to parse
21576       further.  Since an error has occurred, the return value of
21577       cp_parser_parse_definitely will be false, as required.  */
21578    return cp_parser_parse_definitely (parser);
21579
21580  token = cp_lexer_peek_token (parser->lexer);
21581  /* Parse the unqualified-id.  */
21582  identifier = cp_parser_unqualified_id (parser,
21583					 /*template_keyword_p=*/false,
21584					 /*check_dependency_p=*/true,
21585					 /*declarator_p=*/true,
21586					 /*optional_p=*/false);
21587
21588  if (access_declaration_p)
21589    {
21590      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21591	cp_parser_simulate_error (parser);
21592      if (!cp_parser_parse_definitely (parser))
21593	return false;
21594    }
21595  else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21596    {
21597      cp_token *ell = cp_lexer_consume_token (parser->lexer);
21598      if (cxx_dialect < cxx17)
21599	pedwarn (ell->location, OPT_Wc__17_extensions,
21600		 "pack expansion in using-declaration only available "
21601		 "with %<-std=c++17%> or %<-std=gnu++17%>");
21602      qscope = make_pack_expansion (qscope);
21603    }
21604
21605  /* The function we call to handle a using-declaration is different
21606     depending on what scope we are in.  */
21607  if (qscope == error_mark_node || identifier == error_mark_node)
21608    ;
21609  else if (!identifier_p (identifier)
21610	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
21611    /* [namespace.udecl]
21612
21613       A using declaration shall not name a template-id.  */
21614    error_at (token->location,
21615	      "a template-id may not appear in a using-declaration");
21616  else
21617    {
21618      tree decl = finish_using_decl (qscope, identifier, typename_p);
21619
21620      if (decl == error_mark_node)
21621	{
21622	  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21623	  return false;
21624	}
21625    }
21626
21627  if (!access_declaration_p
21628      && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21629    {
21630      cp_token *comma = cp_lexer_consume_token (parser->lexer);
21631      if (cxx_dialect < cxx17)
21632	pedwarn (comma->location, OPT_Wc__17_extensions,
21633		 "comma-separated list in using-declaration only available "
21634		 "with %<-std=c++17%> or %<-std=gnu++17%>");
21635      goto again;
21636    }
21637
21638  /* Look for the final `;'.  */
21639  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21640
21641  if (access_declaration_p && errorcount == oldcount)
21642    warning_at (diag_token->location, OPT_Wdeprecated,
21643		"access declarations are deprecated "
21644		"in favour of using-declarations; "
21645		"suggestion: add the %<using%> keyword");
21646
21647  return true;
21648}
21649
21650/* C++20 using enum declaration.
21651
21652   using-enum-declaration :
21653       using elaborated-enum-specifier ;  */
21654
21655static void
21656cp_parser_using_enum (cp_parser *parser)
21657{
21658  cp_parser_require_keyword (parser, RID_USING, RT_USING);
21659
21660  /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21661     breaks one of the motivating examples in using-enum-5.C.
21662     cp_parser_simple_type_specifier seems to be closer to what we actually
21663     want, though that hasn't been properly specified yet.  */
21664
21665  /* Consume 'enum'.  */
21666  gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
21667  cp_lexer_consume_token (parser->lexer);
21668
21669  cp_token *start = cp_lexer_peek_token (parser->lexer);
21670
21671  tree type = (cp_parser_simple_type_specifier
21672	       (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
21673
21674  cp_token *end = cp_lexer_previous_token (parser->lexer);
21675
21676  if (type == error_mark_node
21677      || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21678    {
21679      cp_parser_skip_to_end_of_block_or_statement (parser);
21680      return;
21681    }
21682  if (TREE_CODE (type) == TYPE_DECL)
21683    type = TREE_TYPE (type);
21684
21685  /* The elaborated-enum-specifier shall not name a dependent type and the type
21686     shall have a reachable enum-specifier.  */
21687  const char *msg = nullptr;
21688  if (cxx_dialect < cxx20)
21689    msg = _("%<using enum%> "
21690	    "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21691  else if (dependent_type_p (type))
21692    msg = _("%<using enum%> of dependent type %qT");
21693  else if (TREE_CODE (type) != ENUMERAL_TYPE)
21694    msg = _("%<using enum%> of non-enumeration type %q#T");
21695  else if (!COMPLETE_TYPE_P (type))
21696    msg = _("%<using enum%> of incomplete type %qT");
21697  else if (OPAQUE_ENUM_P (type))
21698    msg = _("%<using enum%> of %qT before its enum-specifier");
21699  if (msg)
21700    {
21701      location_t loc = make_location (start, start, end);
21702      auto_diagnostic_group g;
21703      error_at (loc, msg, type);
21704      loc = location_of (type);
21705      if (cxx_dialect < cxx20 || loc == input_location)
21706	;
21707      else if (OPAQUE_ENUM_P (type))
21708	inform (loc, "opaque-enum-declaration here");
21709      else
21710	inform (loc, "declared here");
21711    }
21712
21713  /* A using-enum-declaration introduces the enumerator names of the named
21714     enumeration as if by a using-declaration for each enumerator.  */
21715  if (TREE_CODE (type) == ENUMERAL_TYPE)
21716    for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
21717      finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
21718}
21719
21720/* Parse an alias-declaration.
21721
21722   alias-declaration:
21723     using identifier attribute-specifier-seq [opt] = type-id  */
21724
21725static tree
21726cp_parser_alias_declaration (cp_parser* parser)
21727{
21728  tree id, type, decl, pushed_scope = NULL_TREE, attributes;
21729  location_t id_location, type_location;
21730  cp_declarator *declarator;
21731  cp_decl_specifier_seq decl_specs;
21732  bool member_p;
21733  const char *saved_message = NULL;
21734
21735  /* Look for the `using' keyword.  */
21736  cp_token *using_token
21737    = cp_parser_require_keyword (parser, RID_USING, RT_USING);
21738  if (using_token == NULL)
21739    return error_mark_node;
21740
21741  id_location = cp_lexer_peek_token (parser->lexer)->location;
21742  id = cp_parser_identifier (parser);
21743  if (id == error_mark_node)
21744    return error_mark_node;
21745
21746  cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
21747  attributes = cp_parser_attributes_opt (parser);
21748  if (attributes == error_mark_node)
21749    return error_mark_node;
21750
21751  cp_parser_require (parser, CPP_EQ, RT_EQ);
21752
21753  if (cp_parser_error_occurred (parser))
21754    return error_mark_node;
21755
21756  cp_parser_commit_to_tentative_parse (parser);
21757
21758  /* Now we are going to parse the type-id of the declaration.  */
21759
21760  /*
21761    [dcl.type]/3 says:
21762
21763	"A type-specifier-seq shall not define a class or enumeration
21764	 unless it appears in the type-id of an alias-declaration (7.1.3) that
21765	 is not the declaration of a template-declaration."
21766
21767    In other words, if we currently are in an alias template, the
21768    type-id should not define a type.
21769
21770    So let's set parser->type_definition_forbidden_message in that
21771    case; cp_parser_check_type_definition (called by
21772    cp_parser_class_specifier) will then emit an error if a type is
21773    defined in the type-id.  */
21774  if (parser->num_template_parameter_lists)
21775    {
21776      saved_message = parser->type_definition_forbidden_message;
21777      parser->type_definition_forbidden_message =
21778	G_("types may not be defined in alias template declarations");
21779    }
21780
21781  type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21782			    &type_location);
21783
21784  /* Restore the error message if need be.  */
21785  if (parser->num_template_parameter_lists)
21786    parser->type_definition_forbidden_message = saved_message;
21787
21788  if (type == error_mark_node
21789      || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21790    {
21791      cp_parser_skip_to_end_of_block_or_statement (parser);
21792      return error_mark_node;
21793    }
21794
21795  /* A typedef-name can also be introduced by an alias-declaration. The
21796     identifier following the using keyword becomes a typedef-name. It has
21797     the same semantics as if it were introduced by the typedef
21798     specifier. In particular, it does not define a new type and it shall
21799     not appear in the type-id.  */
21800
21801  clear_decl_specs (&decl_specs);
21802  decl_specs.type = type;
21803  if (attributes != NULL_TREE)
21804    {
21805      decl_specs.attributes = attributes;
21806      set_and_check_decl_spec_loc (&decl_specs,
21807				   ds_attribute,
21808				   attrs_token);
21809    }
21810  set_and_check_decl_spec_loc (&decl_specs,
21811			       ds_typedef,
21812			       using_token);
21813  set_and_check_decl_spec_loc (&decl_specs,
21814			       ds_alias,
21815			       using_token);
21816  decl_specs.locations[ds_type_spec] = type_location;
21817
21818  if (parser->num_template_parameter_lists
21819      && !cp_parser_check_template_parameters (parser,
21820					       /*num_templates=*/0,
21821					       /*template_id*/false,
21822					       id_location,
21823					       /*declarator=*/NULL))
21824    return error_mark_node;
21825
21826  declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
21827
21828  member_p = at_class_scope_p ();
21829  if (member_p)
21830    decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
21831		      NULL_TREE, attributes);
21832  else
21833    decl = start_decl (declarator, &decl_specs, 0,
21834		       attributes, NULL_TREE, &pushed_scope);
21835  if (decl == error_mark_node)
21836    return decl;
21837
21838  cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
21839
21840  if (pushed_scope)
21841    pop_scope (pushed_scope);
21842
21843  /* If decl is a template, return its TEMPLATE_DECL so that it gets
21844     added into the symbol table; otherwise, return the TYPE_DECL.  */
21845  if (DECL_LANG_SPECIFIC (decl)
21846      && DECL_TEMPLATE_INFO (decl)
21847      && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
21848    {
21849      decl = DECL_TI_TEMPLATE (decl);
21850      if (member_p)
21851	check_member_template (decl);
21852    }
21853
21854  return decl;
21855}
21856
21857/* Parse a using-directive.
21858
21859   using-directive:
21860     attribute-specifier-seq [opt] using namespace :: [opt]
21861       nested-name-specifier [opt] namespace-name ;  */
21862
21863static void
21864cp_parser_using_directive (cp_parser* parser)
21865{
21866  tree namespace_decl;
21867  tree attribs = cp_parser_std_attribute_spec_seq (parser);
21868  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21869    {
21870      /* Error during attribute parsing that resulted in skipping
21871	 to next semicolon.  */
21872      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21873      return;
21874    }
21875
21876  /* Look for the `using' keyword.  */
21877  cp_parser_require_keyword (parser, RID_USING, RT_USING);
21878  /* And the `namespace' keyword.  */
21879  cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21880  /* Look for the optional `::' operator.  */
21881  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21882  /* And the optional nested-name-specifier.  */
21883  cp_parser_nested_name_specifier_opt (parser,
21884				       /*typename_keyword_p=*/false,
21885				       /*check_dependency_p=*/true,
21886				       /*type_p=*/false,
21887				       /*is_declaration=*/true);
21888  /* Get the namespace being used.  */
21889  namespace_decl = cp_parser_namespace_name (parser);
21890  cp_warn_deprecated_use_scopes (namespace_decl);
21891  /* And any specified GNU attributes.  */
21892  if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21893    attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
21894
21895  /* Update the symbol table.  */
21896  finish_using_directive (namespace_decl, attribs);
21897
21898  /* Look for the final `;'.  */
21899  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21900}
21901
21902/* Parse an asm-definition.
21903
21904  asm-qualifier:
21905    volatile
21906    inline
21907    goto
21908
21909  asm-qualifier-list:
21910    asm-qualifier
21911    asm-qualifier-list asm-qualifier
21912
21913   asm-definition:
21914     asm ( string-literal ) ;
21915
21916   GNU Extension:
21917
21918   asm-definition:
21919     asm asm-qualifier-list [opt] ( string-literal ) ;
21920     asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
21921     asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21922				    : asm-operand-list [opt] ) ;
21923     asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21924				    : asm-operand-list [opt]
21925			  : asm-clobber-list [opt] ) ;
21926     asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
21927				    : asm-clobber-list [opt]
21928				    : asm-goto-list ) ;
21929
21930  The form with asm-goto-list is valid if and only if the asm-qualifier-list
21931  contains goto, and is the only allowed form in that case.  No duplicates are
21932  allowed in an asm-qualifier-list.  */
21933
21934static void
21935cp_parser_asm_definition (cp_parser* parser)
21936{
21937  tree string;
21938  tree outputs = NULL_TREE;
21939  tree inputs = NULL_TREE;
21940  tree clobbers = NULL_TREE;
21941  tree labels = NULL_TREE;
21942  tree asm_stmt;
21943  bool extended_p = false;
21944  bool invalid_inputs_p = false;
21945  bool invalid_outputs_p = false;
21946  required_token missing = RT_NONE;
21947  location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
21948
21949  /* Look for the `asm' keyword.  */
21950  cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
21951
21952  /* In C++20, unevaluated inline assembly is permitted in constexpr
21953     functions.  */
21954  if (parser->in_function_body
21955      && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
21956      && cxx_dialect < cxx20)
21957    pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
21958	     "function only available with %<-std=c++20%> or "
21959	     "%<-std=gnu++20%>");
21960
21961  /* Handle the asm-qualifier-list.  */
21962  location_t volatile_loc = UNKNOWN_LOCATION;
21963  location_t inline_loc = UNKNOWN_LOCATION;
21964  location_t goto_loc = UNKNOWN_LOCATION;
21965  location_t first_loc = UNKNOWN_LOCATION;
21966
21967  if (cp_parser_allow_gnu_extensions_p (parser))
21968    for (;;)
21969      {
21970	cp_token *token = cp_lexer_peek_token (parser->lexer);
21971	location_t loc = token->location;
21972	switch (cp_lexer_peek_token (parser->lexer)->keyword)
21973	  {
21974	  case RID_VOLATILE:
21975	    if (volatile_loc)
21976	      {
21977		error_at (loc, "duplicate %<asm%> qualifier %qT",
21978			  token->u.value);
21979		inform (volatile_loc, "first seen here");
21980	      }
21981	    else
21982	      {
21983		if (!parser->in_function_body)
21984		  warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
21985			      "outside of function body", token->u.value);
21986		volatile_loc = loc;
21987	      }
21988	    cp_lexer_consume_token (parser->lexer);
21989	    continue;
21990
21991	  case RID_INLINE:
21992	    if (inline_loc)
21993	      {
21994		error_at (loc, "duplicate %<asm%> qualifier %qT",
21995			  token->u.value);
21996		inform (inline_loc, "first seen here");
21997	      }
21998	    else
21999	      inline_loc = loc;
22000	    if (!first_loc)
22001	      first_loc = loc;
22002	    cp_lexer_consume_token (parser->lexer);
22003	    continue;
22004
22005	  case RID_GOTO:
22006	    if (goto_loc)
22007	      {
22008		error_at (loc, "duplicate %<asm%> qualifier %qT",
22009			  token->u.value);
22010		inform (goto_loc, "first seen here");
22011	      }
22012	    else
22013	      goto_loc = loc;
22014	    if (!first_loc)
22015	      first_loc = loc;
22016	    cp_lexer_consume_token (parser->lexer);
22017	    continue;
22018
22019	  case RID_CONST:
22020	  case RID_RESTRICT:
22021	    error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22022	    cp_lexer_consume_token (parser->lexer);
22023	    continue;
22024
22025	  default:
22026	    break;
22027	  }
22028	break;
22029      }
22030
22031  bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22032  bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22033  bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22034
22035  if (!parser->in_function_body && (inline_p || goto_p))
22036    {
22037      error_at (first_loc, "%<asm%> qualifier outside of function body");
22038      inline_p = goto_p = false;
22039    }
22040
22041  /* Look for the opening `('.  */
22042  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22043    return;
22044  /* Look for the string.  */
22045  string = cp_parser_string_literal (parser, false, false);
22046  if (string == error_mark_node)
22047    {
22048      cp_parser_skip_to_closing_parenthesis (parser, true, false,
22049					     /*consume_paren=*/true);
22050      return;
22051    }
22052
22053  /* If we're allowing GNU extensions, check for the extended assembly
22054     syntax.  Unfortunately, the `:' tokens need not be separated by
22055     a space in C, and so, for compatibility, we tolerate that here
22056     too.  Doing that means that we have to treat the `::' operator as
22057     two `:' tokens.  */
22058  if (cp_parser_allow_gnu_extensions_p (parser)
22059      && parser->in_function_body
22060      && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22061	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22062    {
22063      bool inputs_p = false;
22064      bool clobbers_p = false;
22065      bool labels_p = false;
22066
22067      /* The extended syntax was used.  */
22068      extended_p = true;
22069
22070      /* Look for outputs.  */
22071      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22072	{
22073	  /* Consume the `:'.  */
22074	  cp_lexer_consume_token (parser->lexer);
22075	  /* Parse the output-operands.  */
22076	  if (cp_lexer_next_token_is_not (parser->lexer,
22077					  CPP_COLON)
22078	      && cp_lexer_next_token_is_not (parser->lexer,
22079					     CPP_SCOPE)
22080	      && cp_lexer_next_token_is_not (parser->lexer,
22081					     CPP_CLOSE_PAREN))
22082            {
22083              outputs = cp_parser_asm_operand_list (parser);
22084              if (outputs == error_mark_node)
22085                invalid_outputs_p = true;
22086            }
22087	}
22088      /* If the next token is `::', there are no outputs, and the
22089	 next token is the beginning of the inputs.  */
22090      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22091	/* The inputs are coming next.  */
22092	inputs_p = true;
22093
22094      /* Look for inputs.  */
22095      if (inputs_p
22096	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22097	{
22098	  /* Consume the `:' or `::'.  */
22099	  cp_lexer_consume_token (parser->lexer);
22100	  /* Parse the output-operands.  */
22101	  if (cp_lexer_next_token_is_not (parser->lexer,
22102					  CPP_COLON)
22103	      && cp_lexer_next_token_is_not (parser->lexer,
22104					     CPP_SCOPE)
22105	      && cp_lexer_next_token_is_not (parser->lexer,
22106					     CPP_CLOSE_PAREN))
22107            {
22108              inputs = cp_parser_asm_operand_list (parser);
22109              if (inputs == error_mark_node)
22110                invalid_inputs_p = true;
22111            }
22112	}
22113      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22114	/* The clobbers are coming next.  */
22115	clobbers_p = true;
22116
22117      /* Look for clobbers.  */
22118      if (clobbers_p
22119	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22120	{
22121	  clobbers_p = true;
22122	  /* Consume the `:' or `::'.  */
22123	  cp_lexer_consume_token (parser->lexer);
22124	  /* Parse the clobbers.  */
22125	  if (cp_lexer_next_token_is_not (parser->lexer,
22126					  CPP_COLON)
22127	      && cp_lexer_next_token_is_not (parser->lexer,
22128					     CPP_CLOSE_PAREN))
22129	    clobbers = cp_parser_asm_clobber_list (parser);
22130	}
22131      else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22132	/* The labels are coming next.  */
22133	labels_p = true;
22134
22135      /* Look for labels.  */
22136      if (labels_p
22137	  || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22138	{
22139	  labels_p = true;
22140	  /* Consume the `:' or `::'.  */
22141	  cp_lexer_consume_token (parser->lexer);
22142	  /* Parse the labels.  */
22143	  labels = cp_parser_asm_label_list (parser);
22144	}
22145
22146      if (goto_p && !labels_p)
22147	missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22148    }
22149  else if (goto_p)
22150    missing = RT_COLON_SCOPE;
22151
22152  /* Look for the closing `)'.  */
22153  if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22154			  missing ? missing : RT_CLOSE_PAREN))
22155    cp_parser_skip_to_closing_parenthesis (parser, true, false,
22156					   /*consume_paren=*/true);
22157  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22158
22159  if (!invalid_inputs_p && !invalid_outputs_p)
22160    {
22161      /* Create the ASM_EXPR.  */
22162      if (parser->in_function_body)
22163	{
22164	  asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22165				      inputs, clobbers, labels, inline_p);
22166	  /* If the extended syntax was not used, mark the ASM_EXPR.  */
22167	  if (!extended_p)
22168	    {
22169	      tree temp = asm_stmt;
22170	      if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22171		temp = TREE_OPERAND (temp, 0);
22172
22173	      ASM_INPUT_P (temp) = 1;
22174	    }
22175	}
22176      else
22177	symtab->finalize_toplevel_asm (string);
22178    }
22179}
22180
22181/* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22182   type that comes from the decl-specifier-seq.  */
22183
22184static tree
22185strip_declarator_types (tree type, cp_declarator *declarator)
22186{
22187  for (cp_declarator *d = declarator; d;)
22188    switch (d->kind)
22189      {
22190      case cdk_id:
22191      case cdk_decomp:
22192      case cdk_error:
22193	d = NULL;
22194	break;
22195
22196      default:
22197	if (TYPE_PTRMEMFUNC_P (type))
22198	  type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22199	type = TREE_TYPE (type);
22200	d = d->declarator;
22201	break;
22202      }
22203
22204  return type;
22205}
22206
22207/* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22208   a construct looks like a variable definition but is actually a function
22209   declaration.  DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22210   is the declarator for this function declaration.  */
22211
22212static void
22213warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22214			    const cp_declarator *declarator)
22215{
22216  /* Only warn if we are declaring a function at block scope.  */
22217  if (!at_function_scope_p ())
22218    return;
22219
22220  /* And only if there is no storage class specified.  */
22221  if (decl_specifiers->storage_class != sc_none
22222      || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22223    return;
22224
22225  if (declarator->kind != cdk_function
22226      || !declarator->declarator
22227      || declarator->declarator->kind != cdk_id
22228      || !identifier_p (get_unqualified_id
22229			(const_cast<cp_declarator *>(declarator))))
22230    return;
22231
22232  /* Don't warn when the whole declarator (not just the declarator-id!)
22233     was parenthesized.  That is, don't warn for int(n()) but do warn
22234     for int(f)().  */
22235  if (declarator->parenthesized != UNKNOWN_LOCATION)
22236    return;
22237
22238  tree type;
22239  if (decl_specifiers->type)
22240    {
22241      type = decl_specifiers->type;
22242      if (TREE_CODE (type) == TYPE_DECL)
22243	type = TREE_TYPE (type);
22244
22245      /* If the return type is void there is no ambiguity.  */
22246      if (same_type_p (type, void_type_node))
22247	return;
22248    }
22249  else if (decl_specifiers->any_type_specifiers_p)
22250    /* Code like long f(); will have null ->type.  If we have any
22251       type-specifiers, pretend we've seen int.  */
22252    type = integer_type_node;
22253  else
22254    return;
22255
22256  auto_diagnostic_group d;
22257  location_t loc = declarator->u.function.parens_loc;
22258  tree params = declarator->u.function.parameters;
22259  const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22260
22261  /* The T t() case.  */
22262  if (params == void_list_node)
22263    {
22264      if (warning_at (loc, OPT_Wvexing_parse,
22265		      "empty parentheses were disambiguated as a function "
22266		      "declaration"))
22267	{
22268	  /* () means value-initialization (C++03 and up); {} (C++11 and up)
22269	     means value-initialization or aggregate-initialization, nothing
22270	     means default-initialization.  We can only suggest removing the
22271	     parentheses/adding {} if T has a default constructor.  */
22272	  if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22273	    {
22274	      gcc_rich_location iloc (loc);
22275	      iloc.add_fixit_remove ();
22276	      inform (&iloc, "remove parentheses to default-initialize "
22277		      "a variable");
22278	      if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22279		{
22280		  if (CP_AGGREGATE_TYPE_P (type))
22281		    inform (loc, "or replace parentheses with braces to "
22282			    "aggregate-initialize a variable");
22283		  else
22284		    inform (loc, "or replace parentheses with braces to "
22285			    "value-initialize a variable");
22286		}
22287	    }
22288	}
22289      return;
22290    }
22291
22292  /* If we had (...) or the parameter-list wasn't parenthesized,
22293     we're done.  */
22294  if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22295    return;
22296
22297  /* The T t(X()) case.  */
22298  if (list_length (params) == 2)
22299    {
22300      if (warning_at (loc, OPT_Wvexing_parse,
22301		      "parentheses were disambiguated as a function "
22302		      "declaration"))
22303	{
22304	  gcc_rich_location iloc (loc);
22305	  /* {}-initialization means that we can use an initializer-list
22306	     constructor if no default constructor is available, so don't
22307	     suggest using {} for classes that have an initializer_list
22308	     constructor.  */
22309	  if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22310	    {
22311	      iloc.add_fixit_replace (get_start (loc), "{");
22312	      iloc.add_fixit_replace (get_finish (loc), "}");
22313	      inform (&iloc, "replace parentheses with braces to declare a "
22314		      "variable");
22315	    }
22316	  else
22317	    {
22318	      iloc.add_fixit_insert_after (get_start (loc), "(");
22319	      iloc.add_fixit_insert_before (get_finish (loc), ")");
22320	      inform (&iloc, "add parentheses to declare a variable");
22321	    }
22322	}
22323    }
22324  /* The T t(X(), X()) case.  */
22325  else if (warning_at (loc, OPT_Wvexing_parse,
22326		       "parentheses were disambiguated as a function "
22327		       "declaration"))
22328    {
22329      gcc_rich_location iloc (loc);
22330      if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22331	{
22332	  iloc.add_fixit_replace (get_start (loc), "{");
22333	  iloc.add_fixit_replace (get_finish (loc), "}");
22334	  inform (&iloc, "replace parentheses with braces to declare a "
22335		  "variable");
22336	}
22337    }
22338}
22339
22340/* If DECLARATOR with DECL_SPECS is a function declarator that has
22341   the form of a deduction guide, tag it as such.  CTOR_DTOR_OR_CONV_P
22342   has the same meaning as in cp_parser_declarator.  */
22343
22344static void
22345cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22346					      cp_decl_specifier_seq *decl_specs,
22347					      cp_declarator *declarator,
22348					      int *ctor_dtor_or_conv_p)
22349{
22350  if (cxx_dialect >= cxx17
22351      && *ctor_dtor_or_conv_p <= 0
22352      && !decl_specs->type
22353      && !decl_specs->any_type_specifiers_p
22354      && function_declarator_p (declarator))
22355    {
22356      cp_declarator *id = get_id_declarator (declarator);
22357      tree name = id->u.id.unqualified_name;
22358      parser->scope = id->u.id.qualifying_scope;
22359      tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22360      if (tmpl
22361	  && (DECL_CLASS_TEMPLATE_P (tmpl)
22362	      || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22363	{
22364	  id->u.id.unqualified_name = dguide_name (tmpl);
22365	  id->u.id.sfk = sfk_deduction_guide;
22366	  *ctor_dtor_or_conv_p = 1;
22367	}
22368    }
22369}
22370
22371/* Declarators [gram.dcl.decl] */
22372
22373/* Parse an init-declarator.
22374
22375   init-declarator:
22376     declarator initializer [opt]
22377
22378   GNU Extension:
22379
22380   init-declarator:
22381     declarator asm-specification [opt] attributes [opt] initializer [opt]
22382
22383   function-definition:
22384     decl-specifier-seq [opt] declarator ctor-initializer [opt]
22385       function-body
22386     decl-specifier-seq [opt] declarator function-try-block
22387
22388   GNU Extension:
22389
22390   function-definition:
22391     __extension__ function-definition
22392
22393   TM Extension:
22394
22395   function-definition:
22396     decl-specifier-seq [opt] declarator function-transaction-block
22397
22398   The parser flags FLAGS is used to control type-specifier parsing.
22399
22400   The DECL_SPECIFIERS apply to this declarator.  Returns a
22401   representation of the entity declared.  If MEMBER_P is TRUE, then
22402   this declarator appears in a class scope.  The new DECL created by
22403   this declarator is returned.
22404
22405   The CHECKS are access checks that should be performed once we know
22406   what entity is being declared (and, therefore, what classes have
22407   befriended it).
22408
22409   If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22410   for a function-definition here as well.  If the declarator is a
22411   declarator for a function-definition, *FUNCTION_DEFINITION_P will
22412   be TRUE upon return.  By that point, the function-definition will
22413   have been completely parsed.
22414
22415   FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22416   is FALSE.
22417
22418   If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22419   parsed declaration if it is an uninitialized single declarator not followed
22420   by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22421   if present, will not be consumed.  If returned, this declarator will be
22422   created with SD_INITIALIZED but will not call cp_finish_decl.
22423
22424   If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22425   and there is an initializer, the pointed location_t is set to the
22426   location of the '=' or `(', or '{' in C++11 token introducing the
22427   initializer.  */
22428
22429static tree
22430cp_parser_init_declarator (cp_parser* parser,
22431			   cp_parser_flags flags,
22432			   cp_decl_specifier_seq *decl_specifiers,
22433			   vec<deferred_access_check, va_gc> *checks,
22434			   bool function_definition_allowed_p,
22435			   bool member_p,
22436			   int declares_class_or_enum,
22437			   bool* function_definition_p,
22438			   tree* maybe_range_for_decl,
22439			   location_t* init_loc,
22440			   tree* auto_result)
22441{
22442  cp_token *token = NULL, *asm_spec_start_token = NULL,
22443           *attributes_start_token = NULL;
22444  cp_declarator *declarator;
22445  tree prefix_attributes;
22446  tree attributes = NULL;
22447  tree asm_specification;
22448  tree initializer;
22449  tree decl = NULL_TREE;
22450  tree scope;
22451  int is_initialized;
22452  /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
22453     initialized with "= ..", CPP_OPEN_PAREN if initialized with
22454     "(...)".  */
22455  enum cpp_ttype initialization_kind;
22456  bool is_direct_init = false;
22457  bool is_non_constant_init;
22458  int ctor_dtor_or_conv_p;
22459  bool friend_p = cp_parser_friend_p (decl_specifiers);
22460  bool static_p = decl_specifiers->storage_class == sc_static;
22461  tree pushed_scope = NULL_TREE;
22462  bool range_for_decl_p = false;
22463  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22464  location_t tmp_init_loc = UNKNOWN_LOCATION;
22465
22466  if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22467    flags |= CP_PARSER_FLAGS_CONSTEVAL;
22468
22469  /* Assume that this is not the declarator for a function
22470     definition.  */
22471  if (function_definition_p)
22472    *function_definition_p = false;
22473
22474  /* Default arguments are only permitted for function parameters.  */
22475  if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22476    parser->default_arg_ok_p = false;
22477
22478  /* Defer access checks while parsing the declarator; we cannot know
22479     what names are accessible until we know what is being
22480     declared.  */
22481  resume_deferring_access_checks ();
22482
22483  token = cp_lexer_peek_token (parser->lexer);
22484
22485  /* Parse the declarator.  */
22486  declarator
22487    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22488			    flags, &ctor_dtor_or_conv_p,
22489			    /*parenthesized_p=*/NULL,
22490			    member_p, friend_p, static_p);
22491  /* Gather up the deferred checks.  */
22492  stop_deferring_access_checks ();
22493
22494  parser->default_arg_ok_p = saved_default_arg_ok_p;
22495
22496  /* If the DECLARATOR was erroneous, there's no need to go
22497     further.  */
22498  if (declarator == cp_error_declarator)
22499    return error_mark_node;
22500
22501  /* Check that the number of template-parameter-lists is OK.  */
22502  if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22503						       token->location))
22504    return error_mark_node;
22505
22506  if (declares_class_or_enum & 2)
22507    cp_parser_check_for_definition_in_return_type (declarator,
22508						   decl_specifiers->type,
22509						   decl_specifiers->locations[ds_type_spec]);
22510
22511  /* Figure out what scope the entity declared by the DECLARATOR is
22512     located in.  `grokdeclarator' sometimes changes the scope, so
22513     we compute it now.  */
22514  scope = get_scope_of_declarator (declarator);
22515
22516  /* Perform any lookups in the declared type which were thought to be
22517     dependent, but are not in the scope of the declarator.  */
22518  decl_specifiers->type
22519    = maybe_update_decl_type (decl_specifiers->type, scope);
22520
22521  /* If we're allowing GNU extensions, look for an
22522     asm-specification.  */
22523  if (cp_parser_allow_gnu_extensions_p (parser))
22524    {
22525      /* Look for an asm-specification.  */
22526      asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22527      asm_specification = cp_parser_asm_specification_opt (parser);
22528    }
22529  else
22530    asm_specification = NULL_TREE;
22531
22532  /* Gather the attributes that were provided with the
22533     decl-specifiers.  */
22534  prefix_attributes = decl_specifiers->attributes;
22535
22536  /* Look for attributes.  */
22537  attributes_start_token = cp_lexer_peek_token (parser->lexer);
22538  attributes = cp_parser_attributes_opt (parser);
22539
22540  /* Peek at the next token.  */
22541  token = cp_lexer_peek_token (parser->lexer);
22542
22543  bool bogus_implicit_tmpl = false;
22544
22545  if (function_declarator_p (declarator))
22546    {
22547      /* Handle C++17 deduction guides.  Note that class-scope
22548	 non-template deduction guides are instead handled in
22549	 cp_parser_member_declaration.  */
22550      cp_parser_maybe_adjust_declarator_for_dguide (parser,
22551						    decl_specifiers,
22552						    declarator,
22553						    &ctor_dtor_or_conv_p);
22554
22555      if (!member_p && !cp_parser_error_occurred (parser))
22556	warn_about_ambiguous_parse (decl_specifiers, declarator);
22557
22558      /* Check to see if the token indicates the start of a
22559	 function-definition.  */
22560      if (cp_parser_token_starts_function_definition_p (token))
22561	{
22562	  if (!function_definition_allowed_p)
22563	    {
22564	      /* If a function-definition should not appear here, issue an
22565		 error message.  */
22566	      cp_parser_error (parser,
22567			       "a function-definition is not allowed here");
22568	      return error_mark_node;
22569	    }
22570
22571	  location_t func_brace_location
22572	    = cp_lexer_peek_token (parser->lexer)->location;
22573
22574	  /* Neither attributes nor an asm-specification are allowed
22575	     on a function-definition.  */
22576	  if (asm_specification)
22577	    error_at (asm_spec_start_token->location,
22578		      "an %<asm%> specification is not allowed "
22579		      "on a function-definition");
22580	  if (attributes)
22581	    error_at (attributes_start_token->location,
22582		      "attributes are not allowed "
22583		      "on a function-definition");
22584	  /* This is a function-definition.  */
22585	  *function_definition_p = true;
22586
22587	  /* Parse the function definition.  */
22588	  if (member_p)
22589	    decl = cp_parser_save_member_function_body (parser,
22590							decl_specifiers,
22591							declarator,
22592							prefix_attributes);
22593	  else
22594	    decl =
22595	      (cp_parser_function_definition_from_specifiers_and_declarator
22596	       (parser, decl_specifiers, prefix_attributes, declarator));
22597
22598	  if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
22599	    {
22600	      /* This is where the prologue starts...  */
22601	      DECL_STRUCT_FUNCTION (decl)->function_start_locus
22602		= func_brace_location;
22603	    }
22604
22605	  return decl;
22606	}
22607    }
22608  else if (parser->fully_implicit_function_template_p)
22609    {
22610      /* A non-template declaration involving a function parameter list
22611	 containing an implicit template parameter will be made into a
22612	 template.  If the resulting declaration is not going to be an
22613	 actual function then finish the template scope here to prevent it.
22614	 An error message will be issued once we have a decl to talk about.
22615
22616         FIXME probably we should do type deduction rather than create an
22617         implicit template, but the standard currently doesn't allow it. */
22618      bogus_implicit_tmpl = true;
22619      finish_fully_implicit_template (parser, NULL_TREE);
22620    }
22621
22622  /* [dcl.dcl]
22623
22624     Only in function declarations for constructors, destructors, type
22625     conversions, and deduction guides can the decl-specifier-seq be omitted.
22626
22627     We explicitly postpone this check past the point where we handle
22628     function-definitions because we tolerate function-definitions
22629     that are missing their return types in some modes.  */
22630  if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
22631    {
22632      cp_parser_error (parser,
22633		       "expected constructor, destructor, or type conversion");
22634      return error_mark_node;
22635    }
22636
22637  /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
22638  if (token->type == CPP_EQ
22639      || token->type == CPP_OPEN_PAREN
22640      || token->type == CPP_OPEN_BRACE)
22641    {
22642      is_initialized = SD_INITIALIZED;
22643      initialization_kind = token->type;
22644      declarator->init_loc = token->location;
22645      if (maybe_range_for_decl)
22646	*maybe_range_for_decl = error_mark_node;
22647      tmp_init_loc = token->location;
22648      if (init_loc && *init_loc == UNKNOWN_LOCATION)
22649	*init_loc = tmp_init_loc;
22650
22651      if (token->type == CPP_EQ
22652	  && function_declarator_p (declarator))
22653	{
22654	  cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
22655	  if (t2->keyword == RID_DEFAULT)
22656	    is_initialized = SD_DEFAULTED;
22657	  else if (t2->keyword == RID_DELETE)
22658	    is_initialized = SD_DELETED;
22659	}
22660    }
22661  else
22662    {
22663      /* If the init-declarator isn't initialized and isn't followed by a
22664	 `,' or `;', it's not a valid init-declarator.  */
22665      if (token->type != CPP_COMMA
22666	  && token->type != CPP_SEMICOLON)
22667	{
22668	  if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
22669	    range_for_decl_p = true;
22670	  else
22671	    {
22672	      if (!maybe_range_for_decl)
22673		cp_parser_error (parser, "expected initializer");
22674	      return error_mark_node;
22675	    }
22676	}
22677      is_initialized = SD_UNINITIALIZED;
22678      initialization_kind = CPP_EOF;
22679    }
22680
22681  /* Because start_decl has side-effects, we should only call it if we
22682     know we're going ahead.  By this point, we know that we cannot
22683     possibly be looking at any other construct.  */
22684  cp_parser_commit_to_tentative_parse (parser);
22685
22686  /* Enter the newly declared entry in the symbol table.  If we're
22687     processing a declaration in a class-specifier, we wait until
22688     after processing the initializer.  */
22689  if (!member_p)
22690    {
22691      if (parser->in_unbraced_linkage_specification_p)
22692	decl_specifiers->storage_class = sc_extern;
22693      decl = start_decl (declarator, decl_specifiers,
22694			 range_for_decl_p? SD_INITIALIZED : is_initialized,
22695			 attributes, prefix_attributes, &pushed_scope);
22696      cp_finalize_omp_declare_simd (parser, decl);
22697      cp_finalize_oacc_routine (parser, decl, false);
22698      /* Adjust location of decl if declarator->id_loc is more appropriate:
22699	 set, and decl wasn't merged with another decl, in which case its
22700	 location would be different from input_location, and more accurate.  */
22701      if (DECL_P (decl)
22702	  && declarator->id_loc != UNKNOWN_LOCATION
22703	  && DECL_SOURCE_LOCATION (decl) == input_location)
22704	DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
22705    }
22706  else if (scope)
22707    /* Enter the SCOPE.  That way unqualified names appearing in the
22708       initializer will be looked up in SCOPE.  */
22709    pushed_scope = push_scope (scope);
22710
22711  /* Perform deferred access control checks, now that we know in which
22712     SCOPE the declared entity resides.  */
22713  if (!member_p && decl)
22714    {
22715      tree saved_current_function_decl = NULL_TREE;
22716
22717      /* If the entity being declared is a function, pretend that we
22718	 are in its scope.  If it is a `friend', it may have access to
22719	 things that would not otherwise be accessible.  */
22720      if (TREE_CODE (decl) == FUNCTION_DECL)
22721	{
22722	  saved_current_function_decl = current_function_decl;
22723	  current_function_decl = decl;
22724	}
22725
22726      /* Perform access checks for template parameters.  */
22727      cp_parser_perform_template_parameter_access_checks (checks);
22728
22729      /* Perform the access control checks for the declarator and the
22730	 decl-specifiers.  */
22731      perform_deferred_access_checks (tf_warning_or_error);
22732
22733      /* Restore the saved value.  */
22734      if (TREE_CODE (decl) == FUNCTION_DECL)
22735	current_function_decl = saved_current_function_decl;
22736    }
22737
22738  /* Parse the initializer.  */
22739  initializer = NULL_TREE;
22740  is_direct_init = false;
22741  is_non_constant_init = true;
22742  if (is_initialized)
22743    {
22744      if (function_declarator_p (declarator))
22745	{
22746	   if (initialization_kind == CPP_EQ)
22747	     initializer = cp_parser_pure_specifier (parser);
22748	   else
22749	     {
22750	       /* If the declaration was erroneous, we don't really
22751		  know what the user intended, so just silently
22752		  consume the initializer.  */
22753	       if (decl != error_mark_node)
22754		 error_at (tmp_init_loc, "initializer provided for function");
22755	       cp_parser_skip_to_closing_parenthesis (parser,
22756						      /*recovering=*/true,
22757						      /*or_comma=*/false,
22758						      /*consume_paren=*/true);
22759	     }
22760	}
22761      else
22762	{
22763	  /* We want to record the extra mangling scope for in-class
22764	     initializers of class members and initializers of static
22765	     data member templates and namespace-scope initializers.
22766	     The former involves deferring parsing of the initializer
22767	     until end of class as with default arguments.  So right
22768	     here we only handle the latter two.  */
22769	  bool has_lambda_scope = false;
22770
22771	  if (decl != error_mark_node
22772	      && !member_p
22773	      && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
22774	    has_lambda_scope = true;
22775
22776	  if (has_lambda_scope)
22777	    start_lambda_scope (decl);
22778	  initializer = cp_parser_initializer (parser,
22779					       &is_direct_init,
22780					       &is_non_constant_init);
22781	  if (has_lambda_scope)
22782	    finish_lambda_scope ();
22783	  if (initializer == error_mark_node)
22784	    cp_parser_skip_to_end_of_statement (parser);
22785	}
22786    }
22787
22788  /* The old parser allows attributes to appear after a parenthesized
22789     initializer.  Mark Mitchell proposed removing this functionality
22790     on the GCC mailing lists on 2002-08-13.  This parser accepts the
22791     attributes -- but ignores them.  Made a permerror in GCC 8.  */
22792  if (cp_parser_allow_gnu_extensions_p (parser)
22793      && initialization_kind == CPP_OPEN_PAREN
22794      && cp_parser_attributes_opt (parser)
22795      && permerror (input_location,
22796		    "attributes after parenthesized initializer ignored"))
22797    {
22798      static bool hint;
22799      if (flag_permissive && !hint)
22800	{
22801	  hint = true;
22802	  inform (input_location,
22803		  "this flexibility is deprecated and will be removed");
22804	}
22805    }
22806
22807  /* And now complain about a non-function implicit template.  */
22808  if (bogus_implicit_tmpl && decl != error_mark_node)
22809    error_at (DECL_SOURCE_LOCATION (decl),
22810	      "non-function %qD declared as implicit template", decl);
22811
22812  /* For an in-class declaration, use `grokfield' to create the
22813     declaration.  */
22814  if (member_p)
22815    {
22816      if (pushed_scope)
22817	{
22818	  pop_scope (pushed_scope);
22819	  pushed_scope = NULL_TREE;
22820	}
22821      decl = grokfield (declarator, decl_specifiers,
22822			initializer, !is_non_constant_init,
22823			/*asmspec=*/NULL_TREE,
22824			attr_chainon (attributes, prefix_attributes));
22825      if (decl && TREE_CODE (decl) == FUNCTION_DECL)
22826	cp_parser_save_default_args (parser, decl);
22827      cp_finalize_omp_declare_simd (parser, decl);
22828      cp_finalize_oacc_routine (parser, decl, false);
22829    }
22830
22831  /* Finish processing the declaration.  But, skip member
22832     declarations.  */
22833  if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
22834    {
22835      cp_finish_decl (decl,
22836		      initializer, !is_non_constant_init,
22837		      asm_specification,
22838		      /* If the initializer is in parentheses, then this is
22839			 a direct-initialization, which means that an
22840			 `explicit' constructor is OK.  Otherwise, an
22841			 `explicit' constructor cannot be used.  */
22842		      ((is_direct_init || !is_initialized)
22843		       ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
22844    }
22845  else if ((cxx_dialect != cxx98) && friend_p
22846	   && decl && TREE_CODE (decl) == FUNCTION_DECL)
22847    /* Core issue #226 (C++0x only): A default template-argument
22848       shall not be specified in a friend class template
22849       declaration. */
22850    check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
22851                             /*is_partial=*/false, /*is_friend_decl=*/1);
22852
22853  if (!friend_p && pushed_scope)
22854    pop_scope (pushed_scope);
22855
22856  if (function_declarator_p (declarator)
22857      && parser->fully_implicit_function_template_p)
22858    {
22859      if (member_p)
22860	decl = finish_fully_implicit_template (parser, decl);
22861      else
22862	finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22863    }
22864
22865  if (auto_result && is_initialized && decl_specifiers->type
22866      && type_uses_auto (decl_specifiers->type))
22867    *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
22868
22869  return decl;
22870}
22871
22872/* Parse a declarator.
22873
22874   declarator:
22875     direct-declarator
22876     ptr-operator declarator
22877
22878   abstract-declarator:
22879     ptr-operator abstract-declarator [opt]
22880     direct-abstract-declarator
22881
22882   GNU Extensions:
22883
22884   declarator:
22885     attributes [opt] direct-declarator
22886     attributes [opt] ptr-operator declarator
22887
22888   abstract-declarator:
22889     attributes [opt] ptr-operator abstract-declarator [opt]
22890     attributes [opt] direct-abstract-declarator
22891
22892   The parser flags FLAGS is used to control type-specifier parsing.
22893
22894   If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
22895   detect constructors, destructors, deduction guides, or conversion operators.
22896   It is set to -1 if the declarator is a name, and +1 if it is a
22897   function. Otherwise it is set to zero. Usually you just want to
22898   test for >0, but internally the negative value is used.
22899
22900   (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
22901   a decl-specifier-seq unless it declares a constructor, destructor,
22902   or conversion.  It might seem that we could check this condition in
22903   semantic analysis, rather than parsing, but that makes it difficult
22904   to handle something like `f()'.  We want to notice that there are
22905   no decl-specifiers, and therefore realize that this is an
22906   expression, not a declaration.)
22907
22908   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
22909   the declarator is a direct-declarator of the form "(...)".
22910
22911   MEMBER_P is true iff this declarator is a member-declarator.
22912
22913   FRIEND_P is true iff this declarator is a friend.
22914
22915   STATIC_P is true iff the keyword static was seen.  */
22916
22917static cp_declarator *
22918cp_parser_declarator (cp_parser* parser,
22919		      cp_parser_declarator_kind dcl_kind,
22920		      cp_parser_flags flags,
22921		      int* ctor_dtor_or_conv_p,
22922		      bool* parenthesized_p,
22923		      bool member_p, bool friend_p, bool static_p)
22924{
22925  cp_declarator *declarator;
22926  enum tree_code code;
22927  cp_cv_quals cv_quals;
22928  tree class_type;
22929  tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
22930
22931  /* Assume this is not a constructor, destructor, or type-conversion
22932     operator.  */
22933  if (ctor_dtor_or_conv_p)
22934    *ctor_dtor_or_conv_p = 0;
22935
22936  if (cp_parser_allow_gnu_extensions_p (parser))
22937    gnu_attributes = cp_parser_gnu_attributes_opt (parser);
22938
22939  /* Check for the ptr-operator production.  */
22940  cp_parser_parse_tentatively (parser);
22941  /* Parse the ptr-operator.  */
22942  code = cp_parser_ptr_operator (parser,
22943				 &class_type,
22944				 &cv_quals,
22945				 &std_attributes);
22946
22947  /* If that worked, then we have a ptr-operator.  */
22948  if (cp_parser_parse_definitely (parser))
22949    {
22950      /* If a ptr-operator was found, then this declarator was not
22951	 parenthesized.  */
22952      if (parenthesized_p)
22953	*parenthesized_p = false;
22954      /* The dependent declarator is optional if we are parsing an
22955	 abstract-declarator.  */
22956      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
22957	cp_parser_parse_tentatively (parser);
22958
22959      /* Parse the dependent declarator.  */
22960      declarator = cp_parser_declarator (parser, dcl_kind, flags,
22961					 /*ctor_dtor_or_conv_p=*/NULL,
22962					 /*parenthesized_p=*/NULL,
22963					 member_p, friend_p, static_p);
22964
22965      /* If we are parsing an abstract-declarator, we must handle the
22966	 case where the dependent declarator is absent.  */
22967      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
22968	  && !cp_parser_parse_definitely (parser))
22969	declarator = NULL;
22970
22971      declarator = cp_parser_make_indirect_declarator
22972	(code, class_type, cv_quals, declarator, std_attributes);
22973    }
22974  /* Everything else is a direct-declarator.  */
22975  else
22976    {
22977      if (parenthesized_p)
22978	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
22979						   CPP_OPEN_PAREN);
22980      declarator = cp_parser_direct_declarator (parser, dcl_kind,
22981						flags, ctor_dtor_or_conv_p,
22982						member_p, friend_p, static_p);
22983    }
22984
22985  if (gnu_attributes && declarator && declarator != cp_error_declarator)
22986    declarator->attributes = gnu_attributes;
22987  return declarator;
22988}
22989
22990/* Parse a direct-declarator or direct-abstract-declarator.
22991
22992   direct-declarator:
22993     declarator-id
22994     direct-declarator ( parameter-declaration-clause )
22995       cv-qualifier-seq [opt]
22996       ref-qualifier [opt]
22997       exception-specification [opt]
22998     direct-declarator [ constant-expression [opt] ]
22999     ( declarator )
23000
23001   direct-abstract-declarator:
23002     direct-abstract-declarator [opt]
23003       ( parameter-declaration-clause )
23004       cv-qualifier-seq [opt]
23005       ref-qualifier [opt]
23006       exception-specification [opt]
23007     direct-abstract-declarator [opt] [ constant-expression [opt] ]
23008     ( abstract-declarator )
23009
23010   Returns a representation of the declarator.  DCL_KIND is
23011   CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23012   direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
23013   we are parsing a direct-declarator.  It is
23014   CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23015   of ambiguity we prefer an abstract declarator, as per
23016   [dcl.ambig.res].
23017   The parser flags FLAGS is used to control type-specifier parsing.
23018   CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23019   as for cp_parser_declarator.  */
23020
23021static cp_declarator *
23022cp_parser_direct_declarator (cp_parser* parser,
23023			     cp_parser_declarator_kind dcl_kind,
23024			     cp_parser_flags flags,
23025			     int* ctor_dtor_or_conv_p,
23026			     bool member_p, bool friend_p, bool static_p)
23027{
23028  cp_token *token;
23029  cp_declarator *declarator = NULL;
23030  tree scope = NULL_TREE;
23031  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23032  bool saved_in_declarator_p = parser->in_declarator_p;
23033  bool first = true;
23034  tree pushed_scope = NULL_TREE;
23035  cp_token *open_paren = NULL, *close_paren = NULL;
23036
23037  while (true)
23038    {
23039      /* Peek at the next token.  */
23040      token = cp_lexer_peek_token (parser->lexer);
23041      if (token->type == CPP_OPEN_PAREN)
23042	{
23043	  /* This is either a parameter-declaration-clause, or a
23044	     parenthesized declarator. When we know we are parsing a
23045	     named declarator, it must be a parenthesized declarator
23046	     if FIRST is true. For instance, `(int)' is a
23047	     parameter-declaration-clause, with an omitted
23048	     direct-abstract-declarator. But `((*))', is a
23049	     parenthesized abstract declarator. Finally, when T is a
23050	     template parameter `(T)' is a
23051	     parameter-declaration-clause, and not a parenthesized
23052	     named declarator.
23053
23054	     We first try and parse a parameter-declaration-clause,
23055	     and then try a nested declarator (if FIRST is true).
23056
23057	     It is not an error for it not to be a
23058	     parameter-declaration-clause, even when FIRST is
23059	     false. Consider,
23060
23061	       int i (int);
23062	       int i (3);
23063
23064	     The first is the declaration of a function while the
23065	     second is the definition of a variable, including its
23066	     initializer.
23067
23068	     Having seen only the parenthesis, we cannot know which of
23069	     these two alternatives should be selected.  Even more
23070	     complex are examples like:
23071
23072	       int i (int (a));
23073	       int i (int (3));
23074
23075	     The former is a function-declaration; the latter is a
23076	     variable initialization.
23077
23078	     Thus again, we try a parameter-declaration-clause, and if
23079	     that fails, we back out and return.  */
23080
23081	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23082	    {
23083	      tree params;
23084	      bool is_declarator = false;
23085
23086	      open_paren = NULL;
23087
23088	      /* In a member-declarator, the only valid interpretation
23089		 of a parenthesis is the start of a
23090		 parameter-declaration-clause.  (It is invalid to
23091		 initialize a static data member with a parenthesized
23092		 initializer; only the "=" form of initialization is
23093		 permitted.)  */
23094	      if (!member_p)
23095		cp_parser_parse_tentatively (parser);
23096
23097	      /* Consume the `('.  */
23098	      const location_t parens_start = token->location;
23099	      matching_parens parens;
23100	      parens.consume_open (parser);
23101	      if (first)
23102		{
23103		  /* If this is going to be an abstract declarator, we're
23104		     in a declarator and we can't have default args.  */
23105		  parser->default_arg_ok_p = false;
23106		  parser->in_declarator_p = true;
23107		}
23108
23109	      begin_scope (sk_function_parms, NULL_TREE);
23110
23111	      /* Signal we are in the immediate function context.  */
23112	      if (flags & CP_PARSER_FLAGS_CONSTEVAL)
23113		current_binding_level->immediate_fn_ctx_p = true;
23114
23115	      /* Parse the parameter-declaration-clause.  */
23116	      params
23117		= cp_parser_parameter_declaration_clause (parser, flags);
23118	      const location_t parens_end
23119		= cp_lexer_peek_token (parser->lexer)->location;
23120
23121	      /* Consume the `)'.  */
23122	      parens.require_close (parser);
23123
23124	      /* If all went well, parse the cv-qualifier-seq,
23125		 ref-qualifier and the exception-specification.  */
23126	      if (member_p || cp_parser_parse_definitely (parser))
23127		{
23128		  cp_cv_quals cv_quals;
23129		  cp_virt_specifiers virt_specifiers;
23130		  cp_ref_qualifier ref_qual;
23131		  tree exception_specification;
23132		  tree late_return;
23133		  tree attrs;
23134		  bool memfn = (member_p || (pushed_scope
23135					     && CLASS_TYPE_P (pushed_scope)));
23136		  unsigned char local_variables_forbidden_p
23137		    = parser->local_variables_forbidden_p;
23138		  /* 'this' is not allowed in static member functions.  */
23139		  if (static_p || friend_p)
23140		    parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23141
23142		  is_declarator = true;
23143
23144		  if (ctor_dtor_or_conv_p)
23145		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23146		  first = false;
23147
23148		  /* Parse the cv-qualifier-seq.  */
23149		  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23150		  /* Parse the ref-qualifier. */
23151		  ref_qual = cp_parser_ref_qualifier_opt (parser);
23152		  /* Parse the tx-qualifier.  */
23153		  tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23154
23155		  tree save_ccp = current_class_ptr;
23156		  tree save_ccr = current_class_ref;
23157		  if (memfn && !friend_p && !static_p)
23158		    /* DR 1207: 'this' is in scope after the cv-quals.  */
23159		    inject_this_parameter (current_class_type, cv_quals);
23160
23161		  /* If it turned out that this is e.g. a pointer to a
23162		     function, we don't want to delay noexcept parsing.  */
23163		  if (declarator == NULL || declarator->kind != cdk_id)
23164		    flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23165
23166		  /* Parse the exception-specification.  */
23167		  exception_specification
23168		    = cp_parser_exception_specification_opt (parser,
23169							     flags);
23170
23171		  attrs = cp_parser_std_attribute_spec_seq (parser);
23172
23173		  cp_omp_declare_simd_data odsd;
23174		  if ((flag_openmp || flag_openmp_simd)
23175		      && declarator
23176		      && declarator->std_attributes
23177		      && declarator->kind == cdk_id)
23178		    {
23179		      tree *pa = &declarator->std_attributes;
23180		      cp_parser_handle_directive_omp_attributes (parser, pa,
23181								 &odsd, false);
23182		    }
23183
23184		  /* In here, we handle cases where attribute is used after
23185		     the function declaration.  For example:
23186		     void func (int x) __attribute__((vector(..)));  */
23187		  tree gnu_attrs = NULL_TREE;
23188		  tree requires_clause = NULL_TREE;
23189		  late_return
23190		    = cp_parser_late_return_type_opt (parser, declarator,
23191						      requires_clause);
23192
23193		  cp_finalize_omp_declare_simd (parser, &odsd);
23194
23195		  /* Parse the virt-specifier-seq.  */
23196		  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23197
23198		  location_t parens_loc = make_location (parens_start,
23199							 parens_start,
23200							 parens_end);
23201		  /* Create the function-declarator.  */
23202		  declarator = make_call_declarator (declarator,
23203						     params,
23204						     cv_quals,
23205						     virt_specifiers,
23206						     ref_qual,
23207						     tx_qual,
23208						     exception_specification,
23209						     late_return,
23210						     requires_clause,
23211						     parens_loc);
23212		  declarator->std_attributes = attrs;
23213		  declarator->attributes = gnu_attrs;
23214		  /* Any subsequent parameter lists are to do with
23215		     return type, so are not those of the declared
23216		     function.  */
23217		  parser->default_arg_ok_p = false;
23218
23219		  current_class_ptr = save_ccp;
23220		  current_class_ref = save_ccr;
23221
23222		  /* Restore the state of local_variables_forbidden_p.  */
23223		  parser->local_variables_forbidden_p
23224		    = local_variables_forbidden_p;
23225		}
23226
23227	      /* Remove the function parms from scope.  */
23228	      pop_bindings_and_leave_scope ();
23229
23230	      if (is_declarator)
23231		/* Repeat the main loop.  */
23232		continue;
23233	    }
23234
23235	  /* If this is the first, we can try a parenthesized
23236	     declarator.  */
23237	  if (first)
23238	    {
23239	      bool saved_in_type_id_in_expr_p;
23240
23241	      parser->default_arg_ok_p = saved_default_arg_ok_p;
23242	      parser->in_declarator_p = saved_in_declarator_p;
23243
23244	      open_paren = token;
23245	      /* Consume the `('.  */
23246	      matching_parens parens;
23247	      parens.consume_open (parser);
23248	      /* Parse the nested declarator.  */
23249	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23250	      parser->in_type_id_in_expr_p = true;
23251	      declarator
23252		= cp_parser_declarator (parser, dcl_kind, flags,
23253					ctor_dtor_or_conv_p,
23254					/*parenthesized_p=*/NULL,
23255					member_p, friend_p,
23256					/*static_p=*/false);
23257	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23258	      first = false;
23259	      /* Expect a `)'.  */
23260	      close_paren = cp_lexer_peek_token (parser->lexer);
23261	      if (!parens.require_close (parser))
23262		declarator = cp_error_declarator;
23263	      if (declarator == cp_error_declarator)
23264		break;
23265
23266	      goto handle_declarator;
23267	    }
23268	  /* Otherwise, we must be done.  */
23269	  else
23270	    break;
23271	}
23272      else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23273	       && token->type == CPP_OPEN_SQUARE
23274	       && !cp_next_tokens_can_be_attribute_p (parser))
23275	{
23276	  /* Parse an array-declarator.  */
23277	  tree bounds, attrs;
23278
23279	  if (ctor_dtor_or_conv_p)
23280	    *ctor_dtor_or_conv_p = 0;
23281
23282	  open_paren = NULL;
23283	  first = false;
23284	  parser->default_arg_ok_p = false;
23285	  parser->in_declarator_p = true;
23286	  /* Consume the `['.  */
23287	  cp_lexer_consume_token (parser->lexer);
23288	  /* Peek at the next token.  */
23289	  token = cp_lexer_peek_token (parser->lexer);
23290	  /* If the next token is `]', then there is no
23291	     constant-expression.  */
23292	  if (token->type != CPP_CLOSE_SQUARE)
23293	    {
23294	      bool non_constant_p;
23295	      bounds
23296		= cp_parser_constant_expression (parser,
23297						 /*allow_non_constant=*/true,
23298						 &non_constant_p);
23299	      if (!non_constant_p)
23300		/* OK */;
23301	      else if (error_operand_p (bounds))
23302		/* Already gave an error.  */;
23303	      else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23304		/* Let compute_array_index_type diagnose this.  */;
23305	      else if (!parser->in_function_body
23306		       || parsing_function_declarator ())
23307		{
23308		  /* Normally, the array bound must be an integral constant
23309		     expression.  However, as an extension, we allow VLAs
23310		     in function scopes as long as they aren't part of a
23311		     parameter declaration.  */
23312		  cp_parser_error (parser,
23313				   "array bound is not an integer constant");
23314		  bounds = error_mark_node;
23315		}
23316	      else if (processing_template_decl
23317		       && !type_dependent_expression_p (bounds))
23318		{
23319		  /* Remember this wasn't a constant-expression.  */
23320		  bounds = build_nop (TREE_TYPE (bounds), bounds);
23321		  TREE_SIDE_EFFECTS (bounds) = 1;
23322		}
23323	    }
23324	  else
23325	    bounds = NULL_TREE;
23326	  /* Look for the closing `]'.  */
23327	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23328	    {
23329	      declarator = cp_error_declarator;
23330	      break;
23331	    }
23332
23333	  attrs = cp_parser_std_attribute_spec_seq (parser);
23334	  declarator = make_array_declarator (declarator, bounds);
23335	  declarator->std_attributes = attrs;
23336	}
23337      else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23338	{
23339	  {
23340	    tree qualifying_scope;
23341	    tree unqualified_name;
23342	    tree attrs;
23343	    special_function_kind sfk;
23344	    bool abstract_ok;
23345	    bool pack_expansion_p = false;
23346	    cp_token *declarator_id_start_token;
23347
23348	    /* Parse a declarator-id */
23349	    abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23350	    if (abstract_ok)
23351	      {
23352		cp_parser_parse_tentatively (parser);
23353
23354		/* If we see an ellipsis, we should be looking at a
23355		   parameter pack. */
23356		if (token->type == CPP_ELLIPSIS)
23357		  {
23358		    /* Consume the `...' */
23359		    cp_lexer_consume_token (parser->lexer);
23360
23361		    pack_expansion_p = true;
23362		  }
23363	      }
23364
23365	    declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23366	    unqualified_name
23367	      = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23368	    qualifying_scope = parser->scope;
23369	    if (abstract_ok)
23370	      {
23371		bool okay = false;
23372
23373		if (!unqualified_name && pack_expansion_p)
23374		  {
23375		    /* Check whether an error occurred. */
23376		    okay = !cp_parser_error_occurred (parser);
23377
23378		    /* We already consumed the ellipsis to mark a
23379		       parameter pack, but we have no way to report it,
23380		       so abort the tentative parse. We will be exiting
23381		       immediately anyway. */
23382		    cp_parser_abort_tentative_parse (parser);
23383		  }
23384		else
23385		  okay = cp_parser_parse_definitely (parser);
23386
23387		if (!okay)
23388		  unqualified_name = error_mark_node;
23389		else if (unqualified_name
23390			 && (qualifying_scope
23391			     || (!identifier_p (unqualified_name))))
23392		  {
23393		    cp_parser_error (parser, "expected unqualified-id");
23394		    unqualified_name = error_mark_node;
23395		  }
23396	      }
23397
23398	    if (!unqualified_name)
23399	      return NULL;
23400	    if (unqualified_name == error_mark_node)
23401	      {
23402		declarator = cp_error_declarator;
23403		pack_expansion_p = false;
23404		declarator->parameter_pack_p = false;
23405		break;
23406	      }
23407
23408	    attrs = cp_parser_std_attribute_spec_seq (parser);
23409
23410	    if (qualifying_scope && at_namespace_scope_p ()
23411		&& TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23412	      {
23413		/* In the declaration of a member of a template class
23414		   outside of the class itself, the SCOPE will sometimes
23415		   be a TYPENAME_TYPE.  For example, given:
23416
23417		   template <typename T>
23418		   int S<T>::R::i = 3;
23419
23420		   the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
23421		   this context, we must resolve S<T>::R to an ordinary
23422		   type, rather than a typename type.
23423
23424		   The reason we normally avoid resolving TYPENAME_TYPEs
23425		   is that a specialization of `S' might render
23426		   `S<T>::R' not a type.  However, if `S' is
23427		   specialized, then this `i' will not be used, so there
23428		   is no harm in resolving the types here.  */
23429		tree type;
23430
23431		/* Resolve the TYPENAME_TYPE.  */
23432		type = resolve_typename_type (qualifying_scope,
23433					      /*only_current_p=*/false);
23434		/* If that failed, the declarator is invalid.  */
23435		if (TREE_CODE (type) == TYPENAME_TYPE)
23436		  {
23437		    if (typedef_variant_p (type))
23438		      error_at (declarator_id_start_token->location,
23439				"cannot define member of dependent typedef "
23440				"%qT", type);
23441		    else
23442		      error_at (declarator_id_start_token->location,
23443				"%<%T::%E%> is not a type",
23444				TYPE_CONTEXT (qualifying_scope),
23445				TYPE_IDENTIFIER (qualifying_scope));
23446		  }
23447		qualifying_scope = type;
23448	      }
23449
23450	    sfk = sfk_none;
23451
23452	    if (unqualified_name)
23453	      {
23454		tree class_type;
23455
23456		if (qualifying_scope
23457		    && CLASS_TYPE_P (qualifying_scope))
23458		  class_type = qualifying_scope;
23459		else
23460		  class_type = current_class_type;
23461
23462		if (TREE_CODE (unqualified_name) == TYPE_DECL)
23463		  {
23464		    tree name_type = TREE_TYPE (unqualified_name);
23465
23466		    if (!class_type || !same_type_p (name_type, class_type))
23467		      {
23468			/* We do not attempt to print the declarator
23469			   here because we do not have enough
23470			   information about its original syntactic
23471			   form.  */
23472			cp_parser_error (parser, "invalid declarator");
23473			declarator = cp_error_declarator;
23474			break;
23475		      }
23476		    else if (qualifying_scope
23477			     && CLASSTYPE_USE_TEMPLATE (name_type))
23478		      {
23479			error_at (declarator_id_start_token->location,
23480				  "invalid use of constructor as a template");
23481			inform (declarator_id_start_token->location,
23482				"use %<%T::%D%> instead of %<%T::%D%> to "
23483				"name the constructor in a qualified name",
23484				class_type,
23485				DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23486				class_type, name_type);
23487			declarator = cp_error_declarator;
23488			break;
23489		      }
23490		    unqualified_name = constructor_name (class_type);
23491		  }
23492
23493		if (class_type)
23494		  {
23495		    if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23496		      sfk = sfk_destructor;
23497		    else if (identifier_p (unqualified_name)
23498			     && IDENTIFIER_CONV_OP_P (unqualified_name))
23499		      sfk = sfk_conversion;
23500		    else if (/* There's no way to declare a constructor
23501				for an unnamed type, even if the type
23502				got a name for linkage purposes.  */
23503			     !TYPE_WAS_UNNAMED (class_type)
23504			     /* Handle correctly (c++/19200):
23505
23506				struct S {
23507				  struct T{};
23508				  friend void S(T);
23509				};
23510
23511				and also:
23512
23513				namespace N {
23514				  void S();
23515				}
23516
23517				struct S {
23518				  friend void N::S();
23519				};  */
23520			     && (!friend_p || class_type == qualifying_scope)
23521			     && constructor_name_p (unqualified_name,
23522						    class_type))
23523		      sfk = sfk_constructor;
23524		    else if (is_overloaded_fn (unqualified_name)
23525			     && DECL_CONSTRUCTOR_P (get_first_fn
23526						    (unqualified_name)))
23527		      sfk = sfk_constructor;
23528
23529		    if (ctor_dtor_or_conv_p && sfk != sfk_none)
23530		      *ctor_dtor_or_conv_p = -1;
23531		  }
23532	      }
23533	    declarator = make_id_declarator (qualifying_scope,
23534					     unqualified_name,
23535					     sfk, token->location);
23536	    declarator->std_attributes = attrs;
23537	    declarator->parameter_pack_p = pack_expansion_p;
23538
23539	    if (pack_expansion_p)
23540	      maybe_warn_variadic_templates ();
23541
23542	    /* We're looking for this case in [temp.res]:
23543	       A qualified-id is assumed to name a type if [...]
23544	       - it is a decl-specifier of the decl-specifier-seq of a
23545		 parameter-declaration in a declarator of a function or
23546		 function template declaration, ... */
23547	    if (cxx_dialect >= cxx20
23548		&& (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23549		&& declarator->kind == cdk_id
23550		&& !at_class_scope_p ()
23551		&& cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23552	      {
23553		/* ...whose declarator-id is qualified.  If it isn't, never
23554		   assume the parameters to refer to types.  */
23555		if (qualifying_scope == NULL_TREE)
23556		  flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23557		else
23558		  {
23559		    /* Now we have something like
23560		       template <typename T> int C::x(S::p);
23561		       which can be a function template declaration or a
23562		       variable template definition.  If name lookup for
23563		       the declarator-id C::x finds one or more function
23564		       templates, assume S::p to name a type.  Otherwise,
23565		       don't.  */
23566		    tree decl
23567		      = cp_parser_lookup_name (parser, unqualified_name,
23568					       none_type,
23569					       /*is_template=*/false,
23570					       /*is_namespace=*/false,
23571					       /*check_dependency=*/false,
23572					       /*ambiguous_decls=*/NULL,
23573					       token->location);
23574
23575		    if (!is_overloaded_fn (decl)
23576			/* Allow
23577			   template<typename T>
23578			   A<T>::A(T::type) { }  */
23579			&& !(MAYBE_CLASS_TYPE_P (qualifying_scope)
23580			     && constructor_name_p (unqualified_name,
23581						    qualifying_scope)))
23582		      flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23583		  }
23584	      }
23585	  }
23586
23587	handle_declarator:;
23588	  scope = get_scope_of_declarator (declarator);
23589	  if (scope)
23590	    {
23591	      /* Any names that appear after the declarator-id for a
23592		 member are looked up in the containing scope.  */
23593	      if (at_function_scope_p ())
23594		{
23595		  /* But declarations with qualified-ids can't appear in a
23596		     function.  */
23597		  cp_parser_error (parser, "qualified-id in declaration");
23598		  declarator = cp_error_declarator;
23599		  break;
23600		}
23601	      pushed_scope = push_scope (scope);
23602	    }
23603	  parser->in_declarator_p = true;
23604	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
23605	      || (declarator && declarator->kind == cdk_id))
23606	    /* Default args are only allowed on function
23607	       declarations.  */
23608	    parser->default_arg_ok_p = saved_default_arg_ok_p;
23609	  else
23610	    parser->default_arg_ok_p = false;
23611
23612	  first = false;
23613	}
23614      /* We're done.  */
23615      else
23616	break;
23617    }
23618
23619  /* For an abstract declarator, we might wind up with nothing at this
23620     point.  That's an error; the declarator is not optional.  */
23621  if (!declarator)
23622    cp_parser_error (parser, "expected declarator");
23623  else if (open_paren)
23624    {
23625      /* Record overly parenthesized declarator so we can give a
23626	 diagnostic about confusing decl/expr disambiguation.  */
23627      if (declarator->kind == cdk_array)
23628	{
23629	  /* If the open and close parens are on different lines, this
23630	     is probably a formatting thing, so ignore.  */
23631	  expanded_location open = expand_location (open_paren->location);
23632	  expanded_location close = expand_location (close_paren->location);
23633	  if (open.line != close.line || open.file != close.file)
23634	    open_paren = NULL;
23635	}
23636      if (open_paren)
23637	declarator->parenthesized = make_location (open_paren->location,
23638						   open_paren->location,
23639						   close_paren->location);
23640    }
23641
23642  /* If we entered a scope, we must exit it now.  */
23643  if (pushed_scope)
23644    pop_scope (pushed_scope);
23645
23646  parser->default_arg_ok_p = saved_default_arg_ok_p;
23647  parser->in_declarator_p = saved_in_declarator_p;
23648
23649  return declarator;
23650}
23651
23652/* Parse a ptr-operator.
23653
23654   ptr-operator:
23655     * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23656     * cv-qualifier-seq [opt]
23657     &
23658     :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23659     nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23660
23661   GNU Extension:
23662
23663   ptr-operator:
23664     & cv-qualifier-seq [opt]
23665
23666   Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23667   Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23668   an rvalue reference. In the case of a pointer-to-member, *TYPE is
23669   filled in with the TYPE containing the member.  *CV_QUALS is
23670   filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23671   are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
23672   Note that the tree codes returned by this function have nothing
23673   to do with the types of trees that will be eventually be created
23674   to represent the pointer or reference type being parsed. They are
23675   just constants with suggestive names. */
23676static enum tree_code
23677cp_parser_ptr_operator (cp_parser* parser,
23678			tree* type,
23679			cp_cv_quals *cv_quals,
23680			tree *attributes)
23681{
23682  enum tree_code code = ERROR_MARK;
23683  cp_token *token;
23684  tree attrs = NULL_TREE;
23685
23686  /* Assume that it's not a pointer-to-member.  */
23687  *type = NULL_TREE;
23688  /* And that there are no cv-qualifiers.  */
23689  *cv_quals = TYPE_UNQUALIFIED;
23690
23691  /* Peek at the next token.  */
23692  token = cp_lexer_peek_token (parser->lexer);
23693
23694  /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
23695  if (token->type == CPP_MULT)
23696    code = INDIRECT_REF;
23697  else if (token->type == CPP_AND)
23698    code = ADDR_EXPR;
23699  else if ((cxx_dialect != cxx98) &&
23700	   token->type == CPP_AND_AND) /* C++0x only */
23701    code = NON_LVALUE_EXPR;
23702
23703  if (code != ERROR_MARK)
23704    {
23705      /* Consume the `*', `&' or `&&'.  */
23706      cp_lexer_consume_token (parser->lexer);
23707
23708      /* A `*' can be followed by a cv-qualifier-seq, and so can a
23709	 `&', if we are allowing GNU extensions.  (The only qualifier
23710	 that can legally appear after `&' is `restrict', but that is
23711	 enforced during semantic analysis.  */
23712      if (code == INDIRECT_REF
23713	  || cp_parser_allow_gnu_extensions_p (parser))
23714	*cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23715
23716      attrs = cp_parser_std_attribute_spec_seq (parser);
23717      if (attributes != NULL)
23718	*attributes = attrs;
23719    }
23720  else
23721    {
23722      /* Try the pointer-to-member case.  */
23723      cp_parser_parse_tentatively (parser);
23724      /* Look for the optional `::' operator.  */
23725      cp_parser_global_scope_opt (parser,
23726				  /*current_scope_valid_p=*/false);
23727      /* Look for the nested-name specifier.  */
23728      token = cp_lexer_peek_token (parser->lexer);
23729      cp_parser_nested_name_specifier (parser,
23730				       /*typename_keyword_p=*/false,
23731				       /*check_dependency_p=*/true,
23732				       /*type_p=*/false,
23733				       /*is_declaration=*/false);
23734      /* If we found it, and the next token is a `*', then we are
23735	 indeed looking at a pointer-to-member operator.  */
23736      if (!cp_parser_error_occurred (parser)
23737	  && cp_parser_require (parser, CPP_MULT, RT_MULT))
23738	{
23739	  /* Indicate that the `*' operator was used.  */
23740	  code = INDIRECT_REF;
23741
23742	  if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
23743	    error_at (token->location, "%qD is a namespace", parser->scope);
23744	  else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
23745	    error_at (token->location, "cannot form pointer to member of "
23746		      "non-class %q#T", parser->scope);
23747	  else
23748	    {
23749	      /* The type of which the member is a member is given by the
23750		 current SCOPE.  */
23751	      *type = parser->scope;
23752	      /* The next name will not be qualified.  */
23753	      parser->scope = NULL_TREE;
23754	      parser->qualifying_scope = NULL_TREE;
23755	      parser->object_scope = NULL_TREE;
23756	      /* Look for optional c++11 attributes.  */
23757	      attrs = cp_parser_std_attribute_spec_seq (parser);
23758	      if (attributes != NULL)
23759		*attributes = attrs;
23760	      /* Look for the optional cv-qualifier-seq.  */
23761	      *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23762	    }
23763	}
23764      /* If that didn't work we don't have a ptr-operator.  */
23765      if (!cp_parser_parse_definitely (parser))
23766	cp_parser_error (parser, "expected ptr-operator");
23767    }
23768
23769  return code;
23770}
23771
23772/* Parse an (optional) cv-qualifier-seq.
23773
23774   cv-qualifier-seq:
23775     cv-qualifier cv-qualifier-seq [opt]
23776
23777   cv-qualifier:
23778     const
23779     volatile
23780
23781   GNU Extension:
23782
23783   cv-qualifier:
23784     __restrict__
23785
23786   Returns a bitmask representing the cv-qualifiers.  */
23787
23788static cp_cv_quals
23789cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
23790{
23791  cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
23792
23793  while (true)
23794    {
23795      cp_token *token;
23796      cp_cv_quals cv_qualifier;
23797
23798      /* Peek at the next token.  */
23799      token = cp_lexer_peek_token (parser->lexer);
23800      /* See if it's a cv-qualifier.  */
23801      switch (token->keyword)
23802	{
23803	case RID_CONST:
23804	  cv_qualifier = TYPE_QUAL_CONST;
23805	  break;
23806
23807	case RID_VOLATILE:
23808	  cv_qualifier = TYPE_QUAL_VOLATILE;
23809	  break;
23810
23811	case RID_RESTRICT:
23812	  cv_qualifier = TYPE_QUAL_RESTRICT;
23813	  break;
23814
23815	default:
23816	  cv_qualifier = TYPE_UNQUALIFIED;
23817	  break;
23818	}
23819
23820      if (!cv_qualifier)
23821	break;
23822
23823      if (cv_quals & cv_qualifier)
23824	{
23825	  gcc_rich_location richloc (token->location);
23826	  richloc.add_fixit_remove ();
23827	  error_at (&richloc, "duplicate cv-qualifier");
23828	  cp_lexer_purge_token (parser->lexer);
23829	}
23830      else
23831	{
23832	  cp_lexer_consume_token (parser->lexer);
23833	  cv_quals |= cv_qualifier;
23834	}
23835    }
23836
23837  return cv_quals;
23838}
23839
23840/* Parse an (optional) ref-qualifier
23841
23842   ref-qualifier:
23843     &
23844     &&
23845
23846   Returns cp_ref_qualifier representing ref-qualifier. */
23847
23848static cp_ref_qualifier
23849cp_parser_ref_qualifier_opt (cp_parser* parser)
23850{
23851  cp_ref_qualifier ref_qual = REF_QUAL_NONE;
23852
23853  /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
23854  if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
23855    return ref_qual;
23856
23857  while (true)
23858    {
23859      cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
23860      cp_token *token = cp_lexer_peek_token (parser->lexer);
23861
23862      switch (token->type)
23863	{
23864	case CPP_AND:
23865	  curr_ref_qual = REF_QUAL_LVALUE;
23866	  break;
23867
23868	case CPP_AND_AND:
23869	  curr_ref_qual = REF_QUAL_RVALUE;
23870	  break;
23871
23872	default:
23873	  curr_ref_qual = REF_QUAL_NONE;
23874	  break;
23875	}
23876
23877      if (!curr_ref_qual)
23878	break;
23879      else if (ref_qual)
23880	{
23881	  error_at (token->location, "multiple ref-qualifiers");
23882	  cp_lexer_purge_token (parser->lexer);
23883	}
23884      else
23885	{
23886	  ref_qual = curr_ref_qual;
23887	  cp_lexer_consume_token (parser->lexer);
23888	}
23889    }
23890
23891  return ref_qual;
23892}
23893
23894/* Parse an optional tx-qualifier.
23895
23896   tx-qualifier:
23897     transaction_safe
23898     transaction_safe_dynamic  */
23899
23900static tree
23901cp_parser_tx_qualifier_opt (cp_parser *parser)
23902{
23903  cp_token *token = cp_lexer_peek_token (parser->lexer);
23904  if (token->type == CPP_NAME)
23905    {
23906      tree name = token->u.value;
23907      const char *p = IDENTIFIER_POINTER (name);
23908      const int len = strlen ("transaction_safe");
23909      if (startswith (p, "transaction_safe"))
23910	{
23911	  p += len;
23912	  if (*p == '\0'
23913	      || !strcmp (p, "_dynamic"))
23914	    {
23915	      cp_lexer_consume_token (parser->lexer);
23916	      if (!flag_tm)
23917		{
23918		  error ("%qE requires %<-fgnu-tm%>", name);
23919		  return NULL_TREE;
23920		}
23921	      else
23922		return name;
23923	    }
23924	}
23925    }
23926  return NULL_TREE;
23927}
23928
23929/* Parse an (optional) virt-specifier-seq.
23930
23931   virt-specifier-seq:
23932     virt-specifier virt-specifier-seq [opt]
23933
23934   virt-specifier:
23935     override
23936     final
23937
23938   Returns a bitmask representing the virt-specifiers.  */
23939
23940static cp_virt_specifiers
23941cp_parser_virt_specifier_seq_opt (cp_parser* parser)
23942{
23943  cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23944
23945  while (true)
23946    {
23947      cp_token *token;
23948      cp_virt_specifiers virt_specifier;
23949
23950      /* Peek at the next token.  */
23951      token = cp_lexer_peek_token (parser->lexer);
23952      /* See if it's a virt-specifier-qualifier.  */
23953      if (token->type != CPP_NAME)
23954        break;
23955      if (id_equal (token->u.value, "override"))
23956        {
23957          maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
23958          virt_specifier = VIRT_SPEC_OVERRIDE;
23959        }
23960      else if (id_equal (token->u.value, "final"))
23961        {
23962          maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
23963          virt_specifier = VIRT_SPEC_FINAL;
23964        }
23965      else if (id_equal (token->u.value, "__final"))
23966        {
23967          virt_specifier = VIRT_SPEC_FINAL;
23968        }
23969      else
23970	break;
23971
23972      if (virt_specifiers & virt_specifier)
23973	{
23974	  gcc_rich_location richloc (token->location);
23975	  richloc.add_fixit_remove ();
23976	  error_at (&richloc, "duplicate virt-specifier");
23977	  cp_lexer_purge_token (parser->lexer);
23978	}
23979      else
23980	{
23981	  cp_lexer_consume_token (parser->lexer);
23982	  virt_specifiers |= virt_specifier;
23983	}
23984    }
23985  return virt_specifiers;
23986}
23987
23988/* Used by handling of trailing-return-types and NSDMI, in which 'this'
23989   is in scope even though it isn't real.  */
23990
23991void
23992inject_this_parameter (tree ctype, cp_cv_quals quals)
23993{
23994  tree this_parm;
23995
23996  if (current_class_ptr)
23997    {
23998      /* We don't clear this between NSDMIs.  Is it already what we want?  */
23999      tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24000      if (DECL_P (current_class_ptr)
24001	  && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24002	  && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24003	  && cp_type_quals (type) == quals)
24004	return;
24005    }
24006
24007  this_parm = build_this_parm (NULL_TREE, ctype, quals);
24008  /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
24009  current_class_ptr = NULL_TREE;
24010  current_class_ref
24011    = cp_build_fold_indirect_ref (this_parm);
24012  current_class_ptr = this_parm;
24013}
24014
24015/* Return true iff our current scope is a non-static data member
24016   initializer.  */
24017
24018bool
24019parsing_nsdmi (void)
24020{
24021  /* We recognize NSDMI context by the context-less 'this' pointer set up
24022     by the function above.  */
24023  if (current_class_ptr
24024      && TREE_CODE (current_class_ptr) == PARM_DECL
24025      && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24026    return true;
24027  return false;
24028}
24029
24030/* True if we're parsing a function declarator.  */
24031
24032bool
24033parsing_function_declarator ()
24034{
24035  /* this_entity is NULL for a function parameter scope while parsing the
24036     declarator; it is set when parsing the body of the function.  */
24037  return (current_binding_level->kind == sk_function_parms
24038	  && !current_binding_level->this_entity);
24039}
24040
24041/* Parse a late-specified return type, if any.  This is not a separate
24042   non-terminal, but part of a function declarator, which looks like
24043
24044   -> trailing-type-specifier-seq abstract-declarator(opt)
24045
24046   Returns the type indicated by the type-id.
24047
24048   In addition to this, parse any queued up #pragma omp declare simd
24049   clauses, and #pragma acc routine clauses.
24050
24051   QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24052   function.  */
24053
24054static tree
24055cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24056				tree& requires_clause)
24057{
24058  cp_token *token;
24059  tree type = NULL_TREE;
24060  bool declare_simd_p = (parser->omp_declare_simd
24061			 && declarator
24062			 && declarator->kind == cdk_id);
24063
24064  bool oacc_routine_p = (parser->oacc_routine
24065			 && declarator
24066			 && declarator->kind == cdk_id);
24067
24068  /* Peek at the next token.  */
24069  token = cp_lexer_peek_token (parser->lexer);
24070  /* A late-specified return type is indicated by an initial '->'. */
24071  if (token->type != CPP_DEREF
24072      && token->keyword != RID_REQUIRES
24073      && !(token->type == CPP_NAME
24074	   && token->u.value == ridpointers[RID_REQUIRES])
24075      && !(declare_simd_p || oacc_routine_p))
24076    return NULL_TREE;
24077
24078  if (token->type == CPP_DEREF)
24079    {
24080      /* Consume the ->.  */
24081      cp_lexer_consume_token (parser->lexer);
24082
24083      type = cp_parser_trailing_type_id (parser);
24084    }
24085
24086  /* Function declarations may be followed by a trailing
24087     requires-clause.  */
24088  requires_clause = cp_parser_requires_clause_opt (parser, false);
24089
24090  if (declare_simd_p)
24091    declarator->attributes
24092      = cp_parser_late_parsing_omp_declare_simd (parser,
24093						 declarator->attributes);
24094  if (oacc_routine_p)
24095    declarator->attributes
24096      = cp_parser_late_parsing_oacc_routine (parser,
24097					     declarator->attributes);
24098
24099  return type;
24100}
24101
24102/* Parse a declarator-id.
24103
24104   declarator-id:
24105     id-expression
24106     :: [opt] nested-name-specifier [opt] type-name
24107
24108   In the `id-expression' case, the value returned is as for
24109   cp_parser_id_expression if the id-expression was an unqualified-id.
24110   If the id-expression was a qualified-id, then a SCOPE_REF is
24111   returned.  The first operand is the scope (either a NAMESPACE_DECL
24112   or TREE_TYPE), but the second is still just a representation of an
24113   unqualified-id.  */
24114
24115static tree
24116cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24117{
24118  tree id;
24119  /* The expression must be an id-expression.  Assume that qualified
24120     names are the names of types so that:
24121
24122       template <class T>
24123       int S<T>::R::i = 3;
24124
24125     will work; we must treat `S<T>::R' as the name of a type.
24126     Similarly, assume that qualified names are templates, where
24127     required, so that:
24128
24129       template <class T>
24130       int S<T>::R<T>::i = 3;
24131
24132     will work, too.  */
24133  id = cp_parser_id_expression (parser,
24134				/*template_keyword_p=*/false,
24135				/*check_dependency_p=*/false,
24136				/*template_p=*/NULL,
24137				/*declarator_p=*/true,
24138				optional_p);
24139  if (id && BASELINK_P (id))
24140    id = BASELINK_FUNCTIONS (id);
24141  return id;
24142}
24143
24144/* Parse a type-id.
24145
24146   type-id:
24147     type-specifier-seq abstract-declarator [opt]
24148
24149   The parser flags FLAGS is used to control type-specifier parsing.
24150
24151   If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24152
24153   If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24154   i.e. we've just seen "->".
24155
24156   Returns the TYPE specified.  */
24157
24158static tree
24159cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24160		     bool is_template_arg, bool is_trailing_return,
24161		     location_t *type_location)
24162{
24163  cp_decl_specifier_seq type_specifier_seq;
24164  cp_declarator *abstract_declarator;
24165
24166  /* Parse the type-specifier-seq.  */
24167  cp_parser_type_specifier_seq (parser, flags,
24168				/*is_declaration=*/false,
24169				is_trailing_return,
24170				&type_specifier_seq);
24171  if (type_location)
24172    *type_location = type_specifier_seq.locations[ds_type_spec];
24173
24174  if (is_template_arg && type_specifier_seq.type
24175      && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24176      && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24177    /* A bare template name as a template argument is a template template
24178       argument, not a placeholder, so fail parsing it as a type argument.  */
24179    {
24180      gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24181      cp_parser_simulate_error (parser);
24182      return error_mark_node;
24183    }
24184  if (type_specifier_seq.type == error_mark_node)
24185    return error_mark_node;
24186
24187  /* There might or might not be an abstract declarator.  */
24188  cp_parser_parse_tentatively (parser);
24189  /* Look for the declarator.  */
24190  abstract_declarator
24191    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24192			    CP_PARSER_FLAGS_NONE, NULL,
24193			    /*parenthesized_p=*/NULL,
24194			    /*member_p=*/false,
24195			    /*friend_p=*/false,
24196			    /*static_p=*/false);
24197  /* Check to see if there really was a declarator.  */
24198  if (!cp_parser_parse_definitely (parser))
24199    abstract_declarator = NULL;
24200
24201  bool auto_typeid_ok = false;
24202  /* The concepts TS allows 'auto' as a type-id.  */
24203  if (flag_concepts_ts)
24204    auto_typeid_ok = !parser->in_type_id_in_expr_p;
24205  /* DR 625 prohibits use of auto as a template-argument.  We allow 'auto'
24206     outside the template-argument-list context here only for the sake of
24207     diagnostic: grokdeclarator then can emit a better error message for
24208     e.g. using T = auto.  */
24209  else if (flag_concepts)
24210    auto_typeid_ok = (!parser->in_type_id_in_expr_p
24211		      && !parser->in_template_argument_list_p);
24212
24213  if (type_specifier_seq.type
24214      && !auto_typeid_ok
24215      /* None of the valid uses of 'auto' in C++14 involve the type-id
24216	 nonterminal, but it is valid in a trailing-return-type.  */
24217      && !(cxx_dialect >= cxx14 && is_trailing_return))
24218    if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24219      {
24220	/* A type-id with type 'auto' is only ok if the abstract declarator
24221	   is a function declarator with a late-specified return type.
24222
24223	   A type-id with 'auto' is also valid in a trailing-return-type
24224	   in a compound-requirement. */
24225	if (abstract_declarator
24226	    && abstract_declarator->kind == cdk_function
24227	    && abstract_declarator->u.function.late_return_type)
24228	  /* OK */;
24229	else if (parser->in_result_type_constraint_p)
24230	  /* OK */;
24231	else
24232	  {
24233	    if (!cp_parser_simulate_error (parser))
24234	      {
24235		location_t loc = type_specifier_seq.locations[ds_type_spec];
24236		if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24237		  {
24238		    error_at (loc, "missing template arguments after %qT",
24239			      auto_node);
24240		    inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24241			    tmpl);
24242		  }
24243		else if (parser->in_template_argument_list_p)
24244		  error_at (loc, "%qT not permitted in template argument",
24245			    auto_node);
24246		else
24247		  error_at (loc, "invalid use of %qT", auto_node);
24248	      }
24249	    return error_mark_node;
24250	  }
24251      }
24252
24253  return groktypename (&type_specifier_seq, abstract_declarator,
24254		       is_template_arg);
24255}
24256
24257/* Wrapper for cp_parser_type_id_1.  */
24258
24259static tree
24260cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24261		   location_t *type_location)
24262{
24263  return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24264}
24265
24266/* Wrapper for cp_parser_type_id_1.  */
24267
24268static tree
24269cp_parser_template_type_arg (cp_parser *parser)
24270{
24271  tree r;
24272  const char *saved_message = parser->type_definition_forbidden_message;
24273  parser->type_definition_forbidden_message
24274    = G_("types may not be defined in template arguments");
24275  r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24276  parser->type_definition_forbidden_message = saved_message;
24277  if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
24278    {
24279      error ("invalid use of %<auto%> in template argument");
24280      r = error_mark_node;
24281    }
24282  return r;
24283}
24284
24285/* Wrapper for cp_parser_type_id_1.  */
24286
24287static tree
24288cp_parser_trailing_type_id (cp_parser *parser)
24289{
24290  return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24291			      false, true, NULL);
24292}
24293
24294/* Parse a type-specifier-seq.
24295
24296   type-specifier-seq:
24297     type-specifier type-specifier-seq [opt]
24298
24299   GNU extension:
24300
24301   type-specifier-seq:
24302     attributes type-specifier-seq [opt]
24303
24304   The parser flags FLAGS is used to control type-specifier parsing.
24305
24306   If IS_DECLARATION is true, we are at the start of a "condition" or
24307   exception-declaration, so we might be followed by a declarator-id.
24308
24309   If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24310   i.e. we've just seen "->".
24311
24312   Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
24313
24314static void
24315cp_parser_type_specifier_seq (cp_parser* parser,
24316			      cp_parser_flags flags,
24317			      bool is_declaration,
24318			      bool is_trailing_return,
24319			      cp_decl_specifier_seq *type_specifier_seq)
24320{
24321  bool seen_type_specifier = false;
24322  cp_token *start_token = NULL;
24323
24324  /* Clear the TYPE_SPECIFIER_SEQ.  */
24325  clear_decl_specs (type_specifier_seq);
24326
24327  flags |= CP_PARSER_FLAGS_OPTIONAL;
24328  /* In the context of a trailing return type, enum E { } is an
24329     elaborated-type-specifier followed by a function-body, not an
24330     enum-specifier.  */
24331  if (is_trailing_return)
24332    flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24333
24334  /* Parse the type-specifiers and attributes.  */
24335  while (true)
24336    {
24337      tree type_specifier;
24338      bool is_cv_qualifier;
24339
24340      /* Check for attributes first.  */
24341      if (cp_next_tokens_can_be_attribute_p (parser))
24342	{
24343	  /* GNU attributes at the end of a declaration apply to the
24344	     declaration as a whole, not to the trailing return type.  So look
24345	     ahead to see if these attributes are at the end.  */
24346	  if (seen_type_specifier && is_trailing_return
24347	      && cp_next_tokens_can_be_gnu_attribute_p (parser))
24348	    {
24349	      size_t n = cp_parser_skip_attributes_opt (parser, 1);
24350	      cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24351	      if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24352		  || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24353		break;
24354	    }
24355	  type_specifier_seq->attributes
24356	    = attr_chainon (type_specifier_seq->attributes,
24357			    cp_parser_attributes_opt (parser));
24358	  continue;
24359	}
24360
24361      /* record the token of the beginning of the type specifier seq,
24362         for error reporting purposes*/
24363     if (!start_token)
24364       start_token = cp_lexer_peek_token (parser->lexer);
24365
24366      /* Look for the type-specifier.  */
24367      type_specifier = cp_parser_type_specifier (parser,
24368						 flags,
24369						 type_specifier_seq,
24370						 /*is_declaration=*/false,
24371						 NULL,
24372						 &is_cv_qualifier);
24373      if (!type_specifier)
24374	{
24375	  /* If the first type-specifier could not be found, this is not a
24376	     type-specifier-seq at all.  */
24377	  if (!seen_type_specifier)
24378	    {
24379	      /* Set in_declarator_p to avoid skipping to the semicolon.  */
24380	      int in_decl = parser->in_declarator_p;
24381	      parser->in_declarator_p = true;
24382
24383	      if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24384		  || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24385		cp_parser_error (parser, "expected type-specifier");
24386
24387	      parser->in_declarator_p = in_decl;
24388
24389	      type_specifier_seq->type = error_mark_node;
24390	      return;
24391	    }
24392	  /* If subsequent type-specifiers could not be found, the
24393	     type-specifier-seq is complete.  */
24394	  break;
24395	}
24396
24397      seen_type_specifier = true;
24398      /* The standard says that a condition can be:
24399
24400	    type-specifier-seq declarator = assignment-expression
24401
24402	 However, given:
24403
24404	   struct S {};
24405	   if (int S = ...)
24406
24407	 we should treat the "S" as a declarator, not as a
24408	 type-specifier.  The standard doesn't say that explicitly for
24409	 type-specifier-seq, but it does say that for
24410	 decl-specifier-seq in an ordinary declaration.  Perhaps it
24411	 would be clearer just to allow a decl-specifier-seq here, and
24412	 then add a semantic restriction that if any decl-specifiers
24413	 that are not type-specifiers appear, the program is invalid.  */
24414      if (is_declaration && !is_cv_qualifier)
24415	flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24416    }
24417}
24418
24419/* Return whether the function currently being declared has an associated
24420   template parameter list.  */
24421
24422static bool
24423function_being_declared_is_template_p (cp_parser* parser)
24424{
24425  if (!current_template_parms || processing_template_parmlist)
24426    return false;
24427
24428  if (parser->implicit_template_scope)
24429    return true;
24430
24431  if (at_class_scope_p ()
24432      && TYPE_BEING_DEFINED (current_class_type))
24433    return parser->num_template_parameter_lists != 0;
24434
24435  return ((int) parser->num_template_parameter_lists > template_class_depth
24436	  (current_class_type));
24437}
24438
24439/* Parse a parameter-declaration-clause.
24440
24441   parameter-declaration-clause:
24442     parameter-declaration-list [opt] ... [opt]
24443     parameter-declaration-list , ...
24444
24445   The parser flags FLAGS is used to control type-specifier parsing.
24446
24447   Returns a representation for the parameter declarations.  A return
24448   value of NULL indicates a parameter-declaration-clause consisting
24449   only of an ellipsis.  */
24450
24451static tree
24452cp_parser_parameter_declaration_clause (cp_parser* parser,
24453					cp_parser_flags flags)
24454{
24455  tree parameters;
24456  cp_token *token;
24457  bool ellipsis_p;
24458
24459  auto cleanup = make_temp_override
24460    (parser->auto_is_implicit_function_template_parm_p);
24461
24462  if (!processing_specialization
24463      && !processing_template_parmlist
24464      && !processing_explicit_instantiation
24465      /* default_arg_ok_p tracks whether this is a parameter-clause for an
24466         actual function or a random abstract declarator.  */
24467      && parser->default_arg_ok_p)
24468    if (!current_function_decl
24469	|| (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24470      parser->auto_is_implicit_function_template_parm_p = true;
24471
24472  /* Peek at the next token.  */
24473  token = cp_lexer_peek_token (parser->lexer);
24474  /* Check for trivial parameter-declaration-clauses.  */
24475  if (token->type == CPP_ELLIPSIS)
24476    {
24477      /* Consume the `...' token.  */
24478      cp_lexer_consume_token (parser->lexer);
24479      return NULL_TREE;
24480    }
24481  else if (token->type == CPP_CLOSE_PAREN)
24482    /* There are no parameters.  */
24483    return void_list_node;
24484  /* Check for `(void)', too, which is a special case.  */
24485  else if (token->keyword == RID_VOID
24486	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24487	       == CPP_CLOSE_PAREN))
24488    {
24489      /* Consume the `void' token.  */
24490      cp_lexer_consume_token (parser->lexer);
24491      /* There are no parameters.  */
24492      return explicit_void_list_node;
24493    }
24494
24495  /* Parse the parameter-declaration-list.  */
24496  parameters = cp_parser_parameter_declaration_list (parser, flags);
24497  /* If a parse error occurred while parsing the
24498     parameter-declaration-list, then the entire
24499     parameter-declaration-clause is erroneous.  */
24500  if (parameters == error_mark_node)
24501    return NULL_TREE;
24502
24503  /* Peek at the next token.  */
24504  token = cp_lexer_peek_token (parser->lexer);
24505  /* If it's a `,', the clause should terminate with an ellipsis.  */
24506  if (token->type == CPP_COMMA)
24507    {
24508      /* Consume the `,'.  */
24509      cp_lexer_consume_token (parser->lexer);
24510      /* Expect an ellipsis.  */
24511      ellipsis_p
24512	= (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24513    }
24514  /* It might also be `...' if the optional trailing `,' was
24515     omitted.  */
24516  else if (token->type == CPP_ELLIPSIS)
24517    {
24518      /* Consume the `...' token.  */
24519      cp_lexer_consume_token (parser->lexer);
24520      /* And remember that we saw it.  */
24521      ellipsis_p = true;
24522    }
24523  else
24524    ellipsis_p = false;
24525
24526  /* Finish the parameter list.  */
24527  if (!ellipsis_p)
24528    parameters = chainon (parameters, void_list_node);
24529
24530  return parameters;
24531}
24532
24533/* Parse a parameter-declaration-list.
24534
24535   parameter-declaration-list:
24536     parameter-declaration
24537     parameter-declaration-list , parameter-declaration
24538
24539   The parser flags FLAGS is used to control type-specifier parsing.
24540
24541   Returns a representation of the parameter-declaration-list, as for
24542   cp_parser_parameter_declaration_clause.  However, the
24543   `void_list_node' is never appended to the list.  */
24544
24545static tree
24546cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
24547{
24548  tree parameters = NULL_TREE;
24549  tree *tail = &parameters;
24550  bool saved_in_unbraced_linkage_specification_p;
24551  int index = 0;
24552
24553  /* The special considerations that apply to a function within an
24554     unbraced linkage specifications do not apply to the parameters
24555     to the function.  */
24556  saved_in_unbraced_linkage_specification_p
24557    = parser->in_unbraced_linkage_specification_p;
24558  parser->in_unbraced_linkage_specification_p = false;
24559
24560  /* Look for more parameters.  */
24561  while (true)
24562    {
24563      cp_parameter_declarator *parameter;
24564      tree decl = error_mark_node;
24565      bool parenthesized_p = false;
24566
24567      /* Parse the parameter.  */
24568      parameter
24569	= cp_parser_parameter_declaration (parser, flags,
24570					   /*template_parm_p=*/false,
24571					   &parenthesized_p);
24572
24573      /* We don't know yet if the enclosing context is unavailable or deprecated,
24574	 so wait and deal with it in grokparms if appropriate.  */
24575      deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
24576
24577      if (parameter && !cp_parser_error_occurred (parser))
24578	{
24579	  decl = grokdeclarator (parameter->declarator,
24580				 &parameter->decl_specifiers,
24581				 PARM,
24582				 parameter->default_argument != NULL_TREE,
24583				 &parameter->decl_specifiers.attributes);
24584	  if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
24585	    DECL_SOURCE_LOCATION (decl) = parameter->loc;
24586	}
24587
24588      deprecated_state = DEPRECATED_NORMAL;
24589
24590      /* If a parse error occurred parsing the parameter declaration,
24591	 then the entire parameter-declaration-list is erroneous.  */
24592      if (decl == error_mark_node)
24593	{
24594	  parameters = error_mark_node;
24595	  break;
24596	}
24597
24598      if (parameter->decl_specifiers.attributes)
24599	cplus_decl_attributes (&decl,
24600			       parameter->decl_specifiers.attributes,
24601			       0);
24602      if (DECL_NAME (decl))
24603	decl = pushdecl (decl);
24604
24605      if (decl != error_mark_node)
24606	{
24607	  retrofit_lang_decl (decl);
24608	  DECL_PARM_INDEX (decl) = ++index;
24609	  DECL_PARM_LEVEL (decl) = function_parm_depth ();
24610	}
24611
24612      /* Add the new parameter to the list.  */
24613      *tail = build_tree_list (parameter->default_argument, decl);
24614      tail = &TREE_CHAIN (*tail);
24615
24616      /* If the parameters were parenthesized, it's the case of
24617	 T foo(X(x)) which looks like a variable definition but
24618	 is a function declaration.  */
24619      if (index == 1 || PARENTHESIZED_LIST_P (parameters))
24620	PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
24621
24622      /* Peek at the next token.  */
24623      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
24624	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
24625	  /* These are for Objective-C++ */
24626	  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24627	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24628	/* The parameter-declaration-list is complete.  */
24629	break;
24630      else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24631	{
24632	  cp_token *token;
24633
24634	  /* Peek at the next token.  */
24635	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
24636	  /* If it's an ellipsis, then the list is complete.  */
24637	  if (token->type == CPP_ELLIPSIS)
24638	    break;
24639	  /* Otherwise, there must be more parameters.  Consume the
24640	     `,'.  */
24641	  cp_lexer_consume_token (parser->lexer);
24642	  /* When parsing something like:
24643
24644		int i(float f, double d)
24645
24646	     we can tell after seeing the declaration for "f" that we
24647	     are not looking at an initialization of a variable "i",
24648	     but rather at the declaration of a function "i".
24649
24650	     Due to the fact that the parsing of template arguments
24651	     (as specified to a template-id) requires backtracking we
24652	     cannot use this technique when inside a template argument
24653	     list.  */
24654	  if (!parser->in_template_argument_list_p
24655	      && !parser->in_type_id_in_expr_p
24656	      && cp_parser_uncommitted_to_tentative_parse_p (parser)
24657	      /* However, a parameter-declaration of the form
24658		 "float(f)" (which is a valid declaration of a
24659		 parameter "f") can also be interpreted as an
24660		 expression (the conversion of "f" to "float").  */
24661	      && !parenthesized_p)
24662	    cp_parser_commit_to_tentative_parse (parser);
24663	}
24664      else
24665	{
24666	  cp_parser_error (parser, "expected %<,%> or %<...%>");
24667	  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24668	    cp_parser_skip_to_closing_parenthesis (parser,
24669						   /*recovering=*/true,
24670						   /*or_comma=*/false,
24671						   /*consume_paren=*/false);
24672	  break;
24673	}
24674    }
24675
24676  parser->in_unbraced_linkage_specification_p
24677    = saved_in_unbraced_linkage_specification_p;
24678
24679  /* Reset implicit_template_scope if we are about to leave the function
24680     parameter list that introduced it.  Note that for out-of-line member
24681     definitions, there will be one or more class scopes before we get to
24682     the template parameter scope.  */
24683
24684  if (cp_binding_level *its = parser->implicit_template_scope)
24685    if (cp_binding_level *maybe_its = current_binding_level->level_chain)
24686      {
24687	while (maybe_its->kind == sk_class)
24688	  maybe_its = maybe_its->level_chain;
24689	if (maybe_its == its)
24690	  {
24691	    parser->implicit_template_parms = 0;
24692	    parser->implicit_template_scope = 0;
24693	  }
24694      }
24695
24696  return parameters;
24697}
24698
24699/* Parse a parameter declaration.
24700
24701   parameter-declaration:
24702     decl-specifier-seq ... [opt] declarator
24703     decl-specifier-seq declarator = assignment-expression
24704     decl-specifier-seq ... [opt] abstract-declarator [opt]
24705     decl-specifier-seq abstract-declarator [opt] = assignment-expression
24706
24707   The parser flags FLAGS is used to control type-specifier parsing.
24708
24709   If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
24710   declares a template parameter.  (In that case, a non-nested `>'
24711   token encountered during the parsing of the assignment-expression
24712   is not interpreted as a greater-than operator.)
24713
24714   Returns a representation of the parameter, or NULL if an error
24715   occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
24716   true iff the declarator is of the form "(p)".  */
24717
24718static cp_parameter_declarator *
24719cp_parser_parameter_declaration (cp_parser *parser,
24720				 cp_parser_flags flags,
24721				 bool template_parm_p,
24722				 bool *parenthesized_p)
24723{
24724  int declares_class_or_enum;
24725  cp_decl_specifier_seq decl_specifiers;
24726  cp_declarator *declarator;
24727  tree default_argument;
24728  cp_token *token = NULL, *declarator_token_start = NULL;
24729  const char *saved_message;
24730  bool template_parameter_pack_p = false;
24731
24732  /* In a template parameter, `>' is not an operator.
24733
24734     [temp.param]
24735
24736     When parsing a default template-argument for a non-type
24737     template-parameter, the first non-nested `>' is taken as the end
24738     of the template parameter-list rather than a greater-than
24739     operator.  */
24740
24741  /* Type definitions may not appear in parameter types.  */
24742  saved_message = parser->type_definition_forbidden_message;
24743  parser->type_definition_forbidden_message
24744    = G_("types may not be defined in parameter types");
24745
24746  int template_parm_idx = (function_being_declared_is_template_p (parser) ?
24747			   TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
24748					    (current_template_parms)) : 0);
24749
24750  /* Parse the declaration-specifiers.  */
24751  cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24752  cp_parser_decl_specifier_seq (parser,
24753				flags,
24754				&decl_specifiers,
24755				&declares_class_or_enum);
24756
24757  /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
24758     type-constraint opt auto can be used as a decl-specifier of the
24759     decl-specifier-seq of a parameter-declaration of a function declaration
24760     or lambda-expression..." but we must not synthesize an implicit template
24761     type parameter in its declarator.  That is, in "void f(auto[auto{10}]);"
24762     we want to synthesize only the first auto.  */
24763  auto cleanup = make_temp_override
24764    (parser->auto_is_implicit_function_template_parm_p, false);
24765
24766  /* Complain about missing 'typename' or other invalid type names.  */
24767  if (!decl_specifiers.any_type_specifiers_p
24768      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24769    decl_specifiers.type = error_mark_node;
24770
24771  /* If an error occurred, there's no reason to attempt to parse the
24772     rest of the declaration.  */
24773  if (cp_parser_error_occurred (parser))
24774    {
24775      parser->type_definition_forbidden_message = saved_message;
24776      return NULL;
24777    }
24778
24779  /* Peek at the next token.  */
24780  token = cp_lexer_peek_token (parser->lexer);
24781
24782  /* If the next token is a `)', `,', `=', `>', or `...', then there
24783     is no declarator. However, when variadic templates are enabled,
24784     there may be a declarator following `...'.  */
24785  if (token->type == CPP_CLOSE_PAREN
24786      || token->type == CPP_COMMA
24787      || token->type == CPP_EQ
24788      || token->type == CPP_GREATER)
24789    {
24790      declarator = NULL;
24791      if (parenthesized_p)
24792	*parenthesized_p = false;
24793    }
24794  /* Otherwise, there should be a declarator.  */
24795  else
24796    {
24797      bool saved_default_arg_ok_p = parser->default_arg_ok_p;
24798      parser->default_arg_ok_p = false;
24799
24800      /* After seeing a decl-specifier-seq, if the next token is not a
24801	 "(" or "{", there is no possibility that the code is a valid
24802	 expression.  Therefore, if parsing tentatively, we commit at
24803	 this point.  */
24804      if (!parser->in_template_argument_list_p
24805	  /* In an expression context, having seen:
24806
24807	       (int((char ...
24808
24809	     we cannot be sure whether we are looking at a
24810	     function-type (taking a "char" as a parameter) or a cast
24811	     of some object of type "char" to "int".  */
24812	  && !parser->in_type_id_in_expr_p
24813	  && cp_parser_uncommitted_to_tentative_parse_p (parser)
24814	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
24815	{
24816	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24817	    {
24818	      if (decl_specifiers.type
24819		  && template_placeholder_p (decl_specifiers.type))
24820		/* This is a CTAD expression, not a parameter declaration.  */
24821		cp_parser_simulate_error (parser);
24822	    }
24823	  else
24824	    cp_parser_commit_to_tentative_parse (parser);
24825	}
24826      /* Parse the declarator.  */
24827      declarator_token_start = token;
24828      declarator = cp_parser_declarator (parser,
24829					 CP_PARSER_DECLARATOR_EITHER,
24830					 CP_PARSER_FLAGS_NONE,
24831					 /*ctor_dtor_or_conv_p=*/NULL,
24832					 parenthesized_p,
24833					 /*member_p=*/false,
24834					 /*friend_p=*/false,
24835					 /*static_p=*/false);
24836      parser->default_arg_ok_p = saved_default_arg_ok_p;
24837      /* After the declarator, allow more attributes.  */
24838      decl_specifiers.attributes
24839	= attr_chainon (decl_specifiers.attributes,
24840			cp_parser_attributes_opt (parser));
24841
24842      /* If the declarator is a template parameter pack, remember that and
24843	 clear the flag in the declarator itself so we don't get errors
24844	 from grokdeclarator.  */
24845      if (template_parm_p && declarator && declarator->parameter_pack_p)
24846	{
24847	  declarator->parameter_pack_p = false;
24848	  template_parameter_pack_p = true;
24849	}
24850    }
24851
24852  /* If the next token is an ellipsis, and we have not seen a declarator
24853     name, and if either the type of the declarator contains parameter
24854     packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
24855     for, eg, abbreviated integral type names), then we actually have a
24856     parameter pack expansion expression. Otherwise, leave the ellipsis
24857     for a C-style variadic function. */
24858  token = cp_lexer_peek_token (parser->lexer);
24859
24860  /* If a function parameter pack was specified and an implicit template
24861     parameter was introduced during cp_parser_parameter_declaration,
24862     change any implicit parameters introduced into packs.  */
24863  if (parser->implicit_template_parms
24864      && ((token->type == CPP_ELLIPSIS
24865	   && declarator_can_be_parameter_pack (declarator))
24866	  || (declarator && declarator->parameter_pack_p)))
24867    {
24868      int latest_template_parm_idx = TREE_VEC_LENGTH
24869	(INNERMOST_TEMPLATE_PARMS (current_template_parms));
24870
24871      if (latest_template_parm_idx != template_parm_idx)
24872	decl_specifiers.type = convert_generic_types_to_packs
24873	  (decl_specifiers.type,
24874	   template_parm_idx, latest_template_parm_idx);
24875    }
24876
24877  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24878    {
24879      tree type = decl_specifiers.type;
24880
24881      if (type && DECL_P (type))
24882        type = TREE_TYPE (type);
24883
24884      if (((type
24885	    && TREE_CODE (type) != TYPE_PACK_EXPANSION
24886	    && (template_parm_p || uses_parameter_packs (type)))
24887	   || (!type && template_parm_p))
24888	  && declarator_can_be_parameter_pack (declarator))
24889	{
24890	  /* Consume the `...'. */
24891	  cp_lexer_consume_token (parser->lexer);
24892	  maybe_warn_variadic_templates ();
24893
24894	  /* Build a pack expansion type */
24895	  if (template_parm_p)
24896	    template_parameter_pack_p = true;
24897	  else if (declarator)
24898	    declarator->parameter_pack_p = true;
24899	  else
24900	    decl_specifiers.type = make_pack_expansion (type);
24901	}
24902    }
24903
24904  /* The restriction on defining new types applies only to the type
24905     of the parameter, not to the default argument.  */
24906  parser->type_definition_forbidden_message = saved_message;
24907
24908  /* If the next token is `=', then process a default argument.  */
24909  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24910    {
24911      tree type = decl_specifiers.type;
24912      token = cp_lexer_peek_token (parser->lexer);
24913      if (declarator)
24914	declarator->init_loc = token->location;
24915      /* If we are defining a class, then the tokens that make up the
24916	 default argument must be saved and processed later.  */
24917      if (!template_parm_p && at_class_scope_p ()
24918	  && TYPE_BEING_DEFINED (current_class_type)
24919	  && !LAMBDA_TYPE_P (current_class_type))
24920	default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
24921
24922      /* A constrained-type-specifier may declare a type
24923	 template-parameter.  */
24924      else if (declares_constrained_type_template_parameter (type))
24925        default_argument
24926          = cp_parser_default_type_template_argument (parser);
24927
24928      /* A constrained-type-specifier may declare a
24929	 template-template-parameter.  */
24930      else if (declares_constrained_template_template_parameter (type))
24931        default_argument
24932          = cp_parser_default_template_template_argument (parser);
24933
24934      /* Outside of a class definition, we can just parse the
24935	 assignment-expression.  */
24936      else
24937	default_argument
24938	  = cp_parser_default_argument (parser, template_parm_p);
24939
24940      if (!parser->default_arg_ok_p)
24941	{
24942	  permerror (token->location,
24943		     "default arguments are only "
24944		     "permitted for function parameters");
24945	}
24946      else if ((declarator && declarator->parameter_pack_p)
24947	       || template_parameter_pack_p
24948	       || (decl_specifiers.type
24949		   && PACK_EXPANSION_P (decl_specifiers.type)))
24950	{
24951	  /* Find the name of the parameter pack.  */
24952	  cp_declarator *id_declarator = declarator;
24953	  while (id_declarator && id_declarator->kind != cdk_id)
24954	    id_declarator = id_declarator->declarator;
24955
24956	  if (id_declarator && id_declarator->kind == cdk_id)
24957	    error_at (declarator_token_start->location,
24958		      template_parm_p
24959		      ? G_("template parameter pack %qD "
24960			   "cannot have a default argument")
24961		      : G_("parameter pack %qD cannot have "
24962			   "a default argument"),
24963		      id_declarator->u.id.unqualified_name);
24964	  else
24965	    error_at (declarator_token_start->location,
24966		      template_parm_p
24967		      ? G_("template parameter pack cannot have "
24968			   "a default argument")
24969		      : G_("parameter pack cannot have a "
24970			   "default argument"));
24971
24972	  default_argument = NULL_TREE;
24973	}
24974    }
24975  else
24976    default_argument = NULL_TREE;
24977
24978  if (default_argument)
24979    STRIP_ANY_LOCATION_WRAPPER (default_argument);
24980
24981  /* Generate a location for the parameter, ranging from the start of the
24982     initial token to the end of the final token (using input_location for
24983     the latter, set up by cp_lexer_set_source_position_from_token when
24984     consuming tokens).
24985
24986     If we have a identifier, then use it for the caret location, e.g.
24987
24988       extern int callee (int one, int (*two)(int, int), float three);
24989                                   ~~~~~~^~~~~~~~~~~~~~
24990
24991     otherwise, reuse the start location for the caret location e.g.:
24992
24993       extern int callee (int one, int (*)(int, int), float three);
24994                                   ^~~~~~~~~~~~~~~~~
24995
24996  */
24997  location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
24998			  ? declarator->id_loc
24999			  : decl_spec_token_start->location);
25000  location_t param_loc = make_location (caret_loc,
25001					decl_spec_token_start->location,
25002					input_location);
25003
25004  return make_parameter_declarator (&decl_specifiers,
25005				    declarator,
25006				    default_argument,
25007				    param_loc,
25008				    template_parameter_pack_p);
25009}
25010
25011/* Parse a default argument and return it.
25012
25013   TEMPLATE_PARM_P is true if this is a default argument for a
25014   non-type template parameter.  */
25015static tree
25016cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25017{
25018  tree default_argument = NULL_TREE;
25019  bool saved_greater_than_is_operator_p;
25020  unsigned char saved_local_variables_forbidden_p;
25021  bool non_constant_p, is_direct_init;
25022
25023  /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25024     set correctly.  */
25025  saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25026  parser->greater_than_is_operator_p = !template_parm_p;
25027  auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25028  auto ord = make_temp_override (parser->oacc_routine, NULL);
25029  auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25030
25031  /* Local variable names (and the `this' keyword) may not
25032     appear in a default argument.  */
25033  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25034  parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25035  /* Parse the assignment-expression.  */
25036  if (template_parm_p)
25037    push_deferring_access_checks (dk_no_deferred);
25038  tree saved_class_ptr = NULL_TREE;
25039  tree saved_class_ref = NULL_TREE;
25040  /* The "this" pointer is not valid in a default argument.  */
25041  if (cfun)
25042    {
25043      saved_class_ptr = current_class_ptr;
25044      cp_function_chain->x_current_class_ptr = NULL_TREE;
25045      saved_class_ref = current_class_ref;
25046      cp_function_chain->x_current_class_ref = NULL_TREE;
25047    }
25048  default_argument
25049    = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
25050  /* Restore the "this" pointer.  */
25051  if (cfun)
25052    {
25053      cp_function_chain->x_current_class_ptr = saved_class_ptr;
25054      cp_function_chain->x_current_class_ref = saved_class_ref;
25055    }
25056  if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25057    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25058  if (template_parm_p)
25059    pop_deferring_access_checks ();
25060  parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25061  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25062
25063  return default_argument;
25064}
25065
25066/* Parse a function-body.
25067
25068   function-body:
25069     compound_statement  */
25070
25071static void
25072cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25073{
25074  cp_parser_compound_statement (parser, NULL, (in_function_try_block
25075					       ? BCS_TRY_BLOCK : BCS_NORMAL),
25076				true);
25077}
25078
25079/* Parse a ctor-initializer-opt followed by a function-body.  Return
25080   true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
25081   is true we are parsing a function-try-block.  */
25082
25083static void
25084cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25085						  bool in_function_try_block)
25086{
25087  tree body, list;
25088  const bool check_body_p
25089     = (DECL_CONSTRUCTOR_P (current_function_decl)
25090	&& DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25091  tree last = NULL;
25092
25093  if (in_function_try_block
25094      && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25095      && cxx_dialect < cxx20)
25096    {
25097      if (DECL_CONSTRUCTOR_P (current_function_decl))
25098	pedwarn (input_location, OPT_Wc__20_extensions,
25099		 "function-try-block body of %<constexpr%> constructor only "
25100		 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25101      else
25102	pedwarn (input_location, OPT_Wc__20_extensions,
25103		 "function-try-block body of %<constexpr%> function only "
25104		 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25105    }
25106
25107  /* Begin the function body.  */
25108  body = begin_function_body ();
25109  /* Parse the optional ctor-initializer.  */
25110  cp_parser_ctor_initializer_opt (parser);
25111
25112  /* If we're parsing a constexpr constructor definition, we need
25113     to check that the constructor body is indeed empty.  However,
25114     before we get to cp_parser_function_body lot of junk has been
25115     generated, so we can't just check that we have an empty block.
25116     Rather we take a snapshot of the outermost block, and check whether
25117     cp_parser_function_body changed its state.  */
25118  if (check_body_p)
25119    {
25120      list = cur_stmt_list;
25121      if (STATEMENT_LIST_TAIL (list))
25122	last = STATEMENT_LIST_TAIL (list)->stmt;
25123    }
25124  /* Parse the function-body.  */
25125  cp_parser_function_body (parser, in_function_try_block);
25126  if (check_body_p)
25127    check_constexpr_ctor_body (last, list, /*complain=*/true);
25128  /* Finish the function body.  */
25129  finish_function_body (body);
25130}
25131
25132/* Parse an initializer.
25133
25134   initializer:
25135     = initializer-clause
25136     ( expression-list )
25137
25138   Returns an expression representing the initializer.  If no
25139   initializer is present, NULL_TREE is returned.
25140
25141   *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25142   production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
25143   set to TRUE if there is no initializer present.  If there is an
25144   initializer, and it is not a constant-expression, *NON_CONSTANT_P
25145   is set to true; otherwise it is set to false.  */
25146
25147static tree
25148cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
25149		       bool* non_constant_p, bool subexpression_p)
25150{
25151  cp_token *token;
25152  tree init;
25153
25154  /* Peek at the next token.  */
25155  token = cp_lexer_peek_token (parser->lexer);
25156
25157  /* Let our caller know whether or not this initializer was
25158     parenthesized.  */
25159  *is_direct_init = (token->type != CPP_EQ);
25160  /* Assume that the initializer is constant.  */
25161  *non_constant_p = false;
25162
25163  if (token->type == CPP_EQ)
25164    {
25165      /* Consume the `='.  */
25166      cp_lexer_consume_token (parser->lexer);
25167      /* Parse the initializer-clause.  */
25168      init = cp_parser_initializer_clause (parser, non_constant_p);
25169    }
25170  else if (token->type == CPP_OPEN_PAREN)
25171    {
25172      vec<tree, va_gc> *vec;
25173      vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25174						     /*cast_p=*/false,
25175						     /*allow_expansion_p=*/true,
25176						     non_constant_p);
25177      if (vec == NULL)
25178	return error_mark_node;
25179      init = build_tree_list_vec (vec);
25180      release_tree_vector (vec);
25181    }
25182  else if (token->type == CPP_OPEN_BRACE)
25183    {
25184      cp_lexer_set_source_position (parser->lexer);
25185      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25186      init = cp_parser_braced_list (parser, non_constant_p);
25187      CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25188    }
25189  else
25190    {
25191      /* Anything else is an error.  */
25192      cp_parser_error (parser, "expected initializer");
25193      init = error_mark_node;
25194    }
25195
25196  if (!subexpression_p && check_for_bare_parameter_packs (init))
25197    init = error_mark_node;
25198
25199  return init;
25200}
25201
25202/* Parse an initializer-clause.
25203
25204   initializer-clause:
25205     assignment-expression
25206     braced-init-list
25207
25208   Returns an expression representing the initializer.
25209
25210   If the `assignment-expression' production is used the value
25211   returned is simply a representation for the expression.
25212
25213   Otherwise, calls cp_parser_braced_list.  */
25214
25215static cp_expr
25216cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25217{
25218  cp_expr initializer;
25219
25220  /* Assume the expression is constant.  */
25221  *non_constant_p = false;
25222
25223  /* If it is not a `{', then we are looking at an
25224     assignment-expression.  */
25225  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25226    {
25227      initializer
25228	= cp_parser_constant_expression (parser,
25229					/*allow_non_constant_p=*/2,
25230					non_constant_p);
25231    }
25232  else
25233    initializer = cp_parser_braced_list (parser, non_constant_p);
25234
25235  return initializer;
25236}
25237
25238/* Parse a brace-enclosed initializer list.
25239
25240   braced-init-list:
25241     { initializer-list , [opt] }
25242     { designated-initializer-list , [opt] }
25243     { }
25244
25245   Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
25246   the elements of the initializer-list (or NULL, if the last
25247   production is used).  The TREE_TYPE for the CONSTRUCTOR will be
25248   NULL_TREE.  There is no way to detect whether or not the optional
25249   trailing `,' was provided.  NON_CONSTANT_P is as for
25250   cp_parser_initializer.  */
25251
25252static cp_expr
25253cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
25254{
25255  tree initializer;
25256  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25257
25258  /* Consume the `{' token.  */
25259  matching_braces braces;
25260  braces.require_open (parser);
25261  /* Create a CONSTRUCTOR to represent the braced-initializer.  */
25262  initializer = make_node (CONSTRUCTOR);
25263  /* If it's not a `}', then there is a non-trivial initializer.  */
25264  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25265    {
25266      bool designated;
25267      /* Parse the initializer list.  */
25268      CONSTRUCTOR_ELTS (initializer)
25269	= cp_parser_initializer_list (parser, non_constant_p, &designated);
25270      CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25271      /* A trailing `,' token is allowed.  */
25272      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25273	cp_lexer_consume_token (parser->lexer);
25274    }
25275  else
25276    *non_constant_p = false;
25277  /* Now, there should be a trailing `}'.  */
25278  location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25279  braces.require_close (parser);
25280  TREE_TYPE (initializer) = init_list_type_node;
25281
25282  cp_expr result (initializer);
25283  /* Build a location of the form:
25284       { ... }
25285       ^~~~~~~
25286     with caret==start at the open brace, finish at the close brace.  */
25287  location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25288  result.set_location (combined_loc);
25289  return result;
25290}
25291
25292/* Consume tokens up to, and including, the next non-nested closing `]'.
25293   Returns true iff we found a closing `]'.  */
25294
25295static bool
25296cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25297{
25298  unsigned square_depth = 0;
25299
25300  while (true)
25301    {
25302      cp_token * token = cp_lexer_peek_token (parser->lexer);
25303
25304      switch (token->type)
25305	{
25306	case CPP_PRAGMA_EOL:
25307	  if (!parser->lexer->in_pragma)
25308	    break;
25309	  /* FALLTHRU */
25310	case CPP_EOF:
25311	  /* If we've run out of tokens, then there is no closing `]'.  */
25312	  return false;
25313
25314        case CPP_OPEN_SQUARE:
25315          ++square_depth;
25316          break;
25317
25318        case CPP_CLOSE_SQUARE:
25319	  if (!square_depth--)
25320	    {
25321	      cp_lexer_consume_token (parser->lexer);
25322	      return true;
25323	    }
25324	  break;
25325
25326	default:
25327	  break;
25328	}
25329
25330      /* Consume the token.  */
25331      cp_lexer_consume_token (parser->lexer);
25332    }
25333}
25334
25335/* Return true if we are looking at an array-designator, false otherwise.  */
25336
25337static bool
25338cp_parser_array_designator_p (cp_parser *parser)
25339{
25340  /* Consume the `['.  */
25341  cp_lexer_consume_token (parser->lexer);
25342
25343  cp_lexer_save_tokens (parser->lexer);
25344
25345  /* Skip tokens until the next token is a closing square bracket.
25346     If we find the closing `]', and the next token is a `=', then
25347     we are looking at an array designator.  */
25348  bool array_designator_p
25349    = (cp_parser_skip_to_closing_square_bracket (parser)
25350       && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25351
25352  /* Roll back the tokens we skipped.  */
25353  cp_lexer_rollback_tokens (parser->lexer);
25354
25355  return array_designator_p;
25356}
25357
25358/* Parse an initializer-list.
25359
25360   initializer-list:
25361     initializer-clause ... [opt]
25362     initializer-list , initializer-clause ... [opt]
25363
25364   C++20 Extension:
25365
25366   designated-initializer-list:
25367     designated-initializer-clause
25368     designated-initializer-list , designated-initializer-clause
25369
25370   designated-initializer-clause:
25371     designator brace-or-equal-initializer
25372
25373   designator:
25374     . identifier
25375
25376   GNU Extension:
25377
25378   initializer-list:
25379     designation initializer-clause ...[opt]
25380     initializer-list , designation initializer-clause ...[opt]
25381
25382   designation:
25383     . identifier =
25384     identifier :
25385     [ constant-expression ] =
25386
25387   Returns a vec of constructor_elt.  The VALUE of each elt is an expression
25388   for the initializer.  If the INDEX of the elt is non-NULL, it is the
25389   IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
25390   as for cp_parser_initializer.  Set *DESIGNATED to a boolean whether there
25391   are any designators.  */
25392
25393static vec<constructor_elt, va_gc> *
25394cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25395			    bool *designated)
25396{
25397  vec<constructor_elt, va_gc> *v = NULL;
25398  bool first_p = true;
25399  tree first_designator = NULL_TREE;
25400
25401  /* Assume all of the expressions are constant.  */
25402  *non_constant_p = false;
25403
25404  unsigned nelts = 0;
25405  int suppress = suppress_location_wrappers;
25406
25407  /* Parse the rest of the list.  */
25408  while (true)
25409    {
25410      cp_token *token;
25411      tree designator;
25412      tree initializer;
25413      bool clause_non_constant_p;
25414      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25415
25416      /* Handle the C++20 syntax, '. id ='.  */
25417      if ((cxx_dialect >= cxx20
25418	   || cp_parser_allow_gnu_extensions_p (parser))
25419	  && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25420	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25421	  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25422	      || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25423		  == CPP_OPEN_BRACE)))
25424	{
25425	  if (pedantic && cxx_dialect < cxx20)
25426	    pedwarn (loc, OPT_Wc__20_extensions,
25427		     "C++ designated initializers only available with "
25428		     "%<-std=c++20%> or %<-std=gnu++20%>");
25429	  /* Consume the `.'.  */
25430	  cp_lexer_consume_token (parser->lexer);
25431	  /* Consume the identifier.  */
25432	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
25433	  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25434	    /* Consume the `='.  */
25435	    cp_lexer_consume_token (parser->lexer);
25436	}
25437      /* Also, if the next token is an identifier and the following one is a
25438	 colon, we are looking at the GNU designated-initializer
25439	 syntax.  */
25440      else if (cp_parser_allow_gnu_extensions_p (parser)
25441	       && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25442	       && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25443		   == CPP_COLON))
25444	{
25445	  /* Warn the user that they are using an extension.  */
25446	  pedwarn (loc, OPT_Wpedantic,
25447		   "ISO C++ does not allow GNU designated initializers");
25448	  /* Consume the identifier.  */
25449	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
25450	  /* Consume the `:'.  */
25451	  cp_lexer_consume_token (parser->lexer);
25452	}
25453      /* Also handle C99 array designators, '[ const ] ='.  */
25454      else if (cp_parser_allow_gnu_extensions_p (parser)
25455	       && !c_dialect_objc ()
25456	       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25457	{
25458	  /* In C++11, [ could start a lambda-introducer.  */
25459	  bool non_const = false;
25460
25461	  cp_parser_parse_tentatively (parser);
25462
25463	  if (!cp_parser_array_designator_p (parser))
25464	    {
25465	      cp_parser_simulate_error (parser);
25466	      designator = NULL_TREE;
25467	    }
25468	  else
25469	    {
25470	      designator = cp_parser_constant_expression (parser, true,
25471							  &non_const);
25472	      cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25473	      cp_parser_require (parser, CPP_EQ, RT_EQ);
25474	    }
25475
25476	  if (!cp_parser_parse_definitely (parser))
25477	    designator = NULL_TREE;
25478	  else if (non_const
25479		   && (!require_potential_rvalue_constant_expression
25480		       (designator)))
25481	    designator = NULL_TREE;
25482	  if (designator)
25483	    /* Warn the user that they are using an extension.  */
25484	    pedwarn (loc, OPT_Wpedantic,
25485		     "ISO C++ does not allow C99 designated initializers");
25486	}
25487      else
25488	designator = NULL_TREE;
25489
25490      if (first_p)
25491	{
25492	  first_designator = designator;
25493	  first_p = false;
25494	}
25495      else if (cxx_dialect >= cxx20
25496	       && first_designator != error_mark_node
25497	       && (!first_designator != !designator))
25498	{
25499	  error_at (loc, "either all initializer clauses should be designated "
25500			 "or none of them should be");
25501	  first_designator = error_mark_node;
25502	}
25503      else if (cxx_dialect < cxx20 && !first_designator)
25504	first_designator = designator;
25505
25506      /* Parse the initializer.  */
25507      initializer = cp_parser_initializer_clause (parser,
25508						  &clause_non_constant_p);
25509      /* If any clause is non-constant, so is the entire initializer.  */
25510      if (clause_non_constant_p)
25511	*non_constant_p = true;
25512
25513      /* If we have an ellipsis, this is an initializer pack
25514	 expansion.  */
25515      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25516        {
25517	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25518
25519          /* Consume the `...'.  */
25520          cp_lexer_consume_token (parser->lexer);
25521
25522	  if (designator && cxx_dialect >= cxx20)
25523	    error_at (loc,
25524		      "%<...%> not allowed in designated initializer list");
25525
25526	  /* Turn the initializer into an initializer expansion.  */
25527	  initializer = make_pack_expansion (initializer);
25528        }
25529
25530      /* Add it to the vector.  */
25531      CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
25532
25533      /* If the next token is not a comma, we have reached the end of
25534	 the list.  */
25535      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25536	break;
25537
25538      /* Peek at the next token.  */
25539      token = cp_lexer_peek_nth_token (parser->lexer, 2);
25540      /* If the next token is a `}', then we're still done.  An
25541	 initializer-clause can have a trailing `,' after the
25542	 initializer-list and before the closing `}'.  */
25543      if (token->type == CPP_CLOSE_BRACE)
25544	break;
25545
25546      /* Suppress location wrappers in a long initializer to save memory
25547	 (14179).  The cutoff is chosen arbitrarily.  */
25548      const unsigned loc_max = 256;
25549      unsigned incr = 1;
25550      if (TREE_CODE (initializer) == CONSTRUCTOR)
25551	/* Look one level down because it's easy.  Looking deeper would require
25552	   passing down a nelts pointer, and I don't think multi-level massive
25553	   initializers are common enough to justify this.  */
25554	incr = CONSTRUCTOR_NELTS (initializer);
25555      nelts += incr;
25556      if (nelts >= loc_max && (nelts - incr) < loc_max)
25557	++suppress_location_wrappers;
25558
25559      /* Consume the `,' token.  */
25560      cp_lexer_consume_token (parser->lexer);
25561    }
25562
25563  /* The same identifier shall not appear in multiple designators
25564     of a designated-initializer-list.  */
25565  if (first_designator)
25566    {
25567      unsigned int i;
25568      tree designator, val;
25569      FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25570	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25571	  {
25572	    if (IDENTIFIER_MARKED (designator))
25573	      {
25574		error_at (cp_expr_loc_or_input_loc (val),
25575			  "%<.%s%> designator used multiple times in "
25576			  "the same initializer list",
25577			  IDENTIFIER_POINTER (designator));
25578		(*v)[i].index = error_mark_node;
25579	      }
25580	    else
25581	      IDENTIFIER_MARKED (designator) = 1;
25582	  }
25583      FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25584	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25585	  IDENTIFIER_MARKED (designator) = 0;
25586    }
25587
25588  suppress_location_wrappers = suppress;
25589
25590  *designated = first_designator != NULL_TREE;
25591  return v;
25592}
25593
25594/* Classes [gram.class] */
25595
25596/* Parse a class-name.
25597
25598   class-name:
25599     identifier
25600     template-id
25601
25602   TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25603   to indicate that names looked up in dependent types should be
25604   assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
25605   keyword has been used to indicate that the name that appears next
25606   is a template.  TAG_TYPE indicates the explicit tag given before
25607   the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
25608   looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
25609   is the class being defined in a class-head.  If ENUM_OK is TRUE,
25610   enum-names are also accepted.
25611
25612   Returns the TYPE_DECL representing the class.  */
25613
25614static tree
25615cp_parser_class_name (cp_parser *parser,
25616		      bool typename_keyword_p,
25617		      bool template_keyword_p,
25618		      enum tag_types tag_type,
25619		      bool check_dependency_p,
25620		      bool class_head_p,
25621		      bool is_declaration,
25622		      bool enum_ok)
25623{
25624  tree decl;
25625  tree identifier = NULL_TREE;
25626
25627  /* All class-names start with an identifier.  */
25628  cp_token *token = cp_lexer_peek_token (parser->lexer);
25629  if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
25630    {
25631      cp_parser_error (parser, "expected class-name");
25632      return error_mark_node;
25633    }
25634
25635  /* PARSER->SCOPE can be cleared when parsing the template-arguments
25636     to a template-id, so we save it here.  Consider object scope too,
25637     so that make_typename_type below can use it (cp_parser_template_name
25638     considers object scope also).  This may happen with code like
25639
25640       p->template A<T>::a()
25641
25642      where we first want to look up A<T>::a in the class of the object
25643      expression, as per [basic.lookup.classref].  */
25644  tree scope = parser->scope ? parser->scope : parser->context->object_type;
25645  /* This only checks parser->scope to avoid duplicate errors; if
25646     ->object_type is erroneous, go on to give a parse error.  */
25647  if (parser->scope == error_mark_node)
25648    return error_mark_node;
25649
25650  /* Any name names a type if we're following the `typename' keyword
25651     in a qualified name where the enclosing scope is type-dependent.  */
25652  const bool typename_p = (typename_keyword_p
25653			   && parser->scope
25654			   && TYPE_P (parser->scope)
25655			   && dependent_scope_p (parser->scope));
25656  /* Handle the common case (an identifier, but not a template-id)
25657     efficiently.  */
25658  if (token->type == CPP_NAME
25659      && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
25660    {
25661      cp_token *identifier_token;
25662      bool ambiguous_p;
25663
25664      /* Look for the identifier.  */
25665      identifier_token = cp_lexer_peek_token (parser->lexer);
25666      ambiguous_p = identifier_token->error_reported;
25667      identifier = cp_parser_identifier (parser);
25668      /* If the next token isn't an identifier, we are certainly not
25669	 looking at a class-name.  */
25670      if (identifier == error_mark_node)
25671	decl = error_mark_node;
25672      /* If we know this is a type-name, there's no need to look it
25673	 up.  */
25674      else if (typename_p)
25675	decl = identifier;
25676      else
25677	{
25678	  tree ambiguous_decls;
25679	  /* If we already know that this lookup is ambiguous, then
25680	     we've already issued an error message; there's no reason
25681	     to check again.  */
25682	  if (ambiguous_p)
25683	    {
25684	      cp_parser_simulate_error (parser);
25685	      return error_mark_node;
25686	    }
25687	  /* If the next token is a `::', then the name must be a type
25688	     name.
25689
25690	     [basic.lookup.qual]
25691
25692	     During the lookup for a name preceding the :: scope
25693	     resolution operator, object, function, and enumerator
25694	     names are ignored.  */
25695	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25696	    tag_type = scope_type;
25697	  /* Look up the name.  */
25698	  decl = cp_parser_lookup_name (parser, identifier,
25699					tag_type,
25700					/*is_template=*/false,
25701					/*is_namespace=*/false,
25702					check_dependency_p,
25703					&ambiguous_decls,
25704					identifier_token->location);
25705	  if (ambiguous_decls)
25706	    {
25707	      if (cp_parser_parsing_tentatively (parser))
25708		cp_parser_simulate_error (parser);
25709	      return error_mark_node;
25710	    }
25711	}
25712    }
25713  else
25714    {
25715      /* Try a template-id.  */
25716      decl = cp_parser_template_id (parser, template_keyword_p,
25717				    check_dependency_p,
25718				    tag_type,
25719				    is_declaration);
25720      if (decl == error_mark_node)
25721	return error_mark_node;
25722    }
25723
25724  decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
25725
25726  /* If this is a typename, create a TYPENAME_TYPE.  */
25727  if (typename_p && decl != error_mark_node)
25728    {
25729      decl = make_typename_type (scope, decl, typename_type,
25730				 /*complain=*/tf_error);
25731      if (decl != error_mark_node)
25732	decl = TYPE_NAME (decl);
25733    }
25734
25735  decl = strip_using_decl (decl);
25736
25737  /* Check to see that it is really the name of a class.  */
25738  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
25739      && identifier_p (TREE_OPERAND (decl, 0))
25740      && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25741    /* Situations like this:
25742
25743	 template <typename T> struct A {
25744	   typename T::template X<int>::I i;
25745	 };
25746
25747       are problematic.  Is `T::template X<int>' a class-name?  The
25748       standard does not seem to be definitive, but there is no other
25749       valid interpretation of the following `::'.  Therefore, those
25750       names are considered class-names.  */
25751    {
25752      decl = make_typename_type (scope, decl, tag_type, tf_error);
25753      if (decl != error_mark_node)
25754	decl = TYPE_NAME (decl);
25755    }
25756  else if (TREE_CODE (decl) != TYPE_DECL
25757	   || TREE_TYPE (decl) == error_mark_node
25758	   || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
25759		|| (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
25760	   /* In Objective-C 2.0, a classname followed by '.' starts a
25761	      dot-syntax expression, and it's not a type-name.  */
25762	   || (c_dialect_objc ()
25763	       && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
25764	       && objc_is_class_name (decl)))
25765    decl = error_mark_node;
25766
25767  if (decl == error_mark_node)
25768    cp_parser_error (parser, "expected class-name");
25769  else if (identifier && !parser->scope)
25770    maybe_note_name_used_in_class (identifier, decl);
25771
25772  return decl;
25773}
25774
25775/* Make sure that any member-function parameters are in scope.
25776   For instance, a function's noexcept-specifier can use the function's
25777   parameters:
25778
25779   struct S {
25780     void fn (int p) noexcept(noexcept(p));
25781   };
25782
25783   so we need to make sure name lookup can find them.  This is used
25784   when we delay parsing of the noexcept-specifier.  */
25785
25786static void
25787inject_parm_decls (tree decl)
25788{
25789  begin_scope (sk_function_parms, decl);
25790  tree args = DECL_ARGUMENTS (decl);
25791
25792  do_push_parm_decls (decl, args, /*nonparms=*/NULL);
25793
25794  if (args && is_this_parameter (args))
25795    {
25796      gcc_checking_assert (current_class_ptr == NULL_TREE);
25797      current_class_ref = cp_build_fold_indirect_ref (args);
25798      current_class_ptr = args;
25799    }
25800}
25801
25802/* Undo the effects of inject_parm_decls.  */
25803
25804static void
25805pop_injected_parms (void)
25806{
25807  pop_bindings_and_leave_scope ();
25808  current_class_ptr = current_class_ref = NULL_TREE;
25809}
25810
25811/* Parse a class-specifier.
25812
25813   class-specifier:
25814     class-head { member-specification [opt] }
25815
25816   Returns the TREE_TYPE representing the class.  */
25817
25818static tree
25819cp_parser_class_specifier_1 (cp_parser* parser)
25820{
25821  tree type;
25822  tree attributes = NULL_TREE;
25823  bool nested_name_specifier_p;
25824  unsigned saved_num_template_parameter_lists;
25825  bool saved_in_function_body;
25826  unsigned char in_statement;
25827  bool in_switch_statement_p;
25828  bool saved_in_unbraced_linkage_specification_p;
25829  tree old_scope = NULL_TREE;
25830  tree scope = NULL_TREE;
25831  cp_token *closing_brace;
25832
25833  push_deferring_access_checks (dk_no_deferred);
25834
25835  /* Parse the class-head.  */
25836  type = cp_parser_class_head (parser,
25837			       &nested_name_specifier_p);
25838  /* If the class-head was a semantic disaster, skip the entire body
25839     of the class.  */
25840  if (!type)
25841    {
25842      cp_parser_skip_to_end_of_block_or_statement (parser);
25843      pop_deferring_access_checks ();
25844      return error_mark_node;
25845    }
25846
25847  /* Look for the `{'.  */
25848  matching_braces braces;
25849  if (!braces.require_open (parser))
25850    {
25851      pop_deferring_access_checks ();
25852      return error_mark_node;
25853    }
25854
25855  cp_ensure_no_omp_declare_simd (parser);
25856  cp_ensure_no_oacc_routine (parser);
25857
25858  /* Issue an error message if type-definitions are forbidden here.  */
25859  bool type_definition_ok_p = cp_parser_check_type_definition (parser);
25860  /* Remember that we are defining one more class.  */
25861  ++parser->num_classes_being_defined;
25862  /* Inside the class, surrounding template-parameter-lists do not
25863     apply.  */
25864  saved_num_template_parameter_lists
25865    = parser->num_template_parameter_lists;
25866  parser->num_template_parameter_lists = 0;
25867  /* We are not in a function body.  */
25868  saved_in_function_body = parser->in_function_body;
25869  parser->in_function_body = false;
25870  /* Or in a loop.  */
25871  in_statement = parser->in_statement;
25872  parser->in_statement = 0;
25873  /* Or in a switch.  */
25874  in_switch_statement_p = parser->in_switch_statement_p;
25875  parser->in_switch_statement_p = false;
25876  /* We are not immediately inside an extern "lang" block.  */
25877  saved_in_unbraced_linkage_specification_p
25878    = parser->in_unbraced_linkage_specification_p;
25879  parser->in_unbraced_linkage_specification_p = false;
25880  /* 'this' from an enclosing non-static member function is unavailable.  */
25881  tree saved_ccp = current_class_ptr;
25882  tree saved_ccr = current_class_ref;
25883  current_class_ptr = NULL_TREE;
25884  current_class_ref = NULL_TREE;
25885
25886  /* Start the class.  */
25887  if (nested_name_specifier_p)
25888    {
25889      scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
25890      /* SCOPE must be a scope nested inside current scope.  */
25891      if (is_nested_namespace (current_namespace,
25892			       decl_namespace_context (scope)))
25893	old_scope = push_inner_scope (scope);
25894      else
25895	nested_name_specifier_p = false;
25896    }
25897  type = begin_class_definition (type);
25898
25899  if (type == error_mark_node)
25900    /* If the type is erroneous, skip the entire body of the class.  */
25901    cp_parser_skip_to_closing_brace (parser);
25902  else
25903    /* Parse the member-specification.  */
25904    cp_parser_member_specification_opt (parser);
25905
25906  /* Look for the trailing `}'.  */
25907  closing_brace = braces.require_close (parser);
25908  /* Look for trailing attributes to apply to this class.  */
25909  if (cp_parser_allow_gnu_extensions_p (parser))
25910    attributes = cp_parser_gnu_attributes_opt (parser);
25911  if (type != error_mark_node)
25912    type = finish_struct (type, attributes);
25913  if (nested_name_specifier_p)
25914    pop_inner_scope (old_scope, scope);
25915
25916  /* We've finished a type definition.  Check for the common syntax
25917     error of forgetting a semicolon after the definition.  We need to
25918     be careful, as we can't just check for not-a-semicolon and be done
25919     with it; the user might have typed:
25920
25921     class X { } c = ...;
25922     class X { } *p = ...;
25923
25924     and so forth.  Instead, enumerate all the possible tokens that
25925     might follow this production; if we don't see one of them, then
25926     complain and silently insert the semicolon.  */
25927  {
25928    cp_token *token = cp_lexer_peek_token (parser->lexer);
25929    bool want_semicolon = true;
25930
25931    if (cp_next_tokens_can_be_std_attribute_p (parser))
25932      /* Don't try to parse c++11 attributes here.  As per the
25933	 grammar, that should be a task for
25934	 cp_parser_decl_specifier_seq.  */
25935      want_semicolon = false;
25936
25937    switch (token->type)
25938      {
25939      case CPP_NAME:
25940      case CPP_SEMICOLON:
25941      case CPP_MULT:
25942      case CPP_AND:
25943      case CPP_OPEN_PAREN:
25944      case CPP_CLOSE_PAREN:
25945      case CPP_COMMA:
25946      case CPP_SCOPE:
25947        want_semicolon = false;
25948        break;
25949
25950        /* While it's legal for type qualifiers and storage class
25951           specifiers to follow type definitions in the grammar, only
25952           compiler testsuites contain code like that.  Assume that if
25953           we see such code, then what we're really seeing is a case
25954           like:
25955
25956           class X { }
25957           const <type> var = ...;
25958
25959           or
25960
25961           class Y { }
25962           static <type> func (...) ...
25963
25964           i.e. the qualifier or specifier applies to the next
25965           declaration.  To do so, however, we need to look ahead one
25966           more token to see if *that* token is a type specifier.
25967
25968	   This code could be improved to handle:
25969
25970	   class Z { }
25971	   static const <type> var = ...;  */
25972      case CPP_KEYWORD:
25973	if (keyword_is_decl_specifier (token->keyword))
25974	  {
25975	    cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
25976
25977	    /* Handling user-defined types here would be nice, but very
25978	       tricky.  */
25979	    want_semicolon
25980	      = (lookahead->type == CPP_KEYWORD
25981		 && keyword_begins_type_specifier (lookahead->keyword));
25982	  }
25983	break;
25984      default:
25985	break;
25986      }
25987
25988    /* If we don't have a type, then something is very wrong and we
25989       shouldn't try to do anything clever.  Likewise for not seeing the
25990       closing brace.  */
25991    if (closing_brace && TYPE_P (type) && want_semicolon)
25992      {
25993	/* Locate the closing brace.  */
25994	cp_token_position prev
25995	  = cp_lexer_previous_token_position (parser->lexer);
25996	cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
25997	location_t loc = prev_token->location;
25998
25999	/* We want to suggest insertion of a ';' immediately *after* the
26000	   closing brace, so, if we can, offset the location by 1 column.  */
26001	location_t next_loc = loc;
26002	if (!linemap_location_from_macro_expansion_p (line_table, loc))
26003	  next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26004
26005	rich_location richloc (line_table, next_loc);
26006
26007	/* If we successfully offset the location, suggest the fix-it.  */
26008	if (next_loc != loc)
26009	  richloc.add_fixit_insert_before (next_loc, ";");
26010
26011	if (CLASSTYPE_DECLARED_CLASS (type))
26012	  error_at (&richloc,
26013		    "expected %<;%> after class definition");
26014	else if (TREE_CODE (type) == RECORD_TYPE)
26015	  error_at (&richloc,
26016		    "expected %<;%> after struct definition");
26017	else if (TREE_CODE (type) == UNION_TYPE)
26018	  error_at (&richloc,
26019		    "expected %<;%> after union definition");
26020	else
26021	  gcc_unreachable ();
26022
26023	/* Unget one token and smash it to look as though we encountered
26024	   a semicolon in the input stream.  */
26025	cp_lexer_set_token_position (parser->lexer, prev);
26026	token = cp_lexer_peek_token (parser->lexer);
26027	token->type = CPP_SEMICOLON;
26028	token->keyword = RID_MAX;
26029      }
26030  }
26031
26032  /* If this class is not itself within the scope of another class,
26033     then we need to parse the bodies of all of the queued function
26034     definitions.  Note that the queued functions defined in a class
26035     are not always processed immediately following the
26036     class-specifier for that class.  Consider:
26037
26038       struct A {
26039	 struct B { void f() { sizeof (A); } };
26040       };
26041
26042     If `f' were processed before the processing of `A' were
26043     completed, there would be no way to compute the size of `A'.
26044     Note that the nesting we are interested in here is lexical --
26045     not the semantic nesting given by TYPE_CONTEXT.  In particular,
26046     for:
26047
26048       struct A { struct B; };
26049       struct A::B { void f() { } };
26050
26051     there is no need to delay the parsing of `A::B::f'.  */
26052  if (--parser->num_classes_being_defined == 0)
26053    {
26054      tree decl;
26055      tree class_type = NULL_TREE;
26056      tree pushed_scope = NULL_TREE;
26057      unsigned ix;
26058      cp_default_arg_entry *e;
26059
26060      if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26061	{
26062	  /* Skip default arguments, NSDMIs, etc, in order to improve
26063	     error recovery (c++/71169, c++/71832).  */
26064	  vec_safe_truncate (unparsed_funs_with_default_args, 0);
26065	  vec_safe_truncate (unparsed_nsdmis, 0);
26066	  vec_safe_truncate (unparsed_funs_with_definitions, 0);
26067	}
26068
26069      /* In a first pass, parse default arguments to the functions.
26070	 Then, in a second pass, parse the bodies of the functions.
26071	 This two-phased approach handles cases like:
26072
26073	    struct S {
26074	      void f() { g(); }
26075	      void g(int i = 3);
26076	    };
26077
26078	 */
26079      FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26080	{
26081	  decl = e->decl;
26082	  /* If there are default arguments that have not yet been processed,
26083	     take care of them now.  */
26084	  if (class_type != e->class_type)
26085	    {
26086	      if (pushed_scope)
26087		pop_scope (pushed_scope);
26088	      class_type = e->class_type;
26089	      pushed_scope = push_scope (class_type);
26090	    }
26091	  /* Make sure that any template parameters are in scope.  */
26092	  maybe_begin_member_template_processing (decl);
26093	  /* Parse the default argument expressions.  */
26094	  cp_parser_late_parsing_default_args (parser, decl);
26095	  /* Remove any template parameters from the symbol table.  */
26096	  maybe_end_member_template_processing ();
26097	}
26098      vec_safe_truncate (unparsed_funs_with_default_args, 0);
26099
26100      /* If there are noexcept-specifiers that have not yet been processed,
26101	 take care of them now.  Do this before processing NSDMIs as they
26102	 may depend on noexcept-specifiers already having been processed.  */
26103      FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26104	{
26105	  tree ctx = DECL_CONTEXT (decl);
26106	  if (class_type != ctx)
26107	    {
26108	      if (pushed_scope)
26109		pop_scope (pushed_scope);
26110	      class_type = ctx;
26111	      pushed_scope = push_scope (class_type);
26112	    }
26113
26114	  tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26115	  def_parse = TREE_PURPOSE (def_parse);
26116
26117	  /* Make sure that any template parameters are in scope.  */
26118	  maybe_begin_member_template_processing (decl);
26119
26120	  /* Make sure that any member-function parameters are in scope.
26121	     This function doesn't expect ccp to be set.  */
26122	  current_class_ptr = current_class_ref = NULL_TREE;
26123	  inject_parm_decls (decl);
26124
26125	  /* 'this' is not allowed in static member functions.  */
26126	  unsigned char local_variables_forbidden_p
26127	    = parser->local_variables_forbidden_p;
26128	  if (DECL_THIS_STATIC (decl))
26129	    parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26130
26131	  /* Now we can parse the noexcept-specifier.  */
26132	  tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26133
26134	  if (spec == error_mark_node)
26135	    spec = NULL_TREE;
26136
26137	  /* Update the fn's type directly -- it might have escaped
26138	     beyond this decl :(  */
26139	  fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26140	  /* Update any instantiations we've already created.  We must
26141	     keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26142	     so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26143	     in the pattern.  */
26144	  for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26145	    DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26146	      = spec ? TREE_PURPOSE (spec) : error_mark_node;
26147
26148	  /* Restore the state of local_variables_forbidden_p.  */
26149	  parser->local_variables_forbidden_p = local_variables_forbidden_p;
26150
26151	  /* The finish_struct call above performed various override checking,
26152	     but it skipped unparsed noexcept-specifier operands.  Now that we
26153	     have resolved them, check again.  */
26154	  noexcept_override_late_checks (type, decl);
26155
26156	  /* Remove any member-function parameters from the symbol table.  */
26157	  pop_injected_parms ();
26158
26159	  /* Remove any template parameters from the symbol table.  */
26160	  maybe_end_member_template_processing ();
26161	}
26162      vec_safe_truncate (unparsed_noexcepts, 0);
26163
26164      /* Now parse any NSDMIs.  */
26165      FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26166	{
26167	  if (class_type != DECL_CONTEXT (decl))
26168	    {
26169	      if (pushed_scope)
26170		pop_scope (pushed_scope);
26171	      class_type = DECL_CONTEXT (decl);
26172	      pushed_scope = push_scope (class_type);
26173	    }
26174	  inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26175	  cp_parser_late_parsing_nsdmi (parser, decl);
26176	}
26177      vec_safe_truncate (unparsed_nsdmis, 0);
26178      current_class_ptr = NULL_TREE;
26179      current_class_ref = NULL_TREE;
26180      if (pushed_scope)
26181	pop_scope (pushed_scope);
26182
26183      /* Now parse the body of the functions.  */
26184      if (flag_openmp)
26185	{
26186	  /* OpenMP UDRs need to be parsed before all other functions.  */
26187	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26188	    if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26189	      cp_parser_late_parsing_for_member (parser, decl);
26190	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26191	    if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26192	      cp_parser_late_parsing_for_member (parser, decl);
26193	}
26194      else
26195	FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26196	  cp_parser_late_parsing_for_member (parser, decl);
26197      vec_safe_truncate (unparsed_funs_with_definitions, 0);
26198    }
26199
26200  /* Put back any saved access checks.  */
26201  pop_deferring_access_checks ();
26202
26203  /* Restore saved state.  */
26204  parser->in_switch_statement_p = in_switch_statement_p;
26205  parser->in_statement = in_statement;
26206  parser->in_function_body = saved_in_function_body;
26207  parser->num_template_parameter_lists
26208    = saved_num_template_parameter_lists;
26209  parser->in_unbraced_linkage_specification_p
26210    = saved_in_unbraced_linkage_specification_p;
26211  current_class_ptr = saved_ccp;
26212  current_class_ref = saved_ccr;
26213
26214  return type;
26215}
26216
26217static tree
26218cp_parser_class_specifier (cp_parser* parser)
26219{
26220  tree ret;
26221  timevar_push (TV_PARSE_STRUCT);
26222  ret = cp_parser_class_specifier_1 (parser);
26223  timevar_pop (TV_PARSE_STRUCT);
26224  return ret;
26225}
26226
26227/* Parse a class-head.
26228
26229   class-head:
26230     class-key identifier [opt] base-clause [opt]
26231     class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26232     class-key nested-name-specifier [opt] template-id
26233       base-clause [opt]
26234
26235   class-virt-specifier:
26236     final
26237
26238   GNU Extensions:
26239     class-key attributes identifier [opt] base-clause [opt]
26240     class-key attributes nested-name-specifier identifier base-clause [opt]
26241     class-key attributes nested-name-specifier [opt] template-id
26242       base-clause [opt]
26243
26244   Upon return BASES is initialized to the list of base classes (or
26245   NULL, if there are none) in the same form returned by
26246   cp_parser_base_clause.
26247
26248   Returns the TYPE of the indicated class.  Sets
26249   *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26250   involving a nested-name-specifier was used, and FALSE otherwise.
26251
26252   Returns error_mark_node if this is not a class-head.
26253
26254   Returns NULL_TREE if the class-head is syntactically valid, but
26255   semantically invalid in a way that means we should skip the entire
26256   body of the class.  */
26257
26258static tree
26259cp_parser_class_head (cp_parser* parser,
26260		      bool* nested_name_specifier_p)
26261{
26262  tree nested_name_specifier;
26263  enum tag_types class_key;
26264  tree id = NULL_TREE;
26265  tree type = NULL_TREE;
26266  tree attributes;
26267  tree bases;
26268  cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26269  bool template_id_p = false;
26270  bool qualified_p = false;
26271  bool invalid_nested_name_p = false;
26272  bool invalid_explicit_specialization_p = false;
26273  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26274  tree pushed_scope = NULL_TREE;
26275  unsigned num_templates;
26276  cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26277  /* Assume no nested-name-specifier will be present.  */
26278  *nested_name_specifier_p = false;
26279  /* Assume no template parameter lists will be used in defining the
26280     type.  */
26281  num_templates = 0;
26282  parser->colon_corrects_to_scope_p = false;
26283
26284  /* Look for the class-key.  */
26285  class_key = cp_parser_class_key (parser);
26286  if (class_key == none_type)
26287    return error_mark_node;
26288
26289  location_t class_head_start_location = input_location;
26290
26291  /* Parse the attributes.  */
26292  attributes = cp_parser_attributes_opt (parser);
26293
26294  /* If the next token is `::', that is invalid -- but sometimes
26295     people do try to write:
26296
26297       struct ::S {};
26298
26299     Handle this gracefully by accepting the extra qualifier, and then
26300     issuing an error about it later if this really is a
26301     class-head.  If it turns out just to be an elaborated type
26302     specifier, remain silent.  */
26303  if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26304    qualified_p = true;
26305
26306  push_deferring_access_checks (dk_no_check);
26307
26308  /* Determine the name of the class.  Begin by looking for an
26309     optional nested-name-specifier.  */
26310  nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26311  nested_name_specifier
26312    = cp_parser_nested_name_specifier_opt (parser,
26313					   /*typename_keyword_p=*/false,
26314					   /*check_dependency_p=*/false,
26315					   /*type_p=*/true,
26316					   /*is_declaration=*/false);
26317  /* If there was a nested-name-specifier, then there *must* be an
26318     identifier.  */
26319
26320  cp_token *bad_template_keyword = NULL;
26321
26322  if (nested_name_specifier)
26323    {
26324      type_start_token = cp_lexer_peek_token (parser->lexer);
26325      /* Although the grammar says `identifier', it really means
26326	 `class-name' or `template-name'.  You are only allowed to
26327	 define a class that has already been declared with this
26328	 syntax.
26329
26330	 The proposed resolution for Core Issue 180 says that wherever
26331	 you see `class T::X' you should treat `X' as a type-name.
26332
26333	 It is OK to define an inaccessible class; for example:
26334
26335	   class A { class B; };
26336	   class A::B {};
26337
26338	 We do not know if we will see a class-name, or a
26339	 template-name.  We look for a class-name first, in case the
26340	 class-name is a template-id; if we looked for the
26341	 template-name first we would stop after the template-name.  */
26342      cp_parser_parse_tentatively (parser);
26343      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26344	bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26345      type = cp_parser_class_name (parser,
26346				   /*typename_keyword_p=*/false,
26347				   /*template_keyword_p=*/false,
26348				   class_type,
26349				   /*check_dependency_p=*/false,
26350				   /*class_head_p=*/true,
26351				   /*is_declaration=*/false);
26352      /* If that didn't work, ignore the nested-name-specifier.  */
26353      if (!cp_parser_parse_definitely (parser))
26354	{
26355	  invalid_nested_name_p = true;
26356	  type_start_token = cp_lexer_peek_token (parser->lexer);
26357	  id = cp_parser_identifier (parser);
26358	  if (id == error_mark_node)
26359	    id = NULL_TREE;
26360	}
26361      /* If we could not find a corresponding TYPE, treat this
26362	 declaration like an unqualified declaration.  */
26363      if (type == error_mark_node)
26364	nested_name_specifier = NULL_TREE;
26365      /* Otherwise, count the number of templates used in TYPE and its
26366	 containing scopes.  */
26367      else
26368	num_templates = num_template_headers_for_class (TREE_TYPE (type));
26369    }
26370  /* Otherwise, the identifier is optional.  */
26371  else
26372    {
26373      /* We don't know whether what comes next is a template-id,
26374	 an identifier, or nothing at all.  */
26375      cp_parser_parse_tentatively (parser);
26376      /* Check for a template-id.  */
26377      type_start_token = cp_lexer_peek_token (parser->lexer);
26378      id = cp_parser_template_id (parser,
26379				  /*template_keyword_p=*/false,
26380				  /*check_dependency_p=*/true,
26381				  class_key,
26382				  /*is_declaration=*/true);
26383      /* If that didn't work, it could still be an identifier.  */
26384      if (!cp_parser_parse_definitely (parser))
26385	{
26386	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26387	    {
26388	      type_start_token = cp_lexer_peek_token (parser->lexer);
26389	      id = cp_parser_identifier (parser);
26390	    }
26391	  else
26392	    id = NULL_TREE;
26393	}
26394      else
26395	{
26396	  template_id_p = true;
26397	  ++num_templates;
26398	}
26399    }
26400
26401  pop_deferring_access_checks ();
26402
26403  if (id)
26404    {
26405      cp_parser_check_for_invalid_template_id (parser, id,
26406					       class_key,
26407                                               type_start_token->location);
26408    }
26409  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26410
26411  /* If it's not a `:' or a `{' then we can't really be looking at a
26412     class-head, since a class-head only appears as part of a
26413     class-specifier.  We have to detect this situation before calling
26414     xref_tag, since that has irreversible side-effects.  */
26415  if (!cp_parser_next_token_starts_class_definition_p (parser))
26416    {
26417      cp_parser_error (parser, "expected %<{%> or %<:%>");
26418      type = error_mark_node;
26419      goto out;
26420    }
26421
26422  /* At this point, we're going ahead with the class-specifier, even
26423     if some other problem occurs.  */
26424  cp_parser_commit_to_tentative_parse (parser);
26425  if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26426    {
26427      cp_parser_error (parser,
26428                       "cannot specify %<override%> for a class");
26429      type = error_mark_node;
26430      goto out;
26431    }
26432  /* Issue the error about the overly-qualified name now.  */
26433  if (qualified_p)
26434    {
26435      cp_parser_error (parser,
26436		       "global qualification of class name is invalid");
26437      type = error_mark_node;
26438      goto out;
26439    }
26440  else if (invalid_nested_name_p)
26441    {
26442      cp_parser_error (parser,
26443		       "qualified name does not name a class");
26444      type = error_mark_node;
26445      goto out;
26446    }
26447  else if (nested_name_specifier)
26448    {
26449      tree scope;
26450
26451      if (bad_template_keyword)
26452	/* [temp.names]: in a qualified-id formed by a class-head-name, the
26453	   keyword template shall not appear at the top level.  */
26454	pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26455		 "keyword %<template%> not allowed in class-head-name");
26456
26457      /* Reject typedef-names in class heads.  */
26458      if (!DECL_IMPLICIT_TYPEDEF_P (type))
26459	{
26460	  error_at (type_start_token->location,
26461		    "invalid class name in declaration of %qD",
26462		    type);
26463	  type = NULL_TREE;
26464	  goto done;
26465	}
26466
26467      /* Figure out in what scope the declaration is being placed.  */
26468      scope = current_scope ();
26469      /* If that scope does not contain the scope in which the
26470	 class was originally declared, the program is invalid.  */
26471      if (scope && !is_ancestor (scope, nested_name_specifier))
26472	{
26473	  if (at_namespace_scope_p ())
26474	    error_at (type_start_token->location,
26475		      "declaration of %qD in namespace %qD which does not "
26476		      "enclose %qD",
26477		      type, scope, nested_name_specifier);
26478	  else
26479	    error_at (type_start_token->location,
26480		      "declaration of %qD in %qD which does not enclose %qD",
26481		      type, scope, nested_name_specifier);
26482	  type = NULL_TREE;
26483	  goto done;
26484	}
26485      /* [dcl.meaning]
26486
26487	 A declarator-id shall not be qualified except for the
26488	 definition of a ... nested class outside of its class
26489	 ... [or] the definition or explicit instantiation of a
26490	 class member of a namespace outside of its namespace.  */
26491      if (scope == nested_name_specifier)
26492	permerror (nested_name_specifier_token_start->location,
26493		   "extra qualification not allowed");
26494    }
26495  /* An explicit-specialization must be preceded by "template <>".  If
26496     it is not, try to recover gracefully.  */
26497  if (at_namespace_scope_p ()
26498      && parser->num_template_parameter_lists == 0
26499      && !processing_template_parmlist
26500      && template_id_p)
26501    {
26502      /* Build a location of this form:
26503           struct typename <ARGS>
26504           ^~~~~~~~~~~~~~~~~~~~~~
26505         with caret==start at the start token, and
26506         finishing at the end of the type.  */
26507      location_t reported_loc
26508        = make_location (class_head_start_location,
26509                         class_head_start_location,
26510                         get_finish (type_start_token->location));
26511      rich_location richloc (line_table, reported_loc);
26512      richloc.add_fixit_insert_before (class_head_start_location,
26513                                       "template <> ");
26514      error_at (&richloc,
26515		"an explicit specialization must be preceded by"
26516		" %<template <>%>");
26517      invalid_explicit_specialization_p = true;
26518      /* Take the same action that would have been taken by
26519	 cp_parser_explicit_specialization.  */
26520      ++parser->num_template_parameter_lists;
26521      begin_specialization ();
26522    }
26523  /* There must be no "return" statements between this point and the
26524     end of this function; set "type "to the correct return value and
26525     use "goto done;" to return.  */
26526  /* Make sure that the right number of template parameters were
26527     present.  */
26528  if (!cp_parser_check_template_parameters (parser, num_templates,
26529					    template_id_p,
26530					    type_start_token->location,
26531					    /*declarator=*/NULL))
26532    {
26533      /* If something went wrong, there is no point in even trying to
26534	 process the class-definition.  */
26535      type = NULL_TREE;
26536      goto done;
26537    }
26538
26539  /* Look up the type.  */
26540  if (template_id_p)
26541    {
26542      if (TREE_CODE (id) == TEMPLATE_ID_EXPR
26543	  && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
26544	      || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
26545	{
26546	  error_at (type_start_token->location,
26547		    "function template %qD redeclared as a class template", id);
26548	  type = error_mark_node;
26549	}
26550      else
26551	{
26552	  type = TREE_TYPE (id);
26553	  type = maybe_process_partial_specialization (type);
26554
26555	  /* Check the scope while we still know whether or not we had a
26556	     nested-name-specifier.  */
26557	  if (type != error_mark_node)
26558	    check_unqualified_spec_or_inst (type, type_start_token->location);
26559	}
26560      if (nested_name_specifier)
26561	pushed_scope = push_scope (nested_name_specifier);
26562    }
26563  else if (nested_name_specifier)
26564    {
26565      type = TREE_TYPE (type);
26566
26567      /* Given:
26568
26569	    template <typename T> struct S { struct T };
26570	    template <typename T> struct S<T>::T { };
26571
26572	 we will get a TYPENAME_TYPE when processing the definition of
26573	 `S::T'.  We need to resolve it to the actual type before we
26574	 try to define it.  */
26575      if (TREE_CODE (type) == TYPENAME_TYPE)
26576	{
26577	  type = resolve_typename_type (type, /*only_current_p=*/false);
26578	  if (TREE_CODE (type) == TYPENAME_TYPE)
26579	    {
26580	      cp_parser_error (parser, "could not resolve typename type");
26581	      type = error_mark_node;
26582	    }
26583	}
26584
26585      type = maybe_process_partial_specialization (type);
26586      if (type == error_mark_node)
26587	{
26588	  type = NULL_TREE;
26589	  goto done;
26590	}
26591
26592      /* Enter the scope indicated by the nested-name-specifier.  */
26593      pushed_scope = push_scope (nested_name_specifier);
26594      /* Get the canonical version of this type.  */
26595      type = TYPE_MAIN_DECL (type);
26596      /* Call push_template_decl if it seems like we should be defining a
26597	 template either from the template headers or the type we're
26598	 defining, so that we diagnose both extra and missing headers.  */
26599      if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26600	   || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
26601	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
26602	{
26603	  type = push_template_decl (type);
26604	  if (type == error_mark_node)
26605	    {
26606	      type = NULL_TREE;
26607	      goto done;
26608	    }
26609	}
26610
26611      type = TREE_TYPE (type);
26612      *nested_name_specifier_p = true;
26613    }
26614  else      /* The name is not a nested name.  */
26615    {
26616      /* If the class was unnamed, create a dummy name.  */
26617      if (!id)
26618	id = make_anon_name ();
26619      TAG_how how = (parser->in_type_id_in_expr_p
26620		     ? TAG_how::INNERMOST_NON_CLASS
26621		     : TAG_how::CURRENT_ONLY);
26622      type = xref_tag (class_key, id, how,
26623		       parser->num_template_parameter_lists);
26624    }
26625
26626  /* Diagnose class/struct/union mismatches.  */
26627  cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
26628			     true, true);
26629
26630  /* Indicate whether this class was declared as a `class' or as a
26631     `struct'.  */
26632  if (TREE_CODE (type) == RECORD_TYPE)
26633    CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
26634
26635  /* If this type was already complete, and we see another definition,
26636     that's an error.  Likewise if the type is already being defined:
26637     this can happen, eg, when it's defined from within an expression
26638     (c++/84605).  */
26639  if (type != error_mark_node
26640      && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
26641    {
26642      error_at (type_start_token->location, "redefinition of %q#T",
26643		type);
26644      inform (location_of (type), "previous definition of %q#T",
26645	      type);
26646      type = NULL_TREE;
26647      goto done;
26648    }
26649  else if (type == error_mark_node)
26650    type = NULL_TREE;
26651
26652  if (type)
26653    {
26654      if (current_lambda_expr ()
26655	  && uses_parameter_packs (attributes))
26656	{
26657	  /* In a lambda this should work, but doesn't currently.  */
26658	  sorry ("unexpanded parameter pack in local class in lambda");
26659	  attributes = NULL_TREE;
26660	}
26661
26662      /* Apply attributes now, before any use of the class as a template
26663	 argument in its base list.  */
26664      cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
26665      fixup_attribute_variants (type);
26666    }
26667
26668  /* Associate constraints with the type.  */
26669  if (flag_concepts)
26670    type = associate_classtype_constraints (type);
26671
26672  /* We will have entered the scope containing the class; the names of
26673     base classes should be looked up in that context.  For example:
26674
26675       struct A { struct B {}; struct C; };
26676       struct A::C : B {};
26677
26678     is valid.  */
26679
26680  /* Get the list of base-classes, if there is one.  Defer access checking
26681     until the entire list has been seen, as per [class.access.general].  */
26682  push_deferring_access_checks (dk_deferred);
26683  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26684    {
26685      if (type)
26686	pushclass (type);
26687      bases = cp_parser_base_clause (parser);
26688      if (type)
26689	popclass ();
26690    }
26691  else
26692    bases = NULL_TREE;
26693
26694  /* If we're really defining a class, process the base classes.
26695     If they're invalid, fail.  */
26696  if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26697    xref_basetypes (type, bases);
26698
26699  /* Now that all bases have been seen and attached to the class, check
26700     accessibility of the types named in the base-clause.  This must be
26701     done relative to the class scope, so that we accept e.g.
26702
26703       struct A { protected: struct B {}; };
26704       struct C : A::B, A {}; // OK: A::B is accessible via base A
26705
26706     as per [class.access.general].  */
26707  if (type)
26708    pushclass (type);
26709  pop_to_parent_deferring_access_checks ();
26710  if (type)
26711    popclass ();
26712
26713 done:
26714  /* Leave the scope given by the nested-name-specifier.  We will
26715     enter the class scope itself while processing the members.  */
26716  if (pushed_scope)
26717    pop_scope (pushed_scope);
26718
26719  if (invalid_explicit_specialization_p)
26720    {
26721      end_specialization ();
26722      --parser->num_template_parameter_lists;
26723    }
26724
26725  if (type)
26726    DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
26727  if (type && (virt_specifiers & VIRT_SPEC_FINAL))
26728    CLASSTYPE_FINAL (type) = 1;
26729 out:
26730  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26731  return type;
26732}
26733
26734/* Parse a class-key.
26735
26736   class-key:
26737     class
26738     struct
26739     union
26740
26741   Returns the kind of class-key specified, or none_type to indicate
26742   error.  */
26743
26744static enum tag_types
26745cp_parser_class_key (cp_parser* parser)
26746{
26747  cp_token *token;
26748  enum tag_types tag_type;
26749
26750  /* Look for the class-key.  */
26751  token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
26752  if (!token)
26753    return none_type;
26754
26755  /* Check to see if the TOKEN is a class-key.  */
26756  tag_type = cp_parser_token_is_class_key (token);
26757  if (!tag_type)
26758    cp_parser_error (parser, "expected class-key");
26759  return tag_type;
26760}
26761
26762/* Parse a type-parameter-key.
26763
26764   type-parameter-key:
26765     class
26766     typename
26767 */
26768
26769static void
26770cp_parser_type_parameter_key (cp_parser* parser)
26771{
26772  /* Look for the type-parameter-key.  */
26773  enum tag_types tag_type = none_type;
26774  cp_token *token = cp_lexer_peek_token (parser->lexer);
26775  if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
26776    {
26777      cp_lexer_consume_token (parser->lexer);
26778      if (pedantic && tag_type == typename_type
26779	  && cxx_dialect < cxx17)
26780	/* typename is not allowed in a template template parameter
26781	   by the standard until C++17.  */
26782	pedwarn (token->location, OPT_Wc__17_extensions,
26783		 "ISO C++ forbids typename key in template template parameter;"
26784		 " use %<-std=c++17%> or %<-std=gnu++17%>");
26785    }
26786  else
26787    cp_parser_error (parser, "expected %<class%> or %<typename%>");
26788
26789  return;
26790}
26791
26792/* Parse an (optional) member-specification.
26793
26794   member-specification:
26795     member-declaration member-specification [opt]
26796     access-specifier : member-specification [opt]  */
26797
26798static void
26799cp_parser_member_specification_opt (cp_parser* parser)
26800{
26801  while (true)
26802    {
26803      cp_token *token;
26804      enum rid keyword;
26805
26806      /* Peek at the next token.  */
26807      token = cp_lexer_peek_token (parser->lexer);
26808      /* If it's a `}', or EOF then we've seen all the members.  */
26809      if (token->type == CPP_CLOSE_BRACE
26810	  || token->type == CPP_EOF
26811	  || token->type == CPP_PRAGMA_EOL)
26812	break;
26813
26814      /* See if this token is a keyword.  */
26815      keyword = token->keyword;
26816      switch (keyword)
26817	{
26818	case RID_PUBLIC:
26819	case RID_PROTECTED:
26820	case RID_PRIVATE:
26821	  /* Consume the access-specifier.  */
26822	  cp_lexer_consume_token (parser->lexer);
26823	  /* Remember which access-specifier is active.  */
26824	  current_access_specifier = token->u.value;
26825	  /* Look for the `:'.  */
26826	  cp_parser_require (parser, CPP_COLON, RT_COLON);
26827	  break;
26828
26829	default:
26830	  /* Accept #pragmas at class scope.  */
26831	  if (token->type == CPP_PRAGMA)
26832	    {
26833	      cp_parser_pragma (parser, pragma_member, NULL);
26834	      break;
26835	    }
26836
26837	  /* Otherwise, the next construction must be a
26838	     member-declaration.  */
26839	  cp_parser_member_declaration (parser);
26840	}
26841    }
26842}
26843
26844/* Parse a member-declaration.
26845
26846   member-declaration:
26847     decl-specifier-seq [opt] member-declarator-list [opt] ;
26848     function-definition ; [opt]
26849     :: [opt] nested-name-specifier template [opt] unqualified-id ;
26850     using-declaration
26851     template-declaration
26852     alias-declaration
26853
26854   member-declarator-list:
26855     member-declarator
26856     member-declarator-list , member-declarator
26857
26858   member-declarator:
26859     declarator pure-specifier [opt]
26860     declarator constant-initializer [opt]
26861     identifier [opt] : constant-expression
26862
26863   GNU Extensions:
26864
26865   member-declaration:
26866     __extension__ member-declaration
26867
26868   member-declarator:
26869     declarator attributes [opt] pure-specifier [opt]
26870     declarator attributes [opt] constant-initializer [opt]
26871     identifier [opt] attributes [opt] : constant-expression
26872
26873   C++0x Extensions:
26874
26875   member-declaration:
26876     static_assert-declaration  */
26877
26878static void
26879cp_parser_member_declaration (cp_parser* parser)
26880{
26881  cp_decl_specifier_seq decl_specifiers;
26882  tree prefix_attributes;
26883  tree decl;
26884  int declares_class_or_enum;
26885  bool friend_p;
26886  cp_token *token = NULL;
26887  cp_token *decl_spec_token_start = NULL;
26888  cp_token *initializer_token_start = NULL;
26889  int saved_pedantic;
26890  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26891
26892  /* Check for the `__extension__' keyword.  */
26893  if (cp_parser_extension_opt (parser, &saved_pedantic))
26894    {
26895      /* Recurse.  */
26896      cp_parser_member_declaration (parser);
26897      /* Restore the old value of the PEDANTIC flag.  */
26898      pedantic = saved_pedantic;
26899
26900      return;
26901    }
26902
26903  /* Check for a template-declaration.  */
26904  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26905    {
26906      /* An explicit specialization here is an error condition, and we
26907	 expect the specialization handler to detect and report this.  */
26908      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
26909	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
26910	cp_parser_explicit_specialization (parser);
26911      else
26912	cp_parser_template_declaration (parser, /*member_p=*/true);
26913
26914      return;
26915    }
26916  /* Check for a template introduction.  */
26917  else if (cp_parser_template_declaration_after_export (parser, true))
26918    return;
26919
26920  /* Check for a using-declaration.  */
26921  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26922    {
26923      if (cxx_dialect < cxx11)
26924	/* Parse the using-declaration.  */
26925	cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
26926      else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
26927	cp_parser_using_enum (parser);
26928      else
26929	{
26930	  tree decl;
26931	  bool alias_decl_expected;
26932	  cp_parser_parse_tentatively (parser);
26933	  decl = cp_parser_alias_declaration (parser);
26934	  /* Note that if we actually see the '=' token after the
26935	     identifier, cp_parser_alias_declaration commits the
26936	     tentative parse.  In that case, we really expect an
26937	     alias-declaration.  Otherwise, we expect a using
26938	     declaration.  */
26939	  alias_decl_expected =
26940	    !cp_parser_uncommitted_to_tentative_parse_p (parser);
26941	  cp_parser_parse_definitely (parser);
26942
26943	  if (alias_decl_expected)
26944	    finish_member_declaration (decl);
26945	  else
26946	    cp_parser_using_declaration (parser,
26947					 /*access_declaration_p=*/false);
26948	}
26949      return;
26950    }
26951
26952  /* Check for @defs.  */
26953  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
26954    {
26955      tree ivar, member;
26956      tree ivar_chains = cp_parser_objc_defs_expression (parser);
26957      ivar = ivar_chains;
26958      while (ivar)
26959	{
26960	  member = ivar;
26961	  ivar = TREE_CHAIN (member);
26962	  TREE_CHAIN (member) = NULL_TREE;
26963	  finish_member_declaration (member);
26964	}
26965      return;
26966    }
26967
26968  /* If the next token is `static_assert' we have a static assertion.  */
26969  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
26970    {
26971      cp_parser_static_assert (parser, /*member_p=*/true);
26972      return;
26973    }
26974
26975  parser->colon_corrects_to_scope_p = false;
26976
26977  cp_omp_declare_simd_data odsd;
26978  if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
26979    goto out;
26980
26981  /* Parse the decl-specifier-seq.  */
26982  decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26983  cp_parser_decl_specifier_seq (parser,
26984				(CP_PARSER_FLAGS_OPTIONAL
26985				 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
26986				&decl_specifiers,
26987				&declares_class_or_enum);
26988
26989  if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
26990    cp_parser_handle_directive_omp_attributes (parser,
26991					       &decl_specifiers.attributes,
26992					       &odsd, true);
26993
26994  /* Check for an invalid type-name.  */
26995  if (!decl_specifiers.any_type_specifiers_p
26996      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26997    goto out;
26998  /* If there is no declarator, then the decl-specifier-seq should
26999     specify a type.  */
27000  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27001    {
27002      /* If there was no decl-specifier-seq, and the next token is a
27003	 `;', then we have something like:
27004
27005	   struct S { ; };
27006
27007	 [class.mem]
27008
27009	 Each member-declaration shall declare at least one member
27010	 name of the class.  */
27011      if (!decl_specifiers.any_specifiers_p)
27012	{
27013	  cp_token *token = cp_lexer_peek_token (parser->lexer);
27014	  if (!in_system_header_at (token->location))
27015	    {
27016	      gcc_rich_location richloc (token->location);
27017	      richloc.add_fixit_remove ();
27018	      pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27019	    }
27020	}
27021      else
27022	{
27023	  /* See if this declaration is a friend.  */
27024	  friend_p = cp_parser_friend_p (&decl_specifiers);
27025	  /* If there were decl-specifiers, check to see if there was
27026	     a class-declaration.  */
27027	  tree type = check_tag_decl (&decl_specifiers,
27028				      /*explicit_type_instantiation_p=*/false);
27029	  /* Nested classes have already been added to the class, but
27030	     a `friend' needs to be explicitly registered.  */
27031	  if (friend_p)
27032	    {
27033	      /* If the `friend' keyword was present, the friend must
27034		 be introduced with a class-key.  */
27035	       if (!declares_class_or_enum && cxx_dialect < cxx11)
27036		 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27037			  "in C++03 a class-key must be used "
27038			  "when declaring a friend");
27039	       /* In this case:
27040
27041		    template <typename T> struct A {
27042		      friend struct A<T>::B;
27043		    };
27044
27045		  A<T>::B will be represented by a TYPENAME_TYPE, and
27046		  therefore not recognized by check_tag_decl.  */
27047	       if (!type)
27048		 {
27049		   type = decl_specifiers.type;
27050		   if (type && TREE_CODE (type) == TYPE_DECL)
27051		     type = TREE_TYPE (type);
27052		 }
27053	       /* Warn if an attribute cannot appear here, as per
27054		  [dcl.attr.grammar]/5.  But not when declares_class_or_enum:
27055		  we ignore attributes in elaborated-type-specifiers.  */
27056	       if (!declares_class_or_enum
27057		   && cxx11_attribute_p (decl_specifiers.attributes))
27058		 {
27059		   decl_specifiers.attributes = NULL_TREE;
27060		   if (warning_at (decl_spec_token_start->location,
27061				   OPT_Wattributes, "attribute ignored"))
27062		     inform (decl_spec_token_start->location, "an attribute "
27063			     "that appertains to a friend declaration that "
27064			     "is not a definition is ignored");
27065		 }
27066	       if (!type || !TYPE_P (type))
27067		 error_at (decl_spec_token_start->location,
27068			   "friend declaration does not name a class or "
27069			   "function");
27070	       else
27071		 make_friend_class (current_class_type, type,
27072				    /*complain=*/true);
27073	    }
27074	  /* If there is no TYPE, an error message will already have
27075	     been issued.  */
27076	  else if (!type || type == error_mark_node)
27077	    ;
27078	  /* An anonymous aggregate has to be handled specially; such
27079	     a declaration really declares a data member (with a
27080	     particular type), as opposed to a nested class.  */
27081	  else if (ANON_AGGR_TYPE_P (type))
27082	    {
27083	      /* C++11 9.5/6.  */
27084	      if (decl_specifiers.storage_class != sc_none)
27085		error_at (decl_spec_token_start->location,
27086			  "a storage class on an anonymous aggregate "
27087			  "in class scope is not allowed");
27088
27089	      /* Remove constructors and such from TYPE, now that we
27090		 know it is an anonymous aggregate.  */
27091	      fixup_anonymous_aggr (type);
27092	      /* And make the corresponding data member.  */
27093	      decl = build_decl (decl_spec_token_start->location,
27094				 FIELD_DECL, NULL_TREE, type);
27095	      /* Add it to the class.  */
27096	      finish_member_declaration (decl);
27097	    }
27098	  else
27099	    cp_parser_check_access_in_redeclaration
27100					      (TYPE_NAME (type),
27101					       decl_spec_token_start->location);
27102	}
27103    }
27104  else
27105    {
27106      bool assume_semicolon = false;
27107
27108      /* Clear attributes from the decl_specifiers but keep them
27109	 around as prefix attributes that apply them to the entity
27110	 being declared.  */
27111      prefix_attributes = decl_specifiers.attributes;
27112      decl_specifiers.attributes = NULL_TREE;
27113      if (parser->omp_declare_simd
27114	  && (parser->omp_declare_simd->attribs[0]
27115	      == &decl_specifiers.attributes))
27116	parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27117
27118      /* See if these declarations will be friends.  */
27119      friend_p = cp_parser_friend_p (&decl_specifiers);
27120
27121      /* Keep going until we hit the `;' at the end of the
27122	 declaration.  */
27123      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27124	{
27125	  tree attributes = NULL_TREE;
27126	  tree first_attribute;
27127	  tree initializer;
27128	  bool named_bitfld = false;
27129
27130	  /* Peek at the next token.  */
27131	  token = cp_lexer_peek_token (parser->lexer);
27132
27133	  /* The following code wants to know early if it is a bit-field
27134	     or some other declaration.  Attributes can appear before
27135	     the `:' token.  Skip over them without consuming any tokens
27136	     to peek if they are followed by `:'.  */
27137	  if (cp_next_tokens_can_be_attribute_p (parser)
27138	      || (token->type == CPP_NAME
27139		  && cp_nth_tokens_can_be_attribute_p (parser, 2)
27140		  && (named_bitfld = true)))
27141	    {
27142	      size_t n
27143		= cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27144	      token = cp_lexer_peek_nth_token (parser->lexer, n);
27145	    }
27146
27147	  /* Check for a bitfield declaration.  */
27148	  if (token->type == CPP_COLON
27149	      || (token->type == CPP_NAME
27150		  && token == cp_lexer_peek_token (parser->lexer)
27151		  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27152		  && (named_bitfld = true)))
27153	    {
27154	      tree identifier;
27155	      tree width;
27156	      tree late_attributes = NULL_TREE;
27157	      location_t id_location
27158		= cp_lexer_peek_token (parser->lexer)->location;
27159
27160	      if (named_bitfld)
27161		identifier = cp_parser_identifier (parser);
27162	      else
27163		identifier = NULL_TREE;
27164
27165	      /* Look for attributes that apply to the bitfield.  */
27166	      attributes = cp_parser_attributes_opt (parser);
27167
27168	      /* Consume the `:' token.  */
27169	      cp_lexer_consume_token (parser->lexer);
27170
27171	      /* Get the width of the bitfield.  */
27172	      width = cp_parser_constant_expression (parser, false, NULL,
27173						     cxx_dialect >= cxx11);
27174
27175	      /* In C++20 and as extension for C++11 and above we allow
27176		 default member initializers for bit-fields.  */
27177	      initializer = NULL_TREE;
27178	      if (cxx_dialect >= cxx11
27179		  && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27180		      || cp_lexer_next_token_is (parser->lexer,
27181						 CPP_OPEN_BRACE)))
27182		{
27183		  location_t loc
27184		    = cp_lexer_peek_token (parser->lexer)->location;
27185		  if (cxx_dialect < cxx20
27186		      && identifier != NULL_TREE)
27187		    pedwarn (loc, OPT_Wc__20_extensions,
27188			     "default member initializers for bit-fields "
27189			     "only available with %<-std=c++20%> or "
27190			     "%<-std=gnu++20%>");
27191
27192		  initializer = cp_parser_save_nsdmi (parser);
27193		  if (identifier == NULL_TREE)
27194		    {
27195		      error_at (loc, "default member initializer for "
27196				     "unnamed bit-field");
27197		      initializer = NULL_TREE;
27198		    }
27199		}
27200	      else
27201		{
27202		  /* Look for attributes that apply to the bitfield after
27203		     the `:' token and width.  This is where GCC used to
27204		     parse attributes in the past, pedwarn if there is
27205		     a std attribute.  */
27206		  if (cp_next_tokens_can_be_std_attribute_p (parser))
27207		    pedwarn (input_location, OPT_Wpedantic,
27208			     "ISO C++ allows bit-field attributes only "
27209			     "before the %<:%> token");
27210
27211		  late_attributes = cp_parser_attributes_opt (parser);
27212		}
27213
27214	      attributes = attr_chainon (attributes, late_attributes);
27215
27216	      /* Remember which attributes are prefix attributes and
27217		 which are not.  */
27218	      first_attribute = attributes;
27219	      /* Combine the attributes.  */
27220	      attributes = attr_chainon (prefix_attributes, attributes);
27221
27222	      /* Create the bitfield declaration.  */
27223	      decl = grokbitfield (identifier
27224				   ? make_id_declarator (NULL_TREE,
27225							 identifier,
27226							 sfk_none,
27227							 id_location)
27228				   : NULL,
27229				   &decl_specifiers,
27230				   width, initializer,
27231				   attributes);
27232	    }
27233	  else
27234	    {
27235	      cp_declarator *declarator;
27236	      tree asm_specification;
27237	      int ctor_dtor_or_conv_p;
27238	      bool static_p = (decl_specifiers.storage_class == sc_static);
27239	      cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27240	      /* We can't delay parsing for friends,
27241		 alias-declarations, and typedefs, even though the
27242		 standard seems to require it.  */
27243	      if (!friend_p
27244		  && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27245		flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27246
27247	      /* Parse the declarator.  */
27248	      declarator
27249		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27250					flags,
27251					&ctor_dtor_or_conv_p,
27252					/*parenthesized_p=*/NULL,
27253					/*member_p=*/true,
27254					friend_p, static_p);
27255
27256	      /* If something went wrong parsing the declarator, make sure
27257		 that we at least consume some tokens.  */
27258	      if (declarator == cp_error_declarator)
27259		{
27260		  /* Skip to the end of the statement.  */
27261		  cp_parser_skip_to_end_of_statement (parser);
27262		  /* If the next token is not a semicolon, that is
27263		     probably because we just skipped over the body of
27264		     a function.  So, we consume a semicolon if
27265		     present, but do not issue an error message if it
27266		     is not present.  */
27267		  if (cp_lexer_next_token_is (parser->lexer,
27268					      CPP_SEMICOLON))
27269		    cp_lexer_consume_token (parser->lexer);
27270		  goto out;
27271		}
27272
27273	      /* Handle class-scope non-template C++17 deduction guides.  */
27274	      cp_parser_maybe_adjust_declarator_for_dguide (parser,
27275							    &decl_specifiers,
27276							    declarator,
27277							    &ctor_dtor_or_conv_p);
27278
27279	      if (declares_class_or_enum & 2)
27280		cp_parser_check_for_definition_in_return_type
27281					    (declarator, decl_specifiers.type,
27282					     decl_specifiers.locations[ds_type_spec]);
27283
27284	      /* Look for an asm-specification.  */
27285	      asm_specification = cp_parser_asm_specification_opt (parser);
27286	      /* Look for attributes that apply to the declaration.  */
27287	      attributes = cp_parser_attributes_opt (parser);
27288	      /* Remember which attributes are prefix attributes and
27289		 which are not.  */
27290	      first_attribute = attributes;
27291	      /* Combine the attributes.  */
27292	      attributes = attr_chainon (prefix_attributes, attributes);
27293
27294	      /* If it's an `=', then we have a constant-initializer or a
27295		 pure-specifier.  It is not correct to parse the
27296		 initializer before registering the member declaration
27297		 since the member declaration should be in scope while
27298		 its initializer is processed.  However, the rest of the
27299		 front end does not yet provide an interface that allows
27300		 us to handle this correctly.  */
27301	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27302		{
27303		  /* In [class.mem]:
27304
27305		     A pure-specifier shall be used only in the declaration of
27306		     a virtual function.
27307
27308		     A member-declarator can contain a constant-initializer
27309		     only if it declares a static member of integral or
27310		     enumeration type.
27311
27312		     Therefore, if the DECLARATOR is for a function, we look
27313		     for a pure-specifier; otherwise, we look for a
27314		     constant-initializer.  When we call `grokfield', it will
27315		     perform more stringent semantics checks.  */
27316		  initializer_token_start = cp_lexer_peek_token (parser->lexer);
27317		  declarator->init_loc = initializer_token_start->location;
27318		  if (function_declarator_p (declarator)
27319		      || (decl_specifiers.type
27320			  && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27321			  && declarator->kind == cdk_id
27322			  && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27323			      == FUNCTION_TYPE)))
27324		    initializer = cp_parser_pure_specifier (parser);
27325		  else if (decl_specifiers.storage_class != sc_static)
27326		    initializer = cp_parser_save_nsdmi (parser);
27327		  else if (cxx_dialect >= cxx11)
27328		    {
27329		      bool nonconst;
27330		      /* Don't require a constant rvalue in C++11, since we
27331			 might want a reference constant.  We'll enforce
27332		         constancy later.  */
27333		      cp_lexer_consume_token (parser->lexer);
27334		      /* Parse the initializer.  */
27335		      initializer = cp_parser_initializer_clause (parser,
27336								  &nonconst);
27337		    }
27338		  else
27339		    /* Parse the initializer.  */
27340		    initializer = cp_parser_constant_initializer (parser);
27341		}
27342	      else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27343		       && !function_declarator_p (declarator))
27344		{
27345		  bool x;
27346		  declarator->init_loc
27347		    = cp_lexer_peek_token (parser->lexer)->location;
27348		  if (decl_specifiers.storage_class != sc_static)
27349		    initializer = cp_parser_save_nsdmi (parser);
27350		  else
27351		    initializer = cp_parser_initializer (parser, &x, &x);
27352		}
27353	      /* Detect invalid bit-field cases such as
27354
27355		   int *p : 4;
27356		   int &&r : 3;
27357
27358		 and similar.  */
27359	      else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27360		       /* If there were no type specifiers, it was a
27361			  constructor.  */
27362		       && decl_specifiers.any_type_specifiers_p)
27363		{
27364		  /* This is called for a decent diagnostic only.  */
27365		  tree d = grokdeclarator (declarator, &decl_specifiers,
27366					   BITFIELD, /*initialized=*/false,
27367					   &attributes);
27368		  if (!error_operand_p (d))
27369		    error_at (DECL_SOURCE_LOCATION (d),
27370			      "bit-field %qD has non-integral type %qT",
27371			      d, TREE_TYPE (d));
27372		  cp_parser_skip_to_end_of_statement (parser);
27373		  /* Avoid "extra ;" pedwarns.  */
27374		  if (cp_lexer_next_token_is (parser->lexer,
27375					      CPP_SEMICOLON))
27376		    cp_lexer_consume_token (parser->lexer);
27377		  goto out;
27378		}
27379	      /* Otherwise, there is no initializer.  */
27380	      else
27381		initializer = NULL_TREE;
27382
27383	      /* See if we are probably looking at a function
27384		 definition.  We are certainly not looking at a
27385		 member-declarator.  Calling `grokfield' has
27386		 side-effects, so we must not do it unless we are sure
27387		 that we are looking at a member-declarator.  */
27388	      if (cp_parser_token_starts_function_definition_p
27389		  (cp_lexer_peek_token (parser->lexer)))
27390		{
27391		  /* The grammar does not allow a pure-specifier to be
27392		     used when a member function is defined.  (It is
27393		     possible that this fact is an oversight in the
27394		     standard, since a pure function may be defined
27395		     outside of the class-specifier.  */
27396		  if (initializer && initializer_token_start)
27397		    error_at (initializer_token_start->location,
27398			      "pure-specifier on function-definition");
27399		  decl = cp_parser_save_member_function_body (parser,
27400							      &decl_specifiers,
27401							      declarator,
27402							      attributes);
27403		  if (parser->fully_implicit_function_template_p)
27404		    decl = finish_fully_implicit_template (parser, decl);
27405		  /* If the member was not a friend, declare it here.  */
27406		  if (!friend_p)
27407		    finish_member_declaration (decl);
27408		  /* Peek at the next token.  */
27409		  token = cp_lexer_peek_token (parser->lexer);
27410		  /* If the next token is a semicolon, consume it.  */
27411		  if (token->type == CPP_SEMICOLON)
27412		    {
27413		      location_t semicolon_loc
27414			= cp_lexer_consume_token (parser->lexer)->location;
27415		      gcc_rich_location richloc (semicolon_loc);
27416		      richloc.add_fixit_remove ();
27417		      warning_at (&richloc, OPT_Wextra_semi,
27418				  "extra %<;%> after in-class "
27419				  "function definition");
27420		    }
27421		  goto out;
27422		}
27423	      else
27424		if (declarator->kind == cdk_function)
27425		  declarator->id_loc = token->location;
27426	      /* Create the declaration.  */
27427	      decl = grokfield (declarator, &decl_specifiers,
27428				initializer, /*init_const_expr_p=*/true,
27429				asm_specification, attributes);
27430	      if (parser->fully_implicit_function_template_p)
27431		{
27432		  if (friend_p)
27433		    finish_fully_implicit_template (parser, 0);
27434		  else
27435		    decl = finish_fully_implicit_template (parser, decl);
27436		}
27437	    }
27438
27439	  cp_finalize_omp_declare_simd (parser, decl);
27440	  cp_finalize_oacc_routine (parser, decl, false);
27441
27442	  /* Reset PREFIX_ATTRIBUTES.  */
27443	  if (attributes != error_mark_node)
27444	    {
27445	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
27446		attributes = TREE_CHAIN (attributes);
27447	      if (attributes)
27448		TREE_CHAIN (attributes) = NULL_TREE;
27449	    }
27450
27451	  /* If there is any qualification still in effect, clear it
27452	     now; we will be starting fresh with the next declarator.  */
27453	  parser->scope = NULL_TREE;
27454	  parser->qualifying_scope = NULL_TREE;
27455	  parser->object_scope = NULL_TREE;
27456	  /* If it's a `,', then there are more declarators.  */
27457	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27458	    {
27459	      cp_lexer_consume_token (parser->lexer);
27460	      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27461		{
27462		  cp_token *token = cp_lexer_previous_token (parser->lexer);
27463		  gcc_rich_location richloc (token->location);
27464		  richloc.add_fixit_remove ();
27465		  error_at (&richloc, "stray %<,%> at end of "
27466			    "member declaration");
27467		}
27468	    }
27469	  /* If the next token isn't a `;', then we have a parse error.  */
27470	  else if (cp_lexer_next_token_is_not (parser->lexer,
27471					       CPP_SEMICOLON))
27472	    {
27473	      /* The next token might be a ways away from where the
27474		 actual semicolon is missing.  Find the previous token
27475		 and use that for our error position.  */
27476	      cp_token *token = cp_lexer_previous_token (parser->lexer);
27477	      gcc_rich_location richloc (token->location);
27478	      richloc.add_fixit_insert_after (";");
27479	      error_at (&richloc, "expected %<;%> at end of "
27480			"member declaration");
27481
27482	      /* Assume that the user meant to provide a semicolon.  If
27483		 we were to cp_parser_skip_to_end_of_statement, we might
27484		 skip to a semicolon inside a member function definition
27485		 and issue nonsensical error messages.  */
27486	      assume_semicolon = true;
27487	    }
27488
27489	  if (decl)
27490	    {
27491	      /* Add DECL to the list of members.  */
27492	      if (!friend_p
27493		  /* Explicitly include, eg, NSDMIs, for better error
27494		     recovery (c++/58650).  */
27495		  || !DECL_DECLARES_FUNCTION_P (decl))
27496		finish_member_declaration (decl);
27497
27498	      if (DECL_DECLARES_FUNCTION_P (decl))
27499		cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
27500	      else if (TREE_CODE (decl) == FIELD_DECL
27501		       && DECL_INITIAL (decl))
27502		/* Add DECL to the queue of NSDMI to be parsed later.  */
27503		vec_safe_push (unparsed_nsdmis, decl);
27504	    }
27505
27506	  if (assume_semicolon)
27507	    goto out;
27508	}
27509    }
27510
27511  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27512 out:
27513  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27514  cp_finalize_omp_declare_simd (parser, &odsd);
27515}
27516
27517/* Parse a pure-specifier.
27518
27519   pure-specifier:
27520     = 0
27521
27522   Returns INTEGER_ZERO_NODE if a pure specifier is found.
27523   Otherwise, ERROR_MARK_NODE is returned.  */
27524
27525static tree
27526cp_parser_pure_specifier (cp_parser* parser)
27527{
27528  cp_token *token;
27529
27530  /* Look for the `=' token.  */
27531  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27532    return error_mark_node;
27533  /* Look for the `0' token.  */
27534  token = cp_lexer_peek_token (parser->lexer);
27535
27536  if (token->type == CPP_EOF
27537      || token->type == CPP_PRAGMA_EOL)
27538    return error_mark_node;
27539
27540  cp_lexer_consume_token (parser->lexer);
27541
27542  /* Accept = default or = delete in c++0x mode.  */
27543  if (token->keyword == RID_DEFAULT
27544      || token->keyword == RID_DELETE)
27545    {
27546      maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
27547      return token->u.value;
27548    }
27549
27550  /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
27551  if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
27552    {
27553      cp_parser_error (parser,
27554		       "invalid pure specifier (only %<= 0%> is allowed)");
27555      cp_parser_skip_to_end_of_statement (parser);
27556      return error_mark_node;
27557    }
27558  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27559    {
27560      error_at (token->location, "templates may not be %<virtual%>");
27561      return error_mark_node;
27562    }
27563
27564  return integer_zero_node;
27565}
27566
27567/* Parse a constant-initializer.
27568
27569   constant-initializer:
27570     = constant-expression
27571
27572   Returns a representation of the constant-expression.  */
27573
27574static tree
27575cp_parser_constant_initializer (cp_parser* parser)
27576{
27577  /* Look for the `=' token.  */
27578  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27579    return error_mark_node;
27580
27581  /* It is invalid to write:
27582
27583       struct S { static const int i = { 7 }; };
27584
27585     */
27586  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27587    {
27588      cp_parser_error (parser,
27589		       "a brace-enclosed initializer is not allowed here");
27590      /* Consume the opening brace.  */
27591      matching_braces braces;
27592      braces.consume_open (parser);
27593      /* Skip the initializer.  */
27594      cp_parser_skip_to_closing_brace (parser);
27595      /* Look for the trailing `}'.  */
27596      braces.require_close (parser);
27597
27598      return error_mark_node;
27599    }
27600
27601  return cp_parser_constant_expression (parser);
27602}
27603
27604/* Derived classes [gram.class.derived] */
27605
27606/* Parse a base-clause.
27607
27608   base-clause:
27609     : base-specifier-list
27610
27611   base-specifier-list:
27612     base-specifier ... [opt]
27613     base-specifier-list , base-specifier ... [opt]
27614
27615   Returns a TREE_LIST representing the base-classes, in the order in
27616   which they were declared.  The representation of each node is as
27617   described by cp_parser_base_specifier.
27618
27619   In the case that no bases are specified, this function will return
27620   NULL_TREE, not ERROR_MARK_NODE.  */
27621
27622static tree
27623cp_parser_base_clause (cp_parser* parser)
27624{
27625  tree bases = NULL_TREE;
27626
27627  /* Look for the `:' that begins the list.  */
27628  cp_parser_require (parser, CPP_COLON, RT_COLON);
27629
27630  /* Scan the base-specifier-list.  */
27631  while (true)
27632    {
27633      cp_token *token;
27634      tree base;
27635      bool pack_expansion_p = false;
27636
27637      /* Look for the base-specifier.  */
27638      base = cp_parser_base_specifier (parser);
27639      /* Look for the (optional) ellipsis. */
27640      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27641        {
27642          /* Consume the `...'. */
27643          cp_lexer_consume_token (parser->lexer);
27644
27645          pack_expansion_p = true;
27646        }
27647
27648      /* Add BASE to the front of the list.  */
27649      if (base && base != error_mark_node)
27650	{
27651          if (pack_expansion_p)
27652            /* Make this a pack expansion type. */
27653            TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
27654
27655          if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
27656            {
27657              TREE_CHAIN (base) = bases;
27658              bases = base;
27659            }
27660	}
27661      /* Peek at the next token.  */
27662      token = cp_lexer_peek_token (parser->lexer);
27663      /* If it's not a comma, then the list is complete.  */
27664      if (token->type != CPP_COMMA)
27665	break;
27666      /* Consume the `,'.  */
27667      cp_lexer_consume_token (parser->lexer);
27668    }
27669
27670  /* PARSER->SCOPE may still be non-NULL at this point, if the last
27671     base class had a qualified name.  However, the next name that
27672     appears is certainly not qualified.  */
27673  parser->scope = NULL_TREE;
27674  parser->qualifying_scope = NULL_TREE;
27675  parser->object_scope = NULL_TREE;
27676
27677  return nreverse (bases);
27678}
27679
27680/* Parse a base-specifier.
27681
27682   base-specifier:
27683     :: [opt] nested-name-specifier [opt] class-name
27684     virtual access-specifier [opt] :: [opt] nested-name-specifier
27685       [opt] class-name
27686     access-specifier virtual [opt] :: [opt] nested-name-specifier
27687       [opt] class-name
27688
27689   Returns a TREE_LIST.  The TREE_PURPOSE will be one of
27690   ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
27691   indicate the specifiers provided.  The TREE_VALUE will be a TYPE
27692   (or the ERROR_MARK_NODE) indicating the type that was specified.  */
27693
27694static tree
27695cp_parser_base_specifier (cp_parser* parser)
27696{
27697  cp_token *token;
27698  bool done = false;
27699  bool virtual_p = false;
27700  bool duplicate_virtual_error_issued_p = false;
27701  bool duplicate_access_error_issued_p = false;
27702  bool class_scope_p, template_p;
27703  tree access = access_default_node;
27704  tree type;
27705
27706  /* Process the optional `virtual' and `access-specifier'.  */
27707  while (!done)
27708    {
27709      /* Peek at the next token.  */
27710      token = cp_lexer_peek_token (parser->lexer);
27711      /* Process `virtual'.  */
27712      switch (token->keyword)
27713	{
27714	case RID_VIRTUAL:
27715	  /* If `virtual' appears more than once, issue an error.  */
27716	  if (virtual_p && !duplicate_virtual_error_issued_p)
27717	    {
27718	      cp_parser_error (parser,
27719			       "%<virtual%> specified more than once in base-specifier");
27720	      duplicate_virtual_error_issued_p = true;
27721	    }
27722
27723	  virtual_p = true;
27724
27725	  /* Consume the `virtual' token.  */
27726	  cp_lexer_consume_token (parser->lexer);
27727
27728	  break;
27729
27730	case RID_PUBLIC:
27731	case RID_PROTECTED:
27732	case RID_PRIVATE:
27733	  /* If more than one access specifier appears, issue an
27734	     error.  */
27735	  if (access != access_default_node
27736	      && !duplicate_access_error_issued_p)
27737	    {
27738	      cp_parser_error (parser,
27739			       "more than one access specifier in base-specifier");
27740	      duplicate_access_error_issued_p = true;
27741	    }
27742
27743	  access = ridpointers[(int) token->keyword];
27744
27745	  /* Consume the access-specifier.  */
27746	  cp_lexer_consume_token (parser->lexer);
27747
27748	  break;
27749
27750	default:
27751	  done = true;
27752	  break;
27753	}
27754    }
27755  /* It is not uncommon to see programs mechanically, erroneously, use
27756     the 'typename' keyword to denote (dependent) qualified types
27757     as base classes.  */
27758  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27759    {
27760      token = cp_lexer_peek_token (parser->lexer);
27761      if (!processing_template_decl)
27762	error_at (token->location,
27763		  "keyword %<typename%> not allowed outside of templates");
27764      else
27765	error_at (token->location,
27766		  "keyword %<typename%> not allowed in this context "
27767		  "(the base class is implicitly a type)");
27768      cp_lexer_consume_token (parser->lexer);
27769    }
27770
27771  /* Look for the optional `::' operator.  */
27772  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
27773  /* Look for the nested-name-specifier.  The simplest way to
27774     implement:
27775
27776       [temp.res]
27777
27778       The keyword `typename' is not permitted in a base-specifier or
27779       mem-initializer; in these contexts a qualified name that
27780       depends on a template-parameter is implicitly assumed to be a
27781       type name.
27782
27783     is to pretend that we have seen the `typename' keyword at this
27784     point.  */
27785  cp_parser_nested_name_specifier_opt (parser,
27786				       /*typename_keyword_p=*/true,
27787				       /*check_dependency_p=*/true,
27788				       /*type_p=*/true,
27789				       /*is_declaration=*/true);
27790  /* If the base class is given by a qualified name, assume that names
27791     we see are type names or templates, as appropriate.  */
27792  class_scope_p = (parser->scope && TYPE_P (parser->scope));
27793  template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
27794
27795  if (!parser->scope
27796      && cp_lexer_next_token_is_decltype (parser->lexer))
27797    /* DR 950 allows decltype as a base-specifier.  */
27798    type = cp_parser_decltype (parser);
27799  else
27800    {
27801      /* Otherwise, look for the class-name.  */
27802      type = cp_parser_class_name (parser,
27803				   class_scope_p,
27804				   template_p,
27805				   typename_type,
27806				   /*check_dependency_p=*/true,
27807				   /*class_head_p=*/false,
27808				   /*is_declaration=*/true);
27809      type = TREE_TYPE (type);
27810    }
27811
27812  if (type == error_mark_node)
27813    return error_mark_node;
27814
27815  return finish_base_specifier (type, access, virtual_p);
27816}
27817
27818/* Exception handling [gram.exception] */
27819
27820/* Save the tokens that make up the noexcept-specifier for a member-function.
27821   Returns a DEFERRED_PARSE.  */
27822
27823static tree
27824cp_parser_save_noexcept (cp_parser *parser)
27825{
27826  cp_token *first = parser->lexer->next_token;
27827  /* We want everything up to, including, the final ')'.  */
27828  cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
27829  cp_token *last = parser->lexer->next_token;
27830
27831  /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
27832     to carry the information we will need.  */
27833  tree expr = make_node (DEFERRED_PARSE);
27834  /* Save away the noexcept-specifier; we will process it when the
27835     class is complete.  */
27836  DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
27837  DEFPARSE_INSTANTIATIONS (expr) = nullptr;
27838  expr = build_tree_list (expr, NULL_TREE);
27839  return expr;
27840}
27841
27842/* Used for late processing of noexcept-specifiers of member-functions.
27843   DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
27844   we saved for later; parse it now.  DECL is the declaration of the
27845   member function.  */
27846
27847static tree
27848cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
27849{
27850  /* Make sure we've gotten something that hasn't been parsed yet.  */
27851  gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
27852
27853  push_unparsed_function_queues (parser);
27854
27855  /* Push the saved tokens for the noexcept-specifier onto the parser's
27856     lexer stack.  */
27857  cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
27858  cp_parser_push_lexer_for_tokens (parser, tokens);
27859
27860  /* Parse the cached noexcept-specifier.  */
27861  tree parsed_arg
27862    = cp_parser_noexcept_specification_opt (parser,
27863					    CP_PARSER_FLAGS_NONE,
27864					    /*require_constexpr=*/true,
27865					    /*consumed_expr=*/NULL,
27866					    /*return_cond=*/false);
27867
27868  /* Revert to the main lexer.  */
27869  cp_parser_pop_lexer (parser);
27870
27871  /* Restore the queue.  */
27872  pop_unparsed_function_queues (parser);
27873
27874  /* And we're done.  */
27875  return parsed_arg;
27876}
27877
27878/* Perform late checking of overriding function with respect to their
27879   noexcept-specifiers.  TYPE is the class and FNDECL is the function
27880   that potentially overrides some virtual function with the same
27881   signature.  */
27882
27883static void
27884noexcept_override_late_checks (tree type, tree fndecl)
27885{
27886  tree binfo = TYPE_BINFO (type);
27887  tree base_binfo;
27888
27889  if (DECL_STATIC_FUNCTION_P (fndecl))
27890    return;
27891
27892  for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
27893    {
27894      tree basetype = BINFO_TYPE (base_binfo);
27895
27896      if (!TYPE_POLYMORPHIC_P (basetype))
27897	continue;
27898
27899      tree fn = look_for_overrides_here (basetype, fndecl);
27900      if (fn)
27901	maybe_check_overriding_exception_spec (fndecl, fn);
27902    }
27903}
27904
27905/* Parse an (optional) noexcept-specification.
27906
27907   noexcept-specification:
27908     noexcept ( constant-expression ) [opt]
27909
27910   If no noexcept-specification is present, returns NULL_TREE.
27911   Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
27912   expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
27913   there are no parentheses.  CONSUMED_EXPR will be set accordingly.
27914   Otherwise, returns a noexcept specification unless RETURN_COND is true,
27915   in which case a boolean condition is returned instead.  The parser flags
27916   FLAGS is used to control parsing.  QUALS are qualifiers indicating whether
27917   the (member) function is `const'.  */
27918
27919static tree
27920cp_parser_noexcept_specification_opt (cp_parser* parser,
27921				      cp_parser_flags flags,
27922				      bool require_constexpr,
27923				      bool* consumed_expr,
27924				      bool return_cond)
27925{
27926  cp_token *token;
27927  const char *saved_message;
27928
27929  /* Peek at the next token.  */
27930  token = cp_lexer_peek_token (parser->lexer);
27931
27932  /* Is it a noexcept-specification?  */
27933  if (cp_parser_is_keyword (token, RID_NOEXCEPT))
27934    {
27935      tree expr;
27936
27937      /* [class.mem]/6 says that a noexcept-specifer (within the
27938	 member-specification of the class) is a complete-class context of
27939	 a class.  So, if the noexcept-specifier has the optional expression,
27940	 just save the tokens, and reparse this after we're done with the
27941	 class.  */
27942
27943      if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
27944	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
27945	  /* No need to delay parsing for a number literal or true/false.  */
27946	  && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
27947		|| cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27948	       && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
27949	  && at_class_scope_p ()
27950	  && TYPE_BEING_DEFINED (current_class_type)
27951	  && !LAMBDA_TYPE_P (current_class_type))
27952	return cp_parser_save_noexcept (parser);
27953
27954      cp_lexer_consume_token (parser->lexer);
27955
27956      if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27957	{
27958	  matching_parens parens;
27959	  parens.consume_open (parser);
27960
27961	  if (require_constexpr)
27962	    {
27963	      /* Types may not be defined in an exception-specification.  */
27964	      saved_message = parser->type_definition_forbidden_message;
27965	      parser->type_definition_forbidden_message
27966	      = G_("types may not be defined in an exception-specification");
27967
27968	      bool non_constant_p;
27969	      expr
27970		= cp_parser_constant_expression (parser,
27971						 /*allow_non_constant=*/true,
27972						 &non_constant_p);
27973	      if (non_constant_p
27974		  && !require_potential_rvalue_constant_expression (expr))
27975		{
27976		  expr = NULL_TREE;
27977		  return_cond = true;
27978		}
27979
27980	      /* Restore the saved message.  */
27981	      parser->type_definition_forbidden_message = saved_message;
27982	    }
27983	  else
27984	    {
27985	      expr = cp_parser_expression (parser);
27986	      *consumed_expr = true;
27987	    }
27988
27989	  parens.require_close (parser);
27990	}
27991      else
27992	{
27993	  expr = boolean_true_node;
27994	  if (!require_constexpr)
27995	    *consumed_expr = false;
27996	}
27997
27998      /* We cannot build a noexcept-spec right away because this will check
27999	 that expr is a constexpr.  */
28000      if (!return_cond)
28001	return build_noexcept_spec (expr, tf_warning_or_error);
28002      else
28003	return expr;
28004    }
28005  else
28006    return NULL_TREE;
28007}
28008
28009/* Parse an (optional) exception-specification.
28010
28011   exception-specification:
28012     throw ( type-id-list [opt] )
28013
28014   Returns a TREE_LIST representing the exception-specification.  The
28015   TREE_VALUE of each node is a type.  The parser flags FLAGS is used to
28016   control parsing.  QUALS are qualifiers indicating whether the (member)
28017   function is `const'.  */
28018
28019static tree
28020cp_parser_exception_specification_opt (cp_parser* parser,
28021				       cp_parser_flags flags)
28022{
28023  cp_token *token;
28024  tree type_id_list;
28025  const char *saved_message;
28026
28027  /* Peek at the next token.  */
28028  token = cp_lexer_peek_token (parser->lexer);
28029
28030  /* Is it a noexcept-specification?  */
28031  type_id_list
28032    = cp_parser_noexcept_specification_opt (parser, flags,
28033					    /*require_constexpr=*/true,
28034					    /*consumed_expr=*/NULL,
28035					    /*return_cond=*/false);
28036  if (type_id_list != NULL_TREE)
28037    return type_id_list;
28038
28039  /* If it's not `throw', then there's no exception-specification.  */
28040  if (!cp_parser_is_keyword (token, RID_THROW))
28041    return NULL_TREE;
28042
28043  location_t loc = token->location;
28044
28045  /* Consume the `throw'.  */
28046  cp_lexer_consume_token (parser->lexer);
28047
28048  /* Look for the `('.  */
28049  matching_parens parens;
28050  parens.require_open (parser);
28051
28052  /* Peek at the next token.  */
28053  token = cp_lexer_peek_token (parser->lexer);
28054  /* If it's not a `)', then there is a type-id-list.  */
28055  if (token->type != CPP_CLOSE_PAREN)
28056    {
28057      /* Types may not be defined in an exception-specification.  */
28058      saved_message = parser->type_definition_forbidden_message;
28059      parser->type_definition_forbidden_message
28060	= G_("types may not be defined in an exception-specification");
28061      /* Parse the type-id-list.  */
28062      type_id_list = cp_parser_type_id_list (parser);
28063      /* Restore the saved message.  */
28064      parser->type_definition_forbidden_message = saved_message;
28065
28066      if (cxx_dialect >= cxx17)
28067	{
28068	  error_at (loc, "ISO C++17 does not allow dynamic exception "
28069			 "specifications");
28070	  type_id_list = NULL_TREE;
28071	}
28072      else if (cxx_dialect >= cxx11)
28073	warning_at (loc, OPT_Wdeprecated,
28074		    "dynamic exception specifications are deprecated in "
28075		    "C++11");
28076    }
28077  /* In C++17, throw() is equivalent to noexcept (true).  throw()
28078     is deprecated in C++11 and above as well, but is still widely used,
28079     so don't warn about it yet.  */
28080  else if (cxx_dialect >= cxx17)
28081    type_id_list = noexcept_true_spec;
28082  else
28083    type_id_list = empty_except_spec;
28084
28085  /* Look for the `)'.  */
28086  parens.require_close (parser);
28087
28088  return type_id_list;
28089}
28090
28091/* Parse an (optional) type-id-list.
28092
28093   type-id-list:
28094     type-id ... [opt]
28095     type-id-list , type-id ... [opt]
28096
28097   Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
28098   in the order that the types were presented.  */
28099
28100static tree
28101cp_parser_type_id_list (cp_parser* parser)
28102{
28103  tree types = NULL_TREE;
28104
28105  while (true)
28106    {
28107      cp_token *token;
28108      tree type;
28109
28110      token = cp_lexer_peek_token (parser->lexer);
28111
28112      /* Get the next type-id.  */
28113      type = cp_parser_type_id (parser);
28114      /* Check for invalid 'auto'.  */
28115      if (flag_concepts && type_uses_auto (type))
28116	{
28117	  error_at (token->location,
28118		    "invalid use of %<auto%> in exception-specification");
28119	  type = error_mark_node;
28120	}
28121      /* Parse the optional ellipsis. */
28122      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28123        {
28124          /* Consume the `...'. */
28125          cp_lexer_consume_token (parser->lexer);
28126
28127          /* Turn the type into a pack expansion expression. */
28128          type = make_pack_expansion (type);
28129        }
28130      /* Add it to the list.  */
28131      types = add_exception_specifier (types, type, /*complain=*/1);
28132      /* Peek at the next token.  */
28133      token = cp_lexer_peek_token (parser->lexer);
28134      /* If it is not a `,', we are done.  */
28135      if (token->type != CPP_COMMA)
28136	break;
28137      /* Consume the `,'.  */
28138      cp_lexer_consume_token (parser->lexer);
28139    }
28140
28141  return nreverse (types);
28142}
28143
28144/* Parse a try-block.
28145
28146   try-block:
28147     try compound-statement handler-seq  */
28148
28149static tree
28150cp_parser_try_block (cp_parser* parser)
28151{
28152  tree try_block;
28153
28154  cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28155  if (parser->in_function_body
28156      && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28157      && cxx_dialect < cxx20)
28158    pedwarn (input_location, OPT_Wc__20_extensions,
28159	     "%<try%> in %<constexpr%> function only "
28160	     "available with %<-std=c++20%> or %<-std=gnu++20%>");
28161
28162  try_block = begin_try_block ();
28163  cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28164  finish_try_block (try_block);
28165  cp_parser_handler_seq (parser);
28166  finish_handler_sequence (try_block);
28167
28168  return try_block;
28169}
28170
28171/* Parse a function-try-block.
28172
28173   function-try-block:
28174     try ctor-initializer [opt] function-body handler-seq  */
28175
28176static void
28177cp_parser_function_try_block (cp_parser* parser)
28178{
28179  tree compound_stmt;
28180  tree try_block;
28181
28182  /* Look for the `try' keyword.  */
28183  if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28184    return;
28185  /* Let the rest of the front end know where we are.  */
28186  try_block = begin_function_try_block (&compound_stmt);
28187  /* Parse the function-body.  */
28188  cp_parser_ctor_initializer_opt_and_function_body
28189    (parser, /*in_function_try_block=*/true);
28190  /* We're done with the `try' part.  */
28191  finish_function_try_block (try_block);
28192  /* Parse the handlers.  */
28193  cp_parser_handler_seq (parser);
28194  /* We're done with the handlers.  */
28195  finish_function_handler_sequence (try_block, compound_stmt);
28196}
28197
28198/* Parse a handler-seq.
28199
28200   handler-seq:
28201     handler handler-seq [opt]  */
28202
28203static void
28204cp_parser_handler_seq (cp_parser* parser)
28205{
28206  while (true)
28207    {
28208      cp_token *token;
28209
28210      /* Parse the handler.  */
28211      cp_parser_handler (parser);
28212      /* Peek at the next token.  */
28213      token = cp_lexer_peek_token (parser->lexer);
28214      /* If it's not `catch' then there are no more handlers.  */
28215      if (!cp_parser_is_keyword (token, RID_CATCH))
28216	break;
28217    }
28218}
28219
28220/* Parse a handler.
28221
28222   handler:
28223     catch ( exception-declaration ) compound-statement  */
28224
28225static void
28226cp_parser_handler (cp_parser* parser)
28227{
28228  tree handler;
28229  tree declaration;
28230
28231  cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28232  handler = begin_handler ();
28233  matching_parens parens;
28234  parens.require_open (parser);
28235  declaration = cp_parser_exception_declaration (parser);
28236  finish_handler_parms (declaration, handler);
28237  parens.require_close (parser);
28238  cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28239  finish_handler (handler);
28240}
28241
28242/* Parse an exception-declaration.
28243
28244   exception-declaration:
28245     type-specifier-seq declarator
28246     type-specifier-seq abstract-declarator
28247     type-specifier-seq
28248     ...
28249
28250   Returns a VAR_DECL for the declaration, or NULL_TREE if the
28251   ellipsis variant is used.  */
28252
28253static tree
28254cp_parser_exception_declaration (cp_parser* parser)
28255{
28256  cp_decl_specifier_seq type_specifiers;
28257  cp_declarator *declarator;
28258  const char *saved_message;
28259
28260  /* If it's an ellipsis, it's easy to handle.  */
28261  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28262    {
28263      /* Consume the `...' token.  */
28264      cp_lexer_consume_token (parser->lexer);
28265      return NULL_TREE;
28266    }
28267
28268  /* Types may not be defined in exception-declarations.  */
28269  saved_message = parser->type_definition_forbidden_message;
28270  parser->type_definition_forbidden_message
28271    = G_("types may not be defined in exception-declarations");
28272
28273  /* Parse the type-specifier-seq.  */
28274  cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28275				/*is_declaration=*/true,
28276				/*is_trailing_return=*/false,
28277				&type_specifiers);
28278  /* If it's a `)', then there is no declarator.  */
28279  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28280    declarator = NULL;
28281  else
28282    declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28283				       CP_PARSER_FLAGS_NONE,
28284				       /*ctor_dtor_or_conv_p=*/NULL,
28285				       /*parenthesized_p=*/NULL,
28286				       /*member_p=*/false,
28287				       /*friend_p=*/false,
28288				       /*static_p=*/false);
28289
28290  /* Restore the saved message.  */
28291  parser->type_definition_forbidden_message = saved_message;
28292
28293  if (!type_specifiers.any_specifiers_p)
28294    return error_mark_node;
28295
28296  return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28297}
28298
28299/* Parse a throw-expression.
28300
28301   throw-expression:
28302     throw assignment-expression [opt]
28303
28304   Returns a THROW_EXPR representing the throw-expression.  */
28305
28306static tree
28307cp_parser_throw_expression (cp_parser* parser)
28308{
28309  tree expression;
28310  cp_token* token;
28311  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28312
28313  cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28314  token = cp_lexer_peek_token (parser->lexer);
28315  /* Figure out whether or not there is an assignment-expression
28316     following the "throw" keyword.  */
28317  if (token->type == CPP_COMMA
28318      || token->type == CPP_SEMICOLON
28319      || token->type == CPP_CLOSE_PAREN
28320      || token->type == CPP_CLOSE_SQUARE
28321      || token->type == CPP_CLOSE_BRACE
28322      || token->type == CPP_COLON)
28323    expression = NULL_TREE;
28324  else
28325    expression = cp_parser_assignment_expression (parser);
28326
28327  /* Construct a location e.g.:
28328       throw x
28329       ^~~~~~~
28330     with caret == start at the start of the "throw" token, and
28331     the end at the end of the final token we consumed.  */
28332  location_t combined_loc = make_location (start_loc, start_loc,
28333					   parser->lexer);
28334  expression = build_throw (combined_loc, expression);
28335
28336  return expression;
28337}
28338
28339/* Parse a yield-expression.
28340
28341   yield-expression:
28342     co_yield assignment-expression
28343     co_yield braced-init-list
28344
28345   Returns a CO_YIELD_EXPR representing the yield-expression.  */
28346
28347static tree
28348cp_parser_yield_expression (cp_parser* parser)
28349{
28350  tree expr;
28351
28352  cp_token *token = cp_lexer_peek_token (parser->lexer);
28353  location_t kw_loc = token->location; /* Save for later.  */
28354
28355  cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28356
28357  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28358    {
28359      bool expr_non_constant_p;
28360      cp_lexer_set_source_position (parser->lexer);
28361      /* ??? : probably a moot point?  */
28362      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28363      expr = cp_parser_braced_list (parser, &expr_non_constant_p);
28364    }
28365  else
28366    expr = cp_parser_assignment_expression (parser);
28367
28368  if (expr == error_mark_node)
28369    return expr;
28370
28371  return finish_co_yield_expr (kw_loc, expr);
28372}
28373
28374/* GNU Extensions */
28375
28376/* Parse an (optional) asm-specification.
28377
28378   asm-specification:
28379     asm ( string-literal )
28380
28381   If the asm-specification is present, returns a STRING_CST
28382   corresponding to the string-literal.  Otherwise, returns
28383   NULL_TREE.  */
28384
28385static tree
28386cp_parser_asm_specification_opt (cp_parser* parser)
28387{
28388  cp_token *token;
28389  tree asm_specification;
28390
28391  /* Peek at the next token.  */
28392  token = cp_lexer_peek_token (parser->lexer);
28393  /* If the next token isn't the `asm' keyword, then there's no
28394     asm-specification.  */
28395  if (!cp_parser_is_keyword (token, RID_ASM))
28396    return NULL_TREE;
28397
28398  /* Consume the `asm' token.  */
28399  cp_lexer_consume_token (parser->lexer);
28400  /* Look for the `('.  */
28401  matching_parens parens;
28402  parens.require_open (parser);
28403
28404  /* Look for the string-literal.  */
28405  asm_specification = cp_parser_string_literal (parser, false, false);
28406
28407  /* Look for the `)'.  */
28408  parens.require_close (parser);
28409
28410  return asm_specification;
28411}
28412
28413/* Parse an asm-operand-list.
28414
28415   asm-operand-list:
28416     asm-operand
28417     asm-operand-list , asm-operand
28418
28419   asm-operand:
28420     string-literal ( expression )
28421     [ string-literal ] string-literal ( expression )
28422
28423   Returns a TREE_LIST representing the operands.  The TREE_VALUE of
28424   each node is the expression.  The TREE_PURPOSE is itself a
28425   TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28426   string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28427   is a STRING_CST for the string literal before the parenthesis. Returns
28428   ERROR_MARK_NODE if any of the operands are invalid.  */
28429
28430static tree
28431cp_parser_asm_operand_list (cp_parser* parser)
28432{
28433  tree asm_operands = NULL_TREE;
28434  bool invalid_operands = false;
28435
28436  while (true)
28437    {
28438      tree string_literal;
28439      tree expression;
28440      tree name;
28441
28442      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28443	{
28444	  /* Consume the `[' token.  */
28445	  cp_lexer_consume_token (parser->lexer);
28446	  /* Read the operand name.  */
28447	  name = cp_parser_identifier (parser);
28448	  if (name != error_mark_node)
28449	    name = build_string (IDENTIFIER_LENGTH (name),
28450				 IDENTIFIER_POINTER (name));
28451	  /* Look for the closing `]'.  */
28452	  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28453	}
28454      else
28455	name = NULL_TREE;
28456      /* Look for the string-literal.  */
28457      string_literal = cp_parser_string_literal (parser, false, false);
28458
28459      /* Look for the `('.  */
28460      matching_parens parens;
28461      parens.require_open (parser);
28462      /* Parse the expression.  */
28463      expression = cp_parser_expression (parser);
28464      /* Look for the `)'.  */
28465      parens.require_close (parser);
28466
28467      if (name == error_mark_node
28468	  || string_literal == error_mark_node
28469	  || expression == error_mark_node)
28470        invalid_operands = true;
28471
28472      /* Add this operand to the list.  */
28473      asm_operands = tree_cons (build_tree_list (name, string_literal),
28474				expression,
28475				asm_operands);
28476      /* If the next token is not a `,', there are no more
28477	 operands.  */
28478      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28479	break;
28480      /* Consume the `,'.  */
28481      cp_lexer_consume_token (parser->lexer);
28482    }
28483
28484  return invalid_operands ? error_mark_node : nreverse (asm_operands);
28485}
28486
28487/* Parse an asm-clobber-list.
28488
28489   asm-clobber-list:
28490     string-literal
28491     asm-clobber-list , string-literal
28492
28493   Returns a TREE_LIST, indicating the clobbers in the order that they
28494   appeared.  The TREE_VALUE of each node is a STRING_CST.  */
28495
28496static tree
28497cp_parser_asm_clobber_list (cp_parser* parser)
28498{
28499  tree clobbers = NULL_TREE;
28500
28501  while (true)
28502    {
28503      tree string_literal;
28504
28505      /* Look for the string literal.  */
28506      string_literal = cp_parser_string_literal (parser, false, false);
28507      /* Add it to the list.  */
28508      clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
28509      /* If the next token is not a `,', then the list is
28510	 complete.  */
28511      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28512	break;
28513      /* Consume the `,' token.  */
28514      cp_lexer_consume_token (parser->lexer);
28515    }
28516
28517  return clobbers;
28518}
28519
28520/* Parse an asm-label-list.
28521
28522   asm-label-list:
28523     identifier
28524     asm-label-list , identifier
28525
28526   Returns a TREE_LIST, indicating the labels in the order that they
28527   appeared.  The TREE_VALUE of each node is a label.  */
28528
28529static tree
28530cp_parser_asm_label_list (cp_parser* parser)
28531{
28532  tree labels = NULL_TREE;
28533
28534  while (true)
28535    {
28536      tree identifier, label, name;
28537
28538      /* Look for the identifier.  */
28539      identifier = cp_parser_identifier (parser);
28540      if (!error_operand_p (identifier))
28541        {
28542	  label = lookup_label (identifier);
28543	  if (TREE_CODE (label) == LABEL_DECL)
28544	    {
28545	      TREE_USED (label) = 1;
28546	      check_goto (label);
28547	      name = build_string (IDENTIFIER_LENGTH (identifier),
28548				   IDENTIFIER_POINTER (identifier));
28549	      labels = tree_cons (name, label, labels);
28550	    }
28551	}
28552      /* If the next token is not a `,', then the list is
28553	 complete.  */
28554      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28555	break;
28556      /* Consume the `,' token.  */
28557      cp_lexer_consume_token (parser->lexer);
28558    }
28559
28560  return nreverse (labels);
28561}
28562
28563/* Return TRUE iff the next tokens in the stream are possibly the
28564   beginning of a GNU extension attribute. */
28565
28566static bool
28567cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
28568{
28569  return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
28570}
28571
28572/* Return TRUE iff the next tokens in the stream are possibly the
28573   beginning of a standard C++-11 attribute specifier.  */
28574
28575static bool
28576cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
28577{
28578  return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
28579}
28580
28581/* Return TRUE iff the next Nth tokens in the stream are possibly the
28582   beginning of a standard C++-11 attribute specifier.  */
28583
28584static bool
28585cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
28586{
28587  cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28588
28589  return (cxx_dialect >= cxx11
28590	  && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
28591	      || (token->type == CPP_OPEN_SQUARE
28592		  && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
28593		  && token->type == CPP_OPEN_SQUARE)));
28594}
28595
28596/* Return TRUE iff the next Nth tokens in the stream are possibly the
28597   beginning of a GNU extension attribute.  */
28598
28599static bool
28600cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
28601{
28602  cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28603
28604  return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
28605}
28606
28607/* Return true iff the next tokens can be the beginning of either a
28608   GNU attribute list, or a standard C++11 attribute sequence.  */
28609
28610static bool
28611cp_next_tokens_can_be_attribute_p (cp_parser *parser)
28612{
28613  return (cp_next_tokens_can_be_gnu_attribute_p (parser)
28614	  || cp_next_tokens_can_be_std_attribute_p (parser));
28615}
28616
28617/* Return true iff the next Nth tokens can be the beginning of either
28618   a GNU attribute list, or a standard C++11 attribute sequence.  */
28619
28620static bool
28621cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
28622{
28623  return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
28624	  || cp_nth_tokens_can_be_std_attribute_p (parser, n));
28625}
28626
28627/* Parse either a standard C++-11 attribute-specifier-seq, or a series
28628   of GNU attributes, or return NULL.  */
28629
28630static tree
28631cp_parser_attributes_opt (cp_parser *parser)
28632{
28633  if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28634    return cp_parser_gnu_attributes_opt (parser);
28635  return cp_parser_std_attribute_spec_seq (parser);
28636}
28637
28638/* Parse an (optional) series of attributes.
28639
28640   attributes:
28641     attributes attribute
28642
28643   attribute:
28644     __attribute__ (( attribute-list [opt] ))
28645
28646   The return value is as for cp_parser_gnu_attribute_list.  */
28647
28648static tree
28649cp_parser_gnu_attributes_opt (cp_parser* parser)
28650{
28651  tree attributes = NULL_TREE;
28652
28653  auto cleanup = make_temp_override
28654    (parser->auto_is_implicit_function_template_parm_p, false);
28655
28656  while (true)
28657    {
28658      cp_token *token;
28659      tree attribute_list;
28660      bool ok = true;
28661
28662      /* Peek at the next token.  */
28663      token = cp_lexer_peek_token (parser->lexer);
28664      /* If it's not `__attribute__', then we're done.  */
28665      if (token->keyword != RID_ATTRIBUTE)
28666	break;
28667
28668      /* Consume the `__attribute__' keyword.  */
28669      cp_lexer_consume_token (parser->lexer);
28670      /* Look for the two `(' tokens.  */
28671      matching_parens outer_parens;
28672      if (!outer_parens.require_open (parser))
28673	ok = false;
28674      matching_parens inner_parens;
28675      if (!inner_parens.require_open (parser))
28676	ok = false;
28677
28678      /* Peek at the next token.  */
28679      token = cp_lexer_peek_token (parser->lexer);
28680      if (token->type != CPP_CLOSE_PAREN)
28681	/* Parse the attribute-list.  */
28682	attribute_list = cp_parser_gnu_attribute_list (parser);
28683      else
28684	/* If the next token is a `)', then there is no attribute
28685	   list.  */
28686	attribute_list = NULL;
28687
28688      /* Look for the two `)' tokens.  */
28689      if (!inner_parens.require_close (parser))
28690	ok = false;
28691      if (!outer_parens.require_close (parser))
28692	ok = false;
28693      if (!ok)
28694	cp_parser_skip_to_end_of_statement (parser);
28695
28696      /* Add these new attributes to the list.  */
28697      attributes = attr_chainon (attributes, attribute_list);
28698    }
28699
28700  return attributes;
28701}
28702
28703/* Parse a GNU attribute-list.
28704
28705   attribute-list:
28706     attribute
28707     attribute-list , attribute
28708
28709   attribute:
28710     identifier
28711     identifier ( identifier )
28712     identifier ( identifier , expression-list )
28713     identifier ( expression-list )
28714
28715   Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
28716   to an attribute.  The TREE_PURPOSE of each node is the identifier
28717   indicating which attribute is in use.  The TREE_VALUE represents
28718   the arguments, if any.  */
28719
28720static tree
28721cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
28722{
28723  tree attribute_list = NULL_TREE;
28724  bool save_translate_strings_p = parser->translate_strings_p;
28725
28726  /* Don't create wrapper nodes within attributes: the
28727     handlers don't know how to handle them.  */
28728  auto_suppress_location_wrappers sentinel;
28729
28730  parser->translate_strings_p = false;
28731  while (true)
28732    {
28733      cp_token *token;
28734      tree identifier;
28735      tree attribute;
28736
28737      /* Look for the identifier.  We also allow keywords here; for
28738	 example `__attribute__ ((const))' is legal.  */
28739      token = cp_lexer_peek_token (parser->lexer);
28740      if (token->type == CPP_NAME
28741	  || token->type == CPP_KEYWORD)
28742	{
28743	  tree arguments = NULL_TREE;
28744
28745	  /* Consume the token, but save it since we need it for the
28746	     SIMD enabled function parsing.  */
28747	  cp_token *id_token = cp_lexer_consume_token (parser->lexer);
28748
28749	  /* Save away the identifier that indicates which attribute
28750	     this is.  */
28751	  identifier = (token->type == CPP_KEYWORD)
28752	    /* For keywords, use the canonical spelling, not the
28753	       parsed identifier.  */
28754	    ? ridpointers[(int) token->keyword]
28755	    : id_token->u.value;
28756
28757	  identifier = canonicalize_attr_name (identifier);
28758	  attribute = build_tree_list (identifier, NULL_TREE);
28759
28760	  /* Peek at the next token.  */
28761	  token = cp_lexer_peek_token (parser->lexer);
28762	  /* If it's an `(', then parse the attribute arguments.  */
28763	  if (token->type == CPP_OPEN_PAREN)
28764	    {
28765	      vec<tree, va_gc> *vec;
28766	      int attr_flag = (attribute_takes_identifier_p (identifier)
28767			       ? id_attr : normal_attr);
28768	      vec = cp_parser_parenthesized_expression_list
28769		    (parser, attr_flag, /*cast_p=*/false,
28770		    /*allow_expansion_p=*/false,
28771		    /*non_constant_p=*/NULL);
28772	      if (vec == NULL)
28773		arguments = error_mark_node;
28774	      else
28775		{
28776		  arguments = build_tree_list_vec (vec);
28777		  release_tree_vector (vec);
28778		}
28779	      /* Save the arguments away.  */
28780	      TREE_VALUE (attribute) = arguments;
28781	    }
28782
28783	  if (arguments != error_mark_node)
28784	    {
28785	      /* Add this attribute to the list.  */
28786	      TREE_CHAIN (attribute) = attribute_list;
28787	      attribute_list = attribute;
28788	    }
28789
28790	  token = cp_lexer_peek_token (parser->lexer);
28791	}
28792      /* Unless EXACTLY_ONE is set look for more attributes.
28793	 If the next token isn't a `,', we're done.  */
28794      if (exactly_one || token->type != CPP_COMMA)
28795	break;
28796
28797      /* Consume the comma and keep going.  */
28798      cp_lexer_consume_token (parser->lexer);
28799    }
28800  parser->translate_strings_p = save_translate_strings_p;
28801
28802  /* We built up the list in reverse order.  */
28803  return nreverse (attribute_list);
28804}
28805
28806/* Parse arguments of omp::directive attribute.
28807
28808   ( directive-name ,[opt] clause-list[opt] )
28809
28810   For directive just remember the first/last tokens for subsequent
28811   parsing.  */
28812
28813static void
28814cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
28815{
28816  cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
28817  if (first->type == CPP_CLOSE_PAREN)
28818    {
28819      cp_lexer_consume_token (parser->lexer);
28820      error_at (first->location, "expected OpenMP directive name");
28821      cp_lexer_consume_token (parser->lexer);
28822      TREE_VALUE (attribute) = NULL_TREE;
28823      return;
28824    }
28825  size_t n = cp_parser_skip_balanced_tokens (parser, 1);
28826  if (n == 1)
28827    {
28828      cp_lexer_consume_token (parser->lexer);
28829      error_at (first->location, "expected attribute argument as balanced "
28830				 "token sequence");
28831      TREE_VALUE (attribute) = NULL_TREE;
28832      return;
28833    }
28834  for (n = n - 2; n; --n)
28835    cp_lexer_consume_token (parser->lexer);
28836  cp_token *last = cp_lexer_peek_token (parser->lexer);
28837  cp_lexer_consume_token (parser->lexer);
28838  tree arg = make_node (DEFERRED_PARSE);
28839  DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
28840  DEFPARSE_INSTANTIATIONS (arg) = nullptr;
28841  TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
28842}
28843
28844/* Parse arguments of omp::sequence attribute.
28845
28846   ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... )  */
28847
28848static void
28849cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
28850{
28851  matching_parens parens;
28852  parens.consume_open (parser);
28853  do
28854    {
28855      cp_token *token = cp_lexer_peek_token (parser->lexer);
28856      if (token->type == CPP_NAME
28857	  && token->u.value == omp_identifier
28858	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
28859	{
28860	  cp_lexer_consume_token (parser->lexer);
28861	  cp_lexer_consume_token (parser->lexer);
28862	  token = cp_lexer_peek_token (parser->lexer);
28863	}
28864      bool directive = false;
28865      const char *p;
28866      if (token->type != CPP_NAME)
28867	p = "";
28868      else
28869	p = IDENTIFIER_POINTER (token->u.value);
28870      if (strcmp (p, "directive") == 0)
28871	directive = true;
28872      else if (strcmp (p, "sequence") != 0)
28873	{
28874	  error_at (token->location, "expected %<directive%> or %<sequence%>");
28875	  cp_parser_skip_to_closing_parenthesis (parser,
28876						 /*recovering=*/true,
28877						 /*or_comma=*/true,
28878						 /*consume_paren=*/false);
28879	  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28880	    break;
28881	  cp_lexer_consume_token (parser->lexer);
28882	}
28883      cp_lexer_consume_token (parser->lexer);
28884      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
28885	cp_parser_required_error (parser, RT_OPEN_PAREN, false,
28886				  UNKNOWN_LOCATION);
28887      else if (directive)
28888	cp_parser_omp_directive_args (parser, attribute);
28889      else
28890	cp_parser_omp_sequence_args (parser, attribute);
28891      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28892	break;
28893      cp_lexer_consume_token (parser->lexer);
28894    }
28895  while (1);
28896  if (!parens.require_close (parser))
28897    cp_parser_skip_to_closing_parenthesis (parser, true, false,
28898					   /*consume_paren=*/true);
28899}
28900
28901/*  Parse a standard C++11 attribute.
28902
28903    The returned representation is a TREE_LIST which TREE_PURPOSE is
28904    the scoped name of the attribute, and the TREE_VALUE is its
28905    arguments list.
28906
28907    Note that the scoped name of the attribute is itself a TREE_LIST
28908    which TREE_PURPOSE is the namespace of the attribute, and
28909    TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
28910    by cp_parser_gnu_attribute_list -- that doesn't have any namespace
28911    and which TREE_PURPOSE is directly the attribute name.
28912
28913    Clients of the attribute code should use get_attribute_namespace
28914    and get_attribute_name to get the actual namespace and name of
28915    attributes, regardless of their being GNU or C++11 attributes.
28916
28917    attribute:
28918      attribute-token attribute-argument-clause [opt]
28919
28920    attribute-token:
28921      identifier
28922      attribute-scoped-token
28923
28924    attribute-scoped-token:
28925      attribute-namespace :: identifier
28926
28927    attribute-namespace:
28928      identifier
28929
28930    attribute-argument-clause:
28931      ( balanced-token-seq )
28932
28933    balanced-token-seq:
28934      balanced-token [opt]
28935      balanced-token-seq balanced-token
28936
28937    balanced-token:
28938      ( balanced-token-seq )
28939      [ balanced-token-seq ]
28940      { balanced-token-seq }.  */
28941
28942static tree
28943cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
28944{
28945  tree attribute, attr_id = NULL_TREE, arguments;
28946  cp_token *token;
28947
28948  auto cleanup = make_temp_override
28949    (parser->auto_is_implicit_function_template_parm_p, false);
28950
28951  /* First, parse name of the attribute, a.k.a attribute-token.  */
28952
28953  token = cp_lexer_peek_token (parser->lexer);
28954  if (token->type == CPP_NAME)
28955    attr_id = token->u.value;
28956  else if (token->type == CPP_KEYWORD)
28957    attr_id = ridpointers[(int) token->keyword];
28958  else if (token->flags & NAMED_OP)
28959    attr_id = get_identifier (cpp_type2name (token->type, token->flags));
28960
28961  if (attr_id == NULL_TREE)
28962    return NULL_TREE;
28963
28964  cp_lexer_consume_token (parser->lexer);
28965
28966  token = cp_lexer_peek_token (parser->lexer);
28967  if (token->type == CPP_SCOPE)
28968    {
28969      /* We are seeing a scoped attribute token.  */
28970
28971      cp_lexer_consume_token (parser->lexer);
28972      if (attr_ns)
28973	error_at (token->location, "attribute using prefix used together "
28974				   "with scoped attribute token");
28975      attr_ns = attr_id;
28976
28977      token = cp_lexer_peek_token (parser->lexer);
28978      if (token->type == CPP_NAME)
28979	attr_id = token->u.value;
28980      else if (token->type == CPP_KEYWORD)
28981	attr_id = ridpointers[(int) token->keyword];
28982      else if (token->flags & NAMED_OP)
28983	attr_id = get_identifier (cpp_type2name (token->type, token->flags));
28984      else
28985	{
28986	  error_at (token->location,
28987		    "expected an identifier for the attribute name");
28988	  return error_mark_node;
28989	}
28990      cp_lexer_consume_token (parser->lexer);
28991
28992      attr_ns = canonicalize_attr_name (attr_ns);
28993      attr_id = canonicalize_attr_name (attr_id);
28994      attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
28995				   NULL_TREE);
28996      token = cp_lexer_peek_token (parser->lexer);
28997    }
28998  else if (attr_ns)
28999    {
29000      attr_ns = canonicalize_attr_name (attr_ns);
29001      attr_id = canonicalize_attr_name (attr_id);
29002      attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29003				   NULL_TREE);
29004    }
29005  else
29006    {
29007      attr_id = canonicalize_attr_name (attr_id);
29008      attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29009				   NULL_TREE);
29010      /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29011	 but no longer: we have to be able to tell [[noreturn]] and
29012	 __attribute__((noreturn)) apart.  */
29013      /* C++14 deprecated attribute is equivalent to GNU's.  */
29014      if (is_attribute_p ("deprecated", attr_id))
29015	TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29016      /* C++17 fallthrough attribute is equivalent to GNU's.  */
29017      else if (is_attribute_p ("fallthrough", attr_id))
29018	TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29019      /* Transactional Memory TS optimize_for_synchronized attribute is
29020	 equivalent to GNU transaction_callable.  */
29021      else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29022	TREE_PURPOSE (attribute)
29023	  = get_identifier ("transaction_callable");
29024      /* Transactional Memory attributes are GNU attributes.  */
29025      else if (tm_attr_to_mask (attr_id))
29026	TREE_PURPOSE (attribute) = attr_id;
29027    }
29028
29029  /* Now parse the optional argument clause of the attribute.  */
29030
29031  if (token->type != CPP_OPEN_PAREN)
29032    {
29033      if ((flag_openmp || flag_openmp_simd)
29034	  && attr_ns == omp_identifier
29035	  && (is_attribute_p ("directive", attr_id)
29036	      || is_attribute_p ("sequence", attr_id)))
29037	{
29038	  error_at (token->location, "%<omp::%E%> attribute requires argument",
29039		    attr_id);
29040	  return NULL_TREE;
29041	}
29042      return attribute;
29043    }
29044
29045  {
29046    vec<tree, va_gc> *vec;
29047    int attr_flag = normal_attr;
29048
29049    /* Maybe we don't expect to see any arguments for this attribute.  */
29050    const attribute_spec *as
29051      = lookup_attribute_spec (TREE_PURPOSE (attribute));
29052    if (as && as->max_length == 0)
29053      {
29054	error_at (token->location, "%qE attribute does not take any arguments",
29055		  attr_id);
29056	cp_parser_skip_to_closing_parenthesis (parser,
29057					       /*recovering=*/true,
29058					       /*or_comma=*/false,
29059					       /*consume_paren=*/true);
29060	return error_mark_node;
29061      }
29062
29063    if (attr_ns == gnu_identifier
29064	&& attribute_takes_identifier_p (attr_id))
29065      /* A GNU attribute that takes an identifier in parameter.  */
29066      attr_flag = id_attr;
29067
29068    /* If this is a fake attribute created to handle -Wno-attributes,
29069       we must skip parsing the arguments.  */
29070    if (as == NULL || attribute_ignored_p (as))
29071      {
29072	if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29073	  {
29074	    if (is_attribute_p ("directive", attr_id))
29075	      {
29076		cp_parser_omp_directive_args (parser, attribute);
29077		return attribute;
29078	      }
29079	    else if (is_attribute_p ("sequence", attr_id))
29080	      {
29081		TREE_VALUE (TREE_PURPOSE (attribute))
29082		  = get_identifier ("directive");
29083		cp_parser_omp_sequence_args (parser, attribute);
29084		TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29085		return attribute;
29086	      }
29087	  }
29088
29089	/* For unknown attributes, just skip balanced tokens instead of
29090	   trying to parse the arguments.  */
29091	for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29092	  cp_lexer_consume_token (parser->lexer);
29093	return attribute;
29094      }
29095
29096    vec = cp_parser_parenthesized_expression_list
29097      (parser, attr_flag, /*cast_p=*/false,
29098       /*allow_expansion_p=*/true,
29099       /*non_constant_p=*/NULL);
29100    if (vec == NULL)
29101      arguments = error_mark_node;
29102    else
29103      {
29104	if (vec->is_empty ())
29105	  /* e.g. [[attr()]].  */
29106	  error_at (token->location, "parentheses must be omitted if "
29107		    "%qE attribute argument list is empty",
29108		    attr_id);
29109	arguments = build_tree_list_vec (vec);
29110	release_tree_vector (vec);
29111      }
29112
29113    if (arguments == error_mark_node)
29114      attribute = error_mark_node;
29115    else
29116      TREE_VALUE (attribute) = arguments;
29117  }
29118
29119  return attribute;
29120}
29121
29122/* Warn if the attribute ATTRIBUTE appears more than once in the
29123   attribute-list ATTRIBUTES.  This used to be enforced for certain
29124   attributes, but the restriction was removed in P2156.  Note that
29125   carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
29126   LOC is the location of ATTRIBUTE.  Returns true if ATTRIBUTE was not
29127   found in ATTRIBUTES.  */
29128
29129static bool
29130cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29131{
29132  static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29133			"likely", "unlikely", "fallthrough",
29134			"no_unique_address" };
29135  if (attributes)
29136    for (const auto &a : alist)
29137      if (is_attribute_p (a, get_attribute_name (attribute))
29138	  && lookup_attribute (a, attributes))
29139	{
29140	  if (!from_macro_expansion_at (loc))
29141	    warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29142			"multiple times", a);
29143	  return false;
29144	}
29145  return true;
29146}
29147
29148/* Parse a list of standard C++-11 attributes.
29149
29150   attribute-list:
29151     attribute [opt]
29152     attribute-list , attribute[opt]
29153     attribute ...
29154     attribute-list , attribute ...
29155*/
29156
29157static tree
29158cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29159{
29160  tree attributes = NULL_TREE, attribute = NULL_TREE;
29161  cp_token *token = NULL;
29162
29163  while (true)
29164    {
29165      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29166      attribute = cp_parser_std_attribute (parser, attr_ns);
29167      if (attribute == error_mark_node)
29168	break;
29169      if (attribute != NULL_TREE)
29170	{
29171	  if (cp_parser_check_std_attribute (loc, attributes, attribute))
29172	    {
29173	      TREE_CHAIN (attribute) = attributes;
29174	      attributes = attribute;
29175	    }
29176	}
29177      token = cp_lexer_peek_token (parser->lexer);
29178      if (token->type == CPP_ELLIPSIS)
29179	{
29180	  cp_lexer_consume_token (parser->lexer);
29181	  if (attribute == NULL_TREE)
29182	    error_at (token->location,
29183		      "expected attribute before %<...%>");
29184	  else
29185	    {
29186	      tree pack = make_pack_expansion (TREE_VALUE (attribute));
29187	      if (pack == error_mark_node)
29188		return error_mark_node;
29189	      TREE_VALUE (attribute) = pack;
29190	    }
29191	  token = cp_lexer_peek_token (parser->lexer);
29192	}
29193      if (token->type != CPP_COMMA)
29194	break;
29195      cp_lexer_consume_token (parser->lexer);
29196    }
29197  attributes = nreverse (attributes);
29198  return attributes;
29199}
29200
29201/* Parse a standard C++-11 attribute specifier.
29202
29203   attribute-specifier:
29204     [ [ attribute-using-prefix [opt] attribute-list ] ]
29205     alignment-specifier
29206
29207   attribute-using-prefix:
29208     using attribute-namespace :
29209
29210   alignment-specifier:
29211     alignas ( type-id ... [opt] )
29212     alignas ( alignment-expression ... [opt] ).  */
29213
29214static tree
29215cp_parser_std_attribute_spec (cp_parser *parser)
29216{
29217  tree attributes = NULL_TREE;
29218  cp_token *token = cp_lexer_peek_token (parser->lexer);
29219
29220  if (token->type == CPP_OPEN_SQUARE
29221      && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
29222    {
29223      tree attr_ns = NULL_TREE;
29224
29225      cp_lexer_consume_token (parser->lexer);
29226      cp_lexer_consume_token (parser->lexer);
29227
29228      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29229	{
29230	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
29231	  if (token->type == CPP_NAME)
29232	    attr_ns = token->u.value;
29233	  else if (token->type == CPP_KEYWORD)
29234	    attr_ns = ridpointers[(int) token->keyword];
29235	  else if (token->flags & NAMED_OP)
29236	    attr_ns = get_identifier (cpp_type2name (token->type,
29237						     token->flags));
29238	  if (attr_ns
29239	      && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
29240	    {
29241	      if (cxx_dialect < cxx17)
29242		pedwarn (input_location, OPT_Wc__17_extensions,
29243			 "attribute using prefix only available "
29244			 "with %<-std=c++17%> or %<-std=gnu++17%>");
29245
29246	      cp_lexer_consume_token (parser->lexer);
29247	      cp_lexer_consume_token (parser->lexer);
29248	      cp_lexer_consume_token (parser->lexer);
29249	    }
29250	  else
29251	    attr_ns = NULL_TREE;
29252	}
29253
29254      attributes = cp_parser_std_attribute_list (parser, attr_ns);
29255
29256      if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
29257	  || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
29258	cp_parser_skip_to_end_of_statement (parser);
29259      else
29260	/* Warn about parsing c++11 attribute in non-c++11 mode, only
29261	   when we are sure that we have actually parsed them.  */
29262	maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29263    }
29264  else
29265    {
29266      tree alignas_expr;
29267
29268      /* Look for an alignment-specifier.  */
29269
29270      token = cp_lexer_peek_token (parser->lexer);
29271
29272      if (token->type != CPP_KEYWORD
29273	  || token->keyword != RID_ALIGNAS)
29274	return NULL_TREE;
29275
29276      cp_lexer_consume_token (parser->lexer);
29277      maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29278
29279      matching_parens parens;
29280      if (!parens.require_open (parser))
29281	return error_mark_node;
29282
29283      cp_parser_parse_tentatively (parser);
29284      alignas_expr = cp_parser_type_id (parser);
29285
29286      if (!cp_parser_parse_definitely (parser))
29287	{
29288	  alignas_expr = cp_parser_assignment_expression (parser);
29289	  if (alignas_expr == error_mark_node)
29290	    cp_parser_skip_to_end_of_statement (parser);
29291	  if (alignas_expr == NULL_TREE
29292	      || alignas_expr == error_mark_node)
29293	    return alignas_expr;
29294	}
29295
29296      alignas_expr = cxx_alignas_expr (alignas_expr);
29297      alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
29298
29299      /* Handle alignas (pack...).  */
29300      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29301	{
29302	  cp_lexer_consume_token (parser->lexer);
29303	  alignas_expr = make_pack_expansion (alignas_expr);
29304	}
29305
29306      /* Something went wrong, so don't build the attribute.  */
29307      if (alignas_expr == error_mark_node)
29308	return error_mark_node;
29309
29310      /* Missing ')' means the code cannot possibly be valid; go ahead
29311	 and commit to make sure we issue a hard error.  */
29312      if (cp_parser_uncommitted_to_tentative_parse_p (parser)
29313	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29314	cp_parser_commit_to_tentative_parse (parser);
29315
29316      if (!parens.require_close (parser))
29317	return error_mark_node;
29318
29319      /* Build the C++-11 representation of an 'aligned'
29320	 attribute.  */
29321      attributes
29322	= build_tree_list (build_tree_list (gnu_identifier,
29323					    aligned_identifier), alignas_expr);
29324    }
29325
29326  return attributes;
29327}
29328
29329/* Parse a standard C++-11 attribute-specifier-seq.
29330
29331   attribute-specifier-seq:
29332     attribute-specifier-seq [opt] attribute-specifier
29333 */
29334
29335static tree
29336cp_parser_std_attribute_spec_seq (cp_parser *parser)
29337{
29338  tree attr_specs = NULL_TREE;
29339  tree attr_last = NULL_TREE;
29340
29341  /* Don't create wrapper nodes within attributes: the
29342     handlers don't know how to handle them.  */
29343  auto_suppress_location_wrappers sentinel;
29344
29345  while (true)
29346    {
29347      tree attr_spec = cp_parser_std_attribute_spec (parser);
29348      if (attr_spec == NULL_TREE)
29349	break;
29350      if (attr_spec == error_mark_node)
29351	return error_mark_node;
29352
29353      if (attr_last)
29354	TREE_CHAIN (attr_last) = attr_spec;
29355      else
29356	attr_specs = attr_last = attr_spec;
29357      attr_last = tree_last (attr_last);
29358    }
29359
29360  return attr_specs;
29361}
29362
29363/* Skip a balanced-token starting at Nth token (with 1 as the next token),
29364   return index of the first token after balanced-token, or N on failure.  */
29365
29366static size_t
29367cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
29368{
29369  size_t orig_n = n;
29370  int nparens = 0, nbraces = 0, nsquares = 0;
29371  do
29372    switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
29373      {
29374      case CPP_PRAGMA_EOL:
29375	if (!parser->lexer->in_pragma)
29376	  break;
29377	/* FALLTHRU */
29378      case CPP_EOF:
29379	/* Ran out of tokens.  */
29380	return orig_n;
29381      case CPP_OPEN_PAREN:
29382	++nparens;
29383	break;
29384      case CPP_OPEN_BRACE:
29385	++nbraces;
29386	break;
29387      case CPP_OPEN_SQUARE:
29388	++nsquares;
29389	break;
29390      case CPP_CLOSE_PAREN:
29391	--nparens;
29392	break;
29393      case CPP_CLOSE_BRACE:
29394	--nbraces;
29395	break;
29396      case CPP_CLOSE_SQUARE:
29397	--nsquares;
29398	break;
29399      default:
29400	break;
29401      }
29402  while (nparens || nbraces || nsquares);
29403  return n;
29404}
29405
29406/* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
29407   return index of the first token after the GNU attribute tokens, or N on
29408   failure.  */
29409
29410static size_t
29411cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
29412{
29413  while (true)
29414    {
29415      if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
29416	  || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
29417	  || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
29418	break;
29419
29420      size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
29421      if (n2 == n + 2)
29422	break;
29423      if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
29424	break;
29425      n = n2 + 1;
29426    }
29427  return n;
29428}
29429
29430/* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
29431   next token), return index of the first token after the standard C++11
29432   attribute tokens, or N on failure.  */
29433
29434static size_t
29435cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
29436{
29437  while (true)
29438    {
29439      if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
29440	  && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
29441	{
29442	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29443	  if (n2 == n + 1)
29444	    break;
29445	  if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
29446	    break;
29447	  n = n2 + 1;
29448	}
29449      else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
29450	       && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
29451	{
29452	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29453	  if (n2 == n + 1)
29454	    break;
29455	  n = n2;
29456	}
29457      else
29458	break;
29459    }
29460  return n;
29461}
29462
29463/* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
29464   as the next token), return index of the first token after the attribute
29465   tokens, or N on failure.  */
29466
29467static size_t
29468cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
29469{
29470  if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
29471    return cp_parser_skip_gnu_attributes_opt (parser, n);
29472  return cp_parser_skip_std_attribute_spec_seq (parser, n);
29473}
29474
29475/* Parse an optional `__extension__' keyword.  Returns TRUE if it is
29476   present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
29477   current value of the PEDANTIC flag, regardless of whether or not
29478   the `__extension__' keyword is present.  The caller is responsible
29479   for restoring the value of the PEDANTIC flag.  */
29480
29481static bool
29482cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
29483{
29484  /* Save the old value of the PEDANTIC flag.  */
29485  *saved_pedantic = pedantic;
29486
29487  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
29488    {
29489      /* Consume the `__extension__' token.  */
29490      cp_lexer_consume_token (parser->lexer);
29491      /* We're not being pedantic while the `__extension__' keyword is
29492	 in effect.  */
29493      pedantic = 0;
29494
29495      return true;
29496    }
29497
29498  return false;
29499}
29500
29501/* Parse a label declaration.
29502
29503   label-declaration:
29504     __label__ label-declarator-seq ;
29505
29506   label-declarator-seq:
29507     identifier , label-declarator-seq
29508     identifier  */
29509
29510static void
29511cp_parser_label_declaration (cp_parser* parser)
29512{
29513  /* Look for the `__label__' keyword.  */
29514  cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
29515
29516  while (true)
29517    {
29518      tree identifier;
29519
29520      /* Look for an identifier.  */
29521      identifier = cp_parser_identifier (parser);
29522      /* If we failed, stop.  */
29523      if (identifier == error_mark_node)
29524	break;
29525      /* Declare it as a label.  */
29526      finish_label_decl (identifier);
29527      /* If the next token is a `;', stop.  */
29528      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29529	break;
29530      /* Look for the `,' separating the label declarations.  */
29531      cp_parser_require (parser, CPP_COMMA, RT_COMMA);
29532    }
29533
29534  /* Look for the final `;'.  */
29535  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29536}
29537
29538// -------------------------------------------------------------------------- //
29539// Concept definitions
29540
29541static tree
29542cp_parser_concept_definition (cp_parser *parser)
29543{
29544  /* A concept definition is an unevaluated context.  */
29545  cp_unevaluated u;
29546
29547  gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
29548  cp_lexer_consume_token (parser->lexer);
29549
29550  cp_expr id = cp_parser_identifier (parser);
29551  if (id == error_mark_node)
29552    {
29553      cp_parser_skip_to_end_of_statement (parser);
29554      cp_parser_consume_semicolon_at_end_of_statement (parser);
29555      return NULL_TREE;
29556    }
29557
29558  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29559    {
29560      cp_parser_skip_to_end_of_statement (parser);
29561      cp_parser_consume_semicolon_at_end_of_statement (parser);
29562      return error_mark_node;
29563    }
29564
29565  processing_constraint_expression_sentinel parsing_constraint;
29566  tree init = cp_parser_constraint_expression (parser);
29567  if (init == error_mark_node)
29568    cp_parser_skip_to_end_of_statement (parser);
29569
29570  /* Consume the trailing ';'. Diagnose the problem if it isn't there,
29571     but continue as if it were.  */
29572  cp_parser_consume_semicolon_at_end_of_statement (parser);
29573
29574  return finish_concept_definition (id, init);
29575}
29576
29577// -------------------------------------------------------------------------- //
29578// Requires Clause
29579
29580/* Diagnose an expression that should appear in ()'s within a requires-clause
29581   and suggest where to place those parentheses.  */
29582
29583static void
29584cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
29585{
29586  error_at (loc, "expression must be enclosed in parentheses");
29587}
29588
29589static void
29590cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
29591{
29592  gcc_rich_location richloc (loc);
29593  richloc.add_fixit_insert_before ("(");
29594  richloc.add_fixit_insert_after (")");
29595  error_at (&richloc, "expression must be enclosed in parentheses");
29596}
29597
29598/* Characterizes the likely kind of expression intended by a mis-written
29599   primary constraint.  */
29600enum primary_constraint_error
29601{
29602  pce_ok,
29603  pce_maybe_operator,
29604  pce_maybe_postfix
29605};
29606
29607/* Returns true if the token(s) following a primary-expression in a
29608   constraint-logical-* expression would require parentheses.  */
29609
29610static primary_constraint_error
29611cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
29612{
29613  cp_token *token = cp_lexer_peek_token (parser->lexer);
29614  switch (token->type)
29615    {
29616      default:
29617	return pce_ok;
29618
29619      case CPP_EQ:
29620	{
29621	  /* An equal sign may be part of the definition of a function,
29622	     and not an assignment operator, when parsing the expression
29623	     for a trailing requires-clause. For example:
29624
29625		template<typename T>
29626		struct S {
29627		  S() requires C<T> = default;
29628		};
29629
29630	     Don't try to reparse this a binary operator.  */
29631	  if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
29632	      || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
29633	    return pce_ok;
29634
29635	  gcc_fallthrough ();
29636	}
29637
29638      /* Arithmetic operators.  */
29639      case CPP_PLUS:
29640      case CPP_MINUS:
29641      case CPP_MULT:
29642      case CPP_DIV:
29643      case CPP_MOD:
29644      /* Bitwise operators.  */
29645      case CPP_AND:
29646      case CPP_OR:
29647      case CPP_XOR:
29648      case CPP_RSHIFT:
29649      case CPP_LSHIFT:
29650      /* Relational operators.  */
29651      case CPP_EQ_EQ:
29652      case CPP_NOT_EQ:
29653      case CPP_LESS:
29654      case CPP_GREATER:
29655      case CPP_LESS_EQ:
29656      case CPP_GREATER_EQ:
29657      case CPP_SPACESHIP:
29658      /* Pointer-to-member.  */
29659      case CPP_DOT_STAR:
29660      case CPP_DEREF_STAR:
29661      /* Assignment operators.  */
29662      case CPP_PLUS_EQ:
29663      case CPP_MINUS_EQ:
29664      case CPP_MULT_EQ:
29665      case CPP_DIV_EQ:
29666      case CPP_MOD_EQ:
29667      case CPP_AND_EQ:
29668      case CPP_OR_EQ:
29669      case CPP_XOR_EQ:
29670      case CPP_RSHIFT_EQ:
29671      case CPP_LSHIFT_EQ:
29672      /* Conditional operator */
29673      case CPP_QUERY:
29674	/* Unenclosed binary or conditional operator.  */
29675	return pce_maybe_operator;
29676
29677      case CPP_OPEN_PAREN:
29678	{
29679	  /* A primary constraint that precedes the parameter-list of a
29680	     lambda expression is followed by an open paren.
29681
29682		[]<typename T> requires C (T a, T b) { ... }
29683
29684	     Don't try to re-parse this as a postfix expression.  */
29685	  if (lambda_p)
29686	    return pce_ok;
29687
29688	  gcc_fallthrough ();
29689	}
29690      case CPP_OPEN_SQUARE:
29691	{
29692	  /* A primary-constraint-expression followed by a '[[' is not a
29693	     postfix expression.  */
29694	  if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
29695	    return pce_ok;
29696
29697	  gcc_fallthrough ();
29698	}
29699      case CPP_PLUS_PLUS:
29700      case CPP_MINUS_MINUS:
29701      case CPP_DOT:
29702	/* Unenclosed postfix operator.  */
29703	return pce_maybe_postfix;
29704
29705      case CPP_DEREF:
29706	/* A primary constraint that precedes the lambda-declarator of a
29707	   lambda expression is followed by trailing return type.
29708
29709	      []<typename T> requires C -> void {}
29710
29711	   Don't try to re-parse this as a postfix expression in
29712	   C++23 and later.  In C++20 ( needs to come in between but we
29713	   allow it to be omitted with pedwarn.  */
29714	if (lambda_p)
29715	  return pce_ok;
29716	/* Unenclosed postfix operator.  */
29717	return pce_maybe_postfix;
29718   }
29719}
29720
29721/* Returns true if the next token begins a unary expression, preceded by
29722   an operator or keyword.  */
29723
29724static bool
29725cp_parser_unary_constraint_requires_parens (cp_parser *parser)
29726{
29727  cp_token *token = cp_lexer_peek_token (parser->lexer);
29728  switch (token->type)
29729    {
29730      case CPP_NOT:
29731      case CPP_PLUS:
29732      case CPP_MINUS:
29733      case CPP_MULT:
29734      case CPP_COMPL:
29735      case CPP_PLUS_PLUS:
29736      case CPP_MINUS_MINUS:
29737	return true;
29738
29739      case CPP_KEYWORD:
29740	{
29741	  switch (token->keyword)
29742	    {
29743	      case RID_STATCAST:
29744	      case RID_DYNCAST:
29745	      case RID_REINTCAST:
29746	      case RID_CONSTCAST:
29747	      case RID_TYPEID:
29748	      case RID_SIZEOF:
29749	      case RID_ALIGNOF:
29750	      case RID_NOEXCEPT:
29751	      case RID_NEW:
29752	      case RID_DELETE:
29753	      case RID_THROW:
29754		return true;
29755
29756	     default:
29757		break;
29758	  }
29759	}
29760
29761      default:
29762	break;
29763    }
29764
29765  return false;
29766}
29767
29768/* Parse a primary expression within a constraint.  */
29769
29770static cp_expr
29771cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
29772{
29773  /* If this looks like a unary expression, parse it as such, but diagnose
29774     it as ill-formed; it requires parens.  */
29775  if (cp_parser_unary_constraint_requires_parens (parser))
29776    {
29777      cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
29778      cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
29779      return e;
29780    }
29781
29782  cp_lexer_save_tokens (parser->lexer);
29783  cp_id_kind idk;
29784  location_t loc = input_location;
29785  cp_expr expr = cp_parser_primary_expression (parser,
29786					       /*address_p=*/false,
29787					       /*cast_p=*/false,
29788					       /*template_arg_p=*/false,
29789					       &idk);
29790  expr.maybe_add_location_wrapper ();
29791
29792  primary_constraint_error pce = pce_ok;
29793  if (expr != error_mark_node)
29794    {
29795      /* The primary-expression could be part of an unenclosed non-logical
29796	 compound expression.  */
29797      pce = cp_parser_constraint_requires_parens (parser, lambda_p);
29798    }
29799  if (pce == pce_ok)
29800    {
29801      cp_lexer_commit_tokens (parser->lexer);
29802      return finish_constraint_primary_expr (expr);
29803    }
29804
29805  /* Retry the parse at a lower precedence. If that succeeds, diagnose the
29806     error, but return the expression as if it were valid.  */
29807  cp_lexer_rollback_tokens (parser->lexer);
29808  cp_parser_parse_tentatively (parser);
29809  if (pce == pce_maybe_operator)
29810    expr = cp_parser_assignment_expression (parser, NULL, false, false);
29811  else
29812    expr = cp_parser_simple_cast_expression (parser);
29813  if (cp_parser_parse_definitely (parser))
29814    {
29815      cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
29816      return expr;
29817    }
29818
29819  /* Otherwise, something has gone very wrong, and we can't generate a more
29820     meaningful diagnostic or recover.  */
29821  cp_parser_diagnose_ungrouped_constraint_plain (loc);
29822  return error_mark_node;
29823}
29824
29825/* Parse a constraint-logical-and-expression.
29826
29827     constraint-logical-and-expression:
29828       primary-expression
29829       constraint-logical-and-expression '&&' primary-expression  */
29830
29831static cp_expr
29832cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
29833{
29834  cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29835  while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
29836    {
29837      cp_token *op = cp_lexer_consume_token (parser->lexer);
29838      tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29839      lhs = finish_constraint_and_expr (op->location, lhs, rhs);
29840    }
29841  return lhs;
29842}
29843
29844/* Parse a constraint-logical-or-expression.
29845
29846     constraint-logical-or-expression:
29847       constraint-logical-and-expression
29848       constraint-logical-or-expression '||' constraint-logical-and-expression  */
29849
29850static cp_expr
29851cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
29852{
29853  cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29854  while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
29855    {
29856      cp_token *op = cp_lexer_consume_token (parser->lexer);
29857      cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29858      lhs = finish_constraint_or_expr (op->location, lhs, rhs);
29859    }
29860  return lhs;
29861}
29862
29863/* Parse the expression after a requires-clause. This has a different grammar
29864    than that in the concepts TS.  */
29865
29866static tree
29867cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
29868{
29869  processing_constraint_expression_sentinel parsing_constraint;
29870  ++processing_template_decl;
29871  cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
29872  --processing_template_decl;
29873  if (check_for_bare_parameter_packs (expr))
29874    expr = error_mark_node;
29875  return expr;
29876}
29877
29878/* Parse a expression after a requires clause.
29879
29880    constraint-expression:
29881      logical-or-expression
29882
29883   The required logical-or-expression must be a constant expression. Note
29884   that we don't check that the expression is constepxr here. We defer until
29885   we analyze constraints and then, we only check atomic constraints.  */
29886
29887static tree
29888cp_parser_constraint_expression (cp_parser *parser)
29889{
29890  processing_constraint_expression_sentinel parsing_constraint;
29891  ++processing_template_decl;
29892  cp_expr expr = cp_parser_binary_expression (parser, false, true,
29893					      PREC_NOT_OPERATOR, NULL);
29894  --processing_template_decl;
29895  if (check_for_bare_parameter_packs (expr))
29896    expr = error_mark_node;
29897  expr.maybe_add_location_wrapper ();
29898  return expr;
29899}
29900
29901/* Optionally parse a requires clause:
29902
29903      requires-clause:
29904	`requires` constraint-logical-or-expression.
29905   [ConceptsTS]
29906	`requires constraint-expression.
29907
29908   LAMBDA_P is true when the requires-clause is parsed before the
29909   parameter-list of a lambda-declarator.  */
29910
29911static tree
29912cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
29913{
29914  /* A requires clause is an unevaluated context.  */
29915  cp_unevaluated u;
29916
29917  cp_token *tok = cp_lexer_peek_token (parser->lexer);
29918  if (tok->keyword != RID_REQUIRES)
29919    {
29920      if (!flag_concepts && tok->type == CPP_NAME
29921	  && tok->u.value == ridpointers[RID_REQUIRES])
29922	{
29923	  error_at (cp_lexer_peek_token (parser->lexer)->location,
29924		    "%<requires%> only available with "
29925		    "%<-std=c++20%> or %<-fconcepts%>");
29926	  /* Parse and discard the requires-clause.  */
29927	  cp_lexer_consume_token (parser->lexer);
29928	  cp_parser_constraint_expression (parser);
29929	}
29930      return NULL_TREE;
29931    }
29932
29933  cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
29934  if (tok2->type == CPP_OPEN_BRACE)
29935    {
29936      /* An opening brace following the start of a requires-clause is
29937	 ill-formed; the user likely forgot the second `requires' that
29938	 would start a requires-expression.  */
29939      gcc_rich_location richloc (tok2->location);
29940      richloc.add_fixit_insert_after (tok->location, " requires");
29941      error_at (&richloc, "missing additional %<requires%> to start "
29942		"a requires-expression");
29943      /* Don't consume the `requires', so that it's reused as the start of a
29944	 requires-expression.  */
29945    }
29946  else
29947    cp_lexer_consume_token (parser->lexer);
29948
29949  if (!flag_concepts_ts)
29950    return cp_parser_requires_clause_expression (parser, lambda_p);
29951  else
29952    return cp_parser_constraint_expression (parser);
29953}
29954
29955/*---------------------------------------------------------------------------
29956                           Requires expressions
29957---------------------------------------------------------------------------*/
29958
29959/* Parse a requires expression
29960
29961   requirement-expression:
29962       'requires' requirement-parameter-list [opt] requirement-body */
29963
29964static tree
29965cp_parser_requires_expression (cp_parser *parser)
29966{
29967  gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
29968  location_t loc = cp_lexer_consume_token (parser->lexer)->location;
29969
29970  /* Avoid committing to outer tentative parse.  */
29971  tentative_firewall firewall (parser);
29972
29973  /* This is definitely a requires-expression.  */
29974  cp_parser_commit_to_tentative_parse (parser);
29975
29976  tree parms, reqs;
29977  {
29978    /* Local parameters are delared as variables within the scope
29979       of the expression.  They are not visible past the end of
29980       the expression.  Expressions within the requires-expression
29981       are unevaluated.  */
29982    struct scope_sentinel
29983    {
29984      scope_sentinel ()
29985      {
29986	++cp_unevaluated_operand;
29987	begin_scope (sk_function_parms, NULL_TREE);
29988	current_binding_level->requires_expression = true;
29989      }
29990
29991      ~scope_sentinel ()
29992      {
29993	pop_bindings_and_leave_scope ();
29994	--cp_unevaluated_operand;
29995      }
29996    } s;
29997
29998    /* Parse the optional parameter list. */
29999    if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30000      {
30001	parms = cp_parser_requirement_parameter_list (parser);
30002	if (parms == error_mark_node)
30003	  return error_mark_node;
30004      }
30005    else
30006      parms = NULL_TREE;
30007
30008    /* Parse the requirement body. */
30009    ++processing_template_decl;
30010    reqs = cp_parser_requirement_body (parser);
30011    --processing_template_decl;
30012    if (reqs == error_mark_node)
30013      return error_mark_node;
30014  }
30015
30016  /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
30017     the parm chain.  */
30018  grokparms (parms, &parms);
30019  loc = make_location (loc, loc, parser->lexer);
30020  tree expr = finish_requires_expr (loc, parms, reqs);
30021  if (!processing_template_decl)
30022    {
30023      /* Perform semantic processing now to diagnose any invalid types and
30024	 expressions.  */
30025      int saved_errorcount = errorcount;
30026      tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
30027      if (errorcount > saved_errorcount)
30028	return error_mark_node;
30029    }
30030  return expr;
30031}
30032
30033/* Parse a parameterized requirement.
30034
30035   requirement-parameter-list:
30036       '(' parameter-declaration-clause ')' */
30037
30038static tree
30039cp_parser_requirement_parameter_list (cp_parser *parser)
30040{
30041  matching_parens parens;
30042  if (!parens.require_open (parser))
30043    return error_mark_node;
30044
30045  tree parms = (cp_parser_parameter_declaration_clause
30046		(parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
30047
30048  if (!parens.require_close (parser))
30049    return error_mark_node;
30050
30051  /* Modify the declared parameters by removing their context
30052     so they don't refer to the enclosing scope and explicitly
30053     indicating that they are constraint variables. */
30054  for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
30055    {
30056      if (parm == void_list_node || parm == explicit_void_list_node)
30057	break;
30058      tree decl = TREE_VALUE (parm);
30059      if (decl != error_mark_node)
30060	{
30061	  DECL_CONTEXT (decl) = NULL_TREE;
30062	  CONSTRAINT_VAR_P (decl) = true;
30063	}
30064    }
30065
30066  return parms;
30067}
30068
30069/* Parse the body of a requirement.
30070
30071   requirement-body:
30072       '{' requirement-list '}' */
30073static tree
30074cp_parser_requirement_body (cp_parser *parser)
30075{
30076  matching_braces braces;
30077  if (!braces.require_open (parser))
30078    return error_mark_node;
30079
30080  tree reqs = cp_parser_requirement_seq (parser);
30081
30082  if (!braces.require_close (parser))
30083    return error_mark_node;
30084
30085  return reqs;
30086}
30087
30088/* Parse a sequence of requirements.
30089
30090   requirement-seq:
30091       requirement
30092       requirement-seq requirement */
30093
30094static tree
30095cp_parser_requirement_seq (cp_parser *parser)
30096{
30097  tree result = NULL_TREE;
30098  do
30099    {
30100      tree req = cp_parser_requirement (parser);
30101      if (req != error_mark_node)
30102	result = tree_cons (NULL_TREE, req, result);
30103    }
30104  while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
30105	 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
30106
30107  /* If there are no valid requirements, this is not a valid expression. */
30108  if (!result)
30109    return error_mark_node;
30110
30111  /* Reverse the order of requirements so they are analyzed in order. */
30112  return nreverse (result);
30113}
30114
30115/* Parse a syntactic requirement or type requirement.
30116
30117     requirement:
30118       simple-requirement
30119       compound-requirement
30120       type-requirement
30121       nested-requirement */
30122
30123static tree
30124cp_parser_requirement (cp_parser *parser)
30125{
30126  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30127    return cp_parser_compound_requirement (parser);
30128  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
30129    return cp_parser_type_requirement (parser);
30130  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
30131    return cp_parser_nested_requirement (parser);
30132  else
30133    return cp_parser_simple_requirement (parser);
30134}
30135
30136/* Parse a simple requirement.
30137
30138     simple-requirement:
30139       expression ';' */
30140
30141static tree
30142cp_parser_simple_requirement (cp_parser *parser)
30143{
30144  location_t start = cp_lexer_peek_token (parser->lexer)->location;
30145  cp_expr expr = cp_parser_expression (parser, NULL, false, false);
30146  if (expr == error_mark_node)
30147    cp_parser_skip_to_end_of_statement (parser);
30148
30149  cp_parser_consume_semicolon_at_end_of_statement (parser);
30150
30151  if (!expr || expr == error_mark_node)
30152    return error_mark_node;
30153
30154  /* Sometimes we don't get locations, so use the cached token location
30155     as a reasonable approximation.  */
30156  if (expr.get_location() == UNKNOWN_LOCATION)
30157    expr.set_location (start);
30158
30159  for (tree t = expr; ; )
30160    {
30161      if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
30162	  || TREE_CODE (t) == TRUTH_ORIF_EXPR)
30163	{
30164	  t = TREE_OPERAND (t, 0);
30165	  continue;
30166	}
30167      if (concept_check_p (t))
30168	{
30169	  gcc_rich_location richloc (get_start (start));
30170	  richloc.add_fixit_insert_before (start, "requires ");
30171	  warning_at (&richloc, OPT_Wmissing_requires, "testing "
30172		      "if a concept-id is a valid expression; add "
30173		      "%<requires%> to check satisfaction");
30174	}
30175      break;
30176    }
30177
30178  return finish_simple_requirement (expr.get_location (), expr);
30179}
30180
30181/* Parse a type requirement
30182
30183     type-requirement
30184         nested-name-specifier [opt] required-type-name ';'
30185
30186     required-type-name:
30187         type-name
30188         'template' [opt] simple-template-id  */
30189
30190static tree
30191cp_parser_type_requirement (cp_parser *parser)
30192{
30193  cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
30194  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30195
30196  // Save the scope before parsing name specifiers.
30197  tree saved_scope = parser->scope;
30198  tree saved_object_scope = parser->object_scope;
30199  tree saved_qualifying_scope = parser->qualifying_scope;
30200  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
30201  cp_parser_nested_name_specifier_opt (parser,
30202                                       /*typename_keyword_p=*/true,
30203                                       /*check_dependency_p=*/false,
30204                                       /*type_p=*/true,
30205                                       /*is_declaration=*/false);
30206
30207  tree type;
30208  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30209    {
30210      cp_lexer_consume_token (parser->lexer);
30211      type = cp_parser_template_id (parser,
30212                                    /*template_keyword_p=*/true,
30213                                    /*check_dependency=*/false,
30214                                    /*tag_type=*/none_type,
30215                                    /*is_declaration=*/false);
30216      type = make_typename_type (parser->scope, type, typename_type,
30217                                 /*complain=*/tf_error);
30218    }
30219  else
30220   type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
30221
30222  if (TREE_CODE (type) == TYPE_DECL)
30223    type = TREE_TYPE (type);
30224
30225  parser->scope = saved_scope;
30226  parser->object_scope = saved_object_scope;
30227  parser->qualifying_scope = saved_qualifying_scope;
30228
30229  if (type == error_mark_node)
30230    cp_parser_skip_to_end_of_statement (parser);
30231
30232  cp_parser_consume_semicolon_at_end_of_statement (parser);
30233
30234  if (type == error_mark_node)
30235    return error_mark_node;
30236
30237  loc = make_location (loc, start_tok->location, parser->lexer);
30238  return finish_type_requirement (loc, type);
30239}
30240
30241/* Parse a compound requirement
30242
30243     compound-requirement:
30244         '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
30245
30246static tree
30247cp_parser_compound_requirement (cp_parser *parser)
30248{
30249  /* Parse an expression enclosed in '{ }'s. */
30250  matching_braces braces;
30251  if (!braces.require_open (parser))
30252    return error_mark_node;
30253
30254  cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
30255
30256  tree expr = cp_parser_expression (parser, NULL, false, false);
30257  if (expr == error_mark_node)
30258    cp_parser_skip_to_closing_brace (parser);
30259
30260  if (!braces.require_close (parser))
30261    {
30262      cp_parser_skip_to_end_of_statement (parser);
30263      cp_parser_consume_semicolon_at_end_of_statement (parser);
30264      return error_mark_node;
30265    }
30266
30267  /* If the expression was invalid, skip the remainder of the requirement.  */
30268  if (!expr || expr == error_mark_node)
30269    {
30270      cp_parser_skip_to_end_of_statement (parser);
30271      cp_parser_consume_semicolon_at_end_of_statement (parser);
30272      return error_mark_node;
30273    }
30274
30275  /* Parse the optional noexcept. */
30276  bool noexcept_p = false;
30277  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
30278    {
30279      cp_lexer_consume_token (parser->lexer);
30280      noexcept_p = true;
30281    }
30282
30283  /* Parse the optional trailing return type. */
30284  tree type = NULL_TREE;
30285  if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
30286    {
30287      cp_lexer_consume_token (parser->lexer);
30288      cp_token *tok = cp_lexer_peek_token (parser->lexer);
30289
30290      bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
30291      parser->in_result_type_constraint_p = true;
30292      /* C++20 allows either a type-id or a type-constraint. Parsing
30293         a type-id will subsume the parsing for a type-constraint but
30294         allow for more syntactic forms (e.g., const C<T>*).  */
30295      type = cp_parser_trailing_type_id (parser);
30296      parser->in_result_type_constraint_p = saved_result_type_constraint_p;
30297      if (type == error_mark_node)
30298        return error_mark_node;
30299
30300      location_t type_loc = make_location (tok->location, tok->location,
30301					   parser->lexer);
30302
30303      /* Check that we haven't written something like 'const C<T>*'.  */
30304      if (type_uses_auto (type))
30305	{
30306	  if (!is_auto (type))
30307	    {
30308	      error_at (type_loc,
30309			"result type is not a plain type-constraint");
30310	      cp_parser_consume_semicolon_at_end_of_statement (parser);
30311	      return error_mark_node;
30312	    }
30313	}
30314      else if (!flag_concepts_ts)
30315	/* P1452R2 removed the trailing-return-type option.  */
30316	error_at (type_loc,
30317		  "return-type-requirement is not a type-constraint");
30318    }
30319
30320  location_t loc = make_location (expr_token->location,
30321				  braces.open_location (),
30322				  parser->lexer);
30323
30324  cp_parser_consume_semicolon_at_end_of_statement (parser);
30325
30326  if (expr == error_mark_node || type == error_mark_node)
30327    return error_mark_node;
30328
30329  return finish_compound_requirement (loc, expr, type, noexcept_p);
30330}
30331
30332/* Parse a nested requirement. This is the same as a requires clause.
30333
30334   nested-requirement:
30335     requires-clause */
30336
30337static tree
30338cp_parser_nested_requirement (cp_parser *parser)
30339{
30340  gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30341  cp_token *tok = cp_lexer_consume_token (parser->lexer);
30342  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30343  tree req = cp_parser_constraint_expression (parser);
30344  if (req == error_mark_node)
30345    cp_parser_skip_to_end_of_statement (parser);
30346  loc = make_location (loc, tok->location, parser->lexer);
30347  cp_parser_consume_semicolon_at_end_of_statement (parser);
30348  if (req == error_mark_node)
30349    return error_mark_node;
30350  return finish_nested_requirement (loc, req);
30351}
30352
30353/* Support Functions */
30354
30355/* Return the appropriate prefer_type argument for lookup_name based on
30356   tag_type.  */
30357
30358static inline LOOK_want
30359prefer_type_arg (tag_types tag_type)
30360{
30361  switch (tag_type)
30362    {
30363    case none_type:  return LOOK_want::NORMAL;	// No preference.
30364    case scope_type: return LOOK_want::TYPE_NAMESPACE;	// Type or namespace.
30365    default:         return LOOK_want::TYPE;	// Type only.
30366    }
30367}
30368
30369/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
30370   NAME should have one of the representations used for an
30371   id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
30372   is returned.  If PARSER->SCOPE is a dependent type, then a
30373   SCOPE_REF is returned.
30374
30375   If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
30376   returned; the name was already resolved when the TEMPLATE_ID_EXPR
30377   was formed.  Abstractly, such entities should not be passed to this
30378   function, because they do not need to be looked up, but it is
30379   simpler to check for this special case here, rather than at the
30380   call-sites.
30381
30382   In cases not explicitly covered above, this function returns a
30383   DECL, OVERLOAD, or baselink representing the result of the lookup.
30384   If there was no entity with the indicated NAME, the ERROR_MARK_NODE
30385   is returned.
30386
30387   If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
30388   (e.g., "struct") that was used.  In that case bindings that do not
30389   refer to types are ignored.
30390
30391   If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
30392   ignored.
30393
30394   If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
30395   are ignored.
30396
30397   If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
30398   types.
30399
30400   If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
30401   TREE_LIST of candidates if name-lookup results in an ambiguity, and
30402   NULL_TREE otherwise.  */
30403
30404static cp_expr
30405cp_parser_lookup_name (cp_parser *parser, tree name,
30406		       enum tag_types tag_type,
30407		       bool is_template,
30408		       bool is_namespace,
30409		       bool check_dependency,
30410		       tree *ambiguous_decls,
30411		       location_t name_location)
30412{
30413  tree decl;
30414  tree object_type = parser->context->object_type;
30415
30416  /* Assume that the lookup will be unambiguous.  */
30417  if (ambiguous_decls)
30418    *ambiguous_decls = NULL_TREE;
30419
30420  /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
30421     no longer valid.  Note that if we are parsing tentatively, and
30422     the parse fails, OBJECT_TYPE will be automatically restored.  */
30423  parser->context->object_type = NULL_TREE;
30424
30425  if (name == error_mark_node)
30426    return error_mark_node;
30427
30428  /* A template-id has already been resolved; there is no lookup to
30429     do.  */
30430  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
30431    return name;
30432  if (BASELINK_P (name))
30433    {
30434      gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
30435		  == TEMPLATE_ID_EXPR);
30436      return name;
30437    }
30438
30439  /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
30440     it should already have been checked to make sure that the name
30441     used matches the type being destroyed.  */
30442  if (TREE_CODE (name) == BIT_NOT_EXPR)
30443    {
30444      tree type;
30445
30446      /* Figure out to which type this destructor applies.  */
30447      if (parser->scope)
30448	type = parser->scope;
30449      else if (object_type)
30450	type = object_type;
30451      else
30452	type = current_class_type;
30453      /* If that's not a class type, there is no destructor.  */
30454      if (!type || !CLASS_TYPE_P (type))
30455	return error_mark_node;
30456
30457      /* In a non-static member function, check implicit this->.  */
30458      if (current_class_ref)
30459	return lookup_destructor (current_class_ref, parser->scope, name,
30460				  tf_warning_or_error);
30461
30462      if (CLASSTYPE_LAZY_DESTRUCTOR (type))
30463	lazily_declare_fn (sfk_destructor, type);
30464
30465      if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
30466	return dtor;
30467
30468      return error_mark_node;
30469    }
30470
30471  /* By this point, the NAME should be an ordinary identifier.  If
30472     the id-expression was a qualified name, the qualifying scope is
30473     stored in PARSER->SCOPE at this point.  */
30474  gcc_assert (identifier_p (name));
30475
30476  /* Perform the lookup.  */
30477  if (parser->scope)
30478    {
30479      bool dependent_p;
30480
30481      if (parser->scope == error_mark_node)
30482	return error_mark_node;
30483
30484      /* If the SCOPE is dependent, the lookup must be deferred until
30485	 the template is instantiated -- unless we are explicitly
30486	 looking up names in uninstantiated templates.  Even then, we
30487	 cannot look up the name if the scope is not a class type; it
30488	 might, for example, be a template type parameter.  */
30489      dependent_p = (TYPE_P (parser->scope)
30490		     && dependent_scope_p (parser->scope));
30491      if ((check_dependency || !CLASS_TYPE_P (parser->scope))
30492	  && dependent_p)
30493	/* Defer lookup.  */
30494	decl = error_mark_node;
30495      else
30496	{
30497	  tree pushed_scope = NULL_TREE;
30498
30499	  /* If PARSER->SCOPE is a dependent type, then it must be a
30500	     class type, and we must not be checking dependencies;
30501	     otherwise, we would have processed this lookup above.  So
30502	     that PARSER->SCOPE is not considered a dependent base by
30503	     lookup_member, we must enter the scope here.  */
30504	  if (dependent_p)
30505	    pushed_scope = push_scope (parser->scope);
30506
30507	  /* If the PARSER->SCOPE is a template specialization, it
30508	     may be instantiated during name lookup.  In that case,
30509	     errors may be issued.  Even if we rollback the current
30510	     tentative parse, those errors are valid.  */
30511	  decl = lookup_qualified_name (parser->scope, name,
30512					prefer_type_arg (tag_type),
30513					/*complain=*/true);
30514
30515	  /* 3.4.3.1: In a lookup in which the constructor is an acceptable
30516	     lookup result and the nested-name-specifier nominates a class C:
30517	       * if the name specified after the nested-name-specifier, when
30518	       looked up in C, is the injected-class-name of C (Clause 9), or
30519	       * if the name specified after the nested-name-specifier is the
30520	       same as the identifier or the simple-template-id's template-
30521	       name in the last component of the nested-name-specifier,
30522	     the name is instead considered to name the constructor of
30523	     class C. [ Note: for example, the constructor is not an
30524	     acceptable lookup result in an elaborated-type-specifier so
30525	     the constructor would not be used in place of the
30526	     injected-class-name. --end note ] Such a constructor name
30527	     shall be used only in the declarator-id of a declaration that
30528	     names a constructor or in a using-declaration.  */
30529	  if (tag_type == none_type
30530	      && DECL_SELF_REFERENCE_P (decl)
30531	      && same_type_p (DECL_CONTEXT (decl), parser->scope))
30532	    decl = lookup_qualified_name (parser->scope, ctor_identifier,
30533					  prefer_type_arg (tag_type),
30534					  /*complain=*/true);
30535
30536	  if (pushed_scope)
30537	    pop_scope (pushed_scope);
30538	}
30539
30540      /* If the scope is a dependent type and either we deferred lookup or
30541	 we did lookup but didn't find the name, rememeber the name.  */
30542      if (decl == error_mark_node && TYPE_P (parser->scope)
30543	  && dependent_type_p (parser->scope))
30544	{
30545	  if (tag_type)
30546	    {
30547	      tree type;
30548
30549	      /* The resolution to Core Issue 180 says that `struct
30550		 A::B' should be considered a type-name, even if `A'
30551		 is dependent.  */
30552	      type = make_typename_type (parser->scope, name, tag_type,
30553					 /*complain=*/tf_error);
30554	      if (type != error_mark_node)
30555		decl = TYPE_NAME (type);
30556	    }
30557	  else if (is_template
30558		   && (cp_parser_next_token_ends_template_argument_p (parser)
30559		       || cp_lexer_next_token_is (parser->lexer,
30560						  CPP_CLOSE_PAREN)))
30561	    decl = make_unbound_class_template (parser->scope,
30562						name, NULL_TREE,
30563						/*complain=*/tf_error);
30564	  else
30565	    decl = build_qualified_name (/*type=*/NULL_TREE,
30566					 parser->scope, name,
30567					 is_template);
30568	}
30569      parser->qualifying_scope = parser->scope;
30570      parser->object_scope = NULL_TREE;
30571    }
30572  else if (object_type)
30573    {
30574      bool dep = dependent_scope_p (object_type);
30575
30576      /* Look up the name in the scope of the OBJECT_TYPE, unless the
30577	 OBJECT_TYPE is not a class.  */
30578      if (CLASS_TYPE_P (object_type)
30579	  && !(dep && LAMBDA_TYPE_P (object_type)))
30580	/* If the OBJECT_TYPE is a template specialization, it may
30581	   be instantiated during name lookup.  In that case, errors
30582	   may be issued.  Even if we rollback the current tentative
30583	   parse, those errors are valid.  */
30584	decl = lookup_member (object_type,
30585			      name,
30586			      /*protect=*/0,
30587			      /*prefer_type=*/tag_type != none_type,
30588			      tf_warning_or_error);
30589      else
30590	decl = NULL_TREE;
30591
30592      if (!decl)
30593	/* Look it up in the enclosing context.  DR 141: When looking for a
30594	   template-name after -> or ., only consider class templates.  */
30595	decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30596			    /* DR 141: When looking in the
30597			       current enclosing context for a
30598			       template-name after -> or ., only
30599			       consider class templates.  */
30600			    : is_template ? LOOK_want::TYPE
30601			    : prefer_type_arg (tag_type));
30602
30603      /* If we know we're looking for a type (e.g. A in p->A::x),
30604	 mock up a typename.  */
30605      if (!decl && object_type && tag_type != none_type
30606	  && dependentish_scope_p (object_type))
30607	{
30608	  tree type = build_typename_type (object_type, name, name,
30609					   typename_type);
30610	  decl = TYPE_NAME (type);
30611	}
30612
30613      parser->object_scope = object_type;
30614      parser->qualifying_scope = NULL_TREE;
30615    }
30616  else
30617    {
30618      decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30619			  : prefer_type_arg (tag_type));
30620      parser->qualifying_scope = NULL_TREE;
30621      parser->object_scope = NULL_TREE;
30622    }
30623
30624  /* If the lookup failed, let our caller know.  */
30625  if (!decl || decl == error_mark_node)
30626    return error_mark_node;
30627
30628  /* If we have resolved the name of a member declaration, check to
30629     see if the declaration is accessible.  When the name resolves to
30630     set of overloaded functions, accessibility is checked when
30631     overload resolution is done.  If we have a TREE_LIST, then the lookup
30632     is either ambiguous or it found multiple injected-class-names, the
30633     accessibility of which is trivially satisfied.
30634
30635     During an explicit instantiation, access is not checked at all,
30636     as per [temp.explicit].  */
30637  if (DECL_P (decl))
30638    check_accessibility_of_qualified_id (decl, object_type, parser->scope,
30639					 tf_warning_or_error);
30640
30641  /* Pull out the template from an injected-class-name (or multiple).  */
30642  if (is_template)
30643    decl = maybe_get_template_decl_from_type_decl (decl);
30644
30645  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
30646  if (TREE_CODE (decl) == TREE_LIST)
30647    {
30648      if (ambiguous_decls)
30649	*ambiguous_decls = decl;
30650      /* The error message we have to print is too complicated for
30651	 cp_parser_error, so we incorporate its actions directly.  */
30652      if (!cp_parser_simulate_error (parser))
30653	{
30654	  error_at (name_location, "reference to %qD is ambiguous",
30655		    name);
30656	  print_candidates (decl);
30657	}
30658      return error_mark_node;
30659    }
30660
30661  gcc_assert (DECL_P (decl)
30662	      || TREE_CODE (decl) == OVERLOAD
30663	      || TREE_CODE (decl) == SCOPE_REF
30664	      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
30665	      || BASELINK_P (decl));
30666
30667  maybe_record_typedef_use (decl);
30668
30669  return cp_expr (decl, name_location);
30670}
30671
30672/* Like cp_parser_lookup_name, but for use in the typical case where
30673   CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
30674   IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
30675
30676static tree
30677cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
30678{
30679  return cp_parser_lookup_name (parser, name,
30680				none_type,
30681				/*is_template=*/false,
30682				/*is_namespace=*/false,
30683				/*check_dependency=*/true,
30684				/*ambiguous_decls=*/NULL,
30685				location);
30686}
30687
30688/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
30689   the current context, return the TYPE_DECL.  If TAG_NAME_P is
30690   true, the DECL indicates the class being defined in a class-head,
30691   or declared in an elaborated-type-specifier.
30692
30693   Otherwise, return DECL.  */
30694
30695static tree
30696cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
30697{
30698  /* If the TEMPLATE_DECL is being declared as part of a class-head,
30699     the translation from TEMPLATE_DECL to TYPE_DECL occurs:
30700
30701       struct A {
30702	 template <typename T> struct B;
30703       };
30704
30705       template <typename T> struct A::B {};
30706
30707     Similarly, in an elaborated-type-specifier:
30708
30709       namespace N { struct X{}; }
30710
30711       struct A {
30712	 template <typename T> friend struct N::X;
30713       };
30714
30715     However, if the DECL refers to a class type, and we are in
30716     the scope of the class, then the name lookup automatically
30717     finds the TYPE_DECL created by build_self_reference rather
30718     than a TEMPLATE_DECL.  For example, in:
30719
30720       template <class T> struct S {
30721	 S s;
30722       };
30723
30724     there is no need to handle such case.  */
30725
30726  if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
30727    return DECL_TEMPLATE_RESULT (decl);
30728
30729  return decl;
30730}
30731
30732/* If too many, or too few, template-parameter lists apply to the
30733   declarator, issue an error message.  Returns TRUE if all went well,
30734   and FALSE otherwise.  */
30735
30736static bool
30737cp_parser_check_declarator_template_parameters (cp_parser* parser,
30738						cp_declarator *declarator,
30739						location_t declarator_location)
30740{
30741  switch (declarator->kind)
30742    {
30743    case cdk_id:
30744      {
30745	unsigned num_templates = 0;
30746	tree scope = declarator->u.id.qualifying_scope;
30747	bool template_id_p = false;
30748
30749	if (scope)
30750	  num_templates = num_template_headers_for_class (scope);
30751	else if (TREE_CODE (declarator->u.id.unqualified_name)
30752		 == TEMPLATE_ID_EXPR)
30753	  {
30754	    /* If the DECLARATOR has the form `X<y>' then it uses one
30755	       additional level of template parameters.  */
30756	    ++num_templates;
30757	    template_id_p = true;
30758	  }
30759
30760	return cp_parser_check_template_parameters
30761	  (parser, num_templates, template_id_p, declarator_location,
30762	   declarator);
30763      }
30764
30765    case cdk_function:
30766    case cdk_array:
30767    case cdk_pointer:
30768    case cdk_reference:
30769    case cdk_ptrmem:
30770      return (cp_parser_check_declarator_template_parameters
30771	      (parser, declarator->declarator, declarator_location));
30772
30773    case cdk_decomp:
30774    case cdk_error:
30775      return true;
30776
30777    default:
30778      gcc_unreachable ();
30779    }
30780  return false;
30781}
30782
30783/* NUM_TEMPLATES were used in the current declaration.  If that is
30784   invalid, return FALSE and issue an error messages.  Otherwise,
30785   return TRUE.  If DECLARATOR is non-NULL, then we are checking a
30786   declarator and we can print more accurate diagnostics.  */
30787
30788static bool
30789cp_parser_check_template_parameters (cp_parser* parser,
30790				     unsigned num_templates,
30791				     bool template_id_p,
30792				     location_t location,
30793				     cp_declarator *declarator)
30794{
30795  /* If there are the same number of template classes and parameter
30796     lists, that's OK.  */
30797  if (parser->num_template_parameter_lists == num_templates)
30798    return true;
30799  /* If there are more, but only one more, and the name ends in an identifier,
30800     then we are declaring a primary template.  That's OK too.  */
30801  if (!template_id_p
30802      && parser->num_template_parameter_lists == num_templates + 1)
30803    return true;
30804
30805  if (cp_parser_simulate_error (parser))
30806    return false;
30807
30808  /* If there are more template classes than parameter lists, we have
30809     something like:
30810
30811       template <class T> void S<T>::R<T>::f ();  */
30812  if (parser->num_template_parameter_lists < num_templates)
30813    {
30814      if (declarator && !current_function_decl)
30815	error_at (location, "specializing member %<%T::%E%> "
30816		  "requires %<template<>%> syntax",
30817		  declarator->u.id.qualifying_scope,
30818		  declarator->u.id.unqualified_name);
30819      else if (declarator)
30820	error_at (location, "invalid declaration of %<%T::%E%>",
30821		  declarator->u.id.qualifying_scope,
30822		  declarator->u.id.unqualified_name);
30823      else
30824	error_at (location, "too few template-parameter-lists");
30825      return false;
30826    }
30827  /* Otherwise, there are too many template parameter lists.  We have
30828     something like:
30829
30830     template <class T> template <class U> void S::f();  */
30831  error_at (location, "too many template-parameter-lists");
30832  return false;
30833}
30834
30835/* Parse an optional `::' token indicating that the following name is
30836   from the global namespace.  If so, PARSER->SCOPE is set to the
30837   GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
30838   unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
30839   Returns the new value of PARSER->SCOPE, if the `::' token is
30840   present, and NULL_TREE otherwise.  */
30841
30842static tree
30843cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
30844{
30845  cp_token *token;
30846
30847  /* Peek at the next token.  */
30848  token = cp_lexer_peek_token (parser->lexer);
30849  /* If we're looking at a `::' token then we're starting from the
30850     global namespace, not our current location.  */
30851  if (token->type == CPP_SCOPE)
30852    {
30853      /* Consume the `::' token.  */
30854      cp_lexer_consume_token (parser->lexer);
30855      /* Set the SCOPE so that we know where to start the lookup.  */
30856      parser->scope = global_namespace;
30857      parser->qualifying_scope = global_namespace;
30858      parser->object_scope = NULL_TREE;
30859
30860      return parser->scope;
30861    }
30862  else if (!current_scope_valid_p)
30863    {
30864      parser->scope = NULL_TREE;
30865      parser->qualifying_scope = NULL_TREE;
30866      parser->object_scope = NULL_TREE;
30867    }
30868
30869  return NULL_TREE;
30870}
30871
30872/* Returns TRUE if the upcoming token sequence is the start of a
30873   constructor declarator or C++17 deduction guide.  If FRIEND_P is true, the
30874   declarator is preceded by the `friend' specifier.  The parser flags FLAGS
30875   is used to control type-specifier parsing.  */
30876
30877static bool
30878cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
30879				    bool friend_p)
30880{
30881  bool constructor_p;
30882  bool outside_class_specifier_p;
30883  tree nested_name_specifier;
30884  cp_token *next_token;
30885
30886  /* The common case is that this is not a constructor declarator, so
30887     try to avoid doing lots of work if at all possible.  It's not
30888     valid declare a constructor at function scope.  */
30889  if (parser->in_function_body)
30890    return false;
30891  /* And only certain tokens can begin a constructor declarator.  */
30892  next_token = cp_lexer_peek_token (parser->lexer);
30893  if (next_token->type != CPP_NAME
30894      && next_token->type != CPP_SCOPE
30895      && next_token->type != CPP_NESTED_NAME_SPECIFIER
30896      /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
30897	 declarator-id of a constructor or destructor.  */
30898      && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
30899    return false;
30900
30901  /* Parse tentatively; we are going to roll back all of the tokens
30902     consumed here.  */
30903  cp_parser_parse_tentatively (parser);
30904  /* Assume that we are looking at a constructor declarator.  */
30905  constructor_p = true;
30906
30907  /* Look for the optional `::' operator.  */
30908  cp_parser_global_scope_opt (parser,
30909			      /*current_scope_valid_p=*/false);
30910  /* Look for the nested-name-specifier.  */
30911  nested_name_specifier
30912    = (cp_parser_nested_name_specifier_opt (parser,
30913					    /*typename_keyword_p=*/false,
30914					    /*check_dependency_p=*/false,
30915					    /*type_p=*/false,
30916					    /*is_declaration=*/false));
30917
30918  /* Resolve the TYPENAME_TYPE, because the call above didn't do it.  */
30919  if (nested_name_specifier
30920      && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
30921    {
30922      tree s = resolve_typename_type (nested_name_specifier,
30923				      /*only_current_p=*/false);
30924      if (TREE_CODE (s) != TYPENAME_TYPE)
30925	nested_name_specifier = s;
30926    }
30927
30928  outside_class_specifier_p = (!at_class_scope_p ()
30929			       || !TYPE_BEING_DEFINED (current_class_type)
30930			       || friend_p);
30931
30932  /* Outside of a class-specifier, there must be a
30933     nested-name-specifier.  Except in C++17 mode, where we
30934     might be declaring a guiding declaration.  */
30935  if (!nested_name_specifier && outside_class_specifier_p
30936      && cxx_dialect < cxx17)
30937    constructor_p = false;
30938  else if (nested_name_specifier == error_mark_node)
30939    constructor_p = false;
30940
30941  /* If we have a class scope, this is easy; DR 147 says that S::S always
30942     names the constructor, and no other qualified name could.  */
30943  if (constructor_p && nested_name_specifier
30944      && CLASS_TYPE_P (nested_name_specifier))
30945    {
30946      tree id = cp_parser_unqualified_id (parser,
30947					  /*template_keyword_p=*/false,
30948					  /*check_dependency_p=*/false,
30949					  /*declarator_p=*/true,
30950					  /*optional_p=*/false);
30951      if (is_overloaded_fn (id))
30952	id = DECL_NAME (get_first_fn (id));
30953      if (!constructor_name_p (id, nested_name_specifier))
30954	constructor_p = false;
30955    }
30956  /* If we still think that this might be a constructor-declarator,
30957     look for a class-name.  */
30958  else if (constructor_p)
30959    {
30960      /* If we have:
30961
30962	   template <typename T> struct S {
30963	     S();
30964	   };
30965
30966	 we must recognize that the nested `S' names a class.  */
30967      if (cxx_dialect >= cxx17)
30968	cp_parser_parse_tentatively (parser);
30969
30970      tree type_decl;
30971      type_decl = cp_parser_class_name (parser,
30972					/*typename_keyword_p=*/false,
30973					/*template_keyword_p=*/false,
30974					none_type,
30975					/*check_dependency_p=*/false,
30976					/*class_head_p=*/false,
30977					/*is_declaration=*/false);
30978
30979      if (cxx_dialect >= cxx17
30980	  && !cp_parser_parse_definitely (parser))
30981	{
30982	  type_decl = NULL_TREE;
30983	  tree tmpl = cp_parser_template_name (parser,
30984					       /*template_keyword*/false,
30985					       /*check_dependency_p*/false,
30986					       /*is_declaration*/false,
30987					       none_type,
30988					       /*is_identifier*/NULL);
30989	  if (DECL_CLASS_TEMPLATE_P (tmpl)
30990	      || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30991	    /* It's a deduction guide, return true.  */;
30992	  else
30993	    cp_parser_simulate_error (parser);
30994	}
30995
30996      /* If there was no class-name, then this is not a constructor.
30997	 Otherwise, if we are in a class-specifier and we aren't
30998	 handling a friend declaration, check that its type matches
30999	 current_class_type (c++/38313).  Note: error_mark_node
31000	 is left alone for error recovery purposes.  */
31001      constructor_p = (!cp_parser_error_occurred (parser)
31002		       && (outside_class_specifier_p
31003			   || type_decl == NULL_TREE
31004			   || type_decl == error_mark_node
31005			   || same_type_p (current_class_type,
31006					   TREE_TYPE (type_decl))));
31007
31008      /* If we're still considering a constructor, we have to see a `(',
31009	 to begin the parameter-declaration-clause, followed by either a
31010	 `)', an `...', or a decl-specifier.  We need to check for a
31011	 type-specifier to avoid being fooled into thinking that:
31012
31013	   S (f) (int);
31014
31015	 is a constructor.  (It is actually a function named `f' that
31016	 takes one parameter (of type `int') and returns a value of type
31017	 `S'.  */
31018      if (constructor_p
31019	  && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31020	constructor_p = false;
31021
31022      if (constructor_p
31023	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31024	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
31025	  /* A parameter declaration begins with a decl-specifier,
31026	     which is either the "attribute" keyword, a storage class
31027	     specifier, or (usually) a type-specifier.  */
31028	  && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
31029	  /* GNU attributes can actually appear both at the start of
31030	     a parameter and parenthesized declarator.
31031	     S (__attribute__((unused)) int);
31032	     is a constructor, but
31033	     S (__attribute__((unused)) foo) (int);
31034	     is a function declaration. [[attribute]] can appear in the
31035	     first form too, but not in the second form.  */
31036	  && !cp_next_tokens_can_be_std_attribute_p (parser))
31037	{
31038	  tree type;
31039	  tree pushed_scope = NULL_TREE;
31040	  unsigned saved_num_template_parameter_lists;
31041
31042	  if (cp_parser_allow_gnu_extensions_p (parser)
31043	      && cp_next_tokens_can_be_gnu_attribute_p (parser))
31044	    {
31045	      unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
31046	      while (--n)
31047		cp_lexer_consume_token (parser->lexer);
31048	    }
31049
31050	  /* Names appearing in the type-specifier should be looked up
31051	     in the scope of the class.  */
31052	  if (current_class_type)
31053	    type = NULL_TREE;
31054	  else if (type_decl)
31055	    {
31056	      type = TREE_TYPE (type_decl);
31057	      if (TREE_CODE (type) == TYPENAME_TYPE)
31058		{
31059		  type = resolve_typename_type (type,
31060						/*only_current_p=*/false);
31061		  if (TREE_CODE (type) == TYPENAME_TYPE)
31062		    {
31063		      cp_parser_abort_tentative_parse (parser);
31064		      return false;
31065		    }
31066		}
31067	      pushed_scope = push_scope (type);
31068	    }
31069
31070	  /* Inside the constructor parameter list, surrounding
31071	     template-parameter-lists do not apply.  */
31072	  saved_num_template_parameter_lists
31073	    = parser->num_template_parameter_lists;
31074	  parser->num_template_parameter_lists = 0;
31075
31076	  /* Look for the type-specifier.  It's not optional, but its typename
31077	     might be.  Unless this is a friend declaration; we don't want to
31078	     treat
31079
31080	       friend S (T::fn)(int);
31081
31082	     as a constructor, but with P0634, we might assume a type when
31083	     looking for the type-specifier.  It is actually a function named
31084	     `T::fn' that takes one parameter (of type `int') and returns a
31085	     value of type `S'.  Constructors can be friends, but they must
31086	     use a qualified name.
31087
31088	     Parse with an empty set of declaration specifiers since we're
31089	     trying to match a decl-specifier-seq of the first parameter.
31090	     This must be non-null so that cp_parser_simple_type_specifier
31091	     will recognize a constrained placeholder type such as:
31092	     'C<int> auto' where C is a type concept.  */
31093	  cp_decl_specifier_seq ctor_specs;
31094	  clear_decl_specs (&ctor_specs);
31095	  cp_parser_type_specifier (parser,
31096				    (friend_p ? CP_PARSER_FLAGS_NONE
31097				     : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
31098				    /*decl_specs=*/&ctor_specs,
31099				    /*is_declarator=*/true,
31100				    /*declares_class_or_enum=*/NULL,
31101				    /*is_cv_qualifier=*/NULL);
31102
31103	  parser->num_template_parameter_lists
31104	    = saved_num_template_parameter_lists;
31105
31106	  /* Leave the scope of the class.  */
31107	  if (pushed_scope)
31108	    pop_scope (pushed_scope);
31109
31110	  constructor_p = !cp_parser_error_occurred (parser);
31111	}
31112    }
31113
31114  /* We did not really want to consume any tokens.  */
31115  cp_parser_abort_tentative_parse (parser);
31116
31117  return constructor_p;
31118}
31119
31120/* Parse the definition of the function given by the DECL_SPECIFIERS,
31121   ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
31122   they must be performed once we are in the scope of the function.
31123
31124   Returns the function defined.  */
31125
31126static tree
31127cp_parser_function_definition_from_specifiers_and_declarator
31128  (cp_parser* parser,
31129   cp_decl_specifier_seq *decl_specifiers,
31130   tree attributes,
31131   const cp_declarator *declarator)
31132{
31133  tree fn;
31134  bool success_p;
31135
31136  /* Begin the function-definition.  */
31137  success_p = start_function (decl_specifiers, declarator, attributes);
31138
31139  /* The things we're about to see are not directly qualified by any
31140     template headers we've seen thus far.  */
31141  reset_specialization ();
31142
31143  /* If there were names looked up in the decl-specifier-seq that we
31144     did not check, check them now.  We must wait until we are in the
31145     scope of the function to perform the checks, since the function
31146     might be a friend.  */
31147  perform_deferred_access_checks (tf_warning_or_error);
31148
31149  if (success_p)
31150    {
31151      cp_finalize_omp_declare_simd (parser, current_function_decl);
31152      parser->omp_declare_simd = NULL;
31153      cp_finalize_oacc_routine (parser, current_function_decl, true);
31154      parser->oacc_routine = NULL;
31155    }
31156
31157  if (!success_p)
31158    {
31159      /* Skip the entire function.  */
31160      cp_parser_skip_to_end_of_block_or_statement (parser);
31161      fn = error_mark_node;
31162    }
31163  else if (DECL_INITIAL (current_function_decl) != error_mark_node)
31164    {
31165      /* Seen already, skip it.  An error message has already been output.  */
31166      cp_parser_skip_to_end_of_block_or_statement (parser);
31167      fn = current_function_decl;
31168      current_function_decl = NULL_TREE;
31169      /* If this is a function from a class, pop the nested class.  */
31170      if (current_class_name)
31171	pop_nested_class ();
31172    }
31173  else
31174    {
31175      timevar_id_t tv;
31176      if (DECL_DECLARED_INLINE_P (current_function_decl))
31177        tv = TV_PARSE_INLINE;
31178      else
31179        tv = TV_PARSE_FUNC;
31180      timevar_push (tv);
31181      fn = cp_parser_function_definition_after_declarator (parser,
31182							 /*inline_p=*/false);
31183      timevar_pop (tv);
31184    }
31185
31186  return fn;
31187}
31188
31189/* Parse the part of a function-definition that follows the
31190   declarator.  INLINE_P is TRUE iff this function is an inline
31191   function defined within a class-specifier.
31192
31193   Returns the function defined.  */
31194
31195static tree
31196cp_parser_function_definition_after_declarator (cp_parser* parser,
31197						bool inline_p)
31198{
31199  tree fn;
31200  bool saved_in_unbraced_linkage_specification_p;
31201  bool saved_in_function_body;
31202  unsigned saved_num_template_parameter_lists;
31203  cp_token *token;
31204  bool fully_implicit_function_template_p
31205    = parser->fully_implicit_function_template_p;
31206  parser->fully_implicit_function_template_p = false;
31207  tree implicit_template_parms
31208    = parser->implicit_template_parms;
31209  parser->implicit_template_parms = 0;
31210  cp_binding_level* implicit_template_scope
31211    = parser->implicit_template_scope;
31212  parser->implicit_template_scope = 0;
31213
31214  saved_in_function_body = parser->in_function_body;
31215  parser->in_function_body = true;
31216  /* If the next token is `return', then the code may be trying to
31217     make use of the "named return value" extension that G++ used to
31218     support.  */
31219  token = cp_lexer_peek_token (parser->lexer);
31220  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
31221    {
31222      /* Consume the `return' keyword.  */
31223      cp_lexer_consume_token (parser->lexer);
31224      /* Look for the identifier that indicates what value is to be
31225	 returned.  */
31226      cp_parser_identifier (parser);
31227      /* Issue an error message.  */
31228      error_at (token->location,
31229		"named return values are no longer supported");
31230      /* Skip tokens until we reach the start of the function body.  */
31231      while (true)
31232	{
31233	  cp_token *token = cp_lexer_peek_token (parser->lexer);
31234	  if (token->type == CPP_OPEN_BRACE
31235	      || token->type == CPP_EOF
31236	      || token->type == CPP_PRAGMA_EOL)
31237	    break;
31238	  cp_lexer_consume_token (parser->lexer);
31239	}
31240    }
31241  /* The `extern' in `extern "C" void f () { ... }' does not apply to
31242     anything declared inside `f'.  */
31243  saved_in_unbraced_linkage_specification_p
31244    = parser->in_unbraced_linkage_specification_p;
31245  parser->in_unbraced_linkage_specification_p = false;
31246  /* Inside the function, surrounding template-parameter-lists do not
31247     apply.  */
31248  saved_num_template_parameter_lists
31249    = parser->num_template_parameter_lists;
31250  parser->num_template_parameter_lists = 0;
31251
31252  /* If the next token is `try', `__transaction_atomic', or
31253     `__transaction_relaxed`, then we are looking at either function-try-block
31254     or function-transaction-block.  Note that all of these include the
31255     function-body.  */
31256  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
31257    cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
31258  else if (cp_lexer_next_token_is_keyword (parser->lexer,
31259      RID_TRANSACTION_RELAXED))
31260    cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
31261  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31262    cp_parser_function_try_block (parser);
31263  else
31264    cp_parser_ctor_initializer_opt_and_function_body
31265      (parser, /*in_function_try_block=*/false);
31266
31267  /* Finish the function.  */
31268  fn = finish_function (inline_p);
31269
31270  if (modules_p ()
31271      && !inline_p
31272      && TYPE_P (DECL_CONTEXT (fn))
31273      && (DECL_DECLARED_INLINE_P (fn)
31274	  || processing_template_decl))
31275    set_defining_module (fn);
31276
31277  /* Generate code for it, if necessary.  */
31278  expand_or_defer_fn (fn);
31279  /* Restore the saved values.  */
31280  parser->in_unbraced_linkage_specification_p
31281    = saved_in_unbraced_linkage_specification_p;
31282  parser->num_template_parameter_lists
31283    = saved_num_template_parameter_lists;
31284  parser->in_function_body = saved_in_function_body;
31285
31286  parser->fully_implicit_function_template_p
31287    = fully_implicit_function_template_p;
31288  parser->implicit_template_parms
31289    = implicit_template_parms;
31290  parser->implicit_template_scope
31291    = implicit_template_scope;
31292
31293  if (parser->fully_implicit_function_template_p)
31294    finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
31295
31296  return fn;
31297}
31298
31299/* Parse a template-declaration body (following argument list).  */
31300
31301static void
31302cp_parser_template_declaration_after_parameters (cp_parser* parser,
31303						 tree parameter_list,
31304						 bool member_p)
31305{
31306  tree decl = NULL_TREE;
31307  bool friend_p = false;
31308
31309  /* We just processed one more parameter list.  */
31310  ++parser->num_template_parameter_lists;
31311
31312  /* Get the deferred access checks from the parameter list.  These
31313     will be checked once we know what is being declared, as for a
31314     member template the checks must be performed in the scope of the
31315     class containing the member.  */
31316  vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
31317
31318  /* Tentatively parse for a new template parameter list, which can either be
31319     the template keyword or a template introduction.  */
31320  if (cp_parser_template_declaration_after_export (parser, member_p))
31321    /* OK */;
31322  else if (cxx_dialect >= cxx11
31323	   && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
31324    decl = cp_parser_alias_declaration (parser);
31325  else if (cxx_dialect >= cxx20 /* Implies flag_concept.  */
31326           && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
31327           && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
31328    /* Allow 'concept bool' to be handled as per the TS.  */
31329    decl = cp_parser_concept_definition (parser);
31330  else
31331    {
31332      cp_token *token = cp_lexer_peek_token (parser->lexer);
31333      decl = cp_parser_single_declaration (parser,
31334					   checks,
31335					   member_p,
31336                                           /*explicit_specialization_p=*/false,
31337					   &friend_p);
31338
31339      /* If this is a member template declaration, let the front
31340	 end know.  */
31341      if (member_p && !friend_p && decl)
31342	{
31343	  if (TREE_CODE (decl) == TYPE_DECL)
31344	    cp_parser_check_access_in_redeclaration (decl, token->location);
31345
31346	  decl = finish_member_template_decl (decl);
31347	}
31348      else if (friend_p && decl
31349	       && DECL_DECLARES_TYPE_P (decl))
31350	make_friend_class (current_class_type, TREE_TYPE (decl),
31351			   /*complain=*/true);
31352    }
31353  /* We are done with the current parameter list.  */
31354  --parser->num_template_parameter_lists;
31355
31356  pop_deferring_access_checks ();
31357
31358  /* Finish up.  */
31359  finish_template_decl (parameter_list);
31360
31361  /* Check the template arguments for a literal operator template.  */
31362  if (decl
31363      && DECL_DECLARES_FUNCTION_P (decl)
31364      && UDLIT_OPER_P (DECL_NAME (decl)))
31365    {
31366      bool ok = true;
31367      if (parameter_list == NULL_TREE)
31368	ok = false;
31369      else
31370	{
31371	  int num_parms = TREE_VEC_LENGTH (parameter_list);
31372	  if (num_parms == 1)
31373	    {
31374	      tree parm_list = TREE_VEC_ELT (parameter_list, 0);
31375	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31376	      if (TREE_CODE (parm) != PARM_DECL)
31377		ok = false;
31378	      else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
31379		       && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31380		/* OK, C++20 string literal operator template.  We don't need
31381		   to warn in lower dialects here because we will have already
31382		   warned about the template parameter.  */;
31383	      else if (TREE_TYPE (parm) != char_type_node
31384		       || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31385		ok = false;
31386	    }
31387	  else if (num_parms == 2 && cxx_dialect >= cxx14)
31388	    {
31389	      tree parm_type = TREE_VEC_ELT (parameter_list, 0);
31390	      tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
31391	      tree parm_list = TREE_VEC_ELT (parameter_list, 1);
31392	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31393	      if (TREE_CODE (parm) != PARM_DECL
31394		  || TREE_TYPE (parm) != TREE_TYPE (type)
31395		  || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31396		ok = false;
31397	      else
31398		/* http://cplusplus.github.io/EWG/ewg-active.html#66  */
31399		pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
31400			 "ISO C++ did not adopt string literal operator templa"
31401			 "tes taking an argument pack of characters");
31402	    }
31403	  else
31404	    ok = false;
31405	}
31406      if (!ok)
31407	{
31408	  if (cxx_dialect > cxx17)
31409	    error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31410		      "template %qD has invalid parameter list; expected "
31411		      "non-type template parameter pack %<<char...>%> or "
31412		      "single non-type parameter of class type",
31413		      decl);
31414	  else
31415	    error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31416		      "template %qD has invalid parameter list; expected "
31417		      "non-type template parameter pack %<<char...>%>",
31418		      decl);
31419	}
31420    }
31421
31422  /* Register member declarations.  */
31423  if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
31424    finish_member_declaration (decl);
31425  /* If DECL is a function template, we must return to parse it later.
31426     (Even though there is no definition, there might be default
31427     arguments that need handling.)  */
31428  if (member_p && decl
31429      && DECL_DECLARES_FUNCTION_P (decl))
31430    vec_safe_push (unparsed_funs_with_definitions, decl);
31431}
31432
31433/* Parse a template introduction header for a template-declaration.  Returns
31434   false if tentative parse fails.  */
31435
31436static bool
31437cp_parser_template_introduction (cp_parser* parser, bool member_p)
31438{
31439  cp_parser_parse_tentatively (parser);
31440
31441  tree saved_scope = parser->scope;
31442  tree saved_object_scope = parser->object_scope;
31443  tree saved_qualifying_scope = parser->qualifying_scope;
31444  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31445
31446  cp_token *start_token = cp_lexer_peek_token (parser->lexer);
31447
31448  /* In classes don't parse valid unnamed bitfields as invalid
31449     template introductions.  */
31450  if (member_p)
31451    parser->colon_corrects_to_scope_p = false;
31452
31453  /* Look for the optional `::' operator.  */
31454  cp_parser_global_scope_opt (parser,
31455			      /*current_scope_valid_p=*/false);
31456  /* Look for the nested-name-specifier.  */
31457  cp_parser_nested_name_specifier_opt (parser,
31458				       /*typename_keyword_p=*/false,
31459				       /*check_dependency_p=*/true,
31460				       /*type_p=*/false,
31461				       /*is_declaration=*/false);
31462
31463  cp_token *token = cp_lexer_peek_token (parser->lexer);
31464  tree concept_name = cp_parser_identifier (parser);
31465
31466  /* Look up the concept for which we will be matching
31467     template parameters.  */
31468  tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
31469						 token->location);
31470  parser->scope = saved_scope;
31471  parser->object_scope = saved_object_scope;
31472  parser->qualifying_scope = saved_qualifying_scope;
31473  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31474
31475  if (concept_name == error_mark_node
31476      || (seen_error () && !concept_definition_p (tmpl_decl)))
31477    cp_parser_simulate_error (parser);
31478
31479  /* Look for opening brace for introduction.  */
31480  matching_braces braces;
31481  braces.require_open (parser);
31482  location_t open_loc = input_location;
31483
31484  if (!cp_parser_parse_definitely (parser))
31485    return false;
31486
31487  push_deferring_access_checks (dk_deferred);
31488
31489  /* Build vector of placeholder parameters and grab
31490     matching identifiers.  */
31491  tree introduction_list = cp_parser_introduction_list (parser);
31492
31493  /* Look for closing brace for introduction.  */
31494  if (!braces.require_close (parser))
31495    return true;
31496
31497  /* The introduction-list shall not be empty.  */
31498  int nargs = TREE_VEC_LENGTH (introduction_list);
31499  if (nargs == 0)
31500    {
31501      /* In cp_parser_introduction_list we have already issued an error.  */
31502      return true;
31503    }
31504
31505  if (tmpl_decl == error_mark_node)
31506    {
31507      cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
31508				   token->location);
31509      return true;
31510    }
31511
31512  /* Build and associate the constraint.  */
31513  location_t introduction_loc = make_location (open_loc,
31514					       start_token->location,
31515					       parser->lexer);
31516  tree parms = finish_template_introduction (tmpl_decl,
31517					     introduction_list,
31518					     introduction_loc);
31519  if (parms && parms != error_mark_node)
31520    {
31521      if (!flag_concepts_ts)
31522	pedwarn (introduction_loc, 0, "template-introductions"
31523		 " are not part of C++20 concepts; use %qs to enable",
31524		 "-fconcepts-ts");
31525
31526      cp_parser_template_declaration_after_parameters (parser, parms,
31527						       member_p);
31528      return true;
31529    }
31530
31531  if (parms == NULL_TREE)
31532    error_at (token->location, "no matching concept for template-introduction");
31533
31534  return true;
31535}
31536
31537/* Parse a normal template-declaration following the template keyword.  */
31538
31539static void
31540cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
31541{
31542  tree parameter_list;
31543  bool need_lang_pop;
31544  location_t location = input_location;
31545
31546  /* Look for the `<' token.  */
31547  if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
31548    return;
31549  if (at_class_scope_p () && current_function_decl)
31550    {
31551      /* 14.5.2.2 [temp.mem]
31552
31553         A local class shall not have member templates.  */
31554      error_at (location,
31555                "invalid declaration of member template in local class");
31556      cp_parser_skip_to_end_of_block_or_statement (parser);
31557      return;
31558    }
31559  /* [temp]
31560
31561     A template ... shall not have C linkage.  */
31562  if (current_lang_name == lang_name_c)
31563    {
31564      error_at (location, "template with C linkage");
31565      maybe_show_extern_c_location ();
31566      /* Give it C++ linkage to avoid confusing other parts of the
31567         front end.  */
31568      push_lang_context (lang_name_cplusplus);
31569      need_lang_pop = true;
31570    }
31571  else
31572    need_lang_pop = false;
31573
31574  /* We cannot perform access checks on the template parameter
31575     declarations until we know what is being declared, just as we
31576     cannot check the decl-specifier list.  */
31577  push_deferring_access_checks (dk_deferred);
31578
31579  /* If the next token is `>', then we have an invalid
31580     specialization.  Rather than complain about an invalid template
31581     parameter, issue an error message here.  */
31582  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
31583    {
31584      cp_parser_error (parser, "invalid explicit specialization");
31585      begin_specialization ();
31586      parameter_list = NULL_TREE;
31587    }
31588  else
31589    {
31590      /* Parse the template parameters.  */
31591      parameter_list = cp_parser_template_parameter_list (parser);
31592    }
31593
31594  /* Look for the `>'.  */
31595  cp_parser_require_end_of_template_parameter_list (parser);
31596
31597  /* Manage template requirements */
31598  if (flag_concepts)
31599  {
31600    tree reqs = get_shorthand_constraints (current_template_parms);
31601    if (tree treqs = cp_parser_requires_clause_opt (parser, false))
31602      reqs = combine_constraint_expressions (reqs, treqs);
31603    TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
31604  }
31605
31606  cp_parser_template_declaration_after_parameters (parser, parameter_list,
31607						   member_p);
31608
31609  /* For the erroneous case of a template with C linkage, we pushed an
31610     implicit C++ linkage scope; exit that scope now.  */
31611  if (need_lang_pop)
31612    pop_lang_context ();
31613}
31614
31615/* Parse a template-declaration, assuming that the `export' (and
31616   `extern') keywords, if present, has already been scanned.  MEMBER_P
31617   is as for cp_parser_template_declaration.  */
31618
31619static bool
31620cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
31621{
31622  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31623    {
31624      cp_lexer_consume_token (parser->lexer);
31625      cp_parser_explicit_template_declaration (parser, member_p);
31626      return true;
31627    }
31628  else if (flag_concepts)
31629    return cp_parser_template_introduction (parser, member_p);
31630
31631  return false;
31632}
31633
31634/* Perform the deferred access checks from a template-parameter-list.
31635   CHECKS is a TREE_LIST of access checks, as returned by
31636   get_deferred_access_checks.  */
31637
31638static void
31639cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
31640{
31641  ++processing_template_parmlist;
31642  perform_access_checks (checks, tf_warning_or_error);
31643  --processing_template_parmlist;
31644}
31645
31646/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
31647   `function-definition' sequence that follows a template header.
31648   If MEMBER_P is true, this declaration appears in a class scope.
31649
31650   Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
31651   *FRIEND_P is set to TRUE iff the declaration is a friend.  */
31652
31653static tree
31654cp_parser_single_declaration (cp_parser* parser,
31655			      vec<deferred_access_check, va_gc> *checks,
31656			      bool member_p,
31657                              bool explicit_specialization_p,
31658			      bool* friend_p)
31659{
31660  int declares_class_or_enum;
31661  tree decl = NULL_TREE;
31662  cp_decl_specifier_seq decl_specifiers;
31663  bool function_definition_p = false;
31664  cp_token *decl_spec_token_start;
31665
31666  /* This function is only used when processing a template
31667     declaration.  */
31668  gcc_assert (innermost_scope_kind () == sk_template_parms
31669	      || innermost_scope_kind () == sk_template_spec);
31670
31671  /* Defer access checks until we know what is being declared.  */
31672  push_deferring_access_checks (dk_deferred);
31673
31674  /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
31675     alternative.  */
31676  decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
31677  cp_parser_decl_specifier_seq (parser,
31678				(CP_PARSER_FLAGS_OPTIONAL
31679				 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
31680				&decl_specifiers,
31681				&declares_class_or_enum);
31682
31683  cp_omp_declare_simd_data odsd;
31684  if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
31685    cp_parser_handle_directive_omp_attributes (parser,
31686					       &decl_specifiers.attributes,
31687					       &odsd, true);
31688
31689  if (friend_p)
31690    *friend_p = cp_parser_friend_p (&decl_specifiers);
31691
31692  /* There are no template typedefs.  */
31693  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
31694    {
31695      error_at (decl_spec_token_start->location,
31696		"template declaration of %<typedef%>");
31697      decl = error_mark_node;
31698    }
31699
31700  /* Gather up the access checks that occurred the
31701     decl-specifier-seq.  */
31702  stop_deferring_access_checks ();
31703
31704  /* Check for the declaration of a template class.  */
31705  if (declares_class_or_enum)
31706    {
31707      if (cp_parser_declares_only_class_p (parser)
31708	  || (declares_class_or_enum & 2))
31709	{
31710	  decl = shadow_tag (&decl_specifiers);
31711
31712	  /* In this case:
31713
31714	       struct C {
31715		 friend template <typename T> struct A<T>::B;
31716	       };
31717
31718	     A<T>::B will be represented by a TYPENAME_TYPE, and
31719	     therefore not recognized by shadow_tag.  */
31720	  if (friend_p && *friend_p
31721	      && !decl
31722	      && decl_specifiers.type
31723	      && TYPE_P (decl_specifiers.type))
31724	    decl = decl_specifiers.type;
31725
31726	  if (decl && decl != error_mark_node)
31727	    decl = TYPE_NAME (decl);
31728	  else
31729	    decl = error_mark_node;
31730
31731	  /* If this is a declaration, but not a definition, associate
31732	     any constraints with the type declaration. Constraints
31733	     are associated with definitions in cp_parser_class_specifier.  */
31734	  if (declares_class_or_enum == 1)
31735	    associate_classtype_constraints (TREE_TYPE (decl));
31736
31737	  /* Perform access checks for template parameters.  */
31738	  cp_parser_perform_template_parameter_access_checks (checks);
31739
31740	  /* Give a helpful diagnostic for
31741	       template <class T> struct A { } a;
31742	     if we aren't already recovering from an error.  */
31743	  if (!cp_parser_declares_only_class_p (parser)
31744	      && !seen_error ())
31745	    {
31746	      error_at (cp_lexer_peek_token (parser->lexer)->location,
31747			"a class template declaration must not declare "
31748			"anything else");
31749	      cp_parser_skip_to_end_of_block_or_statement (parser);
31750	      goto out;
31751	    }
31752	}
31753    }
31754
31755  /* Complain about missing 'typename' or other invalid type names.  */
31756  if (!decl_specifiers.any_type_specifiers_p
31757      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
31758    {
31759      /* cp_parser_parse_and_diagnose_invalid_type_name calls
31760	 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
31761	 the rest of this declaration.  */
31762      decl = error_mark_node;
31763      goto out;
31764    }
31765
31766  /* If it's not a template class, try for a template function.  If
31767     the next token is a `;', then this declaration does not declare
31768     anything.  But, if there were errors in the decl-specifiers, then
31769     the error might well have come from an attempted class-specifier.
31770     In that case, there's no need to warn about a missing declarator.  */
31771  if (!decl
31772      && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
31773	  || decl_specifiers.type != error_mark_node))
31774    {
31775      int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
31776      /* We don't delay parsing for friends, though CWG 2510 may change
31777	 that.  */
31778      if (member_p && !(friend_p && *friend_p))
31779	flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
31780      decl = cp_parser_init_declarator (parser,
31781					flags,
31782				        &decl_specifiers,
31783				        checks,
31784				        /*function_definition_allowed_p=*/true,
31785				        member_p,
31786				        declares_class_or_enum,
31787				        &function_definition_p,
31788					NULL, NULL, NULL);
31789
31790    /* 7.1.1-1 [dcl.stc]
31791
31792       A storage-class-specifier shall not be specified in an explicit
31793       specialization...  */
31794    if (decl
31795        && explicit_specialization_p
31796        && decl_specifiers.storage_class != sc_none)
31797      {
31798        error_at (decl_spec_token_start->location,
31799		  "explicit template specialization cannot have a storage class");
31800        decl = error_mark_node;
31801      }
31802
31803    if (decl && VAR_P (decl))
31804      check_template_variable (decl);
31805    }
31806
31807  /* Look for a trailing `;' after the declaration.  */
31808  if (!function_definition_p
31809      && (decl == error_mark_node
31810	  || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
31811    cp_parser_skip_to_end_of_block_or_statement (parser);
31812
31813 out:
31814  pop_deferring_access_checks ();
31815
31816  /* Clear any current qualification; whatever comes next is the start
31817     of something new.  */
31818  parser->scope = NULL_TREE;
31819  parser->qualifying_scope = NULL_TREE;
31820  parser->object_scope = NULL_TREE;
31821
31822  cp_finalize_omp_declare_simd (parser, &odsd);
31823
31824  return decl;
31825}
31826
31827/* Parse a cast-expression that is not the operand of a unary "&".  */
31828
31829static cp_expr
31830cp_parser_simple_cast_expression (cp_parser *parser)
31831{
31832  return cp_parser_cast_expression (parser, /*address_p=*/false,
31833				    /*cast_p=*/false, /*decltype*/false, NULL);
31834}
31835
31836/* Parse a functional cast to TYPE.  Returns an expression
31837   representing the cast.  */
31838
31839static cp_expr
31840cp_parser_functional_cast (cp_parser* parser, tree type)
31841{
31842  vec<tree, va_gc> *vec;
31843  tree expression_list;
31844  cp_expr cast;
31845  bool nonconst_p;
31846
31847  location_t start_loc = input_location;
31848
31849  if (!type)
31850    type = error_mark_node;
31851
31852  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31853    {
31854      cp_lexer_set_source_position (parser->lexer);
31855      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
31856      expression_list = cp_parser_braced_list (parser, &nonconst_p);
31857      CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
31858      if (TREE_CODE (type) == TYPE_DECL)
31859	type = TREE_TYPE (type);
31860
31861      cast = finish_compound_literal (type, expression_list,
31862				      tf_warning_or_error, fcl_functional);
31863      /* Create a location of the form:
31864	    type_name{i, f}
31865	    ^~~~~~~~~~~~~~~
31866	 with caret == start at the start of the type name,
31867	 finishing at the closing brace.  */
31868      location_t combined_loc = make_location (start_loc, start_loc,
31869					       parser->lexer);
31870      cast.set_location (combined_loc);
31871      return cast;
31872   }
31873
31874
31875  vec = cp_parser_parenthesized_expression_list (parser, non_attr,
31876						 /*cast_p=*/true,
31877						 /*allow_expansion_p=*/true,
31878						 /*non_constant_p=*/NULL);
31879  if (vec == NULL)
31880    expression_list = error_mark_node;
31881  else
31882    {
31883      expression_list = build_tree_list_vec (vec);
31884      release_tree_vector (vec);
31885    }
31886
31887  /* Create a location of the form:
31888       float(i)
31889       ^~~~~~~~
31890     with caret == start at the start of the type name,
31891     finishing at the closing paren.  */
31892  location_t combined_loc = make_location (start_loc, start_loc,
31893					   parser->lexer);
31894  cast = build_functional_cast (combined_loc, type, expression_list,
31895                                tf_warning_or_error);
31896
31897  /* [expr.const]/1: In an integral constant expression "only type
31898     conversions to integral or enumeration type can be used".  */
31899  if (TREE_CODE (type) == TYPE_DECL)
31900    type = TREE_TYPE (type);
31901  if (cast != error_mark_node
31902      && !cast_valid_in_integral_constant_expression_p (type)
31903      && cp_parser_non_integral_constant_expression (parser,
31904						     NIC_CONSTRUCTOR))
31905    return error_mark_node;
31906
31907  return cast;
31908}
31909
31910/* Save the tokens that make up the body of a member function defined
31911   in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
31912   already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
31913   specifiers applied to the declaration.  Returns the FUNCTION_DECL
31914   for the member function.  */
31915
31916static tree
31917cp_parser_save_member_function_body (cp_parser* parser,
31918				     cp_decl_specifier_seq *decl_specifiers,
31919				     cp_declarator *declarator,
31920				     tree attributes)
31921{
31922  cp_token *first;
31923  cp_token *last;
31924  tree fn;
31925  bool function_try_block = false;
31926
31927  /* Create the FUNCTION_DECL.  */
31928  fn = grokmethod (decl_specifiers, declarator, attributes);
31929  cp_finalize_omp_declare_simd (parser, fn);
31930  cp_finalize_oacc_routine (parser, fn, true);
31931  /* If something went badly wrong, bail out now.  */
31932  if (fn == error_mark_node)
31933    {
31934      /* If there's a function-body, skip it.  */
31935      if (cp_parser_token_starts_function_definition_p
31936	  (cp_lexer_peek_token (parser->lexer)))
31937	cp_parser_skip_to_end_of_block_or_statement (parser);
31938      return error_mark_node;
31939    }
31940
31941  /* Remember it, if there are default args to post process.  */
31942  cp_parser_save_default_args (parser, fn);
31943
31944  /* Save away the tokens that make up the body of the
31945     function.  */
31946  first = parser->lexer->next_token;
31947
31948  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
31949    cp_lexer_consume_token (parser->lexer);
31950  else if (cp_lexer_next_token_is_keyword (parser->lexer,
31951					   RID_TRANSACTION_ATOMIC))
31952    {
31953      cp_lexer_consume_token (parser->lexer);
31954      /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
31955      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
31956	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
31957	  && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
31958	      || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
31959	  && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
31960	  && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
31961	{
31962	  cp_lexer_consume_token (parser->lexer);
31963	  cp_lexer_consume_token (parser->lexer);
31964	  cp_lexer_consume_token (parser->lexer);
31965	  cp_lexer_consume_token (parser->lexer);
31966	  cp_lexer_consume_token (parser->lexer);
31967	}
31968      else
31969	while (cp_next_tokens_can_be_gnu_attribute_p (parser)
31970	       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31971	  {
31972	    cp_lexer_consume_token (parser->lexer);
31973	    if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
31974	      break;
31975	  }
31976    }
31977
31978  /* Handle function try blocks.  */
31979  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31980    {
31981      cp_lexer_consume_token (parser->lexer);
31982      function_try_block = true;
31983    }
31984  /* We can have braced-init-list mem-initializers before the fn body.  */
31985  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31986    {
31987      cp_lexer_consume_token (parser->lexer);
31988      while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
31989	{
31990	  /* cache_group will stop after an un-nested { } pair, too.  */
31991	  if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
31992	    break;
31993
31994	  /* variadic mem-inits have ... after the ')'.  */
31995	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31996	    cp_lexer_consume_token (parser->lexer);
31997	}
31998    }
31999  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32000  /* Handle function try blocks.  */
32001  if (function_try_block)
32002    while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
32003      cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32004  last = parser->lexer->next_token;
32005
32006  /* Save away the inline definition; we will process it when the
32007     class is complete.  */
32008  DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
32009  DECL_PENDING_INLINE_P (fn) = 1;
32010
32011  /* We need to know that this was defined in the class, so that
32012     friend templates are handled correctly.  */
32013  DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
32014
32015  /* Add FN to the queue of functions to be parsed later.  */
32016  vec_safe_push (unparsed_funs_with_definitions, fn);
32017
32018  return fn;
32019}
32020
32021/* Save the tokens that make up the in-class initializer for a non-static
32022   data member.  Returns a DEFERRED_PARSE.  */
32023
32024static tree
32025cp_parser_save_nsdmi (cp_parser* parser)
32026{
32027  return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
32028}
32029
32030/* Parse a template-argument-list, as well as the trailing ">" (but
32031   not the opening "<").  See cp_parser_template_argument_list for the
32032   return value.  */
32033
32034static tree
32035cp_parser_enclosed_template_argument_list (cp_parser* parser)
32036{
32037  tree arguments;
32038  tree saved_scope;
32039  tree saved_qualifying_scope;
32040  tree saved_object_scope;
32041  bool saved_greater_than_is_operator_p;
32042
32043  /* [temp.names]
32044
32045     When parsing a template-id, the first non-nested `>' is taken as
32046     the end of the template-argument-list rather than a greater-than
32047     operator.  */
32048  saved_greater_than_is_operator_p
32049    = parser->greater_than_is_operator_p;
32050  parser->greater_than_is_operator_p = false;
32051  /* Parsing the argument list may modify SCOPE, so we save it
32052     here.  */
32053  saved_scope = parser->scope;
32054  saved_qualifying_scope = parser->qualifying_scope;
32055  saved_object_scope = parser->object_scope;
32056  /* We need to evaluate the template arguments, even though this
32057     template-id may be nested within a "sizeof".  */
32058  cp_evaluated ev;
32059  /* Parse the template-argument-list itself.  */
32060  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
32061      || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
32062    {
32063      arguments = make_tree_vec (0);
32064      SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
32065    }
32066  else
32067    arguments = cp_parser_template_argument_list (parser);
32068  /* Look for the `>' that ends the template-argument-list. If we find
32069     a '>>' instead, it's probably just a typo.  */
32070  if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
32071    {
32072      if (cxx_dialect != cxx98)
32073        {
32074          /* In C++0x, a `>>' in a template argument list or cast
32075             expression is considered to be two separate `>'
32076             tokens. So, change the current token to a `>', but don't
32077             consume it: it will be consumed later when the outer
32078             template argument list (or cast expression) is parsed.
32079             Note that this replacement of `>' for `>>' is necessary
32080             even if we are parsing tentatively: in the tentative
32081             case, after calling
32082             cp_parser_enclosed_template_argument_list we will always
32083             throw away all of the template arguments and the first
32084             closing `>', either because the template argument list
32085             was erroneous or because we are replacing those tokens
32086             with a CPP_TEMPLATE_ID token.  The second `>' (which will
32087             not have been thrown away) is needed either to close an
32088             outer template argument list or to complete a new-style
32089             cast.  */
32090	  cp_token *token = cp_lexer_peek_token (parser->lexer);
32091          token->type = CPP_GREATER;
32092        }
32093      else if (!saved_greater_than_is_operator_p)
32094	{
32095	  /* If we're in a nested template argument list, the '>>' has
32096	    to be a typo for '> >'. We emit the error message, but we
32097	    continue parsing and we push a '>' as next token, so that
32098	    the argument list will be parsed correctly.  Note that the
32099	    global source location is still on the token before the
32100	    '>>', so we need to say explicitly where we want it.  */
32101	  cp_token *token = cp_lexer_peek_token (parser->lexer);
32102	  gcc_rich_location richloc (token->location);
32103	  richloc.add_fixit_replace ("> >");
32104	  error_at (&richloc, "%<>>%> should be %<> >%> "
32105		    "within a nested template argument list");
32106
32107	  token->type = CPP_GREATER;
32108	}
32109      else
32110	{
32111	  /* If this is not a nested template argument list, the '>>'
32112	    is a typo for '>'. Emit an error message and continue.
32113	    Same deal about the token location, but here we can get it
32114	    right by consuming the '>>' before issuing the diagnostic.  */
32115	  cp_token *token = cp_lexer_consume_token (parser->lexer);
32116	  error_at (token->location,
32117		    "spurious %<>>%>, use %<>%> to terminate "
32118		    "a template argument list");
32119	}
32120    }
32121  else
32122    cp_parser_require_end_of_template_parameter_list (parser);
32123  /* The `>' token might be a greater-than operator again now.  */
32124  parser->greater_than_is_operator_p
32125    = saved_greater_than_is_operator_p;
32126  /* Restore the SAVED_SCOPE.  */
32127  parser->scope = saved_scope;
32128  parser->qualifying_scope = saved_qualifying_scope;
32129  parser->object_scope = saved_object_scope;
32130
32131  return arguments;
32132}
32133
32134/* MEMBER_FUNCTION is a member function, or a friend.  If default
32135   arguments, or the body of the function have not yet been parsed,
32136   parse them now.  */
32137
32138static void
32139cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
32140{
32141  timevar_push (TV_PARSE_INMETH);
32142  /* If this member is a template, get the underlying
32143     FUNCTION_DECL.  */
32144  if (DECL_FUNCTION_TEMPLATE_P (member_function))
32145    member_function = DECL_TEMPLATE_RESULT (member_function);
32146
32147  /* There should not be any class definitions in progress at this
32148     point; the bodies of members are only parsed outside of all class
32149     definitions.  */
32150  gcc_assert (parser->num_classes_being_defined == 0);
32151  /* While we're parsing the member functions we might encounter more
32152     classes.  We want to handle them right away, but we don't want
32153     them getting mixed up with functions that are currently in the
32154     queue.  */
32155  push_unparsed_function_queues (parser);
32156
32157  /* Make sure that any template parameters are in scope.  */
32158  maybe_begin_member_template_processing (member_function);
32159
32160  /* If the body of the function has not yet been parsed, parse it
32161     now.  Except if the tokens have been purged (PR c++/39751).  */
32162  if (DECL_PENDING_INLINE_P (member_function)
32163      && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
32164    {
32165      tree function_scope;
32166      cp_token_cache *tokens;
32167
32168      /* The function is no longer pending; we are processing it.  */
32169      tokens = DECL_PENDING_INLINE_INFO (member_function);
32170      DECL_PENDING_INLINE_INFO (member_function) = NULL;
32171      DECL_PENDING_INLINE_P (member_function) = 0;
32172
32173      /* If this is a local class, enter the scope of the containing
32174	 function.  */
32175      function_scope = current_function_decl;
32176      if (function_scope)
32177	push_function_context ();
32178
32179      /* Push the body of the function onto the lexer stack.  */
32180      cp_parser_push_lexer_for_tokens (parser, tokens);
32181
32182      /* Let the front end know that we going to be defining this
32183	 function.  */
32184      start_preparsed_function (member_function, NULL_TREE,
32185				SF_PRE_PARSED | SF_INCLASS_INLINE);
32186
32187      /* #pragma omp declare reduction needs special parsing.  */
32188      if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
32189	{
32190	  parser->lexer->in_pragma = true;
32191	  cp_parser_omp_declare_reduction_exprs (member_function, parser);
32192	  finish_function (/*inline_p=*/true);
32193	  cp_check_omp_declare_reduction (member_function);
32194	}
32195      else
32196	/* Now, parse the body of the function.  */
32197	cp_parser_function_definition_after_declarator (parser,
32198							/*inline_p=*/true);
32199
32200      /* Leave the scope of the containing function.  */
32201      if (function_scope)
32202	pop_function_context ();
32203      cp_parser_pop_lexer (parser);
32204    }
32205
32206  /* Remove any template parameters from the symbol table.  */
32207  maybe_end_member_template_processing ();
32208
32209  /* Restore the queue.  */
32210  pop_unparsed_function_queues (parser);
32211  timevar_pop (TV_PARSE_INMETH);
32212}
32213
32214/* If DECL contains any default args, remember it on the unparsed
32215   functions queue.  */
32216
32217static void
32218cp_parser_save_default_args (cp_parser* parser, tree decl)
32219{
32220  tree probe;
32221
32222  for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
32223       probe;
32224       probe = TREE_CHAIN (probe))
32225    if (TREE_PURPOSE (probe))
32226      {
32227	cp_default_arg_entry entry = {current_class_type, decl};
32228	vec_safe_push (unparsed_funs_with_default_args, entry);
32229	break;
32230      }
32231
32232  /* Remember if there is a noexcept-specifier to post process.  */
32233  tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
32234  if (UNPARSED_NOEXCEPT_SPEC_P (spec))
32235    vec_safe_push (unparsed_noexcepts, decl);
32236}
32237
32238/* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
32239   which is either a FIELD_DECL or PARM_DECL.  Parse it and return
32240   the result.  For a PARM_DECL, PARMTYPE is the corresponding type
32241   from the parameter-type-list.  */
32242
32243static tree
32244cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
32245				      tree default_arg, tree parmtype)
32246{
32247  cp_token_cache *tokens;
32248  tree parsed_arg;
32249  bool dummy;
32250
32251  if (default_arg == error_mark_node)
32252    return error_mark_node;
32253
32254  /* Push the saved tokens for the default argument onto the parser's
32255     lexer stack.  */
32256  tokens = DEFPARSE_TOKENS (default_arg);
32257  cp_parser_push_lexer_for_tokens (parser, tokens);
32258
32259  start_lambda_scope (decl);
32260
32261  /* Parse the default argument.  */
32262  parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
32263  if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
32264    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32265
32266  finish_lambda_scope ();
32267
32268  if (parsed_arg == error_mark_node)
32269    cp_parser_skip_to_end_of_statement (parser);
32270
32271  if (!processing_template_decl)
32272    {
32273      /* In a non-template class, check conversions now.  In a template,
32274	 we'll wait and instantiate these as needed.  */
32275      if (TREE_CODE (decl) == PARM_DECL)
32276	parsed_arg = check_default_argument (parmtype, parsed_arg,
32277					     tf_warning_or_error);
32278      else if (maybe_reject_flexarray_init (decl, parsed_arg))
32279	parsed_arg = error_mark_node;
32280      else
32281	parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
32282    }
32283
32284  /* If the token stream has not been completely used up, then
32285     there was extra junk after the end of the default
32286     argument.  */
32287  if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
32288    {
32289      if (TREE_CODE (decl) == PARM_DECL)
32290	cp_parser_error (parser, "expected %<,%>");
32291      else
32292	cp_parser_error (parser, "expected %<;%>");
32293    }
32294
32295  /* Revert to the main lexer.  */
32296  cp_parser_pop_lexer (parser);
32297
32298  return parsed_arg;
32299}
32300
32301/* FIELD is a non-static data member with an initializer which we saved for
32302   later; parse it now.  */
32303
32304static void
32305cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
32306{
32307  tree def;
32308
32309  maybe_begin_member_template_processing (field);
32310
32311  push_unparsed_function_queues (parser);
32312  def = cp_parser_late_parse_one_default_arg (parser, field,
32313					      DECL_INITIAL (field),
32314					      NULL_TREE);
32315  pop_unparsed_function_queues (parser);
32316
32317  maybe_end_member_template_processing ();
32318
32319  DECL_INITIAL (field) = def;
32320}
32321
32322/* FN is a FUNCTION_DECL which may contains a parameter with an
32323   unparsed DEFERRED_PARSE.  Parse the default args now.  This function
32324   assumes that the current scope is the scope in which the default
32325   argument should be processed.  */
32326
32327static void
32328cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
32329{
32330  unsigned char saved_local_variables_forbidden_p;
32331
32332  /* While we're parsing the default args, we might (due to the
32333     statement expression extension) encounter more classes.  We want
32334     to handle them right away, but we don't want them getting mixed
32335     up with default args that are currently in the queue.  */
32336  push_unparsed_function_queues (parser);
32337
32338  /* Local variable names (and the `this' keyword) may not appear
32339     in a default argument.  */
32340  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
32341  parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
32342
32343  push_defarg_context (fn);
32344
32345  begin_scope (sk_function_parms, fn);
32346
32347  /* Gather the PARM_DECLs into a vec so we can keep track of them when
32348     pushdecl clears DECL_CHAIN.  */
32349  releasing_vec parms;
32350  for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
32351       parmdecl = DECL_CHAIN (parmdecl))
32352    vec_safe_push (parms, parmdecl);
32353
32354  tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
32355  for (int i = 0;
32356       parm && parm != void_list_node;
32357       parm = TREE_CHAIN (parm),
32358	 ++i)
32359    {
32360      tree default_arg = TREE_PURPOSE (parm);
32361      tree parsed_arg;
32362
32363      tree parmdecl = parms[i];
32364      pushdecl (parmdecl);
32365
32366      if (!default_arg)
32367	continue;
32368
32369      if (TREE_CODE (default_arg) != DEFERRED_PARSE)
32370	/* This can happen for a friend declaration for a function
32371	   already declared with default arguments.  */
32372	continue;
32373
32374      parsed_arg
32375	= cp_parser_late_parse_one_default_arg (parser, parmdecl,
32376						default_arg,
32377						TREE_VALUE (parm));
32378      TREE_PURPOSE (parm) = parsed_arg;
32379
32380      /* Update any instantiations we've already created.  */
32381      for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
32382	TREE_PURPOSE (copy) = parsed_arg;
32383    }
32384
32385  pop_bindings_and_leave_scope ();
32386
32387  /* Restore DECL_CHAINs after clobbering by pushdecl.  */
32388  parm = NULL_TREE;
32389  for (int i = parms->length () - 1; i >= 0; --i)
32390    {
32391      DECL_CHAIN (parms[i]) = parm;
32392      parm = parms[i];
32393    }
32394
32395  pop_defarg_context ();
32396
32397  /* Make sure no default arg is missing.  */
32398  check_default_args (fn);
32399
32400  /* Restore the state of local_variables_forbidden_p.  */
32401  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
32402
32403  /* Restore the queue.  */
32404  pop_unparsed_function_queues (parser);
32405}
32406
32407/* Subroutine of cp_parser_sizeof_operand, for handling C++11
32408
32409     sizeof ... ( identifier )
32410
32411   where the 'sizeof' token has already been consumed.  */
32412
32413static tree
32414cp_parser_sizeof_pack (cp_parser *parser)
32415{
32416  /* Consume the `...'.  */
32417  cp_lexer_consume_token (parser->lexer);
32418  maybe_warn_variadic_templates ();
32419
32420  matching_parens parens;
32421  bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
32422  if (paren)
32423    parens.consume_open (parser);
32424  else
32425    permerror (cp_lexer_peek_token (parser->lexer)->location,
32426	       "%<sizeof...%> argument must be surrounded by parentheses");
32427
32428  cp_token *token = cp_lexer_peek_token (parser->lexer);
32429  tree name = cp_parser_identifier (parser);
32430  if (name == error_mark_node)
32431    return error_mark_node;
32432  /* The name is not qualified.  */
32433  parser->scope = NULL_TREE;
32434  parser->qualifying_scope = NULL_TREE;
32435  parser->object_scope = NULL_TREE;
32436  tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
32437  if (expr == error_mark_node)
32438    cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
32439				 token->location);
32440  if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
32441    expr = TREE_TYPE (expr);
32442  else if (TREE_CODE (expr) == CONST_DECL)
32443    expr = DECL_INITIAL (expr);
32444  expr = make_pack_expansion (expr);
32445  PACK_EXPANSION_SIZEOF_P (expr) = true;
32446
32447  if (paren)
32448    parens.require_close (parser);
32449
32450  return expr;
32451}
32452
32453/* Parse the operand of `sizeof' (or a similar operator).  Returns
32454   either a TYPE or an expression, depending on the form of the
32455   input.  The KEYWORD indicates which kind of expression we have
32456   encountered.  */
32457
32458static tree
32459cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
32460{
32461  tree expr = NULL_TREE;
32462  const char *saved_message;
32463  const char *saved_message_arg;
32464  bool saved_integral_constant_expression_p;
32465  bool saved_non_integral_constant_expression_p;
32466
32467  /* If it's a `...', then we are computing the length of a parameter
32468     pack.  */
32469  if (keyword == RID_SIZEOF
32470      && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32471    return cp_parser_sizeof_pack (parser);
32472
32473  /* Types cannot be defined in a `sizeof' expression.  Save away the
32474     old message.  */
32475  saved_message = parser->type_definition_forbidden_message;
32476  saved_message_arg = parser->type_definition_forbidden_message_arg;
32477  parser->type_definition_forbidden_message
32478    = G_("types may not be defined in %qs expressions");
32479  parser->type_definition_forbidden_message_arg
32480    = IDENTIFIER_POINTER (ridpointers[keyword]);
32481
32482  /* The restrictions on constant-expressions do not apply inside
32483     sizeof expressions.  */
32484  saved_integral_constant_expression_p
32485    = parser->integral_constant_expression_p;
32486  saved_non_integral_constant_expression_p
32487    = parser->non_integral_constant_expression_p;
32488  parser->integral_constant_expression_p = false;
32489
32490  auto cleanup = make_temp_override
32491    (parser->auto_is_implicit_function_template_parm_p, false);
32492
32493  /* Do not actually evaluate the expression.  */
32494  ++cp_unevaluated_operand;
32495  ++c_inhibit_evaluation_warnings;
32496  /* If it's a `(', then we might be looking at the type-id
32497     construction.  */
32498  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32499    {
32500      tree type = NULL_TREE;
32501
32502      tentative_firewall firewall (parser);
32503
32504      /* We can't be sure yet whether we're looking at a type-id or an
32505	 expression.  */
32506      cp_parser_parse_tentatively (parser);
32507
32508      matching_parens parens;
32509      parens.consume_open (parser);
32510
32511      /* Note: as a GNU Extension, compound literals are considered
32512	 postfix-expressions as they are in C99, so they are valid
32513	 arguments to sizeof.  See comment in cp_parser_cast_expression
32514	 for details.  */
32515      if (cp_parser_compound_literal_p (parser))
32516	cp_parser_simulate_error (parser);
32517      else
32518	{
32519	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
32520	  parser->in_type_id_in_expr_p = true;
32521	  /* Look for the type-id.  */
32522	  type = cp_parser_type_id (parser);
32523	  /* Look for the closing `)'.  */
32524	  parens.require_close (parser);
32525	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
32526	}
32527
32528      /* If all went well, then we're done.  */
32529      if (cp_parser_parse_definitely (parser))
32530	expr = type;
32531      else
32532	{
32533	  /* Commit to the tentative_firewall so we get syntax errors.  */
32534	  cp_parser_commit_to_tentative_parse (parser);
32535
32536	  expr = cp_parser_unary_expression (parser);
32537	}
32538    }
32539  else
32540    expr = cp_parser_unary_expression (parser);
32541
32542  /* Go back to evaluating expressions.  */
32543  --cp_unevaluated_operand;
32544  --c_inhibit_evaluation_warnings;
32545
32546  /* And restore the old one.  */
32547  parser->type_definition_forbidden_message = saved_message;
32548  parser->type_definition_forbidden_message_arg = saved_message_arg;
32549  parser->integral_constant_expression_p
32550    = saved_integral_constant_expression_p;
32551  parser->non_integral_constant_expression_p
32552    = saved_non_integral_constant_expression_p;
32553
32554  return expr;
32555}
32556
32557/* If the current declaration has no declarator, return true.  */
32558
32559static bool
32560cp_parser_declares_only_class_p (cp_parser *parser)
32561{
32562  /* If the next token is a `;' or a `,' then there is no
32563     declarator.  */
32564  return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32565	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
32566}
32567
32568/* Update the DECL_SPECS to reflect the storage class indicated by
32569   KEYWORD.  */
32570
32571static void
32572cp_parser_set_storage_class (cp_parser *parser,
32573			     cp_decl_specifier_seq *decl_specs,
32574			     enum rid keyword,
32575			     cp_token *token)
32576{
32577  cp_storage_class storage_class;
32578
32579  if (parser->in_unbraced_linkage_specification_p)
32580    {
32581      error_at (token->location, "invalid use of %qD in linkage specification",
32582		ridpointers[keyword]);
32583      return;
32584    }
32585  else if (decl_specs->storage_class != sc_none)
32586    {
32587      decl_specs->conflicting_specifiers_p = true;
32588      return;
32589    }
32590
32591  if ((keyword == RID_EXTERN || keyword == RID_STATIC)
32592      && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
32593      && decl_specs->gnu_thread_keyword_p)
32594    {
32595      pedwarn (decl_specs->locations[ds_thread], 0,
32596		"%<__thread%> before %qD", ridpointers[keyword]);
32597    }
32598
32599  switch (keyword)
32600    {
32601    case RID_AUTO:
32602      storage_class = sc_auto;
32603      break;
32604    case RID_REGISTER:
32605      storage_class = sc_register;
32606      break;
32607    case RID_STATIC:
32608      storage_class = sc_static;
32609      break;
32610    case RID_EXTERN:
32611      storage_class = sc_extern;
32612      break;
32613    case RID_MUTABLE:
32614      storage_class = sc_mutable;
32615      break;
32616    default:
32617      gcc_unreachable ();
32618    }
32619  decl_specs->storage_class = storage_class;
32620  set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
32621
32622  /* A storage class specifier cannot be applied alongside a typedef
32623     specifier. If there is a typedef specifier present then set
32624     conflicting_specifiers_p which will trigger an error later
32625     on in grokdeclarator. */
32626  if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
32627    decl_specs->conflicting_specifiers_p = true;
32628}
32629
32630/* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
32631   is true, the type is a class or enum definition.  */
32632
32633static void
32634cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
32635			      tree type_spec,
32636			      cp_token *token,
32637			      bool type_definition_p)
32638{
32639  decl_specs->any_specifiers_p = true;
32640
32641  /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
32642     wchar_t (with, for example, in "typedef int wchar_t;") we remember that
32643     this is what happened.  In system headers, we ignore these
32644     declarations so that G++ can work with system headers that are not
32645     C++-safe.  */
32646  if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
32647      && !type_definition_p
32648      && (type_spec == boolean_type_node
32649	  || type_spec == char8_type_node
32650	  || type_spec == char16_type_node
32651	  || type_spec == char32_type_node
32652	  || type_spec == wchar_type_node)
32653      && (decl_specs->type
32654	  || decl_spec_seq_has_spec_p (decl_specs, ds_long)
32655	  || decl_spec_seq_has_spec_p (decl_specs, ds_short)
32656	  || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
32657	  || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
32658    {
32659      decl_specs->redefined_builtin_type = type_spec;
32660      set_and_check_decl_spec_loc (decl_specs,
32661				   ds_redefined_builtin_type_spec,
32662				   token);
32663      if (!decl_specs->type)
32664	{
32665	  decl_specs->type = type_spec;
32666	  decl_specs->type_definition_p = false;
32667	  set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
32668	}
32669    }
32670  else if (decl_specs->type)
32671    decl_specs->multiple_types_p = true;
32672  else
32673    {
32674      decl_specs->type = type_spec;
32675      decl_specs->type_definition_p = type_definition_p;
32676      decl_specs->redefined_builtin_type = NULL_TREE;
32677      set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
32678    }
32679}
32680
32681/* True iff TOKEN is the GNU keyword __thread.  */
32682
32683static bool
32684token_is__thread (cp_token *token)
32685{
32686  gcc_assert (token->keyword == RID_THREAD);
32687  return id_equal (token->u.value, "__thread");
32688}
32689
32690/* Set the location for a declarator specifier and check if it is
32691   duplicated.
32692
32693   DECL_SPECS is the sequence of declarator specifiers onto which to
32694   set the location.
32695
32696   DS is the single declarator specifier to set which location  is to
32697   be set onto the existing sequence of declarators.
32698
32699   LOCATION is the location for the declarator specifier to
32700   consider.  */
32701
32702static void
32703set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
32704			     cp_decl_spec ds, cp_token *token)
32705{
32706  gcc_assert (ds < ds_last);
32707
32708  if (decl_specs == NULL)
32709    return;
32710
32711  location_t location = token->location;
32712
32713  if (decl_specs->locations[ds] == 0)
32714    {
32715      decl_specs->locations[ds] = location;
32716      if (ds == ds_thread)
32717	decl_specs->gnu_thread_keyword_p = token_is__thread (token);
32718    }
32719  else
32720    {
32721      if (ds == ds_long)
32722	{
32723	  if (decl_specs->locations[ds_long_long] != 0)
32724	    error_at (location,
32725		      "%<long long long%> is too long for GCC");
32726	  else
32727	    {
32728	      decl_specs->locations[ds_long_long] = location;
32729	      pedwarn_cxx98 (location,
32730			     OPT_Wlong_long,
32731			     "ISO C++ 1998 does not support %<long long%>");
32732	    }
32733	}
32734      else if (ds == ds_thread)
32735	{
32736	  bool gnu = token_is__thread (token);
32737	  gcc_rich_location richloc (location);
32738	  if (gnu != decl_specs->gnu_thread_keyword_p)
32739	    {
32740	      richloc.add_range (decl_specs->locations[ds_thread]);
32741	      error_at (&richloc,
32742			"both %<__thread%> and %<thread_local%> specified");
32743	    }
32744	  else
32745	    {
32746	      richloc.add_fixit_remove ();
32747	      error_at (&richloc, "duplicate %qD", token->u.value);
32748	    }
32749	}
32750      else
32751	{
32752	  static const char *const decl_spec_names[] = {
32753	    "signed",
32754	    "unsigned",
32755	    "short",
32756	    "long",
32757	    "const",
32758	    "volatile",
32759	    "restrict",
32760	    "inline",
32761	    "virtual",
32762	    "explicit",
32763	    "friend",
32764	    "typedef",
32765	    "using",
32766	    "constexpr",
32767	    "__complex",
32768	    "constinit",
32769	    "consteval"
32770	  };
32771	  gcc_rich_location richloc (location);
32772	  richloc.add_fixit_remove ();
32773	  error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
32774	}
32775    }
32776}
32777
32778/* Return true iff the declarator specifier DS is present in the
32779   sequence of declarator specifiers DECL_SPECS.  */
32780
32781bool
32782decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
32783			  cp_decl_spec ds)
32784{
32785  gcc_assert (ds < ds_last);
32786
32787  if (decl_specs == NULL)
32788    return false;
32789
32790  return decl_specs->locations[ds] != 0;
32791}
32792
32793/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
32794   Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
32795
32796static bool
32797cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
32798{
32799  return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
32800}
32801
32802/* Issue an error message indicating that TOKEN_DESC was expected.
32803   If KEYWORD is true, it indicated this function is called by
32804   cp_parser_require_keword and the required token can only be
32805   a indicated keyword.
32806
32807   If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32808   within any error as the location of an "opening" token matching
32809   the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32810   RT_CLOSE_PAREN).  */
32811
32812static void
32813cp_parser_required_error (cp_parser *parser,
32814			  required_token token_desc,
32815			  bool keyword,
32816			  location_t matching_location)
32817{
32818  if (cp_parser_simulate_error (parser))
32819    return;
32820
32821  const char *gmsgid = NULL;
32822  switch (token_desc)
32823    {
32824      case RT_NEW:
32825	gmsgid = G_("expected %<new%>");
32826	break;
32827      case RT_DELETE:
32828	gmsgid = G_("expected %<delete%>");
32829	break;
32830      case RT_RETURN:
32831	gmsgid = G_("expected %<return%>");
32832	break;
32833      case RT_WHILE:
32834	gmsgid = G_("expected %<while%>");
32835	break;
32836      case RT_EXTERN:
32837	gmsgid = G_("expected %<extern%>");
32838	break;
32839      case RT_STATIC_ASSERT:
32840	gmsgid = G_("expected %<static_assert%>");
32841	break;
32842      case RT_DECLTYPE:
32843	gmsgid = G_("expected %<decltype%>");
32844	break;
32845      case RT_OPERATOR:
32846	gmsgid = G_("expected %<operator%>");
32847	break;
32848      case RT_CLASS:
32849	gmsgid = G_("expected %<class%>");
32850	break;
32851      case RT_TEMPLATE:
32852	gmsgid = G_("expected %<template%>");
32853	break;
32854      case RT_NAMESPACE:
32855	gmsgid = G_("expected %<namespace%>");
32856	break;
32857      case RT_USING:
32858	gmsgid = G_("expected %<using%>");
32859	break;
32860      case RT_ASM:
32861	gmsgid = G_("expected %<asm%>");
32862	break;
32863      case RT_TRY:
32864	gmsgid = G_("expected %<try%>");
32865	break;
32866      case RT_CATCH:
32867	gmsgid = G_("expected %<catch%>");
32868	break;
32869      case RT_THROW:
32870	gmsgid = G_("expected %<throw%>");
32871	break;
32872      case RT_AUTO:
32873        gmsgid = G_("expected %<auto%>");
32874        break;
32875      case RT_LABEL:
32876	gmsgid = G_("expected %<__label__%>");
32877	break;
32878      case RT_AT_TRY:
32879	gmsgid = G_("expected %<@try%>");
32880	break;
32881      case RT_AT_SYNCHRONIZED:
32882	gmsgid = G_("expected %<@synchronized%>");
32883	break;
32884      case RT_AT_THROW:
32885	gmsgid = G_("expected %<@throw%>");
32886	break;
32887      case RT_TRANSACTION_ATOMIC:
32888	gmsgid = G_("expected %<__transaction_atomic%>");
32889	break;
32890      case RT_TRANSACTION_RELAXED:
32891	gmsgid = G_("expected %<__transaction_relaxed%>");
32892	break;
32893      case RT_CO_YIELD:
32894	gmsgid = G_("expected %<co_yield%>");
32895	break;
32896      default:
32897	break;
32898    }
32899
32900  if (!gmsgid && !keyword)
32901    {
32902      switch (token_desc)
32903        {
32904	  case RT_SEMICOLON:
32905	    gmsgid = G_("expected %<;%>");
32906	    break;
32907	  case RT_OPEN_PAREN:
32908	    gmsgid = G_("expected %<(%>");
32909	    break;
32910	  case RT_CLOSE_BRACE:
32911	    gmsgid = G_("expected %<}%>");
32912	    break;
32913	  case RT_OPEN_BRACE:
32914	    gmsgid = G_("expected %<{%>");
32915	    break;
32916	  case RT_CLOSE_SQUARE:
32917	    gmsgid = G_("expected %<]%>");
32918	    break;
32919	  case RT_OPEN_SQUARE:
32920	    gmsgid = G_("expected %<[%>");
32921	    break;
32922	  case RT_COMMA:
32923	    gmsgid = G_("expected %<,%>");
32924	    break;
32925	  case RT_SCOPE:
32926	    gmsgid = G_("expected %<::%>");
32927	    break;
32928	  case RT_LESS:
32929	    gmsgid = G_("expected %<<%>");
32930	    break;
32931	  case RT_GREATER:
32932	    gmsgid = G_("expected %<>%>");
32933	    break;
32934	  case RT_EQ:
32935	    gmsgid = G_("expected %<=%>");
32936	    break;
32937	  case RT_ELLIPSIS:
32938	    gmsgid = G_("expected %<...%>");
32939	    break;
32940	  case RT_MULT:
32941	    gmsgid = G_("expected %<*%>");
32942	    break;
32943	  case RT_COMPL:
32944	    gmsgid = G_("expected %<~%>");
32945	    break;
32946	  case RT_COLON:
32947	    gmsgid = G_("expected %<:%>");
32948	    break;
32949	  case RT_COLON_SCOPE:
32950	    gmsgid = G_("expected %<:%> or %<::%>");
32951	    break;
32952	  case RT_CLOSE_PAREN:
32953	    gmsgid = G_("expected %<)%>");
32954	    break;
32955	  case RT_COMMA_CLOSE_PAREN:
32956	    gmsgid = G_("expected %<,%> or %<)%>");
32957	    break;
32958	  case RT_PRAGMA_EOL:
32959	    gmsgid = G_("expected end of line");
32960	    break;
32961	  case RT_NAME:
32962	    gmsgid = G_("expected identifier");
32963	    break;
32964	  case RT_SELECT:
32965	    gmsgid = G_("expected selection-statement");
32966	    break;
32967	  case RT_ITERATION:
32968	    gmsgid = G_("expected iteration-statement");
32969	    break;
32970	  case RT_JUMP:
32971	    gmsgid = G_("expected jump-statement");
32972	    break;
32973	  case RT_CLASS_KEY:
32974	    gmsgid = G_("expected class-key");
32975	    break;
32976	  case RT_CLASS_TYPENAME_TEMPLATE:
32977	    gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
32978	    break;
32979	  default:
32980	    gcc_unreachable ();
32981	}
32982    }
32983
32984  if (gmsgid)
32985    cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
32986}
32987
32988
32989/* If the next token is of the indicated TYPE, consume it.  Otherwise,
32990   issue an error message indicating that TOKEN_DESC was expected.
32991
32992   Returns the token consumed, if the token had the appropriate type.
32993   Otherwise, returns NULL.
32994
32995   If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32996   within any error as the location of an "opening" token matching
32997   the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32998   RT_CLOSE_PAREN).  */
32999
33000static cp_token *
33001cp_parser_require (cp_parser* parser,
33002		   enum cpp_ttype type,
33003		   required_token token_desc,
33004		   location_t matching_location)
33005{
33006  if (cp_lexer_next_token_is (parser->lexer, type))
33007    return cp_lexer_consume_token (parser->lexer);
33008  else
33009    {
33010      /* Output the MESSAGE -- unless we're parsing tentatively.  */
33011      if (!cp_parser_simulate_error (parser))
33012	cp_parser_required_error (parser, token_desc, /*keyword=*/false,
33013				  matching_location);
33014      return NULL;
33015    }
33016}
33017
33018/* Skip an entire parameter list from start to finish.  The next token must
33019   be the initial "<" of the parameter list.  Returns true on success and
33020   false otherwise.  */
33021
33022static bool
33023cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
33024{
33025  /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
33026     requires it.  */
33027  cp_lexer_consume_token (parser->lexer);
33028  return cp_parser_skip_to_end_of_template_parameter_list (parser);
33029}
33030
33031/* Ensure we are at the end of a template parameter list.  If we are, return.
33032   If we are not, something has gone wrong, in which case issue an error and
33033   skip to end of the parameter list.  */
33034
33035static void
33036cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
33037{
33038  /* Are we ready, yet?  If not, issue error message.  */
33039  if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
33040    return;
33041
33042  cp_parser_skip_to_end_of_template_parameter_list (parser);
33043}
33044
33045/* You should only call this function from inside a template parameter list
33046   (i.e. the current token should at least be the initial "<" of the
33047   parameter list).  If you are skipping the entire list, it may be better to
33048   use cp_parser_skip_entire_template_parameter_list.
33049
33050   Tokens are skipped until the final ">" is found, or if we see
33051   '{', '}', ';', or if we find an unbalanced ')' or ']'.
33052
33053   Returns true if we successfully reached the end, and false if
33054   something unexpected happened (e.g. end of file).  */
33055
33056static bool
33057cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
33058{
33059  /* Current level of '< ... >'.  */
33060  unsigned level = 0;
33061  /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
33062  unsigned nesting_depth = 0;
33063
33064  /* Skip tokens until the desired token is found.  */
33065  while (true)
33066    {
33067      /* Peek at the next token.  */
33068      switch (cp_lexer_peek_token (parser->lexer)->type)
33069	{
33070	case CPP_LESS:
33071	  if (!nesting_depth)
33072	    ++level;
33073	  break;
33074
33075        case CPP_RSHIFT:
33076          if (cxx_dialect == cxx98)
33077            /* C++0x views the `>>' operator as two `>' tokens, but
33078               C++98 does not. */
33079            break;
33080          else if (!nesting_depth && level-- == 0)
33081	    {
33082              /* We've hit a `>>' where the first `>' closes the
33083                 template argument list, and the second `>' is
33084                 spurious.  Just consume the `>>' and stop; we've
33085                 already produced at least one error.  */
33086	      cp_lexer_consume_token (parser->lexer);
33087	      return false;
33088	    }
33089          /* Fall through for C++0x, so we handle the second `>' in
33090             the `>>'.  */
33091	  gcc_fallthrough ();
33092
33093	case CPP_GREATER:
33094	  if (!nesting_depth && level-- == 0)
33095	    {
33096	      /* We've reached the token we want, consume it and stop.  */
33097	      cp_lexer_consume_token (parser->lexer);
33098	      return true;
33099	    }
33100	  break;
33101
33102	case CPP_OPEN_PAREN:
33103	case CPP_OPEN_SQUARE:
33104	  ++nesting_depth;
33105	  break;
33106
33107	case CPP_CLOSE_PAREN:
33108	case CPP_CLOSE_SQUARE:
33109	  if (nesting_depth-- == 0)
33110	    return false;
33111	  break;
33112
33113	case CPP_EOF:
33114	case CPP_PRAGMA_EOL:
33115	case CPP_SEMICOLON:
33116	case CPP_OPEN_BRACE:
33117	case CPP_CLOSE_BRACE:
33118	  /* The '>' was probably forgotten, don't look further.  */
33119	  return false;
33120
33121	default:
33122	  break;
33123	}
33124
33125      /* Consume this token.  */
33126      cp_lexer_consume_token (parser->lexer);
33127    }
33128}
33129
33130/* If the next token is the indicated keyword, consume it.  Otherwise,
33131   issue an error message indicating that TOKEN_DESC was expected.
33132
33133   Returns the token consumed, if the token had the appropriate type.
33134   Otherwise, returns NULL.  */
33135
33136static cp_token *
33137cp_parser_require_keyword (cp_parser* parser,
33138			   enum rid keyword,
33139			   required_token token_desc)
33140{
33141  cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
33142
33143  if (token && token->keyword != keyword)
33144    {
33145      cp_parser_required_error (parser, token_desc, /*keyword=*/true,
33146                                UNKNOWN_LOCATION);
33147      return NULL;
33148    }
33149
33150  return token;
33151}
33152
33153/* Returns TRUE iff TOKEN is a token that can begin the body of a
33154   function-definition.  */
33155
33156static bool
33157cp_parser_token_starts_function_definition_p (cp_token* token)
33158{
33159  return (/* An ordinary function-body begins with an `{'.  */
33160	  token->type == CPP_OPEN_BRACE
33161	  /* A ctor-initializer begins with a `:'.  */
33162	  || token->type == CPP_COLON
33163	  /* A function-try-block begins with `try'.  */
33164	  || token->keyword == RID_TRY
33165	  /* A function-transaction-block begins with `__transaction_atomic'
33166	     or `__transaction_relaxed'.  */
33167	  || token->keyword == RID_TRANSACTION_ATOMIC
33168	  || token->keyword == RID_TRANSACTION_RELAXED
33169	  /* The named return value extension begins with `return'.  */
33170	  || token->keyword == RID_RETURN);
33171}
33172
33173/* Returns TRUE iff the next token is the ":" or "{" beginning a class
33174   definition.  */
33175
33176static bool
33177cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
33178{
33179  cp_token *token;
33180
33181  token = cp_lexer_peek_token (parser->lexer);
33182  return (token->type == CPP_OPEN_BRACE
33183	  || (token->type == CPP_COLON
33184	      && !parser->colon_doesnt_start_class_def_p));
33185}
33186
33187/* Returns TRUE iff the next token is the "," or ">" (or `>>', in
33188   C++0x) ending a template-argument.  */
33189
33190static bool
33191cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
33192{
33193  cp_token *token;
33194
33195  token = cp_lexer_peek_token (parser->lexer);
33196  return (token->type == CPP_COMMA
33197          || token->type == CPP_GREATER
33198          || token->type == CPP_ELLIPSIS
33199	  || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
33200}
33201
33202/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
33203   (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
33204
33205static bool
33206cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
33207						     size_t n)
33208{
33209  cp_token *token;
33210
33211  token = cp_lexer_peek_nth_token (parser->lexer, n);
33212  if (token->type == CPP_LESS)
33213    return true;
33214  /* Check for the sequence `<::' in the original code. It would be lexed as
33215     `[:', where `[' is a digraph, and there is no whitespace before
33216     `:'.  */
33217  if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
33218    {
33219      cp_token *token2;
33220      token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
33221      if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
33222	return true;
33223    }
33224  return false;
33225}
33226
33227/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
33228   or none_type otherwise.  */
33229
33230static enum tag_types
33231cp_parser_token_is_class_key (cp_token* token)
33232{
33233  switch (token->keyword)
33234    {
33235    case RID_CLASS:
33236      return class_type;
33237    case RID_STRUCT:
33238      return record_type;
33239    case RID_UNION:
33240      return union_type;
33241
33242    default:
33243      return none_type;
33244    }
33245}
33246
33247/* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
33248   or none_type otherwise or if the token is null.  */
33249
33250static enum tag_types
33251cp_parser_token_is_type_parameter_key (cp_token* token)
33252{
33253  if (!token)
33254    return none_type;
33255
33256  switch (token->keyword)
33257    {
33258    case RID_CLASS:
33259      return class_type;
33260    case RID_TYPENAME:
33261      return typename_type;
33262
33263    default:
33264      return none_type;
33265    }
33266}
33267
33268/* Diagnose redundant enum-keys.  */
33269
33270static void
33271cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
33272			       tree type, rid scoped_key)
33273{
33274  if (!warn_redundant_tags)
33275    return;
33276
33277  tree type_decl = TYPE_MAIN_DECL (type);
33278  tree name = DECL_NAME (type_decl);
33279  /* Look up the NAME to see if it unambiguously refers to the TYPE.  */
33280  push_deferring_access_checks (dk_no_check);
33281  tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33282  pop_deferring_access_checks ();
33283
33284  /* The enum-key is redundant for uses of the TYPE that are not
33285     declarations and for which name lookup returns just the type
33286     itself.  */
33287  if (decl != type_decl)
33288    return;
33289
33290  if (scoped_key != RID_CLASS
33291      && scoped_key != RID_STRUCT
33292      && current_lang_name != lang_name_cplusplus
33293      && current_namespace == global_namespace)
33294    {
33295      /* Avoid issuing the diagnostic for apparently redundant (unscoped)
33296	 enum tag in shared C/C++ code in files (such as headers) included
33297	 in the main source file.  */
33298      const line_map_ordinary *map = NULL;
33299      linemap_resolve_location (line_table, key_loc,
33300				LRK_MACRO_DEFINITION_LOCATION,
33301				&map);
33302      if (!MAIN_FILE_P (map))
33303	return;
33304    }
33305
33306  gcc_rich_location richloc (key_loc);
33307  richloc.add_fixit_remove (key_loc);
33308  warning_at (&richloc, OPT_Wredundant_tags,
33309	      "redundant enum-key %<enum%s%> in reference to %q#T",
33310	      (scoped_key == RID_CLASS ? " class"
33311	       : scoped_key == RID_STRUCT ? " struct" : ""), type);
33312}
33313
33314/* Describes the set of declarations of a struct, class, or class template
33315   or its specializations.  Used for -Wmismatched-tags.  */
33316
33317class class_decl_loc_t
33318{
33319 public:
33320
33321  class_decl_loc_t ()
33322    : locvec (), idxdef (), def_class_key ()
33323  {
33324    locvec.create (4);
33325  }
33326
33327  /* Constructs an object for a single declaration of a class with
33328     CLASS_KEY at the current location in the current function (or
33329     at another scope).  KEY_REDUNDANT is true if the class-key may
33330     be omitted in the current context without an ambiguity with
33331     another symbol with the same name.
33332     DEF_P is true for a class declaration that is a definition.
33333     CURLOC is the associated location.  */
33334  class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
33335		    location_t curloc = input_location)
33336    : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
33337  {
33338    locvec.create (4);
33339    class_key_loc_t ckl (current_function_decl, curloc, class_key,
33340			 key_redundant);
33341    locvec.quick_push (ckl);
33342  }
33343
33344  /* Copy, assign, and destroy the object.  Necessary because LOCVEC
33345     isn't safely copyable and assignable and doesn't release storage
33346     on its own.  */
33347  class_decl_loc_t (const class_decl_loc_t &rhs)
33348    : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
33349      def_class_key (rhs.def_class_key)
33350  { }
33351
33352  class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
33353  {
33354    if (this == &rhs)
33355      return *this;
33356    locvec.release ();
33357    locvec = rhs.locvec.copy ();
33358    idxdef = rhs.idxdef;
33359    def_class_key = rhs.def_class_key;
33360    return *this;
33361  }
33362
33363  ~class_decl_loc_t ()
33364  {
33365    locvec.release ();
33366  }
33367
33368  /* Issues -Wmismatched-tags for a single class.  */
33369  void diag_mismatched_tags (tree);
33370
33371  /* Issues -Wmismatched-tags for all classes.  */
33372  static void diag_mismatched_tags ();
33373
33374  /* Adds TYPE_DECL to the collection of class decls and diagnoses
33375     redundant tags (if -Wredundant-tags is enabled).  */
33376  static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
33377
33378  /* Either adds this decl to the collection of class decls
33379     or diagnoses it, whichever is appropriate.  */
33380  void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
33381
33382private:
33383
33384  tree function (unsigned i) const
33385  {
33386    return locvec[i].func;
33387  }
33388
33389  location_t location (unsigned i) const
33390  {
33391    return locvec[i].loc;
33392  }
33393
33394  bool key_redundant (unsigned i) const
33395  {
33396    return locvec[i].key_redundant;
33397  }
33398
33399  tag_types class_key (unsigned i) const
33400  {
33401    return locvec[i].class_key;
33402  }
33403
33404  /* True if a definition for the class has been seen.  */
33405  bool def_p () const
33406  {
33407    return idxdef < locvec.length ();
33408  }
33409
33410  /* The location of a single mention of a class type with the given
33411     class-key.  */
33412  struct class_key_loc_t
33413  {
33414    class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
33415      : func (func), loc (loc), class_key (key), key_redundant (redundant)
33416    { }
33417
33418    /* The function the type is mentioned in.  */
33419    tree func;
33420    /* The exact location.  */
33421    location_t loc;
33422    /* The class-key used in the mention of the type.  */
33423    tag_types class_key;
33424    /* True when the class-key could be omitted at this location
33425       without an ambiguity with another symbol of the same name.  */
33426    bool key_redundant;
33427  };
33428  /* Avoid using auto_vec here since it's not safe to copy due to pr90904.  */
33429  vec <class_key_loc_t> locvec;
33430  /* LOCVEC index of the definition or UINT_MAX if none exists.  */
33431  unsigned idxdef;
33432  /* The class-key the class was last declared with or none_type when
33433     it has been declared with a mismatched key.  */
33434  tag_types def_class_key;
33435
33436  /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
33437     description above.  */
33438  typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
33439  static class_to_loc_map_t class2loc;
33440};
33441
33442class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
33443
33444/* Issue an error message if the CLASS_KEY does not match the TYPE.
33445   DEF_P is expected to be set for a definition of class TYPE.  DECL_P
33446   is set for a declaration of class TYPE and clear for a reference to
33447   it that is not a declaration of it.  */
33448
33449static void
33450cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
33451			   tag_types class_key, tree type, bool def_p,
33452			   bool decl_p)
33453{
33454  if (type == error_mark_node)
33455    return;
33456
33457  bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
33458  if (seen_as_union != (class_key == union_type))
33459    {
33460      if (permerror (input_location, "%qs tag used in naming %q#T",
33461		     class_key == union_type ? "union"
33462		     : class_key == record_type ? "struct" : "class",
33463		     type))
33464	inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
33465		"%q#T was previously declared here", type);
33466      return;
33467    }
33468
33469  if (!warn_mismatched_tags && !warn_redundant_tags)
33470    return;
33471
33472  /* Only consider the true class-keys below and ignore typename_type,
33473     etc. that are not C++ class-keys.  */
33474  if (class_key != class_type
33475      && class_key != record_type
33476      && class_key != union_type)
33477    return;
33478
33479  class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
33480}
33481
33482/* Returns the template or specialization of one to which the RECORD_TYPE
33483   TYPE corresponds.  */
33484
33485static tree
33486specialization_of (tree type)
33487{
33488  tree ret = type;
33489
33490  /* Determine the template or its partial specialization to which TYPE
33491     corresponds.  */
33492  if (tree spec = most_specialized_partial_spec (type, tf_none))
33493    if (spec != error_mark_node)
33494      ret = TREE_TYPE (TREE_VALUE (spec));
33495
33496  if (ret == type)
33497    ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
33498
33499  return TYPE_MAIN_DECL (ret);
33500}
33501
33502
33503/* Adds the class TYPE to the collection of class decls and diagnoses
33504   redundant tags (if -Wredundant-tags is enabled).
33505   DEF_P is expected to be set for a definition of class TYPE.  DECL_P
33506   is set for a (likely, based on syntactic context) declaration of class
33507   TYPE and clear for a reference to it that is not a declaration of it.  */
33508
33509void
33510class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
33511		       tag_types class_key, tree type, bool def_p, bool decl_p)
33512{
33513  tree type_decl = TYPE_MAIN_DECL (type);
33514  tree name = DECL_NAME (type_decl);
33515  /* Look up the NAME to see if it unambiguously refers to the TYPE
33516     and set KEY_REDUNDANT if so.  */
33517  push_deferring_access_checks (dk_no_check);
33518  tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33519  pop_deferring_access_checks ();
33520
33521  /* The class-key is redundant for uses of the CLASS_TYPE that are
33522     neither definitions of it nor declarations, and for which name
33523     lookup returns just the type itself.  */
33524  bool key_redundant = (!def_p && !decl_p
33525			&& (decl == type_decl
33526			    || TREE_CODE (decl) == TEMPLATE_DECL
33527			    || (CLASS_TYPE_P (type)
33528				&& TYPE_BEING_DEFINED (type))));
33529
33530  if (key_redundant
33531      && class_key != class_type
33532      && current_lang_name != lang_name_cplusplus
33533      && current_namespace == global_namespace)
33534    {
33535      /* Avoid issuing the diagnostic for apparently redundant struct
33536	 and union class-keys in shared C/C++ code in files (such as
33537	 headers) included in the main source file.  */
33538      const line_map_ordinary *map = NULL;
33539      linemap_resolve_location (line_table, key_loc,
33540				LRK_MACRO_DEFINITION_LOCATION,
33541				&map);
33542      if (!MAIN_FILE_P (map))
33543	key_redundant = false;
33544    }
33545
33546  /* Set if a declaration of TYPE has previously been seen or if it must
33547     exist in a precompiled header.  */
33548  bool exist;
33549  class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
33550  if (!exist)
33551    {
33552      tree type = TREE_TYPE (type_decl);
33553      if (def_p || !COMPLETE_TYPE_P (type))
33554	{
33555	  /* TYPE_DECL is the first declaration or definition of the type
33556	     (outside precompiled headers -- see below).  Just create
33557	     a new entry for it and return unless it's a declaration
33558	     involving a template that may need to be diagnosed by
33559	     -Wredundant-tags.  */
33560	  *rdl = class_decl_loc_t (class_key, false, def_p);
33561	  if (TREE_CODE (decl) != TEMPLATE_DECL)
33562	    return;
33563	}
33564      else
33565	{
33566	  /* TYPE was previously defined in some unknown precompiled header.
33567	     Simply add a record of its definition at an unknown location and
33568	     proceed below to add a reference to it at the current location.
33569	     (Declarations in precompiled headers that are not definitions
33570	     are ignored.)  */
33571	  tag_types def_key
33572	    = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
33573	  location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
33574	  *rdl = class_decl_loc_t (def_key, false, true, def_loc);
33575	  exist = true;
33576	}
33577    }
33578
33579  /* A prior declaration of TYPE_DECL has been seen.  */
33580
33581  if (key_redundant)
33582    {
33583      gcc_rich_location richloc (key_loc);
33584      richloc.add_fixit_remove (key_loc);
33585      warning_at (&richloc, OPT_Wredundant_tags,
33586		  "redundant class-key %qs in reference to %q#T",
33587		  class_key == union_type ? "union"
33588		  : class_key == record_type ? "struct" : "class",
33589		  type);
33590    }
33591
33592  if (!exist)
33593    /* Do nothing if this is the first declaration of the type.  */
33594    return;
33595
33596  if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
33597    /* Do nothing if the class-key in this declaration matches
33598       the definition.  */
33599    return;
33600
33601  rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
33602				   def_p);
33603}
33604
33605/* Either adds this DECL corresponding to the TYPE_DECL to the collection
33606   of class decls or diagnoses it, whichever is appropriate.  */
33607
33608void
33609class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
33610					      tag_types class_key,
33611					      bool redundant,
33612					      bool def_p)
33613{
33614  /* Reset the CLASS_KEY associated with this type on mismatch.
33615     This is an optimization that lets the diagnostic code skip
33616     over classes that use the same class-key in all declarations.  */
33617  if (def_class_key != class_key)
33618    def_class_key = none_type;
33619
33620  /* Set IDXDEF to the index of the vector corresponding to
33621     the definition.  */
33622  if (def_p)
33623    idxdef = locvec.length ();
33624
33625  /* Append a record of this declaration to the vector.  */
33626  class_key_loc_t ckl (current_function_decl, input_location, class_key,
33627		       redundant);
33628  locvec.safe_push (ckl);
33629
33630  if (idxdef == UINT_MAX)
33631    return;
33632
33633  /* As a space optimization diagnose declarations of a class
33634     whose definition has been seen and purge the LOCVEC of
33635     all entries except the definition.  */
33636  diag_mismatched_tags (type_decl);
33637  if (idxdef)
33638    {
33639      class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
33640      locvec.release ();
33641      locvec.reserve (2);
33642      locvec.safe_push (ent);
33643      idxdef = 0;
33644    }
33645  else
33646    /* Pop the entry pushed above for this declaration.  */
33647    locvec.pop ();
33648}
33649
33650/* Issues -Wmismatched-tags for a single class.  */
33651
33652void
33653class_decl_loc_t::diag_mismatched_tags (tree type_decl)
33654{
33655  if (!warn_mismatched_tags)
33656    return;
33657
33658  /* Number of uses of the class.  */
33659  const unsigned ndecls = locvec.length ();
33660
33661  /* The class (or template) declaration guiding the decisions about
33662     the diagnostic.  For ordinary classes it's the same as THIS.  For
33663     uses of instantiations of templates other than their declarations
33664     it points to the record for the declaration of the corresponding
33665     primary template or partial specialization.  */
33666  class_decl_loc_t *cdlguide = this;
33667
33668  tree type = TREE_TYPE (type_decl);
33669  if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
33670    {
33671      /* For implicit instantiations of a primary template look up
33672	 the primary or partial specialization and use it as
33673	 the expected class-key rather than using the class-key of
33674	 the first reference to the instantiation.  The primary must
33675	 be (and inevitably is) at index zero.  */
33676      tree spec = specialization_of (type);
33677      cdlguide = class2loc.get (spec);
33678      /* It's possible that we didn't find SPEC.  Consider:
33679
33680	   template<typename T> struct A {
33681	     template<typename U> struct W { };
33682	   };
33683	   struct A<int>::W<int> w; // #1
33684
33685	 where while parsing A and #1 we've stashed
33686	   A<T>
33687	   A<T>::W<U>
33688	   A<int>::W<int>
33689	 into CLASS2LOC.  If TYPE is A<int>::W<int>, specialization_of
33690	 will yield A<int>::W<U> which may be in CLASS2LOC if we had
33691	 an A<int> class specialization, but otherwise won't be in it.
33692	 So try to look up A<T>::W<U>.  */
33693      if (!cdlguide)
33694	{
33695	  spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
33696	  cdlguide = class2loc.get (spec);
33697	}
33698      /* Now we really should have found something.  */
33699      gcc_assert (cdlguide != NULL);
33700    }
33701  /* Skip declarations that consistently use the same class-key.  */
33702  else if (def_class_key != none_type)
33703    return;
33704
33705  /* Set if a definition for the class has been seen.  */
33706  const bool def_p = cdlguide->def_p ();
33707
33708  /* The index of the declaration whose class-key this declaration
33709     is expected to match.  It's either the class-key of the class
33710     definition if one exists or the first declaration otherwise.  */
33711  const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
33712
33713  /* The class-key the class is expected to be declared with: it's
33714     either the key used in its definition or the first declaration
33715     if no definition has been provided.
33716     For implicit instantiations of a primary template it's
33717     the class-key used to declare the primary with.  The primary
33718     must be at index zero.  */
33719  const tag_types xpect_key = cdlguide->class_key (idxguide);
33720
33721  unsigned idx = 0;
33722  /* Advance IDX to the first declaration that either is not
33723     a definition or that doesn't match the first declaration
33724     if no definition is provided.  */
33725  while (class_key (idx) == xpect_key)
33726    if (++idx == ndecls)
33727      return;
33728
33729  /* Save the current function before changing it below.  */
33730  tree save_func = current_function_decl;
33731  /* Set the function declaration to print in diagnostic context.  */
33732  current_function_decl = function (idx);
33733
33734  const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
33735  const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
33736
33737  location_t loc = location (idx);
33738  bool key_redundant_p = key_redundant (idx);
33739  auto_diagnostic_group d;
33740  /* Issue a warning for the first mismatched declaration.
33741     Avoid using "%#qT" since the class-key for the same type will
33742     be the same regardless of which one was used in the declaraion.  */
33743  if (warning_at (loc, OPT_Wmismatched_tags,
33744		  "%qT declared with a mismatched class-key %qs",
33745		  type_decl, xmatchkstr))
33746    {
33747      /* Suggest how to avoid the warning for each instance since
33748	 the guidance may be different depending on context.  */
33749      inform (loc,
33750	      (key_redundant_p
33751	       ? G_("remove the class-key or replace it with %qs")
33752	       : G_("replace the class-key with %qs")),
33753	      xpectkstr);
33754
33755      /* Also point to the first declaration or definition that guided
33756	 the decision to issue the warning above.  */
33757      inform (cdlguide->location (idxguide),
33758	      (def_p
33759	       ? G_("%qT defined as %qs here")
33760	       : G_("%qT first declared as %qs here")),
33761	      type_decl, xpectkstr);
33762    }
33763
33764  /* Issue warnings for the remaining inconsistent declarations.  */
33765  for (unsigned i = idx + 1; i != ndecls; ++i)
33766    {
33767      tag_types clskey = class_key (i);
33768      /* Skip over the declarations that match either the definition
33769	 if one was provided or the first declaration.  */
33770      if (clskey == xpect_key)
33771	continue;
33772
33773      loc = location (i);
33774      key_redundant_p = key_redundant (i);
33775      /* Set the function declaration to print in diagnostic context.  */
33776      current_function_decl = function (i);
33777      if (warning_at (loc, OPT_Wmismatched_tags,
33778		      "%qT declared with a mismatched class-key %qs",
33779		      type_decl, xmatchkstr))
33780	/* Suggest how to avoid the warning for each instance since
33781	   the guidance may be different depending on context.  */
33782	inform (loc,
33783		(key_redundant_p
33784		 ? G_("remove the class-key or replace it with %qs")
33785		 : G_("replace the class-key with %qs")),
33786		xpectkstr);
33787    }
33788
33789  /* Restore the current function in case it was replaced above.  */
33790  current_function_decl = save_func;
33791}
33792
33793/* Issues -Wmismatched-tags for all classes.  Called at the end
33794   of processing a translation unit, after declarations of all class
33795   types and their uses have been recorded.  */
33796
33797void
33798class_decl_loc_t::diag_mismatched_tags ()
33799{
33800  /* CLASS2LOC should be empty if both -Wmismatched-tags and
33801     -Wredundant-tags are disabled.  */
33802  gcc_assert (warn_mismatched_tags
33803	      || warn_redundant_tags
33804	      || class2loc.is_empty ());
33805
33806  /* Save the current function before changing on return.  It should
33807     be null at this point.  */
33808  temp_override<tree> cleanup (current_function_decl);
33809
33810  if (warn_mismatched_tags)
33811    {
33812      /* Iterate over the collected class/struct/template declarations.  */
33813      typedef class_to_loc_map_t::iterator iter_t;
33814      for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
33815	{
33816	  tree type_decl = (*it).first;
33817	  class_decl_loc_t &recloc = (*it).second;
33818	  recloc.diag_mismatched_tags (type_decl);
33819	}
33820    }
33821
33822  class2loc.empty ();
33823}
33824
33825/* Issue an error message if DECL is redeclared with different
33826   access than its original declaration [class.access.spec/3].
33827   This applies to nested classes, nested class templates and
33828   enumerations [class.mem/1].  */
33829
33830static void
33831cp_parser_check_access_in_redeclaration (tree decl, location_t location)
33832{
33833  if (!decl
33834      || (!CLASS_TYPE_P (TREE_TYPE (decl))
33835	  && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
33836    return;
33837
33838  if ((TREE_PRIVATE (decl)
33839       != (current_access_specifier == access_private_node))
33840      || (TREE_PROTECTED (decl)
33841	  != (current_access_specifier == access_protected_node)))
33842    error_at (location, "%qD redeclared with different access", decl);
33843}
33844
33845/* Look for the `template' keyword, as a syntactic disambiguator.
33846   Return TRUE iff it is present, in which case it will be
33847   consumed.  */
33848
33849static bool
33850cp_parser_optional_template_keyword (cp_parser *parser)
33851{
33852  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
33853    {
33854      /* In C++98 the `template' keyword can only be used within templates;
33855	 outside templates the parser can always figure out what is a
33856	 template and what is not.  In C++11,  per the resolution of DR 468,
33857	 `template' is allowed in cases where it is not strictly necessary.  */
33858      if (!processing_template_decl
33859	  && pedantic && cxx_dialect == cxx98)
33860	{
33861	  cp_token *token = cp_lexer_peek_token (parser->lexer);
33862	  pedwarn (token->location, OPT_Wpedantic,
33863		   "in C++98 %<template%> (as a disambiguator) is only "
33864		   "allowed within templates");
33865	  /* If this part of the token stream is rescanned, the same
33866	     error message would be generated.  So, we purge the token
33867	     from the stream.  */
33868	  cp_lexer_purge_token (parser->lexer);
33869	  return false;
33870	}
33871      else
33872	{
33873	  /* Consume the `template' keyword.  */
33874	  cp_lexer_consume_token (parser->lexer);
33875	  return true;
33876	}
33877    }
33878  return false;
33879}
33880
33881/* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
33882   set PARSER->SCOPE, and perform other related actions.  */
33883
33884static void
33885cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
33886{
33887  struct tree_check *check_value;
33888
33889  /* Get the stored value.  */
33890  check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
33891  /* Set the scope from the stored value.  */
33892  parser->scope = saved_checks_value (check_value);
33893  parser->qualifying_scope = check_value->qualifying_scope;
33894  parser->object_scope = parser->context->object_type;
33895  parser->context->object_type = NULL_TREE;
33896}
33897
33898/* Consume tokens up through a non-nested END token.  Returns TRUE if we
33899   encounter the end of a block before what we were looking for.  */
33900
33901static bool
33902cp_parser_cache_group (cp_parser *parser,
33903		       enum cpp_ttype end,
33904		       unsigned depth)
33905{
33906  while (true)
33907    {
33908      cp_token *token = cp_lexer_peek_token (parser->lexer);
33909
33910      /* Abort a parenthesized expression if we encounter a semicolon.  */
33911      if ((end == CPP_CLOSE_PAREN || depth == 0)
33912	  && token->type == CPP_SEMICOLON)
33913	return true;
33914      /* If we've reached the end of the file, stop.  */
33915      if (token->type == CPP_EOF
33916	  || (end != CPP_PRAGMA_EOL
33917	      && token->type == CPP_PRAGMA_EOL))
33918	return true;
33919      if (token->type == CPP_CLOSE_BRACE && depth == 0)
33920	/* We've hit the end of an enclosing block, so there's been some
33921	   kind of syntax error.  */
33922	return true;
33923
33924      /* Consume the token.  */
33925      cp_lexer_consume_token (parser->lexer);
33926      /* See if it starts a new group.  */
33927      if (token->type == CPP_OPEN_BRACE)
33928	{
33929	  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
33930	  /* In theory this should probably check end == '}', but
33931	     cp_parser_save_member_function_body needs it to exit
33932	     after either '}' or ')' when called with ')'.  */
33933	  if (depth == 0)
33934	    return false;
33935	}
33936      else if (token->type == CPP_OPEN_PAREN)
33937	{
33938	  cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
33939	  if (depth == 0 && end == CPP_CLOSE_PAREN)
33940	    return false;
33941	}
33942      else if (token->type == CPP_PRAGMA)
33943	cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
33944      else if (token->type == end)
33945	return false;
33946    }
33947}
33948
33949/* Like above, for caching a default argument or NSDMI.  Both of these are
33950   terminated by a non-nested comma, but it can be unclear whether or not a
33951   comma is nested in a template argument list unless we do more parsing.
33952   In order to handle this ambiguity, when we encounter a ',' after a '<'
33953   we try to parse what follows as a parameter-declaration-list (in the
33954   case of a default argument) or a member-declarator (in the case of an
33955   NSDMI).  If that succeeds, then we stop caching.  */
33956
33957static tree
33958cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
33959{
33960  unsigned depth = 0;
33961  int maybe_template_id = 0;
33962  cp_token *first_token;
33963  cp_token *token;
33964  tree default_argument;
33965
33966  /* Add tokens until we have processed the entire default
33967     argument.  We add the range [first_token, token).  */
33968  first_token = cp_lexer_peek_token (parser->lexer);
33969  if (first_token->type == CPP_OPEN_BRACE)
33970    {
33971      /* For list-initialization, this is straightforward.  */
33972      cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33973      token = cp_lexer_peek_token (parser->lexer);
33974    }
33975  else while (true)
33976    {
33977      bool done = false;
33978
33979      /* Peek at the next token.  */
33980      token = cp_lexer_peek_token (parser->lexer);
33981      /* What we do depends on what token we have.  */
33982      switch (token->type)
33983	{
33984	  /* In valid code, a default argument must be
33985	     immediately followed by a `,' `)', or `...'.  */
33986	case CPP_COMMA:
33987	  if (depth == 0 && maybe_template_id)
33988	    {
33989	      /* If we've seen a '<', we might be in a
33990		 template-argument-list.  Until Core issue 325 is
33991		 resolved, we don't know how this situation ought
33992		 to be handled, so try to DTRT.  We check whether
33993		 what comes after the comma is a valid parameter
33994		 declaration list.  If it is, then the comma ends
33995		 the default argument; otherwise the default
33996		 argument continues.  */
33997	      bool error = false;
33998	      cp_token *peek;
33999
34000	      /* Set ITALP so cp_parser_parameter_declaration_list
34001		 doesn't decide to commit to this parse.  */
34002	      bool saved_italp = parser->in_template_argument_list_p;
34003	      parser->in_template_argument_list_p = true;
34004
34005	      cp_parser_parse_tentatively (parser);
34006
34007	      if (nsdmi)
34008		{
34009		  /* Parse declarators until we reach a non-comma or
34010		     somthing that cannot be an initializer.
34011		     Just checking whether we're looking at a single
34012		     declarator is insufficient.  Consider:
34013		       int var = tuple<T,U>::x;
34014		     The template parameter 'U' looks exactly like a
34015		     declarator.  */
34016		  do
34017		    {
34018		      int ctor_dtor_or_conv_p;
34019		      cp_lexer_consume_token (parser->lexer);
34020		      cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34021					    CP_PARSER_FLAGS_NONE,
34022					    &ctor_dtor_or_conv_p,
34023					    /*parenthesized_p=*/NULL,
34024					    /*member_p=*/true,
34025					    /*friend_p=*/false,
34026					    /*static_p=*/false);
34027		      peek = cp_lexer_peek_token (parser->lexer);
34028		      if (cp_parser_error_occurred (parser))
34029			break;
34030		    }
34031		  while (peek->type == CPP_COMMA);
34032		  /* If we met an '=' or ';' then the original comma
34033		     was the end of the NSDMI.  Otherwise assume
34034		     we're still in the NSDMI.  */
34035		  error = (peek->type != CPP_EQ
34036			   && peek->type != CPP_SEMICOLON);
34037		}
34038	      else
34039		{
34040		  cp_lexer_consume_token (parser->lexer);
34041		  begin_scope (sk_function_parms, NULL_TREE);
34042		  tree t = cp_parser_parameter_declaration_list
34043			    (parser, CP_PARSER_FLAGS_NONE);
34044		  if (t == error_mark_node)
34045		    error = true;
34046		  pop_bindings_and_leave_scope ();
34047		}
34048	      if (!cp_parser_error_occurred (parser) && !error)
34049		done = true;
34050	      cp_parser_abort_tentative_parse (parser);
34051
34052	      parser->in_template_argument_list_p = saved_italp;
34053	      break;
34054	    }
34055	  /* FALLTHRU */
34056	case CPP_CLOSE_PAREN:
34057	case CPP_ELLIPSIS:
34058	  /* If we run into a non-nested `;', `}', or `]',
34059	     then the code is invalid -- but the default
34060	     argument is certainly over.  */
34061	case CPP_SEMICOLON:
34062	case CPP_CLOSE_BRACE:
34063	case CPP_CLOSE_SQUARE:
34064	  if (depth == 0
34065	      /* Handle correctly int n = sizeof ... ( p );  */
34066	      && token->type != CPP_ELLIPSIS)
34067	    done = true;
34068	  /* Update DEPTH, if necessary.  */
34069	  else if (token->type == CPP_CLOSE_PAREN
34070		   || token->type == CPP_CLOSE_BRACE
34071		   || token->type == CPP_CLOSE_SQUARE)
34072	    --depth;
34073	  break;
34074
34075	case CPP_OPEN_PAREN:
34076	case CPP_OPEN_SQUARE:
34077	case CPP_OPEN_BRACE:
34078	  ++depth;
34079	  break;
34080
34081	case CPP_LESS:
34082	  if (depth == 0)
34083	    /* This might be the comparison operator, or it might
34084	       start a template argument list.  */
34085	    ++maybe_template_id;
34086	  break;
34087
34088	case CPP_RSHIFT:
34089	  if (cxx_dialect == cxx98)
34090	    break;
34091	  /* Fall through for C++0x, which treats the `>>'
34092	     operator like two `>' tokens in certain
34093	     cases.  */
34094	  gcc_fallthrough ();
34095
34096	case CPP_GREATER:
34097	  if (depth == 0)
34098	    {
34099	      /* This might be an operator, or it might close a
34100		 template argument list.  But if a previous '<'
34101		 started a template argument list, this will have
34102		 closed it, so we can't be in one anymore.  */
34103	      maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
34104	      if (maybe_template_id < 0)
34105		maybe_template_id = 0;
34106	    }
34107	  break;
34108
34109	  /* If we run out of tokens, issue an error message.  */
34110	case CPP_EOF:
34111	case CPP_PRAGMA_EOL:
34112	  error_at (token->location, "file ends in default argument");
34113	  return error_mark_node;
34114
34115	case CPP_NAME:
34116	case CPP_SCOPE:
34117	  /* In these cases, we should look for template-ids.
34118	     For example, if the default argument is
34119	     `X<int, double>()', we need to do name lookup to
34120	     figure out whether or not `X' is a template; if
34121	     so, the `,' does not end the default argument.
34122
34123	     That is not yet done.  */
34124	  break;
34125
34126	default:
34127	  break;
34128	}
34129
34130      /* If we've reached the end, stop.  */
34131      if (done)
34132	break;
34133
34134      /* Add the token to the token block.  */
34135      token = cp_lexer_consume_token (parser->lexer);
34136    }
34137
34138  /* Create a DEFERRED_PARSE to represent the unparsed default
34139     argument.  */
34140  default_argument = make_node (DEFERRED_PARSE);
34141  DEFPARSE_TOKENS (default_argument)
34142    = cp_token_cache_new (first_token, token);
34143  DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
34144
34145  return default_argument;
34146}
34147
34148/* A location to use for diagnostics about an unparsed DEFERRED_PARSE.  */
34149
34150location_t
34151defparse_location (tree default_argument)
34152{
34153  cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
34154  location_t start = tokens->first->location;
34155  location_t end = tokens->last->location;
34156  return make_location (start, start, end);
34157}
34158
34159/* Begin parsing tentatively.  We always save tokens while parsing
34160   tentatively so that if the tentative parsing fails we can restore the
34161   tokens.  */
34162
34163static void
34164cp_parser_parse_tentatively (cp_parser* parser)
34165{
34166  /* Enter a new parsing context.  */
34167  parser->context = cp_parser_context_new (parser->context);
34168  /* Begin saving tokens.  */
34169  cp_lexer_save_tokens (parser->lexer);
34170  /* In order to avoid repetitive access control error messages,
34171     access checks are queued up until we are no longer parsing
34172     tentatively.  */
34173  push_deferring_access_checks (dk_deferred);
34174}
34175
34176/* Commit to the currently active tentative parse.  */
34177
34178static void
34179cp_parser_commit_to_tentative_parse (cp_parser* parser)
34180{
34181  cp_parser_context *context;
34182  cp_lexer *lexer;
34183
34184  /* Mark all of the levels as committed.  */
34185  lexer = parser->lexer;
34186  for (context = parser->context; context->next; context = context->next)
34187    {
34188      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34189	break;
34190      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34191      while (!cp_lexer_saving_tokens (lexer))
34192	lexer = lexer->next;
34193      cp_lexer_commit_tokens (lexer);
34194    }
34195}
34196
34197/* Commit to the topmost currently active tentative parse.
34198
34199   Note that this function shouldn't be called when there are
34200   irreversible side-effects while in a tentative state.  For
34201   example, we shouldn't create a permanent entry in the symbol
34202   table, or issue an error message that might not apply if the
34203   tentative parse is aborted.  */
34204
34205static void
34206cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
34207{
34208  cp_parser_context *context = parser->context;
34209  cp_lexer *lexer = parser->lexer;
34210
34211  if (context)
34212    {
34213      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34214	return;
34215      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34216
34217      while (!cp_lexer_saving_tokens (lexer))
34218	lexer = lexer->next;
34219      cp_lexer_commit_tokens (lexer);
34220    }
34221}
34222
34223/* Abort the currently active tentative parse.  All consumed tokens
34224   will be rolled back, and no diagnostics will be issued.  */
34225
34226static void
34227cp_parser_abort_tentative_parse (cp_parser* parser)
34228{
34229  gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
34230	      || errorcount > 0);
34231  cp_parser_simulate_error (parser);
34232  /* Now, pretend that we want to see if the construct was
34233     successfully parsed.  */
34234  cp_parser_parse_definitely (parser);
34235}
34236
34237/* Stop parsing tentatively.  If a parse error has occurred, restore the
34238   token stream.  Otherwise, commit to the tokens we have consumed.
34239   Returns true if no error occurred; false otherwise.  */
34240
34241static bool
34242cp_parser_parse_definitely (cp_parser* parser)
34243{
34244  bool error_occurred;
34245  cp_parser_context *context;
34246
34247  /* Remember whether or not an error occurred, since we are about to
34248     destroy that information.  */
34249  error_occurred = cp_parser_error_occurred (parser);
34250  /* Remove the topmost context from the stack.  */
34251  context = parser->context;
34252  parser->context = context->next;
34253  /* If no parse errors occurred, commit to the tentative parse.  */
34254  if (!error_occurred)
34255    {
34256      /* Commit to the tokens read tentatively, unless that was
34257	 already done.  */
34258      if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
34259	cp_lexer_commit_tokens (parser->lexer);
34260
34261      pop_to_parent_deferring_access_checks ();
34262    }
34263  /* Otherwise, if errors occurred, roll back our state so that things
34264     are just as they were before we began the tentative parse.  */
34265  else
34266    {
34267      cp_lexer_rollback_tokens (parser->lexer);
34268      pop_deferring_access_checks ();
34269    }
34270  /* Add the context to the front of the free list.  */
34271  context->next = cp_parser_context_free_list;
34272  cp_parser_context_free_list = context;
34273
34274  return !error_occurred;
34275}
34276
34277/* Returns true if we are parsing tentatively and are not committed to
34278   this tentative parse.  */
34279
34280static bool
34281cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
34282{
34283  return (cp_parser_parsing_tentatively (parser)
34284	  && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
34285}
34286
34287/* Returns nonzero iff an error has occurred during the most recent
34288   tentative parse.  */
34289
34290static bool
34291cp_parser_error_occurred (cp_parser* parser)
34292{
34293  return (cp_parser_parsing_tentatively (parser)
34294	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
34295}
34296
34297/* Returns nonzero if GNU extensions are allowed.  */
34298
34299static bool
34300cp_parser_allow_gnu_extensions_p (cp_parser* parser)
34301{
34302  return parser->allow_gnu_extensions_p;
34303}
34304
34305/* Objective-C++ Productions */
34306
34307
34308/* Parse an Objective-C expression, which feeds into a primary-expression
34309   above.
34310
34311   objc-expression:
34312     objc-message-expression
34313     objc-string-literal
34314     objc-encode-expression
34315     objc-protocol-expression
34316     objc-selector-expression
34317
34318  Returns a tree representation of the expression.  */
34319
34320static cp_expr
34321cp_parser_objc_expression (cp_parser* parser)
34322{
34323  /* Try to figure out what kind of declaration is present.  */
34324  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34325
34326  switch (kwd->type)
34327    {
34328    case CPP_OPEN_SQUARE:
34329      return cp_parser_objc_message_expression (parser);
34330
34331    case CPP_OBJC_STRING:
34332      kwd = cp_lexer_consume_token (parser->lexer);
34333      return objc_build_string_object (kwd->u.value);
34334
34335    case CPP_KEYWORD:
34336      switch (kwd->keyword)
34337	{
34338	case RID_AT_ENCODE:
34339	  return cp_parser_objc_encode_expression (parser);
34340
34341	case RID_AT_PROTOCOL:
34342	  return cp_parser_objc_protocol_expression (parser);
34343
34344	case RID_AT_SELECTOR:
34345	  return cp_parser_objc_selector_expression (parser);
34346
34347	default:
34348	  break;
34349	}
34350      /* FALLTHRU */
34351    default:
34352      error_at (kwd->location,
34353		"misplaced %<@%D%> Objective-C++ construct",
34354		kwd->u.value);
34355      cp_parser_skip_to_end_of_block_or_statement (parser);
34356    }
34357
34358  return error_mark_node;
34359}
34360
34361/* Parse an Objective-C message expression.
34362
34363   objc-message-expression:
34364     [ objc-message-receiver objc-message-args ]
34365
34366   Returns a representation of an Objective-C message.  */
34367
34368static tree
34369cp_parser_objc_message_expression (cp_parser* parser)
34370{
34371  tree receiver, messageargs;
34372
34373  parser->objective_c_message_context_p = true;
34374  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34375  cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
34376  receiver = cp_parser_objc_message_receiver (parser);
34377  messageargs = cp_parser_objc_message_args (parser);
34378  location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
34379  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
34380
34381  tree result = objc_build_message_expr (receiver, messageargs);
34382
34383  /* Construct a location e.g.
34384       [self func1:5]
34385       ^~~~~~~~~~~~~~
34386     ranging from the '[' to the ']', with the caret at the start.  */
34387  location_t combined_loc = make_location (start_loc, start_loc, end_loc);
34388  protected_set_expr_location (result, combined_loc);
34389
34390  parser->objective_c_message_context_p = false;
34391  return result;
34392}
34393
34394/* Parse an objc-message-receiver.
34395
34396   objc-message-receiver:
34397     expression
34398     simple-type-specifier
34399
34400  Returns a representation of the type or expression.  */
34401
34402static tree
34403cp_parser_objc_message_receiver (cp_parser* parser)
34404{
34405  tree rcv;
34406
34407  /* An Objective-C message receiver may be either (1) a type
34408     or (2) an expression.  */
34409  cp_parser_parse_tentatively (parser);
34410  rcv = cp_parser_expression (parser);
34411
34412  /* If that worked out, fine.  */
34413  if (cp_parser_parse_definitely (parser))
34414    return rcv;
34415
34416  cp_parser_parse_tentatively (parser);
34417  rcv = cp_parser_simple_type_specifier (parser,
34418					 /*decl_specs=*/NULL,
34419					 CP_PARSER_FLAGS_NONE);
34420
34421  if (cp_parser_parse_definitely (parser))
34422    return objc_get_class_reference (rcv);
34423
34424  cp_parser_error (parser, "objective-c++ message receiver expected");
34425  return error_mark_node;
34426}
34427
34428/* Parse the arguments and selectors comprising an Objective-C message.
34429
34430   objc-message-args:
34431     objc-selector
34432     objc-selector-args
34433     objc-selector-args , objc-comma-args
34434
34435   objc-selector-args:
34436     objc-selector [opt] : assignment-expression
34437     objc-selector-args objc-selector [opt] : assignment-expression
34438
34439   objc-comma-args:
34440     assignment-expression
34441     objc-comma-args , assignment-expression
34442
34443   Returns a TREE_LIST, with TREE_PURPOSE containing a list of
34444   selector arguments and TREE_VALUE containing a list of comma
34445   arguments.  */
34446
34447static tree
34448cp_parser_objc_message_args (cp_parser* parser)
34449{
34450  tree sel_args = NULL_TREE, addl_args = NULL_TREE;
34451  bool maybe_unary_selector_p = true;
34452  cp_token *token = cp_lexer_peek_token (parser->lexer);
34453
34454  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34455    {
34456      tree selector = NULL_TREE, arg;
34457
34458      if (token->type != CPP_COLON)
34459	selector = cp_parser_objc_selector (parser);
34460
34461      /* Detect if we have a unary selector.  */
34462      if (maybe_unary_selector_p
34463	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34464	return build_tree_list (selector, NULL_TREE);
34465
34466      maybe_unary_selector_p = false;
34467      cp_parser_require (parser, CPP_COLON, RT_COLON);
34468      arg = cp_parser_assignment_expression (parser);
34469
34470      sel_args
34471	= chainon (sel_args,
34472		   build_tree_list (selector, arg));
34473
34474      token = cp_lexer_peek_token (parser->lexer);
34475    }
34476
34477  /* Handle non-selector arguments, if any. */
34478  while (token->type == CPP_COMMA)
34479    {
34480      tree arg;
34481
34482      cp_lexer_consume_token (parser->lexer);
34483      arg = cp_parser_assignment_expression (parser);
34484
34485      addl_args
34486	= chainon (addl_args,
34487		   build_tree_list (NULL_TREE, arg));
34488
34489      token = cp_lexer_peek_token (parser->lexer);
34490    }
34491
34492  if (sel_args == NULL_TREE && addl_args == NULL_TREE)
34493    {
34494      cp_parser_error (parser, "objective-c++ message argument(s) are expected");
34495      return build_tree_list (error_mark_node, error_mark_node);
34496    }
34497
34498  return build_tree_list (sel_args, addl_args);
34499}
34500
34501/* Parse an Objective-C encode expression.
34502
34503   objc-encode-expression:
34504     @encode objc-typename
34505
34506   Returns an encoded representation of the type argument.  */
34507
34508static cp_expr
34509cp_parser_objc_encode_expression (cp_parser* parser)
34510{
34511  tree type;
34512  cp_token *token;
34513  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34514
34515  cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
34516  matching_parens parens;
34517  parens.require_open (parser);
34518  token = cp_lexer_peek_token (parser->lexer);
34519  type = complete_type (cp_parser_type_id (parser));
34520  parens.require_close (parser);
34521
34522  if (!type)
34523    {
34524      error_at (token->location,
34525		"%<@encode%> must specify a type as an argument");
34526      return error_mark_node;
34527    }
34528
34529  /* This happens if we find @encode(T) (where T is a template
34530     typename or something dependent on a template typename) when
34531     parsing a template.  In that case, we can't compile it
34532     immediately, but we rather create an AT_ENCODE_EXPR which will
34533     need to be instantiated when the template is used.
34534  */
34535  if (dependent_type_p (type))
34536    {
34537      tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
34538      TREE_READONLY (value) = 1;
34539      return value;
34540    }
34541
34542
34543  /* Build a location of the form:
34544       @encode(int)
34545       ^~~~~~~~~~~~
34546     with caret==start at the @ token, finishing at the close paren.  */
34547  location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34548
34549  return cp_expr (objc_build_encode_expr (type), combined_loc);
34550}
34551
34552/* Parse an Objective-C @defs expression.  */
34553
34554static tree
34555cp_parser_objc_defs_expression (cp_parser *parser)
34556{
34557  tree name;
34558
34559  cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
34560  matching_parens parens;
34561  parens.require_open (parser);
34562  name = cp_parser_identifier (parser);
34563  parens.require_close (parser);
34564
34565  return objc_get_class_ivars (name);
34566}
34567
34568/* Parse an Objective-C protocol expression.
34569
34570  objc-protocol-expression:
34571    @protocol ( identifier )
34572
34573  Returns a representation of the protocol expression.  */
34574
34575static tree
34576cp_parser_objc_protocol_expression (cp_parser* parser)
34577{
34578  tree proto;
34579  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34580
34581  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
34582  matching_parens parens;
34583  parens.require_open (parser);
34584  proto = cp_parser_identifier (parser);
34585  parens.require_close (parser);
34586
34587  /* Build a location of the form:
34588       @protocol(prot)
34589       ^~~~~~~~~~~~~~~
34590     with caret==start at the @ token, finishing at the close paren.  */
34591  location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34592  tree result = objc_build_protocol_expr (proto);
34593  protected_set_expr_location (result, combined_loc);
34594  return result;
34595}
34596
34597/* Parse an Objective-C selector expression.
34598
34599   objc-selector-expression:
34600     @selector ( objc-method-signature )
34601
34602   objc-method-signature:
34603     objc-selector
34604     objc-selector-seq
34605
34606   objc-selector-seq:
34607     objc-selector :
34608     objc-selector-seq objc-selector :
34609
34610  Returns a representation of the method selector.  */
34611
34612static tree
34613cp_parser_objc_selector_expression (cp_parser* parser)
34614{
34615  tree sel_seq = NULL_TREE;
34616  bool maybe_unary_selector_p = true;
34617  cp_token *token;
34618  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34619
34620  cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
34621  matching_parens parens;
34622  parens.require_open (parser);
34623  token = cp_lexer_peek_token (parser->lexer);
34624
34625  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
34626	 || token->type == CPP_SCOPE)
34627    {
34628      tree selector = NULL_TREE;
34629
34630      if (token->type != CPP_COLON
34631	  || token->type == CPP_SCOPE)
34632	selector = cp_parser_objc_selector (parser);
34633
34634      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
34635	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
34636	{
34637	  /* Detect if we have a unary selector.  */
34638	  if (maybe_unary_selector_p)
34639	    {
34640	      sel_seq = selector;
34641	      goto finish_selector;
34642	    }
34643	  else
34644	    {
34645	      cp_parser_error (parser, "expected %<:%>");
34646	    }
34647	}
34648      maybe_unary_selector_p = false;
34649      token = cp_lexer_consume_token (parser->lexer);
34650
34651      if (token->type == CPP_SCOPE)
34652	{
34653	  sel_seq
34654	    = chainon (sel_seq,
34655		       build_tree_list (selector, NULL_TREE));
34656	  sel_seq
34657	    = chainon (sel_seq,
34658		       build_tree_list (NULL_TREE, NULL_TREE));
34659	}
34660      else
34661	sel_seq
34662	  = chainon (sel_seq,
34663		     build_tree_list (selector, NULL_TREE));
34664
34665      token = cp_lexer_peek_token (parser->lexer);
34666    }
34667
34668 finish_selector:
34669  parens.require_close (parser);
34670
34671
34672  /* Build a location of the form:
34673       @selector(func)
34674       ^~~~~~~~~~~~~~~
34675     with caret==start at the @ token, finishing at the close paren.  */
34676  location_t combined_loc = make_location (loc, loc, parser->lexer);
34677  tree result = objc_build_selector_expr (combined_loc, sel_seq);
34678  /* TODO: objc_build_selector_expr doesn't always honor the location.  */
34679  protected_set_expr_location (result, combined_loc);
34680  return result;
34681}
34682
34683/* Parse a list of identifiers.
34684
34685   objc-identifier-list:
34686     identifier
34687     objc-identifier-list , identifier
34688
34689   Returns a TREE_LIST of identifier nodes.  */
34690
34691static tree
34692cp_parser_objc_identifier_list (cp_parser* parser)
34693{
34694  tree identifier;
34695  tree list;
34696  cp_token *sep;
34697
34698  identifier = cp_parser_identifier (parser);
34699  if (identifier == error_mark_node)
34700    return error_mark_node;
34701
34702  list = build_tree_list (NULL_TREE, identifier);
34703  sep = cp_lexer_peek_token (parser->lexer);
34704
34705  while (sep->type == CPP_COMMA)
34706    {
34707      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
34708      identifier = cp_parser_identifier (parser);
34709      if (identifier == error_mark_node)
34710	return list;
34711
34712      list = chainon (list, build_tree_list (NULL_TREE,
34713					     identifier));
34714      sep = cp_lexer_peek_token (parser->lexer);
34715    }
34716
34717  return list;
34718}
34719
34720/* Parse an Objective-C alias declaration.
34721
34722   objc-alias-declaration:
34723     @compatibility_alias identifier identifier ;
34724
34725   This function registers the alias mapping with the Objective-C front end.
34726   It returns nothing.  */
34727
34728static void
34729cp_parser_objc_alias_declaration (cp_parser* parser)
34730{
34731  tree alias, orig;
34732
34733  cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
34734  alias = cp_parser_identifier (parser);
34735  orig = cp_parser_identifier (parser);
34736  objc_declare_alias (alias, orig);
34737  cp_parser_consume_semicolon_at_end_of_statement (parser);
34738}
34739
34740/* Parse an Objective-C class forward-declaration.
34741
34742   objc-class-declaration:
34743     @class objc-identifier-list ;
34744
34745   The function registers the forward declarations with the Objective-C
34746   front end.  It returns nothing.  */
34747
34748static void
34749cp_parser_objc_class_declaration (cp_parser* parser)
34750{
34751  cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
34752  while (true)
34753    {
34754      tree id;
34755
34756      id = cp_parser_identifier (parser);
34757      if (id == error_mark_node)
34758	break;
34759
34760      objc_declare_class (id);
34761
34762      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34763	cp_lexer_consume_token (parser->lexer);
34764      else
34765	break;
34766    }
34767  cp_parser_consume_semicolon_at_end_of_statement (parser);
34768}
34769
34770/* Parse a list of Objective-C protocol references.
34771
34772   objc-protocol-refs-opt:
34773     objc-protocol-refs [opt]
34774
34775   objc-protocol-refs:
34776     < objc-identifier-list >
34777
34778   Returns a TREE_LIST of identifiers, if any.  */
34779
34780static tree
34781cp_parser_objc_protocol_refs_opt (cp_parser* parser)
34782{
34783  tree protorefs = NULL_TREE;
34784
34785  if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
34786    {
34787      cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
34788      protorefs = cp_parser_objc_identifier_list (parser);
34789      cp_parser_require (parser, CPP_GREATER, RT_GREATER);
34790    }
34791
34792  return protorefs;
34793}
34794
34795/* Parse a Objective-C visibility specification.  */
34796
34797static void
34798cp_parser_objc_visibility_spec (cp_parser* parser)
34799{
34800  cp_token *vis = cp_lexer_peek_token (parser->lexer);
34801
34802  switch (vis->keyword)
34803    {
34804    case RID_AT_PRIVATE:
34805      objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
34806      break;
34807    case RID_AT_PROTECTED:
34808      objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
34809      break;
34810    case RID_AT_PUBLIC:
34811      objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
34812      break;
34813    case RID_AT_PACKAGE:
34814      objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
34815      break;
34816    default:
34817      return;
34818    }
34819
34820  /* Eat '@private'/'@protected'/'@public'.  */
34821  cp_lexer_consume_token (parser->lexer);
34822}
34823
34824/* Parse an Objective-C method type.  Return 'true' if it is a class
34825   (+) method, and 'false' if it is an instance (-) method.  */
34826
34827static inline bool
34828cp_parser_objc_method_type (cp_parser* parser)
34829{
34830  if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
34831    return true;
34832  else
34833    return false;
34834}
34835
34836/* Parse an Objective-C protocol qualifier.  */
34837
34838static tree
34839cp_parser_objc_protocol_qualifiers (cp_parser* parser)
34840{
34841  tree quals = NULL_TREE, node;
34842  cp_token *token = cp_lexer_peek_token (parser->lexer);
34843
34844  node = token->u.value;
34845
34846  while (node && identifier_p (node)
34847	 && (node == ridpointers [(int) RID_IN]
34848	     || node == ridpointers [(int) RID_OUT]
34849	     || node == ridpointers [(int) RID_INOUT]
34850	     || node == ridpointers [(int) RID_BYCOPY]
34851	     || node == ridpointers [(int) RID_BYREF]
34852	     || node == ridpointers [(int) RID_ONEWAY]))
34853    {
34854      quals = tree_cons (NULL_TREE, node, quals);
34855      cp_lexer_consume_token (parser->lexer);
34856      token = cp_lexer_peek_token (parser->lexer);
34857      node = token->u.value;
34858    }
34859
34860  return quals;
34861}
34862
34863/* Parse an Objective-C typename.  */
34864
34865static tree
34866cp_parser_objc_typename (cp_parser* parser)
34867{
34868  tree type_name = NULL_TREE;
34869
34870  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34871    {
34872      tree proto_quals, cp_type = NULL_TREE;
34873
34874      matching_parens parens;
34875      parens.consume_open (parser); /* Eat '('.  */
34876      proto_quals = cp_parser_objc_protocol_qualifiers (parser);
34877
34878      /* An ObjC type name may consist of just protocol qualifiers, in which
34879	 case the type shall default to 'id'.  */
34880      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34881	{
34882	  cp_type = cp_parser_type_id (parser);
34883
34884	  /* If the type could not be parsed, an error has already
34885	     been produced.  For error recovery, behave as if it had
34886	     not been specified, which will use the default type
34887	     'id'.  */
34888	  if (cp_type == error_mark_node)
34889	    {
34890	      cp_type = NULL_TREE;
34891	      /* We need to skip to the closing parenthesis as
34892		 cp_parser_type_id() does not seem to do it for
34893		 us.  */
34894	      cp_parser_skip_to_closing_parenthesis (parser,
34895						     /*recovering=*/true,
34896						     /*or_comma=*/false,
34897						     /*consume_paren=*/false);
34898	    }
34899	}
34900
34901      parens.require_close (parser);
34902      type_name = build_tree_list (proto_quals, cp_type);
34903    }
34904
34905  return type_name;
34906}
34907
34908/* Check to see if TYPE refers to an Objective-C selector name.  */
34909
34910static bool
34911cp_parser_objc_selector_p (enum cpp_ttype type)
34912{
34913  return (type == CPP_NAME || type == CPP_KEYWORD
34914	  || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
34915	  || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
34916	  || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
34917	  || type == CPP_XOR || type == CPP_XOR_EQ);
34918}
34919
34920/* Parse an Objective-C selector.  */
34921
34922static tree
34923cp_parser_objc_selector (cp_parser* parser)
34924{
34925  cp_token *token = cp_lexer_consume_token (parser->lexer);
34926
34927  if (!cp_parser_objc_selector_p (token->type))
34928    {
34929      error_at (token->location, "invalid Objective-C++ selector name");
34930      return error_mark_node;
34931    }
34932
34933  /* C++ operator names are allowed to appear in ObjC selectors.  */
34934  switch (token->type)
34935    {
34936    case CPP_AND_AND: return get_identifier ("and");
34937    case CPP_AND_EQ: return get_identifier ("and_eq");
34938    case CPP_AND: return get_identifier ("bitand");
34939    case CPP_OR: return get_identifier ("bitor");
34940    case CPP_COMPL: return get_identifier ("compl");
34941    case CPP_NOT: return get_identifier ("not");
34942    case CPP_NOT_EQ: return get_identifier ("not_eq");
34943    case CPP_OR_OR: return get_identifier ("or");
34944    case CPP_OR_EQ: return get_identifier ("or_eq");
34945    case CPP_XOR: return get_identifier ("xor");
34946    case CPP_XOR_EQ: return get_identifier ("xor_eq");
34947    default: return token->u.value;
34948    }
34949}
34950
34951/* Parse an Objective-C params list.  */
34952
34953static tree
34954cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
34955{
34956  tree params = NULL_TREE;
34957  bool maybe_unary_selector_p = true;
34958  cp_token *token = cp_lexer_peek_token (parser->lexer);
34959
34960  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34961    {
34962      tree selector = NULL_TREE, type_name, identifier;
34963      tree parm_attr = NULL_TREE;
34964
34965      if (token->keyword == RID_ATTRIBUTE)
34966	break;
34967
34968      if (token->type != CPP_COLON)
34969	selector = cp_parser_objc_selector (parser);
34970
34971      /* Detect if we have a unary selector.  */
34972      if (maybe_unary_selector_p
34973	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34974	{
34975	  params = selector; /* Might be followed by attributes.  */
34976	  break;
34977	}
34978
34979      maybe_unary_selector_p = false;
34980      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34981	{
34982	  /* Something went quite wrong.  There should be a colon
34983	     here, but there is not.  Stop parsing parameters.  */
34984	  break;
34985	}
34986      type_name = cp_parser_objc_typename (parser);
34987      /* New ObjC allows attributes on parameters too.  */
34988      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
34989	parm_attr = cp_parser_attributes_opt (parser);
34990      identifier = cp_parser_identifier (parser);
34991
34992      params
34993	= chainon (params,
34994		   objc_build_keyword_decl (selector,
34995					    type_name,
34996					    identifier,
34997					    parm_attr));
34998
34999      token = cp_lexer_peek_token (parser->lexer);
35000    }
35001
35002  if (params == NULL_TREE)
35003    {
35004      cp_parser_error (parser, "objective-c++ method declaration is expected");
35005      return error_mark_node;
35006    }
35007
35008  /* We allow tail attributes for the method.  */
35009  if (token->keyword == RID_ATTRIBUTE)
35010    {
35011      *attributes = cp_parser_attributes_opt (parser);
35012      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35013	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35014	return params;
35015      cp_parser_error (parser,
35016		       "method attributes must be specified at the end");
35017      return error_mark_node;
35018    }
35019
35020  if (params == NULL_TREE)
35021    {
35022      cp_parser_error (parser, "objective-c++ method declaration is expected");
35023      return error_mark_node;
35024    }
35025  return params;
35026}
35027
35028/* Parse the non-keyword Objective-C params.  */
35029
35030static tree
35031cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
35032				       tree* attributes)
35033{
35034  tree params = make_node (TREE_LIST);
35035  cp_token *token = cp_lexer_peek_token (parser->lexer);
35036  *ellipsisp = false;  /* Initially, assume no ellipsis.  */
35037
35038  while (token->type == CPP_COMMA)
35039    {
35040      cp_parameter_declarator *parmdecl;
35041      tree parm;
35042
35043      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
35044      token = cp_lexer_peek_token (parser->lexer);
35045
35046      if (token->type == CPP_ELLIPSIS)
35047	{
35048	  cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
35049	  *ellipsisp = true;
35050	  token = cp_lexer_peek_token (parser->lexer);
35051	  break;
35052	}
35053
35054      /* TODO: parse attributes for tail parameters.  */
35055      parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35056						  false, NULL);
35057      parm = grokdeclarator (parmdecl->declarator,
35058			     &parmdecl->decl_specifiers,
35059			     PARM, /*initialized=*/0,
35060			     /*attrlist=*/NULL);
35061
35062      chainon (params, build_tree_list (NULL_TREE, parm));
35063      token = cp_lexer_peek_token (parser->lexer);
35064    }
35065
35066  /* We allow tail attributes for the method.  */
35067  if (token->keyword == RID_ATTRIBUTE)
35068    {
35069      if (*attributes == NULL_TREE)
35070	{
35071	  *attributes = cp_parser_attributes_opt (parser);
35072	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35073	      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35074	    return params;
35075	}
35076      else
35077	/* We have an error, but parse the attributes, so that we can
35078	   carry on.  */
35079	*attributes = cp_parser_attributes_opt (parser);
35080
35081      cp_parser_error (parser,
35082		       "method attributes must be specified at the end");
35083      return error_mark_node;
35084    }
35085
35086  return params;
35087}
35088
35089/* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
35090
35091static void
35092cp_parser_objc_interstitial_code (cp_parser* parser)
35093{
35094  cp_token *token = cp_lexer_peek_token (parser->lexer);
35095
35096  /* If the next token is `extern' and the following token is a string
35097     literal, then we have a linkage specification.  */
35098  if (token->keyword == RID_EXTERN
35099      && cp_parser_is_pure_string_literal
35100	 (cp_lexer_peek_nth_token (parser->lexer, 2)))
35101    cp_parser_linkage_specification (parser, NULL_TREE);
35102  /* Handle #pragma, if any.  */
35103  else if (token->type == CPP_PRAGMA)
35104    cp_parser_pragma (parser, pragma_objc_icode, NULL);
35105  /* Allow stray semicolons.  */
35106  else if (token->type == CPP_SEMICOLON)
35107    cp_lexer_consume_token (parser->lexer);
35108  /* Mark methods as optional or required, when building protocols.  */
35109  else if (token->keyword == RID_AT_OPTIONAL)
35110    {
35111      cp_lexer_consume_token (parser->lexer);
35112      objc_set_method_opt (true);
35113    }
35114  else if (token->keyword == RID_AT_REQUIRED)
35115    {
35116      cp_lexer_consume_token (parser->lexer);
35117      objc_set_method_opt (false);
35118    }
35119  else if (token->keyword == RID_NAMESPACE)
35120    cp_parser_namespace_definition (parser);
35121  /* Other stray characters must generate errors.  */
35122  else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
35123    {
35124      cp_lexer_consume_token (parser->lexer);
35125      error ("stray %qs between Objective-C++ methods",
35126	     token->type == CPP_OPEN_BRACE ? "{" : "}");
35127    }
35128  /* Finally, try to parse a block-declaration, or a function-definition.  */
35129  else
35130    cp_parser_block_declaration (parser, /*statement_p=*/false);
35131}
35132
35133/* Parse a method signature.  */
35134
35135static tree
35136cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
35137{
35138  tree rettype, kwdparms, optparms;
35139  bool ellipsis = false;
35140  bool is_class_method;
35141
35142  is_class_method = cp_parser_objc_method_type (parser);
35143  rettype = cp_parser_objc_typename (parser);
35144  *attributes = NULL_TREE;
35145  kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
35146  if (kwdparms == error_mark_node)
35147    return error_mark_node;
35148  optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
35149  if (optparms == error_mark_node)
35150    return error_mark_node;
35151
35152  return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
35153}
35154
35155static bool
35156cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
35157{
35158  tree tattr;
35159  cp_lexer_save_tokens (parser->lexer);
35160  tattr = cp_parser_attributes_opt (parser);
35161  gcc_assert (tattr) ;
35162
35163  /* If the attributes are followed by a method introducer, this is not allowed.
35164     Dump the attributes and flag the situation.  */
35165  if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
35166      || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
35167    return true;
35168
35169  /* Otherwise, the attributes introduce some interstitial code, possibly so
35170     rewind to allow that check.  */
35171  cp_lexer_rollback_tokens (parser->lexer);
35172  return false;
35173}
35174
35175/* Parse an Objective-C method prototype list.  */
35176
35177static void
35178cp_parser_objc_method_prototype_list (cp_parser* parser)
35179{
35180  cp_token *token = cp_lexer_peek_token (parser->lexer);
35181
35182  while (token->keyword != RID_AT_END && token->type != CPP_EOF)
35183    {
35184      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35185	{
35186	  tree attributes, sig;
35187	  bool is_class_method;
35188	  if (token->type == CPP_PLUS)
35189	    is_class_method = true;
35190	  else
35191	    is_class_method = false;
35192	  sig = cp_parser_objc_method_signature (parser, &attributes);
35193	  if (sig == error_mark_node)
35194	    {
35195	      cp_parser_skip_to_end_of_block_or_statement (parser);
35196	      token = cp_lexer_peek_token (parser->lexer);
35197	      continue;
35198	    }
35199	  objc_add_method_declaration (is_class_method, sig, attributes);
35200	  cp_parser_consume_semicolon_at_end_of_statement (parser);
35201	}
35202      else if (token->keyword == RID_AT_PROPERTY)
35203	cp_parser_objc_at_property_declaration (parser);
35204      else if (token->keyword == RID_ATTRIBUTE
35205      	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35206	warning_at (cp_lexer_peek_token (parser->lexer)->location,
35207		    OPT_Wattributes,
35208		    "prefix attributes are ignored for methods");
35209      else
35210	/* Allow for interspersed non-ObjC++ code.  */
35211	cp_parser_objc_interstitial_code (parser);
35212
35213      token = cp_lexer_peek_token (parser->lexer);
35214    }
35215
35216  if (token->type != CPP_EOF)
35217    cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
35218  else
35219    cp_parser_error (parser, "expected %<@end%>");
35220
35221  objc_finish_interface ();
35222}
35223
35224/* Parse an Objective-C method definition list.  */
35225
35226static void
35227cp_parser_objc_method_definition_list (cp_parser* parser)
35228{
35229  for (;;)
35230    {
35231      cp_token *token = cp_lexer_peek_token (parser->lexer);
35232
35233      if (token->keyword == RID_AT_END)
35234	{
35235	  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
35236	  break;
35237	}
35238      else if (token->type == CPP_EOF)
35239	{
35240	  cp_parser_error (parser, "expected %<@end%>");
35241	  break;
35242	}
35243      else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35244	{
35245	  bool is_class_method = token->type == CPP_PLUS;
35246
35247	  push_deferring_access_checks (dk_deferred);
35248	  tree attribute;
35249	  tree sig = cp_parser_objc_method_signature (parser, &attribute);
35250	  if (sig == error_mark_node)
35251	    cp_parser_skip_to_end_of_block_or_statement (parser);
35252	  else
35253	    {
35254	      objc_start_method_definition (is_class_method, sig,
35255					    attribute, NULL_TREE);
35256
35257	      /* For historical reasons, we accept an optional semicolon.  */
35258	      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35259		cp_lexer_consume_token (parser->lexer);
35260
35261	      perform_deferred_access_checks (tf_warning_or_error);
35262	      stop_deferring_access_checks ();
35263	      tree meth
35264		= cp_parser_function_definition_after_declarator (parser, false);
35265	      pop_deferring_access_checks ();
35266	      objc_finish_method_definition (meth);
35267	    }
35268	}
35269      /* The following case will be removed once @synthesize is
35270	 completely implemented.  */
35271      else if (token->keyword == RID_AT_PROPERTY)
35272	cp_parser_objc_at_property_declaration (parser);
35273      else if (token->keyword == RID_AT_SYNTHESIZE)
35274	cp_parser_objc_at_synthesize_declaration (parser);
35275      else if (token->keyword == RID_AT_DYNAMIC)
35276	cp_parser_objc_at_dynamic_declaration (parser);
35277      else if (token->keyword == RID_ATTRIBUTE
35278      	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35279	warning_at (token->location, OPT_Wattributes,
35280	       	    "prefix attributes are ignored for methods");
35281      else
35282	/* Allow for interspersed non-ObjC++ code.  */
35283	cp_parser_objc_interstitial_code (parser);
35284    }
35285
35286  objc_finish_implementation ();
35287}
35288
35289/* Parse Objective-C ivars.  */
35290
35291static void
35292cp_parser_objc_class_ivars (cp_parser* parser)
35293{
35294  cp_token *token = cp_lexer_peek_token (parser->lexer);
35295
35296  if (token->type != CPP_OPEN_BRACE)
35297    return;	/* No ivars specified.  */
35298
35299  cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
35300  token = cp_lexer_peek_token (parser->lexer);
35301
35302  while (token->type != CPP_CLOSE_BRACE
35303	&& token->keyword != RID_AT_END && token->type != CPP_EOF)
35304    {
35305      cp_decl_specifier_seq declspecs;
35306      int decl_class_or_enum_p;
35307      tree prefix_attributes;
35308
35309      cp_parser_objc_visibility_spec (parser);
35310
35311      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35312	break;
35313
35314      cp_parser_decl_specifier_seq (parser,
35315				    CP_PARSER_FLAGS_OPTIONAL,
35316				    &declspecs,
35317				    &decl_class_or_enum_p);
35318
35319      /* auto, register, static, extern, mutable.  */
35320      if (declspecs.storage_class != sc_none)
35321	{
35322	  cp_parser_error (parser, "invalid type for instance variable");
35323	  declspecs.storage_class = sc_none;
35324	}
35325
35326      /* thread_local.  */
35327      if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35328	{
35329	  cp_parser_error (parser, "invalid type for instance variable");
35330	  declspecs.locations[ds_thread] = 0;
35331	}
35332
35333      /* typedef.  */
35334      if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35335	{
35336	  cp_parser_error (parser, "invalid type for instance variable");
35337	  declspecs.locations[ds_typedef] = 0;
35338	}
35339
35340      prefix_attributes = declspecs.attributes;
35341      declspecs.attributes = NULL_TREE;
35342
35343      /* Keep going until we hit the `;' at the end of the
35344	 declaration.  */
35345      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35346	{
35347	  tree width = NULL_TREE, attributes, first_attribute, decl;
35348	  cp_declarator *declarator = NULL;
35349	  int ctor_dtor_or_conv_p;
35350
35351	  /* Check for a (possibly unnamed) bitfield declaration.  */
35352	  token = cp_lexer_peek_token (parser->lexer);
35353	  if (token->type == CPP_COLON)
35354	    goto eat_colon;
35355
35356	  if (token->type == CPP_NAME
35357	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
35358		  == CPP_COLON))
35359	    {
35360	      /* Get the name of the bitfield.  */
35361	      declarator = make_id_declarator (NULL_TREE,
35362					       cp_parser_identifier (parser),
35363					       sfk_none, token->location);
35364
35365	     eat_colon:
35366	      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
35367	      /* Get the width of the bitfield.  */
35368	      width
35369		= cp_parser_constant_expression (parser);
35370	    }
35371	  else
35372	    {
35373	      /* Parse the declarator.  */
35374	      declarator
35375		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35376					CP_PARSER_FLAGS_NONE,
35377					&ctor_dtor_or_conv_p,
35378					/*parenthesized_p=*/NULL,
35379					/*member_p=*/false,
35380					/*friend_p=*/false,
35381					/*static_p=*/false);
35382	    }
35383
35384	  /* Look for attributes that apply to the ivar.  */
35385	  attributes = cp_parser_attributes_opt (parser);
35386	  /* Remember which attributes are prefix attributes and
35387	     which are not.  */
35388	  first_attribute = attributes;
35389	  /* Combine the attributes.  */
35390	  attributes = attr_chainon (prefix_attributes, attributes);
35391
35392	  if (width)
35393	    /* Create the bitfield declaration.  */
35394	    decl = grokbitfield (declarator, &declspecs,
35395				 width, NULL_TREE, attributes);
35396	  else
35397	    decl = grokfield (declarator, &declspecs,
35398			      NULL_TREE, /*init_const_expr_p=*/false,
35399			      NULL_TREE, attributes);
35400
35401	  /* Add the instance variable.  */
35402	  if (decl != error_mark_node && decl != NULL_TREE)
35403	    objc_add_instance_variable (decl);
35404
35405	  /* Reset PREFIX_ATTRIBUTES.  */
35406	  if (attributes != error_mark_node)
35407	    {
35408	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
35409		attributes = TREE_CHAIN (attributes);
35410	      if (attributes)
35411		TREE_CHAIN (attributes) = NULL_TREE;
35412	    }
35413
35414	  token = cp_lexer_peek_token (parser->lexer);
35415
35416	  if (token->type == CPP_COMMA)
35417	    {
35418	      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
35419	      continue;
35420	    }
35421	  break;
35422	}
35423
35424      cp_parser_consume_semicolon_at_end_of_statement (parser);
35425      token = cp_lexer_peek_token (parser->lexer);
35426    }
35427
35428  if (token->keyword == RID_AT_END)
35429    cp_parser_error (parser, "expected %<}%>");
35430
35431  /* Do not consume the RID_AT_END, so it will be read again as terminating
35432     the @interface of @implementation.  */
35433  if (token->keyword != RID_AT_END && token->type != CPP_EOF)
35434    cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
35435
35436  /* For historical reasons, we accept an optional semicolon.  */
35437  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35438    cp_lexer_consume_token (parser->lexer);
35439}
35440
35441/* Parse an Objective-C protocol declaration.  */
35442
35443static void
35444cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
35445{
35446  tree proto, protorefs;
35447  cp_token *tok;
35448
35449  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
35450  if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35451    {
35452      tok = cp_lexer_peek_token (parser->lexer);
35453      error_at (tok->location, "identifier expected after %<@protocol%>");
35454      cp_parser_consume_semicolon_at_end_of_statement (parser);
35455      return;
35456    }
35457
35458  /* See if we have a forward declaration or a definition.  */
35459  tok = cp_lexer_peek_nth_token (parser->lexer, 2);
35460
35461  /* Try a forward declaration first.  */
35462  if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
35463    {
35464      while (true)
35465	{
35466	  tree id;
35467
35468	  id = cp_parser_identifier (parser);
35469	  if (id == error_mark_node)
35470	    break;
35471
35472	  objc_declare_protocol (id, attributes);
35473
35474	  if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35475	    cp_lexer_consume_token (parser->lexer);
35476	  else
35477	    break;
35478	}
35479      cp_parser_consume_semicolon_at_end_of_statement (parser);
35480    }
35481
35482  /* Ok, we got a full-fledged definition (or at least should).  */
35483  else
35484    {
35485      proto = cp_parser_identifier (parser);
35486      protorefs = cp_parser_objc_protocol_refs_opt (parser);
35487      objc_start_protocol (proto, protorefs, attributes);
35488      cp_parser_objc_method_prototype_list (parser);
35489    }
35490}
35491
35492/* Parse an Objective-C superclass or category.  */
35493
35494static void
35495cp_parser_objc_superclass_or_category (cp_parser *parser,
35496				       bool iface_p,
35497				       tree *super,
35498				       tree *categ, bool *is_class_extension)
35499{
35500  cp_token *next = cp_lexer_peek_token (parser->lexer);
35501
35502  *super = *categ = NULL_TREE;
35503  *is_class_extension = false;
35504  if (next->type == CPP_COLON)
35505    {
35506      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
35507      *super = cp_parser_identifier (parser);
35508    }
35509  else if (next->type == CPP_OPEN_PAREN)
35510    {
35511      matching_parens parens;
35512      parens.consume_open (parser);  /* Eat '('.  */
35513
35514      /* If there is no category name, and this is an @interface, we
35515	 have a class extension.  */
35516      if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35517	{
35518	  *categ = NULL_TREE;
35519	  *is_class_extension = true;
35520	}
35521      else
35522	*categ = cp_parser_identifier (parser);
35523
35524      parens.require_close (parser);
35525    }
35526}
35527
35528/* Parse an Objective-C class interface.  */
35529
35530static void
35531cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
35532{
35533  tree name, super, categ, protos;
35534  bool is_class_extension;
35535
35536  cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
35537  location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
35538  name = cp_parser_identifier (parser);
35539  if (name == error_mark_node)
35540    {
35541      /* It's hard to recover because even if valid @interface stuff
35542	 is to follow, we can't compile it (or validate it) if we
35543	 don't even know which class it refers to.  Let's assume this
35544	 was a stray '@interface' token in the stream and skip it.
35545      */
35546      return;
35547    }
35548  cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
35549					 &is_class_extension);
35550  protos = cp_parser_objc_protocol_refs_opt (parser);
35551
35552  /* We have either a class or a category on our hands.  */
35553  if (categ || is_class_extension)
35554    objc_start_category_interface (name, categ, protos, attributes);
35555  else
35556    {
35557      objc_start_class_interface (name, nam_loc, super, protos, attributes);
35558      /* Handle instance variable declarations, if any.  */
35559      cp_parser_objc_class_ivars (parser);
35560      objc_continue_interface ();
35561    }
35562
35563  cp_parser_objc_method_prototype_list (parser);
35564}
35565
35566/* Parse an Objective-C class implementation.  */
35567
35568static void
35569cp_parser_objc_class_implementation (cp_parser* parser)
35570{
35571  tree name, super, categ;
35572  bool is_class_extension;
35573
35574  cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
35575  name = cp_parser_identifier (parser);
35576  if (name == error_mark_node)
35577    {
35578      /* It's hard to recover because even if valid @implementation
35579	 stuff is to follow, we can't compile it (or validate it) if
35580	 we don't even know which class it refers to.  Let's assume
35581	 this was a stray '@implementation' token in the stream and
35582	 skip it.
35583      */
35584      return;
35585    }
35586  cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
35587					 &is_class_extension);
35588
35589  /* We have either a class or a category on our hands.  */
35590  if (categ)
35591    objc_start_category_implementation (name, categ);
35592  else
35593    {
35594      objc_start_class_implementation (name, super);
35595      /* Handle instance variable declarations, if any.  */
35596      cp_parser_objc_class_ivars (parser);
35597      objc_continue_implementation ();
35598    }
35599
35600  cp_parser_objc_method_definition_list (parser);
35601}
35602
35603/* Consume the @end token and finish off the implementation.  */
35604
35605static void
35606cp_parser_objc_end_implementation (cp_parser* parser)
35607{
35608  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
35609  objc_finish_implementation ();
35610}
35611
35612/* Parse an Objective-C declaration.  */
35613
35614static void
35615cp_parser_objc_declaration (cp_parser* parser, tree attributes)
35616{
35617  /* Try to figure out what kind of declaration is present.  */
35618  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35619
35620  if (attributes)
35621    switch (kwd->keyword)
35622      {
35623	case RID_AT_ALIAS:
35624	case RID_AT_CLASS:
35625	case RID_AT_END:
35626	  error_at (kwd->location, "attributes may not be specified before"
35627	            " the %<@%D%> Objective-C++ keyword",
35628		    kwd->u.value);
35629	  attributes = NULL;
35630	  break;
35631	case RID_AT_IMPLEMENTATION:
35632	  warning_at (kwd->location, OPT_Wattributes,
35633		      "prefix attributes are ignored before %<@%D%>",
35634		      kwd->u.value);
35635	  attributes = NULL;
35636	default:
35637	  break;
35638      }
35639
35640  switch (kwd->keyword)
35641    {
35642    case RID_AT_ALIAS:
35643      cp_parser_objc_alias_declaration (parser);
35644      break;
35645    case RID_AT_CLASS:
35646      cp_parser_objc_class_declaration (parser);
35647      break;
35648    case RID_AT_PROTOCOL:
35649      cp_parser_objc_protocol_declaration (parser, attributes);
35650      break;
35651    case RID_AT_INTERFACE:
35652      cp_parser_objc_class_interface (parser, attributes);
35653      break;
35654    case RID_AT_IMPLEMENTATION:
35655      cp_parser_objc_class_implementation (parser);
35656      break;
35657    case RID_AT_END:
35658      cp_parser_objc_end_implementation (parser);
35659      break;
35660    default:
35661      error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35662		kwd->u.value);
35663      cp_parser_skip_to_end_of_block_or_statement (parser);
35664    }
35665}
35666
35667/* Parse an Objective-C try-catch-finally statement.
35668
35669   objc-try-catch-finally-stmt:
35670     @try compound-statement objc-catch-clause-seq [opt]
35671       objc-finally-clause [opt]
35672
35673   objc-catch-clause-seq:
35674     objc-catch-clause objc-catch-clause-seq [opt]
35675
35676   objc-catch-clause:
35677     @catch ( objc-exception-declaration ) compound-statement
35678
35679   objc-finally-clause:
35680     @finally compound-statement
35681
35682   objc-exception-declaration:
35683     parameter-declaration
35684     '...'
35685
35686   where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
35687
35688   Returns NULL_TREE.
35689
35690   PS: This function is identical to c_parser_objc_try_catch_finally_statement
35691   for C.  Keep them in sync.  */
35692
35693static tree
35694cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
35695{
35696  location_t location;
35697  tree stmt;
35698
35699  cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
35700  location = cp_lexer_peek_token (parser->lexer)->location;
35701  objc_maybe_warn_exceptions (location);
35702  /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
35703     node, lest it get absorbed into the surrounding block.  */
35704  stmt = push_stmt_list ();
35705  cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35706  objc_begin_try_stmt (location, pop_stmt_list (stmt));
35707
35708  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
35709    {
35710      cp_parameter_declarator *parm;
35711      tree parameter_declaration = error_mark_node;
35712      bool seen_open_paren = false;
35713      matching_parens parens;
35714
35715      cp_lexer_consume_token (parser->lexer);
35716      if (parens.require_open (parser))
35717	seen_open_paren = true;
35718      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
35719	{
35720	  /* We have "@catch (...)" (where the '...' are literally
35721	     what is in the code).  Skip the '...'.
35722	     parameter_declaration is set to NULL_TREE, and
35723	     objc_being_catch_clauses() knows that that means
35724	     '...'.  */
35725	  cp_lexer_consume_token (parser->lexer);
35726	  parameter_declaration = NULL_TREE;
35727	}
35728      else
35729	{
35730	  /* We have "@catch (NSException *exception)" or something
35731	     like that.  Parse the parameter declaration.  */
35732	  parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35733						  false, NULL);
35734	  if (parm == NULL)
35735	    parameter_declaration = error_mark_node;
35736	  else
35737	    parameter_declaration = grokdeclarator (parm->declarator,
35738						    &parm->decl_specifiers,
35739						    PARM, /*initialized=*/0,
35740						    /*attrlist=*/NULL);
35741	}
35742      if (seen_open_paren)
35743	parens.require_close (parser);
35744      else
35745	{
35746	  /* If there was no open parenthesis, we are recovering from
35747	     an error, and we are trying to figure out what mistake
35748	     the user has made.  */
35749
35750	  /* If there is an immediate closing parenthesis, the user
35751	     probably forgot the opening one (ie, they typed "@catch
35752	     NSException *e)".  Parse the closing parenthesis and keep
35753	     going.  */
35754	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35755	    cp_lexer_consume_token (parser->lexer);
35756
35757	  /* If these is no immediate closing parenthesis, the user
35758	     probably doesn't know that parenthesis are required at
35759	     all (ie, they typed "@catch NSException *e").  So, just
35760	     forget about the closing parenthesis and keep going.  */
35761	}
35762      objc_begin_catch_clause (parameter_declaration);
35763      cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35764      objc_finish_catch_clause ();
35765    }
35766  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
35767    {
35768      cp_lexer_consume_token (parser->lexer);
35769      location = cp_lexer_peek_token (parser->lexer)->location;
35770      /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
35771	 node, lest it get absorbed into the surrounding block.  */
35772      stmt = push_stmt_list ();
35773      cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35774      objc_build_finally_clause (location, pop_stmt_list (stmt));
35775    }
35776
35777  return objc_finish_try_stmt ();
35778}
35779
35780/* Parse an Objective-C synchronized statement.
35781
35782   objc-synchronized-stmt:
35783     @synchronized ( expression ) compound-statement
35784
35785   Returns NULL_TREE.  */
35786
35787static tree
35788cp_parser_objc_synchronized_statement (cp_parser *parser)
35789{
35790  location_t location;
35791  tree lock, stmt;
35792
35793  cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
35794
35795  location = cp_lexer_peek_token (parser->lexer)->location;
35796  objc_maybe_warn_exceptions (location);
35797  matching_parens parens;
35798  parens.require_open (parser);
35799  lock = cp_parser_expression (parser);
35800  parens.require_close (parser);
35801
35802  /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
35803     node, lest it get absorbed into the surrounding block.  */
35804  stmt = push_stmt_list ();
35805  cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35806
35807  return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
35808}
35809
35810/* Parse an Objective-C throw statement.
35811
35812   objc-throw-stmt:
35813     @throw assignment-expression [opt] ;
35814
35815   Returns a constructed '@throw' statement.  */
35816
35817static tree
35818cp_parser_objc_throw_statement (cp_parser *parser)
35819{
35820  tree expr = NULL_TREE;
35821  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35822
35823  cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
35824
35825  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35826    expr = cp_parser_expression (parser);
35827
35828  cp_parser_consume_semicolon_at_end_of_statement (parser);
35829
35830  return objc_build_throw_stmt (loc, expr);
35831}
35832
35833/* Parse an Objective-C statement.  */
35834
35835static tree
35836cp_parser_objc_statement (cp_parser * parser)
35837{
35838  /* Try to figure out what kind of declaration is present.  */
35839  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35840
35841  switch (kwd->keyword)
35842    {
35843    case RID_AT_TRY:
35844      return cp_parser_objc_try_catch_finally_statement (parser);
35845    case RID_AT_SYNCHRONIZED:
35846      return cp_parser_objc_synchronized_statement (parser);
35847    case RID_AT_THROW:
35848      return cp_parser_objc_throw_statement (parser);
35849    default:
35850      error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35851	       kwd->u.value);
35852      cp_parser_skip_to_end_of_block_or_statement (parser);
35853    }
35854
35855  return error_mark_node;
35856}
35857
35858/* If we are compiling ObjC++ and we see an __attribute__ we neeed to
35859   look ahead to see if an objc keyword follows the attributes.  This
35860   is to detect the use of prefix attributes on ObjC @interface and
35861   @protocol.  */
35862
35863static bool
35864cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
35865{
35866  cp_lexer_save_tokens (parser->lexer);
35867  tree addon = cp_parser_attributes_opt (parser);
35868  if (addon
35869      && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
35870    {
35871      cp_lexer_commit_tokens (parser->lexer);
35872      if (*attrib)
35873	TREE_CHAIN (*attrib) = addon;
35874      else
35875	*attrib = addon;
35876      return true;
35877    }
35878  cp_lexer_rollback_tokens (parser->lexer);
35879  return false;
35880}
35881
35882/* This routine is a minimal replacement for
35883   c_parser_struct_declaration () used when parsing the list of
35884   types/names or ObjC++ properties.  For example, when parsing the
35885   code
35886
35887   @property (readonly) int a, b, c;
35888
35889   this function is responsible for parsing "int a, int b, int c" and
35890   returning the declarations as CHAIN of DECLs.
35891
35892   TODO: Share this code with cp_parser_objc_class_ivars.  It's very
35893   similar parsing.  */
35894static tree
35895cp_parser_objc_struct_declaration (cp_parser *parser)
35896{
35897  tree decls = NULL_TREE;
35898  cp_decl_specifier_seq declspecs;
35899  int decl_class_or_enum_p;
35900  tree prefix_attributes;
35901
35902  cp_parser_decl_specifier_seq (parser,
35903				CP_PARSER_FLAGS_NONE,
35904				&declspecs,
35905				&decl_class_or_enum_p);
35906
35907  if (declspecs.type == error_mark_node)
35908    return error_mark_node;
35909
35910  /* auto, register, static, extern, mutable.  */
35911  if (declspecs.storage_class != sc_none)
35912    {
35913      cp_parser_error (parser, "invalid type for property");
35914      declspecs.storage_class = sc_none;
35915    }
35916
35917  /* thread_local.  */
35918  if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35919    {
35920      cp_parser_error (parser, "invalid type for property");
35921      declspecs.locations[ds_thread] = 0;
35922    }
35923
35924  /* typedef.  */
35925  if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35926    {
35927      cp_parser_error (parser, "invalid type for property");
35928      declspecs.locations[ds_typedef] = 0;
35929    }
35930
35931  prefix_attributes = declspecs.attributes;
35932  declspecs.attributes = NULL_TREE;
35933
35934  /* Keep going until we hit the `;' at the end of the declaration. */
35935  while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35936    {
35937      tree attributes, first_attribute, decl;
35938      cp_declarator *declarator;
35939      cp_token *token;
35940
35941      /* Parse the declarator.  */
35942      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35943					 CP_PARSER_FLAGS_NONE,
35944					 NULL, NULL, false, false, false);
35945
35946      /* Look for attributes that apply to the ivar.  */
35947      attributes = cp_parser_attributes_opt (parser);
35948      /* Remember which attributes are prefix attributes and
35949	 which are not.  */
35950      first_attribute = attributes;
35951      /* Combine the attributes.  */
35952      attributes = attr_chainon (prefix_attributes, attributes);
35953
35954      decl = grokfield (declarator, &declspecs,
35955			NULL_TREE, /*init_const_expr_p=*/false,
35956			NULL_TREE, attributes);
35957
35958      if (decl == error_mark_node || decl == NULL_TREE)
35959	return error_mark_node;
35960
35961      /* Reset PREFIX_ATTRIBUTES.  */
35962      if (attributes != error_mark_node)
35963	{
35964	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
35965	    attributes = TREE_CHAIN (attributes);
35966	  if (attributes)
35967	    TREE_CHAIN (attributes) = NULL_TREE;
35968	}
35969
35970      DECL_CHAIN (decl) = decls;
35971      decls = decl;
35972
35973      token = cp_lexer_peek_token (parser->lexer);
35974      if (token->type == CPP_COMMA)
35975	{
35976	  cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
35977	  continue;
35978	}
35979      else
35980	break;
35981    }
35982  return decls;
35983}
35984
35985/* Parse an Objective-C @property declaration.  The syntax is:
35986
35987   objc-property-declaration:
35988     '@property' objc-property-attributes[opt] struct-declaration ;
35989
35990   objc-property-attributes:
35991    '(' objc-property-attribute-list ')'
35992
35993   objc-property-attribute-list:
35994     objc-property-attribute
35995     objc-property-attribute-list, objc-property-attribute
35996
35997   objc-property-attribute
35998     'getter' = identifier
35999     'setter' = identifier
36000     'readonly'
36001     'readwrite'
36002     'assign'
36003     'retain'
36004     'copy'
36005     'nonatomic'
36006
36007  For example:
36008    @property NSString *name;
36009    @property (readonly) id object;
36010    @property (retain, nonatomic, getter=getTheName) id name;
36011    @property int a, b, c;
36012
36013   PS: This function is identical to
36014   c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
36015static void
36016cp_parser_objc_at_property_declaration (cp_parser *parser)
36017{
36018  /* Parse the optional attribute list.
36019
36020     A list of parsed, but not verified, attributes.  */
36021  auto_delete_vec<property_attribute_info> prop_attr_list;
36022  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36023
36024  cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
36025
36026  /* Parse the optional attribute list...  */
36027  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36028    {
36029      /* Eat the '('.  */
36030      matching_parens parens;
36031      location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
36032      parens.consume_open (parser);
36033      bool syntax_error = false;
36034
36035      /* Allow empty @property attribute lists, but with a warning.  */
36036      location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
36037      location_t attr_comb;
36038      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36039	{
36040	  attr_comb = make_location (attr_end, attr_start, attr_end);
36041	  warning_at (attr_comb, OPT_Wattributes,
36042		      "empty property attribute list");
36043	}
36044      else
36045	while (true)
36046	  {
36047	    cp_token *token = cp_lexer_peek_token (parser->lexer);
36048	    attr_start = token->location;
36049	    attr_end = get_finish (token->location);
36050	    attr_comb = make_location (attr_start, attr_start, attr_end);
36051
36052	    if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
36053	      {
36054		warning_at (attr_comb, OPT_Wattributes,
36055			    "missing property attribute");
36056		if (token->type == CPP_CLOSE_PAREN)
36057		  break;
36058		cp_lexer_consume_token (parser->lexer);
36059		continue;
36060	      }
36061
36062	    tree attr_name = NULL_TREE;
36063	    if (identifier_p (token->u.value))
36064	      attr_name = token->u.value;
36065
36066	    enum rid keyword;
36067	    if (token->type == CPP_NAME)
36068	      keyword = C_RID_CODE (token->u.value);
36069	    else if (token->type == CPP_KEYWORD
36070		     && token->keyword == RID_CLASS)
36071	      /* Account for accepting the 'class' keyword in this context.  */
36072	      keyword = RID_CLASS;
36073	    else
36074	      keyword = RID_MAX; /* By definition, an unknown property.  */
36075	    cp_lexer_consume_token (parser->lexer);
36076
36077	    enum objc_property_attribute_kind prop_kind
36078	      = objc_prop_attr_kind_for_rid (keyword);
36079	    property_attribute_info *prop
36080	      = new property_attribute_info (attr_name, attr_comb, prop_kind);
36081	    prop_attr_list.safe_push (prop);
36082
36083	    tree meth_name;
36084	    switch (prop->prop_kind)
36085	      {
36086	      default: break;
36087	      case OBJC_PROPERTY_ATTR_UNKNOWN:
36088		if (attr_name)
36089		  error_at (attr_start, "unknown property attribute %qE",
36090			    attr_name);
36091		else
36092		  error_at (attr_start, "unknown property attribute");
36093		prop->parse_error = syntax_error = true;
36094		break;
36095
36096	      case OBJC_PROPERTY_ATTR_GETTER:
36097	      case OBJC_PROPERTY_ATTR_SETTER:
36098		if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36099		  {
36100		    attr_comb = make_location (attr_end, attr_start, attr_end);
36101		    error_at (attr_comb, "expected %<=%> after Objective-C %qE",
36102			      attr_name);
36103		    prop->parse_error = syntax_error = true;
36104		    break;
36105		  }
36106
36107		token = cp_lexer_peek_token (parser->lexer);
36108		attr_end = token->location;
36109		cp_lexer_consume_token (parser->lexer); /* eat the = */
36110
36111		if (!cp_parser_objc_selector_p
36112		     (cp_lexer_peek_token (parser->lexer)->type))
36113		  {
36114		    attr_comb = make_location (attr_end, attr_start, attr_end);
36115		    error_at (attr_comb, "expected %qE selector name",
36116			      attr_name);
36117		    prop->parse_error = syntax_error = true;
36118		    break;
36119		  }
36120
36121		/* Get the end of the method name, and consume the name.  */
36122		token = cp_lexer_peek_token (parser->lexer);
36123		attr_end = get_finish (token->location);
36124		/* Because method names may contain C++ keywords, we have a
36125		   routine to fetch them (this also consumes the token).  */
36126		meth_name = cp_parser_objc_selector (parser);
36127
36128		if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
36129		  {
36130		    if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36131		      {
36132			attr_comb = make_location (attr_end, attr_start,
36133						   attr_end);
36134			error_at (attr_comb, "setter method names must"
36135				  " terminate with %<:%>");
36136			prop->parse_error = syntax_error = true;
36137		      }
36138		    else
36139		      {
36140			attr_end = get_finish (cp_lexer_peek_token
36141					       (parser->lexer)->location);
36142			cp_lexer_consume_token (parser->lexer);
36143		      }
36144		    attr_comb = make_location (attr_start, attr_start,
36145					       attr_end);
36146		  }
36147		else
36148		  attr_comb = make_location (attr_start, attr_start,
36149					     attr_end);
36150		prop->ident = meth_name;
36151		/* Updated location including all that was successfully
36152		   parsed.  */
36153		prop->prop_loc = attr_comb;
36154		break;
36155	      }
36156
36157	    /* If we see a comma here, then keep going - even if we already
36158	       saw a syntax error.  For simple mistakes e.g. (asign, getter=x)
36159	       this makes a more useful output and avoid spurious warnings
36160	       about missing attributes that are, in fact, specified after the
36161	       one with the syntax error.  */
36162	    if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36163	      cp_lexer_consume_token (parser->lexer);
36164	    else
36165	      break;
36166	  }
36167
36168      if (syntax_error || !parens.require_close (parser))
36169	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36170					       /*or_comma=*/false,
36171					       /*consume_paren=*/true);
36172    }
36173
36174  /* 'properties' is the list of properties that we read.  Usually a
36175     single one, but maybe more (eg, in "@property int a, b, c;" there
36176     are three).
36177     TODO: Update this parsing so that it accepts (erroneous) bitfields so
36178     that we can issue a meaningful and consistent (between C/C++) error
36179     message from objc_add_property_declaration ().  */
36180  tree properties = cp_parser_objc_struct_declaration (parser);
36181
36182  if (properties == error_mark_node)
36183    cp_parser_skip_to_end_of_statement (parser);
36184  else if (properties == NULL_TREE)
36185    cp_parser_error (parser, "expected identifier");
36186  else
36187    {
36188      /* Comma-separated properties are chained together in reverse order;
36189	 add them one by one.  */
36190      properties = nreverse (properties);
36191      for (; properties; properties = TREE_CHAIN (properties))
36192	objc_add_property_declaration (loc, copy_node (properties),
36193				       prop_attr_list);
36194    }
36195
36196  cp_parser_consume_semicolon_at_end_of_statement (parser);
36197}
36198
36199/* Parse an Objective-C++ @synthesize declaration.  The syntax is:
36200
36201   objc-synthesize-declaration:
36202     @synthesize objc-synthesize-identifier-list ;
36203
36204   objc-synthesize-identifier-list:
36205     objc-synthesize-identifier
36206     objc-synthesize-identifier-list, objc-synthesize-identifier
36207
36208   objc-synthesize-identifier
36209     identifier
36210     identifier = identifier
36211
36212  For example:
36213    @synthesize MyProperty;
36214    @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
36215
36216  PS: This function is identical to c_parser_objc_at_synthesize_declaration
36217  for C.  Keep them in sync.
36218*/
36219static void
36220cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
36221{
36222  tree list = NULL_TREE;
36223  location_t loc;
36224  loc = cp_lexer_peek_token (parser->lexer)->location;
36225
36226  cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
36227  while (true)
36228    {
36229      tree property, ivar;
36230      property = cp_parser_identifier (parser);
36231      if (property == error_mark_node)
36232	{
36233	  cp_parser_consume_semicolon_at_end_of_statement (parser);
36234	  return;
36235	}
36236      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
36237	{
36238	  cp_lexer_consume_token (parser->lexer);
36239	  ivar = cp_parser_identifier (parser);
36240	  if (ivar == error_mark_node)
36241	    {
36242	      cp_parser_consume_semicolon_at_end_of_statement (parser);
36243	      return;
36244	    }
36245	}
36246      else
36247	ivar = NULL_TREE;
36248      list = chainon (list, build_tree_list (ivar, property));
36249      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36250	cp_lexer_consume_token (parser->lexer);
36251      else
36252	break;
36253    }
36254  cp_parser_consume_semicolon_at_end_of_statement (parser);
36255  objc_add_synthesize_declaration (loc, list);
36256}
36257
36258/* Parse an Objective-C++ @dynamic declaration.  The syntax is:
36259
36260   objc-dynamic-declaration:
36261     @dynamic identifier-list ;
36262
36263   For example:
36264     @dynamic MyProperty;
36265     @dynamic MyProperty, AnotherProperty;
36266
36267  PS: This function is identical to c_parser_objc_at_dynamic_declaration
36268  for C.  Keep them in sync.
36269*/
36270static void
36271cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
36272{
36273  tree list = NULL_TREE;
36274  location_t loc;
36275  loc = cp_lexer_peek_token (parser->lexer)->location;
36276
36277  cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
36278  while (true)
36279    {
36280      tree property;
36281      property = cp_parser_identifier (parser);
36282      if (property == error_mark_node)
36283	{
36284	  cp_parser_consume_semicolon_at_end_of_statement (parser);
36285	  return;
36286	}
36287      list = chainon (list, build_tree_list (NULL, property));
36288      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36289	cp_lexer_consume_token (parser->lexer);
36290      else
36291	break;
36292    }
36293  cp_parser_consume_semicolon_at_end_of_statement (parser);
36294  objc_add_dynamic_declaration (loc, list);
36295}
36296
36297
36298/* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines.  */
36299
36300/* Returns name of the next clause.
36301   If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
36302   the token is not consumed.  Otherwise appropriate pragma_omp_clause is
36303   returned and the token is consumed.  */
36304
36305static pragma_omp_clause
36306cp_parser_omp_clause_name (cp_parser *parser)
36307{
36308  pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
36309
36310  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36311    result = PRAGMA_OACC_CLAUSE_AUTO;
36312  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
36313    result = PRAGMA_OMP_CLAUSE_IF;
36314  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
36315    result = PRAGMA_OMP_CLAUSE_DEFAULT;
36316  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
36317    result = PRAGMA_OACC_CLAUSE_DELETE;
36318  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
36319    result = PRAGMA_OMP_CLAUSE_PRIVATE;
36320  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36321    result = PRAGMA_OMP_CLAUSE_FOR;
36322  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36323    {
36324      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36325      const char *p = IDENTIFIER_POINTER (id);
36326
36327      switch (p[0])
36328	{
36329	case 'a':
36330	  if (!strcmp ("affinity", p))
36331	    result = PRAGMA_OMP_CLAUSE_AFFINITY;
36332	  else if (!strcmp ("aligned", p))
36333	    result = PRAGMA_OMP_CLAUSE_ALIGNED;
36334	  else if (!strcmp ("allocate", p))
36335	    result = PRAGMA_OMP_CLAUSE_ALLOCATE;
36336	  else if (!strcmp ("async", p))
36337	    result = PRAGMA_OACC_CLAUSE_ASYNC;
36338	  else if (!strcmp ("attach", p))
36339	    result = PRAGMA_OACC_CLAUSE_ATTACH;
36340	  break;
36341	case 'b':
36342	  if (!strcmp ("bind", p))
36343	    result = PRAGMA_OMP_CLAUSE_BIND;
36344	  break;
36345	case 'c':
36346	  if (!strcmp ("collapse", p))
36347	    result = PRAGMA_OMP_CLAUSE_COLLAPSE;
36348	  else if (!strcmp ("copy", p))
36349	    result = PRAGMA_OACC_CLAUSE_COPY;
36350	  else if (!strcmp ("copyin", p))
36351	    result = PRAGMA_OMP_CLAUSE_COPYIN;
36352	  else if (!strcmp ("copyout", p))
36353	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
36354	  else if (!strcmp ("copyprivate", p))
36355	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
36356	  else if (!strcmp ("create", p))
36357	    result = PRAGMA_OACC_CLAUSE_CREATE;
36358	  break;
36359	case 'd':
36360	  if (!strcmp ("defaultmap", p))
36361	    result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
36362	  else if (!strcmp ("depend", p))
36363	    result = PRAGMA_OMP_CLAUSE_DEPEND;
36364	  else if (!strcmp ("detach", p))
36365	    result = PRAGMA_OACC_CLAUSE_DETACH;
36366	  else if (!strcmp ("device", p))
36367	    result = PRAGMA_OMP_CLAUSE_DEVICE;
36368	  else if (!strcmp ("deviceptr", p))
36369	    result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
36370	  else if (!strcmp ("device_resident", p))
36371	    result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
36372	  else if (!strcmp ("device_type", p))
36373	    result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
36374	  else if (!strcmp ("dist_schedule", p))
36375	    result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
36376	  break;
36377	case 'f':
36378	  if (!strcmp ("filter", p))
36379	    result = PRAGMA_OMP_CLAUSE_FILTER;
36380	  else if (!strcmp ("final", p))
36381	    result = PRAGMA_OMP_CLAUSE_FINAL;
36382	  else if (!strcmp ("finalize", p))
36383	    result = PRAGMA_OACC_CLAUSE_FINALIZE;
36384	  else if (!strcmp ("firstprivate", p))
36385	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
36386	  else if (!strcmp ("from", p))
36387	    result = PRAGMA_OMP_CLAUSE_FROM;
36388	  break;
36389	case 'g':
36390	  if (!strcmp ("gang", p))
36391	    result = PRAGMA_OACC_CLAUSE_GANG;
36392	  else if (!strcmp ("grainsize", p))
36393	    result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
36394	  break;
36395	case 'h':
36396	  if (!strcmp ("has_device_addr", p))
36397	    result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
36398	  else if (!strcmp ("hint", p))
36399	    result = PRAGMA_OMP_CLAUSE_HINT;
36400	  else if (!strcmp ("host", p))
36401	    result = PRAGMA_OACC_CLAUSE_HOST;
36402	  break;
36403	case 'i':
36404	  if (!strcmp ("if_present", p))
36405	    result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
36406	  else if (!strcmp ("in_reduction", p))
36407	    result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
36408	  else if (!strcmp ("inbranch", p))
36409	    result = PRAGMA_OMP_CLAUSE_INBRANCH;
36410	  else if (!strcmp ("independent", p))
36411	    result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
36412	  else if (!strcmp ("is_device_ptr", p))
36413	    result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
36414	  break;
36415	case 'l':
36416	  if (!strcmp ("lastprivate", p))
36417	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
36418	  else if (!strcmp ("linear", p))
36419	    result = PRAGMA_OMP_CLAUSE_LINEAR;
36420	  else if (!strcmp ("link", p))
36421	    result = PRAGMA_OMP_CLAUSE_LINK;
36422	  break;
36423	case 'm':
36424	  if (!strcmp ("map", p))
36425	    result = PRAGMA_OMP_CLAUSE_MAP;
36426	  else if (!strcmp ("mergeable", p))
36427	    result = PRAGMA_OMP_CLAUSE_MERGEABLE;
36428	  break;
36429	case 'n':
36430	  if (!strcmp ("no_create", p))
36431	    result = PRAGMA_OACC_CLAUSE_NO_CREATE;
36432	  else if (!strcmp ("nogroup", p))
36433	    result = PRAGMA_OMP_CLAUSE_NOGROUP;
36434	  else if (!strcmp ("nohost", p))
36435	    result = PRAGMA_OACC_CLAUSE_NOHOST;
36436	  else if (!strcmp ("nontemporal", p))
36437	    result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
36438	  else if (!strcmp ("notinbranch", p))
36439	    result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
36440	  else if (!strcmp ("nowait", p))
36441	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
36442	  else if (!strcmp ("num_gangs", p))
36443	    result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
36444	  else if (!strcmp ("num_tasks", p))
36445	    result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
36446	  else if (!strcmp ("num_teams", p))
36447	    result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
36448	  else if (!strcmp ("num_threads", p))
36449	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
36450	  else if (!strcmp ("num_workers", p))
36451	    result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
36452	  break;
36453	case 'o':
36454	  if (!strcmp ("ordered", p))
36455	    result = PRAGMA_OMP_CLAUSE_ORDERED;
36456	  else if (!strcmp ("order", p))
36457	    result = PRAGMA_OMP_CLAUSE_ORDER;
36458	  break;
36459	case 'p':
36460	  if (!strcmp ("parallel", p))
36461	    result = PRAGMA_OMP_CLAUSE_PARALLEL;
36462	  else if (!strcmp ("present", p))
36463	    result = PRAGMA_OACC_CLAUSE_PRESENT;
36464	  else if (!strcmp ("present_or_copy", p)
36465		   || !strcmp ("pcopy", p))
36466	    result = PRAGMA_OACC_CLAUSE_COPY;
36467	  else if (!strcmp ("present_or_copyin", p)
36468		   || !strcmp ("pcopyin", p))
36469	    result = PRAGMA_OACC_CLAUSE_COPYIN;
36470	  else if (!strcmp ("present_or_copyout", p)
36471		   || !strcmp ("pcopyout", p))
36472	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
36473	  else if (!strcmp ("present_or_create", p)
36474		   || !strcmp ("pcreate", p))
36475	    result = PRAGMA_OACC_CLAUSE_CREATE;
36476	  else if (!strcmp ("priority", p))
36477	    result = PRAGMA_OMP_CLAUSE_PRIORITY;
36478	  else if (!strcmp ("proc_bind", p))
36479	    result = PRAGMA_OMP_CLAUSE_PROC_BIND;
36480	  break;
36481	case 'r':
36482	  if (!strcmp ("reduction", p))
36483	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
36484	  break;
36485	case 's':
36486	  if (!strcmp ("safelen", p))
36487	    result = PRAGMA_OMP_CLAUSE_SAFELEN;
36488	  else if (!strcmp ("schedule", p))
36489	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
36490	  else if (!strcmp ("sections", p))
36491	    result = PRAGMA_OMP_CLAUSE_SECTIONS;
36492	  else if (!strcmp ("self", p)) /* "self" is a synonym for "host".  */
36493	    result = PRAGMA_OACC_CLAUSE_HOST;
36494	  else if (!strcmp ("seq", p))
36495	    result = PRAGMA_OACC_CLAUSE_SEQ;
36496	  else if (!strcmp ("shared", p))
36497	    result = PRAGMA_OMP_CLAUSE_SHARED;
36498	  else if (!strcmp ("simd", p))
36499	    result = PRAGMA_OMP_CLAUSE_SIMD;
36500	  else if (!strcmp ("simdlen", p))
36501	    result = PRAGMA_OMP_CLAUSE_SIMDLEN;
36502	  break;
36503	case 't':
36504	  if (!strcmp ("task_reduction", p))
36505	    result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
36506	  else if (!strcmp ("taskgroup", p))
36507	    result = PRAGMA_OMP_CLAUSE_TASKGROUP;
36508	  else if (!strcmp ("thread_limit", p))
36509	    result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
36510	  else if (!strcmp ("threads", p))
36511	    result = PRAGMA_OMP_CLAUSE_THREADS;
36512	  else if (!strcmp ("tile", p))
36513	    result = PRAGMA_OACC_CLAUSE_TILE;
36514	  else if (!strcmp ("to", p))
36515	    result = PRAGMA_OMP_CLAUSE_TO;
36516	  break;
36517	case 'u':
36518	  if (!strcmp ("uniform", p))
36519	    result = PRAGMA_OMP_CLAUSE_UNIFORM;
36520	  else if (!strcmp ("untied", p))
36521	    result = PRAGMA_OMP_CLAUSE_UNTIED;
36522	  else if (!strcmp ("use_device", p))
36523	    result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
36524	  else if (!strcmp ("use_device_addr", p))
36525	    result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
36526	  else if (!strcmp ("use_device_ptr", p))
36527	    result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
36528	  break;
36529	case 'v':
36530	  if (!strcmp ("vector", p))
36531	    result = PRAGMA_OACC_CLAUSE_VECTOR;
36532	  else if (!strcmp ("vector_length", p))
36533	    result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
36534	  break;
36535	case 'w':
36536	  if (!strcmp ("wait", p))
36537	    result = PRAGMA_OACC_CLAUSE_WAIT;
36538	  else if (!strcmp ("worker", p))
36539	    result = PRAGMA_OACC_CLAUSE_WORKER;
36540	  break;
36541	}
36542    }
36543
36544  if (result != PRAGMA_OMP_CLAUSE_NONE)
36545    cp_lexer_consume_token (parser->lexer);
36546
36547  return result;
36548}
36549
36550/* Validate that a clause of the given type does not already exist.  */
36551
36552static void
36553check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
36554			   const char *name, location_t location)
36555{
36556  if (omp_find_clause (clauses, code))
36557    error_at (location, "too many %qs clauses", name);
36558}
36559
36560/* OpenMP 2.5:
36561   variable-list:
36562     identifier
36563     variable-list , identifier
36564
36565   In addition, we match a closing parenthesis (or, if COLON is non-NULL,
36566   colon).  An opening parenthesis will have been consumed by the caller.
36567
36568   If KIND is nonzero, create the appropriate node and install the decl
36569   in OMP_CLAUSE_DECL and add the node to the head of the list.
36570
36571   If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
36572   return the list created.
36573
36574   COLON can be NULL if only closing parenthesis should end the list,
36575   or pointer to bool which will receive false if the list is terminated
36576   by closing parenthesis or true if the list is terminated by colon.
36577
36578   The optional ALLOW_DEREF argument is true if list items can use the deref
36579   (->) operator.  */
36580
36581struct omp_dim
36582{
36583  tree low_bound, length;
36584  location_t loc;
36585  bool no_colon;
36586  omp_dim (tree lb, tree len, location_t lo, bool nc)
36587    : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
36588};
36589
36590static tree
36591cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
36592				tree list, bool *colon,
36593				bool allow_deref = false)
36594{
36595  auto_vec<omp_dim> dims;
36596  bool array_section_p;
36597  cp_token *token;
36598  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36599  if (colon)
36600    {
36601      parser->colon_corrects_to_scope_p = false;
36602      *colon = false;
36603    }
36604  while (1)
36605    {
36606      tree name, decl;
36607
36608      if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36609	cp_parser_parse_tentatively (parser);
36610      token = cp_lexer_peek_token (parser->lexer);
36611      if (kind != 0
36612	  && cp_parser_is_keyword (token, RID_THIS))
36613	{
36614	  decl = finish_this_expr ();
36615	  if (TREE_CODE (decl) == NON_LVALUE_EXPR
36616	      || CONVERT_EXPR_P (decl))
36617	    decl = TREE_OPERAND (decl, 0);
36618	  cp_lexer_consume_token (parser->lexer);
36619	}
36620      else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
36621	       || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
36622	       || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
36623	{
36624	  cp_id_kind idk;
36625	  decl = cp_parser_primary_expression (parser, false, false, false,
36626					       &idk);
36627	}
36628      else
36629	{
36630	  name = cp_parser_id_expression (parser, /*template_p=*/false,
36631					  /*check_dependency_p=*/true,
36632					  /*template_p=*/NULL,
36633					  /*declarator_p=*/false,
36634					  /*optional_p=*/false);
36635	  if (name == error_mark_node)
36636	    {
36637	      if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36638		  && cp_parser_simulate_error (parser))
36639		goto depend_lvalue;
36640	      goto skip_comma;
36641	    }
36642
36643	  if (identifier_p (name))
36644	    decl = cp_parser_lookup_name_simple (parser, name, token->location);
36645	  else
36646	    decl = name;
36647	  if (decl == error_mark_node)
36648	    {
36649	      if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36650		  && cp_parser_simulate_error (parser))
36651		goto depend_lvalue;
36652	      cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
36653					   token->location);
36654	    }
36655	}
36656      if (outer_automatic_var_p (decl))
36657	decl = process_outer_var_ref (decl, tf_warning_or_error);
36658      if (decl == error_mark_node)
36659	;
36660      else if (kind != 0)
36661	{
36662	  switch (kind)
36663	    {
36664	    case OMP_CLAUSE__CACHE_:
36665	      /* The OpenACC cache directive explicitly only allows "array
36666		 elements or subarrays".  */
36667	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
36668		{
36669		  error_at (token->location, "expected %<[%>");
36670		  decl = error_mark_node;
36671		  break;
36672		}
36673	      /* FALLTHROUGH.  */
36674	    case OMP_CLAUSE_MAP:
36675	    case OMP_CLAUSE_FROM:
36676	    case OMP_CLAUSE_TO:
36677	    start_component_ref:
36678	      while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36679		     || (allow_deref
36680			 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
36681		{
36682		  cpp_ttype ttype
36683		    = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36684		      ? CPP_DOT : CPP_DEREF;
36685		  location_t loc
36686		    = cp_lexer_peek_token (parser->lexer)->location;
36687		  cp_id_kind idk = CP_ID_KIND_NONE;
36688		  cp_lexer_consume_token (parser->lexer);
36689		  decl = convert_from_reference (decl);
36690		  decl
36691		    = cp_parser_postfix_dot_deref_expression (parser, ttype,
36692							      decl, false,
36693							      &idk, loc);
36694		}
36695	      /* FALLTHROUGH.  */
36696	    case OMP_CLAUSE_AFFINITY:
36697	    case OMP_CLAUSE_DEPEND:
36698	    case OMP_CLAUSE_REDUCTION:
36699	    case OMP_CLAUSE_IN_REDUCTION:
36700	    case OMP_CLAUSE_TASK_REDUCTION:
36701	    case OMP_CLAUSE_HAS_DEVICE_ADDR:
36702	      array_section_p = false;
36703	      dims.truncate (0);
36704	      while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
36705		{
36706		  location_t loc = UNKNOWN_LOCATION;
36707		  tree low_bound = NULL_TREE, length = NULL_TREE;
36708		  bool no_colon = false;
36709
36710		  parser->colon_corrects_to_scope_p = false;
36711		  cp_lexer_consume_token (parser->lexer);
36712		  if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36713		    {
36714		      loc = cp_lexer_peek_token (parser->lexer)->location;
36715		      low_bound = cp_parser_expression (parser);
36716		      /* Later handling is not prepared to see through these.  */
36717		      gcc_checking_assert (!location_wrapper_p (low_bound));
36718		    }
36719		  if (!colon)
36720		    parser->colon_corrects_to_scope_p
36721		      = saved_colon_corrects_to_scope_p;
36722		  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
36723		    {
36724		      length = integer_one_node;
36725		      no_colon = true;
36726		    }
36727		  else
36728		    {
36729		      /* Look for `:'.  */
36730		      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36731			{
36732			  if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36733			      && cp_parser_simulate_error (parser))
36734			    goto depend_lvalue;
36735			  goto skip_comma;
36736			}
36737		      if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36738			cp_parser_commit_to_tentative_parse (parser);
36739		      else
36740			array_section_p = true;
36741		      if (!cp_lexer_next_token_is (parser->lexer,
36742						   CPP_CLOSE_SQUARE))
36743			{
36744			  length = cp_parser_expression (parser);
36745			  /* Later handling is not prepared to see through these.  */
36746			  gcc_checking_assert (!location_wrapper_p (length));
36747			}
36748		    }
36749		  /* Look for the closing `]'.  */
36750		  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
36751					  RT_CLOSE_SQUARE))
36752		    {
36753		      if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36754			  && cp_parser_simulate_error (parser))
36755			goto depend_lvalue;
36756		      goto skip_comma;
36757		    }
36758
36759		  dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
36760		}
36761
36762	      if ((kind == OMP_CLAUSE_MAP
36763		   || kind == OMP_CLAUSE_FROM
36764		   || kind == OMP_CLAUSE_TO)
36765		  && !array_section_p
36766		  && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36767		      || (allow_deref
36768			  && cp_lexer_next_token_is (parser->lexer,
36769						     CPP_DEREF))))
36770		{
36771		  for (unsigned i = 0; i < dims.length (); i++)
36772		    {
36773		      gcc_assert (dims[i].length == integer_one_node);
36774		      decl = build_array_ref (dims[i].loc,
36775					      decl, dims[i].low_bound);
36776		    }
36777		  goto start_component_ref;
36778		}
36779	      else
36780		for (unsigned i = 0; i < dims.length (); i++)
36781		  decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
36782
36783	      break;
36784	    default:
36785	      break;
36786	    }
36787
36788	  if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36789	    {
36790	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
36791		  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
36792		  && cp_parser_simulate_error (parser))
36793		{
36794		depend_lvalue:
36795		  cp_parser_abort_tentative_parse (parser);
36796		  decl = cp_parser_assignment_expression (parser, NULL,
36797							  false, false);
36798		}
36799	      else
36800		cp_parser_parse_definitely (parser);
36801	    }
36802
36803	  tree u = build_omp_clause (token->location, kind);
36804	  OMP_CLAUSE_DECL (u) = decl;
36805	  OMP_CLAUSE_CHAIN (u) = list;
36806	  list = u;
36807	}
36808      else
36809	list = tree_cons (decl, NULL_TREE, list);
36810
36811    get_comma:
36812      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36813	break;
36814      cp_lexer_consume_token (parser->lexer);
36815    }
36816
36817  if (colon)
36818    parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36819
36820  if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36821    {
36822      *colon = true;
36823      cp_parser_require (parser, CPP_COLON, RT_COLON);
36824      return list;
36825    }
36826
36827  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36828    {
36829      int ending;
36830
36831      /* Try to resync to an unnested comma.  Copied from
36832	 cp_parser_parenthesized_expression_list.  */
36833    skip_comma:
36834      if (colon)
36835	parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36836      ending = cp_parser_skip_to_closing_parenthesis (parser,
36837						      /*recovering=*/true,
36838						      /*or_comma=*/true,
36839						      /*consume_paren=*/true);
36840      if (ending < 0)
36841	goto get_comma;
36842    }
36843
36844  return list;
36845}
36846
36847/* Similarly, but expect leading and trailing parenthesis.  This is a very
36848   common case for omp clauses.  */
36849
36850static tree
36851cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
36852			bool allow_deref = false)
36853{
36854  if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36855    return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
36856					   allow_deref);
36857  return list;
36858}
36859
36860/* OpenACC 2.0:
36861   copy ( variable-list )
36862   copyin ( variable-list )
36863   copyout ( variable-list )
36864   create ( variable-list )
36865   delete ( variable-list )
36866   present ( variable-list )
36867
36868   OpenACC 2.6:
36869   no_create ( variable-list )
36870   attach ( variable-list )
36871   detach ( variable-list ) */
36872
36873static tree
36874cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
36875			    tree list)
36876{
36877  enum gomp_map_kind kind;
36878  switch (c_kind)
36879    {
36880    case PRAGMA_OACC_CLAUSE_ATTACH:
36881      kind = GOMP_MAP_ATTACH;
36882      break;
36883    case PRAGMA_OACC_CLAUSE_COPY:
36884      kind = GOMP_MAP_TOFROM;
36885      break;
36886    case PRAGMA_OACC_CLAUSE_COPYIN:
36887      kind = GOMP_MAP_TO;
36888      break;
36889    case PRAGMA_OACC_CLAUSE_COPYOUT:
36890      kind = GOMP_MAP_FROM;
36891      break;
36892    case PRAGMA_OACC_CLAUSE_CREATE:
36893      kind = GOMP_MAP_ALLOC;
36894      break;
36895    case PRAGMA_OACC_CLAUSE_DELETE:
36896      kind = GOMP_MAP_RELEASE;
36897      break;
36898    case PRAGMA_OACC_CLAUSE_DETACH:
36899      kind = GOMP_MAP_DETACH;
36900      break;
36901    case PRAGMA_OACC_CLAUSE_DEVICE:
36902      kind = GOMP_MAP_FORCE_TO;
36903      break;
36904    case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
36905      kind = GOMP_MAP_DEVICE_RESIDENT;
36906      break;
36907    case PRAGMA_OACC_CLAUSE_HOST:
36908      kind = GOMP_MAP_FORCE_FROM;
36909      break;
36910    case PRAGMA_OACC_CLAUSE_LINK:
36911      kind = GOMP_MAP_LINK;
36912      break;
36913    case PRAGMA_OACC_CLAUSE_NO_CREATE:
36914      kind = GOMP_MAP_IF_PRESENT;
36915      break;
36916    case PRAGMA_OACC_CLAUSE_PRESENT:
36917      kind = GOMP_MAP_FORCE_PRESENT;
36918      break;
36919    default:
36920      gcc_unreachable ();
36921    }
36922  tree nl, c;
36923  nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
36924
36925  for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
36926    OMP_CLAUSE_SET_MAP_KIND (c, kind);
36927
36928  return nl;
36929}
36930
36931/* OpenACC 2.0:
36932   deviceptr ( variable-list ) */
36933
36934static tree
36935cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
36936{
36937  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36938  tree vars, t;
36939
36940  /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
36941     cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
36942     variable-list must only allow for pointer variables.  */
36943  vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36944  for (t = vars; t; t = TREE_CHAIN (t))
36945    {
36946      tree v = TREE_PURPOSE (t);
36947      tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
36948      OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
36949      OMP_CLAUSE_DECL (u) = v;
36950      OMP_CLAUSE_CHAIN (u) = list;
36951      list = u;
36952    }
36953
36954  return list;
36955}
36956
36957/* OpenACC 2.5:
36958   auto
36959   finalize
36960   independent
36961   nohost
36962   seq */
36963
36964static tree
36965cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
36966			      tree list)
36967{
36968  check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
36969
36970  tree c = build_omp_clause (loc, code);
36971  OMP_CLAUSE_CHAIN (c) = list;
36972
36973  return c;
36974}
36975
36976 /* OpenACC:
36977   num_gangs ( expression )
36978   num_workers ( expression )
36979   vector_length ( expression )  */
36980
36981static tree
36982cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
36983				  const char *str, tree list)
36984{
36985  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36986
36987  matching_parens parens;
36988  if (!parens.require_open (parser))
36989    return list;
36990
36991  tree t = cp_parser_assignment_expression (parser, NULL, false, false);
36992
36993  if (t == error_mark_node
36994      || !parens.require_close (parser))
36995    {
36996      cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36997					     /*or_comma=*/false,
36998					     /*consume_paren=*/true);
36999      return list;
37000    }
37001
37002  check_no_duplicate_clause (list, code, str, loc);
37003
37004  tree c = build_omp_clause (loc, code);
37005  OMP_CLAUSE_OPERAND (c, 0) = t;
37006  OMP_CLAUSE_CHAIN (c) = list;
37007  return c;
37008}
37009
37010/* OpenACC:
37011
37012    gang [( gang-arg-list )]
37013    worker [( [num:] int-expr )]
37014    vector [( [length:] int-expr )]
37015
37016  where gang-arg is one of:
37017
37018    [num:] int-expr
37019    static: size-expr
37020
37021  and size-expr may be:
37022
37023    *
37024    int-expr
37025*/
37026
37027static tree
37028cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
37029			     omp_clause_code kind,
37030			     const char *str, tree list)
37031{
37032  const char *id = "num";
37033  cp_lexer *lexer = parser->lexer;
37034  tree ops[2] = { NULL_TREE, NULL_TREE }, c;
37035
37036  if (kind == OMP_CLAUSE_VECTOR)
37037    id = "length";
37038
37039  if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
37040    {
37041      matching_parens parens;
37042      parens.consume_open (parser);
37043
37044      do
37045	{
37046	  cp_token *next = cp_lexer_peek_token (lexer);
37047	  int idx = 0;
37048
37049	  /* Gang static argument.  */
37050	  if (kind == OMP_CLAUSE_GANG
37051	      && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
37052	    {
37053	      cp_lexer_consume_token (lexer);
37054
37055	      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37056		goto cleanup_error;
37057
37058	      idx = 1;
37059	      if (ops[idx] != NULL)
37060		{
37061		  cp_parser_error (parser, "too many %<static%> arguments");
37062		  goto cleanup_error;
37063		}
37064
37065	      /* Check for the '*' argument.  */
37066	      if (cp_lexer_next_token_is (lexer, CPP_MULT)
37067		  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37068		      || cp_lexer_nth_token_is (parser->lexer, 2,
37069						CPP_CLOSE_PAREN)))
37070		{
37071		  cp_lexer_consume_token (lexer);
37072		  ops[idx] = integer_minus_one_node;
37073
37074		  if (cp_lexer_next_token_is (lexer, CPP_COMMA))
37075		    {
37076		      cp_lexer_consume_token (lexer);
37077		      continue;
37078		    }
37079		  else break;
37080		}
37081	    }
37082	  /* Worker num: argument and vector length: arguments.  */
37083	  else if (cp_lexer_next_token_is (lexer, CPP_NAME)
37084		   && id_equal (next->u.value, id)
37085		   && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
37086	    {
37087	      cp_lexer_consume_token (lexer);  /* id  */
37088	      cp_lexer_consume_token (lexer);  /* ':'  */
37089	    }
37090
37091	  /* Now collect the actual argument.  */
37092	  if (ops[idx] != NULL_TREE)
37093	    {
37094	      cp_parser_error (parser, "unexpected argument");
37095	      goto cleanup_error;
37096	    }
37097
37098	  tree expr = cp_parser_assignment_expression (parser, NULL, false,
37099						       false);
37100	  if (expr == error_mark_node)
37101	    goto cleanup_error;
37102
37103	  mark_exp_read (expr);
37104	  ops[idx] = expr;
37105
37106	  if (kind == OMP_CLAUSE_GANG
37107	      && cp_lexer_next_token_is (lexer, CPP_COMMA))
37108	    {
37109	      cp_lexer_consume_token (lexer);
37110	      continue;
37111	    }
37112	  break;
37113	}
37114      while (1);
37115
37116      if (!parens.require_close (parser))
37117	goto cleanup_error;
37118    }
37119
37120  check_no_duplicate_clause (list, kind, str, loc);
37121
37122  c = build_omp_clause (loc, kind);
37123
37124  if (ops[1])
37125    OMP_CLAUSE_OPERAND (c, 1) = ops[1];
37126
37127  OMP_CLAUSE_OPERAND (c, 0) = ops[0];
37128  OMP_CLAUSE_CHAIN (c) = list;
37129
37130  return c;
37131
37132 cleanup_error:
37133  cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37134  return list;
37135}
37136
37137/* OpenACC 2.0:
37138   tile ( size-expr-list ) */
37139
37140static tree
37141cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
37142{
37143  tree c, expr = error_mark_node;
37144  tree tile = NULL_TREE;
37145
37146  /* Collapse and tile are mutually exclusive.  (The spec doesn't say
37147     so, but the spec authors never considered such a case and have
37148     differing opinions on what it might mean, including 'not
37149     allowed'.)  */
37150  check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
37151  check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
37152			     clause_loc);
37153
37154  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37155    return list;
37156
37157  do
37158    {
37159      if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
37160	return list;
37161
37162      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
37163	  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37164	      || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
37165	{
37166	  cp_lexer_consume_token (parser->lexer);
37167	  expr = integer_zero_node;
37168	}
37169      else
37170	expr = cp_parser_constant_expression (parser);
37171
37172      tile = tree_cons (NULL_TREE, expr, tile);
37173    }
37174  while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
37175
37176  /* Consume the trailing ')'.  */
37177  cp_lexer_consume_token (parser->lexer);
37178
37179  c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
37180  tile = nreverse (tile);
37181  OMP_CLAUSE_TILE_LIST (c) = tile;
37182  OMP_CLAUSE_CHAIN (c) = list;
37183  return c;
37184}
37185
37186/* OpenACC 2.0
37187   Parse wait clause or directive parameters.  */
37188
37189static tree
37190cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
37191{
37192  vec<tree, va_gc> *args;
37193  tree t, args_tree;
37194
37195  args = cp_parser_parenthesized_expression_list (parser, non_attr,
37196						  /*cast_p=*/false,
37197						  /*allow_expansion_p=*/true,
37198						  /*non_constant_p=*/NULL);
37199
37200  if (args == NULL || args->length () == 0)
37201    {
37202      if (args != NULL)
37203	{
37204	  cp_parser_error (parser, "expected integer expression list");
37205	  release_tree_vector (args);
37206	}
37207      return list;
37208    }
37209
37210  args_tree = build_tree_list_vec (args);
37211
37212  release_tree_vector (args);
37213
37214  for (t = args_tree; t; t = TREE_CHAIN (t))
37215    {
37216      tree targ = TREE_VALUE (t);
37217
37218      if (targ != error_mark_node)
37219	{
37220	  if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
37221	    error ("%<wait%> expression must be integral");
37222	  else
37223	    {
37224	      tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
37225
37226	      targ = mark_rvalue_use (targ);
37227	      OMP_CLAUSE_DECL (c) = targ;
37228	      OMP_CLAUSE_CHAIN (c) = list;
37229	      list = c;
37230	    }
37231	}
37232    }
37233
37234  return list;
37235}
37236
37237/* OpenACC:
37238   wait [( int-expr-list )] */
37239
37240static tree
37241cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
37242{
37243  location_t location = cp_lexer_peek_token (parser->lexer)->location;
37244
37245  if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37246    list = cp_parser_oacc_wait_list (parser, location, list);
37247  else
37248    {
37249      tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
37250
37251      OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37252      OMP_CLAUSE_CHAIN (c) = list;
37253      list = c;
37254    }
37255
37256  return list;
37257}
37258
37259/* OpenMP 3.0:
37260   collapse ( constant-expression ) */
37261
37262static tree
37263cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
37264{
37265  tree c, num;
37266  location_t loc;
37267  HOST_WIDE_INT n;
37268
37269  loc = cp_lexer_peek_token (parser->lexer)->location;
37270  matching_parens parens;
37271  if (!parens.require_open (parser))
37272    return list;
37273
37274  num = cp_parser_constant_expression (parser);
37275
37276  if (!parens.require_close (parser))
37277    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37278					   /*or_comma=*/false,
37279					   /*consume_paren=*/true);
37280
37281  if (num == error_mark_node)
37282    return list;
37283  num = fold_non_dependent_expr (num);
37284  if (!tree_fits_shwi_p (num)
37285      || !INTEGRAL_TYPE_P (TREE_TYPE (num))
37286      || (n = tree_to_shwi (num)) <= 0
37287      || (int) n != n)
37288    {
37289      error_at (loc, "collapse argument needs positive constant integer expression");
37290      return list;
37291    }
37292
37293  check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
37294  check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
37295  c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
37296  OMP_CLAUSE_CHAIN (c) = list;
37297  OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
37298
37299  return c;
37300}
37301
37302/* OpenMP 2.5:
37303   default ( none | shared )
37304
37305   OpenMP 5.1:
37306   default ( private | firstprivate )
37307
37308   OpenACC:
37309   default ( none | present ) */
37310
37311static tree
37312cp_parser_omp_clause_default (cp_parser *parser, tree list,
37313			      location_t location, bool is_oacc)
37314{
37315  enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
37316  tree c;
37317
37318  matching_parens parens;
37319  if (!parens.require_open (parser))
37320    return list;
37321  if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37322    {
37323      kind = OMP_CLAUSE_DEFAULT_PRIVATE;
37324      cp_lexer_consume_token (parser->lexer);
37325    }
37326  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37327    {
37328      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37329      const char *p = IDENTIFIER_POINTER (id);
37330
37331      switch (p[0])
37332	{
37333	case 'n':
37334	  if (strcmp ("none", p) != 0)
37335	    goto invalid_kind;
37336	  kind = OMP_CLAUSE_DEFAULT_NONE;
37337	  break;
37338
37339	case 'p':
37340	  if (strcmp ("present", p) != 0 || !is_oacc)
37341	    goto invalid_kind;
37342	  kind = OMP_CLAUSE_DEFAULT_PRESENT;
37343	  break;
37344
37345	case 'f':
37346	  if (strcmp ("firstprivate", p) != 0 || is_oacc)
37347	    goto invalid_kind;
37348	  kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
37349	  break;
37350
37351	case 's':
37352	  if (strcmp ("shared", p) != 0 || is_oacc)
37353	    goto invalid_kind;
37354	  kind = OMP_CLAUSE_DEFAULT_SHARED;
37355	  break;
37356
37357	default:
37358	  goto invalid_kind;
37359	}
37360
37361      cp_lexer_consume_token (parser->lexer);
37362    }
37363  else
37364    {
37365    invalid_kind:
37366      if (is_oacc)
37367	cp_parser_error (parser, "expected %<none%> or %<present%>");
37368      else
37369	cp_parser_error (parser, "expected %<none%>, %<shared%>, "
37370				 "%<private%> or %<firstprivate%>");
37371    }
37372
37373  if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
37374      || !parens.require_close (parser))
37375    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37376					   /*or_comma=*/false,
37377					   /*consume_paren=*/true);
37378
37379  if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
37380    return list;
37381
37382  check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
37383  c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
37384  OMP_CLAUSE_CHAIN (c) = list;
37385  OMP_CLAUSE_DEFAULT_KIND (c) = kind;
37386
37387  return c;
37388}
37389
37390/* OpenMP 3.1:
37391   final ( expression ) */
37392
37393static tree
37394cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
37395{
37396  tree t, c;
37397
37398  matching_parens parens;
37399  if (!parens.require_open (parser))
37400    return list;
37401
37402  t = cp_parser_assignment_expression (parser);
37403
37404  if (t == error_mark_node
37405      || !parens.require_close (parser))
37406    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37407					   /*or_comma=*/false,
37408					   /*consume_paren=*/true);
37409
37410  check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
37411
37412  c = build_omp_clause (location, OMP_CLAUSE_FINAL);
37413  OMP_CLAUSE_FINAL_EXPR (c) = t;
37414  OMP_CLAUSE_CHAIN (c) = list;
37415
37416  return c;
37417}
37418
37419/* OpenMP 2.5:
37420   if ( expression )
37421
37422   OpenMP 4.5:
37423   if ( directive-name-modifier : expression )
37424
37425   directive-name-modifier:
37426     parallel | task | taskloop | target data | target | target update
37427     | target enter data | target exit data
37428
37429   OpenMP 5.0:
37430   directive-name-modifier:
37431     ... | simd | cancel  */
37432
37433static tree
37434cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
37435			 bool is_omp)
37436{
37437  tree t, c;
37438  enum tree_code if_modifier = ERROR_MARK;
37439
37440  matching_parens parens;
37441  if (!parens.require_open (parser))
37442    return list;
37443
37444  if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37445    {
37446      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37447      const char *p = IDENTIFIER_POINTER (id);
37448      int n = 2;
37449
37450      if (strcmp ("cancel", p) == 0)
37451	if_modifier = VOID_CST;
37452      else if (strcmp ("parallel", p) == 0)
37453	if_modifier = OMP_PARALLEL;
37454      else if (strcmp ("simd", p) == 0)
37455	if_modifier = OMP_SIMD;
37456      else if (strcmp ("task", p) == 0)
37457	if_modifier = OMP_TASK;
37458      else if (strcmp ("taskloop", p) == 0)
37459	if_modifier = OMP_TASKLOOP;
37460      else if (strcmp ("target", p) == 0)
37461	{
37462	  if_modifier = OMP_TARGET;
37463	  if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
37464	    {
37465	      id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
37466	      p = IDENTIFIER_POINTER (id);
37467	      if (strcmp ("data", p) == 0)
37468		if_modifier = OMP_TARGET_DATA;
37469	      else if (strcmp ("update", p) == 0)
37470		if_modifier = OMP_TARGET_UPDATE;
37471	      else if (strcmp ("enter", p) == 0)
37472		if_modifier = OMP_TARGET_ENTER_DATA;
37473	      else if (strcmp ("exit", p) == 0)
37474		if_modifier = OMP_TARGET_EXIT_DATA;
37475	      if (if_modifier != OMP_TARGET)
37476		n = 3;
37477	      else
37478		{
37479		  location_t loc
37480		    = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
37481		  error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
37482				 "or %<exit%>");
37483		  if_modifier = ERROR_MARK;
37484		}
37485	      if (if_modifier == OMP_TARGET_ENTER_DATA
37486		  || if_modifier == OMP_TARGET_EXIT_DATA)
37487		{
37488		  if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
37489		    {
37490		      id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
37491		      p = IDENTIFIER_POINTER (id);
37492		      if (strcmp ("data", p) == 0)
37493			n = 4;
37494		    }
37495		  if (n != 4)
37496		    {
37497		      location_t loc
37498			= cp_lexer_peek_nth_token (parser->lexer, 3)->location;
37499		      error_at (loc, "expected %<data%>");
37500		      if_modifier = ERROR_MARK;
37501		    }
37502		}
37503	    }
37504	}
37505      if (if_modifier != ERROR_MARK)
37506	{
37507	  if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
37508	    {
37509	      while (n-- > 0)
37510		cp_lexer_consume_token (parser->lexer);
37511	    }
37512	  else
37513	    {
37514	      if (n > 2)
37515		{
37516		  location_t loc
37517		    = cp_lexer_peek_nth_token (parser->lexer, n)->location;
37518		  error_at (loc, "expected %<:%>");
37519		}
37520	      if_modifier = ERROR_MARK;
37521	    }
37522	}
37523    }
37524
37525  t = cp_parser_assignment_expression (parser);
37526
37527  if (t == error_mark_node
37528      || !parens.require_close (parser))
37529    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37530					   /*or_comma=*/false,
37531					   /*consume_paren=*/true);
37532
37533  for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37534    if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
37535      {
37536	if (if_modifier != ERROR_MARK
37537	    && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37538	  {
37539	    const char *p = NULL;
37540	    switch (if_modifier)
37541	      {
37542	      case VOID_CST: p = "cancel"; break;
37543	      case OMP_PARALLEL: p = "parallel"; break;
37544	      case OMP_SIMD: p = "simd"; break;
37545	      case OMP_TASK: p = "task"; break;
37546	      case OMP_TASKLOOP: p = "taskloop"; break;
37547	      case OMP_TARGET_DATA: p = "target data"; break;
37548	      case OMP_TARGET: p = "target"; break;
37549	      case OMP_TARGET_UPDATE: p = "target update"; break;
37550	      case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
37551	      case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
37552	      default: gcc_unreachable ();
37553	      }
37554	    error_at (location, "too many %<if%> clauses with %qs modifier",
37555		      p);
37556	    return list;
37557	  }
37558	else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37559	  {
37560	    if (!is_omp)
37561	      error_at (location, "too many %<if%> clauses");
37562	    else
37563	      error_at (location, "too many %<if%> clauses without modifier");
37564	    return list;
37565	  }
37566	else if (if_modifier == ERROR_MARK
37567		 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
37568	  {
37569	    error_at (location, "if any %<if%> clause has modifier, then all "
37570				"%<if%> clauses have to use modifier");
37571	    return list;
37572	  }
37573      }
37574
37575  c = build_omp_clause (location, OMP_CLAUSE_IF);
37576  OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
37577  OMP_CLAUSE_IF_EXPR (c) = t;
37578  OMP_CLAUSE_CHAIN (c) = list;
37579
37580  return c;
37581}
37582
37583/* OpenMP 3.1:
37584   mergeable */
37585
37586static tree
37587cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
37588				tree list, location_t location)
37589{
37590  tree c;
37591
37592  check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
37593			     location);
37594
37595  c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
37596  OMP_CLAUSE_CHAIN (c) = list;
37597  return c;
37598}
37599
37600/* OpenMP 2.5:
37601   nowait */
37602
37603static tree
37604cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
37605			     tree list, location_t location)
37606{
37607  tree c;
37608
37609  check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
37610
37611  c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
37612  OMP_CLAUSE_CHAIN (c) = list;
37613  return c;
37614}
37615
37616/* OpenMP 2.5:
37617   num_threads ( expression ) */
37618
37619static tree
37620cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
37621				  location_t location)
37622{
37623  tree t, c;
37624
37625  matching_parens parens;
37626  if (!parens.require_open (parser))
37627    return list;
37628
37629  t = cp_parser_assignment_expression (parser);
37630
37631  if (t == error_mark_node
37632      || !parens.require_close (parser))
37633    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37634					   /*or_comma=*/false,
37635					   /*consume_paren=*/true);
37636
37637  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
37638			     "num_threads", location);
37639
37640  c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
37641  OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
37642  OMP_CLAUSE_CHAIN (c) = list;
37643
37644  return c;
37645}
37646
37647/* OpenMP 4.5:
37648   num_tasks ( expression )
37649
37650   OpenMP 5.1:
37651   num_tasks ( strict : expression ) */
37652
37653static tree
37654cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
37655				location_t location)
37656{
37657  tree t, c;
37658
37659  matching_parens parens;
37660  if (!parens.require_open (parser))
37661    return list;
37662
37663  bool strict = false;
37664  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37665      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37666    {
37667      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37668      if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37669	{
37670	  strict = true;
37671	  cp_lexer_consume_token (parser->lexer);
37672	  cp_lexer_consume_token (parser->lexer);
37673	}
37674    }
37675
37676  t = cp_parser_assignment_expression (parser);
37677
37678  if (t == error_mark_node
37679      || !parens.require_close (parser))
37680    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37681					   /*or_comma=*/false,
37682					   /*consume_paren=*/true);
37683
37684  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
37685			     "num_tasks", location);
37686
37687  c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
37688  OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
37689  OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
37690  OMP_CLAUSE_CHAIN (c) = list;
37691
37692  return c;
37693}
37694
37695/* OpenMP 4.5:
37696   grainsize ( expression )
37697
37698   OpenMP 5.1:
37699   grainsize ( strict : expression ) */
37700
37701static tree
37702cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
37703				location_t location)
37704{
37705  tree t, c;
37706
37707  matching_parens parens;
37708  if (!parens.require_open (parser))
37709    return list;
37710
37711  bool strict = false;
37712  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37713      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37714    {
37715      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37716      if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37717	{
37718	  strict = true;
37719	  cp_lexer_consume_token (parser->lexer);
37720	  cp_lexer_consume_token (parser->lexer);
37721	}
37722    }
37723
37724  t = cp_parser_assignment_expression (parser);
37725
37726  if (t == error_mark_node
37727      || !parens.require_close (parser))
37728    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37729					   /*or_comma=*/false,
37730					   /*consume_paren=*/true);
37731
37732  check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
37733			     "grainsize", location);
37734
37735  c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
37736  OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
37737  OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
37738  OMP_CLAUSE_CHAIN (c) = list;
37739
37740  return c;
37741}
37742
37743/* OpenMP 4.5:
37744   priority ( expression ) */
37745
37746static tree
37747cp_parser_omp_clause_priority (cp_parser *parser, tree list,
37748			       location_t location)
37749{
37750  tree t, c;
37751
37752  matching_parens parens;
37753  if (!parens.require_open (parser))
37754    return list;
37755
37756  t = cp_parser_assignment_expression (parser);
37757
37758  if (t == error_mark_node
37759      || !parens.require_close (parser))
37760    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37761					   /*or_comma=*/false,
37762					   /*consume_paren=*/true);
37763
37764  check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
37765			     "priority", location);
37766
37767  c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
37768  OMP_CLAUSE_PRIORITY_EXPR (c) = t;
37769  OMP_CLAUSE_CHAIN (c) = list;
37770
37771  return c;
37772}
37773
37774/* OpenMP 4.5:
37775   hint ( expression ) */
37776
37777static tree
37778cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
37779{
37780  tree t, c;
37781
37782  matching_parens parens;
37783  if (!parens.require_open (parser))
37784    return list;
37785
37786  t = cp_parser_assignment_expression (parser);
37787
37788  if (t != error_mark_node)
37789    {
37790      t = fold_non_dependent_expr (t);
37791      if (!value_dependent_expression_p (t)
37792	  && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
37793	      || !tree_fits_shwi_p (t)
37794	      || tree_int_cst_sgn (t) == -1))
37795	error_at (location, "expected constant integer expression with "
37796			    "valid sync-hint value");
37797    }
37798  if (t == error_mark_node
37799      || !parens.require_close (parser))
37800    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37801					   /*or_comma=*/false,
37802					   /*consume_paren=*/true);
37803  check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
37804
37805  c = build_omp_clause (location, OMP_CLAUSE_HINT);
37806  OMP_CLAUSE_HINT_EXPR (c) = t;
37807  OMP_CLAUSE_CHAIN (c) = list;
37808
37809  return c;
37810}
37811
37812/* OpenMP 5.1:
37813   filter ( integer-expression ) */
37814
37815static tree
37816cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
37817{
37818  tree t, c;
37819
37820  matching_parens parens;
37821  if (!parens.require_open (parser))
37822    return list;
37823
37824  t = cp_parser_assignment_expression (parser);
37825
37826  if (t == error_mark_node
37827      || !parens.require_close (parser))
37828    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37829					   /*or_comma=*/false,
37830					   /*consume_paren=*/true);
37831  check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
37832
37833  c = build_omp_clause (location, OMP_CLAUSE_FILTER);
37834  OMP_CLAUSE_FILTER_EXPR (c) = t;
37835  OMP_CLAUSE_CHAIN (c) = list;
37836
37837  return c;
37838}
37839
37840/* OpenMP 4.5:
37841   defaultmap ( tofrom : scalar )
37842
37843   OpenMP 5.0:
37844   defaultmap ( implicit-behavior [ : variable-category ] ) */
37845
37846static tree
37847cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
37848				 location_t location)
37849{
37850  tree c, id;
37851  const char *p;
37852  enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
37853  enum omp_clause_defaultmap_kind category
37854    = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
37855
37856  matching_parens parens;
37857  if (!parens.require_open (parser))
37858    return list;
37859
37860  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37861    p = "default";
37862  else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37863    {
37864    invalid_behavior:
37865      cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
37866			       "%<tofrom%>, %<firstprivate%>, %<none%> "
37867			       "or %<default%>");
37868      goto out_err;
37869    }
37870  else
37871    {
37872      id = cp_lexer_peek_token (parser->lexer)->u.value;
37873      p = IDENTIFIER_POINTER (id);
37874    }
37875
37876  switch (p[0])
37877    {
37878    case 'a':
37879      if (strcmp ("alloc", p) == 0)
37880	behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
37881      else
37882	goto invalid_behavior;
37883      break;
37884
37885    case 'd':
37886      if (strcmp ("default", p) == 0)
37887	behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
37888      else
37889	goto invalid_behavior;
37890      break;
37891
37892    case 'f':
37893      if (strcmp ("firstprivate", p) == 0)
37894	behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
37895      else if (strcmp ("from", p) == 0)
37896	behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
37897      else
37898	goto invalid_behavior;
37899      break;
37900
37901    case 'n':
37902      if (strcmp ("none", p) == 0)
37903	behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
37904      else
37905	goto invalid_behavior;
37906      break;
37907
37908    case 't':
37909      if (strcmp ("tofrom", p) == 0)
37910	behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
37911      else if (strcmp ("to", p) == 0)
37912	behavior = OMP_CLAUSE_DEFAULTMAP_TO;
37913      else
37914	goto invalid_behavior;
37915      break;
37916
37917    default:
37918      goto invalid_behavior;
37919    }
37920  cp_lexer_consume_token (parser->lexer);
37921
37922  if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37923    {
37924      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37925	goto out_err;
37926
37927      if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37928	{
37929	invalid_category:
37930	  cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
37931				   "%<pointer%>");
37932	  goto out_err;
37933	}
37934      id = cp_lexer_peek_token (parser->lexer)->u.value;
37935      p = IDENTIFIER_POINTER (id);
37936
37937      switch (p[0])
37938	{
37939	case 'a':
37940	  if (strcmp ("aggregate", p) == 0)
37941	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
37942	  else
37943	    goto invalid_category;
37944	  break;
37945
37946	case 'p':
37947	  if (strcmp ("pointer", p) == 0)
37948	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
37949	  else
37950	    goto invalid_category;
37951	  break;
37952
37953	case 's':
37954	  if (strcmp ("scalar", p) == 0)
37955	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
37956	  else
37957	    goto invalid_category;
37958	  break;
37959
37960	default:
37961	  goto invalid_category;
37962	}
37963
37964      cp_lexer_consume_token (parser->lexer);
37965    }
37966  if (!parens.require_close (parser))
37967    goto out_err;
37968
37969  for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37970    if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
37971	&& (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
37972	    || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
37973	    || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
37974		== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
37975      {
37976	enum omp_clause_defaultmap_kind cat = category;
37977	location_t loc = OMP_CLAUSE_LOCATION (c);
37978	if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
37979	  cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
37980	p = NULL;
37981	switch (cat)
37982	  {
37983	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
37984	    p = NULL;
37985	    break;
37986	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
37987	    p = "aggregate";
37988	    break;
37989	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
37990	    p = "pointer";
37991	    break;
37992	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
37993	    p = "scalar";
37994	    break;
37995	  default:
37996	    gcc_unreachable ();
37997	  }
37998	if (p)
37999	  error_at (loc, "too many %<defaultmap%> clauses with %qs category",
38000		    p);
38001	else
38002	  error_at (loc, "too many %<defaultmap%> clauses with unspecified "
38003			 "category");
38004	break;
38005      }
38006
38007  c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
38008  OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
38009  OMP_CLAUSE_CHAIN (c) = list;
38010  return c;
38011
38012 out_err:
38013  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38014					 /*or_comma=*/false,
38015					 /*consume_paren=*/true);
38016  return list;
38017}
38018
38019/* OpenMP 5.0:
38020   order ( concurrent )
38021
38022   OpenMP 5.1:
38023   order ( order-modifier : concurrent )
38024
38025   order-modifier:
38026     reproducible
38027     unconstrained  */
38028
38029static tree
38030cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
38031{
38032  tree c, id;
38033  const char *p;
38034  bool unconstrained = false;
38035  bool reproducible = false;
38036
38037  matching_parens parens;
38038  if (!parens.require_open (parser))
38039    return list;
38040
38041  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38042      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38043    {
38044      id = cp_lexer_peek_token (parser->lexer)->u.value;
38045      p = IDENTIFIER_POINTER (id);
38046      if (strcmp (p, "unconstrained") == 0)
38047	unconstrained = true;
38048      else if (strcmp (p, "reproducible") == 0)
38049	reproducible = true;
38050      else
38051	{
38052	  cp_parser_error (parser, "expected %<reproducible%> or "
38053				   "%<unconstrained%>");
38054	  goto out_err;
38055	}
38056      cp_lexer_consume_token (parser->lexer);
38057      cp_lexer_consume_token (parser->lexer);
38058    }
38059  if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38060    {
38061      cp_parser_error (parser, "expected %<concurrent%>");
38062      goto out_err;
38063    }
38064  else
38065    {
38066      id = cp_lexer_peek_token (parser->lexer)->u.value;
38067      p = IDENTIFIER_POINTER (id);
38068    }
38069  if (strcmp (p, "concurrent") != 0)
38070    {
38071      cp_parser_error (parser, "expected %<concurrent%>");
38072      goto out_err;
38073    }
38074  cp_lexer_consume_token (parser->lexer);
38075  if (!parens.require_close (parser))
38076    goto out_err;
38077
38078  check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
38079  c = build_omp_clause (location, OMP_CLAUSE_ORDER);
38080  OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
38081  OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
38082  OMP_CLAUSE_CHAIN (c) = list;
38083  return c;
38084
38085 out_err:
38086  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38087					 /*or_comma=*/false,
38088					 /*consume_paren=*/true);
38089  return list;
38090}
38091
38092/* OpenMP 5.0:
38093   bind ( teams | parallel | thread ) */
38094
38095static tree
38096cp_parser_omp_clause_bind (cp_parser *parser, tree list,
38097			   location_t location)
38098{
38099  tree c;
38100  const char *p;
38101  enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
38102
38103  matching_parens parens;
38104  if (!parens.require_open (parser))
38105    return list;
38106
38107  if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38108    {
38109    invalid:
38110      cp_parser_error (parser,
38111		       "expected %<teams%>, %<parallel%> or %<thread%>");
38112      goto out_err;
38113    }
38114  else
38115    {
38116      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38117      p = IDENTIFIER_POINTER (id);
38118    }
38119  if (strcmp (p, "teams") == 0)
38120    kind = OMP_CLAUSE_BIND_TEAMS;
38121  else if (strcmp (p, "parallel") == 0)
38122    kind = OMP_CLAUSE_BIND_PARALLEL;
38123  else if (strcmp (p, "thread") != 0)
38124    goto invalid;
38125  cp_lexer_consume_token (parser->lexer);
38126  if (!parens.require_close (parser))
38127    goto out_err;
38128
38129  /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
38130  c = build_omp_clause (location, OMP_CLAUSE_BIND);
38131  OMP_CLAUSE_BIND_KIND (c) = kind;
38132  OMP_CLAUSE_CHAIN (c) = list;
38133  return c;
38134
38135 out_err:
38136  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38137					 /*or_comma=*/false,
38138					 /*consume_paren=*/true);
38139  return list;
38140}
38141
38142/* OpenMP 2.5:
38143   ordered
38144
38145   OpenMP 4.5:
38146   ordered ( constant-expression ) */
38147
38148static tree
38149cp_parser_omp_clause_ordered (cp_parser *parser,
38150			      tree list, location_t location)
38151{
38152  tree c, num = NULL_TREE;
38153  HOST_WIDE_INT n;
38154
38155  check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
38156			     "ordered", location);
38157
38158  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38159    {
38160      matching_parens parens;
38161      parens.consume_open (parser);
38162
38163      num = cp_parser_constant_expression (parser);
38164
38165      if (!parens.require_close (parser))
38166	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38167					       /*or_comma=*/false,
38168					       /*consume_paren=*/true);
38169
38170      if (num == error_mark_node)
38171	return list;
38172      num = fold_non_dependent_expr (num);
38173      if (!tree_fits_shwi_p (num)
38174	  || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38175	  || (n = tree_to_shwi (num)) <= 0
38176	  || (int) n != n)
38177	{
38178	  error_at (location,
38179		    "ordered argument needs positive constant integer "
38180		    "expression");
38181	  return list;
38182	}
38183    }
38184
38185  c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
38186  OMP_CLAUSE_ORDERED_EXPR (c) = num;
38187  OMP_CLAUSE_CHAIN (c) = list;
38188  return c;
38189}
38190
38191/* OpenMP 2.5:
38192   reduction ( reduction-operator : variable-list )
38193
38194   reduction-operator:
38195     One of: + * - & ^ | && ||
38196
38197   OpenMP 3.1:
38198
38199   reduction-operator:
38200     One of: + * - & ^ | && || min max
38201
38202   OpenMP 4.0:
38203
38204   reduction-operator:
38205     One of: + * - & ^ | && ||
38206     id-expression
38207
38208   OpenMP 5.0:
38209   reduction ( reduction-modifier, reduction-operator : variable-list )
38210   in_reduction ( reduction-operator : variable-list )
38211   task_reduction ( reduction-operator : variable-list )  */
38212
38213static tree
38214cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
38215				bool is_omp, tree list)
38216{
38217  enum tree_code code = ERROR_MARK;
38218  tree nlist, c, id = NULL_TREE;
38219  bool task = false;
38220  bool inscan = false;
38221
38222  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38223    return list;
38224
38225  if (kind == OMP_CLAUSE_REDUCTION && is_omp)
38226    {
38227      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
38228	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38229	{
38230	  cp_lexer_consume_token (parser->lexer);
38231	  cp_lexer_consume_token (parser->lexer);
38232	}
38233      else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38234	       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38235	{
38236	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38237	  const char *p = IDENTIFIER_POINTER (id);
38238	  if (strcmp (p, "task") == 0)
38239	    task = true;
38240	  else if (strcmp (p, "inscan") == 0)
38241	    inscan = true;
38242	  if (task || inscan)
38243	    {
38244	      cp_lexer_consume_token (parser->lexer);
38245	      cp_lexer_consume_token (parser->lexer);
38246	    }
38247	}
38248    }
38249
38250  switch (cp_lexer_peek_token (parser->lexer)->type)
38251    {
38252    case CPP_PLUS: code = PLUS_EXPR; break;
38253    case CPP_MULT: code = MULT_EXPR; break;
38254    case CPP_MINUS: code = MINUS_EXPR; break;
38255    case CPP_AND: code = BIT_AND_EXPR; break;
38256    case CPP_XOR: code = BIT_XOR_EXPR; break;
38257    case CPP_OR: code = BIT_IOR_EXPR; break;
38258    case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
38259    case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
38260    default: break;
38261    }
38262
38263  if (code != ERROR_MARK)
38264    cp_lexer_consume_token (parser->lexer);
38265  else
38266    {
38267      bool saved_colon_corrects_to_scope_p;
38268      saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38269      parser->colon_corrects_to_scope_p = false;
38270      id = cp_parser_id_expression (parser, /*template_p=*/false,
38271				    /*check_dependency_p=*/true,
38272				    /*template_p=*/NULL,
38273				    /*declarator_p=*/false,
38274				    /*optional_p=*/false);
38275      parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38276      if (identifier_p (id))
38277	{
38278	  const char *p = IDENTIFIER_POINTER (id);
38279
38280	  if (strcmp (p, "min") == 0)
38281	    code = MIN_EXPR;
38282	  else if (strcmp (p, "max") == 0)
38283	    code = MAX_EXPR;
38284	  else if (id == ovl_op_identifier (false, PLUS_EXPR))
38285	    code = PLUS_EXPR;
38286	  else if (id == ovl_op_identifier (false, MULT_EXPR))
38287	    code = MULT_EXPR;
38288	  else if (id == ovl_op_identifier (false, MINUS_EXPR))
38289	    code = MINUS_EXPR;
38290	  else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
38291	    code = BIT_AND_EXPR;
38292	  else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
38293	    code = BIT_IOR_EXPR;
38294	  else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
38295	    code = BIT_XOR_EXPR;
38296	  else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
38297	    code = TRUTH_ANDIF_EXPR;
38298	  else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
38299	    code = TRUTH_ORIF_EXPR;
38300	  id = omp_reduction_id (code, id, NULL_TREE);
38301	  tree scope = parser->scope;
38302	  if (scope)
38303	    id = build_qualified_name (NULL_TREE, scope, id, false);
38304	  parser->scope = NULL_TREE;
38305	  parser->qualifying_scope = NULL_TREE;
38306	  parser->object_scope = NULL_TREE;
38307	}
38308      else
38309	{
38310	  error ("invalid reduction-identifier");
38311	 resync_fail:
38312	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38313						 /*or_comma=*/false,
38314						 /*consume_paren=*/true);
38315	  return list;
38316	}
38317    }
38318
38319  if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38320    goto resync_fail;
38321
38322  nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
38323					  NULL);
38324  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38325    {
38326      OMP_CLAUSE_REDUCTION_CODE (c) = code;
38327      if (task)
38328	OMP_CLAUSE_REDUCTION_TASK (c) = 1;
38329      else if (inscan)
38330	OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
38331      OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
38332    }
38333
38334  return nlist;
38335}
38336
38337/* OpenMP 2.5:
38338   schedule ( schedule-kind )
38339   schedule ( schedule-kind , expression )
38340
38341   schedule-kind:
38342     static | dynamic | guided | runtime | auto
38343
38344   OpenMP 4.5:
38345   schedule ( schedule-modifier : schedule-kind )
38346   schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
38347
38348   schedule-modifier:
38349     simd
38350     monotonic
38351     nonmonotonic  */
38352
38353static tree
38354cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
38355{
38356  tree c, t;
38357  int modifiers = 0, nmodifiers = 0;
38358
38359  matching_parens parens;
38360  if (!parens.require_open (parser))
38361    return list;
38362
38363  c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
38364
38365  location_t comma = UNKNOWN_LOCATION;
38366  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38367    {
38368      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38369      const char *p = IDENTIFIER_POINTER (id);
38370      if (strcmp ("simd", p) == 0)
38371	OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
38372      else if (strcmp ("monotonic", p) == 0)
38373	modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
38374      else if (strcmp ("nonmonotonic", p) == 0)
38375	modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
38376      else
38377	break;
38378      comma = UNKNOWN_LOCATION;
38379      cp_lexer_consume_token (parser->lexer);
38380      if (nmodifiers++ == 0
38381	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38382	{
38383	  comma = cp_lexer_peek_token (parser->lexer)->location;
38384	  cp_lexer_consume_token (parser->lexer);
38385	}
38386      else
38387	{
38388	  cp_parser_require (parser, CPP_COLON, RT_COLON);
38389	  break;
38390	}
38391    }
38392  if (comma != UNKNOWN_LOCATION)
38393    error_at (comma, "expected %<:%>");
38394
38395  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38396    {
38397      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38398      const char *p = IDENTIFIER_POINTER (id);
38399
38400      switch (p[0])
38401	{
38402	case 'd':
38403	  if (strcmp ("dynamic", p) != 0)
38404	    goto invalid_kind;
38405	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
38406	  break;
38407
38408	case 'g':
38409	  if (strcmp ("guided", p) != 0)
38410	    goto invalid_kind;
38411	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
38412	  break;
38413
38414	case 'r':
38415	  if (strcmp ("runtime", p) != 0)
38416	    goto invalid_kind;
38417	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
38418	  break;
38419
38420	default:
38421	  goto invalid_kind;
38422	}
38423    }
38424  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
38425    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
38426  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
38427    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
38428  else
38429    goto invalid_kind;
38430  cp_lexer_consume_token (parser->lexer);
38431
38432  if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
38433		    | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38434      == (OMP_CLAUSE_SCHEDULE_MONOTONIC
38435	  | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38436    {
38437      error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
38438			  "specified");
38439      modifiers = 0;
38440    }
38441
38442  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38443    {
38444      cp_token *token;
38445      cp_lexer_consume_token (parser->lexer);
38446
38447      token = cp_lexer_peek_token (parser->lexer);
38448      t = cp_parser_assignment_expression (parser);
38449
38450      if (t == error_mark_node)
38451	goto resync_fail;
38452      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
38453	error_at (token->location, "schedule %<runtime%> does not take "
38454		  "a %<chunk_size%> parameter");
38455      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
38456	error_at (token->location, "schedule %<auto%> does not take "
38457		  "a %<chunk_size%> parameter");
38458      else
38459	OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
38460
38461      if (!parens.require_close (parser))
38462	goto resync_fail;
38463    }
38464  else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
38465    goto resync_fail;
38466
38467  OMP_CLAUSE_SCHEDULE_KIND (c)
38468    = (enum omp_clause_schedule_kind)
38469      (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
38470
38471  check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
38472  OMP_CLAUSE_CHAIN (c) = list;
38473  return c;
38474
38475 invalid_kind:
38476  cp_parser_error (parser, "invalid schedule kind");
38477 resync_fail:
38478  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38479					 /*or_comma=*/false,
38480					 /*consume_paren=*/true);
38481  return list;
38482}
38483
38484/* OpenMP 3.0:
38485   untied */
38486
38487static tree
38488cp_parser_omp_clause_untied (cp_parser * /*parser*/,
38489			     tree list, location_t location)
38490{
38491  tree c;
38492
38493  check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
38494
38495  c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
38496  OMP_CLAUSE_CHAIN (c) = list;
38497  return c;
38498}
38499
38500/* OpenMP 4.0:
38501   inbranch
38502   notinbranch */
38503
38504static tree
38505cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
38506			     tree list, location_t location)
38507{
38508  check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38509  tree c = build_omp_clause (location, code);
38510  OMP_CLAUSE_CHAIN (c) = list;
38511  return c;
38512}
38513
38514/* OpenMP 4.0:
38515   parallel
38516   for
38517   sections
38518   taskgroup */
38519
38520static tree
38521cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
38522				 enum omp_clause_code code,
38523				 tree list, location_t location)
38524{
38525  tree c = build_omp_clause (location, code);
38526  OMP_CLAUSE_CHAIN (c) = list;
38527  return c;
38528}
38529
38530/* OpenMP 4.5:
38531   nogroup */
38532
38533static tree
38534cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
38535			      tree list, location_t location)
38536{
38537  check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
38538  tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
38539  OMP_CLAUSE_CHAIN (c) = list;
38540  return c;
38541}
38542
38543/* OpenMP 4.5:
38544   simd
38545   threads */
38546
38547static tree
38548cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
38549				  enum omp_clause_code code,
38550				  tree list, location_t location)
38551{
38552  check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38553  tree c = build_omp_clause (location, code);
38554  OMP_CLAUSE_CHAIN (c) = list;
38555  return c;
38556}
38557
38558/* OpenMP 4.0:
38559   num_teams ( expression )
38560
38561   OpenMP 5.1:
38562   num_teams ( expression : expression ) */
38563
38564static tree
38565cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
38566				location_t location)
38567{
38568  tree upper, lower = NULL_TREE, c;
38569
38570  matching_parens parens;
38571  if (!parens.require_open (parser))
38572    return list;
38573
38574  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38575  parser->colon_corrects_to_scope_p = false;
38576  upper = cp_parser_assignment_expression (parser);
38577  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38578
38579  if (upper != error_mark_node
38580      && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38581    {
38582      lower = upper;
38583      cp_lexer_consume_token (parser->lexer);
38584      upper = cp_parser_assignment_expression (parser);
38585    }
38586
38587  if (upper == error_mark_node
38588      || !parens.require_close (parser))
38589    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38590					   /*or_comma=*/false,
38591					   /*consume_paren=*/true);
38592
38593  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
38594			     "num_teams", location);
38595
38596  c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
38597  OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
38598  OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
38599  OMP_CLAUSE_CHAIN (c) = list;
38600
38601  return c;
38602}
38603
38604/* OpenMP 4.0:
38605   thread_limit ( expression ) */
38606
38607static tree
38608cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
38609				   location_t location)
38610{
38611  tree t, c;
38612
38613  matching_parens parens;
38614  if (!parens.require_open (parser))
38615    return list;
38616
38617  t = cp_parser_assignment_expression (parser);
38618
38619  if (t == error_mark_node
38620      || !parens.require_close (parser))
38621    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38622					   /*or_comma=*/false,
38623					   /*consume_paren=*/true);
38624
38625  check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
38626			     "thread_limit", location);
38627
38628  c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
38629  OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
38630  OMP_CLAUSE_CHAIN (c) = list;
38631
38632  return c;
38633}
38634
38635/* OpenMP 4.0:
38636   aligned ( variable-list )
38637   aligned ( variable-list : constant-expression )  */
38638
38639static tree
38640cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
38641{
38642  tree nlist, c, alignment = NULL_TREE;
38643  bool colon;
38644
38645  matching_parens parens;
38646  if (!parens.require_open (parser))
38647    return list;
38648
38649  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
38650					  &colon);
38651
38652  if (colon)
38653    {
38654      alignment = cp_parser_constant_expression (parser);
38655
38656      if (!parens.require_close (parser))
38657	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38658					       /*or_comma=*/false,
38659					       /*consume_paren=*/true);
38660
38661      if (alignment == error_mark_node)
38662	alignment = NULL_TREE;
38663    }
38664
38665  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38666    OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
38667
38668  return nlist;
38669}
38670
38671/* OpenMP 5.0:
38672   allocate ( variable-list )
38673   allocate ( expression : variable-list )
38674
38675   OpenMP 5.1:
38676   allocate ( allocator-modifier : variable-list )
38677   allocate ( allocator-modifier , allocator-modifier : variable-list )
38678
38679   allocator-modifier:
38680   allocator ( expression )
38681   align ( expression )  */
38682
38683static tree
38684cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
38685{
38686  tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
38687  bool colon, has_modifiers = false;
38688
38689  matching_parens parens;
38690  if (!parens.require_open (parser))
38691    return list;
38692
38693  cp_parser_parse_tentatively (parser);
38694  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38695  parser->colon_corrects_to_scope_p = false;
38696  for (int mod = 0; mod < 2; mod++)
38697    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38698	&& cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38699      {
38700	tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38701	const char *p = IDENTIFIER_POINTER (id);
38702	if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
38703	  break;
38704	cp_lexer_consume_token (parser->lexer);
38705	matching_parens parens2;
38706	if (!parens2.require_open (parser))
38707	  break;
38708	if (strcmp (p, "allocator") == 0)
38709	  {
38710	    if (allocator != NULL_TREE)
38711	      break;
38712	    allocator = cp_parser_assignment_expression (parser);
38713	  }
38714	else
38715	  {
38716	    if (align != NULL_TREE)
38717	      break;
38718	    align = cp_parser_assignment_expression (parser);
38719	  }
38720	if (!parens2.require_close (parser))
38721	  break;
38722	if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38723	  {
38724	    has_modifiers = true;
38725	    break;
38726	  }
38727	if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38728	  break;
38729	cp_lexer_consume_token (parser->lexer);
38730      }
38731    else
38732      break;
38733  if (!has_modifiers)
38734    {
38735      cp_parser_abort_tentative_parse (parser);
38736      align = NULL_TREE;
38737      allocator = NULL_TREE;
38738      cp_parser_parse_tentatively (parser);
38739      allocator = cp_parser_assignment_expression (parser);
38740    }
38741  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38742  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38743    {
38744      cp_parser_parse_definitely (parser);
38745      cp_lexer_consume_token (parser->lexer);
38746      if (allocator == error_mark_node)
38747	allocator = NULL_TREE;
38748      if (align == error_mark_node)
38749	align = NULL_TREE;
38750    }
38751  else
38752    {
38753      cp_parser_abort_tentative_parse (parser);
38754      allocator = NULL_TREE;
38755      align = NULL_TREE;
38756    }
38757
38758  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
38759					  &colon);
38760
38761  if (allocator || align)
38762    for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38763      {
38764	OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
38765	OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
38766      }
38767
38768  return nlist;
38769}
38770
38771/* OpenMP 2.5:
38772   lastprivate ( variable-list )
38773
38774   OpenMP 5.0:
38775   lastprivate ( [ lastprivate-modifier : ] variable-list )  */
38776
38777static tree
38778cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
38779{
38780  bool conditional = false;
38781
38782  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38783    return list;
38784
38785  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38786      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38787    {
38788      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38789      const char *p = IDENTIFIER_POINTER (id);
38790
38791      if (strcmp ("conditional", p) == 0)
38792	{
38793	  conditional = true;
38794	  cp_lexer_consume_token (parser->lexer);
38795	  cp_lexer_consume_token (parser->lexer);
38796	}
38797    }
38798
38799  tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
38800					       list, NULL);
38801
38802  if (conditional)
38803    for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38804      OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
38805  return nlist;
38806}
38807
38808/* OpenMP 4.0:
38809   linear ( variable-list )
38810   linear ( variable-list : expression )
38811
38812   OpenMP 4.5:
38813   linear ( modifier ( variable-list ) )
38814   linear ( modifier ( variable-list ) : expression ) */
38815
38816static tree
38817cp_parser_omp_clause_linear (cp_parser *parser, tree list,
38818			     bool declare_simd)
38819{
38820  tree nlist, c, step = integer_one_node;
38821  bool colon;
38822  enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
38823
38824  matching_parens parens;
38825  if (!parens.require_open (parser))
38826    return list;
38827
38828  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38829    {
38830      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38831      const char *p = IDENTIFIER_POINTER (id);
38832
38833      if (strcmp ("ref", p) == 0)
38834	kind = OMP_CLAUSE_LINEAR_REF;
38835      else if (strcmp ("val", p) == 0)
38836	kind = OMP_CLAUSE_LINEAR_VAL;
38837      else if (strcmp ("uval", p) == 0)
38838	kind = OMP_CLAUSE_LINEAR_UVAL;
38839      if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38840	cp_lexer_consume_token (parser->lexer);
38841      else
38842	kind = OMP_CLAUSE_LINEAR_DEFAULT;
38843    }
38844
38845  if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
38846    nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
38847					    &colon);
38848  else
38849    {
38850      nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
38851      colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
38852      if (colon)
38853	cp_parser_require (parser, CPP_COLON, RT_COLON);
38854      else if (!parens.require_close (parser))
38855	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38856					       /*or_comma=*/false,
38857					       /*consume_paren=*/true);
38858    }
38859
38860  if (colon)
38861    {
38862      step = NULL_TREE;
38863      if (declare_simd
38864	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38865	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
38866	{
38867	  cp_token *token = cp_lexer_peek_token (parser->lexer);
38868	  cp_parser_parse_tentatively (parser);
38869	  step = cp_parser_id_expression (parser, /*template_p=*/false,
38870					  /*check_dependency_p=*/true,
38871					  /*template_p=*/NULL,
38872					  /*declarator_p=*/false,
38873					  /*optional_p=*/false);
38874	  if (step != error_mark_node)
38875	    step = cp_parser_lookup_name_simple (parser, step, token->location);
38876	  if (step == error_mark_node)
38877	    {
38878	      step = NULL_TREE;
38879	      cp_parser_abort_tentative_parse (parser);
38880	    }
38881	  else if (!cp_parser_parse_definitely (parser))
38882	    step = NULL_TREE;
38883	}
38884      if (!step)
38885	step = cp_parser_assignment_expression (parser);
38886
38887      if (!parens.require_close (parser))
38888	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38889					       /*or_comma=*/false,
38890					       /*consume_paren=*/true);
38891
38892      if (step == error_mark_node)
38893	return list;
38894    }
38895
38896  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38897    {
38898      OMP_CLAUSE_LINEAR_STEP (c) = step;
38899      OMP_CLAUSE_LINEAR_KIND (c) = kind;
38900    }
38901
38902  return nlist;
38903}
38904
38905/* OpenMP 4.0:
38906   safelen ( constant-expression )  */
38907
38908static tree
38909cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
38910			      location_t location)
38911{
38912  tree t, c;
38913
38914  matching_parens parens;
38915  if (!parens.require_open (parser))
38916    return list;
38917
38918  t = cp_parser_constant_expression (parser);
38919
38920  if (t == error_mark_node
38921      || !parens.require_close (parser))
38922    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38923					   /*or_comma=*/false,
38924					   /*consume_paren=*/true);
38925
38926  check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
38927
38928  c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
38929  OMP_CLAUSE_SAFELEN_EXPR (c) = t;
38930  OMP_CLAUSE_CHAIN (c) = list;
38931
38932  return c;
38933}
38934
38935/* OpenMP 4.0:
38936   simdlen ( constant-expression )  */
38937
38938static tree
38939cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
38940			      location_t location)
38941{
38942  tree t, c;
38943
38944  matching_parens parens;
38945  if (!parens.require_open (parser))
38946    return list;
38947
38948  t = cp_parser_constant_expression (parser);
38949
38950  if (t == error_mark_node
38951      || !parens.require_close (parser))
38952    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38953					   /*or_comma=*/false,
38954					   /*consume_paren=*/true);
38955
38956  check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
38957
38958  c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
38959  OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
38960  OMP_CLAUSE_CHAIN (c) = list;
38961
38962  return c;
38963}
38964
38965/* OpenMP 4.5:
38966   vec:
38967     identifier [+/- integer]
38968     vec , identifier [+/- integer]
38969*/
38970
38971static tree
38972cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
38973				  tree list)
38974{
38975  tree vec = NULL;
38976
38977  if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38978    {
38979      cp_parser_error (parser, "expected identifier");
38980      return list;
38981    }
38982
38983  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38984    {
38985      location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
38986      tree t, identifier = cp_parser_identifier (parser);
38987      tree addend = NULL;
38988
38989      if (identifier == error_mark_node)
38990	t = error_mark_node;
38991      else
38992	{
38993	  t = cp_parser_lookup_name_simple
38994		(parser, identifier,
38995		 cp_lexer_peek_token (parser->lexer)->location);
38996	  if (t == error_mark_node)
38997	    cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
38998					 id_loc);
38999	}
39000
39001      bool neg = false;
39002      if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
39003	neg = true;
39004      else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
39005	{
39006	  addend = integer_zero_node;
39007	  goto add_to_vector;
39008	}
39009      cp_lexer_consume_token (parser->lexer);
39010
39011      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
39012	{
39013	  cp_parser_error (parser, "expected integer");
39014	  return list;
39015	}
39016
39017      addend = cp_lexer_peek_token (parser->lexer)->u.value;
39018      if (TREE_CODE (addend) != INTEGER_CST)
39019	{
39020	  cp_parser_error (parser, "expected integer");
39021	  return list;
39022	}
39023      cp_lexer_consume_token (parser->lexer);
39024
39025    add_to_vector:
39026      if (t != error_mark_node)
39027	{
39028	  vec = tree_cons (addend, t, vec);
39029	  if (neg)
39030	    OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
39031	}
39032
39033      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
39034	  || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39035	break;
39036
39037      cp_lexer_consume_token (parser->lexer);
39038    }
39039
39040  if (vec)
39041    {
39042      tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
39043      OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
39044      OMP_CLAUSE_DECL (u) = nreverse (vec);
39045      OMP_CLAUSE_CHAIN (u) = list;
39046      return u;
39047    }
39048  return list;
39049}
39050
39051/* OpenMP 5.0:
39052   detach ( event-handle ) */
39053
39054static tree
39055cp_parser_omp_clause_detach (cp_parser *parser, tree list)
39056{
39057  matching_parens parens;
39058
39059  if (!parens.require_open (parser))
39060    return list;
39061
39062  cp_token *token;
39063  tree name, decl;
39064
39065  token = cp_lexer_peek_token (parser->lexer);
39066  name = cp_parser_id_expression (parser, /*template_p=*/false,
39067					  /*check_dependency_p=*/true,
39068					  /*template_p=*/NULL,
39069					  /*declarator_p=*/false,
39070					  /*optional_p=*/false);
39071  if (name == error_mark_node)
39072    decl = error_mark_node;
39073  else
39074    {
39075      if (identifier_p (name))
39076	decl = cp_parser_lookup_name_simple (parser, name, token->location);
39077      else
39078	decl = name;
39079      if (decl == error_mark_node)
39080	cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
39081				     token->location);
39082    }
39083
39084  if (decl == error_mark_node
39085      || !parens.require_close (parser))
39086    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39087					   /*or_comma=*/false,
39088					   /*consume_paren=*/true);
39089
39090  tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
39091  OMP_CLAUSE_DECL (u) = decl;
39092  OMP_CLAUSE_CHAIN (u) = list;
39093
39094  return u;
39095}
39096
39097/* OpenMP 5.0:
39098   iterators ( iterators-definition )
39099
39100   iterators-definition:
39101     iterator-specifier
39102     iterator-specifier , iterators-definition
39103
39104   iterator-specifier:
39105     identifier = range-specification
39106     iterator-type identifier = range-specification
39107
39108   range-specification:
39109     begin : end
39110     begin : end : step  */
39111
39112static tree
39113cp_parser_omp_iterators (cp_parser *parser)
39114{
39115  tree ret = NULL_TREE, *last = &ret;
39116  cp_lexer_consume_token (parser->lexer);
39117
39118  matching_parens parens;
39119  if (!parens.require_open (parser))
39120    return error_mark_node;
39121
39122  bool saved_colon_corrects_to_scope_p
39123    = parser->colon_corrects_to_scope_p;
39124  bool saved_colon_doesnt_start_class_def_p
39125    = parser->colon_doesnt_start_class_def_p;
39126
39127  do
39128    {
39129      tree iter_type;
39130      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39131	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
39132	iter_type = integer_type_node;
39133      else
39134	{
39135	  const char *saved_message
39136	    = parser->type_definition_forbidden_message;
39137	  parser->type_definition_forbidden_message
39138	    = G_("types may not be defined in iterator type");
39139
39140	  iter_type = cp_parser_type_id (parser);
39141
39142	  parser->type_definition_forbidden_message = saved_message;
39143	}
39144
39145      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39146      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39147	{
39148	  cp_parser_error (parser, "expected identifier");
39149	  break;
39150	}
39151
39152      tree id = cp_parser_identifier (parser);
39153      if (id == error_mark_node)
39154	break;
39155
39156      if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
39157	break;
39158
39159      parser->colon_corrects_to_scope_p = false;
39160      parser->colon_doesnt_start_class_def_p = true;
39161      tree begin = cp_parser_assignment_expression (parser);
39162
39163      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39164	break;
39165
39166      tree end = cp_parser_assignment_expression (parser);
39167
39168      tree step = integer_one_node;
39169      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39170	{
39171	  cp_lexer_consume_token (parser->lexer);
39172	  step = cp_parser_assignment_expression (parser);
39173	}
39174
39175      tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
39176      DECL_ARTIFICIAL (iter_var) = 1;
39177      DECL_CONTEXT (iter_var) = current_function_decl;
39178      pushdecl (iter_var);
39179
39180      *last = make_tree_vec (6);
39181      TREE_VEC_ELT (*last, 0) = iter_var;
39182      TREE_VEC_ELT (*last, 1) = begin;
39183      TREE_VEC_ELT (*last, 2) = end;
39184      TREE_VEC_ELT (*last, 3) = step;
39185      last = &TREE_CHAIN (*last);
39186
39187      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39188	{
39189	  cp_lexer_consume_token (parser->lexer);
39190	  continue;
39191	}
39192      break;
39193    }
39194  while (1);
39195
39196  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39197  parser->colon_doesnt_start_class_def_p
39198    = saved_colon_doesnt_start_class_def_p;
39199
39200  if (!parens.require_close (parser))
39201    cp_parser_skip_to_closing_parenthesis (parser,
39202					   /*recovering=*/true,
39203					   /*or_comma=*/false,
39204					   /*consume_paren=*/true);
39205
39206  return ret ? ret : error_mark_node;
39207}
39208
39209/* OpenMP 5.0:
39210   affinity ( [aff-modifier :] variable-list )
39211   aff-modifier:
39212     iterator ( iterators-definition )  */
39213
39214static tree
39215cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
39216{
39217  tree nlist, c, iterators = NULL_TREE;
39218
39219  matching_parens parens;
39220  if (!parens.require_open (parser))
39221    return list;
39222
39223  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39224    {
39225      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39226      const char *p = IDENTIFIER_POINTER (id);
39227      bool parse_iter = ((strcmp ("iterator", p) == 0)
39228			 && (cp_lexer_nth_token_is (parser->lexer, 2,
39229						    CPP_OPEN_PAREN)));
39230      if (parse_iter)
39231	{
39232	  size_t n = cp_parser_skip_balanced_tokens (parser, 2);
39233	  parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
39234	}
39235      if (parse_iter)
39236	{
39237	  begin_scope (sk_omp, NULL);
39238	  iterators = cp_parser_omp_iterators (parser);
39239	  if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39240	    {
39241	      if (iterators)
39242		poplevel (0, 1, 0);
39243	      cp_parser_skip_to_closing_parenthesis (parser,
39244						     /*recovering=*/true,
39245						     /*or_comma=*/false,
39246						     /*consume_paren=*/true);
39247	      return list;
39248	    }
39249	}
39250    }
39251  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
39252					  list, NULL);
39253  if (iterators)
39254    {
39255      tree block = poplevel (1, 1, 0);
39256      if (iterators != error_mark_node)
39257	{
39258	  TREE_VEC_ELT (iterators, 5) = block;
39259	  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39260	    OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
39261						   OMP_CLAUSE_DECL (c));
39262	}
39263    }
39264  return nlist;
39265}
39266
39267/* OpenMP 4.0:
39268   depend ( depend-kind : variable-list )
39269
39270   depend-kind:
39271     in | out | inout
39272
39273   OpenMP 4.5:
39274   depend ( source )
39275
39276   depend ( sink : vec )
39277
39278   OpenMP 5.0:
39279   depend ( depend-modifier , depend-kind: variable-list )
39280
39281   depend-kind:
39282     in | out | inout | mutexinoutset | depobj
39283
39284   depend-modifier:
39285     iterator ( iterators-definition )  */
39286
39287static tree
39288cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
39289{
39290  tree nlist, c, iterators = NULL_TREE;
39291  enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
39292
39293  matching_parens parens;
39294  if (!parens.require_open (parser))
39295    return list;
39296
39297  do
39298    {
39299      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39300	goto invalid_kind;
39301
39302      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39303      const char *p = IDENTIFIER_POINTER (id);
39304
39305      if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
39306	{
39307	  begin_scope (sk_omp, NULL);
39308	  iterators = cp_parser_omp_iterators (parser);
39309	  cp_parser_require (parser, CPP_COMMA, RT_COMMA);
39310	  continue;
39311	}
39312      if (strcmp ("in", p) == 0)
39313	kind = OMP_CLAUSE_DEPEND_IN;
39314      else if (strcmp ("inout", p) == 0)
39315	kind = OMP_CLAUSE_DEPEND_INOUT;
39316      else if (strcmp ("mutexinoutset", p) == 0)
39317	kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
39318      else if (strcmp ("out", p) == 0)
39319	kind = OMP_CLAUSE_DEPEND_OUT;
39320      else if (strcmp ("depobj", p) == 0)
39321	kind = OMP_CLAUSE_DEPEND_DEPOBJ;
39322      else if (strcmp ("sink", p) == 0)
39323	kind = OMP_CLAUSE_DEPEND_SINK;
39324      else if (strcmp ("source", p) == 0)
39325	kind = OMP_CLAUSE_DEPEND_SOURCE;
39326      else
39327	goto invalid_kind;
39328      break;
39329    }
39330  while (1);
39331
39332  cp_lexer_consume_token (parser->lexer);
39333
39334  if (iterators
39335      && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
39336    {
39337      poplevel (0, 1, 0);
39338      error_at (loc, "%<iterator%> modifier incompatible with %qs",
39339		kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
39340      iterators = NULL_TREE;
39341    }
39342
39343  if (kind == OMP_CLAUSE_DEPEND_SOURCE)
39344    {
39345      c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
39346      OMP_CLAUSE_DEPEND_KIND (c) = kind;
39347      OMP_CLAUSE_DECL (c) = NULL_TREE;
39348      OMP_CLAUSE_CHAIN (c) = list;
39349      if (!parens.require_close (parser))
39350	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39351					       /*or_comma=*/false,
39352					       /*consume_paren=*/true);
39353      return c;
39354    }
39355
39356  if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39357    goto resync_fail;
39358
39359  if (kind == OMP_CLAUSE_DEPEND_SINK)
39360    {
39361      nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
39362      if (!parens.require_close (parser))
39363	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39364					       /*or_comma=*/false,
39365					       /*consume_paren=*/true);
39366    }
39367  else
39368    {
39369      nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
39370					      list, NULL);
39371
39372      if (iterators)
39373	{
39374	  tree block = poplevel (1, 1, 0);
39375	  if (iterators == error_mark_node)
39376	    iterators = NULL_TREE;
39377	  else
39378	    TREE_VEC_ELT (iterators, 5) = block;
39379	}
39380
39381      for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39382	{
39383	  OMP_CLAUSE_DEPEND_KIND (c) = kind;
39384	  if (iterators)
39385	    OMP_CLAUSE_DECL (c)
39386	      = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
39387	}
39388    }
39389  return nlist;
39390
39391 invalid_kind:
39392  cp_parser_error (parser, "invalid depend kind");
39393 resync_fail:
39394  if (iterators)
39395    poplevel (0, 1, 0);
39396  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39397					 /*or_comma=*/false,
39398					 /*consume_paren=*/true);
39399  return list;
39400}
39401
39402/* OpenMP 4.0:
39403   map ( map-kind : variable-list )
39404   map ( variable-list )
39405
39406   map-kind:
39407     alloc | to | from | tofrom
39408
39409   OpenMP 4.5:
39410   map-kind:
39411     alloc | to | from | tofrom | release | delete
39412
39413   map ( always [,] map-kind: variable-list )
39414
39415   OpenMP 5.0:
39416   map ( [map-type-modifier[,] ...] map-kind: variable-list )
39417
39418   map-type-modifier:
39419     always | close */
39420
39421static tree
39422cp_parser_omp_clause_map (cp_parser *parser, tree list)
39423{
39424  tree nlist, c;
39425  enum gomp_map_kind kind = GOMP_MAP_TOFROM;
39426
39427  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39428    return list;
39429
39430  int pos = 1;
39431  int map_kind_pos = 0;
39432  while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
39433	 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
39434    {
39435      if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
39436	{
39437	  map_kind_pos = pos;
39438	  break;
39439	}
39440
39441      if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
39442	pos++;
39443      pos++;
39444    }
39445
39446  bool always_modifier = false;
39447  bool close_modifier = false;
39448  for (int pos = 1; pos < map_kind_pos; ++pos)
39449    {
39450      cp_token *tok = cp_lexer_peek_token (parser->lexer);
39451      if (tok->type == CPP_COMMA)
39452	{
39453	  cp_lexer_consume_token (parser->lexer);
39454	  continue;
39455	}
39456
39457      const char *p = IDENTIFIER_POINTER (tok->u.value);
39458      if (strcmp ("always", p) == 0)
39459	{
39460	  if (always_modifier)
39461	    {
39462	      cp_parser_error (parser, "too many %<always%> modifiers");
39463	      cp_parser_skip_to_closing_parenthesis (parser,
39464						     /*recovering=*/true,
39465						     /*or_comma=*/false,
39466						     /*consume_paren=*/true);
39467	      return list;
39468	    }
39469	  always_modifier = true;
39470	}
39471      else if (strcmp ("close", p) == 0)
39472	{
39473	  if (close_modifier)
39474	    {
39475	      cp_parser_error (parser, "too many %<close%> modifiers");
39476	      cp_parser_skip_to_closing_parenthesis (parser,
39477						     /*recovering=*/true,
39478						     /*or_comma=*/false,
39479						     /*consume_paren=*/true);
39480	      return list;
39481	    }
39482	  close_modifier = true;
39483	}
39484      else
39485	{
39486	  cp_parser_error (parser, "%<#pragma omp target%> with "
39487				   "modifier other than %<always%> or "
39488				   "%<close%> on %<map%> clause");
39489	  cp_parser_skip_to_closing_parenthesis (parser,
39490						 /*recovering=*/true,
39491						 /*or_comma=*/false,
39492						 /*consume_paren=*/true);
39493	  return list;
39494	}
39495
39496	cp_lexer_consume_token (parser->lexer);
39497    }
39498
39499  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39500      && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39501    {
39502      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39503      const char *p = IDENTIFIER_POINTER (id);
39504
39505      if (strcmp ("alloc", p) == 0)
39506	kind = GOMP_MAP_ALLOC;
39507      else if (strcmp ("to", p) == 0)
39508	kind = always_modifier ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
39509      else if (strcmp ("from", p) == 0)
39510	kind = always_modifier ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
39511      else if (strcmp ("tofrom", p) == 0)
39512	kind = always_modifier ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
39513      else if (strcmp ("release", p) == 0)
39514	kind = GOMP_MAP_RELEASE;
39515      else
39516	{
39517	  cp_parser_error (parser, "invalid map kind");
39518	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39519						 /*or_comma=*/false,
39520						 /*consume_paren=*/true);
39521	  return list;
39522	}
39523      cp_lexer_consume_token (parser->lexer);
39524      cp_lexer_consume_token (parser->lexer);
39525    }
39526  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
39527	   && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39528    {
39529      kind = GOMP_MAP_DELETE;
39530      cp_lexer_consume_token (parser->lexer);
39531      cp_lexer_consume_token (parser->lexer);
39532    }
39533
39534  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
39535					  NULL, true);
39536
39537  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39538    OMP_CLAUSE_SET_MAP_KIND (c, kind);
39539
39540  return nlist;
39541}
39542
39543/* OpenMP 4.0:
39544   device ( expression )
39545
39546   OpenMP 5.0:
39547   device ( [device-modifier :] integer-expression )
39548
39549   device-modifier:
39550     ancestor | device_num */
39551
39552static tree
39553cp_parser_omp_clause_device (cp_parser *parser, tree list,
39554			     location_t location)
39555{
39556  tree t, c;
39557  bool ancestor = false;
39558
39559  matching_parens parens;
39560  if (!parens.require_open (parser))
39561    return list;
39562
39563  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39564      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39565    {
39566      cp_token *tok = cp_lexer_peek_token (parser->lexer);
39567      const char *p = IDENTIFIER_POINTER (tok->u.value);
39568      if (strcmp ("ancestor", p) == 0)
39569	{
39570	  ancestor = true;
39571
39572	  /* A requires directive with the reverse_offload clause must be
39573	  specified.  */
39574	  if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
39575	    {
39576	      error_at (tok->location, "%<ancestor%> device modifier not "
39577				       "preceded by %<requires%> directive "
39578				       "with %<reverse_offload%> clause");
39579	      cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39580	      return list;
39581	    }
39582	}
39583      else if (strcmp ("device_num", p) == 0)
39584	;
39585      else
39586	{
39587	  error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
39588	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39589	  return list;
39590	}
39591      cp_lexer_consume_token (parser->lexer);
39592      cp_lexer_consume_token (parser->lexer);
39593    }
39594
39595  t = cp_parser_assignment_expression (parser);
39596
39597  if (t == error_mark_node
39598      || !parens.require_close (parser))
39599    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39600					   /*or_comma=*/false,
39601					   /*consume_paren=*/true);
39602
39603  check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
39604			     "device", location);
39605
39606  c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
39607  OMP_CLAUSE_DEVICE_ID (c) = t;
39608  OMP_CLAUSE_CHAIN (c) = list;
39609  OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
39610
39611  return c;
39612}
39613
39614/* OpenMP 4.0:
39615   dist_schedule ( static )
39616   dist_schedule ( static , expression )  */
39617
39618static tree
39619cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
39620				    location_t location)
39621{
39622  tree c, t;
39623
39624  matching_parens parens;
39625  if (!parens.require_open (parser))
39626    return list;
39627
39628  c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
39629
39630  if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39631    goto invalid_kind;
39632  cp_lexer_consume_token (parser->lexer);
39633
39634  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39635    {
39636      cp_lexer_consume_token (parser->lexer);
39637
39638      t = cp_parser_assignment_expression (parser);
39639
39640      if (t == error_mark_node)
39641	goto resync_fail;
39642      OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
39643
39644      if (!parens.require_close (parser))
39645	goto resync_fail;
39646    }
39647  else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39648    goto resync_fail;
39649
39650  /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
39651				"dist_schedule", location); */
39652  if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
39653    warning_at (location, 0, "too many %qs clauses", "dist_schedule");
39654  OMP_CLAUSE_CHAIN (c) = list;
39655  return c;
39656
39657 invalid_kind:
39658  cp_parser_error (parser, "invalid dist_schedule kind");
39659 resync_fail:
39660  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39661					 /*or_comma=*/false,
39662					 /*consume_paren=*/true);
39663  return list;
39664}
39665
39666/* OpenMP 4.0:
39667   proc_bind ( proc-bind-kind )
39668
39669   proc-bind-kind:
39670     primary | master | close | spread
39671   where OpenMP 5.1 added 'primary' and deprecated the alias 'master'.  */
39672
39673static tree
39674cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
39675				location_t location)
39676{
39677  tree c;
39678  enum omp_clause_proc_bind_kind kind;
39679
39680  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39681    return list;
39682
39683  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39684    {
39685      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39686      const char *p = IDENTIFIER_POINTER (id);
39687
39688      if (strcmp ("primary", p) == 0)
39689	kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
39690      else if (strcmp ("master", p) == 0)
39691	kind = OMP_CLAUSE_PROC_BIND_MASTER;
39692      else if (strcmp ("close", p) == 0)
39693	kind = OMP_CLAUSE_PROC_BIND_CLOSE;
39694      else if (strcmp ("spread", p) == 0)
39695	kind = OMP_CLAUSE_PROC_BIND_SPREAD;
39696      else
39697	goto invalid_kind;
39698    }
39699  else
39700    goto invalid_kind;
39701
39702  cp_lexer_consume_token (parser->lexer);
39703  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39704    goto resync_fail;
39705
39706  c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
39707  check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
39708			     location);
39709  OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
39710  OMP_CLAUSE_CHAIN (c) = list;
39711  return c;
39712
39713 invalid_kind:
39714  cp_parser_error (parser, "invalid depend kind");
39715 resync_fail:
39716  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39717					 /*or_comma=*/false,
39718					 /*consume_paren=*/true);
39719  return list;
39720}
39721
39722/* OpenMP 5.0:
39723   device_type ( host | nohost | any )  */
39724
39725static tree
39726cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
39727				  location_t location)
39728{
39729  tree c;
39730  enum omp_clause_device_type_kind kind;
39731
39732  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39733    return list;
39734
39735  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39736    {
39737      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39738      const char *p = IDENTIFIER_POINTER (id);
39739
39740      if (strcmp ("host", p) == 0)
39741	kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
39742      else if (strcmp ("nohost", p) == 0)
39743	kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
39744      else if (strcmp ("any", p) == 0)
39745	kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
39746      else
39747	goto invalid_kind;
39748    }
39749  else
39750    goto invalid_kind;
39751
39752  cp_lexer_consume_token (parser->lexer);
39753  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39754    goto resync_fail;
39755
39756  c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
39757  /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
39758				location);  */
39759  OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
39760  OMP_CLAUSE_CHAIN (c) = list;
39761  return c;
39762
39763 invalid_kind:
39764  cp_parser_error (parser, "invalid depend kind");
39765 resync_fail:
39766  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39767					 /*or_comma=*/false,
39768					 /*consume_paren=*/true);
39769  return list;
39770}
39771
39772/* OpenACC:
39773   async [( int-expr )] */
39774
39775static tree
39776cp_parser_oacc_clause_async (cp_parser *parser, tree list)
39777{
39778  tree c, t;
39779  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39780
39781  t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
39782
39783  if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39784    {
39785      matching_parens parens;
39786      parens.consume_open (parser);
39787
39788      t = cp_parser_assignment_expression (parser);
39789      if (t == error_mark_node
39790	  || !parens.require_close (parser))
39791	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39792						/*or_comma=*/false,
39793						/*consume_paren=*/true);
39794    }
39795
39796  check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
39797
39798  c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
39799  OMP_CLAUSE_ASYNC_EXPR (c) = t;
39800  OMP_CLAUSE_CHAIN (c) = list;
39801  list = c;
39802
39803  return list;
39804}
39805
39806/* Parse all OpenACC clauses.  The set clauses allowed by the directive
39807   is a bitmask in MASK.  Return the list of clauses found.  */
39808
39809static tree
39810cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
39811			    const char *where, cp_token *pragma_tok,
39812			    bool finish_p = true)
39813{
39814  tree clauses = NULL;
39815  bool first = true;
39816
39817  /* Don't create location wrapper nodes within OpenACC clauses.  */
39818  auto_suppress_location_wrappers sentinel;
39819
39820  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39821    {
39822      location_t here;
39823      pragma_omp_clause c_kind;
39824      omp_clause_code code;
39825      const char *c_name;
39826      tree prev = clauses;
39827
39828      if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39829	cp_lexer_consume_token (parser->lexer);
39830
39831      here = cp_lexer_peek_token (parser->lexer)->location;
39832      c_kind = cp_parser_omp_clause_name (parser);
39833
39834      switch (c_kind)
39835	{
39836	case PRAGMA_OACC_CLAUSE_ASYNC:
39837	  clauses = cp_parser_oacc_clause_async (parser, clauses);
39838	  c_name = "async";
39839	  break;
39840	case PRAGMA_OACC_CLAUSE_AUTO:
39841	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
39842						  clauses);
39843	  c_name = "auto";
39844	  break;
39845	case PRAGMA_OACC_CLAUSE_ATTACH:
39846	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39847	  c_name = "attach";
39848	  break;
39849	case PRAGMA_OACC_CLAUSE_COLLAPSE:
39850	  clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
39851	  c_name = "collapse";
39852	  break;
39853	case PRAGMA_OACC_CLAUSE_COPY:
39854	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39855	  c_name = "copy";
39856	  break;
39857	case PRAGMA_OACC_CLAUSE_COPYIN:
39858	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39859	  c_name = "copyin";
39860	  break;
39861	case PRAGMA_OACC_CLAUSE_COPYOUT:
39862	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39863	  c_name = "copyout";
39864	  break;
39865	case PRAGMA_OACC_CLAUSE_CREATE:
39866	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39867	  c_name = "create";
39868	  break;
39869	case PRAGMA_OACC_CLAUSE_DELETE:
39870	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39871	  c_name = "delete";
39872	  break;
39873	case PRAGMA_OMP_CLAUSE_DEFAULT:
39874	  clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
39875	  c_name = "default";
39876	  break;
39877	case PRAGMA_OACC_CLAUSE_DETACH:
39878	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39879	  c_name = "detach";
39880	  break;
39881	case PRAGMA_OACC_CLAUSE_DEVICE:
39882	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39883	  c_name = "device";
39884	  break;
39885	case PRAGMA_OACC_CLAUSE_DEVICEPTR:
39886	  clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
39887	  c_name = "deviceptr";
39888	  break;
39889	case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
39890	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39891	  c_name = "device_resident";
39892	  break;
39893	case PRAGMA_OACC_CLAUSE_FINALIZE:
39894	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
39895						  clauses);
39896	  c_name = "finalize";
39897	  break;
39898	case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
39899	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39900					    clauses);
39901	  c_name = "firstprivate";
39902	  break;
39903	case PRAGMA_OACC_CLAUSE_GANG:
39904	  c_name = "gang";
39905	  clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
39906						 c_name, clauses);
39907	  break;
39908	case PRAGMA_OACC_CLAUSE_HOST:
39909	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39910	  c_name = "host";
39911	  break;
39912	case PRAGMA_OACC_CLAUSE_IF:
39913	  clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
39914	  c_name = "if";
39915	  break;
39916	case PRAGMA_OACC_CLAUSE_IF_PRESENT:
39917	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
39918						  clauses);
39919	  c_name = "if_present";
39920	  break;
39921	case PRAGMA_OACC_CLAUSE_INDEPENDENT:
39922	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
39923						  clauses);
39924	  c_name = "independent";
39925	  break;
39926	case PRAGMA_OACC_CLAUSE_LINK:
39927	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39928	  c_name = "link";
39929	  break;
39930	case PRAGMA_OACC_CLAUSE_NO_CREATE:
39931	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39932	  c_name = "no_create";
39933	  break;
39934	case PRAGMA_OACC_CLAUSE_NOHOST:
39935	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
39936						  clauses);
39937	  c_name = "nohost";
39938	  break;
39939	case PRAGMA_OACC_CLAUSE_NUM_GANGS:
39940	  code = OMP_CLAUSE_NUM_GANGS;
39941	  c_name = "num_gangs";
39942	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39943						      clauses);
39944	  break;
39945	case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
39946	  c_name = "num_workers";
39947	  code = OMP_CLAUSE_NUM_WORKERS;
39948	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39949						      clauses);
39950	  break;
39951	case PRAGMA_OACC_CLAUSE_PRESENT:
39952	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39953	  c_name = "present";
39954	  break;
39955	case PRAGMA_OACC_CLAUSE_PRIVATE:
39956	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
39957					    clauses);
39958	  c_name = "private";
39959	  break;
39960	case PRAGMA_OACC_CLAUSE_REDUCTION:
39961	  clauses
39962	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
39963					      false, clauses);
39964	  c_name = "reduction";
39965	  break;
39966	case PRAGMA_OACC_CLAUSE_SEQ:
39967	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
39968						  clauses);
39969	  c_name = "seq";
39970	  break;
39971	case PRAGMA_OACC_CLAUSE_TILE:
39972	  clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
39973	  c_name = "tile";
39974	  break;
39975	case PRAGMA_OACC_CLAUSE_USE_DEVICE:
39976	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
39977					    clauses);
39978	  c_name = "use_device";
39979	  break;
39980	case PRAGMA_OACC_CLAUSE_VECTOR:
39981	  c_name = "vector";
39982	  clauses = cp_parser_oacc_shape_clause (parser, here,
39983						 OMP_CLAUSE_VECTOR,
39984						 c_name, clauses);
39985	  break;
39986	case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
39987	  c_name = "vector_length";
39988	  code = OMP_CLAUSE_VECTOR_LENGTH;
39989	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39990						      clauses);
39991	  break;
39992	case PRAGMA_OACC_CLAUSE_WAIT:
39993	  clauses = cp_parser_oacc_clause_wait (parser, clauses);
39994	  c_name = "wait";
39995	  break;
39996	case PRAGMA_OACC_CLAUSE_WORKER:
39997	  c_name = "worker";
39998	  clauses = cp_parser_oacc_shape_clause (parser, here,
39999						 OMP_CLAUSE_WORKER,
40000						 c_name, clauses);
40001	  break;
40002	default:
40003	  cp_parser_error (parser, "expected %<#pragma acc%> clause");
40004	  goto saw_error;
40005	}
40006
40007      first = false;
40008
40009      if (((mask >> c_kind) & 1) == 0)
40010	{
40011	  /* Remove the invalid clause(s) from the list to avoid
40012	     confusing the rest of the compiler.  */
40013	  clauses = prev;
40014	  error_at (here, "%qs is not valid for %qs", c_name, where);
40015	}
40016    }
40017
40018 saw_error:
40019  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40020
40021  if (finish_p)
40022    return finish_omp_clauses (clauses, C_ORT_ACC);
40023
40024  return clauses;
40025}
40026
40027/* Parse all OpenMP clauses.  The set clauses allowed by the directive
40028   is a bitmask in MASK.  Return the list of clauses found.
40029   FINISH_P set if finish_omp_clauses should be called.
40030   NESTED non-zero if clauses should be terminated by closing paren instead
40031   of end of pragma.  If it is 2, additionally commas are required in between
40032   the clauses.  */
40033
40034static tree
40035cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
40036			   const char *where, cp_token *pragma_tok,
40037			   bool finish_p = true, int nested = 0)
40038{
40039  tree clauses = NULL;
40040  bool first = true;
40041  cp_token *token = NULL;
40042
40043  /* Don't create location wrapper nodes within OpenMP clauses.  */
40044  auto_suppress_location_wrappers sentinel;
40045
40046  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40047    {
40048      pragma_omp_clause c_kind;
40049      const char *c_name;
40050      tree prev = clauses;
40051
40052      if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
40053	break;
40054
40055      if (!first
40056	  /* OpenMP 5.1 allows optional comma in between directive-name and
40057	     clauses everywhere, but as we aren't done with OpenMP 5.0
40058	     implementation yet, let's allow it for now only in C++11
40059	     attributes.  */
40060	  || (parser->lexer->in_omp_attribute_pragma && nested != 2))
40061	{
40062	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40063	    cp_lexer_consume_token (parser->lexer);
40064	  else if (nested == 2)
40065	    error_at (cp_lexer_peek_token (parser->lexer)->location,
40066		      "clauses in %<simd%> trait should be separated "
40067                      "by %<,%>");
40068	}
40069
40070      token = cp_lexer_peek_token (parser->lexer);
40071      c_kind = cp_parser_omp_clause_name (parser);
40072
40073      switch (c_kind)
40074	{
40075	case PRAGMA_OMP_CLAUSE_BIND:
40076	  clauses = cp_parser_omp_clause_bind (parser, clauses,
40077					       token->location);
40078	  c_name = "bind";
40079	  break;
40080	case PRAGMA_OMP_CLAUSE_COLLAPSE:
40081	  clauses = cp_parser_omp_clause_collapse (parser, clauses,
40082						   token->location);
40083	  c_name = "collapse";
40084	  break;
40085	case PRAGMA_OMP_CLAUSE_COPYIN:
40086	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
40087	  c_name = "copyin";
40088	  break;
40089	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
40090	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
40091					    clauses);
40092	  c_name = "copyprivate";
40093	  break;
40094	case PRAGMA_OMP_CLAUSE_DEFAULT:
40095	  clauses = cp_parser_omp_clause_default (parser, clauses,
40096						  token->location, false);
40097	  c_name = "default";
40098	  break;
40099	case PRAGMA_OMP_CLAUSE_FILTER:
40100	  clauses = cp_parser_omp_clause_filter (parser, clauses,
40101						 token->location);
40102	  c_name = "filter";
40103	  break;
40104	case PRAGMA_OMP_CLAUSE_FINAL:
40105	  clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
40106	  c_name = "final";
40107	  break;
40108	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
40109	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
40110					    clauses);
40111	  c_name = "firstprivate";
40112	  break;
40113	case PRAGMA_OMP_CLAUSE_GRAINSIZE:
40114	  clauses = cp_parser_omp_clause_grainsize (parser, clauses,
40115						    token->location);
40116	  c_name = "grainsize";
40117	  break;
40118	case PRAGMA_OMP_CLAUSE_HINT:
40119	  clauses = cp_parser_omp_clause_hint (parser, clauses,
40120					       token->location);
40121	  c_name = "hint";
40122	  break;
40123	case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
40124	  clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
40125						     token->location);
40126	  c_name = "defaultmap";
40127	  break;
40128	case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
40129	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
40130					    clauses);
40131	  c_name = "use_device_ptr";
40132	  break;
40133	case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
40134	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
40135					    clauses);
40136	  c_name = "use_device_addr";
40137	  break;
40138	case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
40139	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
40140					    clauses);
40141	  c_name = "is_device_ptr";
40142	  break;
40143	case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
40144	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
40145					    clauses);
40146	  c_name = "has_device_addr";
40147	  break;
40148	case PRAGMA_OMP_CLAUSE_IF:
40149	  clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
40150					     true);
40151	  c_name = "if";
40152	  break;
40153	case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
40154	  clauses
40155	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
40156					      true, clauses);
40157	  c_name = "in_reduction";
40158	  break;
40159	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
40160	  clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
40161	  c_name = "lastprivate";
40162	  break;
40163	case PRAGMA_OMP_CLAUSE_MERGEABLE:
40164	  clauses = cp_parser_omp_clause_mergeable (parser, clauses,
40165						    token->location);
40166	  c_name = "mergeable";
40167	  break;
40168	case PRAGMA_OMP_CLAUSE_NOWAIT:
40169	  clauses = cp_parser_omp_clause_nowait (parser, clauses,
40170						 token->location);
40171	  c_name = "nowait";
40172	  break;
40173	case PRAGMA_OMP_CLAUSE_NUM_TASKS:
40174	  clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
40175						    token->location);
40176	  c_name = "num_tasks";
40177	  break;
40178	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
40179	  clauses = cp_parser_omp_clause_num_threads (parser, clauses,
40180						      token->location);
40181	  c_name = "num_threads";
40182	  break;
40183	case PRAGMA_OMP_CLAUSE_ORDER:
40184	  clauses = cp_parser_omp_clause_order (parser, clauses,
40185						token->location);
40186	  c_name = "order";
40187	  break;
40188	case PRAGMA_OMP_CLAUSE_ORDERED:
40189	  clauses = cp_parser_omp_clause_ordered (parser, clauses,
40190						  token->location);
40191	  c_name = "ordered";
40192	  break;
40193	case PRAGMA_OMP_CLAUSE_PRIORITY:
40194	  clauses = cp_parser_omp_clause_priority (parser, clauses,
40195						   token->location);
40196	  c_name = "priority";
40197	  break;
40198	case PRAGMA_OMP_CLAUSE_PRIVATE:
40199	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
40200					    clauses);
40201	  c_name = "private";
40202	  break;
40203	case PRAGMA_OMP_CLAUSE_REDUCTION:
40204	  clauses
40205	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
40206					      true, clauses);
40207	  c_name = "reduction";
40208	  break;
40209	case PRAGMA_OMP_CLAUSE_SCHEDULE:
40210	  clauses = cp_parser_omp_clause_schedule (parser, clauses,
40211						   token->location);
40212	  c_name = "schedule";
40213	  break;
40214	case PRAGMA_OMP_CLAUSE_SHARED:
40215	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
40216					    clauses);
40217	  c_name = "shared";
40218	  break;
40219	case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
40220	  clauses
40221	    = cp_parser_omp_clause_reduction (parser,
40222					      OMP_CLAUSE_TASK_REDUCTION,
40223					      true, clauses);
40224	  c_name = "task_reduction";
40225	  break;
40226	case PRAGMA_OMP_CLAUSE_UNTIED:
40227	  clauses = cp_parser_omp_clause_untied (parser, clauses,
40228						 token->location);
40229	  c_name = "untied";
40230	  break;
40231	case PRAGMA_OMP_CLAUSE_INBRANCH:
40232	  clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
40233						 clauses, token->location);
40234	  c_name = "inbranch";
40235	  break;
40236	case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
40237	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
40238					    clauses);
40239	  c_name = "nontemporal";
40240	  break;
40241	case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
40242	  clauses = cp_parser_omp_clause_branch (parser,
40243						 OMP_CLAUSE_NOTINBRANCH,
40244						 clauses, token->location);
40245	  c_name = "notinbranch";
40246	  break;
40247	case PRAGMA_OMP_CLAUSE_PARALLEL:
40248	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
40249						     clauses, token->location);
40250	  c_name = "parallel";
40251	  if (!first)
40252	    {
40253	     clause_not_first:
40254	      error_at (token->location, "%qs must be the first clause of %qs",
40255			c_name, where);
40256	      clauses = prev;
40257	    }
40258	  break;
40259	case PRAGMA_OMP_CLAUSE_FOR:
40260	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
40261						     clauses, token->location);
40262	  c_name = "for";
40263	  if (!first)
40264	    goto clause_not_first;
40265	  break;
40266	case PRAGMA_OMP_CLAUSE_SECTIONS:
40267	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
40268						     clauses, token->location);
40269	  c_name = "sections";
40270	  if (!first)
40271	    goto clause_not_first;
40272	  break;
40273	case PRAGMA_OMP_CLAUSE_TASKGROUP:
40274	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
40275						     clauses, token->location);
40276	  c_name = "taskgroup";
40277	  if (!first)
40278	    goto clause_not_first;
40279	  break;
40280	case PRAGMA_OMP_CLAUSE_LINK:
40281	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
40282	  c_name = "to";
40283	  break;
40284	case PRAGMA_OMP_CLAUSE_TO:
40285	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
40286	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
40287					      clauses);
40288	  else
40289	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses,
40290					      true);
40291	  c_name = "to";
40292	  break;
40293	case PRAGMA_OMP_CLAUSE_FROM:
40294	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses,
40295					    true);
40296	  c_name = "from";
40297	  break;
40298	case PRAGMA_OMP_CLAUSE_UNIFORM:
40299	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
40300					    clauses);
40301	  c_name = "uniform";
40302	  break;
40303	case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
40304	  clauses = cp_parser_omp_clause_num_teams (parser, clauses,
40305						    token->location);
40306	  c_name = "num_teams";
40307	  break;
40308	case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
40309	  clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
40310						       token->location);
40311	  c_name = "thread_limit";
40312	  break;
40313	case PRAGMA_OMP_CLAUSE_ALIGNED:
40314	  clauses = cp_parser_omp_clause_aligned (parser, clauses);
40315	  c_name = "aligned";
40316	  break;
40317	case PRAGMA_OMP_CLAUSE_ALLOCATE:
40318	  clauses = cp_parser_omp_clause_allocate (parser, clauses);
40319	  c_name = "allocate";
40320	  break;
40321	case PRAGMA_OMP_CLAUSE_LINEAR:
40322	  {
40323	    bool declare_simd = false;
40324	    if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
40325	      declare_simd = true;
40326	    clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
40327	  }
40328	  c_name = "linear";
40329	  break;
40330	case PRAGMA_OMP_CLAUSE_AFFINITY:
40331	  clauses = cp_parser_omp_clause_affinity (parser, clauses);
40332	  c_name = "affinity";
40333	  break;
40334	case PRAGMA_OMP_CLAUSE_DEPEND:
40335	  clauses = cp_parser_omp_clause_depend (parser, clauses,
40336						 token->location);
40337	  c_name = "depend";
40338	  break;
40339	case PRAGMA_OMP_CLAUSE_DETACH:
40340	  clauses = cp_parser_omp_clause_detach (parser, clauses);
40341	  c_name = "detach";
40342	  break;
40343	case PRAGMA_OMP_CLAUSE_MAP:
40344	  clauses = cp_parser_omp_clause_map (parser, clauses);
40345	  c_name = "map";
40346	  break;
40347	case PRAGMA_OMP_CLAUSE_DEVICE:
40348	  clauses = cp_parser_omp_clause_device (parser, clauses,
40349						 token->location);
40350	  c_name = "device";
40351	  break;
40352	case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
40353	  clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
40354							token->location);
40355	  c_name = "dist_schedule";
40356	  break;
40357	case PRAGMA_OMP_CLAUSE_PROC_BIND:
40358	  clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
40359						    token->location);
40360	  c_name = "proc_bind";
40361	  break;
40362	case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
40363	  clauses = cp_parser_omp_clause_device_type (parser, clauses,
40364						      token->location);
40365	  c_name = "device_type";
40366	  break;
40367	case PRAGMA_OMP_CLAUSE_SAFELEN:
40368	  clauses = cp_parser_omp_clause_safelen (parser, clauses,
40369						  token->location);
40370	  c_name = "safelen";
40371	  break;
40372	case PRAGMA_OMP_CLAUSE_SIMDLEN:
40373	  clauses = cp_parser_omp_clause_simdlen (parser, clauses,
40374						  token->location);
40375	  c_name = "simdlen";
40376	  break;
40377	case PRAGMA_OMP_CLAUSE_NOGROUP:
40378	  clauses = cp_parser_omp_clause_nogroup (parser, clauses,
40379						  token->location);
40380	  c_name = "nogroup";
40381	  break;
40382	case PRAGMA_OMP_CLAUSE_THREADS:
40383	  clauses
40384	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
40385						clauses, token->location);
40386	  c_name = "threads";
40387	  break;
40388	case PRAGMA_OMP_CLAUSE_SIMD:
40389	  clauses
40390	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
40391						clauses, token->location);
40392	  c_name = "simd";
40393	  break;
40394	default:
40395	  cp_parser_error (parser, "expected %<#pragma omp%> clause");
40396	  goto saw_error;
40397	}
40398
40399      first = false;
40400
40401      if (((mask >> c_kind) & 1) == 0)
40402	{
40403	  /* Remove the invalid clause(s) from the list to avoid
40404	     confusing the rest of the compiler.  */
40405	  clauses = prev;
40406	  error_at (token->location, "%qs is not valid for %qs", c_name, where);
40407	}
40408    }
40409 saw_error:
40410  if (!nested)
40411    cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40412  if (finish_p)
40413    {
40414      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
40415	return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
40416      else
40417	return finish_omp_clauses (clauses, C_ORT_OMP);
40418    }
40419  return clauses;
40420}
40421
40422/* OpenMP 2.5:
40423   structured-block:
40424     statement
40425
40426   In practice, we're also interested in adding the statement to an
40427   outer node.  So it is convenient if we work around the fact that
40428   cp_parser_statement calls add_stmt.  */
40429
40430static unsigned
40431cp_parser_begin_omp_structured_block (cp_parser *parser)
40432{
40433  unsigned save = parser->in_statement;
40434
40435  /* Only move the values to IN_OMP_BLOCK if they weren't false.
40436     This preserves the "not within loop or switch" style error messages
40437     for nonsense cases like
40438	void foo() {
40439	#pragma omp single
40440	  break;
40441	}
40442  */
40443  if (parser->in_statement)
40444    parser->in_statement = IN_OMP_BLOCK;
40445
40446  return save;
40447}
40448
40449static void
40450cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
40451{
40452  parser->in_statement = save;
40453}
40454
40455static tree
40456cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
40457{
40458  tree stmt = begin_omp_structured_block ();
40459  unsigned int save = cp_parser_begin_omp_structured_block (parser);
40460
40461  parser->omp_attrs_forbidden_p = true;
40462  cp_parser_statement (parser, NULL_TREE, false, if_p);
40463
40464  cp_parser_end_omp_structured_block (parser, save);
40465  return finish_omp_structured_block (stmt);
40466}
40467
40468/* OpenMP 5.0:
40469   # pragma omp allocate (list)  [allocator(allocator)]  */
40470
40471static void
40472cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
40473{
40474  tree allocator = NULL_TREE;
40475  location_t loc = pragma_tok->location;
40476  tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
40477
40478  /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
40479  if (parser->lexer->in_omp_attribute_pragma
40480      && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40481      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40482    cp_lexer_consume_token (parser->lexer);
40483
40484  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40485    {
40486      matching_parens parens;
40487      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40488      const char *p = IDENTIFIER_POINTER (id);
40489      location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40490      cp_lexer_consume_token (parser->lexer);
40491      if (strcmp (p, "allocator") != 0)
40492	error_at (cloc, "expected %<allocator%>");
40493      else if (parens.require_open (parser))
40494	{
40495	  allocator = cp_parser_assignment_expression (parser);
40496	  if (allocator == error_mark_node)
40497	    allocator = NULL_TREE;
40498	  parens.require_close (parser);
40499	}
40500    }
40501  cp_parser_require_pragma_eol (parser, pragma_tok);
40502
40503  if (allocator)
40504    for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
40505      OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40506
40507  sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
40508}
40509
40510/* OpenMP 2.5:
40511   # pragma omp atomic new-line
40512     expression-stmt
40513
40514   expression-stmt:
40515     x binop= expr | x++ | ++x | x-- | --x
40516   binop:
40517     +, *, -, /, &, ^, |, <<, >>
40518
40519  where x is an lvalue expression with scalar type.
40520
40521   OpenMP 3.1:
40522   # pragma omp atomic new-line
40523     update-stmt
40524
40525   # pragma omp atomic read new-line
40526     read-stmt
40527
40528   # pragma omp atomic write new-line
40529     write-stmt
40530
40531   # pragma omp atomic update new-line
40532     update-stmt
40533
40534   # pragma omp atomic capture new-line
40535     capture-stmt
40536
40537   # pragma omp atomic capture new-line
40538     capture-block
40539
40540   read-stmt:
40541     v = x
40542   write-stmt:
40543     x = expr
40544   update-stmt:
40545     expression-stmt | x = x binop expr
40546   capture-stmt:
40547     v = expression-stmt
40548   capture-block:
40549     { v = x; update-stmt; } | { update-stmt; v = x; }
40550
40551   OpenMP 4.0:
40552   update-stmt:
40553     expression-stmt | x = x binop expr | x = expr binop x
40554   capture-stmt:
40555     v = update-stmt
40556   capture-block:
40557     { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
40558
40559   OpenMP 5.1:
40560   # pragma omp atomic compare new-line
40561     conditional-update-atomic
40562
40563   # pragma omp atomic compare capture new-line
40564     conditional-update-capture-atomic
40565
40566   conditional-update-atomic:
40567     cond-expr-stmt | cond-update-stmt
40568   cond-expr-stmt:
40569     x = expr ordop x ? expr : x;
40570     x = x ordop expr ? expr : x;
40571     x = x == e ? d : x;
40572   cond-update-stmt:
40573     if (expr ordop x) { x = expr; }
40574     if (x ordop expr) { x = expr; }
40575     if (x == e) { x = d; }
40576   ordop:
40577     <, >
40578   conditional-update-capture-atomic:
40579     v = cond-expr-stmt
40580     { v = x; cond-expr-stmt }
40581     { cond-expr-stmt v = x; }
40582     { v = x; cond-update-stmt }
40583     { cond-update-stmt v = x; }
40584     if (x == e) { x = d; } else { v = x; }
40585     { r = x == e; if (r) { x = d; } }
40586     { r = x == e; if (r) { x = d; } else { v = x; } }
40587
40588  where x, r and v are lvalue expressions with scalar type,
40589  expr, e and d are expressions with scalar type and e might be
40590  the same as v.  */
40591
40592static void
40593cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
40594{
40595  tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
40596  tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
40597  location_t loc = pragma_tok->location;
40598  enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
40599  enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
40600  bool structured_block = false;
40601  bool first = true;
40602  tree clauses = NULL_TREE;
40603  bool capture = false;
40604  bool compare = false;
40605  bool weak = false;
40606  enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40607  bool no_semicolon = false;
40608  bool extra_scope = false;
40609
40610  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40611    {
40612      /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
40613      if ((!first || parser->lexer->in_omp_attribute_pragma)
40614	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40615	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40616	cp_lexer_consume_token (parser->lexer);
40617
40618      first = false;
40619
40620      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40621	{
40622	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40623	  location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40624	  const char *p = IDENTIFIER_POINTER (id);
40625	  enum tree_code new_code = ERROR_MARK;
40626	  enum omp_memory_order new_memory_order
40627	    = OMP_MEMORY_ORDER_UNSPECIFIED;
40628	  bool new_capture = false;
40629	  bool new_compare = false;
40630	  bool new_weak = false;
40631	  enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40632
40633	  if (!strcmp (p, "read"))
40634	    new_code = OMP_ATOMIC_READ;
40635	  else if (!strcmp (p, "write"))
40636	    new_code = NOP_EXPR;
40637	  else if (!strcmp (p, "update"))
40638	    new_code = OMP_ATOMIC;
40639	  else if (openacc && !strcmp (p, "capture"))
40640	    new_code = OMP_ATOMIC_CAPTURE_NEW;
40641	  else if (openacc)
40642	    {
40643	      p = NULL;
40644	      error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40645			      "or %<capture%> clause");
40646	    }
40647	  else if (!strcmp (p, "capture"))
40648	    new_capture = true;
40649	  else if (!strcmp (p, "compare"))
40650	    new_compare = true;
40651	  else if (!strcmp (p, "weak"))
40652	    new_weak = true;
40653	  else if (!strcmp (p, "fail"))
40654	    {
40655	      matching_parens parens;
40656
40657	      cp_lexer_consume_token (parser->lexer);
40658	      if (!parens.require_open (parser))
40659		continue;
40660
40661	      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40662		{
40663		  id = cp_lexer_peek_token (parser->lexer)->u.value;
40664		  const char *q = IDENTIFIER_POINTER (id);
40665
40666		  if (!strcmp (q, "seq_cst"))
40667		    new_fail = OMP_MEMORY_ORDER_SEQ_CST;
40668		  else if (!strcmp (q, "acquire"))
40669		    new_fail = OMP_MEMORY_ORDER_ACQUIRE;
40670		  else if (!strcmp (q, "relaxed"))
40671		    new_fail = OMP_MEMORY_ORDER_RELAXED;
40672		}
40673
40674	      if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40675		{
40676		  cp_lexer_consume_token (parser->lexer);
40677		  if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40678		    error_at (cloc, "too many %qs clauses", "fail");
40679		  else
40680		    fail = new_fail;
40681		}
40682	      else
40683		cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
40684					 "or %<relaxed%>");
40685	      if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
40686		  || !parens.require_close (parser))
40687		cp_parser_skip_to_closing_parenthesis (parser,
40688						       /*recovering=*/true,
40689						       /*or_comma=*/false,
40690						       /*consume_paren=*/true);
40691	      continue;
40692	    }
40693	  else if (!strcmp (p, "seq_cst"))
40694	    new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40695	  else if (!strcmp (p, "acq_rel"))
40696	    new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40697	  else if (!strcmp (p, "release"))
40698	    new_memory_order = OMP_MEMORY_ORDER_RELEASE;
40699	  else if (!strcmp (p, "acquire"))
40700	    new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40701	  else if (!strcmp (p, "relaxed"))
40702	    new_memory_order = OMP_MEMORY_ORDER_RELAXED;
40703	  else if (!strcmp (p, "hint"))
40704	    {
40705	      cp_lexer_consume_token (parser->lexer);
40706	      clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
40707	      continue;
40708	    }
40709	  else
40710	    {
40711	      p = NULL;
40712	      error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40713			      "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
40714			      "%<seq_cst%>, %<acq_rel%>, %<release%>, "
40715			      "%<relaxed%> or %<hint%> clause");
40716	    }
40717	  if (p)
40718	    {
40719	      if (new_code != ERROR_MARK)
40720		{
40721		  /* OpenACC permits 'update capture'.  */
40722		  if (openacc
40723		      && code == OMP_ATOMIC
40724		      && new_code == OMP_ATOMIC_CAPTURE_NEW)
40725		    code = new_code;
40726		  else if (code != ERROR_MARK)
40727		    error_at (cloc, "too many atomic clauses");
40728		  else
40729		    code = new_code;
40730		}
40731	      else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40732		{
40733		  if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40734		    error_at (cloc, "too many memory order clauses");
40735		  else
40736		    memory_order = new_memory_order;
40737		}
40738	      else if (new_capture)
40739		{
40740		  if (capture)
40741		    error_at (cloc, "too many %qs clauses", "capture");
40742		  else
40743		    capture = true;
40744		}
40745	      else if (new_compare)
40746		{
40747		  if (compare)
40748		    error_at (cloc, "too many %qs clauses", "compare");
40749		  else
40750		    compare = true;
40751		}
40752	      else if (new_weak)
40753		{
40754		  if (weak)
40755		    error_at (cloc, "too many %qs clauses", "weak");
40756		  else
40757		    weak = true;
40758		}
40759	      cp_lexer_consume_token (parser->lexer);
40760	      continue;
40761	    }
40762	}
40763      break;
40764    }
40765  cp_parser_require_pragma_eol (parser, pragma_tok);
40766
40767  if (code == ERROR_MARK)
40768    code = OMP_ATOMIC;
40769  if (capture)
40770    {
40771      if (code != OMP_ATOMIC)
40772	error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40773		       "clauses", "capture");
40774      else
40775	code = OMP_ATOMIC_CAPTURE_NEW;
40776    }
40777  if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
40778    {
40779      error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40780		     "clauses", "compare");
40781      compare = false;
40782    }
40783  if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
40784    {
40785      error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
40786      fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40787    }
40788  if (weak && !compare)
40789    {
40790      error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
40791      weak = false;
40792    }
40793  if (openacc)
40794    memory_order = OMP_MEMORY_ORDER_RELAXED;
40795  else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
40796    {
40797      omp_requires_mask
40798	= (enum omp_requires) (omp_requires_mask
40799			       | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
40800      switch ((enum omp_memory_order)
40801	      (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
40802	{
40803	case OMP_MEMORY_ORDER_UNSPECIFIED:
40804	case OMP_MEMORY_ORDER_RELAXED:
40805	  memory_order = OMP_MEMORY_ORDER_RELAXED;
40806	  break;
40807	case OMP_MEMORY_ORDER_SEQ_CST:
40808	  memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40809	  break;
40810	case OMP_MEMORY_ORDER_ACQ_REL:
40811	  switch (code)
40812	    {
40813	    case OMP_ATOMIC_READ:
40814	      memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40815	      break;
40816	    case NOP_EXPR: /* atomic write */
40817	      memory_order = OMP_MEMORY_ORDER_RELEASE;
40818	      break;
40819	    default:
40820	      memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40821	      break;
40822	    }
40823	  break;
40824	default:
40825	  gcc_unreachable ();
40826	}
40827    }
40828  else
40829    switch (code)
40830      {
40831      case OMP_ATOMIC_READ:
40832	if (memory_order == OMP_MEMORY_ORDER_RELEASE)
40833	  {
40834	    error_at (loc, "%<#pragma omp atomic read%> incompatible with "
40835			   "%<release%> clause");
40836	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40837	  }
40838	else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40839	  memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40840	break;
40841      case NOP_EXPR: /* atomic write */
40842	if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
40843	  {
40844	    error_at (loc, "%<#pragma omp atomic write%> incompatible with "
40845			   "%<acquire%> clause");
40846	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40847	  }
40848	else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40849	  memory_order = OMP_MEMORY_ORDER_RELEASE;
40850	break;
40851      default:
40852	break;
40853      }
40854  if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40855    memory_order
40856      = (enum omp_memory_order) (memory_order
40857				 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
40858
40859  switch (code)
40860    {
40861    case OMP_ATOMIC_READ:
40862    case NOP_EXPR: /* atomic write */
40863      v = cp_parser_unary_expression (parser);
40864      if (v == error_mark_node)
40865	goto saw_error;
40866      if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40867	goto saw_error;
40868      if (code == NOP_EXPR)
40869	lhs = cp_parser_expression (parser);
40870      else
40871	lhs = cp_parser_unary_expression (parser);
40872      if (lhs == error_mark_node)
40873	goto saw_error;
40874      if (code == NOP_EXPR)
40875	{
40876	  /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
40877	     opcode.  */
40878	  code = OMP_ATOMIC;
40879	  rhs = lhs;
40880	  lhs = v;
40881	  v = NULL_TREE;
40882	}
40883      goto done;
40884    case OMP_ATOMIC_CAPTURE_NEW:
40885      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
40886	{
40887	  cp_lexer_consume_token (parser->lexer);
40888	  structured_block = true;
40889	}
40890      else if (compare
40891	       && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40892	break;
40893      else
40894	{
40895	  v = cp_parser_unary_expression (parser);
40896	  if (v == error_mark_node)
40897	    goto saw_error;
40898	  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40899	    goto saw_error;
40900	  if (compare
40901	      && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40902	    {
40903	      location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
40904	      error_at (eloc, "expected expression");
40905	      goto saw_error;
40906	    }
40907	}
40908    default:
40909      break;
40910    }
40911
40912restart:
40913  if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40914    {
40915      cp_lexer_consume_token (parser->lexer);
40916
40917      matching_parens parens;
40918      if (!parens.require_open (parser))
40919	goto saw_error;
40920      location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
40921      tree cmp_expr;
40922      if (r)
40923	cmp_expr = cp_parser_unary_expression (parser);
40924      else
40925	cmp_expr = cp_parser_binary_expression (parser, false, true,
40926						PREC_NOT_OPERATOR, NULL);
40927      if (!parens.require_close (parser))
40928	cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40929      if (cmp_expr == error_mark_node)
40930	goto saw_error;
40931      if (r)
40932	{
40933	  if (!cp_tree_equal (cmp_expr, r))
40934	    goto bad_if;
40935	  cmp_expr = rhs;
40936	  rhs = NULL_TREE;
40937	  gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
40938	}
40939      if (TREE_CODE (cmp_expr) == EQ_EXPR)
40940	;
40941      else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
40942	{
40943	  error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
40944		    "expected %<==%> comparison in %<if%> condition");
40945	  goto saw_error;
40946	}
40947      else if (TREE_CODE (cmp_expr) != GT_EXPR
40948	       && TREE_CODE (cmp_expr) != LT_EXPR)
40949	{
40950	  error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
40951		    "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
40952		    "condition");
40953	  goto saw_error;
40954	}
40955      if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
40956	goto saw_error;
40957
40958      extra_scope = true;
40959      eloc = cp_lexer_peek_token (parser->lexer)->location;
40960      lhs = cp_parser_unary_expression (parser);
40961      orig_lhs = lhs;
40962      if (lhs == error_mark_node)
40963	goto saw_error;
40964      if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
40965	{
40966	  cp_parser_error (parser, "expected %<=%>");
40967	  goto saw_error;
40968	}
40969      cp_lexer_consume_token (parser->lexer);
40970      eloc = cp_lexer_peek_token (parser->lexer)->location;
40971      if (TREE_CODE (cmp_expr) == EQ_EXPR)
40972	rhs1 = cp_parser_expression (parser);
40973      else
40974	rhs1 = cp_parser_simple_cast_expression (parser);
40975
40976      if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
40977	goto saw_error;
40978
40979      if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
40980	goto saw_error;
40981
40982      extra_scope = false;
40983      no_semicolon = true;
40984
40985      if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
40986	{
40987	  if (TREE_CODE (cmp_expr) == EQ_EXPR)
40988	    {
40989	      opcode = COND_EXPR;
40990	      rhs = TREE_OPERAND (cmp_expr, 1);
40991	    }
40992	  else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
40993	    {
40994	      opcode = (TREE_CODE (cmp_expr) == GT_EXPR
40995			? MIN_EXPR : MAX_EXPR);
40996	      rhs = rhs1;
40997	      rhs1 = TREE_OPERAND (cmp_expr, 0);
40998	    }
40999	  else
41000	    goto bad_if;
41001	}
41002      else if (TREE_CODE (cmp_expr) == EQ_EXPR)
41003	goto bad_if;
41004      else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
41005	       && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
41006	{
41007	  opcode = (TREE_CODE (cmp_expr) == GT_EXPR
41008		    ? MAX_EXPR : MIN_EXPR);
41009	  rhs = rhs1;
41010	  rhs1 = TREE_OPERAND (cmp_expr, 1);
41011	}
41012      else
41013	{
41014	bad_if:
41015	  cp_parser_error (parser,
41016			   "invalid form of %<#pragma omp atomic compare%>");
41017	  goto saw_error;
41018	}
41019
41020      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
41021	{
41022	  if (code != OMP_ATOMIC_CAPTURE_NEW
41023	      || (structured_block && r == NULL_TREE)
41024	      || TREE_CODE (cmp_expr) != EQ_EXPR)
41025	    {
41026	      eloc = cp_lexer_peek_token (parser->lexer)->location;
41027	      error_at (eloc, "unexpected %<else%>");
41028	      goto saw_error;
41029	    }
41030
41031	  cp_lexer_consume_token (parser->lexer);
41032
41033	  if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
41034	    goto saw_error;
41035
41036	  extra_scope = true;
41037	  v = cp_parser_unary_expression (parser);
41038	  if (v == error_mark_node)
41039	    goto saw_error;
41040	  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41041	    goto saw_error;
41042
41043	  tree expr = cp_parser_simple_cast_expression (parser);
41044
41045	  if (!cp_tree_equal (expr, lhs))
41046	    goto bad_if;
41047
41048	  if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41049	    goto saw_error;
41050
41051	  if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
41052	    goto saw_error;
41053
41054	  extra_scope = false;
41055	  code = OMP_ATOMIC_CAPTURE_OLD;
41056	  if (r == NULL_TREE)
41057	    /* Signal to c_finish_omp_atomic that in
41058	       if (x == e) { x = d; } else { v = x; }
41059	       case the store to v should be conditional.  */
41060	    r = void_list_node;
41061	}
41062      else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41063	{
41064	  cp_parser_error (parser, "expected %<else%>");
41065	  goto saw_error;
41066	}
41067      else if (code == OMP_ATOMIC_CAPTURE_NEW
41068	       && r != NULL_TREE
41069	       && v == NULL_TREE)
41070	code = OMP_ATOMIC;
41071      goto stmt_done;
41072    }
41073  lhs = cp_parser_unary_expression (parser);
41074  orig_lhs = lhs;
41075  switch (TREE_CODE (lhs))
41076    {
41077    case ERROR_MARK:
41078      goto saw_error;
41079
41080    case POSTINCREMENT_EXPR:
41081      if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41082	code = OMP_ATOMIC_CAPTURE_OLD;
41083      /* FALLTHROUGH */
41084    case PREINCREMENT_EXPR:
41085      lhs = TREE_OPERAND (lhs, 0);
41086      opcode = PLUS_EXPR;
41087      rhs = integer_one_node;
41088      if (compare)
41089	goto invalid_compare;
41090      break;
41091
41092    case POSTDECREMENT_EXPR:
41093      if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41094	code = OMP_ATOMIC_CAPTURE_OLD;
41095      /* FALLTHROUGH */
41096    case PREDECREMENT_EXPR:
41097      lhs = TREE_OPERAND (lhs, 0);
41098      opcode = MINUS_EXPR;
41099      rhs = integer_one_node;
41100      if (compare)
41101	goto invalid_compare;
41102      break;
41103
41104    case COMPOUND_EXPR:
41105      if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
41106	 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
41107	 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
41108	 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
41109	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
41110					     (TREE_OPERAND (lhs, 1), 0), 0)))
41111	    == BOOLEAN_TYPE)
41112       /* Undo effects of boolean_increment for post {in,de}crement.  */
41113       lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
41114      /* FALLTHRU */
41115    case MODIFY_EXPR:
41116      if (TREE_CODE (lhs) == MODIFY_EXPR
41117	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
41118	{
41119	  /* Undo effects of boolean_increment.  */
41120	  if (integer_onep (TREE_OPERAND (lhs, 1)))
41121	    {
41122	      /* This is pre or post increment.  */
41123	      rhs = TREE_OPERAND (lhs, 1);
41124	      lhs = TREE_OPERAND (lhs, 0);
41125	      opcode = NOP_EXPR;
41126	      if (code == OMP_ATOMIC_CAPTURE_NEW
41127		  && !structured_block
41128		  && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
41129		code = OMP_ATOMIC_CAPTURE_OLD;
41130	      if (compare)
41131		goto invalid_compare;
41132	      break;
41133	    }
41134	}
41135      /* FALLTHRU */
41136    default:
41137      if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
41138	{
41139	  cp_parser_error (parser, "expected %<=%>");
41140	  goto saw_error;
41141	}
41142      switch (cp_lexer_peek_token (parser->lexer)->type)
41143	{
41144	case CPP_MULT_EQ:
41145	  opcode = MULT_EXPR;
41146	  break;
41147	case CPP_DIV_EQ:
41148	  opcode = TRUNC_DIV_EXPR;
41149	  break;
41150	case CPP_PLUS_EQ:
41151	  opcode = PLUS_EXPR;
41152	  break;
41153	case CPP_MINUS_EQ:
41154	  opcode = MINUS_EXPR;
41155	  break;
41156	case CPP_LSHIFT_EQ:
41157	  opcode = LSHIFT_EXPR;
41158	  break;
41159	case CPP_RSHIFT_EQ:
41160	  opcode = RSHIFT_EXPR;
41161	  break;
41162	case CPP_AND_EQ:
41163	  opcode = BIT_AND_EXPR;
41164	  break;
41165	case CPP_OR_EQ:
41166	  opcode = BIT_IOR_EXPR;
41167	  break;
41168	case CPP_XOR_EQ:
41169	  opcode = BIT_XOR_EXPR;
41170	  break;
41171	case CPP_EQ:
41172	  enum cp_parser_prec oprec;
41173	  cp_token *token;
41174	  cp_lexer_consume_token (parser->lexer);
41175	  cp_parser_parse_tentatively (parser);
41176	  rhs1 = cp_parser_simple_cast_expression (parser);
41177	  if (rhs1 == error_mark_node)
41178	    {
41179	      cp_parser_abort_tentative_parse (parser);
41180	      cp_parser_simple_cast_expression (parser);
41181	      goto saw_error;
41182	    }
41183	  token = cp_lexer_peek_token (parser->lexer);
41184	  if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
41185	    {
41186	      cp_parser_abort_tentative_parse (parser);
41187	      cp_parser_parse_tentatively (parser);
41188	      rhs = cp_parser_binary_expression (parser, false, true,
41189						 PREC_NOT_OPERATOR, NULL);
41190	      if (rhs == error_mark_node)
41191		{
41192		  cp_parser_abort_tentative_parse (parser);
41193		  cp_parser_binary_expression (parser, false, true,
41194					       PREC_NOT_OPERATOR, NULL);
41195		  goto saw_error;
41196		}
41197	      switch (TREE_CODE (rhs))
41198		{
41199		case MULT_EXPR:
41200		case TRUNC_DIV_EXPR:
41201		case RDIV_EXPR:
41202		case PLUS_EXPR:
41203		case MINUS_EXPR:
41204		case LSHIFT_EXPR:
41205		case RSHIFT_EXPR:
41206		case BIT_AND_EXPR:
41207		case BIT_IOR_EXPR:
41208		case BIT_XOR_EXPR:
41209		  if (compare)
41210		    break;
41211		  if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
41212		    {
41213		      if (cp_parser_parse_definitely (parser))
41214			{
41215			  opcode = TREE_CODE (rhs);
41216			  rhs1 = TREE_OPERAND (rhs, 0);
41217			  rhs = TREE_OPERAND (rhs, 1);
41218			  goto stmt_done;
41219			}
41220		      else
41221			goto saw_error;
41222		    }
41223		  break;
41224		case EQ_EXPR:
41225		  if (!compare
41226		      || code != OMP_ATOMIC_CAPTURE_NEW
41227		      || !structured_block
41228		      || v
41229		      || r)
41230		    break;
41231		  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
41232		      && cp_lexer_nth_token_is_keyword (parser->lexer,
41233							2, RID_IF))
41234		    {
41235		      if (cp_parser_parse_definitely (parser))
41236			{
41237			  r = lhs;
41238			  lhs = NULL_TREE;
41239			  rhs1 = NULL_TREE;
41240			  cp_lexer_consume_token (parser->lexer);
41241			  goto restart;
41242			}
41243		    }
41244		  break;
41245		case GT_EXPR:
41246		case LT_EXPR:
41247		  if (compare
41248		      && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
41249		      && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
41250		      && cp_parser_parse_definitely (parser))
41251		    {
41252		      opcode = TREE_CODE (rhs);
41253		      rhs1 = TREE_OPERAND (rhs, 0);
41254		      rhs = TREE_OPERAND (rhs, 1);
41255		     cond_expr:
41256		      cp_lexer_consume_token (parser->lexer);
41257		      bool saved_colon_corrects_to_scope_p
41258			= parser->colon_corrects_to_scope_p;
41259		      parser->colon_corrects_to_scope_p = false;
41260		      tree e1 = cp_parser_expression (parser);
41261		      parser->colon_corrects_to_scope_p
41262			= saved_colon_corrects_to_scope_p;
41263		      cp_parser_require (parser, CPP_COLON, RT_COLON);
41264		      tree e2 = cp_parser_simple_cast_expression (parser);
41265		      if (cp_tree_equal (lhs, e2))
41266			{
41267			  if (cp_tree_equal (lhs, rhs1))
41268			    {
41269			      if (opcode == EQ_EXPR)
41270				{
41271				  opcode = COND_EXPR;
41272				  rhs1 = e1;
41273				  goto stmt_done;
41274				}
41275			      if (cp_tree_equal (rhs, e1))
41276				{
41277				  opcode
41278				    = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
41279				  rhs = e1;
41280				  goto stmt_done;
41281				}
41282			    }
41283			  else
41284			    {
41285			      gcc_assert (opcode != EQ_EXPR);
41286			      if (cp_tree_equal (rhs1, e1))
41287				{
41288				  opcode
41289				    = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
41290				  rhs1 = rhs;
41291				  rhs = e1;
41292				  goto stmt_done;
41293				}
41294			    }
41295			}
41296		      cp_parser_error (parser,
41297				       "invalid form of "
41298				       "%<#pragma omp atomic compare%>");
41299		      goto saw_error;
41300		    }
41301		  break;
41302		default:
41303		  break;
41304		}
41305	      cp_parser_abort_tentative_parse (parser);
41306	      if (structured_block
41307		  && code == OMP_ATOMIC_CAPTURE_OLD
41308		  && !compare)
41309		{
41310		  rhs = cp_parser_expression (parser);
41311		  if (rhs == error_mark_node)
41312		    goto saw_error;
41313		  opcode = NOP_EXPR;
41314		  rhs1 = NULL_TREE;
41315		  goto stmt_done;
41316		}
41317	      cp_parser_error (parser,
41318			       "invalid form of %<#pragma omp atomic%>");
41319	      goto saw_error;
41320	    }
41321	  if (!cp_parser_parse_definitely (parser))
41322	    goto saw_error;
41323	  switch (token->type)
41324	    {
41325	    case CPP_SEMICOLON:
41326	      if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
41327		{
41328		  code = OMP_ATOMIC_CAPTURE_OLD;
41329		  v = lhs;
41330		  lhs = NULL_TREE;
41331		  lhs1 = rhs1;
41332		  rhs1 = NULL_TREE;
41333		  cp_lexer_consume_token (parser->lexer);
41334		  goto restart;
41335		}
41336	      else if (structured_block && !compare)
41337		{
41338		  opcode = NOP_EXPR;
41339		  rhs = rhs1;
41340		  rhs1 = NULL_TREE;
41341		  goto stmt_done;
41342		}
41343	      cp_parser_error (parser,
41344			       "invalid form of %<#pragma omp atomic%>");
41345	      goto saw_error;
41346	    case CPP_MULT:
41347	      opcode = MULT_EXPR;
41348	      break;
41349	    case CPP_DIV:
41350	      opcode = TRUNC_DIV_EXPR;
41351	      break;
41352	    case CPP_PLUS:
41353	      opcode = PLUS_EXPR;
41354	      break;
41355	    case CPP_MINUS:
41356	      opcode = MINUS_EXPR;
41357	      break;
41358	    case CPP_LSHIFT:
41359	      opcode = LSHIFT_EXPR;
41360	      break;
41361	    case CPP_RSHIFT:
41362	      opcode = RSHIFT_EXPR;
41363	      break;
41364	    case CPP_AND:
41365	      opcode = BIT_AND_EXPR;
41366	      break;
41367	    case CPP_OR:
41368	      opcode = BIT_IOR_EXPR;
41369	      break;
41370	    case CPP_XOR:
41371	      opcode = BIT_XOR_EXPR;
41372	      break;
41373	    case CPP_EQ_EQ:
41374	      opcode = EQ_EXPR;
41375	      break;
41376	    case CPP_GREATER:
41377	      opcode = GT_EXPR;
41378	      break;
41379	    case CPP_LESS:
41380	      opcode = LT_EXPR;
41381	      break;
41382	    default:
41383	      cp_parser_error (parser,
41384			       "invalid operator for %<#pragma omp atomic%>");
41385	      goto saw_error;
41386	    }
41387	  if (compare
41388	      && TREE_CODE_CLASS (opcode) != tcc_comparison)
41389	    {
41390	      cp_parser_error (parser,
41391			       "invalid form of "
41392			       "%<#pragma omp atomic compare%>");
41393	      goto saw_error;
41394	    }
41395	  oprec = TOKEN_PRECEDENCE (token);
41396	  gcc_assert (oprec != PREC_NOT_OPERATOR);
41397	  if (commutative_tree_code (opcode))
41398	    oprec = (enum cp_parser_prec) (oprec - 1);
41399	  cp_lexer_consume_token (parser->lexer);
41400	  rhs = cp_parser_binary_expression (parser, false, false,
41401					     oprec, NULL);
41402	  if (rhs == error_mark_node)
41403	    goto saw_error;
41404	  if (compare)
41405	    {
41406	      if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
41407		{
41408		  cp_parser_error (parser,
41409				   "invalid form of "
41410				   "%<#pragma omp atomic compare%>");
41411		  goto saw_error;
41412		}
41413	      goto cond_expr;
41414	    }
41415	  goto stmt_done;
41416	default:
41417	  cp_parser_error (parser,
41418			   "invalid operator for %<#pragma omp atomic%>");
41419	  goto saw_error;
41420	}
41421      cp_lexer_consume_token (parser->lexer);
41422
41423      rhs = cp_parser_expression (parser);
41424      if (rhs == error_mark_node)
41425	goto saw_error;
41426      break;
41427    }
41428stmt_done:
41429  if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
41430    {
41431      if (!no_semicolon
41432	  && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41433	goto saw_error;
41434      no_semicolon = false;
41435      v = cp_parser_unary_expression (parser);
41436      if (v == error_mark_node)
41437	goto saw_error;
41438      if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41439	goto saw_error;
41440      lhs1 = cp_parser_unary_expression (parser);
41441      if (lhs1 == error_mark_node)
41442	goto saw_error;
41443    }
41444  if (structured_block)
41445    {
41446      if (!no_semicolon)
41447	cp_parser_consume_semicolon_at_end_of_statement (parser);
41448      cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
41449    }
41450done:
41451  if (weak && opcode != COND_EXPR)
41452    {
41453      error_at (loc, "%<weak%> clause requires atomic equality comparison");
41454      weak = false;
41455    }
41456  clauses = finish_omp_clauses (clauses, C_ORT_OMP);
41457  finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
41458		     rhs1, r, clauses, memory_order, weak);
41459  if (!structured_block && !no_semicolon)
41460    cp_parser_consume_semicolon_at_end_of_statement (parser);
41461  return;
41462
41463 invalid_compare:
41464  error ("invalid form of %<pragma omp atomic compare%>");
41465  /* FALLTHRU */
41466 saw_error:
41467  cp_parser_skip_to_end_of_block_or_statement (parser);
41468  if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41469    cp_lexer_consume_token (parser->lexer);
41470  if (structured_block)
41471    {
41472      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41473        cp_lexer_consume_token (parser->lexer);
41474      else if (code == OMP_ATOMIC_CAPTURE_NEW)
41475	{
41476	  cp_parser_skip_to_end_of_block_or_statement (parser);
41477	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41478	    cp_lexer_consume_token (parser->lexer);
41479	}
41480    }
41481}
41482
41483
41484/* OpenMP 2.5:
41485   # pragma omp barrier new-line  */
41486
41487static void
41488cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
41489{
41490  cp_parser_require_pragma_eol (parser, pragma_tok);
41491  finish_omp_barrier ();
41492}
41493
41494/* OpenMP 2.5:
41495   # pragma omp critical [(name)] new-line
41496     structured-block
41497
41498   OpenMP 4.5:
41499   # pragma omp critical [(name) [hint(expression)]] new-line
41500     structured-block  */
41501
41502#define OMP_CRITICAL_CLAUSE_MASK		\
41503	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
41504
41505static tree
41506cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41507{
41508  tree stmt, name = NULL_TREE, clauses = NULL_TREE;
41509
41510  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41511    {
41512      matching_parens parens;
41513      parens.consume_open (parser);
41514
41515      name = cp_parser_identifier (parser);
41516
41517      if (name == error_mark_node
41518	  || !parens.require_close (parser))
41519	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41520					       /*or_comma=*/false,
41521					       /*consume_paren=*/true);
41522      if (name == error_mark_node)
41523	name = NULL;
41524
41525      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41526	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41527	cp_lexer_consume_token (parser->lexer);
41528    }
41529
41530  clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
41531				       "#pragma omp critical", pragma_tok);
41532
41533  stmt = cp_parser_omp_structured_block (parser, if_p);
41534  return c_finish_omp_critical (input_location, stmt, name, clauses);
41535}
41536
41537/* OpenMP 5.0:
41538   # pragma omp depobj ( depobj ) depobj-clause new-line
41539
41540   depobj-clause:
41541     depend (dependence-type : locator)
41542     destroy
41543     update (dependence-type)
41544
41545   dependence-type:
41546     in
41547     out
41548     inout
41549     mutexinout  */
41550
41551static void
41552cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
41553{
41554  location_t loc = pragma_tok->location;
41555  matching_parens parens;
41556  if (!parens.require_open (parser))
41557    {
41558      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41559      return;
41560    }
41561
41562  tree depobj = cp_parser_assignment_expression (parser);
41563
41564  if (!parens.require_close (parser))
41565    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41566					   /*or_comma=*/false,
41567					   /*consume_paren=*/true);
41568
41569  tree clause = NULL_TREE;
41570  enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
41571  location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
41572  /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
41573  if (parser->lexer->in_omp_attribute_pragma
41574      && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41575    cp_lexer_consume_token (parser->lexer);
41576  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41577    {
41578      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41579      const char *p = IDENTIFIER_POINTER (id);
41580
41581      cp_lexer_consume_token (parser->lexer);
41582      if (!strcmp ("depend", p))
41583	{
41584	  /* Don't create location wrapper nodes within the depend clause.  */
41585	  auto_suppress_location_wrappers sentinel;
41586	  clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
41587	  if (clause)
41588	    clause = finish_omp_clauses (clause, C_ORT_OMP);
41589	  if (!clause)
41590	    clause = error_mark_node;
41591	}
41592      else if (!strcmp ("destroy", p))
41593	kind = OMP_CLAUSE_DEPEND_LAST;
41594      else if (!strcmp ("update", p))
41595	{
41596	  matching_parens c_parens;
41597	  if (c_parens.require_open (parser))
41598	    {
41599	      location_t c2_loc
41600		= cp_lexer_peek_token (parser->lexer)->location;
41601	      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41602		{
41603		  tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
41604		  const char *p2 = IDENTIFIER_POINTER (id2);
41605
41606		  cp_lexer_consume_token (parser->lexer);
41607		  if (!strcmp ("in", p2))
41608		    kind = OMP_CLAUSE_DEPEND_IN;
41609		  else if (!strcmp ("out", p2))
41610		    kind = OMP_CLAUSE_DEPEND_OUT;
41611		  else if (!strcmp ("inout", p2))
41612		    kind = OMP_CLAUSE_DEPEND_INOUT;
41613		  else if (!strcmp ("mutexinoutset", p2))
41614		    kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41615		}
41616	      if (kind == OMP_CLAUSE_DEPEND_SOURCE)
41617		{
41618		  clause = error_mark_node;
41619		  error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
41620				    "%<mutexinoutset%>");
41621		}
41622	      if (!c_parens.require_close (parser))
41623		cp_parser_skip_to_closing_parenthesis (parser,
41624						       /*recovering=*/true,
41625						       /*or_comma=*/false,
41626						       /*consume_paren=*/true);
41627	    }
41628	  else
41629	    clause = error_mark_node;
41630	}
41631    }
41632  if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
41633    {
41634      clause = error_mark_node;
41635      error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
41636    }
41637  cp_parser_require_pragma_eol (parser, pragma_tok);
41638
41639  finish_omp_depobj (loc, depobj, kind, clause);
41640}
41641
41642
41643/* OpenMP 2.5:
41644   # pragma omp flush flush-vars[opt] new-line
41645
41646   flush-vars:
41647     ( variable-list )
41648
41649   OpenMP 5.0:
41650   # pragma omp flush memory-order-clause new-line  */
41651
41652static void
41653cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
41654{
41655  enum memmodel mo = MEMMODEL_LAST;
41656  /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
41657  if (parser->lexer->in_omp_attribute_pragma
41658      && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41659      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41660    cp_lexer_consume_token (parser->lexer);
41661  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41662    {
41663      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41664      const char *p = IDENTIFIER_POINTER (id);
41665      if (!strcmp (p, "seq_cst"))
41666	mo = MEMMODEL_SEQ_CST;
41667      else if (!strcmp (p, "acq_rel"))
41668	mo = MEMMODEL_ACQ_REL;
41669      else if (!strcmp (p, "release"))
41670	mo = MEMMODEL_RELEASE;
41671      else if (!strcmp (p, "acquire"))
41672	mo = MEMMODEL_ACQUIRE;
41673      else
41674	error_at (cp_lexer_peek_token (parser->lexer)->location,
41675		  "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
41676		  "%<acquire%>");
41677      cp_lexer_consume_token (parser->lexer);
41678    }
41679  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41680    {
41681      if (mo != MEMMODEL_LAST)
41682	error_at (cp_lexer_peek_token (parser->lexer)->location,
41683		  "%<flush%> list specified together with memory order "
41684		  "clause");
41685      (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
41686    }
41687  cp_parser_require_pragma_eol (parser, pragma_tok);
41688
41689  finish_omp_flush (mo);
41690}
41691
41692/* Helper function, to parse omp for increment expression.  */
41693
41694static tree
41695cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
41696{
41697  tree cond = cp_parser_binary_expression (parser, false, true,
41698					   PREC_NOT_OPERATOR, NULL);
41699  if (cond == error_mark_node
41700      || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
41701    {
41702      cp_parser_skip_to_end_of_statement (parser);
41703      return error_mark_node;
41704    }
41705
41706  switch (TREE_CODE (cond))
41707    {
41708    case GT_EXPR:
41709    case GE_EXPR:
41710    case LT_EXPR:
41711    case LE_EXPR:
41712      break;
41713    case NE_EXPR:
41714      if (code != OACC_LOOP)
41715	break;
41716      gcc_fallthrough ();
41717    default:
41718      return error_mark_node;
41719    }
41720
41721  /* If decl is an iterator, preserve LHS and RHS of the relational
41722     expr until finish_omp_for.  */
41723  if (decl
41724      && (type_dependent_expression_p (decl)
41725	  || CLASS_TYPE_P (TREE_TYPE (decl))))
41726    return cond;
41727
41728  return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
41729			    TREE_CODE (cond),
41730			    TREE_OPERAND (cond, 0), ERROR_MARK,
41731			    TREE_OPERAND (cond, 1), ERROR_MARK,
41732			    NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
41733}
41734
41735/* Helper function, to parse omp for increment expression.  */
41736
41737static tree
41738cp_parser_omp_for_incr (cp_parser *parser, tree decl)
41739{
41740  cp_token *token = cp_lexer_peek_token (parser->lexer);
41741  enum tree_code op;
41742  tree lhs, rhs;
41743  cp_id_kind idk;
41744  bool decl_first;
41745
41746  if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41747    {
41748      op = (token->type == CPP_PLUS_PLUS
41749	    ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
41750      cp_lexer_consume_token (parser->lexer);
41751      lhs = cp_parser_simple_cast_expression (parser);
41752      if (lhs != decl
41753	  && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41754	return error_mark_node;
41755      return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41756    }
41757
41758  lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
41759  if (lhs != decl
41760      && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41761    return error_mark_node;
41762
41763  token = cp_lexer_peek_token (parser->lexer);
41764  if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41765    {
41766      op = (token->type == CPP_PLUS_PLUS
41767	    ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
41768      cp_lexer_consume_token (parser->lexer);
41769      return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41770    }
41771
41772  op = cp_parser_assignment_operator_opt (parser);
41773  if (op == ERROR_MARK)
41774    return error_mark_node;
41775
41776  if (op != NOP_EXPR)
41777    {
41778      rhs = cp_parser_assignment_expression (parser);
41779      rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
41780      return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41781    }
41782
41783  lhs = cp_parser_binary_expression (parser, false, false,
41784				     PREC_ADDITIVE_EXPRESSION, NULL);
41785  token = cp_lexer_peek_token (parser->lexer);
41786  decl_first = (lhs == decl
41787		|| (processing_template_decl && cp_tree_equal (lhs, decl)));
41788  if (decl_first)
41789    lhs = NULL_TREE;
41790  if (token->type != CPP_PLUS
41791      && token->type != CPP_MINUS)
41792    return error_mark_node;
41793
41794  do
41795    {
41796      op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
41797      cp_lexer_consume_token (parser->lexer);
41798      rhs = cp_parser_binary_expression (parser, false, false,
41799					 PREC_ADDITIVE_EXPRESSION, NULL);
41800      token = cp_lexer_peek_token (parser->lexer);
41801      if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
41802	{
41803	  if (lhs == NULL_TREE)
41804	    {
41805	      if (op == PLUS_EXPR)
41806		lhs = rhs;
41807	      else
41808		lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
41809					NULL_TREE, tf_warning_or_error);
41810	    }
41811	  else
41812	    lhs = build_x_binary_op (input_location, op,
41813				     lhs, ERROR_MARK,
41814				     rhs, ERROR_MARK,
41815				     NULL_TREE, NULL, tf_warning_or_error);
41816	}
41817    }
41818  while (token->type == CPP_PLUS || token->type == CPP_MINUS);
41819
41820  if (!decl_first)
41821    {
41822      if ((rhs != decl
41823	   && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
41824	  || op == MINUS_EXPR)
41825	return error_mark_node;
41826      rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
41827    }
41828  else
41829    rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
41830
41831  return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41832}
41833
41834/* Parse the initialization statement of an OpenMP for loop.
41835
41836   Return true if the resulting construct should have an
41837   OMP_CLAUSE_PRIVATE added to it.  */
41838
41839static tree
41840cp_parser_omp_for_loop_init (cp_parser *parser,
41841			     tree &this_pre_body,
41842			     releasing_vec &for_block,
41843			     tree &init,
41844			     tree &orig_init,
41845			     tree &decl,
41846			     tree &real_decl)
41847{
41848  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
41849    return NULL_TREE;
41850
41851  tree add_private_clause = NULL_TREE;
41852
41853  /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
41854
41855     init-expr:
41856     var = lb
41857     integer-type var = lb
41858     random-access-iterator-type var = lb
41859     pointer-type var = lb
41860  */
41861  cp_decl_specifier_seq type_specifiers;
41862
41863  /* First, try to parse as an initialized declaration.  See
41864     cp_parser_condition, from whence the bulk of this is copied.  */
41865
41866  cp_parser_parse_tentatively (parser);
41867  cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
41868				/*is_declaration=*/true,
41869				/*is_trailing_return=*/false,
41870				&type_specifiers);
41871  if (cp_parser_parse_definitely (parser))
41872    {
41873      /* If parsing a type specifier seq succeeded, then this
41874	 MUST be a initialized declaration.  */
41875      tree asm_specification, attributes;
41876      cp_declarator *declarator;
41877
41878      declarator = cp_parser_declarator (parser,
41879					 CP_PARSER_DECLARATOR_NAMED,
41880					 CP_PARSER_FLAGS_NONE,
41881					 /*ctor_dtor_or_conv_p=*/NULL,
41882					 /*parenthesized_p=*/NULL,
41883					 /*member_p=*/false,
41884					 /*friend_p=*/false,
41885					 /*static_p=*/false);
41886      attributes = cp_parser_attributes_opt (parser);
41887      asm_specification = cp_parser_asm_specification_opt (parser);
41888
41889      if (declarator == cp_error_declarator)
41890	cp_parser_skip_to_end_of_statement (parser);
41891
41892      else
41893	{
41894	  tree pushed_scope, auto_node;
41895
41896	  decl = start_decl (declarator, &type_specifiers,
41897			     SD_INITIALIZED, attributes,
41898			     /*prefix_attributes=*/NULL_TREE,
41899			     &pushed_scope);
41900
41901	  auto_node = type_uses_auto (TREE_TYPE (decl));
41902	  if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
41903	    {
41904	      if (cp_lexer_next_token_is (parser->lexer,
41905					  CPP_OPEN_PAREN))
41906	        error ("parenthesized initialization is not allowed in "
41907		       "OpenMP %<for%> loop");
41908	      else
41909		/* Trigger an error.  */
41910		cp_parser_require (parser, CPP_EQ, RT_EQ);
41911
41912	      init = error_mark_node;
41913	      cp_parser_skip_to_end_of_statement (parser);
41914	    }
41915	  else if (CLASS_TYPE_P (TREE_TYPE (decl))
41916		   || type_dependent_expression_p (decl)
41917		   || auto_node)
41918	    {
41919	      bool is_direct_init, is_non_constant_init;
41920
41921	      init = cp_parser_initializer (parser,
41922					    &is_direct_init,
41923					    &is_non_constant_init);
41924
41925	      if (auto_node)
41926		{
41927		  TREE_TYPE (decl)
41928		    = do_auto_deduction (TREE_TYPE (decl), init,
41929					 auto_node);
41930
41931		  if (!CLASS_TYPE_P (TREE_TYPE (decl))
41932		      && !type_dependent_expression_p (decl))
41933		    goto non_class;
41934		}
41935
41936	      cp_finish_decl (decl, init, !is_non_constant_init,
41937			      asm_specification,
41938			      LOOKUP_ONLYCONVERTING);
41939	      orig_init = init;
41940	      if (CLASS_TYPE_P (TREE_TYPE (decl)))
41941		{
41942		  vec_safe_push (for_block, this_pre_body);
41943		  init = NULL_TREE;
41944		}
41945	      else
41946		{
41947		  init = pop_stmt_list (this_pre_body);
41948		  if (init && TREE_CODE (init) == STATEMENT_LIST)
41949		    {
41950		      tree_stmt_iterator i = tsi_start (init);
41951		      /* Move lambda DECL_EXPRs to FOR_BLOCK.  */
41952		      while (!tsi_end_p (i))
41953			{
41954			  tree t = tsi_stmt (i);
41955			  if (TREE_CODE (t) == DECL_EXPR
41956			      && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
41957			    {
41958			      tsi_delink (&i);
41959			      vec_safe_push (for_block, t);
41960			      continue;
41961			    }
41962			  break;
41963			}
41964		      if (tsi_one_before_end_p (i))
41965			{
41966			  tree t = tsi_stmt (i);
41967			  tsi_delink (&i);
41968			  free_stmt_list (init);
41969			  init = t;
41970			}
41971		    }
41972		}
41973	      this_pre_body = NULL_TREE;
41974	    }
41975	  else
41976	    {
41977	      /* Consume '='.  */
41978	      cp_lexer_consume_token (parser->lexer);
41979	      init = cp_parser_assignment_expression (parser);
41980
41981	    non_class:
41982	      if (TYPE_REF_P (TREE_TYPE (decl)))
41983		init = error_mark_node;
41984	      else
41985		cp_finish_decl (decl, NULL_TREE,
41986				/*init_const_expr_p=*/false,
41987				asm_specification,
41988				LOOKUP_ONLYCONVERTING);
41989	    }
41990
41991	  if (pushed_scope)
41992	    pop_scope (pushed_scope);
41993	}
41994    }
41995  else
41996    {
41997      cp_id_kind idk;
41998      /* If parsing a type specifier sequence failed, then
41999	 this MUST be a simple expression.  */
42000      cp_parser_parse_tentatively (parser);
42001      decl = cp_parser_primary_expression (parser, false, false,
42002					   false, &idk);
42003      cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
42004      if (!cp_parser_error_occurred (parser)
42005	  && decl
42006	  && (TREE_CODE (decl) == COMPONENT_REF
42007	      || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
42008	{
42009	  cp_parser_abort_tentative_parse (parser);
42010	  cp_parser_parse_tentatively (parser);
42011	  cp_token *token = cp_lexer_peek_token (parser->lexer);
42012	  tree name = cp_parser_id_expression (parser, /*template_p=*/false,
42013					       /*check_dependency_p=*/true,
42014					       /*template_p=*/NULL,
42015					       /*declarator_p=*/false,
42016					       /*optional_p=*/false);
42017	  if (name != error_mark_node
42018	      && last_tok == cp_lexer_peek_token (parser->lexer))
42019	    {
42020	      decl = cp_parser_lookup_name_simple (parser, name,
42021						   token->location);
42022	      if (TREE_CODE (decl) == FIELD_DECL)
42023		add_private_clause = omp_privatize_field (decl, false);
42024	    }
42025	  cp_parser_abort_tentative_parse (parser);
42026	  cp_parser_parse_tentatively (parser);
42027	  decl = cp_parser_primary_expression (parser, false, false,
42028					       false, &idk);
42029	}
42030      if (!cp_parser_error_occurred (parser)
42031	  && decl
42032	  && DECL_P (decl)
42033	  && CLASS_TYPE_P (TREE_TYPE (decl)))
42034	{
42035	  tree rhs;
42036
42037	  cp_parser_parse_definitely (parser);
42038	  cp_parser_require (parser, CPP_EQ, RT_EQ);
42039	  rhs = cp_parser_assignment_expression (parser);
42040	  orig_init = rhs;
42041	  finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
42042						 decl, NOP_EXPR,
42043						 rhs, NULL_TREE,
42044						 tf_warning_or_error));
42045	  if (!add_private_clause)
42046	    add_private_clause = decl;
42047	}
42048      else
42049	{
42050	  decl = NULL;
42051	  cp_parser_abort_tentative_parse (parser);
42052	  init = cp_parser_expression (parser);
42053	  if (init)
42054	    {
42055	      if (TREE_CODE (init) == MODIFY_EXPR
42056		  || TREE_CODE (init) == MODOP_EXPR)
42057		real_decl = TREE_OPERAND (init, 0);
42058	    }
42059	}
42060    }
42061  return add_private_clause;
42062}
42063
42064/* Helper for cp_parser_omp_for_loop, handle one range-for loop.  */
42065
42066void
42067cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
42068			  tree &decl, tree &orig_decl, tree &init,
42069			  tree &orig_init, tree &cond, tree &incr)
42070{
42071  tree begin, end, range_temp_decl = NULL_TREE;
42072  tree iter_type, begin_expr, end_expr;
42073
42074  if (processing_template_decl)
42075    {
42076      if (check_for_bare_parameter_packs (init))
42077	init = error_mark_node;
42078      if (!type_dependent_expression_p (init)
42079	  /* do_auto_deduction doesn't mess with template init-lists.  */
42080	  && !BRACE_ENCLOSED_INITIALIZER_P (init))
42081	{
42082	  tree d = decl;
42083	  if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
42084	    {
42085	      tree v = DECL_VALUE_EXPR (decl);
42086	      if (TREE_CODE (v) == ARRAY_REF
42087		  && VAR_P (TREE_OPERAND (v, 0))
42088		  && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
42089		d = TREE_OPERAND (v, 0);
42090	    }
42091	  do_range_for_auto_deduction (d, init);
42092	}
42093      cond = global_namespace;
42094      incr = NULL_TREE;
42095      orig_init = init;
42096      if (this_pre_body)
42097	this_pre_body = pop_stmt_list (this_pre_body);
42098      return;
42099    }
42100
42101  init = mark_lvalue_use (init);
42102
42103  if (decl == error_mark_node || init == error_mark_node)
42104    /* If an error happened previously do nothing or else a lot of
42105       unhelpful errors would be issued.  */
42106    begin_expr = end_expr = iter_type = error_mark_node;
42107  else
42108    {
42109      tree range_temp;
42110
42111      if (VAR_P (init)
42112	  && array_of_runtime_bound_p (TREE_TYPE (init)))
42113	/* Can't bind a reference to an array of runtime bound.  */
42114	range_temp = init;
42115      else
42116	{
42117	  range_temp = build_range_temp (init);
42118	  DECL_NAME (range_temp) = NULL_TREE;
42119	  pushdecl (range_temp);
42120	  cp_finish_decl (range_temp, init,
42121			  /*is_constant_init*/false, NULL_TREE,
42122			  LOOKUP_ONLYCONVERTING);
42123	  range_temp_decl = range_temp;
42124	  range_temp = convert_from_reference (range_temp);
42125	}
42126      iter_type = cp_parser_perform_range_for_lookup (range_temp,
42127						      &begin_expr, &end_expr);
42128    }
42129
42130  tree end_iter_type = iter_type;
42131  if (cxx_dialect >= cxx17)
42132    end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
42133  end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
42134  TREE_USED (end) = 1;
42135  DECL_ARTIFICIAL (end) = 1;
42136  pushdecl (end);
42137  cp_finish_decl (end, end_expr,
42138		  /*is_constant_init*/false, NULL_TREE,
42139		  LOOKUP_ONLYCONVERTING);
42140
42141  /* The new for initialization statement.  */
42142  begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
42143  TREE_USED (begin) = 1;
42144  DECL_ARTIFICIAL (begin) = 1;
42145  pushdecl (begin);
42146  orig_init = init;
42147  if (CLASS_TYPE_P (iter_type))
42148    init = NULL_TREE;
42149  else
42150    {
42151      init = begin_expr;
42152      begin_expr = NULL_TREE;
42153    }
42154  cp_finish_decl (begin, begin_expr,
42155		  /*is_constant_init*/false, NULL_TREE,
42156		  LOOKUP_ONLYCONVERTING);
42157
42158  /* The new for condition.  */
42159  if (CLASS_TYPE_P (iter_type))
42160    cond = build2 (NE_EXPR, boolean_type_node, begin, end);
42161  else
42162    cond = build_x_binary_op (input_location, NE_EXPR,
42163			      begin, ERROR_MARK,
42164			      end, ERROR_MARK,
42165			      NULL_TREE, NULL, tf_warning_or_error);
42166
42167  /* The new increment expression.  */
42168  if (CLASS_TYPE_P (iter_type))
42169    incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
42170  else
42171    incr = finish_unary_op_expr (input_location,
42172				 PREINCREMENT_EXPR, begin,
42173				 tf_warning_or_error);
42174
42175  orig_decl = decl;
42176  decl = begin;
42177  if (for_block)
42178    {
42179      vec_safe_push (for_block, this_pre_body);
42180      this_pre_body = NULL_TREE;
42181    }
42182
42183  tree decomp_first_name = NULL_TREE;
42184  unsigned decomp_cnt = 0;
42185  if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
42186    {
42187      tree v = DECL_VALUE_EXPR (orig_decl);
42188      if (TREE_CODE (v) == ARRAY_REF
42189	  && VAR_P (TREE_OPERAND (v, 0))
42190	  && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
42191	{
42192	  tree d = orig_decl;
42193	  orig_decl = TREE_OPERAND (v, 0);
42194	  decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
42195	  decomp_first_name = d;
42196	}
42197    }
42198
42199  tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
42200  if (auto_node)
42201    {
42202      tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42203				     NULL_TREE, tf_none);
42204      if (!error_operand_p (t))
42205	TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
42206						   t, auto_node);
42207    }
42208
42209  tree v = make_tree_vec (decomp_cnt + 3);
42210  TREE_VEC_ELT (v, 0) = range_temp_decl;
42211  TREE_VEC_ELT (v, 1) = end;
42212  TREE_VEC_ELT (v, 2) = orig_decl;
42213  for (unsigned i = 0; i < decomp_cnt; i++)
42214    {
42215      TREE_VEC_ELT (v, i + 3) = decomp_first_name;
42216      decomp_first_name = DECL_CHAIN (decomp_first_name);
42217    }
42218  orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
42219}
42220
42221/* Helper for cp_parser_omp_for_loop, finalize part of range for
42222   inside of the collapsed body.  */
42223
42224void
42225cp_finish_omp_range_for (tree orig, tree begin)
42226{
42227  gcc_assert (TREE_CODE (orig) == TREE_LIST
42228	      && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
42229  tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
42230  tree decomp_first_name = NULL_TREE;
42231  unsigned int decomp_cnt = 0;
42232
42233  if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42234    {
42235      decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
42236      decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
42237      cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
42238    }
42239
42240  /* The declaration is initialized with *__begin inside the loop body.  */
42241  cp_finish_decl (decl,
42242		  build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42243					NULL_TREE, tf_warning_or_error),
42244		  /*is_constant_init*/false, NULL_TREE,
42245		  LOOKUP_ONLYCONVERTING);
42246  if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42247    cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
42248}
42249
42250/* Return true if next tokens contain a standard attribute that contains
42251   omp::directive (DIRECTIVE).  */
42252
42253static bool
42254cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
42255			    bool tentative)
42256{
42257  size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
42258  if (n < 10)
42259    return false;
42260  for (i = 5; i < n - 4; i++)
42261    if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
42262	&& cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
42263	&& cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
42264      {
42265	tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
42266	tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
42267	if (strcmp (IDENTIFIER_POINTER (first), "directive"))
42268	  continue;
42269	if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
42270	  break;
42271      }
42272  if (i == n - 4)
42273    return false;
42274  cp_parser_parse_tentatively (parser);
42275  location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
42276  location_t last_loc
42277    = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
42278  location_t middle_loc = UNKNOWN_LOCATION;
42279  tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
42280  int cnt = 0;
42281  bool seen = false;
42282  for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
42283    if (get_attribute_namespace (attr) == omp_identifier
42284	&& is_attribute_p ("directive", get_attribute_name (attr)))
42285      {
42286	for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
42287	  {
42288	    tree d = TREE_VALUE (a);
42289	    gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
42290	    cp_token *first = DEFPARSE_TOKENS (d)->first;
42291	    cnt++;
42292	    if (first->type == CPP_NAME
42293		&& strcmp (IDENTIFIER_POINTER (first->u.value),
42294			   directive) == 0)
42295	      {
42296		seen = true;
42297		if (middle_loc == UNKNOWN_LOCATION)
42298		  middle_loc = first->location;
42299	      }
42300	  }
42301      }
42302  if (!seen || tentative)
42303    {
42304      cp_parser_abort_tentative_parse (parser);
42305      return seen;
42306    }
42307  if (cnt != 1 || TREE_CHAIN (std_attrs))
42308    {
42309      error_at (make_location (first_loc, last_loc, middle_loc),
42310		"%<[[omp::directive(%s)]]%> must be the only specified "
42311		"attribute on a statement", directive);
42312      cp_parser_abort_tentative_parse (parser);
42313      return false;
42314    }
42315  if (!cp_parser_parse_definitely (parser))
42316    return false;
42317  cp_parser_handle_statement_omp_attributes (parser, std_attrs);
42318  return true;
42319}
42320
42321/* Parse an OpenMP structured block sequence.  KIND is the corresponding
42322   separating directive.  */
42323
42324static tree
42325cp_parser_omp_structured_block_sequence (cp_parser *parser,
42326					 enum pragma_kind kind)
42327{
42328  tree stmt = begin_omp_structured_block ();
42329  unsigned int save = cp_parser_begin_omp_structured_block (parser);
42330
42331  cp_parser_statement (parser, NULL_TREE, false, NULL);
42332  while (true)
42333    {
42334      cp_token *token = cp_lexer_peek_token (parser->lexer);
42335
42336      if (token->type == CPP_CLOSE_BRACE
42337	  || token->type == CPP_EOF
42338	  || token->type == CPP_PRAGMA_EOL
42339	  || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
42340	  || (kind != PRAGMA_NONE
42341	      && cp_parser_pragma_kind (token) == kind))
42342	break;
42343
42344      if (kind != PRAGMA_NONE
42345	  && cp_parser_omp_section_scan (parser,
42346					 kind == PRAGMA_OMP_SCAN
42347					 ? "scan" : "section", false))
42348	break;
42349
42350      cp_parser_statement (parser, NULL_TREE, false, NULL);
42351    }
42352
42353  cp_parser_end_omp_structured_block (parser, save);
42354  return finish_omp_structured_block (stmt);
42355}
42356
42357
42358/* OpenMP 5.0:
42359
42360   scan-loop-body:
42361     { structured-block scan-directive structured-block }  */
42362
42363static void
42364cp_parser_omp_scan_loop_body (cp_parser *parser)
42365{
42366  tree substmt, clauses = NULL_TREE;
42367
42368  matching_braces braces;
42369  if (!braces.require_open (parser))
42370    return;
42371
42372  substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
42373  substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
42374  add_stmt (substmt);
42375
42376  cp_token *tok = cp_lexer_peek_token (parser->lexer);
42377  if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
42378    {
42379      enum omp_clause_code clause = OMP_CLAUSE_ERROR;
42380
42381      cp_lexer_consume_token (parser->lexer);
42382
42383      if (parser->lexer->in_omp_attribute_pragma
42384	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42385	cp_lexer_consume_token (parser->lexer);
42386
42387      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42388	{
42389	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42390	  const char *p = IDENTIFIER_POINTER (id);
42391	  if (strcmp (p, "inclusive") == 0)
42392	    clause = OMP_CLAUSE_INCLUSIVE;
42393	  else if (strcmp (p, "exclusive") == 0)
42394	    clause = OMP_CLAUSE_EXCLUSIVE;
42395	}
42396      if (clause != OMP_CLAUSE_ERROR)
42397	{
42398	  cp_lexer_consume_token (parser->lexer);
42399	  clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
42400	}
42401      else
42402	cp_parser_error (parser, "expected %<inclusive%> or "
42403				 "%<exclusive%> clause");
42404
42405      cp_parser_require_pragma_eol (parser, tok);
42406    }
42407  else
42408    error ("expected %<#pragma omp scan%>");
42409
42410  clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42411  substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
42412  substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
42413			clauses);
42414  add_stmt (substmt);
42415
42416  braces.require_close (parser);
42417}
42418
42419/* Parse the restricted form of the for statement allowed by OpenMP.  */
42420
42421static tree
42422cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
42423			tree *cclauses, bool *if_p)
42424{
42425  tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
42426  tree orig_decl;
42427  tree real_decl, initv, condv, incrv, declv, orig_declv;
42428  tree this_pre_body, cl, ordered_cl = NULL_TREE;
42429  location_t loc_first;
42430  bool collapse_err = false;
42431  int i, collapse = 1, ordered = 0, count, nbraces = 0;
42432  releasing_vec for_block;
42433  auto_vec<tree, 4> orig_inits;
42434  bool tiling = false;
42435  bool inscan = false;
42436
42437  for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
42438    if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
42439      collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
42440    else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
42441      {
42442	tiling = true;
42443	collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
42444      }
42445    else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
42446	     && OMP_CLAUSE_ORDERED_EXPR (cl))
42447      {
42448	ordered_cl = cl;
42449	ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
42450      }
42451    else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
42452	     && OMP_CLAUSE_REDUCTION_INSCAN (cl)
42453	     && (code == OMP_SIMD || code == OMP_FOR))
42454      inscan = true;
42455
42456  if (ordered && ordered < collapse)
42457    {
42458      error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42459		"%<ordered%> clause parameter is less than %<collapse%>");
42460      OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
42461	= build_int_cst (NULL_TREE, collapse);
42462      ordered = collapse;
42463    }
42464  if (ordered)
42465    {
42466      for (tree *pc = &clauses; *pc; )
42467	if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
42468	  {
42469	    error_at (OMP_CLAUSE_LOCATION (*pc),
42470		      "%<linear%> clause may not be specified together "
42471		      "with %<ordered%> clause with a parameter");
42472	    *pc = OMP_CLAUSE_CHAIN (*pc);
42473	  }
42474	else
42475	  pc = &OMP_CLAUSE_CHAIN (*pc);
42476    }
42477
42478  gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
42479  count = ordered ? ordered : collapse;
42480
42481  declv = make_tree_vec (count);
42482  initv = make_tree_vec (count);
42483  condv = make_tree_vec (count);
42484  incrv = make_tree_vec (count);
42485  orig_declv = NULL_TREE;
42486
42487  loc_first = cp_lexer_peek_token (parser->lexer)->location;
42488
42489  for (i = 0; i < count; i++)
42490    {
42491      int bracecount = 0;
42492      tree add_private_clause = NULL_TREE;
42493      location_t loc;
42494
42495      if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42496	{
42497	  if (!collapse_err)
42498	    cp_parser_error (parser, "for statement expected");
42499	  return NULL;
42500	}
42501      loc = cp_lexer_consume_token (parser->lexer)->location;
42502
42503      /* Don't create location wrapper nodes within an OpenMP "for"
42504	 statement.  */
42505      auto_suppress_location_wrappers sentinel;
42506
42507      matching_parens parens;
42508      if (!parens.require_open (parser))
42509	return NULL;
42510
42511      init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
42512      this_pre_body = push_stmt_list ();
42513
42514      if (code != OACC_LOOP && cxx_dialect >= cxx11)
42515	{
42516	  /* Save tokens so that we can put them back.  */
42517	  cp_lexer_save_tokens (parser->lexer);
42518
42519	  /* Look for ':' that is not nested in () or {}.  */
42520	  bool is_range_for
42521	    = (cp_parser_skip_to_closing_parenthesis_1 (parser,
42522							/*recovering=*/false,
42523							CPP_COLON,
42524							/*consume_paren=*/
42525							false) == -1);
42526
42527	  /* Roll back the tokens we skipped.  */
42528	  cp_lexer_rollback_tokens (parser->lexer);
42529
42530	  if (is_range_for)
42531	    {
42532	      bool saved_colon_corrects_to_scope_p
42533		= parser->colon_corrects_to_scope_p;
42534
42535	      /* A colon is used in range-based for.  */
42536	      parser->colon_corrects_to_scope_p = false;
42537
42538	      /* Parse the declaration.  */
42539	      cp_parser_simple_declaration (parser,
42540					    /*function_definition_allowed_p=*/
42541					    false, &decl);
42542	      parser->colon_corrects_to_scope_p
42543		= saved_colon_corrects_to_scope_p;
42544
42545	      cp_parser_require (parser, CPP_COLON, RT_COLON);
42546
42547	      init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
42548					  false, 0, true);
42549
42550	      cp_convert_omp_range_for (this_pre_body, for_block, decl,
42551					orig_decl, init, orig_init,
42552					cond, incr);
42553	      if (this_pre_body)
42554		{
42555		  if (pre_body)
42556		    {
42557		      tree t = pre_body;
42558		      pre_body = push_stmt_list ();
42559		      add_stmt (t);
42560		      add_stmt (this_pre_body);
42561		      pre_body = pop_stmt_list (pre_body);
42562		    }
42563		  else
42564		    pre_body = this_pre_body;
42565		}
42566
42567	      if (ordered_cl)
42568		error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42569			  "%<ordered%> clause with parameter on "
42570			  "range-based %<for%> loop");
42571
42572	      goto parse_close_paren;
42573	    }
42574	}
42575
42576      add_private_clause
42577	= cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
42578				       init, orig_init, decl, real_decl);
42579
42580      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42581      if (this_pre_body)
42582	{
42583	  this_pre_body = pop_stmt_list (this_pre_body);
42584	  if (pre_body)
42585	    {
42586	      tree t = pre_body;
42587	      pre_body = push_stmt_list ();
42588	      add_stmt (t);
42589	      add_stmt (this_pre_body);
42590	      pre_body = pop_stmt_list (pre_body);
42591	    }
42592	  else
42593	    pre_body = this_pre_body;
42594	}
42595
42596      if (decl)
42597	real_decl = decl;
42598      if (cclauses != NULL
42599	  && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
42600	  && real_decl != NULL_TREE
42601	  && code != OMP_LOOP)
42602	{
42603	  tree *c;
42604	  for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
42605	    if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
42606		&& OMP_CLAUSE_DECL (*c) == real_decl)
42607	      {
42608		error_at (loc, "iteration variable %qD"
42609			  " should not be firstprivate", real_decl);
42610		*c = OMP_CLAUSE_CHAIN (*c);
42611	      }
42612	    else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
42613		     && OMP_CLAUSE_DECL (*c) == real_decl)
42614	      {
42615		/* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
42616		tree l = *c;
42617		*c = OMP_CLAUSE_CHAIN (*c);
42618		if (code == OMP_SIMD)
42619		  {
42620		    OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
42621		    cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
42622		  }
42623		else
42624		  {
42625		    OMP_CLAUSE_CHAIN (l) = clauses;
42626		    clauses = l;
42627		  }
42628		add_private_clause = NULL_TREE;
42629	      }
42630	    else
42631	      {
42632		if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
42633		    && OMP_CLAUSE_DECL (*c) == real_decl)
42634		  add_private_clause = NULL_TREE;
42635		c = &OMP_CLAUSE_CHAIN (*c);
42636	      }
42637	}
42638
42639      if (add_private_clause)
42640	{
42641	  tree c;
42642	  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
42643	    {
42644	      if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
42645		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
42646		  && OMP_CLAUSE_DECL (c) == decl)
42647		break;
42648	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
42649		       && OMP_CLAUSE_DECL (c) == decl)
42650		error_at (loc, "iteration variable %qD "
42651			  "should not be firstprivate",
42652			  decl);
42653	      else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
42654			|| OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
42655		       && OMP_CLAUSE_DECL (c) == decl)
42656		error_at (loc, "iteration variable %qD should not be reduction",
42657			  decl);
42658	    }
42659	  if (c == NULL)
42660	    {
42661	      if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
42662		c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
42663	      else if (code != OMP_SIMD)
42664		c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
42665	      else
42666		c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
42667	      OMP_CLAUSE_DECL (c) = add_private_clause;
42668	      c = finish_omp_clauses (c, C_ORT_OMP);
42669	      if (c)
42670		{
42671		  OMP_CLAUSE_CHAIN (c) = clauses;
42672		  clauses = c;
42673		  /* For linear, signal that we need to fill up
42674		     the so far unknown linear step.  */
42675		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
42676		    OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
42677		}
42678	    }
42679	}
42680
42681      cond = NULL;
42682      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
42683	cond = cp_parser_omp_for_cond (parser, decl, code);
42684      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42685
42686      incr = NULL;
42687      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
42688	{
42689	  /* If decl is an iterator, preserve the operator on decl
42690	     until finish_omp_for.  */
42691	  if (real_decl
42692	      && ((processing_template_decl
42693		   && (TREE_TYPE (real_decl) == NULL_TREE
42694		       || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
42695		  || CLASS_TYPE_P (TREE_TYPE (real_decl))))
42696	    incr = cp_parser_omp_for_incr (parser, real_decl);
42697	  else
42698	    incr = cp_parser_expression (parser);
42699	  protected_set_expr_location_if_unset (incr, input_location);
42700	}
42701
42702    parse_close_paren:
42703      if (!parens.require_close (parser))
42704	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42705					       /*or_comma=*/false,
42706					       /*consume_paren=*/true);
42707
42708      TREE_VEC_ELT (declv, i) = decl;
42709      TREE_VEC_ELT (initv, i) = init;
42710      TREE_VEC_ELT (condv, i) = cond;
42711      TREE_VEC_ELT (incrv, i) = incr;
42712      if (orig_init)
42713	{
42714	  orig_inits.safe_grow_cleared (i + 1, true);
42715	  orig_inits[i] = orig_init;
42716	}
42717      if (orig_decl)
42718	{
42719	  if (!orig_declv)
42720	    orig_declv = copy_node (declv);
42721	  TREE_VEC_ELT (orig_declv, i) = orig_decl;
42722	}
42723      else if (orig_declv)
42724	TREE_VEC_ELT (orig_declv, i) = decl;
42725
42726      if (i == count - 1)
42727	break;
42728
42729      /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
42730	 in between the collapsed for loops to be still considered perfectly
42731	 nested.  Hopefully the final version clarifies this.
42732	 For now handle (multiple) {'s and empty statements.  */
42733      cp_parser_parse_tentatively (parser);
42734      for (;;)
42735	{
42736	  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42737	    break;
42738	  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42739	    {
42740	      cp_lexer_consume_token (parser->lexer);
42741	      bracecount++;
42742	    }
42743	  else if (bracecount
42744		   && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42745	    cp_lexer_consume_token (parser->lexer);
42746	  else
42747	    {
42748	      loc = cp_lexer_peek_token (parser->lexer)->location;
42749	      error_at (loc, "not enough for loops to collapse");
42750	      collapse_err = true;
42751	      cp_parser_abort_tentative_parse (parser);
42752	      declv = NULL_TREE;
42753	      break;
42754	    }
42755	}
42756
42757      if (declv)
42758	{
42759	  cp_parser_parse_definitely (parser);
42760	  nbraces += bracecount;
42761	}
42762    }
42763
42764  if (nbraces)
42765    if_p = NULL;
42766
42767  /* Note that we saved the original contents of this flag when we entered
42768     the structured block, and so we don't need to re-save it here.  */
42769  parser->in_statement = IN_OMP_FOR;
42770
42771  /* Note that the grammar doesn't call for a structured block here,
42772     though the loop as a whole is a structured block.  */
42773  if (orig_declv)
42774    {
42775      body = begin_omp_structured_block ();
42776      for (i = 0; i < count; i++)
42777	if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
42778	  cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
42779				   TREE_VEC_ELT (declv, i));
42780    }
42781  else
42782    body = push_stmt_list ();
42783  if (inscan)
42784    cp_parser_omp_scan_loop_body (parser);
42785  else
42786    cp_parser_statement (parser, NULL_TREE, false, if_p);
42787  if (orig_declv)
42788    body = finish_omp_structured_block (body);
42789  else
42790    body = pop_stmt_list (body);
42791
42792  if (declv == NULL_TREE)
42793    ret = NULL_TREE;
42794  else
42795    ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
42796			  incrv, body, pre_body, &orig_inits, clauses);
42797
42798  while (nbraces)
42799    {
42800      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42801	{
42802	  cp_lexer_consume_token (parser->lexer);
42803	  nbraces--;
42804	}
42805      else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42806	cp_lexer_consume_token (parser->lexer);
42807      else
42808	{
42809	  if (!collapse_err)
42810	    {
42811	      error_at (cp_lexer_peek_token (parser->lexer)->location,
42812			"collapsed loops not perfectly nested");
42813	    }
42814	  collapse_err = true;
42815	  cp_parser_statement_seq_opt (parser, NULL);
42816	  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
42817	    break;
42818	}
42819    }
42820
42821  while (!for_block->is_empty ())
42822    {
42823      tree t = for_block->pop ();
42824      if (TREE_CODE (t) == STATEMENT_LIST)
42825	add_stmt (pop_stmt_list (t));
42826      else
42827	add_stmt (t);
42828    }
42829
42830  return ret;
42831}
42832
42833/* Helper function for OpenMP parsing, split clauses and call
42834   finish_omp_clauses on each of the set of clauses afterwards.  */
42835
42836static void
42837cp_omp_split_clauses (location_t loc, enum tree_code code,
42838		      omp_clause_mask mask, tree clauses, tree *cclauses)
42839{
42840  int i;
42841  c_omp_split_clauses (loc, code, mask, clauses, cclauses);
42842  for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
42843    if (cclauses[i])
42844      cclauses[i] = finish_omp_clauses (cclauses[i],
42845					i == C_OMP_CLAUSE_SPLIT_TARGET
42846					? C_ORT_OMP_TARGET : C_ORT_OMP);
42847}
42848
42849/* OpenMP 5.0:
42850   #pragma omp loop loop-clause[optseq] new-line
42851     for-loop  */
42852
42853#define OMP_LOOP_CLAUSE_MASK					\
42854	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
42855	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
42856	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
42857	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
42858	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND)		\
42859	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42860
42861static tree
42862cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
42863		    char *p_name, omp_clause_mask mask, tree *cclauses,
42864		    bool *if_p)
42865{
42866  tree clauses, sb, ret;
42867  unsigned int save;
42868  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42869
42870  strcat (p_name, " loop");
42871  mask |= OMP_LOOP_CLAUSE_MASK;
42872
42873  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42874				       cclauses == NULL);
42875  if (cclauses)
42876    {
42877      cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
42878      clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
42879    }
42880
42881  keep_next_level (true);
42882  sb = begin_omp_structured_block ();
42883  save = cp_parser_begin_omp_structured_block (parser);
42884
42885  ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
42886
42887  cp_parser_end_omp_structured_block (parser, save);
42888  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42889
42890  return ret;
42891}
42892
42893/* OpenMP 4.0:
42894   #pragma omp simd simd-clause[optseq] new-line
42895     for-loop  */
42896
42897#define OMP_SIMD_CLAUSE_MASK					\
42898	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)	\
42899	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
42900	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
42901	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
42902	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
42903	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
42904	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
42905	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
42906	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
42907	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL)	\
42908	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42909
42910static tree
42911cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
42912		    char *p_name, omp_clause_mask mask, tree *cclauses,
42913		    bool *if_p)
42914{
42915  tree clauses, sb, ret;
42916  unsigned int save;
42917  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42918
42919  strcat (p_name, " simd");
42920  mask |= OMP_SIMD_CLAUSE_MASK;
42921
42922  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42923				       cclauses == NULL);
42924  if (cclauses)
42925    {
42926      cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
42927      clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
42928      tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
42929				OMP_CLAUSE_ORDERED);
42930      if (c && OMP_CLAUSE_ORDERED_EXPR (c))
42931	{
42932	  error_at (OMP_CLAUSE_LOCATION (c),
42933		    "%<ordered%> clause with parameter may not be specified "
42934		    "on %qs construct", p_name);
42935	  OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
42936	}
42937    }
42938
42939  keep_next_level (true);
42940  sb = begin_omp_structured_block ();
42941  save = cp_parser_begin_omp_structured_block (parser);
42942
42943  ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
42944
42945  cp_parser_end_omp_structured_block (parser, save);
42946  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42947
42948  return ret;
42949}
42950
42951/* OpenMP 2.5:
42952   #pragma omp for for-clause[optseq] new-line
42953     for-loop
42954
42955   OpenMP 4.0:
42956   #pragma omp for simd for-simd-clause[optseq] new-line
42957     for-loop  */
42958
42959#define OMP_FOR_CLAUSE_MASK					\
42960	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
42961	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
42962	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
42963	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
42964	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
42965	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)	\
42966	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)	\
42967	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
42968	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
42969	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
42970	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42971
42972static tree
42973cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
42974		   char *p_name, omp_clause_mask mask, tree *cclauses,
42975		   bool *if_p)
42976{
42977  tree clauses, sb, ret;
42978  unsigned int save;
42979  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42980
42981  strcat (p_name, " for");
42982  mask |= OMP_FOR_CLAUSE_MASK;
42983  /* parallel for{, simd} disallows nowait clause, but for
42984     target {teams distribute ,}parallel for{, simd} it should be accepted.  */
42985  if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
42986    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
42987  /* Composite distribute parallel for{, simd} disallows ordered clause.  */
42988  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
42989    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
42990
42991  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42992    {
42993      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42994      const char *p = IDENTIFIER_POINTER (id);
42995
42996      if (strcmp (p, "simd") == 0)
42997	{
42998	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42999	  if (cclauses == NULL)
43000	    cclauses = cclauses_buf;
43001
43002	  cp_lexer_consume_token (parser->lexer);
43003	  if (!flag_openmp)  /* flag_openmp_simd  */
43004	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43005				       cclauses, if_p);
43006	  sb = begin_omp_structured_block ();
43007	  save = cp_parser_begin_omp_structured_block (parser);
43008	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43009				    cclauses, if_p);
43010	  cp_parser_end_omp_structured_block (parser, save);
43011	  tree body = finish_omp_structured_block (sb);
43012	  if (ret == NULL)
43013	    return ret;
43014	  ret = make_node (OMP_FOR);
43015	  TREE_TYPE (ret) = void_type_node;
43016	  OMP_FOR_BODY (ret) = body;
43017	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43018	  SET_EXPR_LOCATION (ret, loc);
43019	  add_stmt (ret);
43020	  return ret;
43021	}
43022    }
43023  if (!flag_openmp)  /* flag_openmp_simd  */
43024    {
43025      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43026      return NULL_TREE;
43027    }
43028
43029  /* Composite distribute parallel for disallows linear clause.  */
43030  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43031    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
43032
43033  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43034				       cclauses == NULL);
43035  if (cclauses)
43036    {
43037      cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
43038      clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43039    }
43040
43041  keep_next_level (true);
43042  sb = begin_omp_structured_block ();
43043  save = cp_parser_begin_omp_structured_block (parser);
43044
43045  ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
43046
43047  cp_parser_end_omp_structured_block (parser, save);
43048  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43049
43050  return ret;
43051}
43052
43053static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
43054				    omp_clause_mask, tree *, bool *);
43055
43056/* OpenMP 2.5:
43057   # pragma omp master new-line
43058     structured-block  */
43059
43060static tree
43061cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
43062		      char *p_name, omp_clause_mask mask, tree *cclauses,
43063		      bool *if_p)
43064{
43065  tree clauses, sb, ret;
43066  unsigned int save;
43067  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43068
43069  strcat (p_name, " master");
43070
43071  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43072    {
43073      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43074      const char *p = IDENTIFIER_POINTER (id);
43075
43076      if (strcmp (p, "taskloop") == 0)
43077	{
43078	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43079	  if (cclauses == NULL)
43080	    cclauses = cclauses_buf;
43081
43082	  cp_lexer_consume_token (parser->lexer);
43083	  if (!flag_openmp)  /* flag_openmp_simd  */
43084	    return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43085					   cclauses, if_p);
43086	  sb = begin_omp_structured_block ();
43087	  save = cp_parser_begin_omp_structured_block (parser);
43088	  ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43089					cclauses, if_p);
43090	  cp_parser_end_omp_structured_block (parser, save);
43091	  tree body = finish_omp_structured_block (sb);
43092	  if (ret == NULL)
43093	    return ret;
43094	  ret = c_finish_omp_master (loc, body);
43095	  OMP_MASTER_COMBINED (ret) = 1;
43096	  return ret;
43097	}
43098    }
43099  if (!flag_openmp)  /* flag_openmp_simd  */
43100    {
43101      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43102      return NULL_TREE;
43103    }
43104
43105  if (cclauses)
43106    {
43107      clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43108					   false);
43109      cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
43110    }
43111  else
43112    cp_parser_require_pragma_eol (parser, pragma_tok);
43113
43114  return c_finish_omp_master (loc,
43115			      cp_parser_omp_structured_block (parser, if_p));
43116}
43117
43118/* OpenMP 5.1:
43119   # pragma omp masked masked-clauses new-line
43120     structured-block  */
43121
43122#define OMP_MASKED_CLAUSE_MASK					\
43123	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
43124
43125static tree
43126cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
43127		      char *p_name, omp_clause_mask mask, tree *cclauses,
43128		      bool *if_p)
43129{
43130  tree clauses, sb, ret;
43131  unsigned int save;
43132  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43133
43134  strcat (p_name, " masked");
43135  mask |= OMP_MASKED_CLAUSE_MASK;
43136
43137  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43138    {
43139      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43140      const char *p = IDENTIFIER_POINTER (id);
43141
43142      if (strcmp (p, "taskloop") == 0)
43143	{
43144	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43145	  if (cclauses == NULL)
43146	    cclauses = cclauses_buf;
43147
43148	  cp_lexer_consume_token (parser->lexer);
43149	  if (!flag_openmp)  /* flag_openmp_simd  */
43150	    return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43151					   cclauses, if_p);
43152	  sb = begin_omp_structured_block ();
43153	  save = cp_parser_begin_omp_structured_block (parser);
43154	  ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43155					cclauses, if_p);
43156	  cp_parser_end_omp_structured_block (parser, save);
43157	  tree body = finish_omp_structured_block (sb);
43158	  if (ret == NULL)
43159	    return ret;
43160	  ret = c_finish_omp_masked (loc, body,
43161				     cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
43162	  OMP_MASKED_COMBINED (ret) = 1;
43163	  return ret;
43164	}
43165    }
43166  if (!flag_openmp)  /* flag_openmp_simd  */
43167    {
43168      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43169      return NULL_TREE;
43170    }
43171
43172  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43173				       cclauses == NULL);
43174  if (cclauses)
43175    {
43176      cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
43177      clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
43178    }
43179
43180  return c_finish_omp_masked (loc,
43181			      cp_parser_omp_structured_block (parser, if_p),
43182			      clauses);
43183}
43184
43185/* OpenMP 2.5:
43186   # pragma omp ordered new-line
43187     structured-block
43188
43189   OpenMP 4.5:
43190   # pragma omp ordered ordered-clauses new-line
43191     structured-block  */
43192
43193#define OMP_ORDERED_CLAUSE_MASK					\
43194	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)	\
43195	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
43196
43197#define OMP_ORDERED_DEPEND_CLAUSE_MASK				\
43198	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43199
43200static bool
43201cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
43202		       enum pragma_context context, bool *if_p)
43203{
43204  location_t loc = pragma_tok->location;
43205  int n = 1;
43206
43207  /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
43208  if (parser->lexer->in_omp_attribute_pragma
43209      && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43210    n = 2;
43211
43212  if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
43213    {
43214      tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
43215      const char *p = IDENTIFIER_POINTER (id);
43216
43217      if (strcmp (p, "depend") == 0)
43218	{
43219	  if (!flag_openmp)	/* flag_openmp_simd */
43220	    {
43221	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43222	      return false;
43223	    }
43224	  if (context == pragma_stmt)
43225	    {
43226	      error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
43227			"%<depend%> clause may only be used in compound "
43228			"statements");
43229	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43230	      return true;
43231	    }
43232	  tree clauses
43233	    = cp_parser_omp_all_clauses (parser,
43234					 OMP_ORDERED_DEPEND_CLAUSE_MASK,
43235					 "#pragma omp ordered", pragma_tok);
43236	  c_finish_omp_ordered (loc, clauses, NULL_TREE);
43237	  return false;
43238	}
43239    }
43240
43241  tree clauses
43242    = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
43243				 "#pragma omp ordered", pragma_tok);
43244
43245  if (!flag_openmp     /* flag_openmp_simd  */
43246      && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
43247    return false;
43248
43249  c_finish_omp_ordered (loc, clauses,
43250			cp_parser_omp_structured_block (parser, if_p));
43251  return true;
43252}
43253
43254/* OpenMP 2.5:
43255
43256   section-scope:
43257     { section-sequence }
43258
43259   section-sequence:
43260     section-directive[opt] structured-block
43261     section-sequence section-directive structured-block  */
43262
43263static tree
43264cp_parser_omp_sections_scope (cp_parser *parser)
43265{
43266  tree stmt, substmt;
43267  bool error_suppress = false;
43268  cp_token *tok;
43269
43270  matching_braces braces;
43271  if (!braces.require_open (parser))
43272    return NULL_TREE;
43273
43274  stmt = push_stmt_list ();
43275
43276  if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
43277      != PRAGMA_OMP_SECTION
43278      && !cp_parser_omp_section_scan (parser, "section", true))
43279    {
43280      substmt = cp_parser_omp_structured_block_sequence (parser,
43281							 PRAGMA_OMP_SECTION);
43282      substmt = build1 (OMP_SECTION, void_type_node, substmt);
43283      add_stmt (substmt);
43284    }
43285
43286  while (1)
43287    {
43288      tok = cp_lexer_peek_token (parser->lexer);
43289      if (tok->type == CPP_CLOSE_BRACE)
43290	break;
43291      if (tok->type == CPP_EOF)
43292	break;
43293
43294      if (cp_parser_omp_section_scan (parser, "section", false))
43295	tok = cp_lexer_peek_token (parser->lexer);
43296      if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
43297	{
43298	  cp_lexer_consume_token (parser->lexer);
43299	  cp_parser_require_pragma_eol (parser, tok);
43300	  error_suppress = false;
43301	}
43302      else if (!error_suppress)
43303	{
43304	  cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
43305	  error_suppress = true;
43306	}
43307
43308      substmt = cp_parser_omp_structured_block_sequence (parser,
43309							 PRAGMA_OMP_SECTION);
43310      substmt = build1 (OMP_SECTION, void_type_node, substmt);
43311      add_stmt (substmt);
43312    }
43313  braces.require_close (parser);
43314
43315  substmt = pop_stmt_list (stmt);
43316
43317  stmt = make_node (OMP_SECTIONS);
43318  TREE_TYPE (stmt) = void_type_node;
43319  OMP_SECTIONS_BODY (stmt) = substmt;
43320
43321  add_stmt (stmt);
43322  return stmt;
43323}
43324
43325/* OpenMP 2.5:
43326   # pragma omp sections sections-clause[optseq] newline
43327     sections-scope  */
43328
43329#define OMP_SECTIONS_CLAUSE_MASK				\
43330	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43331	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43332	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
43333	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
43334	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43335	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43336
43337static tree
43338cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
43339			char *p_name, omp_clause_mask mask, tree *cclauses)
43340{
43341  tree clauses, ret;
43342  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43343
43344  strcat (p_name, " sections");
43345  mask |= OMP_SECTIONS_CLAUSE_MASK;
43346  if (cclauses)
43347    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
43348
43349  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43350				       cclauses == NULL);
43351  if (cclauses)
43352    {
43353      cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
43354      clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
43355    }
43356
43357  ret = cp_parser_omp_sections_scope (parser);
43358  if (ret)
43359    OMP_SECTIONS_CLAUSES (ret) = clauses;
43360
43361  return ret;
43362}
43363
43364/* OpenMP 2.5:
43365   # pragma omp parallel parallel-clause[optseq] new-line
43366     structured-block
43367   # pragma omp parallel for parallel-for-clause[optseq] new-line
43368     structured-block
43369   # pragma omp parallel sections parallel-sections-clause[optseq] new-line
43370     structured-block
43371
43372   OpenMP 4.0:
43373   # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
43374     structured-block */
43375
43376#define OMP_PARALLEL_CLAUSE_MASK				\
43377	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
43378	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43379	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43380	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
43381	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
43382	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)	\
43383	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
43384	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)	\
43385	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43386	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
43387
43388static tree
43389cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
43390			char *p_name, omp_clause_mask mask, tree *cclauses,
43391			bool *if_p)
43392{
43393  tree stmt, clauses, block;
43394  unsigned int save;
43395  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43396
43397  strcat (p_name, " parallel");
43398  mask |= OMP_PARALLEL_CLAUSE_MASK;
43399  /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
43400  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
43401      && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
43402    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
43403
43404  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43405    {
43406      tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43407      if (cclauses == NULL)
43408	cclauses = cclauses_buf;
43409
43410      cp_lexer_consume_token (parser->lexer);
43411      if (!flag_openmp)  /* flag_openmp_simd  */
43412	return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43413				  if_p);
43414      block = begin_omp_parallel ();
43415      save = cp_parser_begin_omp_structured_block (parser);
43416      tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43417				    if_p);
43418      cp_parser_end_omp_structured_block (parser, save);
43419      stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43420				  block);
43421      if (ret == NULL_TREE)
43422	return ret;
43423      OMP_PARALLEL_COMBINED (stmt) = 1;
43424      return stmt;
43425    }
43426  /* When combined with distribute, parallel has to be followed by for.
43427     #pragma omp target parallel is allowed though.  */
43428  else if (cclauses
43429	   && (mask & (OMP_CLAUSE_MASK_1
43430		       << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43431    {
43432      error_at (loc, "expected %<for%> after %qs", p_name);
43433      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43434      return NULL_TREE;
43435    }
43436  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43437    {
43438      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43439      const char *p = IDENTIFIER_POINTER (id);
43440      if (cclauses == NULL && strcmp (p, "masked") == 0)
43441	{
43442	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43443	  cclauses = cclauses_buf;
43444
43445	  cp_lexer_consume_token (parser->lexer);
43446	  if (!flag_openmp)  /* flag_openmp_simd  */
43447	    return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43448					 cclauses, if_p);
43449	  block = begin_omp_parallel ();
43450	  save = cp_parser_begin_omp_structured_block (parser);
43451	  tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43452					   cclauses, if_p);
43453	  cp_parser_end_omp_structured_block (parser, save);
43454	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43455				      block);
43456	  if (ret == NULL_TREE)
43457	    return ret;
43458	  /* masked does have just filter clause, but during gimplification
43459	     isn't represented by a gimplification omp context, so for
43460	     #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
43461	     so that
43462	     #pragma omp parallel masked
43463	     #pragma omp taskloop simd lastprivate (x)
43464	     isn't confused with
43465	     #pragma omp parallel masked taskloop simd lastprivate (x)  */
43466	  if (OMP_MASKED_COMBINED (ret))
43467	    OMP_PARALLEL_COMBINED (stmt) = 1;
43468	  return stmt;
43469	}
43470      else if (cclauses == NULL && strcmp (p, "master") == 0)
43471	{
43472	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43473	  cclauses = cclauses_buf;
43474
43475	  cp_lexer_consume_token (parser->lexer);
43476	  if (!flag_openmp)  /* flag_openmp_simd  */
43477	    return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43478					 cclauses, if_p);
43479	  block = begin_omp_parallel ();
43480	  save = cp_parser_begin_omp_structured_block (parser);
43481	  tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43482					   cclauses, if_p);
43483	  cp_parser_end_omp_structured_block (parser, save);
43484	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43485				      block);
43486	  if (ret == NULL_TREE)
43487	    return ret;
43488	  /* master doesn't have any clauses and during gimplification
43489	     isn't represented by a gimplification omp context, so for
43490	     #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
43491	     so that
43492	     #pragma omp parallel master
43493	     #pragma omp taskloop simd lastprivate (x)
43494	     isn't confused with
43495	     #pragma omp parallel master taskloop simd lastprivate (x)  */
43496	  if (OMP_MASTER_COMBINED (ret))
43497	    OMP_PARALLEL_COMBINED (stmt) = 1;
43498	  return stmt;
43499	}
43500      else if (strcmp (p, "loop") == 0)
43501	{
43502	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43503	  if (cclauses == NULL)
43504	    cclauses = cclauses_buf;
43505
43506	  cp_lexer_consume_token (parser->lexer);
43507	  if (!flag_openmp)  /* flag_openmp_simd  */
43508	    return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43509				       cclauses, if_p);
43510	  block = begin_omp_parallel ();
43511	  save = cp_parser_begin_omp_structured_block (parser);
43512	  tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43513					 cclauses, if_p);
43514	  cp_parser_end_omp_structured_block (parser, save);
43515	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43516				      block);
43517	  if (ret == NULL_TREE)
43518	    return ret;
43519	  OMP_PARALLEL_COMBINED (stmt) = 1;
43520	  return stmt;
43521	}
43522      else if (!flag_openmp)  /* flag_openmp_simd  */
43523	{
43524	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43525	  return NULL_TREE;
43526	}
43527      else if (cclauses == NULL && strcmp (p, "sections") == 0)
43528	{
43529	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43530	  cclauses = cclauses_buf;
43531
43532	  cp_lexer_consume_token (parser->lexer);
43533	  block = begin_omp_parallel ();
43534	  save = cp_parser_begin_omp_structured_block (parser);
43535	  cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
43536	  cp_parser_end_omp_structured_block (parser, save);
43537	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43538				      block);
43539	  OMP_PARALLEL_COMBINED (stmt) = 1;
43540	  return stmt;
43541	}
43542    }
43543  else if (!flag_openmp)  /* flag_openmp_simd  */
43544    {
43545      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43546      return NULL_TREE;
43547    }
43548
43549  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43550				       cclauses == NULL);
43551  if (cclauses)
43552    {
43553      cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
43554      clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
43555    }
43556
43557  block = begin_omp_parallel ();
43558  save = cp_parser_begin_omp_structured_block (parser);
43559  parser->omp_attrs_forbidden_p = true;
43560  cp_parser_statement (parser, NULL_TREE, false, if_p);
43561  cp_parser_end_omp_structured_block (parser, save);
43562  stmt = finish_omp_parallel (clauses, block);
43563  return stmt;
43564}
43565
43566/* OpenMP 2.5:
43567   # pragma omp single single-clause[optseq] new-line
43568     structured-block  */
43569
43570#define OMP_SINGLE_CLAUSE_MASK					\
43571	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43572	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43573	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)	\
43574	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43575	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43576
43577static tree
43578cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43579{
43580  tree stmt = make_node (OMP_SINGLE);
43581  TREE_TYPE (stmt) = void_type_node;
43582  SET_EXPR_LOCATION (stmt, pragma_tok->location);
43583
43584  OMP_SINGLE_CLAUSES (stmt)
43585    = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
43586				 "#pragma omp single", pragma_tok);
43587  OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43588
43589  return add_stmt (stmt);
43590}
43591
43592/* OpenMP 5.1:
43593   # pragma omp scope scope-clause[optseq] new-line
43594     structured-block  */
43595
43596#define OMP_SCOPE_CLAUSE_MASK					\
43597	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43598	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
43599	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43600
43601static tree
43602cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43603{
43604  tree stmt = make_node (OMP_SCOPE);
43605  TREE_TYPE (stmt) = void_type_node;
43606  SET_EXPR_LOCATION (stmt, pragma_tok->location);
43607
43608  OMP_SCOPE_CLAUSES (stmt)
43609    = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
43610				 "#pragma omp scope", pragma_tok);
43611  OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43612
43613  return add_stmt (stmt);
43614}
43615
43616/* OpenMP 3.0:
43617   # pragma omp task task-clause[optseq] new-line
43618     structured-block  */
43619
43620#define OMP_TASK_CLAUSE_MASK					\
43621	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
43622	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
43623	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
43624	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43625	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43626	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
43627	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
43628	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
43629	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
43630	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY)	\
43631	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43632	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION)	\
43633	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH)	\
43634	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
43635
43636static tree
43637cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43638{
43639  tree clauses, block;
43640  unsigned int save;
43641
43642  clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
43643				       "#pragma omp task", pragma_tok);
43644  block = begin_omp_task ();
43645  save = cp_parser_begin_omp_structured_block (parser);
43646  parser->omp_attrs_forbidden_p = true;
43647  cp_parser_statement (parser, NULL_TREE, false, if_p);
43648  cp_parser_end_omp_structured_block (parser, save);
43649  return finish_omp_task (clauses, block);
43650}
43651
43652/* OpenMP 3.0:
43653   # pragma omp taskwait new-line
43654
43655   OpenMP 5.0:
43656   # pragma omp taskwait taskwait-clause[opt] new-line  */
43657
43658#define OMP_TASKWAIT_CLAUSE_MASK				\
43659	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43660
43661static void
43662cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
43663{
43664  tree clauses
43665    = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
43666				 "#pragma omp taskwait", pragma_tok);
43667
43668  if (clauses)
43669    {
43670      tree stmt = make_node (OMP_TASK);
43671      TREE_TYPE (stmt) = void_node;
43672      OMP_TASK_CLAUSES (stmt) = clauses;
43673      OMP_TASK_BODY (stmt) = NULL_TREE;
43674      SET_EXPR_LOCATION (stmt, pragma_tok->location);
43675      add_stmt (stmt);
43676    }
43677  else
43678    finish_omp_taskwait ();
43679}
43680
43681/* OpenMP 3.1:
43682   # pragma omp taskyield new-line  */
43683
43684static void
43685cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
43686{
43687  cp_parser_require_pragma_eol (parser, pragma_tok);
43688  finish_omp_taskyield ();
43689}
43690
43691/* OpenMP 4.0:
43692   # pragma omp taskgroup new-line
43693     structured-block
43694
43695   OpenMP 5.0:
43696   # pragma omp taskgroup taskgroup-clause[optseq] new-line  */
43697
43698#define OMP_TASKGROUP_CLAUSE_MASK				\
43699	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43700	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
43701
43702static tree
43703cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43704{
43705  tree clauses
43706    = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
43707				 "#pragma omp taskgroup", pragma_tok);
43708  return c_finish_omp_taskgroup (input_location,
43709				 cp_parser_omp_structured_block (parser,
43710								 if_p),
43711				 clauses);
43712}
43713
43714
43715/* OpenMP 2.5:
43716   # pragma omp threadprivate (variable-list) */
43717
43718static void
43719cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
43720{
43721  tree vars;
43722
43723  vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43724  cp_parser_require_pragma_eol (parser, pragma_tok);
43725
43726  finish_omp_threadprivate (vars);
43727}
43728
43729/* OpenMP 4.0:
43730   # pragma omp cancel cancel-clause[optseq] new-line  */
43731
43732#define OMP_CANCEL_CLAUSE_MASK					\
43733	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
43734	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
43735	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
43736	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)	\
43737	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
43738
43739static void
43740cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
43741{
43742  tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
43743					    "#pragma omp cancel", pragma_tok);
43744  finish_omp_cancel (clauses);
43745}
43746
43747/* OpenMP 4.0:
43748   # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
43749
43750#define OMP_CANCELLATION_POINT_CLAUSE_MASK			\
43751	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
43752	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
43753	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
43754	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
43755
43756static bool
43757cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
43758				  enum pragma_context context)
43759{
43760  tree clauses;
43761  bool point_seen = false;
43762
43763  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43764    {
43765      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43766      const char *p = IDENTIFIER_POINTER (id);
43767
43768      if (strcmp (p, "point") == 0)
43769	{
43770	  cp_lexer_consume_token (parser->lexer);
43771	  point_seen = true;
43772	}
43773    }
43774  if (!point_seen)
43775    {
43776      cp_parser_error (parser, "expected %<point%>");
43777      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43778      return false;
43779    }
43780
43781  if (context != pragma_compound)
43782    {
43783      if (context == pragma_stmt)
43784	error_at (pragma_tok->location,
43785		  "%<#pragma %s%> may only be used in compound statements",
43786		  "omp cancellation point");
43787      else
43788	cp_parser_error (parser, "expected declaration specifiers");
43789      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43790      return true;
43791    }
43792
43793  clauses = cp_parser_omp_all_clauses (parser,
43794				       OMP_CANCELLATION_POINT_CLAUSE_MASK,
43795				       "#pragma omp cancellation point",
43796				       pragma_tok);
43797  finish_omp_cancellation_point (clauses);
43798  return true;
43799}
43800
43801/* OpenMP 4.0:
43802   #pragma omp distribute distribute-clause[optseq] new-line
43803     for-loop  */
43804
43805#define OMP_DISTRIBUTE_CLAUSE_MASK				\
43806	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43807	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43808	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
43809	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
43810	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43811	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
43812	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43813
43814static tree
43815cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
43816			  char *p_name, omp_clause_mask mask, tree *cclauses,
43817			  bool *if_p)
43818{
43819  tree clauses, sb, ret;
43820  unsigned int save;
43821  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43822
43823  strcat (p_name, " distribute");
43824  mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
43825
43826  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43827    {
43828      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43829      const char *p = IDENTIFIER_POINTER (id);
43830      bool simd = false;
43831      bool parallel = false;
43832
43833      if (strcmp (p, "simd") == 0)
43834	simd = true;
43835      else
43836	parallel = strcmp (p, "parallel") == 0;
43837      if (parallel || simd)
43838	{
43839	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43840	  if (cclauses == NULL)
43841	    cclauses = cclauses_buf;
43842	  cp_lexer_consume_token (parser->lexer);
43843	  if (!flag_openmp)  /* flag_openmp_simd  */
43844	    {
43845	      if (simd)
43846		return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43847					   cclauses, if_p);
43848	      else
43849		return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
43850					       cclauses, if_p);
43851	    }
43852	  sb = begin_omp_structured_block ();
43853	  save = cp_parser_begin_omp_structured_block (parser);
43854	  if (simd)
43855	    ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43856				      cclauses, if_p);
43857	  else
43858	    ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
43859					  cclauses, if_p);
43860	  cp_parser_end_omp_structured_block (parser, save);
43861	  tree body = finish_omp_structured_block (sb);
43862	  if (ret == NULL)
43863	    return ret;
43864	  ret = make_node (OMP_DISTRIBUTE);
43865	  TREE_TYPE (ret) = void_type_node;
43866	  OMP_FOR_BODY (ret) = body;
43867	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
43868	  SET_EXPR_LOCATION (ret, loc);
43869	  add_stmt (ret);
43870	  return ret;
43871	}
43872    }
43873  if (!flag_openmp)  /* flag_openmp_simd  */
43874    {
43875      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43876      return NULL_TREE;
43877    }
43878
43879  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43880				       cclauses == NULL);
43881  if (cclauses)
43882    {
43883      cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
43884      clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
43885    }
43886
43887  keep_next_level (true);
43888  sb = begin_omp_structured_block ();
43889  save = cp_parser_begin_omp_structured_block (parser);
43890
43891  ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
43892
43893  cp_parser_end_omp_structured_block (parser, save);
43894  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43895
43896  return ret;
43897}
43898
43899/* OpenMP 4.0:
43900   # pragma omp teams teams-clause[optseq] new-line
43901     structured-block  */
43902
43903#define OMP_TEAMS_CLAUSE_MASK					\
43904	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43905	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43906	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
43907	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
43908	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)	\
43909	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT)	\
43910	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43911	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
43912
43913static tree
43914cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
43915		     char *p_name, omp_clause_mask mask, tree *cclauses,
43916		     bool *if_p)
43917{
43918  tree clauses, sb, ret;
43919  unsigned int save;
43920  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43921
43922  strcat (p_name, " teams");
43923  mask |= OMP_TEAMS_CLAUSE_MASK;
43924
43925  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43926    {
43927      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43928      const char *p = IDENTIFIER_POINTER (id);
43929      if (strcmp (p, "distribute") == 0)
43930	{
43931	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43932	  if (cclauses == NULL)
43933	    cclauses = cclauses_buf;
43934
43935	  cp_lexer_consume_token (parser->lexer);
43936	  if (!flag_openmp)  /* flag_openmp_simd  */
43937	    return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
43938					     cclauses, if_p);
43939	  keep_next_level (true);
43940	  sb = begin_omp_structured_block ();
43941	  save = cp_parser_begin_omp_structured_block (parser);
43942	  ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
43943					  cclauses, if_p);
43944	  cp_parser_end_omp_structured_block (parser, save);
43945	  tree body = finish_omp_structured_block (sb);
43946	  if (ret == NULL)
43947	    return ret;
43948	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43949	  ret = make_node (OMP_TEAMS);
43950	  TREE_TYPE (ret) = void_type_node;
43951	  OMP_TEAMS_CLAUSES (ret) = clauses;
43952	  OMP_TEAMS_BODY (ret) = body;
43953	  OMP_TEAMS_COMBINED (ret) = 1;
43954	  SET_EXPR_LOCATION (ret, loc);
43955	  return add_stmt (ret);
43956	}
43957      else if (strcmp (p, "loop") == 0)
43958	{
43959	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43960	  if (cclauses == NULL)
43961	    cclauses = cclauses_buf;
43962
43963	  cp_lexer_consume_token (parser->lexer);
43964	  if (!flag_openmp)  /* flag_openmp_simd  */
43965	    return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43966				       cclauses, if_p);
43967	  keep_next_level (true);
43968	  sb = begin_omp_structured_block ();
43969	  save = cp_parser_begin_omp_structured_block (parser);
43970	  ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43971				    cclauses, if_p);
43972	  cp_parser_end_omp_structured_block (parser, save);
43973	  tree body = finish_omp_structured_block (sb);
43974	  if (ret == NULL)
43975	    return ret;
43976	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43977	  ret = make_node (OMP_TEAMS);
43978	  TREE_TYPE (ret) = void_type_node;
43979	  OMP_TEAMS_CLAUSES (ret) = clauses;
43980	  OMP_TEAMS_BODY (ret) = body;
43981	  OMP_TEAMS_COMBINED (ret) = 1;
43982	  SET_EXPR_LOCATION (ret, loc);
43983	  return add_stmt (ret);
43984	}
43985    }
43986  if (!flag_openmp)  /* flag_openmp_simd  */
43987    {
43988      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43989      return NULL_TREE;
43990    }
43991
43992  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43993				       cclauses == NULL);
43994  if (cclauses)
43995    {
43996      cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
43997      clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43998    }
43999
44000  tree stmt = make_node (OMP_TEAMS);
44001  TREE_TYPE (stmt) = void_type_node;
44002  OMP_TEAMS_CLAUSES (stmt) = clauses;
44003  keep_next_level (true);
44004  OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44005  SET_EXPR_LOCATION (stmt, loc);
44006
44007  return add_stmt (stmt);
44008}
44009
44010/* OpenMP 4.0:
44011   # pragma omp target data target-data-clause[optseq] new-line
44012     structured-block  */
44013
44014#define OMP_TARGET_DATA_CLAUSE_MASK				\
44015	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44016	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
44017	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44018	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
44019	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
44020
44021static tree
44022cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44023{
44024  tree clauses
44025    = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
44026				 "#pragma omp target data", pragma_tok);
44027  c_omp_adjust_map_clauses (clauses, false);
44028  int map_seen = 0;
44029  for (tree *pc = &clauses; *pc;)
44030    {
44031      if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44032	switch (OMP_CLAUSE_MAP_KIND (*pc))
44033	  {
44034	  case GOMP_MAP_TO:
44035	  case GOMP_MAP_ALWAYS_TO:
44036	  case GOMP_MAP_FROM:
44037	  case GOMP_MAP_ALWAYS_FROM:
44038	  case GOMP_MAP_TOFROM:
44039	  case GOMP_MAP_ALWAYS_TOFROM:
44040	  case GOMP_MAP_ALLOC:
44041	    map_seen = 3;
44042	    break;
44043	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
44044	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44045	  case GOMP_MAP_ALWAYS_POINTER:
44046	  case GOMP_MAP_ATTACH_DETACH:
44047	    break;
44048	  default:
44049	    map_seen |= 1;
44050	    error_at (OMP_CLAUSE_LOCATION (*pc),
44051		      "%<#pragma omp target data%> with map-type other "
44052		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
44053		      "on %<map%> clause");
44054	    *pc = OMP_CLAUSE_CHAIN (*pc);
44055	    continue;
44056	  }
44057      else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
44058	       || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
44059	map_seen = 3;
44060      pc = &OMP_CLAUSE_CHAIN (*pc);
44061    }
44062
44063  if (map_seen != 3)
44064    {
44065      if (map_seen == 0)
44066	error_at (pragma_tok->location,
44067		  "%<#pragma omp target data%> must contain at least "
44068		  "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
44069		  "clause");
44070      return NULL_TREE;
44071    }
44072
44073  tree stmt = make_node (OMP_TARGET_DATA);
44074  TREE_TYPE (stmt) = void_type_node;
44075  OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
44076
44077  keep_next_level (true);
44078  OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44079
44080  SET_EXPR_LOCATION (stmt, pragma_tok->location);
44081  return add_stmt (stmt);
44082}
44083
44084/* OpenMP 4.5:
44085   # pragma omp target enter data target-enter-data-clause[optseq] new-line
44086     structured-block  */
44087
44088#define OMP_TARGET_ENTER_DATA_CLAUSE_MASK			\
44089	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44090	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
44091	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44092	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
44093	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44094
44095static bool
44096cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
44097				 enum pragma_context context)
44098{
44099  bool data_seen = false;
44100  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44101    {
44102      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44103      const char *p = IDENTIFIER_POINTER (id);
44104
44105      if (strcmp (p, "data") == 0)
44106	{
44107	  cp_lexer_consume_token (parser->lexer);
44108	  data_seen = true;
44109	}
44110    }
44111  if (!data_seen)
44112    {
44113      cp_parser_error (parser, "expected %<data%>");
44114      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44115      return false;
44116    }
44117
44118  if (context == pragma_stmt)
44119    {
44120      error_at (pragma_tok->location,
44121		"%<#pragma %s%> may only be used in compound statements",
44122		"omp target enter data");
44123      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44124      return true;
44125    }
44126
44127  tree clauses
44128    = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
44129				 "#pragma omp target enter data", pragma_tok);
44130  c_omp_adjust_map_clauses (clauses, false);
44131  int map_seen = 0;
44132  for (tree *pc = &clauses; *pc;)
44133    {
44134      if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44135	switch (OMP_CLAUSE_MAP_KIND (*pc))
44136	  {
44137	  case GOMP_MAP_TO:
44138	  case GOMP_MAP_ALWAYS_TO:
44139	  case GOMP_MAP_ALLOC:
44140	    map_seen = 3;
44141	    break;
44142	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
44143	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44144	  case GOMP_MAP_ALWAYS_POINTER:
44145	  case GOMP_MAP_ATTACH_DETACH:
44146	    break;
44147	  default:
44148	    map_seen |= 1;
44149	    error_at (OMP_CLAUSE_LOCATION (*pc),
44150		      "%<#pragma omp target enter data%> with map-type other "
44151		      "than %<to%> or %<alloc%> on %<map%> clause");
44152	    *pc = OMP_CLAUSE_CHAIN (*pc);
44153	    continue;
44154	  }
44155      pc = &OMP_CLAUSE_CHAIN (*pc);
44156    }
44157
44158  if (map_seen != 3)
44159    {
44160      if (map_seen == 0)
44161	error_at (pragma_tok->location,
44162		  "%<#pragma omp target enter data%> must contain at least "
44163		  "one %<map%> clause");
44164      return true;
44165    }
44166
44167  tree stmt = make_node (OMP_TARGET_ENTER_DATA);
44168  TREE_TYPE (stmt) = void_type_node;
44169  OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
44170  SET_EXPR_LOCATION (stmt, pragma_tok->location);
44171  add_stmt (stmt);
44172  return true;
44173}
44174
44175/* OpenMP 4.5:
44176   # pragma omp target exit data target-enter-data-clause[optseq] new-line
44177     structured-block  */
44178
44179#define OMP_TARGET_EXIT_DATA_CLAUSE_MASK			\
44180	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44181	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
44182	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44183	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
44184	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44185
44186static bool
44187cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
44188				enum pragma_context context)
44189{
44190  bool data_seen = false;
44191  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44192    {
44193      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44194      const char *p = IDENTIFIER_POINTER (id);
44195
44196      if (strcmp (p, "data") == 0)
44197	{
44198	  cp_lexer_consume_token (parser->lexer);
44199	  data_seen = true;
44200	}
44201    }
44202  if (!data_seen)
44203    {
44204      cp_parser_error (parser, "expected %<data%>");
44205      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44206      return false;
44207    }
44208
44209  if (context == pragma_stmt)
44210    {
44211      error_at (pragma_tok->location,
44212		"%<#pragma %s%> may only be used in compound statements",
44213		"omp target exit data");
44214      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44215      return true;
44216    }
44217
44218  tree clauses
44219    = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
44220				 "#pragma omp target exit data", pragma_tok);
44221  c_omp_adjust_map_clauses (clauses, false);
44222  int map_seen = 0;
44223  for (tree *pc = &clauses; *pc;)
44224    {
44225      if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44226	switch (OMP_CLAUSE_MAP_KIND (*pc))
44227	  {
44228	  case GOMP_MAP_FROM:
44229	  case GOMP_MAP_ALWAYS_FROM:
44230	  case GOMP_MAP_RELEASE:
44231	  case GOMP_MAP_DELETE:
44232	    map_seen = 3;
44233	    break;
44234	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
44235	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44236	  case GOMP_MAP_ALWAYS_POINTER:
44237	  case GOMP_MAP_ATTACH_DETACH:
44238	    break;
44239	  default:
44240	    map_seen |= 1;
44241	    error_at (OMP_CLAUSE_LOCATION (*pc),
44242		      "%<#pragma omp target exit data%> with map-type other "
44243		      "than %<from%>, %<release%> or %<delete%> on %<map%>"
44244		      " clause");
44245	    *pc = OMP_CLAUSE_CHAIN (*pc);
44246	    continue;
44247	  }
44248      pc = &OMP_CLAUSE_CHAIN (*pc);
44249    }
44250
44251  if (map_seen != 3)
44252    {
44253      if (map_seen == 0)
44254	error_at (pragma_tok->location,
44255		  "%<#pragma omp target exit data%> must contain at least "
44256		  "one %<map%> clause");
44257      return true;
44258    }
44259
44260  tree stmt = make_node (OMP_TARGET_EXIT_DATA);
44261  TREE_TYPE (stmt) = void_type_node;
44262  OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
44263  SET_EXPR_LOCATION (stmt, pragma_tok->location);
44264  add_stmt (stmt);
44265  return true;
44266}
44267
44268/* OpenMP 4.0:
44269   # pragma omp target update target-update-clause[optseq] new-line */
44270
44271#define OMP_TARGET_UPDATE_CLAUSE_MASK				\
44272	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)		\
44273	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
44274	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44275	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44276	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
44277	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44278
44279static bool
44280cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
44281			     enum pragma_context context)
44282{
44283  if (context == pragma_stmt)
44284    {
44285      error_at (pragma_tok->location,
44286		"%<#pragma %s%> may only be used in compound statements",
44287		"omp target update");
44288      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44289      return true;
44290    }
44291
44292  tree clauses
44293    = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
44294				 "#pragma omp target update", pragma_tok);
44295  if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
44296      && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
44297    {
44298      error_at (pragma_tok->location,
44299		"%<#pragma omp target update%> must contain at least one "
44300		"%<from%> or %<to%> clauses");
44301      return true;
44302    }
44303
44304  tree stmt = make_node (OMP_TARGET_UPDATE);
44305  TREE_TYPE (stmt) = void_type_node;
44306  OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
44307  SET_EXPR_LOCATION (stmt, pragma_tok->location);
44308  add_stmt (stmt);
44309  return true;
44310}
44311
44312/* OpenMP 4.0:
44313   # pragma omp target target-clause[optseq] new-line
44314     structured-block  */
44315
44316#define OMP_TARGET_CLAUSE_MASK					\
44317	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44318	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
44319	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44320	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
44321	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
44322	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
44323	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
44324	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)	\
44325	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
44326	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION)	\
44327	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT)	\
44328	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
44329	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
44330
44331static bool
44332cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
44333		      enum pragma_context context, bool *if_p)
44334{
44335  if (flag_openmp)
44336    omp_requires_mask
44337      = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44338
44339  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44340    {
44341      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44342      const char *p = IDENTIFIER_POINTER (id);
44343      enum tree_code ccode = ERROR_MARK;
44344
44345      if (strcmp (p, "teams") == 0)
44346	ccode = OMP_TEAMS;
44347      else if (strcmp (p, "parallel") == 0)
44348	ccode = OMP_PARALLEL;
44349      else if (strcmp (p, "simd") == 0)
44350	ccode = OMP_SIMD;
44351      if (ccode != ERROR_MARK)
44352	{
44353	  tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
44354	  char p_name[sizeof ("#pragma omp target teams distribute "
44355			      "parallel for simd")];
44356
44357	  cp_lexer_consume_token (parser->lexer);
44358	  strcpy (p_name, "#pragma omp target");
44359	  if (!flag_openmp)  /* flag_openmp_simd  */
44360	    {
44361	      tree stmt;
44362	      switch (ccode)
44363		{
44364		case OMP_TEAMS:
44365		  stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
44366					      OMP_TARGET_CLAUSE_MASK,
44367					      cclauses, if_p);
44368		  break;
44369		case OMP_PARALLEL:
44370		  stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44371						 OMP_TARGET_CLAUSE_MASK,
44372						 cclauses, if_p);
44373		  break;
44374		case OMP_SIMD:
44375		  stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
44376					     OMP_TARGET_CLAUSE_MASK,
44377					     cclauses, if_p);
44378		  break;
44379		default:
44380		  gcc_unreachable ();
44381		}
44382	      return stmt != NULL_TREE;
44383	    }
44384	  keep_next_level (true);
44385	  tree sb = begin_omp_structured_block (), ret;
44386	  unsigned save = cp_parser_begin_omp_structured_block (parser);
44387	  switch (ccode)
44388	    {
44389	    case OMP_TEAMS:
44390	      ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
44391					 OMP_TARGET_CLAUSE_MASK, cclauses,
44392					 if_p);
44393	      break;
44394	    case OMP_PARALLEL:
44395	      ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44396					    OMP_TARGET_CLAUSE_MASK, cclauses,
44397					    if_p);
44398	      break;
44399	    case OMP_SIMD:
44400	      ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
44401					OMP_TARGET_CLAUSE_MASK, cclauses,
44402					if_p);
44403	      break;
44404	    default:
44405	      gcc_unreachable ();
44406	    }
44407	  cp_parser_end_omp_structured_block (parser, save);
44408	  tree body = finish_omp_structured_block (sb);
44409	  if (ret == NULL_TREE)
44410	    return false;
44411	  if (ccode == OMP_TEAMS && !processing_template_decl)
44412	    /* For combined target teams, ensure the num_teams and
44413	       thread_limit clause expressions are evaluated on the host,
44414	       before entering the target construct.  */
44415	    for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44416		 c; c = OMP_CLAUSE_CHAIN (c))
44417	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
44418		  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
44419		for (int i = 0;
44420		     i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
44421		  if (OMP_CLAUSE_OPERAND (c, i)
44422		      && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
44423		    {
44424		      tree expr = OMP_CLAUSE_OPERAND (c, i);
44425		      expr = force_target_expr (TREE_TYPE (expr), expr,
44426						tf_none);
44427		      if (expr == error_mark_node)
44428			continue;
44429		      tree tmp = TARGET_EXPR_SLOT (expr);
44430		      add_stmt (expr);
44431		      OMP_CLAUSE_OPERAND (c, i) = expr;
44432		      tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
44433						  OMP_CLAUSE_FIRSTPRIVATE);
44434		      OMP_CLAUSE_DECL (tc) = tmp;
44435		      OMP_CLAUSE_CHAIN (tc)
44436			= cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
44437		      cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
44438		    }
44439	  c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
44440	  finish_omp_target (pragma_tok->location,
44441			     cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
44442	  return true;
44443	}
44444      else if (!flag_openmp)  /* flag_openmp_simd  */
44445	{
44446	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44447	  return false;
44448	}
44449      else if (strcmp (p, "data") == 0)
44450	{
44451	  cp_lexer_consume_token (parser->lexer);
44452	  cp_parser_omp_target_data (parser, pragma_tok, if_p);
44453	  return true;
44454	}
44455      else if (strcmp (p, "enter") == 0)
44456	{
44457	  cp_lexer_consume_token (parser->lexer);
44458	  return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
44459	}
44460      else if (strcmp (p, "exit") == 0)
44461	{
44462	  cp_lexer_consume_token (parser->lexer);
44463	  return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
44464	}
44465      else if (strcmp (p, "update") == 0)
44466	{
44467	  cp_lexer_consume_token (parser->lexer);
44468	  return cp_parser_omp_target_update (parser, pragma_tok, context);
44469	}
44470    }
44471  if (!flag_openmp)  /* flag_openmp_simd  */
44472    {
44473      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44474      return false;
44475    }
44476
44477  tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
44478					    "#pragma omp target", pragma_tok,
44479					    false);
44480  for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
44481    if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44482      {
44483	tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
44484	OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
44485	OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
44486	OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
44487	OMP_CLAUSE_CHAIN (c) = nc;
44488      }
44489  clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
44490
44491  c_omp_adjust_map_clauses (clauses, true);
44492  keep_next_level (true);
44493  tree body = cp_parser_omp_structured_block (parser, if_p);
44494
44495  finish_omp_target (pragma_tok->location, clauses, body, false);
44496  return true;
44497}
44498
44499/* OpenACC 2.0:
44500   # pragma acc cache (variable-list) new-line
44501*/
44502
44503static tree
44504cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
44505{
44506  /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
44507     clauses.  */
44508  auto_suppress_location_wrappers sentinel;
44509
44510  tree stmt, clauses;
44511
44512  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
44513  clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44514
44515  cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
44516
44517  stmt = make_node (OACC_CACHE);
44518  TREE_TYPE (stmt) = void_type_node;
44519  OACC_CACHE_CLAUSES (stmt) = clauses;
44520  SET_EXPR_LOCATION (stmt, pragma_tok->location);
44521  add_stmt (stmt);
44522
44523  return stmt;
44524}
44525
44526/* OpenACC 2.0:
44527   # pragma acc data oacc-data-clause[optseq] new-line
44528     structured-block  */
44529
44530#define OACC_DATA_CLAUSE_MASK						\
44531	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44532	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44533	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44534	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44535	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44536	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH)		\
44537	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44538	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44539	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
44540	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44541
44542static tree
44543cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44544{
44545  tree stmt, clauses, block;
44546  unsigned int save;
44547
44548  clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
44549					"#pragma acc data", pragma_tok);
44550
44551  block = begin_omp_parallel ();
44552  save = cp_parser_begin_omp_structured_block (parser);
44553  cp_parser_statement (parser, NULL_TREE, false, if_p);
44554  cp_parser_end_omp_structured_block (parser, save);
44555  stmt = finish_oacc_data (clauses, block);
44556  return stmt;
44557}
44558
44559/* OpenACC 2.0:
44560  # pragma acc host_data <clauses> new-line
44561  structured-block  */
44562
44563#define OACC_HOST_DATA_CLAUSE_MASK					\
44564  ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE)                \
44565   | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                        \
44566   | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
44567
44568static tree
44569cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44570{
44571  tree stmt, clauses, block;
44572  unsigned int save;
44573
44574  clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
44575					"#pragma acc host_data", pragma_tok);
44576
44577  block = begin_omp_parallel ();
44578  save = cp_parser_begin_omp_structured_block (parser);
44579  cp_parser_statement (parser, NULL_TREE, false, if_p);
44580  cp_parser_end_omp_structured_block (parser, save);
44581  stmt = finish_oacc_host_data (clauses, block);
44582  return stmt;
44583}
44584
44585/* OpenACC 2.0:
44586   # pragma acc declare oacc-data-clause[optseq] new-line
44587*/
44588
44589#define OACC_DECLARE_CLAUSE_MASK					\
44590	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44591	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44592	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44593	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44594	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44595	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)	\
44596	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)		\
44597	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44598
44599static tree
44600cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
44601{
44602  tree clauses, stmt;
44603  bool error = false;
44604  bool found_in_scope = global_bindings_p ();
44605
44606  clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
44607					"#pragma acc declare", pragma_tok, true);
44608
44609
44610  if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44611    {
44612      error_at (pragma_tok->location,
44613		"no valid clauses specified in %<#pragma acc declare%>");
44614      return NULL_TREE;
44615    }
44616
44617  for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
44618    {
44619      location_t loc = OMP_CLAUSE_LOCATION (t);
44620      tree decl = OMP_CLAUSE_DECL (t);
44621      if (!DECL_P (decl))
44622	{
44623	  error_at (loc, "array section in %<#pragma acc declare%>");
44624	  error = true;
44625	  continue;
44626	}
44627      gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
44628      switch (OMP_CLAUSE_MAP_KIND (t))
44629	{
44630	case GOMP_MAP_FIRSTPRIVATE_POINTER:
44631	case GOMP_MAP_ALLOC:
44632	case GOMP_MAP_TO:
44633	case GOMP_MAP_FORCE_DEVICEPTR:
44634	case GOMP_MAP_DEVICE_RESIDENT:
44635	  break;
44636
44637	case GOMP_MAP_LINK:
44638	  if (!global_bindings_p ()
44639	      && (TREE_STATIC (decl)
44640	       || !DECL_EXTERNAL (decl)))
44641	    {
44642	      error_at (loc,
44643			"%qD must be a global variable in "
44644			"%<#pragma acc declare link%>",
44645			decl);
44646	      error = true;
44647	      continue;
44648	    }
44649	  break;
44650
44651	default:
44652	  if (global_bindings_p ())
44653	    {
44654	      error_at (loc, "invalid OpenACC clause at file scope");
44655	      error = true;
44656	      continue;
44657	    }
44658	  if (DECL_EXTERNAL (decl))
44659	    {
44660	      error_at (loc,
44661			"invalid use of %<extern%> variable %qD "
44662			"in %<#pragma acc declare%>", decl);
44663	      error = true;
44664	      continue;
44665	    }
44666	  else if (TREE_PUBLIC (decl))
44667	    {
44668	      error_at (loc,
44669			"invalid use of %<global%> variable %qD "
44670			"in %<#pragma acc declare%>", decl);
44671	      error = true;
44672	      continue;
44673	    }
44674	  break;
44675	}
44676
44677      if (!found_in_scope)
44678	/* This seems to ignore the existence of cleanup scopes?
44679	   What is the meaning for local extern decls?  The local
44680	   extern is in this scope, but it is referring to a decl that
44681	   is namespace scope.  */
44682	for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
44683	  if (d == decl)
44684	    {
44685	      found_in_scope = true;
44686	      break;
44687	    }
44688      if (!found_in_scope)
44689	{
44690	  error_at (loc,
44691		    "%qD must be a variable declared in the same scope as "
44692		    "%<#pragma acc declare%>", decl);
44693	  error = true;
44694	  continue;
44695	}
44696
44697      if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
44698	  || lookup_attribute ("omp declare target link",
44699			       DECL_ATTRIBUTES (decl)))
44700	{
44701	  error_at (loc, "variable %qD used more than once with "
44702		    "%<#pragma acc declare%>", decl);
44703	  error = true;
44704	  continue;
44705	}
44706
44707      if (!error)
44708	{
44709	  tree id;
44710
44711	  if (DECL_LOCAL_DECL_P (decl))
44712	    /* We need to mark the aliased decl, as that is the entity
44713	       that is being referred to.  This won't work for
44714	       dependent variables, but it didn't work for them before
44715	       DECL_LOCAL_DECL_P was a thing either.  But then
44716	       dependent local extern variable decls are as rare as
44717	       hen's teeth.  */
44718	    if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
44719	      if (alias != error_mark_node)
44720		decl = alias;
44721
44722	  if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
44723	    id = get_identifier ("omp declare target link");
44724	  else
44725	    id = get_identifier ("omp declare target");
44726
44727	  DECL_ATTRIBUTES (decl)
44728	    = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
44729	  if (current_binding_level->kind == sk_namespace)
44730	    {
44731	      symtab_node *node = symtab_node::get (decl);
44732	      if (node != NULL)
44733		{
44734		  node->offloadable = 1;
44735		  if (ENABLE_OFFLOADING)
44736		    {
44737		      g->have_offload = true;
44738		      if (is_a <varpool_node *> (node))
44739			vec_safe_push (offload_vars, decl);
44740		    }
44741		}
44742	    }
44743	}
44744    }
44745
44746  if (error || current_binding_level->kind == sk_namespace)
44747    return NULL_TREE;
44748
44749  stmt = make_node (OACC_DECLARE);
44750  TREE_TYPE (stmt) = void_type_node;
44751  OACC_DECLARE_CLAUSES (stmt) = clauses;
44752  SET_EXPR_LOCATION (stmt, pragma_tok->location);
44753
44754  add_stmt (stmt);
44755
44756  return NULL_TREE;
44757}
44758
44759/* OpenACC 2.0:
44760   # pragma acc enter data oacc-enter-data-clause[optseq] new-line
44761
44762   or
44763
44764   # pragma acc exit data oacc-exit-data-clause[optseq] new-line
44765
44766   LOC is the location of the #pragma token.
44767*/
44768
44769#define OACC_ENTER_DATA_CLAUSE_MASK					\
44770	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44771	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44772	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44773	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44774	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44775	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44776
44777#define OACC_EXIT_DATA_CLAUSE_MASK					\
44778	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44779	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44780	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44781	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) 		\
44782	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH)		\
44783	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) 		\
44784	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44785
44786static tree
44787cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
44788				bool enter)
44789{
44790  location_t loc = pragma_tok->location;
44791  tree stmt, clauses;
44792  const char *p = "";
44793
44794  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44795    p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
44796
44797  if (strcmp (p, "data") != 0)
44798    {
44799      error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
44800		enter ? "enter" : "exit");
44801      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44802      return NULL_TREE;
44803    }
44804
44805  cp_lexer_consume_token (parser->lexer);
44806
44807  if (enter)
44808    clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
44809					 "#pragma acc enter data", pragma_tok);
44810  else
44811    clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
44812					 "#pragma acc exit data", pragma_tok);
44813
44814  if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44815    {
44816      error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
44817		enter ? "enter" : "exit");
44818      return NULL_TREE;
44819    }
44820
44821  stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
44822  TREE_TYPE (stmt) = void_type_node;
44823  OMP_STANDALONE_CLAUSES (stmt) = clauses;
44824  SET_EXPR_LOCATION (stmt, loc);
44825  add_stmt (stmt);
44826  return stmt;
44827}
44828
44829/* OpenACC 2.0:
44830   # pragma acc loop oacc-loop-clause[optseq] new-line
44831     structured-block  */
44832
44833#define OACC_LOOP_CLAUSE_MASK						\
44834	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)		\
44835	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
44836	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
44837	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
44838	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
44839	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
44840	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)		\
44841	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)		\
44842	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)			\
44843	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
44844
44845static tree
44846cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
44847		     omp_clause_mask mask, tree *cclauses, bool *if_p)
44848{
44849  bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
44850
44851  strcat (p_name, " loop");
44852  mask |= OACC_LOOP_CLAUSE_MASK;
44853
44854  tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
44855					     cclauses == NULL);
44856  if (cclauses)
44857    {
44858      clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
44859      if (*cclauses)
44860	*cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
44861      if (clauses)
44862	clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44863    }
44864
44865  tree block = begin_omp_structured_block ();
44866  int save = cp_parser_begin_omp_structured_block (parser);
44867  tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
44868  cp_parser_end_omp_structured_block (parser, save);
44869  add_stmt (finish_omp_structured_block (block));
44870
44871  return stmt;
44872}
44873
44874/* OpenACC 2.0:
44875   # pragma acc kernels oacc-kernels-clause[optseq] new-line
44876     structured-block
44877
44878   or
44879
44880   # pragma acc parallel oacc-parallel-clause[optseq] new-line
44881     structured-block
44882
44883   OpenACC 2.6:
44884
44885   # pragma acc serial oacc-serial-clause[optseq] new-line
44886*/
44887
44888#define OACC_KERNELS_CLAUSE_MASK					\
44889	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44890	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44891	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44892	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44893	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44894	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44895	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
44896	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44897	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44898	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
44899	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
44900	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
44901	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
44902	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)	\
44903	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44904
44905#define OACC_PARALLEL_CLAUSE_MASK					\
44906	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44907	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44908	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44909	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44910	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44911	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44912	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
44913	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44914	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)	\
44915	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44916	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
44917	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
44918	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
44919	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
44920	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
44921	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
44922	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
44923	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44924
44925#define OACC_SERIAL_CLAUSE_MASK						\
44926	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44927	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44928	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44929	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44930	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44931	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44932	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
44933	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44934	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44935	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
44936	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
44937	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)	\
44938	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
44939	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
44940	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44941
44942static tree
44943cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
44944			char *p_name, bool *if_p)
44945{
44946  omp_clause_mask mask;
44947  enum tree_code code;
44948  switch (cp_parser_pragma_kind (pragma_tok))
44949    {
44950    case PRAGMA_OACC_KERNELS:
44951      strcat (p_name, " kernels");
44952      mask = OACC_KERNELS_CLAUSE_MASK;
44953      code = OACC_KERNELS;
44954      break;
44955    case PRAGMA_OACC_PARALLEL:
44956      strcat (p_name, " parallel");
44957      mask = OACC_PARALLEL_CLAUSE_MASK;
44958      code = OACC_PARALLEL;
44959      break;
44960    case PRAGMA_OACC_SERIAL:
44961      strcat (p_name, " serial");
44962      mask = OACC_SERIAL_CLAUSE_MASK;
44963      code = OACC_SERIAL;
44964      break;
44965    default:
44966      gcc_unreachable ();
44967    }
44968
44969  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44970    {
44971      const char *p
44972	= IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
44973      if (strcmp (p, "loop") == 0)
44974	{
44975	  cp_lexer_consume_token (parser->lexer);
44976	  tree block = begin_omp_parallel ();
44977	  tree clauses;
44978	  tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
44979					   &clauses, if_p);
44980	  protected_set_expr_location (stmt, pragma_tok->location);
44981	  return finish_omp_construct (code, block, clauses);
44982	}
44983    }
44984
44985  tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
44986
44987  tree block = begin_omp_parallel ();
44988  unsigned int save = cp_parser_begin_omp_structured_block (parser);
44989  cp_parser_statement (parser, NULL_TREE, false, if_p);
44990  cp_parser_end_omp_structured_block (parser, save);
44991  return finish_omp_construct (code, block, clauses);
44992}
44993
44994/* OpenACC 2.0:
44995   # pragma acc update oacc-update-clause[optseq] new-line
44996*/
44997
44998#define OACC_UPDATE_CLAUSE_MASK						\
44999	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
45000	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)		\
45001	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)		\
45002	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
45003	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT)		\
45004	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
45005
45006static tree
45007cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
45008{
45009  tree stmt, clauses;
45010
45011  clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
45012					 "#pragma acc update", pragma_tok);
45013
45014  if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45015    {
45016      error_at (pragma_tok->location,
45017		"%<#pragma acc update%> must contain at least one "
45018		"%<device%> or %<host%> or %<self%> clause");
45019      return NULL_TREE;
45020    }
45021
45022  stmt = make_node (OACC_UPDATE);
45023  TREE_TYPE (stmt) = void_type_node;
45024  OACC_UPDATE_CLAUSES (stmt) = clauses;
45025  SET_EXPR_LOCATION (stmt, pragma_tok->location);
45026  add_stmt (stmt);
45027  return stmt;
45028}
45029
45030/* OpenACC 2.0:
45031   # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
45032
45033   LOC is the location of the #pragma token.
45034*/
45035
45036#define OACC_WAIT_CLAUSE_MASK					\
45037	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
45038
45039static tree
45040cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
45041{
45042  tree clauses, list = NULL_TREE, stmt = NULL_TREE;
45043  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45044
45045  if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
45046    list = cp_parser_oacc_wait_list (parser, loc, list);
45047
45048  clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
45049					"#pragma acc wait", pragma_tok);
45050
45051  stmt = c_finish_oacc_wait (loc, list, clauses);
45052  stmt = finish_expr_stmt (stmt);
45053
45054  return stmt;
45055}
45056
45057/* OpenMP 4.0:
45058   # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
45059
45060#define OMP_DECLARE_SIMD_CLAUSE_MASK				\
45061	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
45062	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
45063	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
45064	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)	\
45065	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)	\
45066	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
45067
45068static void
45069cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
45070			    enum pragma_context context,
45071			    bool variant_p)
45072{
45073  bool first_p = parser->omp_declare_simd == NULL;
45074  cp_omp_declare_simd_data data;
45075  if (first_p)
45076    {
45077      data.error_seen = false;
45078      data.fndecl_seen = false;
45079      data.variant_p = variant_p;
45080      data.tokens = vNULL;
45081      data.attribs[0] = NULL;
45082      data.attribs[1] = NULL;
45083      data.loc = UNKNOWN_LOCATION;
45084      /* It is safe to take the address of a local variable; it will only be
45085	 used while this scope is live.  */
45086      parser->omp_declare_simd = &data;
45087    }
45088  else if (parser->omp_declare_simd->variant_p != variant_p)
45089    {
45090      error_at (pragma_tok->location,
45091		"%<#pragma omp declare %s%> followed by "
45092		"%<#pragma omp declare %s%>",
45093		parser->omp_declare_simd->variant_p ? "variant" : "simd",
45094		parser->omp_declare_simd->variant_p ? "simd" : "variant");
45095      parser->omp_declare_simd->error_seen = true;
45096    }
45097
45098  /* Store away all pragma tokens.  */
45099  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
45100    cp_lexer_consume_token (parser->lexer);
45101  cp_parser_require_pragma_eol (parser, pragma_tok);
45102  struct cp_token_cache *cp
45103    = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
45104  parser->omp_declare_simd->tokens.safe_push (cp);
45105
45106  if (first_p)
45107    {
45108      while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
45109	cp_parser_pragma (parser, context, NULL);
45110      switch (context)
45111	{
45112	case pragma_external:
45113	  cp_parser_declaration (parser, NULL_TREE);
45114	  break;
45115	case pragma_member:
45116	  cp_parser_member_declaration (parser);
45117	  break;
45118	case pragma_objc_icode:
45119	  cp_parser_block_declaration (parser, /*statement_p=*/false);
45120	  break;
45121	default:
45122	  cp_parser_declaration_statement (parser);
45123	  break;
45124	}
45125      if (parser->omp_declare_simd
45126	  && !parser->omp_declare_simd->error_seen
45127	  && !parser->omp_declare_simd->fndecl_seen)
45128	error_at (pragma_tok->location,
45129		  "%<#pragma omp declare %s%> not immediately followed by "
45130		  "function declaration or definition",
45131		  parser->omp_declare_simd->variant_p ? "variant" : "simd");
45132      data.tokens.release ();
45133      parser->omp_declare_simd = NULL;
45134    }
45135}
45136
45137static const char *const omp_construct_selectors[] = {
45138  "simd", "target", "teams", "parallel", "for", NULL };
45139static const char *const omp_device_selectors[] = {
45140  "kind", "isa", "arch", NULL };
45141static const char *const omp_implementation_selectors[] = {
45142  "vendor", "extension", "atomic_default_mem_order", "unified_address",
45143  "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
45144static const char *const omp_user_selectors[] = {
45145  "condition", NULL };
45146
45147/* OpenMP 5.0:
45148
45149   trait-selector:
45150     trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
45151
45152   trait-score:
45153     score(score-expression)  */
45154
45155static tree
45156cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
45157{
45158  tree ret = NULL_TREE;
45159  do
45160    {
45161      tree selector;
45162      if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45163	  || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45164	selector = cp_lexer_peek_token (parser->lexer)->u.value;
45165      else
45166	{
45167	  cp_parser_error (parser, "expected trait selector name");
45168	  return error_mark_node;
45169	}
45170
45171      tree properties = NULL_TREE;
45172      const char *const *selectors = NULL;
45173      bool allow_score = true;
45174      bool allow_user = false;
45175      int property_limit = 0;
45176      enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
45177	     CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
45178	     CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
45179      switch (IDENTIFIER_POINTER (set)[0])
45180	{
45181	case 'c': /* construct */
45182	  selectors = omp_construct_selectors;
45183	  allow_score = false;
45184	  property_limit = 1;
45185	  property_kind = CTX_PROPERTY_SIMD;
45186	  break;
45187	case 'd': /* device */
45188	  selectors = omp_device_selectors;
45189	  allow_score = false;
45190	  allow_user = true;
45191	  property_limit = 3;
45192	  property_kind = CTX_PROPERTY_NAME_LIST;
45193	  break;
45194	case 'i': /* implementation */
45195	  selectors = omp_implementation_selectors;
45196	  allow_user = true;
45197	  property_limit = 3;
45198	  property_kind = CTX_PROPERTY_NAME_LIST;
45199	  break;
45200	case 'u': /* user */
45201	  selectors = omp_user_selectors;
45202	  property_limit = 1;
45203	  property_kind = CTX_PROPERTY_EXPR;
45204	  break;
45205	default:
45206	  gcc_unreachable ();
45207	}
45208      for (int i = 0; ; i++)
45209	{
45210	  if (selectors[i] == NULL)
45211	    {
45212	      if (allow_user)
45213		{
45214		  property_kind = CTX_PROPERTY_USER;
45215		  break;
45216		}
45217	      else
45218		{
45219		  error ("selector %qs not allowed for context selector "
45220			 "set %qs", IDENTIFIER_POINTER (selector),
45221			 IDENTIFIER_POINTER (set));
45222		  cp_lexer_consume_token (parser->lexer);
45223		  return error_mark_node;
45224		}
45225	    }
45226	  if (i == property_limit)
45227	    property_kind = CTX_PROPERTY_NONE;
45228	  if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
45229	    break;
45230	}
45231      if (property_kind == CTX_PROPERTY_NAME_LIST
45232	  && IDENTIFIER_POINTER (set)[0] == 'i'
45233	  && strcmp (IDENTIFIER_POINTER (selector),
45234		     "atomic_default_mem_order") == 0)
45235	property_kind = CTX_PROPERTY_ID;
45236
45237      cp_lexer_consume_token (parser->lexer);
45238
45239      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45240	{
45241	  if (property_kind == CTX_PROPERTY_NONE)
45242	    {
45243	      error ("selector %qs does not accept any properties",
45244		     IDENTIFIER_POINTER (selector));
45245	      return error_mark_node;
45246	    }
45247
45248	  matching_parens parens;
45249	  parens.consume_open (parser);
45250
45251	  cp_token *token = cp_lexer_peek_token (parser->lexer);
45252	  if (allow_score
45253	      && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45254	      && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
45255	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
45256	    {
45257	      cp_lexer_save_tokens (parser->lexer);
45258	      cp_lexer_consume_token (parser->lexer);
45259	      cp_lexer_consume_token (parser->lexer);
45260	      if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
45261							 true)
45262		  && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
45263		{
45264		  cp_lexer_rollback_tokens (parser->lexer);
45265		  cp_lexer_consume_token (parser->lexer);
45266
45267		  matching_parens parens2;
45268		  parens2.require_open (parser);
45269		  tree score = cp_parser_constant_expression (parser);
45270		  if (!parens2.require_close (parser))
45271		    cp_parser_skip_to_closing_parenthesis (parser, true,
45272							   false, true);
45273		  cp_parser_require (parser, CPP_COLON, RT_COLON);
45274		  if (score != error_mark_node)
45275		    {
45276		      score = fold_non_dependent_expr (score);
45277		      if (value_dependent_expression_p (score))
45278			properties = tree_cons (get_identifier (" score"),
45279						score, properties);
45280		      else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
45281			       || TREE_CODE (score) != INTEGER_CST)
45282			error_at (token->location, "score argument must be "
45283				  "constant integer expression");
45284		      else if (tree_int_cst_sgn (score) < 0)
45285			error_at (token->location, "score argument must be "
45286				  "non-negative");
45287		      else
45288			properties = tree_cons (get_identifier (" score"),
45289						score, properties);
45290		    }
45291		}
45292	      else
45293		cp_lexer_rollback_tokens (parser->lexer);
45294
45295	      token = cp_lexer_peek_token (parser->lexer);
45296	    }
45297
45298	  switch (property_kind)
45299	    {
45300	      tree t;
45301	    case CTX_PROPERTY_USER:
45302	      do
45303		{
45304		  t = cp_parser_constant_expression (parser);
45305		  if (t != error_mark_node)
45306		    {
45307		      t = fold_non_dependent_expr (t);
45308		      if (TREE_CODE (t) == STRING_CST)
45309			properties = tree_cons (NULL_TREE, t, properties);
45310		      else if (!value_dependent_expression_p (t)
45311			       && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45312				   || !tree_fits_shwi_p (t)))
45313			error_at (token->location, "property must be "
45314				  "constant integer expression or string "
45315				  "literal");
45316		      else
45317			properties = tree_cons (NULL_TREE, t, properties);
45318		    }
45319		  else
45320		    return error_mark_node;
45321
45322		  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45323		    cp_lexer_consume_token (parser->lexer);
45324		  else
45325		    break;
45326		}
45327	      while (1);
45328	      break;
45329	    case CTX_PROPERTY_ID:
45330	      if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45331		  || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45332		{
45333		  tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
45334		  cp_lexer_consume_token (parser->lexer);
45335		  properties = tree_cons (prop, NULL_TREE, properties);
45336		}
45337	      else
45338		{
45339		  cp_parser_error (parser, "expected identifier");
45340		  return error_mark_node;
45341		}
45342	      break;
45343	    case CTX_PROPERTY_NAME_LIST:
45344	      do
45345		{
45346		  tree prop = NULL_TREE, value = NULL_TREE;
45347		  if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45348		      || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45349		    {
45350		      prop = cp_lexer_peek_token (parser->lexer)->u.value;
45351		      cp_lexer_consume_token (parser->lexer);
45352		    }
45353		  else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
45354		    value = cp_parser_string_literal (parser, false, false);
45355		  else
45356		    {
45357		      cp_parser_error (parser, "expected identifier or "
45358					       "string literal");
45359		      return error_mark_node;
45360		    }
45361
45362		  properties = tree_cons (prop, value, properties);
45363
45364		  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45365		    cp_lexer_consume_token (parser->lexer);
45366		  else
45367		    break;
45368		}
45369	      while (1);
45370	      break;
45371	    case CTX_PROPERTY_EXPR:
45372	      t = cp_parser_constant_expression (parser);
45373	      if (t != error_mark_node)
45374		{
45375		  t = fold_non_dependent_expr (t);
45376		  if (!value_dependent_expression_p (t)
45377		      && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45378			  || !tree_fits_shwi_p (t)))
45379		    error_at (token->location, "property must be "
45380			      "constant integer expression");
45381		  else
45382		    properties = tree_cons (NULL_TREE, t, properties);
45383		}
45384	      else
45385		return error_mark_node;
45386	      break;
45387	    case CTX_PROPERTY_SIMD:
45388	      if (!has_parms_p)
45389		{
45390		  error_at (token->location, "properties for %<simd%> "
45391			    "selector may not be specified in "
45392			    "%<metadirective%>");
45393		  return error_mark_node;
45394		}
45395	      properties
45396		= cp_parser_omp_all_clauses (parser,
45397					     OMP_DECLARE_SIMD_CLAUSE_MASK,
45398					     "simd", NULL, true, 2);
45399	      break;
45400	    default:
45401	      gcc_unreachable ();
45402	    }
45403
45404	  if (!parens.require_close (parser))
45405	    cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
45406
45407	  properties = nreverse (properties);
45408	}
45409      else if (property_kind == CTX_PROPERTY_NAME_LIST
45410	       || property_kind == CTX_PROPERTY_ID
45411	       || property_kind == CTX_PROPERTY_EXPR)
45412	{
45413	  cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
45414	  return error_mark_node;
45415	}
45416
45417      ret = tree_cons (selector, properties, ret);
45418
45419      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45420	cp_lexer_consume_token (parser->lexer);
45421      else
45422	break;
45423    }
45424  while (1);
45425
45426  return nreverse (ret);
45427}
45428
45429/* OpenMP 5.0:
45430
45431   trait-set-selector[,trait-set-selector[,...]]
45432
45433   trait-set-selector:
45434     trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
45435
45436   trait-set-selector-name:
45437     constructor
45438     device
45439     implementation
45440     user  */
45441
45442static tree
45443cp_parser_omp_context_selector_specification (cp_parser *parser,
45444					      bool has_parms_p)
45445{
45446  tree ret = NULL_TREE;
45447  do
45448    {
45449      const char *setp = "";
45450      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45451	setp
45452	  = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45453      switch (setp[0])
45454	{
45455	case 'c':
45456	  if (strcmp (setp, "construct") == 0)
45457	    setp = NULL;
45458	  break;
45459	case 'd':
45460	  if (strcmp (setp, "device") == 0)
45461	    setp = NULL;
45462	  break;
45463	case 'i':
45464	  if (strcmp (setp, "implementation") == 0)
45465	    setp = NULL;
45466	  break;
45467	case 'u':
45468	  if (strcmp (setp, "user") == 0)
45469	    setp = NULL;
45470	  break;
45471	default:
45472	  break;
45473	}
45474      if (setp)
45475	{
45476	  cp_parser_error (parser, "expected %<construct%>, %<device%>, "
45477				   "%<implementation%> or %<user%>");
45478	  return error_mark_node;
45479	}
45480
45481      tree set = cp_lexer_peek_token (parser->lexer)->u.value;
45482      cp_lexer_consume_token (parser->lexer);
45483
45484      if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
45485	return error_mark_node;
45486
45487      matching_braces braces;
45488      if (!braces.require_open (parser))
45489	return error_mark_node;
45490
45491      tree selectors
45492	= cp_parser_omp_context_selector (parser, set, has_parms_p);
45493      if (selectors == error_mark_node)
45494	{
45495	  cp_parser_skip_to_closing_brace (parser);
45496	  ret = error_mark_node;
45497	}
45498      else if (ret != error_mark_node)
45499	ret = tree_cons (set, selectors, ret);
45500
45501      braces.require_close (parser);
45502
45503      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45504	cp_lexer_consume_token (parser->lexer);
45505      else
45506	break;
45507    }
45508  while (1);
45509
45510  if (ret == error_mark_node)
45511    return ret;
45512  return nreverse (ret);
45513}
45514
45515/* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
45516   that into "omp declare variant base" attribute.  */
45517
45518static tree
45519cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
45520			       tree attrs)
45521{
45522  matching_parens parens;
45523  if (!parens.require_open (parser))
45524    {
45525     fail:
45526      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45527      return attrs;
45528    }
45529
45530  bool template_p;
45531  cp_id_kind idk = CP_ID_KIND_NONE;
45532  cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
45533  cp_expr varid
45534    = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
45535			       /*check_dependency_p=*/true,
45536			       /*template_p=*/&template_p,
45537			       /*declarator_p=*/false,
45538			       /*optional_p=*/false);
45539  parens.require_close (parser);
45540
45541  tree variant;
45542  if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
45543      || TREE_CODE (varid) == TYPE_DECL
45544      || varid == error_mark_node)
45545    variant = varid;
45546  else if (varid_token->type == CPP_NAME && varid_token->error_reported)
45547    variant = NULL_TREE;
45548  else
45549    {
45550      tree ambiguous_decls;
45551      variant = cp_parser_lookup_name (parser, varid, none_type,
45552				       template_p, /*is_namespace=*/false,
45553				       /*check_dependency=*/true,
45554				       &ambiguous_decls,
45555				       varid.get_location ());
45556      if (ambiguous_decls)
45557	variant = NULL_TREE;
45558    }
45559  if (variant == NULL_TREE)
45560    variant = error_mark_node;
45561  else if (TREE_CODE (variant) != SCOPE_REF)
45562    {
45563      const char *error_msg;
45564      variant
45565	= finish_id_expression (varid, variant, parser->scope,
45566				&idk, false, true,
45567				&parser->non_integral_constant_expression_p,
45568				template_p, true, false, false, &error_msg,
45569				varid.get_location ());
45570      if (error_msg)
45571	cp_parser_error (parser, error_msg);
45572    }
45573  location_t caret_loc = get_pure_location (varid.get_location ());
45574  location_t start_loc = get_start (varid_token->location);
45575  location_t finish_loc = get_finish (varid.get_location ());
45576  location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
45577
45578  /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
45579  if (parser->lexer->in_omp_attribute_pragma
45580      && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45581      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45582    cp_lexer_consume_token (parser->lexer);
45583
45584  const char *clause = "";
45585  location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
45586  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45587    clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45588  if (strcmp (clause, "match"))
45589    {
45590      cp_parser_error (parser, "expected %<match%>");
45591      goto fail;
45592    }
45593
45594  cp_lexer_consume_token (parser->lexer);
45595
45596  if (!parens.require_open (parser))
45597    goto fail;
45598
45599  tree ctx = cp_parser_omp_context_selector_specification (parser, true);
45600  if (ctx == error_mark_node)
45601    goto fail;
45602  ctx = omp_check_context_selector (match_loc, ctx);
45603  if (ctx != error_mark_node && variant != error_mark_node)
45604    {
45605      tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
45606						      match_loc);
45607      tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
45608      loc_node = tree_cons (match_loc_node,
45609			    build_int_cst (integer_type_node, idk),
45610			    build_tree_list (loc_node, integer_zero_node));
45611      attrs = tree_cons (get_identifier ("omp declare variant base"),
45612			 tree_cons (variant, ctx, loc_node), attrs);
45613      if (processing_template_decl)
45614	ATTR_IS_DEPENDENT (attrs) = 1;
45615    }
45616
45617  parens.require_close (parser);
45618  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45619  return attrs;
45620}
45621
45622
45623/* Finalize #pragma omp declare simd clauses after direct declarator has
45624   been parsed, and put that into "omp declare simd" attribute.  */
45625
45626static tree
45627cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
45628{
45629  struct cp_token_cache *ce;
45630  cp_omp_declare_simd_data *data = parser->omp_declare_simd;
45631  int i;
45632
45633  if (!data->error_seen && data->fndecl_seen)
45634    {
45635      error ("%<#pragma omp declare %s%> not immediately followed by "
45636	     "a single function declaration or definition",
45637	     data->variant_p ? "variant" : "simd");
45638      data->error_seen = true;
45639    }
45640  if (data->error_seen)
45641    return attrs;
45642
45643  FOR_EACH_VEC_ELT (data->tokens, i, ce)
45644    {
45645      tree c, cl;
45646
45647      cp_parser_push_lexer_for_tokens (parser, ce);
45648      parser->lexer->in_pragma = true;
45649      gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
45650      cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45651      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45652      const char *kind = IDENTIFIER_POINTER (id);
45653      cp_lexer_consume_token (parser->lexer);
45654      if (strcmp (kind, "simd") == 0)
45655	{
45656	  /* For now only in C++ attributes, do it always for OpenMP 5.1.
45657	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45658	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45659	    cp_lexer_consume_token (parser->lexer);  */
45660
45661	  cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
45662					  "#pragma omp declare simd",
45663					  pragma_tok);
45664	  if (cl)
45665	    cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45666	  c = build_tree_list (get_identifier ("omp declare simd"), cl);
45667	  TREE_CHAIN (c) = attrs;
45668	  if (processing_template_decl)
45669	    ATTR_IS_DEPENDENT (c) = 1;
45670	  attrs = c;
45671	}
45672      else
45673	{
45674	  gcc_assert (strcmp (kind, "variant") == 0);
45675	  attrs
45676	    = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
45677	}
45678      cp_parser_pop_lexer (parser);
45679    }
45680
45681  cp_lexer *lexer = NULL;
45682  for (int i = 0; i < 2; i++)
45683    {
45684      if (data->attribs[i] == NULL)
45685	continue;
45686      for (tree *pa = data->attribs[i]; *pa; )
45687	if (get_attribute_namespace (*pa) == omp_identifier
45688	    && is_attribute_p ("directive", get_attribute_name (*pa)))
45689	  {
45690	    for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
45691	      {
45692		tree d = TREE_VALUE (a);
45693		gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
45694		cp_token *first = DEFPARSE_TOKENS (d)->first;
45695		cp_token *last = DEFPARSE_TOKENS (d)->last;
45696		const char *directive[3] = {};
45697		for (int j = 0; j < 3; j++)
45698		  {
45699		    tree id = NULL_TREE;
45700		    if (first + j == last)
45701		      break;
45702		    if (first[j].type == CPP_NAME)
45703		      id = first[j].u.value;
45704		    else if (first[j].type == CPP_KEYWORD)
45705		      id = ridpointers[(int) first[j].keyword];
45706		    else
45707		      break;
45708		    directive[j] = IDENTIFIER_POINTER (id);
45709		  }
45710		const c_omp_directive *dir = NULL;
45711		if (directive[0])
45712		  dir = c_omp_categorize_directive (directive[0], directive[1],
45713						    directive[2]);
45714		if (dir == NULL)
45715		  {
45716		    error_at (first->location,
45717			      "unknown OpenMP directive name in "
45718			      "%<omp::directive%> attribute argument");
45719		    continue;
45720		  }
45721		if (dir->id != PRAGMA_OMP_DECLARE
45722		    || (strcmp (directive[1], "simd") != 0
45723			&& strcmp (directive[1], "variant") != 0))
45724		  {
45725		    error_at (first->location,
45726			      "OpenMP directive other than %<declare simd%> "
45727			      "or %<declare variant%> appertains to a "
45728			      "declaration");
45729		    continue;
45730		  }
45731
45732		if (parser->omp_attrs_forbidden_p)
45733		  {
45734		    error_at (first->location,
45735			      "mixing OpenMP directives with attribute and "
45736			      "pragma syntax on the same statement");
45737		    parser->omp_attrs_forbidden_p = false;
45738		  }
45739
45740		if (!flag_openmp && strcmp (directive[1], "simd") != 0)
45741		  continue;
45742		if (lexer == NULL)
45743		  {
45744		    lexer = cp_lexer_alloc ();
45745		    lexer->debugging_p = parser->lexer->debugging_p;
45746		  }
45747		vec_safe_reserve (lexer->buffer, (last - first) + 2);
45748		cp_token tok = {};
45749		tok.type = CPP_PRAGMA;
45750		tok.keyword = RID_MAX;
45751		tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
45752		tok.location = first->location;
45753		lexer->buffer->quick_push (tok);
45754		while (++first < last)
45755		  lexer->buffer->quick_push (*first);
45756		tok = {};
45757		tok.type = CPP_PRAGMA_EOL;
45758		tok.keyword = RID_MAX;
45759		tok.location = last->location;
45760		lexer->buffer->quick_push (tok);
45761		tok = {};
45762		tok.type = CPP_EOF;
45763		tok.keyword = RID_MAX;
45764		tok.location = last->location;
45765		lexer->buffer->quick_push (tok);
45766		lexer->next = parser->lexer;
45767		lexer->next_token = lexer->buffer->address ();
45768		lexer->last_token = lexer->next_token
45769				    + lexer->buffer->length ()
45770		      - 1;
45771		lexer->in_omp_attribute_pragma = true;
45772		parser->lexer = lexer;
45773		/* Move the current source position to that of the first token
45774		   in the new lexer.  */
45775		cp_lexer_set_source_position_from_token (lexer->next_token);
45776
45777		cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45778		tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45779		const char *kind = IDENTIFIER_POINTER (id);
45780		cp_lexer_consume_token (parser->lexer);
45781
45782		tree c, cl;
45783		if (strcmp (kind, "simd") == 0)
45784		  {
45785		    if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45786			&& cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45787		      cp_lexer_consume_token (parser->lexer);
45788
45789		    omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
45790		    cl = cp_parser_omp_all_clauses (parser, mask,
45791						    "#pragma omp declare simd",
45792						    pragma_tok);
45793		    if (cl)
45794		      cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45795		    c = build_tree_list (get_identifier ("omp declare simd"),
45796					 cl);
45797		    TREE_CHAIN (c) = attrs;
45798		    if (processing_template_decl)
45799		      ATTR_IS_DEPENDENT (c) = 1;
45800		    attrs = c;
45801		  }
45802		else
45803		  {
45804		    gcc_assert (strcmp (kind, "variant") == 0);
45805		    attrs
45806		      = cp_finish_omp_declare_variant (parser, pragma_tok,
45807						       attrs);
45808		  }
45809		gcc_assert (parser->lexer != lexer);
45810		vec_safe_truncate (lexer->buffer, 0);
45811	      }
45812	    *pa = TREE_CHAIN (*pa);
45813	  }
45814	else
45815	  pa = &TREE_CHAIN (*pa);
45816    }
45817  if (lexer)
45818    cp_lexer_destroy (lexer);
45819
45820  data->fndecl_seen = true;
45821  return attrs;
45822}
45823
45824/* Helper for cp_parser_omp_declare_target, handle one to or link clause
45825   on #pragma omp declare target.  Return false if errors were reported.  */
45826
45827static bool
45828handle_omp_declare_target_clause (tree c, tree t, int device_type)
45829{
45830  tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
45831  tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
45832  tree id;
45833  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
45834    {
45835      id = get_identifier ("omp declare target link");
45836      std::swap (at1, at2);
45837    }
45838  else
45839    id = get_identifier ("omp declare target");
45840  if (at2)
45841    {
45842      error_at (OMP_CLAUSE_LOCATION (c),
45843		"%qD specified both in declare target %<link%> and %<to%>"
45844		" clauses", t);
45845      return false;
45846    }
45847  if (!at1)
45848    {
45849      DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45850      if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
45851	return true;
45852
45853      symtab_node *node = symtab_node::get (t);
45854      if (node != NULL)
45855	{
45856	  node->offloadable = 1;
45857	  if (ENABLE_OFFLOADING)
45858	    {
45859	      g->have_offload = true;
45860	      if (is_a <varpool_node *> (node))
45861		vec_safe_push (offload_vars, t);
45862	    }
45863	}
45864    }
45865  if (TREE_CODE (t) != FUNCTION_DECL)
45866    return true;
45867  if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
45868    {
45869      tree at3 = lookup_attribute ("omp declare target host",
45870				   DECL_ATTRIBUTES (t));
45871      if (at3 == NULL_TREE)
45872	{
45873	  id = get_identifier ("omp declare target host");
45874	  DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45875	}
45876    }
45877  if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
45878    {
45879      tree at3 = lookup_attribute ("omp declare target nohost",
45880				   DECL_ATTRIBUTES (t));
45881      if (at3 == NULL_TREE)
45882	{
45883	  id = get_identifier ("omp declare target nohost");
45884	  DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45885	}
45886    }
45887  return true;
45888}
45889
45890/* OpenMP 4.0:
45891   # pragma omp declare target new-line
45892   declarations and definitions
45893   # pragma omp end declare target new-line
45894
45895   OpenMP 4.5:
45896   # pragma omp declare target ( extended-list ) new-line
45897
45898   # pragma omp declare target declare-target-clauses[seq] new-line  */
45899
45900#define OMP_DECLARE_TARGET_CLAUSE_MASK				\
45901	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
45902	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)		\
45903	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
45904
45905static void
45906cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
45907{
45908  tree clauses = NULL_TREE;
45909  int device_type = 0;
45910  bool only_device_type = true;
45911  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45912      /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
45913      || (parser->lexer->in_omp_attribute_pragma
45914	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45915	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
45916    clauses
45917      = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
45918				   "#pragma omp declare target", pragma_tok);
45919  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45920    {
45921      clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
45922					clauses);
45923      clauses = finish_omp_clauses (clauses, C_ORT_OMP);
45924      cp_parser_require_pragma_eol (parser, pragma_tok);
45925    }
45926  else
45927    {
45928      struct omp_declare_target_attr a
45929	= { parser->lexer->in_omp_attribute_pragma };
45930      vec_safe_push (scope_chain->omp_declare_target_attribute, a);
45931      cp_parser_require_pragma_eol (parser, pragma_tok);
45932      return;
45933    }
45934  for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45935    if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
45936      device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
45937  for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45938    {
45939      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
45940	continue;
45941      tree t = OMP_CLAUSE_DECL (c);
45942      only_device_type = false;
45943      if (!handle_omp_declare_target_clause (c, t, device_type))
45944	continue;
45945      if (VAR_OR_FUNCTION_DECL_P (t)
45946	  && DECL_LOCAL_DECL_P (t)
45947	  && DECL_LANG_SPECIFIC (t)
45948	  && DECL_LOCAL_DECL_ALIAS (t)
45949	  && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
45950	handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
45951					  device_type);
45952    }
45953  if (device_type && only_device_type)
45954    warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
45955		"directive with only %<device_type%> clauses ignored");
45956}
45957
45958static void
45959cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
45960{
45961  const char *p = "";
45962  bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
45963  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45964    {
45965      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45966      p = IDENTIFIER_POINTER (id);
45967    }
45968  if (strcmp (p, "declare") == 0)
45969    {
45970      cp_lexer_consume_token (parser->lexer);
45971      p = "";
45972      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45973	{
45974	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45975	  p = IDENTIFIER_POINTER (id);
45976	}
45977      if (strcmp (p, "target") == 0)
45978	cp_lexer_consume_token (parser->lexer);
45979      else
45980	{
45981	  cp_parser_error (parser, "expected %<target%>");
45982	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45983	  return;
45984	}
45985    }
45986  else
45987    {
45988      cp_parser_error (parser, "expected %<declare%>");
45989      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45990      return;
45991    }
45992  cp_parser_require_pragma_eol (parser, pragma_tok);
45993  if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
45994    error_at (pragma_tok->location,
45995	      "%<#pragma omp end declare target%> without corresponding "
45996	      "%<#pragma omp declare target%>");
45997  else
45998    {
45999      omp_declare_target_attr
46000	a = scope_chain->omp_declare_target_attribute->pop ();
46001      if (a.attr_syntax != in_omp_attribute_pragma)
46002	{
46003	  if (a.attr_syntax)
46004	    error_at (pragma_tok->location,
46005		      "%<declare target%> in attribute syntax terminated "
46006		      "with %<end declare target%> in pragma syntax");
46007	  else
46008	    error_at (pragma_tok->location,
46009		      "%<declare target%> in pragma syntax terminated "
46010		      "with %<end declare target%> in attribute syntax");
46011	}
46012    }
46013}
46014
46015/* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
46016   expression and optional initializer clause of
46017   #pragma omp declare reduction.  We store the expression(s) as
46018   either 3, 6 or 7 special statements inside of the artificial function's
46019   body.  The first two statements are DECL_EXPRs for the artificial
46020   OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
46021   expression that uses those variables.
46022   If there was any INITIALIZER clause, this is followed by further statements,
46023   the fourth and fifth statements are DECL_EXPRs for the artificial
46024   OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
46025   constructor variant (first token after open paren is not omp_priv),
46026   then the sixth statement is a statement with the function call expression
46027   that uses the OMP_PRIV and optionally OMP_ORIG variable.
46028   Otherwise, the sixth statement is whatever statement cp_finish_decl emits
46029   to initialize the OMP_PRIV artificial variable and there is seventh
46030   statement, a DECL_EXPR of the OMP_PRIV statement again.  */
46031
46032static bool
46033cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
46034{
46035  tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
46036  gcc_assert (TYPE_REF_P (type));
46037  type = TREE_TYPE (type);
46038  tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
46039  DECL_ARTIFICIAL (omp_out) = 1;
46040  pushdecl (omp_out);
46041  add_decl_expr (omp_out);
46042  tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
46043  DECL_ARTIFICIAL (omp_in) = 1;
46044  pushdecl (omp_in);
46045  add_decl_expr (omp_in);
46046  tree combiner;
46047  tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
46048
46049  keep_next_level (true);
46050  tree block = begin_omp_structured_block ();
46051  combiner = cp_parser_expression (parser);
46052  finish_expr_stmt (combiner);
46053  block = finish_omp_structured_block (block);
46054  if (processing_template_decl)
46055    block = build_stmt (input_location, EXPR_STMT, block);
46056  add_stmt (block);
46057
46058  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
46059    return false;
46060
46061  /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
46062  if (parser->lexer->in_omp_attribute_pragma
46063      && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46064      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46065    cp_lexer_consume_token (parser->lexer);
46066
46067  const char *p = "";
46068  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46069    {
46070      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46071      p = IDENTIFIER_POINTER (id);
46072    }
46073
46074  if (strcmp (p, "initializer") == 0)
46075    {
46076      cp_lexer_consume_token (parser->lexer);
46077      matching_parens parens;
46078      if (!parens.require_open (parser))
46079	return false;
46080
46081      p = "";
46082      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46083	{
46084	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46085	  p = IDENTIFIER_POINTER (id);
46086	}
46087
46088      omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
46089      DECL_ARTIFICIAL (omp_priv) = 1;
46090      pushdecl (omp_priv);
46091      add_decl_expr (omp_priv);
46092      omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
46093      DECL_ARTIFICIAL (omp_orig) = 1;
46094      pushdecl (omp_orig);
46095      add_decl_expr (omp_orig);
46096
46097      keep_next_level (true);
46098      block = begin_omp_structured_block ();
46099
46100      bool ctor = false;
46101      if (strcmp (p, "omp_priv") == 0)
46102	{
46103	  bool is_direct_init, is_non_constant_init;
46104	  ctor = true;
46105	  cp_lexer_consume_token (parser->lexer);
46106	  /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
46107	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
46108	      || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
46109		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
46110		     == CPP_CLOSE_PAREN
46111		  && cp_lexer_peek_nth_token (parser->lexer, 3)->type
46112		     == CPP_CLOSE_PAREN))
46113	    {
46114	      finish_omp_structured_block (block);
46115	      error ("invalid initializer clause");
46116	      return false;
46117	    }
46118	  initializer = cp_parser_initializer (parser, &is_direct_init,
46119					       &is_non_constant_init);
46120	  cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
46121			  NULL_TREE, LOOKUP_ONLYCONVERTING);
46122	}
46123      else
46124	{
46125	  cp_parser_parse_tentatively (parser);
46126	  /* Don't create location wrapper nodes here.  */
46127	  auto_suppress_location_wrappers sentinel;
46128	  tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
46129						  /*check_dependency_p=*/true,
46130						  /*template_p=*/NULL,
46131						  /*declarator_p=*/false,
46132						  /*optional_p=*/false);
46133	  vec<tree, va_gc> *args;
46134	  if (fn_name == error_mark_node
46135	      || cp_parser_error_occurred (parser)
46136	      || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
46137	      || ((args = cp_parser_parenthesized_expression_list
46138				(parser, non_attr, /*cast_p=*/false,
46139				 /*allow_expansion_p=*/true,
46140				 /*non_constant_p=*/NULL)),
46141		  cp_parser_error_occurred (parser)))
46142	    {
46143	      finish_omp_structured_block (block);
46144	      cp_parser_abort_tentative_parse (parser);
46145	      cp_parser_error (parser, "expected id-expression (arguments)");
46146	      return false;
46147	    }
46148	  unsigned int i;
46149	  tree arg;
46150	  FOR_EACH_VEC_SAFE_ELT (args, i, arg)
46151	    if (arg == omp_priv
46152		|| (TREE_CODE (arg) == ADDR_EXPR
46153		    && TREE_OPERAND (arg, 0) == omp_priv))
46154	      break;
46155	  cp_parser_abort_tentative_parse (parser);
46156	  if (arg == NULL_TREE)
46157	    error ("one of the initializer call arguments should be %<omp_priv%>"
46158		   " or %<&omp_priv%>");
46159	  initializer = cp_parser_postfix_expression (parser, false, false, false,
46160						      false, NULL);
46161	  finish_expr_stmt (initializer);
46162	}
46163
46164      block = finish_omp_structured_block (block);
46165      cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
46166      if (processing_template_decl)
46167	block = build_stmt (input_location, EXPR_STMT, block);
46168      add_stmt (block);
46169
46170      if (ctor)
46171	add_decl_expr (omp_orig);
46172
46173      if (!parens.require_close (parser))
46174	return false;
46175    }
46176
46177  if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
46178    cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
46179                              UNKNOWN_LOCATION);
46180
46181  return true;
46182}
46183
46184/* OpenMP 4.0
46185   #pragma omp declare reduction (reduction-id : typename-list : expression) \
46186      initializer-clause[opt] new-line
46187
46188   initializer-clause:
46189      initializer (omp_priv initializer)
46190      initializer (function-name (argument-list))  */
46191
46192static void
46193cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
46194				 enum pragma_context)
46195{
46196  auto_vec<tree> types;
46197  enum tree_code reduc_code = ERROR_MARK;
46198  tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
46199  unsigned int i;
46200  cp_token *first_token;
46201  cp_token_cache *cp;
46202  int errs;
46203  void *p;
46204
46205  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
46206  p = obstack_alloc (&declarator_obstack, 0);
46207
46208  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
46209    goto fail;
46210
46211  switch (cp_lexer_peek_token (parser->lexer)->type)
46212    {
46213    case CPP_PLUS:
46214      reduc_code = PLUS_EXPR;
46215      break;
46216    case CPP_MULT:
46217      reduc_code = MULT_EXPR;
46218      break;
46219    case CPP_MINUS:
46220      reduc_code = MINUS_EXPR;
46221      break;
46222    case CPP_AND:
46223      reduc_code = BIT_AND_EXPR;
46224      break;
46225    case CPP_XOR:
46226      reduc_code = BIT_XOR_EXPR;
46227      break;
46228    case CPP_OR:
46229      reduc_code = BIT_IOR_EXPR;
46230      break;
46231    case CPP_AND_AND:
46232      reduc_code = TRUTH_ANDIF_EXPR;
46233      break;
46234    case CPP_OR_OR:
46235      reduc_code = TRUTH_ORIF_EXPR;
46236      break;
46237    case CPP_NAME:
46238      reduc_id = orig_reduc_id = cp_parser_identifier (parser);
46239      break;
46240    default:
46241      cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
46242			       "%<|%>, %<&&%>, %<||%> or identifier");
46243      goto fail;
46244    }
46245
46246  if (reduc_code != ERROR_MARK)
46247    cp_lexer_consume_token (parser->lexer);
46248
46249  reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
46250  if (reduc_id == error_mark_node)
46251    goto fail;
46252
46253  if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
46254    goto fail;
46255
46256  /* Types may not be defined in declare reduction type list.  */
46257  const char *saved_message;
46258  saved_message = parser->type_definition_forbidden_message;
46259  parser->type_definition_forbidden_message
46260    = G_("types may not be defined in declare reduction type list");
46261  bool saved_colon_corrects_to_scope_p;
46262  saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
46263  parser->colon_corrects_to_scope_p = false;
46264  bool saved_colon_doesnt_start_class_def_p;
46265  saved_colon_doesnt_start_class_def_p
46266    = parser->colon_doesnt_start_class_def_p;
46267  parser->colon_doesnt_start_class_def_p = true;
46268
46269  while (true)
46270    {
46271      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46272      type = cp_parser_type_id (parser);
46273      if (type == error_mark_node)
46274	;
46275      else if (ARITHMETIC_TYPE_P (type)
46276	       && (orig_reduc_id == NULL_TREE
46277		   || (TREE_CODE (type) != COMPLEX_TYPE
46278		       && (id_equal (orig_reduc_id, "min")
46279			   || id_equal (orig_reduc_id, "max")))))
46280	error_at (loc, "predeclared arithmetic type %qT in "
46281		       "%<#pragma omp declare reduction%>", type);
46282      else if (FUNC_OR_METHOD_TYPE_P (type)
46283	       || TREE_CODE (type) == ARRAY_TYPE)
46284	error_at (loc, "function or array type %qT in "
46285		       "%<#pragma omp declare reduction%>", type);
46286      else if (TYPE_REF_P (type))
46287	error_at (loc, "reference type %qT in "
46288		       "%<#pragma omp declare reduction%>", type);
46289      else if (TYPE_QUALS_NO_ADDR_SPACE (type))
46290	error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
46291		  "type %qT in %<#pragma omp declare reduction%>", type);
46292      else
46293	types.safe_push (type);
46294
46295      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46296	cp_lexer_consume_token (parser->lexer);
46297      else
46298	break;
46299    }
46300
46301  /* Restore the saved message.  */
46302  parser->type_definition_forbidden_message = saved_message;
46303  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
46304  parser->colon_doesnt_start_class_def_p
46305    = saved_colon_doesnt_start_class_def_p;
46306
46307  if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
46308      || types.is_empty ())
46309    {
46310     fail:
46311      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46312      goto done;
46313    }
46314
46315  first_token = cp_lexer_peek_token (parser->lexer);
46316  cp = NULL;
46317  errs = errorcount;
46318  FOR_EACH_VEC_ELT (types, i, type)
46319    {
46320      tree fntype
46321	= build_function_type_list (void_type_node,
46322				    cp_build_reference_type (type, false),
46323				    NULL_TREE);
46324      tree this_reduc_id = reduc_id;
46325      if (!dependent_type_p (type))
46326	this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
46327      tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
46328      DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
46329      DECL_ARTIFICIAL (fndecl) = 1;
46330      DECL_EXTERNAL (fndecl) = 1;
46331      DECL_DECLARED_INLINE_P (fndecl) = 1;
46332      DECL_IGNORED_P (fndecl) = 1;
46333      DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
46334      SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
46335      DECL_ATTRIBUTES (fndecl)
46336	= tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
46337		     DECL_ATTRIBUTES (fndecl));
46338      bool block_scope = false;
46339      if (current_function_decl)
46340	{
46341	  block_scope = true;
46342	  DECL_CONTEXT (fndecl) = current_function_decl;
46343	  DECL_LOCAL_DECL_P (fndecl) = true;
46344	}
46345
46346      if (processing_template_decl)
46347	fndecl = push_template_decl (fndecl);
46348
46349      if (block_scope)
46350	{
46351	  if (!processing_template_decl)
46352	    pushdecl (fndecl);
46353	}
46354      else if (current_class_type)
46355	{
46356	  if (cp == NULL)
46357	    {
46358	      while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46359		cp_lexer_consume_token (parser->lexer);
46360	      cp = cp_token_cache_new (first_token,
46361				       cp_lexer_peek_nth_token (parser->lexer,
46362								2));
46363	    }
46364	  DECL_STATIC_FUNCTION_P (fndecl) = 1;
46365	  finish_member_declaration (fndecl);
46366	  DECL_PENDING_INLINE_INFO (fndecl) = cp;
46367	  DECL_PENDING_INLINE_P (fndecl) = 1;
46368	  vec_safe_push (unparsed_funs_with_definitions, fndecl);
46369	  continue;
46370	}
46371      else
46372	{
46373	  DECL_CONTEXT (fndecl) = current_namespace;
46374	  tree d = pushdecl (fndecl);
46375	  /* We should never meet a matched duplicate decl.  */
46376	  gcc_checking_assert (d == error_mark_node || d == fndecl);
46377	}
46378
46379      tree block = NULL_TREE;
46380      if (!block_scope)
46381	start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
46382      else
46383	block = begin_omp_structured_block ();
46384      if (cp)
46385	{
46386	  cp_parser_push_lexer_for_tokens (parser, cp);
46387	  parser->lexer->in_pragma = true;
46388	}
46389
46390      bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
46391
46392      if (cp)
46393	cp_parser_pop_lexer (parser);
46394      if (!block_scope)
46395	finish_function (/*inline_p=*/false);
46396      else
46397	{
46398	  DECL_CONTEXT (fndecl) = current_function_decl;
46399	  if (DECL_TEMPLATE_INFO (fndecl))
46400	    DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
46401	}
46402      if (!ok)
46403	goto fail;
46404
46405      if (block_scope)
46406	{
46407	  block = finish_omp_structured_block (block);
46408	  if (TREE_CODE (block) == BIND_EXPR)
46409	    DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
46410	  else if (TREE_CODE (block) == STATEMENT_LIST)
46411	    DECL_SAVED_TREE (fndecl) = block;
46412	  if (processing_template_decl)
46413	    add_decl_expr (fndecl);
46414	}
46415
46416      cp_check_omp_declare_reduction (fndecl);
46417      if (cp == NULL && types.length () > 1)
46418	cp = cp_token_cache_new (first_token,
46419				 cp_lexer_peek_nth_token (parser->lexer, 2));
46420      if (errs != errorcount)
46421	break;
46422    }
46423
46424  cp_parser_require_pragma_eol (parser, pragma_tok);
46425
46426 done:
46427  /* Free any declarators allocated.  */
46428  obstack_free (&declarator_obstack, p);
46429}
46430
46431/* OpenMP 4.0
46432   #pragma omp declare simd declare-simd-clauses[optseq] new-line
46433   #pragma omp declare reduction (reduction-id : typename-list : expression) \
46434      initializer-clause[opt] new-line
46435   #pragma omp declare target new-line
46436
46437   OpenMP 5.0
46438   #pragma omp declare variant (identifier) match (context-selector)  */
46439
46440static bool
46441cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
46442		       enum pragma_context context)
46443{
46444  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46445    {
46446      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46447      const char *p = IDENTIFIER_POINTER (id);
46448
46449      if (strcmp (p, "simd") == 0)
46450	{
46451	  cp_lexer_consume_token (parser->lexer);
46452	  cp_parser_omp_declare_simd (parser, pragma_tok,
46453				      context, false);
46454	  return true;
46455	}
46456      if (flag_openmp && strcmp (p, "variant") == 0)
46457	{
46458	  cp_lexer_consume_token (parser->lexer);
46459	  cp_parser_omp_declare_simd (parser, pragma_tok,
46460				      context, true);
46461	  return true;
46462	}
46463      cp_ensure_no_omp_declare_simd (parser);
46464      if (strcmp (p, "reduction") == 0)
46465	{
46466	  cp_lexer_consume_token (parser->lexer);
46467	  cp_parser_omp_declare_reduction (parser, pragma_tok,
46468					   context);
46469	  return false;
46470	}
46471      if (!flag_openmp)  /* flag_openmp_simd  */
46472	{
46473	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46474	  return false;
46475	}
46476      if (strcmp (p, "target") == 0)
46477	{
46478	  cp_lexer_consume_token (parser->lexer);
46479	  cp_parser_omp_declare_target (parser, pragma_tok);
46480	  return false;
46481	}
46482    }
46483  cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
46484			   "%<target%> or %<variant%>");
46485  cp_parser_require_pragma_eol (parser, pragma_tok);
46486  return false;
46487}
46488
46489/* OpenMP 5.0
46490   #pragma omp requires clauses[optseq] new-line  */
46491
46492static bool
46493cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
46494{
46495  bool first = true;
46496  enum omp_requires new_req = (enum omp_requires) 0;
46497
46498  location_t loc = pragma_tok->location;
46499  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46500    {
46501      /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
46502      if ((!first || parser->lexer->in_omp_attribute_pragma)
46503	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46504	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46505	cp_lexer_consume_token (parser->lexer);
46506
46507      first = false;
46508
46509      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46510	{
46511	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46512	  const char *p = IDENTIFIER_POINTER (id);
46513	  location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46514	  enum omp_requires this_req = (enum omp_requires) 0;
46515
46516	  if (!strcmp (p, "unified_address"))
46517	    this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
46518	  else if (!strcmp (p, "unified_shared_memory"))
46519	    this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
46520	  else if (!strcmp (p, "dynamic_allocators"))
46521	    this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
46522	  else if (!strcmp (p, "reverse_offload"))
46523	    this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
46524	  else if (!strcmp (p, "atomic_default_mem_order"))
46525	    {
46526	      cp_lexer_consume_token (parser->lexer);
46527
46528	      matching_parens parens;
46529	      if (parens.require_open (parser))
46530		{
46531		  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46532		    {
46533		      id = cp_lexer_peek_token (parser->lexer)->u.value;
46534		      p = IDENTIFIER_POINTER (id);
46535
46536		      if (!strcmp (p, "seq_cst"))
46537			this_req
46538			  = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
46539		      else if (!strcmp (p, "relaxed"))
46540			this_req
46541			  = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
46542		      else if (!strcmp (p, "acq_rel"))
46543			this_req
46544			  = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
46545		    }
46546		  if (this_req == 0)
46547		    {
46548		      error_at (cp_lexer_peek_token (parser->lexer)->location,
46549				"expected %<seq_cst%>, %<relaxed%> or "
46550				"%<acq_rel%>");
46551		      switch (cp_lexer_peek_token (parser->lexer)->type)
46552			{
46553			case CPP_EOF:
46554			case CPP_PRAGMA_EOL:
46555			case CPP_CLOSE_PAREN:
46556			  break;
46557			default:
46558			  if (cp_lexer_nth_token_is (parser->lexer, 2,
46559						     CPP_CLOSE_PAREN))
46560			    cp_lexer_consume_token (parser->lexer);
46561			  break;
46562			}
46563		    }
46564		  else
46565		    cp_lexer_consume_token (parser->lexer);
46566
46567		  if (!parens.require_close (parser))
46568		    cp_parser_skip_to_closing_parenthesis (parser,
46569							   /*recovering=*/true,
46570							   /*or_comma=*/false,
46571							   /*consume_paren=*/
46572							   true);
46573
46574		  if (this_req == 0)
46575		    {
46576		      cp_parser_require_pragma_eol (parser, pragma_tok);
46577		      return false;
46578		    }
46579		}
46580	      p = NULL;
46581	    }
46582	  else
46583	    {
46584	      error_at (cloc, "expected %<unified_address%>, "
46585			      "%<unified_shared_memory%>, "
46586			      "%<dynamic_allocators%>, "
46587			       "%<reverse_offload%> "
46588			       "or %<atomic_default_mem_order%> clause");
46589	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46590	      return false;
46591	    }
46592	  if (p && this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS)
46593	    sorry_at (cloc, "%qs clause on %<requires%> directive not "
46594			    "supported yet", p);
46595	  if (p)
46596	    cp_lexer_consume_token (parser->lexer);
46597	  if (this_req)
46598	    {
46599	      if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46600		{
46601		  if ((this_req & new_req) != 0)
46602		    error_at (cloc, "too many %qs clauses", p);
46603		  if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
46604		      && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
46605		    error_at (cloc, "%qs clause used lexically after first "
46606				    "target construct or offloading API", p);
46607		}
46608	      else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46609		{
46610		  error_at (cloc, "too many %qs clauses",
46611			    "atomic_default_mem_order");
46612		  this_req = (enum omp_requires) 0;
46613		}
46614	      else if ((omp_requires_mask
46615			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46616		{
46617		  error_at (cloc, "more than one %<atomic_default_mem_order%>"
46618				  " clause in a single compilation unit");
46619		  this_req
46620		    = (enum omp_requires)
46621		       (omp_requires_mask
46622			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
46623		}
46624	      else if ((omp_requires_mask
46625			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
46626		error_at (cloc, "%<atomic_default_mem_order%> clause used "
46627				"lexically after first %<atomic%> construct "
46628				"without memory order clause");
46629	      new_req = (enum omp_requires) (new_req | this_req);
46630	      omp_requires_mask
46631		= (enum omp_requires) (omp_requires_mask | this_req);
46632	      continue;
46633	    }
46634	}
46635      break;
46636    }
46637  cp_parser_require_pragma_eol (parser, pragma_tok);
46638
46639  if (new_req == 0)
46640    error_at (loc, "%<pragma omp requires%> requires at least one clause");
46641  return false;
46642}
46643
46644
46645/* OpenMP 5.1:
46646   #pragma omp nothing new-line  */
46647
46648static void
46649cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
46650{
46651  cp_parser_require_pragma_eol (parser, pragma_tok);
46652}
46653
46654
46655/* OpenMP 5.1
46656   #pragma omp error clauses[optseq] new-line  */
46657
46658static bool
46659cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
46660		     enum pragma_context context)
46661{
46662  int at_compilation = -1;
46663  int severity_fatal = -1;
46664  tree message = NULL_TREE;
46665  bool first = true;
46666  bool bad = false;
46667  location_t loc = pragma_tok->location;
46668
46669  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46670    {
46671      /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
46672      if ((!first || parser->lexer->in_omp_attribute_pragma)
46673	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46674	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46675	cp_lexer_consume_token (parser->lexer);
46676
46677      first = false;
46678
46679      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
46680	break;
46681
46682      const char *p
46683	= IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46684      location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46685      static const char *args[] = {
46686	"execution", "compilation", "warning", "fatal"
46687      };
46688      int *v = NULL;
46689      int idx = 0, n = -1;
46690      tree m = NULL_TREE;
46691
46692      if (!strcmp (p, "at"))
46693	v = &at_compilation;
46694      else if (!strcmp (p, "severity"))
46695	{
46696	  v = &severity_fatal;
46697	  idx += 2;
46698	}
46699      else if (strcmp (p, "message"))
46700	{
46701	  error_at (cloc,
46702		    "expected %<at%>, %<severity%> or %<message%> clause");
46703	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46704	  return false;
46705	}
46706
46707      cp_lexer_consume_token (parser->lexer);
46708
46709      matching_parens parens;
46710      if (parens.require_open (parser))
46711	{
46712	  if (v == NULL)
46713	    {
46714	      m = cp_parser_assignment_expression (parser);
46715	      if (type_dependent_expression_p (m))
46716		m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
46717	      else
46718		m = perform_implicit_conversion_flags (const_string_type_node, m,
46719						       tf_warning_or_error,
46720						       LOOKUP_NORMAL);
46721	    }
46722	  else
46723	    {
46724	      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46725		{
46726		  tree val = cp_lexer_peek_token (parser->lexer)->u.value;
46727		  const char *q = IDENTIFIER_POINTER (val);
46728
46729		  if (!strcmp (q, args[idx]))
46730		    n = 0;
46731		  else if (!strcmp (q, args[idx + 1]))
46732		    n = 1;
46733		}
46734	      if (n == -1)
46735		{
46736		  error_at (cp_lexer_peek_token (parser->lexer)->location,
46737			    "expected %qs or %qs", args[idx], args[idx + 1]);
46738		  bad = true;
46739		  switch (cp_lexer_peek_token (parser->lexer)->type)
46740		    {
46741		    case CPP_EOF:
46742		    case CPP_PRAGMA_EOL:
46743		    case CPP_CLOSE_PAREN:
46744		      break;
46745		    default:
46746		      if (cp_lexer_nth_token_is (parser->lexer, 2,
46747						 CPP_CLOSE_PAREN))
46748			cp_lexer_consume_token (parser->lexer);
46749		      break;
46750		    }
46751		}
46752	      else
46753		cp_lexer_consume_token (parser->lexer);
46754	    }
46755
46756	  if (!parens.require_close (parser))
46757	    cp_parser_skip_to_closing_parenthesis (parser,
46758						   /*recovering=*/true,
46759						   /*or_comma=*/false,
46760						   /*consume_paren=*/
46761						   true);
46762
46763	  if (v == NULL)
46764	    {
46765	      if (message)
46766		{
46767		  error_at (cloc, "too many %qs clauses", p);
46768		  bad = true;
46769		}
46770	      else
46771		message = m;
46772	    }
46773	  else if (n != -1)
46774	    {
46775	      if (*v != -1)
46776		{
46777		  error_at (cloc, "too many %qs clauses", p);
46778		  bad = true;
46779		}
46780	      else
46781		*v = n;
46782	    }
46783	}
46784      else
46785	bad = true;
46786    }
46787  cp_parser_require_pragma_eol (parser, pragma_tok);
46788  if (bad)
46789    return true;
46790
46791  if (at_compilation == -1)
46792    at_compilation = 1;
46793  if (severity_fatal == -1)
46794    severity_fatal = 1;
46795  if (!at_compilation)
46796    {
46797      if (context != pragma_compound)
46798	{
46799	  error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
46800			 "may only be used in compound statements");
46801	  return true;
46802	}
46803      tree fndecl
46804	= builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
46805						: BUILT_IN_GOMP_WARNING);
46806      if (!message)
46807	message = build_zero_cst (const_string_type_node);
46808      tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
46809				       build_all_ones_cst (size_type_node));
46810      add_stmt (stmt);
46811      return true;
46812    }
46813
46814  if (in_discarded_stmt)
46815    return false;
46816
46817  const char *msg = NULL;
46818  if (message)
46819    {
46820      msg = c_getstr (fold_for_warn (message));
46821      if (msg == NULL)
46822	msg = _("<message unknown at compile time>");
46823    }
46824  if (msg)
46825    emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46826		     "%<pragma omp error%> encountered: %s", msg);
46827  else
46828    emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46829		     "%<pragma omp error%> encountered");
46830  return false;
46831}
46832
46833/* OpenMP 4.5:
46834   #pragma omp taskloop taskloop-clause[optseq] new-line
46835     for-loop
46836
46837   #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
46838     for-loop  */
46839
46840#define OMP_TASKLOOP_CLAUSE_MASK				\
46841	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
46842	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
46843	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
46844	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
46845	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
46846	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)	\
46847	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)	\
46848	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
46849	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
46850	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
46851	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
46852	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
46853	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)	\
46854	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY)	\
46855	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
46856	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
46857	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
46858
46859static tree
46860cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
46861			char *p_name, omp_clause_mask mask, tree *cclauses,
46862			bool *if_p)
46863{
46864  tree clauses, sb, ret;
46865  unsigned int save;
46866  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46867
46868  strcat (p_name, " taskloop");
46869  mask |= OMP_TASKLOOP_CLAUSE_MASK;
46870  /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
46871     clause.  */
46872  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
46873    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
46874
46875  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46876    {
46877      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46878      const char *p = IDENTIFIER_POINTER (id);
46879
46880      if (strcmp (p, "simd") == 0)
46881	{
46882	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46883	  if (cclauses == NULL)
46884	    cclauses = cclauses_buf;
46885
46886	  cp_lexer_consume_token (parser->lexer);
46887	  if (!flag_openmp)  /* flag_openmp_simd  */
46888	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46889				       cclauses, if_p);
46890	  sb = begin_omp_structured_block ();
46891	  save = cp_parser_begin_omp_structured_block (parser);
46892	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46893				    cclauses, if_p);
46894	  cp_parser_end_omp_structured_block (parser, save);
46895	  tree body = finish_omp_structured_block (sb);
46896	  if (ret == NULL)
46897	    return ret;
46898	  ret = make_node (OMP_TASKLOOP);
46899	  TREE_TYPE (ret) = void_type_node;
46900	  OMP_FOR_BODY (ret) = body;
46901	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
46902	  SET_EXPR_LOCATION (ret, loc);
46903	  add_stmt (ret);
46904	  return ret;
46905	}
46906    }
46907  if (!flag_openmp)  /* flag_openmp_simd  */
46908    {
46909      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46910      return NULL_TREE;
46911    }
46912
46913  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46914				       cclauses == NULL);
46915  if (cclauses)
46916    {
46917      cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
46918      clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
46919    }
46920
46921  keep_next_level (true);
46922  sb = begin_omp_structured_block ();
46923  save = cp_parser_begin_omp_structured_block (parser);
46924
46925  ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
46926				if_p);
46927
46928  cp_parser_end_omp_structured_block (parser, save);
46929  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
46930
46931  return ret;
46932}
46933
46934
46935/* OpenACC 2.0:
46936   # pragma acc routine oacc-routine-clause[optseq] new-line
46937     function-definition
46938
46939   # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
46940*/
46941
46942#define OACC_ROUTINE_CLAUSE_MASK					\
46943	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
46944	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
46945	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
46946	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)			\
46947	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
46948
46949/* Parse the OpenACC routine pragma.  This has an optional '( name )'
46950   component, which must resolve to a declared namespace-scope
46951   function.  The clauses are either processed directly (for a named
46952   function), or defered until the immediatley following declaration
46953   is parsed.  */
46954
46955static void
46956cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
46957			enum pragma_context context)
46958{
46959  gcc_checking_assert (context == pragma_external);
46960  /* The checking for "another pragma following this one" in the "no optional
46961     '( name )'" case makes sure that we dont re-enter.  */
46962  gcc_checking_assert (parser->oacc_routine == NULL);
46963
46964  cp_oacc_routine_data data;
46965  data.error_seen = false;
46966  data.fndecl_seen = false;
46967  data.tokens = vNULL;
46968  data.clauses = NULL_TREE;
46969  data.loc = pragma_tok->location;
46970  /* It is safe to take the address of a local variable; it will only be
46971     used while this scope is live.  */
46972  parser->oacc_routine = &data;
46973
46974  /* Look for optional '( name )'.  */
46975  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46976    {
46977      matching_parens parens;
46978      parens.consume_open (parser); /* '(' */
46979
46980      /* We parse the name as an id-expression.  If it resolves to
46981	 anything other than a non-overloaded function at namespace
46982	 scope, it's an error.  */
46983      location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
46984      tree name = cp_parser_id_expression (parser,
46985					   /*template_keyword_p=*/false,
46986					   /*check_dependency_p=*/false,
46987					   /*template_p=*/NULL,
46988					   /*declarator_p=*/false,
46989					   /*optional_p=*/false);
46990      tree decl = (identifier_p (name)
46991		   ? cp_parser_lookup_name_simple (parser, name, name_loc)
46992		   : name);
46993      if (name != error_mark_node && decl == error_mark_node)
46994	cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
46995
46996      if (decl == error_mark_node
46997	  || !parens.require_close (parser))
46998	{
46999	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47000	  parser->oacc_routine = NULL;
47001	  return;
47002	}
47003
47004      data.clauses
47005	= cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
47006				      "#pragma acc routine",
47007				      cp_lexer_peek_token (parser->lexer));
47008      /* The clauses are in reverse order; fix that to make later diagnostic
47009	 emission easier.  */
47010      data.clauses = nreverse (data.clauses);
47011
47012      if (decl && is_overloaded_fn (decl)
47013	  && (TREE_CODE (decl) != FUNCTION_DECL
47014	      || DECL_FUNCTION_TEMPLATE_P  (decl)))
47015	{
47016	  error_at (name_loc,
47017		    "%<#pragma acc routine%> names a set of overloads");
47018	  parser->oacc_routine = NULL;
47019	  return;
47020	}
47021
47022      /* Perhaps we should use the same rule as declarations in different
47023	 namespaces?  */
47024      if (!DECL_NAMESPACE_SCOPE_P (decl))
47025	{
47026	  error_at (name_loc,
47027		    "%qD does not refer to a namespace scope function", decl);
47028	  parser->oacc_routine = NULL;
47029	  return;
47030	}
47031
47032      if (TREE_CODE (decl) != FUNCTION_DECL)
47033	{
47034	  error_at (name_loc, "%qD does not refer to a function", decl);
47035	  parser->oacc_routine = NULL;
47036	  return;
47037	}
47038
47039      cp_finalize_oacc_routine (parser, decl, false);
47040      parser->oacc_routine = NULL;
47041    }
47042  else /* No optional '( name )'.  */
47043    {
47044      /* Store away all pragma tokens.  */
47045      while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47046	cp_lexer_consume_token (parser->lexer);
47047      cp_parser_require_pragma_eol (parser, pragma_tok);
47048      struct cp_token_cache *cp
47049	= cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47050      parser->oacc_routine->tokens.safe_push (cp);
47051
47052      /* Emit a helpful diagnostic if there's another pragma following this
47053	 one.  */
47054      if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47055	{
47056	  cp_ensure_no_oacc_routine (parser);
47057	  data.tokens.release ();
47058	  /* ..., and then just keep going.  */
47059	  return;
47060	}
47061
47062      /* We only have to consider the pragma_external case here.  */
47063      cp_parser_declaration (parser, NULL_TREE);
47064      if (parser->oacc_routine
47065	  && !parser->oacc_routine->fndecl_seen)
47066	cp_ensure_no_oacc_routine (parser);
47067      else
47068	parser->oacc_routine = NULL;
47069      data.tokens.release ();
47070    }
47071}
47072
47073/* Finalize #pragma acc routine clauses after direct declarator has
47074   been parsed.  */
47075
47076static tree
47077cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
47078{
47079  struct cp_token_cache *ce;
47080  cp_oacc_routine_data *data = parser->oacc_routine;
47081
47082  if (!data->error_seen && data->fndecl_seen)
47083    {
47084      error_at (data->loc,
47085		"%<#pragma acc routine%> not immediately followed by "
47086		"a single function declaration or definition");
47087      data->error_seen = true;
47088    }
47089  if (data->error_seen)
47090    return attrs;
47091
47092  gcc_checking_assert (data->tokens.length () == 1);
47093  ce = data->tokens[0];
47094
47095  cp_parser_push_lexer_for_tokens (parser, ce);
47096  parser->lexer->in_pragma = true;
47097  gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
47098
47099  cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47100  gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
47101  parser->oacc_routine->clauses
47102    = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
47103				  "#pragma acc routine", pragma_tok);
47104  /* The clauses are in reverse order; fix that to make later diagnostic
47105     emission easier.  */
47106  parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
47107  cp_parser_pop_lexer (parser);
47108  /* Later, cp_finalize_oacc_routine will process the clauses.  */
47109  parser->oacc_routine->fndecl_seen = true;
47110
47111  return attrs;
47112}
47113
47114/* Apply any saved OpenACC routine clauses to a just-parsed
47115   declaration.  */
47116
47117static void
47118cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
47119{
47120  if (__builtin_expect (parser->oacc_routine != NULL, 0))
47121    {
47122      /* Keep going if we're in error reporting mode.  */
47123      if (parser->oacc_routine->error_seen
47124	  || fndecl == error_mark_node)
47125	return;
47126
47127      if (TREE_CODE (fndecl) != FUNCTION_DECL)
47128	{
47129	  if (parser->oacc_routine->fndecl_seen)
47130	    {
47131	      error_at (parser->oacc_routine->loc,
47132			"%<#pragma acc routine%> not immediately followed by"
47133			" a single function declaration or definition");
47134	      parser->oacc_routine = NULL;
47135	      return;
47136	    }
47137
47138	  cp_ensure_no_oacc_routine (parser);
47139	  return;
47140	}
47141
47142      int compatible
47143	= oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
47144				       parser->oacc_routine->loc,
47145				       "#pragma acc routine");
47146      if (compatible < 0)
47147	{
47148	  parser->oacc_routine = NULL;
47149	  return;
47150	}
47151      if (compatible > 0)
47152	{
47153	}
47154      else
47155	{
47156	  if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
47157	    {
47158	      error_at (parser->oacc_routine->loc,
47159			TREE_USED (fndecl)
47160			? G_("%<#pragma acc routine%> must be applied before"
47161			     " use")
47162			: G_("%<#pragma acc routine%> must be applied before"
47163			     " definition"));
47164	      parser->oacc_routine = NULL;
47165	      return;
47166	    }
47167
47168	  /* Set the routine's level of parallelism.  */
47169	  tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
47170	  oacc_replace_fn_attrib (fndecl, dims);
47171
47172	  /* Add an "omp declare target" attribute.  */
47173	  DECL_ATTRIBUTES (fndecl)
47174	    = tree_cons (get_identifier ("omp declare target"),
47175			 parser->oacc_routine->clauses,
47176			 DECL_ATTRIBUTES (fndecl));
47177	}
47178    }
47179}
47180
47181/* Main entry point to OpenMP statement pragmas.  */
47182
47183static void
47184cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47185{
47186  tree stmt;
47187  char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
47188  omp_clause_mask mask (0);
47189
47190  switch (cp_parser_pragma_kind (pragma_tok))
47191    {
47192    case PRAGMA_OACC_ATOMIC:
47193      cp_parser_omp_atomic (parser, pragma_tok, true);
47194      return;
47195    case PRAGMA_OACC_CACHE:
47196      stmt = cp_parser_oacc_cache (parser, pragma_tok);
47197      break;
47198    case PRAGMA_OACC_DATA:
47199      stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
47200      break;
47201    case PRAGMA_OACC_ENTER_DATA:
47202      stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
47203      break;
47204    case PRAGMA_OACC_EXIT_DATA:
47205      stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
47206      break;
47207    case PRAGMA_OACC_HOST_DATA:
47208      stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
47209      break;
47210    case PRAGMA_OACC_KERNELS:
47211    case PRAGMA_OACC_PARALLEL:
47212    case PRAGMA_OACC_SERIAL:
47213      strcpy (p_name, "#pragma acc");
47214      stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
47215      break;
47216    case PRAGMA_OACC_LOOP:
47217      strcpy (p_name, "#pragma acc");
47218      stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
47219				  if_p);
47220      break;
47221    case PRAGMA_OACC_UPDATE:
47222      stmt = cp_parser_oacc_update (parser, pragma_tok);
47223      break;
47224    case PRAGMA_OACC_WAIT:
47225      stmt = cp_parser_oacc_wait (parser, pragma_tok);
47226      break;
47227    case PRAGMA_OMP_ALLOCATE:
47228      cp_parser_omp_allocate (parser, pragma_tok);
47229      return;
47230    case PRAGMA_OMP_ATOMIC:
47231      cp_parser_omp_atomic (parser, pragma_tok, false);
47232      return;
47233    case PRAGMA_OMP_CRITICAL:
47234      stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
47235      break;
47236    case PRAGMA_OMP_DISTRIBUTE:
47237      strcpy (p_name, "#pragma omp");
47238      stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
47239				       if_p);
47240      break;
47241    case PRAGMA_OMP_FOR:
47242      strcpy (p_name, "#pragma omp");
47243      stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
47244				if_p);
47245      break;
47246    case PRAGMA_OMP_LOOP:
47247      strcpy (p_name, "#pragma omp");
47248      stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
47249				 if_p);
47250      break;
47251    case PRAGMA_OMP_MASKED:
47252      strcpy (p_name, "#pragma omp");
47253      stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
47254				   if_p);
47255      break;
47256    case PRAGMA_OMP_MASTER:
47257      strcpy (p_name, "#pragma omp");
47258      stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
47259				   if_p);
47260      break;
47261    case PRAGMA_OMP_PARALLEL:
47262      strcpy (p_name, "#pragma omp");
47263      stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
47264				     if_p);
47265      break;
47266    case PRAGMA_OMP_SCOPE:
47267      stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
47268      break;
47269    case PRAGMA_OMP_SECTIONS:
47270      strcpy (p_name, "#pragma omp");
47271      stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
47272      break;
47273    case PRAGMA_OMP_SIMD:
47274      strcpy (p_name, "#pragma omp");
47275      stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
47276				 if_p);
47277      break;
47278    case PRAGMA_OMP_SINGLE:
47279      stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
47280      break;
47281    case PRAGMA_OMP_TASK:
47282      stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
47283      break;
47284    case PRAGMA_OMP_TASKGROUP:
47285      stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
47286      break;
47287    case PRAGMA_OMP_TASKLOOP:
47288      strcpy (p_name, "#pragma omp");
47289      stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
47290				     if_p);
47291      break;
47292    case PRAGMA_OMP_TEAMS:
47293      strcpy (p_name, "#pragma omp");
47294      stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
47295				  if_p);
47296      break;
47297    default:
47298      gcc_unreachable ();
47299    }
47300
47301  protected_set_expr_location (stmt, pragma_tok->location);
47302}
47303
47304/* Transactional Memory parsing routines.  */
47305
47306/* Parse a transaction attribute.
47307
47308   txn-attribute:
47309	attribute
47310	[ [ identifier ] ]
47311
47312   We use this instead of cp_parser_attributes_opt for transactions to avoid
47313   the pedwarn in C++98 mode.  */
47314
47315static tree
47316cp_parser_txn_attribute_opt (cp_parser *parser)
47317{
47318  cp_token *token;
47319  tree attr_name, attr = NULL;
47320
47321  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
47322    return cp_parser_attributes_opt (parser);
47323
47324  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
47325    return NULL_TREE;
47326  cp_lexer_consume_token (parser->lexer);
47327  if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
47328    goto error1;
47329
47330  token = cp_lexer_peek_token (parser->lexer);
47331  if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
47332    {
47333      token = cp_lexer_consume_token (parser->lexer);
47334
47335      attr_name = (token->type == CPP_KEYWORD
47336		   /* For keywords, use the canonical spelling,
47337		      not the parsed identifier.  */
47338		   ? ridpointers[(int) token->keyword]
47339		   : token->u.value);
47340      attr = build_tree_list (attr_name, NULL_TREE);
47341    }
47342  else
47343    cp_parser_error (parser, "expected identifier");
47344
47345  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47346 error1:
47347  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47348  return attr;
47349}
47350
47351/* Parse a __transaction_atomic or __transaction_relaxed statement.
47352
47353   transaction-statement:
47354     __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
47355       compound-statement
47356     __transaction_relaxed txn-noexcept-spec[opt] compound-statement
47357*/
47358
47359static tree
47360cp_parser_transaction (cp_parser *parser, cp_token *token)
47361{
47362  unsigned char old_in = parser->in_transaction;
47363  unsigned char this_in = 1, new_in;
47364  enum rid keyword = token->keyword;
47365  tree stmt, attrs, noex;
47366
47367  cp_lexer_consume_token (parser->lexer);
47368
47369  if (keyword == RID_TRANSACTION_RELAXED
47370      || keyword == RID_SYNCHRONIZED)
47371    this_in |= TM_STMT_ATTR_RELAXED;
47372  else
47373    {
47374      attrs = cp_parser_txn_attribute_opt (parser);
47375      if (attrs)
47376	this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47377    }
47378
47379  /* Parse a noexcept specification.  */
47380  if (keyword == RID_ATOMIC_NOEXCEPT)
47381    noex = boolean_true_node;
47382  else if (keyword == RID_ATOMIC_CANCEL)
47383    {
47384      /* cancel-and-throw is unimplemented.  */
47385      sorry ("%<atomic_cancel%>");
47386      noex = NULL_TREE;
47387    }
47388  else
47389    noex = cp_parser_noexcept_specification_opt (parser,
47390						 CP_PARSER_FLAGS_NONE,
47391						 /*require_constexpr=*/true,
47392						 /*consumed_expr=*/NULL,
47393						 /*return_cond=*/true);
47394
47395  /* Keep track if we're in the lexical scope of an outer transaction.  */
47396  new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
47397
47398  stmt = begin_transaction_stmt (token->location, NULL, this_in);
47399
47400  parser->in_transaction = new_in;
47401  cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
47402  parser->in_transaction = old_in;
47403
47404  finish_transaction_stmt (stmt, NULL, this_in, noex);
47405
47406  return stmt;
47407}
47408
47409/* Parse a __transaction_atomic or __transaction_relaxed expression.
47410
47411   transaction-expression:
47412     __transaction_atomic txn-noexcept-spec[opt] ( expression )
47413     __transaction_relaxed txn-noexcept-spec[opt] ( expression )
47414*/
47415
47416static tree
47417cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
47418{
47419  unsigned char old_in = parser->in_transaction;
47420  unsigned char this_in = 1;
47421  cp_token *token;
47422  tree expr, noex;
47423  bool noex_expr;
47424  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47425
47426  gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47427      || keyword == RID_TRANSACTION_RELAXED);
47428
47429  if (!flag_tm)
47430    error_at (loc,
47431	      keyword == RID_TRANSACTION_RELAXED
47432	      ? G_("%<__transaction_relaxed%> without transactional memory "
47433		  "support enabled")
47434	      : G_("%<__transaction_atomic%> without transactional memory "
47435		   "support enabled"));
47436
47437  token = cp_parser_require_keyword (parser, keyword,
47438      (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47439	  : RT_TRANSACTION_RELAXED));
47440  gcc_assert (token != NULL);
47441
47442  if (keyword == RID_TRANSACTION_RELAXED)
47443    this_in |= TM_STMT_ATTR_RELAXED;
47444
47445  /* Set this early.  This might mean that we allow transaction_cancel in
47446     an expression that we find out later actually has to be a constexpr.
47447     However, we expect that cxx_constant_value will be able to deal with
47448     this; also, if the noexcept has no constexpr, then what we parse next
47449     really is a transaction's body.  */
47450  parser->in_transaction = this_in;
47451
47452  /* Parse a noexcept specification.  */
47453  noex = cp_parser_noexcept_specification_opt (parser,
47454					       CP_PARSER_FLAGS_NONE,
47455					       /*require_constexpr=*/false,
47456					       &noex_expr,
47457					       /*return_cond=*/true);
47458
47459  if (!noex || !noex_expr
47460      || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47461    {
47462      matching_parens parens;
47463      parens.require_open (parser);
47464
47465      expr = cp_parser_expression (parser);
47466      expr = finish_parenthesized_expr (expr);
47467
47468      parens.require_close (parser);
47469    }
47470  else
47471    {
47472      /* The only expression that is available got parsed for the noexcept
47473         already.  noexcept is true then.  */
47474      expr = noex;
47475      noex = boolean_true_node;
47476    }
47477
47478  expr = build_transaction_expr (token->location, expr, this_in, noex);
47479  parser->in_transaction = old_in;
47480
47481  if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
47482    return error_mark_node;
47483
47484  return (flag_tm ? expr : error_mark_node);
47485}
47486
47487/* Parse a function-transaction-block.
47488
47489   function-transaction-block:
47490     __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
47491	 function-body
47492     __transaction_atomic txn-attribute[opt] function-try-block
47493     __transaction_relaxed ctor-initializer[opt] function-body
47494     __transaction_relaxed function-try-block
47495*/
47496
47497static void
47498cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
47499{
47500  unsigned char old_in = parser->in_transaction;
47501  unsigned char new_in = 1;
47502  tree compound_stmt, stmt, attrs;
47503  cp_token *token;
47504
47505  gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47506      || keyword == RID_TRANSACTION_RELAXED);
47507  token = cp_parser_require_keyword (parser, keyword,
47508      (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47509	  : RT_TRANSACTION_RELAXED));
47510  gcc_assert (token != NULL);
47511
47512  if (keyword == RID_TRANSACTION_RELAXED)
47513    new_in |= TM_STMT_ATTR_RELAXED;
47514  else
47515    {
47516      attrs = cp_parser_txn_attribute_opt (parser);
47517      if (attrs)
47518	new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47519    }
47520
47521  stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
47522
47523  parser->in_transaction = new_in;
47524
47525  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
47526    cp_parser_function_try_block (parser);
47527  else
47528    cp_parser_ctor_initializer_opt_and_function_body
47529      (parser, /*in_function_try_block=*/false);
47530
47531  parser->in_transaction = old_in;
47532
47533  finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
47534}
47535
47536/* Parse a __transaction_cancel statement.
47537
47538   cancel-statement:
47539     __transaction_cancel txn-attribute[opt] ;
47540     __transaction_cancel txn-attribute[opt] throw-expression ;
47541
47542   ??? Cancel and throw is not yet implemented.  */
47543
47544static tree
47545cp_parser_transaction_cancel (cp_parser *parser)
47546{
47547  cp_token *token;
47548  bool is_outer = false;
47549  tree stmt, attrs;
47550
47551  token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
47552				     RT_TRANSACTION_CANCEL);
47553  gcc_assert (token != NULL);
47554
47555  attrs = cp_parser_txn_attribute_opt (parser);
47556  if (attrs)
47557    is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
47558
47559  /* ??? Parse cancel-and-throw here.  */
47560
47561  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
47562
47563  if (!flag_tm)
47564    {
47565      error_at (token->location, "%<__transaction_cancel%> without "
47566		"transactional memory support enabled");
47567      return error_mark_node;
47568    }
47569  else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
47570    {
47571      error_at (token->location, "%<__transaction_cancel%> within a "
47572		"%<__transaction_relaxed%>");
47573      return error_mark_node;
47574    }
47575  else if (is_outer)
47576    {
47577      if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
47578	  && !is_tm_may_cancel_outer (current_function_decl))
47579	{
47580	  error_at (token->location, "outer %<__transaction_cancel%> not "
47581		    "within outer %<__transaction_atomic%>");
47582	  error_at (token->location,
47583		    "  or a %<transaction_may_cancel_outer%> function");
47584	  return error_mark_node;
47585	}
47586    }
47587  else if (parser->in_transaction == 0)
47588    {
47589      error_at (token->location, "%<__transaction_cancel%> not within "
47590		"%<__transaction_atomic%>");
47591      return error_mark_node;
47592    }
47593
47594  stmt = build_tm_abort_call (token->location, is_outer);
47595  add_stmt (stmt);
47596
47597  return stmt;
47598}
47599
47600/* The parser.  */
47601
47602static GTY (()) cp_parser *the_parser;
47603
47604
47605/* Special handling for the first token or line in the file.  The first
47606   thing in the file might be #pragma GCC pch_preprocess, which loads a
47607   PCH file, which is a GC collection point.  So we need to handle this
47608   first pragma without benefit of an existing lexer structure.
47609
47610   Always returns one token to the caller in *FIRST_TOKEN.  This is
47611   either the true first token of the file, or the first token after
47612   the initial pragma.  */
47613
47614static void
47615cp_parser_initial_pragma (cp_token *first_token)
47616{
47617  if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
47618    return;
47619
47620  cp_lexer_get_preprocessor_token (0, first_token);
47621
47622  tree name = NULL;
47623  if (first_token->type == CPP_STRING)
47624    {
47625      name = first_token->u.value;
47626
47627      cp_lexer_get_preprocessor_token (0, first_token);
47628    }
47629
47630  /* Skip to the end of the pragma.  */
47631  if (first_token->type != CPP_PRAGMA_EOL)
47632    {
47633      error_at (first_token->location,
47634		"malformed %<#pragma GCC pch_preprocess%>");
47635      do
47636	cp_lexer_get_preprocessor_token (0, first_token);
47637      while (first_token->type != CPP_PRAGMA_EOL);
47638    }
47639
47640  /* Now actually load the PCH file.  */
47641  if (name)
47642    c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
47643
47644  /* Read one more token to return to our caller.  We have to do this
47645     after reading the PCH file in, since its pointers have to be
47646     live.  */
47647  cp_lexer_get_preprocessor_token (0, first_token);
47648}
47649
47650/* Parse a pragma GCC ivdep.  */
47651
47652static bool
47653cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
47654{
47655  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47656  return true;
47657}
47658
47659/* Parse a pragma GCC unroll.  */
47660
47661static unsigned short
47662cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
47663{
47664  location_t location = cp_lexer_peek_token (parser->lexer)->location;
47665  tree expr = cp_parser_constant_expression (parser);
47666  unsigned short unroll;
47667  expr = maybe_constant_value (expr);
47668  HOST_WIDE_INT lunroll = 0;
47669  if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
47670      || TREE_CODE (expr) != INTEGER_CST
47671      || (lunroll = tree_to_shwi (expr)) < 0
47672      || lunroll >= USHRT_MAX)
47673    {
47674      error_at (location, "%<#pragma GCC unroll%> requires an"
47675		" assignment-expression that evaluates to a non-negative"
47676		" integral constant less than %u", USHRT_MAX);
47677      unroll = 0;
47678    }
47679  else
47680    {
47681      unroll = (unsigned short)lunroll;
47682      if (unroll == 0)
47683	unroll = 1;
47684    }
47685  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47686  return unroll;
47687}
47688
47689/* Normal parsing of a pragma token.  Here we can (and must) use the
47690   regular lexer.  */
47691
47692static bool
47693cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
47694{
47695  cp_token *pragma_tok;
47696  unsigned int id;
47697  tree stmt;
47698  bool ret = false;
47699
47700  pragma_tok = cp_lexer_consume_token (parser->lexer);
47701  gcc_assert (pragma_tok->type == CPP_PRAGMA);
47702  parser->lexer->in_pragma = true;
47703
47704  id = cp_parser_pragma_kind (pragma_tok);
47705  if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
47706    cp_ensure_no_omp_declare_simd (parser);
47707  switch (id)
47708    {
47709    case PRAGMA_GCC_PCH_PREPROCESS:
47710      error_at (pragma_tok->location,
47711		"%<#pragma GCC pch_preprocess%> must be first");
47712      break;
47713
47714    case PRAGMA_OMP_BARRIER:
47715      switch (context)
47716	{
47717	case pragma_compound:
47718	  cp_parser_omp_barrier (parser, pragma_tok);
47719	  return false;
47720	case pragma_stmt:
47721	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47722		    "used in compound statements", "omp barrier");
47723	  ret = true;
47724	  break;
47725	default:
47726	  goto bad_stmt;
47727	}
47728      break;
47729
47730    case PRAGMA_OMP_DEPOBJ:
47731      switch (context)
47732	{
47733	case pragma_compound:
47734	  cp_parser_omp_depobj (parser, pragma_tok);
47735	  return false;
47736	case pragma_stmt:
47737	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47738		    "used in compound statements", "omp depobj");
47739	  ret = true;
47740	  break;
47741	default:
47742	  goto bad_stmt;
47743	}
47744      break;
47745
47746    case PRAGMA_OMP_FLUSH:
47747      switch (context)
47748	{
47749	case pragma_compound:
47750	  cp_parser_omp_flush (parser, pragma_tok);
47751	  return false;
47752	case pragma_stmt:
47753	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47754		    "used in compound statements", "omp flush");
47755	  ret = true;
47756	  break;
47757	default:
47758	  goto bad_stmt;
47759	}
47760      break;
47761
47762    case PRAGMA_OMP_TASKWAIT:
47763      switch (context)
47764	{
47765	case pragma_compound:
47766	  cp_parser_omp_taskwait (parser, pragma_tok);
47767	  return false;
47768	case pragma_stmt:
47769	  error_at (pragma_tok->location,
47770		    "%<#pragma %s%> may only be used in compound statements",
47771		    "omp taskwait");
47772	  ret = true;
47773	  break;
47774	default:
47775	  goto bad_stmt;
47776	}
47777      break;
47778
47779    case PRAGMA_OMP_TASKYIELD:
47780      switch (context)
47781	{
47782	case pragma_compound:
47783	  cp_parser_omp_taskyield (parser, pragma_tok);
47784	  return false;
47785	case pragma_stmt:
47786	  error_at (pragma_tok->location,
47787		    "%<#pragma %s%> may only be used in compound statements",
47788		    "omp taskyield");
47789	  ret = true;
47790	  break;
47791	default:
47792	  goto bad_stmt;
47793	}
47794      break;
47795
47796    case PRAGMA_OMP_CANCEL:
47797      switch (context)
47798	{
47799	case pragma_compound:
47800	  cp_parser_omp_cancel (parser, pragma_tok);
47801	  return false;
47802	case pragma_stmt:
47803	  error_at (pragma_tok->location,
47804		    "%<#pragma %s%> may only be used in compound statements",
47805		    "omp cancel");
47806	  ret = true;
47807	  break;
47808	default:
47809	  goto bad_stmt;
47810	}
47811      break;
47812
47813    case PRAGMA_OMP_CANCELLATION_POINT:
47814      return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
47815
47816    case PRAGMA_OMP_THREADPRIVATE:
47817      cp_parser_omp_threadprivate (parser, pragma_tok);
47818      return false;
47819
47820    case PRAGMA_OMP_DECLARE:
47821      return cp_parser_omp_declare (parser, pragma_tok, context);
47822
47823    case PRAGMA_OACC_DECLARE:
47824      cp_parser_oacc_declare (parser, pragma_tok);
47825      return false;
47826
47827    case PRAGMA_OACC_ENTER_DATA:
47828      if (context == pragma_stmt)
47829	{
47830	  error_at (pragma_tok->location,
47831		    "%<#pragma %s%> may only be used in compound statements",
47832		    "acc enter data");
47833	  ret = true;
47834	  break;
47835	}
47836      else if (context != pragma_compound)
47837	goto bad_stmt;
47838      cp_parser_omp_construct (parser, pragma_tok, if_p);
47839      return true;
47840
47841    case PRAGMA_OACC_EXIT_DATA:
47842      if (context == pragma_stmt)
47843	{
47844	  error_at (pragma_tok->location,
47845		    "%<#pragma %s%> may only be used in compound statements",
47846		    "acc exit data");
47847	  ret = true;
47848	  break;
47849	}
47850      else if (context != pragma_compound)
47851	goto bad_stmt;
47852      cp_parser_omp_construct (parser, pragma_tok, if_p);
47853      return true;
47854
47855    case PRAGMA_OACC_ROUTINE:
47856      if (context != pragma_external)
47857	{
47858	  error_at (pragma_tok->location,
47859		    "%<#pragma acc routine%> must be at file scope");
47860	  ret = true;
47861	  break;
47862	}
47863      cp_parser_oacc_routine (parser, pragma_tok, context);
47864      return false;
47865
47866    case PRAGMA_OACC_UPDATE:
47867      if (context == pragma_stmt)
47868	{
47869	  error_at (pragma_tok->location,
47870		    "%<#pragma %s%> may only be used in compound statements",
47871		    "acc update");
47872	  ret = true;
47873	  break;
47874	}
47875      else if (context != pragma_compound)
47876	goto bad_stmt;
47877      cp_parser_omp_construct (parser, pragma_tok, if_p);
47878      return true;
47879
47880    case PRAGMA_OACC_WAIT:
47881      if (context == pragma_stmt)
47882	{
47883	  error_at (pragma_tok->location,
47884		    "%<#pragma %s%> may only be used in compound statements",
47885		    "acc wait");
47886	  ret = true;
47887	  break;
47888	}
47889      else if (context != pragma_compound)
47890	goto bad_stmt;
47891      cp_parser_omp_construct (parser, pragma_tok, if_p);
47892      return true;
47893    case PRAGMA_OMP_ALLOCATE:
47894      cp_parser_omp_allocate (parser, pragma_tok);
47895      return false;
47896    case PRAGMA_OACC_ATOMIC:
47897    case PRAGMA_OACC_CACHE:
47898    case PRAGMA_OACC_DATA:
47899    case PRAGMA_OACC_HOST_DATA:
47900    case PRAGMA_OACC_KERNELS:
47901    case PRAGMA_OACC_LOOP:
47902    case PRAGMA_OACC_PARALLEL:
47903    case PRAGMA_OACC_SERIAL:
47904    case PRAGMA_OMP_ATOMIC:
47905    case PRAGMA_OMP_CRITICAL:
47906    case PRAGMA_OMP_DISTRIBUTE:
47907    case PRAGMA_OMP_FOR:
47908    case PRAGMA_OMP_LOOP:
47909    case PRAGMA_OMP_MASKED:
47910    case PRAGMA_OMP_MASTER:
47911    case PRAGMA_OMP_PARALLEL:
47912    case PRAGMA_OMP_SCOPE:
47913    case PRAGMA_OMP_SECTIONS:
47914    case PRAGMA_OMP_SIMD:
47915    case PRAGMA_OMP_SINGLE:
47916    case PRAGMA_OMP_TASK:
47917    case PRAGMA_OMP_TASKGROUP:
47918    case PRAGMA_OMP_TASKLOOP:
47919    case PRAGMA_OMP_TEAMS:
47920      if (context != pragma_stmt && context != pragma_compound)
47921	goto bad_stmt;
47922      stmt = push_omp_privatization_clauses (false);
47923      cp_parser_omp_construct (parser, pragma_tok, if_p);
47924      pop_omp_privatization_clauses (stmt);
47925      return true;
47926
47927    case PRAGMA_OMP_REQUIRES:
47928      if (context != pragma_external)
47929	{
47930	  error_at (pragma_tok->location,
47931		    "%<#pragma omp requires%> may only be used at file or "
47932		    "namespace scope");
47933	  ret = true;
47934	  break;
47935	}
47936      return cp_parser_omp_requires (parser, pragma_tok);
47937
47938    case PRAGMA_OMP_NOTHING:
47939      cp_parser_omp_nothing (parser, pragma_tok);
47940      return false;
47941
47942    case PRAGMA_OMP_ERROR:
47943      return cp_parser_omp_error (parser, pragma_tok, context);
47944
47945    case PRAGMA_OMP_ORDERED:
47946      if (context != pragma_stmt && context != pragma_compound)
47947	goto bad_stmt;
47948      stmt = push_omp_privatization_clauses (false);
47949      ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
47950      pop_omp_privatization_clauses (stmt);
47951      return ret;
47952
47953    case PRAGMA_OMP_TARGET:
47954      if (context != pragma_stmt && context != pragma_compound)
47955	goto bad_stmt;
47956      stmt = push_omp_privatization_clauses (false);
47957      ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
47958      pop_omp_privatization_clauses (stmt);
47959      return ret;
47960
47961    case PRAGMA_OMP_END_DECLARE_TARGET:
47962      cp_parser_omp_end_declare_target (parser, pragma_tok);
47963      return false;
47964
47965    case PRAGMA_OMP_SCAN:
47966      error_at (pragma_tok->location,
47967		"%<#pragma omp scan%> may only be used in "
47968		"a loop construct with %<inscan%> %<reduction%> clause");
47969      break;
47970
47971    case PRAGMA_OMP_SECTION:
47972      error_at (pragma_tok->location,
47973		"%<#pragma omp section%> may only be used in "
47974		"%<#pragma omp sections%> construct");
47975      break;
47976
47977    case PRAGMA_IVDEP:
47978      {
47979	if (context == pragma_external)
47980	  {
47981	    error_at (pragma_tok->location,
47982		      "%<#pragma GCC ivdep%> must be inside a function");
47983	    break;
47984	  }
47985	const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
47986	unsigned short unroll;
47987	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
47988	if (tok->type == CPP_PRAGMA
47989	    && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
47990	  {
47991	    tok = cp_lexer_consume_token (parser->lexer);
47992	    unroll = cp_parser_pragma_unroll (parser, tok);
47993	    tok = cp_lexer_peek_token (the_parser->lexer);
47994	  }
47995	else
47996	  unroll = 0;
47997	if (tok->type != CPP_KEYWORD
47998	    || (tok->keyword != RID_FOR
47999		&& tok->keyword != RID_WHILE
48000		&& tok->keyword != RID_DO))
48001	  {
48002	    cp_parser_error (parser, "for, while or do statement expected");
48003	    return false;
48004	  }
48005	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
48006	return true;
48007      }
48008
48009    case PRAGMA_UNROLL:
48010      {
48011	if (context == pragma_external)
48012	  {
48013	    error_at (pragma_tok->location,
48014		      "%<#pragma GCC unroll%> must be inside a function");
48015	    break;
48016	  }
48017	const unsigned short unroll
48018	  = cp_parser_pragma_unroll (parser, pragma_tok);
48019	bool ivdep;
48020	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48021	if (tok->type == CPP_PRAGMA
48022	    && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
48023	  {
48024	    tok = cp_lexer_consume_token (parser->lexer);
48025	    ivdep = cp_parser_pragma_ivdep (parser, tok);
48026	    tok = cp_lexer_peek_token (the_parser->lexer);
48027	  }
48028	else
48029	  ivdep = false;
48030	if (tok->type != CPP_KEYWORD
48031	    || (tok->keyword != RID_FOR
48032		&& tok->keyword != RID_WHILE
48033		&& tok->keyword != RID_DO))
48034	  {
48035	    cp_parser_error (parser, "for, while or do statement expected");
48036	    return false;
48037	  }
48038	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
48039	return true;
48040      }
48041
48042    default:
48043      gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
48044      c_invoke_pragma_handler (id);
48045      break;
48046
48047    bad_stmt:
48048      cp_parser_error (parser, "expected declaration specifiers");
48049      break;
48050    }
48051
48052  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48053  return ret;
48054}
48055
48056/* The interface the pragma parsers have to the lexer.  */
48057
48058enum cpp_ttype
48059pragma_lex (tree *value, location_t *loc)
48060{
48061  cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48062  enum cpp_ttype ret = tok->type;
48063
48064  *value = tok->u.value;
48065  if (loc)
48066    *loc = tok->location;
48067
48068  if (ret == CPP_PRAGMA_EOL)
48069    ret = CPP_EOF;
48070  else if (ret == CPP_STRING)
48071    *value = cp_parser_string_literal (the_parser, false, false);
48072  else
48073    {
48074      if (ret == CPP_KEYWORD)
48075	ret = CPP_NAME;
48076      cp_lexer_consume_token (the_parser->lexer);
48077    }
48078
48079  return ret;
48080}
48081
48082
48083/* External interface.  */
48084
48085/* Parse one entire translation unit.  */
48086
48087void
48088c_parse_file (void)
48089{
48090  static bool already_called = false;
48091
48092  if (already_called)
48093    fatal_error (input_location,
48094		 "multi-source compilation not implemented for C++");
48095  already_called = true;
48096
48097  /* cp_lexer_new_main is called before doing any GC allocation
48098     because tokenization might load a PCH file.  */
48099  cp_lexer *lexer = cp_lexer_new_main ();
48100
48101  the_parser = cp_parser_new (lexer);
48102
48103  cp_parser_translation_unit (the_parser);
48104  class_decl_loc_t::diag_mismatched_tags ();
48105
48106  the_parser = NULL;
48107
48108  finish_translation_unit ();
48109}
48110
48111/* Create an identifier for a generic parameter type (a synthesized
48112   template parameter implied by `auto' or a concept identifier). */
48113
48114static GTY(()) int generic_parm_count;
48115static tree
48116make_generic_type_name ()
48117{
48118  char buf[32];
48119  sprintf (buf, "auto:%d", ++generic_parm_count);
48120  return get_identifier (buf);
48121}
48122
48123/* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
48124   (creating a new template parameter list if necessary).  Returns the newly
48125   created template type parm.  */
48126
48127static tree
48128synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
48129{
48130  /* A requires-clause is not a function and cannot have placeholders.  */
48131  if (current_binding_level->requires_expression)
48132    {
48133      error ("placeholder type not allowed in this context");
48134      return error_mark_node;
48135    }
48136
48137  gcc_assert (current_binding_level->kind == sk_function_parms);
48138
48139  /* We are either continuing a function template that already contains implicit
48140     template parameters, creating a new fully-implicit function template, or
48141     extending an existing explicit function template with implicit template
48142     parameters.  */
48143
48144  cp_binding_level *const entry_scope = current_binding_level;
48145
48146  bool become_template = false;
48147  cp_binding_level *parent_scope = 0;
48148
48149  if (parser->implicit_template_scope)
48150    {
48151      gcc_assert (parser->implicit_template_parms);
48152
48153      current_binding_level = parser->implicit_template_scope;
48154    }
48155  else
48156    {
48157      /* Roll back to the existing template parameter scope (in the case of
48158	 extending an explicit function template) or introduce a new template
48159	 parameter scope ahead of the function parameter scope (or class scope
48160	 in the case of out-of-line member definitions).  The function scope is
48161	 added back after template parameter synthesis below.  */
48162
48163      cp_binding_level *scope = entry_scope;
48164
48165      while (scope->kind == sk_function_parms)
48166	{
48167	  parent_scope = scope;
48168	  scope = scope->level_chain;
48169	}
48170      if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
48171	{
48172	  /* If not defining a class, then any class scope is a scope level in
48173	     an out-of-line member definition.  In this case simply wind back
48174	     beyond the first such scope to inject the template parameter list.
48175	     Otherwise wind back to the class being defined.  The latter can
48176	     occur in class member friend declarations such as:
48177
48178	       class A {
48179		 void foo (auto);
48180	       };
48181	       class B {
48182		 friend void A::foo (auto);
48183	       };
48184
48185	    The template parameter list synthesized for the friend declaration
48186	    must be injected in the scope of 'B'.  This can also occur in
48187	    erroneous cases such as:
48188
48189	       struct A {
48190	         struct B {
48191		   void foo (auto);
48192		 };
48193		 void B::foo (auto) {}
48194	       };
48195
48196	    Here the attempted definition of 'B::foo' within 'A' is ill-formed
48197	    but, nevertheless, the template parameter list synthesized for the
48198	    declarator should be injected into the scope of 'A' as if the
48199	    ill-formed template was specified explicitly.  */
48200
48201	  while (scope->kind == sk_class && !scope->defining_class_p)
48202	    {
48203	      parent_scope = scope;
48204	      scope = scope->level_chain;
48205	    }
48206	}
48207
48208      current_binding_level = scope;
48209
48210      if (scope->kind != sk_template_parms
48211	  || !function_being_declared_is_template_p (parser))
48212	{
48213	  /* Introduce a new template parameter list for implicit template
48214	     parameters.  */
48215
48216	  become_template = true;
48217
48218	  parser->implicit_template_scope
48219	      = begin_scope (sk_template_parms, NULL);
48220
48221	  ++processing_template_decl;
48222
48223	  parser->fully_implicit_function_template_p = true;
48224	  ++parser->num_template_parameter_lists;
48225	}
48226      else
48227	{
48228	  /* Synthesize implicit template parameters at the end of the explicit
48229	     template parameter list.  */
48230
48231	  gcc_assert (current_template_parms);
48232
48233	  parser->implicit_template_scope = scope;
48234
48235	  tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48236	  parser->implicit_template_parms
48237	    = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
48238	}
48239    }
48240
48241  /* Synthesize a new template parameter and track the current template
48242     parameter chain with implicit_template_parms.  */
48243
48244  tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
48245  tree synth_id = make_generic_type_name ();
48246  tree synth_tmpl_parm;
48247  bool non_type = false;
48248
48249  /* Synthesize the type template parameter.  */
48250  gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
48251  synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
48252
48253  if (become_template)
48254    current_template_parms = tree_cons (size_int (current_template_depth + 1),
48255					NULL_TREE, current_template_parms);
48256
48257  /* Attach the constraint to the parm before processing.  */
48258  tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
48259  TREE_TYPE (node) = constr;
48260  tree new_parm
48261    = process_template_parm (parser->implicit_template_parms,
48262			     input_location,
48263			     node,
48264			     /*non_type=*/non_type,
48265			     /*param_pack=*/false);
48266
48267  /* Mark the synthetic declaration "virtual". This is used when
48268     comparing template-heads to determine if whether an abbreviated
48269     function template is equivalent to an explicit template.
48270
48271     Note that DECL_ARTIFICIAL is used elsewhere for template parameters.  */
48272  if (TREE_VALUE (new_parm) != error_mark_node)
48273    DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
48274
48275  // Chain the new parameter to the list of implicit parameters.
48276  if (parser->implicit_template_parms)
48277    parser->implicit_template_parms
48278      = TREE_CHAIN (parser->implicit_template_parms);
48279  else
48280    parser->implicit_template_parms = new_parm;
48281
48282  tree new_decl = get_local_decls ();
48283  if (non_type)
48284    /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
48285    new_decl = DECL_INITIAL (new_decl);
48286
48287  /* If creating a fully implicit function template, start the new implicit
48288     template parameter list with this synthesized type, otherwise grow the
48289     current template parameter list.  */
48290
48291  if (become_template)
48292    {
48293      parent_scope->level_chain = current_binding_level;
48294
48295      tree new_parms = make_tree_vec (1);
48296      TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
48297      TREE_VALUE (current_template_parms) = new_parms;
48298    }
48299  else
48300    {
48301      tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48302      int new_parm_idx = TREE_VEC_LENGTH (new_parms);
48303      new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
48304      TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
48305    }
48306
48307  /* If the new parameter was constrained, we need to add that to the
48308     constraints in the template parameter list.  */
48309  if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
48310    {
48311      tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
48312      reqs = combine_constraint_expressions (reqs, req);
48313      TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
48314    }
48315
48316  current_binding_level = entry_scope;
48317
48318  return new_decl;
48319}
48320
48321/* Finish the declaration of a fully implicit function template.  Such a
48322   template has no explicit template parameter list so has not been through the
48323   normal template head and tail processing.  synthesize_implicit_template_parm
48324   tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
48325   provided if the declaration is a class member such that its template
48326   declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
48327   form is returned.  Otherwise NULL_TREE is returned. */
48328
48329static tree
48330finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
48331{
48332  gcc_assert (parser->fully_implicit_function_template_p);
48333
48334  if (member_decl_opt && member_decl_opt != error_mark_node
48335      && DECL_VIRTUAL_P (member_decl_opt))
48336    {
48337      error_at (DECL_SOURCE_LOCATION (member_decl_opt),
48338		"implicit templates may not be %<virtual%>");
48339      DECL_VIRTUAL_P (member_decl_opt) = false;
48340    }
48341
48342  if (member_decl_opt)
48343    member_decl_opt = finish_member_template_decl (member_decl_opt);
48344  end_template_decl ();
48345
48346  parser->fully_implicit_function_template_p = false;
48347  parser->implicit_template_parms = 0;
48348  parser->implicit_template_scope = 0;
48349  --parser->num_template_parameter_lists;
48350
48351  return member_decl_opt;
48352}
48353
48354/* Like finish_fully_implicit_template, but to be used in error
48355   recovery, rearranging scopes so that we restore the state we had
48356   before synthesize_implicit_template_parm inserted the implement
48357   template parms scope.  */
48358
48359static void
48360abort_fully_implicit_template (cp_parser *parser)
48361{
48362  cp_binding_level *return_to_scope = current_binding_level;
48363
48364  if (parser->implicit_template_scope
48365      && return_to_scope != parser->implicit_template_scope)
48366    {
48367      cp_binding_level *child = return_to_scope;
48368      for (cp_binding_level *scope = child->level_chain;
48369	   scope != parser->implicit_template_scope;
48370	   scope = child->level_chain)
48371	child = scope;
48372      child->level_chain = parser->implicit_template_scope->level_chain;
48373      parser->implicit_template_scope->level_chain = return_to_scope;
48374      current_binding_level = parser->implicit_template_scope;
48375    }
48376  else
48377    return_to_scope = return_to_scope->level_chain;
48378
48379  finish_fully_implicit_template (parser, NULL);
48380
48381  gcc_assert (current_binding_level == return_to_scope);
48382}
48383
48384/* Helper function for diagnostics that have complained about things
48385   being used with 'extern "C"' linkage.
48386
48387   Attempt to issue a note showing where the 'extern "C"' linkage began.  */
48388
48389void
48390maybe_show_extern_c_location (void)
48391{
48392  if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
48393    inform (the_parser->innermost_linkage_specification_location,
48394	    "%<extern \"C\"%> linkage started here");
48395}
48396
48397#include "gt-cp-parser.h"
48398