1/* -*- C++ -*- Parser.
2   Copyright (C) 2000-2020 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_UNIQUE_PTR
23#include "system.h"
24#include "coretypes.h"
25#include "cp-tree.h"
26#include "c-family/c-common.h"
27#include "timevar.h"
28#include "stringpool.h"
29#include "cgraph.h"
30#include "print-tree.h"
31#include "attribs.h"
32#include "trans-mem.h"
33#include "intl.h"
34#include "decl.h"
35#include "c-family/c-objc.h"
36#include "plugin.h"
37#include "tree-pretty-print.h"
38#include "parser.h"
39#include "gomp-constants.h"
40#include "omp-general.h"
41#include "omp-offload.h"
42#include "c-family/c-indentation.h"
43#include "context.h"
44#include "gcc-rich-location.h"
45#include "tree-iterator.h"
46#include "cp-name-hint.h"
47#include "memmodel.h"
48
49
50/* The lexer.  */
51
52/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53   and c-lex.c) and the C++ parser.  */
54
55/* The various kinds of non integral constant we encounter. */
56enum non_integral_constant {
57  NIC_NONE,
58  /* floating-point literal */
59  NIC_FLOAT,
60  /* %<this%> */
61  NIC_THIS,
62  /* %<__FUNCTION__%> */
63  NIC_FUNC_NAME,
64  /* %<__PRETTY_FUNCTION__%> */
65  NIC_PRETTY_FUNC,
66  /* %<__func__%> */
67  NIC_C99_FUNC,
68  /* "%<va_arg%> */
69  NIC_VA_ARG,
70  /* a cast */
71  NIC_CAST,
72  /* %<typeid%> operator */
73  NIC_TYPEID,
74  /* non-constant compound literals */
75  NIC_NCC,
76  /* a function call */
77  NIC_FUNC_CALL,
78  /* an increment */
79  NIC_INC,
80  /* an decrement */
81  NIC_DEC,
82  /* an array reference */
83  NIC_ARRAY_REF,
84  /* %<->%> */
85  NIC_ARROW,
86  /* %<.%> */
87  NIC_POINT,
88  /* the address of a label */
89  NIC_ADDR_LABEL,
90  /* %<*%> */
91  NIC_STAR,
92  /* %<&%> */
93  NIC_ADDR,
94  /* %<++%> */
95  NIC_PREINCREMENT,
96  /* %<--%> */
97  NIC_PREDECREMENT,
98  /* %<new%> */
99  NIC_NEW,
100  /* %<delete%> */
101  NIC_DEL,
102  /* calls to overloaded operators */
103  NIC_OVERLOADED,
104  /* an assignment */
105  NIC_ASSIGNMENT,
106  /* a comma operator */
107  NIC_COMMA,
108  /* a call to a constructor */
109  NIC_CONSTRUCTOR,
110  /* a transaction expression */
111  NIC_TRANSACTION
112};
113
114/* The various kinds of errors about name-lookup failing. */
115enum name_lookup_error {
116  /* NULL */
117  NLE_NULL,
118  /* is not a type */
119  NLE_TYPE,
120  /* is not a class or namespace */
121  NLE_CXX98,
122  /* is not a class, namespace, or enumeration */
123  NLE_NOT_CXX98
124};
125
126/* The various kinds of required token */
127enum required_token {
128  RT_NONE,
129  RT_SEMICOLON,  /* ';' */
130  RT_OPEN_PAREN, /* '(' */
131  RT_CLOSE_BRACE, /* '}' */
132  RT_OPEN_BRACE,  /* '{' */
133  RT_CLOSE_SQUARE, /* ']' */
134  RT_OPEN_SQUARE,  /* '[' */
135  RT_COMMA, /* ',' */
136  RT_SCOPE, /* '::' */
137  RT_LESS, /* '<' */
138  RT_GREATER, /* '>' */
139  RT_EQ, /* '=' */
140  RT_ELLIPSIS, /* '...' */
141  RT_MULT, /* '*' */
142  RT_COMPL, /* '~' */
143  RT_COLON, /* ':' */
144  RT_COLON_SCOPE, /* ':' or '::' */
145  RT_CLOSE_PAREN, /* ')' */
146  RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147  RT_PRAGMA_EOL, /* end of line */
148  RT_NAME, /* identifier */
149
150  /* The type is CPP_KEYWORD */
151  RT_NEW, /* new */
152  RT_DELETE, /* delete */
153  RT_RETURN, /* return */
154  RT_WHILE, /* while */
155  RT_EXTERN, /* extern */
156  RT_STATIC_ASSERT, /* static_assert */
157  RT_DECLTYPE, /* decltype */
158  RT_OPERATOR, /* operator */
159  RT_CLASS, /* class */
160  RT_TEMPLATE, /* template */
161  RT_NAMESPACE, /* namespace */
162  RT_USING, /* using */
163  RT_ASM, /* asm */
164  RT_TRY, /* try */
165  RT_CATCH, /* catch */
166  RT_THROW, /* throw */
167  RT_AUTO, /* auto */
168  RT_LABEL, /* __label__ */
169  RT_AT_TRY, /* @try */
170  RT_AT_SYNCHRONIZED, /* @synchronized */
171  RT_AT_THROW, /* @throw */
172
173  RT_SELECT,  /* selection-statement */
174  RT_ITERATION, /* iteration-statement */
175  RT_JUMP, /* jump-statement */
176  RT_CLASS_KEY, /* class-key */
177  RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
178  RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
179  RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
180  RT_TRANSACTION_CANCEL, /* __transaction_cancel */
181
182  RT_CO_YIELD /* co_yield */
183};
184
185/* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186   reverting it on destruction.  */
187
188class type_id_in_expr_sentinel
189{
190  cp_parser *parser;
191  bool saved;
192public:
193  type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194    : parser (parser),
195      saved (parser->in_type_id_in_expr_p)
196  { parser->in_type_id_in_expr_p = set; }
197  ~type_id_in_expr_sentinel ()
198  { parser->in_type_id_in_expr_p = saved; }
199};
200
201/* Prototypes.  */
202
203static cp_lexer *cp_lexer_new_main
204  (void);
205static cp_lexer *cp_lexer_new_from_tokens
206  (cp_token_cache *tokens);
207static void cp_lexer_destroy
208  (cp_lexer *);
209static int cp_lexer_saving_tokens
210  (const cp_lexer *);
211static cp_token *cp_lexer_token_at
212  (cp_lexer *, cp_token_position);
213static void cp_lexer_get_preprocessor_token
214  (cp_lexer *, cp_token *);
215static inline cp_token *cp_lexer_peek_token
216  (cp_lexer *);
217static cp_token *cp_lexer_peek_nth_token
218  (cp_lexer *, size_t);
219static inline bool cp_lexer_next_token_is
220  (cp_lexer *, enum cpp_ttype);
221static bool cp_lexer_next_token_is_not
222  (cp_lexer *, enum cpp_ttype);
223static bool cp_lexer_next_token_is_keyword
224  (cp_lexer *, enum rid);
225static cp_token *cp_lexer_consume_token
226  (cp_lexer *);
227static void cp_lexer_purge_token
228  (cp_lexer *);
229static void cp_lexer_purge_tokens_after
230  (cp_lexer *, cp_token_position);
231static void cp_lexer_save_tokens
232  (cp_lexer *);
233static void cp_lexer_commit_tokens
234  (cp_lexer *);
235static void cp_lexer_rollback_tokens
236  (cp_lexer *);
237static void cp_lexer_print_token
238  (FILE *, cp_token *);
239static inline bool cp_lexer_debugging_p
240  (cp_lexer *);
241static void cp_lexer_start_debugging
242  (cp_lexer *) ATTRIBUTE_UNUSED;
243static void cp_lexer_stop_debugging
244  (cp_lexer *) ATTRIBUTE_UNUSED;
245
246static cp_token_cache *cp_token_cache_new
247  (cp_token *, cp_token *);
248static tree cp_parser_late_noexcept_specifier
249  (cp_parser *, tree, tree);
250static void noexcept_override_late_checks
251  (tree, tree);
252
253static void cp_parser_initial_pragma
254  (cp_token *);
255
256static bool cp_parser_omp_declare_reduction_exprs
257  (tree, cp_parser *);
258static void cp_finalize_oacc_routine
259  (cp_parser *, tree, bool);
260
261/* Manifest constants.  */
262#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
263#define CP_SAVED_TOKEN_STACK 5
264
265/* Variables.  */
266
267/* The stream to which debugging output should be written.  */
268static FILE *cp_lexer_debug_stream;
269
270/* Nonzero if we are parsing an unevaluated operand: an operand to
271   sizeof, typeof, or alignof.  */
272int cp_unevaluated_operand;
273
274/* Dump up to NUM tokens in BUFFER to FILE starting with token
275   START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
276   first token in BUFFER.  If NUM is 0, dump all the tokens.  If
277   CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
278   highlighted by surrounding it in [[ ]].  */
279
280static void
281cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
282		      cp_token *start_token, unsigned num,
283		      cp_token *curr_token)
284{
285  unsigned i, nprinted;
286  cp_token *token;
287  bool do_print;
288
289  fprintf (file, "%u tokens\n", vec_safe_length (buffer));
290
291  if (buffer == NULL)
292    return;
293
294  if (num == 0)
295    num = buffer->length ();
296
297  if (start_token == NULL)
298    start_token = buffer->address ();
299
300  if (start_token > buffer->address ())
301    {
302      cp_lexer_print_token (file, &(*buffer)[0]);
303      fprintf (file, " ... ");
304    }
305
306  do_print = false;
307  nprinted = 0;
308  for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
309    {
310      if (token == start_token)
311	do_print = true;
312
313      if (!do_print)
314	continue;
315
316      nprinted++;
317      if (token == curr_token)
318	fprintf (file, "[[");
319
320      cp_lexer_print_token (file, token);
321
322      if (token == curr_token)
323	fprintf (file, "]]");
324
325      switch (token->type)
326	{
327	  case CPP_SEMICOLON:
328	  case CPP_OPEN_BRACE:
329	  case CPP_CLOSE_BRACE:
330	  case CPP_EOF:
331	    fputc ('\n', file);
332	    break;
333
334	  default:
335	    fputc (' ', file);
336	}
337    }
338
339  if (i == num && i < buffer->length ())
340    {
341      fprintf (file, " ... ");
342      cp_lexer_print_token (file, &buffer->last ());
343    }
344
345  fprintf (file, "\n");
346}
347
348
349/* Dump all tokens in BUFFER to stderr.  */
350
351void
352cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
353{
354  cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
355}
356
357DEBUG_FUNCTION void
358debug (vec<cp_token, va_gc> &ref)
359{
360  cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
361}
362
363DEBUG_FUNCTION void
364debug (vec<cp_token, va_gc> *ptr)
365{
366  if (ptr)
367    debug (*ptr);
368  else
369    fprintf (stderr, "<nil>\n");
370}
371
372
373/* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
374   description for T.  */
375
376static void
377cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
378{
379  if (t)
380    {
381      fprintf (file, "%s: ", desc);
382      print_node_brief (file, "", t, 0);
383    }
384}
385
386
387/* Dump parser context C to FILE.  */
388
389static void
390cp_debug_print_context (FILE *file, cp_parser_context *c)
391{
392  const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
393  fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
394  print_node_brief (file, "", c->object_type, 0);
395  fprintf (file, "}\n");
396}
397
398
399/* Print the stack of parsing contexts to FILE starting with FIRST.  */
400
401static void
402cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
403{
404  unsigned i;
405  cp_parser_context *c;
406
407  fprintf (file, "Parsing context stack:\n");
408  for (i = 0, c = first; c; c = c->next, i++)
409    {
410      fprintf (file, "\t#%u: ", i);
411      cp_debug_print_context (file, c);
412    }
413}
414
415
416/* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
417
418static void
419cp_debug_print_flag (FILE *file, const char *desc, bool flag)
420{
421  if (flag)
422    fprintf (file, "%s: true\n", desc);
423}
424
425
426/* Print an unparsed function entry UF to FILE.  */
427
428static void
429cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
430{
431  unsigned i;
432  cp_default_arg_entry *default_arg_fn;
433  tree fn;
434
435  fprintf (file, "\tFunctions with default args:\n");
436  for (i = 0;
437       vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
438       i++)
439    {
440      fprintf (file, "\t\tClass type: ");
441      print_node_brief (file, "", default_arg_fn->class_type, 0);
442      fprintf (file, "\t\tDeclaration: ");
443      print_node_brief (file, "", default_arg_fn->decl, 0);
444      fprintf (file, "\n");
445    }
446
447  fprintf (file, "\n\tFunctions with definitions that require "
448	   "post-processing\n\t\t");
449  for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
450    {
451      print_node_brief (file, "", fn, 0);
452      fprintf (file, " ");
453    }
454  fprintf (file, "\n");
455
456  fprintf (file, "\n\tNon-static data members with initializers that require "
457           "post-processing\n\t\t");
458  for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
459    {
460      print_node_brief (file, "", fn, 0);
461      fprintf (file, " ");
462    }
463  fprintf (file, "\n");
464}
465
466
467/* Print the stack of unparsed member functions S to FILE.  */
468
469static void
470cp_debug_print_unparsed_queues (FILE *file,
471				vec<cp_unparsed_functions_entry, va_gc> *s)
472{
473  unsigned i;
474  cp_unparsed_functions_entry *uf;
475
476  fprintf (file, "Unparsed functions\n");
477  for (i = 0; vec_safe_iterate (s, i, &uf); i++)
478    {
479      fprintf (file, "#%u:\n", i);
480      cp_debug_print_unparsed_function (file, uf);
481    }
482}
483
484
485/* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
486   the given PARSER.  If FILE is NULL, the output is printed on stderr. */
487
488static void
489cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
490{
491  cp_token *next_token, *first_token, *start_token;
492
493  if (file == NULL)
494    file = stderr;
495
496  next_token = parser->lexer->next_token;
497  first_token = parser->lexer->buffer->address ();
498  start_token = (next_token > first_token + window_size / 2)
499		? next_token - window_size / 2
500		: first_token;
501  cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
502			next_token);
503}
504
505
506/* Dump debugging information for the given PARSER.  If FILE is NULL,
507   the output is printed on stderr.  */
508
509void
510cp_debug_parser (FILE *file, cp_parser *parser)
511{
512  const size_t window_size = 20;
513  cp_token *token;
514  expanded_location eloc;
515
516  if (file == NULL)
517    file = stderr;
518
519  fprintf (file, "Parser state\n\n");
520  fprintf (file, "Number of tokens: %u\n",
521	   vec_safe_length (parser->lexer->buffer));
522  cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
523  cp_debug_print_tree_if_set (file, "Object scope",
524				     parser->object_scope);
525  cp_debug_print_tree_if_set (file, "Qualifying scope",
526				     parser->qualifying_scope);
527  cp_debug_print_context_stack (file, parser->context);
528  cp_debug_print_flag (file, "Allow GNU extensions",
529			      parser->allow_gnu_extensions_p);
530  cp_debug_print_flag (file, "'>' token is greater-than",
531			      parser->greater_than_is_operator_p);
532  cp_debug_print_flag (file, "Default args allowed in current "
533			      "parameter list", parser->default_arg_ok_p);
534  cp_debug_print_flag (file, "Parsing integral constant-expression",
535			      parser->integral_constant_expression_p);
536  cp_debug_print_flag (file, "Allow non-constant expression in current "
537			      "constant-expression",
538			      parser->allow_non_integral_constant_expression_p);
539  cp_debug_print_flag (file, "Seen non-constant expression",
540			      parser->non_integral_constant_expression_p);
541  cp_debug_print_flag (file, "Local names forbidden in current context",
542			      (parser->local_variables_forbidden_p
543			       & LOCAL_VARS_FORBIDDEN));
544  cp_debug_print_flag (file, "'this' forbidden in current context",
545			      (parser->local_variables_forbidden_p
546			       & THIS_FORBIDDEN));
547  cp_debug_print_flag (file, "In unbraced linkage specification",
548			      parser->in_unbraced_linkage_specification_p);
549  cp_debug_print_flag (file, "Parsing a declarator",
550			      parser->in_declarator_p);
551  cp_debug_print_flag (file, "In template argument list",
552			      parser->in_template_argument_list_p);
553  cp_debug_print_flag (file, "Parsing an iteration statement",
554			      parser->in_statement & IN_ITERATION_STMT);
555  cp_debug_print_flag (file, "Parsing a switch statement",
556			      parser->in_statement & IN_SWITCH_STMT);
557  cp_debug_print_flag (file, "Parsing a structured OpenMP block",
558			      parser->in_statement & IN_OMP_BLOCK);
559  cp_debug_print_flag (file, "Parsing an OpenMP loop",
560			      parser->in_statement & IN_OMP_FOR);
561  cp_debug_print_flag (file, "Parsing an if statement",
562			      parser->in_statement & IN_IF_STMT);
563  cp_debug_print_flag (file, "Parsing a type-id in an expression "
564			      "context", parser->in_type_id_in_expr_p);
565  cp_debug_print_flag (file, "String expressions should be translated "
566			      "to execution character set",
567			      parser->translate_strings_p);
568  cp_debug_print_flag (file, "Parsing function body outside of a "
569			      "local class", parser->in_function_body);
570  cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
571			      parser->colon_corrects_to_scope_p);
572  cp_debug_print_flag (file, "Colon doesn't start a class definition",
573			      parser->colon_doesnt_start_class_def_p);
574  cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
575			      parser->objective_c_message_context_p);
576  if (parser->type_definition_forbidden_message)
577    fprintf (file, "Error message for forbidden type definitions: %s %s\n",
578	     parser->type_definition_forbidden_message,
579	     parser->type_definition_forbidden_message_arg
580	     ? parser->type_definition_forbidden_message_arg : "<none>");
581  cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
582  fprintf (file, "Number of class definitions in progress: %u\n",
583	   parser->num_classes_being_defined);
584  fprintf (file, "Number of template parameter lists for the current "
585	   "declaration: %u\n", parser->num_template_parameter_lists);
586  cp_debug_parser_tokens (file, parser, window_size);
587  token = parser->lexer->next_token;
588  fprintf (file, "Next token to parse:\n");
589  fprintf (file, "\tToken:  ");
590  cp_lexer_print_token (file, token);
591  eloc = expand_location (token->location);
592  fprintf (file, "\n\tFile:   %s\n", eloc.file);
593  fprintf (file, "\tLine:   %d\n", eloc.line);
594  fprintf (file, "\tColumn: %d\n", eloc.column);
595}
596
597DEBUG_FUNCTION void
598debug (cp_parser &ref)
599{
600  cp_debug_parser (stderr, &ref);
601}
602
603DEBUG_FUNCTION void
604debug (cp_parser *ptr)
605{
606  if (ptr)
607    debug (*ptr);
608  else
609    fprintf (stderr, "<nil>\n");
610}
611
612/* Allocate memory for a new lexer object and return it.  */
613
614static cp_lexer *
615cp_lexer_alloc (void)
616{
617  cp_lexer *lexer;
618
619  c_common_no_more_pch ();
620
621  /* Allocate the memory.  */
622  lexer = ggc_cleared_alloc<cp_lexer> ();
623
624  /* Initially we are not debugging.  */
625  lexer->debugging_p = false;
626
627  lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628
629  /* Create the buffer.  */
630  vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631
632  return lexer;
633}
634
635
636/* Create a new main C++ lexer, the lexer that gets tokens from the
637   preprocessor.  */
638
639static cp_lexer *
640cp_lexer_new_main (void)
641{
642  cp_lexer *lexer;
643  cp_token token;
644
645  /* It's possible that parsing the first pragma will load a PCH file,
646     which is a GC collection point.  So we have to do that before
647     allocating any memory.  */
648  cp_parser_initial_pragma (&token);
649
650  lexer = cp_lexer_alloc ();
651
652  /* Put the first token in the buffer.  */
653  lexer->buffer->quick_push (token);
654
655  /* Get the remaining tokens from the preprocessor.  */
656  while (token.type != CPP_EOF)
657    {
658      cp_lexer_get_preprocessor_token (lexer, &token);
659      vec_safe_push (lexer->buffer, token);
660    }
661
662  lexer->next_token = lexer->buffer->address ();
663  lexer->last_token = lexer->next_token
664                      + lexer->buffer->length ()
665		      - 1;
666
667  /* Subsequent preprocessor diagnostics should use compiler
668     diagnostic functions to get the compiler source location.  */
669  done_lexing = true;
670
671  gcc_assert (!lexer->next_token->purged_p);
672  return lexer;
673}
674
675/* Create a new lexer whose token stream is primed with the tokens in
676   CACHE.  When these tokens are exhausted, no new tokens will be read.  */
677
678static cp_lexer *
679cp_lexer_new_from_tokens (cp_token_cache *cache)
680{
681  cp_token *first = cache->first;
682  cp_token *last = cache->last;
683  cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
684
685  /* We do not own the buffer.  */
686  lexer->buffer = NULL;
687
688  /* Insert an EOF token.  */
689  lexer->saved_type = last->type;
690  lexer->saved_keyword = last->keyword;
691  last->type = CPP_EOF;
692  last->keyword = RID_MAX;
693
694  lexer->next_token = first;
695  lexer->last_token = last;
696
697  lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
698
699  /* Initially we are not debugging.  */
700  lexer->debugging_p = false;
701
702  gcc_assert (!lexer->next_token->purged_p);
703  return lexer;
704}
705
706/* Frees all resources associated with LEXER.  */
707
708static void
709cp_lexer_destroy (cp_lexer *lexer)
710{
711  if (lexer->buffer)
712    vec_free (lexer->buffer);
713  else
714    {
715      /* Restore the token we overwrite with EOF.  */
716      lexer->last_token->type = lexer->saved_type;
717      lexer->last_token->keyword = lexer->saved_keyword;
718    }
719  lexer->saved_tokens.release ();
720  ggc_free (lexer);
721}
722
723/* This needs to be set to TRUE before the lexer-debugging infrastructure can
724   be used.  The point of this flag is to help the compiler to fold away calls
725   to cp_lexer_debugging_p within this source file at compile time, when the
726   lexer is not being debugged.  */
727
728#define LEXER_DEBUGGING_ENABLED_P false
729
730/* Returns nonzero if debugging information should be output.  */
731
732static inline bool
733cp_lexer_debugging_p (cp_lexer *lexer)
734{
735  if (!LEXER_DEBUGGING_ENABLED_P)
736    return false;
737
738  return lexer->debugging_p;
739}
740
741
742static inline cp_token_position
743cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
744{
745  return lexer->next_token - previous_p;
746}
747
748static inline cp_token *
749cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
750{
751  return pos;
752}
753
754static inline void
755cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
756{
757  lexer->next_token = cp_lexer_token_at (lexer, pos);
758}
759
760static inline cp_token_position
761cp_lexer_previous_token_position (cp_lexer *lexer)
762{
763  return cp_lexer_token_position (lexer, true);
764}
765
766static inline cp_token *
767cp_lexer_previous_token (cp_lexer *lexer)
768{
769  cp_token_position tp = cp_lexer_previous_token_position (lexer);
770
771  /* Skip past purged tokens.  */
772  while (tp->purged_p)
773    {
774      gcc_assert (tp != vec_safe_address (lexer->buffer));
775      tp--;
776    }
777
778  return cp_lexer_token_at (lexer, tp);
779}
780
781/* Overload for make_location, taking the lexer to mean the location of the
782   previous token.  */
783
784static inline location_t
785make_location (location_t caret, location_t start, cp_lexer *lexer)
786{
787  cp_token *t = cp_lexer_previous_token (lexer);
788  return make_location (caret, start, t->location);
789}
790
791/* nonzero if we are presently saving tokens.  */
792
793static inline int
794cp_lexer_saving_tokens (const cp_lexer* lexer)
795{
796  return lexer->saved_tokens.length () != 0;
797}
798
799/* Store the next token from the preprocessor in *TOKEN.  Return true
800   if we reach EOF.  If LEXER is NULL, assume we are handling an
801   initial #pragma pch_preprocess, and thus want the lexer to return
802   processed strings.  */
803
804static void
805cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
806{
807  static int is_extern_c = 0;
808
809   /* Get a new token from the preprocessor.  */
810  token->type
811    = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
812			lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
813  token->keyword = RID_MAX;
814  token->purged_p = false;
815  token->error_reported = false;
816  token->tree_check_p = false;
817
818  /* On some systems, some header files are surrounded by an
819     implicit extern "C" block.  Set a flag in the token if it
820     comes from such a header.  */
821  is_extern_c += pending_lang_change;
822  pending_lang_change = 0;
823  token->implicit_extern_c = is_extern_c > 0;
824
825  /* Check to see if this token is a keyword.  */
826  if (token->type == CPP_NAME)
827    {
828      if (IDENTIFIER_KEYWORD_P (token->u.value))
829	{
830	  /* Mark this token as a keyword.  */
831	  token->type = CPP_KEYWORD;
832	  /* Record which keyword.  */
833	  token->keyword = C_RID_CODE (token->u.value);
834	}
835      else
836	{
837          if (warn_cxx11_compat
838              && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
839              && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
840            {
841              /* Warn about the C++0x keyword (but still treat it as
842                 an identifier).  */
843	      warning_at (token->location, OPT_Wc__11_compat,
844			  "identifier %qE is a keyword in C++11",
845			  token->u.value);
846
847              /* Clear out the C_RID_CODE so we don't warn about this
848                 particular identifier-turned-keyword again.  */
849              C_SET_RID_CODE (token->u.value, RID_MAX);
850            }
851	  if (warn_cxx20_compat
852	      && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
853	      && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
854	    {
855	      /* Warn about the C++20 keyword (but still treat it as
856		 an identifier).  */
857	      warning_at (token->location, OPT_Wc__20_compat,
858			  "identifier %qE is a keyword in C++20",
859			  token->u.value);
860
861	      /* Clear out the C_RID_CODE so we don't warn about this
862		 particular identifier-turned-keyword again.  */
863	      C_SET_RID_CODE (token->u.value, RID_MAX);
864	    }
865
866	  token->keyword = RID_MAX;
867	}
868    }
869  else if (token->type == CPP_AT_NAME)
870    {
871      /* This only happens in Objective-C++; it must be a keyword.  */
872      token->type = CPP_KEYWORD;
873      switch (C_RID_CODE (token->u.value))
874	{
875	  /* Replace 'class' with '@class', 'private' with '@private',
876	     etc.  This prevents confusion with the C++ keyword
877	     'class', and makes the tokens consistent with other
878	     Objective-C 'AT' keywords.  For example '@class' is
879	     reported as RID_AT_CLASS which is consistent with
880	     '@synchronized', which is reported as
881	     RID_AT_SYNCHRONIZED.
882	  */
883	case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
884	case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
885	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
886	case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
887	case RID_THROW:     token->keyword = RID_AT_THROW; break;
888	case RID_TRY:       token->keyword = RID_AT_TRY; break;
889	case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
890	case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
891	default:            token->keyword = C_RID_CODE (token->u.value);
892	}
893    }
894}
895
896/* Update the globals input_location and the input file stack from TOKEN.  */
897static inline void
898cp_lexer_set_source_position_from_token (cp_token *token)
899{
900  if (token->type != CPP_EOF)
901    {
902      input_location = token->location;
903    }
904}
905
906/* Update the globals input_location and the input file stack from LEXER.  */
907static inline void
908cp_lexer_set_source_position (cp_lexer *lexer)
909{
910  cp_token *token = cp_lexer_peek_token (lexer);
911  cp_lexer_set_source_position_from_token (token);
912}
913
914/* Return a pointer to the next token in the token stream, but do not
915   consume it.  */
916
917static inline cp_token *
918cp_lexer_peek_token (cp_lexer *lexer)
919{
920  if (cp_lexer_debugging_p (lexer))
921    {
922      fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
923      cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
924      putc ('\n', cp_lexer_debug_stream);
925    }
926  return lexer->next_token;
927}
928
929/* Return true if the next token has the indicated TYPE.  */
930
931static inline bool
932cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
933{
934  return cp_lexer_peek_token (lexer)->type == type;
935}
936
937/* Return true if the next token does not have the indicated TYPE.  */
938
939static inline bool
940cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
941{
942  return !cp_lexer_next_token_is (lexer, type);
943}
944
945/* Return true if the next token is the indicated KEYWORD.  */
946
947static inline bool
948cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
949{
950  return cp_lexer_peek_token (lexer)->keyword == keyword;
951}
952
953static inline bool
954cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
955{
956  return cp_lexer_peek_nth_token (lexer, n)->type == type;
957}
958
959static inline bool
960cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
961{
962  return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
963}
964
965/* Return true if KEYWORD can start a decl-specifier.  */
966
967bool
968cp_keyword_starts_decl_specifier_p (enum rid keyword)
969{
970  switch (keyword)
971    {
972      /* auto specifier: storage-class-specifier in C++,
973         simple-type-specifier in C++0x.  */
974    case RID_AUTO:
975      /* Storage classes.  */
976    case RID_REGISTER:
977    case RID_STATIC:
978    case RID_EXTERN:
979    case RID_MUTABLE:
980    case RID_THREAD:
981      /* Elaborated type specifiers.  */
982    case RID_ENUM:
983    case RID_CLASS:
984    case RID_STRUCT:
985    case RID_UNION:
986    case RID_TYPENAME:
987      /* Simple type specifiers.  */
988    case RID_CHAR:
989    case RID_CHAR8:
990    case RID_CHAR16:
991    case RID_CHAR32:
992    case RID_WCHAR:
993    case RID_BOOL:
994    case RID_SHORT:
995    case RID_INT:
996    case RID_LONG:
997    case RID_SIGNED:
998    case RID_UNSIGNED:
999    case RID_FLOAT:
1000    case RID_DOUBLE:
1001    case RID_VOID:
1002      /* GNU extensions.  */
1003    case RID_ATTRIBUTE:
1004    case RID_TYPEOF:
1005      /* C++11 extensions.  */
1006    case RID_DECLTYPE:
1007    case RID_UNDERLYING_TYPE:
1008    case RID_CONSTEXPR:
1009      /* C++20 extensions.  */
1010    case RID_CONSTINIT:
1011    case RID_CONSTEVAL:
1012      return true;
1013
1014    default:
1015      if (keyword >= RID_FIRST_INT_N
1016	  && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1017	  && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1018	return true;
1019      return false;
1020    }
1021}
1022
1023/* Return true if the next token is a keyword for a decl-specifier.  */
1024
1025static bool
1026cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1027{
1028  cp_token *token;
1029
1030  token = cp_lexer_peek_token (lexer);
1031  return cp_keyword_starts_decl_specifier_p (token->keyword);
1032}
1033
1034/* Returns TRUE iff the token T begins a decltype type.  */
1035
1036static bool
1037token_is_decltype (cp_token *t)
1038{
1039  return (t->keyword == RID_DECLTYPE
1040	  || t->type == CPP_DECLTYPE);
1041}
1042
1043/* Returns TRUE iff the next token begins a decltype type.  */
1044
1045static bool
1046cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1047{
1048  cp_token *t = cp_lexer_peek_token (lexer);
1049  return token_is_decltype (t);
1050}
1051
1052/* Called when processing a token with tree_check_value; perform or defer the
1053   associated checks and return the value.  */
1054
1055static tree
1056saved_checks_value (struct tree_check *check_value)
1057{
1058  /* Perform any access checks that were deferred.  */
1059  vec<deferred_access_check, va_gc> *checks;
1060  deferred_access_check *chk;
1061  checks = check_value->checks;
1062  if (checks)
1063    {
1064      int i;
1065      FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1066	perform_or_defer_access_check (chk->binfo,
1067				       chk->decl,
1068				       chk->diag_decl, tf_warning_or_error);
1069    }
1070  /* Return the stored value.  */
1071  return check_value->value;
1072}
1073
1074/* Return a pointer to the Nth token in the token stream.  If N is 1,
1075   then this is precisely equivalent to cp_lexer_peek_token (except
1076   that it is not inline).  One would like to disallow that case, but
1077   there is one case (cp_parser_nth_token_starts_template_id) where
1078   the caller passes a variable for N and it might be 1.  */
1079
1080static cp_token *
1081cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1082{
1083  cp_token *token;
1084
1085  /* N is 1-based, not zero-based.  */
1086  gcc_assert (n > 0);
1087
1088  if (cp_lexer_debugging_p (lexer))
1089    fprintf (cp_lexer_debug_stream,
1090	     "cp_lexer: peeking ahead %ld at token: ", (long)n);
1091
1092  --n;
1093  token = lexer->next_token;
1094  while (n && token->type != CPP_EOF)
1095    {
1096      ++token;
1097      if (!token->purged_p)
1098	--n;
1099    }
1100
1101  if (cp_lexer_debugging_p (lexer))
1102    {
1103      cp_lexer_print_token (cp_lexer_debug_stream, token);
1104      putc ('\n', cp_lexer_debug_stream);
1105    }
1106
1107  return token;
1108}
1109
1110/* Return the next token, and advance the lexer's next_token pointer
1111   to point to the next non-purged token.  */
1112
1113static cp_token *
1114cp_lexer_consume_token (cp_lexer* lexer)
1115{
1116  cp_token *token = lexer->next_token;
1117
1118  gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1119
1120  do
1121    {
1122      gcc_assert (token->type != CPP_EOF);
1123      lexer->next_token++;
1124    }
1125  while (lexer->next_token->purged_p);
1126
1127  cp_lexer_set_source_position_from_token (token);
1128
1129  /* Provide debugging output.  */
1130  if (cp_lexer_debugging_p (lexer))
1131    {
1132      fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1133      cp_lexer_print_token (cp_lexer_debug_stream, token);
1134      putc ('\n', cp_lexer_debug_stream);
1135    }
1136
1137  return token;
1138}
1139
1140/* Permanently remove the next token from the token stream, and
1141   advance the next_token pointer to refer to the next non-purged
1142   token.  */
1143
1144static void
1145cp_lexer_purge_token (cp_lexer *lexer)
1146{
1147  cp_token *tok = lexer->next_token;
1148
1149  gcc_assert (tok->type != CPP_EOF);
1150  tok->purged_p = true;
1151  tok->location = UNKNOWN_LOCATION;
1152  tok->u.value = NULL_TREE;
1153  tok->keyword = RID_MAX;
1154
1155  do
1156    tok++;
1157  while (tok->purged_p);
1158  lexer->next_token = tok;
1159}
1160
1161/* Permanently remove all tokens after TOK, up to, but not
1162   including, the token that will be returned next by
1163   cp_lexer_peek_token.  */
1164
1165static void
1166cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1167{
1168  cp_token *peek = lexer->next_token;
1169
1170  gcc_assert (tok < peek);
1171
1172  for (tok++; tok != peek; tok++)
1173    {
1174      tok->purged_p = true;
1175      tok->location = UNKNOWN_LOCATION;
1176      tok->u.value = NULL_TREE;
1177      tok->keyword = RID_MAX;
1178    }
1179}
1180
1181/* Begin saving tokens.  All tokens consumed after this point will be
1182   preserved.  */
1183
1184static void
1185cp_lexer_save_tokens (cp_lexer* lexer)
1186{
1187  /* Provide debugging output.  */
1188  if (cp_lexer_debugging_p (lexer))
1189    fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1190
1191  lexer->saved_tokens.safe_push (lexer->next_token);
1192}
1193
1194/* Commit to the portion of the token stream most recently saved.  */
1195
1196static void
1197cp_lexer_commit_tokens (cp_lexer* lexer)
1198{
1199  /* Provide debugging output.  */
1200  if (cp_lexer_debugging_p (lexer))
1201    fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1202
1203  lexer->saved_tokens.pop ();
1204}
1205
1206/* Return all tokens saved since the last call to cp_lexer_save_tokens
1207   to the token stream.  Stop saving tokens.  */
1208
1209static void
1210cp_lexer_rollback_tokens (cp_lexer* lexer)
1211{
1212  /* Provide debugging output.  */
1213  if (cp_lexer_debugging_p (lexer))
1214    fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1215
1216  lexer->next_token = lexer->saved_tokens.pop ();
1217}
1218
1219/* RAII wrapper around the above functions, with sanity checking.  Creating
1220   a variable saves tokens, which are committed when the variable is
1221   destroyed unless they are explicitly rolled back by calling the rollback
1222   member function.  */
1223
1224struct saved_token_sentinel
1225{
1226  cp_lexer *lexer;
1227  unsigned len;
1228  bool commit;
1229  saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1230  {
1231    len = lexer->saved_tokens.length ();
1232    cp_lexer_save_tokens (lexer);
1233  }
1234  void rollback ()
1235  {
1236    cp_lexer_rollback_tokens (lexer);
1237    commit = false;
1238  }
1239  ~saved_token_sentinel()
1240  {
1241    if (commit)
1242      cp_lexer_commit_tokens (lexer);
1243    gcc_assert (lexer->saved_tokens.length () == len);
1244  }
1245};
1246
1247/* Print a representation of the TOKEN on the STREAM.  */
1248
1249static void
1250cp_lexer_print_token (FILE * stream, cp_token *token)
1251{
1252  /* We don't use cpp_type2name here because the parser defines
1253     a few tokens of its own.  */
1254  static const char *const token_names[] = {
1255    /* cpplib-defined token types */
1256#define OP(e, s) #e,
1257#define TK(e, s) #e,
1258    TTYPE_TABLE
1259#undef OP
1260#undef TK
1261    /* C++ parser token types - see "Manifest constants", above.  */
1262    "KEYWORD",
1263    "TEMPLATE_ID",
1264    "NESTED_NAME_SPECIFIER",
1265  };
1266
1267  /* For some tokens, print the associated data.  */
1268  switch (token->type)
1269    {
1270    case CPP_KEYWORD:
1271      /* Some keywords have a value that is not an IDENTIFIER_NODE.
1272	 For example, `struct' is mapped to an INTEGER_CST.  */
1273      if (!identifier_p (token->u.value))
1274	break;
1275      /* fall through */
1276    case CPP_NAME:
1277      fputs (IDENTIFIER_POINTER (token->u.value), stream);
1278      break;
1279
1280    case CPP_STRING:
1281    case CPP_STRING16:
1282    case CPP_STRING32:
1283    case CPP_WSTRING:
1284    case CPP_UTF8STRING:
1285      fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1286      break;
1287
1288    case CPP_NUMBER:
1289      print_generic_expr (stream, token->u.value);
1290      break;
1291
1292    default:
1293      /* If we have a name for the token, print it out.  Otherwise, we
1294	 simply give the numeric code.  */
1295      if (token->type < ARRAY_SIZE(token_names))
1296	fputs (token_names[token->type], stream);
1297      else
1298	fprintf (stream, "[%d]", token->type);
1299      break;
1300    }
1301}
1302
1303DEBUG_FUNCTION void
1304debug (cp_token &ref)
1305{
1306  cp_lexer_print_token (stderr, &ref);
1307  fprintf (stderr, "\n");
1308}
1309
1310DEBUG_FUNCTION void
1311debug (cp_token *ptr)
1312{
1313  if (ptr)
1314    debug (*ptr);
1315  else
1316    fprintf (stderr, "<nil>\n");
1317}
1318
1319
1320/* Start emitting debugging information.  */
1321
1322static void
1323cp_lexer_start_debugging (cp_lexer* lexer)
1324{
1325  if (!LEXER_DEBUGGING_ENABLED_P)
1326    fatal_error (input_location,
1327		 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1328
1329  lexer->debugging_p = true;
1330  cp_lexer_debug_stream = stderr;
1331}
1332
1333/* Stop emitting debugging information.  */
1334
1335static void
1336cp_lexer_stop_debugging (cp_lexer* lexer)
1337{
1338  if (!LEXER_DEBUGGING_ENABLED_P)
1339    fatal_error (input_location,
1340		 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1341
1342  lexer->debugging_p = false;
1343  cp_lexer_debug_stream = NULL;
1344}
1345
1346/* Create a new cp_token_cache, representing a range of tokens.  */
1347
1348static cp_token_cache *
1349cp_token_cache_new (cp_token *first, cp_token *last)
1350{
1351  cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1352  cache->first = first;
1353  cache->last = last;
1354  return cache;
1355}
1356
1357/* Diagnose if #pragma omp declare simd isn't followed immediately
1358   by function declaration or definition.  */
1359
1360static inline void
1361cp_ensure_no_omp_declare_simd (cp_parser *parser)
1362{
1363  if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1364    {
1365      error ("%<#pragma omp declare %s%> not immediately followed by "
1366	     "function declaration or definition",
1367	     parser->omp_declare_simd->variant_p ? "variant" : "simd");
1368      parser->omp_declare_simd = NULL;
1369    }
1370}
1371
1372/* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1373   and put that into "omp declare simd" attribute.  */
1374
1375static inline void
1376cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1377{
1378  if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1379    {
1380      if (fndecl == error_mark_node)
1381	{
1382	  parser->omp_declare_simd = NULL;
1383	  return;
1384	}
1385      if (TREE_CODE (fndecl) != FUNCTION_DECL)
1386	{
1387	  cp_ensure_no_omp_declare_simd (parser);
1388	  return;
1389	}
1390    }
1391}
1392
1393/* Diagnose if #pragma acc routine isn't followed immediately by function
1394   declaration or definition.  */
1395
1396static inline void
1397cp_ensure_no_oacc_routine (cp_parser *parser)
1398{
1399  if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1400    {
1401      error_at (parser->oacc_routine->loc,
1402		"%<#pragma acc routine%> not immediately followed by "
1403		"function declaration or definition");
1404      parser->oacc_routine = NULL;
1405    }
1406}
1407
1408/* Decl-specifiers.  */
1409
1410/* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1411
1412static void
1413clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1414{
1415  memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1416}
1417
1418/* Declarators.  */
1419
1420/* Nothing other than the parser should be creating declarators;
1421   declarators are a semi-syntactic representation of C++ entities.
1422   Other parts of the front end that need to create entities (like
1423   VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1424
1425static cp_declarator *make_call_declarator
1426  (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1427static cp_declarator *make_array_declarator
1428  (cp_declarator *, tree);
1429static cp_declarator *make_pointer_declarator
1430  (cp_cv_quals, cp_declarator *, tree);
1431static cp_declarator *make_reference_declarator
1432  (cp_cv_quals, cp_declarator *, bool, tree);
1433static cp_declarator *make_ptrmem_declarator
1434  (cp_cv_quals, tree, cp_declarator *, tree);
1435
1436/* An erroneous declarator.  */
1437static cp_declarator *cp_error_declarator;
1438
1439/* The obstack on which declarators and related data structures are
1440   allocated.  */
1441static struct obstack declarator_obstack;
1442
1443/* Alloc BYTES from the declarator memory pool.  */
1444
1445static inline void *
1446alloc_declarator (size_t bytes)
1447{
1448  return obstack_alloc (&declarator_obstack, bytes);
1449}
1450
1451/* Allocate a declarator of the indicated KIND.  Clear fields that are
1452   common to all declarators.  */
1453
1454static cp_declarator *
1455make_declarator (cp_declarator_kind kind)
1456{
1457  cp_declarator *declarator;
1458
1459  declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1460  declarator->kind = kind;
1461  declarator->parenthesized = UNKNOWN_LOCATION;
1462  declarator->attributes = NULL_TREE;
1463  declarator->std_attributes = NULL_TREE;
1464  declarator->declarator = NULL;
1465  declarator->parameter_pack_p = false;
1466  declarator->id_loc = UNKNOWN_LOCATION;
1467
1468  return declarator;
1469}
1470
1471/* Make a declarator for a generalized identifier.  If
1472   QUALIFYING_SCOPE is non-NULL, the identifier is
1473   QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1474   UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1475   is, if any.   */
1476
1477static cp_declarator *
1478make_id_declarator (tree qualifying_scope, tree unqualified_name,
1479		    special_function_kind sfk, location_t id_location)
1480{
1481  cp_declarator *declarator;
1482
1483  /* It is valid to write:
1484
1485       class C { void f(); };
1486       typedef C D;
1487       void D::f();
1488
1489     The standard is not clear about whether `typedef const C D' is
1490     legal; as of 2002-09-15 the committee is considering that
1491     question.  EDG 3.0 allows that syntax.  Therefore, we do as
1492     well.  */
1493  if (qualifying_scope && TYPE_P (qualifying_scope))
1494    qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1495
1496  gcc_assert (identifier_p (unqualified_name)
1497	      || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1498	      || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1499
1500  declarator = make_declarator (cdk_id);
1501  declarator->u.id.qualifying_scope = qualifying_scope;
1502  declarator->u.id.unqualified_name = unqualified_name;
1503  declarator->u.id.sfk = sfk;
1504  declarator->id_loc = id_location;
1505
1506  return declarator;
1507}
1508
1509/* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1510   of modifiers such as const or volatile to apply to the pointer
1511   type, represented as identifiers.  ATTRIBUTES represent the attributes that
1512   appertain to the pointer or reference.  */
1513
1514cp_declarator *
1515make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1516			 tree attributes)
1517{
1518  cp_declarator *declarator;
1519
1520  declarator = make_declarator (cdk_pointer);
1521  declarator->declarator = target;
1522  declarator->u.pointer.qualifiers = cv_qualifiers;
1523  declarator->u.pointer.class_type = NULL_TREE;
1524  if (target)
1525    {
1526      declarator->id_loc = target->id_loc;
1527      declarator->parameter_pack_p = target->parameter_pack_p;
1528      target->parameter_pack_p = false;
1529    }
1530  else
1531    declarator->parameter_pack_p = false;
1532
1533  declarator->std_attributes = attributes;
1534
1535  return declarator;
1536}
1537
1538/* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1539   represent the attributes that appertain to the pointer or
1540   reference.  */
1541
1542cp_declarator *
1543make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1544			   bool rvalue_ref, tree attributes)
1545{
1546  cp_declarator *declarator;
1547
1548  declarator = make_declarator (cdk_reference);
1549  declarator->declarator = target;
1550  declarator->u.reference.qualifiers = cv_qualifiers;
1551  declarator->u.reference.rvalue_ref = rvalue_ref;
1552  if (target)
1553    {
1554      declarator->id_loc = target->id_loc;
1555      declarator->parameter_pack_p = target->parameter_pack_p;
1556      target->parameter_pack_p = false;
1557    }
1558  else
1559    declarator->parameter_pack_p = false;
1560
1561  declarator->std_attributes = attributes;
1562
1563  return declarator;
1564}
1565
1566/* Like make_pointer_declarator -- but for a pointer to a non-static
1567   member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1568   appertain to the pointer or reference.  */
1569
1570cp_declarator *
1571make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1572			cp_declarator *pointee,
1573			tree attributes)
1574{
1575  cp_declarator *declarator;
1576
1577  declarator = make_declarator (cdk_ptrmem);
1578  declarator->declarator = pointee;
1579  declarator->u.pointer.qualifiers = cv_qualifiers;
1580  declarator->u.pointer.class_type = class_type;
1581
1582  if (pointee)
1583    {
1584      declarator->parameter_pack_p = pointee->parameter_pack_p;
1585      pointee->parameter_pack_p = false;
1586    }
1587  else
1588    declarator->parameter_pack_p = false;
1589
1590  declarator->std_attributes = attributes;
1591
1592  return declarator;
1593}
1594
1595/* Make a declarator for the function given by TARGET, with the
1596   indicated PARMS.  The CV_QUALIFIERS apply to the function, as in
1597   "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1598   indicates what exceptions can be thrown.  */
1599
1600cp_declarator *
1601make_call_declarator (cp_declarator *target,
1602		      tree parms,
1603		      cp_cv_quals cv_qualifiers,
1604		      cp_virt_specifiers virt_specifiers,
1605		      cp_ref_qualifier ref_qualifier,
1606		      tree tx_qualifier,
1607		      tree exception_specification,
1608		      tree late_return_type,
1609		      tree requires_clause)
1610{
1611  cp_declarator *declarator;
1612
1613  declarator = make_declarator (cdk_function);
1614  declarator->declarator = target;
1615  declarator->u.function.parameters = parms;
1616  declarator->u.function.qualifiers = cv_qualifiers;
1617  declarator->u.function.virt_specifiers = virt_specifiers;
1618  declarator->u.function.ref_qualifier = ref_qualifier;
1619  declarator->u.function.tx_qualifier = tx_qualifier;
1620  declarator->u.function.exception_specification = exception_specification;
1621  declarator->u.function.late_return_type = late_return_type;
1622  declarator->u.function.requires_clause = requires_clause;
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  return declarator;
1633}
1634
1635/* Make a declarator for an array of BOUNDS elements, each of which is
1636   defined by ELEMENT.  */
1637
1638cp_declarator *
1639make_array_declarator (cp_declarator *element, tree bounds)
1640{
1641  cp_declarator *declarator;
1642
1643  declarator = make_declarator (cdk_array);
1644  declarator->declarator = element;
1645  declarator->u.array.bounds = bounds;
1646  if (element)
1647    {
1648      declarator->id_loc = element->id_loc;
1649      declarator->parameter_pack_p = element->parameter_pack_p;
1650      element->parameter_pack_p = false;
1651    }
1652  else
1653    declarator->parameter_pack_p = false;
1654
1655  return declarator;
1656}
1657
1658/* Determine whether the declarator we've seen so far can be a
1659   parameter pack, when followed by an ellipsis.  */
1660static bool
1661declarator_can_be_parameter_pack (cp_declarator *declarator)
1662{
1663  if (declarator && declarator->parameter_pack_p)
1664    /* We already saw an ellipsis.  */
1665    return false;
1666
1667  /* Search for a declarator name, or any other declarator that goes
1668     after the point where the ellipsis could appear in a parameter
1669     pack. If we find any of these, then this declarator cannot be
1670     made into a parameter pack.  */
1671  bool found = false;
1672  while (declarator && !found)
1673    {
1674      switch ((int)declarator->kind)
1675	{
1676	case cdk_id:
1677	case cdk_array:
1678	case cdk_decomp:
1679	  found = true;
1680	  break;
1681
1682	case cdk_error:
1683	  return true;
1684
1685	default:
1686	  declarator = declarator->declarator;
1687	  break;
1688	}
1689    }
1690
1691  return !found;
1692}
1693
1694cp_parameter_declarator *no_parameters;
1695
1696/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1697   DECLARATOR and DEFAULT_ARGUMENT.  */
1698
1699cp_parameter_declarator *
1700make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1701			   cp_declarator *declarator,
1702			   tree default_argument,
1703			   location_t loc,
1704			   bool template_parameter_pack_p = false)
1705{
1706  cp_parameter_declarator *parameter;
1707
1708  parameter = ((cp_parameter_declarator *)
1709	       alloc_declarator (sizeof (cp_parameter_declarator)));
1710  parameter->next = NULL;
1711  if (decl_specifiers)
1712    parameter->decl_specifiers = *decl_specifiers;
1713  else
1714    clear_decl_specs (&parameter->decl_specifiers);
1715  parameter->declarator = declarator;
1716  parameter->default_argument = default_argument;
1717  parameter->template_parameter_pack_p = template_parameter_pack_p;
1718  parameter->loc = loc;
1719
1720  return parameter;
1721}
1722
1723/* Returns true iff DECLARATOR  is a declaration for a function.  */
1724
1725static bool
1726function_declarator_p (const cp_declarator *declarator)
1727{
1728  while (declarator)
1729    {
1730      if (declarator->kind == cdk_function
1731	  && declarator->declarator->kind == cdk_id)
1732	return true;
1733      if (declarator->kind == cdk_id
1734	  || declarator->kind == cdk_decomp
1735	  || declarator->kind == cdk_error)
1736	return false;
1737      declarator = declarator->declarator;
1738    }
1739  return false;
1740}
1741
1742/* The parser.  */
1743
1744/* Overview
1745   --------
1746
1747   A cp_parser parses the token stream as specified by the C++
1748   grammar.  Its job is purely parsing, not semantic analysis.  For
1749   example, the parser breaks the token stream into declarators,
1750   expressions, statements, and other similar syntactic constructs.
1751   It does not check that the types of the expressions on either side
1752   of an assignment-statement are compatible, or that a function is
1753   not declared with a parameter of type `void'.
1754
1755   The parser invokes routines elsewhere in the compiler to perform
1756   semantic analysis and to build up the abstract syntax tree for the
1757   code processed.
1758
1759   The parser (and the template instantiation code, which is, in a
1760   way, a close relative of parsing) are the only parts of the
1761   compiler that should be calling push_scope and pop_scope, or
1762   related functions.  The parser (and template instantiation code)
1763   keeps track of what scope is presently active; everything else
1764   should simply honor that.  (The code that generates static
1765   initializers may also need to set the scope, in order to check
1766   access control correctly when emitting the initializers.)
1767
1768   Methodology
1769   -----------
1770
1771   The parser is of the standard recursive-descent variety.  Upcoming
1772   tokens in the token stream are examined in order to determine which
1773   production to use when parsing a non-terminal.  Some C++ constructs
1774   require arbitrary look ahead to disambiguate.  For example, it is
1775   impossible, in the general case, to tell whether a statement is an
1776   expression or declaration without scanning the entire statement.
1777   Therefore, the parser is capable of "parsing tentatively."  When the
1778   parser is not sure what construct comes next, it enters this mode.
1779   Then, while we attempt to parse the construct, the parser queues up
1780   error messages, rather than issuing them immediately, and saves the
1781   tokens it consumes.  If the construct is parsed successfully, the
1782   parser "commits", i.e., it issues any queued error messages and
1783   the tokens that were being preserved are permanently discarded.
1784   If, however, the construct is not parsed successfully, the parser
1785   rolls back its state completely so that it can resume parsing using
1786   a different alternative.
1787
1788   Future Improvements
1789   -------------------
1790
1791   The performance of the parser could probably be improved substantially.
1792   We could often eliminate the need to parse tentatively by looking ahead
1793   a little bit.  In some places, this approach might not entirely eliminate
1794   the need to parse tentatively, but it might still speed up the average
1795   case.  */
1796
1797/* Flags that are passed to some parsing functions.  These values can
1798   be bitwise-ored together.  */
1799
1800enum
1801{
1802  /* No flags.  */
1803  CP_PARSER_FLAGS_NONE = 0x0,
1804  /* The construct is optional.  If it is not present, then no error
1805     should be issued.  */
1806  CP_PARSER_FLAGS_OPTIONAL = 0x1,
1807  /* When parsing a type-specifier, treat user-defined type-names
1808     as non-type identifiers.  */
1809  CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1810  /* When parsing a type-specifier, do not try to parse a class-specifier
1811     or enum-specifier.  */
1812  CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1813  /* When parsing a decl-specifier-seq, only allow type-specifier or
1814     constexpr.  */
1815  CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1816  /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1817     for C++2A consteval.  */
1818  CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1819  /* When parsing a decl-specifier-seq, allow missing typename.  */
1820  CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1821  /* When parsing of the noexcept-specifier should be delayed.  */
1822  CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1823  /* When parsing a consteval declarator.  */
1824  CP_PARSER_FLAGS_CONSTEVAL = 0x80
1825};
1826
1827/* This type is used for parameters and variables which hold
1828   combinations of the above flags.  */
1829typedef int cp_parser_flags;
1830
1831/* The different kinds of declarators we want to parse.  */
1832
1833enum cp_parser_declarator_kind
1834{
1835  /* We want an abstract declarator.  */
1836  CP_PARSER_DECLARATOR_ABSTRACT,
1837  /* We want a named declarator.  */
1838  CP_PARSER_DECLARATOR_NAMED,
1839  /* We don't mind, but the name must be an unqualified-id.  */
1840  CP_PARSER_DECLARATOR_EITHER
1841};
1842
1843/* The precedence values used to parse binary expressions.  The minimum value
1844   of PREC must be 1, because zero is reserved to quickly discriminate
1845   binary operators from other tokens.  */
1846
1847enum cp_parser_prec
1848{
1849  PREC_NOT_OPERATOR,
1850  PREC_LOGICAL_OR_EXPRESSION,
1851  PREC_LOGICAL_AND_EXPRESSION,
1852  PREC_INCLUSIVE_OR_EXPRESSION,
1853  PREC_EXCLUSIVE_OR_EXPRESSION,
1854  PREC_AND_EXPRESSION,
1855  PREC_EQUALITY_EXPRESSION,
1856  PREC_RELATIONAL_EXPRESSION,
1857  PREC_SPACESHIP_EXPRESSION,
1858  PREC_SHIFT_EXPRESSION,
1859  PREC_ADDITIVE_EXPRESSION,
1860  PREC_MULTIPLICATIVE_EXPRESSION,
1861  PREC_PM_EXPRESSION,
1862  NUM_PREC_VALUES = PREC_PM_EXPRESSION
1863};
1864
1865/* A mapping from a token type to a corresponding tree node type, with a
1866   precedence value.  */
1867
1868struct cp_parser_binary_operations_map_node
1869{
1870  /* The token type.  */
1871  enum cpp_ttype token_type;
1872  /* The corresponding tree code.  */
1873  enum tree_code tree_type;
1874  /* The precedence of this operator.  */
1875  enum cp_parser_prec prec;
1876};
1877
1878struct cp_parser_expression_stack_entry
1879{
1880  /* Left hand side of the binary operation we are currently
1881     parsing.  */
1882  cp_expr lhs;
1883  /* Original tree code for left hand side, if it was a binary
1884     expression itself (used for -Wparentheses).  */
1885  enum tree_code lhs_type;
1886  /* Tree code for the binary operation we are parsing.  */
1887  enum tree_code tree_type;
1888  /* Precedence of the binary operation we are parsing.  */
1889  enum cp_parser_prec prec;
1890  /* Location of the binary operation we are parsing.  */
1891  location_t loc;
1892};
1893
1894/* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1895   entries because precedence levels on the stack are monotonically
1896   increasing.  */
1897typedef struct cp_parser_expression_stack_entry
1898  cp_parser_expression_stack[NUM_PREC_VALUES];
1899
1900/* Prototypes.  */
1901
1902/* Constructors and destructors.  */
1903
1904static cp_parser_context *cp_parser_context_new
1905  (cp_parser_context *);
1906
1907/* Class variables.  */
1908
1909static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1910
1911/* The operator-precedence table used by cp_parser_binary_expression.
1912   Transformed into an associative array (binops_by_token) by
1913   cp_parser_new.  */
1914
1915static const cp_parser_binary_operations_map_node binops[] = {
1916  { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1917  { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1918
1919  { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1920  { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1921  { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1922
1923  { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1924  { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1925
1926  { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1927  { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1928
1929  { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
1930
1931  { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1932  { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1933  { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1934  { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1935
1936  { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1937  { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1938
1939  { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1940
1941  { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1942
1943  { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1944
1945  { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1946
1947  { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1948};
1949
1950/* The same as binops, but initialized by cp_parser_new so that
1951   binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1952   for speed.  */
1953static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1954
1955/* Constructors and destructors.  */
1956
1957/* Construct a new context.  The context below this one on the stack
1958   is given by NEXT.  */
1959
1960static cp_parser_context *
1961cp_parser_context_new (cp_parser_context* next)
1962{
1963  cp_parser_context *context;
1964
1965  /* Allocate the storage.  */
1966  if (cp_parser_context_free_list != NULL)
1967    {
1968      /* Pull the first entry from the free list.  */
1969      context = cp_parser_context_free_list;
1970      cp_parser_context_free_list = context->next;
1971      memset (context, 0, sizeof (*context));
1972    }
1973  else
1974    context = ggc_cleared_alloc<cp_parser_context> ();
1975
1976  /* No errors have occurred yet in this context.  */
1977  context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1978  /* If this is not the bottommost context, copy information that we
1979     need from the previous context.  */
1980  if (next)
1981    {
1982      /* If, in the NEXT context, we are parsing an `x->' or `x.'
1983	 expression, then we are parsing one in this context, too.  */
1984      context->object_type = next->object_type;
1985      /* Thread the stack.  */
1986      context->next = next;
1987    }
1988
1989  return context;
1990}
1991
1992/* Managing the unparsed function queues.  */
1993
1994#define unparsed_funs_with_default_args \
1995  parser->unparsed_queues->last ().funs_with_default_args
1996#define unparsed_funs_with_definitions \
1997  parser->unparsed_queues->last ().funs_with_definitions
1998#define unparsed_nsdmis \
1999  parser->unparsed_queues->last ().nsdmis
2000#define unparsed_noexcepts \
2001  parser->unparsed_queues->last ().noexcepts
2002
2003static void
2004push_unparsed_function_queues (cp_parser *parser)
2005{
2006  cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2007  vec_safe_push (parser->unparsed_queues, e);
2008}
2009
2010static void
2011pop_unparsed_function_queues (cp_parser *parser)
2012{
2013  release_tree_vector (unparsed_funs_with_definitions);
2014  parser->unparsed_queues->pop ();
2015}
2016
2017/* Prototypes.  */
2018
2019/* Constructors and destructors.  */
2020
2021static cp_parser *cp_parser_new
2022  (void);
2023
2024/* Routines to parse various constructs.
2025
2026   Those that return `tree' will return the error_mark_node (rather
2027   than NULL_TREE) if a parse error occurs, unless otherwise noted.
2028   Sometimes, they will return an ordinary node if error-recovery was
2029   attempted, even though a parse error occurred.  So, to check
2030   whether or not a parse error occurred, you should always use
2031   cp_parser_error_occurred.  If the construct is optional (indicated
2032   either by an `_opt' in the name of the function that does the
2033   parsing or via a FLAGS parameter), then NULL_TREE is returned if
2034   the construct is not present.  */
2035
2036/* Lexical conventions [gram.lex]  */
2037
2038static cp_expr cp_parser_identifier
2039  (cp_parser *);
2040static cp_expr cp_parser_string_literal
2041  (cp_parser *, bool, bool, bool);
2042static cp_expr cp_parser_userdef_char_literal
2043  (cp_parser *);
2044static tree cp_parser_userdef_string_literal
2045  (tree);
2046static cp_expr cp_parser_userdef_numeric_literal
2047  (cp_parser *);
2048
2049/* Basic concepts [gram.basic]  */
2050
2051static void cp_parser_translation_unit (cp_parser *);
2052
2053/* Expressions [gram.expr]  */
2054
2055static cp_expr cp_parser_primary_expression
2056  (cp_parser *, bool, bool, bool, cp_id_kind *);
2057static cp_expr cp_parser_id_expression
2058  (cp_parser *, bool, bool, bool *, bool, bool);
2059static cp_expr cp_parser_unqualified_id
2060  (cp_parser *, bool, bool, bool, bool);
2061static tree cp_parser_nested_name_specifier_opt
2062  (cp_parser *, bool, bool, bool, bool, bool = false);
2063static tree cp_parser_nested_name_specifier
2064  (cp_parser *, bool, bool, bool, bool);
2065static tree cp_parser_qualifying_entity
2066  (cp_parser *, bool, bool, bool, bool, bool);
2067static cp_expr cp_parser_postfix_expression
2068  (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2069static tree cp_parser_postfix_open_square_expression
2070  (cp_parser *, tree, bool, bool);
2071static tree cp_parser_postfix_dot_deref_expression
2072  (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2073static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2074  (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2075   bool = false);
2076/* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
2077enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2078static void cp_parser_pseudo_destructor_name
2079  (cp_parser *, tree, tree *, tree *);
2080static cp_expr cp_parser_unary_expression
2081  (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2082static enum tree_code cp_parser_unary_operator
2083  (cp_token *);
2084static tree cp_parser_has_attribute_expression
2085  (cp_parser *);
2086static tree cp_parser_new_expression
2087  (cp_parser *);
2088static vec<tree, va_gc> *cp_parser_new_placement
2089  (cp_parser *);
2090static tree cp_parser_new_type_id
2091  (cp_parser *, tree *);
2092static cp_declarator *cp_parser_new_declarator_opt
2093  (cp_parser *);
2094static cp_declarator *cp_parser_direct_new_declarator
2095  (cp_parser *);
2096static vec<tree, va_gc> *cp_parser_new_initializer
2097  (cp_parser *);
2098static tree cp_parser_delete_expression
2099  (cp_parser *);
2100static cp_expr cp_parser_cast_expression
2101  (cp_parser *, bool, bool, bool, cp_id_kind *);
2102static cp_expr cp_parser_binary_expression
2103  (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2104static tree cp_parser_question_colon_clause
2105  (cp_parser *, cp_expr);
2106static cp_expr cp_parser_assignment_expression
2107  (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2108static enum tree_code cp_parser_assignment_operator_opt
2109  (cp_parser *);
2110static cp_expr cp_parser_expression
2111  (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2112static cp_expr cp_parser_constant_expression
2113  (cp_parser *, bool = false, bool * = NULL, bool = false);
2114static cp_expr cp_parser_builtin_offsetof
2115  (cp_parser *);
2116static cp_expr cp_parser_lambda_expression
2117  (cp_parser *);
2118static void cp_parser_lambda_introducer
2119  (cp_parser *, tree);
2120static bool cp_parser_lambda_declarator_opt
2121  (cp_parser *, tree);
2122static void cp_parser_lambda_body
2123  (cp_parser *, tree);
2124
2125/* Statements [gram.stmt.stmt]  */
2126
2127static void cp_parser_statement
2128  (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2129static void cp_parser_label_for_labeled_statement
2130(cp_parser *, tree);
2131static tree cp_parser_expression_statement
2132  (cp_parser *, tree);
2133static tree cp_parser_compound_statement
2134  (cp_parser *, tree, int, bool);
2135static void cp_parser_statement_seq_opt
2136  (cp_parser *, tree);
2137static tree cp_parser_selection_statement
2138  (cp_parser *, bool *, vec<tree> *);
2139static tree cp_parser_condition
2140  (cp_parser *);
2141static tree cp_parser_iteration_statement
2142  (cp_parser *, bool *, bool, unsigned short);
2143static bool cp_parser_init_statement
2144  (cp_parser *, tree *decl);
2145static tree cp_parser_for
2146  (cp_parser *, bool, unsigned short);
2147static tree cp_parser_c_for
2148  (cp_parser *, tree, tree, bool, unsigned short);
2149static tree cp_parser_range_for
2150  (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2151static void do_range_for_auto_deduction
2152  (tree, tree);
2153static tree cp_parser_perform_range_for_lookup
2154  (tree, tree *, tree *);
2155static tree cp_parser_range_for_member_function
2156  (tree, tree);
2157static tree cp_parser_jump_statement
2158  (cp_parser *);
2159static void cp_parser_declaration_statement
2160  (cp_parser *);
2161
2162static tree cp_parser_implicitly_scoped_statement
2163  (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2164static void cp_parser_already_scoped_statement
2165  (cp_parser *, bool *, const token_indent_info &);
2166
2167/* Declarations [gram.dcl.dcl] */
2168
2169static void cp_parser_declaration_seq_opt
2170  (cp_parser *);
2171static void cp_parser_declaration
2172  (cp_parser *);
2173static void cp_parser_toplevel_declaration
2174  (cp_parser *);
2175static void cp_parser_block_declaration
2176  (cp_parser *, bool);
2177static void cp_parser_simple_declaration
2178  (cp_parser *, bool, tree *);
2179static void cp_parser_decl_specifier_seq
2180  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2181static tree cp_parser_storage_class_specifier_opt
2182  (cp_parser *);
2183static tree cp_parser_function_specifier_opt
2184  (cp_parser *, cp_decl_specifier_seq *);
2185static tree cp_parser_type_specifier
2186  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2187   int *, bool *);
2188static tree cp_parser_simple_type_specifier
2189  (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2190static tree cp_parser_placeholder_type_specifier
2191  (cp_parser *, location_t, tree, bool);
2192static tree cp_parser_type_name
2193  (cp_parser *, bool);
2194static tree cp_parser_nonclass_name
2195  (cp_parser* parser);
2196static tree cp_parser_elaborated_type_specifier
2197  (cp_parser *, bool, bool);
2198static tree cp_parser_enum_specifier
2199  (cp_parser *);
2200static void cp_parser_enumerator_list
2201  (cp_parser *, tree);
2202static void cp_parser_enumerator_definition
2203  (cp_parser *, tree);
2204static tree cp_parser_namespace_name
2205  (cp_parser *);
2206static void cp_parser_namespace_definition
2207  (cp_parser *);
2208static void cp_parser_namespace_body
2209  (cp_parser *);
2210static tree cp_parser_qualified_namespace_specifier
2211  (cp_parser *);
2212static void cp_parser_namespace_alias_definition
2213  (cp_parser *);
2214static bool cp_parser_using_declaration
2215  (cp_parser *, bool);
2216static void cp_parser_using_directive
2217  (cp_parser *);
2218static tree cp_parser_alias_declaration
2219  (cp_parser *);
2220static void cp_parser_asm_definition
2221  (cp_parser *);
2222static void cp_parser_linkage_specification
2223  (cp_parser *);
2224static void cp_parser_static_assert
2225  (cp_parser *, bool);
2226static tree cp_parser_decltype
2227  (cp_parser *);
2228static tree cp_parser_decomposition_declaration
2229  (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2230
2231/* Declarators [gram.dcl.decl] */
2232
2233static tree cp_parser_init_declarator
2234  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2235   vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2236   location_t *, tree *);
2237static cp_declarator *cp_parser_declarator
2238  (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2239   bool, bool, bool);
2240static cp_declarator *cp_parser_direct_declarator
2241  (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2242   bool);
2243static enum tree_code cp_parser_ptr_operator
2244  (cp_parser *, tree *, cp_cv_quals *, tree *);
2245static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2246  (cp_parser *);
2247static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2248  (cp_parser *);
2249static cp_ref_qualifier cp_parser_ref_qualifier_opt
2250  (cp_parser *);
2251static tree cp_parser_tx_qualifier_opt
2252  (cp_parser *);
2253static tree cp_parser_late_return_type_opt
2254  (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2255static tree cp_parser_declarator_id
2256  (cp_parser *, bool);
2257static tree cp_parser_type_id
2258  (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2259static tree cp_parser_template_type_arg
2260  (cp_parser *);
2261static tree cp_parser_trailing_type_id (cp_parser *);
2262static tree cp_parser_type_id_1
2263  (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2264static void cp_parser_type_specifier_seq
2265  (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2266static tree cp_parser_parameter_declaration_clause
2267  (cp_parser *, cp_parser_flags);
2268static tree cp_parser_parameter_declaration_list
2269  (cp_parser *, cp_parser_flags);
2270static cp_parameter_declarator *cp_parser_parameter_declaration
2271  (cp_parser *, cp_parser_flags, bool, bool *);
2272static tree cp_parser_default_argument
2273  (cp_parser *, bool);
2274static void cp_parser_function_body
2275  (cp_parser *, bool);
2276static tree cp_parser_initializer
2277  (cp_parser *, bool *, bool *, bool = false);
2278static cp_expr cp_parser_initializer_clause
2279  (cp_parser *, bool *);
2280static cp_expr cp_parser_braced_list
2281  (cp_parser*, bool*);
2282static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2283  (cp_parser *, bool *, bool *);
2284
2285static void cp_parser_ctor_initializer_opt_and_function_body
2286  (cp_parser *, bool);
2287
2288static tree cp_parser_late_parsing_omp_declare_simd
2289  (cp_parser *, tree);
2290
2291static tree cp_parser_late_parsing_oacc_routine
2292  (cp_parser *, tree);
2293
2294static tree synthesize_implicit_template_parm
2295  (cp_parser *, tree);
2296static tree finish_fully_implicit_template
2297  (cp_parser *, tree);
2298static void abort_fully_implicit_template
2299  (cp_parser *);
2300
2301/* Classes [gram.class] */
2302
2303static tree cp_parser_class_name
2304  (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2305static tree cp_parser_class_specifier
2306  (cp_parser *);
2307static tree cp_parser_class_head
2308  (cp_parser *, bool *);
2309static enum tag_types cp_parser_class_key
2310  (cp_parser *);
2311static void cp_parser_type_parameter_key
2312  (cp_parser* parser);
2313static void cp_parser_member_specification_opt
2314  (cp_parser *);
2315static void cp_parser_member_declaration
2316  (cp_parser *);
2317static tree cp_parser_pure_specifier
2318  (cp_parser *);
2319static tree cp_parser_constant_initializer
2320  (cp_parser *);
2321
2322/* Derived classes [gram.class.derived] */
2323
2324static tree cp_parser_base_clause
2325  (cp_parser *);
2326static tree cp_parser_base_specifier
2327  (cp_parser *);
2328
2329/* Special member functions [gram.special] */
2330
2331static tree cp_parser_conversion_function_id
2332  (cp_parser *);
2333static tree cp_parser_conversion_type_id
2334  (cp_parser *);
2335static cp_declarator *cp_parser_conversion_declarator_opt
2336  (cp_parser *);
2337static void cp_parser_ctor_initializer_opt
2338  (cp_parser *);
2339static void cp_parser_mem_initializer_list
2340  (cp_parser *);
2341static tree cp_parser_mem_initializer
2342  (cp_parser *);
2343static tree cp_parser_mem_initializer_id
2344  (cp_parser *);
2345
2346/* Overloading [gram.over] */
2347
2348static cp_expr cp_parser_operator_function_id
2349  (cp_parser *);
2350static cp_expr cp_parser_operator
2351  (cp_parser *, location_t);
2352
2353/* Templates [gram.temp] */
2354
2355static void cp_parser_template_declaration
2356  (cp_parser *, bool);
2357static tree cp_parser_template_parameter_list
2358  (cp_parser *);
2359static tree cp_parser_template_parameter
2360  (cp_parser *, bool *, bool *);
2361static tree cp_parser_type_parameter
2362  (cp_parser *, bool *);
2363static tree cp_parser_template_id
2364  (cp_parser *, bool, bool, enum tag_types, bool);
2365static tree cp_parser_template_id_expr
2366  (cp_parser *, bool, bool, bool);
2367static tree cp_parser_template_name
2368  (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2369static tree cp_parser_template_argument_list
2370  (cp_parser *);
2371static tree cp_parser_template_argument
2372  (cp_parser *);
2373static void cp_parser_explicit_instantiation
2374  (cp_parser *);
2375static void cp_parser_explicit_specialization
2376  (cp_parser *);
2377
2378/* Exception handling [gram.except] */
2379
2380static tree cp_parser_try_block
2381  (cp_parser *);
2382static void cp_parser_function_try_block
2383  (cp_parser *);
2384static void cp_parser_handler_seq
2385  (cp_parser *);
2386static void cp_parser_handler
2387  (cp_parser *);
2388static tree cp_parser_exception_declaration
2389  (cp_parser *);
2390static tree cp_parser_throw_expression
2391  (cp_parser *);
2392static tree cp_parser_exception_specification_opt
2393  (cp_parser *, cp_parser_flags, cp_cv_quals);
2394static tree cp_parser_type_id_list
2395  (cp_parser *);
2396static tree cp_parser_noexcept_specification_opt
2397  (cp_parser *, cp_parser_flags, bool, bool *, bool, cp_cv_quals);
2398
2399/* GNU Extensions */
2400
2401static tree cp_parser_asm_specification_opt
2402  (cp_parser *);
2403static tree cp_parser_asm_operand_list
2404  (cp_parser *);
2405static tree cp_parser_asm_clobber_list
2406  (cp_parser *);
2407static tree cp_parser_asm_label_list
2408  (cp_parser *);
2409static bool cp_next_tokens_can_be_attribute_p
2410  (cp_parser *);
2411static bool cp_next_tokens_can_be_gnu_attribute_p
2412  (cp_parser *);
2413static bool cp_next_tokens_can_be_std_attribute_p
2414  (cp_parser *);
2415static bool cp_nth_tokens_can_be_std_attribute_p
2416  (cp_parser *, size_t);
2417static bool cp_nth_tokens_can_be_gnu_attribute_p
2418  (cp_parser *, size_t);
2419static bool cp_nth_tokens_can_be_attribute_p
2420  (cp_parser *, size_t);
2421static tree cp_parser_attributes_opt
2422  (cp_parser *);
2423static tree cp_parser_gnu_attributes_opt
2424  (cp_parser *);
2425static tree cp_parser_gnu_attribute_list
2426  (cp_parser *, bool = false);
2427static tree cp_parser_std_attribute
2428  (cp_parser *, tree);
2429static tree cp_parser_std_attribute_spec
2430  (cp_parser *);
2431static tree cp_parser_std_attribute_spec_seq
2432  (cp_parser *);
2433static size_t cp_parser_skip_attributes_opt
2434  (cp_parser *, size_t);
2435static bool cp_parser_extension_opt
2436  (cp_parser *, int *);
2437static void cp_parser_label_declaration
2438  (cp_parser *);
2439
2440/* Concept Extensions */
2441
2442static tree cp_parser_concept_definition
2443  (cp_parser *);
2444static tree cp_parser_constraint_expression
2445  (cp_parser *);
2446static tree cp_parser_requires_clause_opt
2447  (cp_parser *, bool);
2448static tree cp_parser_requires_expression
2449  (cp_parser *);
2450static tree cp_parser_requirement_parameter_list
2451  (cp_parser *);
2452static tree cp_parser_requirement_body
2453  (cp_parser *);
2454static tree cp_parser_requirement_seq
2455  (cp_parser *);
2456static tree cp_parser_requirement
2457  (cp_parser *);
2458static tree cp_parser_simple_requirement
2459  (cp_parser *);
2460static tree cp_parser_compound_requirement
2461  (cp_parser *);
2462static tree cp_parser_type_requirement
2463  (cp_parser *);
2464static tree cp_parser_nested_requirement
2465  (cp_parser *);
2466
2467/* Transactional Memory Extensions */
2468
2469static tree cp_parser_transaction
2470  (cp_parser *, cp_token *);
2471static tree cp_parser_transaction_expression
2472  (cp_parser *, enum rid);
2473static void cp_parser_function_transaction
2474  (cp_parser *, enum rid);
2475static tree cp_parser_transaction_cancel
2476  (cp_parser *);
2477
2478/* Coroutine extensions.  */
2479
2480static tree cp_parser_yield_expression
2481  (cp_parser *);
2482
2483
2484enum pragma_context {
2485  pragma_external,
2486  pragma_member,
2487  pragma_objc_icode,
2488  pragma_stmt,
2489  pragma_compound
2490};
2491static bool cp_parser_pragma
2492  (cp_parser *, enum pragma_context, bool *);
2493
2494/* Objective-C++ Productions */
2495
2496static tree cp_parser_objc_message_receiver
2497  (cp_parser *);
2498static tree cp_parser_objc_message_args
2499  (cp_parser *);
2500static tree cp_parser_objc_message_expression
2501  (cp_parser *);
2502static cp_expr cp_parser_objc_encode_expression
2503  (cp_parser *);
2504static tree cp_parser_objc_defs_expression
2505  (cp_parser *);
2506static tree cp_parser_objc_protocol_expression
2507  (cp_parser *);
2508static tree cp_parser_objc_selector_expression
2509  (cp_parser *);
2510static cp_expr cp_parser_objc_expression
2511  (cp_parser *);
2512static bool cp_parser_objc_selector_p
2513  (enum cpp_ttype);
2514static tree cp_parser_objc_selector
2515  (cp_parser *);
2516static tree cp_parser_objc_protocol_refs_opt
2517  (cp_parser *);
2518static void cp_parser_objc_declaration
2519  (cp_parser *, tree);
2520static tree cp_parser_objc_statement
2521  (cp_parser *);
2522static bool cp_parser_objc_valid_prefix_attributes
2523  (cp_parser *, tree *);
2524static void cp_parser_objc_at_property_declaration
2525  (cp_parser *) ;
2526static void cp_parser_objc_at_synthesize_declaration
2527  (cp_parser *) ;
2528static void cp_parser_objc_at_dynamic_declaration
2529  (cp_parser *) ;
2530static tree cp_parser_objc_struct_declaration
2531  (cp_parser *) ;
2532
2533/* Utility Routines */
2534
2535static cp_expr cp_parser_lookup_name
2536  (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2537static tree cp_parser_lookup_name_simple
2538  (cp_parser *, tree, location_t);
2539static tree cp_parser_maybe_treat_template_as_class
2540  (tree, bool);
2541static bool cp_parser_check_declarator_template_parameters
2542  (cp_parser *, cp_declarator *, location_t);
2543static bool cp_parser_check_template_parameters
2544  (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2545static cp_expr cp_parser_simple_cast_expression
2546  (cp_parser *);
2547static tree cp_parser_global_scope_opt
2548  (cp_parser *, bool);
2549static bool cp_parser_constructor_declarator_p
2550  (cp_parser *, cp_parser_flags, bool);
2551static tree cp_parser_function_definition_from_specifiers_and_declarator
2552  (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2553static tree cp_parser_function_definition_after_declarator
2554  (cp_parser *, bool);
2555static bool cp_parser_template_declaration_after_export
2556  (cp_parser *, bool);
2557static void cp_parser_perform_template_parameter_access_checks
2558  (vec<deferred_access_check, va_gc> *);
2559static tree cp_parser_single_declaration
2560  (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2561static cp_expr cp_parser_functional_cast
2562  (cp_parser *, tree);
2563static tree cp_parser_save_member_function_body
2564  (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2565static tree cp_parser_save_nsdmi
2566  (cp_parser *);
2567static tree cp_parser_enclosed_template_argument_list
2568  (cp_parser *);
2569static void cp_parser_save_default_args
2570  (cp_parser *, tree);
2571static void cp_parser_late_parsing_for_member
2572  (cp_parser *, tree);
2573static tree cp_parser_late_parse_one_default_arg
2574  (cp_parser *, tree, tree, tree);
2575static void cp_parser_late_parsing_nsdmi
2576  (cp_parser *, tree);
2577static void cp_parser_late_parsing_default_args
2578  (cp_parser *, tree);
2579static tree cp_parser_sizeof_operand
2580  (cp_parser *, enum rid);
2581static cp_expr cp_parser_trait_expr
2582  (cp_parser *, enum rid);
2583static bool cp_parser_declares_only_class_p
2584  (cp_parser *);
2585static void cp_parser_set_storage_class
2586  (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2587static void cp_parser_set_decl_spec_type
2588  (cp_decl_specifier_seq *, tree, cp_token *, bool);
2589static void set_and_check_decl_spec_loc
2590  (cp_decl_specifier_seq *decl_specs,
2591   cp_decl_spec ds, cp_token *);
2592static bool cp_parser_friend_p
2593  (const cp_decl_specifier_seq *);
2594static void cp_parser_required_error
2595  (cp_parser *, required_token, bool, location_t);
2596static cp_token *cp_parser_require
2597  (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2598static cp_token *cp_parser_require_keyword
2599  (cp_parser *, enum rid, required_token);
2600static bool cp_parser_token_starts_function_definition_p
2601  (cp_token *);
2602static bool cp_parser_next_token_starts_class_definition_p
2603  (cp_parser *);
2604static bool cp_parser_next_token_ends_template_argument_p
2605  (cp_parser *);
2606static bool cp_parser_nth_token_starts_template_argument_list_p
2607  (cp_parser *, size_t);
2608static enum tag_types cp_parser_token_is_class_key
2609  (cp_token *);
2610static enum tag_types cp_parser_token_is_type_parameter_key
2611  (cp_token *);
2612static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2613static void cp_parser_check_class_key
2614(cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2615static void cp_parser_check_access_in_redeclaration
2616  (tree type, location_t location);
2617static bool cp_parser_optional_template_keyword
2618  (cp_parser *);
2619static void cp_parser_pre_parsed_nested_name_specifier
2620  (cp_parser *);
2621static bool cp_parser_cache_group
2622  (cp_parser *, enum cpp_ttype, unsigned);
2623static tree cp_parser_cache_defarg
2624  (cp_parser *parser, bool nsdmi);
2625static void cp_parser_parse_tentatively
2626  (cp_parser *);
2627static void cp_parser_commit_to_tentative_parse
2628  (cp_parser *);
2629static void cp_parser_commit_to_topmost_tentative_parse
2630  (cp_parser *);
2631static void cp_parser_abort_tentative_parse
2632  (cp_parser *);
2633static bool cp_parser_parse_definitely
2634  (cp_parser *);
2635static inline bool cp_parser_parsing_tentatively
2636  (cp_parser *);
2637static bool cp_parser_uncommitted_to_tentative_parse_p
2638  (cp_parser *);
2639static void cp_parser_error
2640  (cp_parser *, const char *);
2641static void cp_parser_name_lookup_error
2642  (cp_parser *, tree, tree, name_lookup_error, location_t);
2643static bool cp_parser_simulate_error
2644  (cp_parser *);
2645static bool cp_parser_check_type_definition
2646  (cp_parser *);
2647static void cp_parser_check_for_definition_in_return_type
2648  (cp_declarator *, tree, location_t type_location);
2649static void cp_parser_check_for_invalid_template_id
2650  (cp_parser *, tree, enum tag_types, location_t location);
2651static bool cp_parser_non_integral_constant_expression
2652  (cp_parser *, non_integral_constant);
2653static void cp_parser_diagnose_invalid_type_name
2654  (cp_parser *, tree, location_t);
2655static bool cp_parser_parse_and_diagnose_invalid_type_name
2656  (cp_parser *);
2657static int cp_parser_skip_to_closing_parenthesis
2658  (cp_parser *, bool, bool, bool);
2659static void cp_parser_skip_to_end_of_statement
2660  (cp_parser *);
2661static void cp_parser_consume_semicolon_at_end_of_statement
2662  (cp_parser *);
2663static void cp_parser_skip_to_end_of_block_or_statement
2664  (cp_parser *);
2665static bool cp_parser_skip_to_closing_brace
2666  (cp_parser *);
2667static void cp_parser_skip_to_end_of_template_parameter_list
2668  (cp_parser *);
2669static void cp_parser_skip_to_pragma_eol
2670  (cp_parser*, cp_token *);
2671static bool cp_parser_error_occurred
2672  (cp_parser *);
2673static bool cp_parser_allow_gnu_extensions_p
2674  (cp_parser *);
2675static bool cp_parser_is_pure_string_literal
2676  (cp_token *);
2677static bool cp_parser_is_string_literal
2678  (cp_token *);
2679static bool cp_parser_is_keyword
2680  (cp_token *, enum rid);
2681static tree cp_parser_make_typename_type
2682  (cp_parser *, tree, location_t location);
2683static cp_declarator * cp_parser_make_indirect_declarator
2684  (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2685static bool cp_parser_compound_literal_p
2686  (cp_parser *);
2687static bool cp_parser_array_designator_p
2688  (cp_parser *);
2689static bool cp_parser_init_statement_p
2690  (cp_parser *);
2691static bool cp_parser_skip_to_closing_square_bracket
2692  (cp_parser *);
2693static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2694
2695// -------------------------------------------------------------------------- //
2696// Unevaluated Operand Guard
2697//
2698// Implementation of an RAII helper for unevaluated operand parsing.
2699cp_unevaluated::cp_unevaluated ()
2700{
2701  ++cp_unevaluated_operand;
2702  ++c_inhibit_evaluation_warnings;
2703}
2704
2705cp_unevaluated::~cp_unevaluated ()
2706{
2707  --c_inhibit_evaluation_warnings;
2708  --cp_unevaluated_operand;
2709}
2710
2711// -------------------------------------------------------------------------- //
2712// Tentative Parsing
2713
2714/* Returns nonzero if we are parsing tentatively.  */
2715
2716static inline bool
2717cp_parser_parsing_tentatively (cp_parser* parser)
2718{
2719  return parser->context->next != NULL;
2720}
2721
2722/* Returns nonzero if TOKEN is a string literal.  */
2723
2724static bool
2725cp_parser_is_pure_string_literal (cp_token* token)
2726{
2727  return (token->type == CPP_STRING ||
2728	  token->type == CPP_STRING16 ||
2729	  token->type == CPP_STRING32 ||
2730	  token->type == CPP_WSTRING ||
2731	  token->type == CPP_UTF8STRING);
2732}
2733
2734/* Returns nonzero if TOKEN is a string literal
2735   of a user-defined string literal.  */
2736
2737static bool
2738cp_parser_is_string_literal (cp_token* token)
2739{
2740  return (cp_parser_is_pure_string_literal (token) ||
2741	  token->type == CPP_STRING_USERDEF ||
2742	  token->type == CPP_STRING16_USERDEF ||
2743	  token->type == CPP_STRING32_USERDEF ||
2744	  token->type == CPP_WSTRING_USERDEF ||
2745	  token->type == CPP_UTF8STRING_USERDEF);
2746}
2747
2748/* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2749
2750static bool
2751cp_parser_is_keyword (cp_token* token, enum rid keyword)
2752{
2753  return token->keyword == keyword;
2754}
2755
2756/* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2757   PRAGMA_NONE.  */
2758
2759static enum pragma_kind
2760cp_parser_pragma_kind (cp_token *token)
2761{
2762  if (token->type != CPP_PRAGMA)
2763    return PRAGMA_NONE;
2764  /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
2765  return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2766}
2767
2768/* Helper function for cp_parser_error.
2769   Having peeked a token of kind TOK1_KIND that might signify
2770   a conflict marker, peek successor tokens to determine
2771   if we actually do have a conflict marker.
2772   Specifically, we consider a run of 7 '<', '=' or '>' characters
2773   at the start of a line as a conflict marker.
2774   These come through the lexer as three pairs and a single,
2775   e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2776   If it returns true, *OUT_LOC is written to with the location/range
2777   of the marker.  */
2778
2779static bool
2780cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2781			       location_t *out_loc)
2782{
2783  cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2784  if (token2->type != tok1_kind)
2785    return false;
2786  cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2787  if (token3->type != tok1_kind)
2788    return false;
2789  cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2790  if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2791    return false;
2792
2793  /* It must be at the start of the line.  */
2794  location_t start_loc = cp_lexer_peek_token (lexer)->location;
2795  if (LOCATION_COLUMN (start_loc) != 1)
2796    return false;
2797
2798  /* We have a conflict marker.  Construct a location of the form:
2799       <<<<<<<
2800       ^~~~~~~
2801     with start == caret, finishing at the end of the marker.  */
2802  location_t finish_loc = get_finish (token4->location);
2803  *out_loc = make_location (start_loc, start_loc, finish_loc);
2804
2805  return true;
2806}
2807
2808/* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2809   RT_CLOSE_PAREN.  */
2810
2811static const char *
2812get_matching_symbol (required_token token_desc)
2813{
2814  switch (token_desc)
2815    {
2816    default:
2817      gcc_unreachable ();
2818      return "";
2819    case RT_CLOSE_BRACE:
2820      return "{";
2821    case RT_CLOSE_PAREN:
2822      return "(";
2823    }
2824}
2825
2826/* Attempt to convert TOKEN_DESC from a required_token to an
2827   enum cpp_ttype, returning CPP_EOF if there is no good conversion.  */
2828
2829static enum cpp_ttype
2830get_required_cpp_ttype (required_token token_desc)
2831{
2832  switch (token_desc)
2833    {
2834    case RT_SEMICOLON:
2835      return CPP_SEMICOLON;
2836    case RT_OPEN_PAREN:
2837      return CPP_OPEN_PAREN;
2838    case RT_CLOSE_BRACE:
2839      return CPP_CLOSE_BRACE;
2840    case RT_OPEN_BRACE:
2841      return CPP_OPEN_BRACE;
2842    case RT_CLOSE_SQUARE:
2843      return CPP_CLOSE_SQUARE;
2844    case RT_OPEN_SQUARE:
2845      return CPP_OPEN_SQUARE;
2846    case RT_COMMA:
2847      return CPP_COMMA;
2848    case RT_COLON:
2849      return CPP_COLON;
2850    case RT_CLOSE_PAREN:
2851      return CPP_CLOSE_PAREN;
2852
2853    default:
2854      /* Use CPP_EOF as a "no completions possible" code.  */
2855      return CPP_EOF;
2856    }
2857}
2858
2859
2860/* Subroutine of cp_parser_error and cp_parser_required_error.
2861
2862   Issue a diagnostic of the form
2863      FILE:LINE: MESSAGE before TOKEN
2864   where TOKEN is the next token in the input stream.  MESSAGE
2865   (specified by the caller) is usually of the form "expected
2866   OTHER-TOKEN".
2867
2868   This bypasses the check for tentative passing, and potentially
2869   adds material needed by cp_parser_required_error.
2870
2871   If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2872   suggesting insertion of the missing token.
2873
2874   Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2875   have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2876   location.  */
2877
2878static void
2879cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2880		   required_token missing_token_desc,
2881		   location_t matching_location)
2882{
2883  cp_token *token = cp_lexer_peek_token (parser->lexer);
2884  /* This diagnostic makes more sense if it is tagged to the line
2885     of the token we just peeked at.  */
2886  cp_lexer_set_source_position_from_token (token);
2887
2888  if (token->type == CPP_PRAGMA)
2889    {
2890      error_at (token->location,
2891		"%<#pragma%> is not allowed here");
2892      cp_parser_skip_to_pragma_eol (parser, token);
2893      return;
2894    }
2895
2896  /* If this is actually a conflict marker, report it as such.  */
2897  if (token->type == CPP_LSHIFT
2898      || token->type == CPP_RSHIFT
2899      || token->type == CPP_EQ_EQ)
2900    {
2901      location_t loc;
2902      if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2903	{
2904	  error_at (loc, "version control conflict marker in file");
2905	  expanded_location token_exploc = expand_location (token->location);
2906	  /* Consume tokens until the end of the source line.  */
2907	  for (;;)
2908	    {
2909	      cp_lexer_consume_token (parser->lexer);
2910	      cp_token *next = cp_lexer_peek_token (parser->lexer);
2911	      if (next->type == CPP_EOF)
2912		break;
2913	      if (next->location == UNKNOWN_LOCATION
2914		  || loc == UNKNOWN_LOCATION)
2915		break;
2916
2917	      expanded_location next_exploc = expand_location (next->location);
2918	      if (next_exploc.file != token_exploc.file)
2919		break;
2920	      if (next_exploc.line != token_exploc.line)
2921		break;
2922	    }
2923	  return;
2924	}
2925    }
2926
2927  gcc_rich_location richloc (input_location);
2928
2929  bool added_matching_location = false;
2930
2931  if (missing_token_desc != RT_NONE)
2932    {
2933      /* Potentially supply a fix-it hint, suggesting to add the
2934	 missing token immediately after the *previous* token.
2935	 This may move the primary location within richloc.  */
2936      enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2937      location_t prev_token_loc
2938	= cp_lexer_previous_token (parser->lexer)->location;
2939      maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2940
2941      /* If matching_location != UNKNOWN_LOCATION, highlight it.
2942	 Attempt to consolidate diagnostics by printing it as a
2943	secondary range within the main diagnostic.  */
2944      if (matching_location != UNKNOWN_LOCATION)
2945	added_matching_location
2946	  = richloc.add_location_if_nearby (matching_location);
2947    }
2948
2949  /* Actually emit the error.  */
2950  c_parse_error (gmsgid,
2951		 /* Because c_parser_error does not understand
2952		    CPP_KEYWORD, keywords are treated like
2953		    identifiers.  */
2954		 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2955		 token->u.value, token->flags, &richloc);
2956
2957  if (missing_token_desc != RT_NONE)
2958    {
2959      /* If we weren't able to consolidate matching_location, then
2960	 print it as a secondary diagnostic.  */
2961      if (matching_location != UNKNOWN_LOCATION
2962	  && !added_matching_location)
2963	inform (matching_location, "to match this %qs",
2964		get_matching_symbol (missing_token_desc));
2965    }
2966}
2967
2968/* If not parsing tentatively, issue a diagnostic of the form
2969      FILE:LINE: MESSAGE before TOKEN
2970   where TOKEN is the next token in the input stream.  MESSAGE
2971   (specified by the caller) is usually of the form "expected
2972   OTHER-TOKEN".  */
2973
2974static void
2975cp_parser_error (cp_parser* parser, const char* gmsgid)
2976{
2977  if (!cp_parser_simulate_error (parser))
2978    cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2979}
2980
2981/* Issue an error about name-lookup failing.  NAME is the
2982   IDENTIFIER_NODE DECL is the result of
2983   the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2984   the thing that we hoped to find.  */
2985
2986static void
2987cp_parser_name_lookup_error (cp_parser* parser,
2988			     tree name,
2989			     tree decl,
2990			     name_lookup_error desired,
2991			     location_t location)
2992{
2993  /* If name lookup completely failed, tell the user that NAME was not
2994     declared.  */
2995  if (decl == error_mark_node)
2996    {
2997      if (parser->scope && parser->scope != global_namespace)
2998	error_at (location, "%<%E::%E%> has not been declared",
2999		  parser->scope, name);
3000      else if (parser->scope == global_namespace)
3001	error_at (location, "%<::%E%> has not been declared", name);
3002      else if (parser->object_scope
3003	       && !CLASS_TYPE_P (parser->object_scope))
3004	error_at (location, "request for member %qE in non-class type %qT",
3005		  name, parser->object_scope);
3006      else if (parser->object_scope)
3007	error_at (location, "%<%T::%E%> has not been declared",
3008		  parser->object_scope, name);
3009      else
3010	error_at (location, "%qE has not been declared", name);
3011    }
3012  else if (parser->scope && parser->scope != global_namespace)
3013    {
3014      switch (desired)
3015	{
3016	  case NLE_TYPE:
3017	    error_at (location, "%<%E::%E%> is not a type",
3018	    			parser->scope, name);
3019	    break;
3020	  case NLE_CXX98:
3021	    error_at (location, "%<%E::%E%> is not a class or namespace",
3022	    			parser->scope, name);
3023	    break;
3024	  case NLE_NOT_CXX98:
3025	    error_at (location,
3026	    	      "%<%E::%E%> is not a class, namespace, or enumeration",
3027		      parser->scope, name);
3028	    break;
3029	  default:
3030	    gcc_unreachable ();
3031
3032	}
3033    }
3034  else if (parser->scope == global_namespace)
3035    {
3036      switch (desired)
3037	{
3038	  case NLE_TYPE:
3039	    error_at (location, "%<::%E%> is not a type", name);
3040	    break;
3041	  case NLE_CXX98:
3042	    error_at (location, "%<::%E%> is not a class or namespace", name);
3043	    break;
3044	  case NLE_NOT_CXX98:
3045	    error_at (location,
3046		      "%<::%E%> is not a class, namespace, or enumeration",
3047		      name);
3048	    break;
3049	  default:
3050	    gcc_unreachable ();
3051	}
3052    }
3053  else
3054    {
3055      switch (desired)
3056	{
3057	  case NLE_TYPE:
3058	    error_at (location, "%qE is not a type", name);
3059	    break;
3060	  case NLE_CXX98:
3061	    error_at (location, "%qE is not a class or namespace", name);
3062	    break;
3063	  case NLE_NOT_CXX98:
3064	    error_at (location,
3065		      "%qE is not a class, namespace, or enumeration", name);
3066	    break;
3067	  default:
3068	    gcc_unreachable ();
3069	}
3070    }
3071}
3072
3073/* If we are parsing tentatively, remember that an error has occurred
3074   during this tentative parse.  Returns true if the error was
3075   simulated; false if a message should be issued by the caller.  */
3076
3077static bool
3078cp_parser_simulate_error (cp_parser* parser)
3079{
3080  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3081    {
3082      parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3083      return true;
3084    }
3085  return false;
3086}
3087
3088/* This function is called when a type is defined.  If type
3089   definitions are forbidden at this point, an error message is
3090   issued.  */
3091
3092static bool
3093cp_parser_check_type_definition (cp_parser* parser)
3094{
3095  /* If types are forbidden here, issue a message.  */
3096  if (parser->type_definition_forbidden_message)
3097    {
3098      /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3099	 or %qs in the message need to be interpreted.  */
3100      error (parser->type_definition_forbidden_message,
3101	     parser->type_definition_forbidden_message_arg);
3102      return false;
3103    }
3104  return true;
3105}
3106
3107/* This function is called when the DECLARATOR is processed.  The TYPE
3108   was a type defined in the decl-specifiers.  If it is invalid to
3109   define a type in the decl-specifiers for DECLARATOR, an error is
3110   issued. TYPE_LOCATION is the location of TYPE and is used
3111   for error reporting.  */
3112
3113static void
3114cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3115					       tree type, location_t type_location)
3116{
3117  /* [dcl.fct] forbids type definitions in return types.
3118     Unfortunately, it's not easy to know whether or not we are
3119     processing a return type until after the fact.  */
3120  while (declarator
3121	 && (declarator->kind == cdk_pointer
3122	     || declarator->kind == cdk_reference
3123	     || declarator->kind == cdk_ptrmem))
3124    declarator = declarator->declarator;
3125  if (declarator
3126      && declarator->kind == cdk_function)
3127    {
3128      error_at (type_location,
3129		"new types may not be defined in a return type");
3130      inform (type_location,
3131	      "(perhaps a semicolon is missing after the definition of %qT)",
3132	      type);
3133    }
3134}
3135
3136/* A type-specifier (TYPE) has been parsed which cannot be followed by
3137   "<" in any valid C++ program.  If the next token is indeed "<",
3138   issue a message warning the user about what appears to be an
3139   invalid attempt to form a template-id. LOCATION is the location
3140   of the type-specifier (TYPE) */
3141
3142static void
3143cp_parser_check_for_invalid_template_id (cp_parser* parser,
3144					 tree type,
3145					 enum tag_types tag_type,
3146					 location_t location)
3147{
3148  cp_token_position start = 0;
3149
3150  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3151    {
3152      if (TREE_CODE (type) == TYPE_DECL)
3153	type = TREE_TYPE (type);
3154      if (TYPE_P (type) && !template_placeholder_p (type))
3155	error_at (location, "%qT is not a template", type);
3156      else if (identifier_p (type))
3157	{
3158	  if (tag_type != none_type)
3159	    error_at (location, "%qE is not a class template", type);
3160	  else
3161	    error_at (location, "%qE is not a template", type);
3162	}
3163      else
3164	error_at (location, "invalid template-id");
3165      /* Remember the location of the invalid "<".  */
3166      if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3167	start = cp_lexer_token_position (parser->lexer, true);
3168      /* Consume the "<".  */
3169      cp_lexer_consume_token (parser->lexer);
3170      /* Parse the template arguments.  */
3171      cp_parser_enclosed_template_argument_list (parser);
3172      /* Permanently remove the invalid template arguments so that
3173	 this error message is not issued again.  */
3174      if (start)
3175	cp_lexer_purge_tokens_after (parser->lexer, start);
3176    }
3177}
3178
3179/* If parsing an integral constant-expression, issue an error message
3180   about the fact that THING appeared and return true.  Otherwise,
3181   return false.  In either case, set
3182   PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
3183
3184static bool
3185cp_parser_non_integral_constant_expression (cp_parser  *parser,
3186					    non_integral_constant thing)
3187{
3188  parser->non_integral_constant_expression_p = true;
3189  if (parser->integral_constant_expression_p)
3190    {
3191      if (!parser->allow_non_integral_constant_expression_p)
3192	{
3193	  const char *msg = NULL;
3194	  switch (thing)
3195	    {
3196  	      case NIC_FLOAT:
3197		pedwarn (input_location, OPT_Wpedantic,
3198			 "ISO C++ forbids using a floating-point literal "
3199			 "in a constant-expression");
3200		return true;
3201	      case NIC_CAST:
3202		error ("a cast to a type other than an integral or "
3203		       "enumeration type cannot appear in a "
3204		       "constant-expression");
3205		return true;
3206	      case NIC_TYPEID:
3207		error ("%<typeid%> operator "
3208		       "cannot appear in a constant-expression");
3209		return true;
3210	      case NIC_NCC:
3211		error ("non-constant compound literals "
3212		       "cannot appear in a constant-expression");
3213		return true;
3214	      case NIC_FUNC_CALL:
3215		error ("a function call "
3216		       "cannot appear in a constant-expression");
3217		return true;
3218	      case NIC_INC:
3219		error ("an increment "
3220		       "cannot appear in a constant-expression");
3221		return true;
3222	      case NIC_DEC:
3223		error ("an decrement "
3224		       "cannot appear in a constant-expression");
3225		return true;
3226	      case NIC_ARRAY_REF:
3227		error ("an array reference "
3228		       "cannot appear in a constant-expression");
3229		return true;
3230	      case NIC_ADDR_LABEL:
3231		error ("the address of a label "
3232		       "cannot appear in a constant-expression");
3233		return true;
3234	      case NIC_OVERLOADED:
3235		error ("calls to overloaded operators "
3236		       "cannot appear in a constant-expression");
3237		return true;
3238	      case NIC_ASSIGNMENT:
3239		error ("an assignment cannot appear in a constant-expression");
3240		return true;
3241	      case NIC_COMMA:
3242		error ("a comma operator "
3243		       "cannot appear in a constant-expression");
3244		return true;
3245	      case NIC_CONSTRUCTOR:
3246		error ("a call to a constructor "
3247		       "cannot appear in a constant-expression");
3248		return true;
3249	      case NIC_TRANSACTION:
3250		error ("a transaction expression "
3251		       "cannot appear in a constant-expression");
3252		return true;
3253	      case NIC_THIS:
3254		msg = "this";
3255		break;
3256	      case NIC_FUNC_NAME:
3257		msg = "__FUNCTION__";
3258		break;
3259  	      case NIC_PRETTY_FUNC:
3260		msg = "__PRETTY_FUNCTION__";
3261		break;
3262	      case NIC_C99_FUNC:
3263		msg = "__func__";
3264		break;
3265	      case NIC_VA_ARG:
3266		msg = "va_arg";
3267		break;
3268	      case NIC_ARROW:
3269		msg = "->";
3270		break;
3271	      case NIC_POINT:
3272		msg = ".";
3273		break;
3274	      case NIC_STAR:
3275		msg = "*";
3276		break;
3277	      case NIC_ADDR:
3278		msg = "&";
3279		break;
3280	      case NIC_PREINCREMENT:
3281		msg = "++";
3282		break;
3283	      case NIC_PREDECREMENT:
3284		msg = "--";
3285		break;
3286	      case NIC_NEW:
3287		msg = "new";
3288		break;
3289	      case NIC_DEL:
3290		msg = "delete";
3291		break;
3292	      default:
3293		gcc_unreachable ();
3294	    }
3295	  if (msg)
3296	    error ("%qs cannot appear in a constant-expression", msg);
3297	  return true;
3298	}
3299    }
3300  return false;
3301}
3302
3303/* Emit a diagnostic for an invalid type name.  This function commits
3304   to the current active tentative parse, if any.  (Otherwise, the
3305   problematic construct might be encountered again later, resulting
3306   in duplicate error messages.) LOCATION is the location of ID.  */
3307
3308static void
3309cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3310				      location_t location)
3311{
3312  tree decl, ambiguous_decls;
3313  cp_parser_commit_to_tentative_parse (parser);
3314  /* Try to lookup the identifier.  */
3315  decl = cp_parser_lookup_name (parser, id, none_type,
3316				/*is_template=*/false,
3317				/*is_namespace=*/false,
3318				/*check_dependency=*/true,
3319				&ambiguous_decls, location);
3320  if (ambiguous_decls)
3321    /* If the lookup was ambiguous, an error will already have
3322       been issued.  */
3323    return;
3324  /* If the lookup found a template-name, it means that the user forgot
3325  to specify an argument list. Emit a useful error message.  */
3326  if (DECL_TYPE_TEMPLATE_P (decl))
3327    {
3328      auto_diagnostic_group d;
3329      error_at (location,
3330		"invalid use of template-name %qE without an argument list",
3331		decl);
3332      if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3333	inform (location, "class template argument deduction is only available "
3334		"with %<-std=c++17%> or %<-std=gnu++17%>");
3335      inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3336    }
3337  else if (TREE_CODE (id) == BIT_NOT_EXPR)
3338    error_at (location, "invalid use of destructor %qD as a type", id);
3339  else if (TREE_CODE (decl) == TYPE_DECL)
3340    /* Something like 'unsigned A a;'  */
3341    error_at (location, "invalid combination of multiple type-specifiers");
3342  else if (!parser->scope)
3343    {
3344      /* Issue an error message.  */
3345      auto_diagnostic_group d;
3346      name_hint hint;
3347      if (TREE_CODE (id) == IDENTIFIER_NODE)
3348	hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3349      if (const char *suggestion = hint.suggestion ())
3350	{
3351	  gcc_rich_location richloc (location);
3352	  richloc.add_fixit_replace (suggestion);
3353	  error_at (&richloc,
3354		    "%qE does not name a type; did you mean %qs?",
3355		    id, suggestion);
3356	}
3357      else
3358	error_at (location, "%qE does not name a type", id);
3359      /* If we're in a template class, it's possible that the user was
3360	 referring to a type from a base class.  For example:
3361
3362	   template <typename T> struct A { typedef T X; };
3363	   template <typename T> struct B : public A<T> { X x; };
3364
3365	 The user should have said "typename A<T>::X".  */
3366      if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3367	inform (location, "C++11 %<constexpr%> only available with "
3368		"%<-std=c++11%> or %<-std=gnu++11%>");
3369      else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3370	inform (location, "C++11 %<noexcept%> only available with "
3371		"%<-std=c++11%> or %<-std=gnu++11%>");
3372      else if (cxx_dialect < cxx11
3373	       && TREE_CODE (id) == IDENTIFIER_NODE
3374	       && id_equal (id, "thread_local"))
3375	inform (location, "C++11 %<thread_local%> only available with "
3376		"%<-std=c++11%> or %<-std=gnu++11%>");
3377      else if (cxx_dialect < cxx2a && id == ridpointers[(int)RID_CONSTINIT])
3378	inform (location, "C++20 %<constinit%> only available with "
3379		"%<-std=c++2a%> or %<-std=gnu++2a%>");
3380      else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3381	inform (location, "%<concept%> only available with %<-std=c++2a%> or "
3382		"%<-fconcepts%>");
3383      else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3384	inform (location, "%<requires%> only available with %<-std=c++2a%> or "
3385		"%<-fconcepts%>");
3386      else if (processing_template_decl && current_class_type
3387	       && TYPE_BINFO (current_class_type))
3388	{
3389	  tree b;
3390
3391	  for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3392	       b;
3393	       b = TREE_CHAIN (b))
3394	    {
3395	      tree base_type = BINFO_TYPE (b);
3396	      if (CLASS_TYPE_P (base_type)
3397		  && dependent_type_p (base_type))
3398		{
3399		  tree field;
3400		  /* Go from a particular instantiation of the
3401		     template (which will have an empty TYPE_FIELDs),
3402		     to the main version.  */
3403		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3404		  for (field = TYPE_FIELDS (base_type);
3405		       field;
3406		       field = DECL_CHAIN (field))
3407		    if (TREE_CODE (field) == TYPE_DECL
3408			&& DECL_NAME (field) == id)
3409		      {
3410			inform (location,
3411				"(perhaps %<typename %T::%E%> was intended)",
3412				BINFO_TYPE (b), id);
3413			break;
3414		      }
3415		  if (field)
3416		    break;
3417		}
3418	    }
3419	}
3420    }
3421  /* Here we diagnose qualified-ids where the scope is actually correct,
3422     but the identifier does not resolve to a valid type name.  */
3423  else if (parser->scope != error_mark_node)
3424    {
3425      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3426	{
3427	  auto_diagnostic_group d;
3428	  name_hint hint;
3429	  if (decl == error_mark_node)
3430	    hint = suggest_alternative_in_explicit_scope (location, id,
3431							  parser->scope);
3432	  const char *suggestion = hint.suggestion ();
3433	  gcc_rich_location richloc (location_of (id));
3434	  if (suggestion)
3435	    richloc.add_fixit_replace (suggestion);
3436	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3437	    {
3438	      if (suggestion)
3439		error_at (&richloc,
3440			  "%qE in namespace %qE does not name a template"
3441			  " type; did you mean %qs?",
3442			  id, parser->scope, suggestion);
3443	      else
3444		error_at (&richloc,
3445			  "%qE in namespace %qE does not name a template type",
3446			  id, parser->scope);
3447	    }
3448	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3449	    {
3450	      if (suggestion)
3451		error_at (&richloc,
3452			  "%qE in namespace %qE does not name a template"
3453			  " type; did you mean %qs?",
3454			  TREE_OPERAND (id, 0), parser->scope, suggestion);
3455	      else
3456		error_at (&richloc,
3457			  "%qE in namespace %qE does not name a template"
3458			  " type",
3459			  TREE_OPERAND (id, 0), parser->scope);
3460	    }
3461	  else
3462	    {
3463	      if (suggestion)
3464		error_at (&richloc,
3465			  "%qE in namespace %qE does not name a type"
3466			  "; did you mean %qs?",
3467			  id, parser->scope, suggestion);
3468	      else
3469		error_at (&richloc,
3470			  "%qE in namespace %qE does not name a type",
3471			  id, parser->scope);
3472	    }
3473	  if (DECL_P (decl))
3474	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3475	}
3476      else if (CLASS_TYPE_P (parser->scope)
3477	       && constructor_name_p (id, parser->scope))
3478	{
3479	  /* A<T>::A<T>() */
3480	  auto_diagnostic_group d;
3481	  error_at (location, "%<%T::%E%> names the constructor, not"
3482		    " the type", parser->scope, id);
3483	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3484	    error_at (location, "and %qT has no template constructors",
3485		      parser->scope);
3486	}
3487      else if (TYPE_P (parser->scope)
3488	       && dependent_scope_p (parser->scope))
3489	{
3490	  gcc_rich_location richloc (location);
3491	  richloc.add_fixit_insert_before ("typename ");
3492	  if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3493	    error_at (&richloc,
3494		      "need %<typename%> before %<%T::%D::%E%> because "
3495		      "%<%T::%D%> is a dependent scope",
3496		      TYPE_CONTEXT (parser->scope),
3497		      TYPENAME_TYPE_FULLNAME (parser->scope),
3498		      id,
3499		      TYPE_CONTEXT (parser->scope),
3500		      TYPENAME_TYPE_FULLNAME (parser->scope));
3501	  else
3502	    error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3503		      "%qT is a dependent scope",
3504		      parser->scope, id, parser->scope);
3505	}
3506      else if (TYPE_P (parser->scope))
3507	{
3508	  auto_diagnostic_group d;
3509	  if (!COMPLETE_TYPE_P (parser->scope))
3510	    cxx_incomplete_type_error (location_of (id), NULL_TREE,
3511				       parser->scope);
3512	  else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3513	    error_at (location_of (id),
3514		      "%qE in %q#T does not name a template type",
3515		      id, parser->scope);
3516	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3517	    error_at (location_of (id),
3518		      "%qE in %q#T does not name a template type",
3519		      TREE_OPERAND (id, 0), parser->scope);
3520	  else
3521	    error_at (location_of (id),
3522		      "%qE in %q#T does not name a type",
3523		      id, parser->scope);
3524	  if (DECL_P (decl))
3525	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3526	}
3527      else
3528	gcc_unreachable ();
3529    }
3530}
3531
3532/* Check for a common situation where a type-name should be present,
3533   but is not, and issue a sensible error message.  Returns true if an
3534   invalid type-name was detected.
3535
3536   The situation handled by this function are variable declarations of the
3537   form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3538   Usually, `ID' should name a type, but if we got here it means that it
3539   does not. We try to emit the best possible error message depending on
3540   how exactly the id-expression looks like.  */
3541
3542static bool
3543cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3544{
3545  tree id;
3546  cp_token *token = cp_lexer_peek_token (parser->lexer);
3547
3548  /* Avoid duplicate error about ambiguous lookup.  */
3549  if (token->type == CPP_NESTED_NAME_SPECIFIER)
3550    {
3551      cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3552      if (next->type == CPP_NAME && next->error_reported)
3553	goto out;
3554    }
3555
3556  cp_parser_parse_tentatively (parser);
3557  id = cp_parser_id_expression (parser,
3558				/*template_keyword_p=*/false,
3559				/*check_dependency_p=*/true,
3560				/*template_p=*/NULL,
3561				/*declarator_p=*/false,
3562				/*optional_p=*/false);
3563  /* If the next token is a (, this is a function with no explicit return
3564     type, i.e. constructor, destructor or conversion op.  */
3565  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3566      || TREE_CODE (id) == TYPE_DECL)
3567    {
3568      cp_parser_abort_tentative_parse (parser);
3569      return false;
3570    }
3571  if (!cp_parser_parse_definitely (parser))
3572    return false;
3573
3574  /* Emit a diagnostic for the invalid type.  */
3575  cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3576 out:
3577  /* If we aren't in the middle of a declarator (i.e. in a
3578     parameter-declaration-clause), skip to the end of the declaration;
3579     there's no point in trying to process it.  */
3580  if (!parser->in_declarator_p)
3581    cp_parser_skip_to_end_of_block_or_statement (parser);
3582  return true;
3583}
3584
3585/* Consume tokens up to, and including, the next non-nested closing `)'.
3586   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3587   are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3588   found an unnested token of that type.  */
3589
3590static int
3591cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3592					 bool recovering,
3593					 cpp_ttype or_ttype,
3594					 bool consume_paren)
3595{
3596  unsigned paren_depth = 0;
3597  unsigned brace_depth = 0;
3598  unsigned square_depth = 0;
3599  unsigned condop_depth = 0;
3600
3601  if (recovering && or_ttype == CPP_EOF
3602      && cp_parser_uncommitted_to_tentative_parse_p (parser))
3603    return 0;
3604
3605  while (true)
3606    {
3607      cp_token * token = cp_lexer_peek_token (parser->lexer);
3608
3609      /* Have we found what we're looking for before the closing paren?  */
3610      if (token->type == or_ttype && or_ttype != CPP_EOF
3611	  && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3612	return -1;
3613
3614      switch (token->type)
3615	{
3616	case CPP_PRAGMA_EOL:
3617	  if (!parser->lexer->in_pragma)
3618	    break;
3619	  /* FALLTHRU */
3620	case CPP_EOF:
3621	  /* If we've run out of tokens, then there is no closing `)'.  */
3622	  return 0;
3623
3624        /* This is good for lambda expression capture-lists.  */
3625        case CPP_OPEN_SQUARE:
3626          ++square_depth;
3627          break;
3628        case CPP_CLOSE_SQUARE:
3629          if (!square_depth--)
3630            return 0;
3631          break;
3632
3633	case CPP_SEMICOLON:
3634	  /* This matches the processing in skip_to_end_of_statement.  */
3635	  if (!brace_depth)
3636	    return 0;
3637	  break;
3638
3639	case CPP_OPEN_BRACE:
3640	  ++brace_depth;
3641	  break;
3642	case CPP_CLOSE_BRACE:
3643	  if (!brace_depth--)
3644	    return 0;
3645	  break;
3646
3647	case CPP_OPEN_PAREN:
3648	  if (!brace_depth)
3649	    ++paren_depth;
3650	  break;
3651
3652	case CPP_CLOSE_PAREN:
3653	  if (!brace_depth && !paren_depth--)
3654	    {
3655	      if (consume_paren)
3656		cp_lexer_consume_token (parser->lexer);
3657	      return 1;
3658	    }
3659	  break;
3660
3661	case CPP_QUERY:
3662	  if (!brace_depth && !paren_depth && !square_depth)
3663	    ++condop_depth;
3664	  break;
3665
3666	case CPP_COLON:
3667	  if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3668	    condop_depth--;
3669	  break;
3670
3671	default:
3672	  break;
3673	}
3674
3675      /* Consume the token.  */
3676      cp_lexer_consume_token (parser->lexer);
3677    }
3678}
3679
3680/* Consume tokens up to, and including, the next non-nested closing `)'.
3681   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3682   are doing error recovery. Returns -1 if OR_COMMA is true and we
3683   found an unnested token of that type.  */
3684
3685static int
3686cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3687				       bool recovering,
3688				       bool or_comma,
3689				       bool consume_paren)
3690{
3691  cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3692  return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3693						  ttype, consume_paren);
3694}
3695
3696/* Consume tokens until we reach the end of the current statement.
3697   Normally, that will be just before consuming a `;'.  However, if a
3698   non-nested `}' comes first, then we stop before consuming that.  */
3699
3700static void
3701cp_parser_skip_to_end_of_statement (cp_parser* parser)
3702{
3703  unsigned nesting_depth = 0;
3704
3705  /* Unwind generic function template scope if necessary.  */
3706  if (parser->fully_implicit_function_template_p)
3707    abort_fully_implicit_template (parser);
3708
3709  while (true)
3710    {
3711      cp_token *token = cp_lexer_peek_token (parser->lexer);
3712
3713      switch (token->type)
3714	{
3715	case CPP_PRAGMA_EOL:
3716	  if (!parser->lexer->in_pragma)
3717	    break;
3718	  /* FALLTHRU */
3719	case CPP_EOF:
3720	  /* If we've run out of tokens, stop.  */
3721	  return;
3722
3723	case CPP_SEMICOLON:
3724	  /* If the next token is a `;', we have reached the end of the
3725	     statement.  */
3726	  if (!nesting_depth)
3727	    return;
3728	  break;
3729
3730	case CPP_CLOSE_BRACE:
3731	  /* If this is a non-nested '}', stop before consuming it.
3732	     That way, when confronted with something like:
3733
3734	       { 3 + }
3735
3736	     we stop before consuming the closing '}', even though we
3737	     have not yet reached a `;'.  */
3738	  if (nesting_depth == 0)
3739	    return;
3740
3741	  /* If it is the closing '}' for a block that we have
3742	     scanned, stop -- but only after consuming the token.
3743	     That way given:
3744
3745		void f g () { ... }
3746		typedef int I;
3747
3748	     we will stop after the body of the erroneously declared
3749	     function, but before consuming the following `typedef'
3750	     declaration.  */
3751	  if (--nesting_depth == 0)
3752	    {
3753	      cp_lexer_consume_token (parser->lexer);
3754	      return;
3755	    }
3756	  break;
3757
3758	case CPP_OPEN_BRACE:
3759	  ++nesting_depth;
3760	  break;
3761
3762	default:
3763	  break;
3764	}
3765
3766      /* Consume the token.  */
3767      cp_lexer_consume_token (parser->lexer);
3768    }
3769}
3770
3771/* This function is called at the end of a statement or declaration.
3772   If the next token is a semicolon, it is consumed; otherwise, error
3773   recovery is attempted.  */
3774
3775static void
3776cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3777{
3778  /* Look for the trailing `;'.  */
3779  if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3780    {
3781      /* If there is additional (erroneous) input, skip to the end of
3782	 the statement.  */
3783      cp_parser_skip_to_end_of_statement (parser);
3784      /* If the next token is now a `;', consume it.  */
3785      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3786	cp_lexer_consume_token (parser->lexer);
3787    }
3788}
3789
3790/* Skip tokens until we have consumed an entire block, or until we
3791   have consumed a non-nested `;'.  */
3792
3793static void
3794cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3795{
3796  int nesting_depth = 0;
3797
3798  /* Unwind generic function template scope if necessary.  */
3799  if (parser->fully_implicit_function_template_p)
3800    abort_fully_implicit_template (parser);
3801
3802  while (nesting_depth >= 0)
3803    {
3804      cp_token *token = cp_lexer_peek_token (parser->lexer);
3805
3806      switch (token->type)
3807	{
3808	case CPP_PRAGMA_EOL:
3809	  if (!parser->lexer->in_pragma)
3810	    break;
3811	  /* FALLTHRU */
3812	case CPP_EOF:
3813	  /* If we've run out of tokens, stop.  */
3814	  return;
3815
3816	case CPP_SEMICOLON:
3817	  /* Stop if this is an unnested ';'. */
3818	  if (!nesting_depth)
3819	    nesting_depth = -1;
3820	  break;
3821
3822	case CPP_CLOSE_BRACE:
3823	  /* Stop if this is an unnested '}', or closes the outermost
3824	     nesting level.  */
3825	  nesting_depth--;
3826	  if (nesting_depth < 0)
3827	    return;
3828	  if (!nesting_depth)
3829	    nesting_depth = -1;
3830	  break;
3831
3832	case CPP_OPEN_BRACE:
3833	  /* Nest. */
3834	  nesting_depth++;
3835	  break;
3836
3837	default:
3838	  break;
3839	}
3840
3841      /* Consume the token.  */
3842      cp_lexer_consume_token (parser->lexer);
3843    }
3844}
3845
3846/* Skip tokens until a non-nested closing curly brace is the next
3847   token, or there are no more tokens. Return true in the first case,
3848   false otherwise.  */
3849
3850static bool
3851cp_parser_skip_to_closing_brace (cp_parser *parser)
3852{
3853  unsigned nesting_depth = 0;
3854
3855  while (true)
3856    {
3857      cp_token *token = cp_lexer_peek_token (parser->lexer);
3858
3859      switch (token->type)
3860	{
3861	case CPP_PRAGMA_EOL:
3862	  if (!parser->lexer->in_pragma)
3863	    break;
3864	  /* FALLTHRU */
3865	case CPP_EOF:
3866	  /* If we've run out of tokens, stop.  */
3867	  return false;
3868
3869	case CPP_CLOSE_BRACE:
3870	  /* If the next token is a non-nested `}', then we have reached
3871	     the end of the current block.  */
3872	  if (nesting_depth-- == 0)
3873	    return true;
3874	  break;
3875
3876	case CPP_OPEN_BRACE:
3877	  /* If it the next token is a `{', then we are entering a new
3878	     block.  Consume the entire block.  */
3879	  ++nesting_depth;
3880	  break;
3881
3882	default:
3883	  break;
3884	}
3885
3886      /* Consume the token.  */
3887      cp_lexer_consume_token (parser->lexer);
3888    }
3889}
3890
3891/* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3892   parameter is the PRAGMA token, allowing us to purge the entire pragma
3893   sequence.  */
3894
3895static void
3896cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3897{
3898  cp_token *token;
3899
3900  parser->lexer->in_pragma = false;
3901
3902  do
3903    token = cp_lexer_consume_token (parser->lexer);
3904  while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3905
3906  /* Ensure that the pragma is not parsed again.  */
3907  cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3908}
3909
3910/* Require pragma end of line, resyncing with it as necessary.  The
3911   arguments are as for cp_parser_skip_to_pragma_eol.  */
3912
3913static void
3914cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3915{
3916  parser->lexer->in_pragma = false;
3917  if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3918    cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3919}
3920
3921/* This is a simple wrapper around make_typename_type. When the id is
3922   an unresolved identifier node, we can provide a superior diagnostic
3923   using cp_parser_diagnose_invalid_type_name.  */
3924
3925static tree
3926cp_parser_make_typename_type (cp_parser *parser, tree id,
3927			      location_t id_location)
3928{
3929  tree result;
3930  if (identifier_p (id))
3931    {
3932      result = make_typename_type (parser->scope, id, typename_type,
3933				   /*complain=*/tf_none);
3934      if (result == error_mark_node)
3935	cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3936      return result;
3937    }
3938  return make_typename_type (parser->scope, id, typename_type, tf_error);
3939}
3940
3941/* This is a wrapper around the
3942   make_{pointer,ptrmem,reference}_declarator functions that decides
3943   which one to call based on the CODE and CLASS_TYPE arguments. The
3944   CODE argument should be one of the values returned by
3945   cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3946   appertain to the pointer or reference.  */
3947
3948static cp_declarator *
3949cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3950				    cp_cv_quals cv_qualifiers,
3951				    cp_declarator *target,
3952				    tree attributes)
3953{
3954  if (code == ERROR_MARK || target == cp_error_declarator)
3955    return cp_error_declarator;
3956
3957  if (code == INDIRECT_REF)
3958    if (class_type == NULL_TREE)
3959      return make_pointer_declarator (cv_qualifiers, target, attributes);
3960    else
3961      return make_ptrmem_declarator (cv_qualifiers, class_type,
3962				     target, attributes);
3963  else if (code == ADDR_EXPR && class_type == NULL_TREE)
3964    return make_reference_declarator (cv_qualifiers, target,
3965				      false, attributes);
3966  else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3967    return make_reference_declarator (cv_qualifiers, target,
3968				      true, attributes);
3969  gcc_unreachable ();
3970}
3971
3972/* Create a new C++ parser.  */
3973
3974static cp_parser *
3975cp_parser_new (void)
3976{
3977  cp_parser *parser;
3978  cp_lexer *lexer;
3979  unsigned i;
3980
3981  /* cp_lexer_new_main is called before doing GC allocation because
3982     cp_lexer_new_main might load a PCH file.  */
3983  lexer = cp_lexer_new_main ();
3984
3985  /* Initialize the binops_by_token so that we can get the tree
3986     directly from the token.  */
3987  for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3988    binops_by_token[binops[i].token_type] = binops[i];
3989
3990  parser = ggc_cleared_alloc<cp_parser> ();
3991  parser->lexer = lexer;
3992  parser->context = cp_parser_context_new (NULL);
3993
3994  /* For now, we always accept GNU extensions.  */
3995  parser->allow_gnu_extensions_p = 1;
3996
3997  /* The `>' token is a greater-than operator, not the end of a
3998     template-id.  */
3999  parser->greater_than_is_operator_p = true;
4000
4001  parser->default_arg_ok_p = true;
4002
4003  /* We are not parsing a constant-expression.  */
4004  parser->integral_constant_expression_p = false;
4005  parser->allow_non_integral_constant_expression_p = false;
4006  parser->non_integral_constant_expression_p = false;
4007
4008  /* Local variable names are not forbidden.  */
4009  parser->local_variables_forbidden_p = 0;
4010
4011  /* We are not processing an `extern "C"' declaration.  */
4012  parser->in_unbraced_linkage_specification_p = false;
4013
4014  /* We are not processing a declarator.  */
4015  parser->in_declarator_p = false;
4016
4017  /* We are not processing a template-argument-list.  */
4018  parser->in_template_argument_list_p = false;
4019
4020  /* We are not in an iteration statement.  */
4021  parser->in_statement = 0;
4022
4023  /* We are not in a switch statement.  */
4024  parser->in_switch_statement_p = false;
4025
4026  /* We are not parsing a type-id inside an expression.  */
4027  parser->in_type_id_in_expr_p = false;
4028
4029  /* String literals should be translated to the execution character set.  */
4030  parser->translate_strings_p = true;
4031
4032  /* We are not parsing a function body.  */
4033  parser->in_function_body = false;
4034
4035  /* We can correct until told otherwise.  */
4036  parser->colon_corrects_to_scope_p = true;
4037
4038  /* The unparsed function queue is empty.  */
4039  push_unparsed_function_queues (parser);
4040
4041  /* There are no classes being defined.  */
4042  parser->num_classes_being_defined = 0;
4043
4044  /* No template parameters apply.  */
4045  parser->num_template_parameter_lists = 0;
4046
4047  /* Special parsing data structures.  */
4048  parser->omp_declare_simd = NULL;
4049  parser->oacc_routine = NULL;
4050
4051  /* Not declaring an implicit function template.  */
4052  parser->auto_is_implicit_function_template_parm_p = false;
4053  parser->fully_implicit_function_template_p = false;
4054  parser->implicit_template_parms = 0;
4055  parser->implicit_template_scope = 0;
4056
4057  /* Allow constrained-type-specifiers. */
4058  parser->prevent_constrained_type_specifiers = 0;
4059
4060  /* We haven't yet seen an 'extern "C"'.  */
4061  parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4062
4063  return parser;
4064}
4065
4066/* Create a cp_lexer structure which will emit the tokens in CACHE
4067   and push it onto the parser's lexer stack.  This is used for delayed
4068   parsing of in-class method bodies and default arguments, and should
4069   not be confused with tentative parsing.  */
4070static void
4071cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4072{
4073  cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4074  lexer->next = parser->lexer;
4075  parser->lexer = lexer;
4076
4077  /* Move the current source position to that of the first token in the
4078     new lexer.  */
4079  cp_lexer_set_source_position_from_token (lexer->next_token);
4080}
4081
4082/* Pop the top lexer off the parser stack.  This is never used for the
4083   "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
4084static void
4085cp_parser_pop_lexer (cp_parser *parser)
4086{
4087  cp_lexer *lexer = parser->lexer;
4088  parser->lexer = lexer->next;
4089  cp_lexer_destroy (lexer);
4090
4091  /* Put the current source position back where it was before this
4092     lexer was pushed.  */
4093  cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4094}
4095
4096/* Lexical conventions [gram.lex]  */
4097
4098/* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
4099   identifier.  */
4100
4101static cp_expr
4102cp_parser_identifier (cp_parser* parser)
4103{
4104  cp_token *token;
4105
4106  /* Look for the identifier.  */
4107  token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4108  /* Return the value.  */
4109  if (token)
4110    return cp_expr (token->u.value, token->location);
4111  else
4112    return error_mark_node;
4113}
4114
4115/* Parse a sequence of adjacent string constants.  Returns a
4116   TREE_STRING representing the combined, nul-terminated string
4117   constant.  If TRANSLATE is true, translate the string to the
4118   execution character set.  If WIDE_OK is true, a wide string is
4119   invalid here.
4120
4121   C++98 [lex.string] says that if a narrow string literal token is
4122   adjacent to a wide string literal token, the behavior is undefined.
4123   However, C99 6.4.5p4 says that this results in a wide string literal.
4124   We follow C99 here, for consistency with the C front end.
4125
4126   This code is largely lifted from lex_string() in c-lex.c.
4127
4128   FUTURE: ObjC++ will need to handle @-strings here.  */
4129static cp_expr
4130cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4131			  bool lookup_udlit = true)
4132{
4133  tree value;
4134  size_t count;
4135  struct obstack str_ob;
4136  struct obstack loc_ob;
4137  cpp_string str, istr, *strs;
4138  cp_token *tok;
4139  enum cpp_ttype type, curr_type;
4140  int have_suffix_p = 0;
4141  tree string_tree;
4142  tree suffix_id = NULL_TREE;
4143  bool curr_tok_is_userdef_p = false;
4144
4145  tok = cp_lexer_peek_token (parser->lexer);
4146  if (!cp_parser_is_string_literal (tok))
4147    {
4148      cp_parser_error (parser, "expected string-literal");
4149      return error_mark_node;
4150    }
4151
4152  location_t loc = tok->location;
4153
4154  if (cpp_userdef_string_p (tok->type))
4155    {
4156      string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4157      curr_type = cpp_userdef_string_remove_type (tok->type);
4158      curr_tok_is_userdef_p = true;
4159    }
4160  else
4161    {
4162      string_tree = tok->u.value;
4163      curr_type = tok->type;
4164    }
4165  type = curr_type;
4166
4167  /* Try to avoid the overhead of creating and destroying an obstack
4168     for the common case of just one string.  */
4169  if (!cp_parser_is_string_literal
4170      (cp_lexer_peek_nth_token (parser->lexer, 2)))
4171    {
4172      cp_lexer_consume_token (parser->lexer);
4173
4174      str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4175      str.len = TREE_STRING_LENGTH (string_tree);
4176      count = 1;
4177
4178      if (curr_tok_is_userdef_p)
4179	{
4180	  suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4181	  have_suffix_p = 1;
4182	  curr_type = cpp_userdef_string_remove_type (tok->type);
4183	}
4184      else
4185	curr_type = tok->type;
4186
4187      strs = &str;
4188    }
4189  else
4190    {
4191      location_t last_tok_loc = tok->location;
4192      gcc_obstack_init (&str_ob);
4193      gcc_obstack_init (&loc_ob);
4194      count = 0;
4195
4196      do
4197	{
4198	  cp_lexer_consume_token (parser->lexer);
4199	  count++;
4200	  str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4201	  str.len = TREE_STRING_LENGTH (string_tree);
4202
4203	  if (curr_tok_is_userdef_p)
4204	    {
4205	      tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4206	      if (have_suffix_p == 0)
4207		{
4208		  suffix_id = curr_suffix_id;
4209		  have_suffix_p = 1;
4210		}
4211	      else if (have_suffix_p == 1
4212		       && curr_suffix_id != suffix_id)
4213		{
4214		  error ("inconsistent user-defined literal suffixes"
4215			 " %qD and %qD in string literal",
4216			 suffix_id, curr_suffix_id);
4217		  have_suffix_p = -1;
4218		}
4219	      curr_type = cpp_userdef_string_remove_type (tok->type);
4220	    }
4221	  else
4222	    curr_type = tok->type;
4223
4224	  if (type != curr_type)
4225	    {
4226	      if (type == CPP_STRING)
4227		type = curr_type;
4228	      else if (curr_type != CPP_STRING)
4229		{
4230		  rich_location rich_loc (line_table, tok->location);
4231		  rich_loc.add_range (last_tok_loc);
4232		  error_at (&rich_loc,
4233			    "unsupported non-standard concatenation "
4234			    "of string literals");
4235		}
4236	    }
4237
4238	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
4239	  obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4240
4241	  last_tok_loc = tok->location;
4242
4243	  tok = cp_lexer_peek_token (parser->lexer);
4244	  if (cpp_userdef_string_p (tok->type))
4245	    {
4246	      string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4247	      curr_type = cpp_userdef_string_remove_type (tok->type);
4248	      curr_tok_is_userdef_p = true;
4249	    }
4250	  else
4251	    {
4252	      string_tree = tok->u.value;
4253	      curr_type = tok->type;
4254	      curr_tok_is_userdef_p = false;
4255	    }
4256	}
4257      while (cp_parser_is_string_literal (tok));
4258
4259      /* A string literal built by concatenation has its caret=start at
4260	 the start of the initial string, and its finish at the finish of
4261	 the final string literal.  */
4262      loc = make_location (loc, loc, get_finish (last_tok_loc));
4263
4264      strs = (cpp_string *) obstack_finish (&str_ob);
4265    }
4266
4267  if (type != CPP_STRING && !wide_ok)
4268    {
4269      cp_parser_error (parser, "a wide string is invalid in this context");
4270      type = CPP_STRING;
4271    }
4272
4273  if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4274      (parse_in, strs, count, &istr, type))
4275    {
4276      value = build_string (istr.len, (const char *)istr.text);
4277      free (CONST_CAST (unsigned char *, istr.text));
4278      if (count > 1)
4279	{
4280	  location_t *locs = (location_t *)obstack_finish (&loc_ob);
4281	  gcc_assert (g_string_concat_db);
4282	  g_string_concat_db->record_string_concatenation (count, locs);
4283	}
4284
4285      switch (type)
4286	{
4287	default:
4288	case CPP_STRING:
4289	  TREE_TYPE (value) = char_array_type_node;
4290	  break;
4291	case CPP_UTF8STRING:
4292	  if (flag_char8_t)
4293	    TREE_TYPE (value) = char8_array_type_node;
4294	  else
4295	    TREE_TYPE (value) = char_array_type_node;
4296	  break;
4297	case CPP_STRING16:
4298	  TREE_TYPE (value) = char16_array_type_node;
4299	  break;
4300	case CPP_STRING32:
4301	  TREE_TYPE (value) = char32_array_type_node;
4302	  break;
4303	case CPP_WSTRING:
4304	  TREE_TYPE (value) = wchar_array_type_node;
4305	  break;
4306	}
4307
4308      value = fix_string_type (value);
4309
4310      if (have_suffix_p)
4311	{
4312	  tree literal = build_userdef_literal (suffix_id, value,
4313						OT_NONE, NULL_TREE);
4314	  if (lookup_udlit)
4315	    value = cp_parser_userdef_string_literal (literal);
4316	  else
4317	    value = literal;
4318	}
4319    }
4320  else
4321    /* cpp_interpret_string has issued an error.  */
4322    value = error_mark_node;
4323
4324  if (count > 1)
4325    {
4326      obstack_free (&str_ob, 0);
4327      obstack_free (&loc_ob, 0);
4328    }
4329
4330  return cp_expr (value, loc);
4331}
4332
4333/* Look up a literal operator with the name and the exact arguments.  */
4334
4335static tree
4336lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4337{
4338  tree decl = lookup_name (name);
4339  if (!decl || !is_overloaded_fn (decl))
4340    return error_mark_node;
4341
4342  for (lkp_iterator iter (decl); iter; ++iter)
4343    {
4344      tree fn = *iter;
4345
4346      if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4347	{
4348	  unsigned int ix;
4349	  bool found = true;
4350
4351	  for (ix = 0;
4352	       found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4353	       ++ix, parmtypes = TREE_CHAIN (parmtypes))
4354	    {
4355	      tree tparm = TREE_VALUE (parmtypes);
4356	      tree targ = TREE_TYPE ((*args)[ix]);
4357	      bool ptr = TYPE_PTR_P (tparm);
4358	      bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4359	      if ((ptr || arr || !same_type_p (tparm, targ))
4360		  && (!ptr || !arr
4361		      || !same_type_p (TREE_TYPE (tparm),
4362				       TREE_TYPE (targ))))
4363		found = false;
4364	    }
4365
4366	  if (found
4367	      && ix == vec_safe_length (args)
4368	      /* May be this should be sufficient_parms_p instead,
4369		 depending on how exactly should user-defined literals
4370		 work in presence of default arguments on the literal
4371		 operator parameters.  */
4372	      && parmtypes == void_list_node)
4373	    return decl;
4374	}
4375    }
4376
4377  return error_mark_node;
4378}
4379
4380/* Parse a user-defined char constant.  Returns a call to a user-defined
4381   literal operator taking the character as an argument.  */
4382
4383static cp_expr
4384cp_parser_userdef_char_literal (cp_parser *parser)
4385{
4386  cp_token *token = cp_lexer_consume_token (parser->lexer);
4387  tree literal = token->u.value;
4388  tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4389  tree value = USERDEF_LITERAL_VALUE (literal);
4390  tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4391  tree decl, result;
4392
4393  /* Build up a call to the user-defined operator  */
4394  /* Lookup the name we got back from the id-expression.  */
4395  releasing_vec args;
4396  vec_safe_push (args, value);
4397  decl = lookup_literal_operator (name, args);
4398  if (!decl || decl == error_mark_node)
4399    {
4400      error ("unable to find character literal operator %qD with %qT argument",
4401	     name, TREE_TYPE (value));
4402      return error_mark_node;
4403    }
4404  result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4405  return result;
4406}
4407
4408/* A subroutine of cp_parser_userdef_numeric_literal to
4409   create a char... template parameter pack from a string node.  */
4410
4411static tree
4412make_char_string_pack (tree value)
4413{
4414  tree charvec;
4415  tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4416  const char *str = TREE_STRING_POINTER (value);
4417  int i, len = TREE_STRING_LENGTH (value) - 1;
4418  tree argvec = make_tree_vec (1);
4419
4420  /* Fill in CHARVEC with all of the parameters.  */
4421  charvec = make_tree_vec (len);
4422  for (i = 0; i < len; ++i)
4423    {
4424      unsigned char s[3] = { '\'', str[i], '\'' };
4425      cpp_string in = { 3, s };
4426      cpp_string out = { 0, 0 };
4427      if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4428	return NULL_TREE;
4429      gcc_assert (out.len == 2);
4430      TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4431						 out.text[0]);
4432    }
4433
4434  /* Build the argument packs.  */
4435  SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4436
4437  TREE_VEC_ELT (argvec, 0) = argpack;
4438
4439  return argvec;
4440}
4441
4442/* A subroutine of cp_parser_userdef_numeric_literal to
4443   create a char... template parameter pack from a string node.  */
4444
4445static tree
4446make_string_pack (tree value)
4447{
4448  tree charvec;
4449  tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4450  const unsigned char *str
4451    = (const unsigned char *) TREE_STRING_POINTER (value);
4452  int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4453  int len = TREE_STRING_LENGTH (value) / sz - 1;
4454  tree argvec = make_tree_vec (2);
4455
4456  tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4457  str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4458
4459  /* First template parm is character type.  */
4460  TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4461
4462  /* Fill in CHARVEC with all of the parameters.  */
4463  charvec = make_tree_vec (len);
4464  for (int i = 0; i < len; ++i)
4465    TREE_VEC_ELT (charvec, i)
4466      = double_int_to_tree (str_char_type_node,
4467			    double_int::from_buffer (str + i * sz, sz));
4468
4469  /* Build the argument packs.  */
4470  SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4471
4472  TREE_VEC_ELT (argvec, 1) = argpack;
4473
4474  return argvec;
4475}
4476
4477/* Parse a user-defined numeric constant.  returns a call to a user-defined
4478   literal operator.  */
4479
4480static cp_expr
4481cp_parser_userdef_numeric_literal (cp_parser *parser)
4482{
4483  cp_token *token = cp_lexer_consume_token (parser->lexer);
4484  tree literal = token->u.value;
4485  tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4486  tree value = USERDEF_LITERAL_VALUE (literal);
4487  int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4488  tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4489  tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4490  tree decl, result;
4491
4492  /* Look for a literal operator taking the exact type of numeric argument
4493     as the literal value.  */
4494  releasing_vec args;
4495  vec_safe_push (args, value);
4496  decl = lookup_literal_operator (name, args);
4497  if (decl && decl != error_mark_node)
4498    {
4499      result = finish_call_expr (decl, &args, false, true,
4500				 tf_warning_or_error);
4501
4502      if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4503	{
4504	  warning_at (token->location, OPT_Woverflow,
4505		      "integer literal exceeds range of %qT type",
4506		      long_long_unsigned_type_node);
4507	}
4508      else
4509	{
4510	  if (overflow > 0)
4511	    warning_at (token->location, OPT_Woverflow,
4512			"floating literal exceeds range of %qT type",
4513			long_double_type_node);
4514	  else if (overflow < 0)
4515	    warning_at (token->location, OPT_Woverflow,
4516			"floating literal truncated to zero");
4517	}
4518
4519      return result;
4520    }
4521
4522  /* If the numeric argument didn't work, look for a raw literal
4523     operator taking a const char* argument consisting of the number
4524     in string format.  */
4525  args->truncate (0);
4526  vec_safe_push (args, num_string);
4527  decl = lookup_literal_operator (name, args);
4528  if (decl && decl != error_mark_node)
4529    {
4530      result = finish_call_expr (decl, &args, false, true,
4531				 tf_warning_or_error);
4532      return result;
4533    }
4534
4535  /* If the raw literal didn't work, look for a non-type template
4536     function with parameter pack char....  Call the function with
4537     template parameter characters representing the number.  */
4538  args->truncate (0);
4539  decl = lookup_literal_operator (name, args);
4540  if (decl && decl != error_mark_node)
4541    {
4542      tree tmpl_args = make_char_string_pack (num_string);
4543      if (tmpl_args == NULL_TREE)
4544	{
4545	  error ("failed to translate literal to execution character set %qT",
4546		 num_string);
4547	  return error_mark_node;
4548	}
4549      decl = lookup_template_function (decl, tmpl_args);
4550      result = finish_call_expr (decl, &args, false, true,
4551				 tf_warning_or_error);
4552      return result;
4553    }
4554
4555  /* In C++14 the standard library defines complex number suffixes that
4556     conflict with GNU extensions.  Prefer them if <complex> is #included.  */
4557  bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4558  bool i14 = (cxx_dialect > cxx11
4559	      && (id_equal (suffix_id, "i")
4560		  || id_equal (suffix_id, "if")
4561		  || id_equal (suffix_id, "il")));
4562  diagnostic_t kind = DK_ERROR;
4563  int opt = 0;
4564
4565  if (i14 && ext)
4566    {
4567      tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4568					  0, false);
4569      if (cxlit == error_mark_node)
4570	{
4571	  /* No <complex>, so pedwarn and use GNU semantics.  */
4572	  kind = DK_PEDWARN;
4573	  opt = OPT_Wpedantic;
4574	}
4575    }
4576
4577  bool complained
4578    = emit_diagnostic (kind, input_location, opt,
4579		       "unable to find numeric literal operator %qD", name);
4580
4581  if (!complained)
4582    /* Don't inform either.  */;
4583  else if (i14)
4584    {
4585      inform (token->location, "add %<using namespace std::complex_literals%> "
4586	      "(from %<<complex>%>) to enable the C++14 user-defined literal "
4587	      "suffixes");
4588      if (ext)
4589	inform (token->location, "or use %<j%> instead of %<i%> for the "
4590		"GNU built-in suffix");
4591    }
4592  else if (!ext)
4593    inform (token->location, "use %<-fext-numeric-literals%> "
4594	    "to enable more built-in suffixes");
4595
4596  if (kind == DK_ERROR)
4597    value = error_mark_node;
4598  else
4599    {
4600      /* Use the built-in semantics.  */
4601      tree type;
4602      if (id_equal (suffix_id, "i"))
4603	{
4604	  if (TREE_CODE (value) == INTEGER_CST)
4605	    type = integer_type_node;
4606	  else
4607	    type = double_type_node;
4608	}
4609      else if (id_equal (suffix_id, "if"))
4610	type = float_type_node;
4611      else /* if (id_equal (suffix_id, "il")) */
4612	type = long_double_type_node;
4613
4614      value = build_complex (build_complex_type (type),
4615			     fold_convert (type, integer_zero_node),
4616			     fold_convert (type, value));
4617    }
4618
4619  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4620    /* Avoid repeated diagnostics.  */
4621    token->u.value = value;
4622  return value;
4623}
4624
4625/* Parse a user-defined string constant.  Returns a call to a user-defined
4626   literal operator taking a character pointer and the length of the string
4627   as arguments.  */
4628
4629static tree
4630cp_parser_userdef_string_literal (tree literal)
4631{
4632  tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4633  tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4634  tree value = USERDEF_LITERAL_VALUE (literal);
4635  int len = TREE_STRING_LENGTH (value)
4636	/ TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4637  tree decl;
4638
4639  /* Build up a call to the user-defined operator.  */
4640  /* Lookup the name we got back from the id-expression.  */
4641  releasing_vec args;
4642  vec_safe_push (args, value);
4643  vec_safe_push (args, build_int_cst (size_type_node, len));
4644  decl = lookup_literal_operator (name, args);
4645
4646  if (decl && decl != error_mark_node)
4647    return finish_call_expr (decl, &args, false, true,
4648			     tf_warning_or_error);
4649
4650  /* Look for a suitable template function, either (C++20) with a single
4651     parameter of class type, or (N3599) with typename parameter CharT and
4652     parameter pack CharT...  */
4653  args->truncate (0);
4654  decl = lookup_literal_operator (name, args);
4655  if (decl && decl != error_mark_node)
4656    {
4657      /* Use resolve_nondeduced_context to try to choose one form of template
4658	 or the other.  */
4659      tree tmpl_args = make_tree_vec (1);
4660      TREE_VEC_ELT (tmpl_args, 0) = value;
4661      decl = lookup_template_function (decl, tmpl_args);
4662      tree res = resolve_nondeduced_context (decl, tf_none);
4663      if (DECL_P (res))
4664	decl = res;
4665      else
4666	{
4667	  TREE_OPERAND (decl, 1) = make_string_pack (value);
4668	  res = resolve_nondeduced_context (decl, tf_none);
4669	  if (DECL_P (res))
4670	    decl = res;
4671	}
4672      if (!DECL_P (decl) && cxx_dialect > cxx17)
4673	TREE_OPERAND (decl, 1) = tmpl_args;
4674      return finish_call_expr (decl, &args, false, true,
4675			       tf_warning_or_error);
4676    }
4677
4678  error ("unable to find string literal operator %qD with %qT, %qT arguments",
4679	 name, TREE_TYPE (value), size_type_node);
4680  return error_mark_node;
4681}
4682
4683
4684/* Basic concepts [gram.basic]  */
4685
4686/* Parse a translation-unit.
4687
4688   translation-unit:
4689     declaration-seq [opt]  */
4690
4691static void
4692cp_parser_translation_unit (cp_parser* parser)
4693{
4694  gcc_checking_assert (!cp_error_declarator);
4695
4696  /* Create the declarator obstack.  */
4697  gcc_obstack_init (&declarator_obstack);
4698  /* Create the error declarator.  */
4699  cp_error_declarator = make_declarator (cdk_error);
4700  /* Create the empty parameter list.  */
4701  no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4702					     UNKNOWN_LOCATION);
4703  /* Remember where the base of the declarator obstack lies.  */
4704  void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4705
4706  bool implicit_extern_c = false;
4707
4708  for (;;)
4709    {
4710      cp_token *token = cp_lexer_peek_token (parser->lexer);
4711
4712      /* If we're entering or exiting a region that's implicitly
4713	 extern "C", modify the lang context appropriately.  */
4714      if (implicit_extern_c
4715	  != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4716	{
4717	  implicit_extern_c = !implicit_extern_c;
4718	  if (implicit_extern_c)
4719	    push_lang_context (lang_name_c);
4720	  else
4721	    pop_lang_context ();
4722	}
4723
4724      if (token->type == CPP_EOF)
4725	break;
4726
4727      if (token->type == CPP_CLOSE_BRACE)
4728	{
4729	  cp_parser_error (parser, "expected declaration");
4730	  cp_lexer_consume_token (parser->lexer);
4731	  /* If the next token is now a `;', consume it.  */
4732	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4733	    cp_lexer_consume_token (parser->lexer);
4734	}
4735      else
4736	cp_parser_toplevel_declaration (parser);
4737    }
4738
4739  /* Get rid of the token array; we don't need it any more.  */
4740  cp_lexer_destroy (parser->lexer);
4741  parser->lexer = NULL;
4742
4743  /* The EOF should have reset this. */
4744  gcc_checking_assert (!implicit_extern_c);
4745
4746  /* Make sure the declarator obstack was fully cleaned up.  */
4747  gcc_assert (obstack_next_free (&declarator_obstack)
4748	      == declarator_obstack_base);
4749}
4750
4751/* Return the appropriate tsubst flags for parsing, possibly in N3276
4752   decltype context.  */
4753
4754static inline tsubst_flags_t
4755complain_flags (bool decltype_p)
4756{
4757  tsubst_flags_t complain = tf_warning_or_error;
4758  if (decltype_p)
4759    complain |= tf_decltype;
4760  return complain;
4761}
4762
4763/* We're about to parse a collection of statements.  If we're currently
4764   parsing tentatively, set up a firewall so that any nested
4765   cp_parser_commit_to_tentative_parse won't affect the current context.  */
4766
4767static cp_token_position
4768cp_parser_start_tentative_firewall (cp_parser *parser)
4769{
4770  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4771    return 0;
4772
4773  cp_parser_parse_tentatively (parser);
4774  cp_parser_commit_to_topmost_tentative_parse (parser);
4775  return cp_lexer_token_position (parser->lexer, false);
4776}
4777
4778/* We've finished parsing the collection of statements.  Wrap up the
4779   firewall and replace the relevant tokens with the parsed form.  */
4780
4781static void
4782cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4783				  tree expr)
4784{
4785  if (!start)
4786    return;
4787
4788  /* Finish the firewall level.  */
4789  cp_parser_parse_definitely (parser);
4790  /* And remember the result of the parse for when we try again.  */
4791  cp_token *token = cp_lexer_token_at (parser->lexer, start);
4792  token->type = CPP_PREPARSED_EXPR;
4793  token->u.value = expr;
4794  token->keyword = RID_MAX;
4795  cp_lexer_purge_tokens_after (parser->lexer, start);
4796}
4797
4798/* Like the above functions, but let the user modify the tokens.  Used by
4799   CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4800   later parses, so it makes sense to localize the effects of
4801   cp_parser_commit_to_tentative_parse.  */
4802
4803struct tentative_firewall
4804{
4805  cp_parser *parser;
4806  bool set;
4807
4808  tentative_firewall (cp_parser *p): parser(p)
4809  {
4810    /* If we're currently parsing tentatively, start a committed level as a
4811       firewall and then an inner tentative parse.  */
4812    if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4813      {
4814	cp_parser_parse_tentatively (parser);
4815	cp_parser_commit_to_topmost_tentative_parse (parser);
4816	cp_parser_parse_tentatively (parser);
4817      }
4818  }
4819
4820  ~tentative_firewall()
4821  {
4822    if (set)
4823      {
4824	/* Finish the inner tentative parse and the firewall, propagating any
4825	   uncommitted error state to the outer tentative parse.  */
4826	bool err = cp_parser_error_occurred (parser);
4827	cp_parser_parse_definitely (parser);
4828	cp_parser_parse_definitely (parser);
4829	if (err)
4830	  cp_parser_simulate_error (parser);
4831      }
4832  }
4833};
4834
4835/* Some tokens naturally come in pairs e.g.'(' and ')'.
4836   This class is for tracking such a matching pair of symbols.
4837   In particular, it tracks the location of the first token,
4838   so that if the second token is missing, we can highlight the
4839   location of the first token when notifying the user about the
4840   problem.  */
4841
4842template <typename traits_t>
4843class token_pair
4844{
4845 public:
4846  /* token_pair's ctor.  */
4847  token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4848
4849  /* If the next token is the opening symbol for this pair, consume it and
4850     return true.
4851     Otherwise, issue an error and return false.
4852     In either case, record the location of the opening token.  */
4853
4854  bool require_open (cp_parser *parser)
4855  {
4856    m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4857    return cp_parser_require (parser, traits_t::open_token_type,
4858			      traits_t::required_token_open);
4859  }
4860
4861  /* Consume the next token from PARSER, recording its location as
4862     that of the opening token within the pair.  */
4863
4864  cp_token * consume_open (cp_parser *parser)
4865  {
4866    cp_token *tok = cp_lexer_consume_token (parser->lexer);
4867    gcc_assert (tok->type == traits_t::open_token_type);
4868    m_open_loc = tok->location;
4869    return tok;
4870  }
4871
4872  /* If the next token is the closing symbol for this pair, consume it
4873     and return it.
4874     Otherwise, issue an error, highlighting the location of the
4875     corresponding opening token, and return NULL.  */
4876
4877  cp_token *require_close (cp_parser *parser) const
4878  {
4879    return cp_parser_require (parser, traits_t::close_token_type,
4880			      traits_t::required_token_close,
4881			      m_open_loc);
4882  }
4883
4884  location_t open_location () const { return m_open_loc; }
4885
4886 private:
4887  location_t m_open_loc;
4888};
4889
4890/* Traits for token_pair<T> for tracking matching pairs of parentheses.  */
4891
4892struct matching_paren_traits
4893{
4894  static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4895  static const enum required_token required_token_open  = RT_OPEN_PAREN;
4896  static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4897  static const enum required_token required_token_close = RT_CLOSE_PAREN;
4898};
4899
4900/* "matching_parens" is a token_pair<T> class for tracking matching
4901   pairs of parentheses.  */
4902
4903typedef token_pair<matching_paren_traits> matching_parens;
4904
4905/* Traits for token_pair<T> for tracking matching pairs of braces.  */
4906
4907struct matching_brace_traits
4908{
4909  static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4910  static const enum required_token required_token_open = RT_OPEN_BRACE;
4911  static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4912  static const enum required_token required_token_close = RT_CLOSE_BRACE;
4913};
4914
4915/* "matching_braces" is a token_pair<T> class for tracking matching
4916   pairs of braces.  */
4917
4918typedef token_pair<matching_brace_traits> matching_braces;
4919
4920
4921/* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4922   enclosing parentheses.  */
4923
4924static cp_expr
4925cp_parser_statement_expr (cp_parser *parser)
4926{
4927  cp_token_position start = cp_parser_start_tentative_firewall (parser);
4928
4929  /* Consume the '('.  */
4930  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4931  matching_parens parens;
4932  parens.consume_open (parser);
4933  /* Start the statement-expression.  */
4934  tree expr = begin_stmt_expr ();
4935  /* Parse the compound-statement.  */
4936  cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4937  /* Finish up.  */
4938  expr = finish_stmt_expr (expr, false);
4939  /* Consume the ')'.  */
4940  location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4941  if (!parens.require_close (parser))
4942    cp_parser_skip_to_end_of_statement (parser);
4943
4944  cp_parser_end_tentative_firewall (parser, start, expr);
4945  location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4946  return cp_expr (expr, combined_loc);
4947}
4948
4949/* Expressions [gram.expr] */
4950
4951/* Parse a fold-operator.
4952
4953    fold-operator:
4954        -  *  /  %  ^  &  |  =  <  >  <<  >>
4955      =  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=
4956      ==  !=  <=  >=  &&  ||  ,  .*  ->*
4957
4958   This returns the tree code corresponding to the matched operator
4959   as an int. When the current token matches a compound assignment
4960   operator, the resulting tree code is the negative value of the
4961   non-assignment operator. */
4962
4963static int
4964cp_parser_fold_operator (cp_token *token)
4965{
4966  switch (token->type)
4967    {
4968    case CPP_PLUS: return PLUS_EXPR;
4969    case CPP_MINUS: return MINUS_EXPR;
4970    case CPP_MULT: return MULT_EXPR;
4971    case CPP_DIV: return TRUNC_DIV_EXPR;
4972    case CPP_MOD: return TRUNC_MOD_EXPR;
4973    case CPP_XOR: return BIT_XOR_EXPR;
4974    case CPP_AND: return BIT_AND_EXPR;
4975    case CPP_OR: return BIT_IOR_EXPR;
4976    case CPP_LSHIFT: return LSHIFT_EXPR;
4977    case CPP_RSHIFT: return RSHIFT_EXPR;
4978
4979    case CPP_EQ: return -NOP_EXPR;
4980    case CPP_PLUS_EQ: return -PLUS_EXPR;
4981    case CPP_MINUS_EQ: return -MINUS_EXPR;
4982    case CPP_MULT_EQ: return -MULT_EXPR;
4983    case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4984    case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4985    case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4986    case CPP_AND_EQ: return -BIT_AND_EXPR;
4987    case CPP_OR_EQ: return -BIT_IOR_EXPR;
4988    case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4989    case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4990
4991    case CPP_EQ_EQ: return EQ_EXPR;
4992    case CPP_NOT_EQ: return NE_EXPR;
4993    case CPP_LESS: return LT_EXPR;
4994    case CPP_GREATER: return GT_EXPR;
4995    case CPP_LESS_EQ: return LE_EXPR;
4996    case CPP_GREATER_EQ: return GE_EXPR;
4997
4998    case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4999    case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5000
5001    case CPP_COMMA: return COMPOUND_EXPR;
5002
5003    case CPP_DOT_STAR: return DOTSTAR_EXPR;
5004    case CPP_DEREF_STAR: return MEMBER_REF;
5005
5006    default: return ERROR_MARK;
5007    }
5008}
5009
5010/* Returns true if CODE indicates a binary expression, which is not allowed in
5011   the LHS of a fold-expression.  More codes will need to be added to use this
5012   function in other contexts.  */
5013
5014static bool
5015is_binary_op (tree_code code)
5016{
5017  switch (code)
5018    {
5019    case PLUS_EXPR:
5020    case POINTER_PLUS_EXPR:
5021    case MINUS_EXPR:
5022    case MULT_EXPR:
5023    case TRUNC_DIV_EXPR:
5024    case TRUNC_MOD_EXPR:
5025    case BIT_XOR_EXPR:
5026    case BIT_AND_EXPR:
5027    case BIT_IOR_EXPR:
5028    case LSHIFT_EXPR:
5029    case RSHIFT_EXPR:
5030
5031    case MODOP_EXPR:
5032
5033    case EQ_EXPR:
5034    case NE_EXPR:
5035    case LE_EXPR:
5036    case GE_EXPR:
5037    case LT_EXPR:
5038    case GT_EXPR:
5039
5040    case TRUTH_ANDIF_EXPR:
5041    case TRUTH_ORIF_EXPR:
5042
5043    case COMPOUND_EXPR:
5044
5045    case DOTSTAR_EXPR:
5046    case MEMBER_REF:
5047      return true;
5048
5049    default:
5050      return false;
5051    }
5052}
5053
5054/* If the next token is a suitable fold operator, consume it and return as
5055   the function above.  */
5056
5057static int
5058cp_parser_fold_operator (cp_parser *parser)
5059{
5060  cp_token* token = cp_lexer_peek_token (parser->lexer);
5061  int code = cp_parser_fold_operator (token);
5062  if (code != ERROR_MARK)
5063    cp_lexer_consume_token (parser->lexer);
5064  return code;
5065}
5066
5067/* Parse a fold-expression.
5068
5069     fold-expression:
5070       ( ... folding-operator cast-expression)
5071       ( cast-expression folding-operator ... )
5072       ( cast-expression folding operator ... folding-operator cast-expression)
5073
5074   Note that the '(' and ')' are matched in primary expression. */
5075
5076static cp_expr
5077cp_parser_fold_expression (cp_parser *parser, tree expr1)
5078{
5079  cp_id_kind pidk;
5080
5081  // Left fold.
5082  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5083    {
5084      cp_lexer_consume_token (parser->lexer);
5085      int op = cp_parser_fold_operator (parser);
5086      if (op == ERROR_MARK)
5087        {
5088          cp_parser_error (parser, "expected binary operator");
5089          return error_mark_node;
5090        }
5091
5092      tree expr = cp_parser_cast_expression (parser, false, false,
5093					     false, &pidk);
5094      if (expr == error_mark_node)
5095        return error_mark_node;
5096      return finish_left_unary_fold_expr (expr, op);
5097    }
5098
5099  const cp_token* token = cp_lexer_peek_token (parser->lexer);
5100  int op = cp_parser_fold_operator (parser);
5101  if (op == ERROR_MARK)
5102    {
5103      cp_parser_error (parser, "expected binary operator");
5104      return error_mark_node;
5105    }
5106
5107  if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5108    {
5109      cp_parser_error (parser, "expected ...");
5110      return error_mark_node;
5111    }
5112  cp_lexer_consume_token (parser->lexer);
5113
5114  /* The operands of a fold-expression are cast-expressions, so binary or
5115     conditional expressions are not allowed.  We check this here to avoid
5116     tentative parsing.  */
5117  if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5118    /* OK, the expression was parenthesized.  */;
5119  else if (is_binary_op (TREE_CODE (expr1)))
5120    error_at (location_of (expr1),
5121	      "binary expression in operand of fold-expression");
5122  else if (TREE_CODE (expr1) == COND_EXPR
5123	   || (REFERENCE_REF_P (expr1)
5124	       && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5125    error_at (location_of (expr1),
5126	      "conditional expression in operand of fold-expression");
5127
5128  // Right fold.
5129  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5130    return finish_right_unary_fold_expr (expr1, op);
5131
5132  if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5133    {
5134      cp_parser_error (parser, "mismatched operator in fold-expression");
5135      return error_mark_node;
5136    }
5137  cp_lexer_consume_token (parser->lexer);
5138
5139  // Binary left or right fold.
5140  tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5141  if (expr2 == error_mark_node)
5142    return error_mark_node;
5143  return finish_binary_fold_expr (expr1, expr2, op);
5144}
5145
5146/* Parse a primary-expression.
5147
5148   primary-expression:
5149     literal
5150     this
5151     ( expression )
5152     id-expression
5153     lambda-expression (C++11)
5154
5155   GNU Extensions:
5156
5157   primary-expression:
5158     ( compound-statement )
5159     __builtin_va_arg ( assignment-expression , type-id )
5160     __builtin_offsetof ( type-id , offsetof-expression )
5161
5162   C++ Extensions:
5163     __has_nothrow_assign ( type-id )
5164     __has_nothrow_constructor ( type-id )
5165     __has_nothrow_copy ( type-id )
5166     __has_trivial_assign ( type-id )
5167     __has_trivial_constructor ( type-id )
5168     __has_trivial_copy ( type-id )
5169     __has_trivial_destructor ( type-id )
5170     __has_virtual_destructor ( type-id )
5171     __is_abstract ( type-id )
5172     __is_base_of ( type-id , type-id )
5173     __is_class ( type-id )
5174     __is_empty ( type-id )
5175     __is_enum ( type-id )
5176     __is_final ( type-id )
5177     __is_literal_type ( type-id )
5178     __is_pod ( type-id )
5179     __is_polymorphic ( type-id )
5180     __is_std_layout ( type-id )
5181     __is_trivial ( type-id )
5182     __is_union ( type-id )
5183
5184   Objective-C++ Extension:
5185
5186   primary-expression:
5187     objc-expression
5188
5189   literal:
5190     __null
5191
5192   ADDRESS_P is true iff this expression was immediately preceded by
5193   "&" and therefore might denote a pointer-to-member.  CAST_P is true
5194   iff this expression is the target of a cast.  TEMPLATE_ARG_P is
5195   true iff this expression is a template argument.
5196
5197   Returns a representation of the expression.  Upon return, *IDK
5198   indicates what kind of id-expression (if any) was present.  */
5199
5200static cp_expr
5201cp_parser_primary_expression (cp_parser *parser,
5202			      bool address_p,
5203			      bool cast_p,
5204			      bool template_arg_p,
5205			      bool decltype_p,
5206			      cp_id_kind *idk)
5207{
5208  cp_token *token = NULL;
5209
5210  /* Assume the primary expression is not an id-expression.  */
5211  *idk = CP_ID_KIND_NONE;
5212
5213  /* Peek at the next token.  */
5214  token = cp_lexer_peek_token (parser->lexer);
5215  switch ((int) token->type)
5216    {
5217      /* literal:
5218	   integer-literal
5219	   character-literal
5220	   floating-literal
5221	   string-literal
5222	   boolean-literal
5223	   pointer-literal
5224	   user-defined-literal  */
5225    case CPP_CHAR:
5226    case CPP_CHAR16:
5227    case CPP_CHAR32:
5228    case CPP_WCHAR:
5229    case CPP_UTF8CHAR:
5230    case CPP_NUMBER:
5231    case CPP_PREPARSED_EXPR:
5232      if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5233	return cp_parser_userdef_numeric_literal (parser);
5234      token = cp_lexer_consume_token (parser->lexer);
5235      if (TREE_CODE (token->u.value) == FIXED_CST)
5236	{
5237	  error_at (token->location,
5238		    "fixed-point types not supported in C++");
5239	  return error_mark_node;
5240	}
5241      /* Floating-point literals are only allowed in an integral
5242	 constant expression if they are cast to an integral or
5243	 enumeration type.  */
5244      if (TREE_CODE (token->u.value) == REAL_CST
5245	  && parser->integral_constant_expression_p
5246	  && pedantic)
5247	{
5248	  /* CAST_P will be set even in invalid code like "int(2.7 +
5249	     ...)".   Therefore, we have to check that the next token
5250	     is sure to end the cast.  */
5251	  if (cast_p)
5252	    {
5253	      cp_token *next_token;
5254
5255	      next_token = cp_lexer_peek_token (parser->lexer);
5256	      if (/* The comma at the end of an
5257		     enumerator-definition.  */
5258		  next_token->type != CPP_COMMA
5259		  /* The curly brace at the end of an enum-specifier.  */
5260		  && next_token->type != CPP_CLOSE_BRACE
5261		  /* The end of a statement.  */
5262		  && next_token->type != CPP_SEMICOLON
5263		  /* The end of the cast-expression.  */
5264		  && next_token->type != CPP_CLOSE_PAREN
5265		  /* The end of an array bound.  */
5266		  && next_token->type != CPP_CLOSE_SQUARE
5267		  /* The closing ">" in a template-argument-list.  */
5268		  && (next_token->type != CPP_GREATER
5269		      || parser->greater_than_is_operator_p)
5270		  /* C++0x only: A ">>" treated like two ">" tokens,
5271                     in a template-argument-list.  */
5272		  && (next_token->type != CPP_RSHIFT
5273                      || (cxx_dialect == cxx98)
5274		      || parser->greater_than_is_operator_p))
5275		cast_p = false;
5276	    }
5277
5278	  /* If we are within a cast, then the constraint that the
5279	     cast is to an integral or enumeration type will be
5280	     checked at that point.  If we are not within a cast, then
5281	     this code is invalid.  */
5282	  if (!cast_p)
5283	    cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5284	}
5285      return (cp_expr (token->u.value, token->location)
5286	      .maybe_add_location_wrapper ());
5287
5288    case CPP_CHAR_USERDEF:
5289    case CPP_CHAR16_USERDEF:
5290    case CPP_CHAR32_USERDEF:
5291    case CPP_WCHAR_USERDEF:
5292    case CPP_UTF8CHAR_USERDEF:
5293      return cp_parser_userdef_char_literal (parser);
5294
5295    case CPP_STRING:
5296    case CPP_STRING16:
5297    case CPP_STRING32:
5298    case CPP_WSTRING:
5299    case CPP_UTF8STRING:
5300    case CPP_STRING_USERDEF:
5301    case CPP_STRING16_USERDEF:
5302    case CPP_STRING32_USERDEF:
5303    case CPP_WSTRING_USERDEF:
5304    case CPP_UTF8STRING_USERDEF:
5305      /* ??? Should wide strings be allowed when parser->translate_strings_p
5306	 is false (i.e. in attributes)?  If not, we can kill the third
5307	 argument to cp_parser_string_literal.  */
5308      return (cp_parser_string_literal (parser,
5309					parser->translate_strings_p,
5310					true)
5311	      .maybe_add_location_wrapper ());
5312
5313    case CPP_OPEN_PAREN:
5314      /* If we see `( { ' then we are looking at the beginning of
5315	 a GNU statement-expression.  */
5316      if (cp_parser_allow_gnu_extensions_p (parser)
5317	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5318	{
5319	  /* Statement-expressions are not allowed by the standard.  */
5320	  pedwarn (token->location, OPT_Wpedantic,
5321		   "ISO C++ forbids braced-groups within expressions");
5322
5323	  /* And they're not allowed outside of a function-body; you
5324	     cannot, for example, write:
5325
5326	     int i = ({ int j = 3; j + 1; });
5327
5328	     at class or namespace scope.  */
5329	  if (!parser->in_function_body
5330	      || parser->in_template_argument_list_p)
5331	    {
5332	      error_at (token->location,
5333			"statement-expressions are not allowed outside "
5334			"functions nor in template-argument lists");
5335	      cp_parser_skip_to_end_of_block_or_statement (parser);
5336	      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5337		cp_lexer_consume_token (parser->lexer);
5338	      return error_mark_node;
5339	    }
5340	  else
5341	    return cp_parser_statement_expr (parser);
5342	}
5343      /* Otherwise it's a normal parenthesized expression.  */
5344      {
5345	cp_expr expr;
5346	bool saved_greater_than_is_operator_p;
5347
5348	location_t open_paren_loc = token->location;
5349
5350	/* Consume the `('.  */
5351	matching_parens parens;
5352	parens.consume_open (parser);
5353	/* Within a parenthesized expression, a `>' token is always
5354	   the greater-than operator.  */
5355	saved_greater_than_is_operator_p
5356	  = parser->greater_than_is_operator_p;
5357	parser->greater_than_is_operator_p = true;
5358
5359	if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5360	  /* Left fold expression. */
5361	  expr = NULL_TREE;
5362	else
5363	  /* Parse the parenthesized expression.  */
5364	  expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5365
5366	token = cp_lexer_peek_token (parser->lexer);
5367	if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5368	  {
5369	    expr = cp_parser_fold_expression (parser, expr);
5370	    if (expr != error_mark_node
5371		&& cxx_dialect < cxx17)
5372	      pedwarn (input_location, 0, "fold-expressions only available "
5373		       "with %<-std=c++17%> or %<-std=gnu++17%>");
5374	  }
5375	else
5376	  /* Let the front end know that this expression was
5377	     enclosed in parentheses. This matters in case, for
5378	     example, the expression is of the form `A::B', since
5379	     `&A::B' might be a pointer-to-member, but `&(A::B)' is
5380	     not.  */
5381	  expr = finish_parenthesized_expr (expr);
5382
5383	/* DR 705: Wrapping an unqualified name in parentheses
5384	   suppresses arg-dependent lookup.  We want to pass back
5385	   CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5386	   (c++/37862), but none of the others.  */
5387	if (*idk != CP_ID_KIND_QUALIFIED)
5388	  *idk = CP_ID_KIND_NONE;
5389
5390	/* The `>' token might be the end of a template-id or
5391	   template-parameter-list now.  */
5392	parser->greater_than_is_operator_p
5393	  = saved_greater_than_is_operator_p;
5394
5395	/* Consume the `)'.  */
5396	token = cp_lexer_peek_token (parser->lexer);
5397	location_t close_paren_loc = token->location;
5398	expr.set_range (open_paren_loc, close_paren_loc);
5399	if (!parens.require_close (parser)
5400	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5401	  cp_parser_skip_to_end_of_statement (parser);
5402
5403	return expr;
5404      }
5405
5406    case CPP_OPEN_SQUARE:
5407      {
5408	if (c_dialect_objc ())
5409	  {
5410	    /* We might have an Objective-C++ message. */
5411	    cp_parser_parse_tentatively (parser);
5412	    tree msg = cp_parser_objc_message_expression (parser);
5413	    /* If that works out, we're done ... */
5414	    if (cp_parser_parse_definitely (parser))
5415	      return msg;
5416	    /* ... else, fall though to see if it's a lambda.  */
5417	  }
5418	cp_expr lam = cp_parser_lambda_expression (parser);
5419	/* Don't warn about a failed tentative parse.  */
5420	if (cp_parser_error_occurred (parser))
5421	  return error_mark_node;
5422	maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5423	return lam;
5424      }
5425
5426    case CPP_OBJC_STRING:
5427      if (c_dialect_objc ())
5428	/* We have an Objective-C++ string literal. */
5429        return cp_parser_objc_expression (parser);
5430      cp_parser_error (parser, "expected primary-expression");
5431      return error_mark_node;
5432
5433    case CPP_KEYWORD:
5434      switch (token->keyword)
5435	{
5436	  /* These two are the boolean literals.  */
5437	case RID_TRUE:
5438	  cp_lexer_consume_token (parser->lexer);
5439	  return cp_expr (boolean_true_node, token->location);
5440	case RID_FALSE:
5441	  cp_lexer_consume_token (parser->lexer);
5442	  return cp_expr (boolean_false_node, token->location);
5443
5444	  /* The `__null' literal.  */
5445	case RID_NULL:
5446	  cp_lexer_consume_token (parser->lexer);
5447	  return cp_expr (null_node, token->location);
5448
5449	  /* The `nullptr' literal.  */
5450	case RID_NULLPTR:
5451	  cp_lexer_consume_token (parser->lexer);
5452	  return cp_expr (nullptr_node, token->location);
5453
5454	  /* Recognize the `this' keyword.  */
5455	case RID_THIS:
5456	  cp_lexer_consume_token (parser->lexer);
5457	  if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5458	    {
5459	      error_at (token->location,
5460			"%<this%> may not be used in this context");
5461	      return error_mark_node;
5462	    }
5463	  /* Pointers cannot appear in constant-expressions.  */
5464	  if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5465	    return error_mark_node;
5466	  return cp_expr (finish_this_expr (), token->location);
5467
5468	  /* The `operator' keyword can be the beginning of an
5469	     id-expression.  */
5470	case RID_OPERATOR:
5471	  goto id_expression;
5472
5473	case RID_FUNCTION_NAME:
5474	case RID_PRETTY_FUNCTION_NAME:
5475	case RID_C99_FUNCTION_NAME:
5476	  {
5477	    non_integral_constant name;
5478
5479	    /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5480	       __func__ are the names of variables -- but they are
5481	       treated specially.  Therefore, they are handled here,
5482	       rather than relying on the generic id-expression logic
5483	       below.  Grammatically, these names are id-expressions.
5484
5485	       Consume the token.  */
5486	    token = cp_lexer_consume_token (parser->lexer);
5487
5488	    switch (token->keyword)
5489	      {
5490	      case RID_FUNCTION_NAME:
5491		name = NIC_FUNC_NAME;
5492		break;
5493	      case RID_PRETTY_FUNCTION_NAME:
5494		name = NIC_PRETTY_FUNC;
5495		break;
5496	      case RID_C99_FUNCTION_NAME:
5497		name = NIC_C99_FUNC;
5498		break;
5499	      default:
5500		gcc_unreachable ();
5501	      }
5502
5503	    if (cp_parser_non_integral_constant_expression (parser, name))
5504	      return error_mark_node;
5505
5506	    /* Look up the name.  */
5507	    return finish_fname (token->u.value);
5508	  }
5509
5510	case RID_VA_ARG:
5511	  {
5512	    tree expression;
5513	    tree type;
5514	    location_t type_location;
5515	    location_t start_loc
5516	      = cp_lexer_peek_token (parser->lexer)->location;
5517	    /* The `__builtin_va_arg' construct is used to handle
5518	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
5519	    cp_lexer_consume_token (parser->lexer);
5520	    /* Look for the opening `('.  */
5521	    matching_parens parens;
5522	    parens.require_open (parser);
5523	    /* Now, parse the assignment-expression.  */
5524	    expression = cp_parser_assignment_expression (parser);
5525	    /* Look for the `,'.  */
5526	    cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5527	    type_location = cp_lexer_peek_token (parser->lexer)->location;
5528	    /* Parse the type-id.  */
5529	    {
5530	      type_id_in_expr_sentinel s (parser);
5531	      type = cp_parser_type_id (parser);
5532	    }
5533	    /* Look for the closing `)'.  */
5534	    location_t finish_loc
5535	      = cp_lexer_peek_token (parser->lexer)->location;
5536	    parens.require_close (parser);
5537	    /* Using `va_arg' in a constant-expression is not
5538	       allowed.  */
5539	    if (cp_parser_non_integral_constant_expression (parser,
5540							    NIC_VA_ARG))
5541	      return error_mark_node;
5542	    /* Construct a location of the form:
5543		 __builtin_va_arg (v, int)
5544		 ~~~~~~~~~~~~~~~~~~~~~^~~~
5545	       with the caret at the type, ranging from the start of the
5546	       "__builtin_va_arg" token to the close paren.  */
5547	    location_t combined_loc
5548	      = make_location (type_location, start_loc, finish_loc);
5549	    return build_x_va_arg (combined_loc, expression, type);
5550	  }
5551
5552	case RID_OFFSETOF:
5553	  return cp_parser_builtin_offsetof (parser);
5554
5555	case RID_HAS_NOTHROW_ASSIGN:
5556	case RID_HAS_NOTHROW_CONSTRUCTOR:
5557	case RID_HAS_NOTHROW_COPY:
5558	case RID_HAS_TRIVIAL_ASSIGN:
5559	case RID_HAS_TRIVIAL_CONSTRUCTOR:
5560	case RID_HAS_TRIVIAL_COPY:
5561	case RID_HAS_TRIVIAL_DESTRUCTOR:
5562	case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5563	case RID_HAS_VIRTUAL_DESTRUCTOR:
5564	case RID_IS_ABSTRACT:
5565	case RID_IS_AGGREGATE:
5566	case RID_IS_BASE_OF:
5567	case RID_IS_CLASS:
5568	case RID_IS_EMPTY:
5569	case RID_IS_ENUM:
5570	case RID_IS_FINAL:
5571	case RID_IS_LITERAL_TYPE:
5572	case RID_IS_POD:
5573	case RID_IS_POLYMORPHIC:
5574	case RID_IS_SAME_AS:
5575	case RID_IS_STD_LAYOUT:
5576	case RID_IS_TRIVIAL:
5577	case RID_IS_TRIVIALLY_ASSIGNABLE:
5578	case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5579	case RID_IS_TRIVIALLY_COPYABLE:
5580	case RID_IS_UNION:
5581	case RID_IS_ASSIGNABLE:
5582	case RID_IS_CONSTRUCTIBLE:
5583	  return cp_parser_trait_expr (parser, token->keyword);
5584
5585	// C++ concepts
5586	case RID_REQUIRES:
5587	  return cp_parser_requires_expression (parser);
5588
5589	/* Objective-C++ expressions.  */
5590	case RID_AT_ENCODE:
5591	case RID_AT_PROTOCOL:
5592	case RID_AT_SELECTOR:
5593	  return cp_parser_objc_expression (parser);
5594
5595	case RID_TEMPLATE:
5596	  if (parser->in_function_body
5597	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5598	      	  == CPP_LESS))
5599	    {
5600	      error_at (token->location,
5601			"a template declaration cannot appear at block scope");
5602	      cp_parser_skip_to_end_of_block_or_statement (parser);
5603	      return error_mark_node;
5604	    }
5605	  /* FALLTHRU */
5606	default:
5607	  cp_parser_error (parser, "expected primary-expression");
5608	  return error_mark_node;
5609	}
5610
5611      /* An id-expression can start with either an identifier, a
5612	 `::' as the beginning of a qualified-id, or the "operator"
5613	 keyword.  */
5614    case CPP_NAME:
5615    case CPP_SCOPE:
5616    case CPP_TEMPLATE_ID:
5617    case CPP_NESTED_NAME_SPECIFIER:
5618      {
5619      id_expression:
5620	cp_expr id_expression;
5621	cp_expr decl;
5622	const char *error_msg;
5623	bool template_p;
5624	bool done;
5625	cp_token *id_expr_token;
5626
5627	/* Parse the id-expression.  */
5628	id_expression
5629	  = cp_parser_id_expression (parser,
5630				     /*template_keyword_p=*/false,
5631				     /*check_dependency_p=*/true,
5632				     &template_p,
5633				     /*declarator_p=*/false,
5634				     /*optional_p=*/false);
5635	if (id_expression == error_mark_node)
5636	  return error_mark_node;
5637	id_expr_token = token;
5638	token = cp_lexer_peek_token (parser->lexer);
5639	done = (token->type != CPP_OPEN_SQUARE
5640		&& token->type != CPP_OPEN_PAREN
5641		&& token->type != CPP_DOT
5642		&& token->type != CPP_DEREF
5643		&& token->type != CPP_PLUS_PLUS
5644		&& token->type != CPP_MINUS_MINUS);
5645	/* If we have a template-id, then no further lookup is
5646	   required.  If the template-id was for a template-class, we
5647	   will sometimes have a TYPE_DECL at this point.  */
5648	if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5649		 || TREE_CODE (id_expression) == TYPE_DECL)
5650	  decl = id_expression;
5651	/* Look up the name.  */
5652	else
5653	  {
5654	    tree ambiguous_decls;
5655
5656	    /* If we already know that this lookup is ambiguous, then
5657	       we've already issued an error message; there's no reason
5658	       to check again.  */
5659	    if (id_expr_token->type == CPP_NAME
5660		&& id_expr_token->error_reported)
5661	      {
5662		cp_parser_simulate_error (parser);
5663		return error_mark_node;
5664	      }
5665
5666	    decl = cp_parser_lookup_name (parser, id_expression,
5667					  none_type,
5668					  template_p,
5669					  /*is_namespace=*/false,
5670					  /*check_dependency=*/true,
5671					  &ambiguous_decls,
5672					  id_expression.get_location ());
5673	    /* If the lookup was ambiguous, an error will already have
5674	       been issued.  */
5675	    if (ambiguous_decls)
5676	      return error_mark_node;
5677
5678	    /* In Objective-C++, we may have an Objective-C 2.0
5679	       dot-syntax for classes here.  */
5680	    if (c_dialect_objc ()
5681		&& cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5682		&& TREE_CODE (decl) == TYPE_DECL
5683		&& objc_is_class_name (decl))
5684	      {
5685		tree component;
5686		cp_lexer_consume_token (parser->lexer);
5687		component = cp_parser_identifier (parser);
5688		if (component == error_mark_node)
5689		  return error_mark_node;
5690
5691		tree result = objc_build_class_component_ref (id_expression,
5692							      component);
5693		/* Build a location of the form:
5694		     expr.component
5695		     ~~~~~^~~~~~~~~
5696		   with caret at the start of the component name (at
5697		   input_location), ranging from the start of the id_expression
5698		   to the end of the component name.  */
5699		location_t combined_loc
5700		  = make_location (input_location, id_expression.get_start (),
5701				   get_finish (input_location));
5702		protected_set_expr_location (result, combined_loc);
5703		return result;
5704	      }
5705
5706	    /* In Objective-C++, an instance variable (ivar) may be preferred
5707	       to whatever cp_parser_lookup_name() found.
5708	       Call objc_lookup_ivar.  To avoid exposing cp_expr to the
5709	       rest of c-family, we have to do a little extra work to preserve
5710	       any location information in cp_expr "decl".  Given that
5711	       objc_lookup_ivar is implemented in "c-family" and "objc", we
5712	       have a trip through the pure "tree" type, rather than cp_expr.
5713	       Naively copying it back to "decl" would implicitly give the
5714	       new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5715	       store an EXPR_LOCATION.  Hence we only update "decl" (and
5716	       hence its location_t) if we get back a different tree node.  */
5717	    tree decl_tree = objc_lookup_ivar (decl.get_value (),
5718					       id_expression);
5719	    if (decl_tree != decl.get_value ())
5720	      decl = cp_expr (decl_tree);
5721
5722	    /* If name lookup gives us a SCOPE_REF, then the
5723	       qualifying scope was dependent.  */
5724	    if (TREE_CODE (decl) == SCOPE_REF)
5725	      {
5726		/* At this point, we do not know if DECL is a valid
5727		   integral constant expression.  We assume that it is
5728		   in fact such an expression, so that code like:
5729
5730		      template <int N> struct A {
5731			int a[B<N>::i];
5732		      };
5733
5734		   is accepted.  At template-instantiation time, we
5735		   will check that B<N>::i is actually a constant.  */
5736		return decl;
5737	      }
5738	    /* Check to see if DECL is a local variable in a context
5739	       where that is forbidden.  */
5740	    if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5741		&& local_variable_p (decl))
5742	      {
5743		error_at (id_expression.get_location (),
5744			  "local variable %qD may not appear in this context",
5745			  decl.get_value ());
5746		return error_mark_node;
5747	      }
5748	  }
5749
5750	decl = (finish_id_expression
5751		(id_expression, decl, parser->scope,
5752		 idk,
5753		 parser->integral_constant_expression_p,
5754		 parser->allow_non_integral_constant_expression_p,
5755		 &parser->non_integral_constant_expression_p,
5756		 template_p, done, address_p,
5757		 template_arg_p,
5758		 &error_msg,
5759		 id_expression.get_location ()));
5760	if (error_msg)
5761	  cp_parser_error (parser, error_msg);
5762	/* Build a location for an id-expression of the form:
5763	     ::ns::id
5764             ~~~~~~^~
5765	  or:
5766	     id
5767	     ^~
5768	   i.e. from the start of the first token to the end of the final
5769	   token, with the caret at the start of the unqualified-id.  */
5770	location_t caret_loc = get_pure_location (id_expression.get_location ());
5771	location_t start_loc = get_start (id_expr_token->location);
5772	location_t finish_loc = get_finish (id_expression.get_location ());
5773	location_t combined_loc
5774	  = make_location (caret_loc, start_loc, finish_loc);
5775
5776	decl.set_location (combined_loc);
5777	return decl;
5778      }
5779
5780      /* Anything else is an error.  */
5781    default:
5782      cp_parser_error (parser, "expected primary-expression");
5783      return error_mark_node;
5784    }
5785}
5786
5787static inline cp_expr
5788cp_parser_primary_expression (cp_parser *parser,
5789			      bool address_p,
5790			      bool cast_p,
5791			      bool template_arg_p,
5792			      cp_id_kind *idk)
5793{
5794  return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5795				       /*decltype*/false, idk);
5796}
5797
5798/* Parse an id-expression.
5799
5800   id-expression:
5801     unqualified-id
5802     qualified-id
5803
5804   qualified-id:
5805     :: [opt] nested-name-specifier template [opt] unqualified-id
5806     :: identifier
5807     :: operator-function-id
5808     :: template-id
5809
5810   Return a representation of the unqualified portion of the
5811   identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
5812   a `::' or nested-name-specifier.
5813
5814   Often, if the id-expression was a qualified-id, the caller will
5815   want to make a SCOPE_REF to represent the qualified-id.  This
5816   function does not do this in order to avoid wastefully creating
5817   SCOPE_REFs when they are not required.
5818
5819   If TEMPLATE_KEYWORD_P is true, then we have just seen the
5820   `template' keyword.
5821
5822   If CHECK_DEPENDENCY_P is false, then names are looked up inside
5823   uninstantiated templates.
5824
5825   If *TEMPLATE_P is non-NULL, it is set to true iff the
5826   `template' keyword is used to explicitly indicate that the entity
5827   named is a template.
5828
5829   If DECLARATOR_P is true, the id-expression is appearing as part of
5830   a declarator, rather than as part of an expression.  */
5831
5832static cp_expr
5833cp_parser_id_expression (cp_parser *parser,
5834			 bool template_keyword_p,
5835			 bool check_dependency_p,
5836			 bool *template_p,
5837			 bool declarator_p,
5838			 bool optional_p)
5839{
5840  bool global_scope_p;
5841  bool nested_name_specifier_p;
5842
5843  /* Assume the `template' keyword was not used.  */
5844  if (template_p)
5845    *template_p = template_keyword_p;
5846
5847  /* Look for the optional `::' operator.  */
5848  global_scope_p
5849    = (!template_keyword_p
5850       && (cp_parser_global_scope_opt (parser,
5851				       /*current_scope_valid_p=*/false)
5852	   != NULL_TREE));
5853
5854  /* Look for the optional nested-name-specifier.  */
5855  nested_name_specifier_p
5856    = (cp_parser_nested_name_specifier_opt (parser,
5857					    /*typename_keyword_p=*/false,
5858					    check_dependency_p,
5859					    /*type_p=*/false,
5860					    declarator_p,
5861					    template_keyword_p)
5862       != NULL_TREE);
5863
5864  /* If there is a nested-name-specifier, then we are looking at
5865     the first qualified-id production.  */
5866  if (nested_name_specifier_p)
5867    {
5868      tree saved_scope;
5869      tree saved_object_scope;
5870      tree saved_qualifying_scope;
5871      cp_expr unqualified_id;
5872      bool is_template;
5873
5874      /* See if the next token is the `template' keyword.  */
5875      if (!template_p)
5876	template_p = &is_template;
5877      *template_p = cp_parser_optional_template_keyword (parser);
5878      /* Name lookup we do during the processing of the
5879	 unqualified-id might obliterate SCOPE.  */
5880      saved_scope = parser->scope;
5881      saved_object_scope = parser->object_scope;
5882      saved_qualifying_scope = parser->qualifying_scope;
5883      /* Process the final unqualified-id.  */
5884      unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5885						 check_dependency_p,
5886						 declarator_p,
5887						 /*optional_p=*/false);
5888      /* Restore the SAVED_SCOPE for our caller.  */
5889      parser->scope = saved_scope;
5890      parser->object_scope = saved_object_scope;
5891      parser->qualifying_scope = saved_qualifying_scope;
5892
5893      return unqualified_id;
5894    }
5895  /* Otherwise, if we are in global scope, then we are looking at one
5896     of the other qualified-id productions.  */
5897  else if (global_scope_p)
5898    {
5899      cp_token *token;
5900      tree id;
5901
5902      /* Peek at the next token.  */
5903      token = cp_lexer_peek_token (parser->lexer);
5904
5905      /* If it's an identifier, and the next token is not a "<", then
5906	 we can avoid the template-id case.  This is an optimization
5907	 for this common case.  */
5908      if (token->type == CPP_NAME
5909	  && !cp_parser_nth_token_starts_template_argument_list_p
5910	       (parser, 2))
5911	return cp_parser_identifier (parser);
5912
5913      cp_parser_parse_tentatively (parser);
5914      /* Try a template-id.  */
5915      id = cp_parser_template_id_expr (parser,
5916				       /*template_keyword_p=*/false,
5917				       /*check_dependency_p=*/true,
5918				       declarator_p);
5919      /* If that worked, we're done.  */
5920      if (cp_parser_parse_definitely (parser))
5921	return id;
5922
5923      /* Peek at the next token.  (Changes in the token buffer may
5924	 have invalidated the pointer obtained above.)  */
5925      token = cp_lexer_peek_token (parser->lexer);
5926
5927      switch (token->type)
5928	{
5929	case CPP_NAME:
5930	  return cp_parser_identifier (parser);
5931
5932	case CPP_KEYWORD:
5933	  if (token->keyword == RID_OPERATOR)
5934	    return cp_parser_operator_function_id (parser);
5935	  /* Fall through.  */
5936
5937	default:
5938	  cp_parser_error (parser, "expected id-expression");
5939	  return error_mark_node;
5940	}
5941    }
5942  else
5943    return cp_parser_unqualified_id (parser, template_keyword_p,
5944				     /*check_dependency_p=*/true,
5945				     declarator_p,
5946				     optional_p);
5947}
5948
5949/* Parse an unqualified-id.
5950
5951   unqualified-id:
5952     identifier
5953     operator-function-id
5954     conversion-function-id
5955     ~ class-name
5956     template-id
5957
5958   If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5959   keyword, in a construct like `A::template ...'.
5960
5961   Returns a representation of unqualified-id.  For the `identifier'
5962   production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
5963   production a BIT_NOT_EXPR is returned; the operand of the
5964   BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
5965   other productions, see the documentation accompanying the
5966   corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
5967   names are looked up in uninstantiated templates.  If DECLARATOR_P
5968   is true, the unqualified-id is appearing as part of a declarator,
5969   rather than as part of an expression.  */
5970
5971static cp_expr
5972cp_parser_unqualified_id (cp_parser* parser,
5973			  bool template_keyword_p,
5974			  bool check_dependency_p,
5975			  bool declarator_p,
5976			  bool optional_p)
5977{
5978  cp_token *token;
5979
5980  /* Peek at the next token.  */
5981  token = cp_lexer_peek_token (parser->lexer);
5982
5983  switch ((int) token->type)
5984    {
5985    case CPP_NAME:
5986      {
5987	tree id;
5988
5989	/* We don't know yet whether or not this will be a
5990	   template-id.  */
5991	cp_parser_parse_tentatively (parser);
5992	/* Try a template-id.  */
5993	id = cp_parser_template_id_expr (parser, template_keyword_p,
5994					 check_dependency_p,
5995					 declarator_p);
5996	/* If it worked, we're done.  */
5997	if (cp_parser_parse_definitely (parser))
5998	  return id;
5999	/* Otherwise, it's an ordinary identifier.  */
6000	return cp_parser_identifier (parser);
6001      }
6002
6003    case CPP_TEMPLATE_ID:
6004      return cp_parser_template_id_expr (parser, template_keyword_p,
6005					 check_dependency_p,
6006					 declarator_p);
6007
6008    case CPP_COMPL:
6009      {
6010	tree type_decl;
6011	tree qualifying_scope;
6012	tree object_scope;
6013	tree scope;
6014	bool done;
6015	location_t tilde_loc = token->location;
6016
6017	/* Consume the `~' token.  */
6018	cp_lexer_consume_token (parser->lexer);
6019	/* Parse the class-name.  The standard, as written, seems to
6020	   say that:
6021
6022	     template <typename T> struct S { ~S (); };
6023	     template <typename T> S<T>::~S() {}
6024
6025	   is invalid, since `~' must be followed by a class-name, but
6026	   `S<T>' is dependent, and so not known to be a class.
6027	   That's not right; we need to look in uninstantiated
6028	   templates.  A further complication arises from:
6029
6030	     template <typename T> void f(T t) {
6031	       t.T::~T();
6032	     }
6033
6034	   Here, it is not possible to look up `T' in the scope of `T'
6035	   itself.  We must look in both the current scope, and the
6036	   scope of the containing complete expression.
6037
6038	   Yet another issue is:
6039
6040	     struct S {
6041	       int S;
6042	       ~S();
6043	     };
6044
6045	     S::~S() {}
6046
6047	   The standard does not seem to say that the `S' in `~S'
6048	   should refer to the type `S' and not the data member
6049	   `S::S'.  */
6050
6051	/* DR 244 says that we look up the name after the "~" in the
6052	   same scope as we looked up the qualifying name.  That idea
6053	   isn't fully worked out; it's more complicated than that.  */
6054	scope = parser->scope;
6055	object_scope = parser->object_scope;
6056	qualifying_scope = parser->qualifying_scope;
6057
6058	/* Check for invalid scopes.  */
6059	if (scope == error_mark_node)
6060	  {
6061	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6062	      cp_lexer_consume_token (parser->lexer);
6063	    return error_mark_node;
6064	  }
6065	if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6066	  {
6067	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6068	      error_at (token->location,
6069			"scope %qT before %<~%> is not a class-name",
6070			scope);
6071	    cp_parser_simulate_error (parser);
6072	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6073	      cp_lexer_consume_token (parser->lexer);
6074	    return error_mark_node;
6075	  }
6076	if (template_keyword_p)
6077	  {
6078	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6079	      error_at (tilde_loc, "%<template%> keyword not permitted in "
6080			"destructor name");
6081	    cp_parser_simulate_error (parser);
6082	    return error_mark_node;
6083	  }
6084
6085	gcc_assert (!scope || TYPE_P (scope));
6086
6087	token = cp_lexer_peek_token (parser->lexer);
6088
6089	/* Create a location with caret == start at the tilde,
6090	   finishing at the end of the peeked token, e.g:
6091	   ~token
6092	   ^~~~~~.  */
6093	location_t loc
6094	  = make_location (tilde_loc, tilde_loc, token->location);
6095
6096	/* If the name is of the form "X::~X" it's OK even if X is a
6097	   typedef.  */
6098
6099	if (scope
6100	    && token->type == CPP_NAME
6101	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6102		!= CPP_LESS)
6103	    && (token->u.value == TYPE_IDENTIFIER (scope)
6104		|| (CLASS_TYPE_P (scope)
6105		    && constructor_name_p (token->u.value, scope))))
6106	  {
6107	    cp_lexer_consume_token (parser->lexer);
6108	    return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6109	  }
6110
6111	/* ~auto means the destructor of whatever the object is.  */
6112	if (cp_parser_is_keyword (token, RID_AUTO))
6113	  {
6114	    if (cxx_dialect < cxx14)
6115	      pedwarn (loc, 0,
6116		       "%<~auto%> only available with "
6117		       "%<-std=c++14%> or %<-std=gnu++14%>");
6118	    cp_lexer_consume_token (parser->lexer);
6119	    return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6120	  }
6121
6122	/* If there was an explicit qualification (S::~T), first look
6123	   in the scope given by the qualification (i.e., S).
6124
6125	   Note: in the calls to cp_parser_class_name below we pass
6126	   typename_type so that lookup finds the injected-class-name
6127	   rather than the constructor.  */
6128	done = false;
6129	type_decl = NULL_TREE;
6130	if (scope)
6131	  {
6132	    cp_parser_parse_tentatively (parser);
6133	    type_decl = cp_parser_class_name (parser,
6134					      /*typename_keyword_p=*/false,
6135					      /*template_keyword_p=*/false,
6136					      typename_type,
6137					      /*check_dependency=*/false,
6138					      /*class_head_p=*/false,
6139					      declarator_p);
6140	    if (cp_parser_parse_definitely (parser))
6141	      done = true;
6142	  }
6143	/* In "N::S::~S", look in "N" as well.  */
6144	if (!done && scope && qualifying_scope)
6145	  {
6146	    cp_parser_parse_tentatively (parser);
6147	    parser->scope = qualifying_scope;
6148	    parser->object_scope = NULL_TREE;
6149	    parser->qualifying_scope = NULL_TREE;
6150	    type_decl
6151	      = cp_parser_class_name (parser,
6152				      /*typename_keyword_p=*/false,
6153				      /*template_keyword_p=*/false,
6154				      typename_type,
6155				      /*check_dependency=*/false,
6156				      /*class_head_p=*/false,
6157				      declarator_p);
6158	    if (cp_parser_parse_definitely (parser))
6159	      done = true;
6160	  }
6161	/* In "p->S::~T", look in the scope given by "*p" as well.  */
6162	else if (!done && object_scope)
6163	  {
6164	    cp_parser_parse_tentatively (parser);
6165	    parser->scope = object_scope;
6166	    parser->object_scope = NULL_TREE;
6167	    parser->qualifying_scope = NULL_TREE;
6168	    type_decl
6169	      = cp_parser_class_name (parser,
6170				      /*typename_keyword_p=*/false,
6171				      /*template_keyword_p=*/false,
6172				      typename_type,
6173				      /*check_dependency=*/false,
6174				      /*class_head_p=*/false,
6175				      declarator_p);
6176	    if (cp_parser_parse_definitely (parser))
6177	      done = true;
6178	  }
6179	/* Look in the surrounding context.  */
6180	if (!done)
6181	  {
6182	    parser->scope = NULL_TREE;
6183	    parser->object_scope = NULL_TREE;
6184	    parser->qualifying_scope = NULL_TREE;
6185	    if (processing_template_decl)
6186	      cp_parser_parse_tentatively (parser);
6187	    type_decl
6188	      = cp_parser_class_name (parser,
6189				      /*typename_keyword_p=*/false,
6190				      /*template_keyword_p=*/false,
6191				      typename_type,
6192				      /*check_dependency=*/false,
6193				      /*class_head_p=*/false,
6194				      declarator_p);
6195	    if (processing_template_decl
6196		&& ! cp_parser_parse_definitely (parser))
6197	      {
6198		/* We couldn't find a type with this name.  If we're parsing
6199		   tentatively, fail and try something else.  */
6200		if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6201		  {
6202		    cp_parser_simulate_error (parser);
6203		    return error_mark_node;
6204		  }
6205		/* Otherwise, accept it and check for a match at instantiation
6206		   time.  */
6207		type_decl = cp_parser_identifier (parser);
6208		if (type_decl != error_mark_node)
6209		  type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6210		return type_decl;
6211	      }
6212	  }
6213	/* If an error occurred, assume that the name of the
6214	   destructor is the same as the name of the qualifying
6215	   class.  That allows us to keep parsing after running
6216	   into ill-formed destructor names.  */
6217	if (type_decl == error_mark_node && scope)
6218	  return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6219	else if (type_decl == error_mark_node)
6220	  return error_mark_node;
6221
6222	/* Check that destructor name and scope match.  */
6223	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6224	  {
6225	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6226	      error_at (loc,
6227			"declaration of %<~%T%> as member of %qT",
6228			type_decl, scope);
6229	    cp_parser_simulate_error (parser);
6230	    return error_mark_node;
6231	  }
6232
6233	/* [class.dtor]
6234
6235	   A typedef-name that names a class shall not be used as the
6236	   identifier in the declarator for a destructor declaration.  */
6237	if (declarator_p
6238	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6239	    && !DECL_SELF_REFERENCE_P (type_decl)
6240	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6241	  error_at (loc,
6242		    "typedef-name %qD used as destructor declarator",
6243		    type_decl);
6244
6245	return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6246      }
6247
6248    case CPP_KEYWORD:
6249      if (token->keyword == RID_OPERATOR)
6250	{
6251	  cp_expr id;
6252
6253	  /* This could be a template-id, so we try that first.  */
6254	  cp_parser_parse_tentatively (parser);
6255	  /* Try a template-id.  */
6256	  id = cp_parser_template_id_expr (parser, template_keyword_p,
6257					   /*check_dependency_p=*/true,
6258					   declarator_p);
6259	  /* If that worked, we're done.  */
6260	  if (cp_parser_parse_definitely (parser))
6261	    return id;
6262	  /* We still don't know whether we're looking at an
6263	     operator-function-id or a conversion-function-id.  */
6264	  cp_parser_parse_tentatively (parser);
6265	  /* Try an operator-function-id.  */
6266	  id = cp_parser_operator_function_id (parser);
6267	  /* If that didn't work, try a conversion-function-id.  */
6268	  if (!cp_parser_parse_definitely (parser))
6269	    id = cp_parser_conversion_function_id (parser);
6270
6271	  return id;
6272	}
6273      /* Fall through.  */
6274
6275    default:
6276      if (optional_p)
6277	return NULL_TREE;
6278      cp_parser_error (parser, "expected unqualified-id");
6279      return error_mark_node;
6280    }
6281}
6282
6283/* Check [temp.names]/5: A name prefixed by the keyword template shall
6284   be a template-id or the name shall refer to a class template or an
6285   alias template.  */
6286
6287static void
6288check_template_keyword_in_nested_name_spec (tree name)
6289{
6290  if (CLASS_TYPE_P (name)
6291      && ((CLASSTYPE_USE_TEMPLATE (name)
6292	   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6293	  || CLASSTYPE_IS_TEMPLATE (name)))
6294    return;
6295
6296  if (TREE_CODE (name) == TYPENAME_TYPE
6297      && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6298    return;
6299  /* Alias templates are also OK.  */
6300  else if (alias_template_specialization_p (name, nt_opaque))
6301    return;
6302
6303  permerror (input_location, TYPE_P (name)
6304	     ? G_("%qT is not a template")
6305	     : G_("%qD is not a template"),
6306	     name);
6307}
6308
6309/* Parse an (optional) nested-name-specifier.
6310
6311   nested-name-specifier: [C++98]
6312     class-or-namespace-name :: nested-name-specifier [opt]
6313     class-or-namespace-name :: template nested-name-specifier [opt]
6314
6315   nested-name-specifier: [C++0x]
6316     type-name ::
6317     namespace-name ::
6318     nested-name-specifier identifier ::
6319     nested-name-specifier template [opt] simple-template-id ::
6320
6321   PARSER->SCOPE should be set appropriately before this function is
6322   called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6323   effect.  TYPE_P is TRUE if we non-type bindings should be ignored
6324   in name lookups.
6325
6326   Sets PARSER->SCOPE to the class (TYPE) or namespace
6327   (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6328   it unchanged if there is no nested-name-specifier.  Returns the new
6329   scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6330
6331   If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6332   part of a declaration and/or decl-specifier.  */
6333
6334static tree
6335cp_parser_nested_name_specifier_opt (cp_parser *parser,
6336				     bool typename_keyword_p,
6337				     bool check_dependency_p,
6338				     bool type_p,
6339				     bool is_declaration,
6340				     bool template_keyword_p /* = false */)
6341{
6342  bool success = false;
6343  cp_token_position start = 0;
6344  cp_token *token;
6345
6346  /* Remember where the nested-name-specifier starts.  */
6347  if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6348      && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6349    {
6350      start = cp_lexer_token_position (parser->lexer, false);
6351      push_deferring_access_checks (dk_deferred);
6352    }
6353
6354  while (true)
6355    {
6356      tree new_scope;
6357      tree old_scope;
6358      tree saved_qualifying_scope;
6359
6360      /* Spot cases that cannot be the beginning of a
6361	 nested-name-specifier.  */
6362      token = cp_lexer_peek_token (parser->lexer);
6363
6364      /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6365	 the already parsed nested-name-specifier.  */
6366      if (token->type == CPP_NESTED_NAME_SPECIFIER)
6367	{
6368	  /* Grab the nested-name-specifier and continue the loop.  */
6369	  cp_parser_pre_parsed_nested_name_specifier (parser);
6370	  /* If we originally encountered this nested-name-specifier
6371	     with IS_DECLARATION set to false, we will not have
6372	     resolved TYPENAME_TYPEs, so we must do so here.  */
6373	  if (is_declaration
6374	      && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6375	    {
6376	      new_scope = resolve_typename_type (parser->scope,
6377						 /*only_current_p=*/false);
6378	      if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6379		parser->scope = new_scope;
6380	    }
6381	  success = true;
6382	  continue;
6383	}
6384
6385      /* Spot cases that cannot be the beginning of a
6386	 nested-name-specifier.  On the second and subsequent times
6387	 through the loop, we look for the `template' keyword.  */
6388      if (success && token->keyword == RID_TEMPLATE)
6389	;
6390      /* A template-id can start a nested-name-specifier.  */
6391      else if (token->type == CPP_TEMPLATE_ID)
6392	;
6393      /* DR 743: decltype can be used in a nested-name-specifier.  */
6394      else if (token_is_decltype (token))
6395	;
6396      else
6397	{
6398	  /* If the next token is not an identifier, then it is
6399	     definitely not a type-name or namespace-name.  */
6400	  if (token->type != CPP_NAME)
6401	    break;
6402	  /* If the following token is neither a `<' (to begin a
6403	     template-id), nor a `::', then we are not looking at a
6404	     nested-name-specifier.  */
6405	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
6406
6407	  if (token->type == CPP_COLON
6408	      && parser->colon_corrects_to_scope_p
6409	      && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6410	      /* name:name is a valid sequence in an Objective C message.  */
6411	      && !parser->objective_c_message_context_p)
6412	    {
6413	      gcc_rich_location richloc (token->location);
6414	      richloc.add_fixit_replace ("::");
6415	      error_at (&richloc,
6416			"found %<:%> in nested-name-specifier, "
6417			"expected %<::%>");
6418	      token->type = CPP_SCOPE;
6419	    }
6420
6421	  if (token->type != CPP_SCOPE
6422	      && !cp_parser_nth_token_starts_template_argument_list_p
6423		  (parser, 2))
6424	    break;
6425	}
6426
6427      /* The nested-name-specifier is optional, so we parse
6428	 tentatively.  */
6429      cp_parser_parse_tentatively (parser);
6430
6431      /* Look for the optional `template' keyword, if this isn't the
6432	 first time through the loop.  */
6433      if (success)
6434	{
6435	  template_keyword_p = cp_parser_optional_template_keyword (parser);
6436	  /* DR1710: "In a qualified-id used as the name in
6437	     a typename-specifier, elaborated-type-specifier, using-declaration,
6438	     or class-or-decltype, an optional keyword template appearing at
6439	     the top level is ignored."  */
6440	  if (!template_keyword_p
6441	      && typename_keyword_p
6442	      && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6443	    template_keyword_p = true;
6444	}
6445
6446      /* Save the old scope since the name lookup we are about to do
6447	 might destroy it.  */
6448      old_scope = parser->scope;
6449      saved_qualifying_scope = parser->qualifying_scope;
6450      /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6451	 look up names in "X<T>::I" in order to determine that "Y" is
6452	 a template.  So, if we have a typename at this point, we make
6453	 an effort to look through it.  */
6454      if (is_declaration
6455	  && !typename_keyword_p
6456	  && parser->scope
6457	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6458	parser->scope = resolve_typename_type (parser->scope,
6459					       /*only_current_p=*/false);
6460      /* Parse the qualifying entity.  */
6461      new_scope
6462	= cp_parser_qualifying_entity (parser,
6463                                       typename_keyword_p,
6464                                       template_keyword_p,
6465                                       check_dependency_p,
6466                                       type_p,
6467                                       is_declaration);
6468      /* Look for the `::' token.  */
6469      cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6470
6471      /* If we found what we wanted, we keep going; otherwise, we're
6472	 done.  */
6473      if (!cp_parser_parse_definitely (parser))
6474	{
6475	  bool error_p = false;
6476
6477	  /* Restore the OLD_SCOPE since it was valid before the
6478	     failed attempt at finding the last
6479	     class-or-namespace-name.  */
6480	  parser->scope = old_scope;
6481	  parser->qualifying_scope = saved_qualifying_scope;
6482
6483	  /* If the next token is a decltype, and the one after that is a
6484	     `::', then the decltype has failed to resolve to a class or
6485	     enumeration type.  Give this error even when parsing
6486	     tentatively since it can't possibly be valid--and we're going
6487	     to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6488	     won't get another chance.*/
6489	  if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6490	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6491		  == CPP_SCOPE))
6492	    {
6493	      token = cp_lexer_consume_token (parser->lexer);
6494	      tree dtype = token->u.tree_check_value->value;
6495	      if (dtype != error_mark_node)
6496		error_at (token->location, "%<decltype%> evaluates to %qT, "
6497			  "which is not a class or enumeration type",
6498			  dtype);
6499	      parser->scope = error_mark_node;
6500	      error_p = true;
6501	      /* As below.  */
6502	      success = true;
6503	      cp_lexer_consume_token (parser->lexer);
6504	    }
6505
6506	  if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6507	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6508	    {
6509	      /* If we have a non-type template-id followed by ::, it can't
6510		 possibly be valid.  */
6511	      token = cp_lexer_peek_token (parser->lexer);
6512	      tree tid = token->u.tree_check_value->value;
6513	      if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6514		  && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6515		{
6516		  tree tmpl = NULL_TREE;
6517		  if (is_overloaded_fn (tid))
6518		    {
6519		      tree fns = get_fns (tid);
6520		      if (OVL_SINGLE_P (fns))
6521			tmpl = OVL_FIRST (fns);
6522		      if (function_concept_p (fns))
6523			error_at (token->location, "concept-id %qD "
6524				  "in nested-name-specifier", tid);
6525		      else
6526			error_at (token->location, "function template-id "
6527				  "%qD in nested-name-specifier", tid);
6528		    }
6529		  else
6530		    {
6531		      tmpl = TREE_OPERAND (tid, 0);
6532		      if (variable_concept_p (tmpl)
6533			  || standard_concept_p (tmpl))
6534			error_at (token->location, "concept-id %qD "
6535				  "in nested-name-specifier", tid);
6536		      else
6537			{
6538			  /* Variable template.  */
6539			  gcc_assert (variable_template_p (tmpl));
6540			  error_at (token->location, "variable template-id "
6541				    "%qD in nested-name-specifier", tid);
6542			}
6543		    }
6544		  if (tmpl)
6545		    inform (DECL_SOURCE_LOCATION (tmpl),
6546			    "%qD declared here", tmpl);
6547
6548		  parser->scope = error_mark_node;
6549		  error_p = true;
6550		  /* As below.  */
6551		  success = true;
6552		  cp_lexer_consume_token (parser->lexer);
6553		  cp_lexer_consume_token (parser->lexer);
6554		}
6555	    }
6556
6557	  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6558	    break;
6559	  /* If the next token is an identifier, and the one after
6560	     that is a `::', then any valid interpretation would have
6561	     found a class-or-namespace-name.  */
6562	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6563		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6564		     == CPP_SCOPE)
6565		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6566		     != CPP_COMPL))
6567	    {
6568	      token = cp_lexer_consume_token (parser->lexer);
6569	      if (!error_p)
6570		{
6571		  if (!token->error_reported)
6572		    {
6573		      tree decl;
6574		      tree ambiguous_decls;
6575
6576		      decl = cp_parser_lookup_name (parser, token->u.value,
6577						    none_type,
6578						    /*is_template=*/false,
6579						    /*is_namespace=*/false,
6580						    /*check_dependency=*/true,
6581						    &ambiguous_decls,
6582						    token->location);
6583		      if (TREE_CODE (decl) == TEMPLATE_DECL)
6584			error_at (token->location,
6585				  "%qD used without template arguments",
6586				  decl);
6587		      else if (ambiguous_decls)
6588			{
6589			  // cp_parser_lookup_name has the same diagnostic,
6590			  // thus make sure to emit it at most once.
6591			  if (cp_parser_uncommitted_to_tentative_parse_p
6592			      (parser))
6593			    {
6594			      error_at (token->location,
6595					"reference to %qD is ambiguous",
6596					token->u.value);
6597			      print_candidates (ambiguous_decls);
6598			    }
6599			  decl = error_mark_node;
6600			}
6601		      else
6602                        {
6603                          if (cxx_dialect != cxx98)
6604                            cp_parser_name_lookup_error
6605                            (parser, token->u.value, decl, NLE_NOT_CXX98,
6606	  		     token->location);
6607			  else
6608			    cp_parser_name_lookup_error
6609			    (parser, token->u.value, decl, NLE_CXX98,
6610			     token->location);
6611                        }
6612		    }
6613		  parser->scope = error_mark_node;
6614		  error_p = true;
6615		  /* Treat this as a successful nested-name-specifier
6616		     due to:
6617
6618		     [basic.lookup.qual]
6619
6620		     If the name found is not a class-name (clause
6621		     _class_) or namespace-name (_namespace.def_), the
6622		     program is ill-formed.  */
6623		  success = true;
6624		}
6625	      cp_lexer_consume_token (parser->lexer);
6626	    }
6627	  break;
6628	}
6629      /* We've found one valid nested-name-specifier.  */
6630      success = true;
6631      /* Name lookup always gives us a DECL.  */
6632      if (TREE_CODE (new_scope) == TYPE_DECL)
6633	new_scope = TREE_TYPE (new_scope);
6634      /* Uses of "template" must be followed by actual templates.  */
6635      if (template_keyword_p)
6636	check_template_keyword_in_nested_name_spec (new_scope);
6637      /* If it is a class scope, try to complete it; we are about to
6638	 be looking up names inside the class.  */
6639      if (TYPE_P (new_scope)
6640	  /* Since checking types for dependency can be expensive,
6641	     avoid doing it if the type is already complete.  */
6642	  && !COMPLETE_TYPE_P (new_scope)
6643	  /* Do not try to complete dependent types.  */
6644	  && !dependent_type_p (new_scope))
6645	{
6646	  new_scope = complete_type (new_scope);
6647	  /* If it is a typedef to current class, use the current
6648	     class instead, as the typedef won't have any names inside
6649	     it yet.  */
6650	  if (!COMPLETE_TYPE_P (new_scope)
6651	      && currently_open_class (new_scope))
6652	    new_scope = TYPE_MAIN_VARIANT (new_scope);
6653	}
6654      /* Make sure we look in the right scope the next time through
6655	 the loop.  */
6656      parser->scope = new_scope;
6657    }
6658
6659  /* If parsing tentatively, replace the sequence of tokens that makes
6660     up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6661     token.  That way, should we re-parse the token stream, we will
6662     not have to repeat the effort required to do the parse, nor will
6663     we issue duplicate error messages.  */
6664  if (success && start)
6665    {
6666      cp_token *token;
6667
6668      token = cp_lexer_token_at (parser->lexer, start);
6669      /* Reset the contents of the START token.  */
6670      token->type = CPP_NESTED_NAME_SPECIFIER;
6671      /* Retrieve any deferred checks.  Do not pop this access checks yet
6672	 so the memory will not be reclaimed during token replacing below.  */
6673      token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6674      token->tree_check_p = true;
6675      token->u.tree_check_value->value = parser->scope;
6676      token->u.tree_check_value->checks = get_deferred_access_checks ();
6677      token->u.tree_check_value->qualifying_scope =
6678	parser->qualifying_scope;
6679      token->keyword = RID_MAX;
6680
6681      /* Purge all subsequent tokens.  */
6682      cp_lexer_purge_tokens_after (parser->lexer, start);
6683    }
6684
6685  if (start)
6686    pop_to_parent_deferring_access_checks ();
6687
6688  return success ? parser->scope : NULL_TREE;
6689}
6690
6691/* Parse a nested-name-specifier.  See
6692   cp_parser_nested_name_specifier_opt for details.  This function
6693   behaves identically, except that it will an issue an error if no
6694   nested-name-specifier is present.  */
6695
6696static tree
6697cp_parser_nested_name_specifier (cp_parser *parser,
6698				 bool typename_keyword_p,
6699				 bool check_dependency_p,
6700				 bool type_p,
6701				 bool is_declaration)
6702{
6703  tree scope;
6704
6705  /* Look for the nested-name-specifier.  */
6706  scope = cp_parser_nested_name_specifier_opt (parser,
6707					       typename_keyword_p,
6708					       check_dependency_p,
6709					       type_p,
6710					       is_declaration);
6711  /* If it was not present, issue an error message.  */
6712  if (!scope)
6713    {
6714      cp_parser_error (parser, "expected nested-name-specifier");
6715      parser->scope = NULL_TREE;
6716    }
6717
6718  return scope;
6719}
6720
6721/* Parse the qualifying entity in a nested-name-specifier. For C++98,
6722   this is either a class-name or a namespace-name (which corresponds
6723   to the class-or-namespace-name production in the grammar). For
6724   C++0x, it can also be a type-name that refers to an enumeration
6725   type or a simple-template-id.
6726
6727   TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6728   TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6729   CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6730   TYPE_P is TRUE iff the next name should be taken as a class-name,
6731   even the same name is declared to be another entity in the same
6732   scope.
6733
6734   Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6735   specified by the class-or-namespace-name.  If neither is found the
6736   ERROR_MARK_NODE is returned.  */
6737
6738static tree
6739cp_parser_qualifying_entity (cp_parser *parser,
6740			     bool typename_keyword_p,
6741			     bool template_keyword_p,
6742			     bool check_dependency_p,
6743			     bool type_p,
6744			     bool is_declaration)
6745{
6746  tree saved_scope;
6747  tree saved_qualifying_scope;
6748  tree saved_object_scope;
6749  tree scope;
6750  bool only_class_p;
6751  bool successful_parse_p;
6752
6753  /* DR 743: decltype can appear in a nested-name-specifier.  */
6754  if (cp_lexer_next_token_is_decltype (parser->lexer))
6755    {
6756      scope = cp_parser_decltype (parser);
6757      if (TREE_CODE (scope) != ENUMERAL_TYPE
6758	  && !MAYBE_CLASS_TYPE_P (scope))
6759	{
6760	  cp_parser_simulate_error (parser);
6761	  return error_mark_node;
6762	}
6763      if (TYPE_NAME (scope))
6764	scope = TYPE_NAME (scope);
6765      return scope;
6766    }
6767
6768  /* Before we try to parse the class-name, we must save away the
6769     current PARSER->SCOPE since cp_parser_class_name will destroy
6770     it.  */
6771  saved_scope = parser->scope;
6772  saved_qualifying_scope = parser->qualifying_scope;
6773  saved_object_scope = parser->object_scope;
6774  /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
6775     there is no need to look for a namespace-name.  */
6776  only_class_p = template_keyword_p
6777    || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6778  if (!only_class_p)
6779    cp_parser_parse_tentatively (parser);
6780  scope = cp_parser_class_name (parser,
6781				typename_keyword_p,
6782				template_keyword_p,
6783				type_p ? class_type : none_type,
6784				check_dependency_p,
6785				/*class_head_p=*/false,
6786				is_declaration,
6787				/*enum_ok=*/cxx_dialect > cxx98);
6788  successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6789  /* If that didn't work, try for a namespace-name.  */
6790  if (!only_class_p && !successful_parse_p)
6791    {
6792      /* Restore the saved scope.  */
6793      parser->scope = saved_scope;
6794      parser->qualifying_scope = saved_qualifying_scope;
6795      parser->object_scope = saved_object_scope;
6796      /* If we are not looking at an identifier followed by the scope
6797	 resolution operator, then this is not part of a
6798	 nested-name-specifier.  (Note that this function is only used
6799	 to parse the components of a nested-name-specifier.)  */
6800      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6801	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6802	return error_mark_node;
6803      scope = cp_parser_namespace_name (parser);
6804    }
6805
6806  return scope;
6807}
6808
6809/* Return true if we are looking at a compound-literal, false otherwise.  */
6810
6811static bool
6812cp_parser_compound_literal_p (cp_parser *parser)
6813{
6814  cp_lexer_save_tokens (parser->lexer);
6815
6816  /* Skip tokens until the next token is a closing parenthesis.
6817     If we find the closing `)', and the next token is a `{', then
6818     we are looking at a compound-literal.  */
6819  bool compound_literal_p
6820    = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6821					      /*consume_paren=*/true)
6822       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6823
6824  /* Roll back the tokens we skipped.  */
6825  cp_lexer_rollback_tokens (parser->lexer);
6826
6827  return compound_literal_p;
6828}
6829
6830/* Return true if EXPR is the integer constant zero or a complex constant
6831   of zero, without any folding, but ignoring location wrappers.  */
6832
6833bool
6834literal_integer_zerop (const_tree expr)
6835{
6836  return (location_wrapper_p (expr)
6837	  && integer_zerop (TREE_OPERAND (expr, 0)));
6838}
6839
6840/* Parse a postfix-expression.
6841
6842   postfix-expression:
6843     primary-expression
6844     postfix-expression [ expression ]
6845     postfix-expression ( expression-list [opt] )
6846     simple-type-specifier ( expression-list [opt] )
6847     typename :: [opt] nested-name-specifier identifier
6848       ( expression-list [opt] )
6849     typename :: [opt] nested-name-specifier template [opt] template-id
6850       ( expression-list [opt] )
6851     postfix-expression . template [opt] id-expression
6852     postfix-expression -> template [opt] id-expression
6853     postfix-expression . pseudo-destructor-name
6854     postfix-expression -> pseudo-destructor-name
6855     postfix-expression ++
6856     postfix-expression --
6857     dynamic_cast < type-id > ( expression )
6858     static_cast < type-id > ( expression )
6859     reinterpret_cast < type-id > ( expression )
6860     const_cast < type-id > ( expression )
6861     typeid ( expression )
6862     typeid ( type-id )
6863
6864   GNU Extension:
6865
6866   postfix-expression:
6867     ( type-id ) { initializer-list , [opt] }
6868
6869   This extension is a GNU version of the C99 compound-literal
6870   construct.  (The C99 grammar uses `type-name' instead of `type-id',
6871   but they are essentially the same concept.)
6872
6873   If ADDRESS_P is true, the postfix expression is the operand of the
6874   `&' operator.  CAST_P is true if this expression is the target of a
6875   cast.
6876
6877   If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6878   class member access expressions [expr.ref].
6879
6880   Returns a representation of the expression.  */
6881
6882static cp_expr
6883cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6884                              bool member_access_only_p, bool decltype_p,
6885			      cp_id_kind * pidk_return)
6886{
6887  cp_token *token;
6888  location_t loc;
6889  enum rid keyword;
6890  cp_id_kind idk = CP_ID_KIND_NONE;
6891  cp_expr postfix_expression = NULL_TREE;
6892  bool is_member_access = false;
6893
6894  /* Peek at the next token.  */
6895  token = cp_lexer_peek_token (parser->lexer);
6896  loc = token->location;
6897  location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6898
6899  /* Some of the productions are determined by keywords.  */
6900  keyword = token->keyword;
6901  switch (keyword)
6902    {
6903    case RID_DYNCAST:
6904    case RID_STATCAST:
6905    case RID_REINTCAST:
6906    case RID_CONSTCAST:
6907      {
6908	tree type;
6909	cp_expr expression;
6910	const char *saved_message;
6911	bool saved_in_type_id_in_expr_p;
6912
6913	/* All of these can be handled in the same way from the point
6914	   of view of parsing.  Begin by consuming the token
6915	   identifying the cast.  */
6916	cp_lexer_consume_token (parser->lexer);
6917
6918	/* New types cannot be defined in the cast.  */
6919	saved_message = parser->type_definition_forbidden_message;
6920	parser->type_definition_forbidden_message
6921	  = G_("types may not be defined in casts");
6922
6923	/* Look for the opening `<'.  */
6924	cp_parser_require (parser, CPP_LESS, RT_LESS);
6925	/* Parse the type to which we are casting.  */
6926	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6927	parser->in_type_id_in_expr_p = true;
6928	type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6929				  NULL);
6930	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6931	/* Look for the closing `>'.  */
6932	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6933	/* Restore the old message.  */
6934	parser->type_definition_forbidden_message = saved_message;
6935
6936	bool saved_greater_than_is_operator_p
6937	  = parser->greater_than_is_operator_p;
6938	parser->greater_than_is_operator_p = true;
6939
6940	/* And the expression which is being cast.  */
6941	matching_parens parens;
6942	parens.require_open (parser);
6943	expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6944	cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6945						   RT_CLOSE_PAREN);
6946	location_t end_loc = close_paren ?
6947	  close_paren->location : UNKNOWN_LOCATION;
6948
6949	parser->greater_than_is_operator_p
6950	  = saved_greater_than_is_operator_p;
6951
6952	/* Only type conversions to integral or enumeration types
6953	   can be used in constant-expressions.  */
6954	if (!cast_valid_in_integral_constant_expression_p (type)
6955	    && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6956	  {
6957	    postfix_expression = error_mark_node;
6958	    break;
6959	  }
6960
6961	/* Construct a location e.g. :
6962	     reinterpret_cast <int *> (expr)
6963	     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6964	   ranging from the start of the "*_cast" token to the final closing
6965	   paren, with the caret at the start.  */
6966	location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6967
6968	switch (keyword)
6969	  {
6970	  case RID_DYNCAST:
6971	    postfix_expression
6972	      = build_dynamic_cast (cp_cast_loc, type, expression,
6973				    tf_warning_or_error);
6974	    break;
6975	  case RID_STATCAST:
6976	    postfix_expression
6977	      = build_static_cast (cp_cast_loc, type, expression,
6978				   tf_warning_or_error);
6979	    break;
6980	  case RID_REINTCAST:
6981	    postfix_expression
6982	      = build_reinterpret_cast (cp_cast_loc, type, expression,
6983                                        tf_warning_or_error);
6984	    break;
6985	  case RID_CONSTCAST:
6986	    postfix_expression
6987	      = build_const_cast (cp_cast_loc, type, expression,
6988				  tf_warning_or_error);
6989	    break;
6990	  default:
6991	    gcc_unreachable ();
6992	  }
6993      }
6994      break;
6995
6996    case RID_TYPEID:
6997      {
6998	tree type;
6999	const char *saved_message;
7000	bool saved_in_type_id_in_expr_p;
7001
7002	/* Consume the `typeid' token.  */
7003	cp_lexer_consume_token (parser->lexer);
7004	/* Look for the `(' token.  */
7005	matching_parens parens;
7006	parens.require_open (parser);
7007	/* Types cannot be defined in a `typeid' expression.  */
7008	saved_message = parser->type_definition_forbidden_message;
7009	parser->type_definition_forbidden_message
7010	  = G_("types may not be defined in a %<typeid%> expression");
7011	/* We can't be sure yet whether we're looking at a type-id or an
7012	   expression.  */
7013	cp_parser_parse_tentatively (parser);
7014	/* Try a type-id first.  */
7015	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7016	parser->in_type_id_in_expr_p = true;
7017	type = cp_parser_type_id (parser);
7018	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7019	/* Look for the `)' token.  Otherwise, we can't be sure that
7020	   we're not looking at an expression: consider `typeid (int
7021	   (3))', for example.  */
7022	cp_token *close_paren = parens.require_close (parser);
7023	/* If all went well, simply lookup the type-id.  */
7024	if (cp_parser_parse_definitely (parser))
7025	  postfix_expression = get_typeid (type, tf_warning_or_error);
7026	/* Otherwise, fall back to the expression variant.  */
7027	else
7028	  {
7029	    tree expression;
7030
7031	    /* Look for an expression.  */
7032	    expression = cp_parser_expression (parser, & idk);
7033	    /* Compute its typeid.  */
7034	    postfix_expression = build_typeid (expression, tf_warning_or_error);
7035	    /* Look for the `)' token.  */
7036	    close_paren = parens.require_close (parser);
7037	  }
7038	/* Restore the saved message.  */
7039	parser->type_definition_forbidden_message = saved_message;
7040	/* `typeid' may not appear in an integral constant expression.  */
7041	if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7042	  postfix_expression = error_mark_node;
7043
7044	/* Construct a location e.g. :
7045	     typeid (expr)
7046	     ^~~~~~~~~~~~~
7047	   ranging from the start of the "typeid" token to the final closing
7048	   paren, with the caret at the start.  */
7049	if (close_paren)
7050	  {
7051	    location_t typeid_loc
7052	      = make_location (start_loc, start_loc, close_paren->location);
7053	    postfix_expression.set_location (typeid_loc);
7054	    postfix_expression.maybe_add_location_wrapper ();
7055	  }
7056      }
7057      break;
7058
7059    case RID_TYPENAME:
7060      {
7061	tree type;
7062	/* The syntax permitted here is the same permitted for an
7063	   elaborated-type-specifier.  */
7064        ++parser->prevent_constrained_type_specifiers;
7065	type = cp_parser_elaborated_type_specifier (parser,
7066						    /*is_friend=*/false,
7067						    /*is_declaration=*/false);
7068        --parser->prevent_constrained_type_specifiers;
7069	postfix_expression = cp_parser_functional_cast (parser, type);
7070      }
7071      break;
7072
7073    case RID_ADDRESSOF:
7074    case RID_BUILTIN_SHUFFLE:
7075    case RID_BUILTIN_LAUNDER:
7076      {
7077	vec<tree, va_gc> *vec;
7078	unsigned int i;
7079	tree p;
7080
7081	cp_lexer_consume_token (parser->lexer);
7082	vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7083		    /*cast_p=*/false, /*allow_expansion_p=*/true,
7084		    /*non_constant_p=*/NULL);
7085	if (vec == NULL)
7086	  {
7087	    postfix_expression = error_mark_node;
7088	    break;
7089	  }
7090
7091	FOR_EACH_VEC_ELT (*vec, i, p)
7092	  mark_exp_read (p);
7093
7094	switch (keyword)
7095	  {
7096	  case RID_ADDRESSOF:
7097	    if (vec->length () == 1)
7098	      postfix_expression
7099		= cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7100	    else
7101	      {
7102		error_at (loc, "wrong number of arguments to "
7103			       "%<__builtin_addressof%>");
7104		postfix_expression = error_mark_node;
7105	      }
7106	    break;
7107
7108	  case RID_BUILTIN_LAUNDER:
7109	    if (vec->length () == 1)
7110	      postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7111							   tf_warning_or_error);
7112	    else
7113	      {
7114		error_at (loc, "wrong number of arguments to "
7115			       "%<__builtin_launder%>");
7116		postfix_expression = error_mark_node;
7117	      }
7118	    break;
7119
7120	  case RID_BUILTIN_SHUFFLE:
7121	    if (vec->length () == 2)
7122	      postfix_expression
7123		= build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7124					 (*vec)[1], tf_warning_or_error);
7125	    else if (vec->length () == 3)
7126	      postfix_expression
7127		= build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7128					 (*vec)[2], tf_warning_or_error);
7129	    else
7130	      {
7131		error_at (loc, "wrong number of arguments to "
7132			       "%<__builtin_shuffle%>");
7133		postfix_expression = error_mark_node;
7134	      }
7135	    break;
7136
7137	  default:
7138	    gcc_unreachable ();
7139	  }
7140	break;
7141      }
7142
7143    case RID_BUILTIN_CONVERTVECTOR:
7144      {
7145	tree expression;
7146	tree type;
7147	/* Consume the `__builtin_convertvector' token.  */
7148	cp_lexer_consume_token (parser->lexer);
7149	/* Look for the opening `('.  */
7150	matching_parens parens;
7151	parens.require_open (parser);
7152	/* Now, parse the assignment-expression.  */
7153	expression = cp_parser_assignment_expression (parser);
7154	/* Look for the `,'.  */
7155	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7156	location_t type_location
7157	  = cp_lexer_peek_token (parser->lexer)->location;
7158	/* Parse the type-id.  */
7159	{
7160	  type_id_in_expr_sentinel s (parser);
7161	  type = cp_parser_type_id (parser);
7162	}
7163	/* Look for the closing `)'.  */
7164	parens.require_close (parser);
7165	postfix_expression
7166	  = cp_build_vec_convert (expression, type_location, type,
7167				  tf_warning_or_error);
7168	break;
7169      }
7170
7171    default:
7172      {
7173	tree type;
7174
7175	/* If the next thing is a simple-type-specifier, we may be
7176	   looking at a functional cast.  We could also be looking at
7177	   an id-expression.  So, we try the functional cast, and if
7178	   that doesn't work we fall back to the primary-expression.  */
7179	cp_parser_parse_tentatively (parser);
7180	/* Look for the simple-type-specifier.  */
7181        ++parser->prevent_constrained_type_specifiers;
7182	type = cp_parser_simple_type_specifier (parser,
7183						/*decl_specs=*/NULL,
7184						CP_PARSER_FLAGS_NONE);
7185        --parser->prevent_constrained_type_specifiers;
7186	/* Parse the cast itself.  */
7187	if (!cp_parser_error_occurred (parser))
7188	  postfix_expression
7189	    = cp_parser_functional_cast (parser, type);
7190	/* If that worked, we're done.  */
7191	if (cp_parser_parse_definitely (parser))
7192	  break;
7193
7194	/* If the functional-cast didn't work out, try a
7195	   compound-literal.  */
7196	if (cp_parser_allow_gnu_extensions_p (parser)
7197	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7198	  {
7199	    cp_expr initializer = NULL_TREE;
7200
7201	    cp_parser_parse_tentatively (parser);
7202
7203	    matching_parens parens;
7204	    parens.consume_open (parser);
7205
7206	    /* Avoid calling cp_parser_type_id pointlessly, see comment
7207	       in cp_parser_cast_expression about c++/29234.  */
7208	    if (!cp_parser_compound_literal_p (parser))
7209	      cp_parser_simulate_error (parser);
7210	    else
7211	      {
7212		/* Parse the type.  */
7213		bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7214		parser->in_type_id_in_expr_p = true;
7215		type = cp_parser_type_id (parser);
7216		parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7217		parens.require_close (parser);
7218	      }
7219
7220	    /* If things aren't going well, there's no need to
7221	       keep going.  */
7222	    if (!cp_parser_error_occurred (parser))
7223	      {
7224		bool non_constant_p;
7225		/* Parse the brace-enclosed initializer list.  */
7226		initializer = cp_parser_braced_list (parser,
7227						     &non_constant_p);
7228	      }
7229	    /* If that worked, we're definitely looking at a
7230	       compound-literal expression.  */
7231	    if (cp_parser_parse_definitely (parser))
7232	      {
7233		/* Warn the user that a compound literal is not
7234		   allowed in standard C++.  */
7235		pedwarn (input_location, OPT_Wpedantic,
7236			 "ISO C++ forbids compound-literals");
7237		/* For simplicity, we disallow compound literals in
7238		   constant-expressions.  We could
7239		   allow compound literals of integer type, whose
7240		   initializer was a constant, in constant
7241		   expressions.  Permitting that usage, as a further
7242		   extension, would not change the meaning of any
7243		   currently accepted programs.  (Of course, as
7244		   compound literals are not part of ISO C++, the
7245		   standard has nothing to say.)  */
7246		if (cp_parser_non_integral_constant_expression (parser,
7247								NIC_NCC))
7248		  {
7249		    postfix_expression = error_mark_node;
7250		    break;
7251		  }
7252		/* Form the representation of the compound-literal.  */
7253		postfix_expression
7254		  = finish_compound_literal (type, initializer,
7255					     tf_warning_or_error, fcl_c99);
7256		postfix_expression.set_location (initializer.get_location ());
7257		break;
7258	      }
7259	  }
7260
7261	/* It must be a primary-expression.  */
7262	postfix_expression
7263	  = cp_parser_primary_expression (parser, address_p, cast_p,
7264					  /*template_arg_p=*/false,
7265					  decltype_p,
7266					  &idk);
7267      }
7268      break;
7269    }
7270
7271  /* Note that we don't need to worry about calling build_cplus_new on a
7272     class-valued CALL_EXPR in decltype when it isn't the end of the
7273     postfix-expression; unary_complex_lvalue will take care of that for
7274     all these cases.  */
7275
7276  /* Keep looping until the postfix-expression is complete.  */
7277  while (true)
7278    {
7279      if (idk == CP_ID_KIND_UNQUALIFIED
7280	  && identifier_p (postfix_expression)
7281	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7282	/* It is not a Koenig lookup function call.  */
7283	postfix_expression
7284	  = unqualified_name_lookup_error (postfix_expression);
7285
7286      /* Peek at the next token.  */
7287      token = cp_lexer_peek_token (parser->lexer);
7288
7289      switch (token->type)
7290	{
7291	case CPP_OPEN_SQUARE:
7292	  if (cp_next_tokens_can_be_std_attribute_p (parser))
7293	    {
7294	      cp_parser_error (parser,
7295			       "two consecutive %<[%> shall "
7296			       "only introduce an attribute");
7297	      return error_mark_node;
7298	    }
7299	  postfix_expression
7300	    = cp_parser_postfix_open_square_expression (parser,
7301							postfix_expression,
7302							false,
7303							decltype_p);
7304	  postfix_expression.set_range (start_loc,
7305					postfix_expression.get_location ());
7306
7307	  idk = CP_ID_KIND_NONE;
7308          is_member_access = false;
7309	  break;
7310
7311	case CPP_OPEN_PAREN:
7312	  /* postfix-expression ( expression-list [opt] ) */
7313	  {
7314	    bool koenig_p;
7315	    bool is_builtin_constant_p;
7316	    bool saved_integral_constant_expression_p = false;
7317	    bool saved_non_integral_constant_expression_p = false;
7318	    tsubst_flags_t complain = complain_flags (decltype_p);
7319	    vec<tree, va_gc> *args;
7320	    location_t close_paren_loc = UNKNOWN_LOCATION;
7321
7322            is_member_access = false;
7323
7324	    tree stripped_expression
7325	      = tree_strip_any_location_wrapper (postfix_expression);
7326	    is_builtin_constant_p
7327	      = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7328	    if (is_builtin_constant_p)
7329	      {
7330		/* The whole point of __builtin_constant_p is to allow
7331		   non-constant expressions to appear as arguments.  */
7332		saved_integral_constant_expression_p
7333		  = parser->integral_constant_expression_p;
7334		saved_non_integral_constant_expression_p
7335		  = parser->non_integral_constant_expression_p;
7336		parser->integral_constant_expression_p = false;
7337	      }
7338	    args = (cp_parser_parenthesized_expression_list
7339		    (parser, non_attr,
7340		     /*cast_p=*/false, /*allow_expansion_p=*/true,
7341		     /*non_constant_p=*/NULL,
7342		     /*close_paren_loc=*/&close_paren_loc,
7343		     /*wrap_locations_p=*/true));
7344	    if (is_builtin_constant_p)
7345	      {
7346		parser->integral_constant_expression_p
7347		  = saved_integral_constant_expression_p;
7348		parser->non_integral_constant_expression_p
7349		  = saved_non_integral_constant_expression_p;
7350	      }
7351
7352	    if (args == NULL)
7353	      {
7354		postfix_expression = error_mark_node;
7355		break;
7356	      }
7357
7358	    /* Function calls are not permitted in
7359	       constant-expressions.  */
7360	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
7361		&& cp_parser_non_integral_constant_expression (parser,
7362							       NIC_FUNC_CALL))
7363	      {
7364		postfix_expression = error_mark_node;
7365		release_tree_vector (args);
7366		break;
7367	      }
7368
7369	    koenig_p = false;
7370	    if (idk == CP_ID_KIND_UNQUALIFIED
7371		|| idk == CP_ID_KIND_TEMPLATE_ID)
7372	      {
7373		if (identifier_p (postfix_expression)
7374		    /* In C++2A, we may need to perform ADL for a template
7375		       name.  */
7376		    || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7377			&& identifier_p (TREE_OPERAND (postfix_expression, 0))))
7378		  {
7379		    if (!args->is_empty ())
7380		      {
7381			koenig_p = true;
7382			if (!any_type_dependent_arguments_p (args))
7383			  postfix_expression
7384			    = perform_koenig_lookup (postfix_expression, args,
7385						     complain);
7386		      }
7387		    else
7388		      postfix_expression
7389			= unqualified_fn_lookup_error (postfix_expression);
7390		  }
7391		/* We do not perform argument-dependent lookup if
7392		   normal lookup finds a non-function, in accordance
7393		   with the expected resolution of DR 218.  */
7394		else if (!args->is_empty ()
7395			 && is_overloaded_fn (postfix_expression))
7396		  {
7397		    /* We only need to look at the first function,
7398		       because all the fns share the attribute we're
7399		       concerned with (all member fns or all local
7400		       fns).  */
7401		    tree fn = get_first_fn (postfix_expression);
7402		    fn = STRIP_TEMPLATE (fn);
7403
7404		    /* Do not do argument dependent lookup if regular
7405		       lookup finds a member function or a block-scope
7406		       function declaration.  [basic.lookup.argdep]/3  */
7407		    if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7408			  || DECL_FUNCTION_MEMBER_P (fn)
7409			  || DECL_LOCAL_FUNCTION_P (fn)))
7410		      {
7411			koenig_p = true;
7412			if (!any_type_dependent_arguments_p (args))
7413			  postfix_expression
7414			    = perform_koenig_lookup (postfix_expression, args,
7415						     complain);
7416		      }
7417		  }
7418	      }
7419
7420	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7421	      {
7422		tree instance = TREE_OPERAND (postfix_expression, 0);
7423		tree fn = TREE_OPERAND (postfix_expression, 1);
7424
7425		if (processing_template_decl
7426		    && (type_dependent_object_expression_p (instance)
7427			|| (!BASELINK_P (fn)
7428			    && TREE_CODE (fn) != FIELD_DECL)
7429			|| type_dependent_expression_p (fn)
7430			|| any_type_dependent_arguments_p (args)))
7431		  {
7432		    maybe_generic_this_capture (instance, fn);
7433		    postfix_expression
7434		      = build_min_nt_call_vec (postfix_expression, args);
7435		  }
7436		else if (BASELINK_P (fn))
7437		  {
7438		  postfix_expression
7439		    = (build_new_method_call
7440		       (instance, fn, &args, NULL_TREE,
7441			(idk == CP_ID_KIND_QUALIFIED
7442			 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7443			 : LOOKUP_NORMAL),
7444			/*fn_p=*/NULL,
7445			complain));
7446		  }
7447		else
7448		  postfix_expression
7449		    = finish_call_expr (postfix_expression, &args,
7450					/*disallow_virtual=*/false,
7451					/*koenig_p=*/false,
7452					complain);
7453	      }
7454	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
7455		     || TREE_CODE (postfix_expression) == MEMBER_REF
7456		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7457	      postfix_expression = (build_offset_ref_call_from_tree
7458				    (postfix_expression, &args,
7459				     complain));
7460	    else if (idk == CP_ID_KIND_QUALIFIED)
7461	      /* A call to a static class member, or a namespace-scope
7462		 function.  */
7463	      postfix_expression
7464		= finish_call_expr (postfix_expression, &args,
7465				    /*disallow_virtual=*/true,
7466				    koenig_p,
7467				    complain);
7468	    else
7469	      /* All other function calls.  */
7470	      postfix_expression
7471		= finish_call_expr (postfix_expression, &args,
7472				    /*disallow_virtual=*/false,
7473				    koenig_p,
7474				    complain);
7475
7476	    if (close_paren_loc != UNKNOWN_LOCATION)
7477	      {
7478		location_t combined_loc = make_location (token->location,
7479							 start_loc,
7480							 close_paren_loc);
7481		postfix_expression.set_location (combined_loc);
7482	      }
7483
7484	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
7485	    idk = CP_ID_KIND_NONE;
7486
7487	    release_tree_vector (args);
7488	  }
7489	  break;
7490
7491	case CPP_DOT:
7492	case CPP_DEREF:
7493	  /* postfix-expression . template [opt] id-expression
7494	     postfix-expression . pseudo-destructor-name
7495	     postfix-expression -> template [opt] id-expression
7496	     postfix-expression -> pseudo-destructor-name */
7497
7498	  /* Consume the `.' or `->' operator.  */
7499	  cp_lexer_consume_token (parser->lexer);
7500
7501	  postfix_expression
7502	    = cp_parser_postfix_dot_deref_expression (parser, token->type,
7503						      postfix_expression,
7504						      false, &idk, loc);
7505
7506          is_member_access = true;
7507	  break;
7508
7509	case CPP_PLUS_PLUS:
7510	  /* postfix-expression ++  */
7511	  /* Consume the `++' token.  */
7512	  cp_lexer_consume_token (parser->lexer);
7513	  /* Generate a representation for the complete expression.  */
7514	  postfix_expression
7515	    = finish_increment_expr (postfix_expression,
7516				     POSTINCREMENT_EXPR);
7517	  /* Increments may not appear in constant-expressions.  */
7518	  if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7519	    postfix_expression = error_mark_node;
7520	  idk = CP_ID_KIND_NONE;
7521          is_member_access = false;
7522	  break;
7523
7524	case CPP_MINUS_MINUS:
7525	  /* postfix-expression -- */
7526	  /* Consume the `--' token.  */
7527	  cp_lexer_consume_token (parser->lexer);
7528	  /* Generate a representation for the complete expression.  */
7529	  postfix_expression
7530	    = finish_increment_expr (postfix_expression,
7531				     POSTDECREMENT_EXPR);
7532	  /* Decrements may not appear in constant-expressions.  */
7533	  if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7534	    postfix_expression = error_mark_node;
7535	  idk = CP_ID_KIND_NONE;
7536          is_member_access = false;
7537	  break;
7538
7539	default:
7540	  if (pidk_return != NULL)
7541	    * pidk_return = idk;
7542          if (member_access_only_p)
7543            return is_member_access
7544              ? postfix_expression
7545              : cp_expr (error_mark_node);
7546          else
7547            return postfix_expression;
7548	}
7549    }
7550
7551  /* We should never get here.  */
7552  gcc_unreachable ();
7553  return error_mark_node;
7554}
7555
7556/* A subroutine of cp_parser_postfix_expression that also gets hijacked
7557   by cp_parser_builtin_offsetof.  We're looking for
7558
7559     postfix-expression [ expression ]
7560     postfix-expression [ braced-init-list ] (C++11)
7561
7562   FOR_OFFSETOF is set if we're being called in that context, which
7563   changes how we deal with integer constant expressions.  */
7564
7565static tree
7566cp_parser_postfix_open_square_expression (cp_parser *parser,
7567					  tree postfix_expression,
7568					  bool for_offsetof,
7569					  bool decltype_p)
7570{
7571  tree index = NULL_TREE;
7572  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7573  bool saved_greater_than_is_operator_p;
7574
7575  /* Consume the `[' token.  */
7576  cp_lexer_consume_token (parser->lexer);
7577
7578  saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7579  parser->greater_than_is_operator_p = true;
7580
7581  /* Parse the index expression.  */
7582  /* ??? For offsetof, there is a question of what to allow here.  If
7583     offsetof is not being used in an integral constant expression context,
7584     then we *could* get the right answer by computing the value at runtime.
7585     If we are in an integral constant expression context, then we might
7586     could accept any constant expression; hard to say without analysis.
7587     Rather than open the barn door too wide right away, allow only integer
7588     constant expressions here.  */
7589  if (for_offsetof)
7590    index = cp_parser_constant_expression (parser);
7591  else
7592    {
7593      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7594	{
7595	  bool expr_nonconst_p;
7596	  cp_lexer_set_source_position (parser->lexer);
7597	  maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7598	  index = cp_parser_braced_list (parser, &expr_nonconst_p);
7599	}
7600      else
7601	index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
7602				      /*decltype_p=*/false,
7603				      /*warn_comma_p=*/warn_comma_subscript);
7604    }
7605
7606  parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7607
7608  /* Look for the closing `]'.  */
7609  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7610
7611  /* Build the ARRAY_REF.  */
7612  postfix_expression = grok_array_decl (loc, postfix_expression,
7613					index, decltype_p);
7614
7615  /* When not doing offsetof, array references are not permitted in
7616     constant-expressions.  */
7617  if (!for_offsetof
7618      && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7619    postfix_expression = error_mark_node;
7620
7621  return postfix_expression;
7622}
7623
7624/* A subroutine of cp_parser_postfix_dot_deref_expression.  Handle dot
7625   dereference of incomplete type, returns true if error_mark_node should
7626   be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7627   and *DEPENDENT_P.  */
7628
7629bool
7630cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7631				bool *dependent_p)
7632{
7633  /* In a template, be permissive by treating an object expression
7634     of incomplete type as dependent (after a pedwarn).  */
7635  diagnostic_t kind = (processing_template_decl
7636		       && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7637
7638  switch (TREE_CODE (*postfix_expression))
7639    {
7640    case CAST_EXPR:
7641    case REINTERPRET_CAST_EXPR:
7642    case CONST_CAST_EXPR:
7643    case STATIC_CAST_EXPR:
7644    case DYNAMIC_CAST_EXPR:
7645    case IMPLICIT_CONV_EXPR:
7646    case VIEW_CONVERT_EXPR:
7647    case NON_LVALUE_EXPR:
7648      kind = DK_ERROR;
7649      break;
7650    case OVERLOAD:
7651      /* Don't emit any diagnostic for OVERLOADs.  */
7652      kind = DK_IGNORED;
7653      break;
7654    default:
7655      /* Avoid clobbering e.g. DECLs.  */
7656      if (!EXPR_P (*postfix_expression))
7657	kind = DK_ERROR;
7658      break;
7659    }
7660
7661  if (kind == DK_IGNORED)
7662    return false;
7663
7664  location_t exploc = location_of (*postfix_expression);
7665  cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7666  if (!MAYBE_CLASS_TYPE_P (*scope))
7667    return true;
7668  if (kind == DK_ERROR)
7669    *scope = *postfix_expression = error_mark_node;
7670  else if (processing_template_decl)
7671    {
7672      *dependent_p = true;
7673      *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7674    }
7675  return false;
7676}
7677
7678/* A subroutine of cp_parser_postfix_expression that also gets hijacked
7679   by cp_parser_builtin_offsetof.  We're looking for
7680
7681     postfix-expression . template [opt] id-expression
7682     postfix-expression . pseudo-destructor-name
7683     postfix-expression -> template [opt] id-expression
7684     postfix-expression -> pseudo-destructor-name
7685
7686   FOR_OFFSETOF is set if we're being called in that context.  That sorta
7687   limits what of the above we'll actually accept, but nevermind.
7688   TOKEN_TYPE is the "." or "->" token, which will already have been
7689   removed from the stream.  */
7690
7691static tree
7692cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7693					enum cpp_ttype token_type,
7694					cp_expr postfix_expression,
7695					bool for_offsetof, cp_id_kind *idk,
7696					location_t location)
7697{
7698  tree name;
7699  bool dependent_p;
7700  bool pseudo_destructor_p;
7701  tree scope = NULL_TREE;
7702  location_t start_loc = postfix_expression.get_start ();
7703
7704  /* If this is a `->' operator, dereference the pointer.  */
7705  if (token_type == CPP_DEREF)
7706    postfix_expression = build_x_arrow (location, postfix_expression,
7707					tf_warning_or_error);
7708  /* Check to see whether or not the expression is type-dependent and
7709     not the current instantiation.  */
7710  dependent_p = type_dependent_object_expression_p (postfix_expression);
7711  /* The identifier following the `->' or `.' is not qualified.  */
7712  parser->scope = NULL_TREE;
7713  parser->qualifying_scope = NULL_TREE;
7714  parser->object_scope = NULL_TREE;
7715  *idk = CP_ID_KIND_NONE;
7716
7717  /* Enter the scope corresponding to the type of the object
7718     given by the POSTFIX_EXPRESSION.  */
7719  if (!dependent_p)
7720    {
7721      scope = TREE_TYPE (postfix_expression);
7722      /* According to the standard, no expression should ever have
7723	 reference type.  Unfortunately, we do not currently match
7724	 the standard in this respect in that our internal representation
7725	 of an expression may have reference type even when the standard
7726	 says it does not.  Therefore, we have to manually obtain the
7727	 underlying type here.  */
7728      scope = non_reference (scope);
7729      /* The type of the POSTFIX_EXPRESSION must be complete.  */
7730      /* Unlike the object expression in other contexts, *this is not
7731	 required to be of complete type for purposes of class member
7732	 access (5.2.5) outside the member function body.  */
7733      if (postfix_expression != current_class_ref
7734	  && scope != error_mark_node
7735	  && !currently_open_class (scope))
7736	{
7737	  scope = complete_type (scope);
7738	  if (!COMPLETE_TYPE_P (scope)
7739	      && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7740						 &dependent_p))
7741	    return error_mark_node;
7742	}
7743
7744      if (!dependent_p)
7745	{
7746	  /* Let the name lookup machinery know that we are processing a
7747	     class member access expression.  */
7748	  parser->context->object_type = scope;
7749	  /* If something went wrong, we want to be able to discern that case,
7750	     as opposed to the case where there was no SCOPE due to the type
7751	     of expression being dependent.  */
7752	  if (!scope)
7753	    scope = error_mark_node;
7754	  /* If the SCOPE was erroneous, make the various semantic analysis
7755	     functions exit quickly -- and without issuing additional error
7756	     messages.  */
7757	  if (scope == error_mark_node)
7758	    postfix_expression = error_mark_node;
7759	}
7760    }
7761
7762  if (dependent_p)
7763    /* Tell cp_parser_lookup_name that there was an object, even though it's
7764       type-dependent.  */
7765    parser->context->object_type = unknown_type_node;
7766
7767  /* Assume this expression is not a pseudo-destructor access.  */
7768  pseudo_destructor_p = false;
7769
7770  /* If the SCOPE is a scalar type, then, if this is a valid program,
7771     we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
7772     is type dependent, it can be pseudo-destructor-name or something else.
7773     Try to parse it as pseudo-destructor-name first.  */
7774  if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7775    {
7776      tree s;
7777      tree type;
7778
7779      cp_parser_parse_tentatively (parser);
7780      /* Parse the pseudo-destructor-name.  */
7781      s = NULL_TREE;
7782      cp_parser_pseudo_destructor_name (parser, postfix_expression,
7783					&s, &type);
7784      if (dependent_p
7785	  && (cp_parser_error_occurred (parser)
7786	      || !SCALAR_TYPE_P (type)))
7787	cp_parser_abort_tentative_parse (parser);
7788      else if (cp_parser_parse_definitely (parser))
7789	{
7790	  pseudo_destructor_p = true;
7791	  postfix_expression
7792	    = finish_pseudo_destructor_expr (postfix_expression,
7793					     s, type, location);
7794	}
7795    }
7796
7797  if (!pseudo_destructor_p)
7798    {
7799      /* If the SCOPE is not a scalar type, we are looking at an
7800	 ordinary class member access expression, rather than a
7801	 pseudo-destructor-name.  */
7802      bool template_p;
7803      cp_token *token = cp_lexer_peek_token (parser->lexer);
7804      /* Parse the id-expression.  */
7805      name = (cp_parser_id_expression
7806	      (parser,
7807	       cp_parser_optional_template_keyword (parser),
7808	       /*check_dependency_p=*/true,
7809	       &template_p,
7810	       /*declarator_p=*/false,
7811	       /*optional_p=*/false));
7812      /* In general, build a SCOPE_REF if the member name is qualified.
7813	 However, if the name was not dependent and has already been
7814	 resolved; there is no need to build the SCOPE_REF.  For example;
7815
7816	     struct X { void f(); };
7817	     template <typename T> void f(T* t) { t->X::f(); }
7818
7819	 Even though "t" is dependent, "X::f" is not and has been resolved
7820	 to a BASELINK; there is no need to include scope information.  */
7821
7822      /* But we do need to remember that there was an explicit scope for
7823	 virtual function calls.  */
7824      if (parser->scope)
7825	*idk = CP_ID_KIND_QUALIFIED;
7826
7827      /* If the name is a template-id that names a type, we will get a
7828	 TYPE_DECL here.  That is invalid code.  */
7829      if (TREE_CODE (name) == TYPE_DECL)
7830	{
7831	  error_at (token->location, "invalid use of %qD", name);
7832	  postfix_expression = error_mark_node;
7833	}
7834      else
7835	{
7836	  if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7837	    {
7838	      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7839		{
7840		  error_at (token->location, "%<%D::%D%> is not a class member",
7841			    parser->scope, name);
7842		  postfix_expression = error_mark_node;
7843		}
7844	      else
7845		name = build_qualified_name (/*type=*/NULL_TREE,
7846					     parser->scope,
7847					     name,
7848					     template_p);
7849	      parser->scope = NULL_TREE;
7850	      parser->qualifying_scope = NULL_TREE;
7851	      parser->object_scope = NULL_TREE;
7852	    }
7853	  if (parser->scope && name && BASELINK_P (name))
7854	    adjust_result_of_qualified_name_lookup
7855	      (name, parser->scope, scope);
7856	  postfix_expression
7857	    = finish_class_member_access_expr (postfix_expression, name,
7858					       template_p,
7859					       tf_warning_or_error);
7860	  /* Build a location e.g.:
7861	       ptr->access_expr
7862	       ~~~^~~~~~~~~~~~~
7863	     where the caret is at the deref token, ranging from
7864	     the start of postfix_expression to the end of the access expr.  */
7865	  location_t combined_loc
7866	    = make_location (input_location, start_loc, parser->lexer);
7867	  protected_set_expr_location (postfix_expression, combined_loc);
7868	}
7869    }
7870
7871  /* We no longer need to look up names in the scope of the object on
7872     the left-hand side of the `.' or `->' operator.  */
7873  parser->context->object_type = NULL_TREE;
7874
7875  /* Outside of offsetof, these operators may not appear in
7876     constant-expressions.  */
7877  if (!for_offsetof
7878      && (cp_parser_non_integral_constant_expression
7879	  (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7880    postfix_expression = error_mark_node;
7881
7882  return postfix_expression;
7883}
7884
7885/* Parse a parenthesized expression-list.
7886
7887   expression-list:
7888     assignment-expression
7889     expression-list, assignment-expression
7890
7891   attribute-list:
7892     expression-list
7893     identifier
7894     identifier, expression-list
7895
7896   CAST_P is true if this expression is the target of a cast.
7897
7898   ALLOW_EXPANSION_P is true if this expression allows expansion of an
7899   argument pack.
7900
7901   WRAP_LOCATIONS_P is true if expressions within this list for which
7902   CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7903   their source locations.
7904
7905   Returns a vector of trees.  Each element is a representation of an
7906   assignment-expression.  NULL is returned if the ( and or ) are
7907   missing.  An empty, but allocated, vector is returned on no
7908   expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
7909   if we are parsing an attribute list for an attribute that wants a
7910   plain identifier argument, normal_attr for an attribute that wants
7911   an expression, or non_attr if we aren't parsing an attribute list.  If
7912   NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7913   not all of the expressions in the list were constant.
7914   If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7915   will be written to with the location of the closing parenthesis.  If
7916   an error occurs, it may or may not be written to.  */
7917
7918static vec<tree, va_gc> *
7919cp_parser_parenthesized_expression_list (cp_parser* parser,
7920					 int is_attribute_list,
7921					 bool cast_p,
7922                                         bool allow_expansion_p,
7923					 bool *non_constant_p,
7924					 location_t *close_paren_loc,
7925					 bool wrap_locations_p)
7926{
7927  vec<tree, va_gc> *expression_list;
7928  bool fold_expr_p = is_attribute_list != non_attr;
7929  tree identifier = NULL_TREE;
7930  bool saved_greater_than_is_operator_p;
7931
7932  /* Assume all the expressions will be constant.  */
7933  if (non_constant_p)
7934    *non_constant_p = false;
7935
7936  matching_parens parens;
7937  if (!parens.require_open (parser))
7938    return NULL;
7939
7940  expression_list = make_tree_vector ();
7941
7942  /* Within a parenthesized expression, a `>' token is always
7943     the greater-than operator.  */
7944  saved_greater_than_is_operator_p
7945    = parser->greater_than_is_operator_p;
7946  parser->greater_than_is_operator_p = true;
7947
7948  cp_expr expr (NULL_TREE);
7949
7950  /* Consume expressions until there are no more.  */
7951  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7952    while (true)
7953      {
7954	/* At the beginning of attribute lists, check to see if the
7955	   next token is an identifier.  */
7956	if (is_attribute_list == id_attr
7957	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7958	  {
7959	    cp_token *token;
7960
7961	    /* Consume the identifier.  */
7962	    token = cp_lexer_consume_token (parser->lexer);
7963	    /* Save the identifier.  */
7964	    identifier = token->u.value;
7965	  }
7966	else
7967	  {
7968	    bool expr_non_constant_p;
7969
7970	    /* Parse the next assignment-expression.  */
7971	    if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7972	      {
7973		/* A braced-init-list.  */
7974		cp_lexer_set_source_position (parser->lexer);
7975		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7976		expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7977		if (non_constant_p && expr_non_constant_p)
7978		  *non_constant_p = true;
7979	      }
7980	    else if (non_constant_p)
7981	      {
7982		expr = (cp_parser_constant_expression
7983			(parser, /*allow_non_constant_p=*/true,
7984			 &expr_non_constant_p));
7985		if (expr_non_constant_p)
7986		  *non_constant_p = true;
7987	      }
7988	    else
7989	      expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7990						      cast_p);
7991
7992	    if (fold_expr_p)
7993	      expr = instantiate_non_dependent_expr (expr);
7994
7995            /* If we have an ellipsis, then this is an expression
7996	       expansion.  */
7997            if (allow_expansion_p
7998                && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7999              {
8000                /* Consume the `...'.  */
8001                cp_lexer_consume_token (parser->lexer);
8002
8003                /* Build the argument pack.  */
8004                expr = make_pack_expansion (expr);
8005              }
8006
8007	    if (wrap_locations_p)
8008	      expr.maybe_add_location_wrapper ();
8009
8010	     /* Add it to the list.  We add error_mark_node
8011		expressions to the list, so that we can still tell if
8012		the correct form for a parenthesized expression-list
8013		is found. That gives better errors.  */
8014	    vec_safe_push (expression_list, expr.get_value ());
8015
8016	    if (expr == error_mark_node)
8017	      goto skip_comma;
8018	  }
8019
8020	/* After the first item, attribute lists look the same as
8021	   expression lists.  */
8022	is_attribute_list = non_attr;
8023
8024      get_comma:;
8025	/* If the next token isn't a `,', then we are done.  */
8026	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8027	  break;
8028
8029	/* Otherwise, consume the `,' and keep going.  */
8030	cp_lexer_consume_token (parser->lexer);
8031      }
8032
8033  if (close_paren_loc)
8034    *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8035
8036  if (!parens.require_close (parser))
8037    {
8038      int ending;
8039
8040    skip_comma:;
8041      /* We try and resync to an unnested comma, as that will give the
8042	 user better diagnostics.  */
8043      ending = cp_parser_skip_to_closing_parenthesis (parser,
8044						      /*recovering=*/true,
8045						      /*or_comma=*/true,
8046						      /*consume_paren=*/true);
8047      if (ending < 0)
8048	goto get_comma;
8049      if (!ending)
8050	{
8051	  parser->greater_than_is_operator_p
8052	    = saved_greater_than_is_operator_p;
8053	  return NULL;
8054	}
8055    }
8056
8057  parser->greater_than_is_operator_p
8058    = saved_greater_than_is_operator_p;
8059
8060  if (identifier)
8061    vec_safe_insert (expression_list, 0, identifier);
8062
8063  return expression_list;
8064}
8065
8066/* Parse a pseudo-destructor-name.
8067
8068   pseudo-destructor-name:
8069     :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8070     :: [opt] nested-name-specifier template template-id :: ~ type-name
8071     :: [opt] nested-name-specifier [opt] ~ type-name
8072
8073   If either of the first two productions is used, sets *SCOPE to the
8074   TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
8075   NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
8076   or ERROR_MARK_NODE if the parse fails.  */
8077
8078static void
8079cp_parser_pseudo_destructor_name (cp_parser* parser,
8080				  tree object,
8081				  tree* scope,
8082				  tree* type)
8083{
8084  bool nested_name_specifier_p;
8085
8086  /* Handle ~auto.  */
8087  if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8088      && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8089      && !type_dependent_expression_p (object))
8090    {
8091      if (cxx_dialect < cxx14)
8092	pedwarn (input_location, 0,
8093		 "%<~auto%> only available with "
8094		 "%<-std=c++14%> or %<-std=gnu++14%>");
8095      cp_lexer_consume_token (parser->lexer);
8096      cp_lexer_consume_token (parser->lexer);
8097      *scope = NULL_TREE;
8098      *type = TREE_TYPE (object);
8099      return;
8100    }
8101
8102  /* Assume that things will not work out.  */
8103  *type = error_mark_node;
8104
8105  /* Look for the optional `::' operator.  */
8106  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8107  /* Look for the optional nested-name-specifier.  */
8108  nested_name_specifier_p
8109    = (cp_parser_nested_name_specifier_opt (parser,
8110					    /*typename_keyword_p=*/false,
8111					    /*check_dependency_p=*/true,
8112					    /*type_p=*/false,
8113					    /*is_declaration=*/false)
8114       != NULL_TREE);
8115  /* Now, if we saw a nested-name-specifier, we might be doing the
8116     second production.  */
8117  if (nested_name_specifier_p
8118      && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8119    {
8120      /* Consume the `template' keyword.  */
8121      cp_lexer_consume_token (parser->lexer);
8122      /* Parse the template-id.  */
8123      cp_parser_template_id (parser,
8124			     /*template_keyword_p=*/true,
8125			     /*check_dependency_p=*/false,
8126			     class_type,
8127			     /*is_declaration=*/true);
8128      /* Look for the `::' token.  */
8129      cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8130    }
8131  /* If the next token is not a `~', then there might be some
8132     additional qualification.  */
8133  else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8134    {
8135      /* At this point, we're looking for "type-name :: ~".  The type-name
8136	 must not be a class-name, since this is a pseudo-destructor.  So,
8137	 it must be either an enum-name, or a typedef-name -- both of which
8138	 are just identifiers.  So, we peek ahead to check that the "::"
8139	 and "~" tokens are present; if they are not, then we can avoid
8140	 calling type_name.  */
8141      if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8142	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8143	  || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8144	{
8145	  cp_parser_error (parser, "non-scalar type");
8146	  return;
8147	}
8148
8149      /* Look for the type-name.  */
8150      *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8151      if (*scope == error_mark_node)
8152	return;
8153
8154      /* Look for the `::' token.  */
8155      cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8156    }
8157  else
8158    *scope = NULL_TREE;
8159
8160  /* Look for the `~'.  */
8161  cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8162
8163  /* Once we see the ~, this has to be a pseudo-destructor.  */
8164  if (!processing_template_decl && !cp_parser_error_occurred (parser))
8165    cp_parser_commit_to_topmost_tentative_parse (parser);
8166
8167  /* Look for the type-name again.  We are not responsible for
8168     checking that it matches the first type-name.  */
8169  *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8170}
8171
8172/* Parse a unary-expression.
8173
8174   unary-expression:
8175     postfix-expression
8176     ++ cast-expression
8177     -- cast-expression
8178     await-expression
8179     unary-operator cast-expression
8180     sizeof unary-expression
8181     sizeof ( type-id )
8182     alignof ( type-id )  [C++0x]
8183     new-expression
8184     delete-expression
8185
8186   GNU Extensions:
8187
8188   unary-expression:
8189     __extension__ cast-expression
8190     __alignof__ unary-expression
8191     __alignof__ ( type-id )
8192     alignof unary-expression  [C++0x]
8193     __real__ cast-expression
8194     __imag__ cast-expression
8195     && identifier
8196     sizeof ( type-id ) { initializer-list , [opt] }
8197     alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8198     __alignof__ ( type-id ) { initializer-list , [opt] }
8199
8200   ADDRESS_P is true iff the unary-expression is appearing as the
8201   operand of the `&' operator.   CAST_P is true if this expression is
8202   the target of a cast.
8203
8204   Returns a representation of the expression.  */
8205
8206static cp_expr
8207cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8208			    bool address_p, bool cast_p, bool decltype_p)
8209{
8210  cp_token *token;
8211  enum tree_code unary_operator;
8212
8213  /* Peek at the next token.  */
8214  token = cp_lexer_peek_token (parser->lexer);
8215  /* Some keywords give away the kind of expression.  */
8216  if (token->type == CPP_KEYWORD)
8217    {
8218      enum rid keyword = token->keyword;
8219
8220      switch (keyword)
8221	{
8222	case RID_ALIGNOF:
8223	case RID_SIZEOF:
8224	  {
8225	    tree operand, ret;
8226	    enum tree_code op;
8227	    location_t start_loc = token->location;
8228
8229	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8230	    bool std_alignof = id_equal (token->u.value, "alignof");
8231
8232	    /* Consume the token.  */
8233	    cp_lexer_consume_token (parser->lexer);
8234	    /* Parse the operand.  */
8235	    operand = cp_parser_sizeof_operand (parser, keyword);
8236
8237	    /* Construct a location e.g. :
8238              alignof (expr)
8239              ^~~~~~~~~~~~~~
8240              with start == caret at the start of the "alignof"/"sizeof"
8241              token, with the endpoint at the final closing paren.  */
8242	    location_t compound_loc
8243	      = make_location (start_loc, start_loc, parser->lexer);
8244
8245	    if (TYPE_P (operand))
8246	      ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8247						std_alignof, true);
8248	    else
8249	      {
8250		/* ISO C++ defines alignof only with types, not with
8251		   expressions. So pedwarn if alignof is used with a non-
8252		   type expression. However, __alignof__ is ok.  */
8253		if (std_alignof)
8254		  pedwarn (token->location, OPT_Wpedantic,
8255			   "ISO C++ does not allow %<alignof%> "
8256			   "with a non-type");
8257
8258		ret = cxx_sizeof_or_alignof_expr (compound_loc,
8259						  operand, op, true);
8260	      }
8261	    /* For SIZEOF_EXPR, just issue diagnostics, but keep
8262	       SIZEOF_EXPR with the original operand.  */
8263	    if (op == SIZEOF_EXPR && ret != error_mark_node)
8264	      {
8265		if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8266		  {
8267		    if (!processing_template_decl && TYPE_P (operand))
8268		      {
8269			ret = build_min (SIZEOF_EXPR, size_type_node,
8270					 build1 (NOP_EXPR, operand,
8271						 error_mark_node));
8272			SIZEOF_EXPR_TYPE_P (ret) = 1;
8273		      }
8274		    else
8275		      ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8276		    TREE_SIDE_EFFECTS (ret) = 0;
8277		    TREE_READONLY (ret) = 1;
8278		    SET_EXPR_LOCATION (ret, compound_loc);
8279		  }
8280	      }
8281
8282	    cp_expr ret_expr (ret, compound_loc);
8283	    ret_expr = ret_expr.maybe_add_location_wrapper ();
8284	    return ret_expr;
8285	  }
8286
8287	case RID_BUILTIN_HAS_ATTRIBUTE:
8288	  return cp_parser_has_attribute_expression (parser);
8289
8290	case RID_NEW:
8291	  return cp_parser_new_expression (parser);
8292
8293	case RID_DELETE:
8294	  return cp_parser_delete_expression (parser);
8295
8296	case RID_EXTENSION:
8297	  {
8298	    /* The saved value of the PEDANTIC flag.  */
8299	    int saved_pedantic;
8300	    tree expr;
8301
8302	    /* Save away the PEDANTIC flag.  */
8303	    cp_parser_extension_opt (parser, &saved_pedantic);
8304	    /* Parse the cast-expression.  */
8305	    expr = cp_parser_simple_cast_expression (parser);
8306	    /* Restore the PEDANTIC flag.  */
8307	    pedantic = saved_pedantic;
8308
8309	    return expr;
8310	  }
8311
8312	case RID_REALPART:
8313	case RID_IMAGPART:
8314	  {
8315	    tree expression;
8316
8317	    /* Consume the `__real__' or `__imag__' token.  */
8318	    cp_lexer_consume_token (parser->lexer);
8319	    /* Parse the cast-expression.  */
8320	    expression = cp_parser_simple_cast_expression (parser);
8321	    /* Create the complete representation.  */
8322	    return build_x_unary_op (token->location,
8323				     (keyword == RID_REALPART
8324				      ? REALPART_EXPR : IMAGPART_EXPR),
8325				     expression,
8326                                     tf_warning_or_error);
8327	  }
8328	  break;
8329
8330	case RID_TRANSACTION_ATOMIC:
8331	case RID_TRANSACTION_RELAXED:
8332	  return cp_parser_transaction_expression (parser, keyword);
8333
8334	case RID_NOEXCEPT:
8335	  {
8336	    tree expr;
8337	    const char *saved_message;
8338	    bool saved_integral_constant_expression_p;
8339	    bool saved_non_integral_constant_expression_p;
8340	    bool saved_greater_than_is_operator_p;
8341
8342	    location_t start_loc = token->location;
8343
8344	    cp_lexer_consume_token (parser->lexer);
8345	    matching_parens parens;
8346	    parens.require_open (parser);
8347
8348	    saved_message = parser->type_definition_forbidden_message;
8349	    parser->type_definition_forbidden_message
8350	      = G_("types may not be defined in %<noexcept%> expressions");
8351
8352	    saved_integral_constant_expression_p
8353	      = parser->integral_constant_expression_p;
8354	    saved_non_integral_constant_expression_p
8355	      = parser->non_integral_constant_expression_p;
8356	    parser->integral_constant_expression_p = false;
8357
8358	    saved_greater_than_is_operator_p
8359	      = parser->greater_than_is_operator_p;
8360	    parser->greater_than_is_operator_p = true;
8361
8362	    ++cp_unevaluated_operand;
8363	    ++c_inhibit_evaluation_warnings;
8364	    ++cp_noexcept_operand;
8365	    expr = cp_parser_expression (parser);
8366	    --cp_noexcept_operand;
8367	    --c_inhibit_evaluation_warnings;
8368	    --cp_unevaluated_operand;
8369
8370	    parser->greater_than_is_operator_p
8371	      = saved_greater_than_is_operator_p;
8372
8373	    parser->integral_constant_expression_p
8374	      = saved_integral_constant_expression_p;
8375	    parser->non_integral_constant_expression_p
8376	      = saved_non_integral_constant_expression_p;
8377
8378	    parser->type_definition_forbidden_message = saved_message;
8379
8380	    parens.require_close (parser);
8381
8382	    /* Construct a location of the form:
8383	       noexcept (expr)
8384	       ^~~~~~~~~~~~~~~
8385	       with start == caret, finishing at the close-paren.  */
8386	    location_t noexcept_loc
8387	      = make_location (start_loc, start_loc, parser->lexer);
8388
8389	    return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8390			    noexcept_loc);
8391	  }
8392
8393	case RID_CO_AWAIT:
8394	  {
8395	    tree expr;
8396	    location_t kw_loc = token->location;
8397
8398	    /* Consume the `co_await' token.  */
8399	    cp_lexer_consume_token (parser->lexer);
8400	    /* Parse its cast-expression.  */
8401	    expr = cp_parser_simple_cast_expression (parser);
8402	    if (expr == error_mark_node)
8403	      return error_mark_node;
8404
8405	    /* Handle [expr.await].  */
8406	    return cp_expr (finish_co_await_expr (kw_loc, expr));
8407	  }
8408
8409	default:
8410	  break;
8411	}
8412    }
8413
8414  /* Look for the `:: new' and `:: delete', which also signal the
8415     beginning of a new-expression, or delete-expression,
8416     respectively.  If the next token is `::', then it might be one of
8417     these.  */
8418  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8419    {
8420      enum rid keyword;
8421
8422      /* See if the token after the `::' is one of the keywords in
8423	 which we're interested.  */
8424      keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8425      /* If it's `new', we have a new-expression.  */
8426      if (keyword == RID_NEW)
8427	return cp_parser_new_expression (parser);
8428      /* Similarly, for `delete'.  */
8429      else if (keyword == RID_DELETE)
8430	return cp_parser_delete_expression (parser);
8431    }
8432
8433  /* Look for a unary operator.  */
8434  unary_operator = cp_parser_unary_operator (token);
8435  /* The `++' and `--' operators can be handled similarly, even though
8436     they are not technically unary-operators in the grammar.  */
8437  if (unary_operator == ERROR_MARK)
8438    {
8439      if (token->type == CPP_PLUS_PLUS)
8440	unary_operator = PREINCREMENT_EXPR;
8441      else if (token->type == CPP_MINUS_MINUS)
8442	unary_operator = PREDECREMENT_EXPR;
8443      /* Handle the GNU address-of-label extension.  */
8444      else if (cp_parser_allow_gnu_extensions_p (parser)
8445	       && token->type == CPP_AND_AND)
8446	{
8447	  tree identifier;
8448	  tree expression;
8449	  location_t start_loc = token->location;
8450
8451	  /* Consume the '&&' token.  */
8452	  cp_lexer_consume_token (parser->lexer);
8453	  /* Look for the identifier.  */
8454	  identifier = cp_parser_identifier (parser);
8455	  /* Construct a location of the form:
8456	       &&label
8457	       ^~~~~~~
8458	     with caret==start at the "&&", finish at the end of the label.  */
8459	  location_t combined_loc
8460	    = make_location (start_loc, start_loc, parser->lexer);
8461	  /* Create an expression representing the address.  */
8462	  expression = finish_label_address_expr (identifier, combined_loc);
8463	  if (cp_parser_non_integral_constant_expression (parser,
8464							  NIC_ADDR_LABEL))
8465	    expression = error_mark_node;
8466	  return expression;
8467	}
8468    }
8469  if (unary_operator != ERROR_MARK)
8470    {
8471      cp_expr cast_expression;
8472      cp_expr expression = error_mark_node;
8473      non_integral_constant non_constant_p = NIC_NONE;
8474      location_t loc = token->location;
8475      tsubst_flags_t complain = complain_flags (decltype_p);
8476
8477      /* Consume the operator token.  */
8478      token = cp_lexer_consume_token (parser->lexer);
8479      enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8480
8481      /* Parse the cast-expression.  */
8482      cast_expression
8483	= cp_parser_cast_expression (parser,
8484				     unary_operator == ADDR_EXPR,
8485				     /*cast_p=*/false,
8486				     /*decltype*/false,
8487				     pidk);
8488
8489      /* Make a location:
8490	    OP_TOKEN  CAST_EXPRESSION
8491	    ^~~~~~~~~~~~~~~~~~~~~~~~~
8492	 with start==caret at the operator token, and
8493	 extending to the end of the cast_expression.  */
8494      loc = make_location (loc, loc, cast_expression.get_finish ());
8495
8496      /* Now, build an appropriate representation.  */
8497      switch (unary_operator)
8498	{
8499	case INDIRECT_REF:
8500	  non_constant_p = NIC_STAR;
8501	  expression = build_x_indirect_ref (loc, cast_expression,
8502					     RO_UNARY_STAR,
8503                                             complain);
8504          /* TODO: build_x_indirect_ref does not always honor the
8505             location, so ensure it is set.  */
8506          expression.set_location (loc);
8507	  break;
8508
8509	case ADDR_EXPR:
8510	   non_constant_p = NIC_ADDR;
8511	  /* Fall through.  */
8512	case BIT_NOT_EXPR:
8513	  expression = build_x_unary_op (loc, unary_operator,
8514					 cast_expression,
8515                                         complain);
8516          /* TODO: build_x_unary_op does not always honor the location,
8517             so ensure it is set.  */
8518          expression.set_location (loc);
8519	  break;
8520
8521	case PREINCREMENT_EXPR:
8522	case PREDECREMENT_EXPR:
8523	  non_constant_p = unary_operator == PREINCREMENT_EXPR
8524			   ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8525	  /* Fall through.  */
8526	case NEGATE_EXPR:
8527	  /* Immediately fold negation of a constant, unless the constant is 0
8528	     (since -0 == 0) or it would overflow.  */
8529	  if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8530	    {
8531	      tree stripped_expr
8532		= tree_strip_any_location_wrapper (cast_expression);
8533	      if (CONSTANT_CLASS_P (stripped_expr)
8534		  && !integer_zerop (stripped_expr)
8535		  && !TREE_OVERFLOW (stripped_expr))
8536		{
8537		  tree folded = fold_build1 (unary_operator,
8538					     TREE_TYPE (stripped_expr),
8539					     stripped_expr);
8540		  if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8541		    {
8542		      expression = maybe_wrap_with_location (folded, loc);
8543		      break;
8544		    }
8545		}
8546	    }
8547	  /* Fall through.  */
8548	case UNARY_PLUS_EXPR:
8549	case TRUTH_NOT_EXPR:
8550	  expression = finish_unary_op_expr (loc, unary_operator,
8551					     cast_expression, complain);
8552	  break;
8553
8554	default:
8555	  gcc_unreachable ();
8556	}
8557
8558      if (non_constant_p != NIC_NONE
8559	  && cp_parser_non_integral_constant_expression (parser,
8560							 non_constant_p))
8561	expression = error_mark_node;
8562
8563      return expression;
8564    }
8565
8566  return cp_parser_postfix_expression (parser, address_p, cast_p,
8567                                       /*member_access_only_p=*/false,
8568				       decltype_p,
8569				       pidk);
8570}
8571
8572/* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
8573   unary-operator, the corresponding tree code is returned.  */
8574
8575static enum tree_code
8576cp_parser_unary_operator (cp_token* token)
8577{
8578  switch (token->type)
8579    {
8580    case CPP_MULT:
8581      return INDIRECT_REF;
8582
8583    case CPP_AND:
8584      return ADDR_EXPR;
8585
8586    case CPP_PLUS:
8587      return UNARY_PLUS_EXPR;
8588
8589    case CPP_MINUS:
8590      return NEGATE_EXPR;
8591
8592    case CPP_NOT:
8593      return TRUTH_NOT_EXPR;
8594
8595    case CPP_COMPL:
8596      return BIT_NOT_EXPR;
8597
8598    default:
8599      return ERROR_MARK;
8600    }
8601}
8602
8603/* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8604   Returns a representation of the expression.  */
8605
8606static tree
8607cp_parser_has_attribute_expression (cp_parser *parser)
8608{
8609  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8610
8611  /* Consume the __builtin_has_attribute token.  */
8612  cp_lexer_consume_token (parser->lexer);
8613
8614  matching_parens parens;
8615  if (!parens.require_open (parser))
8616    return error_mark_node;
8617
8618  /* Types cannot be defined in a `sizeof' expression.  Save away the
8619     old message.  */
8620  const char *saved_message = parser->type_definition_forbidden_message;
8621  const char *saved_message_arg
8622    = parser->type_definition_forbidden_message_arg;
8623  parser->type_definition_forbidden_message
8624    = G_("types may not be defined in %qs expressions");
8625  parser->type_definition_forbidden_message_arg
8626    = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8627
8628  /* The restrictions on constant-expressions do not apply inside
8629     sizeof expressions.  */
8630  bool saved_integral_constant_expression_p
8631    = parser->integral_constant_expression_p;
8632  bool saved_non_integral_constant_expression_p
8633    = parser->non_integral_constant_expression_p;
8634  parser->integral_constant_expression_p = false;
8635
8636  /* Do not actually evaluate the expression.  */
8637  ++cp_unevaluated_operand;
8638  ++c_inhibit_evaluation_warnings;
8639
8640  tree oper = NULL_TREE;
8641
8642  /* We can't be sure yet whether we're looking at a type-id or an
8643     expression.  */
8644  cp_parser_parse_tentatively (parser);
8645
8646  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8647  parser->in_type_id_in_expr_p = true;
8648  /* Look for the type-id.  */
8649  oper = cp_parser_type_id (parser);
8650  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8651
8652  cp_parser_parse_definitely (parser);
8653
8654  /* If the type-id production did not work out, then we must be
8655     looking at an expression.  */
8656  if (!oper || oper == error_mark_node)
8657    oper = cp_parser_assignment_expression (parser);
8658
8659  STRIP_ANY_LOCATION_WRAPPER (oper);
8660
8661  /* Go back to evaluating expressions.  */
8662  --cp_unevaluated_operand;
8663  --c_inhibit_evaluation_warnings;
8664
8665  /* And restore the old one.  */
8666  parser->type_definition_forbidden_message = saved_message;
8667  parser->type_definition_forbidden_message_arg = saved_message_arg;
8668  parser->integral_constant_expression_p
8669    = saved_integral_constant_expression_p;
8670  parser->non_integral_constant_expression_p
8671    = saved_non_integral_constant_expression_p;
8672
8673  /* Consume the comma if it's there.  */
8674  if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8675    {
8676      cp_parser_skip_to_closing_parenthesis (parser, false, false,
8677					     /*consume_paren=*/true);
8678      return error_mark_node;
8679    }
8680
8681  /* Parse the attribute specification.  */
8682  bool ret = false;
8683  location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8684  if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8685    {
8686      if (oper == error_mark_node)
8687	/* Nothing.  */;
8688      else if (processing_template_decl && uses_template_parms (oper))
8689	sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
8690		  "not supported yet");
8691      else
8692	{
8693	  /* Fold constant expressions used in attributes first.  */
8694	  cp_check_const_attributes (attr);
8695
8696	  /* Finally, see if OPER has been declared with ATTR.  */
8697	  ret = has_attribute (atloc, oper, attr, default_conversion);
8698	}
8699
8700      parens.require_close (parser);
8701    }
8702  else
8703    {
8704      error_at (atloc, "expected identifier");
8705      cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8706    }
8707
8708  /* Construct a location e.g. :
8709     __builtin_has_attribute (oper, attr)
8710     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8711     with start == caret at the start of the built-in token,
8712     and with the endpoint at the final closing paren.  */
8713  location_t compound_loc
8714    = make_location (start_loc, start_loc, parser->lexer);
8715
8716  cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8717  ret_expr.set_location (compound_loc);
8718  ret_expr = ret_expr.maybe_add_location_wrapper ();
8719  return ret_expr;
8720}
8721
8722/* Parse a new-expression.
8723
8724   new-expression:
8725     :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8726     :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8727
8728   Returns a representation of the expression.  */
8729
8730static tree
8731cp_parser_new_expression (cp_parser* parser)
8732{
8733  bool global_scope_p;
8734  vec<tree, va_gc> *placement;
8735  tree type;
8736  vec<tree, va_gc> *initializer;
8737  tree nelts = NULL_TREE;
8738  tree ret;
8739
8740  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8741
8742  /* Look for the optional `::' operator.  */
8743  global_scope_p
8744    = (cp_parser_global_scope_opt (parser,
8745				   /*current_scope_valid_p=*/false)
8746       != NULL_TREE);
8747  /* Look for the `new' operator.  */
8748  cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8749  /* There's no easy way to tell a new-placement from the
8750     `( type-id )' construct.  */
8751  cp_parser_parse_tentatively (parser);
8752  /* Look for a new-placement.  */
8753  placement = cp_parser_new_placement (parser);
8754  /* If that didn't work out, there's no new-placement.  */
8755  if (!cp_parser_parse_definitely (parser))
8756    {
8757      if (placement != NULL)
8758	release_tree_vector (placement);
8759      placement = NULL;
8760    }
8761
8762  /* If the next token is a `(', then we have a parenthesized
8763     type-id.  */
8764  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8765    {
8766      cp_token *token;
8767      const char *saved_message = parser->type_definition_forbidden_message;
8768
8769      /* Consume the `('.  */
8770      matching_parens parens;
8771      parens.consume_open (parser);
8772
8773      /* Parse the type-id.  */
8774      parser->type_definition_forbidden_message
8775	= G_("types may not be defined in a new-expression");
8776      {
8777	type_id_in_expr_sentinel s (parser);
8778	type = cp_parser_type_id (parser);
8779      }
8780      parser->type_definition_forbidden_message = saved_message;
8781
8782      /* Look for the closing `)'.  */
8783      parens.require_close (parser);
8784      token = cp_lexer_peek_token (parser->lexer);
8785      /* There should not be a direct-new-declarator in this production,
8786	 but GCC used to allowed this, so we check and emit a sensible error
8787	 message for this case.  */
8788      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8789	{
8790	  error_at (token->location,
8791		    "array bound forbidden after parenthesized type-id");
8792	  inform (token->location,
8793		  "try removing the parentheses around the type-id");
8794	  cp_parser_direct_new_declarator (parser);
8795	}
8796    }
8797  /* Otherwise, there must be a new-type-id.  */
8798  else
8799    type = cp_parser_new_type_id (parser, &nelts);
8800
8801  /* If the next token is a `(' or '{', then we have a new-initializer.  */
8802  cp_token *token = cp_lexer_peek_token (parser->lexer);
8803  if (token->type == CPP_OPEN_PAREN
8804      || token->type == CPP_OPEN_BRACE)
8805    initializer = cp_parser_new_initializer (parser);
8806  else
8807    initializer = NULL;
8808
8809  /* A new-expression may not appear in an integral constant
8810     expression.  */
8811  if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8812    ret = error_mark_node;
8813  /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8814     of a new-type-id or type-id of a new-expression, the new-expression shall
8815     contain a new-initializer of the form ( assignment-expression )".
8816     Additionally, consistently with the spirit of DR 1467, we want to accept
8817     'new auto { 2 }' too.  */
8818  else if ((ret = type_uses_auto (type))
8819	   && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8820	   && (vec_safe_length (initializer) != 1
8821	       || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8822		   && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8823    {
8824      error_at (token->location,
8825		"initialization of new-expression for type %<auto%> "
8826		"requires exactly one element");
8827      ret = error_mark_node;
8828    }
8829  else
8830    {
8831      /* Construct a location e.g.:
8832           ptr = new int[100]
8833                 ^~~~~~~~~~~~
8834         with caret == start at the start of the "new" token, and the end
8835         at the end of the final token we consumed.  */
8836      location_t combined_loc = make_location (start_loc, start_loc,
8837					       parser->lexer);
8838      /* Create a representation of the new-expression.  */
8839      ret = build_new (combined_loc, &placement, type, nelts, &initializer,
8840		       global_scope_p, tf_warning_or_error);
8841    }
8842
8843  if (placement != NULL)
8844    release_tree_vector (placement);
8845  if (initializer != NULL)
8846    release_tree_vector (initializer);
8847
8848  return ret;
8849}
8850
8851/* Parse a new-placement.
8852
8853   new-placement:
8854     ( expression-list )
8855
8856   Returns the same representation as for an expression-list.  */
8857
8858static vec<tree, va_gc> *
8859cp_parser_new_placement (cp_parser* parser)
8860{
8861  vec<tree, va_gc> *expression_list;
8862
8863  /* Parse the expression-list.  */
8864  expression_list = (cp_parser_parenthesized_expression_list
8865		     (parser, non_attr, /*cast_p=*/false,
8866		      /*allow_expansion_p=*/true,
8867		      /*non_constant_p=*/NULL));
8868
8869  if (expression_list && expression_list->is_empty ())
8870    error ("expected expression-list or type-id");
8871
8872  return expression_list;
8873}
8874
8875/* Parse a new-type-id.
8876
8877   new-type-id:
8878     type-specifier-seq new-declarator [opt]
8879
8880   Returns the TYPE allocated.  If the new-type-id indicates an array
8881   type, *NELTS is set to the number of elements in the last array
8882   bound; the TYPE will not include the last array bound.  */
8883
8884static tree
8885cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8886{
8887  cp_decl_specifier_seq type_specifier_seq;
8888  cp_declarator *new_declarator;
8889  cp_declarator *declarator;
8890  cp_declarator *outer_declarator;
8891  const char *saved_message;
8892
8893  /* The type-specifier sequence must not contain type definitions.
8894     (It cannot contain declarations of new types either, but if they
8895     are not definitions we will catch that because they are not
8896     complete.)  */
8897  saved_message = parser->type_definition_forbidden_message;
8898  parser->type_definition_forbidden_message
8899    = G_("types may not be defined in a new-type-id");
8900  /* Parse the type-specifier-seq.  */
8901  cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8902				/*is_declaration=*/false,
8903				/*is_trailing_return=*/false,
8904				&type_specifier_seq);
8905  /* Restore the old message.  */
8906  parser->type_definition_forbidden_message = saved_message;
8907
8908  if (type_specifier_seq.type == error_mark_node)
8909    return error_mark_node;
8910
8911  /* Parse the new-declarator.  */
8912  new_declarator = cp_parser_new_declarator_opt (parser);
8913
8914  /* Determine the number of elements in the last array dimension, if
8915     any.  */
8916  *nelts = NULL_TREE;
8917  /* Skip down to the last array dimension.  */
8918  declarator = new_declarator;
8919  outer_declarator = NULL;
8920  while (declarator && (declarator->kind == cdk_pointer
8921			|| declarator->kind == cdk_ptrmem))
8922    {
8923      outer_declarator = declarator;
8924      declarator = declarator->declarator;
8925    }
8926  while (declarator
8927	 && declarator->kind == cdk_array
8928	 && declarator->declarator
8929	 && declarator->declarator->kind == cdk_array)
8930    {
8931      outer_declarator = declarator;
8932      declarator = declarator->declarator;
8933    }
8934
8935  if (declarator && declarator->kind == cdk_array)
8936    {
8937      *nelts = declarator->u.array.bounds;
8938      if (*nelts == error_mark_node)
8939	*nelts = integer_one_node;
8940
8941      if (outer_declarator)
8942	outer_declarator->declarator = declarator->declarator;
8943      else
8944	new_declarator = NULL;
8945    }
8946
8947  return groktypename (&type_specifier_seq, new_declarator, false);
8948}
8949
8950/* Parse an (optional) new-declarator.
8951
8952   new-declarator:
8953     ptr-operator new-declarator [opt]
8954     direct-new-declarator
8955
8956   Returns the declarator.  */
8957
8958static cp_declarator *
8959cp_parser_new_declarator_opt (cp_parser* parser)
8960{
8961  enum tree_code code;
8962  tree type, std_attributes = NULL_TREE;
8963  cp_cv_quals cv_quals;
8964
8965  /* We don't know if there's a ptr-operator next, or not.  */
8966  cp_parser_parse_tentatively (parser);
8967  /* Look for a ptr-operator.  */
8968  code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8969  /* If that worked, look for more new-declarators.  */
8970  if (cp_parser_parse_definitely (parser))
8971    {
8972      cp_declarator *declarator;
8973
8974      /* Parse another optional declarator.  */
8975      declarator = cp_parser_new_declarator_opt (parser);
8976
8977      declarator = cp_parser_make_indirect_declarator
8978	(code, type, cv_quals, declarator, std_attributes);
8979
8980      return declarator;
8981    }
8982
8983  /* If the next token is a `[', there is a direct-new-declarator.  */
8984  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8985    return cp_parser_direct_new_declarator (parser);
8986
8987  return NULL;
8988}
8989
8990/* Parse a direct-new-declarator.
8991
8992   direct-new-declarator:
8993     [ expression ]
8994     direct-new-declarator [constant-expression]
8995
8996   */
8997
8998static cp_declarator *
8999cp_parser_direct_new_declarator (cp_parser* parser)
9000{
9001  cp_declarator *declarator = NULL;
9002
9003  while (true)
9004    {
9005      tree expression;
9006      cp_token *token;
9007
9008      /* Look for the opening `['.  */
9009      cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9010
9011      token = cp_lexer_peek_token (parser->lexer);
9012      expression = cp_parser_expression (parser);
9013      /* The standard requires that the expression have integral
9014	 type.  DR 74 adds enumeration types.  We believe that the
9015	 real intent is that these expressions be handled like the
9016	 expression in a `switch' condition, which also allows
9017	 classes with a single conversion to integral or
9018	 enumeration type.  */
9019      if (!processing_template_decl)
9020	{
9021	  expression
9022	    = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9023					  expression,
9024					  /*complain=*/true);
9025	  if (!expression)
9026	    {
9027	      error_at (token->location,
9028			"expression in new-declarator must have integral "
9029			"or enumeration type");
9030	      expression = error_mark_node;
9031	    }
9032	}
9033
9034      /* Look for the closing `]'.  */
9035      cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9036
9037      /* Add this bound to the declarator.  */
9038      declarator = make_array_declarator (declarator, expression);
9039
9040      /* If the next token is not a `[', then there are no more
9041	 bounds.  */
9042      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9043	break;
9044    }
9045
9046  return declarator;
9047}
9048
9049/* Parse a new-initializer.
9050
9051   new-initializer:
9052     ( expression-list [opt] )
9053     braced-init-list
9054
9055   Returns a representation of the expression-list.  */
9056
9057static vec<tree, va_gc> *
9058cp_parser_new_initializer (cp_parser* parser)
9059{
9060  vec<tree, va_gc> *expression_list;
9061
9062  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9063    {
9064      tree t;
9065      bool expr_non_constant_p;
9066      cp_lexer_set_source_position (parser->lexer);
9067      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9068      t = cp_parser_braced_list (parser, &expr_non_constant_p);
9069      CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9070      expression_list = make_tree_vector_single (t);
9071    }
9072  else
9073    expression_list = (cp_parser_parenthesized_expression_list
9074		       (parser, non_attr, /*cast_p=*/false,
9075			/*allow_expansion_p=*/true,
9076			/*non_constant_p=*/NULL));
9077
9078  return expression_list;
9079}
9080
9081/* Parse a delete-expression.
9082
9083   delete-expression:
9084     :: [opt] delete cast-expression
9085     :: [opt] delete [ ] cast-expression
9086
9087   Returns a representation of the expression.  */
9088
9089static tree
9090cp_parser_delete_expression (cp_parser* parser)
9091{
9092  bool global_scope_p;
9093  bool array_p;
9094  tree expression;
9095  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9096
9097  /* Look for the optional `::' operator.  */
9098  global_scope_p
9099    = (cp_parser_global_scope_opt (parser,
9100				   /*current_scope_valid_p=*/false)
9101       != NULL_TREE);
9102  /* Look for the `delete' keyword.  */
9103  cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9104  /* See if the array syntax is in use.  */
9105  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9106    {
9107      /* Consume the `[' token.  */
9108      cp_lexer_consume_token (parser->lexer);
9109      /* Look for the `]' token.  */
9110      cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9111      /* Remember that this is the `[]' construct.  */
9112      array_p = true;
9113    }
9114  else
9115    array_p = false;
9116
9117  /* Parse the cast-expression.  */
9118  expression = cp_parser_simple_cast_expression (parser);
9119
9120  /* A delete-expression may not appear in an integral constant
9121     expression.  */
9122  if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9123    return error_mark_node;
9124
9125  /* Construct a location e.g.:
9126       delete [ ] ptr
9127       ^~~~~~~~~~~~~~
9128     with caret == start at the start of the "delete" token, and
9129     the end at the end of the final token we consumed.  */
9130  location_t combined_loc = make_location (start_loc, start_loc,
9131					   parser->lexer);
9132  expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9133			      global_scope_p, tf_warning_or_error);
9134
9135  return expression;
9136}
9137
9138/* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9139   neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9140   0 otherwise.  */
9141
9142static int
9143cp_parser_tokens_start_cast_expression (cp_parser *parser)
9144{
9145  cp_token *token = cp_lexer_peek_token (parser->lexer);
9146  switch (token->type)
9147    {
9148    case CPP_COMMA:
9149    case CPP_SEMICOLON:
9150    case CPP_QUERY:
9151    case CPP_COLON:
9152    case CPP_CLOSE_SQUARE:
9153    case CPP_CLOSE_PAREN:
9154    case CPP_CLOSE_BRACE:
9155    case CPP_OPEN_BRACE:
9156    case CPP_DOT:
9157    case CPP_DOT_STAR:
9158    case CPP_DEREF:
9159    case CPP_DEREF_STAR:
9160    case CPP_DIV:
9161    case CPP_MOD:
9162    case CPP_LSHIFT:
9163    case CPP_RSHIFT:
9164    case CPP_LESS:
9165    case CPP_GREATER:
9166    case CPP_LESS_EQ:
9167    case CPP_GREATER_EQ:
9168    case CPP_EQ_EQ:
9169    case CPP_NOT_EQ:
9170    case CPP_EQ:
9171    case CPP_MULT_EQ:
9172    case CPP_DIV_EQ:
9173    case CPP_MOD_EQ:
9174    case CPP_PLUS_EQ:
9175    case CPP_MINUS_EQ:
9176    case CPP_RSHIFT_EQ:
9177    case CPP_LSHIFT_EQ:
9178    case CPP_AND_EQ:
9179    case CPP_XOR_EQ:
9180    case CPP_OR_EQ:
9181    case CPP_XOR:
9182    case CPP_OR:
9183    case CPP_OR_OR:
9184    case CPP_EOF:
9185    case CPP_ELLIPSIS:
9186      return 0;
9187
9188    case CPP_OPEN_PAREN:
9189      /* In ((type ()) () the last () isn't a valid cast-expression,
9190	 so the whole must be parsed as postfix-expression.  */
9191      return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9192	     != CPP_CLOSE_PAREN;
9193
9194    case CPP_OPEN_SQUARE:
9195      /* '[' may start a primary-expression in obj-c++ and in C++11,
9196	 as a lambda-expression, eg, '(void)[]{}'.  */
9197      if (cxx_dialect >= cxx11)
9198	return -1;
9199      return c_dialect_objc ();
9200
9201    case CPP_PLUS_PLUS:
9202    case CPP_MINUS_MINUS:
9203      /* '++' and '--' may or may not start a cast-expression:
9204
9205	 struct T { void operator++(int); };
9206	 void f() { (T())++; }
9207
9208	 vs
9209
9210	 int a;
9211	 (int)++a;  */
9212      return -1;
9213
9214    default:
9215      return 1;
9216    }
9217}
9218
9219/* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9220   in the order: const_cast, static_cast, reinterpret_cast.
9221
9222   Don't suggest dynamic_cast.
9223
9224   Return the first legal cast kind found, or NULL otherwise.  */
9225
9226static const char *
9227get_cast_suggestion (tree dst_type, tree orig_expr)
9228{
9229  tree trial;
9230
9231  /* Reuse the parser logic by attempting to build the various kinds of
9232     cast, with "complain" disabled.
9233     Identify the first such cast that is valid.  */
9234
9235  /* Don't attempt to run such logic within template processing.  */
9236  if (processing_template_decl)
9237    return NULL;
9238
9239  /* First try const_cast.  */
9240  trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9241  if (trial != error_mark_node)
9242    return "const_cast";
9243
9244  /* If that fails, try static_cast.  */
9245  trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9246  if (trial != error_mark_node)
9247    return "static_cast";
9248
9249  /* Finally, try reinterpret_cast.  */
9250  trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9251				  tf_none);
9252  if (trial != error_mark_node)
9253    return "reinterpret_cast";
9254
9255  /* No such cast possible.  */
9256  return NULL;
9257}
9258
9259/* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9260   suggesting how to convert a C-style cast of the form:
9261
9262     (DST_TYPE)ORIG_EXPR
9263
9264   to a C++-style cast.
9265
9266   The primary range of RICHLOC is asssumed to be that of the original
9267   expression.  OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9268   of the parens in the C-style cast.  */
9269
9270static void
9271maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9272		      location_t close_paren_loc, tree orig_expr,
9273		      tree dst_type)
9274{
9275  /* This function is non-trivial, so bail out now if the warning isn't
9276     going to be emitted.  */
9277  if (!warn_old_style_cast)
9278    return;
9279
9280  /* Try to find a legal C++ cast, trying them in order:
9281     const_cast, static_cast, reinterpret_cast.  */
9282  const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9283  if (!cast_suggestion)
9284    return;
9285
9286  /* Replace the open paren with "CAST_SUGGESTION<".  */
9287  pretty_printer pp;
9288  pp_printf (&pp, "%s<", cast_suggestion);
9289  rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9290
9291  /* Replace the close paren with "> (".  */
9292  rich_loc->add_fixit_replace (close_paren_loc, "> (");
9293
9294  /* Add a closing paren after the expr (the primary range of RICH_LOC).  */
9295  rich_loc->add_fixit_insert_after (")");
9296}
9297
9298
9299/* Parse a cast-expression.
9300
9301   cast-expression:
9302     unary-expression
9303     ( type-id ) cast-expression
9304
9305   ADDRESS_P is true iff the unary-expression is appearing as the
9306   operand of the `&' operator.   CAST_P is true if this expression is
9307   the target of a cast.
9308
9309   Returns a representation of the expression.  */
9310
9311static cp_expr
9312cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9313			   bool decltype_p, cp_id_kind * pidk)
9314{
9315  /* If it's a `(', then we might be looking at a cast.  */
9316  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9317    {
9318      tree type = NULL_TREE;
9319      cp_expr expr (NULL_TREE);
9320      int cast_expression = 0;
9321      const char *saved_message;
9322
9323      /* There's no way to know yet whether or not this is a cast.
9324	 For example, `(int (3))' is a unary-expression, while `(int)
9325	 3' is a cast.  So, we resort to parsing tentatively.  */
9326      cp_parser_parse_tentatively (parser);
9327      /* Types may not be defined in a cast.  */
9328      saved_message = parser->type_definition_forbidden_message;
9329      parser->type_definition_forbidden_message
9330	= G_("types may not be defined in casts");
9331      /* Consume the `('.  */
9332      matching_parens parens;
9333      cp_token *open_paren = parens.consume_open (parser);
9334      location_t open_paren_loc = open_paren->location;
9335      location_t close_paren_loc = UNKNOWN_LOCATION;
9336
9337      /* A very tricky bit is that `(struct S) { 3 }' is a
9338	 compound-literal (which we permit in C++ as an extension).
9339	 But, that construct is not a cast-expression -- it is a
9340	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
9341	 is legal; if the compound-literal were a cast-expression,
9342	 you'd need an extra set of parentheses.)  But, if we parse
9343	 the type-id, and it happens to be a class-specifier, then we
9344	 will commit to the parse at that point, because we cannot
9345	 undo the action that is done when creating a new class.  So,
9346	 then we cannot back up and do a postfix-expression.
9347
9348	 Another tricky case is the following (c++/29234):
9349
9350         struct S { void operator () (); };
9351
9352         void foo ()
9353         {
9354           ( S()() );
9355         }
9356
9357	 As a type-id we parse the parenthesized S()() as a function
9358	 returning a function, groktypename complains and we cannot
9359	 back up in this case either.
9360
9361	 Therefore, we scan ahead to the closing `)', and check to see
9362	 if the tokens after the `)' can start a cast-expression.  Otherwise
9363	 we are dealing with an unary-expression, a postfix-expression
9364	 or something else.
9365
9366	 Yet another tricky case, in C++11, is the following (c++/54891):
9367
9368	 (void)[]{};
9369
9370         The issue is that usually, besides the case of lambda-expressions,
9371	 the parenthesized type-id cannot be followed by '[', and, eg, we
9372	 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9373	 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9374	 we don't commit, we try a cast-expression, then an unary-expression.
9375
9376	 Save tokens so that we can put them back.  */
9377      cp_lexer_save_tokens (parser->lexer);
9378
9379      /* We may be looking at a cast-expression.  */
9380      if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9381						 /*consume_paren=*/true))
9382	cast_expression
9383	  = cp_parser_tokens_start_cast_expression (parser);
9384
9385      /* Roll back the tokens we skipped.  */
9386      cp_lexer_rollback_tokens (parser->lexer);
9387      /* If we aren't looking at a cast-expression, simulate an error so
9388	 that the call to cp_parser_error_occurred below returns true.  */
9389      if (!cast_expression)
9390	cp_parser_simulate_error (parser);
9391      else
9392	{
9393	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9394	  parser->in_type_id_in_expr_p = true;
9395	  /* Look for the type-id.  */
9396	  type = cp_parser_type_id (parser);
9397	  /* Look for the closing `)'.  */
9398	  cp_token *close_paren = parens.require_close (parser);
9399	  if (close_paren)
9400	    close_paren_loc = close_paren->location;
9401	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9402	}
9403
9404      /* Restore the saved message.  */
9405      parser->type_definition_forbidden_message = saved_message;
9406
9407      /* At this point this can only be either a cast or a
9408	 parenthesized ctor such as `(T ())' that looks like a cast to
9409	 function returning T.  */
9410      if (!cp_parser_error_occurred (parser))
9411	{
9412	  /* Only commit if the cast-expression doesn't start with
9413	     '++', '--', or '[' in C++11.  */
9414	  if (cast_expression > 0)
9415	    cp_parser_commit_to_topmost_tentative_parse (parser);
9416
9417	  expr = cp_parser_cast_expression (parser,
9418					    /*address_p=*/false,
9419					    /*cast_p=*/true,
9420					    /*decltype_p=*/false,
9421					    pidk);
9422
9423	  if (cp_parser_parse_definitely (parser))
9424	    {
9425	      /* Warn about old-style casts, if so requested.  */
9426	      if (warn_old_style_cast
9427		  && !in_system_header_at (input_location)
9428		  && !VOID_TYPE_P (type)
9429		  && current_lang_name != lang_name_c)
9430		{
9431		  gcc_rich_location rich_loc (input_location);
9432		  maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9433					expr, type);
9434		  warning_at (&rich_loc, OPT_Wold_style_cast,
9435			      "use of old-style cast to %q#T", type);
9436		}
9437
9438	      /* Only type conversions to integral or enumeration types
9439		 can be used in constant-expressions.  */
9440	      if (!cast_valid_in_integral_constant_expression_p (type)
9441		  && cp_parser_non_integral_constant_expression (parser,
9442								 NIC_CAST))
9443		return error_mark_node;
9444
9445	      /* Perform the cast.  */
9446	      /* Make a location:
9447		   (TYPE) EXPR
9448		   ^~~~~~~~~~~
9449		 with start==caret at the open paren, extending to the
9450		 end of "expr".  */
9451	      location_t cast_loc = make_location (open_paren_loc,
9452						   open_paren_loc,
9453						   expr.get_finish ());
9454	      expr = build_c_cast (cast_loc, type, expr);
9455	      return expr;
9456	    }
9457	}
9458      else
9459        cp_parser_abort_tentative_parse (parser);
9460    }
9461
9462  /* If we get here, then it's not a cast, so it must be a
9463     unary-expression.  */
9464  return cp_parser_unary_expression (parser, pidk, address_p,
9465				     cast_p, decltype_p);
9466}
9467
9468/* Parse a binary expression of the general form:
9469
9470   pm-expression:
9471     cast-expression
9472     pm-expression .* cast-expression
9473     pm-expression ->* cast-expression
9474
9475   multiplicative-expression:
9476     pm-expression
9477     multiplicative-expression * pm-expression
9478     multiplicative-expression / pm-expression
9479     multiplicative-expression % pm-expression
9480
9481   additive-expression:
9482     multiplicative-expression
9483     additive-expression + multiplicative-expression
9484     additive-expression - multiplicative-expression
9485
9486   shift-expression:
9487     additive-expression
9488     shift-expression << additive-expression
9489     shift-expression >> additive-expression
9490
9491   relational-expression:
9492     shift-expression
9493     relational-expression < shift-expression
9494     relational-expression > shift-expression
9495     relational-expression <= shift-expression
9496     relational-expression >= shift-expression
9497
9498  GNU Extension:
9499
9500   relational-expression:
9501     relational-expression <? shift-expression
9502     relational-expression >? shift-expression
9503
9504   equality-expression:
9505     relational-expression
9506     equality-expression == relational-expression
9507     equality-expression != relational-expression
9508
9509   and-expression:
9510     equality-expression
9511     and-expression & equality-expression
9512
9513   exclusive-or-expression:
9514     and-expression
9515     exclusive-or-expression ^ and-expression
9516
9517   inclusive-or-expression:
9518     exclusive-or-expression
9519     inclusive-or-expression | exclusive-or-expression
9520
9521   logical-and-expression:
9522     inclusive-or-expression
9523     logical-and-expression && inclusive-or-expression
9524
9525   logical-or-expression:
9526     logical-and-expression
9527     logical-or-expression || logical-and-expression
9528
9529   All these are implemented with a single function like:
9530
9531   binary-expression:
9532     simple-cast-expression
9533     binary-expression <token> binary-expression
9534
9535   CAST_P is true if this expression is the target of a cast.
9536
9537   The binops_by_token map is used to get the tree codes for each <token> type.
9538   binary-expressions are associated according to a precedence table.  */
9539
9540#define TOKEN_PRECEDENCE(token)				     \
9541(((token->type == CPP_GREATER				     \
9542   || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9543  && !parser->greater_than_is_operator_p)		     \
9544 ? PREC_NOT_OPERATOR					     \
9545 : binops_by_token[token->type].prec)
9546
9547static cp_expr
9548cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9549			     bool no_toplevel_fold_p,
9550			     bool decltype_p,
9551			     enum cp_parser_prec prec,
9552			     cp_id_kind * pidk)
9553{
9554  cp_parser_expression_stack stack;
9555  cp_parser_expression_stack_entry *sp = &stack[0];
9556  cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9557  cp_parser_expression_stack_entry current;
9558  cp_expr rhs;
9559  cp_token *token;
9560  enum tree_code rhs_type;
9561  enum cp_parser_prec new_prec, lookahead_prec;
9562  tree overload;
9563
9564  /* Parse the first expression.  */
9565  current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9566		      ? TRUTH_NOT_EXPR : ERROR_MARK);
9567  current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9568					   cast_p, decltype_p, pidk);
9569  current.prec = prec;
9570
9571  if (cp_parser_error_occurred (parser))
9572    return error_mark_node;
9573
9574  for (;;)
9575    {
9576      /* Get an operator token.  */
9577      token = cp_lexer_peek_token (parser->lexer);
9578
9579      if (warn_cxx11_compat
9580          && token->type == CPP_RSHIFT
9581          && !parser->greater_than_is_operator_p)
9582        {
9583          if (warning_at (token->location, OPT_Wc__11_compat,
9584			  "%<>>%> operator is treated"
9585			  " as two right angle brackets in C++11"))
9586	    inform (token->location,
9587		    "suggest parentheses around %<>>%> expression");
9588        }
9589
9590      new_prec = TOKEN_PRECEDENCE (token);
9591      if (new_prec != PREC_NOT_OPERATOR
9592	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9593	/* This is a fold-expression; handle it later.  */
9594	new_prec = PREC_NOT_OPERATOR;
9595
9596      /* Popping an entry off the stack means we completed a subexpression:
9597	 - either we found a token which is not an operator (`>' where it is not
9598	   an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9599	   will happen repeatedly;
9600	 - or, we found an operator which has lower priority.  This is the case
9601	   where the recursive descent *ascends*, as in `3 * 4 + 5' after
9602	   parsing `3 * 4'.  */
9603      if (new_prec <= current.prec)
9604	{
9605	  if (sp == stack)
9606	    break;
9607	  else
9608	    goto pop;
9609	}
9610
9611     get_rhs:
9612      current.tree_type = binops_by_token[token->type].tree_type;
9613      current.loc = token->location;
9614
9615      /* We used the operator token.  */
9616      cp_lexer_consume_token (parser->lexer);
9617
9618      /* For "false && x" or "true || x", x will never be executed;
9619	 disable warnings while evaluating it.  */
9620      if ((current.tree_type == TRUTH_ANDIF_EXPR
9621	   && cp_fully_fold (current.lhs) == truthvalue_false_node)
9622	  || (current.tree_type == TRUTH_ORIF_EXPR
9623	      && cp_fully_fold (current.lhs) == truthvalue_true_node))
9624	{
9625	  disable_warnings_sp = sp;
9626	  ++c_inhibit_evaluation_warnings;
9627	}
9628
9629      /* Extract another operand.  It may be the RHS of this expression
9630	 or the LHS of a new, higher priority expression.  */
9631      rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9632		  ? TRUTH_NOT_EXPR : ERROR_MARK);
9633      rhs = cp_parser_simple_cast_expression (parser);
9634
9635      /* Get another operator token.  Look up its precedence to avoid
9636	 building a useless (immediately popped) stack entry for common
9637	 cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
9638      token = cp_lexer_peek_token (parser->lexer);
9639      lookahead_prec = TOKEN_PRECEDENCE (token);
9640      if (lookahead_prec != PREC_NOT_OPERATOR
9641	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9642	lookahead_prec = PREC_NOT_OPERATOR;
9643      if (lookahead_prec > new_prec)
9644	{
9645	  /* ... and prepare to parse the RHS of the new, higher priority
9646	     expression.  Since precedence levels on the stack are
9647	     monotonically increasing, we do not have to care about
9648	     stack overflows.  */
9649	  *sp = current;
9650	  ++sp;
9651	  current.lhs = rhs;
9652	  current.lhs_type = rhs_type;
9653	  current.prec = new_prec;
9654	  new_prec = lookahead_prec;
9655	  goto get_rhs;
9656
9657	 pop:
9658	  lookahead_prec = new_prec;
9659	  /* If the stack is not empty, we have parsed into LHS the right side
9660	     (`4' in the example above) of an expression we had suspended.
9661	     We can use the information on the stack to recover the LHS (`3')
9662	     from the stack together with the tree code (`MULT_EXPR'), and
9663	     the precedence of the higher level subexpression
9664	     (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
9665	     which will be used to actually build the additive expression.  */
9666	  rhs = current.lhs;
9667	  rhs_type = current.lhs_type;
9668	  --sp;
9669	  current = *sp;
9670	}
9671
9672      /* Undo the disabling of warnings done above.  */
9673      if (sp == disable_warnings_sp)
9674	{
9675	  disable_warnings_sp = NULL;
9676	  --c_inhibit_evaluation_warnings;
9677	}
9678
9679      if (warn_logical_not_paren
9680	  && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9681	  && current.lhs_type == TRUTH_NOT_EXPR
9682	  /* Avoid warning for !!x == y.  */
9683	  && (TREE_CODE (current.lhs) != NE_EXPR
9684	      || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9685	  && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9686	      || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9687		  /* Avoid warning for !b == y where b is boolean.  */
9688		  && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9689		      || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9690			  != BOOLEAN_TYPE))))
9691	  /* Avoid warning for !!b == y where b is boolean.  */
9692	  && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
9693		|| (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
9694		    && DECL_P (tree_strip_any_location_wrapper
9695					    (TREE_OPERAND (current.lhs, 0)))))
9696	      || TREE_TYPE (current.lhs) == NULL_TREE
9697	      || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9698	warn_logical_not_parentheses (current.loc, current.tree_type,
9699				      current.lhs, maybe_constant_value (rhs));
9700
9701      overload = NULL;
9702
9703      location_t combined_loc = make_location (current.loc,
9704					       current.lhs.get_start (),
9705					       rhs.get_finish ());
9706
9707      /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9708	 ERROR_MARK for everything that is not a binary expression.
9709	 This makes warn_about_parentheses miss some warnings that
9710	 involve unary operators.  For unary expressions we should
9711	 pass the correct tree_code unless the unary expression was
9712	 surrounded by parentheses.
9713      */
9714      if (no_toplevel_fold_p
9715	  && lookahead_prec <= current.prec
9716	  && sp == stack)
9717	{
9718	  if (current.lhs == error_mark_node || rhs == error_mark_node)
9719	    current.lhs = error_mark_node;
9720	  else
9721	    {
9722	      current.lhs.maybe_add_location_wrapper ();
9723	      rhs.maybe_add_location_wrapper ();
9724	      current.lhs
9725		= build_min (current.tree_type,
9726			     TREE_CODE_CLASS (current.tree_type)
9727			     == tcc_comparison
9728			     ? boolean_type_node : TREE_TYPE (current.lhs),
9729			     current.lhs.get_value (), rhs.get_value ());
9730	      SET_EXPR_LOCATION (current.lhs, combined_loc);
9731	    }
9732	}
9733      else
9734        {
9735	  op_location_t op_loc (current.loc, combined_loc);
9736	  current.lhs = build_x_binary_op (op_loc, current.tree_type,
9737                                           current.lhs, current.lhs_type,
9738                                           rhs, rhs_type, &overload,
9739                                           complain_flags (decltype_p));
9740          /* TODO: build_x_binary_op doesn't always honor the location.  */
9741          current.lhs.set_location (combined_loc);
9742        }
9743      current.lhs_type = current.tree_type;
9744
9745      /* If the binary operator required the use of an overloaded operator,
9746	 then this expression cannot be an integral constant-expression.
9747	 An overloaded operator can be used even if both operands are
9748	 otherwise permissible in an integral constant-expression if at
9749	 least one of the operands is of enumeration type.  */
9750
9751      if (overload
9752	  && cp_parser_non_integral_constant_expression (parser,
9753							 NIC_OVERLOADED))
9754	return error_mark_node;
9755    }
9756
9757  return current.lhs;
9758}
9759
9760static cp_expr
9761cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9762			     bool no_toplevel_fold_p,
9763			     enum cp_parser_prec prec,
9764			     cp_id_kind * pidk)
9765{
9766  return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9767				      /*decltype*/false, prec, pidk);
9768}
9769
9770/* Parse the `? expression : assignment-expression' part of a
9771   conditional-expression.  The LOGICAL_OR_EXPR is the
9772   logical-or-expression that started the conditional-expression.
9773   Returns a representation of the entire conditional-expression.
9774
9775   This routine is used by cp_parser_assignment_expression.
9776
9777     ? expression : assignment-expression
9778
9779   GNU Extensions:
9780
9781     ? : assignment-expression */
9782
9783static tree
9784cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9785{
9786  tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9787  cp_expr assignment_expr;
9788  struct cp_token *token;
9789  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9790
9791  /* Consume the `?' token.  */
9792  cp_lexer_consume_token (parser->lexer);
9793  token = cp_lexer_peek_token (parser->lexer);
9794  if (cp_parser_allow_gnu_extensions_p (parser)
9795      && token->type == CPP_COLON)
9796    {
9797      pedwarn (token->location, OPT_Wpedantic,
9798	       "ISO C++ does not allow %<?:%> with omitted middle operand");
9799      /* Implicit true clause.  */
9800      expr = NULL_TREE;
9801      c_inhibit_evaluation_warnings +=
9802	folded_logical_or_expr == truthvalue_true_node;
9803      warn_for_omitted_condop (token->location, logical_or_expr);
9804    }
9805  else
9806    {
9807      bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9808      parser->colon_corrects_to_scope_p = false;
9809      /* Parse the expression.  */
9810      c_inhibit_evaluation_warnings +=
9811	folded_logical_or_expr == truthvalue_false_node;
9812      expr = cp_parser_expression (parser);
9813      c_inhibit_evaluation_warnings +=
9814	((folded_logical_or_expr == truthvalue_true_node)
9815	 - (folded_logical_or_expr == truthvalue_false_node));
9816      parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9817    }
9818
9819  /* The next token should be a `:'.  */
9820  cp_parser_require (parser, CPP_COLON, RT_COLON);
9821  /* Parse the assignment-expression.  */
9822  assignment_expr = cp_parser_assignment_expression (parser);
9823  c_inhibit_evaluation_warnings -=
9824    folded_logical_or_expr == truthvalue_true_node;
9825
9826  /* Make a location:
9827       LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9828       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9829     with the caret at the "?", ranging from the start of
9830     the logical_or_expr to the end of the assignment_expr.  */
9831  loc = make_location (loc,
9832		       logical_or_expr.get_start (),
9833		       assignment_expr.get_finish ());
9834
9835  /* Build the conditional-expression.  */
9836  return build_x_conditional_expr (loc, logical_or_expr,
9837				   expr,
9838				   assignment_expr,
9839                                   tf_warning_or_error);
9840}
9841
9842/* Parse an assignment-expression.
9843
9844   assignment-expression:
9845     conditional-expression
9846     logical-or-expression assignment-operator assignment_expression
9847     throw-expression
9848     yield-expression
9849
9850   CAST_P is true if this expression is the target of a cast.
9851   DECLTYPE_P is true if this expression is the operand of decltype.
9852
9853   Returns a representation for the expression.  */
9854
9855static cp_expr
9856cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9857				 bool cast_p, bool decltype_p)
9858{
9859  cp_expr expr;
9860
9861  /* If the next token is the `throw' keyword, then we're looking at
9862     a throw-expression.  */
9863  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9864    expr = cp_parser_throw_expression (parser);
9865  /* If the next token is the `co_yield' keyword, then we're looking at
9866     a yield-expression.  */
9867  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
9868    expr = cp_parser_yield_expression (parser);
9869  /* Otherwise, it must be that we are looking at a
9870     logical-or-expression.  */
9871  else
9872    {
9873      /* Parse the binary expressions (logical-or-expression).  */
9874      expr = cp_parser_binary_expression (parser, cast_p, false,
9875					  decltype_p,
9876					  PREC_NOT_OPERATOR, pidk);
9877      /* If the next token is a `?' then we're actually looking at a
9878	 conditional-expression.  */
9879      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9880	return cp_parser_question_colon_clause (parser, expr);
9881      else
9882	{
9883	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9884
9885	  /* If it's an assignment-operator, we're using the second
9886	     production.  */
9887	  enum tree_code assignment_operator
9888	    = cp_parser_assignment_operator_opt (parser);
9889	  if (assignment_operator != ERROR_MARK)
9890	    {
9891	      bool non_constant_p;
9892
9893	      /* Parse the right-hand side of the assignment.  */
9894	      cp_expr rhs = cp_parser_initializer_clause (parser,
9895							  &non_constant_p);
9896
9897	      if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9898		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9899
9900	      /* An assignment may not appear in a
9901		 constant-expression.  */
9902	      if (cp_parser_non_integral_constant_expression (parser,
9903							      NIC_ASSIGNMENT))
9904		return error_mark_node;
9905	      /* Build the assignment expression.  Its default
9906		 location:
9907		   LHS = RHS
9908		   ~~~~^~~~~
9909		 is the location of the '=' token as the
9910		 caret, ranging from the start of the lhs to the
9911		 end of the rhs.  */
9912	      loc = make_location (loc,
9913				   expr.get_start (),
9914				   rhs.get_finish ());
9915	      expr = build_x_modify_expr (loc, expr,
9916					  assignment_operator,
9917					  rhs,
9918					  complain_flags (decltype_p));
9919              /* TODO: build_x_modify_expr doesn't honor the location,
9920                 so we must set it here.  */
9921              expr.set_location (loc);
9922	    }
9923	}
9924    }
9925
9926  return expr;
9927}
9928
9929/* Parse an (optional) assignment-operator.
9930
9931   assignment-operator: one of
9932     = *= /= %= += -= >>= <<= &= ^= |=
9933
9934   GNU Extension:
9935
9936   assignment-operator: one of
9937     <?= >?=
9938
9939   If the next token is an assignment operator, the corresponding tree
9940   code is returned, and the token is consumed.  For example, for
9941   `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
9942   NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
9943   TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
9944   operator, ERROR_MARK is returned.  */
9945
9946static enum tree_code
9947cp_parser_assignment_operator_opt (cp_parser* parser)
9948{
9949  enum tree_code op;
9950  cp_token *token;
9951
9952  /* Peek at the next token.  */
9953  token = cp_lexer_peek_token (parser->lexer);
9954
9955  switch (token->type)
9956    {
9957    case CPP_EQ:
9958      op = NOP_EXPR;
9959      break;
9960
9961    case CPP_MULT_EQ:
9962      op = MULT_EXPR;
9963      break;
9964
9965    case CPP_DIV_EQ:
9966      op = TRUNC_DIV_EXPR;
9967      break;
9968
9969    case CPP_MOD_EQ:
9970      op = TRUNC_MOD_EXPR;
9971      break;
9972
9973    case CPP_PLUS_EQ:
9974      op = PLUS_EXPR;
9975      break;
9976
9977    case CPP_MINUS_EQ:
9978      op = MINUS_EXPR;
9979      break;
9980
9981    case CPP_RSHIFT_EQ:
9982      op = RSHIFT_EXPR;
9983      break;
9984
9985    case CPP_LSHIFT_EQ:
9986      op = LSHIFT_EXPR;
9987      break;
9988
9989    case CPP_AND_EQ:
9990      op = BIT_AND_EXPR;
9991      break;
9992
9993    case CPP_XOR_EQ:
9994      op = BIT_XOR_EXPR;
9995      break;
9996
9997    case CPP_OR_EQ:
9998      op = BIT_IOR_EXPR;
9999      break;
10000
10001    default:
10002      /* Nothing else is an assignment operator.  */
10003      op = ERROR_MARK;
10004    }
10005
10006  /* An operator followed by ... is a fold-expression, handled elsewhere.  */
10007  if (op != ERROR_MARK
10008      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10009    op = ERROR_MARK;
10010
10011  /* If it was an assignment operator, consume it.  */
10012  if (op != ERROR_MARK)
10013    cp_lexer_consume_token (parser->lexer);
10014
10015  return op;
10016}
10017
10018/* Parse an expression.
10019
10020   expression:
10021     assignment-expression
10022     expression , assignment-expression
10023
10024   CAST_P is true if this expression is the target of a cast.
10025   DECLTYPE_P is true if this expression is the immediate operand of decltype,
10026     except possibly parenthesized or on the RHS of a comma (N3276).
10027   WARN_COMMA_P is true if a comma should be diagnosed.
10028
10029   Returns a representation of the expression.  */
10030
10031static cp_expr
10032cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10033		      bool cast_p, bool decltype_p, bool warn_comma_p)
10034{
10035  cp_expr expression = NULL_TREE;
10036  location_t loc = UNKNOWN_LOCATION;
10037
10038  while (true)
10039    {
10040      cp_expr assignment_expression;
10041
10042      /* Parse the next assignment-expression.  */
10043      assignment_expression
10044	= cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10045
10046      /* We don't create a temporary for a call that is the immediate operand
10047	 of decltype or on the RHS of a comma.  But when we see a comma, we
10048	 need to create a temporary for a call on the LHS.  */
10049      if (decltype_p && !processing_template_decl
10050	  && TREE_CODE (assignment_expression) == CALL_EXPR
10051	  && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10052	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10053	assignment_expression
10054	  = build_cplus_new (TREE_TYPE (assignment_expression),
10055			     assignment_expression, tf_warning_or_error);
10056
10057      /* If this is the first assignment-expression, we can just
10058	 save it away.  */
10059      if (!expression)
10060	expression = assignment_expression;
10061      else
10062	{
10063	  /* Create a location with caret at the comma, ranging
10064	     from the start of the LHS to the end of the RHS.  */
10065	  loc = make_location (loc,
10066			       expression.get_start (),
10067			       assignment_expression.get_finish ());
10068	  expression = build_x_compound_expr (loc, expression,
10069					      assignment_expression,
10070					      complain_flags (decltype_p));
10071	  expression.set_location (loc);
10072	}
10073      /* If the next token is not a comma, or we're in a fold-expression, then
10074	 we are done with the expression.  */
10075      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10076	  || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10077	break;
10078      /* Consume the `,'.  */
10079      loc = cp_lexer_peek_token (parser->lexer)->location;
10080      if (warn_comma_p)
10081	{
10082	  /* [depr.comma.subscript]: A comma expression appearing as
10083	     the expr-or-braced-init-list of a subscripting expression
10084	     is deprecated.  A parenthesized comma expression is not
10085	     deprecated.  */
10086	  warning_at (loc, OPT_Wcomma_subscript,
10087		      "top-level comma expression in array subscript "
10088		      "is deprecated");
10089	  warn_comma_p = false;
10090	}
10091      cp_lexer_consume_token (parser->lexer);
10092      /* A comma operator cannot appear in a constant-expression.  */
10093      if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10094	expression = error_mark_node;
10095    }
10096
10097  return expression;
10098}
10099
10100/* Parse a constant-expression.
10101
10102   constant-expression:
10103     conditional-expression
10104
10105  If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10106  accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
10107  constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
10108  is false, NON_CONSTANT_P should be NULL.  If STRICT_P is true,
10109  only parse a conditional-expression, otherwise parse an
10110  assignment-expression.  See below for rationale.  */
10111
10112static cp_expr
10113cp_parser_constant_expression (cp_parser* parser,
10114			       bool allow_non_constant_p,
10115			       bool *non_constant_p,
10116			       bool strict_p)
10117{
10118  bool saved_integral_constant_expression_p;
10119  bool saved_allow_non_integral_constant_expression_p;
10120  bool saved_non_integral_constant_expression_p;
10121  cp_expr expression;
10122
10123  /* It might seem that we could simply parse the
10124     conditional-expression, and then check to see if it were
10125     TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
10126     one that the compiler can figure out is constant, possibly after
10127     doing some simplifications or optimizations.  The standard has a
10128     precise definition of constant-expression, and we must honor
10129     that, even though it is somewhat more restrictive.
10130
10131     For example:
10132
10133       int i[(2, 3)];
10134
10135     is not a legal declaration, because `(2, 3)' is not a
10136     constant-expression.  The `,' operator is forbidden in a
10137     constant-expression.  However, GCC's constant-folding machinery
10138     will fold this operation to an INTEGER_CST for `3'.  */
10139
10140  /* Save the old settings.  */
10141  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10142  saved_allow_non_integral_constant_expression_p
10143    = parser->allow_non_integral_constant_expression_p;
10144  saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10145  /* We are now parsing a constant-expression.  */
10146  parser->integral_constant_expression_p = true;
10147  parser->allow_non_integral_constant_expression_p
10148    = (allow_non_constant_p || cxx_dialect >= cxx11);
10149  parser->non_integral_constant_expression_p = false;
10150  /* Although the grammar says "conditional-expression", when not STRICT_P,
10151     we parse an "assignment-expression", which also permits
10152     "throw-expression" and the use of assignment operators.  In the case
10153     that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10154     otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
10155     actually essential that we look for an assignment-expression.
10156     For example, cp_parser_initializer_clauses uses this function to
10157     determine whether a particular assignment-expression is in fact
10158     constant.  */
10159  if (strict_p)
10160    {
10161      /* Parse the binary expressions (logical-or-expression).  */
10162      expression = cp_parser_binary_expression (parser, false, false, false,
10163						PREC_NOT_OPERATOR, NULL);
10164      /* If the next token is a `?' then we're actually looking at
10165	 a conditional-expression; otherwise we're done.  */
10166      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10167	expression = cp_parser_question_colon_clause (parser, expression);
10168    }
10169  else
10170    expression = cp_parser_assignment_expression (parser);
10171  /* Restore the old settings.  */
10172  parser->integral_constant_expression_p
10173    = saved_integral_constant_expression_p;
10174  parser->allow_non_integral_constant_expression_p
10175    = saved_allow_non_integral_constant_expression_p;
10176  if (cxx_dialect >= cxx11)
10177    {
10178      /* Require an rvalue constant expression here; that's what our
10179	 callers expect.  Reference constant expressions are handled
10180	 separately in e.g. cp_parser_template_argument.  */
10181      tree decay = expression;
10182      if (TREE_TYPE (expression)
10183	  && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10184	decay = build_address (expression);
10185      bool is_const = potential_rvalue_constant_expression (decay);
10186      parser->non_integral_constant_expression_p = !is_const;
10187      if (!is_const && !allow_non_constant_p)
10188	require_potential_rvalue_constant_expression (decay);
10189    }
10190  if (allow_non_constant_p)
10191    *non_constant_p = parser->non_integral_constant_expression_p;
10192  parser->non_integral_constant_expression_p
10193    = saved_non_integral_constant_expression_p;
10194
10195  return expression;
10196}
10197
10198/* Parse __builtin_offsetof.
10199
10200   offsetof-expression:
10201     "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10202
10203   offsetof-member-designator:
10204     id-expression
10205     | offsetof-member-designator "." id-expression
10206     | offsetof-member-designator "[" expression "]"
10207     | offsetof-member-designator "->" id-expression  */
10208
10209static cp_expr
10210cp_parser_builtin_offsetof (cp_parser *parser)
10211{
10212  int save_ice_p, save_non_ice_p;
10213  tree type;
10214  cp_expr expr;
10215  cp_id_kind dummy;
10216  cp_token *token;
10217  location_t finish_loc;
10218
10219  /* We're about to accept non-integral-constant things, but will
10220     definitely yield an integral constant expression.  Save and
10221     restore these values around our local parsing.  */
10222  save_ice_p = parser->integral_constant_expression_p;
10223  save_non_ice_p = parser->non_integral_constant_expression_p;
10224
10225  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10226
10227  /* Consume the "__builtin_offsetof" token.  */
10228  cp_lexer_consume_token (parser->lexer);
10229  /* Consume the opening `('.  */
10230  matching_parens parens;
10231  parens.require_open (parser);
10232  /* Parse the type-id.  */
10233  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10234  {
10235    const char *saved_message = parser->type_definition_forbidden_message;
10236    parser->type_definition_forbidden_message
10237      = G_("types may not be defined within %<__builtin_offsetof%>");
10238    type = cp_parser_type_id (parser);
10239    parser->type_definition_forbidden_message = saved_message;
10240  }
10241  /* Look for the `,'.  */
10242  cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10243  token = cp_lexer_peek_token (parser->lexer);
10244
10245  /* Build the (type *)null that begins the traditional offsetof macro.  */
10246  tree object_ptr
10247    = build_static_cast (input_location, build_pointer_type (type),
10248			 null_pointer_node, tf_warning_or_error);
10249
10250  /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
10251  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10252						 true, &dummy, token->location);
10253  while (true)
10254    {
10255      token = cp_lexer_peek_token (parser->lexer);
10256      switch (token->type)
10257	{
10258	case CPP_OPEN_SQUARE:
10259	  /* offsetof-member-designator "[" expression "]" */
10260	  expr = cp_parser_postfix_open_square_expression (parser, expr,
10261							   true, false);
10262	  break;
10263
10264	case CPP_DEREF:
10265	  /* offsetof-member-designator "->" identifier */
10266	  expr = grok_array_decl (token->location, expr,
10267				  integer_zero_node, false);
10268	  /* FALLTHRU */
10269
10270	case CPP_DOT:
10271	  /* offsetof-member-designator "." identifier */
10272	  cp_lexer_consume_token (parser->lexer);
10273	  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10274							 expr, true, &dummy,
10275							 token->location);
10276	  break;
10277
10278	case CPP_CLOSE_PAREN:
10279	  /* Consume the ")" token.  */
10280	  finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10281	  cp_lexer_consume_token (parser->lexer);
10282	  goto success;
10283
10284	default:
10285	  /* Error.  We know the following require will fail, but
10286	     that gives the proper error message.  */
10287	  parens.require_close (parser);
10288	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10289	  expr = error_mark_node;
10290	  goto failure;
10291	}
10292    }
10293
10294 success:
10295  /* Make a location of the form:
10296       __builtin_offsetof (struct s, f)
10297       ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10298     with caret at the type-id, ranging from the start of the
10299     "_builtin_offsetof" token to the close paren.  */
10300  loc = make_location (loc, start_loc, finish_loc);
10301  /* The result will be an INTEGER_CST, so we need to explicitly
10302     preserve the location.  */
10303  expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10304
10305 failure:
10306  parser->integral_constant_expression_p = save_ice_p;
10307  parser->non_integral_constant_expression_p = save_non_ice_p;
10308
10309  expr = expr.maybe_add_location_wrapper ();
10310  return expr;
10311}
10312
10313/* Parse a trait expression.
10314
10315   Returns a representation of the expression, the underlying type
10316   of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
10317
10318static cp_expr
10319cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10320{
10321  cp_trait_kind kind;
10322  tree type1, type2 = NULL_TREE;
10323  bool binary = false;
10324  bool variadic = false;
10325
10326  switch (keyword)
10327    {
10328    case RID_HAS_NOTHROW_ASSIGN:
10329      kind = CPTK_HAS_NOTHROW_ASSIGN;
10330      break;
10331    case RID_HAS_NOTHROW_CONSTRUCTOR:
10332      kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10333      break;
10334    case RID_HAS_NOTHROW_COPY:
10335      kind = CPTK_HAS_NOTHROW_COPY;
10336      break;
10337    case RID_HAS_TRIVIAL_ASSIGN:
10338      kind = CPTK_HAS_TRIVIAL_ASSIGN;
10339      break;
10340    case RID_HAS_TRIVIAL_CONSTRUCTOR:
10341      kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10342      break;
10343    case RID_HAS_TRIVIAL_COPY:
10344      kind = CPTK_HAS_TRIVIAL_COPY;
10345      break;
10346    case RID_HAS_TRIVIAL_DESTRUCTOR:
10347      kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10348      break;
10349    case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10350      kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10351      break;
10352    case RID_HAS_VIRTUAL_DESTRUCTOR:
10353      kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10354      break;
10355    case RID_IS_ABSTRACT:
10356      kind = CPTK_IS_ABSTRACT;
10357      break;
10358    case RID_IS_AGGREGATE:
10359      kind = CPTK_IS_AGGREGATE;
10360      break;
10361    case RID_IS_BASE_OF:
10362      kind = CPTK_IS_BASE_OF;
10363      binary = true;
10364      break;
10365    case RID_IS_CLASS:
10366      kind = CPTK_IS_CLASS;
10367      break;
10368    case RID_IS_EMPTY:
10369      kind = CPTK_IS_EMPTY;
10370      break;
10371    case RID_IS_ENUM:
10372      kind = CPTK_IS_ENUM;
10373      break;
10374    case RID_IS_FINAL:
10375      kind = CPTK_IS_FINAL;
10376      break;
10377    case RID_IS_LITERAL_TYPE:
10378      kind = CPTK_IS_LITERAL_TYPE;
10379      break;
10380    case RID_IS_POD:
10381      kind = CPTK_IS_POD;
10382      break;
10383    case RID_IS_POLYMORPHIC:
10384      kind = CPTK_IS_POLYMORPHIC;
10385      break;
10386    case RID_IS_SAME_AS:
10387      kind = CPTK_IS_SAME_AS;
10388      binary = true;
10389      break;
10390    case RID_IS_STD_LAYOUT:
10391      kind = CPTK_IS_STD_LAYOUT;
10392      break;
10393    case RID_IS_TRIVIAL:
10394      kind = CPTK_IS_TRIVIAL;
10395      break;
10396    case RID_IS_TRIVIALLY_ASSIGNABLE:
10397      kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10398      binary = true;
10399      break;
10400    case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10401      kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10402      variadic = true;
10403      break;
10404    case RID_IS_TRIVIALLY_COPYABLE:
10405      kind = CPTK_IS_TRIVIALLY_COPYABLE;
10406      break;
10407    case RID_IS_UNION:
10408      kind = CPTK_IS_UNION;
10409      break;
10410    case RID_UNDERLYING_TYPE:
10411      kind = CPTK_UNDERLYING_TYPE;
10412      break;
10413    case RID_BASES:
10414      kind = CPTK_BASES;
10415      break;
10416    case RID_DIRECT_BASES:
10417      kind = CPTK_DIRECT_BASES;
10418      break;
10419    case RID_IS_ASSIGNABLE:
10420      kind = CPTK_IS_ASSIGNABLE;
10421      binary = true;
10422      break;
10423    case RID_IS_CONSTRUCTIBLE:
10424      kind = CPTK_IS_CONSTRUCTIBLE;
10425      variadic = true;
10426      break;
10427    default:
10428      gcc_unreachable ();
10429    }
10430
10431  /* Get location of initial token.  */
10432  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10433
10434  /* Consume the token.  */
10435  cp_lexer_consume_token (parser->lexer);
10436
10437  matching_parens parens;
10438  parens.require_open (parser);
10439
10440  {
10441    type_id_in_expr_sentinel s (parser);
10442    type1 = cp_parser_type_id (parser);
10443  }
10444
10445  if (type1 == error_mark_node)
10446    return error_mark_node;
10447
10448  if (binary)
10449    {
10450      cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10451
10452      {
10453	type_id_in_expr_sentinel s (parser);
10454	type2 = cp_parser_type_id (parser);
10455      }
10456
10457      if (type2 == error_mark_node)
10458	return error_mark_node;
10459    }
10460  else if (variadic)
10461    {
10462      while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10463	{
10464	  cp_lexer_consume_token (parser->lexer);
10465	  tree elt = cp_parser_type_id (parser);
10466	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10467	    {
10468	      cp_lexer_consume_token (parser->lexer);
10469	      elt = make_pack_expansion (elt);
10470	    }
10471	  if (elt == error_mark_node)
10472	    return error_mark_node;
10473	  type2 = tree_cons (NULL_TREE, elt, type2);
10474	}
10475    }
10476
10477  location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10478  parens.require_close (parser);
10479
10480  /* Construct a location of the form:
10481       __is_trivially_copyable(_Tp)
10482       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10483     with start == caret, finishing at the close-paren.  */
10484  location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10485
10486  /* Complete the trait expression, which may mean either processing
10487     the trait expr now or saving it for template instantiation.  */
10488  switch (kind)
10489    {
10490    case CPTK_UNDERLYING_TYPE:
10491      return cp_expr (finish_underlying_type (type1), trait_loc);
10492    case CPTK_BASES:
10493      return cp_expr (finish_bases (type1, false), trait_loc);
10494    case CPTK_DIRECT_BASES:
10495      return cp_expr (finish_bases (type1, true), trait_loc);
10496    default:
10497      return finish_trait_expr (trait_loc, kind, type1, type2);
10498    }
10499}
10500
10501/* Parse a lambda expression.
10502
10503   lambda-expression:
10504     lambda-introducer lambda-declarator [opt] compound-statement
10505
10506   Returns a representation of the expression.  */
10507
10508static cp_expr
10509cp_parser_lambda_expression (cp_parser* parser)
10510{
10511  tree lambda_expr = build_lambda_expr ();
10512  tree type;
10513  bool ok = true;
10514  cp_token *token = cp_lexer_peek_token (parser->lexer);
10515  cp_token_position start = 0;
10516
10517  LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10518
10519  if (cxx_dialect >= cxx2a)
10520    /* C++20 allows lambdas in unevaluated context.  */;
10521  else if (cp_unevaluated_operand)
10522    {
10523      if (!token->error_reported)
10524	{
10525	  error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10526		    "lambda-expression in unevaluated context"
10527		    " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10528	  token->error_reported = true;
10529	}
10530      ok = false;
10531    }
10532  else if (parser->in_template_argument_list_p || processing_template_parmlist)
10533    {
10534      if (!token->error_reported)
10535	{
10536	  error_at (token->location, "lambda-expression in template-argument"
10537		    " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10538	  token->error_reported = true;
10539	}
10540      ok = false;
10541    }
10542
10543  /* We may be in the middle of deferred access check.  Disable
10544     it now.  */
10545  push_deferring_access_checks (dk_no_deferred);
10546
10547  cp_parser_lambda_introducer (parser, lambda_expr);
10548  if (cp_parser_error_occurred (parser))
10549    return error_mark_node;
10550
10551  type = begin_lambda_type (lambda_expr);
10552  if (type == error_mark_node)
10553    return error_mark_node;
10554
10555  record_lambda_scope (lambda_expr);
10556
10557  /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
10558  determine_visibility (TYPE_NAME (type));
10559
10560  /* Now that we've started the type, add the capture fields for any
10561     explicit captures.  */
10562  register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10563
10564  {
10565    /* Inside the class, surrounding template-parameter-lists do not apply.  */
10566    unsigned int saved_num_template_parameter_lists
10567        = parser->num_template_parameter_lists;
10568    unsigned char in_statement = parser->in_statement;
10569    bool in_switch_statement_p = parser->in_switch_statement_p;
10570    bool fully_implicit_function_template_p
10571        = parser->fully_implicit_function_template_p;
10572    tree implicit_template_parms = parser->implicit_template_parms;
10573    cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10574    bool auto_is_implicit_function_template_parm_p
10575        = parser->auto_is_implicit_function_template_parm_p;
10576
10577    parser->num_template_parameter_lists = 0;
10578    parser->in_statement = 0;
10579    parser->in_switch_statement_p = false;
10580    parser->fully_implicit_function_template_p = false;
10581    parser->implicit_template_parms = 0;
10582    parser->implicit_template_scope = 0;
10583    parser->auto_is_implicit_function_template_parm_p = false;
10584
10585    /* The body of a lambda in a discarded statement is not discarded.  */
10586    bool discarded = in_discarded_stmt;
10587    in_discarded_stmt = 0;
10588
10589    /* By virtue of defining a local class, a lambda expression has access to
10590       the private variables of enclosing classes.  */
10591
10592    if (cp_parser_start_tentative_firewall (parser))
10593      start = token;
10594
10595    ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10596
10597    if (ok && cp_parser_error_occurred (parser))
10598      ok = false;
10599
10600    if (ok)
10601      {
10602	cp_parser_lambda_body (parser, lambda_expr);
10603      }
10604    else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10605      {
10606	if (cp_parser_skip_to_closing_brace (parser))
10607	  cp_lexer_consume_token (parser->lexer);
10608      }
10609
10610    /* The capture list was built up in reverse order; fix that now.  */
10611    LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10612      = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10613
10614    if (ok)
10615      maybe_add_lambda_conv_op (type);
10616
10617    finish_struct (type, /*attributes=*/NULL_TREE);
10618
10619    in_discarded_stmt = discarded;
10620
10621    parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10622    parser->in_statement = in_statement;
10623    parser->in_switch_statement_p = in_switch_statement_p;
10624    parser->fully_implicit_function_template_p
10625	= fully_implicit_function_template_p;
10626    parser->implicit_template_parms = implicit_template_parms;
10627    parser->implicit_template_scope = implicit_template_scope;
10628    parser->auto_is_implicit_function_template_parm_p
10629	= auto_is_implicit_function_template_parm_p;
10630  }
10631
10632  /* This field is only used during parsing of the lambda.  */
10633  LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10634
10635  /* This lambda shouldn't have any proxies left at this point.  */
10636  gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10637  /* And now that we're done, push proxies for an enclosing lambda.  */
10638  insert_pending_capture_proxies ();
10639
10640  /* Update the lambda expression to a range.  */
10641  LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10642						      token->location,
10643						      parser->lexer);
10644
10645  if (ok)
10646    lambda_expr = build_lambda_object (lambda_expr);
10647  else
10648    lambda_expr = error_mark_node;
10649
10650  cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10651
10652  pop_deferring_access_checks ();
10653
10654  return lambda_expr;
10655}
10656
10657/* Parse the beginning of a lambda expression.
10658
10659   lambda-introducer:
10660     [ lambda-capture [opt] ]
10661
10662   LAMBDA_EXPR is the current representation of the lambda expression.  */
10663
10664static void
10665cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10666{
10667  /* Need commas after the first capture.  */
10668  bool first = true;
10669
10670  /* Eat the leading `['.  */
10671  cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10672
10673  /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
10674  if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10675      && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
10676      && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
10677      && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10678    LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10679  else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10680    LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10681
10682  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10683    {
10684      cp_lexer_consume_token (parser->lexer);
10685      first = false;
10686
10687      if (!(at_function_scope_p () || parsing_nsdmi ()))
10688	error ("non-local lambda expression cannot have a capture-default");
10689    }
10690
10691  hash_set<tree, true> ids;
10692  tree first_capture_id = NULL_TREE;
10693  while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10694    {
10695      cp_token* capture_token;
10696      tree capture_id;
10697      tree capture_init_expr;
10698      cp_id_kind idk = CP_ID_KIND_NONE;
10699      bool explicit_init_p = false;
10700
10701      enum capture_kind_type
10702      {
10703	BY_COPY,
10704	BY_REFERENCE
10705      };
10706      enum capture_kind_type capture_kind = BY_COPY;
10707
10708      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10709	{
10710	  error ("expected end of capture-list");
10711	  return;
10712	}
10713
10714      if (first)
10715	first = false;
10716      else
10717	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10718
10719      /* Possibly capture `this'.  */
10720      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10721	{
10722	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10723	  if (cxx_dialect < cxx2a
10724	      && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10725	    pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10726		     "with by-copy capture default");
10727	  cp_lexer_consume_token (parser->lexer);
10728	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10729	    pedwarn (input_location, 0,
10730		     "already captured %qD in lambda expression",
10731		     this_identifier);
10732	  else
10733	    add_capture (lambda_expr, /*id=*/this_identifier,
10734			 /*initializer=*/finish_this_expr (),
10735			 /*by_reference_p=*/true, explicit_init_p);
10736	  continue;
10737	}
10738
10739      /* Possibly capture `*this'.  */
10740      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10741	  && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10742	{
10743	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10744	  if (cxx_dialect < cxx17)
10745	    pedwarn (loc, 0, "%<*this%> capture only available with "
10746			     "%<-std=c++17%> or %<-std=gnu++17%>");
10747	  cp_lexer_consume_token (parser->lexer);
10748	  cp_lexer_consume_token (parser->lexer);
10749	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10750	    pedwarn (input_location, 0,
10751		     "already captured %qD in lambda expression",
10752		     this_identifier);
10753	  else
10754	    add_capture (lambda_expr, /*id=*/this_identifier,
10755			 /*initializer=*/finish_this_expr (),
10756			 /*by_reference_p=*/false, explicit_init_p);
10757	  continue;
10758	}
10759
10760      /* But reject `&this'.  */
10761      if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10762	  && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10763	{
10764	  error_at (cp_lexer_peek_token (parser->lexer)->location,
10765		    "%<this%> cannot be captured by reference");
10766	  cp_lexer_consume_token (parser->lexer);
10767	  cp_lexer_consume_token (parser->lexer);
10768	  continue;
10769	}
10770
10771      /* Remember whether we want to capture as a reference or not.  */
10772      if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10773	{
10774	  capture_kind = BY_REFERENCE;
10775	  cp_lexer_consume_token (parser->lexer);
10776	}
10777
10778      bool init_pack_expansion = false;
10779      location_t ellipsis_loc = UNKNOWN_LOCATION;
10780      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10781	{
10782	  ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10783	  if (cxx_dialect < cxx2a)
10784	    pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10785		     "%<-std=c++2a%> or %<-std=gnu++2a%>");
10786	  cp_lexer_consume_token (parser->lexer);
10787	  init_pack_expansion = true;
10788	}
10789
10790      /* Early C++20 drafts had ...& instead of &...; be forgiving.  */
10791      if (init_pack_expansion && capture_kind != BY_REFERENCE
10792	  && cp_lexer_next_token_is (parser->lexer, CPP_AND))
10793	{
10794	  pedwarn (cp_lexer_peek_token (parser->lexer)->location,
10795		   0, "%<&%> should come before %<...%>");
10796	  capture_kind = BY_REFERENCE;
10797	  cp_lexer_consume_token (parser->lexer);
10798	}
10799
10800      /* Get the identifier.  */
10801      capture_token = cp_lexer_peek_token (parser->lexer);
10802      capture_id = cp_parser_identifier (parser);
10803
10804      if (capture_id == error_mark_node)
10805	/* Would be nice to have a cp_parser_skip_to_closing_x for general
10806           delimiters, but I modified this to stop on unnested ']' as well.  It
10807           was already changed to stop on unnested '}', so the
10808           "closing_parenthesis" name is no more misleading with my change.  */
10809	{
10810	  cp_parser_skip_to_closing_parenthesis (parser,
10811						 /*recovering=*/true,
10812						 /*or_comma=*/true,
10813						 /*consume_paren=*/true);
10814	  break;
10815	}
10816
10817      /* Find the initializer for this capture.  */
10818      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10819	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10820	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10821	{
10822	  bool direct, non_constant;
10823	  /* An explicit initializer exists.  */
10824	  if (cxx_dialect < cxx14)
10825	    pedwarn (input_location, 0,
10826		     "lambda capture initializers "
10827		     "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10828	  capture_init_expr = cp_parser_initializer (parser, &direct,
10829						     &non_constant, true);
10830	  explicit_init_p = true;
10831	  if (capture_init_expr == NULL_TREE)
10832	    {
10833	      error ("empty initializer for lambda init-capture");
10834	      capture_init_expr = error_mark_node;
10835	    }
10836	  if (init_pack_expansion)
10837	    capture_init_expr = make_pack_expansion (capture_init_expr);
10838	}
10839      else
10840	{
10841	  const char* error_msg;
10842
10843	  /* Turn the identifier into an id-expression.  */
10844	  capture_init_expr
10845	    = cp_parser_lookup_name_simple (parser, capture_id,
10846					    capture_token->location);
10847
10848	  if (capture_init_expr == error_mark_node)
10849	    {
10850	      unqualified_name_lookup_error (capture_id);
10851	      continue;
10852	    }
10853	  else if (!VAR_P (capture_init_expr)
10854		   && TREE_CODE (capture_init_expr) != PARM_DECL)
10855	    {
10856	      error_at (capture_token->location,
10857			"capture of non-variable %qE",
10858			capture_init_expr);
10859	      if (DECL_P (capture_init_expr))
10860		inform (DECL_SOURCE_LOCATION (capture_init_expr),
10861			"%q#D declared here", capture_init_expr);
10862	      continue;
10863	    }
10864	  if (VAR_P (capture_init_expr)
10865	      && decl_storage_duration (capture_init_expr) != dk_auto)
10866	    {
10867	      if (pedwarn (capture_token->location, 0, "capture of variable "
10868			   "%qD with non-automatic storage duration",
10869			   capture_init_expr))
10870		inform (DECL_SOURCE_LOCATION (capture_init_expr),
10871			"%q#D declared here", capture_init_expr);
10872	      continue;
10873	    }
10874
10875	  capture_init_expr
10876            = finish_id_expression
10877                (capture_id,
10878		 capture_init_expr,
10879                 parser->scope,
10880                 &idk,
10881                 /*integral_constant_expression_p=*/false,
10882                 /*allow_non_integral_constant_expression_p=*/false,
10883                 /*non_integral_constant_expression_p=*/NULL,
10884                 /*template_p=*/false,
10885                 /*done=*/true,
10886                 /*address_p=*/false,
10887                 /*template_arg_p=*/false,
10888                 &error_msg,
10889                 capture_token->location);
10890
10891	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10892	    {
10893	      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10894	      cp_lexer_consume_token (parser->lexer);
10895	      capture_init_expr = make_pack_expansion (capture_init_expr);
10896	      if (init_pack_expansion)
10897		{
10898		  /* If what follows is an initializer, the second '...' is
10899		     invalid.  But for cases like [...xs...], the first one
10900		     is invalid.  */
10901		  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10902		      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10903		      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10904		    ellipsis_loc = loc;
10905		  error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10906		  continue;
10907		}
10908	    }
10909	}
10910
10911      if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10912	  && !explicit_init_p)
10913	{
10914	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10915	      && capture_kind == BY_COPY)
10916	    pedwarn (capture_token->location, 0, "explicit by-copy capture "
10917		     "of %qD redundant with by-copy capture default",
10918		     capture_id);
10919	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10920	      && capture_kind == BY_REFERENCE)
10921	    pedwarn (capture_token->location, 0, "explicit by-reference "
10922		     "capture of %qD redundant with by-reference capture "
10923		     "default", capture_id);
10924	}
10925
10926      /* Check for duplicates.
10927	 Optimize for the zero or one explicit captures cases and only create
10928	 the hash_set after adding second capture.  */
10929      bool found = false;
10930      if (!ids.is_empty ())
10931	found = ids.add (capture_id);
10932      else if (first_capture_id == NULL_TREE)
10933	first_capture_id = capture_id;
10934      else if (capture_id == first_capture_id)
10935	found = true;
10936      else
10937	{
10938	  ids.add (first_capture_id);
10939	  ids.add (capture_id);
10940	}
10941      if (found)
10942	pedwarn (input_location, 0,
10943		 "already captured %qD in lambda expression", capture_id);
10944      else
10945	add_capture (lambda_expr, capture_id, capture_init_expr,
10946		     /*by_reference_p=*/capture_kind == BY_REFERENCE,
10947		     explicit_init_p);
10948
10949      /* If there is any qualification still in effect, clear it
10950	 now; we will be starting fresh with the next capture.  */
10951      parser->scope = NULL_TREE;
10952      parser->qualifying_scope = NULL_TREE;
10953      parser->object_scope = NULL_TREE;
10954    }
10955
10956  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10957}
10958
10959/* Parse the (optional) middle of a lambda expression.
10960
10961   lambda-declarator:
10962     < template-parameter-list [opt] >
10963       requires-clause [opt]
10964     ( parameter-declaration-clause [opt] )
10965       attribute-specifier [opt]
10966       decl-specifier-seq [opt]
10967       exception-specification [opt]
10968       lambda-return-type-clause [opt]
10969       requires-clause [opt]
10970
10971   LAMBDA_EXPR is the current representation of the lambda expression.  */
10972
10973static bool
10974cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10975{
10976  /* 5.1.1.4 of the standard says:
10977       If a lambda-expression does not include a lambda-declarator, it is as if
10978       the lambda-declarator were ().
10979     This means an empty parameter list, no attributes, and no exception
10980     specification.  */
10981  tree param_list = void_list_node;
10982  tree std_attrs = NULL_TREE;
10983  tree gnu_attrs = NULL_TREE;
10984  tree exception_spec = NULL_TREE;
10985  tree template_param_list = NULL_TREE;
10986  tree tx_qual = NULL_TREE;
10987  tree return_type = NULL_TREE;
10988  tree trailing_requires_clause = NULL_TREE;
10989  cp_decl_specifier_seq lambda_specs;
10990  clear_decl_specs (&lambda_specs);
10991  /* A lambda op() is const unless explicitly 'mutable'.  */
10992  cp_cv_quals quals = TYPE_QUAL_CONST;
10993
10994  /* The template-parameter-list is optional, but must begin with
10995     an opening angle if present.  */
10996  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10997    {
10998      if (cxx_dialect < cxx14)
10999	pedwarn (parser->lexer->next_token->location, 0,
11000		 "lambda templates are only available with "
11001		 "%<-std=c++14%> or %<-std=gnu++14%>");
11002      else if (cxx_dialect < cxx2a)
11003	pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
11004		 "lambda templates are only available with "
11005		 "%<-std=c++2a%> or %<-std=gnu++2a%>");
11006
11007      cp_lexer_consume_token (parser->lexer);
11008
11009      template_param_list = cp_parser_template_parameter_list (parser);
11010      cp_parser_skip_to_end_of_template_parameter_list (parser);
11011
11012      /* We may have a constrained generic lambda; parse the requires-clause
11013	 immediately after the template-parameter-list and combine with any
11014	 shorthand constraints present.  */
11015      tree dreqs = cp_parser_requires_clause_opt (parser, true);
11016      if (flag_concepts)
11017	{
11018	  tree reqs = get_shorthand_constraints (current_template_parms);
11019	  if (dreqs)
11020	    reqs = combine_constraint_expressions (reqs, dreqs);
11021	  TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11022	}
11023
11024      /* We just processed one more parameter list.  */
11025      ++parser->num_template_parameter_lists;
11026    }
11027
11028  /* Committee discussion supports allowing attributes here.  */
11029  lambda_specs.attributes = cp_parser_attributes_opt (parser);
11030
11031  /* The parameter-declaration-clause is optional (unless
11032     template-parameter-list was given), but must begin with an
11033     opening parenthesis if present.  */
11034  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11035    {
11036      bool is_consteval = false;
11037      /* For C++20, before parsing the parameter list check if there is
11038	 a consteval specifier in the corresponding decl-specifier-seq.  */
11039      if (cxx_dialect >= cxx2a)
11040	{
11041	  for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11042	       cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11043	    {
11044	      if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11045		  == RID_CONSTEVAL)
11046		{
11047		  is_consteval = true;
11048		  break;
11049		}
11050	    }
11051	}
11052
11053      matching_parens parens;
11054      parens.consume_open (parser);
11055
11056      begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11057
11058      if (is_consteval)
11059	current_binding_level->immediate_fn_ctx_p = true;
11060
11061      /* Parse parameters.  */
11062      param_list
11063	= cp_parser_parameter_declaration_clause
11064	    (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11065
11066      /* Default arguments shall not be specified in the
11067	 parameter-declaration-clause of a lambda-declarator.  */
11068      if (cxx_dialect < cxx14)
11069	for (tree t = param_list; t; t = TREE_CHAIN (t))
11070	  if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11071	    pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
11072		     "default argument specified for lambda parameter");
11073
11074      parens.require_close (parser);
11075
11076      /* In the decl-specifier-seq of the lambda-declarator, each
11077	 decl-specifier shall either be mutable or constexpr.  */
11078      int declares_class_or_enum;
11079      if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
11080	  && !cp_next_tokens_can_be_gnu_attribute_p (parser))
11081	cp_parser_decl_specifier_seq (parser,
11082				      CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11083				      &lambda_specs, &declares_class_or_enum);
11084      if (lambda_specs.storage_class == sc_mutable)
11085	{
11086	  LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11087	  quals = TYPE_UNQUALIFIED;
11088	  if (lambda_specs.conflicting_specifiers_p)
11089	    error_at (lambda_specs.locations[ds_storage_class],
11090		      "duplicate %<mutable%>");
11091	}
11092
11093      tx_qual = cp_parser_tx_qualifier_opt (parser);
11094
11095      /* Parse optional exception specification.  */
11096      exception_spec
11097	= cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE,
11098						 quals);
11099
11100      /* GCC 8 accepted attributes here, and this is the place for standard
11101	 C++11 attributes that appertain to the function type.  */
11102      if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11103	gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11104      else
11105	std_attrs = cp_parser_std_attribute_spec_seq (parser);
11106
11107      /* Parse optional trailing return type.  */
11108      if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11109        {
11110          cp_lexer_consume_token (parser->lexer);
11111          return_type = cp_parser_trailing_type_id (parser);
11112        }
11113
11114      /* Also allow GNU attributes at the very end of the declaration, the
11115	 usual place for GNU attributes.  */
11116      if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11117	gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11118
11119      /* Parse optional trailing requires clause.  */
11120      trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11121
11122      /* The function parameters must be in scope all the way until after the
11123         trailing-return-type in case of decltype.  */
11124      pop_bindings_and_leave_scope ();
11125    }
11126  else if (template_param_list != NULL_TREE) // generate diagnostic
11127    cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11128
11129  /* Create the function call operator.
11130
11131     Messing with declarators like this is no uglier than building up the
11132     FUNCTION_DECL by hand, and this is less likely to get out of sync with
11133     other code.  */
11134  {
11135    cp_decl_specifier_seq return_type_specs;
11136    cp_declarator* declarator;
11137    tree fco;
11138    void *p;
11139
11140    clear_decl_specs (&return_type_specs);
11141    return_type_specs.type = make_auto ();
11142
11143    if (lambda_specs.locations[ds_constexpr])
11144      {
11145	if (cxx_dialect >= cxx17)
11146	  return_type_specs.locations[ds_constexpr]
11147	    = lambda_specs.locations[ds_constexpr];
11148	else
11149	  error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11150		    "lambda only available with %<-std=c++17%> or "
11151		    "%<-std=gnu++17%>");
11152      }
11153    if (lambda_specs.locations[ds_consteval])
11154      return_type_specs.locations[ds_consteval]
11155	= lambda_specs.locations[ds_consteval];
11156
11157    p = obstack_alloc (&declarator_obstack, 0);
11158
11159    declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11160				     LAMBDA_EXPR_LOCATION (lambda_expr));
11161
11162    declarator = make_call_declarator (declarator, param_list, quals,
11163				       VIRT_SPEC_UNSPECIFIED,
11164                                       REF_QUAL_NONE,
11165				       tx_qual,
11166				       exception_spec,
11167                                       return_type,
11168				       trailing_requires_clause);
11169    declarator->std_attributes = std_attrs;
11170
11171    fco = grokmethod (&return_type_specs,
11172		      declarator,
11173		      chainon (gnu_attrs, lambda_specs.attributes));
11174    if (fco != error_mark_node)
11175      {
11176	DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11177	DECL_ARTIFICIAL (fco) = 1;
11178	/* Give the object parameter a different name.  */
11179	DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11180	DECL_SET_LAMBDA_FUNCTION (fco, true);
11181      }
11182    if (template_param_list)
11183      {
11184	fco = finish_member_template_decl (fco);
11185	finish_template_decl (template_param_list);
11186	--parser->num_template_parameter_lists;
11187      }
11188    else if (parser->fully_implicit_function_template_p)
11189      fco = finish_fully_implicit_template (parser, fco);
11190
11191    finish_member_declaration (fco);
11192
11193    obstack_free (&declarator_obstack, p);
11194
11195    return (fco != error_mark_node);
11196  }
11197}
11198
11199/* Parse the body of a lambda expression, which is simply
11200
11201   compound-statement
11202
11203   but which requires special handling.
11204   LAMBDA_EXPR is the current representation of the lambda expression.  */
11205
11206static void
11207cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11208{
11209  bool nested = (current_function_decl != NULL_TREE);
11210  unsigned char local_variables_forbidden_p
11211    = parser->local_variables_forbidden_p;
11212  bool in_function_body = parser->in_function_body;
11213
11214  /* The body of a lambda-expression is not a subexpression of the enclosing
11215     expression.  */
11216  cp_evaluated ev;
11217
11218  if (nested)
11219    push_function_context ();
11220  else
11221    /* Still increment function_depth so that we don't GC in the
11222       middle of an expression.  */
11223    ++function_depth;
11224
11225  vec<tree> omp_privatization_save;
11226  save_omp_privatization_clauses (omp_privatization_save);
11227  /* Clear this in case we're in the middle of a default argument.  */
11228  parser->local_variables_forbidden_p = 0;
11229  parser->in_function_body = true;
11230
11231  {
11232    local_specialization_stack s (lss_copy);
11233    tree fco = lambda_function (lambda_expr);
11234    tree body = start_lambda_function (fco, lambda_expr);
11235
11236    /* Originally C++11 required us to peek for 'return expr'; and
11237       process it specially here to deduce the return type.  N3638
11238       removed the need for that.  */
11239    cp_parser_function_body (parser, false);
11240
11241    finish_lambda_function (body);
11242  }
11243
11244  restore_omp_privatization_clauses (omp_privatization_save);
11245  parser->local_variables_forbidden_p = local_variables_forbidden_p;
11246  parser->in_function_body = in_function_body;
11247  if (nested)
11248    pop_function_context();
11249  else
11250    --function_depth;
11251}
11252
11253/* Statements [gram.stmt.stmt]  */
11254
11255/* Build and add a DEBUG_BEGIN_STMT statement with location LOC.  */
11256
11257static void
11258add_debug_begin_stmt (location_t loc)
11259{
11260  if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11261    return;
11262  if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11263    /* A concept is never expanded normally.  */
11264    return;
11265
11266  tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11267  SET_EXPR_LOCATION (stmt, loc);
11268  add_stmt (stmt);
11269}
11270
11271/* Parse a statement.
11272
11273   statement:
11274     labeled-statement
11275     expression-statement
11276     compound-statement
11277     selection-statement
11278     iteration-statement
11279     jump-statement
11280     declaration-statement
11281     try-block
11282
11283  C++11:
11284
11285  statement:
11286    labeled-statement
11287    attribute-specifier-seq (opt) expression-statement
11288    attribute-specifier-seq (opt) compound-statement
11289    attribute-specifier-seq (opt) selection-statement
11290    attribute-specifier-seq (opt) iteration-statement
11291    attribute-specifier-seq (opt) jump-statement
11292    declaration-statement
11293    attribute-specifier-seq (opt) try-block
11294
11295  init-statement:
11296    expression-statement
11297    simple-declaration
11298
11299  TM Extension:
11300
11301   statement:
11302     atomic-statement
11303
11304  IN_COMPOUND is true when the statement is nested inside a
11305  cp_parser_compound_statement; this matters for certain pragmas.
11306
11307  If IF_P is not NULL, *IF_P is set to indicate whether the statement
11308  is a (possibly labeled) if statement which is not enclosed in braces
11309  and has an else clause.  This is used to implement -Wparentheses.
11310
11311  CHAIN is a vector of if-else-if conditions.  */
11312
11313static void
11314cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11315		     bool in_compound, bool *if_p, vec<tree> *chain,
11316		     location_t *loc_after_labels)
11317{
11318  tree statement, std_attrs = NULL_TREE;
11319  cp_token *token;
11320  location_t statement_location, attrs_loc;
11321
11322 restart:
11323  if (if_p != NULL)
11324    *if_p = false;
11325  /* There is no statement yet.  */
11326  statement = NULL_TREE;
11327
11328  saved_token_sentinel saved_tokens (parser->lexer);
11329  attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11330  if (c_dialect_objc ())
11331    /* In obj-c++, seeing '[[' might be the either the beginning of
11332       c++11 attributes, or a nested objc-message-expression.  So
11333       let's parse the c++11 attributes tentatively.  */
11334    cp_parser_parse_tentatively (parser);
11335  std_attrs = cp_parser_std_attribute_spec_seq (parser);
11336  if (std_attrs)
11337    attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
11338  if (c_dialect_objc ())
11339    {
11340      if (!cp_parser_parse_definitely (parser))
11341	std_attrs = NULL_TREE;
11342    }
11343
11344  /* Peek at the next token.  */
11345  token = cp_lexer_peek_token (parser->lexer);
11346  /* Remember the location of the first token in the statement.  */
11347  cp_token *statement_token = token;
11348  statement_location = token->location;
11349  add_debug_begin_stmt (statement_location);
11350  /* If this is a keyword, then that will often determine what kind of
11351     statement we have.  */
11352  if (token->type == CPP_KEYWORD)
11353    {
11354      enum rid keyword = token->keyword;
11355
11356      switch (keyword)
11357	{
11358	case RID_CASE:
11359	case RID_DEFAULT:
11360	  /* Looks like a labeled-statement with a case label.
11361	     Parse the label, and then use tail recursion to parse
11362	     the statement.  */
11363	  cp_parser_label_for_labeled_statement (parser, std_attrs);
11364	  in_compound = false;
11365	  goto restart;
11366
11367	case RID_IF:
11368	case RID_SWITCH:
11369	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11370	  statement = cp_parser_selection_statement (parser, if_p, chain);
11371	  break;
11372
11373	case RID_WHILE:
11374	case RID_DO:
11375	case RID_FOR:
11376	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11377	  statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11378	  break;
11379
11380	case RID_BREAK:
11381	case RID_CONTINUE:
11382	case RID_RETURN:
11383	case RID_CO_RETURN:
11384	case RID_GOTO:
11385	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11386	  statement = cp_parser_jump_statement (parser);
11387	  break;
11388
11389	  /* Objective-C++ exception-handling constructs.  */
11390	case RID_AT_TRY:
11391	case RID_AT_CATCH:
11392	case RID_AT_FINALLY:
11393	case RID_AT_SYNCHRONIZED:
11394	case RID_AT_THROW:
11395	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11396	  statement = cp_parser_objc_statement (parser);
11397	  break;
11398
11399	case RID_TRY:
11400	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11401	  statement = cp_parser_try_block (parser);
11402	  break;
11403
11404	case RID_NAMESPACE:
11405	  /* This must be a namespace alias definition.  */
11406	  if (std_attrs != NULL_TREE)
11407	    {
11408	      /* Attributes should be parsed as part of the
11409		 declaration, so let's un-parse them.  */
11410	      saved_tokens.rollback();
11411	      std_attrs = NULL_TREE;
11412	    }
11413	  cp_parser_declaration_statement (parser);
11414	  return;
11415
11416	case RID_TRANSACTION_ATOMIC:
11417	case RID_TRANSACTION_RELAXED:
11418	case RID_SYNCHRONIZED:
11419	case RID_ATOMIC_NOEXCEPT:
11420	case RID_ATOMIC_CANCEL:
11421	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11422	  statement = cp_parser_transaction (parser, token);
11423	  break;
11424	case RID_TRANSACTION_CANCEL:
11425	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11426	  statement = cp_parser_transaction_cancel (parser);
11427	  break;
11428
11429	default:
11430	  /* It might be a keyword like `int' that can start a
11431	     declaration-statement.  */
11432	  break;
11433	}
11434    }
11435  else if (token->type == CPP_NAME)
11436    {
11437      /* If the next token is a `:', then we are looking at a
11438	 labeled-statement.  */
11439      token = cp_lexer_peek_nth_token (parser->lexer, 2);
11440      if (token->type == CPP_COLON)
11441	{
11442	  /* Looks like a labeled-statement with an ordinary label.
11443	     Parse the label, and then use tail recursion to parse
11444	     the statement.  */
11445
11446	  cp_parser_label_for_labeled_statement (parser, std_attrs);
11447	  in_compound = false;
11448	  goto restart;
11449	}
11450    }
11451  /* Anything that starts with a `{' must be a compound-statement.  */
11452  else if (token->type == CPP_OPEN_BRACE)
11453    {
11454      std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11455      statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11456    }
11457  /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11458     a statement all its own.  */
11459  else if (token->type == CPP_PRAGMA)
11460    {
11461      /* Only certain OpenMP pragmas are attached to statements, and thus
11462	 are considered statements themselves.  All others are not.  In
11463	 the context of a compound, accept the pragma as a "statement" and
11464	 return so that we can check for a close brace.  Otherwise we
11465	 require a real statement and must go back and read one.  */
11466      if (in_compound)
11467	cp_parser_pragma (parser, pragma_compound, if_p);
11468      else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11469	goto restart;
11470      return;
11471    }
11472  else if (token->type == CPP_EOF)
11473    {
11474      cp_parser_error (parser, "expected statement");
11475      return;
11476    }
11477
11478  /* Everything else must be a declaration-statement or an
11479     expression-statement.  Try for the declaration-statement
11480     first, unless we are looking at a `;', in which case we know that
11481     we have an expression-statement.  */
11482  if (!statement)
11483    {
11484      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11485	{
11486	  if (std_attrs != NULL_TREE)
11487	    /* Attributes should be parsed as part of the declaration,
11488	       so let's un-parse them.  */
11489	    saved_tokens.rollback();
11490
11491	  cp_parser_parse_tentatively (parser);
11492	  /* Try to parse the declaration-statement.  */
11493	  cp_parser_declaration_statement (parser);
11494	  /* If that worked, we're done.  */
11495	  if (cp_parser_parse_definitely (parser))
11496	    return;
11497	  /* It didn't work, restore the post-attribute position.  */
11498	  if (std_attrs)
11499	    cp_lexer_set_token_position (parser->lexer, statement_token);
11500	}
11501      /* All preceding labels have been parsed at this point.  */
11502      if (loc_after_labels != NULL)
11503	*loc_after_labels = statement_location;
11504
11505      std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11506
11507      /* Look for an expression-statement instead.  */
11508      statement = cp_parser_expression_statement (parser, in_statement_expr);
11509
11510      /* Handle [[fallthrough]];.  */
11511      if (attribute_fallthrough_p (std_attrs))
11512	{
11513	  /* The next token after the fallthrough attribute is ';'.  */
11514	  if (statement == NULL_TREE)
11515	    {
11516	      /* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
11517	      statement = build_call_expr_internal_loc (statement_location,
11518							IFN_FALLTHROUGH,
11519							void_type_node, 0);
11520	      finish_expr_stmt (statement);
11521	    }
11522	  else
11523	    warning_at (statement_location, OPT_Wattributes,
11524			"%<fallthrough%> attribute not followed by %<;%>");
11525	  std_attrs = NULL_TREE;
11526	}
11527    }
11528
11529  /* Set the line number for the statement.  */
11530  if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11531    SET_EXPR_LOCATION (statement, statement_location);
11532
11533  /* Allow "[[fallthrough]];", but warn otherwise.  */
11534  if (std_attrs != NULL_TREE)
11535    warning_at (attrs_loc,
11536		OPT_Wattributes,
11537		"attributes at the beginning of statement are ignored");
11538}
11539
11540/* Append ATTR to attribute list ATTRS.  */
11541
11542static tree
11543attr_chainon (tree attrs, tree attr)
11544{
11545  if (attrs == error_mark_node)
11546    return error_mark_node;
11547  if (attr == error_mark_node)
11548    return error_mark_node;
11549  return chainon (attrs, attr);
11550}
11551
11552/* Parse the label for a labeled-statement, i.e.
11553
11554   identifier :
11555   case constant-expression :
11556   default :
11557
11558   GNU Extension:
11559   case constant-expression ... constant-expression : statement
11560
11561   When a label is parsed without errors, the label is added to the
11562   parse tree by the finish_* functions, so this function doesn't
11563   have to return the label.  */
11564
11565static void
11566cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11567{
11568  cp_token *token;
11569  tree label = NULL_TREE;
11570  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11571
11572  /* The next token should be an identifier.  */
11573  token = cp_lexer_peek_token (parser->lexer);
11574  if (token->type != CPP_NAME
11575      && token->type != CPP_KEYWORD)
11576    {
11577      cp_parser_error (parser, "expected labeled-statement");
11578      return;
11579    }
11580
11581  /* Remember whether this case or a user-defined label is allowed to fall
11582     through to.  */
11583  bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11584
11585  parser->colon_corrects_to_scope_p = false;
11586  switch (token->keyword)
11587    {
11588    case RID_CASE:
11589      {
11590	tree expr, expr_hi;
11591	cp_token *ellipsis;
11592
11593	/* Consume the `case' token.  */
11594	cp_lexer_consume_token (parser->lexer);
11595	/* Parse the constant-expression.  */
11596	expr = cp_parser_constant_expression (parser);
11597	if (check_for_bare_parameter_packs (expr))
11598	  expr = error_mark_node;
11599
11600	ellipsis = cp_lexer_peek_token (parser->lexer);
11601	if (ellipsis->type == CPP_ELLIPSIS)
11602	  {
11603	    /* Consume the `...' token.  */
11604	    cp_lexer_consume_token (parser->lexer);
11605	    expr_hi = cp_parser_constant_expression (parser);
11606	    if (check_for_bare_parameter_packs (expr_hi))
11607	      expr_hi = error_mark_node;
11608
11609	    /* We don't need to emit warnings here, as the common code
11610	       will do this for us.  */
11611	  }
11612	else
11613	  expr_hi = NULL_TREE;
11614
11615	if (parser->in_switch_statement_p)
11616	  {
11617	    tree l = finish_case_label (token->location, expr, expr_hi);
11618	    if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11619	      {
11620		label = CASE_LABEL (l);
11621		FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11622	      }
11623	  }
11624	else
11625	  error_at (token->location,
11626		    "case label %qE not within a switch statement",
11627		    expr);
11628      }
11629      break;
11630
11631    case RID_DEFAULT:
11632      /* Consume the `default' token.  */
11633      cp_lexer_consume_token (parser->lexer);
11634
11635      if (parser->in_switch_statement_p)
11636	{
11637	  tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11638	  if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11639	      {
11640		label = CASE_LABEL (l);
11641		FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11642	      }
11643	}
11644      else
11645	error_at (token->location, "case label not within a switch statement");
11646      break;
11647
11648    default:
11649      /* Anything else must be an ordinary label.  */
11650      label = finish_label_stmt (cp_parser_identifier (parser));
11651      if (label && TREE_CODE (label) == LABEL_DECL)
11652	FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11653      break;
11654    }
11655
11656  /* Require the `:' token.  */
11657  cp_parser_require (parser, CPP_COLON, RT_COLON);
11658
11659  /* An ordinary label may optionally be followed by attributes.
11660     However, this is only permitted if the attributes are then
11661     followed by a semicolon.  This is because, for backward
11662     compatibility, when parsing
11663       lab: __attribute__ ((unused)) int i;
11664     we want the attribute to attach to "i", not "lab".  */
11665  if (label != NULL_TREE
11666      && cp_next_tokens_can_be_gnu_attribute_p (parser))
11667    {
11668      tree attrs;
11669      cp_parser_parse_tentatively (parser);
11670      attrs = cp_parser_gnu_attributes_opt (parser);
11671      if (attrs == NULL_TREE
11672	  /* And fallthrough always binds to the expression-statement.  */
11673	  || attribute_fallthrough_p (attrs)
11674	  || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11675	cp_parser_abort_tentative_parse (parser);
11676      else if (!cp_parser_parse_definitely (parser))
11677	;
11678      else
11679	attributes = attr_chainon (attributes, attrs);
11680    }
11681
11682  if (attributes != NULL_TREE)
11683    cplus_decl_attributes (&label, attributes, 0);
11684
11685  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11686}
11687
11688/* Parse an expression-statement.
11689
11690   expression-statement:
11691     expression [opt] ;
11692
11693   Returns the new EXPR_STMT -- or NULL_TREE if the expression
11694   statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11695   indicates whether this expression-statement is part of an
11696   expression statement.  */
11697
11698static tree
11699cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11700{
11701  tree statement = NULL_TREE;
11702  cp_token *token = cp_lexer_peek_token (parser->lexer);
11703  location_t loc = token->location;
11704
11705  /* There might be attribute fallthrough.  */
11706  tree attr = cp_parser_gnu_attributes_opt (parser);
11707
11708  /* If the next token is a ';', then there is no expression
11709     statement.  */
11710  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11711    {
11712      statement = cp_parser_expression (parser);
11713      if (statement == error_mark_node
11714	  && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11715	{
11716	  cp_parser_skip_to_end_of_block_or_statement (parser);
11717	  return error_mark_node;
11718	}
11719    }
11720
11721  /* Handle [[fallthrough]];.  */
11722  if (attribute_fallthrough_p (attr))
11723    {
11724      /* The next token after the fallthrough attribute is ';'.  */
11725      if (statement == NULL_TREE)
11726	/* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
11727	statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11728						  void_type_node, 0);
11729      else
11730	warning_at (loc, OPT_Wattributes,
11731		    "%<fallthrough%> attribute not followed by %<;%>");
11732      attr = NULL_TREE;
11733    }
11734
11735  /* Allow "[[fallthrough]];", but warn otherwise.  */
11736  if (attr != NULL_TREE)
11737    warning_at (loc, OPT_Wattributes,
11738		"attributes at the beginning of statement are ignored");
11739
11740  /* Give a helpful message for "A<T>::type t;" and the like.  */
11741  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11742      && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11743    {
11744      if (TREE_CODE (statement) == SCOPE_REF)
11745	error_at (token->location, "need %<typename%> before %qE because "
11746		  "%qT is a dependent scope",
11747		  statement, TREE_OPERAND (statement, 0));
11748      else if (is_overloaded_fn (statement)
11749	       && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11750	{
11751	  /* A::A a; */
11752	  tree fn = get_first_fn (statement);
11753	  error_at (token->location,
11754		    "%<%T::%D%> names the constructor, not the type",
11755		    DECL_CONTEXT (fn), DECL_NAME (fn));
11756	}
11757    }
11758
11759  /* Consume the final `;'.  */
11760  cp_parser_consume_semicolon_at_end_of_statement (parser);
11761
11762  if (in_statement_expr
11763      && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11764    /* This is the final expression statement of a statement
11765       expression.  */
11766    statement = finish_stmt_expr_expr (statement, in_statement_expr);
11767  else if (statement)
11768    statement = finish_expr_stmt (statement);
11769
11770  return statement;
11771}
11772
11773/* Parse a compound-statement.
11774
11775   compound-statement:
11776     { statement-seq [opt] }
11777
11778   GNU extension:
11779
11780   compound-statement:
11781     { label-declaration-seq [opt] statement-seq [opt] }
11782
11783   label-declaration-seq:
11784     label-declaration
11785     label-declaration-seq label-declaration
11786
11787   Returns a tree representing the statement.  */
11788
11789static tree
11790cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11791			      int bcs_flags, bool function_body)
11792{
11793  tree compound_stmt;
11794  matching_braces braces;
11795
11796  /* Consume the `{'.  */
11797  if (!braces.require_open (parser))
11798    return error_mark_node;
11799  if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11800      && !function_body && cxx_dialect < cxx14)
11801    pedwarn (input_location, OPT_Wpedantic,
11802	     "compound-statement in %<constexpr%> function");
11803  /* Begin the compound-statement.  */
11804  compound_stmt = begin_compound_stmt (bcs_flags);
11805  /* If the next keyword is `__label__' we have a label declaration.  */
11806  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11807    cp_parser_label_declaration (parser);
11808  /* Parse an (optional) statement-seq.  */
11809  cp_parser_statement_seq_opt (parser, in_statement_expr);
11810
11811  if (function_body)
11812    maybe_splice_retval_cleanup (compound_stmt);
11813
11814  /* Finish the compound-statement.  */
11815  finish_compound_stmt (compound_stmt);
11816  /* Consume the `}'.  */
11817  braces.require_close (parser);
11818
11819  return compound_stmt;
11820}
11821
11822/* Parse an (optional) statement-seq.
11823
11824   statement-seq:
11825     statement
11826     statement-seq [opt] statement  */
11827
11828static void
11829cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11830{
11831  /* Scan statements until there aren't any more.  */
11832  while (true)
11833    {
11834      cp_token *token = cp_lexer_peek_token (parser->lexer);
11835
11836      /* If we are looking at a `}', then we have run out of
11837	 statements; the same is true if we have reached the end
11838	 of file, or have stumbled upon a stray '@end'.  */
11839      if (token->type == CPP_CLOSE_BRACE
11840	  || token->type == CPP_EOF
11841	  || token->type == CPP_PRAGMA_EOL
11842	  || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11843	break;
11844
11845      /* If we are in a compound statement and find 'else' then
11846	 something went wrong.  */
11847      else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11848	{
11849	  if (parser->in_statement & IN_IF_STMT)
11850	    break;
11851	  else
11852	    {
11853	      token = cp_lexer_consume_token (parser->lexer);
11854	      error_at (token->location, "%<else%> without a previous %<if%>");
11855	    }
11856	}
11857
11858      /* Parse the statement.  */
11859      cp_parser_statement (parser, in_statement_expr, true, NULL);
11860    }
11861}
11862
11863/* Return true if this is the C++20 version of range-based-for with
11864   init-statement.  */
11865
11866static bool
11867cp_parser_range_based_for_with_init_p (cp_parser *parser)
11868{
11869  bool r = false;
11870
11871  /* Save tokens so that we can put them back.  */
11872  cp_lexer_save_tokens (parser->lexer);
11873
11874  /* There has to be an unnested ; followed by an unnested :.  */
11875  if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11876					       /*recovering=*/false,
11877					       CPP_SEMICOLON,
11878					       /*consume_paren=*/false) != -1)
11879    goto out;
11880
11881  /* We found the semicolon, eat it now.  */
11882  cp_lexer_consume_token (parser->lexer);
11883
11884  /* Now look for ':' that is not nested in () or {}.  */
11885  r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11886						/*recovering=*/false,
11887						CPP_COLON,
11888						/*consume_paren=*/false) == -1);
11889
11890out:
11891  /* Roll back the tokens we skipped.  */
11892  cp_lexer_rollback_tokens (parser->lexer);
11893
11894  return r;
11895}
11896
11897/* Return true if we're looking at (init; cond), false otherwise.  */
11898
11899static bool
11900cp_parser_init_statement_p (cp_parser *parser)
11901{
11902  /* Save tokens so that we can put them back.  */
11903  cp_lexer_save_tokens (parser->lexer);
11904
11905  /* Look for ';' that is not nested in () or {}.  */
11906  int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11907						     /*recovering=*/false,
11908						     CPP_SEMICOLON,
11909						     /*consume_paren=*/false);
11910
11911  /* Roll back the tokens we skipped.  */
11912  cp_lexer_rollback_tokens (parser->lexer);
11913
11914  return ret == -1;
11915}
11916
11917/* Parse a selection-statement.
11918
11919   selection-statement:
11920     if ( init-statement [opt] condition ) statement
11921     if ( init-statement [opt] condition ) statement else statement
11922     switch ( init-statement [opt] condition ) statement
11923
11924   Returns the new IF_STMT or SWITCH_STMT.
11925
11926   If IF_P is not NULL, *IF_P is set to indicate whether the statement
11927   is a (possibly labeled) if statement which is not enclosed in
11928   braces and has an else clause.  This is used to implement
11929   -Wparentheses.
11930
11931   CHAIN is a vector of if-else-if conditions.  This is used to implement
11932   -Wduplicated-cond.  */
11933
11934static tree
11935cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11936			       vec<tree> *chain)
11937{
11938  cp_token *token;
11939  enum rid keyword;
11940  token_indent_info guard_tinfo;
11941
11942  if (if_p != NULL)
11943    *if_p = false;
11944
11945  /* Peek at the next token.  */
11946  token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11947  guard_tinfo = get_token_indent_info (token);
11948
11949  /* See what kind of keyword it is.  */
11950  keyword = token->keyword;
11951  switch (keyword)
11952    {
11953    case RID_IF:
11954    case RID_SWITCH:
11955      {
11956	tree statement;
11957	tree condition;
11958
11959	bool cx = false;
11960	if (keyword == RID_IF
11961	    && cp_lexer_next_token_is_keyword (parser->lexer,
11962					       RID_CONSTEXPR))
11963	  {
11964	    cx = true;
11965	    cp_token *tok = cp_lexer_consume_token (parser->lexer);
11966	    if (cxx_dialect < cxx17)
11967	      pedwarn (tok->location, 0, "%<if constexpr%> only available "
11968		       "with %<-std=c++17%> or %<-std=gnu++17%>");
11969	  }
11970
11971	/* Look for the `('.  */
11972	matching_parens parens;
11973	if (!parens.require_open (parser))
11974	  {
11975	    cp_parser_skip_to_end_of_statement (parser);
11976	    return error_mark_node;
11977	  }
11978
11979	/* Begin the selection-statement.  */
11980	if (keyword == RID_IF)
11981	  {
11982	    statement = begin_if_stmt ();
11983	    IF_STMT_CONSTEXPR_P (statement) = cx;
11984	  }
11985	else
11986	  statement = begin_switch_stmt ();
11987
11988	/* Parse the optional init-statement.  */
11989	if (cp_parser_init_statement_p (parser))
11990	  {
11991	    tree decl;
11992	    if (cxx_dialect < cxx17)
11993	      pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11994		       "init-statement in selection statements only available "
11995		       "with %<-std=c++17%> or %<-std=gnu++17%>");
11996	    if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11997	      {
11998		/* A non-empty init-statement can have arbitrary side
11999		   effects.  */
12000		delete chain;
12001		chain = NULL;
12002	      }
12003	    cp_parser_init_statement (parser, &decl);
12004	  }
12005
12006	/* Parse the condition.  */
12007	condition = cp_parser_condition (parser);
12008	/* Look for the `)'.  */
12009	if (!parens.require_close (parser))
12010	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
12011						 /*consume_paren=*/true);
12012
12013	if (keyword == RID_IF)
12014	  {
12015	    bool nested_if;
12016	    unsigned char in_statement;
12017
12018	    /* Add the condition.  */
12019	    condition = finish_if_stmt_cond (condition, statement);
12020
12021	    if (warn_duplicated_cond)
12022	      warn_duplicated_cond_add_or_warn (token->location, condition,
12023						&chain);
12024
12025	    /* Parse the then-clause.  */
12026	    in_statement = parser->in_statement;
12027	    parser->in_statement |= IN_IF_STMT;
12028
12029	    /* Outside a template, the non-selected branch of a constexpr
12030	       if is a 'discarded statement', i.e. unevaluated.  */
12031	    bool was_discarded = in_discarded_stmt;
12032	    bool discard_then = (cx && !processing_template_decl
12033				 && integer_zerop (condition));
12034	    if (discard_then)
12035	      {
12036		in_discarded_stmt = true;
12037		++c_inhibit_evaluation_warnings;
12038	      }
12039
12040	    cp_parser_implicitly_scoped_statement (parser, &nested_if,
12041						   guard_tinfo);
12042
12043	    parser->in_statement = in_statement;
12044
12045	    finish_then_clause (statement);
12046
12047	    if (discard_then)
12048	      {
12049		THEN_CLAUSE (statement) = NULL_TREE;
12050		in_discarded_stmt = was_discarded;
12051		--c_inhibit_evaluation_warnings;
12052	      }
12053
12054	    /* If the next token is `else', parse the else-clause.  */
12055	    if (cp_lexer_next_token_is_keyword (parser->lexer,
12056						RID_ELSE))
12057	      {
12058		bool discard_else = (cx && !processing_template_decl
12059				     && integer_nonzerop (condition));
12060		if (discard_else)
12061		  {
12062		    in_discarded_stmt = true;
12063		    ++c_inhibit_evaluation_warnings;
12064		  }
12065
12066		guard_tinfo
12067		  = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12068		/* Consume the `else' keyword.  */
12069		cp_lexer_consume_token (parser->lexer);
12070		if (warn_duplicated_cond)
12071		  {
12072		    if (cp_lexer_next_token_is_keyword (parser->lexer,
12073							RID_IF)
12074			&& chain == NULL)
12075		      {
12076			/* We've got "if (COND) else if (COND2)".  Start
12077			   the condition chain and add COND as the first
12078			   element.  */
12079			chain = new vec<tree> ();
12080			if (!CONSTANT_CLASS_P (condition)
12081			    && !TREE_SIDE_EFFECTS (condition))
12082			{
12083			  /* Wrap it in a NOP_EXPR so that we can set the
12084			     location of the condition.  */
12085			  tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
12086					   condition);
12087			  SET_EXPR_LOCATION (e, token->location);
12088			  chain->safe_push (e);
12089			}
12090		      }
12091		    else if (!cp_lexer_next_token_is_keyword (parser->lexer,
12092							      RID_IF))
12093		      {
12094			/* This is if-else without subsequent if.  Zap the
12095			   condition chain; we would have already warned at
12096			   this point.  */
12097			delete chain;
12098			chain = NULL;
12099		      }
12100		  }
12101		begin_else_clause (statement);
12102		/* Parse the else-clause.  */
12103		cp_parser_implicitly_scoped_statement (parser, NULL,
12104						       guard_tinfo, chain);
12105
12106		finish_else_clause (statement);
12107
12108		/* If we are currently parsing a then-clause, then
12109		   IF_P will not be NULL.  We set it to true to
12110		   indicate that this if statement has an else clause.
12111		   This may trigger the Wparentheses warning below
12112		   when we get back up to the parent if statement.  */
12113		if (if_p != NULL)
12114		  *if_p = true;
12115
12116		if (discard_else)
12117		  {
12118		    ELSE_CLAUSE (statement) = NULL_TREE;
12119		    in_discarded_stmt = was_discarded;
12120		    --c_inhibit_evaluation_warnings;
12121		  }
12122	      }
12123	    else
12124	      {
12125		/* This if statement does not have an else clause.  If
12126		   NESTED_IF is true, then the then-clause has an if
12127		   statement which does have an else clause.  We warn
12128		   about the potential ambiguity.  */
12129		if (nested_if)
12130		  warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
12131			      "suggest explicit braces to avoid ambiguous"
12132			      " %<else%>");
12133		if (warn_duplicated_cond)
12134		  {
12135		    /* We don't need the condition chain anymore.  */
12136		    delete chain;
12137		    chain = NULL;
12138		  }
12139	      }
12140
12141	    /* Now we're all done with the if-statement.  */
12142	    finish_if_stmt (statement);
12143	  }
12144	else
12145	  {
12146	    bool in_switch_statement_p;
12147	    unsigned char in_statement;
12148
12149	    /* Add the condition.  */
12150	    finish_switch_cond (condition, statement);
12151
12152	    /* Parse the body of the switch-statement.  */
12153	    in_switch_statement_p = parser->in_switch_statement_p;
12154	    in_statement = parser->in_statement;
12155	    parser->in_switch_statement_p = true;
12156	    parser->in_statement |= IN_SWITCH_STMT;
12157	    cp_parser_implicitly_scoped_statement (parser, if_p,
12158						   guard_tinfo);
12159	    parser->in_switch_statement_p = in_switch_statement_p;
12160	    parser->in_statement = in_statement;
12161
12162	    /* Now we're all done with the switch-statement.  */
12163	    finish_switch_stmt (statement);
12164	  }
12165
12166	return statement;
12167      }
12168      break;
12169
12170    default:
12171      cp_parser_error (parser, "expected selection-statement");
12172      return error_mark_node;
12173    }
12174}
12175
12176/* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12177   If we have seen at least one decl-specifier, and the next token is not
12178   a parenthesis (after "int (" we might be looking at a functional cast)
12179   neither we are dealing with a concept-check expression then we must be
12180   looking at a declaration.  */
12181
12182static void
12183cp_parser_maybe_commit_to_declaration (cp_parser* parser,
12184				       cp_decl_specifier_seq *decl_specs)
12185{
12186  if (decl_specs->any_specifiers_p
12187      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12188      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12189      && !cp_parser_error_occurred (parser)
12190      && !(decl_specs->type
12191	   && TREE_CODE (decl_specs->type) == TYPE_DECL
12192	   && is_constrained_auto (TREE_TYPE (decl_specs->type))))
12193    cp_parser_commit_to_tentative_parse (parser);
12194}
12195
12196/* Helper function for cp_parser_condition.  Enforces [stmt.stmt]/2:
12197   The declarator shall not specify a function or an array.  Returns
12198   TRUE if the declarator is valid, FALSE otherwise.  */
12199
12200static bool
12201cp_parser_check_condition_declarator (cp_parser* parser,
12202                                     cp_declarator *declarator,
12203                                     location_t loc)
12204{
12205  if (declarator == cp_error_declarator
12206      || function_declarator_p (declarator)
12207      || declarator->kind == cdk_array)
12208    {
12209      if (declarator == cp_error_declarator)
12210	/* Already complained.  */;
12211      else if (declarator->kind == cdk_array)
12212       error_at (loc, "condition declares an array");
12213      else
12214       error_at (loc, "condition declares a function");
12215      if (parser->fully_implicit_function_template_p)
12216       abort_fully_implicit_template (parser);
12217      cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12218                                            /*or_comma=*/false,
12219                                            /*consume_paren=*/false);
12220      return false;
12221    }
12222  else
12223    return true;
12224}
12225
12226/* Parse a condition.
12227
12228   condition:
12229     expression
12230     type-specifier-seq declarator = initializer-clause
12231     type-specifier-seq declarator braced-init-list
12232
12233   GNU Extension:
12234
12235   condition:
12236     type-specifier-seq declarator asm-specification [opt]
12237       attributes [opt] = assignment-expression
12238
12239   Returns the expression that should be tested.  */
12240
12241static tree
12242cp_parser_condition (cp_parser* parser)
12243{
12244  cp_decl_specifier_seq type_specifiers;
12245  const char *saved_message;
12246  int declares_class_or_enum;
12247
12248  /* Try the declaration first.  */
12249  cp_parser_parse_tentatively (parser);
12250  /* New types are not allowed in the type-specifier-seq for a
12251     condition.  */
12252  saved_message = parser->type_definition_forbidden_message;
12253  parser->type_definition_forbidden_message
12254    = G_("types may not be defined in conditions");
12255  /* Parse the type-specifier-seq.  */
12256  cp_parser_decl_specifier_seq (parser,
12257				CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12258				&type_specifiers,
12259				&declares_class_or_enum);
12260  /* Restore the saved message.  */
12261  parser->type_definition_forbidden_message = saved_message;
12262
12263  /* Gather the attributes that were provided with the
12264     decl-specifiers.  */
12265  tree prefix_attributes = type_specifiers.attributes;
12266
12267  cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
12268
12269  /* If all is well, we might be looking at a declaration.  */
12270  if (!cp_parser_error_occurred (parser))
12271    {
12272      tree decl;
12273      tree asm_specification;
12274      tree attributes;
12275      cp_declarator *declarator;
12276      tree initializer = NULL_TREE;
12277      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12278
12279      /* Parse the declarator.  */
12280      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12281					 CP_PARSER_FLAGS_NONE,
12282					 /*ctor_dtor_or_conv_p=*/NULL,
12283					 /*parenthesized_p=*/NULL,
12284					 /*member_p=*/false,
12285					 /*friend_p=*/false,
12286					 /*static_p=*/false);
12287      /* Parse the attributes.  */
12288      attributes = cp_parser_attributes_opt (parser);
12289      /* Parse the asm-specification.  */
12290      asm_specification = cp_parser_asm_specification_opt (parser);
12291      /* If the next token is not an `=' or '{', then we might still be
12292	 looking at an expression.  For example:
12293
12294	   if (A(a).x)
12295
12296	 looks like a decl-specifier-seq and a declarator -- but then
12297	 there is no `=', so this is an expression.  */
12298      if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12299	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12300	cp_parser_simulate_error (parser);
12301
12302      /* If we did see an `=' or '{', then we are looking at a declaration
12303	 for sure.  */
12304      if (cp_parser_parse_definitely (parser))
12305	{
12306	  tree pushed_scope;
12307	  bool non_constant_p = false;
12308	  int flags = LOOKUP_ONLYCONVERTING;
12309
12310	  if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12311	    return error_mark_node;
12312
12313	  /* Create the declaration.  */
12314	  decl = start_decl (declarator, &type_specifiers,
12315			     /*initialized_p=*/true,
12316			     attributes, prefix_attributes,
12317			     &pushed_scope);
12318
12319	  /* Parse the initializer.  */
12320	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12321	    {
12322	      initializer = cp_parser_braced_list (parser, &non_constant_p);
12323	      CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12324	      flags = 0;
12325	    }
12326	  else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12327	    {
12328	      /* Consume the `='.  */
12329	      cp_lexer_consume_token (parser->lexer);
12330	      initializer = cp_parser_initializer_clause (parser,
12331							  &non_constant_p);
12332	    }
12333	  else
12334	    {
12335	      cp_parser_error (parser, "expected initializer");
12336	      initializer = error_mark_node;
12337	    }
12338	  if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12339	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12340
12341	  /* Process the initializer.  */
12342	  cp_finish_decl (decl,
12343			  initializer, !non_constant_p,
12344			  asm_specification,
12345			  flags);
12346
12347	  if (pushed_scope)
12348	    pop_scope (pushed_scope);
12349
12350	  return convert_from_reference (decl);
12351	}
12352    }
12353  /* If we didn't even get past the declarator successfully, we are
12354     definitely not looking at a declaration.  */
12355  else
12356    cp_parser_abort_tentative_parse (parser);
12357
12358  /* Otherwise, we are looking at an expression.  */
12359  return cp_parser_expression (parser);
12360}
12361
12362/* Parses a for-statement or range-for-statement until the closing ')',
12363   not included. */
12364
12365static tree
12366cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12367{
12368  tree init, scope, decl;
12369  bool is_range_for;
12370
12371  /* Begin the for-statement.  */
12372  scope = begin_for_scope (&init);
12373
12374  /* Parse the initialization.  */
12375  is_range_for = cp_parser_init_statement (parser, &decl);
12376
12377  if (is_range_for)
12378    return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12379				false);
12380  else
12381    return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12382}
12383
12384static tree
12385cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12386		 unsigned short unroll)
12387{
12388  /* Normal for loop */
12389  tree condition = NULL_TREE;
12390  tree expression = NULL_TREE;
12391  tree stmt;
12392
12393  stmt = begin_for_stmt (scope, init);
12394  /* The init-statement has already been parsed in
12395     cp_parser_init_statement, so no work is needed here.  */
12396  finish_init_stmt (stmt);
12397
12398  /* If there's a condition, process it.  */
12399  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12400    condition = cp_parser_condition (parser);
12401  else if (ivdep)
12402    {
12403      cp_parser_error (parser, "missing loop condition in loop with "
12404		       "%<GCC ivdep%> pragma");
12405      condition = error_mark_node;
12406    }
12407  else if (unroll)
12408    {
12409      cp_parser_error (parser, "missing loop condition in loop with "
12410		       "%<GCC unroll%> pragma");
12411      condition = error_mark_node;
12412    }
12413  finish_for_cond (condition, stmt, ivdep, unroll);
12414  /* Look for the `;'.  */
12415  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12416
12417  /* If there's an expression, process it.  */
12418  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12419    expression = cp_parser_expression (parser);
12420  finish_for_expr (expression, stmt);
12421
12422  return stmt;
12423}
12424
12425/* Tries to parse a range-based for-statement:
12426
12427  range-based-for:
12428    decl-specifier-seq declarator : expression
12429
12430  The decl-specifier-seq declarator and the `:' are already parsed by
12431  cp_parser_init_statement.  If processing_template_decl it returns a
12432  newly created RANGE_FOR_STMT; if not, it is converted to a
12433  regular FOR_STMT.  */
12434
12435static tree
12436cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12437		     bool ivdep, unsigned short unroll, bool is_omp)
12438{
12439  tree stmt, range_expr;
12440  auto_vec <cxx_binding *, 16> bindings;
12441  auto_vec <tree, 16> names;
12442  tree decomp_first_name = NULL_TREE;
12443  unsigned int decomp_cnt = 0;
12444
12445  /* Get the range declaration momentarily out of the way so that
12446     the range expression doesn't clash with it. */
12447  if (range_decl != error_mark_node)
12448    {
12449      if (DECL_HAS_VALUE_EXPR_P (range_decl))
12450	{
12451	  tree v = DECL_VALUE_EXPR (range_decl);
12452	  /* For decomposition declaration get all of the corresponding
12453	     declarations out of the way.  */
12454	  if (TREE_CODE (v) == ARRAY_REF
12455	      && VAR_P (TREE_OPERAND (v, 0))
12456	      && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12457	    {
12458	      tree d = range_decl;
12459	      range_decl = TREE_OPERAND (v, 0);
12460	      decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12461	      decomp_first_name = d;
12462	      for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12463		{
12464		  tree name = DECL_NAME (d);
12465		  names.safe_push (name);
12466		  bindings.safe_push (IDENTIFIER_BINDING (name));
12467		  IDENTIFIER_BINDING (name)
12468		    = IDENTIFIER_BINDING (name)->previous;
12469		}
12470	    }
12471	}
12472      if (names.is_empty ())
12473	{
12474	  tree name = DECL_NAME (range_decl);
12475	  names.safe_push (name);
12476	  bindings.safe_push (IDENTIFIER_BINDING (name));
12477	  IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12478	}
12479    }
12480
12481  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12482    {
12483      bool expr_non_constant_p;
12484      range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12485    }
12486  else
12487    range_expr = cp_parser_expression (parser);
12488
12489  /* Put the range declaration(s) back into scope. */
12490  for (unsigned int i = 0; i < names.length (); i++)
12491    {
12492      cxx_binding *binding = bindings[i];
12493      binding->previous = IDENTIFIER_BINDING (names[i]);
12494      IDENTIFIER_BINDING (names[i]) = binding;
12495    }
12496
12497  /* finish_omp_for has its own code for the following, so just
12498     return the range_expr instead.  */
12499  if (is_omp)
12500    return range_expr;
12501
12502  /* If in template, STMT is converted to a normal for-statement
12503     at instantiation. If not, it is done just ahead. */
12504  if (processing_template_decl)
12505    {
12506      if (check_for_bare_parameter_packs (range_expr))
12507	range_expr = error_mark_node;
12508      stmt = begin_range_for_stmt (scope, init);
12509      if (ivdep)
12510	RANGE_FOR_IVDEP (stmt) = 1;
12511      if (unroll)
12512	RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12513      finish_range_for_decl (stmt, range_decl, range_expr);
12514      if (!type_dependent_expression_p (range_expr)
12515	  /* do_auto_deduction doesn't mess with template init-lists.  */
12516	  && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12517	do_range_for_auto_deduction (range_decl, range_expr);
12518    }
12519  else
12520    {
12521      stmt = begin_for_stmt (scope, init);
12522      stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12523				   decomp_first_name, decomp_cnt, ivdep,
12524				   unroll);
12525    }
12526  return stmt;
12527}
12528
12529/* Subroutine of cp_convert_range_for: given the initializer expression,
12530   builds up the range temporary.  */
12531
12532static tree
12533build_range_temp (tree range_expr)
12534{
12535  tree range_type, range_temp;
12536
12537  /* Find out the type deduced by the declaration
12538     `auto &&__range = range_expr'.  */
12539  range_type = cp_build_reference_type (make_auto (), true);
12540  range_type = do_auto_deduction (range_type, range_expr,
12541				  type_uses_auto (range_type));
12542
12543  /* Create the __range variable.  */
12544  range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12545			   range_type);
12546  TREE_USED (range_temp) = 1;
12547  DECL_ARTIFICIAL (range_temp) = 1;
12548
12549  return range_temp;
12550}
12551
12552/* Used by cp_parser_range_for in template context: we aren't going to
12553   do a full conversion yet, but we still need to resolve auto in the
12554   type of the for-range-declaration if present.  This is basically
12555   a shortcut version of cp_convert_range_for.  */
12556
12557static void
12558do_range_for_auto_deduction (tree decl, tree range_expr)
12559{
12560  tree auto_node = type_uses_auto (TREE_TYPE (decl));
12561  if (auto_node)
12562    {
12563      tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12564      range_temp = convert_from_reference (build_range_temp (range_expr));
12565      iter_type = (cp_parser_perform_range_for_lookup
12566		   (range_temp, &begin_dummy, &end_dummy));
12567      if (iter_type)
12568	{
12569	  iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12570				  iter_type);
12571	  iter_decl = build_x_indirect_ref (input_location, iter_decl,
12572					    RO_UNARY_STAR,
12573					    tf_warning_or_error);
12574	  TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12575						iter_decl, auto_node);
12576	}
12577    }
12578}
12579
12580/* Converts a range-based for-statement into a normal
12581   for-statement, as per the definition.
12582
12583      for (RANGE_DECL : RANGE_EXPR)
12584	BLOCK
12585
12586   should be equivalent to:
12587
12588      {
12589	auto &&__range = RANGE_EXPR;
12590	for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12591	      __begin != __end;
12592	      ++__begin)
12593	  {
12594	      RANGE_DECL = *__begin;
12595	      BLOCK
12596	  }
12597      }
12598
12599   If RANGE_EXPR is an array:
12600	BEGIN_EXPR = __range
12601	END_EXPR = __range + ARRAY_SIZE(__range)
12602   Else if RANGE_EXPR has a member 'begin' or 'end':
12603	BEGIN_EXPR = __range.begin()
12604	END_EXPR = __range.end()
12605   Else:
12606	BEGIN_EXPR = begin(__range)
12607	END_EXPR = end(__range);
12608
12609   If __range has a member 'begin' but not 'end', or vice versa, we must
12610   still use the second alternative (it will surely fail, however).
12611   When calling begin()/end() in the third alternative we must use
12612   argument dependent lookup, but always considering 'std' as an associated
12613   namespace.  */
12614
12615tree
12616cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12617		      tree decomp_first_name, unsigned int decomp_cnt,
12618		      bool ivdep, unsigned short unroll)
12619{
12620  tree begin, end;
12621  tree iter_type, begin_expr, end_expr;
12622  tree condition, expression;
12623
12624  range_expr = mark_lvalue_use (range_expr);
12625
12626  if (range_decl == error_mark_node || range_expr == error_mark_node)
12627    /* If an error happened previously do nothing or else a lot of
12628       unhelpful errors would be issued.  */
12629    begin_expr = end_expr = iter_type = error_mark_node;
12630  else
12631    {
12632      tree range_temp;
12633
12634      if (VAR_P (range_expr)
12635	  && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12636	/* Can't bind a reference to an array of runtime bound.  */
12637	range_temp = range_expr;
12638      else
12639	{
12640	  range_temp = build_range_temp (range_expr);
12641	  pushdecl (range_temp);
12642	  cp_finish_decl (range_temp, range_expr,
12643			  /*is_constant_init*/false, NULL_TREE,
12644			  LOOKUP_ONLYCONVERTING);
12645	  range_temp = convert_from_reference (range_temp);
12646	}
12647      iter_type = cp_parser_perform_range_for_lookup (range_temp,
12648						      &begin_expr, &end_expr);
12649    }
12650
12651  /* The new for initialization statement.  */
12652  begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12653		      iter_type);
12654  TREE_USED (begin) = 1;
12655  DECL_ARTIFICIAL (begin) = 1;
12656  pushdecl (begin);
12657  cp_finish_decl (begin, begin_expr,
12658		  /*is_constant_init*/false, NULL_TREE,
12659		  LOOKUP_ONLYCONVERTING);
12660
12661  if (cxx_dialect >= cxx17)
12662    iter_type = cv_unqualified (TREE_TYPE (end_expr));
12663  end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12664  TREE_USED (end) = 1;
12665  DECL_ARTIFICIAL (end) = 1;
12666  pushdecl (end);
12667  cp_finish_decl (end, end_expr,
12668		  /*is_constant_init*/false, NULL_TREE,
12669		  LOOKUP_ONLYCONVERTING);
12670
12671  finish_init_stmt (statement);
12672
12673  /* The new for condition.  */
12674  condition = build_x_binary_op (input_location, NE_EXPR,
12675				 begin, ERROR_MARK,
12676				 end, ERROR_MARK,
12677				 NULL, tf_warning_or_error);
12678  finish_for_cond (condition, statement, ivdep, unroll);
12679
12680  /* The new increment expression.  */
12681  expression = finish_unary_op_expr (input_location,
12682				     PREINCREMENT_EXPR, begin,
12683				     tf_warning_or_error);
12684  finish_for_expr (expression, statement);
12685
12686  if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12687    cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12688
12689  /* The declaration is initialized with *__begin inside the loop body.  */
12690  cp_finish_decl (range_decl,
12691		  build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12692					tf_warning_or_error),
12693		  /*is_constant_init*/false, NULL_TREE,
12694		  LOOKUP_ONLYCONVERTING);
12695  if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12696    cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12697
12698  return statement;
12699}
12700
12701/* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12702   We need to solve both at the same time because the method used
12703   depends on the existence of members begin or end.
12704   Returns the type deduced for the iterator expression.  */
12705
12706static tree
12707cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12708{
12709  if (error_operand_p (range))
12710    {
12711      *begin = *end = error_mark_node;
12712      return error_mark_node;
12713    }
12714
12715  if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12716    {
12717      error ("range-based %<for%> expression of type %qT "
12718	     "has incomplete type", TREE_TYPE (range));
12719      *begin = *end = error_mark_node;
12720      return error_mark_node;
12721    }
12722  if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12723    {
12724      /* If RANGE is an array, we will use pointer arithmetic.  */
12725      *begin = decay_conversion (range, tf_warning_or_error);
12726      *end = build_binary_op (input_location, PLUS_EXPR,
12727			      range,
12728			      array_type_nelts_top (TREE_TYPE (range)),
12729			      false);
12730      return TREE_TYPE (*begin);
12731    }
12732  else
12733    {
12734      /* If it is not an array, we must do a bit of magic.  */
12735      tree id_begin, id_end;
12736      tree member_begin, member_end;
12737
12738      *begin = *end = error_mark_node;
12739
12740      id_begin = get_identifier ("begin");
12741      id_end = get_identifier ("end");
12742      member_begin = lookup_member (TREE_TYPE (range), id_begin,
12743				    /*protect=*/2, /*want_type=*/false,
12744				    tf_warning_or_error);
12745      member_end = lookup_member (TREE_TYPE (range), id_end,
12746				  /*protect=*/2, /*want_type=*/false,
12747				  tf_warning_or_error);
12748
12749      if (member_begin != NULL_TREE && member_end != NULL_TREE)
12750	{
12751	  /* Use the member functions.  */
12752	  *begin = cp_parser_range_for_member_function (range, id_begin);
12753	  *end = cp_parser_range_for_member_function (range, id_end);
12754	}
12755      else
12756	{
12757	  /* Use global functions with ADL.  */
12758	  releasing_vec vec;
12759
12760	  vec_safe_push (vec, range);
12761
12762	  member_begin = perform_koenig_lookup (id_begin, vec,
12763						tf_warning_or_error);
12764	  *begin = finish_call_expr (member_begin, &vec, false, true,
12765				     tf_warning_or_error);
12766	  member_end = perform_koenig_lookup (id_end, vec,
12767					      tf_warning_or_error);
12768	  *end = finish_call_expr (member_end, &vec, false, true,
12769				   tf_warning_or_error);
12770	}
12771
12772      /* Last common checks.  */
12773      if (*begin == error_mark_node || *end == error_mark_node)
12774	{
12775	  /* If one of the expressions is an error do no more checks.  */
12776	  *begin = *end = error_mark_node;
12777	  return error_mark_node;
12778	}
12779      else if (type_dependent_expression_p (*begin)
12780	       || type_dependent_expression_p (*end))
12781	/* Can happen, when, eg, in a template context, Koenig lookup
12782	   can't resolve begin/end (c++/58503).  */
12783	return NULL_TREE;
12784      else
12785	{
12786	  tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12787	  /* The unqualified type of the __begin and __end temporaries should
12788	     be the same, as required by the multiple auto declaration.  */
12789	  if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12790	    {
12791	      if (cxx_dialect >= cxx17
12792		  && (build_x_binary_op (input_location, NE_EXPR,
12793					 *begin, ERROR_MARK,
12794					 *end, ERROR_MARK,
12795					 NULL, tf_none)
12796		      != error_mark_node))
12797		/* P0184R0 allows __begin and __end to have different types,
12798		   but make sure they are comparable so we can give a better
12799		   diagnostic.  */;
12800	      else
12801		error ("inconsistent begin/end types in range-based %<for%> "
12802		       "statement: %qT and %qT",
12803		       TREE_TYPE (*begin), TREE_TYPE (*end));
12804	    }
12805	  return iter_type;
12806	}
12807    }
12808}
12809
12810/* Helper function for cp_parser_perform_range_for_lookup.
12811   Builds a tree for RANGE.IDENTIFIER().  */
12812
12813static tree
12814cp_parser_range_for_member_function (tree range, tree identifier)
12815{
12816  tree member, res;
12817
12818  member = finish_class_member_access_expr (range, identifier,
12819					    false, tf_warning_or_error);
12820  if (member == error_mark_node)
12821    return error_mark_node;
12822
12823  releasing_vec vec;
12824  res = finish_call_expr (member, &vec,
12825			  /*disallow_virtual=*/false,
12826			  /*koenig_p=*/false,
12827			  tf_warning_or_error);
12828  return res;
12829}
12830
12831/* Parse an iteration-statement.
12832
12833   iteration-statement:
12834     while ( condition ) statement
12835     do statement while ( expression ) ;
12836     for ( init-statement condition [opt] ; expression [opt] )
12837       statement
12838
12839   Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
12840
12841static tree
12842cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12843			       unsigned short unroll)
12844{
12845  cp_token *token;
12846  enum rid keyword;
12847  tree statement;
12848  unsigned char in_statement;
12849  token_indent_info guard_tinfo;
12850
12851  /* Peek at the next token.  */
12852  token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12853  if (!token)
12854    return error_mark_node;
12855
12856  guard_tinfo = get_token_indent_info (token);
12857
12858  /* Remember whether or not we are already within an iteration
12859     statement.  */
12860  in_statement = parser->in_statement;
12861
12862  /* See what kind of keyword it is.  */
12863  keyword = token->keyword;
12864  switch (keyword)
12865    {
12866    case RID_WHILE:
12867      {
12868	tree condition;
12869
12870	/* Begin the while-statement.  */
12871	statement = begin_while_stmt ();
12872	/* Look for the `('.  */
12873	matching_parens parens;
12874	parens.require_open (parser);
12875	/* Parse the condition.  */
12876	condition = cp_parser_condition (parser);
12877	finish_while_stmt_cond (condition, statement, ivdep, unroll);
12878	/* Look for the `)'.  */
12879	parens.require_close (parser);
12880	/* Parse the dependent statement.  */
12881	parser->in_statement = IN_ITERATION_STMT;
12882	bool prev = note_iteration_stmt_body_start ();
12883	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12884	note_iteration_stmt_body_end (prev);
12885	parser->in_statement = in_statement;
12886	/* We're done with the while-statement.  */
12887	finish_while_stmt (statement);
12888      }
12889      break;
12890
12891    case RID_DO:
12892      {
12893	tree expression;
12894
12895	/* Begin the do-statement.  */
12896	statement = begin_do_stmt ();
12897	/* Parse the body of the do-statement.  */
12898	parser->in_statement = IN_ITERATION_STMT;
12899	bool prev = note_iteration_stmt_body_start ();
12900	cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12901	note_iteration_stmt_body_end (prev);
12902	parser->in_statement = in_statement;
12903	finish_do_body (statement);
12904	/* Look for the `while' keyword.  */
12905	cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12906	/* Look for the `('.  */
12907	matching_parens parens;
12908	parens.require_open (parser);
12909	/* Parse the expression.  */
12910	expression = cp_parser_expression (parser);
12911	/* We're done with the do-statement.  */
12912	finish_do_stmt (expression, statement, ivdep, unroll);
12913	/* Look for the `)'.  */
12914	parens.require_close (parser);
12915	/* Look for the `;'.  */
12916	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12917      }
12918      break;
12919
12920    case RID_FOR:
12921      {
12922	/* Look for the `('.  */
12923	matching_parens parens;
12924	parens.require_open (parser);
12925
12926	statement = cp_parser_for (parser, ivdep, unroll);
12927
12928	/* Look for the `)'.  */
12929	parens.require_close (parser);
12930
12931	/* Parse the body of the for-statement.  */
12932	parser->in_statement = IN_ITERATION_STMT;
12933	bool prev = note_iteration_stmt_body_start ();
12934	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12935	note_iteration_stmt_body_end (prev);
12936	parser->in_statement = in_statement;
12937
12938	/* We're done with the for-statement.  */
12939	finish_for_stmt (statement);
12940      }
12941      break;
12942
12943    default:
12944      cp_parser_error (parser, "expected iteration-statement");
12945      statement = error_mark_node;
12946      break;
12947    }
12948
12949  return statement;
12950}
12951
12952/* Parse a init-statement or the declarator of a range-based-for.
12953   Returns true if a range-based-for declaration is seen.
12954
12955   init-statement:
12956     expression-statement
12957     simple-declaration  */
12958
12959static bool
12960cp_parser_init_statement (cp_parser *parser, tree *decl)
12961{
12962  /* If the next token is a `;', then we have an empty
12963     expression-statement.  Grammatically, this is also a
12964     simple-declaration, but an invalid one, because it does not
12965     declare anything.  Therefore, if we did not handle this case
12966     specially, we would issue an error message about an invalid
12967     declaration.  */
12968  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12969    {
12970      bool is_range_for = false;
12971      bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12972
12973      /* Try to parse the init-statement.  */
12974      if (cp_parser_range_based_for_with_init_p (parser))
12975	{
12976	  tree dummy;
12977	  cp_parser_parse_tentatively (parser);
12978	  /* Parse the declaration.  */
12979	  cp_parser_simple_declaration (parser,
12980					/*function_definition_allowed_p=*/false,
12981					&dummy);
12982	  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12983	  if (!cp_parser_parse_definitely (parser))
12984	    /* That didn't work, try to parse it as an expression-statement.  */
12985	    cp_parser_expression_statement (parser, NULL_TREE);
12986
12987	  if (cxx_dialect < cxx2a)
12988	    {
12989	      pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12990		       "range-based %<for%> loops with initializer only "
12991		       "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12992	      *decl = error_mark_node;
12993	    }
12994	}
12995
12996      /* A colon is used in range-based for.  */
12997      parser->colon_corrects_to_scope_p = false;
12998
12999      /* We're going to speculatively look for a declaration, falling back
13000	 to an expression, if necessary.  */
13001      cp_parser_parse_tentatively (parser);
13002      /* Parse the declaration.  */
13003      cp_parser_simple_declaration (parser,
13004				    /*function_definition_allowed_p=*/false,
13005				    decl);
13006      parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13007      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13008	{
13009	  /* It is a range-for, consume the ':'.  */
13010	  cp_lexer_consume_token (parser->lexer);
13011	  is_range_for = true;
13012	  if (cxx_dialect < cxx11)
13013	    pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
13014		     "range-based %<for%> loops only available with "
13015		     "%<-std=c++11%> or %<-std=gnu++11%>");
13016	}
13017      else
13018	/* The ';' is not consumed yet because we told
13019	   cp_parser_simple_declaration not to.  */
13020	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13021
13022      if (cp_parser_parse_definitely (parser))
13023	return is_range_for;
13024      /* If the tentative parse failed, then we shall need to look for an
13025	 expression-statement.  */
13026    }
13027  /* If we are here, it is an expression-statement.  */
13028  cp_parser_expression_statement (parser, NULL_TREE);
13029  return false;
13030}
13031
13032/* Parse a jump-statement.
13033
13034   jump-statement:
13035     break ;
13036     continue ;
13037     return expression [opt] ;
13038     return braced-init-list ;
13039     coroutine-return-statement;
13040     goto identifier ;
13041
13042   GNU extension:
13043
13044   jump-statement:
13045     goto * expression ;
13046
13047   Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
13048
13049static tree
13050cp_parser_jump_statement (cp_parser* parser)
13051{
13052  tree statement = error_mark_node;
13053  cp_token *token;
13054  enum rid keyword;
13055  unsigned char in_statement;
13056
13057  /* Peek at the next token.  */
13058  token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
13059  if (!token)
13060    return error_mark_node;
13061
13062  /* See what kind of keyword it is.  */
13063  keyword = token->keyword;
13064  switch (keyword)
13065    {
13066    case RID_BREAK:
13067      in_statement = parser->in_statement & ~IN_IF_STMT;
13068      switch (in_statement)
13069	{
13070	case 0:
13071	  error_at (token->location, "break statement not within loop or switch");
13072	  break;
13073	default:
13074	  gcc_assert ((in_statement & IN_SWITCH_STMT)
13075		      || in_statement == IN_ITERATION_STMT);
13076	  statement = finish_break_stmt ();
13077	  if (in_statement == IN_ITERATION_STMT)
13078	    break_maybe_infinite_loop ();
13079	  break;
13080	case IN_OMP_BLOCK:
13081	  error_at (token->location, "invalid exit from OpenMP structured block");
13082	  break;
13083	case IN_OMP_FOR:
13084	  error_at (token->location, "break statement used with OpenMP for loop");
13085	  break;
13086	}
13087      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13088      break;
13089
13090    case RID_CONTINUE:
13091      switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
13092	{
13093	case 0:
13094	  error_at (token->location, "continue statement not within a loop");
13095	  break;
13096	  /* Fall through.  */
13097	case IN_ITERATION_STMT:
13098	case IN_OMP_FOR:
13099	  statement = finish_continue_stmt ();
13100	  break;
13101	case IN_OMP_BLOCK:
13102	  error_at (token->location, "invalid exit from OpenMP structured block");
13103	  break;
13104	default:
13105	  gcc_unreachable ();
13106	}
13107      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13108      break;
13109
13110    case RID_CO_RETURN:
13111    case RID_RETURN:
13112      {
13113	tree expr;
13114	bool expr_non_constant_p;
13115
13116	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13117	  {
13118	    cp_lexer_set_source_position (parser->lexer);
13119	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13120	    expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13121	  }
13122	else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13123	  expr = cp_parser_expression (parser);
13124	else
13125	  /* If the next token is a `;', then there is no
13126	     expression.  */
13127	  expr = NULL_TREE;
13128	/* Build the return-statement, check co-return first, since type
13129	   deduction is not valid there.  */
13130	if (keyword == RID_CO_RETURN)
13131	  statement = finish_co_return_stmt (token->location, expr);
13132	else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
13133	  /* Don't deduce from a discarded return statement.  */;
13134	else
13135	  statement = finish_return_stmt (expr);
13136	/* Look for the final `;'.  */
13137	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13138      }
13139      break;
13140
13141    case RID_GOTO:
13142      if (parser->in_function_body
13143	  && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
13144	{
13145	  error ("%<goto%> in %<constexpr%> function");
13146	  cp_function_chain->invalid_constexpr = true;
13147	}
13148
13149      /* Create the goto-statement.  */
13150      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
13151	{
13152	  /* Issue a warning about this use of a GNU extension.  */
13153	  pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
13154	  /* Consume the '*' token.  */
13155	  cp_lexer_consume_token (parser->lexer);
13156	  /* Parse the dependent expression.  */
13157	  finish_goto_stmt (cp_parser_expression (parser));
13158	}
13159      else
13160	finish_goto_stmt (cp_parser_identifier (parser));
13161      /* Look for the final `;'.  */
13162      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13163      break;
13164
13165    default:
13166      cp_parser_error (parser, "expected jump-statement");
13167      break;
13168    }
13169
13170  return statement;
13171}
13172
13173/* Parse a declaration-statement.
13174
13175   declaration-statement:
13176     block-declaration  */
13177
13178static void
13179cp_parser_declaration_statement (cp_parser* parser)
13180{
13181  void *p;
13182
13183  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
13184  p = obstack_alloc (&declarator_obstack, 0);
13185
13186 /* Parse the block-declaration.  */
13187  cp_parser_block_declaration (parser, /*statement_p=*/true);
13188
13189  /* Free any declarators allocated.  */
13190  obstack_free (&declarator_obstack, p);
13191}
13192
13193/* Some dependent statements (like `if (cond) statement'), are
13194   implicitly in their own scope.  In other words, if the statement is
13195   a single statement (as opposed to a compound-statement), it is
13196   none-the-less treated as if it were enclosed in braces.  Any
13197   declarations appearing in the dependent statement are out of scope
13198   after control passes that point.  This function parses a statement,
13199   but ensures that is in its own scope, even if it is not a
13200   compound-statement.
13201
13202   If IF_P is not NULL, *IF_P is set to indicate whether the statement
13203   is a (possibly labeled) if statement which is not enclosed in
13204   braces and has an else clause.  This is used to implement
13205   -Wparentheses.
13206
13207   CHAIN is a vector of if-else-if conditions.  This is used to implement
13208   -Wduplicated-cond.
13209
13210   Returns the new statement.  */
13211
13212static tree
13213cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
13214				       const token_indent_info &guard_tinfo,
13215				       vec<tree> *chain)
13216{
13217  tree statement;
13218  location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13219  location_t body_loc_after_labels = UNKNOWN_LOCATION;
13220  token_indent_info body_tinfo
13221    = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13222
13223  if (if_p != NULL)
13224    *if_p = false;
13225
13226  /* Mark if () ; with a special NOP_EXPR.  */
13227  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13228    {
13229      cp_lexer_consume_token (parser->lexer);
13230      statement = add_stmt (build_empty_stmt (body_loc));
13231
13232      if (guard_tinfo.keyword == RID_IF
13233	  && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13234	warning_at (body_loc, OPT_Wempty_body,
13235		    "suggest braces around empty body in an %<if%> statement");
13236      else if (guard_tinfo.keyword == RID_ELSE)
13237	warning_at (body_loc, OPT_Wempty_body,
13238		    "suggest braces around empty body in an %<else%> statement");
13239    }
13240  /* if a compound is opened, we simply parse the statement directly.  */
13241  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13242    statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13243  /* If the token is not a `{', then we must take special action.  */
13244  else
13245    {
13246      /* Create a compound-statement.  */
13247      statement = begin_compound_stmt (0);
13248      /* Parse the dependent-statement.  */
13249      cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13250			   &body_loc_after_labels);
13251      /* Finish the dummy compound-statement.  */
13252      finish_compound_stmt (statement);
13253    }
13254
13255  token_indent_info next_tinfo
13256    = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13257  warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13258
13259  if (body_loc_after_labels != UNKNOWN_LOCATION
13260      && next_tinfo.type != CPP_SEMICOLON)
13261    warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13262				    guard_tinfo.location, guard_tinfo.keyword);
13263
13264  /* Return the statement.  */
13265  return statement;
13266}
13267
13268/* For some dependent statements (like `while (cond) statement'), we
13269   have already created a scope.  Therefore, even if the dependent
13270   statement is a compound-statement, we do not want to create another
13271   scope.  */
13272
13273static void
13274cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13275				    const token_indent_info &guard_tinfo)
13276{
13277  /* If the token is a `{', then we must take special action.  */
13278  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13279    {
13280      token_indent_info body_tinfo
13281	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13282      location_t loc_after_labels = UNKNOWN_LOCATION;
13283
13284      cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13285			   &loc_after_labels);
13286      token_indent_info next_tinfo
13287	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13288      warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13289
13290      if (loc_after_labels != UNKNOWN_LOCATION
13291	  && next_tinfo.type != CPP_SEMICOLON)
13292	warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13293					guard_tinfo.location,
13294					guard_tinfo.keyword);
13295    }
13296  else
13297    {
13298      /* Avoid calling cp_parser_compound_statement, so that we
13299	 don't create a new scope.  Do everything else by hand.  */
13300      matching_braces braces;
13301      braces.require_open (parser);
13302      /* If the next keyword is `__label__' we have a label declaration.  */
13303      while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13304	cp_parser_label_declaration (parser);
13305      /* Parse an (optional) statement-seq.  */
13306      cp_parser_statement_seq_opt (parser, NULL_TREE);
13307      braces.require_close (parser);
13308    }
13309}
13310
13311/* Declarations [gram.dcl.dcl] */
13312
13313/* Parse an optional declaration-sequence.
13314
13315   declaration-seq:
13316     declaration
13317     declaration-seq declaration  */
13318
13319static void
13320cp_parser_declaration_seq_opt (cp_parser* parser)
13321{
13322  while (true)
13323    {
13324      cp_token *token = cp_lexer_peek_token (parser->lexer);
13325
13326      if (token->type == CPP_CLOSE_BRACE
13327	  || token->type == CPP_EOF)
13328	break;
13329      else
13330	cp_parser_toplevel_declaration (parser);
13331    }
13332}
13333
13334/* Parse a declaration.
13335
13336   declaration:
13337     block-declaration
13338     function-definition
13339     template-declaration
13340     explicit-instantiation
13341     explicit-specialization
13342     linkage-specification
13343     namespace-definition
13344
13345   C++17:
13346     deduction-guide
13347
13348   GNU extension:
13349
13350   declaration:
13351      __extension__ declaration */
13352
13353static void
13354cp_parser_declaration (cp_parser* parser)
13355{
13356  cp_token token1;
13357  cp_token token2;
13358  int saved_pedantic;
13359  void *p;
13360  tree attributes = NULL_TREE;
13361
13362  /* Check for the `__extension__' keyword.  */
13363  if (cp_parser_extension_opt (parser, &saved_pedantic))
13364    {
13365      /* Parse the qualified declaration.  */
13366      cp_parser_declaration (parser);
13367      /* Restore the PEDANTIC flag.  */
13368      pedantic = saved_pedantic;
13369
13370      return;
13371    }
13372
13373  /* Try to figure out what kind of declaration is present.  */
13374  token1 = *cp_lexer_peek_token (parser->lexer);
13375
13376  if (token1.type != CPP_EOF)
13377    token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13378  else
13379    {
13380      token2.type = CPP_EOF;
13381      token2.keyword = RID_MAX;
13382    }
13383
13384  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
13385  p = obstack_alloc (&declarator_obstack, 0);
13386
13387  /* If the next token is `extern' and the following token is a string
13388     literal, then we have a linkage specification.  */
13389  if (token1.keyword == RID_EXTERN
13390      && cp_parser_is_pure_string_literal (&token2))
13391    cp_parser_linkage_specification (parser);
13392  /* If the next token is `template', then we have either a template
13393     declaration, an explicit instantiation, or an explicit
13394     specialization.  */
13395  else if (token1.keyword == RID_TEMPLATE)
13396    {
13397      /* `template <>' indicates a template specialization.  */
13398      if (token2.type == CPP_LESS
13399	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13400	cp_parser_explicit_specialization (parser);
13401      /* `template <' indicates a template declaration.  */
13402      else if (token2.type == CPP_LESS)
13403	cp_parser_template_declaration (parser, /*member_p=*/false);
13404      /* Anything else must be an explicit instantiation.  */
13405      else
13406	cp_parser_explicit_instantiation (parser);
13407    }
13408  /* If the next token is `export', then we have a template
13409     declaration.  */
13410  else if (token1.keyword == RID_EXPORT)
13411    cp_parser_template_declaration (parser, /*member_p=*/false);
13412  /* If the next token is `extern', 'static' or 'inline' and the one
13413     after that is `template', we have a GNU extended explicit
13414     instantiation directive.  */
13415  else if (cp_parser_allow_gnu_extensions_p (parser)
13416	   && (token1.keyword == RID_EXTERN
13417	       || token1.keyword == RID_STATIC
13418	       || token1.keyword == RID_INLINE)
13419	   && token2.keyword == RID_TEMPLATE)
13420    cp_parser_explicit_instantiation (parser);
13421  /* If the next token is `namespace', check for a named or unnamed
13422     namespace definition.  */
13423  else if (token1.keyword == RID_NAMESPACE
13424	   && (/* A named namespace definition.  */
13425	       (token2.type == CPP_NAME
13426		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13427		    != CPP_EQ))
13428               || (token2.type == CPP_OPEN_SQUARE
13429                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13430                   == CPP_OPEN_SQUARE)
13431	       /* An unnamed namespace definition.  */
13432	       || token2.type == CPP_OPEN_BRACE
13433	       || token2.keyword == RID_ATTRIBUTE))
13434    cp_parser_namespace_definition (parser);
13435  /* An inline (associated) namespace definition.  */
13436  else if (token1.keyword == RID_INLINE
13437	   && token2.keyword == RID_NAMESPACE)
13438    cp_parser_namespace_definition (parser);
13439  /* Objective-C++ declaration/definition.  */
13440  else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13441    cp_parser_objc_declaration (parser, NULL_TREE);
13442  else if (c_dialect_objc ()
13443	   && token1.keyword == RID_ATTRIBUTE
13444	   && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13445    cp_parser_objc_declaration (parser, attributes);
13446  /* At this point we may have a template declared by a concept
13447     introduction.  */
13448  else if (flag_concepts
13449	   && cp_parser_template_declaration_after_export (parser,
13450							   /*member_p=*/false))
13451    /* We did.  */;
13452  else
13453    /* Try to parse a block-declaration, or a function-definition.  */
13454    cp_parser_block_declaration (parser, /*statement_p=*/false);
13455
13456  /* Free any declarators allocated.  */
13457  obstack_free (&declarator_obstack, p);
13458}
13459
13460/* Parse a namespace-scope declaration.  */
13461
13462static void
13463cp_parser_toplevel_declaration (cp_parser* parser)
13464{
13465  cp_token *token = cp_lexer_peek_token (parser->lexer);
13466
13467  if (token->type == CPP_PRAGMA)
13468    /* A top-level declaration can consist solely of a #pragma.  A
13469       nested declaration cannot, so this is done here and not in
13470       cp_parser_declaration.  (A #pragma at block scope is
13471       handled in cp_parser_statement.)  */
13472    cp_parser_pragma (parser, pragma_external, NULL);
13473  else if (token->type == CPP_SEMICOLON)
13474    {
13475      /* A declaration consisting of a single semicolon is
13476	 invalid.  Allow it unless we're being pedantic.  */
13477      cp_lexer_consume_token (parser->lexer);
13478      pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13479    }
13480  else
13481    /* Parse the declaration itself.  */
13482    cp_parser_declaration (parser);
13483}
13484
13485/* Parse a block-declaration.
13486
13487   block-declaration:
13488     simple-declaration
13489     asm-definition
13490     namespace-alias-definition
13491     using-declaration
13492     using-directive
13493
13494   GNU Extension:
13495
13496   block-declaration:
13497     __extension__ block-declaration
13498
13499   C++0x Extension:
13500
13501   block-declaration:
13502     static_assert-declaration
13503
13504   If STATEMENT_P is TRUE, then this block-declaration is occurring as
13505   part of a declaration-statement.  */
13506
13507static void
13508cp_parser_block_declaration (cp_parser *parser,
13509			     bool      statement_p)
13510{
13511  cp_token *token1;
13512  int saved_pedantic;
13513
13514  /* Check for the `__extension__' keyword.  */
13515  if (cp_parser_extension_opt (parser, &saved_pedantic))
13516    {
13517      /* Parse the qualified declaration.  */
13518      cp_parser_block_declaration (parser, statement_p);
13519      /* Restore the PEDANTIC flag.  */
13520      pedantic = saved_pedantic;
13521
13522      return;
13523    }
13524
13525  /* Peek at the next token to figure out which kind of declaration is
13526     present.  */
13527  token1 = cp_lexer_peek_token (parser->lexer);
13528
13529  /* If the next keyword is `asm', we have an asm-definition.  */
13530  if (token1->keyword == RID_ASM)
13531    {
13532      if (statement_p)
13533	cp_parser_commit_to_tentative_parse (parser);
13534      cp_parser_asm_definition (parser);
13535    }
13536  /* If the next keyword is `namespace', we have a
13537     namespace-alias-definition.  */
13538  else if (token1->keyword == RID_NAMESPACE)
13539    cp_parser_namespace_alias_definition (parser);
13540  /* If the next keyword is `using', we have a
13541     using-declaration, a using-directive, or an alias-declaration.  */
13542  else if (token1->keyword == RID_USING)
13543    {
13544      cp_token *token2;
13545
13546      if (statement_p)
13547	cp_parser_commit_to_tentative_parse (parser);
13548      /* If the token after `using' is `namespace', then we have a
13549	 using-directive.  */
13550      token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13551      if (token2->keyword == RID_NAMESPACE)
13552	cp_parser_using_directive (parser);
13553      /* If the second token after 'using' is '=', then we have an
13554	 alias-declaration.  */
13555      else if (cxx_dialect >= cxx11
13556	       && token2->type == CPP_NAME
13557	       && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13558		   || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13559	cp_parser_alias_declaration (parser);
13560      /* Otherwise, it's a using-declaration.  */
13561      else
13562	cp_parser_using_declaration (parser,
13563				     /*access_declaration_p=*/false);
13564    }
13565  /* If the next keyword is `__label__' we have a misplaced label
13566     declaration.  */
13567  else if (token1->keyword == RID_LABEL)
13568    {
13569      cp_lexer_consume_token (parser->lexer);
13570      error_at (token1->location, "%<__label__%> not at the beginning of a block");
13571      cp_parser_skip_to_end_of_statement (parser);
13572      /* If the next token is now a `;', consume it.  */
13573      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13574	cp_lexer_consume_token (parser->lexer);
13575    }
13576  /* If the next token is `static_assert' we have a static assertion.  */
13577  else if (token1->keyword == RID_STATIC_ASSERT)
13578    cp_parser_static_assert (parser, /*member_p=*/false);
13579  /* Anything else must be a simple-declaration.  */
13580  else
13581    cp_parser_simple_declaration (parser, !statement_p,
13582				  /*maybe_range_for_decl*/NULL);
13583}
13584
13585/* Parse a simple-declaration.
13586
13587   simple-declaration:
13588     decl-specifier-seq [opt] init-declarator-list [opt] ;
13589     decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13590       brace-or-equal-initializer ;
13591
13592   init-declarator-list:
13593     init-declarator
13594     init-declarator-list , init-declarator
13595
13596   If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13597   function-definition as a simple-declaration.
13598
13599   If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13600   parsed declaration if it is an uninitialized single declarator not followed
13601   by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13602   if present, will not be consumed.  */
13603
13604static void
13605cp_parser_simple_declaration (cp_parser* parser,
13606			      bool function_definition_allowed_p,
13607			      tree *maybe_range_for_decl)
13608{
13609  cp_decl_specifier_seq decl_specifiers;
13610  int declares_class_or_enum;
13611  bool saw_declarator;
13612  location_t comma_loc = UNKNOWN_LOCATION;
13613  location_t init_loc = UNKNOWN_LOCATION;
13614
13615  if (maybe_range_for_decl)
13616    *maybe_range_for_decl = NULL_TREE;
13617
13618  /* Defer access checks until we know what is being declared; the
13619     checks for names appearing in the decl-specifier-seq should be
13620     done as if we were in the scope of the thing being declared.  */
13621  push_deferring_access_checks (dk_deferred);
13622
13623  /* Parse the decl-specifier-seq.  We have to keep track of whether
13624     or not the decl-specifier-seq declares a named class or
13625     enumeration type, since that is the only case in which the
13626     init-declarator-list is allowed to be empty.
13627
13628     [dcl.dcl]
13629
13630     In a simple-declaration, the optional init-declarator-list can be
13631     omitted only when declaring a class or enumeration, that is when
13632     the decl-specifier-seq contains either a class-specifier, an
13633     elaborated-type-specifier, or an enum-specifier.  */
13634  cp_parser_decl_specifier_seq (parser,
13635				CP_PARSER_FLAGS_OPTIONAL,
13636				&decl_specifiers,
13637				&declares_class_or_enum);
13638  /* We no longer need to defer access checks.  */
13639  stop_deferring_access_checks ();
13640
13641  /* In a block scope, a valid declaration must always have a
13642     decl-specifier-seq.  By not trying to parse declarators, we can
13643     resolve the declaration/expression ambiguity more quickly.  */
13644  if (!function_definition_allowed_p
13645      && !decl_specifiers.any_specifiers_p)
13646    {
13647      cp_parser_error (parser, "expected declaration");
13648      goto done;
13649    }
13650
13651  /* If the next two tokens are both identifiers, the code is
13652     erroneous. The usual cause of this situation is code like:
13653
13654       T t;
13655
13656     where "T" should name a type -- but does not.  */
13657  if (!decl_specifiers.any_type_specifiers_p
13658      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13659    {
13660      /* If parsing tentatively, we should commit; we really are
13661	 looking at a declaration.  */
13662      cp_parser_commit_to_tentative_parse (parser);
13663      /* Give up.  */
13664      goto done;
13665    }
13666
13667  cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
13668
13669  /* Look for C++17 decomposition declaration.  */
13670  for (size_t n = 1; ; n++)
13671    if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13672	|| cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13673      continue;
13674    else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13675	     && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13676	     && decl_specifiers.any_specifiers_p)
13677      {
13678	tree decl
13679	  = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13680						 maybe_range_for_decl,
13681						 &init_loc);
13682
13683	/* The next token should be either a `,' or a `;'.  */
13684	cp_token *token = cp_lexer_peek_token (parser->lexer);
13685	/* If it's a `;', we are done.  */
13686	if (token->type == CPP_SEMICOLON)
13687	  goto finish;
13688	else if (maybe_range_for_decl)
13689	  {
13690	    if (*maybe_range_for_decl == NULL_TREE)
13691	      *maybe_range_for_decl = error_mark_node;
13692	    goto finish;
13693	  }
13694	/* Anything else is an error.  */
13695	else
13696	  {
13697	    /* If we have already issued an error message we don't need
13698	       to issue another one.  */
13699	    if ((decl != error_mark_node
13700		 && DECL_INITIAL (decl) != error_mark_node)
13701		|| cp_parser_uncommitted_to_tentative_parse_p (parser))
13702	      cp_parser_error (parser, "expected %<;%>");
13703	    /* Skip tokens until we reach the end of the statement.  */
13704	    cp_parser_skip_to_end_of_statement (parser);
13705	    /* If the next token is now a `;', consume it.  */
13706	    if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13707	      cp_lexer_consume_token (parser->lexer);
13708	    goto done;
13709	  }
13710      }
13711    else
13712      break;
13713
13714  tree last_type;
13715  bool auto_specifier_p;
13716  /* NULL_TREE if both variable and function declaration are allowed,
13717     error_mark_node if function declaration are not allowed and
13718     a FUNCTION_DECL that should be diagnosed if it is followed by
13719     variable declarations.  */
13720  tree auto_function_declaration;
13721
13722  last_type = NULL_TREE;
13723  auto_specifier_p
13724    = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13725  auto_function_declaration = NULL_TREE;
13726
13727  /* Keep going until we hit the `;' at the end of the simple
13728     declaration.  */
13729  saw_declarator = false;
13730  while (cp_lexer_next_token_is_not (parser->lexer,
13731				     CPP_SEMICOLON))
13732    {
13733      cp_token *token;
13734      bool function_definition_p;
13735      tree decl;
13736      tree auto_result = NULL_TREE;
13737
13738      if (saw_declarator)
13739	{
13740	  /* If we are processing next declarator, comma is expected */
13741	  token = cp_lexer_peek_token (parser->lexer);
13742	  gcc_assert (token->type == CPP_COMMA);
13743	  cp_lexer_consume_token (parser->lexer);
13744	  if (maybe_range_for_decl)
13745	    {
13746	      *maybe_range_for_decl = error_mark_node;
13747	      if (comma_loc == UNKNOWN_LOCATION)
13748		comma_loc = token->location;
13749	    }
13750	}
13751      else
13752	saw_declarator = true;
13753
13754      /* Parse the init-declarator.  */
13755      decl = cp_parser_init_declarator (parser,
13756					CP_PARSER_FLAGS_NONE,
13757					&decl_specifiers,
13758					/*checks=*/NULL,
13759					function_definition_allowed_p,
13760					/*member_p=*/false,
13761					declares_class_or_enum,
13762					&function_definition_p,
13763					maybe_range_for_decl,
13764					&init_loc,
13765					&auto_result);
13766      /* If an error occurred while parsing tentatively, exit quickly.
13767	 (That usually happens when in the body of a function; each
13768	 statement is treated as a declaration-statement until proven
13769	 otherwise.)  */
13770      if (cp_parser_error_occurred (parser))
13771	goto done;
13772
13773      if (auto_specifier_p && cxx_dialect >= cxx14)
13774	{
13775	  /* If the init-declarator-list contains more than one
13776	     init-declarator, they shall all form declarations of
13777	     variables.  */
13778	  if (auto_function_declaration == NULL_TREE)
13779	    auto_function_declaration
13780	      = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13781	  else if (TREE_CODE (decl) == FUNCTION_DECL
13782		   || auto_function_declaration != error_mark_node)
13783	    {
13784	      error_at (decl_specifiers.locations[ds_type_spec],
13785			"non-variable %qD in declaration with more than one "
13786			"declarator with placeholder type",
13787			TREE_CODE (decl) == FUNCTION_DECL
13788			? decl : auto_function_declaration);
13789	      auto_function_declaration = error_mark_node;
13790	    }
13791	}
13792
13793      if (auto_result
13794	  && (!processing_template_decl || !type_uses_auto (auto_result)))
13795	{
13796	  if (last_type
13797	      && last_type != error_mark_node
13798	      && !same_type_p (auto_result, last_type))
13799	    {
13800	      /* If the list of declarators contains more than one declarator,
13801		 the type of each declared variable is determined as described
13802		 above. If the type deduced for the template parameter U is not
13803		 the same in each deduction, the program is ill-formed.  */
13804	      error_at (decl_specifiers.locations[ds_type_spec],
13805			"inconsistent deduction for %qT: %qT and then %qT",
13806			decl_specifiers.type, last_type, auto_result);
13807	      last_type = error_mark_node;
13808	    }
13809	  else
13810	    last_type = auto_result;
13811	}
13812
13813      /* Handle function definitions specially.  */
13814      if (function_definition_p)
13815	{
13816	  /* If the next token is a `,', then we are probably
13817	     processing something like:
13818
13819	       void f() {}, *p;
13820
13821	     which is erroneous.  */
13822	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13823	    {
13824	      cp_token *token = cp_lexer_peek_token (parser->lexer);
13825	      error_at (token->location,
13826			"mixing"
13827			" declarations and function-definitions is forbidden");
13828	    }
13829	  /* Otherwise, we're done with the list of declarators.  */
13830	  else
13831	    {
13832	      pop_deferring_access_checks ();
13833	      return;
13834	    }
13835	}
13836      if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13837	*maybe_range_for_decl = decl;
13838      /* The next token should be either a `,' or a `;'.  */
13839      token = cp_lexer_peek_token (parser->lexer);
13840      /* If it's a `,', there are more declarators to come.  */
13841      if (token->type == CPP_COMMA)
13842	/* will be consumed next time around */;
13843      /* If it's a `;', we are done.  */
13844      else if (token->type == CPP_SEMICOLON)
13845	break;
13846      else if (maybe_range_for_decl)
13847	{
13848	  if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13849	    permerror (decl_specifiers.locations[ds_type_spec],
13850		       "types may not be defined in a for-range-declaration");
13851	  break;
13852	}
13853      /* Anything else is an error.  */
13854      else
13855	{
13856	  /* If we have already issued an error message we don't need
13857	     to issue another one.  */
13858	  if ((decl != error_mark_node
13859	       && DECL_INITIAL (decl) != error_mark_node)
13860	      || cp_parser_uncommitted_to_tentative_parse_p (parser))
13861	    cp_parser_error (parser, "expected %<,%> or %<;%>");
13862	  /* Skip tokens until we reach the end of the statement.  */
13863	  cp_parser_skip_to_end_of_statement (parser);
13864	  /* If the next token is now a `;', consume it.  */
13865	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13866	    cp_lexer_consume_token (parser->lexer);
13867	  goto done;
13868	}
13869      /* After the first time around, a function-definition is not
13870	 allowed -- even if it was OK at first.  For example:
13871
13872	   int i, f() {}
13873
13874	 is not valid.  */
13875      function_definition_allowed_p = false;
13876    }
13877
13878  /* Issue an error message if no declarators are present, and the
13879     decl-specifier-seq does not itself declare a class or
13880     enumeration: [dcl.dcl]/3.  */
13881  if (!saw_declarator)
13882    {
13883      if (cp_parser_declares_only_class_p (parser))
13884	{
13885	  if (!declares_class_or_enum
13886	      && decl_specifiers.type
13887	      && OVERLOAD_TYPE_P (decl_specifiers.type))
13888	    /* Ensure an error is issued anyway when finish_decltype_type,
13889	       called via cp_parser_decl_specifier_seq, returns a class or
13890	       an enumeration (c++/51786).  */
13891	    decl_specifiers.type = NULL_TREE;
13892	  shadow_tag (&decl_specifiers);
13893	}
13894      /* Perform any deferred access checks.  */
13895      perform_deferred_access_checks (tf_warning_or_error);
13896    }
13897
13898  /* Consume the `;'.  */
13899 finish:
13900  if (!maybe_range_for_decl)
13901    cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13902  else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13903    {
13904      if (init_loc != UNKNOWN_LOCATION)
13905	error_at (init_loc, "initializer in range-based %<for%> loop");
13906      if (comma_loc != UNKNOWN_LOCATION)
13907	error_at (comma_loc,
13908		  "multiple declarations in range-based %<for%> loop");
13909    }
13910
13911 done:
13912  pop_deferring_access_checks ();
13913}
13914
13915/* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13916     decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13917       initializer ;  */
13918
13919static tree
13920cp_parser_decomposition_declaration (cp_parser *parser,
13921				     cp_decl_specifier_seq *decl_specifiers,
13922				     tree *maybe_range_for_decl,
13923				     location_t *init_loc)
13924{
13925  cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13926  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13927  cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13928
13929  /* Parse the identifier-list.  */
13930  auto_vec<cp_expr, 10> v;
13931  if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13932    while (true)
13933      {
13934	cp_expr e = cp_parser_identifier (parser);
13935	if (e.get_value () == error_mark_node)
13936	  break;
13937	v.safe_push (e);
13938	if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13939	  break;
13940	cp_lexer_consume_token (parser->lexer);
13941      }
13942
13943  location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13944  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13945    {
13946      end_loc = UNKNOWN_LOCATION;
13947      cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13948					       false);
13949      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13950	cp_lexer_consume_token (parser->lexer);
13951      else
13952	{
13953	  cp_parser_skip_to_end_of_statement (parser);
13954	  return error_mark_node;
13955	}
13956    }
13957
13958  if (cxx_dialect < cxx17)
13959    pedwarn (loc, 0, "structured bindings only available with "
13960		     "%<-std=c++17%> or %<-std=gnu++17%>");
13961
13962  tree pushed_scope;
13963  cp_declarator *declarator = make_declarator (cdk_decomp);
13964  loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13965  declarator->id_loc = loc;
13966  if (ref_qual != REF_QUAL_NONE)
13967    declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13968					    ref_qual == REF_QUAL_RVALUE,
13969					    NULL_TREE);
13970  tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13971			  NULL_TREE, decl_specifiers->attributes,
13972			  &pushed_scope);
13973  tree orig_decl = decl;
13974
13975  unsigned int i;
13976  cp_expr e;
13977  cp_decl_specifier_seq decl_specs;
13978  clear_decl_specs (&decl_specs);
13979  decl_specs.type = make_auto ();
13980  tree prev = decl;
13981  FOR_EACH_VEC_ELT (v, i, e)
13982    {
13983      if (i == 0)
13984	declarator = make_id_declarator (NULL_TREE, e.get_value (),
13985					 sfk_none, e.get_location ());
13986      else
13987	{
13988	  declarator->u.id.unqualified_name = e.get_value ();
13989	  declarator->id_loc = e.get_location ();
13990	}
13991      tree elt_pushed_scope;
13992      tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13993			       NULL_TREE, NULL_TREE, &elt_pushed_scope);
13994      if (decl2 == error_mark_node)
13995	decl = error_mark_node;
13996      else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13997	{
13998	  /* Ensure we've diagnosed redeclaration if we aren't creating
13999	     a new VAR_DECL.  */
14000	  gcc_assert (errorcount);
14001	  decl = error_mark_node;
14002	}
14003      else
14004	prev = decl2;
14005      if (elt_pushed_scope)
14006	pop_scope (elt_pushed_scope);
14007    }
14008
14009  if (v.is_empty ())
14010    {
14011      error_at (loc, "empty structured binding declaration");
14012      decl = error_mark_node;
14013    }
14014
14015  if (maybe_range_for_decl == NULL
14016      || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14017    {
14018      bool non_constant_p = false, is_direct_init = false;
14019      *init_loc = cp_lexer_peek_token (parser->lexer)->location;
14020      tree initializer = cp_parser_initializer (parser, &is_direct_init,
14021						&non_constant_p);
14022      if (initializer == NULL_TREE
14023	  || (TREE_CODE (initializer) == TREE_LIST
14024	      && TREE_CHAIN (initializer))
14025	  || (is_direct_init
14026	      && BRACE_ENCLOSED_INITIALIZER_P (initializer)
14027	      && CONSTRUCTOR_NELTS (initializer) != 1))
14028	{
14029	  error_at (loc, "invalid initializer for structured binding "
14030		    "declaration");
14031	  initializer = error_mark_node;
14032	}
14033
14034      if (decl != error_mark_node)
14035	{
14036	  int flags = (decl_spec_seq_has_spec_p (decl_specifiers, ds_constinit)
14037		       ? LOOKUP_CONSTINIT : 0);
14038	  cp_maybe_mangle_decomp (decl, prev, v.length ());
14039	  cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
14040			  (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT)
14041			  | flags);
14042	  cp_finish_decomp (decl, prev, v.length ());
14043	}
14044    }
14045  else if (decl != error_mark_node)
14046    {
14047      *maybe_range_for_decl = prev;
14048      /* Ensure DECL_VALUE_EXPR is created for all the decls but
14049	 the underlying DECL.  */
14050      cp_finish_decomp (decl, prev, v.length ());
14051    }
14052
14053  if (pushed_scope)
14054    pop_scope (pushed_scope);
14055
14056  if (decl == error_mark_node && DECL_P (orig_decl))
14057    {
14058      if (DECL_NAMESPACE_SCOPE_P (orig_decl))
14059	SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
14060    }
14061
14062  return decl;
14063}
14064
14065/* Parse a decl-specifier-seq.
14066
14067   decl-specifier-seq:
14068     decl-specifier-seq [opt] decl-specifier
14069     decl-specifier attribute-specifier-seq [opt] (C++11)
14070
14071   decl-specifier:
14072     storage-class-specifier
14073     type-specifier
14074     function-specifier
14075     friend
14076     typedef
14077
14078   GNU Extension:
14079
14080   decl-specifier:
14081     attributes
14082
14083   Concepts Extension:
14084
14085   decl-specifier:
14086     concept
14087
14088   Set *DECL_SPECS to a representation of the decl-specifier-seq.
14089
14090   The parser flags FLAGS is used to control type-specifier parsing.
14091
14092   *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
14093   flags:
14094
14095     1: one of the decl-specifiers is an elaborated-type-specifier
14096	(i.e., a type declaration)
14097     2: one of the decl-specifiers is an enum-specifier or a
14098	class-specifier (i.e., a type definition)
14099
14100   */
14101
14102static void
14103cp_parser_decl_specifier_seq (cp_parser* parser,
14104			      cp_parser_flags flags,
14105			      cp_decl_specifier_seq *decl_specs,
14106			      int* declares_class_or_enum)
14107{
14108  bool constructor_possible_p = !parser->in_declarator_p;
14109  bool found_decl_spec = false;
14110  cp_token *start_token = NULL;
14111  cp_decl_spec ds;
14112
14113  /* Clear DECL_SPECS.  */
14114  clear_decl_specs (decl_specs);
14115
14116  /* Assume no class or enumeration type is declared.  */
14117  *declares_class_or_enum = 0;
14118
14119  /* Keep reading specifiers until there are no more to read.  */
14120  while (true)
14121    {
14122      bool constructor_p;
14123      cp_token *token;
14124      ds = ds_last;
14125
14126      /* Peek at the next token.  */
14127      token = cp_lexer_peek_token (parser->lexer);
14128
14129      /* Save the first token of the decl spec list for error
14130         reporting.  */
14131      if (!start_token)
14132	start_token = token;
14133      /* Handle attributes.  */
14134      if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
14135	  && cp_next_tokens_can_be_attribute_p (parser))
14136	{
14137	  /* Parse the attributes.  */
14138	  tree attrs = cp_parser_attributes_opt (parser);
14139
14140	  /* In a sequence of declaration specifiers, c++11 attributes
14141	     appertain to the type that precede them. In that case
14142	     [dcl.spec]/1 says:
14143
14144	         The attribute-specifier-seq affects the type only for
14145		 the declaration it appears in, not other declarations
14146		 involving the same type.
14147
14148             But for now let's force the user to position the
14149             attribute either at the beginning of the declaration or
14150             after the declarator-id, which would clearly mean that it
14151             applies to the declarator.  */
14152	  if (cxx11_attribute_p (attrs))
14153	    {
14154	      if (!found_decl_spec)
14155		/* The c++11 attribute is at the beginning of the
14156		   declaration.  It appertains to the entity being
14157		   declared.  */;
14158	      else
14159		{
14160		  if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
14161		    {
14162		      /*  This is an attribute following a
14163			  class-specifier.  */
14164		      if (decl_specs->type_definition_p)
14165			warn_misplaced_attr_for_class_type (token->location,
14166							    decl_specs->type);
14167		      attrs = NULL_TREE;
14168		    }
14169		  else
14170		    {
14171		      decl_specs->std_attributes
14172			= attr_chainon (decl_specs->std_attributes, attrs);
14173		      if (decl_specs->locations[ds_std_attribute] == 0)
14174			decl_specs->locations[ds_std_attribute] = token->location;
14175		    }
14176		  continue;
14177		}
14178	    }
14179
14180	  decl_specs->attributes
14181	    = attr_chainon (decl_specs->attributes, attrs);
14182	  if (decl_specs->locations[ds_attribute] == 0)
14183	    decl_specs->locations[ds_attribute] = token->location;
14184	  continue;
14185	}
14186      /* Assume we will find a decl-specifier keyword.  */
14187      found_decl_spec = true;
14188      /* If the next token is an appropriate keyword, we can simply
14189	 add it to the list.  */
14190      switch (token->keyword)
14191	{
14192	  /* decl-specifier:
14193	       friend
14194	       constexpr
14195	       constinit */
14196	case RID_FRIEND:
14197	  if (!at_class_scope_p ())
14198	    {
14199	      gcc_rich_location richloc (token->location);
14200	      richloc.add_fixit_remove ();
14201	      error_at (&richloc, "%<friend%> used outside of class");
14202	      cp_lexer_purge_token (parser->lexer);
14203	    }
14204	  else
14205	    {
14206	      ds = ds_friend;
14207	      /* Consume the token.  */
14208	      cp_lexer_consume_token (parser->lexer);
14209	    }
14210	  break;
14211
14212        case RID_CONSTEXPR:
14213	  ds = ds_constexpr;
14214          cp_lexer_consume_token (parser->lexer);
14215          break;
14216
14217	case RID_CONSTINIT:
14218	  ds = ds_constinit;
14219	  cp_lexer_consume_token (parser->lexer);
14220	  break;
14221
14222	case RID_CONSTEVAL:
14223	  ds = ds_consteval;
14224	  cp_lexer_consume_token (parser->lexer);
14225	  break;
14226
14227        case RID_CONCEPT:
14228          ds = ds_concept;
14229          cp_lexer_consume_token (parser->lexer);
14230
14231	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14232	    break;
14233
14234          /* Warn for concept as a decl-specifier. We'll rewrite these as
14235             concept declarations later.  */
14236          if (!flag_concepts_ts)
14237            {
14238	      cp_token *next = cp_lexer_peek_token (parser->lexer);
14239	      if (next->keyword == RID_BOOL)
14240		pedwarn (next->location, 0, "the %<bool%> keyword is not "
14241			 "allowed in a C++20 concept definition");
14242	      else
14243		pedwarn (token->location, 0, "C++20 concept definition syntax "
14244			 "is %<concept <name> = <expr>%>");
14245            }
14246
14247	  /* In C++20 a concept definition is just 'concept name = expr;'
14248	     Support that syntax as a TS extension by pretending we've seen
14249	     the 'bool' specifier.  */
14250	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14251	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
14252	      && !decl_specs->any_type_specifiers_p)
14253	    {
14254	      cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14255					    token, /*type_definition*/false);
14256	      decl_specs->any_type_specifiers_p = true;
14257	    }
14258          break;
14259
14260	  /* function-specifier:
14261	       inline
14262	       virtual
14263	       explicit  */
14264	case RID_INLINE:
14265	case RID_VIRTUAL:
14266	case RID_EXPLICIT:
14267	  cp_parser_function_specifier_opt (parser, decl_specs);
14268	  break;
14269
14270	  /* decl-specifier:
14271	       typedef  */
14272	case RID_TYPEDEF:
14273	  ds = ds_typedef;
14274	  /* Consume the token.  */
14275	  cp_lexer_consume_token (parser->lexer);
14276
14277	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14278	    break;
14279
14280	  /* A constructor declarator cannot appear in a typedef.  */
14281	  constructor_possible_p = false;
14282	  /* The "typedef" keyword can only occur in a declaration; we
14283	     may as well commit at this point.  */
14284	  cp_parser_commit_to_tentative_parse (parser);
14285
14286          if (decl_specs->storage_class != sc_none)
14287            decl_specs->conflicting_specifiers_p = true;
14288	  break;
14289
14290	  /* storage-class-specifier:
14291	       auto
14292	       register
14293	       static
14294	       extern
14295	       mutable
14296
14297	     GNU Extension:
14298	       thread  */
14299	case RID_AUTO:
14300          if (cxx_dialect == cxx98)
14301            {
14302	      /* Consume the token.  */
14303	      cp_lexer_consume_token (parser->lexer);
14304
14305	      /* Complain about `auto' as a storage specifier, if
14306		 we're complaining about C++0x compatibility.  */
14307	      gcc_rich_location richloc (token->location);
14308	      richloc.add_fixit_remove ();
14309	      warning_at (&richloc, OPT_Wc__11_compat,
14310			  "%<auto%> changes meaning in C++11; "
14311			  "please remove it");
14312
14313              /* Set the storage class anyway.  */
14314              cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14315					   token);
14316            }
14317          else
14318	    /* C++0x auto type-specifier.  */
14319	    found_decl_spec = false;
14320          break;
14321
14322	case RID_REGISTER:
14323	case RID_STATIC:
14324	case RID_EXTERN:
14325	case RID_MUTABLE:
14326	  /* Consume the token.  */
14327	  cp_lexer_consume_token (parser->lexer);
14328          cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14329				       token);
14330	  break;
14331	case RID_THREAD:
14332	  /* Consume the token.  */
14333	  ds = ds_thread;
14334	  cp_lexer_consume_token (parser->lexer);
14335	  break;
14336
14337	default:
14338	  /* We did not yet find a decl-specifier yet.  */
14339	  found_decl_spec = false;
14340	  break;
14341	}
14342
14343      if (found_decl_spec
14344	  && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14345	  && token->keyword != RID_CONSTEXPR)
14346	error ("%<decl-specifier%> invalid in condition");
14347
14348      if (found_decl_spec
14349	  && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14350	  && token->keyword != RID_MUTABLE
14351	  && token->keyword != RID_CONSTEXPR
14352	  && token->keyword != RID_CONSTEVAL)
14353	error_at (token->location, "%qD invalid in lambda",
14354		  ridpointers[token->keyword]);
14355
14356      if (ds != ds_last)
14357	set_and_check_decl_spec_loc (decl_specs, ds, token);
14358
14359      /* Constructors are a special case.  The `S' in `S()' is not a
14360	 decl-specifier; it is the beginning of the declarator.  */
14361      constructor_p
14362	= (!found_decl_spec
14363	   && constructor_possible_p
14364	   && (cp_parser_constructor_declarator_p
14365	       (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14366							 ds_friend))));
14367
14368      /* If we don't have a DECL_SPEC yet, then we must be looking at
14369	 a type-specifier.  */
14370      if (!found_decl_spec && !constructor_p)
14371	{
14372	  int decl_spec_declares_class_or_enum;
14373	  bool is_cv_qualifier;
14374	  tree type_spec;
14375
14376	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14377	    flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14378
14379	  type_spec
14380	    = cp_parser_type_specifier (parser, flags,
14381					decl_specs,
14382					/*is_declaration=*/true,
14383					&decl_spec_declares_class_or_enum,
14384					&is_cv_qualifier);
14385	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14386
14387	  /* If this type-specifier referenced a user-defined type
14388	     (a typedef, class-name, etc.), then we can't allow any
14389	     more such type-specifiers henceforth.
14390
14391	     [dcl.spec]
14392
14393	     The longest sequence of decl-specifiers that could
14394	     possibly be a type name is taken as the
14395	     decl-specifier-seq of a declaration.  The sequence shall
14396	     be self-consistent as described below.
14397
14398	     [dcl.type]
14399
14400	     As a general rule, at most one type-specifier is allowed
14401	     in the complete decl-specifier-seq of a declaration.  The
14402	     only exceptions are the following:
14403
14404	     -- const or volatile can be combined with any other
14405		type-specifier.
14406
14407	     -- signed or unsigned can be combined with char, long,
14408		short, or int.
14409
14410	     -- ..
14411
14412	     Example:
14413
14414	       typedef char* Pc;
14415	       void g (const int Pc);
14416
14417	     Here, Pc is *not* part of the decl-specifier seq; it's
14418	     the declarator.  Therefore, once we see a type-specifier
14419	     (other than a cv-qualifier), we forbid any additional
14420	     user-defined types.  We *do* still allow things like `int
14421	     int' to be considered a decl-specifier-seq, and issue the
14422	     error message later.  */
14423	  if (type_spec && !is_cv_qualifier)
14424	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14425	  /* A constructor declarator cannot follow a type-specifier.  */
14426	  if (type_spec)
14427	    {
14428	      constructor_possible_p = false;
14429	      found_decl_spec = true;
14430	      if (!is_cv_qualifier)
14431		decl_specs->any_type_specifiers_p = true;
14432
14433	      if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14434		error_at (token->location, "type-specifier invalid in lambda");
14435	    }
14436	}
14437
14438      /* If we still do not have a DECL_SPEC, then there are no more
14439	 decl-specifiers.  */
14440      if (!found_decl_spec)
14441	break;
14442
14443      decl_specs->any_specifiers_p = true;
14444      /* After we see one decl-specifier, further decl-specifiers are
14445	 always optional.  */
14446      flags |= CP_PARSER_FLAGS_OPTIONAL;
14447    }
14448
14449  /* Don't allow a friend specifier with a class definition.  */
14450  if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14451      && (*declares_class_or_enum & 2))
14452    error_at (decl_specs->locations[ds_friend],
14453	      "class definition may not be declared a friend");
14454}
14455
14456/* Parse an (optional) storage-class-specifier.
14457
14458   storage-class-specifier:
14459     auto
14460     register
14461     static
14462     extern
14463     mutable
14464
14465   GNU Extension:
14466
14467   storage-class-specifier:
14468     thread
14469
14470   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
14471
14472static tree
14473cp_parser_storage_class_specifier_opt (cp_parser* parser)
14474{
14475  switch (cp_lexer_peek_token (parser->lexer)->keyword)
14476    {
14477    case RID_AUTO:
14478      if (cxx_dialect != cxx98)
14479        return NULL_TREE;
14480      /* Fall through for C++98.  */
14481      gcc_fallthrough ();
14482
14483    case RID_REGISTER:
14484    case RID_STATIC:
14485    case RID_EXTERN:
14486    case RID_MUTABLE:
14487    case RID_THREAD:
14488      /* Consume the token.  */
14489      return cp_lexer_consume_token (parser->lexer)->u.value;
14490
14491    default:
14492      return NULL_TREE;
14493    }
14494}
14495
14496/* Parse an (optional) function-specifier.
14497
14498   function-specifier:
14499     inline
14500     virtual
14501     explicit
14502
14503   C++2A Extension:
14504     explicit(constant-expression)
14505
14506   Returns an IDENTIFIER_NODE corresponding to the keyword used.
14507   Updates DECL_SPECS, if it is non-NULL.  */
14508
14509static tree
14510cp_parser_function_specifier_opt (cp_parser* parser,
14511				  cp_decl_specifier_seq *decl_specs)
14512{
14513  cp_token *token = cp_lexer_peek_token (parser->lexer);
14514  switch (token->keyword)
14515    {
14516    case RID_INLINE:
14517      set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14518      break;
14519
14520    case RID_VIRTUAL:
14521      /* 14.5.2.3 [temp.mem]
14522
14523	 A member function template shall not be virtual.  */
14524      if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14525	  && current_class_type)
14526	error_at (token->location, "templates may not be %<virtual%>");
14527      else
14528	set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14529      break;
14530
14531    case RID_EXPLICIT:
14532      {
14533	tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14534	/* If we see '(', it's C++20 explicit(bool).  */
14535	tree expr;
14536	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14537	  {
14538	    matching_parens parens;
14539	    parens.consume_open (parser);
14540
14541	    /* New types are not allowed in an explicit-specifier.  */
14542	    const char *saved_message
14543	      = parser->type_definition_forbidden_message;
14544	    parser->type_definition_forbidden_message
14545	      = G_("types may not be defined in explicit-specifier");
14546
14547	    if (cxx_dialect < cxx2a)
14548	      pedwarn (token->location, 0,
14549		       "%<explicit(bool)%> only available with %<-std=c++2a%> "
14550		       "or %<-std=gnu++2a%>");
14551
14552	    /* Parse the constant-expression.  */
14553	    expr = cp_parser_constant_expression (parser);
14554
14555	    /* Restore the saved message.  */
14556	    parser->type_definition_forbidden_message = saved_message;
14557	    parens.require_close (parser);
14558	  }
14559	else
14560	  /* The explicit-specifier explicit without a constant-expression is
14561	     equivalent to the explicit-specifier explicit(true).  */
14562	  expr = boolean_true_node;
14563
14564	/* [dcl.fct.spec]
14565	   "the constant-expression, if supplied, shall be a contextually
14566	   converted constant expression of type bool."  */
14567	expr = build_explicit_specifier (expr, tf_warning_or_error);
14568	/* We could evaluate it -- mark the decl as appropriate.  */
14569	if (expr == boolean_true_node)
14570	  set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14571	else if (expr == boolean_false_node)
14572	  /* Don't mark the decl as explicit.  */;
14573	else if (decl_specs)
14574	  /* The expression was value-dependent.  Remember it so that we can
14575	     substitute it later.  */
14576	  decl_specs->explicit_specifier = expr;
14577	return id;
14578      }
14579
14580    default:
14581      return NULL_TREE;
14582    }
14583
14584  /* Consume the token.  */
14585  return cp_lexer_consume_token (parser->lexer)->u.value;
14586}
14587
14588/* Parse a linkage-specification.
14589
14590   linkage-specification:
14591     extern string-literal { declaration-seq [opt] }
14592     extern string-literal declaration  */
14593
14594static void
14595cp_parser_linkage_specification (cp_parser* parser)
14596{
14597  tree linkage;
14598
14599  /* Look for the `extern' keyword.  */
14600  cp_token *extern_token
14601    = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14602
14603  /* Look for the string-literal.  */
14604  cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14605  linkage = cp_parser_string_literal (parser, false, false);
14606
14607  /* Transform the literal into an identifier.  If the literal is a
14608     wide-character string, or contains embedded NULs, then we can't
14609     handle it as the user wants.  */
14610  if (strlen (TREE_STRING_POINTER (linkage))
14611      != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14612    {
14613      cp_parser_error (parser, "invalid linkage-specification");
14614      /* Assume C++ linkage.  */
14615      linkage = lang_name_cplusplus;
14616    }
14617  else
14618    linkage = get_identifier (TREE_STRING_POINTER (linkage));
14619
14620  /* We're now using the new linkage.  */
14621  push_lang_context (linkage);
14622
14623  /* Preserve the location of the innermost linkage specification,
14624     tracking the locations of nested specifications via a local.  */
14625  location_t saved_location
14626    = parser->innermost_linkage_specification_location;
14627  /* Construct a location ranging from the start of the "extern" to
14628     the end of the string-literal, with the caret at the start, e.g.:
14629       extern "C" {
14630       ^~~~~~~~~~
14631  */
14632  parser->innermost_linkage_specification_location
14633    = make_location (extern_token->location,
14634		     extern_token->location,
14635		     get_finish (string_token->location));
14636
14637  /* If the next token is a `{', then we're using the first
14638     production.  */
14639  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14640    {
14641      cp_ensure_no_omp_declare_simd (parser);
14642      cp_ensure_no_oacc_routine (parser);
14643
14644      /* Consume the `{' token.  */
14645      matching_braces braces;
14646      braces.consume_open (parser);
14647      /* Parse the declarations.  */
14648      cp_parser_declaration_seq_opt (parser);
14649      /* Look for the closing `}'.  */
14650      braces.require_close (parser);
14651    }
14652  /* Otherwise, there's just one declaration.  */
14653  else
14654    {
14655      bool saved_in_unbraced_linkage_specification_p;
14656
14657      saved_in_unbraced_linkage_specification_p
14658	= parser->in_unbraced_linkage_specification_p;
14659      parser->in_unbraced_linkage_specification_p = true;
14660      cp_parser_declaration (parser);
14661      parser->in_unbraced_linkage_specification_p
14662	= saved_in_unbraced_linkage_specification_p;
14663    }
14664
14665  /* We're done with the linkage-specification.  */
14666  pop_lang_context ();
14667
14668  /* Restore location of parent linkage specification, if any.  */
14669  parser->innermost_linkage_specification_location = saved_location;
14670}
14671
14672/* Parse a static_assert-declaration.
14673
14674   static_assert-declaration:
14675     static_assert ( constant-expression , string-literal ) ;
14676     static_assert ( constant-expression ) ; (C++17)
14677
14678   If MEMBER_P, this static_assert is a class member.  */
14679
14680static void
14681cp_parser_static_assert(cp_parser *parser, bool member_p)
14682{
14683  cp_expr condition;
14684  location_t token_loc;
14685  tree message;
14686  bool dummy;
14687
14688  /* Peek at the `static_assert' token so we can keep track of exactly
14689     where the static assertion started.  */
14690  token_loc = cp_lexer_peek_token (parser->lexer)->location;
14691
14692  /* Look for the `static_assert' keyword.  */
14693  if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14694                                  RT_STATIC_ASSERT))
14695    return;
14696
14697  /*  We know we are in a static assertion; commit to any tentative
14698      parse.  */
14699  if (cp_parser_parsing_tentatively (parser))
14700    cp_parser_commit_to_tentative_parse (parser);
14701
14702  /* Parse the `(' starting the static assertion condition.  */
14703  matching_parens parens;
14704  parens.require_open (parser);
14705
14706  /* Parse the constant-expression.  Allow a non-constant expression
14707     here in order to give better diagnostics in finish_static_assert.  */
14708  condition =
14709    cp_parser_constant_expression (parser,
14710                                   /*allow_non_constant_p=*/true,
14711                                   /*non_constant_p=*/&dummy);
14712
14713  if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14714    {
14715      if (cxx_dialect < cxx17)
14716	pedwarn (input_location, OPT_Wpedantic,
14717		 "%<static_assert%> without a message "
14718		 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14719      /* Eat the ')'  */
14720      cp_lexer_consume_token (parser->lexer);
14721      message = build_string (1, "");
14722      TREE_TYPE (message) = char_array_type_node;
14723      fix_string_type (message);
14724    }
14725  else
14726    {
14727      /* Parse the separating `,'.  */
14728      cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14729
14730      /* Parse the string-literal message.  */
14731      message = cp_parser_string_literal (parser,
14732                                	  /*translate=*/false,
14733                                	  /*wide_ok=*/true);
14734
14735      /* A `)' completes the static assertion.  */
14736      if (!parens.require_close (parser))
14737	cp_parser_skip_to_closing_parenthesis (parser,
14738                                               /*recovering=*/true,
14739                                               /*or_comma=*/false,
14740					       /*consume_paren=*/true);
14741    }
14742
14743  /* A semicolon terminates the declaration.  */
14744  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14745
14746  /* Get the location for the static assertion.  Use that of the
14747     condition if available, otherwise, use that of the "static_assert"
14748     token.  */
14749  location_t assert_loc = condition.get_location ();
14750  if (assert_loc == UNKNOWN_LOCATION)
14751    assert_loc = token_loc;
14752
14753  /* Complete the static assertion, which may mean either processing
14754     the static assert now or saving it for template instantiation.  */
14755  finish_static_assert (condition, message, assert_loc, member_p);
14756}
14757
14758/* Parse the expression in decltype ( expression ).  */
14759
14760static tree
14761cp_parser_decltype_expr (cp_parser *parser,
14762			 bool &id_expression_or_member_access_p)
14763{
14764  cp_token *id_expr_start_token;
14765  tree expr;
14766
14767  /* First, try parsing an id-expression.  */
14768  id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14769  cp_parser_parse_tentatively (parser);
14770  expr = cp_parser_id_expression (parser,
14771                                  /*template_keyword_p=*/false,
14772                                  /*check_dependency_p=*/true,
14773                                  /*template_p=*/NULL,
14774                                  /*declarator_p=*/false,
14775                                  /*optional_p=*/false);
14776
14777  if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14778    {
14779      bool non_integral_constant_expression_p = false;
14780      tree id_expression = expr;
14781      cp_id_kind idk;
14782      const char *error_msg;
14783
14784      if (identifier_p (expr))
14785	/* Lookup the name we got back from the id-expression.  */
14786	expr = cp_parser_lookup_name_simple (parser, expr,
14787					     id_expr_start_token->location);
14788
14789      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14790	/* A template without args is not a complete id-expression.  */
14791	expr = error_mark_node;
14792
14793      if (expr
14794          && expr != error_mark_node
14795          && TREE_CODE (expr) != TYPE_DECL
14796	  && (TREE_CODE (expr) != BIT_NOT_EXPR
14797	      || !TYPE_P (TREE_OPERAND (expr, 0)))
14798          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14799        {
14800          /* Complete lookup of the id-expression.  */
14801          expr = (finish_id_expression
14802                  (id_expression, expr, parser->scope, &idk,
14803                   /*integral_constant_expression_p=*/false,
14804                   /*allow_non_integral_constant_expression_p=*/true,
14805                   &non_integral_constant_expression_p,
14806                   /*template_p=*/false,
14807                   /*done=*/true,
14808                   /*address_p=*/false,
14809                   /*template_arg_p=*/false,
14810                   &error_msg,
14811		   id_expr_start_token->location));
14812
14813          if (expr == error_mark_node)
14814            /* We found an id-expression, but it was something that we
14815               should not have found. This is an error, not something
14816               we can recover from, so note that we found an
14817               id-expression and we'll recover as gracefully as
14818               possible.  */
14819            id_expression_or_member_access_p = true;
14820        }
14821
14822      if (expr
14823          && expr != error_mark_node
14824          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14825        /* We have an id-expression.  */
14826        id_expression_or_member_access_p = true;
14827    }
14828
14829  if (!id_expression_or_member_access_p)
14830    {
14831      /* Abort the id-expression parse.  */
14832      cp_parser_abort_tentative_parse (parser);
14833
14834      /* Parsing tentatively, again.  */
14835      cp_parser_parse_tentatively (parser);
14836
14837      /* Parse a class member access.  */
14838      expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14839                                           /*cast_p=*/false, /*decltype*/true,
14840                                           /*member_access_only_p=*/true, NULL);
14841
14842      if (expr
14843          && expr != error_mark_node
14844          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14845        /* We have an id-expression.  */
14846        id_expression_or_member_access_p = true;
14847    }
14848
14849  if (id_expression_or_member_access_p)
14850    /* We have parsed the complete id-expression or member access.  */
14851    cp_parser_parse_definitely (parser);
14852  else
14853    {
14854      /* Abort our attempt to parse an id-expression or member access
14855         expression.  */
14856      cp_parser_abort_tentative_parse (parser);
14857
14858      /* Parse a full expression.  */
14859      expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14860				   /*decltype_p=*/true);
14861    }
14862
14863  return expr;
14864}
14865
14866/* Parse a `decltype' type. Returns the type.
14867
14868   simple-type-specifier:
14869     decltype ( expression )
14870   C++14 proposal:
14871     decltype ( auto )  */
14872
14873static tree
14874cp_parser_decltype (cp_parser *parser)
14875{
14876  bool id_expression_or_member_access_p = false;
14877  cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14878
14879  if (start_token->type == CPP_DECLTYPE)
14880    {
14881      /* Already parsed.  */
14882      cp_lexer_consume_token (parser->lexer);
14883      return saved_checks_value (start_token->u.tree_check_value);
14884    }
14885
14886  /* Look for the `decltype' token.  */
14887  if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14888    return error_mark_node;
14889
14890  /* Parse the opening `('.  */
14891  matching_parens parens;
14892  if (!parens.require_open (parser))
14893    return error_mark_node;
14894
14895  /* Since we're going to preserve any side-effects from this parse, set up a
14896     firewall to protect our callers from cp_parser_commit_to_tentative_parse
14897     in the expression.  */
14898  tentative_firewall firewall (parser);
14899
14900  /* If in_declarator_p, a reparse as an expression might succeed (60361).
14901     Otherwise, commit now for better diagnostics.  */
14902  if (cp_parser_uncommitted_to_tentative_parse_p (parser)
14903      && !parser->in_declarator_p)
14904    cp_parser_commit_to_topmost_tentative_parse (parser);
14905
14906  push_deferring_access_checks (dk_deferred);
14907
14908  tree expr = NULL_TREE;
14909
14910  if (cxx_dialect >= cxx14
14911      && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14912    /* decltype (auto) */
14913    cp_lexer_consume_token (parser->lexer);
14914  else
14915    {
14916      /* decltype (expression)  */
14917
14918      /* Types cannot be defined in a `decltype' expression.  Save away the
14919	 old message and set the new one.  */
14920      const char *saved_message = parser->type_definition_forbidden_message;
14921      parser->type_definition_forbidden_message
14922	= G_("types may not be defined in %<decltype%> expressions");
14923
14924      /* The restrictions on constant-expressions do not apply inside
14925	 decltype expressions.  */
14926      bool saved_integral_constant_expression_p
14927	= parser->integral_constant_expression_p;
14928      bool saved_non_integral_constant_expression_p
14929	= parser->non_integral_constant_expression_p;
14930      parser->integral_constant_expression_p = false;
14931
14932      /* Within a parenthesized expression, a `>' token is always
14933	 the greater-than operator.  */
14934      bool saved_greater_than_is_operator_p
14935	= parser->greater_than_is_operator_p;
14936      parser->greater_than_is_operator_p = true;
14937
14938      /* Do not actually evaluate the expression.  */
14939      ++cp_unevaluated_operand;
14940
14941      /* Do not warn about problems with the expression.  */
14942      ++c_inhibit_evaluation_warnings;
14943
14944      expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14945      STRIP_ANY_LOCATION_WRAPPER (expr);
14946
14947      /* Go back to evaluating expressions.  */
14948      --cp_unevaluated_operand;
14949      --c_inhibit_evaluation_warnings;
14950
14951      /* The `>' token might be the end of a template-id or
14952	 template-parameter-list now.  */
14953      parser->greater_than_is_operator_p
14954	= saved_greater_than_is_operator_p;
14955
14956      /* Restore the old message and the integral constant expression
14957	 flags.  */
14958      parser->type_definition_forbidden_message = saved_message;
14959      parser->integral_constant_expression_p
14960	= saved_integral_constant_expression_p;
14961      parser->non_integral_constant_expression_p
14962	= saved_non_integral_constant_expression_p;
14963    }
14964
14965  /* Parse to the closing `)'.  */
14966  if (expr == error_mark_node || !parens.require_close (parser))
14967    {
14968      cp_parser_skip_to_closing_parenthesis (parser, true, false,
14969					     /*consume_paren=*/true);
14970      expr = error_mark_node;
14971    }
14972
14973  /* If we got a parse error while tentative, bail out now.  */
14974  if (cp_parser_error_occurred (parser))
14975    {
14976      pop_deferring_access_checks ();
14977      return error_mark_node;
14978    }
14979
14980  if (!expr)
14981    /* Build auto.  */
14982    expr = make_decltype_auto ();
14983  else
14984    expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14985				 tf_warning_or_error);
14986
14987  /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14988     it again.  */
14989  start_token->type = CPP_DECLTYPE;
14990  start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14991  start_token->tree_check_p = true;
14992  start_token->u.tree_check_value->value = expr;
14993  start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14994  start_token->keyword = RID_MAX;
14995
14996  location_t loc = start_token->location;
14997  loc = make_location (loc, loc, parser->lexer);
14998  start_token->location = loc;
14999
15000  cp_lexer_purge_tokens_after (parser->lexer, start_token);
15001
15002  pop_to_parent_deferring_access_checks ();
15003
15004  return expr;
15005}
15006
15007/* Special member functions [gram.special] */
15008
15009/* Parse a conversion-function-id.
15010
15011   conversion-function-id:
15012     operator conversion-type-id
15013
15014   Returns an IDENTIFIER_NODE representing the operator.  */
15015
15016static tree
15017cp_parser_conversion_function_id (cp_parser* parser)
15018{
15019  tree type;
15020  tree saved_scope;
15021  tree saved_qualifying_scope;
15022  tree saved_object_scope;
15023  tree pushed_scope = NULL_TREE;
15024
15025  /* Look for the `operator' token.  */
15026  if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15027    return error_mark_node;
15028  /* When we parse the conversion-type-id, the current scope will be
15029     reset.  However, we need that information in able to look up the
15030     conversion function later, so we save it here.  */
15031  saved_scope = parser->scope;
15032  saved_qualifying_scope = parser->qualifying_scope;
15033  saved_object_scope = parser->object_scope;
15034  /* We must enter the scope of the class so that the names of
15035     entities declared within the class are available in the
15036     conversion-type-id.  For example, consider:
15037
15038       struct S {
15039	 typedef int I;
15040	 operator I();
15041       };
15042
15043       S::operator I() { ... }
15044
15045     In order to see that `I' is a type-name in the definition, we
15046     must be in the scope of `S'.  */
15047  if (saved_scope)
15048    pushed_scope = push_scope (saved_scope);
15049  /* Parse the conversion-type-id.  */
15050  type = cp_parser_conversion_type_id (parser);
15051  /* Leave the scope of the class, if any.  */
15052  if (pushed_scope)
15053    pop_scope (pushed_scope);
15054  /* Restore the saved scope.  */
15055  parser->scope = saved_scope;
15056  parser->qualifying_scope = saved_qualifying_scope;
15057  parser->object_scope = saved_object_scope;
15058  /* If the TYPE is invalid, indicate failure.  */
15059  if (type == error_mark_node)
15060    return error_mark_node;
15061  return make_conv_op_name (type);
15062}
15063
15064/* Parse a conversion-type-id:
15065
15066   conversion-type-id:
15067     type-specifier-seq conversion-declarator [opt]
15068
15069   Returns the TYPE specified.  */
15070
15071static tree
15072cp_parser_conversion_type_id (cp_parser* parser)
15073{
15074  tree attributes;
15075  cp_decl_specifier_seq type_specifiers;
15076  cp_declarator *declarator;
15077  tree type_specified;
15078  const char *saved_message;
15079
15080  /* Parse the attributes.  */
15081  attributes = cp_parser_attributes_opt (parser);
15082
15083  saved_message = parser->type_definition_forbidden_message;
15084  parser->type_definition_forbidden_message
15085    = G_("types may not be defined in a conversion-type-id");
15086
15087  /* Parse the type-specifiers.  DR 2413 clarifies that `typename' is
15088     optional in conversion-type-id.  */
15089  cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15090				/*is_declaration=*/false,
15091				/*is_trailing_return=*/false,
15092				&type_specifiers);
15093
15094  parser->type_definition_forbidden_message = saved_message;
15095
15096  /* If that didn't work, stop.  */
15097  if (type_specifiers.type == error_mark_node)
15098    return error_mark_node;
15099  /* Parse the conversion-declarator.  */
15100  declarator = cp_parser_conversion_declarator_opt (parser);
15101
15102  type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
15103				    /*initialized=*/0, &attributes);
15104  if (attributes)
15105    cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
15106
15107  /* Don't give this error when parsing tentatively.  This happens to
15108     work because we always parse this definitively once.  */
15109  if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
15110      && type_uses_auto (type_specified))
15111    {
15112      if (cxx_dialect < cxx14)
15113	{
15114	  error ("invalid use of %<auto%> in conversion operator");
15115	  return error_mark_node;
15116	}
15117      else if (template_parm_scope_p ())
15118	warning (0, "use of %<auto%> in member template "
15119		 "conversion operator can never be deduced");
15120    }
15121
15122  return type_specified;
15123}
15124
15125/* Parse an (optional) conversion-declarator.
15126
15127   conversion-declarator:
15128     ptr-operator conversion-declarator [opt]
15129
15130   */
15131
15132static cp_declarator *
15133cp_parser_conversion_declarator_opt (cp_parser* parser)
15134{
15135  enum tree_code code;
15136  tree class_type, std_attributes = NULL_TREE;
15137  cp_cv_quals cv_quals;
15138
15139  /* We don't know if there's a ptr-operator next, or not.  */
15140  cp_parser_parse_tentatively (parser);
15141  /* Try the ptr-operator.  */
15142  code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
15143				 &std_attributes);
15144  /* If it worked, look for more conversion-declarators.  */
15145  if (cp_parser_parse_definitely (parser))
15146    {
15147      cp_declarator *declarator;
15148
15149      /* Parse another optional declarator.  */
15150      declarator = cp_parser_conversion_declarator_opt (parser);
15151
15152      declarator = cp_parser_make_indirect_declarator
15153	(code, class_type, cv_quals, declarator, std_attributes);
15154
15155      return declarator;
15156   }
15157
15158  return NULL;
15159}
15160
15161/* Parse an (optional) ctor-initializer.
15162
15163   ctor-initializer:
15164     : mem-initializer-list  */
15165
15166static void
15167cp_parser_ctor_initializer_opt (cp_parser* parser)
15168{
15169  /* If the next token is not a `:', then there is no
15170     ctor-initializer.  */
15171  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15172    {
15173      /* Do default initialization of any bases and members.  */
15174      if (DECL_CONSTRUCTOR_P (current_function_decl))
15175	finish_mem_initializers (NULL_TREE);
15176      return;
15177    }
15178
15179  /* Consume the `:' token.  */
15180  cp_lexer_consume_token (parser->lexer);
15181  /* And the mem-initializer-list.  */
15182  cp_parser_mem_initializer_list (parser);
15183}
15184
15185/* Parse a mem-initializer-list.
15186
15187   mem-initializer-list:
15188     mem-initializer ... [opt]
15189     mem-initializer ... [opt] , mem-initializer-list  */
15190
15191static void
15192cp_parser_mem_initializer_list (cp_parser* parser)
15193{
15194  tree mem_initializer_list = NULL_TREE;
15195  tree target_ctor = error_mark_node;
15196  cp_token *token = cp_lexer_peek_token (parser->lexer);
15197
15198  /* Let the semantic analysis code know that we are starting the
15199     mem-initializer-list.  */
15200  if (!DECL_CONSTRUCTOR_P (current_function_decl))
15201    error_at (token->location,
15202	      "only constructors take member initializers");
15203
15204  /* Loop through the list.  */
15205  while (true)
15206    {
15207      tree mem_initializer;
15208
15209      token = cp_lexer_peek_token (parser->lexer);
15210      /* Parse the mem-initializer.  */
15211      mem_initializer = cp_parser_mem_initializer (parser);
15212      /* If the next token is a `...', we're expanding member initializers. */
15213      bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15214      if (ellipsis
15215	  || (mem_initializer != error_mark_node
15216	      && check_for_bare_parameter_packs (TREE_PURPOSE
15217						 (mem_initializer))))
15218        {
15219          /* Consume the `...'. */
15220	  if (ellipsis)
15221	    cp_lexer_consume_token (parser->lexer);
15222
15223          /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15224             can be expanded but members cannot. */
15225          if (mem_initializer != error_mark_node
15226              && !TYPE_P (TREE_PURPOSE (mem_initializer)))
15227            {
15228              error_at (token->location,
15229			"cannot expand initializer for member %qD",
15230			TREE_PURPOSE (mem_initializer));
15231              mem_initializer = error_mark_node;
15232            }
15233
15234          /* Construct the pack expansion type. */
15235          if (mem_initializer != error_mark_node)
15236            mem_initializer = make_pack_expansion (mem_initializer);
15237        }
15238      if (target_ctor != error_mark_node
15239	  && mem_initializer != error_mark_node)
15240	{
15241	  error ("mem-initializer for %qD follows constructor delegation",
15242		 TREE_PURPOSE (mem_initializer));
15243	  mem_initializer = error_mark_node;
15244	}
15245      /* Look for a target constructor. */
15246      if (mem_initializer != error_mark_node
15247	  && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
15248	  && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
15249	{
15250	  maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
15251	  if (mem_initializer_list)
15252	    {
15253	      error ("constructor delegation follows mem-initializer for %qD",
15254		     TREE_PURPOSE (mem_initializer_list));
15255	      mem_initializer = error_mark_node;
15256	    }
15257	  target_ctor = mem_initializer;
15258	}
15259      /* Add it to the list, unless it was erroneous.  */
15260      if (mem_initializer != error_mark_node)
15261	{
15262	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
15263	  mem_initializer_list = mem_initializer;
15264	}
15265      /* If the next token is not a `,', we're done.  */
15266      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15267	break;
15268      /* Consume the `,' token.  */
15269      cp_lexer_consume_token (parser->lexer);
15270    }
15271
15272  /* Perform semantic analysis.  */
15273  if (DECL_CONSTRUCTOR_P (current_function_decl))
15274    finish_mem_initializers (mem_initializer_list);
15275}
15276
15277/* Parse a mem-initializer.
15278
15279   mem-initializer:
15280     mem-initializer-id ( expression-list [opt] )
15281     mem-initializer-id braced-init-list
15282
15283   GNU extension:
15284
15285   mem-initializer:
15286     ( expression-list [opt] )
15287
15288   Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
15289   class) or FIELD_DECL (for a non-static data member) to initialize;
15290   the TREE_VALUE is the expression-list.  An empty initialization
15291   list is represented by void_list_node.  */
15292
15293static tree
15294cp_parser_mem_initializer (cp_parser* parser)
15295{
15296  tree mem_initializer_id;
15297  tree expression_list;
15298  tree member;
15299  cp_token *token = cp_lexer_peek_token (parser->lexer);
15300
15301  /* Find out what is being initialized.  */
15302  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15303    {
15304      permerror (token->location,
15305		 "anachronistic old-style base class initializer");
15306      mem_initializer_id = NULL_TREE;
15307    }
15308  else
15309    {
15310      mem_initializer_id = cp_parser_mem_initializer_id (parser);
15311      if (mem_initializer_id == error_mark_node)
15312	return mem_initializer_id;
15313    }
15314  member = expand_member_init (mem_initializer_id);
15315  if (member && !DECL_P (member))
15316    in_base_initializer = 1;
15317
15318  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15319    {
15320      bool expr_non_constant_p;
15321      cp_lexer_set_source_position (parser->lexer);
15322      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15323      expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15324      CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15325      expression_list = build_tree_list (NULL_TREE, expression_list);
15326    }
15327  else
15328    {
15329      vec<tree, va_gc> *vec;
15330      vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15331						     /*cast_p=*/false,
15332						     /*allow_expansion_p=*/true,
15333						     /*non_constant_p=*/NULL,
15334						     /*close_paren_loc=*/NULL,
15335						     /*wrap_locations_p=*/true);
15336      if (vec == NULL)
15337	return error_mark_node;
15338      expression_list = build_tree_list_vec (vec);
15339      release_tree_vector (vec);
15340    }
15341
15342  if (expression_list == error_mark_node)
15343    return error_mark_node;
15344  if (!expression_list)
15345    expression_list = void_type_node;
15346
15347  in_base_initializer = 0;
15348
15349  return member ? build_tree_list (member, expression_list) : error_mark_node;
15350}
15351
15352/* Parse a mem-initializer-id.
15353
15354   mem-initializer-id:
15355     :: [opt] nested-name-specifier [opt] class-name
15356     decltype-specifier (C++11)
15357     identifier
15358
15359   Returns a TYPE indicating the class to be initialized for the first
15360   production (and the second in C++11).  Returns an IDENTIFIER_NODE
15361   indicating the data member to be initialized for the last production.  */
15362
15363static tree
15364cp_parser_mem_initializer_id (cp_parser* parser)
15365{
15366  bool global_scope_p;
15367  bool nested_name_specifier_p;
15368  bool template_p = false;
15369  tree id;
15370
15371  cp_token *token = cp_lexer_peek_token (parser->lexer);
15372
15373  /* `typename' is not allowed in this context ([temp.res]).  */
15374  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15375    {
15376      error_at (token->location,
15377		"keyword %<typename%> not allowed in this context (a qualified "
15378		"member initializer is implicitly a type)");
15379      cp_lexer_consume_token (parser->lexer);
15380    }
15381  /* Look for the optional `::' operator.  */
15382  global_scope_p
15383    = (cp_parser_global_scope_opt (parser,
15384				   /*current_scope_valid_p=*/false)
15385       != NULL_TREE);
15386  /* Look for the optional nested-name-specifier.  The simplest way to
15387     implement:
15388
15389       [temp.res]
15390
15391       The keyword `typename' is not permitted in a base-specifier or
15392       mem-initializer; in these contexts a qualified name that
15393       depends on a template-parameter is implicitly assumed to be a
15394       type name.
15395
15396     is to assume that we have seen the `typename' keyword at this
15397     point.  */
15398  nested_name_specifier_p
15399    = (cp_parser_nested_name_specifier_opt (parser,
15400					    /*typename_keyword_p=*/true,
15401					    /*check_dependency_p=*/true,
15402					    /*type_p=*/true,
15403					    /*is_declaration=*/true)
15404       != NULL_TREE);
15405  if (nested_name_specifier_p)
15406    template_p = cp_parser_optional_template_keyword (parser);
15407  /* If there is a `::' operator or a nested-name-specifier, then we
15408     are definitely looking for a class-name.  */
15409  if (global_scope_p || nested_name_specifier_p)
15410    return cp_parser_class_name (parser,
15411				 /*typename_keyword_p=*/true,
15412				 /*template_keyword_p=*/template_p,
15413				 typename_type,
15414				 /*check_dependency_p=*/true,
15415				 /*class_head_p=*/false,
15416				 /*is_declaration=*/true);
15417  /* Otherwise, we could also be looking for an ordinary identifier.  */
15418  cp_parser_parse_tentatively (parser);
15419  if (cp_lexer_next_token_is_decltype (parser->lexer))
15420    /* Try a decltype-specifier.  */
15421    id = cp_parser_decltype (parser);
15422  else
15423    /* Otherwise, try a class-name.  */
15424    id = cp_parser_class_name (parser,
15425			       /*typename_keyword_p=*/true,
15426			       /*template_keyword_p=*/false,
15427			       none_type,
15428			       /*check_dependency_p=*/true,
15429			       /*class_head_p=*/false,
15430			       /*is_declaration=*/true);
15431  /* If we found one, we're done.  */
15432  if (cp_parser_parse_definitely (parser))
15433    return id;
15434  /* Otherwise, look for an ordinary identifier.  */
15435  return cp_parser_identifier (parser);
15436}
15437
15438/* Overloading [gram.over] */
15439
15440/* Parse an operator-function-id.
15441
15442   operator-function-id:
15443     operator operator
15444
15445   Returns an IDENTIFIER_NODE for the operator which is a
15446   human-readable spelling of the identifier, e.g., `operator +'.  */
15447
15448static cp_expr
15449cp_parser_operator_function_id (cp_parser* parser)
15450{
15451  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15452  /* Look for the `operator' keyword.  */
15453  if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15454    return error_mark_node;
15455  /* And then the name of the operator itself.  */
15456  return cp_parser_operator (parser, start_loc);
15457}
15458
15459/* Return an identifier node for a user-defined literal operator.
15460   The suffix identifier is chained to the operator name identifier.  */
15461
15462tree
15463cp_literal_operator_id (const char* name)
15464{
15465  tree identifier;
15466  char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15467			      + strlen (name) + 10);
15468  sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15469  identifier = get_identifier (buffer);
15470  XDELETEVEC (buffer);
15471
15472  return identifier;
15473}
15474
15475/* Parse an operator.
15476
15477   operator:
15478     new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15479     += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15480     || ++ -- , ->* -> () []
15481
15482   GNU Extensions:
15483
15484   operator:
15485     <? >? <?= >?=
15486
15487   Returns an IDENTIFIER_NODE for the operator which is a
15488   human-readable spelling of the identifier, e.g., `operator +'.  */
15489
15490static cp_expr
15491cp_parser_operator (cp_parser* parser, location_t start_loc)
15492{
15493  tree id = NULL_TREE;
15494  cp_token *token;
15495  bool utf8 = false;
15496
15497  /* Peek at the next token.  */
15498  token = cp_lexer_peek_token (parser->lexer);
15499
15500  location_t end_loc = token->location;
15501
15502  /* Figure out which operator we have.  */
15503  enum tree_code op = ERROR_MARK;
15504  bool assop = false;
15505  bool consumed = false;
15506  switch (token->type)
15507    {
15508    case CPP_KEYWORD:
15509      {
15510	/* The keyword should be either `new', `delete' or `co_await'.  */
15511	if (token->keyword == RID_NEW)
15512	  op = NEW_EXPR;
15513	else if (token->keyword == RID_DELETE)
15514	  op = DELETE_EXPR;
15515	else if (token->keyword == RID_CO_AWAIT)
15516	  op = CO_AWAIT_EXPR;
15517	else
15518	  break;
15519
15520	/* Consume the `new', `delete' or co_await token.  */
15521	end_loc = cp_lexer_consume_token (parser->lexer)->location;
15522
15523	/* Peek at the next token.  */
15524	token = cp_lexer_peek_token (parser->lexer);
15525	/* If it's a `[' token then this is the array variant of the
15526	   operator.  */
15527	if (token->type == CPP_OPEN_SQUARE
15528	    && op != CO_AWAIT_EXPR)
15529	  {
15530	    /* Consume the `[' token.  */
15531	    cp_lexer_consume_token (parser->lexer);
15532	    /* Look for the `]' token.  */
15533	    if (cp_token *close_token
15534		= cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15535	      end_loc = close_token->location;
15536	    op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15537	  }
15538	consumed = true;
15539	break;
15540      }
15541
15542    case CPP_PLUS:
15543      op = PLUS_EXPR;
15544      break;
15545
15546    case CPP_MINUS:
15547      op = MINUS_EXPR;
15548      break;
15549
15550    case CPP_MULT:
15551      op = MULT_EXPR;
15552      break;
15553
15554    case CPP_DIV:
15555      op = TRUNC_DIV_EXPR;
15556      break;
15557
15558    case CPP_MOD:
15559      op = TRUNC_MOD_EXPR;
15560      break;
15561
15562    case CPP_XOR:
15563      op = BIT_XOR_EXPR;
15564      break;
15565
15566    case CPP_AND:
15567      op = BIT_AND_EXPR;
15568      break;
15569
15570    case CPP_OR:
15571      op = BIT_IOR_EXPR;
15572      break;
15573
15574    case CPP_COMPL:
15575      op = BIT_NOT_EXPR;
15576      break;
15577
15578    case CPP_NOT:
15579      op = TRUTH_NOT_EXPR;
15580      break;
15581
15582    case CPP_EQ:
15583      assop = true;
15584      op = NOP_EXPR;
15585      break;
15586
15587    case CPP_LESS:
15588      op = LT_EXPR;
15589      break;
15590
15591    case CPP_GREATER:
15592      op = GT_EXPR;
15593      break;
15594
15595    case CPP_PLUS_EQ:
15596      assop = true;
15597      op = PLUS_EXPR;
15598      break;
15599
15600    case CPP_MINUS_EQ:
15601      assop = true;
15602      op = MINUS_EXPR;
15603      break;
15604
15605    case CPP_MULT_EQ:
15606      assop = true;
15607      op = MULT_EXPR;
15608      break;
15609
15610    case CPP_DIV_EQ:
15611      assop = true;
15612      op = TRUNC_DIV_EXPR;
15613      break;
15614
15615    case CPP_MOD_EQ:
15616      assop = true;
15617      op = TRUNC_MOD_EXPR;
15618      break;
15619
15620    case CPP_XOR_EQ:
15621      assop = true;
15622      op = BIT_XOR_EXPR;
15623      break;
15624
15625    case CPP_AND_EQ:
15626      assop = true;
15627      op = BIT_AND_EXPR;
15628      break;
15629
15630    case CPP_OR_EQ:
15631      assop = true;
15632      op = BIT_IOR_EXPR;
15633      break;
15634
15635    case CPP_LSHIFT:
15636      op = LSHIFT_EXPR;
15637      break;
15638
15639    case CPP_RSHIFT:
15640      op = RSHIFT_EXPR;
15641      break;
15642
15643    case CPP_LSHIFT_EQ:
15644      assop = true;
15645      op = LSHIFT_EXPR;
15646      break;
15647
15648    case CPP_RSHIFT_EQ:
15649      assop = true;
15650      op = RSHIFT_EXPR;
15651      break;
15652
15653    case CPP_EQ_EQ:
15654      op = EQ_EXPR;
15655      break;
15656
15657    case CPP_NOT_EQ:
15658      op = NE_EXPR;
15659      break;
15660
15661    case CPP_LESS_EQ:
15662      op = LE_EXPR;
15663      break;
15664
15665    case CPP_GREATER_EQ:
15666      op = GE_EXPR;
15667      break;
15668
15669    case CPP_SPACESHIP:
15670      op = SPACESHIP_EXPR;
15671      break;
15672
15673    case CPP_AND_AND:
15674      op = TRUTH_ANDIF_EXPR;
15675      break;
15676
15677    case CPP_OR_OR:
15678      op = TRUTH_ORIF_EXPR;
15679      break;
15680
15681    case CPP_PLUS_PLUS:
15682      op = POSTINCREMENT_EXPR;
15683      break;
15684
15685    case CPP_MINUS_MINUS:
15686      op = PREDECREMENT_EXPR;
15687      break;
15688
15689    case CPP_COMMA:
15690      op = COMPOUND_EXPR;
15691      break;
15692
15693    case CPP_DEREF_STAR:
15694      op = MEMBER_REF;
15695      break;
15696
15697    case CPP_DEREF:
15698      op = COMPONENT_REF;
15699      break;
15700
15701    case CPP_QUERY:
15702      op = COND_EXPR;
15703      /* Consume the `?'.  */
15704      cp_lexer_consume_token (parser->lexer);
15705      /* Look for the matching `:'.  */
15706      cp_parser_require (parser, CPP_COLON, RT_COLON);
15707      consumed = true;
15708      break;
15709
15710    case CPP_OPEN_PAREN:
15711      {
15712        /* Consume the `('.  */
15713        matching_parens parens;
15714        parens.consume_open (parser);
15715        /* Look for the matching `)'.  */
15716        token = parens.require_close (parser);
15717        if (token)
15718	  end_loc = token->location;
15719	op = CALL_EXPR;
15720	consumed = true;
15721	break;
15722      }
15723
15724    case CPP_OPEN_SQUARE:
15725      /* Consume the `['.  */
15726      cp_lexer_consume_token (parser->lexer);
15727      /* Look for the matching `]'.  */
15728      token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15729      if (token)
15730	end_loc = token->location;
15731      op = ARRAY_REF;
15732      consumed = true;
15733      break;
15734
15735    case CPP_UTF8STRING:
15736    case CPP_UTF8STRING_USERDEF:
15737      utf8 = true;
15738      /* FALLTHRU */
15739    case CPP_STRING:
15740    case CPP_WSTRING:
15741    case CPP_STRING16:
15742    case CPP_STRING32:
15743    case CPP_STRING_USERDEF:
15744    case CPP_WSTRING_USERDEF:
15745    case CPP_STRING16_USERDEF:
15746    case CPP_STRING32_USERDEF:
15747      {
15748	cp_expr str;
15749	tree string_tree;
15750	int sz, len;
15751
15752	if (cxx_dialect == cxx98)
15753	  maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15754
15755	/* Consume the string.  */
15756	str = cp_parser_string_literal (parser, /*translate=*/true,
15757				      /*wide_ok=*/true, /*lookup_udlit=*/false);
15758	if (str == error_mark_node)
15759	  return error_mark_node;
15760	else if (TREE_CODE (str) == USERDEF_LITERAL)
15761	  {
15762	    string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15763	    id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15764	    end_loc = str.get_location ();
15765	  }
15766	else
15767	  {
15768	    string_tree = str;
15769	    /* Look for the suffix identifier.  */
15770	    token = cp_lexer_peek_token (parser->lexer);
15771	    if (token->type == CPP_NAME)
15772	      {
15773		id = cp_parser_identifier (parser);
15774		end_loc = token->location;
15775	      }
15776	    else if (token->type == CPP_KEYWORD)
15777	      {
15778		error ("unexpected keyword;"
15779		       " remove space between quotes and suffix identifier");
15780		return error_mark_node;
15781	      }
15782	    else
15783	      {
15784		error ("expected suffix identifier");
15785		return error_mark_node;
15786	      }
15787	  }
15788	sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15789			       (TREE_TYPE (TREE_TYPE (string_tree))));
15790	len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15791	if (len != 0)
15792	  {
15793	    error ("expected empty string after %<operator%> keyword");
15794	    return error_mark_node;
15795	  }
15796	if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15797	    != char_type_node)
15798	  {
15799	    error ("invalid encoding prefix in literal operator");
15800	    return error_mark_node;
15801	  }
15802	if (id != error_mark_node)
15803	  {
15804	    const char *name = IDENTIFIER_POINTER (id);
15805	    id = cp_literal_operator_id (name);
15806	  }
15807	/* Generate a location of the form:
15808	     "" _suffix_identifier
15809	     ^~~~~~~~~~~~~~~~~~~~~
15810	   with caret == start at the start token, finish at the end of the
15811	   suffix identifier.  */
15812	location_t combined_loc
15813	  = make_location (start_loc, start_loc, parser->lexer);
15814	return cp_expr (id, combined_loc);
15815      }
15816
15817    default:
15818      /* Anything else is an error.  */
15819      break;
15820    }
15821
15822  /* If we have selected an identifier, we need to consume the
15823     operator token.  */
15824  if (op != ERROR_MARK)
15825    {
15826      id = ovl_op_identifier (assop, op);
15827      if (!consumed)
15828	cp_lexer_consume_token (parser->lexer);
15829    }
15830  /* Otherwise, no valid operator name was present.  */
15831  else
15832    {
15833      cp_parser_error (parser, "expected operator");
15834      id = error_mark_node;
15835    }
15836
15837  start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15838  return cp_expr (id, start_loc);
15839}
15840
15841/* Parse a template-declaration.
15842
15843   template-declaration:
15844     export [opt] template < template-parameter-list > declaration
15845
15846   If MEMBER_P is TRUE, this template-declaration occurs within a
15847   class-specifier.
15848
15849   The grammar rule given by the standard isn't correct.  What
15850   is really meant is:
15851
15852   template-declaration:
15853     export [opt] template-parameter-list-seq
15854       decl-specifier-seq [opt] init-declarator [opt] ;
15855     export [opt] template-parameter-list-seq
15856       function-definition
15857
15858   template-parameter-list-seq:
15859     template-parameter-list-seq [opt]
15860     template < template-parameter-list >
15861
15862   Concept Extensions:
15863
15864   template-parameter-list-seq:
15865     template < template-parameter-list > requires-clause [opt]
15866
15867   requires-clause:
15868     requires logical-or-expression  */
15869
15870static void
15871cp_parser_template_declaration (cp_parser* parser, bool member_p)
15872{
15873  /* Check for `export'.  */
15874  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15875    {
15876      /* Consume the `export' token.  */
15877      cp_lexer_consume_token (parser->lexer);
15878      /* Warn that we do not support `export'.  */
15879      warning (0, "keyword %<export%> not implemented, and will be ignored");
15880    }
15881
15882  cp_parser_template_declaration_after_export (parser, member_p);
15883}
15884
15885/* Parse a template-parameter-list.
15886
15887   template-parameter-list:
15888     template-parameter
15889     template-parameter-list , template-parameter
15890
15891   Returns a TREE_LIST.  Each node represents a template parameter.
15892   The nodes are connected via their TREE_CHAINs.  */
15893
15894static tree
15895cp_parser_template_parameter_list (cp_parser* parser)
15896{
15897  tree parameter_list = NULL_TREE;
15898
15899  /* Don't create wrapper nodes within a template-parameter-list,
15900     since we don't want to have different types based on the
15901     spelling location of constants and decls within them.  */
15902  auto_suppress_location_wrappers sentinel;
15903
15904  begin_template_parm_list ();
15905
15906  /* The loop below parses the template parms.  We first need to know
15907     the total number of template parms to be able to compute proper
15908     canonical types of each dependent type. So after the loop, when
15909     we know the total number of template parms,
15910     end_template_parm_list computes the proper canonical types and
15911     fixes up the dependent types accordingly.  */
15912  while (true)
15913    {
15914      tree parameter;
15915      bool is_non_type;
15916      bool is_parameter_pack;
15917      location_t parm_loc;
15918
15919      /* Parse the template-parameter.  */
15920      parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15921      parameter = cp_parser_template_parameter (parser,
15922                                                &is_non_type,
15923                                                &is_parameter_pack);
15924      /* Add it to the list.  */
15925      if (parameter != error_mark_node)
15926	parameter_list = process_template_parm (parameter_list,
15927						parm_loc,
15928						parameter,
15929						is_non_type,
15930						is_parameter_pack);
15931      else
15932       {
15933         tree err_parm = build_tree_list (parameter, parameter);
15934         parameter_list = chainon (parameter_list, err_parm);
15935       }
15936
15937      /* If the next token is not a `,', we're done.  */
15938      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15939	break;
15940      /* Otherwise, consume the `,' token.  */
15941      cp_lexer_consume_token (parser->lexer);
15942    }
15943
15944  return end_template_parm_list (parameter_list);
15945}
15946
15947/* Parse a introduction-list.
15948
15949   introduction-list:
15950     introduced-parameter
15951     introduction-list , introduced-parameter
15952
15953   introduced-parameter:
15954     ...[opt] identifier
15955
15956   Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
15957   then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
15958   WILDCARD_DECL will also have DECL_NAME set and token location in
15959   DECL_SOURCE_LOCATION.  */
15960
15961static tree
15962cp_parser_introduction_list (cp_parser *parser)
15963{
15964  vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15965
15966  while (true)
15967    {
15968      bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15969      if (is_pack)
15970	cp_lexer_consume_token (parser->lexer);
15971
15972      tree identifier = cp_parser_identifier (parser);
15973      if (identifier == error_mark_node)
15974	break;
15975
15976      /* Build placeholder. */
15977      tree parm = build_nt (WILDCARD_DECL);
15978      DECL_SOURCE_LOCATION (parm)
15979	= cp_lexer_peek_token (parser->lexer)->location;
15980      DECL_NAME (parm) = identifier;
15981      WILDCARD_PACK_P (parm) = is_pack;
15982      vec_safe_push (introduction_vec, parm);
15983
15984      /* If the next token is not a `,', we're done.  */
15985      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15986	break;
15987      /* Otherwise, consume the `,' token.  */
15988      cp_lexer_consume_token (parser->lexer);
15989    }
15990
15991  /* Convert the vec into a TREE_VEC.  */
15992  tree introduction_list = make_tree_vec (introduction_vec->length ());
15993  unsigned int n;
15994  tree parm;
15995  FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15996    TREE_VEC_ELT (introduction_list, n) = parm;
15997
15998  release_tree_vector (introduction_vec);
15999  return introduction_list;
16000}
16001
16002/* Given a declarator, get the declarator-id part, or NULL_TREE if this
16003   is an abstract declarator. */
16004
16005static inline cp_declarator*
16006get_id_declarator (cp_declarator *declarator)
16007{
16008  cp_declarator *d = declarator;
16009  while (d && d->kind != cdk_id)
16010    d = d->declarator;
16011  return d;
16012}
16013
16014/* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
16015   is an abstract declarator. */
16016
16017static inline tree
16018get_unqualified_id (cp_declarator *declarator)
16019{
16020  declarator = get_id_declarator (declarator);
16021  if (declarator)
16022    return declarator->u.id.unqualified_name;
16023  else
16024    return NULL_TREE;
16025}
16026
16027/* Returns true if TYPE would declare a constrained constrained-parameter.  */
16028
16029static inline bool
16030is_constrained_parameter (tree type)
16031{
16032  return (type
16033          && TREE_CODE (type) == TYPE_DECL
16034          && CONSTRAINED_PARM_CONCEPT (type)
16035          && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
16036}
16037
16038/* Returns true if PARM declares a constrained-parameter. */
16039
16040static inline bool
16041is_constrained_parameter (cp_parameter_declarator *parm)
16042{
16043  return is_constrained_parameter (parm->decl_specifiers.type);
16044}
16045
16046/* Check that the type parameter is only a declarator-id, and that its
16047   type is not cv-qualified. */
16048
16049bool
16050cp_parser_check_constrained_type_parm (cp_parser *parser,
16051				       cp_parameter_declarator *parm)
16052{
16053  if (!parm->declarator)
16054    return true;
16055
16056  if (parm->declarator->kind != cdk_id)
16057    {
16058      cp_parser_error (parser, "invalid constrained type parameter");
16059      return false;
16060    }
16061
16062  /* Don't allow cv-qualified type parameters.  */
16063  if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
16064      || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
16065    {
16066      cp_parser_error (parser, "cv-qualified type parameter");
16067      return false;
16068    }
16069
16070  return true;
16071}
16072
16073/* Finish parsing/processing a template type parameter and checking
16074   various restrictions. */
16075
16076static inline tree
16077cp_parser_constrained_type_template_parm (cp_parser *parser,
16078                                          tree id,
16079                                          cp_parameter_declarator* parmdecl)
16080{
16081  if (cp_parser_check_constrained_type_parm (parser, parmdecl))
16082    return finish_template_type_parm (class_type_node, id);
16083  else
16084    return error_mark_node;
16085}
16086
16087static tree
16088finish_constrained_template_template_parm (tree proto, tree id)
16089{
16090  /* FIXME: This should probably be copied, and we may need to adjust
16091     the template parameter depths.  */
16092  tree saved_parms = current_template_parms;
16093  begin_template_parm_list ();
16094  current_template_parms = DECL_TEMPLATE_PARMS (proto);
16095  end_template_parm_list ();
16096
16097  tree parm = finish_template_template_parm (class_type_node, id);
16098  current_template_parms = saved_parms;
16099
16100  return parm;
16101}
16102
16103/* Finish parsing/processing a template template parameter by borrowing
16104   the template parameter list from the prototype parameter.  */
16105
16106static tree
16107cp_parser_constrained_template_template_parm (cp_parser *parser,
16108                                              tree proto,
16109                                              tree id,
16110                                              cp_parameter_declarator *parmdecl)
16111{
16112  if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
16113    return error_mark_node;
16114  return finish_constrained_template_template_parm (proto, id);
16115}
16116
16117/* Create a new non-type template parameter from the given PARM
16118   declarator.  */
16119
16120static tree
16121cp_parser_constrained_non_type_template_parm (bool *is_non_type,
16122					      cp_parameter_declarator *parm)
16123{
16124  *is_non_type = true;
16125  cp_declarator *decl = parm->declarator;
16126  cp_decl_specifier_seq *specs = &parm->decl_specifiers;
16127  specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
16128  return grokdeclarator (decl, specs, TPARM, 0, NULL);
16129}
16130
16131/* Build a constrained template parameter based on the PARMDECL
16132   declarator. The type of PARMDECL is the constrained type, which
16133   refers to the prototype template parameter that ultimately
16134   specifies the type of the declared parameter. */
16135
16136static tree
16137finish_constrained_parameter (cp_parser *parser,
16138                              cp_parameter_declarator *parmdecl,
16139                              bool *is_non_type)
16140{
16141  tree decl = parmdecl->decl_specifiers.type;
16142  tree id = get_unqualified_id (parmdecl->declarator);
16143  tree def = parmdecl->default_argument;
16144  tree proto = DECL_INITIAL (decl);
16145
16146  /* Build the parameter. Return an error if the declarator was invalid. */
16147  tree parm;
16148  if (TREE_CODE (proto) == TYPE_DECL)
16149    parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
16150  else if (TREE_CODE (proto) == TEMPLATE_DECL)
16151    parm = cp_parser_constrained_template_template_parm (parser, proto, id,
16152							 parmdecl);
16153  else
16154    parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
16155  if (parm == error_mark_node)
16156    return error_mark_node;
16157
16158  /* Finish the parameter decl and create a node attaching the
16159     default argument and constraint.  */
16160  parm = build_tree_list (def, parm);
16161  TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
16162
16163  return parm;
16164}
16165
16166/* Returns true if the parsed type actually represents the declaration
16167   of a type template-parameter.  */
16168
16169static bool
16170declares_constrained_type_template_parameter (tree type)
16171{
16172  return (is_constrained_parameter (type)
16173	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
16174}
16175
16176/* Returns true if the parsed type actually represents the declaration of
16177   a template template-parameter.  */
16178
16179static bool
16180declares_constrained_template_template_parameter (tree type)
16181{
16182  return (is_constrained_parameter (type)
16183	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
16184}
16185
16186/* Parse a default argument for a type template-parameter.
16187   Note that diagnostics are handled in cp_parser_template_parameter.  */
16188
16189static tree
16190cp_parser_default_type_template_argument (cp_parser *parser)
16191{
16192  gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16193
16194  /* Consume the `=' token.  */
16195  cp_lexer_consume_token (parser->lexer);
16196
16197  cp_token *token = cp_lexer_peek_token (parser->lexer);
16198
16199  /* Parse the default-argument.  */
16200  push_deferring_access_checks (dk_no_deferred);
16201  tree default_argument = cp_parser_type_id (parser,
16202					     CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16203					     NULL);
16204  pop_deferring_access_checks ();
16205
16206  if (flag_concepts && type_uses_auto (default_argument))
16207    {
16208      error_at (token->location,
16209		"invalid use of %<auto%> in default template argument");
16210      return error_mark_node;
16211    }
16212
16213  return default_argument;
16214}
16215
16216/* Parse a default argument for a template template-parameter.  */
16217
16218static tree
16219cp_parser_default_template_template_argument (cp_parser *parser)
16220{
16221  gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16222
16223  bool is_template;
16224
16225  /* Consume the `='.  */
16226  cp_lexer_consume_token (parser->lexer);
16227  /* Parse the id-expression.  */
16228  push_deferring_access_checks (dk_no_deferred);
16229  /* save token before parsing the id-expression, for error
16230     reporting */
16231  const cp_token* token = cp_lexer_peek_token (parser->lexer);
16232  tree default_argument
16233    = cp_parser_id_expression (parser,
16234                               /*template_keyword_p=*/false,
16235                               /*check_dependency_p=*/true,
16236                               /*template_p=*/&is_template,
16237                               /*declarator_p=*/false,
16238                               /*optional_p=*/false);
16239  if (TREE_CODE (default_argument) == TYPE_DECL)
16240    /* If the id-expression was a template-id that refers to
16241       a template-class, we already have the declaration here,
16242       so no further lookup is needed.  */
16243    ;
16244  else
16245    /* Look up the name.  */
16246    default_argument
16247      = cp_parser_lookup_name (parser, default_argument,
16248                               none_type,
16249                               /*is_template=*/is_template,
16250                               /*is_namespace=*/false,
16251                               /*check_dependency=*/true,
16252                               /*ambiguous_decls=*/NULL,
16253                               token->location);
16254  /* See if the default argument is valid.  */
16255  default_argument = check_template_template_default_arg (default_argument);
16256  pop_deferring_access_checks ();
16257  return default_argument;
16258}
16259
16260/* Parse a template-parameter.
16261
16262   template-parameter:
16263     type-parameter
16264     parameter-declaration
16265
16266   If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
16267   the parameter.  The TREE_PURPOSE is the default value, if any.
16268   Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
16269   iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
16270   set to true iff this parameter is a parameter pack. */
16271
16272static tree
16273cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
16274                              bool *is_parameter_pack)
16275{
16276  cp_token *token;
16277  cp_parameter_declarator *parameter_declarator;
16278  tree parm;
16279
16280  /* Assume it is a type parameter or a template parameter.  */
16281  *is_non_type = false;
16282  /* Assume it not a parameter pack. */
16283  *is_parameter_pack = false;
16284  /* Peek at the next token.  */
16285  token = cp_lexer_peek_token (parser->lexer);
16286  /* If it is `template', we have a type-parameter.  */
16287  if (token->keyword == RID_TEMPLATE)
16288    return cp_parser_type_parameter (parser, is_parameter_pack);
16289  /* If it is `class' or `typename' we do not know yet whether it is a
16290     type parameter or a non-type parameter.  Consider:
16291
16292       template <typename T, typename T::X X> ...
16293
16294     or:
16295
16296       template <class C, class D*> ...
16297
16298     Here, the first parameter is a type parameter, and the second is
16299     a non-type parameter.  We can tell by looking at the token after
16300     the identifier -- if it is a `,', `=', or `>' then we have a type
16301     parameter.  */
16302  if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16303    {
16304      /* Peek at the token after `class' or `typename'.  */
16305      token = cp_lexer_peek_nth_token (parser->lexer, 2);
16306      /* If it's an ellipsis, we have a template type parameter
16307         pack. */
16308      if (token->type == CPP_ELLIPSIS)
16309        return cp_parser_type_parameter (parser, is_parameter_pack);
16310      /* If it's an identifier, skip it.  */
16311      if (token->type == CPP_NAME)
16312	token = cp_lexer_peek_nth_token (parser->lexer, 3);
16313      /* Now, see if the token looks like the end of a template
16314	 parameter.  */
16315      if (token->type == CPP_COMMA
16316	  || token->type == CPP_EQ
16317	  || token->type == CPP_GREATER)
16318	return cp_parser_type_parameter (parser, is_parameter_pack);
16319    }
16320
16321  /* Otherwise, it is a non-type parameter or a constrained parameter.
16322
16323     [temp.param]
16324
16325     When parsing a default template-argument for a non-type
16326     template-parameter, the first non-nested `>' is taken as the end
16327     of the template parameter-list rather than a greater-than
16328     operator.  */
16329  parameter_declarator
16330     = cp_parser_parameter_declaration (parser,
16331					CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16332					/*template_parm_p=*/true,
16333					/*parenthesized_p=*/NULL);
16334
16335  if (!parameter_declarator)
16336    return error_mark_node;
16337
16338  /* If the parameter declaration is marked as a parameter pack, set
16339   *IS_PARAMETER_PACK to notify the caller.  */
16340  if (parameter_declarator->template_parameter_pack_p)
16341    *is_parameter_pack = true;
16342
16343  if (parameter_declarator->default_argument)
16344    {
16345      /* Can happen in some cases of erroneous input (c++/34892).  */
16346      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16347	/* Consume the `...' for better error recovery.  */
16348	cp_lexer_consume_token (parser->lexer);
16349    }
16350
16351  /* The parameter may have been constrained type parameter.  */
16352  if (is_constrained_parameter (parameter_declarator))
16353    return finish_constrained_parameter (parser,
16354                                         parameter_declarator,
16355                                         is_non_type);
16356
16357  // Now we're sure that the parameter is a non-type parameter.
16358  *is_non_type = true;
16359
16360  parm = grokdeclarator (parameter_declarator->declarator,
16361			 &parameter_declarator->decl_specifiers,
16362			 TPARM, /*initialized=*/0,
16363			 /*attrlist=*/NULL);
16364  if (parm == error_mark_node)
16365    return error_mark_node;
16366
16367  return build_tree_list (parameter_declarator->default_argument, parm);
16368}
16369
16370/* Parse a type-parameter.
16371
16372   type-parameter:
16373     class identifier [opt]
16374     class identifier [opt] = type-id
16375     typename identifier [opt]
16376     typename identifier [opt] = type-id
16377     template < template-parameter-list > class identifier [opt]
16378     template < template-parameter-list > class identifier [opt]
16379       = id-expression
16380
16381   GNU Extension (variadic templates):
16382
16383   type-parameter:
16384     class ... identifier [opt]
16385     typename ... identifier [opt]
16386
16387   Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
16388   TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
16389   the declaration of the parameter.
16390
16391   Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16392
16393static tree
16394cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16395{
16396  cp_token *token;
16397  tree parameter;
16398
16399  /* Look for a keyword to tell us what kind of parameter this is.  */
16400  token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16401  if (!token)
16402    return error_mark_node;
16403
16404  switch (token->keyword)
16405    {
16406    case RID_CLASS:
16407    case RID_TYPENAME:
16408      {
16409	tree identifier;
16410	tree default_argument;
16411
16412        /* If the next token is an ellipsis, we have a template
16413           argument pack. */
16414        if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16415          {
16416            /* Consume the `...' token. */
16417            cp_lexer_consume_token (parser->lexer);
16418            maybe_warn_variadic_templates ();
16419
16420            *is_parameter_pack = true;
16421          }
16422
16423	/* If the next token is an identifier, then it names the
16424	   parameter.  */
16425	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16426	  identifier = cp_parser_identifier (parser);
16427	else
16428	  identifier = NULL_TREE;
16429
16430	/* Create the parameter.  */
16431	parameter = finish_template_type_parm (class_type_node, identifier);
16432
16433	/* If the next token is an `=', we have a default argument.  */
16434	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16435	  {
16436	    default_argument
16437	      = cp_parser_default_type_template_argument (parser);
16438
16439            /* Template parameter packs cannot have default
16440               arguments. */
16441            if (*is_parameter_pack)
16442              {
16443                if (identifier)
16444                  error_at (token->location,
16445			    "template parameter pack %qD cannot have a "
16446			    "default argument", identifier);
16447                else
16448                  error_at (token->location,
16449			    "template parameter packs cannot have "
16450			    "default arguments");
16451                default_argument = NULL_TREE;
16452              }
16453	    else if (check_for_bare_parameter_packs (default_argument))
16454	      default_argument = error_mark_node;
16455	  }
16456	else
16457	  default_argument = NULL_TREE;
16458
16459	/* Create the combined representation of the parameter and the
16460	   default argument.  */
16461	parameter = build_tree_list (default_argument, parameter);
16462      }
16463      break;
16464
16465    case RID_TEMPLATE:
16466      {
16467	tree identifier;
16468	tree default_argument;
16469
16470	/* Look for the `<'.  */
16471	cp_parser_require (parser, CPP_LESS, RT_LESS);
16472	/* Parse the template-parameter-list.  */
16473	cp_parser_template_parameter_list (parser);
16474	/* Look for the `>'.  */
16475	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16476
16477	/* If template requirements are present, parse them.  */
16478	if (flag_concepts)
16479          {
16480	    tree reqs = get_shorthand_constraints (current_template_parms);
16481	    if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
16482              reqs = combine_constraint_expressions (reqs, dreqs);
16483	    TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16484          }
16485
16486	/* Look for the `class' or 'typename' keywords.  */
16487	cp_parser_type_parameter_key (parser);
16488        /* If the next token is an ellipsis, we have a template
16489           argument pack. */
16490        if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16491          {
16492            /* Consume the `...' token. */
16493            cp_lexer_consume_token (parser->lexer);
16494            maybe_warn_variadic_templates ();
16495
16496            *is_parameter_pack = true;
16497          }
16498	/* If the next token is an `=', then there is a
16499	   default-argument.  If the next token is a `>', we are at
16500	   the end of the parameter-list.  If the next token is a `,',
16501	   then we are at the end of this parameter.  */
16502	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16503	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16504	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16505	  {
16506	    identifier = cp_parser_identifier (parser);
16507	    /* Treat invalid names as if the parameter were nameless.  */
16508	    if (identifier == error_mark_node)
16509	      identifier = NULL_TREE;
16510	  }
16511	else
16512	  identifier = NULL_TREE;
16513
16514	/* Create the template parameter.  */
16515	parameter = finish_template_template_parm (class_type_node,
16516						   identifier);
16517
16518	/* If the next token is an `=', then there is a
16519	   default-argument.  */
16520	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16521	  {
16522	    default_argument
16523	      = cp_parser_default_template_template_argument (parser);
16524
16525            /* Template parameter packs cannot have default
16526               arguments. */
16527            if (*is_parameter_pack)
16528              {
16529                if (identifier)
16530                  error_at (token->location,
16531			    "template parameter pack %qD cannot "
16532			    "have a default argument",
16533			    identifier);
16534                else
16535                  error_at (token->location, "template parameter packs cannot "
16536			    "have default arguments");
16537                default_argument = NULL_TREE;
16538              }
16539	  }
16540	else
16541	  default_argument = NULL_TREE;
16542
16543	/* Create the combined representation of the parameter and the
16544	   default argument.  */
16545	parameter = build_tree_list (default_argument, parameter);
16546      }
16547      break;
16548
16549    default:
16550      gcc_unreachable ();
16551      break;
16552    }
16553
16554  return parameter;
16555}
16556
16557/* Parse a template-id.
16558
16559   template-id:
16560     template-name < template-argument-list [opt] >
16561
16562   If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16563   `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
16564   returned.  Otherwise, if the template-name names a function, or set
16565   of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
16566   names a class, returns a TYPE_DECL for the specialization.
16567
16568   If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16569   uninstantiated templates.  */
16570
16571static tree
16572cp_parser_template_id (cp_parser *parser,
16573		       bool template_keyword_p,
16574		       bool check_dependency_p,
16575		       enum tag_types tag_type,
16576		       bool is_declaration)
16577{
16578  tree templ;
16579  tree arguments;
16580  tree template_id;
16581  cp_token_position start_of_id = 0;
16582  cp_token *next_token = NULL, *next_token_2 = NULL;
16583  bool is_identifier;
16584
16585  /* If the next token corresponds to a template-id, there is no need
16586     to reparse it.  */
16587  cp_token *token = cp_lexer_peek_token (parser->lexer);
16588
16589  if (token->type == CPP_TEMPLATE_ID)
16590    {
16591      cp_lexer_consume_token (parser->lexer);
16592      return saved_checks_value (token->u.tree_check_value);
16593    }
16594
16595  /* Avoid performing name lookup if there is no possibility of
16596     finding a template-id.  */
16597  if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16598      || (token->type == CPP_NAME
16599	  && !cp_parser_nth_token_starts_template_argument_list_p
16600	       (parser, 2)))
16601    {
16602      cp_parser_error (parser, "expected template-id");
16603      return error_mark_node;
16604    }
16605
16606  /* Remember where the template-id starts.  */
16607  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16608    start_of_id = cp_lexer_token_position (parser->lexer, false);
16609
16610  push_deferring_access_checks (dk_deferred);
16611
16612  /* Parse the template-name.  */
16613  is_identifier = false;
16614  templ = cp_parser_template_name (parser, template_keyword_p,
16615				   check_dependency_p,
16616				   is_declaration,
16617				   tag_type,
16618				   &is_identifier);
16619
16620  /* Push any access checks inside the firewall we're about to create.  */
16621  vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16622  pop_deferring_access_checks ();
16623  if (templ == error_mark_node || is_identifier)
16624    return templ;
16625
16626  /* Since we're going to preserve any side-effects from this parse, set up a
16627     firewall to protect our callers from cp_parser_commit_to_tentative_parse
16628     in the template arguments.  */
16629  tentative_firewall firewall (parser);
16630  reopen_deferring_access_checks (checks);
16631
16632  /* If we find the sequence `[:' after a template-name, it's probably
16633     a digraph-typo for `< ::'. Substitute the tokens and check if we can
16634     parse correctly the argument list.  */
16635  if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16636       == CPP_OPEN_SQUARE)
16637      && next_token->flags & DIGRAPH
16638      && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16639	  == CPP_COLON)
16640      && !(next_token_2->flags & PREV_WHITE))
16641    {
16642      cp_parser_parse_tentatively (parser);
16643      /* Change `:' into `::'.  */
16644      next_token_2->type = CPP_SCOPE;
16645      /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16646	 CPP_LESS.  */
16647      cp_lexer_consume_token (parser->lexer);
16648
16649      /* Parse the arguments.  */
16650      arguments = cp_parser_enclosed_template_argument_list (parser);
16651      if (!cp_parser_parse_definitely (parser))
16652	{
16653	  /* If we couldn't parse an argument list, then we revert our changes
16654	     and return simply an error. Maybe this is not a template-id
16655	     after all.  */
16656	  next_token_2->type = CPP_COLON;
16657	  cp_parser_error (parser, "expected %<<%>");
16658	  pop_deferring_access_checks ();
16659	  return error_mark_node;
16660	}
16661      /* Otherwise, emit an error about the invalid digraph, but continue
16662	 parsing because we got our argument list.  */
16663      if (permerror (next_token->location,
16664		     "%<<::%> cannot begin a template-argument list"))
16665	{
16666	  static bool hint = false;
16667	  inform (next_token->location,
16668		  "%<<:%> is an alternate spelling for %<[%>."
16669		  " Insert whitespace between %<<%> and %<::%>");
16670	  if (!hint && !flag_permissive)
16671	    {
16672	      inform (next_token->location, "(if you use %<-fpermissive%> "
16673		      "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16674		      "accept your code)");
16675	      hint = true;
16676	    }
16677	}
16678    }
16679  else
16680    {
16681      /* Look for the `<' that starts the template-argument-list.  */
16682      if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16683	{
16684	  pop_deferring_access_checks ();
16685	  return error_mark_node;
16686	}
16687      /* Parse the arguments.  */
16688      arguments = cp_parser_enclosed_template_argument_list (parser);
16689
16690      if ((cxx_dialect > cxx17)
16691	  && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16692	  && !template_keyword_p
16693	  && (cp_parser_error_occurred (parser)
16694	      || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16695	{
16696	  /* This didn't go well.  */
16697	  if (TREE_CODE (templ) == FUNCTION_DECL)
16698	    {
16699	      /* C++2A says that "function-name < a;" is now ill-formed.  */
16700	      if (cp_parser_error_occurred (parser))
16701		{
16702		  error_at (token->location, "invalid template-argument-list");
16703		  inform (token->location, "function name as the left hand "
16704			  "operand of %<<%> is ill-formed in C++2a; wrap the "
16705			  "function name in %<()%>");
16706		}
16707	      else
16708		/* We expect "f<targs>" to be followed by "(args)".  */
16709		error_at (cp_lexer_peek_token (parser->lexer)->location,
16710			  "expected %<(%> after template-argument-list");
16711	      if (start_of_id)
16712		/* Purge all subsequent tokens.  */
16713		cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16714	    }
16715	  else
16716	    cp_parser_simulate_error (parser);
16717	  pop_deferring_access_checks ();
16718	  return error_mark_node;
16719	}
16720    }
16721
16722  /* Set the location to be of the form:
16723     template-name < template-argument-list [opt] >
16724     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16725     with caret == start at the start of the template-name,
16726     ranging until the closing '>'.  */
16727  location_t combined_loc
16728    = make_location (token->location, token->location, parser->lexer);
16729
16730  /* Check for concepts autos where they don't belong.  We could
16731     identify types in some cases of identifier TEMPL, looking ahead
16732     for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16733     types.  We reject them in functions, but if what we have is an
16734     identifier, even with none_type we can't conclude it's NOT a
16735     type, we have to wait for template substitution.  */
16736  if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16737    template_id = error_mark_node;
16738  /* Build a representation of the specialization.  */
16739  else if (identifier_p (templ))
16740    template_id = build_min_nt_loc (combined_loc,
16741				    TEMPLATE_ID_EXPR,
16742				    templ, arguments);
16743  else if (DECL_TYPE_TEMPLATE_P (templ)
16744	   || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16745    {
16746      /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16747	 template (rather than some instantiation thereof) only if
16748	 is not nested within some other construct.  For example, in
16749	 "template <typename T> void f(T) { A<T>::", A<T> is just an
16750	 instantiation of A.  */
16751      bool entering_scope
16752	= (template_parm_scope_p ()
16753	   && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16754      template_id
16755	= finish_template_type (templ, arguments, entering_scope);
16756    }
16757  else if (concept_definition_p (templ))
16758    {
16759      /* The caller will decide whether this is a concept check or type
16760	 constraint.  */
16761      template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
16762				boolean_type_node, templ, arguments);
16763    }
16764  else if (variable_template_p (templ))
16765    {
16766      template_id = lookup_template_variable (templ, arguments);
16767      if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16768	SET_EXPR_LOCATION (template_id, combined_loc);
16769    }
16770  else
16771    {
16772      /* If it's not a class-template or a template-template, it should be
16773	 a function-template.  */
16774      gcc_assert (OVL_P (templ) || BASELINK_P (templ));
16775
16776      template_id = lookup_template_function (templ, arguments);
16777      if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16778	SET_EXPR_LOCATION (template_id, combined_loc);
16779    }
16780
16781  /* If parsing tentatively, replace the sequence of tokens that makes
16782     up the template-id with a CPP_TEMPLATE_ID token.  That way,
16783     should we re-parse the token stream, we will not have to repeat
16784     the effort required to do the parse, nor will we issue duplicate
16785     error messages about problems during instantiation of the
16786     template.  */
16787  if (start_of_id
16788      /* Don't do this if we had a parse error in a declarator; re-parsing
16789	 might succeed if a name changes meaning (60361).  */
16790      && !(cp_parser_error_occurred (parser)
16791	   && cp_parser_parsing_tentatively (parser)
16792	   && parser->in_declarator_p))
16793    {
16794      /* Reset the contents of the START_OF_ID token.  */
16795      token->type = CPP_TEMPLATE_ID;
16796      token->location = combined_loc;
16797
16798      /* Retrieve any deferred checks.  Do not pop this access checks yet
16799	 so the memory will not be reclaimed during token replacing below.  */
16800      token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16801      token->tree_check_p = true;
16802      token->u.tree_check_value->value = template_id;
16803      token->u.tree_check_value->checks = get_deferred_access_checks ();
16804      token->keyword = RID_MAX;
16805
16806      /* Purge all subsequent tokens.  */
16807      cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16808
16809      /* ??? Can we actually assume that, if template_id ==
16810	 error_mark_node, we will have issued a diagnostic to the
16811	 user, as opposed to simply marking the tentative parse as
16812	 failed?  */
16813      if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16814	error_at (token->location, "parse error in template argument list");
16815    }
16816
16817  pop_to_parent_deferring_access_checks ();
16818  return template_id;
16819}
16820
16821/* Like cp_parser_template_id, called in non-type context.  */
16822
16823static tree
16824cp_parser_template_id_expr (cp_parser *parser,
16825			    bool template_keyword_p,
16826			    bool check_dependency_p,
16827			    bool is_declaration)
16828{
16829  tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
16830				  none_type, is_declaration);
16831  if (TREE_CODE (x) == TEMPLATE_ID_EXPR
16832      && concept_check_p (x))
16833    /* We didn't check the arguments in cp_parser_template_id; do that now.  */
16834    return build_concept_id (x);
16835  return x;
16836}
16837
16838/* Parse a template-name.
16839
16840   template-name:
16841     identifier
16842
16843   The standard should actually say:
16844
16845   template-name:
16846     identifier
16847     operator-function-id
16848
16849   A defect report has been filed about this issue.
16850
16851   A conversion-function-id cannot be a template name because they cannot
16852   be part of a template-id. In fact, looking at this code:
16853
16854   a.operator K<int>()
16855
16856   the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16857   It is impossible to call a templated conversion-function-id with an
16858   explicit argument list, since the only allowed template parameter is
16859   the type to which it is converting.
16860
16861   If TEMPLATE_KEYWORD_P is true, then we have just seen the
16862   `template' keyword, in a construction like:
16863
16864     T::template f<3>()
16865
16866   In that case `f' is taken to be a template-name, even though there
16867   is no way of knowing for sure.
16868
16869   Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16870   name refers to a set of overloaded functions, at least one of which
16871   is a template, or an IDENTIFIER_NODE with the name of the template,
16872   if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
16873   names are looked up inside uninstantiated templates.  */
16874
16875static tree
16876cp_parser_template_name (cp_parser* parser,
16877			 bool template_keyword_p,
16878			 bool check_dependency_p,
16879			 bool is_declaration,
16880			 enum tag_types tag_type,
16881			 bool *is_identifier)
16882{
16883  tree identifier;
16884  tree decl;
16885  cp_token *token = cp_lexer_peek_token (parser->lexer);
16886
16887  /* If the next token is `operator', then we have either an
16888     operator-function-id or a conversion-function-id.  */
16889  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16890    {
16891      /* We don't know whether we're looking at an
16892	 operator-function-id or a conversion-function-id.  */
16893      cp_parser_parse_tentatively (parser);
16894      /* Try an operator-function-id.  */
16895      identifier = cp_parser_operator_function_id (parser);
16896      /* If that didn't work, try a conversion-function-id.  */
16897      if (!cp_parser_parse_definitely (parser))
16898	{
16899	  cp_parser_error (parser, "expected template-name");
16900	  return error_mark_node;
16901	}
16902    }
16903  /* Look for the identifier.  */
16904  else
16905    identifier = cp_parser_identifier (parser);
16906
16907  /* If we didn't find an identifier, we don't have a template-id.  */
16908  if (identifier == error_mark_node)
16909    return error_mark_node;
16910
16911  /* If the name immediately followed the `template' keyword, then it
16912     is a template-name.  However, if the next token is not `<', then
16913     we do not treat it as a template-name, since it is not being used
16914     as part of a template-id.  This enables us to handle constructs
16915     like:
16916
16917       template <typename T> struct S { S(); };
16918       template <typename T> S<T>::S();
16919
16920     correctly.  We would treat `S' as a template -- if it were `S<T>'
16921     -- but we do not if there is no `<'.  */
16922
16923  if (processing_template_decl
16924      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16925    {
16926      /* In a declaration, in a dependent context, we pretend that the
16927	 "template" keyword was present in order to improve error
16928	 recovery.  For example, given:
16929
16930	   template <typename T> void f(T::X<int>);
16931
16932	 we want to treat "X<int>" as a template-id.  */
16933      if (is_declaration
16934	  && !template_keyword_p
16935	  && parser->scope && TYPE_P (parser->scope)
16936	  && check_dependency_p
16937	  && dependent_scope_p (parser->scope)
16938	  /* Do not do this for dtors (or ctors), since they never
16939	     need the template keyword before their name.  */
16940	  && !constructor_name_p (identifier, parser->scope))
16941	{
16942	  cp_token_position start = 0;
16943
16944	  /* Explain what went wrong.  */
16945	  error_at (token->location, "non-template %qD used as template",
16946		    identifier);
16947	  inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16948		  parser->scope, identifier);
16949	  /* If parsing tentatively, find the location of the "<" token.  */
16950	  if (cp_parser_simulate_error (parser))
16951	    start = cp_lexer_token_position (parser->lexer, true);
16952	  /* Parse the template arguments so that we can issue error
16953	     messages about them.  */
16954	  cp_lexer_consume_token (parser->lexer);
16955	  cp_parser_enclosed_template_argument_list (parser);
16956	  /* Skip tokens until we find a good place from which to
16957	     continue parsing.  */
16958	  cp_parser_skip_to_closing_parenthesis (parser,
16959						 /*recovering=*/true,
16960						 /*or_comma=*/true,
16961						 /*consume_paren=*/false);
16962	  /* If parsing tentatively, permanently remove the
16963	     template argument list.  That will prevent duplicate
16964	     error messages from being issued about the missing
16965	     "template" keyword.  */
16966	  if (start)
16967	    cp_lexer_purge_tokens_after (parser->lexer, start);
16968	  if (is_identifier)
16969	    *is_identifier = true;
16970	  parser->context->object_type = NULL_TREE;
16971	  return identifier;
16972	}
16973
16974      /* If the "template" keyword is present, then there is generally
16975	 no point in doing name-lookup, so we just return IDENTIFIER.
16976	 But, if the qualifying scope is non-dependent then we can
16977	 (and must) do name-lookup normally.  */
16978      if (template_keyword_p)
16979	{
16980	  tree scope = (parser->scope ? parser->scope
16981			: parser->context->object_type);
16982	  if (scope && TYPE_P (scope)
16983	      && (!CLASS_TYPE_P (scope)
16984		  || (check_dependency_p && dependent_type_p (scope))))
16985	    {
16986	      /* We're optimizing away the call to cp_parser_lookup_name, but
16987		 we still need to do this.  */
16988	      parser->context->object_type = NULL_TREE;
16989	      return identifier;
16990	    }
16991	}
16992    }
16993
16994  /* cp_parser_lookup_name clears OBJECT_TYPE.  */
16995  const bool scoped_p = ((parser->scope ? parser->scope
16996			  : parser->context->object_type) != NULL_TREE);
16997
16998  /* Look up the name.  */
16999  decl = cp_parser_lookup_name (parser, identifier,
17000				tag_type,
17001				/*is_template=*/true,
17002				/*is_namespace=*/false,
17003				check_dependency_p,
17004				/*ambiguous_decls=*/NULL,
17005				token->location);
17006
17007  decl = strip_using_decl (decl);
17008
17009  /* If DECL is a template, then the name was a template-name.  */
17010  if (TREE_CODE (decl) == TEMPLATE_DECL)
17011    {
17012      if (TREE_DEPRECATED (decl)
17013	  && deprecated_state != DEPRECATED_SUPPRESS)
17014	{
17015	  tree d = DECL_TEMPLATE_RESULT (decl);
17016	  tree attr;
17017	  if (TREE_CODE (d) == TYPE_DECL)
17018	    attr = lookup_attribute ("deprecated",
17019				     TYPE_ATTRIBUTES (TREE_TYPE (d)));
17020	  else
17021	    attr = lookup_attribute ("deprecated",
17022				     DECL_ATTRIBUTES (d));
17023	  warn_deprecated_use (decl, attr);
17024	}
17025    }
17026  else
17027    {
17028      /* The standard does not explicitly indicate whether a name that
17029	 names a set of overloaded declarations, some of which are
17030	 templates, is a template-name.  However, such a name should
17031	 be a template-name; otherwise, there is no way to form a
17032	 template-id for the overloaded templates.  */
17033      bool found = false;
17034
17035      for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
17036	   !found && iter; ++iter)
17037	if (TREE_CODE (*iter) == TEMPLATE_DECL)
17038	  found = true;
17039
17040      if (!found
17041	  && (cxx_dialect > cxx17)
17042	  && !scoped_p
17043	  && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
17044	  && tag_type == none_type)
17045	{
17046	  /* [temp.names] says "A name is also considered to refer to a template
17047	     if it is an unqualified-id followed by a < and name lookup finds
17048	     either one or more functions or finds nothing."  */
17049
17050	  /* The "more functions" case.  Just use the OVERLOAD as normally.
17051	     We don't use is_overloaded_fn here to avoid considering
17052	     BASELINKs.  */
17053	  if (TREE_CODE (decl) == OVERLOAD
17054	      /* Name lookup found one function.  */
17055	      || TREE_CODE (decl) == FUNCTION_DECL)
17056	    found = true;
17057	  /* Name lookup found nothing.  */
17058	  else if (decl == error_mark_node)
17059	    return identifier;
17060	}
17061
17062      if (!found)
17063	{
17064	  /* The name does not name a template.  */
17065	  cp_parser_error (parser, "expected template-name");
17066	  return error_mark_node;
17067	}
17068    }
17069
17070  return decl;
17071}
17072
17073/* Parse a template-argument-list.
17074
17075   template-argument-list:
17076     template-argument ... [opt]
17077     template-argument-list , template-argument ... [opt]
17078
17079   Returns a TREE_VEC containing the arguments.  */
17080
17081static tree
17082cp_parser_template_argument_list (cp_parser* parser)
17083{
17084  tree fixed_args[10];
17085  unsigned n_args = 0;
17086  unsigned alloced = 10;
17087  tree *arg_ary = fixed_args;
17088  tree vec;
17089  bool saved_in_template_argument_list_p;
17090  bool saved_ice_p;
17091  bool saved_non_ice_p;
17092
17093  /* Don't create location wrapper nodes within a template-argument-list.  */
17094  auto_suppress_location_wrappers sentinel;
17095
17096  saved_in_template_argument_list_p = parser->in_template_argument_list_p;
17097  parser->in_template_argument_list_p = true;
17098  /* Even if the template-id appears in an integral
17099     constant-expression, the contents of the argument list do
17100     not.  */
17101  saved_ice_p = parser->integral_constant_expression_p;
17102  parser->integral_constant_expression_p = false;
17103  saved_non_ice_p = parser->non_integral_constant_expression_p;
17104  parser->non_integral_constant_expression_p = false;
17105
17106  /* Parse the arguments.  */
17107  do
17108    {
17109      tree argument;
17110
17111      if (n_args)
17112	/* Consume the comma.  */
17113	cp_lexer_consume_token (parser->lexer);
17114
17115      /* Parse the template-argument.  */
17116      argument = cp_parser_template_argument (parser);
17117
17118      /* If the next token is an ellipsis, we're expanding a template
17119         argument pack. */
17120      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17121        {
17122	  if (argument == error_mark_node)
17123	    {
17124	      cp_token *token = cp_lexer_peek_token (parser->lexer);
17125	      error_at (token->location,
17126			"expected parameter pack before %<...%>");
17127	    }
17128          /* Consume the `...' token. */
17129          cp_lexer_consume_token (parser->lexer);
17130
17131          /* Make the argument into a TYPE_PACK_EXPANSION or
17132             EXPR_PACK_EXPANSION. */
17133          argument = make_pack_expansion (argument);
17134        }
17135
17136      if (n_args == alloced)
17137	{
17138	  alloced *= 2;
17139
17140	  if (arg_ary == fixed_args)
17141	    {
17142	      arg_ary = XNEWVEC (tree, alloced);
17143	      memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
17144	    }
17145	  else
17146	    arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
17147	}
17148      arg_ary[n_args++] = argument;
17149    }
17150  while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17151
17152  vec = make_tree_vec (n_args);
17153
17154  while (n_args--)
17155    TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
17156
17157  if (arg_ary != fixed_args)
17158    free (arg_ary);
17159  parser->non_integral_constant_expression_p = saved_non_ice_p;
17160  parser->integral_constant_expression_p = saved_ice_p;
17161  parser->in_template_argument_list_p = saved_in_template_argument_list_p;
17162  if (CHECKING_P)
17163    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
17164  return vec;
17165}
17166
17167/* Parse a template-argument.
17168
17169   template-argument:
17170     assignment-expression
17171     type-id
17172     id-expression
17173
17174   The representation is that of an assignment-expression, type-id, or
17175   id-expression -- except that the qualified id-expression is
17176   evaluated, so that the value returned is either a DECL or an
17177   OVERLOAD.
17178
17179   Although the standard says "assignment-expression", it forbids
17180   throw-expressions or assignments in the template argument.
17181   Therefore, we use "conditional-expression" instead.  */
17182
17183static tree
17184cp_parser_template_argument (cp_parser* parser)
17185{
17186  tree argument;
17187  bool template_p;
17188  bool address_p;
17189  bool maybe_type_id = false;
17190  cp_token *token = NULL, *argument_start_token = NULL;
17191  location_t loc = 0;
17192  cp_id_kind idk;
17193
17194  /* There's really no way to know what we're looking at, so we just
17195     try each alternative in order.
17196
17197       [temp.arg]
17198
17199       In a template-argument, an ambiguity between a type-id and an
17200       expression is resolved to a type-id, regardless of the form of
17201       the corresponding template-parameter.
17202
17203     Therefore, we try a type-id first.  */
17204  cp_parser_parse_tentatively (parser);
17205  argument = cp_parser_template_type_arg (parser);
17206  /* If there was no error parsing the type-id but the next token is a
17207     '>>', our behavior depends on which dialect of C++ we're
17208     parsing. In C++98, we probably found a typo for '> >'. But there
17209     are type-id which are also valid expressions. For instance:
17210
17211     struct X { int operator >> (int); };
17212     template <int V> struct Foo {};
17213     Foo<X () >> 5> r;
17214
17215     Here 'X()' is a valid type-id of a function type, but the user just
17216     wanted to write the expression "X() >> 5". Thus, we remember that we
17217     found a valid type-id, but we still try to parse the argument as an
17218     expression to see what happens.
17219
17220     In C++0x, the '>>' will be considered two separate '>'
17221     tokens.  */
17222  if (!cp_parser_error_occurred (parser)
17223      && cxx_dialect == cxx98
17224      && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17225    {
17226      maybe_type_id = true;
17227      cp_parser_abort_tentative_parse (parser);
17228    }
17229  else
17230    {
17231      /* If the next token isn't a `,' or a `>', then this argument wasn't
17232      really finished. This means that the argument is not a valid
17233      type-id.  */
17234      if (!cp_parser_next_token_ends_template_argument_p (parser))
17235	cp_parser_error (parser, "expected template-argument");
17236      /* If that worked, we're done.  */
17237      if (cp_parser_parse_definitely (parser))
17238	return argument;
17239    }
17240  /* We're still not sure what the argument will be.  */
17241  cp_parser_parse_tentatively (parser);
17242  /* Try a template.  */
17243  argument_start_token = cp_lexer_peek_token (parser->lexer);
17244  argument = cp_parser_id_expression (parser,
17245				      /*template_keyword_p=*/false,
17246				      /*check_dependency_p=*/true,
17247				      &template_p,
17248				      /*declarator_p=*/false,
17249				      /*optional_p=*/false);
17250  /* If the next token isn't a `,' or a `>', then this argument wasn't
17251     really finished.  */
17252  if (!cp_parser_next_token_ends_template_argument_p (parser))
17253    cp_parser_error (parser, "expected template-argument");
17254  if (!cp_parser_error_occurred (parser))
17255    {
17256      /* Figure out what is being referred to.  If the id-expression
17257	 was for a class template specialization, then we will have a
17258	 TYPE_DECL at this point.  There is no need to do name lookup
17259	 at this point in that case.  */
17260      if (TREE_CODE (argument) != TYPE_DECL)
17261	argument = cp_parser_lookup_name (parser, argument,
17262					  none_type,
17263					  /*is_template=*/template_p,
17264					  /*is_namespace=*/false,
17265					  /*check_dependency=*/true,
17266					  /*ambiguous_decls=*/NULL,
17267					  argument_start_token->location);
17268      if (TREE_CODE (argument) != TEMPLATE_DECL
17269	       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
17270	cp_parser_error (parser, "expected template-name");
17271    }
17272  if (cp_parser_parse_definitely (parser))
17273    {
17274      if (TREE_DEPRECATED (argument))
17275	warn_deprecated_use (argument, NULL_TREE);
17276      return argument;
17277    }
17278  /* It must be a non-type argument.  In C++17 any constant-expression is
17279     allowed.  */
17280  if (cxx_dialect > cxx14)
17281    goto general_expr;
17282
17283  /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17284
17285     -- an integral constant-expression of integral or enumeration
17286	type; or
17287
17288     -- the name of a non-type template-parameter; or
17289
17290     -- the name of an object or function with external linkage...
17291
17292     -- the address of an object or function with external linkage...
17293
17294     -- a pointer to member...  */
17295  /* Look for a non-type template parameter.  */
17296  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17297    {
17298      cp_parser_parse_tentatively (parser);
17299      argument = cp_parser_primary_expression (parser,
17300					       /*address_p=*/false,
17301					       /*cast_p=*/false,
17302					       /*template_arg_p=*/true,
17303					       &idk);
17304      if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17305	  || !cp_parser_next_token_ends_template_argument_p (parser))
17306	cp_parser_simulate_error (parser);
17307      if (cp_parser_parse_definitely (parser))
17308	return argument;
17309    }
17310
17311  /* If the next token is "&", the argument must be the address of an
17312     object or function with external linkage.  */
17313  address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17314  if (address_p)
17315    {
17316      loc = cp_lexer_peek_token (parser->lexer)->location;
17317      cp_lexer_consume_token (parser->lexer);
17318    }
17319  /* See if we might have an id-expression.  */
17320  token = cp_lexer_peek_token (parser->lexer);
17321  if (token->type == CPP_NAME
17322      || token->keyword == RID_OPERATOR
17323      || token->type == CPP_SCOPE
17324      || token->type == CPP_TEMPLATE_ID
17325      || token->type == CPP_NESTED_NAME_SPECIFIER)
17326    {
17327      cp_parser_parse_tentatively (parser);
17328      argument = cp_parser_primary_expression (parser,
17329					       address_p,
17330					       /*cast_p=*/false,
17331					       /*template_arg_p=*/true,
17332					       &idk);
17333      if (cp_parser_error_occurred (parser)
17334	  || !cp_parser_next_token_ends_template_argument_p (parser))
17335	cp_parser_abort_tentative_parse (parser);
17336      else
17337	{
17338	  tree probe;
17339
17340	  if (INDIRECT_REF_P (argument))
17341	    {
17342	      /* Strip the dereference temporarily.  */
17343	      gcc_assert (REFERENCE_REF_P (argument));
17344	      argument = TREE_OPERAND (argument, 0);
17345	    }
17346
17347	  /* If we're in a template, we represent a qualified-id referring
17348	     to a static data member as a SCOPE_REF even if the scope isn't
17349	     dependent so that we can check access control later.  */
17350	  probe = argument;
17351	  if (TREE_CODE (probe) == SCOPE_REF)
17352	    probe = TREE_OPERAND (probe, 1);
17353	  if (VAR_P (probe))
17354	    {
17355	      /* A variable without external linkage might still be a
17356		 valid constant-expression, so no error is issued here
17357		 if the external-linkage check fails.  */
17358	      if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17359		cp_parser_simulate_error (parser);
17360	    }
17361	  else if (is_overloaded_fn (argument))
17362	    /* All overloaded functions are allowed; if the external
17363	       linkage test does not pass, an error will be issued
17364	       later.  */
17365	    ;
17366	  else if (address_p
17367		   && (TREE_CODE (argument) == OFFSET_REF
17368		       || TREE_CODE (argument) == SCOPE_REF))
17369	    /* A pointer-to-member.  */
17370	    ;
17371	  else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17372	    ;
17373	  else
17374	    cp_parser_simulate_error (parser);
17375
17376	  if (cp_parser_parse_definitely (parser))
17377	    {
17378	      if (address_p)
17379		argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17380					     tf_warning_or_error);
17381	      else
17382		argument = convert_from_reference (argument);
17383	      return argument;
17384	    }
17385	}
17386    }
17387  /* If the argument started with "&", there are no other valid
17388     alternatives at this point.  */
17389  if (address_p)
17390    {
17391      cp_parser_error (parser, "invalid non-type template argument");
17392      return error_mark_node;
17393    }
17394
17395 general_expr:
17396  /* If the argument wasn't successfully parsed as a type-id followed
17397     by '>>', the argument can only be a constant expression now.
17398     Otherwise, we try parsing the constant-expression tentatively,
17399     because the argument could really be a type-id.  */
17400  if (maybe_type_id)
17401    cp_parser_parse_tentatively (parser);
17402
17403  if (cxx_dialect <= cxx14)
17404    argument = cp_parser_constant_expression (parser);
17405  else
17406    {
17407      /* In C++20, we can encounter a braced-init-list.  */
17408      if (cxx_dialect >= cxx2a
17409	  && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17410	{
17411	  bool expr_non_constant_p;
17412	  return cp_parser_braced_list (parser, &expr_non_constant_p);
17413	}
17414
17415      /* With C++17 generalized non-type template arguments we need to handle
17416	 lvalue constant expressions, too.  */
17417      argument = cp_parser_assignment_expression (parser);
17418      require_potential_constant_expression (argument);
17419    }
17420
17421  if (!maybe_type_id)
17422    return argument;
17423  if (!cp_parser_next_token_ends_template_argument_p (parser))
17424    cp_parser_error (parser, "expected template-argument");
17425  if (cp_parser_parse_definitely (parser))
17426    return argument;
17427  /* We did our best to parse the argument as a non type-id, but that
17428     was the only alternative that matched (albeit with a '>' after
17429     it). We can assume it's just a typo from the user, and a
17430     diagnostic will then be issued.  */
17431  return cp_parser_template_type_arg (parser);
17432}
17433
17434/* Parse an explicit-instantiation.
17435
17436   explicit-instantiation:
17437     template declaration
17438
17439   Although the standard says `declaration', what it really means is:
17440
17441   explicit-instantiation:
17442     template decl-specifier-seq [opt] declarator [opt] ;
17443
17444   Things like `template int S<int>::i = 5, int S<double>::j;' are not
17445   supposed to be allowed.  A defect report has been filed about this
17446   issue.
17447
17448   GNU Extension:
17449
17450   explicit-instantiation:
17451     storage-class-specifier template
17452       decl-specifier-seq [opt] declarator [opt] ;
17453     function-specifier template
17454       decl-specifier-seq [opt] declarator [opt] ;  */
17455
17456static void
17457cp_parser_explicit_instantiation (cp_parser* parser)
17458{
17459  int declares_class_or_enum;
17460  cp_decl_specifier_seq decl_specifiers;
17461  tree extension_specifier = NULL_TREE;
17462
17463  timevar_push (TV_TEMPLATE_INST);
17464
17465  /* Look for an (optional) storage-class-specifier or
17466     function-specifier.  */
17467  if (cp_parser_allow_gnu_extensions_p (parser))
17468    {
17469      extension_specifier
17470	= cp_parser_storage_class_specifier_opt (parser);
17471      if (!extension_specifier)
17472	extension_specifier
17473	  = cp_parser_function_specifier_opt (parser,
17474					      /*decl_specs=*/NULL);
17475    }
17476
17477  /* Look for the `template' keyword.  */
17478  cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17479  /* Let the front end know that we are processing an explicit
17480     instantiation.  */
17481  begin_explicit_instantiation ();
17482  /* [temp.explicit] says that we are supposed to ignore access
17483     control while processing explicit instantiation directives.  */
17484  push_deferring_access_checks (dk_no_check);
17485  /* Parse a decl-specifier-seq.  */
17486  cp_parser_decl_specifier_seq (parser,
17487				CP_PARSER_FLAGS_OPTIONAL,
17488				&decl_specifiers,
17489				&declares_class_or_enum);
17490  /* If there was exactly one decl-specifier, and it declared a class,
17491     and there's no declarator, then we have an explicit type
17492     instantiation.  */
17493  if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17494    {
17495      tree type;
17496
17497      type = check_tag_decl (&decl_specifiers,
17498			     /*explicit_type_instantiation_p=*/true);
17499      /* Turn access control back on for names used during
17500	 template instantiation.  */
17501      pop_deferring_access_checks ();
17502      if (type)
17503	do_type_instantiation (type, extension_specifier,
17504			       /*complain=*/tf_error);
17505    }
17506  else
17507    {
17508      cp_declarator *declarator;
17509      tree decl;
17510
17511      /* Parse the declarator.  */
17512      declarator
17513	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17514				CP_PARSER_FLAGS_NONE,
17515				/*ctor_dtor_or_conv_p=*/NULL,
17516				/*parenthesized_p=*/NULL,
17517				/*member_p=*/false,
17518				/*friend_p=*/false,
17519				/*static_p=*/false);
17520      if (declares_class_or_enum & 2)
17521	cp_parser_check_for_definition_in_return_type (declarator,
17522						       decl_specifiers.type,
17523						       decl_specifiers.locations[ds_type_spec]);
17524      if (declarator != cp_error_declarator)
17525	{
17526	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17527	    permerror (decl_specifiers.locations[ds_inline],
17528		       "explicit instantiation shall not use"
17529		       " %<inline%> specifier");
17530	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17531	    permerror (decl_specifiers.locations[ds_constexpr],
17532		       "explicit instantiation shall not use"
17533		       " %<constexpr%> specifier");
17534	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
17535	    permerror (decl_specifiers.locations[ds_consteval],
17536		       "explicit instantiation shall not use"
17537		       " %<consteval%> specifier");
17538
17539	  decl = grokdeclarator (declarator, &decl_specifiers,
17540				 NORMAL, 0, &decl_specifiers.attributes);
17541	  /* Turn access control back on for names used during
17542	     template instantiation.  */
17543	  pop_deferring_access_checks ();
17544	  /* Do the explicit instantiation.  */
17545	  do_decl_instantiation (decl, extension_specifier);
17546	}
17547      else
17548	{
17549	  pop_deferring_access_checks ();
17550	  /* Skip the body of the explicit instantiation.  */
17551	  cp_parser_skip_to_end_of_statement (parser);
17552	}
17553    }
17554  /* We're done with the instantiation.  */
17555  end_explicit_instantiation ();
17556
17557  cp_parser_consume_semicolon_at_end_of_statement (parser);
17558
17559  timevar_pop (TV_TEMPLATE_INST);
17560}
17561
17562/* Parse an explicit-specialization.
17563
17564   explicit-specialization:
17565     template < > declaration
17566
17567   Although the standard says `declaration', what it really means is:
17568
17569   explicit-specialization:
17570     template <> decl-specifier [opt] init-declarator [opt] ;
17571     template <> function-definition
17572     template <> explicit-specialization
17573     template <> template-declaration  */
17574
17575static void
17576cp_parser_explicit_specialization (cp_parser* parser)
17577{
17578  bool need_lang_pop;
17579  cp_token *token = cp_lexer_peek_token (parser->lexer);
17580
17581  /* Look for the `template' keyword.  */
17582  cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17583  /* Look for the `<'.  */
17584  cp_parser_require (parser, CPP_LESS, RT_LESS);
17585  /* Look for the `>'.  */
17586  cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17587  /* We have processed another parameter list.  */
17588  ++parser->num_template_parameter_lists;
17589  /* [temp]
17590
17591     A template ... explicit specialization ... shall not have C
17592     linkage.  */
17593  if (current_lang_name == lang_name_c)
17594    {
17595      error_at (token->location, "template specialization with C linkage");
17596      maybe_show_extern_c_location ();
17597      /* Give it C++ linkage to avoid confusing other parts of the
17598	 front end.  */
17599      push_lang_context (lang_name_cplusplus);
17600      need_lang_pop = true;
17601    }
17602  else
17603    need_lang_pop = false;
17604  /* Let the front end know that we are beginning a specialization.  */
17605  if (!begin_specialization ())
17606    {
17607      end_specialization ();
17608      return;
17609    }
17610
17611  /* If the next keyword is `template', we need to figure out whether
17612     or not we're looking a template-declaration.  */
17613  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17614    {
17615      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17616	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17617	cp_parser_template_declaration_after_export (parser,
17618						     /*member_p=*/false);
17619      else
17620	cp_parser_explicit_specialization (parser);
17621    }
17622  else
17623    /* Parse the dependent declaration.  */
17624    cp_parser_single_declaration (parser,
17625				  /*checks=*/NULL,
17626				  /*member_p=*/false,
17627				  /*explicit_specialization_p=*/true,
17628				  /*friend_p=*/NULL);
17629  /* We're done with the specialization.  */
17630  end_specialization ();
17631  /* For the erroneous case of a template with C linkage, we pushed an
17632     implicit C++ linkage scope; exit that scope now.  */
17633  if (need_lang_pop)
17634    pop_lang_context ();
17635  /* We're done with this parameter list.  */
17636  --parser->num_template_parameter_lists;
17637}
17638
17639/* Preserve the attributes across a garbage collect (by making it a GC
17640   root), which can occur when parsing a member function.  */
17641
17642static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
17643
17644/* Parse a type-specifier.
17645
17646   type-specifier:
17647     simple-type-specifier
17648     class-specifier
17649     enum-specifier
17650     elaborated-type-specifier
17651     cv-qualifier
17652
17653   GNU Extension:
17654
17655   type-specifier:
17656     __complex__
17657
17658   Returns a representation of the type-specifier.  For a
17659   class-specifier, enum-specifier, or elaborated-type-specifier, a
17660   TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17661
17662   The parser flags FLAGS is used to control type-specifier parsing.
17663
17664   If IS_DECLARATION is TRUE, then this type-specifier is appearing
17665   in a decl-specifier-seq.
17666
17667   If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17668   class-specifier, enum-specifier, or elaborated-type-specifier, then
17669   *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
17670   if a type is declared; 2 if it is defined.  Otherwise, it is set to
17671   zero.
17672
17673   If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17674   cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
17675   is set to FALSE.  */
17676
17677static tree
17678cp_parser_type_specifier (cp_parser* parser,
17679			  cp_parser_flags flags,
17680			  cp_decl_specifier_seq *decl_specs,
17681			  bool is_declaration,
17682			  int* declares_class_or_enum,
17683			  bool* is_cv_qualifier)
17684{
17685  tree type_spec = NULL_TREE;
17686  cp_token *token;
17687  enum rid keyword;
17688  cp_decl_spec ds = ds_last;
17689
17690  /* Assume this type-specifier does not declare a new type.  */
17691  if (declares_class_or_enum)
17692    *declares_class_or_enum = 0;
17693  /* And that it does not specify a cv-qualifier.  */
17694  if (is_cv_qualifier)
17695    *is_cv_qualifier = false;
17696  /* Peek at the next token.  */
17697  token = cp_lexer_peek_token (parser->lexer);
17698
17699  /* If we're looking at a keyword, we can use that to guide the
17700     production we choose.  */
17701  keyword = token->keyword;
17702  switch (keyword)
17703    {
17704    case RID_ENUM:
17705      if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17706	goto elaborated_type_specifier;
17707
17708      /* Look for the enum-specifier.  */
17709      type_spec = cp_parser_enum_specifier (parser);
17710      /* If that worked, we're done.  */
17711      if (type_spec)
17712	{
17713	  if (declares_class_or_enum)
17714	    *declares_class_or_enum = 2;
17715	  if (decl_specs)
17716	    cp_parser_set_decl_spec_type (decl_specs,
17717					  type_spec,
17718					  token,
17719					  /*type_definition_p=*/true);
17720	  return type_spec;
17721	}
17722      else
17723	goto elaborated_type_specifier;
17724
17725      /* Any of these indicate either a class-specifier, or an
17726	 elaborated-type-specifier.  */
17727    case RID_CLASS:
17728    case RID_STRUCT:
17729    case RID_UNION:
17730      if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17731	goto elaborated_type_specifier;
17732
17733      /* Parse tentatively so that we can back up if we don't find a
17734	 class-specifier.  */
17735      cp_parser_parse_tentatively (parser);
17736      if (decl_specs->attributes)
17737	vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
17738      /* Look for the class-specifier.  */
17739      type_spec = cp_parser_class_specifier (parser);
17740      if (decl_specs->attributes)
17741	cp_parser_decl_specs_attrs->pop ();
17742      invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17743      /* If that worked, we're done.  */
17744      if (cp_parser_parse_definitely (parser))
17745	{
17746	  if (declares_class_or_enum)
17747	    *declares_class_or_enum = 2;
17748	  if (decl_specs)
17749	    cp_parser_set_decl_spec_type (decl_specs,
17750					  type_spec,
17751					  token,
17752					  /*type_definition_p=*/true);
17753	  return type_spec;
17754	}
17755
17756      /* Fall through.  */
17757    elaborated_type_specifier:
17758      /* We're declaring (not defining) a class or enum.  */
17759      if (declares_class_or_enum)
17760	*declares_class_or_enum = 1;
17761
17762      /* Fall through.  */
17763    case RID_TYPENAME:
17764      /* Look for an elaborated-type-specifier.  */
17765      type_spec
17766	= (cp_parser_elaborated_type_specifier
17767	   (parser,
17768	    decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17769	    is_declaration));
17770      if (decl_specs)
17771	cp_parser_set_decl_spec_type (decl_specs,
17772				      type_spec,
17773				      token,
17774				      /*type_definition_p=*/false);
17775      return type_spec;
17776
17777    case RID_CONST:
17778      ds = ds_const;
17779      if (is_cv_qualifier)
17780	*is_cv_qualifier = true;
17781      break;
17782
17783    case RID_VOLATILE:
17784      ds = ds_volatile;
17785      if (is_cv_qualifier)
17786	*is_cv_qualifier = true;
17787      break;
17788
17789    case RID_RESTRICT:
17790      ds = ds_restrict;
17791      if (is_cv_qualifier)
17792	*is_cv_qualifier = true;
17793      break;
17794
17795    case RID_COMPLEX:
17796      /* The `__complex__' keyword is a GNU extension.  */
17797      ds = ds_complex;
17798      break;
17799
17800    default:
17801      break;
17802    }
17803
17804  /* Handle simple keywords.  */
17805  if (ds != ds_last)
17806    {
17807      if (decl_specs)
17808	{
17809	  set_and_check_decl_spec_loc (decl_specs, ds, token);
17810	  decl_specs->any_specifiers_p = true;
17811	}
17812      return cp_lexer_consume_token (parser->lexer)->u.value;
17813    }
17814
17815  /* If we do not already have a type-specifier, assume we are looking
17816     at a simple-type-specifier.  */
17817  type_spec = cp_parser_simple_type_specifier (parser,
17818					       decl_specs,
17819					       flags);
17820
17821  /* If we didn't find a type-specifier, and a type-specifier was not
17822     optional in this context, issue an error message.  */
17823  if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17824    {
17825      cp_parser_error (parser, "expected type specifier");
17826      return error_mark_node;
17827    }
17828
17829  return type_spec;
17830}
17831
17832/* Parse a simple-type-specifier.
17833
17834   simple-type-specifier:
17835     :: [opt] nested-name-specifier [opt] type-name
17836     :: [opt] nested-name-specifier template template-id
17837     char
17838     wchar_t
17839     bool
17840     short
17841     int
17842     long
17843     signed
17844     unsigned
17845     float
17846     double
17847     void
17848
17849   C++11 Extension:
17850
17851   simple-type-specifier:
17852     auto
17853     decltype ( expression )
17854     char16_t
17855     char32_t
17856     __underlying_type ( type-id )
17857
17858   C++17 extension:
17859
17860     nested-name-specifier(opt) template-name
17861
17862   GNU Extension:
17863
17864   simple-type-specifier:
17865     __int128
17866     __typeof__ unary-expression
17867     __typeof__ ( type-id )
17868     __typeof__ ( type-id ) { initializer-list , [opt] }
17869
17870   Concepts Extension:
17871
17872   simple-type-specifier:
17873     constrained-type-specifier
17874
17875   Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
17876   appropriately updated.  */
17877
17878static tree
17879cp_parser_simple_type_specifier (cp_parser* parser,
17880				 cp_decl_specifier_seq *decl_specs,
17881				 cp_parser_flags flags)
17882{
17883  tree type = NULL_TREE;
17884  cp_token *token;
17885  int idx;
17886
17887  /* Peek at the next token.  */
17888  token = cp_lexer_peek_token (parser->lexer);
17889
17890  /* If we're looking at a keyword, things are easy.  */
17891  switch (token->keyword)
17892    {
17893    case RID_CHAR:
17894      if (decl_specs)
17895	decl_specs->explicit_char_p = true;
17896      type = char_type_node;
17897      break;
17898    case RID_CHAR8:
17899      type = char8_type_node;
17900      break;
17901    case RID_CHAR16:
17902      type = char16_type_node;
17903      break;
17904    case RID_CHAR32:
17905      type = char32_type_node;
17906      break;
17907    case RID_WCHAR:
17908      type = wchar_type_node;
17909      break;
17910    case RID_BOOL:
17911      type = boolean_type_node;
17912      break;
17913    case RID_SHORT:
17914      set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17915      type = short_integer_type_node;
17916      break;
17917    case RID_INT:
17918      if (decl_specs)
17919	decl_specs->explicit_int_p = true;
17920      type = integer_type_node;
17921      break;
17922    case RID_INT_N_0:
17923    case RID_INT_N_1:
17924    case RID_INT_N_2:
17925    case RID_INT_N_3:
17926      idx = token->keyword - RID_INT_N_0;
17927      if (! int_n_enabled_p [idx])
17928	break;
17929      if (decl_specs)
17930	{
17931	  decl_specs->explicit_intN_p = true;
17932	  decl_specs->int_n_idx = idx;
17933	  /* Check if the alternate "__intN__" form has been used instead of
17934	     "__intN".  */
17935	  if (strncmp (IDENTIFIER_POINTER (token->u.value)
17936			+ (IDENTIFIER_LENGTH (token->u.value) - 2),
17937			"__", 2) == 0)
17938	    decl_specs->int_n_alt = true;
17939	}
17940      type = int_n_trees [idx].signed_type;
17941      break;
17942    case RID_LONG:
17943      if (decl_specs)
17944	set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17945      type = long_integer_type_node;
17946      break;
17947    case RID_SIGNED:
17948      set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17949      type = integer_type_node;
17950      break;
17951    case RID_UNSIGNED:
17952      set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17953      type = unsigned_type_node;
17954      break;
17955    case RID_FLOAT:
17956      type = float_type_node;
17957      break;
17958    case RID_DOUBLE:
17959      type = double_type_node;
17960      break;
17961    case RID_VOID:
17962      type = void_type_node;
17963      break;
17964
17965    case RID_AUTO:
17966      maybe_warn_cpp0x (CPP0X_AUTO);
17967      if (parser->auto_is_implicit_function_template_parm_p)
17968	{
17969	  /* The 'auto' might be the placeholder return type for a function decl
17970	     with trailing return type.  */
17971	  bool have_trailing_return_fn_decl = false;
17972
17973	  cp_parser_parse_tentatively (parser);
17974	  cp_lexer_consume_token (parser->lexer);
17975	  while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17976		 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17977		 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17978		 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17979	    {
17980	      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17981		{
17982		  cp_lexer_consume_token (parser->lexer);
17983		  cp_parser_skip_to_closing_parenthesis (parser,
17984							 /*recovering*/false,
17985							 /*or_comma*/false,
17986							 /*consume_paren*/true);
17987		  continue;
17988		}
17989
17990	      if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17991		{
17992		  have_trailing_return_fn_decl = true;
17993		  break;
17994		}
17995
17996	      cp_lexer_consume_token (parser->lexer);
17997	    }
17998	  cp_parser_abort_tentative_parse (parser);
17999
18000	  if (have_trailing_return_fn_decl)
18001	    {
18002	      type = make_auto ();
18003	      break;
18004	    }
18005
18006	  if (cxx_dialect >= cxx14)
18007	    {
18008	      type = synthesize_implicit_template_parm (parser, NULL_TREE);
18009	      type = TREE_TYPE (type);
18010	    }
18011	  else
18012	    type = error_mark_node;
18013
18014	  if (current_class_type && LAMBDA_TYPE_P (current_class_type))
18015	    {
18016	      if (cxx_dialect < cxx14)
18017		error_at (token->location,
18018			 "use of %<auto%> in lambda parameter declaration "
18019			 "only available with "
18020			 "%<-std=c++14%> or %<-std=gnu++14%>");
18021	    }
18022	  else if (cxx_dialect < cxx14)
18023	    error_at (token->location,
18024		     "use of %<auto%> in parameter declaration "
18025		     "only available with "
18026		     "%<-std=c++14%> or %<-std=gnu++14%>");
18027	  else if (!flag_concepts)
18028	    pedwarn (token->location, 0,
18029		     "use of %<auto%> in parameter declaration "
18030		     "only available with %<-fconcepts-ts%>");
18031	}
18032      else
18033	type = make_auto ();
18034      break;
18035
18036    case RID_DECLTYPE:
18037      /* Since DR 743, decltype can either be a simple-type-specifier by
18038	 itself or begin a nested-name-specifier.  Parsing it will replace
18039	 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
18040	 handling below decide what to do.  */
18041      cp_parser_decltype (parser);
18042      cp_lexer_set_token_position (parser->lexer, token);
18043      break;
18044
18045    case RID_TYPEOF:
18046      /* Consume the `typeof' token.  */
18047      cp_lexer_consume_token (parser->lexer);
18048      /* Parse the operand to `typeof'.  */
18049      type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
18050      /* If it is not already a TYPE, take its type.  */
18051      if (!TYPE_P (type))
18052	type = finish_typeof (type);
18053
18054      if (decl_specs)
18055	cp_parser_set_decl_spec_type (decl_specs, type,
18056				      token,
18057				      /*type_definition_p=*/false);
18058
18059      return type;
18060
18061    case RID_UNDERLYING_TYPE:
18062      type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
18063      if (decl_specs)
18064	cp_parser_set_decl_spec_type (decl_specs, type,
18065				      token,
18066				      /*type_definition_p=*/false);
18067
18068      return type;
18069
18070    case RID_BASES:
18071    case RID_DIRECT_BASES:
18072      type = cp_parser_trait_expr (parser, token->keyword);
18073      if (decl_specs)
18074       cp_parser_set_decl_spec_type (decl_specs, type,
18075                                     token,
18076                                     /*type_definition_p=*/false);
18077      return type;
18078    default:
18079      break;
18080    }
18081
18082  /* If token is an already-parsed decltype not followed by ::,
18083     it's a simple-type-specifier.  */
18084  if (token->type == CPP_DECLTYPE
18085      && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
18086    {
18087      type = saved_checks_value (token->u.tree_check_value);
18088      if (decl_specs)
18089	{
18090	  cp_parser_set_decl_spec_type (decl_specs, type,
18091					token,
18092					/*type_definition_p=*/false);
18093	  /* Remember that we are handling a decltype in order to
18094	     implement the resolution of DR 1510 when the argument
18095	     isn't instantiation dependent.  */
18096	  decl_specs->decltype_p = true;
18097	}
18098      cp_lexer_consume_token (parser->lexer);
18099      return type;
18100    }
18101
18102  /* If the type-specifier was for a built-in type, we're done.  */
18103  if (type)
18104    {
18105      /* Record the type.  */
18106      if (decl_specs
18107	  && (token->keyword != RID_SIGNED
18108	      && token->keyword != RID_UNSIGNED
18109	      && token->keyword != RID_SHORT
18110	      && token->keyword != RID_LONG))
18111	cp_parser_set_decl_spec_type (decl_specs,
18112				      type,
18113				      token,
18114				      /*type_definition_p=*/false);
18115      if (decl_specs)
18116	decl_specs->any_specifiers_p = true;
18117
18118      /* Consume the token.  */
18119      cp_lexer_consume_token (parser->lexer);
18120
18121      if (type == error_mark_node)
18122	return error_mark_node;
18123
18124      /* There is no valid C++ program where a non-template type is
18125	 followed by a "<".  That usually indicates that the user thought
18126	 that the type was a template.  */
18127      cp_parser_check_for_invalid_template_id (parser, type, none_type,
18128					       token->location);
18129
18130      return TYPE_NAME (type);
18131    }
18132
18133  /* The type-specifier must be a user-defined type.  */
18134  if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
18135    {
18136      bool qualified_p;
18137      bool global_p;
18138      const bool typename_p = (cxx_dialect >= cxx2a
18139			       && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
18140
18141      /* Don't gobble tokens or issue error messages if this is an
18142	 optional type-specifier.  */
18143      if (flags & CP_PARSER_FLAGS_OPTIONAL)
18144	cp_parser_parse_tentatively (parser);
18145
18146      /* Remember current tentative parsing state -- if we know we need
18147	 a type, we can give better diagnostics here.  */
18148      bool tent = cp_parser_parsing_tentatively (parser);
18149
18150      token = cp_lexer_peek_token (parser->lexer);
18151
18152      /* Look for the optional `::' operator.  */
18153      global_p
18154	= (cp_parser_global_scope_opt (parser,
18155				       /*current_scope_valid_p=*/false)
18156	   != NULL_TREE);
18157      /* Look for the nested-name specifier.  */
18158      qualified_p
18159	= (cp_parser_nested_name_specifier_opt (parser,
18160						/*typename_keyword_p=*/false,
18161						/*check_dependency_p=*/true,
18162						/*type_p=*/false,
18163						/*is_declaration=*/false)
18164	   != NULL_TREE);
18165      /* If we have seen a nested-name-specifier, and the next token
18166	 is `template', then we are using the template-id production.  */
18167      if (parser->scope
18168	  && cp_parser_optional_template_keyword (parser))
18169	{
18170	  /* Look for the template-id.  */
18171	  type = cp_parser_template_id (parser,
18172					/*template_keyword_p=*/true,
18173					/*check_dependency_p=*/true,
18174					none_type,
18175					/*is_declaration=*/false);
18176	  /* If the template-id did not name a type, we are out of
18177	     luck.  */
18178	  if (TREE_CODE (type) != TYPE_DECL)
18179	    {
18180	      /* ...unless we pretend we have seen 'typename'.  */
18181	      if (typename_p)
18182		type = cp_parser_make_typename_type (parser, type,
18183						     token->location);
18184	      else
18185		{
18186		  cp_parser_error (parser, "expected template-id for type");
18187		  type = error_mark_node;
18188		}
18189	    }
18190	}
18191      /* DR 1812: A < following a qualified-id in a typename-specifier
18192	 could safely be assumed to begin a template argument list, so
18193	 the template keyword should be optional.  */
18194      else if (parser->scope
18195	       && qualified_p
18196	       && typename_p
18197	       && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
18198	{
18199	  cp_parser_parse_tentatively (parser);
18200
18201	  type = cp_parser_template_id (parser,
18202					/*template_keyword_p=*/true,
18203					/*check_dependency_p=*/true,
18204					none_type,
18205					/*is_declaration=*/false);
18206	  /* This is handled below, so back off.  */
18207	  if (type && concept_check_p (type))
18208	    cp_parser_simulate_error (parser);
18209
18210	  if (!cp_parser_parse_definitely (parser))
18211	    type = NULL_TREE;
18212	  else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
18213	    type = make_typename_type (parser->scope, type, typename_type,
18214				       /*complain=*/tf_error);
18215	  else if (TREE_CODE (type) != TYPE_DECL)
18216	    type = NULL_TREE;
18217	}
18218
18219      /* Otherwise, look for a type-name.  */
18220      if (!type)
18221	{
18222	  if (cxx_dialect >= cxx17)
18223	    cp_parser_parse_tentatively (parser);
18224
18225	  type = cp_parser_type_name (parser, (qualified_p && typename_p));
18226
18227	  if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
18228	    type = NULL_TREE;
18229	}
18230
18231      if (!type && flag_concepts && decl_specs)
18232	{
18233	  /* Try for a type-constraint with template arguments.  We check
18234	     decl_specs here to avoid trying this for a functional cast.  */
18235
18236	  cp_parser_parse_tentatively (parser);
18237
18238	  type = cp_parser_template_id (parser,
18239					/*template_keyword_p=*/false,
18240					/*check_dependency_p=*/true,
18241					none_type,
18242					/*is_declaration=*/false);
18243	  if (type && concept_check_p (type))
18244	    {
18245	      location_t loc = EXPR_LOCATION (type);
18246	      type = cp_parser_placeholder_type_specifier (parser, loc,
18247							   type, tent);
18248	      if (tent && type == error_mark_node)
18249		/* Perhaps it's a concept-check expression.  */
18250		cp_parser_simulate_error (parser);
18251	    }
18252	  else
18253	    cp_parser_simulate_error (parser);
18254
18255	  if (!cp_parser_parse_definitely (parser))
18256	    type = NULL_TREE;
18257	}
18258
18259      if (!type && cxx_dialect >= cxx17)
18260	{
18261	  /* Try class template argument deduction or type-constraint without
18262	     template arguments.  */
18263	  tree name = cp_parser_identifier (parser);
18264	  if (name && TREE_CODE (name) == IDENTIFIER_NODE
18265	      && parser->scope != error_mark_node)
18266	    {
18267	      location_t loc
18268		= cp_lexer_previous_token (parser->lexer)->location;
18269	      tree tmpl = cp_parser_lookup_name (parser, name,
18270						 none_type,
18271						 /*is_template=*/false,
18272						 /*is_namespace=*/false,
18273						 /*check_dependency=*/true,
18274						 /*ambiguous_decls=*/NULL,
18275						 token->location);
18276	      if (tmpl && tmpl != error_mark_node
18277		  && ctad_template_p (tmpl))
18278		type = make_template_placeholder (tmpl);
18279	      else if (flag_concepts && tmpl && concept_definition_p (tmpl))
18280		type = cp_parser_placeholder_type_specifier (parser, loc,
18281							     tmpl, tent);
18282	      else
18283		{
18284		  type = error_mark_node;
18285		  if (!cp_parser_simulate_error (parser))
18286		    cp_parser_name_lookup_error (parser, name, tmpl,
18287						 NLE_TYPE, token->location);
18288		}
18289	    }
18290	  else
18291	    type = error_mark_node;
18292	}
18293
18294      /* If it didn't work out, we don't have a TYPE.  */
18295      if ((flags & CP_PARSER_FLAGS_OPTIONAL)
18296	  && !cp_parser_parse_definitely (parser))
18297	type = NULL_TREE;
18298
18299      /* Keep track of all name-lookups performed in class scopes.  */
18300      if (type
18301	  && !global_p
18302	  && !qualified_p
18303	  && TREE_CODE (type) == TYPE_DECL
18304	  && identifier_p (DECL_NAME (type)))
18305	maybe_note_name_used_in_class (DECL_NAME (type), type);
18306
18307      if (type && decl_specs)
18308	cp_parser_set_decl_spec_type (decl_specs, type,
18309				      token,
18310				      /*type_definition_p=*/false);
18311    }
18312
18313  /* If we didn't get a type-name, issue an error message.  */
18314  if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
18315    {
18316      cp_parser_error (parser, "expected type-name");
18317      return error_mark_node;
18318    }
18319
18320  if (type && type != error_mark_node)
18321    {
18322      /* See if TYPE is an Objective-C type, and if so, parse and
18323	 accept any protocol references following it.  Do this before
18324	 the cp_parser_check_for_invalid_template_id() call, because
18325	 Objective-C types can be followed by '<...>' which would
18326	 enclose protocol names rather than template arguments, and so
18327	 everything is fine.  */
18328      if (c_dialect_objc () && !parser->scope
18329	  && (objc_is_id (type) || objc_is_class_name (type)))
18330	{
18331	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
18332	  tree qual_type = objc_get_protocol_qualified_type (type, protos);
18333
18334	  /* Clobber the "unqualified" type previously entered into
18335	     DECL_SPECS with the new, improved protocol-qualified version.  */
18336	  if (decl_specs)
18337	    decl_specs->type = qual_type;
18338
18339	  return qual_type;
18340	}
18341
18342      /* There is no valid C++ program where a non-template type is
18343	 followed by a "<".  That usually indicates that the user
18344	 thought that the type was a template.  */
18345      cp_parser_check_for_invalid_template_id (parser, type,
18346					       none_type,
18347					       token->location);
18348    }
18349
18350  return type;
18351}
18352
18353/* Parse the remainder of a placholder-type-specifier.
18354
18355   placeholder-type-specifier:
18356     type-constraint_opt auto
18357     type-constraint_opt decltype(auto)
18358
18359  The raw form of the constraint is parsed in cp_parser_simple_type_specifier
18360  and passed as TMPL. This function converts TMPL to an actual type-constraint,
18361  parses the placeholder type, and performs some contextual syntactic analysis.
18362
18363  LOC provides the location of the template name.
18364
18365  TENTATIVE is true if the type-specifier parsing is tentative; in that case,
18366  don't give an error if TMPL isn't a valid type-constraint, as the template-id
18367  might actually be a concept-check,
18368
18369  Note that the Concepts TS allows the auto or decltype(auto) to be
18370  omitted in a constrained-type-specifier.  */
18371
18372tree
18373cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
18374				      tree tmpl, bool tentative)
18375{
18376  if (tmpl == error_mark_node)
18377    return error_mark_node;
18378
18379  tree orig_tmpl = tmpl;
18380
18381  /* Get the arguments as written for subsequent analysis.  */
18382  tree args = NULL_TREE;
18383  if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
18384    {
18385      args = TREE_OPERAND (tmpl, 1);
18386      tmpl = TREE_OPERAND (tmpl, 0);
18387    }
18388  if (args == NULL_TREE)
18389    /* A concept-name with no arguments can't be an expression.  */
18390    tentative = false;
18391
18392  tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
18393
18394  /* Get the concept and prototype parameter for the constraint.  */
18395  tree_pair info = finish_type_constraints (tmpl, args, complain);
18396  tree con = info.first;
18397  tree proto = info.second;
18398  if (con == error_mark_node)
18399    return error_mark_node;
18400
18401  /* As per the standard, require auto or decltype(auto), except in some
18402     cases (template parameter lists, -fconcepts-ts enabled).  */
18403  cp_token *placeholder = NULL, *close_paren = NULL;
18404  if (cxx_dialect >= cxx2a)
18405    {
18406      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
18407	placeholder = cp_lexer_consume_token (parser->lexer);
18408      else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
18409	{
18410	  placeholder = cp_lexer_consume_token (parser->lexer);
18411	  matching_parens parens;
18412	  parens.require_open (parser);
18413	  cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
18414	  close_paren = parens.require_close (parser);
18415	}
18416    }
18417
18418  /* A type constraint constrains a contextually determined type or type
18419     parameter pack. However, the Concepts TS does allow concepts
18420     to introduce non-type and template template parameters.  */
18421  if (TREE_CODE (proto) != TYPE_DECL)
18422    {
18423      if (!flag_concepts_ts
18424	  || !processing_template_parmlist)
18425	{
18426	  error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
18427	  inform (DECL_SOURCE_LOCATION (con), "concept defined here");
18428	  return error_mark_node;
18429	}
18430    }
18431
18432  /* In a template parameter list, a type-parameter can be introduced
18433     by type-constraints alone.  */
18434  if (processing_template_parmlist && !placeholder)
18435    return build_constrained_parameter (con, proto, args);
18436
18437  /* Diagnose issues placeholder issues.  */
18438  if (!flag_concepts_ts
18439      && !parser->in_result_type_constraint_p
18440      && !placeholder)
18441    {
18442      if (tentative)
18443	/* Perhaps it's a concept-check expression (c++/91073).  */
18444	return error_mark_node;
18445
18446      tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
18447      tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
18448      error_at (input_location,
18449		"expected %<auto%> or %<decltype(auto)%> after %qE", expr);
18450      /* Fall through. This is an error of omission.  */
18451    }
18452  else if (parser->in_result_type_constraint_p && placeholder)
18453    {
18454      /* A trailing return type only allows type-constraints.  */
18455      error_at (input_location,
18456		"unexpected placeholder in constrained result type");
18457    }
18458
18459  /* In a parameter-declaration-clause, a placeholder-type-specifier
18460     results in an invented template parameter.  */
18461  if (parser->auto_is_implicit_function_template_parm_p)
18462    {
18463      if (close_paren)
18464	{
18465	  location_t loc = make_location (placeholder->location,
18466					  placeholder->location,
18467					  close_paren->location);
18468	  error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
18469	  return error_mark_node;
18470	}
18471      tree parm = build_constrained_parameter (con, proto, args);
18472      return synthesize_implicit_template_parm (parser, parm);
18473    }
18474
18475  /* Determine if the type should be deduced using template argument
18476     deduction or decltype deduction. Note that the latter is always
18477     used for type-constraints in trailing return types.  */
18478  bool decltype_p = placeholder
18479    ? placeholder->keyword == RID_DECLTYPE
18480    : parser->in_result_type_constraint_p;
18481
18482  /* Otherwise, this is the type of a variable or return type.  */
18483  if (decltype_p)
18484    return make_constrained_decltype_auto (con, args);
18485  else
18486    return make_constrained_auto (con, args);
18487}
18488
18489/* Parse a type-name.
18490
18491   type-name:
18492     class-name
18493     enum-name
18494     typedef-name
18495     simple-template-id [in c++0x]
18496
18497   enum-name:
18498     identifier
18499
18500   typedef-name:
18501     identifier
18502
18503  Concepts:
18504
18505   type-name:
18506     concept-name
18507     partial-concept-id
18508
18509   concept-name:
18510     identifier
18511
18512   Returns a TYPE_DECL for the type.  */
18513
18514static tree
18515cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
18516{
18517  tree type_decl;
18518
18519  /* We can't know yet whether it is a class-name or not.  */
18520  cp_parser_parse_tentatively (parser);
18521  /* Try a class-name.  */
18522  type_decl = cp_parser_class_name (parser,
18523				    typename_keyword_p,
18524				    /*template_keyword_p=*/false,
18525				    none_type,
18526				    /*check_dependency_p=*/true,
18527				    /*class_head_p=*/false,
18528				    /*is_declaration=*/false);
18529  /* If it's not a class-name, keep looking.  */
18530  if (!cp_parser_parse_definitely (parser))
18531    {
18532      if (cxx_dialect < cxx11)
18533	/* It must be a typedef-name or an enum-name.  */
18534	return cp_parser_nonclass_name (parser);
18535
18536      cp_parser_parse_tentatively (parser);
18537      /* It is either a simple-template-id representing an
18538	 instantiation of an alias template...  */
18539      type_decl = cp_parser_template_id (parser,
18540					 /*template_keyword_p=*/false,
18541					 /*check_dependency_p=*/true,
18542					 none_type,
18543					 /*is_declaration=*/false);
18544      /* Note that this must be an instantiation of an alias template
18545	 because [temp.names]/6 says:
18546
18547	     A template-id that names an alias template specialization
18548	     is a type-name.
18549
18550	 Whereas [temp.names]/7 says:
18551
18552	     A simple-template-id that names a class template
18553	     specialization is a class-name.
18554
18555         With concepts, this could also be a partial-concept-id that
18556         declares a non-type template parameter. */
18557      if (type_decl != NULL_TREE
18558	  && TREE_CODE (type_decl) == TYPE_DECL
18559	  && TYPE_DECL_ALIAS_P (type_decl))
18560	gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18561      else
18562	cp_parser_simulate_error (parser);
18563
18564      if (!cp_parser_parse_definitely (parser))
18565	/* ... Or a typedef-name or an enum-name.  */
18566	return cp_parser_nonclass_name (parser);
18567    }
18568
18569  return type_decl;
18570}
18571
18572/* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18573   or a concept-name.
18574
18575   enum-name:
18576     identifier
18577
18578   typedef-name:
18579     identifier
18580
18581   concept-name:
18582     identifier
18583
18584   Returns a TYPE_DECL for the type.  */
18585
18586static tree
18587cp_parser_nonclass_name (cp_parser* parser)
18588{
18589  tree type_decl;
18590  tree identifier;
18591
18592  cp_token *token = cp_lexer_peek_token (parser->lexer);
18593  identifier = cp_parser_identifier (parser);
18594  if (identifier == error_mark_node)
18595    return error_mark_node;
18596
18597  /* Look up the type-name.  */
18598  type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18599
18600  type_decl = strip_using_decl (type_decl);
18601
18602  if (TREE_CODE (type_decl) != TYPE_DECL
18603      && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18604    {
18605      /* See if this is an Objective-C type.  */
18606      tree protos = cp_parser_objc_protocol_refs_opt (parser);
18607      tree type = objc_get_protocol_qualified_type (identifier, protos);
18608      if (type)
18609	type_decl = TYPE_NAME (type);
18610    }
18611
18612  /* Issue an error if we did not find a type-name.  */
18613  if (TREE_CODE (type_decl) != TYPE_DECL
18614      /* In Objective-C, we have the complication that class names are
18615	 normally type names and start declarations (eg, the
18616	 "NSObject" in "NSObject *object;"), but can be used in an
18617	 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18618	 is an expression.  So, a classname followed by a dot is not a
18619	 valid type-name.  */
18620      || (objc_is_class_name (TREE_TYPE (type_decl))
18621	  && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18622    {
18623      if (!cp_parser_simulate_error (parser))
18624	cp_parser_name_lookup_error (parser, identifier, type_decl,
18625				     NLE_TYPE, token->location);
18626      return error_mark_node;
18627    }
18628  /* Remember that the name was used in the definition of the
18629     current class so that we can check later to see if the
18630     meaning would have been different after the class was
18631     entirely defined.  */
18632  else if (type_decl != error_mark_node
18633	   && !parser->scope)
18634    maybe_note_name_used_in_class (identifier, type_decl);
18635
18636  return type_decl;
18637}
18638
18639/* Parse an elaborated-type-specifier.  Note that the grammar given
18640   here incorporates the resolution to DR68.
18641
18642   elaborated-type-specifier:
18643     class-key :: [opt] nested-name-specifier [opt] identifier
18644     class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18645     enum-key :: [opt] nested-name-specifier [opt] identifier
18646     typename :: [opt] nested-name-specifier identifier
18647     typename :: [opt] nested-name-specifier template [opt]
18648       template-id
18649
18650   GNU extension:
18651
18652   elaborated-type-specifier:
18653     class-key attributes :: [opt] nested-name-specifier [opt] identifier
18654     class-key attributes :: [opt] nested-name-specifier [opt]
18655	       template [opt] template-id
18656     enum attributes :: [opt] nested-name-specifier [opt] identifier
18657
18658   If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18659   declared `friend'.  If IS_DECLARATION is TRUE, then this
18660   elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18661   something is being declared.
18662
18663   Returns the TYPE specified.  */
18664
18665static tree
18666cp_parser_elaborated_type_specifier (cp_parser* parser,
18667				     bool is_friend,
18668				     bool is_declaration)
18669{
18670  enum tag_types tag_type;
18671  tree identifier;
18672  tree type = NULL_TREE;
18673  tree attributes = NULL_TREE;
18674  tree globalscope;
18675  cp_token *token = NULL;
18676
18677  /* For class and enum types the location of the class-key or enum-key.  */
18678  location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
18679  /* For a scoped enum, the 'class' or 'struct' keyword id.  */
18680  rid scoped_key = RID_MAX;
18681
18682  /* See if we're looking at the `enum' keyword.  */
18683  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18684    {
18685      /* Consume the `enum' token.  */
18686      cp_lexer_consume_token (parser->lexer);
18687      /* Remember that it's an enumeration type.  */
18688      tag_type = enum_type;
18689      /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18690	 enums) is used here.  */
18691      cp_token *token = cp_lexer_peek_token (parser->lexer);
18692      if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
18693	  || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
18694	{
18695	  location_t loc = token->location;
18696	  gcc_rich_location richloc (loc);
18697	  richloc.add_range (input_location);
18698	  richloc.add_fixit_remove ();
18699	  pedwarn (&richloc, 0, "elaborated-type-specifier for "
18700		   "a scoped enum must not use the %qD keyword",
18701		   token->u.value);
18702	  /* Consume the `struct' or `class' and parse it anyway.  */
18703	  cp_lexer_consume_token (parser->lexer);
18704	  /* Create a combined location for the whole scoped-enum-key.  */
18705	  key_loc = make_location (key_loc, key_loc, loc);
18706	}
18707      else
18708	scoped_key = RID_MAX;
18709
18710      /* Parse the attributes.  */
18711      attributes = cp_parser_attributes_opt (parser);
18712    }
18713  /* Or, it might be `typename'.  */
18714  else if (cp_lexer_next_token_is_keyword (parser->lexer,
18715					   RID_TYPENAME))
18716    {
18717      /* Consume the `typename' token.  */
18718      cp_lexer_consume_token (parser->lexer);
18719      /* Remember that it's a `typename' type.  */
18720      tag_type = typename_type;
18721    }
18722  /* Otherwise it must be a class-key.  */
18723  else
18724    {
18725      key_loc = cp_lexer_peek_token (parser->lexer)->location;
18726      tag_type = cp_parser_class_key (parser);
18727      if (tag_type == none_type)
18728	return error_mark_node;
18729      /* Parse the attributes.  */
18730      attributes = cp_parser_attributes_opt (parser);
18731    }
18732
18733  /* Look for the `::' operator.  */
18734  globalscope =  cp_parser_global_scope_opt (parser,
18735					     /*current_scope_valid_p=*/false);
18736  /* Look for the nested-name-specifier.  */
18737  tree nested_name_specifier;
18738  if (tag_type == typename_type && !globalscope)
18739    {
18740      nested_name_specifier
18741	= cp_parser_nested_name_specifier (parser,
18742					   /*typename_keyword_p=*/true,
18743					   /*check_dependency_p=*/true,
18744					   /*type_p=*/true,
18745					   is_declaration);
18746      if (!nested_name_specifier)
18747	return error_mark_node;
18748    }
18749  else
18750    /* Even though `typename' is not present, the proposed resolution
18751       to Core Issue 180 says that in `class A<T>::B', `B' should be
18752       considered a type-name, even if `A<T>' is dependent.  */
18753    nested_name_specifier
18754      = cp_parser_nested_name_specifier_opt (parser,
18755					     /*typename_keyword_p=*/true,
18756					     /*check_dependency_p=*/true,
18757					     /*type_p=*/true,
18758					     is_declaration);
18759 /* For everything but enumeration types, consider a template-id.
18760    For an enumeration type, consider only a plain identifier.  */
18761  if (tag_type != enum_type)
18762    {
18763      bool template_p = false;
18764      tree decl;
18765
18766      /* Allow the `template' keyword.  */
18767      template_p = cp_parser_optional_template_keyword (parser);
18768      /* If we didn't see `template', we don't know if there's a
18769	 template-id or not.  */
18770      if (!template_p)
18771	cp_parser_parse_tentatively (parser);
18772      /* The `template' keyword must follow a nested-name-specifier.  */
18773      else if (!nested_name_specifier && !globalscope)
18774	{
18775	  cp_parser_error (parser, "%<template%> must follow a nested-"
18776			   "name-specifier");
18777	  return error_mark_node;
18778	}
18779
18780      /* Parse the template-id.  */
18781      token = cp_lexer_peek_token (parser->lexer);
18782      decl = cp_parser_template_id (parser, template_p,
18783				    /*check_dependency_p=*/true,
18784				    tag_type,
18785				    is_declaration);
18786      /* If we didn't find a template-id, look for an ordinary
18787	 identifier.  */
18788      if (!template_p && !cp_parser_parse_definitely (parser))
18789	;
18790      /* We can get here when cp_parser_template_id, called by
18791	 cp_parser_class_name with tag_type == none_type, succeeds
18792	 and caches a BASELINK.  Then, when called again here,
18793	 instead of failing and returning an error_mark_node
18794	 returns it (see template/typename17.C in C++11).
18795	 ??? Could we diagnose this earlier?  */
18796      else if (tag_type == typename_type && BASELINK_P (decl))
18797	{
18798	  cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18799	  type = error_mark_node;
18800	}
18801      /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18802	 in effect, then we must assume that, upon instantiation, the
18803	 template will correspond to a class.  */
18804      else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18805	       && tag_type == typename_type)
18806	type = make_typename_type (parser->scope, decl,
18807				   typename_type,
18808				   /*complain=*/tf_error);
18809      /* If the `typename' keyword is in effect and DECL is not a type
18810	 decl, then type is non existent.   */
18811      else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18812        ;
18813      else if (TREE_CODE (decl) == TYPE_DECL)
18814	{
18815	  type = check_elaborated_type_specifier (tag_type, decl,
18816						  /*allow_template_p=*/true);
18817
18818	  /* If the next token is a semicolon, this must be a specialization,
18819	     instantiation, or friend declaration.  Check the scope while we
18820	     still know whether or not we had a nested-name-specifier.  */
18821	  if (type != error_mark_node
18822	      && !nested_name_specifier && !is_friend
18823	      && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18824	    check_unqualified_spec_or_inst (type, token->location);
18825	}
18826      else if (decl == error_mark_node)
18827	type = error_mark_node;
18828    }
18829
18830  if (!type)
18831    {
18832      token = cp_lexer_peek_token (parser->lexer);
18833      identifier = cp_parser_identifier (parser);
18834
18835      if (identifier == error_mark_node)
18836	{
18837	  parser->scope = NULL_TREE;
18838	  return error_mark_node;
18839	}
18840
18841      /* For a `typename', we needn't call xref_tag.  */
18842      if (tag_type == typename_type
18843	  && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18844	return cp_parser_make_typename_type (parser, identifier,
18845					     token->location);
18846
18847      /* Template parameter lists apply only if we are not within a
18848	 function parameter list.  */
18849      bool template_parm_lists_apply
18850	  = parser->num_template_parameter_lists;
18851      if (template_parm_lists_apply)
18852	for (cp_binding_level *s = current_binding_level;
18853	     s && s->kind != sk_template_parms;
18854	     s = s->level_chain)
18855	  if (s->kind == sk_function_parms)
18856	    template_parm_lists_apply = false;
18857
18858      /* Look up a qualified name in the usual way.  */
18859      if (parser->scope)
18860	{
18861	  tree decl;
18862	  tree ambiguous_decls;
18863
18864	  decl = cp_parser_lookup_name (parser, identifier,
18865					tag_type,
18866					/*is_template=*/false,
18867					/*is_namespace=*/false,
18868					/*check_dependency=*/true,
18869					&ambiguous_decls,
18870					token->location);
18871
18872	  /* If the lookup was ambiguous, an error will already have been
18873	     issued.  */
18874	  if (ambiguous_decls)
18875	    return error_mark_node;
18876
18877	  /* If we are parsing friend declaration, DECL may be a
18878	     TEMPLATE_DECL tree node here.  However, we need to check
18879	     whether this TEMPLATE_DECL results in valid code.  Consider
18880	     the following example:
18881
18882	       namespace N {
18883		 template <class T> class C {};
18884	       }
18885	       class X {
18886		 template <class T> friend class N::C; // #1, valid code
18887	       };
18888	       template <class T> class Y {
18889		 friend class N::C;		       // #2, invalid code
18890	       };
18891
18892	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18893	     name lookup of `N::C'.  We see that friend declaration must
18894	     be template for the code to be valid.  Note that
18895	     processing_template_decl does not work here since it is
18896	     always 1 for the above two cases.  */
18897
18898	  decl = (cp_parser_maybe_treat_template_as_class
18899		  (decl, /*tag_name_p=*/is_friend
18900			 && template_parm_lists_apply));
18901
18902	  if (TREE_CODE (decl) != TYPE_DECL)
18903	    {
18904	      cp_parser_diagnose_invalid_type_name (parser,
18905						    identifier,
18906						    token->location);
18907	      return error_mark_node;
18908	    }
18909
18910	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18911            {
18912              bool allow_template = (template_parm_lists_apply
18913		                     || DECL_SELF_REFERENCE_P (decl));
18914              type = check_elaborated_type_specifier (tag_type, decl,
18915                                                      allow_template);
18916
18917              if (type == error_mark_node)
18918                return error_mark_node;
18919            }
18920
18921          /* Forward declarations of nested types, such as
18922
18923               class C1::C2;
18924               class C1::C2::C3;
18925
18926             are invalid unless all components preceding the final '::'
18927             are complete.  If all enclosing types are complete, these
18928             declarations become merely pointless.
18929
18930             Invalid forward declarations of nested types are errors
18931             caught elsewhere in parsing.  Those that are pointless arrive
18932             here.  */
18933
18934          if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18935              && !is_friend && !processing_explicit_instantiation)
18936            warning (0, "declaration %qD does not declare anything", decl);
18937
18938	  type = TREE_TYPE (decl);
18939	}
18940      else
18941	{
18942	  /* An elaborated-type-specifier sometimes introduces a new type and
18943	     sometimes names an existing type.  Normally, the rule is that it
18944	     introduces a new type only if there is not an existing type of
18945	     the same name already in scope.  For example, given:
18946
18947	       struct S {};
18948	       void f() { struct S s; }
18949
18950	     the `struct S' in the body of `f' is the same `struct S' as in
18951	     the global scope; the existing definition is used.  However, if
18952	     there were no global declaration, this would introduce a new
18953	     local class named `S'.
18954
18955	     An exception to this rule applies to the following code:
18956
18957	       namespace N { struct S; }
18958
18959	     Here, the elaborated-type-specifier names a new type
18960	     unconditionally; even if there is already an `S' in the
18961	     containing scope this declaration names a new type.
18962	     This exception only applies if the elaborated-type-specifier
18963	     forms the complete declaration:
18964
18965	       [class.name]
18966
18967	       A declaration consisting solely of `class-key identifier ;' is
18968	       either a redeclaration of the name in the current scope or a
18969	       forward declaration of the identifier as a class name.  It
18970	       introduces the name into the current scope.
18971
18972	     We are in this situation precisely when the next token is a `;'.
18973
18974	     An exception to the exception is that a `friend' declaration does
18975	     *not* name a new type; i.e., given:
18976
18977	       struct S { friend struct T; };
18978
18979	     `T' is not a new type in the scope of `S'.
18980
18981	     Also, `new struct S' or `sizeof (struct S)' never results in the
18982	     definition of a new type; a new type can only be declared in a
18983	     declaration context.  */
18984
18985	  tag_scope ts;
18986	  bool template_p;
18987
18988	  if (is_friend)
18989	    /* Friends have special name lookup rules.  */
18990	    ts = ts_within_enclosing_non_class;
18991	  else if (is_declaration
18992		   && cp_lexer_next_token_is (parser->lexer,
18993					      CPP_SEMICOLON))
18994	    /* This is a `class-key identifier ;' */
18995	    ts = ts_current;
18996	  else
18997	    ts = ts_global;
18998
18999	  template_p =
19000	    (template_parm_lists_apply
19001	     && (cp_parser_next_token_starts_class_definition_p (parser)
19002		 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
19003	  /* An unqualified name was used to reference this type, so
19004	     there were no qualifying templates.  */
19005	  if (template_parm_lists_apply
19006	      && !cp_parser_check_template_parameters (parser,
19007						       /*num_templates=*/0,
19008						       /*template_id*/false,
19009						       token->location,
19010						       /*declarator=*/NULL))
19011	    return error_mark_node;
19012	  type = xref_tag (tag_type, identifier, ts, template_p);
19013	}
19014    }
19015
19016  if (type == error_mark_node)
19017    return error_mark_node;
19018
19019  /* Allow attributes on forward declarations of classes.  */
19020  if (attributes)
19021    {
19022      if (TREE_CODE (type) == TYPENAME_TYPE)
19023	warning (OPT_Wattributes,
19024		 "attributes ignored on uninstantiated type");
19025      else if (tag_type != enum_type
19026	       && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
19027	       && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
19028	       && ! processing_explicit_instantiation)
19029	warning (OPT_Wattributes,
19030		 "attributes ignored on template instantiation");
19031      else if (is_declaration && cp_parser_declares_only_class_p (parser))
19032	cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
19033      else
19034	warning (OPT_Wattributes,
19035		 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
19036    }
19037
19038  if (tag_type == enum_type)
19039    cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
19040  else
19041    {
19042      /* Diagnose class/struct/union mismatches.  IS_DECLARATION is false
19043	 for alias definition.  */
19044      bool decl_class = (is_declaration
19045			 && cp_parser_declares_only_class_p (parser));
19046      cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
19047				 decl_class);
19048
19049      /* Indicate whether this class was declared as a `class' or as a
19050	 `struct'.  */
19051      if (CLASS_TYPE_P (type) && !currently_open_class (type))
19052	CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
19053    }
19054
19055  /* A "<" cannot follow an elaborated type specifier.  If that
19056     happens, the user was probably trying to form a template-id.  */
19057  cp_parser_check_for_invalid_template_id (parser, type, tag_type,
19058					   token->location);
19059
19060  return type;
19061}
19062
19063/* Parse an enum-specifier.
19064
19065   enum-specifier:
19066     enum-head { enumerator-list [opt] }
19067     enum-head { enumerator-list , } [C++0x]
19068
19069   enum-head:
19070     enum-key identifier [opt] enum-base [opt]
19071     enum-key nested-name-specifier identifier enum-base [opt]
19072
19073   enum-key:
19074     enum
19075     enum class   [C++0x]
19076     enum struct  [C++0x]
19077
19078   enum-base:   [C++0x]
19079     : type-specifier-seq
19080
19081   opaque-enum-specifier:
19082     enum-key identifier enum-base [opt] ;
19083
19084   GNU Extensions:
19085     enum-key attributes[opt] identifier [opt] enum-base [opt]
19086       { enumerator-list [opt] }attributes[opt]
19087     enum-key attributes[opt] identifier [opt] enum-base [opt]
19088       { enumerator-list, }attributes[opt] [C++0x]
19089
19090   Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
19091   if the token stream isn't an enum-specifier after all.  */
19092
19093static tree
19094cp_parser_enum_specifier (cp_parser* parser)
19095{
19096  tree identifier;
19097  tree type = NULL_TREE;
19098  tree prev_scope;
19099  tree nested_name_specifier = NULL_TREE;
19100  tree attributes;
19101  bool scoped_enum_p = false;
19102  bool has_underlying_type = false;
19103  bool nested_being_defined = false;
19104  bool new_value_list = false;
19105  bool is_new_type = false;
19106  bool is_unnamed = false;
19107  tree underlying_type = NULL_TREE;
19108  cp_token *type_start_token = NULL;
19109  temp_override<bool> cleanup (parser->colon_corrects_to_scope_p, false);
19110
19111  /* Parse tentatively so that we can back up if we don't find a
19112     enum-specifier.  */
19113  cp_parser_parse_tentatively (parser);
19114
19115  /* Caller guarantees that the current token is 'enum', an identifier
19116     possibly follows, and the token after that is an opening brace.
19117     If we don't have an identifier, fabricate an anonymous name for
19118     the enumeration being defined.  */
19119  cp_lexer_consume_token (parser->lexer);
19120
19121  /* Parse the "class" or "struct", which indicates a scoped
19122     enumeration type in C++0x.  */
19123  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
19124      || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
19125    {
19126      if (cxx_dialect < cxx11)
19127        maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19128
19129      /* Consume the `struct' or `class' token.  */
19130      cp_lexer_consume_token (parser->lexer);
19131
19132      scoped_enum_p = true;
19133    }
19134
19135  attributes = cp_parser_attributes_opt (parser);
19136
19137  /* Clear the qualification.  */
19138  parser->scope = NULL_TREE;
19139  parser->qualifying_scope = NULL_TREE;
19140  parser->object_scope = NULL_TREE;
19141
19142  /* Figure out in what scope the declaration is being placed.  */
19143  prev_scope = current_scope ();
19144
19145  type_start_token = cp_lexer_peek_token (parser->lexer);
19146
19147  push_deferring_access_checks (dk_no_check);
19148  nested_name_specifier
19149    = cp_parser_nested_name_specifier_opt (parser,
19150					   /*typename_keyword_p=*/true,
19151					   /*check_dependency_p=*/false,
19152					   /*type_p=*/false,
19153					   /*is_declaration=*/false);
19154
19155  if (nested_name_specifier)
19156    {
19157      tree name;
19158
19159      identifier = cp_parser_identifier (parser);
19160      name = cp_parser_lookup_name (parser, identifier,
19161				    enum_type,
19162				    /*is_template=*/false,
19163				    /*is_namespace=*/false,
19164				    /*check_dependency=*/true,
19165				    /*ambiguous_decls=*/NULL,
19166				    input_location);
19167      if (name && name != error_mark_node)
19168	{
19169	  type = TREE_TYPE (name);
19170	  if (TREE_CODE (type) == TYPENAME_TYPE)
19171	    {
19172	      /* Are template enums allowed in ISO? */
19173	      if (template_parm_scope_p ())
19174		pedwarn (type_start_token->location, OPT_Wpedantic,
19175			 "%qD is an enumeration template", name);
19176	      /* ignore a typename reference, for it will be solved by name
19177	         in start_enum.  */
19178	      type = NULL_TREE;
19179	    }
19180	}
19181      else if (nested_name_specifier == error_mark_node)
19182	/* We already issued an error.  */;
19183      else
19184	{
19185	  error_at (type_start_token->location,
19186		    "%qD does not name an enumeration in %qT",
19187		    identifier, nested_name_specifier);
19188	  nested_name_specifier = error_mark_node;
19189	}
19190    }
19191  else
19192    {
19193      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19194	identifier = cp_parser_identifier (parser);
19195      else
19196	{
19197	  identifier = make_anon_name ();
19198	  is_unnamed = true;
19199	  if (scoped_enum_p)
19200	    error_at (type_start_token->location,
19201		      "unnamed scoped enum is not allowed");
19202	}
19203    }
19204  pop_deferring_access_checks ();
19205
19206  /* Check for the `:' that denotes a specified underlying type in C++0x.
19207     Note that a ':' could also indicate a bitfield width, however.  */
19208  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19209    {
19210      cp_decl_specifier_seq type_specifiers;
19211
19212      /* Consume the `:'.  */
19213      cp_lexer_consume_token (parser->lexer);
19214
19215      /* Parse the type-specifier-seq.  */
19216      cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
19217				    /*is_declaration=*/false,
19218				    /*is_trailing_return=*/false,
19219                                    &type_specifiers);
19220
19221      /* At this point this is surely not elaborated type specifier.  */
19222      if (!cp_parser_parse_definitely (parser))
19223	return NULL_TREE;
19224
19225      if (cxx_dialect < cxx11)
19226        maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19227
19228      has_underlying_type = true;
19229
19230      /* If that didn't work, stop.  */
19231      if (type_specifiers.type != error_mark_node)
19232        {
19233          underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
19234                                            /*initialized=*/0, NULL);
19235          if (underlying_type == error_mark_node
19236	      || check_for_bare_parameter_packs (underlying_type))
19237            underlying_type = NULL_TREE;
19238        }
19239    }
19240
19241  /* Look for the `{' but don't consume it yet.  */
19242  if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19243    {
19244      if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
19245	{
19246	  if (has_underlying_type)
19247	    cp_parser_commit_to_tentative_parse (parser);
19248	  cp_parser_error (parser, "expected %<{%>");
19249	  if (has_underlying_type)
19250	    return error_mark_node;
19251	}
19252      /* An opaque-enum-specifier must have a ';' here.  */
19253      if ((scoped_enum_p || underlying_type)
19254	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19255	{
19256	  if (has_underlying_type)
19257	    cp_parser_commit_to_tentative_parse (parser);
19258	  cp_parser_error (parser, "expected %<;%> or %<{%>");
19259	  if (has_underlying_type)
19260	    return error_mark_node;
19261	}
19262    }
19263
19264  if (!has_underlying_type && !cp_parser_parse_definitely (parser))
19265    return NULL_TREE;
19266
19267  if (nested_name_specifier)
19268    {
19269      if (CLASS_TYPE_P (nested_name_specifier))
19270	{
19271	  nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
19272	  TYPE_BEING_DEFINED (nested_name_specifier) = 1;
19273	  push_scope (nested_name_specifier);
19274	}
19275      else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19276	push_nested_namespace (nested_name_specifier);
19277    }
19278
19279  /* Issue an error message if type-definitions are forbidden here.  */
19280  if (!cp_parser_check_type_definition (parser))
19281    type = error_mark_node;
19282  else
19283    /* Create the new type.  We do this before consuming the opening
19284       brace so the enum will be recorded as being on the line of its
19285       tag (or the 'enum' keyword, if there is no tag).  */
19286    type = start_enum (identifier, type, underlying_type,
19287		       attributes, scoped_enum_p, &is_new_type);
19288
19289  /* If the next token is not '{' it is an opaque-enum-specifier or an
19290     elaborated-type-specifier.  */
19291  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19292    {
19293      timevar_push (TV_PARSE_ENUM);
19294      if (nested_name_specifier
19295	  && nested_name_specifier != error_mark_node)
19296	{
19297	  /* The following catches invalid code such as:
19298	     enum class S<int>::E { A, B, C }; */
19299	  if (!processing_specialization
19300	      && CLASS_TYPE_P (nested_name_specifier)
19301	      && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
19302	    error_at (type_start_token->location, "cannot add an enumerator "
19303		      "list to a template instantiation");
19304
19305	  if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
19306	    {
19307	      error_at (type_start_token->location,
19308			"%<%T::%E%> has not been declared",
19309			TYPE_CONTEXT (nested_name_specifier),
19310			nested_name_specifier);
19311	      type = error_mark_node;
19312	    }
19313	  else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
19314		   && !CLASS_TYPE_P (nested_name_specifier))
19315	    {
19316	      error_at (type_start_token->location, "nested name specifier "
19317			"%qT for enum declaration does not name a class "
19318			"or namespace", nested_name_specifier);
19319	      type = error_mark_node;
19320	    }
19321	  /* If that scope does not contain the scope in which the
19322	     class was originally declared, the program is invalid.  */
19323	  else if (prev_scope && !is_ancestor (prev_scope,
19324					       nested_name_specifier))
19325	    {
19326	      if (at_namespace_scope_p ())
19327		error_at (type_start_token->location,
19328			  "declaration of %qD in namespace %qD which does not "
19329			  "enclose %qD",
19330			  type, prev_scope, nested_name_specifier);
19331	      else
19332		error_at (type_start_token->location,
19333			  "declaration of %qD in %qD which does not "
19334			  "enclose %qD",
19335			  type, prev_scope, nested_name_specifier);
19336	      type = error_mark_node;
19337	    }
19338	  /* If that scope is the scope where the declaration is being placed
19339	     the program is invalid.  */
19340	  else if (CLASS_TYPE_P (nested_name_specifier)
19341		   && CLASS_TYPE_P (prev_scope)
19342		   && same_type_p (nested_name_specifier, prev_scope))
19343	    {
19344	      permerror (type_start_token->location,
19345			 "extra qualification not allowed");
19346	      nested_name_specifier = NULL_TREE;
19347	    }
19348	}
19349
19350      if (scoped_enum_p)
19351	begin_scope (sk_scoped_enum, type);
19352
19353      /* Consume the opening brace.  */
19354      matching_braces braces;
19355      braces.consume_open (parser);
19356
19357      if (type == error_mark_node)
19358	; /* Nothing to add */
19359      else if (OPAQUE_ENUM_P (type)
19360	       || (cxx_dialect > cxx98 && processing_specialization))
19361	{
19362	  new_value_list = true;
19363	  SET_OPAQUE_ENUM_P (type, false);
19364	  DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19365	}
19366      else
19367	{
19368	  error_at (type_start_token->location,
19369		    "multiple definition of %q#T", type);
19370	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
19371		  "previous definition here");
19372	  type = error_mark_node;
19373	}
19374
19375      if (type == error_mark_node)
19376	cp_parser_skip_to_end_of_block_or_statement (parser);
19377      /* If the next token is not '}', then there are some enumerators.  */
19378      else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19379	{
19380	  if (is_unnamed && !scoped_enum_p)
19381	    pedwarn (type_start_token->location, OPT_Wpedantic,
19382		     "ISO C++ forbids empty unnamed enum");
19383	}
19384      else
19385	{
19386	  /* We've seen a '{' so we know we're in an enum-specifier.
19387	     Commit to any tentative parse to get syntax errors.  */
19388	  cp_parser_commit_to_tentative_parse (parser);
19389	  cp_parser_enumerator_list (parser, type);
19390	}
19391
19392      /* Consume the final '}'.  */
19393      braces.require_close (parser);
19394
19395      if (scoped_enum_p)
19396	finish_scope ();
19397      timevar_pop (TV_PARSE_ENUM);
19398    }
19399  else
19400    {
19401      /* If a ';' follows, then it is an opaque-enum-specifier
19402	and additional restrictions apply.  */
19403      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19404	{
19405	  if (is_unnamed)
19406	    error_at (type_start_token->location,
19407		      "opaque-enum-specifier without name");
19408	  else if (nested_name_specifier)
19409	    error_at (type_start_token->location,
19410		      "opaque-enum-specifier must use a simple identifier");
19411	}
19412    }
19413
19414  /* Look for trailing attributes to apply to this enumeration, and
19415     apply them if appropriate.  */
19416  if (cp_parser_allow_gnu_extensions_p (parser))
19417    {
19418      tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
19419      cplus_decl_attributes (&type,
19420			     trailing_attr,
19421			     (int) ATTR_FLAG_TYPE_IN_PLACE);
19422    }
19423
19424  /* Finish up the enumeration.  */
19425  if (type != error_mark_node)
19426    {
19427      if (new_value_list)
19428	finish_enum_value_list (type);
19429      if (is_new_type)
19430	finish_enum (type);
19431    }
19432
19433  if (nested_name_specifier)
19434    {
19435      if (CLASS_TYPE_P (nested_name_specifier))
19436	{
19437	  TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19438	  pop_scope (nested_name_specifier);
19439	}
19440      else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19441	pop_nested_namespace (nested_name_specifier);
19442    }
19443  return type;
19444}
19445
19446/* Parse an enumerator-list.  The enumerators all have the indicated
19447   TYPE.
19448
19449   enumerator-list:
19450     enumerator-definition
19451     enumerator-list , enumerator-definition  */
19452
19453static void
19454cp_parser_enumerator_list (cp_parser* parser, tree type)
19455{
19456  while (true)
19457    {
19458      /* Parse an enumerator-definition.  */
19459      cp_parser_enumerator_definition (parser, type);
19460
19461      /* If the next token is not a ',', we've reached the end of
19462	 the list.  */
19463      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19464	break;
19465      /* Otherwise, consume the `,' and keep going.  */
19466      cp_lexer_consume_token (parser->lexer);
19467      /* If the next token is a `}', there is a trailing comma.  */
19468      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19469	{
19470	  if (cxx_dialect < cxx11)
19471	    pedwarn (input_location, OPT_Wpedantic,
19472                     "comma at end of enumerator list");
19473	  break;
19474	}
19475    }
19476}
19477
19478/* Parse an enumerator-definition.  The enumerator has the indicated
19479   TYPE.
19480
19481   enumerator-definition:
19482     enumerator
19483     enumerator = constant-expression
19484
19485   enumerator:
19486     identifier
19487
19488   GNU Extensions:
19489
19490   enumerator-definition:
19491     enumerator attributes [opt]
19492     enumerator attributes [opt] = constant-expression  */
19493
19494static void
19495cp_parser_enumerator_definition (cp_parser* parser, tree type)
19496{
19497  tree identifier;
19498  tree value;
19499  location_t loc;
19500
19501  /* Save the input location because we are interested in the location
19502     of the identifier and not the location of the explicit value.  */
19503  loc = cp_lexer_peek_token (parser->lexer)->location;
19504
19505  /* Look for the identifier.  */
19506  identifier = cp_parser_identifier (parser);
19507  if (identifier == error_mark_node)
19508    return;
19509
19510  /* Parse any specified attributes.  */
19511  tree attrs = cp_parser_attributes_opt (parser);
19512
19513  /* If the next token is an '=', then there is an explicit value.  */
19514  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19515    {
19516      /* Consume the `=' token.  */
19517      cp_lexer_consume_token (parser->lexer);
19518      /* Parse the value.  */
19519      value = cp_parser_constant_expression (parser);
19520    }
19521  else
19522    value = NULL_TREE;
19523
19524  /* If we are processing a template, make sure the initializer of the
19525     enumerator doesn't contain any bare template parameter pack.  */
19526  if (check_for_bare_parameter_packs (value))
19527    value = error_mark_node;
19528
19529  /* Create the enumerator.  */
19530  build_enumerator (identifier, value, type, attrs, loc);
19531}
19532
19533/* Parse a namespace-name.
19534
19535   namespace-name:
19536     original-namespace-name
19537     namespace-alias
19538
19539   Returns the NAMESPACE_DECL for the namespace.  */
19540
19541static tree
19542cp_parser_namespace_name (cp_parser* parser)
19543{
19544  tree identifier;
19545  tree namespace_decl;
19546
19547  cp_token *token = cp_lexer_peek_token (parser->lexer);
19548
19549  /* Get the name of the namespace.  */
19550  identifier = cp_parser_identifier (parser);
19551  if (identifier == error_mark_node)
19552    return error_mark_node;
19553
19554  /* Look up the identifier in the currently active scope.  Look only
19555     for namespaces, due to:
19556
19557       [basic.lookup.udir]
19558
19559       When looking up a namespace-name in a using-directive or alias
19560       definition, only namespace names are considered.
19561
19562     And:
19563
19564       [basic.lookup.qual]
19565
19566       During the lookup of a name preceding the :: scope resolution
19567       operator, object, function, and enumerator names are ignored.
19568
19569     (Note that cp_parser_qualifying_entity only calls this
19570     function if the token after the name is the scope resolution
19571     operator.)  */
19572  namespace_decl = cp_parser_lookup_name (parser, identifier,
19573					  none_type,
19574					  /*is_template=*/false,
19575					  /*is_namespace=*/true,
19576					  /*check_dependency=*/true,
19577					  /*ambiguous_decls=*/NULL,
19578					  token->location);
19579  /* If it's not a namespace, issue an error.  */
19580  if (namespace_decl == error_mark_node
19581      || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19582    {
19583      if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19584	{
19585	  auto_diagnostic_group d;
19586	  name_hint hint;
19587	  if (namespace_decl == error_mark_node
19588	      && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19589	    hint = suggest_alternative_in_explicit_scope (token->location,
19590							  identifier,
19591							  parser->scope);
19592	  if (const char *suggestion = hint.suggestion ())
19593	    {
19594	      gcc_rich_location richloc (token->location);
19595	      richloc.add_fixit_replace (suggestion);
19596	      error_at (&richloc,
19597			"%qD is not a namespace-name; did you mean %qs?",
19598			identifier, suggestion);
19599	    }
19600	  else
19601	    error_at (token->location, "%qD is not a namespace-name",
19602		      identifier);
19603	}
19604      else
19605	cp_parser_error (parser, "expected namespace-name");
19606      namespace_decl = error_mark_node;
19607    }
19608
19609  return namespace_decl;
19610}
19611
19612/* Parse a namespace-definition.
19613
19614   namespace-definition:
19615     named-namespace-definition
19616     unnamed-namespace-definition
19617
19618   named-namespace-definition:
19619     original-namespace-definition
19620     extension-namespace-definition
19621
19622   original-namespace-definition:
19623     namespace identifier { namespace-body }
19624
19625   extension-namespace-definition:
19626     namespace original-namespace-name { namespace-body }
19627
19628   unnamed-namespace-definition:
19629     namespace { namespace-body } */
19630
19631static void
19632cp_parser_namespace_definition (cp_parser* parser)
19633{
19634  tree identifier;
19635  int nested_definition_count = 0;
19636
19637  cp_ensure_no_omp_declare_simd (parser);
19638  cp_ensure_no_oacc_routine (parser);
19639
19640  bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19641  const bool topmost_inline_p = is_inline;
19642
19643  if (is_inline)
19644    {
19645      maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19646      cp_lexer_consume_token (parser->lexer);
19647    }
19648
19649  /* Look for the `namespace' keyword.  */
19650  cp_token* token
19651    = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19652
19653  /* Parse any specified attributes before the identifier.  */
19654  tree attribs = cp_parser_attributes_opt (parser);
19655
19656  for (;;)
19657    {
19658      identifier = NULL_TREE;
19659
19660      bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19661							     RID_INLINE);
19662      if (nested_inline_p && nested_definition_count != 0)
19663	{
19664	  if (cxx_dialect < cxx2a)
19665	    pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19666		     OPT_Wpedantic, "nested inline namespace definitions only "
19667		     "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19668	  cp_lexer_consume_token (parser->lexer);
19669	}
19670
19671      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19672	{
19673	  identifier = cp_parser_identifier (parser);
19674
19675	  if (cp_next_tokens_can_be_std_attribute_p (parser))
19676	    pedwarn (input_location, OPT_Wpedantic,
19677		     "standard attributes on namespaces must precede "
19678		     "the namespace name");
19679
19680	  /* Parse any attributes specified after the identifier.  */
19681	  attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19682	}
19683
19684      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19685	{
19686	  /* Don't forget that the innermost namespace might have been
19687	     marked as inline.  Use |= because we cannot overwrite
19688	     IS_INLINE in case the outermost namespace is inline, but
19689	     there are no nested inlines.  */
19690	  is_inline |= nested_inline_p;
19691	  break;
19692	}
19693
19694      if (!nested_definition_count && cxx_dialect < cxx17)
19695        pedwarn (input_location, OPT_Wpedantic,
19696		 "nested namespace definitions only available with "
19697		 "%<-std=c++17%> or %<-std=gnu++17%>");
19698
19699      /* Nested namespace names can create new namespaces (unlike
19700	 other qualified-ids).  */
19701      if (int count = (identifier
19702		       ? push_namespace (identifier, nested_inline_p)
19703		       : 0))
19704	nested_definition_count += count;
19705      else
19706	cp_parser_error (parser, "nested namespace name required");
19707      cp_lexer_consume_token (parser->lexer);
19708    }
19709
19710  if (nested_definition_count && !identifier)
19711    cp_parser_error (parser, "namespace name required");
19712
19713  if (nested_definition_count && attribs)
19714    error_at (token->location,
19715	      "a nested namespace definition cannot have attributes");
19716  if (nested_definition_count && topmost_inline_p)
19717    error_at (token->location,
19718	      "a nested namespace definition cannot be inline");
19719
19720  /* Start the namespace.  */
19721  nested_definition_count += push_namespace (identifier, is_inline);
19722
19723  bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19724
19725  warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19726
19727  /* Look for the `{' to validate starting the namespace.  */
19728  matching_braces braces;
19729  if (braces.require_open (parser))
19730    {
19731      /* Parse the body of the namespace.  */
19732      cp_parser_namespace_body (parser);
19733
19734      /* Look for the final `}'.  */
19735      braces.require_close (parser);
19736    }
19737
19738  if (has_visibility)
19739    pop_visibility (1);
19740
19741  /* Pop the nested namespace definitions.  */
19742  while (nested_definition_count--)
19743    pop_namespace ();
19744}
19745
19746/* Parse a namespace-body.
19747
19748   namespace-body:
19749     declaration-seq [opt]  */
19750
19751static void
19752cp_parser_namespace_body (cp_parser* parser)
19753{
19754  cp_parser_declaration_seq_opt (parser);
19755}
19756
19757/* Parse a namespace-alias-definition.
19758
19759   namespace-alias-definition:
19760     namespace identifier = qualified-namespace-specifier ;  */
19761
19762static void
19763cp_parser_namespace_alias_definition (cp_parser* parser)
19764{
19765  tree identifier;
19766  tree namespace_specifier;
19767
19768  cp_token *token = cp_lexer_peek_token (parser->lexer);
19769
19770  /* Look for the `namespace' keyword.  */
19771  cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19772  /* Look for the identifier.  */
19773  identifier = cp_parser_identifier (parser);
19774  if (identifier == error_mark_node)
19775    return;
19776  /* Look for the `=' token.  */
19777  if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19778      && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19779    {
19780      error_at (token->location, "%<namespace%> definition is not allowed here");
19781      /* Skip the definition.  */
19782      cp_lexer_consume_token (parser->lexer);
19783      if (cp_parser_skip_to_closing_brace (parser))
19784	cp_lexer_consume_token (parser->lexer);
19785      return;
19786    }
19787  cp_parser_require (parser, CPP_EQ, RT_EQ);
19788  /* Look for the qualified-namespace-specifier.  */
19789  namespace_specifier
19790    = cp_parser_qualified_namespace_specifier (parser);
19791  cp_warn_deprecated_use_scopes (namespace_specifier);
19792  /* Look for the `;' token.  */
19793  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19794
19795  /* Register the alias in the symbol table.  */
19796  do_namespace_alias (identifier, namespace_specifier);
19797}
19798
19799/* Parse a qualified-namespace-specifier.
19800
19801   qualified-namespace-specifier:
19802     :: [opt] nested-name-specifier [opt] namespace-name
19803
19804   Returns a NAMESPACE_DECL corresponding to the specified
19805   namespace.  */
19806
19807static tree
19808cp_parser_qualified_namespace_specifier (cp_parser* parser)
19809{
19810  /* Look for the optional `::'.  */
19811  cp_parser_global_scope_opt (parser,
19812			      /*current_scope_valid_p=*/false);
19813
19814  /* Look for the optional nested-name-specifier.  */
19815  cp_parser_nested_name_specifier_opt (parser,
19816				       /*typename_keyword_p=*/false,
19817				       /*check_dependency_p=*/true,
19818				       /*type_p=*/false,
19819				       /*is_declaration=*/true);
19820
19821  return cp_parser_namespace_name (parser);
19822}
19823
19824/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19825   access declaration.
19826
19827   using-declaration:
19828     using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19829     using :: unqualified-id ;
19830
19831   access-declaration:
19832     qualified-id ;
19833
19834   */
19835
19836static bool
19837cp_parser_using_declaration (cp_parser* parser,
19838			     bool access_declaration_p)
19839{
19840  cp_token *token;
19841  bool typename_p = false;
19842  bool global_scope_p;
19843  tree decl;
19844  tree identifier;
19845  tree qscope;
19846  int oldcount = errorcount;
19847  cp_token *diag_token = NULL;
19848
19849  if (access_declaration_p)
19850    {
19851      diag_token = cp_lexer_peek_token (parser->lexer);
19852      cp_parser_parse_tentatively (parser);
19853    }
19854  else
19855    {
19856      /* Look for the `using' keyword.  */
19857      cp_parser_require_keyword (parser, RID_USING, RT_USING);
19858
19859 again:
19860      /* Peek at the next token.  */
19861      token = cp_lexer_peek_token (parser->lexer);
19862      /* See if it's `typename'.  */
19863      if (token->keyword == RID_TYPENAME)
19864	{
19865	  /* Remember that we've seen it.  */
19866	  typename_p = true;
19867	  /* Consume the `typename' token.  */
19868	  cp_lexer_consume_token (parser->lexer);
19869	}
19870    }
19871
19872  /* Look for the optional global scope qualification.  */
19873  global_scope_p
19874    = (cp_parser_global_scope_opt (parser,
19875				   /*current_scope_valid_p=*/false)
19876       != NULL_TREE);
19877
19878  /* If we saw `typename', or didn't see `::', then there must be a
19879     nested-name-specifier present.  */
19880  if (typename_p || !global_scope_p)
19881    {
19882      qscope = cp_parser_nested_name_specifier (parser, typename_p,
19883						/*check_dependency_p=*/true,
19884						/*type_p=*/false,
19885						/*is_declaration=*/true);
19886      if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19887	{
19888	  cp_parser_skip_to_end_of_block_or_statement (parser);
19889	  return false;
19890	}
19891    }
19892  /* Otherwise, we could be in either of the two productions.  In that
19893     case, treat the nested-name-specifier as optional.  */
19894  else
19895    qscope = cp_parser_nested_name_specifier_opt (parser,
19896						  /*typename_keyword_p=*/false,
19897						  /*check_dependency_p=*/true,
19898						  /*type_p=*/false,
19899						  /*is_declaration=*/true);
19900  if (!qscope)
19901    qscope = global_namespace;
19902  else if (UNSCOPED_ENUM_P (qscope)
19903	   && !TYPE_FUNCTION_SCOPE_P (qscope))
19904    qscope = CP_TYPE_CONTEXT (qscope);
19905
19906  cp_warn_deprecated_use_scopes (qscope);
19907
19908  if (access_declaration_p && cp_parser_error_occurred (parser))
19909    /* Something has already gone wrong; there's no need to parse
19910       further.  Since an error has occurred, the return value of
19911       cp_parser_parse_definitely will be false, as required.  */
19912    return cp_parser_parse_definitely (parser);
19913
19914  token = cp_lexer_peek_token (parser->lexer);
19915  /* Parse the unqualified-id.  */
19916  identifier = cp_parser_unqualified_id (parser,
19917					 /*template_keyword_p=*/false,
19918					 /*check_dependency_p=*/true,
19919					 /*declarator_p=*/true,
19920					 /*optional_p=*/false);
19921
19922  if (access_declaration_p)
19923    {
19924      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19925	cp_parser_simulate_error (parser);
19926      if (!cp_parser_parse_definitely (parser))
19927	return false;
19928    }
19929  else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19930    {
19931      cp_token *ell = cp_lexer_consume_token (parser->lexer);
19932      if (cxx_dialect < cxx17)
19933	pedwarn (ell->location, 0,
19934		 "pack expansion in using-declaration only available "
19935		 "with %<-std=c++17%> or %<-std=gnu++17%>");
19936      qscope = make_pack_expansion (qscope);
19937    }
19938
19939  /* The function we call to handle a using-declaration is different
19940     depending on what scope we are in.  */
19941  if (qscope == error_mark_node || identifier == error_mark_node)
19942    ;
19943  else if (!identifier_p (identifier)
19944	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
19945    /* [namespace.udecl]
19946
19947       A using declaration shall not name a template-id.  */
19948    error_at (token->location,
19949	      "a template-id may not appear in a using-declaration");
19950  else
19951    {
19952      if (at_class_scope_p ())
19953	{
19954	  /* Create the USING_DECL.  */
19955	  decl = do_class_using_decl (qscope, identifier);
19956
19957	  if (decl && typename_p)
19958	    USING_DECL_TYPENAME_P (decl) = 1;
19959
19960	  if (check_for_bare_parameter_packs (decl))
19961	    {
19962	      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19963	      return false;
19964	    }
19965	  else
19966	    /* Add it to the list of members in this class.  */
19967	    finish_member_declaration (decl);
19968	}
19969      else
19970	finish_nonmember_using_decl (qscope, identifier);
19971    }
19972
19973  if (!access_declaration_p
19974      && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19975    {
19976      cp_token *comma = cp_lexer_consume_token (parser->lexer);
19977      if (cxx_dialect < cxx17)
19978	pedwarn (comma->location, 0,
19979		 "comma-separated list in using-declaration only available "
19980		 "with %<-std=c++17%> or %<-std=gnu++17%>");
19981      goto again;
19982    }
19983
19984  /* Look for the final `;'.  */
19985  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19986
19987  if (access_declaration_p && errorcount == oldcount)
19988    warning_at (diag_token->location, OPT_Wdeprecated,
19989		"access declarations are deprecated "
19990		"in favour of using-declarations; "
19991		"suggestion: add the %<using%> keyword");
19992
19993  return true;
19994}
19995
19996/* Parse an alias-declaration.
19997
19998   alias-declaration:
19999     using identifier attribute-specifier-seq [opt] = type-id  */
20000
20001static tree
20002cp_parser_alias_declaration (cp_parser* parser)
20003{
20004  tree id, type, decl, pushed_scope = NULL_TREE, attributes;
20005  location_t id_location, type_location;
20006  cp_declarator *declarator;
20007  cp_decl_specifier_seq decl_specs;
20008  bool member_p;
20009  const char *saved_message = NULL;
20010
20011  /* Look for the `using' keyword.  */
20012  cp_token *using_token
20013    = cp_parser_require_keyword (parser, RID_USING, RT_USING);
20014  if (using_token == NULL)
20015    return error_mark_node;
20016
20017  id_location = cp_lexer_peek_token (parser->lexer)->location;
20018  id = cp_parser_identifier (parser);
20019  if (id == error_mark_node)
20020    return error_mark_node;
20021
20022  cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
20023  attributes = cp_parser_attributes_opt (parser);
20024  if (attributes == error_mark_node)
20025    return error_mark_node;
20026
20027  cp_parser_require (parser, CPP_EQ, RT_EQ);
20028
20029  if (cp_parser_error_occurred (parser))
20030    return error_mark_node;
20031
20032  cp_parser_commit_to_tentative_parse (parser);
20033
20034  /* Now we are going to parse the type-id of the declaration.  */
20035
20036  /*
20037    [dcl.type]/3 says:
20038
20039	"A type-specifier-seq shall not define a class or enumeration
20040	 unless it appears in the type-id of an alias-declaration (7.1.3) that
20041	 is not the declaration of a template-declaration."
20042
20043    In other words, if we currently are in an alias template, the
20044    type-id should not define a type.
20045
20046    So let's set parser->type_definition_forbidden_message in that
20047    case; cp_parser_check_type_definition (called by
20048    cp_parser_class_specifier) will then emit an error if a type is
20049    defined in the type-id.  */
20050  if (parser->num_template_parameter_lists)
20051    {
20052      saved_message = parser->type_definition_forbidden_message;
20053      parser->type_definition_forbidden_message =
20054	G_("types may not be defined in alias template declarations");
20055    }
20056
20057  type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
20058			    &type_location);
20059
20060  /* Restore the error message if need be.  */
20061  if (parser->num_template_parameter_lists)
20062    parser->type_definition_forbidden_message = saved_message;
20063
20064  if (type == error_mark_node
20065      || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
20066    {
20067      cp_parser_skip_to_end_of_block_or_statement (parser);
20068      return error_mark_node;
20069    }
20070
20071  /* A typedef-name can also be introduced by an alias-declaration. The
20072     identifier following the using keyword becomes a typedef-name. It has
20073     the same semantics as if it were introduced by the typedef
20074     specifier. In particular, it does not define a new type and it shall
20075     not appear in the type-id.  */
20076
20077  clear_decl_specs (&decl_specs);
20078  decl_specs.type = type;
20079  if (attributes != NULL_TREE)
20080    {
20081      decl_specs.attributes = attributes;
20082      set_and_check_decl_spec_loc (&decl_specs,
20083				   ds_attribute,
20084				   attrs_token);
20085    }
20086  set_and_check_decl_spec_loc (&decl_specs,
20087			       ds_typedef,
20088			       using_token);
20089  set_and_check_decl_spec_loc (&decl_specs,
20090			       ds_alias,
20091			       using_token);
20092  decl_specs.locations[ds_type_spec] = type_location;
20093
20094  if (parser->num_template_parameter_lists
20095      && !cp_parser_check_template_parameters (parser,
20096					       /*num_templates=*/0,
20097					       /*template_id*/false,
20098					       id_location,
20099					       /*declarator=*/NULL))
20100    return error_mark_node;
20101
20102  declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
20103
20104  member_p = at_class_scope_p ();
20105  if (member_p)
20106    decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
20107		      NULL_TREE, attributes);
20108  else
20109    decl = start_decl (declarator, &decl_specs, 0,
20110		       attributes, NULL_TREE, &pushed_scope);
20111  if (decl == error_mark_node)
20112    return decl;
20113
20114  cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
20115
20116  if (pushed_scope)
20117    pop_scope (pushed_scope);
20118
20119  /* If decl is a template, return its TEMPLATE_DECL so that it gets
20120     added into the symbol table; otherwise, return the TYPE_DECL.  */
20121  if (DECL_LANG_SPECIFIC (decl)
20122      && DECL_TEMPLATE_INFO (decl)
20123      && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
20124    {
20125      decl = DECL_TI_TEMPLATE (decl);
20126      if (member_p)
20127	check_member_template (decl);
20128    }
20129
20130  return decl;
20131}
20132
20133/* Parse a using-directive.
20134
20135   using-directive:
20136     using namespace :: [opt] nested-name-specifier [opt]
20137       namespace-name ;  */
20138
20139static void
20140cp_parser_using_directive (cp_parser* parser)
20141{
20142  tree namespace_decl;
20143  tree attribs;
20144
20145  /* Look for the `using' keyword.  */
20146  cp_parser_require_keyword (parser, RID_USING, RT_USING);
20147  /* And the `namespace' keyword.  */
20148  cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
20149  /* Look for the optional `::' operator.  */
20150  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20151  /* And the optional nested-name-specifier.  */
20152  cp_parser_nested_name_specifier_opt (parser,
20153				       /*typename_keyword_p=*/false,
20154				       /*check_dependency_p=*/true,
20155				       /*type_p=*/false,
20156				       /*is_declaration=*/true);
20157  /* Get the namespace being used.  */
20158  namespace_decl = cp_parser_namespace_name (parser);
20159  cp_warn_deprecated_use_scopes (namespace_decl);
20160  /* And any specified attributes.  */
20161  attribs = cp_parser_attributes_opt (parser);
20162
20163  /* Update the symbol table.  */
20164  finish_using_directive (namespace_decl, attribs);
20165
20166  /* Look for the final `;'.  */
20167  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20168}
20169
20170/* Parse an asm-definition.
20171
20172  asm-qualifier:
20173    volatile
20174    inline
20175    goto
20176
20177  asm-qualifier-list:
20178    asm-qualifier
20179    asm-qualifier-list asm-qualifier
20180
20181   asm-definition:
20182     asm ( string-literal ) ;
20183
20184   GNU Extension:
20185
20186   asm-definition:
20187     asm asm-qualifier-list [opt] ( string-literal ) ;
20188     asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
20189     asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20190				    : asm-operand-list [opt] ) ;
20191     asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20192				    : asm-operand-list [opt]
20193			  : asm-clobber-list [opt] ) ;
20194     asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
20195				    : asm-clobber-list [opt]
20196				    : asm-goto-list ) ;
20197
20198  The form with asm-goto-list is valid if and only if the asm-qualifier-list
20199  contains goto, and is the only allowed form in that case.  No duplicates are
20200  allowed in an asm-qualifier-list.  */
20201
20202static void
20203cp_parser_asm_definition (cp_parser* parser)
20204{
20205  tree string;
20206  tree outputs = NULL_TREE;
20207  tree inputs = NULL_TREE;
20208  tree clobbers = NULL_TREE;
20209  tree labels = NULL_TREE;
20210  tree asm_stmt;
20211  bool extended_p = false;
20212  bool invalid_inputs_p = false;
20213  bool invalid_outputs_p = false;
20214  required_token missing = RT_NONE;
20215  location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
20216
20217  /* Look for the `asm' keyword.  */
20218  cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
20219
20220  /* In C++2a, unevaluated inline assembly is permitted in constexpr
20221     functions.  */
20222  if (parser->in_function_body
20223      && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
20224      && (cxx_dialect < cxx2a))
20225    pedwarn (asm_loc, 0, "%<asm%> in %<constexpr%> function only available "
20226	     "with %<-std=c++2a%> or %<-std=gnu++2a%>");
20227
20228  /* Handle the asm-qualifier-list.  */
20229  location_t volatile_loc = UNKNOWN_LOCATION;
20230  location_t inline_loc = UNKNOWN_LOCATION;
20231  location_t goto_loc = UNKNOWN_LOCATION;
20232  location_t first_loc = UNKNOWN_LOCATION;
20233
20234  if (cp_parser_allow_gnu_extensions_p (parser))
20235    for (;;)
20236      {
20237	cp_token *token = cp_lexer_peek_token (parser->lexer);
20238	location_t loc = token->location;
20239	switch (cp_lexer_peek_token (parser->lexer)->keyword)
20240	  {
20241	  case RID_VOLATILE:
20242	    if (volatile_loc)
20243	      {
20244		error_at (loc, "duplicate %<asm%> qualifier %qT",
20245			  token->u.value);
20246		inform (volatile_loc, "first seen here");
20247	      }
20248	    else
20249	      {
20250		if (!parser->in_function_body)
20251		  warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
20252			      "outside of function body", token->u.value);
20253		volatile_loc = loc;
20254	      }
20255	    cp_lexer_consume_token (parser->lexer);
20256	    continue;
20257
20258	  case RID_INLINE:
20259	    if (inline_loc)
20260	      {
20261		error_at (loc, "duplicate %<asm%> qualifier %qT",
20262			  token->u.value);
20263		inform (inline_loc, "first seen here");
20264	      }
20265	    else
20266	      inline_loc = loc;
20267	    if (!first_loc)
20268	      first_loc = loc;
20269	    cp_lexer_consume_token (parser->lexer);
20270	    continue;
20271
20272	  case RID_GOTO:
20273	    if (goto_loc)
20274	      {
20275		error_at (loc, "duplicate %<asm%> qualifier %qT",
20276			  token->u.value);
20277		inform (goto_loc, "first seen here");
20278	      }
20279	    else
20280	      goto_loc = loc;
20281	    if (!first_loc)
20282	      first_loc = loc;
20283	    cp_lexer_consume_token (parser->lexer);
20284	    continue;
20285
20286	  case RID_CONST:
20287	  case RID_RESTRICT:
20288	    error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
20289	    cp_lexer_consume_token (parser->lexer);
20290	    continue;
20291
20292	  default:
20293	    break;
20294	  }
20295	break;
20296      }
20297
20298  bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
20299  bool inline_p = (inline_loc != UNKNOWN_LOCATION);
20300  bool goto_p = (goto_loc != UNKNOWN_LOCATION);
20301
20302  if (!parser->in_function_body && (inline_p || goto_p))
20303    {
20304      error_at (first_loc, "%<asm%> qualifier outside of function body");
20305      inline_p = goto_p = false;
20306    }
20307
20308  /* Look for the opening `('.  */
20309  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20310    return;
20311  /* Look for the string.  */
20312  string = cp_parser_string_literal (parser, false, false);
20313  if (string == error_mark_node)
20314    {
20315      cp_parser_skip_to_closing_parenthesis (parser, true, false,
20316					     /*consume_paren=*/true);
20317      return;
20318    }
20319
20320  /* If we're allowing GNU extensions, check for the extended assembly
20321     syntax.  Unfortunately, the `:' tokens need not be separated by
20322     a space in C, and so, for compatibility, we tolerate that here
20323     too.  Doing that means that we have to treat the `::' operator as
20324     two `:' tokens.  */
20325  if (cp_parser_allow_gnu_extensions_p (parser)
20326      && parser->in_function_body
20327      && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
20328	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
20329    {
20330      bool inputs_p = false;
20331      bool clobbers_p = false;
20332      bool labels_p = false;
20333
20334      /* The extended syntax was used.  */
20335      extended_p = true;
20336
20337      /* Look for outputs.  */
20338      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20339	{
20340	  /* Consume the `:'.  */
20341	  cp_lexer_consume_token (parser->lexer);
20342	  /* Parse the output-operands.  */
20343	  if (cp_lexer_next_token_is_not (parser->lexer,
20344					  CPP_COLON)
20345	      && cp_lexer_next_token_is_not (parser->lexer,
20346					     CPP_SCOPE)
20347	      && cp_lexer_next_token_is_not (parser->lexer,
20348					     CPP_CLOSE_PAREN)
20349	      && !goto_p)
20350            {
20351              outputs = cp_parser_asm_operand_list (parser);
20352              if (outputs == error_mark_node)
20353                invalid_outputs_p = true;
20354            }
20355	}
20356      /* If the next token is `::', there are no outputs, and the
20357	 next token is the beginning of the inputs.  */
20358      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20359	/* The inputs are coming next.  */
20360	inputs_p = true;
20361
20362      /* Look for inputs.  */
20363      if (inputs_p
20364	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20365	{
20366	  /* Consume the `:' or `::'.  */
20367	  cp_lexer_consume_token (parser->lexer);
20368	  /* Parse the output-operands.  */
20369	  if (cp_lexer_next_token_is_not (parser->lexer,
20370					  CPP_COLON)
20371	      && cp_lexer_next_token_is_not (parser->lexer,
20372					     CPP_SCOPE)
20373	      && cp_lexer_next_token_is_not (parser->lexer,
20374					     CPP_CLOSE_PAREN))
20375            {
20376              inputs = cp_parser_asm_operand_list (parser);
20377              if (inputs == error_mark_node)
20378                invalid_inputs_p = true;
20379            }
20380	}
20381      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20382	/* The clobbers are coming next.  */
20383	clobbers_p = true;
20384
20385      /* Look for clobbers.  */
20386      if (clobbers_p
20387	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20388	{
20389	  clobbers_p = true;
20390	  /* Consume the `:' or `::'.  */
20391	  cp_lexer_consume_token (parser->lexer);
20392	  /* Parse the clobbers.  */
20393	  if (cp_lexer_next_token_is_not (parser->lexer,
20394					  CPP_COLON)
20395	      && cp_lexer_next_token_is_not (parser->lexer,
20396					     CPP_CLOSE_PAREN))
20397	    clobbers = cp_parser_asm_clobber_list (parser);
20398	}
20399      else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20400	/* The labels are coming next.  */
20401	labels_p = true;
20402
20403      /* Look for labels.  */
20404      if (labels_p
20405	  || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
20406	{
20407	  labels_p = true;
20408	  /* Consume the `:' or `::'.  */
20409	  cp_lexer_consume_token (parser->lexer);
20410	  /* Parse the labels.  */
20411	  labels = cp_parser_asm_label_list (parser);
20412	}
20413
20414      if (goto_p && !labels_p)
20415	missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20416    }
20417  else if (goto_p)
20418    missing = RT_COLON_SCOPE;
20419
20420  /* Look for the closing `)'.  */
20421  if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20422			  missing ? missing : RT_CLOSE_PAREN))
20423    cp_parser_skip_to_closing_parenthesis (parser, true, false,
20424					   /*consume_paren=*/true);
20425  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20426
20427  if (!invalid_inputs_p && !invalid_outputs_p)
20428    {
20429      /* Create the ASM_EXPR.  */
20430      if (parser->in_function_body)
20431	{
20432	  asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
20433				      inputs, clobbers, labels, inline_p);
20434	  /* If the extended syntax was not used, mark the ASM_EXPR.  */
20435	  if (!extended_p)
20436	    {
20437	      tree temp = asm_stmt;
20438	      if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20439		temp = TREE_OPERAND (temp, 0);
20440
20441	      ASM_INPUT_P (temp) = 1;
20442	    }
20443	}
20444      else
20445	symtab->finalize_toplevel_asm (string);
20446    }
20447}
20448
20449/* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20450   type that comes from the decl-specifier-seq.  */
20451
20452static tree
20453strip_declarator_types (tree type, cp_declarator *declarator)
20454{
20455  for (cp_declarator *d = declarator; d;)
20456    switch (d->kind)
20457      {
20458      case cdk_id:
20459      case cdk_decomp:
20460      case cdk_error:
20461	d = NULL;
20462	break;
20463
20464      default:
20465	if (TYPE_PTRMEMFUNC_P (type))
20466	  type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20467	type = TREE_TYPE (type);
20468	d = d->declarator;
20469	break;
20470      }
20471
20472  return type;
20473}
20474
20475/* Declarators [gram.dcl.decl] */
20476
20477/* Parse an init-declarator.
20478
20479   init-declarator:
20480     declarator initializer [opt]
20481
20482   GNU Extension:
20483
20484   init-declarator:
20485     declarator asm-specification [opt] attributes [opt] initializer [opt]
20486
20487   function-definition:
20488     decl-specifier-seq [opt] declarator ctor-initializer [opt]
20489       function-body
20490     decl-specifier-seq [opt] declarator function-try-block
20491
20492   GNU Extension:
20493
20494   function-definition:
20495     __extension__ function-definition
20496
20497   TM Extension:
20498
20499   function-definition:
20500     decl-specifier-seq [opt] declarator function-transaction-block
20501
20502   The parser flags FLAGS is used to control type-specifier parsing.
20503
20504   The DECL_SPECIFIERS apply to this declarator.  Returns a
20505   representation of the entity declared.  If MEMBER_P is TRUE, then
20506   this declarator appears in a class scope.  The new DECL created by
20507   this declarator is returned.
20508
20509   The CHECKS are access checks that should be performed once we know
20510   what entity is being declared (and, therefore, what classes have
20511   befriended it).
20512
20513   If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20514   for a function-definition here as well.  If the declarator is a
20515   declarator for a function-definition, *FUNCTION_DEFINITION_P will
20516   be TRUE upon return.  By that point, the function-definition will
20517   have been completely parsed.
20518
20519   FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20520   is FALSE.
20521
20522   If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20523   parsed declaration if it is an uninitialized single declarator not followed
20524   by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20525   if present, will not be consumed.  If returned, this declarator will be
20526   created with SD_INITIALIZED but will not call cp_finish_decl.
20527
20528   If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20529   and there is an initializer, the pointed location_t is set to the
20530   location of the '=' or `(', or '{' in C++11 token introducing the
20531   initializer.  */
20532
20533static tree
20534cp_parser_init_declarator (cp_parser* parser,
20535			   cp_parser_flags flags,
20536			   cp_decl_specifier_seq *decl_specifiers,
20537			   vec<deferred_access_check, va_gc> *checks,
20538			   bool function_definition_allowed_p,
20539			   bool member_p,
20540			   int declares_class_or_enum,
20541			   bool* function_definition_p,
20542			   tree* maybe_range_for_decl,
20543			   location_t* init_loc,
20544			   tree* auto_result)
20545{
20546  cp_token *token = NULL, *asm_spec_start_token = NULL,
20547           *attributes_start_token = NULL;
20548  cp_declarator *declarator;
20549  tree prefix_attributes;
20550  tree attributes = NULL;
20551  tree asm_specification;
20552  tree initializer;
20553  tree decl = NULL_TREE;
20554  tree scope;
20555  int is_initialized;
20556  /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
20557     initialized with "= ..", CPP_OPEN_PAREN if initialized with
20558     "(...)".  */
20559  enum cpp_ttype initialization_kind;
20560  bool is_direct_init = false;
20561  bool is_non_constant_init;
20562  int ctor_dtor_or_conv_p;
20563  bool friend_p = cp_parser_friend_p (decl_specifiers);
20564  tree pushed_scope = NULL_TREE;
20565  bool range_for_decl_p = false;
20566  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20567  location_t tmp_init_loc = UNKNOWN_LOCATION;
20568
20569  if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
20570    flags |= CP_PARSER_FLAGS_CONSTEVAL;
20571
20572  /* Gather the attributes that were provided with the
20573     decl-specifiers.  */
20574  prefix_attributes = decl_specifiers->attributes;
20575
20576  /* Assume that this is not the declarator for a function
20577     definition.  */
20578  if (function_definition_p)
20579    *function_definition_p = false;
20580
20581  /* Default arguments are only permitted for function parameters.  */
20582  if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20583    parser->default_arg_ok_p = false;
20584
20585  /* Defer access checks while parsing the declarator; we cannot know
20586     what names are accessible until we know what is being
20587     declared.  */
20588  resume_deferring_access_checks ();
20589
20590  token = cp_lexer_peek_token (parser->lexer);
20591
20592  /* Parse the declarator.  */
20593  declarator
20594    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20595			    flags, &ctor_dtor_or_conv_p,
20596			    /*parenthesized_p=*/NULL,
20597			    member_p, friend_p, /*static_p=*/false);
20598  /* Gather up the deferred checks.  */
20599  stop_deferring_access_checks ();
20600
20601  parser->default_arg_ok_p = saved_default_arg_ok_p;
20602
20603  /* If the DECLARATOR was erroneous, there's no need to go
20604     further.  */
20605  if (declarator == cp_error_declarator)
20606    return error_mark_node;
20607
20608  /* Check that the number of template-parameter-lists is OK.  */
20609  if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20610						       token->location))
20611    return error_mark_node;
20612
20613  if (declares_class_or_enum & 2)
20614    cp_parser_check_for_definition_in_return_type (declarator,
20615						   decl_specifiers->type,
20616						   decl_specifiers->locations[ds_type_spec]);
20617
20618  /* Figure out what scope the entity declared by the DECLARATOR is
20619     located in.  `grokdeclarator' sometimes changes the scope, so
20620     we compute it now.  */
20621  scope = get_scope_of_declarator (declarator);
20622
20623  /* Perform any lookups in the declared type which were thought to be
20624     dependent, but are not in the scope of the declarator.  */
20625  decl_specifiers->type
20626    = maybe_update_decl_type (decl_specifiers->type, scope);
20627
20628  /* If we're allowing GNU extensions, look for an
20629     asm-specification.  */
20630  if (cp_parser_allow_gnu_extensions_p (parser))
20631    {
20632      /* Look for an asm-specification.  */
20633      asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20634      asm_specification = cp_parser_asm_specification_opt (parser);
20635    }
20636  else
20637    asm_specification = NULL_TREE;
20638
20639  /* Look for attributes.  */
20640  attributes_start_token = cp_lexer_peek_token (parser->lexer);
20641  attributes = cp_parser_attributes_opt (parser);
20642
20643  /* Peek at the next token.  */
20644  token = cp_lexer_peek_token (parser->lexer);
20645
20646  bool bogus_implicit_tmpl = false;
20647
20648  if (function_declarator_p (declarator))
20649    {
20650      /* Handle C++17 deduction guides.  */
20651      if (!decl_specifiers->type
20652	  && !decl_specifiers->any_type_specifiers_p
20653	  && ctor_dtor_or_conv_p <= 0
20654	  && cxx_dialect >= cxx17)
20655	{
20656	  cp_declarator *id = get_id_declarator (declarator);
20657	  tree name = id->u.id.unqualified_name;
20658	  parser->scope = id->u.id.qualifying_scope;
20659	  tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20660	  if (tmpl
20661	      && (DECL_CLASS_TEMPLATE_P (tmpl)
20662		  || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20663	    {
20664	      id->u.id.unqualified_name = dguide_name (tmpl);
20665	      id->u.id.sfk = sfk_deduction_guide;
20666	      ctor_dtor_or_conv_p = 1;
20667	    }
20668	}
20669
20670      /* Check to see if the token indicates the start of a
20671	 function-definition.  */
20672      if (cp_parser_token_starts_function_definition_p (token))
20673	{
20674	  if (!function_definition_allowed_p)
20675	    {
20676	      /* If a function-definition should not appear here, issue an
20677		 error message.  */
20678	      cp_parser_error (parser,
20679			       "a function-definition is not allowed here");
20680	      return error_mark_node;
20681	    }
20682
20683	  location_t func_brace_location
20684	    = cp_lexer_peek_token (parser->lexer)->location;
20685
20686	  /* Neither attributes nor an asm-specification are allowed
20687	     on a function-definition.  */
20688	  if (asm_specification)
20689	    error_at (asm_spec_start_token->location,
20690		      "an %<asm%> specification is not allowed "
20691		      "on a function-definition");
20692	  if (attributes)
20693	    error_at (attributes_start_token->location,
20694		      "attributes are not allowed "
20695		      "on a function-definition");
20696	  /* This is a function-definition.  */
20697	  *function_definition_p = true;
20698
20699	  /* Parse the function definition.  */
20700	  if (member_p)
20701	    decl = cp_parser_save_member_function_body (parser,
20702							decl_specifiers,
20703							declarator,
20704							prefix_attributes);
20705	  else
20706	    decl =
20707	      (cp_parser_function_definition_from_specifiers_and_declarator
20708	       (parser, decl_specifiers, prefix_attributes, declarator));
20709
20710	  if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20711	    {
20712	      /* This is where the prologue starts...  */
20713	      DECL_STRUCT_FUNCTION (decl)->function_start_locus
20714		= func_brace_location;
20715	    }
20716
20717	  return decl;
20718	}
20719    }
20720  else if (parser->fully_implicit_function_template_p)
20721    {
20722      /* A non-template declaration involving a function parameter list
20723	 containing an implicit template parameter will be made into a
20724	 template.  If the resulting declaration is not going to be an
20725	 actual function then finish the template scope here to prevent it.
20726	 An error message will be issued once we have a decl to talk about.
20727
20728         FIXME probably we should do type deduction rather than create an
20729         implicit template, but the standard currently doesn't allow it. */
20730      bogus_implicit_tmpl = true;
20731      finish_fully_implicit_template (parser, NULL_TREE);
20732    }
20733
20734  /* [dcl.dcl]
20735
20736     Only in function declarations for constructors, destructors, type
20737     conversions, and deduction guides can the decl-specifier-seq be omitted.
20738
20739     We explicitly postpone this check past the point where we handle
20740     function-definitions because we tolerate function-definitions
20741     that are missing their return types in some modes.  */
20742  if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20743    {
20744      cp_parser_error (parser,
20745		       "expected constructor, destructor, or type conversion");
20746      return error_mark_node;
20747    }
20748
20749  /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
20750  if (token->type == CPP_EQ
20751      || token->type == CPP_OPEN_PAREN
20752      || token->type == CPP_OPEN_BRACE)
20753    {
20754      is_initialized = SD_INITIALIZED;
20755      initialization_kind = token->type;
20756      if (maybe_range_for_decl)
20757	*maybe_range_for_decl = error_mark_node;
20758      tmp_init_loc = token->location;
20759      if (init_loc && *init_loc == UNKNOWN_LOCATION)
20760	*init_loc = tmp_init_loc;
20761
20762      if (token->type == CPP_EQ
20763	  && function_declarator_p (declarator))
20764	{
20765	  cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20766	  if (t2->keyword == RID_DEFAULT)
20767	    is_initialized = SD_DEFAULTED;
20768	  else if (t2->keyword == RID_DELETE)
20769	    is_initialized = SD_DELETED;
20770	}
20771    }
20772  else
20773    {
20774      /* If the init-declarator isn't initialized and isn't followed by a
20775	 `,' or `;', it's not a valid init-declarator.  */
20776      if (token->type != CPP_COMMA
20777	  && token->type != CPP_SEMICOLON)
20778	{
20779	  if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20780	    range_for_decl_p = true;
20781	  else
20782	    {
20783	      if (!maybe_range_for_decl)
20784		cp_parser_error (parser, "expected initializer");
20785	      return error_mark_node;
20786	    }
20787	}
20788      is_initialized = SD_UNINITIALIZED;
20789      initialization_kind = CPP_EOF;
20790    }
20791
20792  /* Because start_decl has side-effects, we should only call it if we
20793     know we're going ahead.  By this point, we know that we cannot
20794     possibly be looking at any other construct.  */
20795  cp_parser_commit_to_tentative_parse (parser);
20796
20797  /* Enter the newly declared entry in the symbol table.  If we're
20798     processing a declaration in a class-specifier, we wait until
20799     after processing the initializer.  */
20800  if (!member_p)
20801    {
20802      if (parser->in_unbraced_linkage_specification_p)
20803	decl_specifiers->storage_class = sc_extern;
20804      decl = start_decl (declarator, decl_specifiers,
20805			 range_for_decl_p? SD_INITIALIZED : is_initialized,
20806			 attributes, prefix_attributes, &pushed_scope);
20807      cp_finalize_omp_declare_simd (parser, decl);
20808      cp_finalize_oacc_routine (parser, decl, false);
20809      /* Adjust location of decl if declarator->id_loc is more appropriate:
20810	 set, and decl wasn't merged with another decl, in which case its
20811	 location would be different from input_location, and more accurate.  */
20812      if (DECL_P (decl)
20813	  && declarator->id_loc != UNKNOWN_LOCATION
20814	  && DECL_SOURCE_LOCATION (decl) == input_location)
20815	DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20816    }
20817  else if (scope)
20818    /* Enter the SCOPE.  That way unqualified names appearing in the
20819       initializer will be looked up in SCOPE.  */
20820    pushed_scope = push_scope (scope);
20821
20822  /* Perform deferred access control checks, now that we know in which
20823     SCOPE the declared entity resides.  */
20824  if (!member_p && decl)
20825    {
20826      tree saved_current_function_decl = NULL_TREE;
20827
20828      /* If the entity being declared is a function, pretend that we
20829	 are in its scope.  If it is a `friend', it may have access to
20830	 things that would not otherwise be accessible.  */
20831      if (TREE_CODE (decl) == FUNCTION_DECL)
20832	{
20833	  saved_current_function_decl = current_function_decl;
20834	  current_function_decl = decl;
20835	}
20836
20837      /* Perform access checks for template parameters.  */
20838      cp_parser_perform_template_parameter_access_checks (checks);
20839
20840      /* Perform the access control checks for the declarator and the
20841	 decl-specifiers.  */
20842      perform_deferred_access_checks (tf_warning_or_error);
20843
20844      /* Restore the saved value.  */
20845      if (TREE_CODE (decl) == FUNCTION_DECL)
20846	current_function_decl = saved_current_function_decl;
20847    }
20848
20849  /* Parse the initializer.  */
20850  initializer = NULL_TREE;
20851  is_direct_init = false;
20852  is_non_constant_init = true;
20853  if (is_initialized)
20854    {
20855      if (function_declarator_p (declarator))
20856	{
20857	   if (initialization_kind == CPP_EQ)
20858	     initializer = cp_parser_pure_specifier (parser);
20859	   else
20860	     {
20861	       /* If the declaration was erroneous, we don't really
20862		  know what the user intended, so just silently
20863		  consume the initializer.  */
20864	       if (decl != error_mark_node)
20865		 error_at (tmp_init_loc, "initializer provided for function");
20866	       cp_parser_skip_to_closing_parenthesis (parser,
20867						      /*recovering=*/true,
20868						      /*or_comma=*/false,
20869						      /*consume_paren=*/true);
20870	     }
20871	}
20872      else
20873	{
20874	  /* We want to record the extra mangling scope for in-class
20875	     initializers of class members and initializers of static
20876	     data member templates and namespace-scope initializers.
20877	     The former involves deferring parsing of the initializer
20878	     until end of class as with default arguments.  So right
20879	     here we only handle the latter two.  */
20880	  bool has_lambda_scope = false;
20881
20882	  if (decl != error_mark_node
20883	      && !member_p
20884	      && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
20885	    has_lambda_scope = true;
20886
20887	  if (has_lambda_scope)
20888	    start_lambda_scope (decl);
20889	  initializer = cp_parser_initializer (parser,
20890					       &is_direct_init,
20891					       &is_non_constant_init);
20892	  if (has_lambda_scope)
20893	    finish_lambda_scope ();
20894	  if (initializer == error_mark_node)
20895	    cp_parser_skip_to_end_of_statement (parser);
20896	}
20897    }
20898
20899  /* The old parser allows attributes to appear after a parenthesized
20900     initializer.  Mark Mitchell proposed removing this functionality
20901     on the GCC mailing lists on 2002-08-13.  This parser accepts the
20902     attributes -- but ignores them.  Made a permerror in GCC 8.  */
20903  if (cp_parser_allow_gnu_extensions_p (parser)
20904      && initialization_kind == CPP_OPEN_PAREN
20905      && cp_parser_attributes_opt (parser)
20906      && permerror (input_location,
20907		    "attributes after parenthesized initializer ignored"))
20908    {
20909      static bool hint;
20910      if (flag_permissive && !hint)
20911	{
20912	  hint = true;
20913	  inform (input_location,
20914		  "this flexibility is deprecated and will be removed");
20915	}
20916    }
20917
20918  /* And now complain about a non-function implicit template.  */
20919  if (bogus_implicit_tmpl && decl != error_mark_node)
20920    error_at (DECL_SOURCE_LOCATION (decl),
20921	      "non-function %qD declared as implicit template", decl);
20922
20923  /* For an in-class declaration, use `grokfield' to create the
20924     declaration.  */
20925  if (member_p)
20926    {
20927      if (pushed_scope)
20928	{
20929	  pop_scope (pushed_scope);
20930	  pushed_scope = NULL_TREE;
20931	}
20932      decl = grokfield (declarator, decl_specifiers,
20933			initializer, !is_non_constant_init,
20934			/*asmspec=*/NULL_TREE,
20935			attr_chainon (attributes, prefix_attributes));
20936      if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20937	cp_parser_save_default_args (parser, decl);
20938      cp_finalize_omp_declare_simd (parser, decl);
20939      cp_finalize_oacc_routine (parser, decl, false);
20940    }
20941
20942  /* Finish processing the declaration.  But, skip member
20943     declarations.  */
20944  if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20945    {
20946      int cf = (decl_spec_seq_has_spec_p (decl_specifiers, ds_constinit)
20947		? LOOKUP_CONSTINIT : 0);
20948      cp_finish_decl (decl,
20949		      initializer, !is_non_constant_init,
20950		      asm_specification,
20951		      /* If the initializer is in parentheses, then this is
20952			 a direct-initialization, which means that an
20953			 `explicit' constructor is OK.  Otherwise, an
20954			 `explicit' constructor cannot be used.  */
20955		      ((is_direct_init || !is_initialized)
20956		       ? LOOKUP_NORMAL : LOOKUP_IMPLICIT) | cf);
20957    }
20958  else if ((cxx_dialect != cxx98) && friend_p
20959	   && decl && TREE_CODE (decl) == FUNCTION_DECL)
20960    /* Core issue #226 (C++0x only): A default template-argument
20961       shall not be specified in a friend class template
20962       declaration. */
20963    check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20964                             /*is_partial=*/false, /*is_friend_decl=*/1);
20965
20966  if (!friend_p && pushed_scope)
20967    pop_scope (pushed_scope);
20968
20969  if (function_declarator_p (declarator)
20970      && parser->fully_implicit_function_template_p)
20971    {
20972      if (member_p)
20973	decl = finish_fully_implicit_template (parser, decl);
20974      else
20975	finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20976    }
20977
20978  if (auto_result && is_initialized && decl_specifiers->type
20979      && type_uses_auto (decl_specifiers->type))
20980    *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20981
20982  return decl;
20983}
20984
20985/* Parse a declarator.
20986
20987   declarator:
20988     direct-declarator
20989     ptr-operator declarator
20990
20991   abstract-declarator:
20992     ptr-operator abstract-declarator [opt]
20993     direct-abstract-declarator
20994
20995   GNU Extensions:
20996
20997   declarator:
20998     attributes [opt] direct-declarator
20999     attributes [opt] ptr-operator declarator
21000
21001   abstract-declarator:
21002     attributes [opt] ptr-operator abstract-declarator [opt]
21003     attributes [opt] direct-abstract-declarator
21004
21005   The parser flags FLAGS is used to control type-specifier parsing.
21006
21007   If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
21008   detect constructors, destructors, deduction guides, or conversion operators.
21009   It is set to -1 if the declarator is a name, and +1 if it is a
21010   function. Otherwise it is set to zero. Usually you just want to
21011   test for >0, but internally the negative value is used.
21012
21013   (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
21014   a decl-specifier-seq unless it declares a constructor, destructor,
21015   or conversion.  It might seem that we could check this condition in
21016   semantic analysis, rather than parsing, but that makes it difficult
21017   to handle something like `f()'.  We want to notice that there are
21018   no decl-specifiers, and therefore realize that this is an
21019   expression, not a declaration.)
21020
21021   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
21022   the declarator is a direct-declarator of the form "(...)".
21023
21024   MEMBER_P is true iff this declarator is a member-declarator.
21025
21026   FRIEND_P is true iff this declarator is a friend.
21027
21028   STATIC_P is true iff the keyword static was seen.  */
21029
21030static cp_declarator *
21031cp_parser_declarator (cp_parser* parser,
21032		      cp_parser_declarator_kind dcl_kind,
21033		      cp_parser_flags flags,
21034		      int* ctor_dtor_or_conv_p,
21035		      bool* parenthesized_p,
21036		      bool member_p, bool friend_p, bool static_p)
21037{
21038  cp_declarator *declarator;
21039  enum tree_code code;
21040  cp_cv_quals cv_quals;
21041  tree class_type;
21042  tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
21043
21044  /* Assume this is not a constructor, destructor, or type-conversion
21045     operator.  */
21046  if (ctor_dtor_or_conv_p)
21047    *ctor_dtor_or_conv_p = 0;
21048
21049  if (cp_parser_allow_gnu_extensions_p (parser))
21050    gnu_attributes = cp_parser_gnu_attributes_opt (parser);
21051
21052  /* Check for the ptr-operator production.  */
21053  cp_parser_parse_tentatively (parser);
21054  /* Parse the ptr-operator.  */
21055  code = cp_parser_ptr_operator (parser,
21056				 &class_type,
21057				 &cv_quals,
21058				 &std_attributes);
21059
21060  /* If that worked, then we have a ptr-operator.  */
21061  if (cp_parser_parse_definitely (parser))
21062    {
21063      /* If a ptr-operator was found, then this declarator was not
21064	 parenthesized.  */
21065      if (parenthesized_p)
21066	*parenthesized_p = true;
21067      /* The dependent declarator is optional if we are parsing an
21068	 abstract-declarator.  */
21069      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21070	cp_parser_parse_tentatively (parser);
21071
21072      /* Parse the dependent declarator.  */
21073      declarator = cp_parser_declarator (parser, dcl_kind,
21074					 CP_PARSER_FLAGS_NONE,
21075					 /*ctor_dtor_or_conv_p=*/NULL,
21076					 /*parenthesized_p=*/NULL,
21077					 /*member_p=*/false,
21078					 friend_p, /*static_p=*/false);
21079
21080      /* If we are parsing an abstract-declarator, we must handle the
21081	 case where the dependent declarator is absent.  */
21082      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
21083	  && !cp_parser_parse_definitely (parser))
21084	declarator = NULL;
21085
21086      declarator = cp_parser_make_indirect_declarator
21087	(code, class_type, cv_quals, declarator, std_attributes);
21088    }
21089  /* Everything else is a direct-declarator.  */
21090  else
21091    {
21092      if (parenthesized_p)
21093	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
21094						   CPP_OPEN_PAREN);
21095      declarator = cp_parser_direct_declarator (parser, dcl_kind,
21096						flags, ctor_dtor_or_conv_p,
21097						member_p, friend_p, static_p);
21098    }
21099
21100  if (gnu_attributes && declarator && declarator != cp_error_declarator)
21101    declarator->attributes = gnu_attributes;
21102  return declarator;
21103}
21104
21105/* Parse a direct-declarator or direct-abstract-declarator.
21106
21107   direct-declarator:
21108     declarator-id
21109     direct-declarator ( parameter-declaration-clause )
21110       cv-qualifier-seq [opt]
21111       ref-qualifier [opt]
21112       exception-specification [opt]
21113     direct-declarator [ constant-expression [opt] ]
21114     ( declarator )
21115
21116   direct-abstract-declarator:
21117     direct-abstract-declarator [opt]
21118       ( parameter-declaration-clause )
21119       cv-qualifier-seq [opt]
21120       ref-qualifier [opt]
21121       exception-specification [opt]
21122     direct-abstract-declarator [opt] [ constant-expression [opt] ]
21123     ( abstract-declarator )
21124
21125   Returns a representation of the declarator.  DCL_KIND is
21126   CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
21127   direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
21128   we are parsing a direct-declarator.  It is
21129   CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
21130   of ambiguity we prefer an abstract declarator, as per
21131   [dcl.ambig.res].
21132   The parser flags FLAGS is used to control type-specifier parsing.
21133   CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
21134   as for cp_parser_declarator.  */
21135
21136static cp_declarator *
21137cp_parser_direct_declarator (cp_parser* parser,
21138			     cp_parser_declarator_kind dcl_kind,
21139			     cp_parser_flags flags,
21140			     int* ctor_dtor_or_conv_p,
21141			     bool member_p, bool friend_p, bool static_p)
21142{
21143  cp_token *token;
21144  cp_declarator *declarator = NULL;
21145  tree scope = NULL_TREE;
21146  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21147  bool saved_in_declarator_p = parser->in_declarator_p;
21148  bool first = true;
21149  tree pushed_scope = NULL_TREE;
21150  cp_token *open_paren = NULL, *close_paren = NULL;
21151
21152  while (true)
21153    {
21154      /* Peek at the next token.  */
21155      token = cp_lexer_peek_token (parser->lexer);
21156      if (token->type == CPP_OPEN_PAREN)
21157	{
21158	  /* This is either a parameter-declaration-clause, or a
21159	     parenthesized declarator. When we know we are parsing a
21160	     named declarator, it must be a parenthesized declarator
21161	     if FIRST is true. For instance, `(int)' is a
21162	     parameter-declaration-clause, with an omitted
21163	     direct-abstract-declarator. But `((*))', is a
21164	     parenthesized abstract declarator. Finally, when T is a
21165	     template parameter `(T)' is a
21166	     parameter-declaration-clause, and not a parenthesized
21167	     named declarator.
21168
21169	     We first try and parse a parameter-declaration-clause,
21170	     and then try a nested declarator (if FIRST is true).
21171
21172	     It is not an error for it not to be a
21173	     parameter-declaration-clause, even when FIRST is
21174	     false. Consider,
21175
21176	       int i (int);
21177	       int i (3);
21178
21179	     The first is the declaration of a function while the
21180	     second is the definition of a variable, including its
21181	     initializer.
21182
21183	     Having seen only the parenthesis, we cannot know which of
21184	     these two alternatives should be selected.  Even more
21185	     complex are examples like:
21186
21187	       int i (int (a));
21188	       int i (int (3));
21189
21190	     The former is a function-declaration; the latter is a
21191	     variable initialization.
21192
21193	     Thus again, we try a parameter-declaration-clause, and if
21194	     that fails, we back out and return.  */
21195
21196	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21197	    {
21198	      tree params;
21199	      bool is_declarator = false;
21200
21201	      open_paren = NULL;
21202
21203	      /* In a member-declarator, the only valid interpretation
21204		 of a parenthesis is the start of a
21205		 parameter-declaration-clause.  (It is invalid to
21206		 initialize a static data member with a parenthesized
21207		 initializer; only the "=" form of initialization is
21208		 permitted.)  */
21209	      if (!member_p)
21210		cp_parser_parse_tentatively (parser);
21211
21212	      /* Consume the `('.  */
21213	      matching_parens parens;
21214	      parens.consume_open (parser);
21215	      if (first)
21216		{
21217		  /* If this is going to be an abstract declarator, we're
21218		     in a declarator and we can't have default args.  */
21219		  parser->default_arg_ok_p = false;
21220		  parser->in_declarator_p = true;
21221		}
21222
21223	      begin_scope (sk_function_parms, NULL_TREE);
21224
21225	      /* Signal we are in the immediate function context.  */
21226	      if (flags & CP_PARSER_FLAGS_CONSTEVAL)
21227		current_binding_level->immediate_fn_ctx_p = true;
21228
21229	      /* Parse the parameter-declaration-clause.  */
21230	      params
21231		= cp_parser_parameter_declaration_clause (parser, flags);
21232
21233	      /* Consume the `)'.  */
21234	      parens.require_close (parser);
21235
21236	      /* If all went well, parse the cv-qualifier-seq,
21237		 ref-qualifier and the exception-specification.  */
21238	      if (member_p || cp_parser_parse_definitely (parser))
21239		{
21240		  cp_cv_quals cv_quals;
21241		  cp_virt_specifiers virt_specifiers;
21242		  cp_ref_qualifier ref_qual;
21243		  tree exception_specification;
21244		  tree late_return;
21245		  tree attrs;
21246		  bool memfn = (member_p || (pushed_scope
21247					     && CLASS_TYPE_P (pushed_scope)));
21248		  unsigned char local_variables_forbidden_p
21249		    = parser->local_variables_forbidden_p;
21250		  /* 'this' is not allowed in static member functions.  */
21251		  if (static_p || friend_p)
21252		    parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
21253
21254		  is_declarator = true;
21255
21256		  if (ctor_dtor_or_conv_p)
21257		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
21258		  first = false;
21259
21260		  /* Parse the cv-qualifier-seq.  */
21261		  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21262		  /* Parse the ref-qualifier. */
21263		  ref_qual = cp_parser_ref_qualifier_opt (parser);
21264		  /* Parse the tx-qualifier.  */
21265		  tree tx_qual = cp_parser_tx_qualifier_opt (parser);
21266
21267		  /* If it turned out that this is e.g. a pointer to a
21268		     function, we don't want to delay noexcept parsing.  */
21269		  if (declarator == NULL || declarator->kind != cdk_id)
21270		    flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
21271
21272		  /* And the exception-specification.  */
21273		  exception_specification
21274		    = cp_parser_exception_specification_opt (parser,
21275							     flags,
21276							     cv_quals);
21277
21278		  attrs = cp_parser_std_attribute_spec_seq (parser);
21279
21280		  /* In here, we handle cases where attribute is used after
21281		     the function declaration.  For example:
21282		     void func (int x) __attribute__((vector(..)));  */
21283		  tree gnu_attrs = NULL_TREE;
21284		  tree requires_clause = NULL_TREE;
21285		  late_return = (cp_parser_late_return_type_opt
21286				 (parser, declarator, requires_clause,
21287				  memfn ? cv_quals : -1));
21288
21289		  /* Parse the virt-specifier-seq.  */
21290		  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21291
21292		  /* Create the function-declarator.  */
21293		  declarator = make_call_declarator (declarator,
21294						     params,
21295						     cv_quals,
21296						     virt_specifiers,
21297						     ref_qual,
21298						     tx_qual,
21299						     exception_specification,
21300						     late_return,
21301						     requires_clause);
21302		  declarator->std_attributes = attrs;
21303		  declarator->attributes = gnu_attrs;
21304		  /* Any subsequent parameter lists are to do with
21305		     return type, so are not those of the declared
21306		     function.  */
21307		  parser->default_arg_ok_p = false;
21308
21309		  /* Restore the state of local_variables_forbidden_p.  */
21310		  parser->local_variables_forbidden_p
21311		    = local_variables_forbidden_p;
21312		}
21313
21314	      /* Remove the function parms from scope.  */
21315	      pop_bindings_and_leave_scope ();
21316
21317	      if (is_declarator)
21318		/* Repeat the main loop.  */
21319		continue;
21320	    }
21321
21322	  /* If this is the first, we can try a parenthesized
21323	     declarator.  */
21324	  if (first)
21325	    {
21326	      bool saved_in_type_id_in_expr_p;
21327
21328	      parser->default_arg_ok_p = saved_default_arg_ok_p;
21329	      parser->in_declarator_p = saved_in_declarator_p;
21330
21331	      open_paren = token;
21332	      /* Consume the `('.  */
21333	      matching_parens parens;
21334	      parens.consume_open (parser);
21335	      /* Parse the nested declarator.  */
21336	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21337	      parser->in_type_id_in_expr_p = true;
21338	      declarator
21339		= cp_parser_declarator (parser, dcl_kind, flags,
21340					ctor_dtor_or_conv_p,
21341					/*parenthesized_p=*/NULL,
21342					member_p, friend_p,
21343					/*static_p=*/false);
21344	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21345	      first = false;
21346	      /* Expect a `)'.  */
21347	      close_paren = cp_lexer_peek_token (parser->lexer);
21348	      if (!parens.require_close (parser))
21349		declarator = cp_error_declarator;
21350	      if (declarator == cp_error_declarator)
21351		break;
21352
21353	      goto handle_declarator;
21354	    }
21355	  /* Otherwise, we must be done.  */
21356	  else
21357	    break;
21358	}
21359      else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21360	       && token->type == CPP_OPEN_SQUARE
21361	       && !cp_next_tokens_can_be_attribute_p (parser))
21362	{
21363	  /* Parse an array-declarator.  */
21364	  tree bounds, attrs;
21365
21366	  if (ctor_dtor_or_conv_p)
21367	    *ctor_dtor_or_conv_p = 0;
21368
21369	  open_paren = NULL;
21370	  first = false;
21371	  parser->default_arg_ok_p = false;
21372	  parser->in_declarator_p = true;
21373	  /* Consume the `['.  */
21374	  cp_lexer_consume_token (parser->lexer);
21375	  /* Peek at the next token.  */
21376	  token = cp_lexer_peek_token (parser->lexer);
21377	  /* If the next token is `]', then there is no
21378	     constant-expression.  */
21379	  if (token->type != CPP_CLOSE_SQUARE)
21380	    {
21381	      bool non_constant_p;
21382	      bounds
21383		= cp_parser_constant_expression (parser,
21384						 /*allow_non_constant=*/true,
21385						 &non_constant_p);
21386	      if (!non_constant_p)
21387		/* OK */;
21388	      else if (error_operand_p (bounds))
21389		/* Already gave an error.  */;
21390	      else if (!parser->in_function_body
21391		       || current_binding_level->kind == sk_function_parms)
21392		{
21393		  /* Normally, the array bound must be an integral constant
21394		     expression.  However, as an extension, we allow VLAs
21395		     in function scopes as long as they aren't part of a
21396		     parameter declaration.  */
21397		  cp_parser_error (parser,
21398				   "array bound is not an integer constant");
21399		  bounds = error_mark_node;
21400		}
21401	      else if (processing_template_decl
21402		       && !type_dependent_expression_p (bounds))
21403		{
21404		  /* Remember this wasn't a constant-expression.  */
21405		  bounds = build_nop (TREE_TYPE (bounds), bounds);
21406		  TREE_SIDE_EFFECTS (bounds) = 1;
21407		}
21408	    }
21409	  else
21410	    bounds = NULL_TREE;
21411	  /* Look for the closing `]'.  */
21412	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21413	    {
21414	      declarator = cp_error_declarator;
21415	      break;
21416	    }
21417
21418	  attrs = cp_parser_std_attribute_spec_seq (parser);
21419	  declarator = make_array_declarator (declarator, bounds);
21420	  declarator->std_attributes = attrs;
21421	}
21422      else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
21423	{
21424	  {
21425	    tree qualifying_scope;
21426	    tree unqualified_name;
21427	    tree attrs;
21428	    special_function_kind sfk;
21429	    bool abstract_ok;
21430	    bool pack_expansion_p = false;
21431	    cp_token *declarator_id_start_token;
21432
21433	    /* Parse a declarator-id */
21434	    abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
21435	    if (abstract_ok)
21436	      {
21437		cp_parser_parse_tentatively (parser);
21438
21439		/* If we see an ellipsis, we should be looking at a
21440		   parameter pack. */
21441		if (token->type == CPP_ELLIPSIS)
21442		  {
21443		    /* Consume the `...' */
21444		    cp_lexer_consume_token (parser->lexer);
21445
21446		    pack_expansion_p = true;
21447		  }
21448	      }
21449
21450	    declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21451	    unqualified_name
21452	      = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21453	    qualifying_scope = parser->scope;
21454	    if (abstract_ok)
21455	      {
21456		bool okay = false;
21457
21458		if (!unqualified_name && pack_expansion_p)
21459		  {
21460		    /* Check whether an error occurred. */
21461		    okay = !cp_parser_error_occurred (parser);
21462
21463		    /* We already consumed the ellipsis to mark a
21464		       parameter pack, but we have no way to report it,
21465		       so abort the tentative parse. We will be exiting
21466		       immediately anyway. */
21467		    cp_parser_abort_tentative_parse (parser);
21468		  }
21469		else
21470		  okay = cp_parser_parse_definitely (parser);
21471
21472		if (!okay)
21473		  unqualified_name = error_mark_node;
21474		else if (unqualified_name
21475			 && (qualifying_scope
21476			     || (!identifier_p (unqualified_name))))
21477		  {
21478		    cp_parser_error (parser, "expected unqualified-id");
21479		    unqualified_name = error_mark_node;
21480		  }
21481	      }
21482
21483	    if (!unqualified_name)
21484	      return NULL;
21485	    if (unqualified_name == error_mark_node)
21486	      {
21487		declarator = cp_error_declarator;
21488		pack_expansion_p = false;
21489		declarator->parameter_pack_p = false;
21490		break;
21491	      }
21492
21493	    attrs = cp_parser_std_attribute_spec_seq (parser);
21494
21495	    if (qualifying_scope && at_namespace_scope_p ()
21496		&& TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21497	      {
21498		/* In the declaration of a member of a template class
21499		   outside of the class itself, the SCOPE will sometimes
21500		   be a TYPENAME_TYPE.  For example, given:
21501
21502		   template <typename T>
21503		   int S<T>::R::i = 3;
21504
21505		   the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
21506		   this context, we must resolve S<T>::R to an ordinary
21507		   type, rather than a typename type.
21508
21509		   The reason we normally avoid resolving TYPENAME_TYPEs
21510		   is that a specialization of `S' might render
21511		   `S<T>::R' not a type.  However, if `S' is
21512		   specialized, then this `i' will not be used, so there
21513		   is no harm in resolving the types here.  */
21514		tree type;
21515
21516		/* Resolve the TYPENAME_TYPE.  */
21517		type = resolve_typename_type (qualifying_scope,
21518					      /*only_current_p=*/false);
21519		/* If that failed, the declarator is invalid.  */
21520		if (TREE_CODE (type) == TYPENAME_TYPE)
21521		  {
21522		    if (typedef_variant_p (type))
21523		      error_at (declarator_id_start_token->location,
21524				"cannot define member of dependent typedef "
21525				"%qT", type);
21526		    else
21527		      error_at (declarator_id_start_token->location,
21528				"%<%T::%E%> is not a type",
21529				TYPE_CONTEXT (qualifying_scope),
21530				TYPE_IDENTIFIER (qualifying_scope));
21531		  }
21532		qualifying_scope = type;
21533	      }
21534
21535	    sfk = sfk_none;
21536
21537	    if (unqualified_name)
21538	      {
21539		tree class_type;
21540
21541		if (qualifying_scope
21542		    && CLASS_TYPE_P (qualifying_scope))
21543		  class_type = qualifying_scope;
21544		else
21545		  class_type = current_class_type;
21546
21547		if (TREE_CODE (unqualified_name) == TYPE_DECL)
21548		  {
21549		    tree name_type = TREE_TYPE (unqualified_name);
21550
21551		    if (!class_type || !same_type_p (name_type, class_type))
21552		      {
21553			/* We do not attempt to print the declarator
21554			   here because we do not have enough
21555			   information about its original syntactic
21556			   form.  */
21557			cp_parser_error (parser, "invalid declarator");
21558			declarator = cp_error_declarator;
21559			break;
21560		      }
21561		    else if (qualifying_scope
21562			     && CLASSTYPE_USE_TEMPLATE (name_type))
21563		      {
21564			error_at (declarator_id_start_token->location,
21565				  "invalid use of constructor as a template");
21566			inform (declarator_id_start_token->location,
21567				"use %<%T::%D%> instead of %<%T::%D%> to "
21568				"name the constructor in a qualified name",
21569				class_type,
21570				DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21571				class_type, name_type);
21572			declarator = cp_error_declarator;
21573			break;
21574		      }
21575		    unqualified_name = constructor_name (class_type);
21576		  }
21577
21578		if (class_type)
21579		  {
21580		    if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21581		      sfk = sfk_destructor;
21582		    else if (identifier_p (unqualified_name)
21583			     && IDENTIFIER_CONV_OP_P (unqualified_name))
21584		      sfk = sfk_conversion;
21585		    else if (/* There's no way to declare a constructor
21586				for an unnamed type, even if the type
21587				got a name for linkage purposes.  */
21588			     !TYPE_WAS_UNNAMED (class_type)
21589			     /* Handle correctly (c++/19200):
21590
21591				struct S {
21592				  struct T{};
21593				  friend void S(T);
21594				};
21595
21596				and also:
21597
21598				namespace N {
21599				  void S();
21600				}
21601
21602				struct S {
21603				  friend void N::S();
21604				};  */
21605			     && (!friend_p || class_type == qualifying_scope)
21606			     && constructor_name_p (unqualified_name,
21607						    class_type))
21608		      sfk = sfk_constructor;
21609		    else if (is_overloaded_fn (unqualified_name)
21610			     && DECL_CONSTRUCTOR_P (get_first_fn
21611						    (unqualified_name)))
21612		      sfk = sfk_constructor;
21613
21614		    if (ctor_dtor_or_conv_p && sfk != sfk_none)
21615		      *ctor_dtor_or_conv_p = -1;
21616		  }
21617	      }
21618	    declarator = make_id_declarator (qualifying_scope,
21619					     unqualified_name,
21620					     sfk, token->location);
21621	    declarator->std_attributes = attrs;
21622	    declarator->parameter_pack_p = pack_expansion_p;
21623
21624	    if (pack_expansion_p)
21625	      maybe_warn_variadic_templates ();
21626
21627	    /* We're looking for this case in [temp.res]:
21628	       A qualified-id is assumed to name a type if [...]
21629	       - it is a decl-specifier of the decl-specifier-seq of a
21630		 parameter-declaration in a declarator of a function or
21631		 function template declaration, ... */
21632	    if (cxx_dialect >= cxx2a
21633		&& (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21634		&& declarator->kind == cdk_id
21635		&& !at_class_scope_p ()
21636		&& cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21637	      {
21638		/* ...whose declarator-id is qualified.  If it isn't, never
21639		   assume the parameters to refer to types.  */
21640		if (qualifying_scope == NULL_TREE)
21641		  flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21642		else
21643		  {
21644		    /* Now we have something like
21645		       template <typename T> int C::x(S::p);
21646		       which can be a function template declaration or a
21647		       variable template definition.  If name lookup for
21648		       the declarator-id C::x finds one or more function
21649		       templates, assume S::p to name a type.  Otherwise,
21650		       don't.  */
21651		    tree decl
21652		      = cp_parser_lookup_name_simple (parser, unqualified_name,
21653						      token->location);
21654		    if (!is_overloaded_fn (decl)
21655			/* Allow
21656			   template<typename T>
21657			   A<T>::A(T::type) { }  */
21658			&& !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21659			     && constructor_name_p (unqualified_name,
21660						    qualifying_scope)))
21661		      flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21662		  }
21663	      }
21664	  }
21665
21666	handle_declarator:;
21667	  scope = get_scope_of_declarator (declarator);
21668	  if (scope)
21669	    {
21670	      /* Any names that appear after the declarator-id for a
21671		 member are looked up in the containing scope.  */
21672	      if (at_function_scope_p ())
21673		{
21674		  /* But declarations with qualified-ids can't appear in a
21675		     function.  */
21676		  cp_parser_error (parser, "qualified-id in declaration");
21677		  declarator = cp_error_declarator;
21678		  break;
21679		}
21680	      pushed_scope = push_scope (scope);
21681	    }
21682	  parser->in_declarator_p = true;
21683	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21684	      || (declarator && declarator->kind == cdk_id))
21685	    /* Default args are only allowed on function
21686	       declarations.  */
21687	    parser->default_arg_ok_p = saved_default_arg_ok_p;
21688	  else
21689	    parser->default_arg_ok_p = false;
21690
21691	  first = false;
21692	}
21693      /* We're done.  */
21694      else
21695	break;
21696    }
21697
21698  /* For an abstract declarator, we might wind up with nothing at this
21699     point.  That's an error; the declarator is not optional.  */
21700  if (!declarator)
21701    cp_parser_error (parser, "expected declarator");
21702  else if (open_paren)
21703    {
21704      /* Record overly parenthesized declarator so we can give a
21705	 diagnostic about confusing decl/expr disambiguation.  */
21706      if (declarator->kind == cdk_array)
21707	{
21708	  /* If the open and close parens are on different lines, this
21709	     is probably a formatting thing, so ignore.  */
21710	  expanded_location open = expand_location (open_paren->location);
21711	  expanded_location close = expand_location (close_paren->location);
21712	  if (open.line != close.line || open.file != close.file)
21713	    open_paren = NULL;
21714	}
21715      if (open_paren)
21716	declarator->parenthesized = open_paren->location;
21717    }
21718
21719  /* If we entered a scope, we must exit it now.  */
21720  if (pushed_scope)
21721    pop_scope (pushed_scope);
21722
21723  parser->default_arg_ok_p = saved_default_arg_ok_p;
21724  parser->in_declarator_p = saved_in_declarator_p;
21725
21726  return declarator;
21727}
21728
21729/* Parse a ptr-operator.
21730
21731   ptr-operator:
21732     * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21733     * cv-qualifier-seq [opt]
21734     &
21735     :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21736     nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21737
21738   GNU Extension:
21739
21740   ptr-operator:
21741     & cv-qualifier-seq [opt]
21742
21743   Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21744   Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21745   an rvalue reference. In the case of a pointer-to-member, *TYPE is
21746   filled in with the TYPE containing the member.  *CV_QUALS is
21747   filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21748   are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
21749   Note that the tree codes returned by this function have nothing
21750   to do with the types of trees that will be eventually be created
21751   to represent the pointer or reference type being parsed. They are
21752   just constants with suggestive names. */
21753static enum tree_code
21754cp_parser_ptr_operator (cp_parser* parser,
21755			tree* type,
21756			cp_cv_quals *cv_quals,
21757			tree *attributes)
21758{
21759  enum tree_code code = ERROR_MARK;
21760  cp_token *token;
21761  tree attrs = NULL_TREE;
21762
21763  /* Assume that it's not a pointer-to-member.  */
21764  *type = NULL_TREE;
21765  /* And that there are no cv-qualifiers.  */
21766  *cv_quals = TYPE_UNQUALIFIED;
21767
21768  /* Peek at the next token.  */
21769  token = cp_lexer_peek_token (parser->lexer);
21770
21771  /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
21772  if (token->type == CPP_MULT)
21773    code = INDIRECT_REF;
21774  else if (token->type == CPP_AND)
21775    code = ADDR_EXPR;
21776  else if ((cxx_dialect != cxx98) &&
21777	   token->type == CPP_AND_AND) /* C++0x only */
21778    code = NON_LVALUE_EXPR;
21779
21780  if (code != ERROR_MARK)
21781    {
21782      /* Consume the `*', `&' or `&&'.  */
21783      cp_lexer_consume_token (parser->lexer);
21784
21785      /* A `*' can be followed by a cv-qualifier-seq, and so can a
21786	 `&', if we are allowing GNU extensions.  (The only qualifier
21787	 that can legally appear after `&' is `restrict', but that is
21788	 enforced during semantic analysis.  */
21789      if (code == INDIRECT_REF
21790	  || cp_parser_allow_gnu_extensions_p (parser))
21791	*cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21792
21793      attrs = cp_parser_std_attribute_spec_seq (parser);
21794      if (attributes != NULL)
21795	*attributes = attrs;
21796    }
21797  else
21798    {
21799      /* Try the pointer-to-member case.  */
21800      cp_parser_parse_tentatively (parser);
21801      /* Look for the optional `::' operator.  */
21802      cp_parser_global_scope_opt (parser,
21803				  /*current_scope_valid_p=*/false);
21804      /* Look for the nested-name specifier.  */
21805      token = cp_lexer_peek_token (parser->lexer);
21806      cp_parser_nested_name_specifier (parser,
21807				       /*typename_keyword_p=*/false,
21808				       /*check_dependency_p=*/true,
21809				       /*type_p=*/false,
21810				       /*is_declaration=*/false);
21811      /* If we found it, and the next token is a `*', then we are
21812	 indeed looking at a pointer-to-member operator.  */
21813      if (!cp_parser_error_occurred (parser)
21814	  && cp_parser_require (parser, CPP_MULT, RT_MULT))
21815	{
21816	  /* Indicate that the `*' operator was used.  */
21817	  code = INDIRECT_REF;
21818
21819	  if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21820	    error_at (token->location, "%qD is a namespace", parser->scope);
21821	  else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21822	    error_at (token->location, "cannot form pointer to member of "
21823		      "non-class %q#T", parser->scope);
21824	  else
21825	    {
21826	      /* The type of which the member is a member is given by the
21827		 current SCOPE.  */
21828	      *type = parser->scope;
21829	      /* The next name will not be qualified.  */
21830	      parser->scope = NULL_TREE;
21831	      parser->qualifying_scope = NULL_TREE;
21832	      parser->object_scope = NULL_TREE;
21833	      /* Look for optional c++11 attributes.  */
21834	      attrs = cp_parser_std_attribute_spec_seq (parser);
21835	      if (attributes != NULL)
21836		*attributes = attrs;
21837	      /* Look for the optional cv-qualifier-seq.  */
21838	      *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21839	    }
21840	}
21841      /* If that didn't work we don't have a ptr-operator.  */
21842      if (!cp_parser_parse_definitely (parser))
21843	cp_parser_error (parser, "expected ptr-operator");
21844    }
21845
21846  return code;
21847}
21848
21849/* Parse an (optional) cv-qualifier-seq.
21850
21851   cv-qualifier-seq:
21852     cv-qualifier cv-qualifier-seq [opt]
21853
21854   cv-qualifier:
21855     const
21856     volatile
21857
21858   GNU Extension:
21859
21860   cv-qualifier:
21861     __restrict__
21862
21863   Returns a bitmask representing the cv-qualifiers.  */
21864
21865static cp_cv_quals
21866cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21867{
21868  cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21869
21870  while (true)
21871    {
21872      cp_token *token;
21873      cp_cv_quals cv_qualifier;
21874
21875      /* Peek at the next token.  */
21876      token = cp_lexer_peek_token (parser->lexer);
21877      /* See if it's a cv-qualifier.  */
21878      switch (token->keyword)
21879	{
21880	case RID_CONST:
21881	  cv_qualifier = TYPE_QUAL_CONST;
21882	  break;
21883
21884	case RID_VOLATILE:
21885	  cv_qualifier = TYPE_QUAL_VOLATILE;
21886	  break;
21887
21888	case RID_RESTRICT:
21889	  cv_qualifier = TYPE_QUAL_RESTRICT;
21890	  break;
21891
21892	default:
21893	  cv_qualifier = TYPE_UNQUALIFIED;
21894	  break;
21895	}
21896
21897      if (!cv_qualifier)
21898	break;
21899
21900      if (cv_quals & cv_qualifier)
21901	{
21902	  gcc_rich_location richloc (token->location);
21903	  richloc.add_fixit_remove ();
21904	  error_at (&richloc, "duplicate cv-qualifier");
21905	  cp_lexer_purge_token (parser->lexer);
21906	}
21907      else
21908	{
21909	  cp_lexer_consume_token (parser->lexer);
21910	  cv_quals |= cv_qualifier;
21911	}
21912    }
21913
21914  return cv_quals;
21915}
21916
21917/* Parse an (optional) ref-qualifier
21918
21919   ref-qualifier:
21920     &
21921     &&
21922
21923   Returns cp_ref_qualifier representing ref-qualifier. */
21924
21925static cp_ref_qualifier
21926cp_parser_ref_qualifier_opt (cp_parser* parser)
21927{
21928  cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21929
21930  /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
21931  if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21932    return ref_qual;
21933
21934  while (true)
21935    {
21936      cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21937      cp_token *token = cp_lexer_peek_token (parser->lexer);
21938
21939      switch (token->type)
21940	{
21941	case CPP_AND:
21942	  curr_ref_qual = REF_QUAL_LVALUE;
21943	  break;
21944
21945	case CPP_AND_AND:
21946	  curr_ref_qual = REF_QUAL_RVALUE;
21947	  break;
21948
21949	default:
21950	  curr_ref_qual = REF_QUAL_NONE;
21951	  break;
21952	}
21953
21954      if (!curr_ref_qual)
21955	break;
21956      else if (ref_qual)
21957	{
21958	  error_at (token->location, "multiple ref-qualifiers");
21959	  cp_lexer_purge_token (parser->lexer);
21960	}
21961      else
21962	{
21963	  ref_qual = curr_ref_qual;
21964	  cp_lexer_consume_token (parser->lexer);
21965	}
21966    }
21967
21968  return ref_qual;
21969}
21970
21971/* Parse an optional tx-qualifier.
21972
21973   tx-qualifier:
21974     transaction_safe
21975     transaction_safe_dynamic  */
21976
21977static tree
21978cp_parser_tx_qualifier_opt (cp_parser *parser)
21979{
21980  cp_token *token = cp_lexer_peek_token (parser->lexer);
21981  if (token->type == CPP_NAME)
21982    {
21983      tree name = token->u.value;
21984      const char *p = IDENTIFIER_POINTER (name);
21985      const int len = strlen ("transaction_safe");
21986      if (!strncmp (p, "transaction_safe", len))
21987	{
21988	  p += len;
21989	  if (*p == '\0'
21990	      || !strcmp (p, "_dynamic"))
21991	    {
21992	      cp_lexer_consume_token (parser->lexer);
21993	      if (!flag_tm)
21994		{
21995		  error ("%qE requires %<-fgnu-tm%>", name);
21996		  return NULL_TREE;
21997		}
21998	      else
21999		return name;
22000	    }
22001	}
22002    }
22003  return NULL_TREE;
22004}
22005
22006/* Parse an (optional) virt-specifier-seq.
22007
22008   virt-specifier-seq:
22009     virt-specifier virt-specifier-seq [opt]
22010
22011   virt-specifier:
22012     override
22013     final
22014
22015   Returns a bitmask representing the virt-specifiers.  */
22016
22017static cp_virt_specifiers
22018cp_parser_virt_specifier_seq_opt (cp_parser* parser)
22019{
22020  cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22021
22022  while (true)
22023    {
22024      cp_token *token;
22025      cp_virt_specifiers virt_specifier;
22026
22027      /* Peek at the next token.  */
22028      token = cp_lexer_peek_token (parser->lexer);
22029      /* See if it's a virt-specifier-qualifier.  */
22030      if (token->type != CPP_NAME)
22031        break;
22032      if (id_equal (token->u.value, "override"))
22033        {
22034          maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22035          virt_specifier = VIRT_SPEC_OVERRIDE;
22036        }
22037      else if (id_equal (token->u.value, "final"))
22038        {
22039          maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22040          virt_specifier = VIRT_SPEC_FINAL;
22041        }
22042      else if (id_equal (token->u.value, "__final"))
22043        {
22044          virt_specifier = VIRT_SPEC_FINAL;
22045        }
22046      else
22047	break;
22048
22049      if (virt_specifiers & virt_specifier)
22050	{
22051	  gcc_rich_location richloc (token->location);
22052	  richloc.add_fixit_remove ();
22053	  error_at (&richloc, "duplicate virt-specifier");
22054	  cp_lexer_purge_token (parser->lexer);
22055	}
22056      else
22057	{
22058	  cp_lexer_consume_token (parser->lexer);
22059	  virt_specifiers |= virt_specifier;
22060	}
22061    }
22062  return virt_specifiers;
22063}
22064
22065/* Used by handling of trailing-return-types and NSDMI, in which 'this'
22066   is in scope even though it isn't real.  */
22067
22068void
22069inject_this_parameter (tree ctype, cp_cv_quals quals)
22070{
22071  tree this_parm;
22072
22073  if (current_class_ptr)
22074    {
22075      /* We don't clear this between NSDMIs.  Is it already what we want?  */
22076      tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
22077      if (DECL_P (current_class_ptr)
22078	  && DECL_CONTEXT (current_class_ptr) == NULL_TREE
22079	  && same_type_ignoring_top_level_qualifiers_p (ctype, type)
22080	  && cp_type_quals (type) == quals)
22081	return;
22082    }
22083
22084  this_parm = build_this_parm (NULL_TREE, ctype, quals);
22085  /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
22086  current_class_ptr = NULL_TREE;
22087  current_class_ref
22088    = cp_build_fold_indirect_ref (this_parm);
22089  current_class_ptr = this_parm;
22090}
22091
22092/* Return true iff our current scope is a non-static data member
22093   initializer.  */
22094
22095bool
22096parsing_nsdmi (void)
22097{
22098  /* We recognize NSDMI context by the context-less 'this' pointer set up
22099     by the function above.  */
22100  if (current_class_ptr
22101      && TREE_CODE (current_class_ptr) == PARM_DECL
22102      && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
22103    return true;
22104  return false;
22105}
22106
22107/* Parse a late-specified return type, if any.  This is not a separate
22108   non-terminal, but part of a function declarator, which looks like
22109
22110   -> trailing-type-specifier-seq abstract-declarator(opt)
22111
22112   Returns the type indicated by the type-id.
22113
22114   In addition to this, parse any queued up #pragma omp declare simd
22115   clauses, and #pragma acc routine clauses.
22116
22117   QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
22118   function.  */
22119
22120static tree
22121cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
22122				tree& requires_clause, cp_cv_quals quals)
22123{
22124  cp_token *token;
22125  tree type = NULL_TREE;
22126  bool declare_simd_p = (parser->omp_declare_simd
22127			 && declarator
22128			 && declarator->kind == cdk_id);
22129
22130  bool oacc_routine_p = (parser->oacc_routine
22131			 && declarator
22132			 && declarator->kind == cdk_id);
22133
22134  /* Peek at the next token.  */
22135  token = cp_lexer_peek_token (parser->lexer);
22136  /* A late-specified return type is indicated by an initial '->'. */
22137  if (token->type != CPP_DEREF
22138      && token->keyword != RID_REQUIRES
22139      && !(token->type == CPP_NAME
22140	   && token->u.value == ridpointers[RID_REQUIRES])
22141      && !(declare_simd_p || oacc_routine_p))
22142    return NULL_TREE;
22143
22144  tree save_ccp = current_class_ptr;
22145  tree save_ccr = current_class_ref;
22146  if (quals >= 0)
22147    {
22148      /* DR 1207: 'this' is in scope in the trailing return type.  */
22149      inject_this_parameter (current_class_type, quals);
22150    }
22151
22152  if (token->type == CPP_DEREF)
22153    {
22154      /* Consume the ->.  */
22155      cp_lexer_consume_token (parser->lexer);
22156
22157      type = cp_parser_trailing_type_id (parser);
22158    }
22159
22160  /* Function declarations may be followed by a trailing
22161     requires-clause.  */
22162  requires_clause = cp_parser_requires_clause_opt (parser, false);
22163
22164  if (declare_simd_p)
22165    declarator->attributes
22166      = cp_parser_late_parsing_omp_declare_simd (parser,
22167						 declarator->attributes);
22168  if (oacc_routine_p)
22169    declarator->attributes
22170      = cp_parser_late_parsing_oacc_routine (parser,
22171					     declarator->attributes);
22172
22173  if (quals >= 0)
22174    {
22175      current_class_ptr = save_ccp;
22176      current_class_ref = save_ccr;
22177    }
22178
22179  return type;
22180}
22181
22182/* Parse a declarator-id.
22183
22184   declarator-id:
22185     id-expression
22186     :: [opt] nested-name-specifier [opt] type-name
22187
22188   In the `id-expression' case, the value returned is as for
22189   cp_parser_id_expression if the id-expression was an unqualified-id.
22190   If the id-expression was a qualified-id, then a SCOPE_REF is
22191   returned.  The first operand is the scope (either a NAMESPACE_DECL
22192   or TREE_TYPE), but the second is still just a representation of an
22193   unqualified-id.  */
22194
22195static tree
22196cp_parser_declarator_id (cp_parser* parser, bool optional_p)
22197{
22198  tree id;
22199  /* The expression must be an id-expression.  Assume that qualified
22200     names are the names of types so that:
22201
22202       template <class T>
22203       int S<T>::R::i = 3;
22204
22205     will work; we must treat `S<T>::R' as the name of a type.
22206     Similarly, assume that qualified names are templates, where
22207     required, so that:
22208
22209       template <class T>
22210       int S<T>::R<T>::i = 3;
22211
22212     will work, too.  */
22213  id = cp_parser_id_expression (parser,
22214				/*template_keyword_p=*/false,
22215				/*check_dependency_p=*/false,
22216				/*template_p=*/NULL,
22217				/*declarator_p=*/true,
22218				optional_p);
22219  if (id && BASELINK_P (id))
22220    id = BASELINK_FUNCTIONS (id);
22221  return id;
22222}
22223
22224/* Parse a type-id.
22225
22226   type-id:
22227     type-specifier-seq abstract-declarator [opt]
22228
22229   The parser flags FLAGS is used to control type-specifier parsing.
22230
22231   If IS_TEMPLATE_ARG is true, we are parsing a template argument.
22232
22233   If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22234   i.e. we've just seen "->".
22235
22236   Returns the TYPE specified.  */
22237
22238static tree
22239cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
22240		     bool is_template_arg, bool is_trailing_return,
22241		     location_t *type_location)
22242{
22243  cp_decl_specifier_seq type_specifier_seq;
22244  cp_declarator *abstract_declarator;
22245
22246  /* Parse the type-specifier-seq.  */
22247  cp_parser_type_specifier_seq (parser, flags,
22248				/*is_declaration=*/false,
22249				is_trailing_return,
22250				&type_specifier_seq);
22251  if (type_location)
22252    *type_location = type_specifier_seq.locations[ds_type_spec];
22253
22254  if (is_template_arg && type_specifier_seq.type
22255      && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
22256      && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
22257    /* A bare template name as a template argument is a template template
22258       argument, not a placeholder, so fail parsing it as a type argument.  */
22259    {
22260      gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
22261      cp_parser_simulate_error (parser);
22262      return error_mark_node;
22263    }
22264  if (type_specifier_seq.type == error_mark_node)
22265    return error_mark_node;
22266
22267  /* There might or might not be an abstract declarator.  */
22268  cp_parser_parse_tentatively (parser);
22269  /* Look for the declarator.  */
22270  abstract_declarator
22271    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
22272			    CP_PARSER_FLAGS_NONE, NULL,
22273			    /*parenthesized_p=*/NULL,
22274			    /*member_p=*/false,
22275			    /*friend_p=*/false,
22276			    /*static_p=*/false);
22277  /* Check to see if there really was a declarator.  */
22278  if (!cp_parser_parse_definitely (parser))
22279    abstract_declarator = NULL;
22280
22281  if (type_specifier_seq.type
22282      /* The concepts TS allows 'auto' as a type-id.  */
22283      && (!flag_concepts || parser->in_type_id_in_expr_p)
22284      /* None of the valid uses of 'auto' in C++14 involve the type-id
22285	 nonterminal, but it is valid in a trailing-return-type.  */
22286      && !(cxx_dialect >= cxx14 && is_trailing_return))
22287    if (tree auto_node = type_uses_auto (type_specifier_seq.type))
22288      {
22289	/* A type-id with type 'auto' is only ok if the abstract declarator
22290	   is a function declarator with a late-specified return type.
22291
22292	   A type-id with 'auto' is also valid in a trailing-return-type
22293	   in a compound-requirement. */
22294	if (abstract_declarator
22295	    && abstract_declarator->kind == cdk_function
22296	    && abstract_declarator->u.function.late_return_type)
22297	  /* OK */;
22298	else if (parser->in_result_type_constraint_p)
22299	  /* OK */;
22300	else
22301	  {
22302	    location_t loc = type_specifier_seq.locations[ds_type_spec];
22303	    if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
22304	      {
22305		error_at (loc, "missing template arguments after %qT",
22306			  auto_node);
22307		inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
22308			tmpl);
22309	      }
22310	    else
22311	      error_at (loc, "invalid use of %qT", auto_node);
22312	    return error_mark_node;
22313	  }
22314      }
22315
22316  return groktypename (&type_specifier_seq, abstract_declarator,
22317		       is_template_arg);
22318}
22319
22320/* Wrapper for cp_parser_type_id_1.  */
22321
22322static tree
22323cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
22324		   location_t *type_location)
22325{
22326  return cp_parser_type_id_1 (parser, flags, false, false, type_location);
22327}
22328
22329/* Wrapper for cp_parser_type_id_1.  */
22330
22331static tree
22332cp_parser_template_type_arg (cp_parser *parser)
22333{
22334  tree r;
22335  const char *saved_message = parser->type_definition_forbidden_message;
22336  parser->type_definition_forbidden_message
22337    = G_("types may not be defined in template arguments");
22338  r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
22339  parser->type_definition_forbidden_message = saved_message;
22340  if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
22341    {
22342      error ("invalid use of %<auto%> in template argument");
22343      r = error_mark_node;
22344    }
22345  return r;
22346}
22347
22348/* Wrapper for cp_parser_type_id_1.  */
22349
22350static tree
22351cp_parser_trailing_type_id (cp_parser *parser)
22352{
22353  return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22354			      false, true, NULL);
22355}
22356
22357/* Parse a type-specifier-seq.
22358
22359   type-specifier-seq:
22360     type-specifier type-specifier-seq [opt]
22361
22362   GNU extension:
22363
22364   type-specifier-seq:
22365     attributes type-specifier-seq [opt]
22366
22367   The parser flags FLAGS is used to control type-specifier parsing.
22368
22369   If IS_DECLARATION is true, we are at the start of a "condition" or
22370   exception-declaration, so we might be followed by a declarator-id.
22371
22372   If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22373   i.e. we've just seen "->".
22374
22375   Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
22376
22377static void
22378cp_parser_type_specifier_seq (cp_parser* parser,
22379			      cp_parser_flags flags,
22380			      bool is_declaration,
22381			      bool is_trailing_return,
22382			      cp_decl_specifier_seq *type_specifier_seq)
22383{
22384  bool seen_type_specifier = false;
22385  cp_token *start_token = NULL;
22386
22387  /* Clear the TYPE_SPECIFIER_SEQ.  */
22388  clear_decl_specs (type_specifier_seq);
22389
22390  flags |= CP_PARSER_FLAGS_OPTIONAL;
22391  /* In the context of a trailing return type, enum E { } is an
22392     elaborated-type-specifier followed by a function-body, not an
22393     enum-specifier.  */
22394  if (is_trailing_return)
22395    flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
22396
22397  /* Parse the type-specifiers and attributes.  */
22398  while (true)
22399    {
22400      tree type_specifier;
22401      bool is_cv_qualifier;
22402
22403      /* Check for attributes first.  */
22404      if (cp_next_tokens_can_be_attribute_p (parser))
22405	{
22406	  /* GNU attributes at the end of a declaration apply to the
22407	     declaration as a whole, not to the trailing return type.  So look
22408	     ahead to see if these attributes are at the end.  */
22409	  if (seen_type_specifier && is_trailing_return
22410	      && cp_next_tokens_can_be_gnu_attribute_p (parser))
22411	    {
22412	      size_t n = cp_parser_skip_attributes_opt (parser, 1);
22413	      cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
22414	      if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
22415		  || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
22416		break;
22417	    }
22418	  type_specifier_seq->attributes
22419	    = attr_chainon (type_specifier_seq->attributes,
22420			    cp_parser_attributes_opt (parser));
22421	  continue;
22422	}
22423
22424      /* record the token of the beginning of the type specifier seq,
22425         for error reporting purposes*/
22426     if (!start_token)
22427       start_token = cp_lexer_peek_token (parser->lexer);
22428
22429      /* Look for the type-specifier.  */
22430      type_specifier = cp_parser_type_specifier (parser,
22431						 flags,
22432						 type_specifier_seq,
22433						 /*is_declaration=*/false,
22434						 NULL,
22435						 &is_cv_qualifier);
22436      if (!type_specifier)
22437	{
22438	  /* If the first type-specifier could not be found, this is not a
22439	     type-specifier-seq at all.  */
22440	  if (!seen_type_specifier)
22441	    {
22442	      /* Set in_declarator_p to avoid skipping to the semicolon.  */
22443	      int in_decl = parser->in_declarator_p;
22444	      parser->in_declarator_p = true;
22445
22446	      if (cp_parser_uncommitted_to_tentative_parse_p (parser)
22447		  || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
22448		cp_parser_error (parser, "expected type-specifier");
22449
22450	      parser->in_declarator_p = in_decl;
22451
22452	      type_specifier_seq->type = error_mark_node;
22453	      return;
22454	    }
22455	  /* If subsequent type-specifiers could not be found, the
22456	     type-specifier-seq is complete.  */
22457	  break;
22458	}
22459
22460      seen_type_specifier = true;
22461      /* The standard says that a condition can be:
22462
22463	    type-specifier-seq declarator = assignment-expression
22464
22465	 However, given:
22466
22467	   struct S {};
22468	   if (int S = ...)
22469
22470	 we should treat the "S" as a declarator, not as a
22471	 type-specifier.  The standard doesn't say that explicitly for
22472	 type-specifier-seq, but it does say that for
22473	 decl-specifier-seq in an ordinary declaration.  Perhaps it
22474	 would be clearer just to allow a decl-specifier-seq here, and
22475	 then add a semantic restriction that if any decl-specifiers
22476	 that are not type-specifiers appear, the program is invalid.  */
22477      if (is_declaration && !is_cv_qualifier)
22478	flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22479    }
22480}
22481
22482/* Return whether the function currently being declared has an associated
22483   template parameter list.  */
22484
22485static bool
22486function_being_declared_is_template_p (cp_parser* parser)
22487{
22488  if (!current_template_parms || processing_template_parmlist)
22489    return false;
22490
22491  if (parser->implicit_template_scope)
22492    return true;
22493
22494  if (at_class_scope_p ()
22495      && TYPE_BEING_DEFINED (current_class_type))
22496    return parser->num_template_parameter_lists != 0;
22497
22498  return ((int) parser->num_template_parameter_lists > template_class_depth
22499	  (current_class_type));
22500}
22501
22502/* Parse a parameter-declaration-clause.
22503
22504   parameter-declaration-clause:
22505     parameter-declaration-list [opt] ... [opt]
22506     parameter-declaration-list , ...
22507
22508   The parser flags FLAGS is used to control type-specifier parsing.
22509
22510   Returns a representation for the parameter declarations.  A return
22511   value of NULL indicates a parameter-declaration-clause consisting
22512   only of an ellipsis.  */
22513
22514static tree
22515cp_parser_parameter_declaration_clause (cp_parser* parser,
22516					cp_parser_flags flags)
22517{
22518  tree parameters;
22519  cp_token *token;
22520  bool ellipsis_p;
22521
22522  temp_override<bool> cleanup
22523    (parser->auto_is_implicit_function_template_parm_p);
22524
22525  if (!processing_specialization
22526      && !processing_template_parmlist
22527      && !processing_explicit_instantiation
22528      /* default_arg_ok_p tracks whether this is a parameter-clause for an
22529         actual function or a random abstract declarator.  */
22530      && parser->default_arg_ok_p)
22531    if (!current_function_decl
22532	|| (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22533      parser->auto_is_implicit_function_template_parm_p = true;
22534
22535  /* Peek at the next token.  */
22536  token = cp_lexer_peek_token (parser->lexer);
22537  /* Check for trivial parameter-declaration-clauses.  */
22538  if (token->type == CPP_ELLIPSIS)
22539    {
22540      /* Consume the `...' token.  */
22541      cp_lexer_consume_token (parser->lexer);
22542      return NULL_TREE;
22543    }
22544  else if (token->type == CPP_CLOSE_PAREN)
22545    /* There are no parameters.  */
22546    return void_list_node;
22547  /* Check for `(void)', too, which is a special case.  */
22548  else if (token->keyword == RID_VOID
22549	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22550	       == CPP_CLOSE_PAREN))
22551    {
22552      /* Consume the `void' token.  */
22553      cp_lexer_consume_token (parser->lexer);
22554      /* There are no parameters.  */
22555      return void_list_node;
22556    }
22557
22558  /* Parse the parameter-declaration-list.  */
22559  parameters = cp_parser_parameter_declaration_list (parser, flags);
22560  /* If a parse error occurred while parsing the
22561     parameter-declaration-list, then the entire
22562     parameter-declaration-clause is erroneous.  */
22563  if (parameters == error_mark_node)
22564    return NULL_TREE;
22565
22566  /* Peek at the next token.  */
22567  token = cp_lexer_peek_token (parser->lexer);
22568  /* If it's a `,', the clause should terminate with an ellipsis.  */
22569  if (token->type == CPP_COMMA)
22570    {
22571      /* Consume the `,'.  */
22572      cp_lexer_consume_token (parser->lexer);
22573      /* Expect an ellipsis.  */
22574      ellipsis_p
22575	= (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22576    }
22577  /* It might also be `...' if the optional trailing `,' was
22578     omitted.  */
22579  else if (token->type == CPP_ELLIPSIS)
22580    {
22581      /* Consume the `...' token.  */
22582      cp_lexer_consume_token (parser->lexer);
22583      /* And remember that we saw it.  */
22584      ellipsis_p = true;
22585    }
22586  else
22587    ellipsis_p = false;
22588
22589  /* Finish the parameter list.  */
22590  if (!ellipsis_p)
22591    parameters = chainon (parameters, void_list_node);
22592
22593  return parameters;
22594}
22595
22596/* Parse a parameter-declaration-list.
22597
22598   parameter-declaration-list:
22599     parameter-declaration
22600     parameter-declaration-list , parameter-declaration
22601
22602   The parser flags FLAGS is used to control type-specifier parsing.
22603
22604   Returns a representation of the parameter-declaration-list, as for
22605   cp_parser_parameter_declaration_clause.  However, the
22606   `void_list_node' is never appended to the list.  */
22607
22608static tree
22609cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22610{
22611  tree parameters = NULL_TREE;
22612  tree *tail = &parameters;
22613  bool saved_in_unbraced_linkage_specification_p;
22614  int index = 0;
22615
22616  /* The special considerations that apply to a function within an
22617     unbraced linkage specifications do not apply to the parameters
22618     to the function.  */
22619  saved_in_unbraced_linkage_specification_p
22620    = parser->in_unbraced_linkage_specification_p;
22621  parser->in_unbraced_linkage_specification_p = false;
22622
22623  /* Look for more parameters.  */
22624  while (true)
22625    {
22626      cp_parameter_declarator *parameter;
22627      tree decl = error_mark_node;
22628      bool parenthesized_p = false;
22629
22630      /* Parse the parameter.  */
22631      parameter
22632	= cp_parser_parameter_declaration (parser, flags,
22633					   /*template_parm_p=*/false,
22634					   &parenthesized_p);
22635
22636      /* We don't know yet if the enclosing context is deprecated, so wait
22637	 and warn in grokparms if appropriate.  */
22638      deprecated_state = DEPRECATED_SUPPRESS;
22639
22640      if (parameter)
22641	{
22642	  decl = grokdeclarator (parameter->declarator,
22643				 &parameter->decl_specifiers,
22644				 PARM,
22645				 parameter->default_argument != NULL_TREE,
22646				 &parameter->decl_specifiers.attributes);
22647	  if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22648	    DECL_SOURCE_LOCATION (decl) = parameter->loc;
22649	}
22650
22651      deprecated_state = DEPRECATED_NORMAL;
22652
22653      /* If a parse error occurred parsing the parameter declaration,
22654	 then the entire parameter-declaration-list is erroneous.  */
22655      if (decl == error_mark_node)
22656	{
22657	  parameters = error_mark_node;
22658	  break;
22659	}
22660
22661      if (parameter->decl_specifiers.attributes)
22662	cplus_decl_attributes (&decl,
22663			       parameter->decl_specifiers.attributes,
22664			       0);
22665      if (DECL_NAME (decl))
22666	decl = pushdecl (decl);
22667
22668      if (decl != error_mark_node)
22669	{
22670	  retrofit_lang_decl (decl);
22671	  DECL_PARM_INDEX (decl) = ++index;
22672	  DECL_PARM_LEVEL (decl) = function_parm_depth ();
22673	}
22674
22675      /* Add the new parameter to the list.  */
22676      *tail = build_tree_list (parameter->default_argument, decl);
22677      tail = &TREE_CHAIN (*tail);
22678
22679      /* Peek at the next token.  */
22680      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22681	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22682	  /* These are for Objective-C++ */
22683	  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22684	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22685	/* The parameter-declaration-list is complete.  */
22686	break;
22687      else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22688	{
22689	  cp_token *token;
22690
22691	  /* Peek at the next token.  */
22692	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
22693	  /* If it's an ellipsis, then the list is complete.  */
22694	  if (token->type == CPP_ELLIPSIS)
22695	    break;
22696	  /* Otherwise, there must be more parameters.  Consume the
22697	     `,'.  */
22698	  cp_lexer_consume_token (parser->lexer);
22699	  /* When parsing something like:
22700
22701		int i(float f, double d)
22702
22703	     we can tell after seeing the declaration for "f" that we
22704	     are not looking at an initialization of a variable "i",
22705	     but rather at the declaration of a function "i".
22706
22707	     Due to the fact that the parsing of template arguments
22708	     (as specified to a template-id) requires backtracking we
22709	     cannot use this technique when inside a template argument
22710	     list.  */
22711	  if (!parser->in_template_argument_list_p
22712	      && !parser->in_type_id_in_expr_p
22713	      && cp_parser_uncommitted_to_tentative_parse_p (parser)
22714	      /* However, a parameter-declaration of the form
22715		 "float(f)" (which is a valid declaration of a
22716		 parameter "f") can also be interpreted as an
22717		 expression (the conversion of "f" to "float").  */
22718	      && !parenthesized_p)
22719	    cp_parser_commit_to_tentative_parse (parser);
22720	}
22721      else
22722	{
22723	  cp_parser_error (parser, "expected %<,%> or %<...%>");
22724	  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22725	    cp_parser_skip_to_closing_parenthesis (parser,
22726						   /*recovering=*/true,
22727						   /*or_comma=*/false,
22728						   /*consume_paren=*/false);
22729	  break;
22730	}
22731    }
22732
22733  parser->in_unbraced_linkage_specification_p
22734    = saved_in_unbraced_linkage_specification_p;
22735
22736  /* Reset implicit_template_scope if we are about to leave the function
22737     parameter list that introduced it.  Note that for out-of-line member
22738     definitions, there will be one or more class scopes before we get to
22739     the template parameter scope.  */
22740
22741  if (cp_binding_level *its = parser->implicit_template_scope)
22742    if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22743      {
22744	while (maybe_its->kind == sk_class)
22745	  maybe_its = maybe_its->level_chain;
22746	if (maybe_its == its)
22747	  {
22748	    parser->implicit_template_parms = 0;
22749	    parser->implicit_template_scope = 0;
22750	  }
22751      }
22752
22753  return parameters;
22754}
22755
22756/* Parse a parameter declaration.
22757
22758   parameter-declaration:
22759     decl-specifier-seq ... [opt] declarator
22760     decl-specifier-seq declarator = assignment-expression
22761     decl-specifier-seq ... [opt] abstract-declarator [opt]
22762     decl-specifier-seq abstract-declarator [opt] = assignment-expression
22763
22764   The parser flags FLAGS is used to control type-specifier parsing.
22765
22766   If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22767   declares a template parameter.  (In that case, a non-nested `>'
22768   token encountered during the parsing of the assignment-expression
22769   is not interpreted as a greater-than operator.)
22770
22771   Returns a representation of the parameter, or NULL if an error
22772   occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22773   true iff the declarator is of the form "(p)".  */
22774
22775static cp_parameter_declarator *
22776cp_parser_parameter_declaration (cp_parser *parser,
22777				 cp_parser_flags flags,
22778				 bool template_parm_p,
22779				 bool *parenthesized_p)
22780{
22781  int declares_class_or_enum;
22782  cp_decl_specifier_seq decl_specifiers;
22783  cp_declarator *declarator;
22784  tree default_argument;
22785  cp_token *token = NULL, *declarator_token_start = NULL;
22786  const char *saved_message;
22787  bool template_parameter_pack_p = false;
22788
22789  /* In a template parameter, `>' is not an operator.
22790
22791     [temp.param]
22792
22793     When parsing a default template-argument for a non-type
22794     template-parameter, the first non-nested `>' is taken as the end
22795     of the template parameter-list rather than a greater-than
22796     operator.  */
22797
22798  /* Type definitions may not appear in parameter types.  */
22799  saved_message = parser->type_definition_forbidden_message;
22800  parser->type_definition_forbidden_message
22801    = G_("types may not be defined in parameter types");
22802
22803  int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22804			   TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22805					    (current_template_parms)) : 0);
22806
22807  /* Parse the declaration-specifiers.  */
22808  cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22809  cp_parser_decl_specifier_seq (parser,
22810				flags,
22811				&decl_specifiers,
22812				&declares_class_or_enum);
22813
22814  /* Complain about missing 'typename' or other invalid type names.  */
22815  if (!decl_specifiers.any_type_specifiers_p
22816      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22817    decl_specifiers.type = error_mark_node;
22818
22819  /* If an error occurred, there's no reason to attempt to parse the
22820     rest of the declaration.  */
22821  if (cp_parser_error_occurred (parser))
22822    {
22823      parser->type_definition_forbidden_message = saved_message;
22824      return NULL;
22825    }
22826
22827  /* Peek at the next token.  */
22828  token = cp_lexer_peek_token (parser->lexer);
22829
22830  /* If the next token is a `)', `,', `=', `>', or `...', then there
22831     is no declarator. However, when variadic templates are enabled,
22832     there may be a declarator following `...'.  */
22833  if (token->type == CPP_CLOSE_PAREN
22834      || token->type == CPP_COMMA
22835      || token->type == CPP_EQ
22836      || token->type == CPP_GREATER)
22837    {
22838      declarator = NULL;
22839      if (parenthesized_p)
22840	*parenthesized_p = false;
22841    }
22842  /* Otherwise, there should be a declarator.  */
22843  else
22844    {
22845      bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22846      parser->default_arg_ok_p = false;
22847
22848      /* After seeing a decl-specifier-seq, if the next token is not a
22849	 "(", there is no possibility that the code is a valid
22850	 expression.  Therefore, if parsing tentatively, we commit at
22851	 this point.  */
22852      if (!parser->in_template_argument_list_p
22853	  /* In an expression context, having seen:
22854
22855	       (int((char ...
22856
22857	     we cannot be sure whether we are looking at a
22858	     function-type (taking a "char" as a parameter) or a cast
22859	     of some object of type "char" to "int".  */
22860	  && !parser->in_type_id_in_expr_p
22861	  && cp_parser_uncommitted_to_tentative_parse_p (parser)
22862	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22863	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22864	cp_parser_commit_to_tentative_parse (parser);
22865      /* Parse the declarator.  */
22866      declarator_token_start = token;
22867      declarator = cp_parser_declarator (parser,
22868					 CP_PARSER_DECLARATOR_EITHER,
22869					 CP_PARSER_FLAGS_NONE,
22870					 /*ctor_dtor_or_conv_p=*/NULL,
22871					 parenthesized_p,
22872					 /*member_p=*/false,
22873					 /*friend_p=*/false,
22874					 /*static_p=*/false);
22875      parser->default_arg_ok_p = saved_default_arg_ok_p;
22876      /* After the declarator, allow more attributes.  */
22877      decl_specifiers.attributes
22878	= attr_chainon (decl_specifiers.attributes,
22879			cp_parser_attributes_opt (parser));
22880
22881      /* If the declarator is a template parameter pack, remember that and
22882	 clear the flag in the declarator itself so we don't get errors
22883	 from grokdeclarator.  */
22884      if (template_parm_p && declarator && declarator->parameter_pack_p)
22885	{
22886	  declarator->parameter_pack_p = false;
22887	  template_parameter_pack_p = true;
22888	}
22889    }
22890
22891  /* If the next token is an ellipsis, and we have not seen a declarator
22892     name, and if either the type of the declarator contains parameter
22893     packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22894     for, eg, abbreviated integral type names), then we actually have a
22895     parameter pack expansion expression. Otherwise, leave the ellipsis
22896     for a C-style variadic function. */
22897  token = cp_lexer_peek_token (parser->lexer);
22898
22899  /* If a function parameter pack was specified and an implicit template
22900     parameter was introduced during cp_parser_parameter_declaration,
22901     change any implicit parameters introduced into packs.  */
22902  if (parser->implicit_template_parms
22903      && ((token->type == CPP_ELLIPSIS
22904	   && declarator_can_be_parameter_pack (declarator))
22905	  || (declarator && declarator->parameter_pack_p)))
22906    {
22907      int latest_template_parm_idx = TREE_VEC_LENGTH
22908	(INNERMOST_TEMPLATE_PARMS (current_template_parms));
22909
22910      if (latest_template_parm_idx != template_parm_idx)
22911	decl_specifiers.type = convert_generic_types_to_packs
22912	  (decl_specifiers.type,
22913	   template_parm_idx, latest_template_parm_idx);
22914    }
22915
22916  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22917    {
22918      tree type = decl_specifiers.type;
22919
22920      if (type && DECL_P (type))
22921        type = TREE_TYPE (type);
22922
22923      if (((type
22924	    && TREE_CODE (type) != TYPE_PACK_EXPANSION
22925	    && (template_parm_p || uses_parameter_packs (type)))
22926	   || (!type && template_parm_p))
22927	  && declarator_can_be_parameter_pack (declarator))
22928	{
22929	  /* Consume the `...'. */
22930	  cp_lexer_consume_token (parser->lexer);
22931	  maybe_warn_variadic_templates ();
22932
22933	  /* Build a pack expansion type */
22934	  if (template_parm_p)
22935	    template_parameter_pack_p = true;
22936	  else if (declarator)
22937	    declarator->parameter_pack_p = true;
22938	  else
22939	    decl_specifiers.type = make_pack_expansion (type);
22940	}
22941    }
22942
22943  /* The restriction on defining new types applies only to the type
22944     of the parameter, not to the default argument.  */
22945  parser->type_definition_forbidden_message = saved_message;
22946
22947  /* If the next token is `=', then process a default argument.  */
22948  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22949    {
22950      tree type = decl_specifiers.type;
22951      token = cp_lexer_peek_token (parser->lexer);
22952      /* If we are defining a class, then the tokens that make up the
22953	 default argument must be saved and processed later.  */
22954      if (!template_parm_p && at_class_scope_p ()
22955	  && TYPE_BEING_DEFINED (current_class_type)
22956	  && !LAMBDA_TYPE_P (current_class_type))
22957	default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22958
22959      /* A constrained-type-specifier may declare a type
22960	 template-parameter.  */
22961      else if (declares_constrained_type_template_parameter (type))
22962        default_argument
22963          = cp_parser_default_type_template_argument (parser);
22964
22965      /* A constrained-type-specifier may declare a
22966	 template-template-parameter.  */
22967      else if (declares_constrained_template_template_parameter (type))
22968        default_argument
22969          = cp_parser_default_template_template_argument (parser);
22970
22971      /* Outside of a class definition, we can just parse the
22972	 assignment-expression.  */
22973      else
22974	default_argument
22975	  = cp_parser_default_argument (parser, template_parm_p);
22976
22977      if (!parser->default_arg_ok_p)
22978	{
22979	  permerror (token->location,
22980		     "default arguments are only "
22981		     "permitted for function parameters");
22982	}
22983      else if ((declarator && declarator->parameter_pack_p)
22984	       || template_parameter_pack_p
22985	       || (decl_specifiers.type
22986		   && PACK_EXPANSION_P (decl_specifiers.type)))
22987	{
22988	  /* Find the name of the parameter pack.  */
22989	  cp_declarator *id_declarator = declarator;
22990	  while (id_declarator && id_declarator->kind != cdk_id)
22991	    id_declarator = id_declarator->declarator;
22992
22993	  if (id_declarator && id_declarator->kind == cdk_id)
22994	    error_at (declarator_token_start->location,
22995		      template_parm_p
22996		      ? G_("template parameter pack %qD "
22997			   "cannot have a default argument")
22998		      : G_("parameter pack %qD cannot have "
22999			   "a default argument"),
23000		      id_declarator->u.id.unqualified_name);
23001	  else
23002	    error_at (declarator_token_start->location,
23003		      template_parm_p
23004		      ? G_("template parameter pack cannot have "
23005			   "a default argument")
23006		      : G_("parameter pack cannot have a "
23007			   "default argument"));
23008
23009	  default_argument = NULL_TREE;
23010	}
23011    }
23012  else
23013    default_argument = NULL_TREE;
23014
23015  if (default_argument)
23016    STRIP_ANY_LOCATION_WRAPPER (default_argument);
23017
23018  /* Generate a location for the parameter, ranging from the start of the
23019     initial token to the end of the final token (using input_location for
23020     the latter, set up by cp_lexer_set_source_position_from_token when
23021     consuming tokens).
23022
23023     If we have a identifier, then use it for the caret location, e.g.
23024
23025       extern int callee (int one, int (*two)(int, int), float three);
23026                                   ~~~~~~^~~~~~~~~~~~~~
23027
23028     otherwise, reuse the start location for the caret location e.g.:
23029
23030       extern int callee (int one, int (*)(int, int), float three);
23031                                   ^~~~~~~~~~~~~~~~~
23032
23033  */
23034  location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
23035			  ? declarator->id_loc
23036			  : decl_spec_token_start->location);
23037  location_t param_loc = make_location (caret_loc,
23038					decl_spec_token_start->location,
23039					input_location);
23040
23041  return make_parameter_declarator (&decl_specifiers,
23042				    declarator,
23043				    default_argument,
23044				    param_loc,
23045				    template_parameter_pack_p);
23046}
23047
23048/* Parse a default argument and return it.
23049
23050   TEMPLATE_PARM_P is true if this is a default argument for a
23051   non-type template parameter.  */
23052static tree
23053cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
23054{
23055  tree default_argument = NULL_TREE;
23056  bool saved_greater_than_is_operator_p;
23057  unsigned char saved_local_variables_forbidden_p;
23058  bool non_constant_p, is_direct_init;
23059
23060  /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
23061     set correctly.  */
23062  saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
23063  parser->greater_than_is_operator_p = !template_parm_p;
23064  /* Local variable names (and the `this' keyword) may not
23065     appear in a default argument.  */
23066  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23067  parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
23068  /* Parse the assignment-expression.  */
23069  if (template_parm_p)
23070    push_deferring_access_checks (dk_no_deferred);
23071  tree saved_class_ptr = NULL_TREE;
23072  tree saved_class_ref = NULL_TREE;
23073  /* The "this" pointer is not valid in a default argument.  */
23074  if (cfun)
23075    {
23076      saved_class_ptr = current_class_ptr;
23077      cp_function_chain->x_current_class_ptr = NULL_TREE;
23078      saved_class_ref = current_class_ref;
23079      cp_function_chain->x_current_class_ref = NULL_TREE;
23080    }
23081  default_argument
23082    = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
23083  /* Restore the "this" pointer.  */
23084  if (cfun)
23085    {
23086      cp_function_chain->x_current_class_ptr = saved_class_ptr;
23087      cp_function_chain->x_current_class_ref = saved_class_ref;
23088    }
23089  if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
23090    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23091  if (template_parm_p)
23092    pop_deferring_access_checks ();
23093  parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
23094  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23095
23096  return default_argument;
23097}
23098
23099/* Parse a function-body.
23100
23101   function-body:
23102     compound_statement  */
23103
23104static void
23105cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
23106{
23107  cp_parser_compound_statement (parser, NULL, (in_function_try_block
23108					       ? BCS_TRY_BLOCK : BCS_NORMAL),
23109				true);
23110}
23111
23112/* Parse a ctor-initializer-opt followed by a function-body.  Return
23113   true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
23114   is true we are parsing a function-try-block.  */
23115
23116static void
23117cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
23118						  bool in_function_try_block)
23119{
23120  tree body, list;
23121  const bool check_body_p
23122     = (DECL_CONSTRUCTOR_P (current_function_decl)
23123	&& DECL_DECLARED_CONSTEXPR_P (current_function_decl));
23124  tree last = NULL;
23125
23126  if (in_function_try_block
23127      && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
23128      && cxx_dialect < cxx2a)
23129    {
23130      if (DECL_CONSTRUCTOR_P (current_function_decl))
23131	pedwarn (input_location, 0,
23132		 "function-try-block body of %<constexpr%> constructor only "
23133		 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
23134      else
23135	pedwarn (input_location, 0,
23136		 "function-try-block body of %<constexpr%> function only "
23137		 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
23138    }
23139
23140  /* Begin the function body.  */
23141  body = begin_function_body ();
23142  /* Parse the optional ctor-initializer.  */
23143  cp_parser_ctor_initializer_opt (parser);
23144
23145  /* If we're parsing a constexpr constructor definition, we need
23146     to check that the constructor body is indeed empty.  However,
23147     before we get to cp_parser_function_body lot of junk has been
23148     generated, so we can't just check that we have an empty block.
23149     Rather we take a snapshot of the outermost block, and check whether
23150     cp_parser_function_body changed its state.  */
23151  if (check_body_p)
23152    {
23153      list = cur_stmt_list;
23154      if (STATEMENT_LIST_TAIL (list))
23155	last = STATEMENT_LIST_TAIL (list)->stmt;
23156    }
23157  /* Parse the function-body.  */
23158  cp_parser_function_body (parser, in_function_try_block);
23159  if (check_body_p)
23160    check_constexpr_ctor_body (last, list, /*complain=*/true);
23161  /* Finish the function body.  */
23162  finish_function_body (body);
23163}
23164
23165/* Parse an initializer.
23166
23167   initializer:
23168     = initializer-clause
23169     ( expression-list )
23170
23171   Returns an expression representing the initializer.  If no
23172   initializer is present, NULL_TREE is returned.
23173
23174   *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
23175   production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
23176   set to TRUE if there is no initializer present.  If there is an
23177   initializer, and it is not a constant-expression, *NON_CONSTANT_P
23178   is set to true; otherwise it is set to false.  */
23179
23180static tree
23181cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
23182		       bool* non_constant_p, bool subexpression_p)
23183{
23184  cp_token *token;
23185  tree init;
23186
23187  /* Peek at the next token.  */
23188  token = cp_lexer_peek_token (parser->lexer);
23189
23190  /* Let our caller know whether or not this initializer was
23191     parenthesized.  */
23192  *is_direct_init = (token->type != CPP_EQ);
23193  /* Assume that the initializer is constant.  */
23194  *non_constant_p = false;
23195
23196  if (token->type == CPP_EQ)
23197    {
23198      /* Consume the `='.  */
23199      cp_lexer_consume_token (parser->lexer);
23200      /* Parse the initializer-clause.  */
23201      init = cp_parser_initializer_clause (parser, non_constant_p);
23202    }
23203  else if (token->type == CPP_OPEN_PAREN)
23204    {
23205      vec<tree, va_gc> *vec;
23206      vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23207						     /*cast_p=*/false,
23208						     /*allow_expansion_p=*/true,
23209						     non_constant_p);
23210      if (vec == NULL)
23211	return error_mark_node;
23212      init = build_tree_list_vec (vec);
23213      release_tree_vector (vec);
23214    }
23215  else if (token->type == CPP_OPEN_BRACE)
23216    {
23217      cp_lexer_set_source_position (parser->lexer);
23218      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23219      init = cp_parser_braced_list (parser, non_constant_p);
23220      CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
23221    }
23222  else
23223    {
23224      /* Anything else is an error.  */
23225      cp_parser_error (parser, "expected initializer");
23226      init = error_mark_node;
23227    }
23228
23229  if (!subexpression_p && check_for_bare_parameter_packs (init))
23230    init = error_mark_node;
23231
23232  return init;
23233}
23234
23235/* Parse an initializer-clause.
23236
23237   initializer-clause:
23238     assignment-expression
23239     braced-init-list
23240
23241   Returns an expression representing the initializer.
23242
23243   If the `assignment-expression' production is used the value
23244   returned is simply a representation for the expression.
23245
23246   Otherwise, calls cp_parser_braced_list.  */
23247
23248static cp_expr
23249cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
23250{
23251  cp_expr initializer;
23252
23253  /* Assume the expression is constant.  */
23254  *non_constant_p = false;
23255
23256  /* If it is not a `{', then we are looking at an
23257     assignment-expression.  */
23258  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23259    {
23260      initializer
23261	= cp_parser_constant_expression (parser,
23262					/*allow_non_constant_p=*/true,
23263					non_constant_p);
23264    }
23265  else
23266    initializer = cp_parser_braced_list (parser, non_constant_p);
23267
23268  return initializer;
23269}
23270
23271/* Parse a brace-enclosed initializer list.
23272
23273   braced-init-list:
23274     { initializer-list , [opt] }
23275     { designated-initializer-list , [opt] }
23276     { }
23277
23278   Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
23279   the elements of the initializer-list (or NULL, if the last
23280   production is used).  The TREE_TYPE for the CONSTRUCTOR will be
23281   NULL_TREE.  There is no way to detect whether or not the optional
23282   trailing `,' was provided.  NON_CONSTANT_P is as for
23283   cp_parser_initializer.  */
23284
23285static cp_expr
23286cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
23287{
23288  tree initializer;
23289  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
23290
23291  /* Consume the `{' token.  */
23292  matching_braces braces;
23293  braces.require_open (parser);
23294  /* Create a CONSTRUCTOR to represent the braced-initializer.  */
23295  initializer = make_node (CONSTRUCTOR);
23296  /* If it's not a `}', then there is a non-trivial initializer.  */
23297  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
23298    {
23299      bool designated;
23300      /* Parse the initializer list.  */
23301      CONSTRUCTOR_ELTS (initializer)
23302	= cp_parser_initializer_list (parser, non_constant_p, &designated);
23303      CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
23304      /* A trailing `,' token is allowed.  */
23305      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23306	cp_lexer_consume_token (parser->lexer);
23307    }
23308  else
23309    *non_constant_p = false;
23310  /* Now, there should be a trailing `}'.  */
23311  location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
23312  braces.require_close (parser);
23313  TREE_TYPE (initializer) = init_list_type_node;
23314
23315  cp_expr result (initializer);
23316  /* Build a location of the form:
23317       { ... }
23318       ^~~~~~~
23319     with caret==start at the open brace, finish at the close brace.  */
23320  location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
23321  result.set_location (combined_loc);
23322  return result;
23323}
23324
23325/* Consume tokens up to, and including, the next non-nested closing `]'.
23326   Returns true iff we found a closing `]'.  */
23327
23328static bool
23329cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
23330{
23331  unsigned square_depth = 0;
23332
23333  while (true)
23334    {
23335      cp_token * token = cp_lexer_peek_token (parser->lexer);
23336
23337      switch (token->type)
23338	{
23339	case CPP_PRAGMA_EOL:
23340	  if (!parser->lexer->in_pragma)
23341	    break;
23342	  /* FALLTHRU */
23343	case CPP_EOF:
23344	  /* If we've run out of tokens, then there is no closing `]'.  */
23345	  return false;
23346
23347        case CPP_OPEN_SQUARE:
23348          ++square_depth;
23349          break;
23350
23351        case CPP_CLOSE_SQUARE:
23352	  if (!square_depth--)
23353	    {
23354	      cp_lexer_consume_token (parser->lexer);
23355	      return true;
23356	    }
23357	  break;
23358
23359	default:
23360	  break;
23361	}
23362
23363      /* Consume the token.  */
23364      cp_lexer_consume_token (parser->lexer);
23365    }
23366}
23367
23368/* Return true if we are looking at an array-designator, false otherwise.  */
23369
23370static bool
23371cp_parser_array_designator_p (cp_parser *parser)
23372{
23373  /* Consume the `['.  */
23374  cp_lexer_consume_token (parser->lexer);
23375
23376  cp_lexer_save_tokens (parser->lexer);
23377
23378  /* Skip tokens until the next token is a closing square bracket.
23379     If we find the closing `]', and the next token is a `=', then
23380     we are looking at an array designator.  */
23381  bool array_designator_p
23382    = (cp_parser_skip_to_closing_square_bracket (parser)
23383       && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
23384
23385  /* Roll back the tokens we skipped.  */
23386  cp_lexer_rollback_tokens (parser->lexer);
23387
23388  return array_designator_p;
23389}
23390
23391/* Parse an initializer-list.
23392
23393   initializer-list:
23394     initializer-clause ... [opt]
23395     initializer-list , initializer-clause ... [opt]
23396
23397   C++2A Extension:
23398
23399   designated-initializer-list:
23400     designated-initializer-clause
23401     designated-initializer-list , designated-initializer-clause
23402
23403   designated-initializer-clause:
23404     designator brace-or-equal-initializer
23405
23406   designator:
23407     . identifier
23408
23409   GNU Extension:
23410
23411   initializer-list:
23412     designation initializer-clause ...[opt]
23413     initializer-list , designation initializer-clause ...[opt]
23414
23415   designation:
23416     . identifier =
23417     identifier :
23418     [ constant-expression ] =
23419
23420   Returns a vec of constructor_elt.  The VALUE of each elt is an expression
23421   for the initializer.  If the INDEX of the elt is non-NULL, it is the
23422   IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
23423   as for cp_parser_initializer.  Set *DESIGNATED to a boolean whether there
23424   are any designators.  */
23425
23426static vec<constructor_elt, va_gc> *
23427cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
23428			    bool *designated)
23429{
23430  vec<constructor_elt, va_gc> *v = NULL;
23431  bool first_p = true;
23432  tree first_designator = NULL_TREE;
23433
23434  /* Assume all of the expressions are constant.  */
23435  *non_constant_p = false;
23436
23437  unsigned nelts = 0;
23438  int suppress = suppress_location_wrappers;
23439
23440  /* Parse the rest of the list.  */
23441  while (true)
23442    {
23443      cp_token *token;
23444      tree designator;
23445      tree initializer;
23446      bool clause_non_constant_p;
23447      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23448
23449      /* Handle the C++2A syntax, '. id ='.  */
23450      if ((cxx_dialect >= cxx2a
23451	   || cp_parser_allow_gnu_extensions_p (parser))
23452	  && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
23453	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
23454	  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
23455	      || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
23456		  == CPP_OPEN_BRACE)))
23457	{
23458	  if (cxx_dialect < cxx2a)
23459	    pedwarn (loc, OPT_Wpedantic,
23460		     "C++ designated initializers only available with "
23461		     "%<-std=c++2a%> or %<-std=gnu++2a%>");
23462	  /* Consume the `.'.  */
23463	  cp_lexer_consume_token (parser->lexer);
23464	  /* Consume the identifier.  */
23465	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
23466	  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23467	    /* Consume the `='.  */
23468	    cp_lexer_consume_token (parser->lexer);
23469	}
23470      /* Also, if the next token is an identifier and the following one is a
23471	 colon, we are looking at the GNU designated-initializer
23472	 syntax.  */
23473      else if (cp_parser_allow_gnu_extensions_p (parser)
23474	       && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23475	       && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23476		   == CPP_COLON))
23477	{
23478	  /* Warn the user that they are using an extension.  */
23479	  pedwarn (loc, OPT_Wpedantic,
23480		   "ISO C++ does not allow GNU designated initializers");
23481	  /* Consume the identifier.  */
23482	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
23483	  /* Consume the `:'.  */
23484	  cp_lexer_consume_token (parser->lexer);
23485	}
23486      /* Also handle C99 array designators, '[ const ] ='.  */
23487      else if (cp_parser_allow_gnu_extensions_p (parser)
23488	       && !c_dialect_objc ()
23489	       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23490	{
23491	  /* In C++11, [ could start a lambda-introducer.  */
23492	  bool non_const = false;
23493
23494	  cp_parser_parse_tentatively (parser);
23495
23496	  if (!cp_parser_array_designator_p (parser))
23497	    {
23498	      cp_parser_simulate_error (parser);
23499	      designator = NULL_TREE;
23500	    }
23501	  else
23502	    {
23503	      designator = cp_parser_constant_expression (parser, true,
23504							  &non_const);
23505	      cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23506	      cp_parser_require (parser, CPP_EQ, RT_EQ);
23507	    }
23508
23509	  if (!cp_parser_parse_definitely (parser))
23510	    designator = NULL_TREE;
23511	  else if (non_const
23512		   && (!require_potential_rvalue_constant_expression
23513		       (designator)))
23514	    designator = NULL_TREE;
23515	  if (designator)
23516	    /* Warn the user that they are using an extension.  */
23517	    pedwarn (loc, OPT_Wpedantic,
23518		     "ISO C++ does not allow C99 designated initializers");
23519	}
23520      else
23521	designator = NULL_TREE;
23522
23523      if (first_p)
23524	{
23525	  first_designator = designator;
23526	  first_p = false;
23527	}
23528      else if (cxx_dialect >= cxx2a
23529	       && first_designator != error_mark_node
23530	       && (!first_designator != !designator))
23531	{
23532	  error_at (loc, "either all initializer clauses should be designated "
23533			 "or none of them should be");
23534	  first_designator = error_mark_node;
23535	}
23536      else if (cxx_dialect < cxx2a && !first_designator)
23537	first_designator = designator;
23538
23539      /* Parse the initializer.  */
23540      initializer = cp_parser_initializer_clause (parser,
23541						  &clause_non_constant_p);
23542      /* If any clause is non-constant, so is the entire initializer.  */
23543      if (clause_non_constant_p)
23544	*non_constant_p = true;
23545
23546      /* If we have an ellipsis, this is an initializer pack
23547	 expansion.  */
23548      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23549        {
23550	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23551
23552          /* Consume the `...'.  */
23553          cp_lexer_consume_token (parser->lexer);
23554
23555	  if (designator && cxx_dialect >= cxx2a)
23556	    error_at (loc,
23557		      "%<...%> not allowed in designated initializer list");
23558
23559	  /* Turn the initializer into an initializer expansion.  */
23560	  initializer = make_pack_expansion (initializer);
23561        }
23562
23563      /* Add it to the vector.  */
23564      CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23565
23566      /* If the next token is not a comma, we have reached the end of
23567	 the list.  */
23568      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23569	break;
23570
23571      /* Peek at the next token.  */
23572      token = cp_lexer_peek_nth_token (parser->lexer, 2);
23573      /* If the next token is a `}', then we're still done.  An
23574	 initializer-clause can have a trailing `,' after the
23575	 initializer-list and before the closing `}'.  */
23576      if (token->type == CPP_CLOSE_BRACE)
23577	break;
23578
23579      /* Suppress location wrappers in a long initializer to save memory
23580	 (14179).  The cutoff is chosen arbitrarily.  */
23581      const unsigned loc_max = 256;
23582      unsigned incr = 1;
23583      if (TREE_CODE (initializer) == CONSTRUCTOR)
23584	/* Look one level down because it's easy.  Looking deeper would require
23585	   passing down a nelts pointer, and I don't think multi-level massive
23586	   initializers are common enough to justify this.  */
23587	incr = CONSTRUCTOR_NELTS (initializer);
23588      nelts += incr;
23589      if (nelts >= loc_max && (nelts - incr) < loc_max)
23590	++suppress_location_wrappers;
23591
23592      /* Consume the `,' token.  */
23593      cp_lexer_consume_token (parser->lexer);
23594    }
23595
23596  /* The same identifier shall not appear in multiple designators
23597     of a designated-initializer-list.  */
23598  if (first_designator)
23599    {
23600      unsigned int i;
23601      tree designator, val;
23602      FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23603	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23604	  {
23605	    if (IDENTIFIER_MARKED (designator))
23606	      {
23607		error_at (cp_expr_loc_or_input_loc (val),
23608			  "%<.%s%> designator used multiple times in "
23609			  "the same initializer list",
23610			  IDENTIFIER_POINTER (designator));
23611		(*v)[i].index = error_mark_node;
23612	      }
23613	    else
23614	      IDENTIFIER_MARKED (designator) = 1;
23615	  }
23616      FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23617	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23618	  IDENTIFIER_MARKED (designator) = 0;
23619    }
23620
23621  suppress_location_wrappers = suppress;
23622
23623  *designated = first_designator != NULL_TREE;
23624  return v;
23625}
23626
23627/* Classes [gram.class] */
23628
23629/* Parse a class-name.
23630
23631   class-name:
23632     identifier
23633     template-id
23634
23635   TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23636   to indicate that names looked up in dependent types should be
23637   assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
23638   keyword has been used to indicate that the name that appears next
23639   is a template.  TAG_TYPE indicates the explicit tag given before
23640   the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
23641   looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
23642   is the class being defined in a class-head.  If ENUM_OK is TRUE,
23643   enum-names are also accepted.
23644
23645   Returns the TYPE_DECL representing the class.  */
23646
23647static tree
23648cp_parser_class_name (cp_parser *parser,
23649		      bool typename_keyword_p,
23650		      bool template_keyword_p,
23651		      enum tag_types tag_type,
23652		      bool check_dependency_p,
23653		      bool class_head_p,
23654		      bool is_declaration,
23655		      bool enum_ok)
23656{
23657  tree decl;
23658  tree scope;
23659  bool typename_p;
23660  cp_token *token;
23661  tree identifier = NULL_TREE;
23662
23663  /* All class-names start with an identifier.  */
23664  token = cp_lexer_peek_token (parser->lexer);
23665  if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23666    {
23667      cp_parser_error (parser, "expected class-name");
23668      return error_mark_node;
23669    }
23670
23671  /* PARSER->SCOPE can be cleared when parsing the template-arguments
23672     to a template-id, so we save it here.  */
23673  scope = parser->scope;
23674  if (scope == error_mark_node)
23675    return error_mark_node;
23676
23677  /* Any name names a type if we're following the `typename' keyword
23678     in a qualified name where the enclosing scope is type-dependent.  */
23679  typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23680		&& dependent_type_p (scope));
23681  /* Handle the common case (an identifier, but not a template-id)
23682     efficiently.  */
23683  if (token->type == CPP_NAME
23684      && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23685    {
23686      cp_token *identifier_token;
23687      bool ambiguous_p;
23688
23689      /* Look for the identifier.  */
23690      identifier_token = cp_lexer_peek_token (parser->lexer);
23691      ambiguous_p = identifier_token->error_reported;
23692      identifier = cp_parser_identifier (parser);
23693      /* If the next token isn't an identifier, we are certainly not
23694	 looking at a class-name.  */
23695      if (identifier == error_mark_node)
23696	decl = error_mark_node;
23697      /* If we know this is a type-name, there's no need to look it
23698	 up.  */
23699      else if (typename_p)
23700	decl = identifier;
23701      else
23702	{
23703	  tree ambiguous_decls;
23704	  /* If we already know that this lookup is ambiguous, then
23705	     we've already issued an error message; there's no reason
23706	     to check again.  */
23707	  if (ambiguous_p)
23708	    {
23709	      cp_parser_simulate_error (parser);
23710	      return error_mark_node;
23711	    }
23712	  /* If the next token is a `::', then the name must be a type
23713	     name.
23714
23715	     [basic.lookup.qual]
23716
23717	     During the lookup for a name preceding the :: scope
23718	     resolution operator, object, function, and enumerator
23719	     names are ignored.  */
23720	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23721	    tag_type = scope_type;
23722	  /* Look up the name.  */
23723	  decl = cp_parser_lookup_name (parser, identifier,
23724					tag_type,
23725					/*is_template=*/false,
23726					/*is_namespace=*/false,
23727					check_dependency_p,
23728					&ambiguous_decls,
23729					identifier_token->location);
23730	  if (ambiguous_decls)
23731	    {
23732	      if (cp_parser_parsing_tentatively (parser))
23733		cp_parser_simulate_error (parser);
23734	      return error_mark_node;
23735	    }
23736	}
23737    }
23738  else
23739    {
23740      /* Try a template-id.  */
23741      decl = cp_parser_template_id (parser, template_keyword_p,
23742				    check_dependency_p,
23743				    tag_type,
23744				    is_declaration);
23745      if (decl == error_mark_node)
23746	return error_mark_node;
23747    }
23748
23749  decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23750
23751  /* If this is a typename, create a TYPENAME_TYPE.  */
23752  if (typename_p
23753      && decl != error_mark_node
23754      && !is_overloaded_fn (decl))
23755    {
23756      decl = make_typename_type (scope, decl, typename_type,
23757				 /*complain=*/tf_error);
23758      if (decl != error_mark_node)
23759	decl = TYPE_NAME (decl);
23760    }
23761
23762  decl = strip_using_decl (decl);
23763
23764  /* Check to see that it is really the name of a class.  */
23765  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23766      && identifier_p (TREE_OPERAND (decl, 0))
23767      && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23768    /* Situations like this:
23769
23770	 template <typename T> struct A {
23771	   typename T::template X<int>::I i;
23772	 };
23773
23774       are problematic.  Is `T::template X<int>' a class-name?  The
23775       standard does not seem to be definitive, but there is no other
23776       valid interpretation of the following `::'.  Therefore, those
23777       names are considered class-names.  */
23778    {
23779      decl = make_typename_type (scope, decl, tag_type, tf_error);
23780      if (decl != error_mark_node)
23781	decl = TYPE_NAME (decl);
23782    }
23783  else if (TREE_CODE (decl) != TYPE_DECL
23784	   || TREE_TYPE (decl) == error_mark_node
23785	   || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23786		|| (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23787	   /* In Objective-C 2.0, a classname followed by '.' starts a
23788	      dot-syntax expression, and it's not a type-name.  */
23789	   || (c_dialect_objc ()
23790	       && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23791	       && objc_is_class_name (decl)))
23792    decl = error_mark_node;
23793
23794  if (decl == error_mark_node)
23795    cp_parser_error (parser, "expected class-name");
23796  else if (identifier && !parser->scope)
23797    maybe_note_name_used_in_class (identifier, decl);
23798
23799  return decl;
23800}
23801
23802/* Make sure that any member-function parameters are in scope.
23803   For instance, a function's noexcept-specifier can use the function's
23804   parameters:
23805
23806   struct S {
23807     void fn (int p) noexcept(noexcept(p));
23808   };
23809
23810   so we need to make sure name lookup can find them.  This is used
23811   when we delay parsing of the noexcept-specifier.  */
23812
23813static void
23814inject_parm_decls (tree decl)
23815{
23816  begin_scope (sk_function_parms, decl);
23817  tree args = DECL_ARGUMENTS (decl);
23818
23819  do_push_parm_decls (decl, args, /*nonparms=*/NULL);
23820}
23821
23822/* Undo the effects of inject_parm_decls.  */
23823
23824static void
23825pop_injected_parms (void)
23826{
23827  pop_bindings_and_leave_scope ();
23828}
23829
23830/* Parse a class-specifier.
23831
23832   class-specifier:
23833     class-head { member-specification [opt] }
23834
23835   Returns the TREE_TYPE representing the class.  */
23836
23837static tree
23838cp_parser_class_specifier_1 (cp_parser* parser)
23839{
23840  tree type;
23841  tree attributes = NULL_TREE;
23842  bool nested_name_specifier_p;
23843  unsigned saved_num_template_parameter_lists;
23844  bool saved_in_function_body;
23845  unsigned char in_statement;
23846  bool in_switch_statement_p;
23847  bool saved_in_unbraced_linkage_specification_p;
23848  tree old_scope = NULL_TREE;
23849  tree scope = NULL_TREE;
23850  cp_token *closing_brace;
23851
23852  push_deferring_access_checks (dk_no_deferred);
23853
23854  /* Parse the class-head.  */
23855  type = cp_parser_class_head (parser,
23856			       &nested_name_specifier_p);
23857  /* If the class-head was a semantic disaster, skip the entire body
23858     of the class.  */
23859  if (!type)
23860    {
23861      cp_parser_skip_to_end_of_block_or_statement (parser);
23862      pop_deferring_access_checks ();
23863      return error_mark_node;
23864    }
23865
23866  /* Look for the `{'.  */
23867  matching_braces braces;
23868  if (!braces.require_open (parser))
23869    {
23870      pop_deferring_access_checks ();
23871      return error_mark_node;
23872    }
23873
23874  cp_ensure_no_omp_declare_simd (parser);
23875  cp_ensure_no_oacc_routine (parser);
23876
23877  /* Issue an error message if type-definitions are forbidden here.  */
23878  bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23879  /* Remember that we are defining one more class.  */
23880  ++parser->num_classes_being_defined;
23881  /* Inside the class, surrounding template-parameter-lists do not
23882     apply.  */
23883  saved_num_template_parameter_lists
23884    = parser->num_template_parameter_lists;
23885  parser->num_template_parameter_lists = 0;
23886  /* We are not in a function body.  */
23887  saved_in_function_body = parser->in_function_body;
23888  parser->in_function_body = false;
23889  /* Or in a loop.  */
23890  in_statement = parser->in_statement;
23891  parser->in_statement = 0;
23892  /* Or in a switch.  */
23893  in_switch_statement_p = parser->in_switch_statement_p;
23894  parser->in_switch_statement_p = false;
23895  /* We are not immediately inside an extern "lang" block.  */
23896  saved_in_unbraced_linkage_specification_p
23897    = parser->in_unbraced_linkage_specification_p;
23898  parser->in_unbraced_linkage_specification_p = false;
23899
23900  /* Start the class.  */
23901  if (nested_name_specifier_p)
23902    {
23903      scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23904      old_scope = push_inner_scope (scope);
23905    }
23906  type = begin_class_definition (type);
23907
23908  if (type == error_mark_node)
23909    /* If the type is erroneous, skip the entire body of the class.  */
23910    cp_parser_skip_to_closing_brace (parser);
23911  else
23912    /* Parse the member-specification.  */
23913    cp_parser_member_specification_opt (parser);
23914
23915  /* Look for the trailing `}'.  */
23916  closing_brace = braces.require_close (parser);
23917  /* Look for trailing attributes to apply to this class.  */
23918  if (cp_parser_allow_gnu_extensions_p (parser))
23919    attributes = cp_parser_gnu_attributes_opt (parser);
23920  if (type != error_mark_node)
23921    type = finish_struct (type, attributes);
23922  if (nested_name_specifier_p)
23923    pop_inner_scope (old_scope, scope);
23924
23925  /* We've finished a type definition.  Check for the common syntax
23926     error of forgetting a semicolon after the definition.  We need to
23927     be careful, as we can't just check for not-a-semicolon and be done
23928     with it; the user might have typed:
23929
23930     class X { } c = ...;
23931     class X { } *p = ...;
23932
23933     and so forth.  Instead, enumerate all the possible tokens that
23934     might follow this production; if we don't see one of them, then
23935     complain and silently insert the semicolon.  */
23936  {
23937    cp_token *token = cp_lexer_peek_token (parser->lexer);
23938    bool want_semicolon = true;
23939
23940    if (cp_next_tokens_can_be_std_attribute_p (parser))
23941      /* Don't try to parse c++11 attributes here.  As per the
23942	 grammar, that should be a task for
23943	 cp_parser_decl_specifier_seq.  */
23944      want_semicolon = false;
23945
23946    switch (token->type)
23947      {
23948      case CPP_NAME:
23949      case CPP_SEMICOLON:
23950      case CPP_MULT:
23951      case CPP_AND:
23952      case CPP_OPEN_PAREN:
23953      case CPP_CLOSE_PAREN:
23954      case CPP_COMMA:
23955        want_semicolon = false;
23956        break;
23957
23958        /* While it's legal for type qualifiers and storage class
23959           specifiers to follow type definitions in the grammar, only
23960           compiler testsuites contain code like that.  Assume that if
23961           we see such code, then what we're really seeing is a case
23962           like:
23963
23964           class X { }
23965           const <type> var = ...;
23966
23967           or
23968
23969           class Y { }
23970           static <type> func (...) ...
23971
23972           i.e. the qualifier or specifier applies to the next
23973           declaration.  To do so, however, we need to look ahead one
23974           more token to see if *that* token is a type specifier.
23975
23976	   This code could be improved to handle:
23977
23978	   class Z { }
23979	   static const <type> var = ...;  */
23980      case CPP_KEYWORD:
23981	if (keyword_is_decl_specifier (token->keyword))
23982	  {
23983	    cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23984
23985	    /* Handling user-defined types here would be nice, but very
23986	       tricky.  */
23987	    want_semicolon
23988	      = (lookahead->type == CPP_KEYWORD
23989		 && keyword_begins_type_specifier (lookahead->keyword));
23990	  }
23991	break;
23992      default:
23993	break;
23994      }
23995
23996    /* If we don't have a type, then something is very wrong and we
23997       shouldn't try to do anything clever.  Likewise for not seeing the
23998       closing brace.  */
23999    if (closing_brace && TYPE_P (type) && want_semicolon)
24000      {
24001	/* Locate the closing brace.  */
24002	cp_token_position prev
24003	  = cp_lexer_previous_token_position (parser->lexer);
24004	cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
24005	location_t loc = prev_token->location;
24006
24007	/* We want to suggest insertion of a ';' immediately *after* the
24008	   closing brace, so, if we can, offset the location by 1 column.  */
24009	location_t next_loc = loc;
24010	if (!linemap_location_from_macro_expansion_p (line_table, loc))
24011	  next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
24012
24013	rich_location richloc (line_table, next_loc);
24014
24015	/* If we successfully offset the location, suggest the fix-it.  */
24016	if (next_loc != loc)
24017	  richloc.add_fixit_insert_before (next_loc, ";");
24018
24019	if (CLASSTYPE_DECLARED_CLASS (type))
24020	  error_at (&richloc,
24021		    "expected %<;%> after class definition");
24022	else if (TREE_CODE (type) == RECORD_TYPE)
24023	  error_at (&richloc,
24024		    "expected %<;%> after struct definition");
24025	else if (TREE_CODE (type) == UNION_TYPE)
24026	  error_at (&richloc,
24027		    "expected %<;%> after union definition");
24028	else
24029	  gcc_unreachable ();
24030
24031	/* Unget one token and smash it to look as though we encountered
24032	   a semicolon in the input stream.  */
24033	cp_lexer_set_token_position (parser->lexer, prev);
24034	token = cp_lexer_peek_token (parser->lexer);
24035	token->type = CPP_SEMICOLON;
24036	token->keyword = RID_MAX;
24037      }
24038  }
24039
24040  /* If this class is not itself within the scope of another class,
24041     then we need to parse the bodies of all of the queued function
24042     definitions.  Note that the queued functions defined in a class
24043     are not always processed immediately following the
24044     class-specifier for that class.  Consider:
24045
24046       struct A {
24047	 struct B { void f() { sizeof (A); } };
24048       };
24049
24050     If `f' were processed before the processing of `A' were
24051     completed, there would be no way to compute the size of `A'.
24052     Note that the nesting we are interested in here is lexical --
24053     not the semantic nesting given by TYPE_CONTEXT.  In particular,
24054     for:
24055
24056       struct A { struct B; };
24057       struct A::B { void f() { } };
24058
24059     there is no need to delay the parsing of `A::B::f'.  */
24060  if (--parser->num_classes_being_defined == 0)
24061    {
24062      tree decl;
24063      tree class_type = NULL_TREE;
24064      tree pushed_scope = NULL_TREE;
24065      unsigned ix;
24066      cp_default_arg_entry *e;
24067      tree save_ccp, save_ccr;
24068
24069      if (!type_definition_ok_p || any_erroneous_template_args_p (type))
24070	{
24071	  /* Skip default arguments, NSDMIs, etc, in order to improve
24072	     error recovery (c++/71169, c++/71832).  */
24073	  vec_safe_truncate (unparsed_funs_with_default_args, 0);
24074	  vec_safe_truncate (unparsed_nsdmis, 0);
24075	  vec_safe_truncate (unparsed_funs_with_definitions, 0);
24076	}
24077
24078      /* In a first pass, parse default arguments to the functions.
24079	 Then, in a second pass, parse the bodies of the functions.
24080	 This two-phased approach handles cases like:
24081
24082	    struct S {
24083	      void f() { g(); }
24084	      void g(int i = 3);
24085	    };
24086
24087	 */
24088      FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
24089	{
24090	  decl = e->decl;
24091	  /* If there are default arguments that have not yet been processed,
24092	     take care of them now.  */
24093	  if (class_type != e->class_type)
24094	    {
24095	      if (pushed_scope)
24096		pop_scope (pushed_scope);
24097	      class_type = e->class_type;
24098	      pushed_scope = push_scope (class_type);
24099	    }
24100	  /* Make sure that any template parameters are in scope.  */
24101	  maybe_begin_member_template_processing (decl);
24102	  /* Parse the default argument expressions.  */
24103	  cp_parser_late_parsing_default_args (parser, decl);
24104	  /* Remove any template parameters from the symbol table.  */
24105	  maybe_end_member_template_processing ();
24106	}
24107      vec_safe_truncate (unparsed_funs_with_default_args, 0);
24108
24109      /* If there are noexcept-specifiers that have not yet been processed,
24110	 take care of them now.  Do this before processing NSDMIs as they
24111	 may depend on noexcept-specifiers already having been processed.  */
24112      FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
24113	{
24114	  tree ctx = DECL_CONTEXT (decl);
24115	  if (class_type != ctx)
24116	    {
24117	      if (pushed_scope)
24118		pop_scope (pushed_scope);
24119	      class_type = ctx;
24120	      pushed_scope = push_scope (class_type);
24121	    }
24122
24123	  tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
24124	  spec = TREE_PURPOSE (spec);
24125
24126	  /* Make sure that any template parameters are in scope.  */
24127	  maybe_begin_member_template_processing (decl);
24128
24129	  /* Make sure that any member-function parameters are in scope.  */
24130	  inject_parm_decls (decl);
24131
24132	  /* 'this' is not allowed in static member functions.  */
24133	  unsigned char local_variables_forbidden_p
24134	    = parser->local_variables_forbidden_p;
24135	  if (DECL_THIS_STATIC (decl))
24136	    parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
24137
24138	  /* Now we can parse the noexcept-specifier.  */
24139	  spec = cp_parser_late_noexcept_specifier (parser, spec, decl);
24140
24141	  if (spec != error_mark_node)
24142	    TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
24143
24144	  /* Restore the state of local_variables_forbidden_p.  */
24145	  parser->local_variables_forbidden_p = local_variables_forbidden_p;
24146
24147	  /* The finish_struct call above performed various override checking,
24148	     but it skipped unparsed noexcept-specifier operands.  Now that we
24149	     have resolved them, check again.  */
24150	  noexcept_override_late_checks (type, decl);
24151
24152	  /* Remove any member-function parameters from the symbol table.  */
24153	  pop_injected_parms ();
24154
24155	  /* Remove any template parameters from the symbol table.  */
24156	  maybe_end_member_template_processing ();
24157	}
24158      vec_safe_truncate (unparsed_noexcepts, 0);
24159
24160      /* Now parse any NSDMIs.  */
24161      save_ccp = current_class_ptr;
24162      save_ccr = current_class_ref;
24163      FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
24164	{
24165	  if (class_type != DECL_CONTEXT (decl))
24166	    {
24167	      if (pushed_scope)
24168		pop_scope (pushed_scope);
24169	      class_type = DECL_CONTEXT (decl);
24170	      pushed_scope = push_scope (class_type);
24171	    }
24172	  inject_this_parameter (class_type, TYPE_UNQUALIFIED);
24173	  cp_parser_late_parsing_nsdmi (parser, decl);
24174	}
24175      vec_safe_truncate (unparsed_nsdmis, 0);
24176      current_class_ptr = save_ccp;
24177      current_class_ref = save_ccr;
24178      if (pushed_scope)
24179	pop_scope (pushed_scope);
24180
24181      /* Now parse the body of the functions.  */
24182      if (flag_openmp)
24183	{
24184	  /* OpenMP UDRs need to be parsed before all other functions.  */
24185	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24186	    if (DECL_OMP_DECLARE_REDUCTION_P (decl))
24187	      cp_parser_late_parsing_for_member (parser, decl);
24188	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24189	    if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
24190	      cp_parser_late_parsing_for_member (parser, decl);
24191	}
24192      else
24193	FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24194	  cp_parser_late_parsing_for_member (parser, decl);
24195      vec_safe_truncate (unparsed_funs_with_definitions, 0);
24196    }
24197
24198  /* Put back any saved access checks.  */
24199  pop_deferring_access_checks ();
24200
24201  /* Restore saved state.  */
24202  parser->in_switch_statement_p = in_switch_statement_p;
24203  parser->in_statement = in_statement;
24204  parser->in_function_body = saved_in_function_body;
24205  parser->num_template_parameter_lists
24206    = saved_num_template_parameter_lists;
24207  parser->in_unbraced_linkage_specification_p
24208    = saved_in_unbraced_linkage_specification_p;
24209
24210  return type;
24211}
24212
24213static tree
24214cp_parser_class_specifier (cp_parser* parser)
24215{
24216  tree ret;
24217  timevar_push (TV_PARSE_STRUCT);
24218  ret = cp_parser_class_specifier_1 (parser);
24219  timevar_pop (TV_PARSE_STRUCT);
24220  return ret;
24221}
24222
24223/* Parse a class-head.
24224
24225   class-head:
24226     class-key identifier [opt] base-clause [opt]
24227     class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
24228     class-key nested-name-specifier [opt] template-id
24229       base-clause [opt]
24230
24231   class-virt-specifier:
24232     final
24233
24234   GNU Extensions:
24235     class-key attributes identifier [opt] base-clause [opt]
24236     class-key attributes nested-name-specifier identifier base-clause [opt]
24237     class-key attributes nested-name-specifier [opt] template-id
24238       base-clause [opt]
24239
24240   Upon return BASES is initialized to the list of base classes (or
24241   NULL, if there are none) in the same form returned by
24242   cp_parser_base_clause.
24243
24244   Returns the TYPE of the indicated class.  Sets
24245   *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
24246   involving a nested-name-specifier was used, and FALSE otherwise.
24247
24248   Returns error_mark_node if this is not a class-head.
24249
24250   Returns NULL_TREE if the class-head is syntactically valid, but
24251   semantically invalid in a way that means we should skip the entire
24252   body of the class.  */
24253
24254static tree
24255cp_parser_class_head (cp_parser* parser,
24256		      bool* nested_name_specifier_p)
24257{
24258  tree nested_name_specifier;
24259  enum tag_types class_key;
24260  tree id = NULL_TREE;
24261  tree type = NULL_TREE;
24262  tree attributes;
24263  tree bases;
24264  cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24265  bool template_id_p = false;
24266  bool qualified_p = false;
24267  bool invalid_nested_name_p = false;
24268  bool invalid_explicit_specialization_p = false;
24269  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24270  tree pushed_scope = NULL_TREE;
24271  unsigned num_templates;
24272  cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
24273  /* Assume no nested-name-specifier will be present.  */
24274  *nested_name_specifier_p = false;
24275  /* Assume no template parameter lists will be used in defining the
24276     type.  */
24277  num_templates = 0;
24278  parser->colon_corrects_to_scope_p = false;
24279
24280  /* Look for the class-key.  */
24281  class_key = cp_parser_class_key (parser);
24282  if (class_key == none_type)
24283    return error_mark_node;
24284
24285  location_t class_head_start_location = input_location;
24286
24287  /* Parse the attributes.  */
24288  attributes = cp_parser_attributes_opt (parser);
24289
24290  /* If the next token is `::', that is invalid -- but sometimes
24291     people do try to write:
24292
24293       struct ::S {};
24294
24295     Handle this gracefully by accepting the extra qualifier, and then
24296     issuing an error about it later if this really is a
24297     class-head.  If it turns out just to be an elaborated type
24298     specifier, remain silent.  */
24299  if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
24300    qualified_p = true;
24301
24302  push_deferring_access_checks (dk_no_check);
24303
24304  /* Determine the name of the class.  Begin by looking for an
24305     optional nested-name-specifier.  */
24306  nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
24307  nested_name_specifier
24308    = cp_parser_nested_name_specifier_opt (parser,
24309					   /*typename_keyword_p=*/false,
24310					   /*check_dependency_p=*/false,
24311					   /*type_p=*/true,
24312					   /*is_declaration=*/false);
24313  /* If there was a nested-name-specifier, then there *must* be an
24314     identifier.  */
24315
24316  cp_token *bad_template_keyword = NULL;
24317
24318  if (nested_name_specifier)
24319    {
24320      type_start_token = cp_lexer_peek_token (parser->lexer);
24321      /* Although the grammar says `identifier', it really means
24322	 `class-name' or `template-name'.  You are only allowed to
24323	 define a class that has already been declared with this
24324	 syntax.
24325
24326	 The proposed resolution for Core Issue 180 says that wherever
24327	 you see `class T::X' you should treat `X' as a type-name.
24328
24329	 It is OK to define an inaccessible class; for example:
24330
24331	   class A { class B; };
24332	   class A::B {};
24333
24334	 We do not know if we will see a class-name, or a
24335	 template-name.  We look for a class-name first, in case the
24336	 class-name is a template-id; if we looked for the
24337	 template-name first we would stop after the template-name.  */
24338      cp_parser_parse_tentatively (parser);
24339      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24340	bad_template_keyword = cp_lexer_consume_token (parser->lexer);
24341      type = cp_parser_class_name (parser,
24342				   /*typename_keyword_p=*/false,
24343				   /*template_keyword_p=*/false,
24344				   class_type,
24345				   /*check_dependency_p=*/false,
24346				   /*class_head_p=*/true,
24347				   /*is_declaration=*/false);
24348      /* If that didn't work, ignore the nested-name-specifier.  */
24349      if (!cp_parser_parse_definitely (parser))
24350	{
24351	  invalid_nested_name_p = true;
24352	  type_start_token = cp_lexer_peek_token (parser->lexer);
24353	  id = cp_parser_identifier (parser);
24354	  if (id == error_mark_node)
24355	    id = NULL_TREE;
24356	}
24357      /* If we could not find a corresponding TYPE, treat this
24358	 declaration like an unqualified declaration.  */
24359      if (type == error_mark_node)
24360	nested_name_specifier = NULL_TREE;
24361      /* Otherwise, count the number of templates used in TYPE and its
24362	 containing scopes.  */
24363      else
24364	num_templates = num_template_headers_for_class (TREE_TYPE (type));
24365    }
24366  /* Otherwise, the identifier is optional.  */
24367  else
24368    {
24369      /* We don't know whether what comes next is a template-id,
24370	 an identifier, or nothing at all.  */
24371      cp_parser_parse_tentatively (parser);
24372      /* Check for a template-id.  */
24373      type_start_token = cp_lexer_peek_token (parser->lexer);
24374      id = cp_parser_template_id (parser,
24375				  /*template_keyword_p=*/false,
24376				  /*check_dependency_p=*/true,
24377				  class_key,
24378				  /*is_declaration=*/true);
24379      /* If that didn't work, it could still be an identifier.  */
24380      if (!cp_parser_parse_definitely (parser))
24381	{
24382	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24383	    {
24384	      type_start_token = cp_lexer_peek_token (parser->lexer);
24385	      id = cp_parser_identifier (parser);
24386	    }
24387	  else
24388	    id = NULL_TREE;
24389	}
24390      else
24391	{
24392	  template_id_p = true;
24393	  ++num_templates;
24394	}
24395    }
24396
24397  pop_deferring_access_checks ();
24398
24399  if (id)
24400    {
24401      cp_parser_check_for_invalid_template_id (parser, id,
24402					       class_key,
24403                                               type_start_token->location);
24404    }
24405  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
24406
24407  /* If it's not a `:' or a `{' then we can't really be looking at a
24408     class-head, since a class-head only appears as part of a
24409     class-specifier.  We have to detect this situation before calling
24410     xref_tag, since that has irreversible side-effects.  */
24411  if (!cp_parser_next_token_starts_class_definition_p (parser))
24412    {
24413      cp_parser_error (parser, "expected %<{%> or %<:%>");
24414      type = error_mark_node;
24415      goto out;
24416    }
24417
24418  /* At this point, we're going ahead with the class-specifier, even
24419     if some other problem occurs.  */
24420  cp_parser_commit_to_tentative_parse (parser);
24421  if (virt_specifiers & VIRT_SPEC_OVERRIDE)
24422    {
24423      cp_parser_error (parser,
24424                       "cannot specify %<override%> for a class");
24425      type = error_mark_node;
24426      goto out;
24427    }
24428  /* Issue the error about the overly-qualified name now.  */
24429  if (qualified_p)
24430    {
24431      cp_parser_error (parser,
24432		       "global qualification of class name is invalid");
24433      type = error_mark_node;
24434      goto out;
24435    }
24436  else if (invalid_nested_name_p)
24437    {
24438      cp_parser_error (parser,
24439		       "qualified name does not name a class");
24440      type = error_mark_node;
24441      goto out;
24442    }
24443  else if (nested_name_specifier)
24444    {
24445      tree scope;
24446
24447      if (bad_template_keyword)
24448	/* [temp.names]: in a qualified-id formed by a class-head-name, the
24449	   keyword template shall not appear at the top level.  */
24450	pedwarn (bad_template_keyword->location, OPT_Wpedantic,
24451		 "keyword %<template%> not allowed in class-head-name");
24452
24453      /* Reject typedef-names in class heads.  */
24454      if (!DECL_IMPLICIT_TYPEDEF_P (type))
24455	{
24456	  error_at (type_start_token->location,
24457		    "invalid class name in declaration of %qD",
24458		    type);
24459	  type = NULL_TREE;
24460	  goto done;
24461	}
24462
24463      /* Figure out in what scope the declaration is being placed.  */
24464      scope = current_scope ();
24465      /* If that scope does not contain the scope in which the
24466	 class was originally declared, the program is invalid.  */
24467      if (scope && !is_ancestor (scope, nested_name_specifier))
24468	{
24469	  if (at_namespace_scope_p ())
24470	    error_at (type_start_token->location,
24471		      "declaration of %qD in namespace %qD which does not "
24472		      "enclose %qD",
24473		      type, scope, nested_name_specifier);
24474	  else
24475	    error_at (type_start_token->location,
24476		      "declaration of %qD in %qD which does not enclose %qD",
24477		      type, scope, nested_name_specifier);
24478	  type = NULL_TREE;
24479	  goto done;
24480	}
24481      /* [dcl.meaning]
24482
24483	 A declarator-id shall not be qualified except for the
24484	 definition of a ... nested class outside of its class
24485	 ... [or] the definition or explicit instantiation of a
24486	 class member of a namespace outside of its namespace.  */
24487      if (scope == nested_name_specifier)
24488	permerror (nested_name_specifier_token_start->location,
24489		   "extra qualification not allowed");
24490    }
24491  /* An explicit-specialization must be preceded by "template <>".  If
24492     it is not, try to recover gracefully.  */
24493  if (at_namespace_scope_p ()
24494      && parser->num_template_parameter_lists == 0
24495      && !processing_template_parmlist
24496      && template_id_p)
24497    {
24498      /* Build a location of this form:
24499           struct typename <ARGS>
24500           ^~~~~~~~~~~~~~~~~~~~~~
24501         with caret==start at the start token, and
24502         finishing at the end of the type.  */
24503      location_t reported_loc
24504        = make_location (class_head_start_location,
24505                         class_head_start_location,
24506                         get_finish (type_start_token->location));
24507      rich_location richloc (line_table, reported_loc);
24508      richloc.add_fixit_insert_before (class_head_start_location,
24509                                       "template <> ");
24510      error_at (&richloc,
24511		"an explicit specialization must be preceded by"
24512		" %<template <>%>");
24513      invalid_explicit_specialization_p = true;
24514      /* Take the same action that would have been taken by
24515	 cp_parser_explicit_specialization.  */
24516      ++parser->num_template_parameter_lists;
24517      begin_specialization ();
24518    }
24519  /* There must be no "return" statements between this point and the
24520     end of this function; set "type "to the correct return value and
24521     use "goto done;" to return.  */
24522  /* Make sure that the right number of template parameters were
24523     present.  */
24524  if (!cp_parser_check_template_parameters (parser, num_templates,
24525					    template_id_p,
24526					    type_start_token->location,
24527					    /*declarator=*/NULL))
24528    {
24529      /* If something went wrong, there is no point in even trying to
24530	 process the class-definition.  */
24531      type = NULL_TREE;
24532      goto done;
24533    }
24534
24535  /* Look up the type.  */
24536  if (template_id_p)
24537    {
24538      if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24539	  && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24540	      || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24541	{
24542	  error_at (type_start_token->location,
24543		    "function template %qD redeclared as a class template", id);
24544	  type = error_mark_node;
24545	}
24546      else
24547	{
24548	  type = TREE_TYPE (id);
24549	  type = maybe_process_partial_specialization (type);
24550
24551	  /* Check the scope while we still know whether or not we had a
24552	     nested-name-specifier.  */
24553	  if (type != error_mark_node)
24554	    check_unqualified_spec_or_inst (type, type_start_token->location);
24555	}
24556      if (nested_name_specifier)
24557	pushed_scope = push_scope (nested_name_specifier);
24558    }
24559  else if (nested_name_specifier)
24560    {
24561      tree class_type;
24562
24563      /* Given:
24564
24565	    template <typename T> struct S { struct T };
24566	    template <typename T> struct S<T>::T { };
24567
24568	 we will get a TYPENAME_TYPE when processing the definition of
24569	 `S::T'.  We need to resolve it to the actual type before we
24570	 try to define it.  */
24571      if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24572	{
24573	  class_type = resolve_typename_type (TREE_TYPE (type),
24574					      /*only_current_p=*/false);
24575	  if (TREE_CODE (class_type) != TYPENAME_TYPE)
24576	    type = TYPE_NAME (class_type);
24577	  else
24578	    {
24579	      cp_parser_error (parser, "could not resolve typename type");
24580	      type = error_mark_node;
24581	    }
24582	}
24583
24584      if (maybe_process_partial_specialization (TREE_TYPE (type))
24585	  == error_mark_node)
24586	{
24587	  type = NULL_TREE;
24588	  goto done;
24589	}
24590
24591      class_type = current_class_type;
24592      /* Enter the scope indicated by the nested-name-specifier.  */
24593      pushed_scope = push_scope (nested_name_specifier);
24594      /* Get the canonical version of this type.  */
24595      type = TYPE_MAIN_DECL (TREE_TYPE (type));
24596      /* Call push_template_decl if it seems like we should be defining a
24597	 template either from the template headers or the type we're
24598	 defining, so that we diagnose both extra and missing headers.  */
24599      if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24600	   || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24601	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24602	{
24603	  type = push_template_decl (type);
24604	  if (type == error_mark_node)
24605	    {
24606	      type = NULL_TREE;
24607	      goto done;
24608	    }
24609	}
24610
24611      type = TREE_TYPE (type);
24612      *nested_name_specifier_p = true;
24613    }
24614  else      /* The name is not a nested name.  */
24615    {
24616      /* If the class was unnamed, create a dummy name.  */
24617      if (!id)
24618	id = make_anon_name ();
24619      tag_scope tag_scope = (parser->in_type_id_in_expr_p
24620			     ? ts_within_enclosing_non_class
24621			     : ts_current);
24622      type = xref_tag (class_key, id, tag_scope,
24623		       parser->num_template_parameter_lists);
24624    }
24625
24626  /* Diagnose class/struct/union mismatches.  */
24627  cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
24628			     true, true);
24629
24630  /* Indicate whether this class was declared as a `class' or as a
24631     `struct'.  */
24632  if (TREE_CODE (type) == RECORD_TYPE)
24633    CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
24634
24635  /* If this type was already complete, and we see another definition,
24636     that's an error.  Likewise if the type is already being defined:
24637     this can happen, eg, when it's defined from within an expression
24638     (c++/84605).  */
24639  if (type != error_mark_node
24640      && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24641    {
24642      error_at (type_start_token->location, "redefinition of %q#T",
24643		type);
24644      inform (location_of (type), "previous definition of %q#T",
24645	      type);
24646      type = NULL_TREE;
24647      goto done;
24648    }
24649  else if (type == error_mark_node)
24650    type = NULL_TREE;
24651
24652  if (type)
24653    {
24654      /* Apply attributes now, before any use of the class as a template
24655	 argument in its base list.  */
24656      cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24657      fixup_attribute_variants (type);
24658    }
24659
24660  /* Associate constraints with the type.  */
24661  if (flag_concepts)
24662    type = associate_classtype_constraints (type);
24663
24664  /* We will have entered the scope containing the class; the names of
24665     base classes should be looked up in that context.  For example:
24666
24667       struct A { struct B {}; struct C; };
24668       struct A::C : B {};
24669
24670     is valid.  */
24671
24672  /* Get the list of base-classes, if there is one.  */
24673  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24674    {
24675      /* PR59482: enter the class scope so that base-specifiers are looked
24676	 up correctly.  */
24677      if (type)
24678	pushclass (type);
24679      bases = cp_parser_base_clause (parser);
24680      /* PR59482: get out of the previously pushed class scope so that the
24681	 subsequent pops pop the right thing.  */
24682      if (type)
24683	popclass ();
24684    }
24685  else
24686    bases = NULL_TREE;
24687
24688  /* If we're really defining a class, process the base classes.
24689     If they're invalid, fail.  */
24690  if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24691    xref_basetypes (type, bases);
24692
24693 done:
24694  /* Leave the scope given by the nested-name-specifier.  We will
24695     enter the class scope itself while processing the members.  */
24696  if (pushed_scope)
24697    pop_scope (pushed_scope);
24698
24699  if (invalid_explicit_specialization_p)
24700    {
24701      end_specialization ();
24702      --parser->num_template_parameter_lists;
24703    }
24704
24705  if (type)
24706    DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24707  if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24708    CLASSTYPE_FINAL (type) = 1;
24709 out:
24710  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24711  return type;
24712}
24713
24714/* Parse a class-key.
24715
24716   class-key:
24717     class
24718     struct
24719     union
24720
24721   Returns the kind of class-key specified, or none_type to indicate
24722   error.  */
24723
24724static enum tag_types
24725cp_parser_class_key (cp_parser* parser)
24726{
24727  cp_token *token;
24728  enum tag_types tag_type;
24729
24730  /* Look for the class-key.  */
24731  token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24732  if (!token)
24733    return none_type;
24734
24735  /* Check to see if the TOKEN is a class-key.  */
24736  tag_type = cp_parser_token_is_class_key (token);
24737  if (!tag_type)
24738    cp_parser_error (parser, "expected class-key");
24739  return tag_type;
24740}
24741
24742/* Parse a type-parameter-key.
24743
24744   type-parameter-key:
24745     class
24746     typename
24747 */
24748
24749static void
24750cp_parser_type_parameter_key (cp_parser* parser)
24751{
24752  /* Look for the type-parameter-key.  */
24753  enum tag_types tag_type = none_type;
24754  cp_token *token = cp_lexer_peek_token (parser->lexer);
24755  if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24756    {
24757      cp_lexer_consume_token (parser->lexer);
24758      if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24759	/* typename is not allowed in a template template parameter
24760	   by the standard until C++17.  */
24761	pedwarn (token->location, OPT_Wpedantic,
24762		 "ISO C++ forbids typename key in template template parameter;"
24763		 " use %<-std=c++17%> or %<-std=gnu++17%>");
24764    }
24765  else
24766    cp_parser_error (parser, "expected %<class%> or %<typename%>");
24767
24768  return;
24769}
24770
24771/* Parse an (optional) member-specification.
24772
24773   member-specification:
24774     member-declaration member-specification [opt]
24775     access-specifier : member-specification [opt]  */
24776
24777static void
24778cp_parser_member_specification_opt (cp_parser* parser)
24779{
24780  while (true)
24781    {
24782      cp_token *token;
24783      enum rid keyword;
24784
24785      /* Peek at the next token.  */
24786      token = cp_lexer_peek_token (parser->lexer);
24787      /* If it's a `}', or EOF then we've seen all the members.  */
24788      if (token->type == CPP_CLOSE_BRACE
24789	  || token->type == CPP_EOF
24790	  || token->type == CPP_PRAGMA_EOL)
24791	break;
24792
24793      /* See if this token is a keyword.  */
24794      keyword = token->keyword;
24795      switch (keyword)
24796	{
24797	case RID_PUBLIC:
24798	case RID_PROTECTED:
24799	case RID_PRIVATE:
24800	  /* Consume the access-specifier.  */
24801	  cp_lexer_consume_token (parser->lexer);
24802	  /* Remember which access-specifier is active.  */
24803	  current_access_specifier = token->u.value;
24804	  /* Look for the `:'.  */
24805	  cp_parser_require (parser, CPP_COLON, RT_COLON);
24806	  break;
24807
24808	default:
24809	  /* Accept #pragmas at class scope.  */
24810	  if (token->type == CPP_PRAGMA)
24811	    {
24812	      cp_parser_pragma (parser, pragma_member, NULL);
24813	      break;
24814	    }
24815
24816	  /* Otherwise, the next construction must be a
24817	     member-declaration.  */
24818	  cp_parser_member_declaration (parser);
24819	}
24820    }
24821}
24822
24823/* Parse a member-declaration.
24824
24825   member-declaration:
24826     decl-specifier-seq [opt] member-declarator-list [opt] ;
24827     function-definition ; [opt]
24828     :: [opt] nested-name-specifier template [opt] unqualified-id ;
24829     using-declaration
24830     template-declaration
24831     alias-declaration
24832
24833   member-declarator-list:
24834     member-declarator
24835     member-declarator-list , member-declarator
24836
24837   member-declarator:
24838     declarator pure-specifier [opt]
24839     declarator constant-initializer [opt]
24840     identifier [opt] : constant-expression
24841
24842   GNU Extensions:
24843
24844   member-declaration:
24845     __extension__ member-declaration
24846
24847   member-declarator:
24848     declarator attributes [opt] pure-specifier [opt]
24849     declarator attributes [opt] constant-initializer [opt]
24850     identifier [opt] attributes [opt] : constant-expression
24851
24852   C++0x Extensions:
24853
24854   member-declaration:
24855     static_assert-declaration  */
24856
24857static void
24858cp_parser_member_declaration (cp_parser* parser)
24859{
24860  cp_decl_specifier_seq decl_specifiers;
24861  tree prefix_attributes;
24862  tree decl;
24863  int declares_class_or_enum;
24864  bool friend_p;
24865  cp_token *token = NULL;
24866  cp_token *decl_spec_token_start = NULL;
24867  cp_token *initializer_token_start = NULL;
24868  int saved_pedantic;
24869  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24870
24871  /* Check for the `__extension__' keyword.  */
24872  if (cp_parser_extension_opt (parser, &saved_pedantic))
24873    {
24874      /* Recurse.  */
24875      cp_parser_member_declaration (parser);
24876      /* Restore the old value of the PEDANTIC flag.  */
24877      pedantic = saved_pedantic;
24878
24879      return;
24880    }
24881
24882  /* Check for a template-declaration.  */
24883  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24884    {
24885      /* An explicit specialization here is an error condition, and we
24886	 expect the specialization handler to detect and report this.  */
24887      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24888	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24889	cp_parser_explicit_specialization (parser);
24890      else
24891	cp_parser_template_declaration (parser, /*member_p=*/true);
24892
24893      return;
24894    }
24895  /* Check for a template introduction.  */
24896  else if (cp_parser_template_declaration_after_export (parser, true))
24897    return;
24898
24899  /* Check for a using-declaration.  */
24900  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24901    {
24902      if (cxx_dialect < cxx11)
24903	{
24904	  /* Parse the using-declaration.  */
24905	  cp_parser_using_declaration (parser,
24906				       /*access_declaration_p=*/false);
24907	  return;
24908	}
24909      else
24910	{
24911	  tree decl;
24912	  bool alias_decl_expected;
24913	  cp_parser_parse_tentatively (parser);
24914	  decl = cp_parser_alias_declaration (parser);
24915	  /* Note that if we actually see the '=' token after the
24916	     identifier, cp_parser_alias_declaration commits the
24917	     tentative parse.  In that case, we really expect an
24918	     alias-declaration.  Otherwise, we expect a using
24919	     declaration.  */
24920	  alias_decl_expected =
24921	    !cp_parser_uncommitted_to_tentative_parse_p (parser);
24922	  cp_parser_parse_definitely (parser);
24923
24924	  if (alias_decl_expected)
24925	    finish_member_declaration (decl);
24926	  else
24927	    cp_parser_using_declaration (parser,
24928					 /*access_declaration_p=*/false);
24929	  return;
24930	}
24931    }
24932
24933  /* Check for @defs.  */
24934  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24935    {
24936      tree ivar, member;
24937      tree ivar_chains = cp_parser_objc_defs_expression (parser);
24938      ivar = ivar_chains;
24939      while (ivar)
24940	{
24941	  member = ivar;
24942	  ivar = TREE_CHAIN (member);
24943	  TREE_CHAIN (member) = NULL_TREE;
24944	  finish_member_declaration (member);
24945	}
24946      return;
24947    }
24948
24949  /* If the next token is `static_assert' we have a static assertion.  */
24950  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24951    {
24952      cp_parser_static_assert (parser, /*member_p=*/true);
24953      return;
24954    }
24955
24956  parser->colon_corrects_to_scope_p = false;
24957
24958  if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24959      goto out;
24960
24961  /* Parse the decl-specifier-seq.  */
24962  decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24963  cp_parser_decl_specifier_seq (parser,
24964				(CP_PARSER_FLAGS_OPTIONAL
24965				 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24966				&decl_specifiers,
24967				&declares_class_or_enum);
24968  /* Check for an invalid type-name.  */
24969  if (!decl_specifiers.any_type_specifiers_p
24970      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24971    goto out;
24972  /* If there is no declarator, then the decl-specifier-seq should
24973     specify a type.  */
24974  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24975    {
24976      /* If there was no decl-specifier-seq, and the next token is a
24977	 `;', then we have something like:
24978
24979	   struct S { ; };
24980
24981	 [class.mem]
24982
24983	 Each member-declaration shall declare at least one member
24984	 name of the class.  */
24985      if (!decl_specifiers.any_specifiers_p)
24986	{
24987	  cp_token *token = cp_lexer_peek_token (parser->lexer);
24988	  if (!in_system_header_at (token->location))
24989	    {
24990	      gcc_rich_location richloc (token->location);
24991	      richloc.add_fixit_remove ();
24992	      pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24993	    }
24994	}
24995      else
24996	{
24997	  tree type;
24998
24999	  /* See if this declaration is a friend.  */
25000	  friend_p = cp_parser_friend_p (&decl_specifiers);
25001	  /* If there were decl-specifiers, check to see if there was
25002	     a class-declaration.  */
25003	  type = check_tag_decl (&decl_specifiers,
25004				 /*explicit_type_instantiation_p=*/false);
25005	  /* Nested classes have already been added to the class, but
25006	     a `friend' needs to be explicitly registered.  */
25007	  if (friend_p)
25008	    {
25009	      /* If the `friend' keyword was present, the friend must
25010		 be introduced with a class-key.  */
25011	       if (!declares_class_or_enum && cxx_dialect < cxx11)
25012		 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
25013			  "in C++03 a class-key must be used "
25014			  "when declaring a friend");
25015	       /* In this case:
25016
25017		    template <typename T> struct A {
25018		      friend struct A<T>::B;
25019		    };
25020
25021		  A<T>::B will be represented by a TYPENAME_TYPE, and
25022		  therefore not recognized by check_tag_decl.  */
25023	       if (!type)
25024		 {
25025		   type = decl_specifiers.type;
25026		   if (type && TREE_CODE (type) == TYPE_DECL)
25027		     type = TREE_TYPE (type);
25028		 }
25029	       if (!type || !TYPE_P (type))
25030		 error_at (decl_spec_token_start->location,
25031			   "friend declaration does not name a class or "
25032			   "function");
25033	       else
25034		 make_friend_class (current_class_type, type,
25035				    /*complain=*/true);
25036	    }
25037	  /* If there is no TYPE, an error message will already have
25038	     been issued.  */
25039	  else if (!type || type == error_mark_node)
25040	    ;
25041	  /* An anonymous aggregate has to be handled specially; such
25042	     a declaration really declares a data member (with a
25043	     particular type), as opposed to a nested class.  */
25044	  else if (ANON_AGGR_TYPE_P (type))
25045	    {
25046	      /* C++11 9.5/6.  */
25047	      if (decl_specifiers.storage_class != sc_none)
25048		error_at (decl_spec_token_start->location,
25049			  "a storage class on an anonymous aggregate "
25050			  "in class scope is not allowed");
25051
25052	      /* Remove constructors and such from TYPE, now that we
25053		 know it is an anonymous aggregate.  */
25054	      fixup_anonymous_aggr (type);
25055	      /* And make the corresponding data member.  */
25056	      decl = build_decl (decl_spec_token_start->location,
25057				 FIELD_DECL, NULL_TREE, type);
25058	      /* Add it to the class.  */
25059	      finish_member_declaration (decl);
25060	    }
25061	  else
25062	    cp_parser_check_access_in_redeclaration
25063					      (TYPE_NAME (type),
25064					       decl_spec_token_start->location);
25065	}
25066    }
25067  else
25068    {
25069      bool assume_semicolon = false;
25070
25071      /* Clear attributes from the decl_specifiers but keep them
25072	 around as prefix attributes that apply them to the entity
25073	 being declared.  */
25074      prefix_attributes = decl_specifiers.attributes;
25075      decl_specifiers.attributes = NULL_TREE;
25076
25077      /* See if these declarations will be friends.  */
25078      friend_p = cp_parser_friend_p (&decl_specifiers);
25079
25080      /* Keep going until we hit the `;' at the end of the
25081	 declaration.  */
25082      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25083	{
25084	  tree attributes = NULL_TREE;
25085	  tree first_attribute;
25086	  tree initializer;
25087	  bool named_bitfld = false;
25088
25089	  /* Peek at the next token.  */
25090	  token = cp_lexer_peek_token (parser->lexer);
25091
25092	  /* The following code wants to know early if it is a bit-field
25093	     or some other declaration.  Attributes can appear before
25094	     the `:' token.  Skip over them without consuming any tokens
25095	     to peek if they are followed by `:'.  */
25096	  if (cp_next_tokens_can_be_attribute_p (parser)
25097	      || (token->type == CPP_NAME
25098		  && cp_nth_tokens_can_be_attribute_p (parser, 2)
25099		  && (named_bitfld = true)))
25100	    {
25101	      size_t n
25102		= cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
25103	      token = cp_lexer_peek_nth_token (parser->lexer, n);
25104	    }
25105
25106	  /* Check for a bitfield declaration.  */
25107	  if (token->type == CPP_COLON
25108	      || (token->type == CPP_NAME
25109		  && token == cp_lexer_peek_token (parser->lexer)
25110		  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
25111		  && (named_bitfld = true)))
25112	    {
25113	      tree identifier;
25114	      tree width;
25115	      tree late_attributes = NULL_TREE;
25116	      location_t id_location
25117		= cp_lexer_peek_token (parser->lexer)->location;
25118
25119	      if (named_bitfld)
25120		identifier = cp_parser_identifier (parser);
25121	      else
25122		identifier = NULL_TREE;
25123
25124	      /* Look for attributes that apply to the bitfield.  */
25125	      attributes = cp_parser_attributes_opt (parser);
25126
25127	      /* Consume the `:' token.  */
25128	      cp_lexer_consume_token (parser->lexer);
25129
25130	      /* Get the width of the bitfield.  */
25131	      width = cp_parser_constant_expression (parser, false, NULL,
25132						     cxx_dialect >= cxx11);
25133
25134	      /* In C++2A and as extension for C++11 and above we allow
25135		 default member initializers for bit-fields.  */
25136	      initializer = NULL_TREE;
25137	      if (cxx_dialect >= cxx11
25138		  && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
25139		      || cp_lexer_next_token_is (parser->lexer,
25140						 CPP_OPEN_BRACE)))
25141		{
25142		  location_t loc
25143		    = cp_lexer_peek_token (parser->lexer)->location;
25144		  if (cxx_dialect < cxx2a
25145		      && identifier != NULL_TREE)
25146		    pedwarn (loc, 0,
25147			     "default member initializers for bit-fields "
25148			     "only available with %<-std=c++2a%> or "
25149			     "%<-std=gnu++2a%>");
25150
25151		  initializer = cp_parser_save_nsdmi (parser);
25152		  if (identifier == NULL_TREE)
25153		    {
25154		      error_at (loc, "default member initializer for "
25155				     "unnamed bit-field");
25156		      initializer = NULL_TREE;
25157		    }
25158		}
25159	      else
25160		{
25161		  /* Look for attributes that apply to the bitfield after
25162		     the `:' token and width.  This is where GCC used to
25163		     parse attributes in the past, pedwarn if there is
25164		     a std attribute.  */
25165		  if (cp_next_tokens_can_be_std_attribute_p (parser))
25166		    pedwarn (input_location, OPT_Wpedantic,
25167			     "ISO C++ allows bit-field attributes only "
25168			     "before the %<:%> token");
25169
25170		  late_attributes = cp_parser_attributes_opt (parser);
25171		}
25172
25173	      attributes = attr_chainon (attributes, late_attributes);
25174
25175	      /* Remember which attributes are prefix attributes and
25176		 which are not.  */
25177	      first_attribute = attributes;
25178	      /* Combine the attributes.  */
25179	      attributes = attr_chainon (prefix_attributes, attributes);
25180
25181	      /* Create the bitfield declaration.  */
25182	      decl = grokbitfield (identifier
25183				   ? make_id_declarator (NULL_TREE,
25184							 identifier,
25185							 sfk_none,
25186							 id_location)
25187				   : NULL,
25188				   &decl_specifiers,
25189				   width, initializer,
25190				   attributes);
25191	    }
25192	  else
25193	    {
25194	      cp_declarator *declarator;
25195	      tree asm_specification;
25196	      int ctor_dtor_or_conv_p;
25197	      bool static_p = (decl_specifiers.storage_class == sc_static);
25198	      cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
25199	      if (!friend_p
25200		  && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25201		flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
25202
25203	      /* Parse the declarator.  */
25204	      declarator
25205		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25206					flags,
25207					&ctor_dtor_or_conv_p,
25208					/*parenthesized_p=*/NULL,
25209					/*member_p=*/true,
25210					friend_p, static_p);
25211
25212	      /* If something went wrong parsing the declarator, make sure
25213		 that we at least consume some tokens.  */
25214	      if (declarator == cp_error_declarator)
25215		{
25216		  /* Skip to the end of the statement.  */
25217		  cp_parser_skip_to_end_of_statement (parser);
25218		  /* If the next token is not a semicolon, that is
25219		     probably because we just skipped over the body of
25220		     a function.  So, we consume a semicolon if
25221		     present, but do not issue an error message if it
25222		     is not present.  */
25223		  if (cp_lexer_next_token_is (parser->lexer,
25224					      CPP_SEMICOLON))
25225		    cp_lexer_consume_token (parser->lexer);
25226		  goto out;
25227		}
25228
25229	      if (declares_class_or_enum & 2)
25230		cp_parser_check_for_definition_in_return_type
25231					    (declarator, decl_specifiers.type,
25232					     decl_specifiers.locations[ds_type_spec]);
25233
25234	      /* Look for an asm-specification.  */
25235	      asm_specification = cp_parser_asm_specification_opt (parser);
25236	      /* Look for attributes that apply to the declaration.  */
25237	      attributes = cp_parser_attributes_opt (parser);
25238	      /* Remember which attributes are prefix attributes and
25239		 which are not.  */
25240	      first_attribute = attributes;
25241	      /* Combine the attributes.  */
25242	      attributes = attr_chainon (prefix_attributes, attributes);
25243
25244	      /* If it's an `=', then we have a constant-initializer or a
25245		 pure-specifier.  It is not correct to parse the
25246		 initializer before registering the member declaration
25247		 since the member declaration should be in scope while
25248		 its initializer is processed.  However, the rest of the
25249		 front end does not yet provide an interface that allows
25250		 us to handle this correctly.  */
25251	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25252		{
25253		  /* In [class.mem]:
25254
25255		     A pure-specifier shall be used only in the declaration of
25256		     a virtual function.
25257
25258		     A member-declarator can contain a constant-initializer
25259		     only if it declares a static member of integral or
25260		     enumeration type.
25261
25262		     Therefore, if the DECLARATOR is for a function, we look
25263		     for a pure-specifier; otherwise, we look for a
25264		     constant-initializer.  When we call `grokfield', it will
25265		     perform more stringent semantics checks.  */
25266		  initializer_token_start = cp_lexer_peek_token (parser->lexer);
25267		  if (function_declarator_p (declarator)
25268		      || (decl_specifiers.type
25269			  && TREE_CODE (decl_specifiers.type) == TYPE_DECL
25270			  && declarator->kind == cdk_id
25271			  && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
25272			      == FUNCTION_TYPE)))
25273		    initializer = cp_parser_pure_specifier (parser);
25274		  else if (decl_specifiers.storage_class != sc_static)
25275		    initializer = cp_parser_save_nsdmi (parser);
25276		  else if (cxx_dialect >= cxx11)
25277		    {
25278		      bool nonconst;
25279		      /* Don't require a constant rvalue in C++11, since we
25280			 might want a reference constant.  We'll enforce
25281		         constancy later.  */
25282		      cp_lexer_consume_token (parser->lexer);
25283		      /* Parse the initializer.  */
25284		      initializer = cp_parser_initializer_clause (parser,
25285								  &nonconst);
25286		    }
25287		  else
25288		    /* Parse the initializer.  */
25289		    initializer = cp_parser_constant_initializer (parser);
25290		}
25291	      else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
25292		       && !function_declarator_p (declarator))
25293		{
25294		  bool x;
25295		  if (decl_specifiers.storage_class != sc_static)
25296		    initializer = cp_parser_save_nsdmi (parser);
25297		  else
25298		    initializer = cp_parser_initializer (parser, &x, &x);
25299		}
25300	      /* Detect invalid bit-field cases such as
25301
25302		   int *p : 4;
25303		   int &&r : 3;
25304
25305		 and similar.  */
25306	      else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
25307		       /* If there were no type specifiers, it was a
25308			  constructor.  */
25309		       && decl_specifiers.any_type_specifiers_p)
25310		{
25311		  /* This is called for a decent diagnostic only.  */
25312		  tree d = grokdeclarator (declarator, &decl_specifiers,
25313					   BITFIELD, /*initialized=*/false,
25314					   &attributes);
25315		  if (!error_operand_p (d))
25316		    error_at (DECL_SOURCE_LOCATION (d),
25317			      "bit-field %qD has non-integral type %qT",
25318			      d, TREE_TYPE (d));
25319		  cp_parser_skip_to_end_of_statement (parser);
25320		  /* Avoid "extra ;" pedwarns.  */
25321		  if (cp_lexer_next_token_is (parser->lexer,
25322					      CPP_SEMICOLON))
25323		    cp_lexer_consume_token (parser->lexer);
25324		  goto out;
25325		}
25326	      /* Otherwise, there is no initializer.  */
25327	      else
25328		initializer = NULL_TREE;
25329
25330	      /* See if we are probably looking at a function
25331		 definition.  We are certainly not looking at a
25332		 member-declarator.  Calling `grokfield' has
25333		 side-effects, so we must not do it unless we are sure
25334		 that we are looking at a member-declarator.  */
25335	      if (cp_parser_token_starts_function_definition_p
25336		  (cp_lexer_peek_token (parser->lexer)))
25337		{
25338		  /* The grammar does not allow a pure-specifier to be
25339		     used when a member function is defined.  (It is
25340		     possible that this fact is an oversight in the
25341		     standard, since a pure function may be defined
25342		     outside of the class-specifier.  */
25343		  if (initializer && initializer_token_start)
25344		    error_at (initializer_token_start->location,
25345			      "pure-specifier on function-definition");
25346		  decl = cp_parser_save_member_function_body (parser,
25347							      &decl_specifiers,
25348							      declarator,
25349							      attributes);
25350		  if (parser->fully_implicit_function_template_p)
25351		    decl = finish_fully_implicit_template (parser, decl);
25352		  /* If the member was not a friend, declare it here.  */
25353		  if (!friend_p)
25354		    finish_member_declaration (decl);
25355		  /* Peek at the next token.  */
25356		  token = cp_lexer_peek_token (parser->lexer);
25357		  /* If the next token is a semicolon, consume it.  */
25358		  if (token->type == CPP_SEMICOLON)
25359		    {
25360		      location_t semicolon_loc
25361			= cp_lexer_consume_token (parser->lexer)->location;
25362		      gcc_rich_location richloc (semicolon_loc);
25363		      richloc.add_fixit_remove ();
25364		      warning_at (&richloc, OPT_Wextra_semi,
25365				  "extra %<;%> after in-class "
25366				  "function definition");
25367		    }
25368		  goto out;
25369		}
25370	      else
25371		if (declarator->kind == cdk_function)
25372		  declarator->id_loc = token->location;
25373	      /* Create the declaration.  */
25374	      decl = grokfield (declarator, &decl_specifiers,
25375				initializer, /*init_const_expr_p=*/true,
25376				asm_specification, attributes);
25377	      if (parser->fully_implicit_function_template_p)
25378		{
25379		  if (friend_p)
25380		    finish_fully_implicit_template (parser, 0);
25381		  else
25382		    decl = finish_fully_implicit_template (parser, decl);
25383		}
25384	    }
25385
25386	  cp_finalize_omp_declare_simd (parser, decl);
25387	  cp_finalize_oacc_routine (parser, decl, false);
25388
25389	  /* Reset PREFIX_ATTRIBUTES.  */
25390	  if (attributes != error_mark_node)
25391	    {
25392	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
25393		attributes = TREE_CHAIN (attributes);
25394	      if (attributes)
25395		TREE_CHAIN (attributes) = NULL_TREE;
25396	    }
25397
25398	  /* If there is any qualification still in effect, clear it
25399	     now; we will be starting fresh with the next declarator.  */
25400	  parser->scope = NULL_TREE;
25401	  parser->qualifying_scope = NULL_TREE;
25402	  parser->object_scope = NULL_TREE;
25403	  /* If it's a `,', then there are more declarators.  */
25404	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25405	    {
25406	      cp_lexer_consume_token (parser->lexer);
25407	      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25408		{
25409		  cp_token *token = cp_lexer_previous_token (parser->lexer);
25410		  gcc_rich_location richloc (token->location);
25411		  richloc.add_fixit_remove ();
25412		  error_at (&richloc, "stray %<,%> at end of "
25413			    "member declaration");
25414		}
25415	    }
25416	  /* If the next token isn't a `;', then we have a parse error.  */
25417	  else if (cp_lexer_next_token_is_not (parser->lexer,
25418					       CPP_SEMICOLON))
25419	    {
25420	      /* The next token might be a ways away from where the
25421		 actual semicolon is missing.  Find the previous token
25422		 and use that for our error position.  */
25423	      cp_token *token = cp_lexer_previous_token (parser->lexer);
25424	      gcc_rich_location richloc (token->location);
25425	      richloc.add_fixit_insert_after (";");
25426	      error_at (&richloc, "expected %<;%> at end of "
25427			"member declaration");
25428
25429	      /* Assume that the user meant to provide a semicolon.  If
25430		 we were to cp_parser_skip_to_end_of_statement, we might
25431		 skip to a semicolon inside a member function definition
25432		 and issue nonsensical error messages.  */
25433	      assume_semicolon = true;
25434	    }
25435
25436	  if (decl)
25437	    {
25438	      /* Add DECL to the list of members.  */
25439	      if (!friend_p
25440		  /* Explicitly include, eg, NSDMIs, for better error
25441		     recovery (c++/58650).  */
25442		  || !DECL_DECLARES_FUNCTION_P (decl))
25443		finish_member_declaration (decl);
25444
25445	      if (DECL_DECLARES_FUNCTION_P (decl))
25446		cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
25447	      else if (TREE_CODE (decl) == FIELD_DECL
25448		       && DECL_INITIAL (decl))
25449		/* Add DECL to the queue of NSDMI to be parsed later.  */
25450		vec_safe_push (unparsed_nsdmis, decl);
25451	    }
25452
25453	  if (assume_semicolon)
25454	    goto out;
25455	}
25456    }
25457
25458  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25459 out:
25460  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
25461}
25462
25463/* Parse a pure-specifier.
25464
25465   pure-specifier:
25466     = 0
25467
25468   Returns INTEGER_ZERO_NODE if a pure specifier is found.
25469   Otherwise, ERROR_MARK_NODE is returned.  */
25470
25471static tree
25472cp_parser_pure_specifier (cp_parser* parser)
25473{
25474  cp_token *token;
25475
25476  /* Look for the `=' token.  */
25477  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25478    return error_mark_node;
25479  /* Look for the `0' token.  */
25480  token = cp_lexer_peek_token (parser->lexer);
25481
25482  if (token->type == CPP_EOF
25483      || token->type == CPP_PRAGMA_EOL)
25484    return error_mark_node;
25485
25486  cp_lexer_consume_token (parser->lexer);
25487
25488  /* Accept = default or = delete in c++0x mode.  */
25489  if (token->keyword == RID_DEFAULT
25490      || token->keyword == RID_DELETE)
25491    {
25492      maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
25493      return token->u.value;
25494    }
25495
25496  /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
25497  if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
25498    {
25499      cp_parser_error (parser,
25500		       "invalid pure specifier (only %<= 0%> is allowed)");
25501      cp_parser_skip_to_end_of_statement (parser);
25502      return error_mark_node;
25503    }
25504  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25505    {
25506      error_at (token->location, "templates may not be %<virtual%>");
25507      return error_mark_node;
25508    }
25509
25510  return integer_zero_node;
25511}
25512
25513/* Parse a constant-initializer.
25514
25515   constant-initializer:
25516     = constant-expression
25517
25518   Returns a representation of the constant-expression.  */
25519
25520static tree
25521cp_parser_constant_initializer (cp_parser* parser)
25522{
25523  /* Look for the `=' token.  */
25524  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25525    return error_mark_node;
25526
25527  /* It is invalid to write:
25528
25529       struct S { static const int i = { 7 }; };
25530
25531     */
25532  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25533    {
25534      cp_parser_error (parser,
25535		       "a brace-enclosed initializer is not allowed here");
25536      /* Consume the opening brace.  */
25537      matching_braces braces;
25538      braces.consume_open (parser);
25539      /* Skip the initializer.  */
25540      cp_parser_skip_to_closing_brace (parser);
25541      /* Look for the trailing `}'.  */
25542      braces.require_close (parser);
25543
25544      return error_mark_node;
25545    }
25546
25547  return cp_parser_constant_expression (parser);
25548}
25549
25550/* Derived classes [gram.class.derived] */
25551
25552/* Parse a base-clause.
25553
25554   base-clause:
25555     : base-specifier-list
25556
25557   base-specifier-list:
25558     base-specifier ... [opt]
25559     base-specifier-list , base-specifier ... [opt]
25560
25561   Returns a TREE_LIST representing the base-classes, in the order in
25562   which they were declared.  The representation of each node is as
25563   described by cp_parser_base_specifier.
25564
25565   In the case that no bases are specified, this function will return
25566   NULL_TREE, not ERROR_MARK_NODE.  */
25567
25568static tree
25569cp_parser_base_clause (cp_parser* parser)
25570{
25571  tree bases = NULL_TREE;
25572
25573  /* Look for the `:' that begins the list.  */
25574  cp_parser_require (parser, CPP_COLON, RT_COLON);
25575
25576  /* Scan the base-specifier-list.  */
25577  while (true)
25578    {
25579      cp_token *token;
25580      tree base;
25581      bool pack_expansion_p = false;
25582
25583      /* Look for the base-specifier.  */
25584      base = cp_parser_base_specifier (parser);
25585      /* Look for the (optional) ellipsis. */
25586      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25587        {
25588          /* Consume the `...'. */
25589          cp_lexer_consume_token (parser->lexer);
25590
25591          pack_expansion_p = true;
25592        }
25593
25594      /* Add BASE to the front of the list.  */
25595      if (base && base != error_mark_node)
25596	{
25597          if (pack_expansion_p)
25598            /* Make this a pack expansion type. */
25599            TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25600
25601          if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25602            {
25603              TREE_CHAIN (base) = bases;
25604              bases = base;
25605            }
25606	}
25607      /* Peek at the next token.  */
25608      token = cp_lexer_peek_token (parser->lexer);
25609      /* If it's not a comma, then the list is complete.  */
25610      if (token->type != CPP_COMMA)
25611	break;
25612      /* Consume the `,'.  */
25613      cp_lexer_consume_token (parser->lexer);
25614    }
25615
25616  /* PARSER->SCOPE may still be non-NULL at this point, if the last
25617     base class had a qualified name.  However, the next name that
25618     appears is certainly not qualified.  */
25619  parser->scope = NULL_TREE;
25620  parser->qualifying_scope = NULL_TREE;
25621  parser->object_scope = NULL_TREE;
25622
25623  return nreverse (bases);
25624}
25625
25626/* Parse a base-specifier.
25627
25628   base-specifier:
25629     :: [opt] nested-name-specifier [opt] class-name
25630     virtual access-specifier [opt] :: [opt] nested-name-specifier
25631       [opt] class-name
25632     access-specifier virtual [opt] :: [opt] nested-name-specifier
25633       [opt] class-name
25634
25635   Returns a TREE_LIST.  The TREE_PURPOSE will be one of
25636   ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25637   indicate the specifiers provided.  The TREE_VALUE will be a TYPE
25638   (or the ERROR_MARK_NODE) indicating the type that was specified.  */
25639
25640static tree
25641cp_parser_base_specifier (cp_parser* parser)
25642{
25643  cp_token *token;
25644  bool done = false;
25645  bool virtual_p = false;
25646  bool duplicate_virtual_error_issued_p = false;
25647  bool duplicate_access_error_issued_p = false;
25648  bool class_scope_p, template_p;
25649  tree access = access_default_node;
25650  tree type;
25651
25652  /* Process the optional `virtual' and `access-specifier'.  */
25653  while (!done)
25654    {
25655      /* Peek at the next token.  */
25656      token = cp_lexer_peek_token (parser->lexer);
25657      /* Process `virtual'.  */
25658      switch (token->keyword)
25659	{
25660	case RID_VIRTUAL:
25661	  /* If `virtual' appears more than once, issue an error.  */
25662	  if (virtual_p && !duplicate_virtual_error_issued_p)
25663	    {
25664	      cp_parser_error (parser,
25665			       "%<virtual%> specified more than once in base-specifier");
25666	      duplicate_virtual_error_issued_p = true;
25667	    }
25668
25669	  virtual_p = true;
25670
25671	  /* Consume the `virtual' token.  */
25672	  cp_lexer_consume_token (parser->lexer);
25673
25674	  break;
25675
25676	case RID_PUBLIC:
25677	case RID_PROTECTED:
25678	case RID_PRIVATE:
25679	  /* If more than one access specifier appears, issue an
25680	     error.  */
25681	  if (access != access_default_node
25682	      && !duplicate_access_error_issued_p)
25683	    {
25684	      cp_parser_error (parser,
25685			       "more than one access specifier in base-specifier");
25686	      duplicate_access_error_issued_p = true;
25687	    }
25688
25689	  access = ridpointers[(int) token->keyword];
25690
25691	  /* Consume the access-specifier.  */
25692	  cp_lexer_consume_token (parser->lexer);
25693
25694	  break;
25695
25696	default:
25697	  done = true;
25698	  break;
25699	}
25700    }
25701  /* It is not uncommon to see programs mechanically, erroneously, use
25702     the 'typename' keyword to denote (dependent) qualified types
25703     as base classes.  */
25704  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25705    {
25706      token = cp_lexer_peek_token (parser->lexer);
25707      if (!processing_template_decl)
25708	error_at (token->location,
25709		  "keyword %<typename%> not allowed outside of templates");
25710      else
25711	error_at (token->location,
25712		  "keyword %<typename%> not allowed in this context "
25713		  "(the base class is implicitly a type)");
25714      cp_lexer_consume_token (parser->lexer);
25715    }
25716
25717  /* Look for the optional `::' operator.  */
25718  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25719  /* Look for the nested-name-specifier.  The simplest way to
25720     implement:
25721
25722       [temp.res]
25723
25724       The keyword `typename' is not permitted in a base-specifier or
25725       mem-initializer; in these contexts a qualified name that
25726       depends on a template-parameter is implicitly assumed to be a
25727       type name.
25728
25729     is to pretend that we have seen the `typename' keyword at this
25730     point.  */
25731  cp_parser_nested_name_specifier_opt (parser,
25732				       /*typename_keyword_p=*/true,
25733				       /*check_dependency_p=*/true,
25734				       /*type_p=*/true,
25735				       /*is_declaration=*/true);
25736  /* If the base class is given by a qualified name, assume that names
25737     we see are type names or templates, as appropriate.  */
25738  class_scope_p = (parser->scope && TYPE_P (parser->scope));
25739  template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25740
25741  if (!parser->scope
25742      && cp_lexer_next_token_is_decltype (parser->lexer))
25743    /* DR 950 allows decltype as a base-specifier.  */
25744    type = cp_parser_decltype (parser);
25745  else
25746    {
25747      /* Otherwise, look for the class-name.  */
25748      type = cp_parser_class_name (parser,
25749				   class_scope_p,
25750				   template_p,
25751				   typename_type,
25752				   /*check_dependency_p=*/true,
25753				   /*class_head_p=*/false,
25754				   /*is_declaration=*/true);
25755      type = TREE_TYPE (type);
25756    }
25757
25758  if (type == error_mark_node)
25759    return error_mark_node;
25760
25761  return finish_base_specifier (type, access, virtual_p);
25762}
25763
25764/* Exception handling [gram.exception] */
25765
25766/* Save the tokens that make up the noexcept-specifier for a member-function.
25767   Returns a DEFERRED_PARSE.  */
25768
25769static tree
25770cp_parser_save_noexcept (cp_parser *parser)
25771{
25772  cp_token *first = parser->lexer->next_token;
25773  /* We want everything up to, including, the final ')'.  */
25774  cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
25775  cp_token *last = parser->lexer->next_token;
25776
25777  /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
25778     to carry the information we will need.  */
25779  tree expr = make_node (DEFERRED_PARSE);
25780  /* Save away the noexcept-specifier; we will process it when the
25781     class is complete.  */
25782  DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
25783  expr = build_tree_list (expr, NULL_TREE);
25784  return expr;
25785}
25786
25787/* Used for late processing of noexcept-specifiers of member-functions.
25788   DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
25789   we saved for later; parse it now.  DECL is the declaration of the
25790   member function.  */
25791
25792static tree
25793cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg,
25794				   tree decl)
25795{
25796  /* Make sure we've gotten something that hasn't been parsed yet.  */
25797  gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
25798
25799  push_unparsed_function_queues (parser);
25800
25801  /* Push the saved tokens for the noexcept-specifier onto the parser's
25802     lexer stack.  */
25803  cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
25804  cp_parser_push_lexer_for_tokens (parser, tokens);
25805
25806  /* We need to know if this member function was declared `const'.  Look
25807     at the this parameter to figure that out.  */
25808  cp_cv_quals quals = type_memfn_quals (TREE_TYPE (decl));
25809  /* Parse the cached noexcept-specifier.  */
25810  tree parsed_arg
25811    = cp_parser_noexcept_specification_opt (parser,
25812					    CP_PARSER_FLAGS_NONE,
25813					    /*require_constexpr=*/true,
25814					    /*consumed_expr=*/NULL,
25815					    /*return_cond=*/false, quals);
25816
25817  /* Revert to the main lexer.  */
25818  cp_parser_pop_lexer (parser);
25819
25820  /* Restore the queue.  */
25821  pop_unparsed_function_queues (parser);
25822
25823  /* And we're done.  */
25824  return parsed_arg;
25825}
25826
25827/* Perform late checking of overriding function with respect to their
25828   noexcept-specifiers.  TYPE is the class and FNDECL is the function
25829   that potentially overrides some virtual function with the same
25830   signature.  */
25831
25832static void
25833noexcept_override_late_checks (tree type, tree fndecl)
25834{
25835  tree binfo = TYPE_BINFO (type);
25836  tree base_binfo;
25837
25838  if (DECL_STATIC_FUNCTION_P (fndecl))
25839    return;
25840
25841  for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
25842    {
25843      tree basetype = BINFO_TYPE (base_binfo);
25844
25845      if (!TYPE_POLYMORPHIC_P (basetype))
25846	continue;
25847
25848      tree fn = look_for_overrides_here (basetype, fndecl);
25849      if (fn)
25850	maybe_check_overriding_exception_spec (fndecl, fn);
25851    }
25852}
25853
25854/* Parse an (optional) noexcept-specification.
25855
25856   noexcept-specification:
25857     noexcept ( constant-expression ) [opt]
25858
25859   If no noexcept-specification is present, returns NULL_TREE.
25860   Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25861   expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25862   there are no parentheses.  CONSUMED_EXPR will be set accordingly.
25863   Otherwise, returns a noexcept specification unless RETURN_COND is true,
25864   in which case a boolean condition is returned instead.  The parser flags
25865   FLAGS is used to control parsing.  QUALS are qualifiers indicating whether
25866   the (member) function is `const'.  */
25867
25868static tree
25869cp_parser_noexcept_specification_opt (cp_parser* parser,
25870				      cp_parser_flags flags,
25871				      bool require_constexpr,
25872				      bool* consumed_expr,
25873				      bool return_cond,
25874				      cp_cv_quals quals)
25875{
25876  cp_token *token;
25877  const char *saved_message;
25878
25879  /* Peek at the next token.  */
25880  token = cp_lexer_peek_token (parser->lexer);
25881
25882  /* Is it a noexcept-specification?  */
25883  if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25884    {
25885      tree expr;
25886
25887      /* [class.mem]/6 says that a noexcept-specifer (within the
25888	 member-specification of the class) is a complete-class context of
25889	 a class.  So, if the noexcept-specifier has the optional expression,
25890	 just save the tokens, and reparse this after we're done with the
25891	 class.  */
25892      const bool literal_p
25893	= ((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
25894	    || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
25895	   && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN));
25896
25897      if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
25898	  /* No need to delay parsing for a number literal or true/false.  */
25899	  && !literal_p
25900	  && at_class_scope_p ()
25901	  /* We don't delay parsing for friend member functions,
25902	     alias-declarations, and typedefs, even though the standard seems
25903	     to require it.  */
25904	  && (flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
25905	  && TYPE_BEING_DEFINED (current_class_type)
25906	  && !LAMBDA_TYPE_P (current_class_type))
25907	return cp_parser_save_noexcept (parser);
25908
25909      cp_lexer_consume_token (parser->lexer);
25910
25911      if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25912	{
25913	  matching_parens parens;
25914	  parens.consume_open (parser);
25915
25916	  tree save_ccp = current_class_ptr;
25917	  tree save_ccr = current_class_ref;
25918
25919	  if (current_class_type)
25920	    inject_this_parameter (current_class_type, quals);
25921
25922	  if (require_constexpr)
25923	    {
25924	      /* Types may not be defined in an exception-specification.  */
25925	      saved_message = parser->type_definition_forbidden_message;
25926	      parser->type_definition_forbidden_message
25927	      = G_("types may not be defined in an exception-specification");
25928
25929	      bool non_constant_p;
25930	      expr
25931		= cp_parser_constant_expression (parser,
25932						 /*allow_non_constant=*/true,
25933						 &non_constant_p);
25934	      if (non_constant_p
25935		  && !require_potential_rvalue_constant_expression (expr))
25936		{
25937		  expr = NULL_TREE;
25938		  return_cond = true;
25939		}
25940
25941	      /* Restore the saved message.  */
25942	      parser->type_definition_forbidden_message = saved_message;
25943	    }
25944	  else
25945	    {
25946	      expr = cp_parser_expression (parser);
25947	      *consumed_expr = true;
25948	    }
25949
25950	  parens.require_close (parser);
25951
25952	  current_class_ptr = save_ccp;
25953	  current_class_ref = save_ccr;
25954	}
25955      else
25956	{
25957	  expr = boolean_true_node;
25958	  if (!require_constexpr)
25959	    *consumed_expr = false;
25960	}
25961
25962      /* We cannot build a noexcept-spec right away because this will check
25963	 that expr is a constexpr.  */
25964      if (!return_cond)
25965	return build_noexcept_spec (expr, tf_warning_or_error);
25966      else
25967	return expr;
25968    }
25969  else
25970    return NULL_TREE;
25971}
25972
25973/* Parse an (optional) exception-specification.
25974
25975   exception-specification:
25976     throw ( type-id-list [opt] )
25977
25978   Returns a TREE_LIST representing the exception-specification.  The
25979   TREE_VALUE of each node is a type.  The parser flags FLAGS is used to
25980   control parsing.  QUALS are qualifiers indicating whether the (member)
25981   function is `const'.  */
25982
25983static tree
25984cp_parser_exception_specification_opt (cp_parser* parser,
25985				       cp_parser_flags flags,
25986				       cp_cv_quals quals)
25987{
25988  cp_token *token;
25989  tree type_id_list;
25990  const char *saved_message;
25991
25992  /* Peek at the next token.  */
25993  token = cp_lexer_peek_token (parser->lexer);
25994
25995  /* Is it a noexcept-specification?  */
25996  type_id_list
25997    = cp_parser_noexcept_specification_opt (parser, flags,
25998					    /*require_constexpr=*/true,
25999					    /*consumed_expr=*/NULL,
26000					    /*return_cond=*/false, quals);
26001  if (type_id_list != NULL_TREE)
26002    return type_id_list;
26003
26004  /* If it's not `throw', then there's no exception-specification.  */
26005  if (!cp_parser_is_keyword (token, RID_THROW))
26006    return NULL_TREE;
26007
26008  location_t loc = token->location;
26009
26010  /* Consume the `throw'.  */
26011  cp_lexer_consume_token (parser->lexer);
26012
26013  /* Look for the `('.  */
26014  matching_parens parens;
26015  parens.require_open (parser);
26016
26017  /* Peek at the next token.  */
26018  token = cp_lexer_peek_token (parser->lexer);
26019  /* If it's not a `)', then there is a type-id-list.  */
26020  if (token->type != CPP_CLOSE_PAREN)
26021    {
26022      /* Types may not be defined in an exception-specification.  */
26023      saved_message = parser->type_definition_forbidden_message;
26024      parser->type_definition_forbidden_message
26025	= G_("types may not be defined in an exception-specification");
26026      /* Parse the type-id-list.  */
26027      type_id_list = cp_parser_type_id_list (parser);
26028      /* Restore the saved message.  */
26029      parser->type_definition_forbidden_message = saved_message;
26030
26031      if (cxx_dialect >= cxx17)
26032	{
26033	  error_at (loc, "ISO C++17 does not allow dynamic exception "
26034			 "specifications");
26035	  type_id_list = NULL_TREE;
26036	}
26037      else if (cxx_dialect >= cxx11)
26038	warning_at (loc, OPT_Wdeprecated,
26039		    "dynamic exception specifications are deprecated in "
26040		    "C++11");
26041    }
26042  /* In C++17, throw() is equivalent to noexcept (true).  throw()
26043     is deprecated in C++11 and above as well, but is still widely used,
26044     so don't warn about it yet.  */
26045  else if (cxx_dialect >= cxx17)
26046    type_id_list = noexcept_true_spec;
26047  else
26048    type_id_list = empty_except_spec;
26049
26050  /* Look for the `)'.  */
26051  parens.require_close (parser);
26052
26053  return type_id_list;
26054}
26055
26056/* Parse an (optional) type-id-list.
26057
26058   type-id-list:
26059     type-id ... [opt]
26060     type-id-list , type-id ... [opt]
26061
26062   Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
26063   in the order that the types were presented.  */
26064
26065static tree
26066cp_parser_type_id_list (cp_parser* parser)
26067{
26068  tree types = NULL_TREE;
26069
26070  while (true)
26071    {
26072      cp_token *token;
26073      tree type;
26074
26075      token = cp_lexer_peek_token (parser->lexer);
26076
26077      /* Get the next type-id.  */
26078      type = cp_parser_type_id (parser);
26079      /* Check for invalid 'auto'.  */
26080      if (flag_concepts && type_uses_auto (type))
26081	{
26082	  error_at (token->location,
26083		    "invalid use of %<auto%> in exception-specification");
26084	  type = error_mark_node;
26085	}
26086      /* Parse the optional ellipsis. */
26087      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26088        {
26089          /* Consume the `...'. */
26090          cp_lexer_consume_token (parser->lexer);
26091
26092          /* Turn the type into a pack expansion expression. */
26093          type = make_pack_expansion (type);
26094        }
26095      /* Add it to the list.  */
26096      types = add_exception_specifier (types, type, /*complain=*/1);
26097      /* Peek at the next token.  */
26098      token = cp_lexer_peek_token (parser->lexer);
26099      /* If it is not a `,', we are done.  */
26100      if (token->type != CPP_COMMA)
26101	break;
26102      /* Consume the `,'.  */
26103      cp_lexer_consume_token (parser->lexer);
26104    }
26105
26106  return nreverse (types);
26107}
26108
26109/* Parse a try-block.
26110
26111   try-block:
26112     try compound-statement handler-seq  */
26113
26114static tree
26115cp_parser_try_block (cp_parser* parser)
26116{
26117  tree try_block;
26118
26119  cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
26120  if (parser->in_function_body
26121      && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
26122      && cxx_dialect < cxx2a)
26123    pedwarn (input_location, 0,
26124	     "%<try%> in %<constexpr%> function only "
26125	     "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
26126
26127  try_block = begin_try_block ();
26128  cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
26129  finish_try_block (try_block);
26130  cp_parser_handler_seq (parser);
26131  finish_handler_sequence (try_block);
26132
26133  return try_block;
26134}
26135
26136/* Parse a function-try-block.
26137
26138   function-try-block:
26139     try ctor-initializer [opt] function-body handler-seq  */
26140
26141static void
26142cp_parser_function_try_block (cp_parser* parser)
26143{
26144  tree compound_stmt;
26145  tree try_block;
26146
26147  /* Look for the `try' keyword.  */
26148  if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
26149    return;
26150  /* Let the rest of the front end know where we are.  */
26151  try_block = begin_function_try_block (&compound_stmt);
26152  /* Parse the function-body.  */
26153  cp_parser_ctor_initializer_opt_and_function_body
26154    (parser, /*in_function_try_block=*/true);
26155  /* We're done with the `try' part.  */
26156  finish_function_try_block (try_block);
26157  /* Parse the handlers.  */
26158  cp_parser_handler_seq (parser);
26159  /* We're done with the handlers.  */
26160  finish_function_handler_sequence (try_block, compound_stmt);
26161}
26162
26163/* Parse a handler-seq.
26164
26165   handler-seq:
26166     handler handler-seq [opt]  */
26167
26168static void
26169cp_parser_handler_seq (cp_parser* parser)
26170{
26171  while (true)
26172    {
26173      cp_token *token;
26174
26175      /* Parse the handler.  */
26176      cp_parser_handler (parser);
26177      /* Peek at the next token.  */
26178      token = cp_lexer_peek_token (parser->lexer);
26179      /* If it's not `catch' then there are no more handlers.  */
26180      if (!cp_parser_is_keyword (token, RID_CATCH))
26181	break;
26182    }
26183}
26184
26185/* Parse a handler.
26186
26187   handler:
26188     catch ( exception-declaration ) compound-statement  */
26189
26190static void
26191cp_parser_handler (cp_parser* parser)
26192{
26193  tree handler;
26194  tree declaration;
26195
26196  cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
26197  handler = begin_handler ();
26198  matching_parens parens;
26199  parens.require_open (parser);
26200  declaration = cp_parser_exception_declaration (parser);
26201  finish_handler_parms (declaration, handler);
26202  parens.require_close (parser);
26203  cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
26204  finish_handler (handler);
26205}
26206
26207/* Parse an exception-declaration.
26208
26209   exception-declaration:
26210     type-specifier-seq declarator
26211     type-specifier-seq abstract-declarator
26212     type-specifier-seq
26213     ...
26214
26215   Returns a VAR_DECL for the declaration, or NULL_TREE if the
26216   ellipsis variant is used.  */
26217
26218static tree
26219cp_parser_exception_declaration (cp_parser* parser)
26220{
26221  cp_decl_specifier_seq type_specifiers;
26222  cp_declarator *declarator;
26223  const char *saved_message;
26224
26225  /* If it's an ellipsis, it's easy to handle.  */
26226  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26227    {
26228      /* Consume the `...' token.  */
26229      cp_lexer_consume_token (parser->lexer);
26230      return NULL_TREE;
26231    }
26232
26233  /* Types may not be defined in exception-declarations.  */
26234  saved_message = parser->type_definition_forbidden_message;
26235  parser->type_definition_forbidden_message
26236    = G_("types may not be defined in exception-declarations");
26237
26238  /* Parse the type-specifier-seq.  */
26239  cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
26240				/*is_declaration=*/true,
26241				/*is_trailing_return=*/false,
26242				&type_specifiers);
26243  /* If it's a `)', then there is no declarator.  */
26244  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26245    declarator = NULL;
26246  else
26247    declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
26248				       CP_PARSER_FLAGS_NONE,
26249				       /*ctor_dtor_or_conv_p=*/NULL,
26250				       /*parenthesized_p=*/NULL,
26251				       /*member_p=*/false,
26252				       /*friend_p=*/false,
26253				       /*static_p=*/false);
26254
26255  /* Restore the saved message.  */
26256  parser->type_definition_forbidden_message = saved_message;
26257
26258  if (!type_specifiers.any_specifiers_p)
26259    return error_mark_node;
26260
26261  return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
26262}
26263
26264/* Parse a throw-expression.
26265
26266   throw-expression:
26267     throw assignment-expression [opt]
26268
26269   Returns a THROW_EXPR representing the throw-expression.  */
26270
26271static tree
26272cp_parser_throw_expression (cp_parser* parser)
26273{
26274  tree expression;
26275  cp_token* token;
26276  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26277
26278  cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
26279  token = cp_lexer_peek_token (parser->lexer);
26280  /* Figure out whether or not there is an assignment-expression
26281     following the "throw" keyword.  */
26282  if (token->type == CPP_COMMA
26283      || token->type == CPP_SEMICOLON
26284      || token->type == CPP_CLOSE_PAREN
26285      || token->type == CPP_CLOSE_SQUARE
26286      || token->type == CPP_CLOSE_BRACE
26287      || token->type == CPP_COLON)
26288    expression = NULL_TREE;
26289  else
26290    expression = cp_parser_assignment_expression (parser);
26291
26292  /* Construct a location e.g.:
26293       throw x
26294       ^~~~~~~
26295     with caret == start at the start of the "throw" token, and
26296     the end at the end of the final token we consumed.  */
26297  location_t combined_loc = make_location (start_loc, start_loc,
26298					   parser->lexer);
26299  expression = build_throw (combined_loc, expression);
26300
26301  return expression;
26302}
26303
26304/* Parse a yield-expression.
26305
26306   yield-expression:
26307     co_yield assignment-expression
26308     co_yield braced-init-list
26309
26310   Returns a CO_YIELD_EXPR representing the yield-expression.  */
26311
26312static tree
26313cp_parser_yield_expression (cp_parser* parser)
26314{
26315  tree expr;
26316
26317  cp_token *token = cp_lexer_peek_token (parser->lexer);
26318  location_t kw_loc = token->location; /* Save for later.  */
26319
26320  cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
26321
26322  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26323    {
26324      bool expr_non_constant_p;
26325      cp_lexer_set_source_position (parser->lexer);
26326      /* ??? : probably a moot point?  */
26327      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26328      expr = cp_parser_braced_list (parser, &expr_non_constant_p);
26329    }
26330  else
26331    expr = cp_parser_assignment_expression (parser);
26332
26333  if (expr == error_mark_node)
26334    return expr;
26335
26336  return finish_co_yield_expr (kw_loc, expr);
26337}
26338
26339/* GNU Extensions */
26340
26341/* Parse an (optional) asm-specification.
26342
26343   asm-specification:
26344     asm ( string-literal )
26345
26346   If the asm-specification is present, returns a STRING_CST
26347   corresponding to the string-literal.  Otherwise, returns
26348   NULL_TREE.  */
26349
26350static tree
26351cp_parser_asm_specification_opt (cp_parser* parser)
26352{
26353  cp_token *token;
26354  tree asm_specification;
26355
26356  /* Peek at the next token.  */
26357  token = cp_lexer_peek_token (parser->lexer);
26358  /* If the next token isn't the `asm' keyword, then there's no
26359     asm-specification.  */
26360  if (!cp_parser_is_keyword (token, RID_ASM))
26361    return NULL_TREE;
26362
26363  /* Consume the `asm' token.  */
26364  cp_lexer_consume_token (parser->lexer);
26365  /* Look for the `('.  */
26366  matching_parens parens;
26367  parens.require_open (parser);
26368
26369  /* Look for the string-literal.  */
26370  asm_specification = cp_parser_string_literal (parser, false, false);
26371
26372  /* Look for the `)'.  */
26373  parens.require_close (parser);
26374
26375  return asm_specification;
26376}
26377
26378/* Parse an asm-operand-list.
26379
26380   asm-operand-list:
26381     asm-operand
26382     asm-operand-list , asm-operand
26383
26384   asm-operand:
26385     string-literal ( expression )
26386     [ string-literal ] string-literal ( expression )
26387
26388   Returns a TREE_LIST representing the operands.  The TREE_VALUE of
26389   each node is the expression.  The TREE_PURPOSE is itself a
26390   TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
26391   string-literal (or NULL_TREE if not present) and whose TREE_VALUE
26392   is a STRING_CST for the string literal before the parenthesis. Returns
26393   ERROR_MARK_NODE if any of the operands are invalid.  */
26394
26395static tree
26396cp_parser_asm_operand_list (cp_parser* parser)
26397{
26398  tree asm_operands = NULL_TREE;
26399  bool invalid_operands = false;
26400
26401  while (true)
26402    {
26403      tree string_literal;
26404      tree expression;
26405      tree name;
26406
26407      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26408	{
26409	  /* Consume the `[' token.  */
26410	  cp_lexer_consume_token (parser->lexer);
26411	  /* Read the operand name.  */
26412	  name = cp_parser_identifier (parser);
26413	  if (name != error_mark_node)
26414	    name = build_string (IDENTIFIER_LENGTH (name),
26415				 IDENTIFIER_POINTER (name));
26416	  /* Look for the closing `]'.  */
26417	  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26418	}
26419      else
26420	name = NULL_TREE;
26421      /* Look for the string-literal.  */
26422      string_literal = cp_parser_string_literal (parser, false, false);
26423
26424      /* Look for the `('.  */
26425      matching_parens parens;
26426      parens.require_open (parser);
26427      /* Parse the expression.  */
26428      expression = cp_parser_expression (parser);
26429      /* Look for the `)'.  */
26430      parens.require_close (parser);
26431
26432      if (name == error_mark_node
26433	  || string_literal == error_mark_node
26434	  || expression == error_mark_node)
26435        invalid_operands = true;
26436
26437      /* Add this operand to the list.  */
26438      asm_operands = tree_cons (build_tree_list (name, string_literal),
26439				expression,
26440				asm_operands);
26441      /* If the next token is not a `,', there are no more
26442	 operands.  */
26443      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26444	break;
26445      /* Consume the `,'.  */
26446      cp_lexer_consume_token (parser->lexer);
26447    }
26448
26449  return invalid_operands ? error_mark_node : nreverse (asm_operands);
26450}
26451
26452/* Parse an asm-clobber-list.
26453
26454   asm-clobber-list:
26455     string-literal
26456     asm-clobber-list , string-literal
26457
26458   Returns a TREE_LIST, indicating the clobbers in the order that they
26459   appeared.  The TREE_VALUE of each node is a STRING_CST.  */
26460
26461static tree
26462cp_parser_asm_clobber_list (cp_parser* parser)
26463{
26464  tree clobbers = NULL_TREE;
26465
26466  while (true)
26467    {
26468      tree string_literal;
26469
26470      /* Look for the string literal.  */
26471      string_literal = cp_parser_string_literal (parser, false, false);
26472      /* Add it to the list.  */
26473      clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
26474      /* If the next token is not a `,', then the list is
26475	 complete.  */
26476      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26477	break;
26478      /* Consume the `,' token.  */
26479      cp_lexer_consume_token (parser->lexer);
26480    }
26481
26482  return clobbers;
26483}
26484
26485/* Parse an asm-label-list.
26486
26487   asm-label-list:
26488     identifier
26489     asm-label-list , identifier
26490
26491   Returns a TREE_LIST, indicating the labels in the order that they
26492   appeared.  The TREE_VALUE of each node is a label.  */
26493
26494static tree
26495cp_parser_asm_label_list (cp_parser* parser)
26496{
26497  tree labels = NULL_TREE;
26498
26499  while (true)
26500    {
26501      tree identifier, label, name;
26502
26503      /* Look for the identifier.  */
26504      identifier = cp_parser_identifier (parser);
26505      if (!error_operand_p (identifier))
26506        {
26507	  label = lookup_label (identifier);
26508	  if (TREE_CODE (label) == LABEL_DECL)
26509	    {
26510	      TREE_USED (label) = 1;
26511	      check_goto (label);
26512	      name = build_string (IDENTIFIER_LENGTH (identifier),
26513				   IDENTIFIER_POINTER (identifier));
26514	      labels = tree_cons (name, label, labels);
26515	    }
26516	}
26517      /* If the next token is not a `,', then the list is
26518	 complete.  */
26519      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26520	break;
26521      /* Consume the `,' token.  */
26522      cp_lexer_consume_token (parser->lexer);
26523    }
26524
26525  return nreverse (labels);
26526}
26527
26528/* Return TRUE iff the next tokens in the stream are possibly the
26529   beginning of a GNU extension attribute. */
26530
26531static bool
26532cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
26533{
26534  return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
26535}
26536
26537/* Return TRUE iff the next tokens in the stream are possibly the
26538   beginning of a standard C++-11 attribute specifier.  */
26539
26540static bool
26541cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
26542{
26543  return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
26544}
26545
26546/* Return TRUE iff the next Nth tokens in the stream are possibly the
26547   beginning of a standard C++-11 attribute specifier.  */
26548
26549static bool
26550cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
26551{
26552  cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26553
26554  return (cxx_dialect >= cxx11
26555	  && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
26556	      || (token->type == CPP_OPEN_SQUARE
26557		  && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
26558		  && token->type == CPP_OPEN_SQUARE)));
26559}
26560
26561/* Return TRUE iff the next Nth tokens in the stream are possibly the
26562   beginning of a GNU extension attribute.  */
26563
26564static bool
26565cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
26566{
26567  cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26568
26569  return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
26570}
26571
26572/* Return true iff the next tokens can be the beginning of either a
26573   GNU attribute list, or a standard C++11 attribute sequence.  */
26574
26575static bool
26576cp_next_tokens_can_be_attribute_p (cp_parser *parser)
26577{
26578  return (cp_next_tokens_can_be_gnu_attribute_p (parser)
26579	  || cp_next_tokens_can_be_std_attribute_p (parser));
26580}
26581
26582/* Return true iff the next Nth tokens can be the beginning of either
26583   a GNU attribute list, or a standard C++11 attribute sequence.  */
26584
26585static bool
26586cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
26587{
26588  return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
26589	  || cp_nth_tokens_can_be_std_attribute_p (parser, n));
26590}
26591
26592/* Parse either a standard C++-11 attribute-specifier-seq, or a series
26593   of GNU attributes, or return NULL.  */
26594
26595static tree
26596cp_parser_attributes_opt (cp_parser *parser)
26597{
26598  if (cp_next_tokens_can_be_gnu_attribute_p (parser))
26599    return cp_parser_gnu_attributes_opt (parser);
26600  return cp_parser_std_attribute_spec_seq (parser);
26601}
26602
26603/* Parse an (optional) series of attributes.
26604
26605   attributes:
26606     attributes attribute
26607
26608   attribute:
26609     __attribute__ (( attribute-list [opt] ))
26610
26611   The return value is as for cp_parser_gnu_attribute_list.  */
26612
26613static tree
26614cp_parser_gnu_attributes_opt (cp_parser* parser)
26615{
26616  tree attributes = NULL_TREE;
26617
26618  temp_override<bool> cleanup
26619    (parser->auto_is_implicit_function_template_parm_p, false);
26620
26621  while (true)
26622    {
26623      cp_token *token;
26624      tree attribute_list;
26625      bool ok = true;
26626
26627      /* Peek at the next token.  */
26628      token = cp_lexer_peek_token (parser->lexer);
26629      /* If it's not `__attribute__', then we're done.  */
26630      if (token->keyword != RID_ATTRIBUTE)
26631	break;
26632
26633      /* Consume the `__attribute__' keyword.  */
26634      cp_lexer_consume_token (parser->lexer);
26635      /* Look for the two `(' tokens.  */
26636      matching_parens outer_parens;
26637      if (!outer_parens.require_open (parser))
26638	ok = false;
26639      matching_parens inner_parens;
26640      if (!inner_parens.require_open (parser))
26641	ok = false;
26642
26643      /* Peek at the next token.  */
26644      token = cp_lexer_peek_token (parser->lexer);
26645      if (token->type != CPP_CLOSE_PAREN)
26646	/* Parse the attribute-list.  */
26647	attribute_list = cp_parser_gnu_attribute_list (parser);
26648      else
26649	/* If the next token is a `)', then there is no attribute
26650	   list.  */
26651	attribute_list = NULL;
26652
26653      /* Look for the two `)' tokens.  */
26654      if (!inner_parens.require_close (parser))
26655	ok = false;
26656      if (!outer_parens.require_close (parser))
26657	ok = false;
26658      if (!ok)
26659	cp_parser_skip_to_end_of_statement (parser);
26660
26661      /* Add these new attributes to the list.  */
26662      attributes = attr_chainon (attributes, attribute_list);
26663    }
26664
26665  return attributes;
26666}
26667
26668/* Parse a GNU attribute-list.
26669
26670   attribute-list:
26671     attribute
26672     attribute-list , attribute
26673
26674   attribute:
26675     identifier
26676     identifier ( identifier )
26677     identifier ( identifier , expression-list )
26678     identifier ( expression-list )
26679
26680   Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
26681   to an attribute.  The TREE_PURPOSE of each node is the identifier
26682   indicating which attribute is in use.  The TREE_VALUE represents
26683   the arguments, if any.  */
26684
26685static tree
26686cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
26687{
26688  tree attribute_list = NULL_TREE;
26689  bool save_translate_strings_p = parser->translate_strings_p;
26690
26691  /* Don't create wrapper nodes within attributes: the
26692     handlers don't know how to handle them.  */
26693  auto_suppress_location_wrappers sentinel;
26694
26695  parser->translate_strings_p = false;
26696  while (true)
26697    {
26698      cp_token *token;
26699      tree identifier;
26700      tree attribute;
26701
26702      /* Look for the identifier.  We also allow keywords here; for
26703	 example `__attribute__ ((const))' is legal.  */
26704      token = cp_lexer_peek_token (parser->lexer);
26705      if (token->type == CPP_NAME
26706	  || token->type == CPP_KEYWORD)
26707	{
26708	  tree arguments = NULL_TREE;
26709
26710	  /* Consume the token, but save it since we need it for the
26711	     SIMD enabled function parsing.  */
26712	  cp_token *id_token = cp_lexer_consume_token (parser->lexer);
26713
26714	  /* Save away the identifier that indicates which attribute
26715	     this is.  */
26716	  identifier = (token->type == CPP_KEYWORD)
26717	    /* For keywords, use the canonical spelling, not the
26718	       parsed identifier.  */
26719	    ? ridpointers[(int) token->keyword]
26720	    : id_token->u.value;
26721
26722	  identifier = canonicalize_attr_name (identifier);
26723	  attribute = build_tree_list (identifier, NULL_TREE);
26724
26725	  /* Peek at the next token.  */
26726	  token = cp_lexer_peek_token (parser->lexer);
26727	  /* If it's an `(', then parse the attribute arguments.  */
26728	  if (token->type == CPP_OPEN_PAREN)
26729	    {
26730	      vec<tree, va_gc> *vec;
26731	      int attr_flag = (attribute_takes_identifier_p (identifier)
26732			       ? id_attr : normal_attr);
26733	      vec = cp_parser_parenthesized_expression_list
26734		    (parser, attr_flag, /*cast_p=*/false,
26735		    /*allow_expansion_p=*/false,
26736		    /*non_constant_p=*/NULL);
26737	      if (vec == NULL)
26738		arguments = error_mark_node;
26739	      else
26740		{
26741		  arguments = build_tree_list_vec (vec);
26742		  release_tree_vector (vec);
26743		}
26744	      /* Save the arguments away.  */
26745	      TREE_VALUE (attribute) = arguments;
26746	    }
26747
26748	  if (arguments != error_mark_node)
26749	    {
26750	      /* Add this attribute to the list.  */
26751	      TREE_CHAIN (attribute) = attribute_list;
26752	      attribute_list = attribute;
26753	    }
26754
26755	  token = cp_lexer_peek_token (parser->lexer);
26756	}
26757      /* Unless EXACTLY_ONE is set look for more attributes.
26758	 If the next token isn't a `,', we're done.  */
26759      if (exactly_one || token->type != CPP_COMMA)
26760	break;
26761
26762      /* Consume the comma and keep going.  */
26763      cp_lexer_consume_token (parser->lexer);
26764    }
26765  parser->translate_strings_p = save_translate_strings_p;
26766
26767  /* We built up the list in reverse order.  */
26768  return nreverse (attribute_list);
26769}
26770
26771/*  Parse a standard C++11 attribute.
26772
26773    The returned representation is a TREE_LIST which TREE_PURPOSE is
26774    the scoped name of the attribute, and the TREE_VALUE is its
26775    arguments list.
26776
26777    Note that the scoped name of the attribute is itself a TREE_LIST
26778    which TREE_PURPOSE is the namespace of the attribute, and
26779    TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
26780    by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26781    and which TREE_PURPOSE is directly the attribute name.
26782
26783    Clients of the attribute code should use get_attribute_namespace
26784    and get_attribute_name to get the actual namespace and name of
26785    attributes, regardless of their being GNU or C++11 attributes.
26786
26787    attribute:
26788      attribute-token attribute-argument-clause [opt]
26789
26790    attribute-token:
26791      identifier
26792      attribute-scoped-token
26793
26794    attribute-scoped-token:
26795      attribute-namespace :: identifier
26796
26797    attribute-namespace:
26798      identifier
26799
26800    attribute-argument-clause:
26801      ( balanced-token-seq )
26802
26803    balanced-token-seq:
26804      balanced-token [opt]
26805      balanced-token-seq balanced-token
26806
26807    balanced-token:
26808      ( balanced-token-seq )
26809      [ balanced-token-seq ]
26810      { balanced-token-seq }.  */
26811
26812static tree
26813cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26814{
26815  tree attribute, attr_id = NULL_TREE, arguments;
26816  cp_token *token;
26817
26818  temp_override<bool> cleanup
26819    (parser->auto_is_implicit_function_template_parm_p, false);
26820
26821  /* First, parse name of the attribute, a.k.a attribute-token.  */
26822
26823  token = cp_lexer_peek_token (parser->lexer);
26824  if (token->type == CPP_NAME)
26825    attr_id = token->u.value;
26826  else if (token->type == CPP_KEYWORD)
26827    attr_id = ridpointers[(int) token->keyword];
26828  else if (token->flags & NAMED_OP)
26829    attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26830
26831  if (attr_id == NULL_TREE)
26832    return NULL_TREE;
26833
26834  cp_lexer_consume_token (parser->lexer);
26835
26836  token = cp_lexer_peek_token (parser->lexer);
26837  if (token->type == CPP_SCOPE)
26838    {
26839      /* We are seeing a scoped attribute token.  */
26840
26841      cp_lexer_consume_token (parser->lexer);
26842      if (attr_ns)
26843	error_at (token->location, "attribute using prefix used together "
26844				   "with scoped attribute token");
26845      attr_ns = attr_id;
26846
26847      token = cp_lexer_peek_token (parser->lexer);
26848      if (token->type == CPP_NAME)
26849	attr_id = token->u.value;
26850      else if (token->type == CPP_KEYWORD)
26851	attr_id = ridpointers[(int) token->keyword];
26852      else if (token->flags & NAMED_OP)
26853	attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26854      else
26855	{
26856	  error_at (token->location,
26857		    "expected an identifier for the attribute name");
26858	  return error_mark_node;
26859	}
26860      cp_lexer_consume_token (parser->lexer);
26861
26862      attr_ns = canonicalize_attr_name (attr_ns);
26863      attr_id = canonicalize_attr_name (attr_id);
26864      attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26865				   NULL_TREE);
26866      token = cp_lexer_peek_token (parser->lexer);
26867    }
26868  else if (attr_ns)
26869    {
26870      attr_ns = canonicalize_attr_name (attr_ns);
26871      attr_id = canonicalize_attr_name (attr_id);
26872      attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26873				   NULL_TREE);
26874    }
26875  else
26876    {
26877      attr_id = canonicalize_attr_name (attr_id);
26878      attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26879				   NULL_TREE);
26880      /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
26881	 but no longer: we have to be able to tell [[noreturn]] and
26882	 __attribute__((noreturn)) apart.  */
26883      /* C++14 deprecated attribute is equivalent to GNU's.  */
26884      if (is_attribute_p ("deprecated", attr_id))
26885	TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26886      /* C++17 fallthrough attribute is equivalent to GNU's.  */
26887      else if (is_attribute_p ("fallthrough", attr_id))
26888	TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26889      /* Transactional Memory TS optimize_for_synchronized attribute is
26890	 equivalent to GNU transaction_callable.  */
26891      else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26892	TREE_PURPOSE (attribute)
26893	  = get_identifier ("transaction_callable");
26894      /* Transactional Memory attributes are GNU attributes.  */
26895      else if (tm_attr_to_mask (attr_id))
26896	TREE_PURPOSE (attribute) = attr_id;
26897    }
26898
26899  /* Now parse the optional argument clause of the attribute.  */
26900
26901  if (token->type != CPP_OPEN_PAREN)
26902    return attribute;
26903
26904  {
26905    vec<tree, va_gc> *vec;
26906    int attr_flag = normal_attr;
26907
26908    /* Maybe we don't expect to see any arguments for this attribute.  */
26909    const attribute_spec *as
26910      = lookup_attribute_spec (TREE_PURPOSE (attribute));
26911    if (as && as->max_length == 0)
26912      {
26913	error_at (token->location, "%qE attribute does not take any arguments",
26914		  attr_id);
26915	cp_parser_skip_to_closing_parenthesis (parser,
26916					       /*recovering=*/true,
26917					       /*or_comma=*/false,
26918					       /*consume_paren=*/true);
26919	return error_mark_node;
26920      }
26921
26922    if (attr_ns == gnu_identifier
26923	&& attribute_takes_identifier_p (attr_id))
26924      /* A GNU attribute that takes an identifier in parameter.  */
26925      attr_flag = id_attr;
26926
26927    if (as == NULL)
26928      {
26929	/* For unknown attributes, just skip balanced tokens instead of
26930	   trying to parse the arguments.  */
26931	for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
26932	  cp_lexer_consume_token (parser->lexer);
26933	return attribute;
26934      }
26935
26936    vec = cp_parser_parenthesized_expression_list
26937      (parser, attr_flag, /*cast_p=*/false,
26938       /*allow_expansion_p=*/true,
26939       /*non_constant_p=*/NULL);
26940    if (vec == NULL)
26941      arguments = error_mark_node;
26942    else
26943      {
26944	if (vec->is_empty ())
26945	  /* e.g. [[attr()]].  */
26946	  error_at (token->location, "parentheses must be omitted if "
26947		    "%qE attribute argument list is empty",
26948		    attr_id);
26949	arguments = build_tree_list_vec (vec);
26950	release_tree_vector (vec);
26951      }
26952
26953    if (arguments == error_mark_node)
26954      attribute = error_mark_node;
26955    else
26956      TREE_VALUE (attribute) = arguments;
26957  }
26958
26959  return attribute;
26960}
26961
26962/* Check that the attribute ATTRIBUTE appears at most once in the
26963   attribute-list ATTRIBUTES.  This is enforced for noreturn (7.6.3),
26964   nodiscard, and deprecated (7.6.5).  Note that
26965   carries_dependency (7.6.4) isn't implemented yet in GCC.  */
26966
26967static void
26968cp_parser_check_std_attribute (tree attributes, tree attribute)
26969{
26970  if (attributes)
26971    {
26972      tree name = get_attribute_name (attribute);
26973      if (is_attribute_p ("noreturn", name)
26974	  && lookup_attribute ("noreturn", attributes))
26975	error ("attribute %<noreturn%> can appear at most once "
26976	       "in an attribute-list");
26977      else if (is_attribute_p ("deprecated", name)
26978	       && lookup_attribute ("deprecated", attributes))
26979	error ("attribute %<deprecated%> can appear at most once "
26980	       "in an attribute-list");
26981      else if (is_attribute_p ("nodiscard", name)
26982	       && lookup_attribute ("nodiscard", attributes))
26983	error ("attribute %<nodiscard%> can appear at most once "
26984	       "in an attribute-list");
26985    }
26986}
26987
26988/* Parse a list of standard C++-11 attributes.
26989
26990   attribute-list:
26991     attribute [opt]
26992     attribute-list , attribute[opt]
26993     attribute ...
26994     attribute-list , attribute ...
26995*/
26996
26997static tree
26998cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26999{
27000  tree attributes = NULL_TREE, attribute = NULL_TREE;
27001  cp_token *token = NULL;
27002
27003  while (true)
27004    {
27005      attribute = cp_parser_std_attribute (parser, attr_ns);
27006      if (attribute == error_mark_node)
27007	break;
27008      if (attribute != NULL_TREE)
27009	{
27010	  cp_parser_check_std_attribute (attributes, attribute);
27011	  TREE_CHAIN (attribute) = attributes;
27012	  attributes = attribute;
27013	}
27014      token = cp_lexer_peek_token (parser->lexer);
27015      if (token->type == CPP_ELLIPSIS)
27016	{
27017	  cp_lexer_consume_token (parser->lexer);
27018	  if (attribute == NULL_TREE)
27019	    error_at (token->location,
27020		      "expected attribute before %<...%>");
27021	  else
27022	    {
27023	      tree pack = make_pack_expansion (TREE_VALUE (attribute));
27024	      if (pack == error_mark_node)
27025		return error_mark_node;
27026	      TREE_VALUE (attribute) = pack;
27027	    }
27028	  token = cp_lexer_peek_token (parser->lexer);
27029	}
27030      if (token->type != CPP_COMMA)
27031	break;
27032      cp_lexer_consume_token (parser->lexer);
27033    }
27034  attributes = nreverse (attributes);
27035  return attributes;
27036}
27037
27038/* Parse a standard C++-11 attribute specifier.
27039
27040   attribute-specifier:
27041     [ [ attribute-using-prefix [opt] attribute-list ] ]
27042     alignment-specifier
27043
27044   attribute-using-prefix:
27045     using attribute-namespace :
27046
27047   alignment-specifier:
27048     alignas ( type-id ... [opt] )
27049     alignas ( alignment-expression ... [opt] ).  */
27050
27051static tree
27052cp_parser_std_attribute_spec (cp_parser *parser)
27053{
27054  tree attributes = NULL_TREE;
27055  cp_token *token = cp_lexer_peek_token (parser->lexer);
27056
27057  if (token->type == CPP_OPEN_SQUARE
27058      && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
27059    {
27060      tree attr_ns = NULL_TREE;
27061
27062      cp_lexer_consume_token (parser->lexer);
27063      cp_lexer_consume_token (parser->lexer);
27064
27065      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27066	{
27067	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
27068	  if (token->type == CPP_NAME)
27069	    attr_ns = token->u.value;
27070	  else if (token->type == CPP_KEYWORD)
27071	    attr_ns = ridpointers[(int) token->keyword];
27072	  else if (token->flags & NAMED_OP)
27073	    attr_ns = get_identifier (cpp_type2name (token->type,
27074						     token->flags));
27075	  if (attr_ns
27076	      && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
27077	    {
27078	      if (cxx_dialect < cxx17)
27079		pedwarn (input_location, 0,
27080			 "attribute using prefix only available "
27081			 "with %<-std=c++17%> or %<-std=gnu++17%>");
27082
27083	      cp_lexer_consume_token (parser->lexer);
27084	      cp_lexer_consume_token (parser->lexer);
27085	      cp_lexer_consume_token (parser->lexer);
27086	    }
27087	  else
27088	    attr_ns = NULL_TREE;
27089	}
27090
27091      attributes = cp_parser_std_attribute_list (parser, attr_ns);
27092
27093      if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
27094	  || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
27095	cp_parser_skip_to_end_of_statement (parser);
27096      else
27097	/* Warn about parsing c++11 attribute in non-c++11 mode, only
27098	   when we are sure that we have actually parsed them.  */
27099	maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27100    }
27101  else
27102    {
27103      tree alignas_expr;
27104
27105      /* Look for an alignment-specifier.  */
27106
27107      token = cp_lexer_peek_token (parser->lexer);
27108
27109      if (token->type != CPP_KEYWORD
27110	  || token->keyword != RID_ALIGNAS)
27111	return NULL_TREE;
27112
27113      cp_lexer_consume_token (parser->lexer);
27114      maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27115
27116      matching_parens parens;
27117      if (!parens.require_open (parser))
27118	return error_mark_node;
27119
27120      cp_parser_parse_tentatively (parser);
27121      alignas_expr = cp_parser_type_id (parser);
27122
27123      if (!cp_parser_parse_definitely (parser))
27124	{
27125	  alignas_expr = cp_parser_assignment_expression (parser);
27126	  if (alignas_expr == error_mark_node)
27127	    cp_parser_skip_to_end_of_statement (parser);
27128	  if (alignas_expr == NULL_TREE
27129	      || alignas_expr == error_mark_node)
27130	    return alignas_expr;
27131	}
27132
27133      alignas_expr = cxx_alignas_expr (alignas_expr);
27134      alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
27135
27136      /* Handle alignas (pack...).  */
27137      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27138	{
27139	  cp_lexer_consume_token (parser->lexer);
27140	  alignas_expr = make_pack_expansion (alignas_expr);
27141	}
27142
27143      /* Something went wrong, so don't build the attribute.  */
27144      if (alignas_expr == error_mark_node)
27145	return error_mark_node;
27146
27147      /* Missing ')' means the code cannot possibly be valid; go ahead
27148	 and commit to make sure we issue a hard error.  */
27149      if (cp_parser_uncommitted_to_tentative_parse_p (parser)
27150	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27151	cp_parser_commit_to_tentative_parse (parser);
27152
27153      if (!parens.require_close (parser))
27154	return error_mark_node;
27155
27156      /* Build the C++-11 representation of an 'aligned'
27157	 attribute.  */
27158      attributes
27159	= build_tree_list (build_tree_list (gnu_identifier,
27160					    aligned_identifier), alignas_expr);
27161    }
27162
27163  return attributes;
27164}
27165
27166/* Parse a standard C++-11 attribute-specifier-seq.
27167
27168   attribute-specifier-seq:
27169     attribute-specifier-seq [opt] attribute-specifier
27170 */
27171
27172static tree
27173cp_parser_std_attribute_spec_seq (cp_parser *parser)
27174{
27175  tree attr_specs = NULL_TREE;
27176  tree attr_last = NULL_TREE;
27177
27178  /* Don't create wrapper nodes within attributes: the
27179     handlers don't know how to handle them.  */
27180  auto_suppress_location_wrappers sentinel;
27181
27182  while (true)
27183    {
27184      tree attr_spec = cp_parser_std_attribute_spec (parser);
27185      if (attr_spec == NULL_TREE)
27186	break;
27187      if (attr_spec == error_mark_node)
27188	return error_mark_node;
27189
27190      if (attr_last)
27191	TREE_CHAIN (attr_last) = attr_spec;
27192      else
27193	attr_specs = attr_last = attr_spec;
27194      attr_last = tree_last (attr_last);
27195    }
27196
27197  return attr_specs;
27198}
27199
27200/* Skip a balanced-token starting at Nth token (with 1 as the next token),
27201   return index of the first token after balanced-token, or N on failure.  */
27202
27203static size_t
27204cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
27205{
27206  size_t orig_n = n;
27207  int nparens = 0, nbraces = 0, nsquares = 0;
27208  do
27209    switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
27210      {
27211      case CPP_PRAGMA_EOL:
27212	if (!parser->lexer->in_pragma)
27213	  break;
27214	/* FALLTHRU */
27215      case CPP_EOF:
27216	/* Ran out of tokens.  */
27217	return orig_n;
27218      case CPP_OPEN_PAREN:
27219	++nparens;
27220	break;
27221      case CPP_OPEN_BRACE:
27222	++nbraces;
27223	break;
27224      case CPP_OPEN_SQUARE:
27225	++nsquares;
27226	break;
27227      case CPP_CLOSE_PAREN:
27228	--nparens;
27229	break;
27230      case CPP_CLOSE_BRACE:
27231	--nbraces;
27232	break;
27233      case CPP_CLOSE_SQUARE:
27234	--nsquares;
27235	break;
27236      default:
27237	break;
27238      }
27239  while (nparens || nbraces || nsquares);
27240  return n;
27241}
27242
27243/* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
27244   return index of the first token after the GNU attribute tokens, or N on
27245   failure.  */
27246
27247static size_t
27248cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
27249{
27250  while (true)
27251    {
27252      if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
27253	  || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
27254	  || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
27255	break;
27256
27257      size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
27258      if (n2 == n + 2)
27259	break;
27260      if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
27261	break;
27262      n = n2 + 1;
27263    }
27264  return n;
27265}
27266
27267/* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
27268   next token), return index of the first token after the standard C++11
27269   attribute tokens, or N on failure.  */
27270
27271static size_t
27272cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
27273{
27274  while (true)
27275    {
27276      if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
27277	  && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
27278	{
27279	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
27280	  if (n2 == n + 1)
27281	    break;
27282	  if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
27283	    break;
27284	  n = n2 + 1;
27285	}
27286      else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
27287	       && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
27288	{
27289	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
27290	  if (n2 == n + 1)
27291	    break;
27292	  n = n2;
27293	}
27294      else
27295	break;
27296    }
27297  return n;
27298}
27299
27300/* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
27301   as the next token), return index of the first token after the attribute
27302   tokens, or N on failure.  */
27303
27304static size_t
27305cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
27306{
27307  if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
27308    return cp_parser_skip_gnu_attributes_opt (parser, n);
27309  return cp_parser_skip_std_attribute_spec_seq (parser, n);
27310}
27311
27312/* Parse an optional `__extension__' keyword.  Returns TRUE if it is
27313   present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
27314   current value of the PEDANTIC flag, regardless of whether or not
27315   the `__extension__' keyword is present.  The caller is responsible
27316   for restoring the value of the PEDANTIC flag.  */
27317
27318static bool
27319cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
27320{
27321  /* Save the old value of the PEDANTIC flag.  */
27322  *saved_pedantic = pedantic;
27323
27324  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
27325    {
27326      /* Consume the `__extension__' token.  */
27327      cp_lexer_consume_token (parser->lexer);
27328      /* We're not being pedantic while the `__extension__' keyword is
27329	 in effect.  */
27330      pedantic = 0;
27331
27332      return true;
27333    }
27334
27335  return false;
27336}
27337
27338/* Parse a label declaration.
27339
27340   label-declaration:
27341     __label__ label-declarator-seq ;
27342
27343   label-declarator-seq:
27344     identifier , label-declarator-seq
27345     identifier  */
27346
27347static void
27348cp_parser_label_declaration (cp_parser* parser)
27349{
27350  /* Look for the `__label__' keyword.  */
27351  cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
27352
27353  while (true)
27354    {
27355      tree identifier;
27356
27357      /* Look for an identifier.  */
27358      identifier = cp_parser_identifier (parser);
27359      /* If we failed, stop.  */
27360      if (identifier == error_mark_node)
27361	break;
27362      /* Declare it as a label.  */
27363      finish_label_decl (identifier);
27364      /* If the next token is a `;', stop.  */
27365      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27366	break;
27367      /* Look for the `,' separating the label declarations.  */
27368      cp_parser_require (parser, CPP_COMMA, RT_COMMA);
27369    }
27370
27371  /* Look for the final `;'.  */
27372  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27373}
27374
27375// -------------------------------------------------------------------------- //
27376// Concept definitions
27377
27378static tree
27379cp_parser_concept_definition (cp_parser *parser)
27380{
27381  gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
27382  cp_lexer_consume_token (parser->lexer);
27383
27384  cp_expr id = cp_parser_identifier (parser);
27385  if (id == error_mark_node)
27386    {
27387      cp_parser_skip_to_end_of_statement (parser);
27388      cp_parser_consume_semicolon_at_end_of_statement (parser);
27389      return NULL_TREE;
27390    }
27391
27392  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27393    {
27394      cp_parser_skip_to_end_of_statement (parser);
27395      cp_parser_consume_semicolon_at_end_of_statement (parser);
27396      return error_mark_node;
27397    }
27398
27399  processing_constraint_expression_sentinel parsing_constraint;
27400  tree init = cp_parser_constraint_expression (parser);
27401  if (init == error_mark_node)
27402    cp_parser_skip_to_end_of_statement (parser);
27403
27404  /* Consume the trailing ';'. Diagnose the problem if it isn't there,
27405     but continue as if it were.  */
27406  cp_parser_consume_semicolon_at_end_of_statement (parser);
27407
27408  return finish_concept_definition (id, init);
27409}
27410
27411// -------------------------------------------------------------------------- //
27412// Requires Clause
27413
27414/* Diagnose an expression that should appear in ()'s within a requires-clause
27415   and suggest where to place those parentheses.  */
27416
27417static void
27418cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
27419{
27420  error_at (loc, "expression must be enclosed in parentheses");
27421}
27422
27423static void
27424cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
27425{
27426  gcc_rich_location richloc (loc);
27427  richloc.add_fixit_insert_before ("(");
27428  richloc.add_fixit_insert_after (")");
27429  error_at (&richloc, "expression must be enclosed in parentheses");
27430}
27431
27432/* Characterizes the likely kind of expression intended by a mis-written
27433   primary constraint.  */
27434enum primary_constraint_error
27435{
27436  pce_ok,
27437  pce_maybe_operator,
27438  pce_maybe_postfix
27439};
27440
27441/* Returns true if the token(s) following a primary-expression in a
27442   constraint-logical-* expression would require parentheses.  */
27443
27444static primary_constraint_error
27445cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
27446{
27447  cp_token *token = cp_lexer_peek_token (parser->lexer);
27448  switch (token->type)
27449    {
27450      default:
27451	return pce_ok;
27452
27453      case CPP_EQ:
27454	{
27455	  /* An equal sign may be part of the definition of a function,
27456	     and not an assignment operator, when parsing the expression
27457	     for a trailing requires-clause. For example:
27458
27459		template<typename T>
27460		struct S {
27461		  S() requires C<T> = default;
27462		};
27463
27464	     Don't try to reparse this a binary operator.  */
27465	  if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
27466	      || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
27467	    return pce_ok;
27468
27469	  gcc_fallthrough ();
27470	}
27471
27472      /* Arithmetic operators.  */
27473      case CPP_PLUS:
27474      case CPP_MINUS:
27475      case CPP_MULT:
27476      case CPP_DIV:
27477      case CPP_MOD:
27478      /* Bitwise operators.  */
27479      case CPP_AND:
27480      case CPP_OR:
27481      case CPP_XOR:
27482      case CPP_RSHIFT:
27483      case CPP_LSHIFT:
27484      /* Relational operators.  */
27485      case CPP_EQ_EQ:
27486      case CPP_NOT_EQ:
27487      case CPP_LESS:
27488      case CPP_GREATER:
27489      case CPP_LESS_EQ:
27490      case CPP_GREATER_EQ:
27491      case CPP_SPACESHIP:
27492      /* Pointer-to-member.  */
27493      case CPP_DOT_STAR:
27494      case CPP_DEREF_STAR:
27495      /* Assignment operators.  */
27496      case CPP_PLUS_EQ:
27497      case CPP_MINUS_EQ:
27498      case CPP_MULT_EQ:
27499      case CPP_DIV_EQ:
27500      case CPP_MOD_EQ:
27501      case CPP_AND_EQ:
27502      case CPP_OR_EQ:
27503      case CPP_XOR_EQ:
27504      case CPP_RSHIFT_EQ:
27505      case CPP_LSHIFT_EQ:
27506      /* Conditional operator */
27507      case CPP_QUERY:
27508	/* Unenclosed binary or conditional operator.  */
27509	return pce_maybe_operator;
27510
27511      case CPP_OPEN_PAREN:
27512	{
27513	  /* A primary constraint that precedes the parameter-list of a
27514	     lambda expression is followed by an open paren.
27515
27516		[]<typename T> requires C (T a, T b) { ... }
27517
27518	     Don't try to re-parse this as a postfix expression.  */
27519	  if (lambda_p)
27520	    return pce_ok;
27521
27522	  gcc_fallthrough ();
27523	}
27524      case CPP_OPEN_SQUARE:
27525	{
27526	  /* A primary-constraint-expression followed by a '[[' is not a
27527	     postfix expression.  */
27528	  if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
27529	    return pce_ok;
27530
27531	  gcc_fallthrough ();
27532	}
27533      case CPP_PLUS_PLUS:
27534      case CPP_MINUS_MINUS:
27535      case CPP_DOT:
27536      case CPP_DEREF:
27537	/* Unenclosed postfix operator.  */
27538	return pce_maybe_postfix;
27539   }
27540}
27541
27542/* Returns true if the next token begins a unary expression, preceded by
27543   an operator or keyword.  */
27544
27545static bool
27546cp_parser_unary_constraint_requires_parens (cp_parser *parser)
27547{
27548  cp_token *token = cp_lexer_peek_token (parser->lexer);
27549  switch (token->type)
27550    {
27551      case CPP_NOT:
27552      case CPP_PLUS:
27553      case CPP_MINUS:
27554      case CPP_MULT:
27555      case CPP_COMPL:
27556      case CPP_PLUS_PLUS:
27557      case CPP_MINUS_MINUS:
27558	return true;
27559
27560      case CPP_KEYWORD:
27561	{
27562	  switch (token->keyword)
27563	    {
27564	      case RID_STATCAST:
27565	      case RID_DYNCAST:
27566	      case RID_REINTCAST:
27567	      case RID_CONSTCAST:
27568	      case RID_TYPEID:
27569	      case RID_SIZEOF:
27570	      case RID_ALIGNOF:
27571	      case RID_NOEXCEPT:
27572	      case RID_NEW:
27573	      case RID_DELETE:
27574	      case RID_THROW:
27575		return true;
27576
27577	     default:
27578		break;
27579	  }
27580	}
27581
27582      default:
27583	break;
27584    }
27585
27586  return false;
27587}
27588
27589/* Parse a primary expression within a constraint.  */
27590
27591static cp_expr
27592cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
27593{
27594  /* If this looks like a unary expression, parse it as such, but diagnose
27595     it as ill-formed; it requires parens.  */
27596  if (cp_parser_unary_constraint_requires_parens (parser))
27597    {
27598      cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
27599      cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
27600      return e;
27601    }
27602
27603  cp_lexer_save_tokens (parser->lexer);
27604  cp_id_kind idk;
27605  location_t loc = input_location;
27606  cp_expr expr = cp_parser_primary_expression (parser,
27607					       /*address_p=*/false,
27608					       /*cast_p=*/false,
27609					       /*template_arg_p=*/false,
27610					       &idk);
27611  expr.maybe_add_location_wrapper ();
27612
27613  primary_constraint_error pce = pce_ok;
27614  if (expr != error_mark_node)
27615    {
27616      /* The primary-expression could be part of an unenclosed non-logical
27617	 compound expression.  */
27618      pce = cp_parser_constraint_requires_parens (parser, lambda_p);
27619    }
27620  if (pce == pce_ok)
27621    {
27622      cp_lexer_commit_tokens (parser->lexer);
27623      return finish_constraint_primary_expr (expr);
27624    }
27625
27626  /* Retry the parse at a lower precedence. If that succeeds, diagnose the
27627     error, but return the expression as if it were valid.  */
27628  cp_lexer_rollback_tokens (parser->lexer);
27629  cp_parser_parse_tentatively (parser);
27630  if (pce == pce_maybe_operator)
27631    expr = cp_parser_assignment_expression (parser, NULL, false, false);
27632  else
27633    expr = cp_parser_simple_cast_expression (parser);
27634  if (cp_parser_parse_definitely (parser))
27635    {
27636      cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
27637      return expr;
27638    }
27639
27640  /* Otherwise, something has gone very wrong, and we can't generate a more
27641     meaningful diagnostic or recover.  */
27642  cp_parser_diagnose_ungrouped_constraint_plain (loc);
27643  return error_mark_node;
27644}
27645
27646/* Parse a constraint-logical-and-expression.
27647
27648     constraint-logical-and-expression:
27649       primary-expression
27650       constraint-logical-and-expression '&&' primary-expression  */
27651
27652static cp_expr
27653cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
27654{
27655  cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27656  while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
27657    {
27658      cp_token *op = cp_lexer_consume_token (parser->lexer);
27659      tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27660      lhs = finish_constraint_and_expr (op->location, lhs, rhs);
27661    }
27662  return lhs;
27663}
27664
27665/* Parse a constraint-logical-or-expression.
27666
27667     constraint-logical-or-expression:
27668       constraint-logical-and-expression
27669       constraint-logical-or-expression '||' constraint-logical-and-expression  */
27670
27671static cp_expr
27672cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
27673{
27674  cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
27675  while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
27676    {
27677      cp_token *op = cp_lexer_consume_token (parser->lexer);
27678      cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
27679      lhs = finish_constraint_or_expr (op->location, lhs, rhs);
27680    }
27681  return lhs;
27682}
27683
27684/* Parse the expression after a requires-clause. This has a different grammar
27685    than that in the concepts TS.  */
27686
27687static tree
27688cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
27689{
27690  processing_constraint_expression_sentinel parsing_constraint;
27691  temp_override<int> ovr (processing_template_decl);
27692  if (!processing_template_decl)
27693    /* Adjust processing_template_decl so that we always obtain template
27694       trees here.  We don't do the usual ++processing_template_decl
27695       because that would skew the template parameter depth of a lambda
27696       within if we're already inside a template.  */
27697    processing_template_decl = 1;
27698  cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
27699  if (check_for_bare_parameter_packs (expr))
27700    expr = error_mark_node;
27701  return expr;
27702}
27703
27704/* Parse a expression after a requires clause.
27705
27706    constraint-expression:
27707      logical-or-expression
27708
27709   The required logical-or-expression must be a constant expression. Note
27710   that we don't check that the expression is constepxr here. We defer until
27711   we analyze constraints and then, we only check atomic constraints.  */
27712
27713static tree
27714cp_parser_constraint_expression (cp_parser *parser)
27715{
27716  processing_constraint_expression_sentinel parsing_constraint;
27717  temp_override<int> ovr (processing_template_decl);
27718  if (!processing_template_decl)
27719    /* As in cp_parser_requires_clause_expression.  */
27720    processing_template_decl = 1;
27721  cp_expr expr = cp_parser_binary_expression (parser, false, true,
27722					      PREC_NOT_OPERATOR, NULL);
27723  if (check_for_bare_parameter_packs (expr))
27724    expr = error_mark_node;
27725  expr.maybe_add_location_wrapper ();
27726  return expr;
27727}
27728
27729/* Optionally parse a requires clause:
27730
27731      requires-clause:
27732	`requires` constraint-logical-or-expression.
27733   [ConceptsTS]
27734	`requires constraint-expression.
27735
27736   LAMBDA_P is true when the requires-clause is parsed before the
27737   parameter-list of a lambda-declarator.  */
27738
27739static tree
27740cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
27741{
27742  cp_token *tok = cp_lexer_peek_token (parser->lexer);
27743  if (tok->keyword != RID_REQUIRES)
27744    {
27745      if (!flag_concepts && tok->type == CPP_NAME
27746	  && tok->u.value == ridpointers[RID_REQUIRES])
27747	{
27748	  error_at (cp_lexer_peek_token (parser->lexer)->location,
27749		    "%<requires%> only available with "
27750                    "%<-std=c++2a%> or %<-fconcepts%>");
27751	  /* Parse and discard the requires-clause.  */
27752	  cp_lexer_consume_token (parser->lexer);
27753	  cp_parser_constraint_expression (parser);
27754	}
27755      return NULL_TREE;
27756    }
27757
27758  cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
27759  if (tok2->type == CPP_OPEN_BRACE)
27760    {
27761      /* An opening brace following the start of a requires-clause is
27762	 ill-formed; the user likely forgot the second `requires' that
27763	 would start a requires-expression.  */
27764      gcc_rich_location richloc (tok2->location);
27765      richloc.add_fixit_insert_after (tok->location, " requires");
27766      error_at (&richloc, "missing additional %<requires%> to start "
27767		"a requires-expression");
27768      /* Don't consume the `requires', so that it's reused as the start of a
27769	 requires-expression.  */
27770    }
27771  else
27772    cp_lexer_consume_token (parser->lexer);
27773
27774  if (!flag_concepts_ts)
27775    return cp_parser_requires_clause_expression (parser, lambda_p);
27776  else
27777    return cp_parser_constraint_expression (parser);
27778}
27779
27780/*---------------------------------------------------------------------------
27781                           Requires expressions
27782---------------------------------------------------------------------------*/
27783
27784/* Parse a requires expression
27785
27786   requirement-expression:
27787       'requires' requirement-parameter-list [opt] requirement-body */
27788
27789static tree
27790cp_parser_requires_expression (cp_parser *parser)
27791{
27792  gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
27793  location_t loc = cp_lexer_consume_token (parser->lexer)->location;
27794
27795  /* Avoid committing to outer tentative parse.  */
27796  tentative_firewall firewall (parser);
27797
27798  /* This is definitely a requires-expression.  */
27799  cp_parser_commit_to_tentative_parse (parser);
27800
27801  tree parms, reqs;
27802  {
27803    /* Local parameters are delared as variables within the scope
27804       of the expression.  They are not visible past the end of
27805       the expression.  Expressions within the requires-expression
27806       are unevaluated.  */
27807    struct scope_sentinel
27808    {
27809      scope_sentinel ()
27810      {
27811	++cp_unevaluated_operand;
27812	begin_scope (sk_block, NULL_TREE);
27813      }
27814
27815      ~scope_sentinel ()
27816      {
27817	pop_bindings_and_leave_scope ();
27818	--cp_unevaluated_operand;
27819      }
27820    } s;
27821
27822    /* Parse the optional parameter list. */
27823    if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27824      {
27825	parms = cp_parser_requirement_parameter_list (parser);
27826	if (parms == error_mark_node)
27827	  return error_mark_node;
27828      }
27829    else
27830      parms = NULL_TREE;
27831
27832    /* Parse the requirement body. */
27833    temp_override<int> ovr (processing_template_decl);
27834    if (!processing_template_decl)
27835      /* As in cp_parser_requires_clause_expression.  */
27836      processing_template_decl = 1;
27837    reqs = cp_parser_requirement_body (parser);
27838    if (reqs == error_mark_node)
27839      return error_mark_node;
27840  }
27841
27842  /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
27843     the parm chain.  */
27844  grokparms (parms, &parms);
27845  loc = make_location (loc, loc, parser->lexer);
27846  tree expr = finish_requires_expr (loc, parms, reqs);
27847  if (!processing_template_decl)
27848    {
27849      /* Perform semantic processing now to diagnose any invalid types and
27850	 expressions.  */
27851      int saved_errorcount = errorcount;
27852      tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
27853      if (errorcount > saved_errorcount)
27854	return error_mark_node;
27855    }
27856  return expr;
27857}
27858
27859/* Parse a parameterized requirement.
27860
27861   requirement-parameter-list:
27862       '(' parameter-declaration-clause ')' */
27863
27864static tree
27865cp_parser_requirement_parameter_list (cp_parser *parser)
27866{
27867  matching_parens parens;
27868  if (!parens.require_open (parser))
27869    return error_mark_node;
27870
27871  tree parms = (cp_parser_parameter_declaration_clause
27872		(parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
27873
27874  if (!parens.require_close (parser))
27875    return error_mark_node;
27876
27877  return parms;
27878}
27879
27880/* Parse the body of a requirement.
27881
27882   requirement-body:
27883       '{' requirement-list '}' */
27884static tree
27885cp_parser_requirement_body (cp_parser *parser)
27886{
27887  matching_braces braces;
27888  if (!braces.require_open (parser))
27889    return error_mark_node;
27890
27891  tree reqs = cp_parser_requirement_seq (parser);
27892
27893  if (!braces.require_close (parser))
27894    return error_mark_node;
27895
27896  return reqs;
27897}
27898
27899/* Parse a sequence of requirements.
27900
27901   requirement-seq:
27902       requirement
27903       requirement-seq requirement */
27904
27905static tree
27906cp_parser_requirement_seq (cp_parser *parser)
27907{
27908  tree result = NULL_TREE;
27909  do
27910    {
27911      tree req = cp_parser_requirement (parser);
27912      if (req != error_mark_node)
27913	result = tree_cons (NULL_TREE, req, result);
27914    }
27915  while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
27916	 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
27917
27918  /* If there are no valid requirements, this is not a valid expression. */
27919  if (!result)
27920    return error_mark_node;
27921
27922  /* Reverse the order of requirements so they are analyzed in order. */
27923  return nreverse (result);
27924}
27925
27926/* Parse a syntactic requirement or type requirement.
27927
27928     requirement:
27929       simple-requirement
27930       compound-requirement
27931       type-requirement
27932       nested-requirement */
27933
27934static tree
27935cp_parser_requirement (cp_parser *parser)
27936{
27937  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27938    return cp_parser_compound_requirement (parser);
27939  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27940    return cp_parser_type_requirement (parser);
27941  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
27942    return cp_parser_nested_requirement (parser);
27943  else
27944    return cp_parser_simple_requirement (parser);
27945}
27946
27947/* Parse a simple requirement.
27948
27949     simple-requirement:
27950       expression ';' */
27951
27952static tree
27953cp_parser_simple_requirement (cp_parser *parser)
27954{
27955  location_t start = cp_lexer_peek_token (parser->lexer)->location;
27956  cp_expr expr = cp_parser_expression (parser, NULL, false, false);
27957  if (expr == error_mark_node)
27958    cp_parser_skip_to_end_of_statement (parser);
27959
27960  cp_parser_consume_semicolon_at_end_of_statement (parser);
27961
27962  if (!expr || expr == error_mark_node)
27963    return error_mark_node;
27964
27965  /* Sometimes we don't get locations, so use the cached token location
27966     as a reasonable approximation.  */
27967  if (expr.get_location() == UNKNOWN_LOCATION)
27968    expr.set_location (start);
27969
27970  return finish_simple_requirement (expr.get_location (), expr);
27971}
27972
27973/* Parse a type requirement
27974
27975     type-requirement
27976         nested-name-specifier [opt] required-type-name ';'
27977
27978     required-type-name:
27979         type-name
27980         'template' [opt] simple-template-id  */
27981
27982static tree
27983cp_parser_type_requirement (cp_parser *parser)
27984{
27985  cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
27986  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27987
27988  // Save the scope before parsing name specifiers.
27989  tree saved_scope = parser->scope;
27990  tree saved_object_scope = parser->object_scope;
27991  tree saved_qualifying_scope = parser->qualifying_scope;
27992  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
27993  cp_parser_nested_name_specifier_opt (parser,
27994                                       /*typename_keyword_p=*/true,
27995                                       /*check_dependency_p=*/false,
27996                                       /*type_p=*/true,
27997                                       /*is_declaration=*/false);
27998
27999  tree type;
28000  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28001    {
28002      cp_lexer_consume_token (parser->lexer);
28003      type = cp_parser_template_id (parser,
28004                                    /*template_keyword_p=*/true,
28005                                    /*check_dependency=*/false,
28006                                    /*tag_type=*/none_type,
28007                                    /*is_declaration=*/false);
28008      type = make_typename_type (parser->scope, type, typename_type,
28009                                 /*complain=*/tf_error);
28010    }
28011  else
28012   type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
28013
28014  if (TREE_CODE (type) == TYPE_DECL)
28015    type = TREE_TYPE (type);
28016
28017  parser->scope = saved_scope;
28018  parser->object_scope = saved_object_scope;
28019  parser->qualifying_scope = saved_qualifying_scope;
28020
28021  if (type == error_mark_node)
28022    cp_parser_skip_to_end_of_statement (parser);
28023
28024  cp_parser_consume_semicolon_at_end_of_statement (parser);
28025
28026  if (type == error_mark_node)
28027    return error_mark_node;
28028
28029  loc = make_location (loc, start_tok->location, parser->lexer);
28030  return finish_type_requirement (loc, type);
28031}
28032
28033/* Parse a compound requirement
28034
28035     compound-requirement:
28036         '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
28037
28038static tree
28039cp_parser_compound_requirement (cp_parser *parser)
28040{
28041  /* Parse an expression enclosed in '{ }'s. */
28042  matching_braces braces;
28043  if (!braces.require_open (parser))
28044    return error_mark_node;
28045
28046  cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
28047
28048  tree expr = cp_parser_expression (parser, NULL, false, false);
28049  if (expr == error_mark_node)
28050    cp_parser_skip_to_closing_brace (parser);
28051
28052  if (!braces.require_close (parser))
28053    {
28054      cp_parser_skip_to_end_of_statement (parser);
28055      cp_parser_consume_semicolon_at_end_of_statement (parser);
28056      return error_mark_node;
28057    }
28058
28059  /* If the expression was invalid, skip the remainder of the requirement.  */
28060  if (!expr || expr == error_mark_node)
28061    {
28062      cp_parser_skip_to_end_of_statement (parser);
28063      cp_parser_consume_semicolon_at_end_of_statement (parser);
28064      return error_mark_node;
28065    }
28066
28067  /* Parse the optional noexcept. */
28068  bool noexcept_p = false;
28069  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
28070    {
28071      cp_lexer_consume_token (parser->lexer);
28072      noexcept_p = true;
28073    }
28074
28075  /* Parse the optional trailing return type. */
28076  tree type = NULL_TREE;
28077  if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
28078    {
28079      cp_lexer_consume_token (parser->lexer);
28080      cp_token *tok = cp_lexer_peek_token (parser->lexer);
28081
28082      bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
28083      parser->in_result_type_constraint_p = true;
28084      /* C++2a allows either a type-id or a type-constraint. Parsing
28085         a type-id will subsume the parsing for a type-constraint but
28086         allow for more syntactic forms (e.g., const C<T>*).  */
28087      type = cp_parser_trailing_type_id (parser);
28088      parser->in_result_type_constraint_p = saved_result_type_constraint_p;
28089      if (type == error_mark_node)
28090        return error_mark_node;
28091
28092      location_t type_loc = make_location (tok->location, tok->location,
28093					   parser->lexer);
28094
28095      /* Check that we haven't written something like 'const C<T>*'.  */
28096      if (type_uses_auto (type))
28097	{
28098	  if (!is_auto (type))
28099	    {
28100	      error_at (type_loc,
28101			"result type is not a plain type-constraint");
28102	      cp_parser_consume_semicolon_at_end_of_statement (parser);
28103	      return error_mark_node;
28104	    }
28105	}
28106      else if (!flag_concepts_ts)
28107	/* P1452R2 removed the trailing-return-type option.  */
28108	error_at (type_loc,
28109		  "return-type-requirement is not a type-constraint");
28110    }
28111
28112  location_t loc = make_location (expr_token->location,
28113				  braces.open_location (),
28114				  parser->lexer);
28115
28116  cp_parser_consume_semicolon_at_end_of_statement (parser);
28117
28118  if (expr == error_mark_node || type == error_mark_node)
28119    return error_mark_node;
28120
28121  return finish_compound_requirement (loc, expr, type, noexcept_p);
28122}
28123
28124/* Parse a nested requirement. This is the same as a requires clause.
28125
28126   nested-requirement:
28127     requires-clause */
28128
28129static tree
28130cp_parser_nested_requirement (cp_parser *parser)
28131{
28132  gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
28133  cp_token *tok = cp_lexer_consume_token (parser->lexer);
28134  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28135  tree req = cp_parser_constraint_expression (parser);
28136  if (req == error_mark_node)
28137    cp_parser_skip_to_end_of_statement (parser);
28138  loc = make_location (loc, tok->location, parser->lexer);
28139  cp_parser_consume_semicolon_at_end_of_statement (parser);
28140  if (req == error_mark_node)
28141    return error_mark_node;
28142  return finish_nested_requirement (loc, req);
28143}
28144
28145/* Support Functions */
28146
28147/* Return the appropriate prefer_type argument for lookup_name_real based on
28148   tag_type and template_mem_access.  */
28149
28150static inline int
28151prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
28152{
28153  /* DR 141: When looking in the current enclosing context for a template-name
28154     after -> or ., only consider class templates.  */
28155  if (template_mem_access)
28156    return 2;
28157  switch (tag_type)
28158    {
28159    case none_type:  return 0;	// No preference.
28160    case scope_type: return 1;	// Type or namespace.
28161    default:         return 2;	// Type only.
28162    }
28163}
28164
28165/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
28166   NAME should have one of the representations used for an
28167   id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
28168   is returned.  If PARSER->SCOPE is a dependent type, then a
28169   SCOPE_REF is returned.
28170
28171   If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
28172   returned; the name was already resolved when the TEMPLATE_ID_EXPR
28173   was formed.  Abstractly, such entities should not be passed to this
28174   function, because they do not need to be looked up, but it is
28175   simpler to check for this special case here, rather than at the
28176   call-sites.
28177
28178   In cases not explicitly covered above, this function returns a
28179   DECL, OVERLOAD, or baselink representing the result of the lookup.
28180   If there was no entity with the indicated NAME, the ERROR_MARK_NODE
28181   is returned.
28182
28183   If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
28184   (e.g., "struct") that was used.  In that case bindings that do not
28185   refer to types are ignored.
28186
28187   If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
28188   ignored.
28189
28190   If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
28191   are ignored.
28192
28193   If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
28194   types.
28195
28196   If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
28197   TREE_LIST of candidates if name-lookup results in an ambiguity, and
28198   NULL_TREE otherwise.  */
28199
28200static cp_expr
28201cp_parser_lookup_name (cp_parser *parser, tree name,
28202		       enum tag_types tag_type,
28203		       bool is_template,
28204		       bool is_namespace,
28205		       bool check_dependency,
28206		       tree *ambiguous_decls,
28207		       location_t name_location)
28208{
28209  tree decl;
28210  tree object_type = parser->context->object_type;
28211
28212  /* Assume that the lookup will be unambiguous.  */
28213  if (ambiguous_decls)
28214    *ambiguous_decls = NULL_TREE;
28215
28216  /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
28217     no longer valid.  Note that if we are parsing tentatively, and
28218     the parse fails, OBJECT_TYPE will be automatically restored.  */
28219  parser->context->object_type = NULL_TREE;
28220
28221  if (name == error_mark_node)
28222    return error_mark_node;
28223
28224  /* A template-id has already been resolved; there is no lookup to
28225     do.  */
28226  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
28227    return name;
28228  if (BASELINK_P (name))
28229    {
28230      gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
28231		  == TEMPLATE_ID_EXPR);
28232      return name;
28233    }
28234
28235  /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
28236     it should already have been checked to make sure that the name
28237     used matches the type being destroyed.  */
28238  if (TREE_CODE (name) == BIT_NOT_EXPR)
28239    {
28240      tree type;
28241
28242      /* Figure out to which type this destructor applies.  */
28243      if (parser->scope)
28244	type = parser->scope;
28245      else if (object_type)
28246	type = object_type;
28247      else
28248	type = current_class_type;
28249      /* If that's not a class type, there is no destructor.  */
28250      if (!type || !CLASS_TYPE_P (type))
28251	return error_mark_node;
28252
28253      /* In a non-static member function, check implicit this->.  */
28254      if (current_class_ref)
28255	return lookup_destructor (current_class_ref, parser->scope, name,
28256				  tf_warning_or_error);
28257
28258      if (CLASSTYPE_LAZY_DESTRUCTOR (type))
28259	lazily_declare_fn (sfk_destructor, type);
28260
28261      if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
28262	return dtor;
28263
28264      return error_mark_node;
28265    }
28266
28267  /* By this point, the NAME should be an ordinary identifier.  If
28268     the id-expression was a qualified name, the qualifying scope is
28269     stored in PARSER->SCOPE at this point.  */
28270  gcc_assert (identifier_p (name));
28271
28272  /* Perform the lookup.  */
28273  if (parser->scope)
28274    {
28275      bool dependent_p;
28276
28277      if (parser->scope == error_mark_node)
28278	return error_mark_node;
28279
28280      /* If the SCOPE is dependent, the lookup must be deferred until
28281	 the template is instantiated -- unless we are explicitly
28282	 looking up names in uninstantiated templates.  Even then, we
28283	 cannot look up the name if the scope is not a class type; it
28284	 might, for example, be a template type parameter.  */
28285      dependent_p = (TYPE_P (parser->scope)
28286		     && dependent_scope_p (parser->scope));
28287      if ((check_dependency || !CLASS_TYPE_P (parser->scope))
28288	  && dependent_p)
28289	/* Defer lookup.  */
28290	decl = error_mark_node;
28291      else
28292	{
28293	  tree pushed_scope = NULL_TREE;
28294
28295	  /* If PARSER->SCOPE is a dependent type, then it must be a
28296	     class type, and we must not be checking dependencies;
28297	     otherwise, we would have processed this lookup above.  So
28298	     that PARSER->SCOPE is not considered a dependent base by
28299	     lookup_member, we must enter the scope here.  */
28300	  if (dependent_p)
28301	    pushed_scope = push_scope (parser->scope);
28302
28303	  /* If the PARSER->SCOPE is a template specialization, it
28304	     may be instantiated during name lookup.  In that case,
28305	     errors may be issued.  Even if we rollback the current
28306	     tentative parse, those errors are valid.  */
28307	  decl = lookup_qualified_name (parser->scope, name,
28308					prefer_type_arg (tag_type),
28309					/*complain=*/true);
28310
28311	  /* 3.4.3.1: In a lookup in which the constructor is an acceptable
28312	     lookup result and the nested-name-specifier nominates a class C:
28313	       * if the name specified after the nested-name-specifier, when
28314	       looked up in C, is the injected-class-name of C (Clause 9), or
28315	       * if the name specified after the nested-name-specifier is the
28316	       same as the identifier or the simple-template-id's template-
28317	       name in the last component of the nested-name-specifier,
28318	     the name is instead considered to name the constructor of
28319	     class C. [ Note: for example, the constructor is not an
28320	     acceptable lookup result in an elaborated-type-specifier so
28321	     the constructor would not be used in place of the
28322	     injected-class-name. --end note ] Such a constructor name
28323	     shall be used only in the declarator-id of a declaration that
28324	     names a constructor or in a using-declaration.  */
28325	  if (tag_type == none_type
28326	      && DECL_SELF_REFERENCE_P (decl)
28327	      && same_type_p (DECL_CONTEXT (decl), parser->scope))
28328	    decl = lookup_qualified_name (parser->scope, ctor_identifier,
28329					  prefer_type_arg (tag_type),
28330					  /*complain=*/true);
28331
28332	  /* If we have a single function from a using decl, pull it out.  */
28333	  if (TREE_CODE (decl) == OVERLOAD
28334	      && !really_overloaded_fn (decl))
28335	    decl = OVL_FUNCTION (decl);
28336
28337	  if (pushed_scope)
28338	    pop_scope (pushed_scope);
28339	}
28340
28341      /* If the scope is a dependent type and either we deferred lookup or
28342	 we did lookup but didn't find the name, rememeber the name.  */
28343      if (decl == error_mark_node && TYPE_P (parser->scope)
28344	  && dependent_type_p (parser->scope))
28345	{
28346	  if (tag_type)
28347	    {
28348	      tree type;
28349
28350	      /* The resolution to Core Issue 180 says that `struct
28351		 A::B' should be considered a type-name, even if `A'
28352		 is dependent.  */
28353	      type = make_typename_type (parser->scope, name, tag_type,
28354					 /*complain=*/tf_error);
28355	      if (type != error_mark_node)
28356		decl = TYPE_NAME (type);
28357	    }
28358	  else if (is_template
28359		   && (cp_parser_next_token_ends_template_argument_p (parser)
28360		       || cp_lexer_next_token_is (parser->lexer,
28361						  CPP_CLOSE_PAREN)))
28362	    decl = make_unbound_class_template (parser->scope,
28363						name, NULL_TREE,
28364						/*complain=*/tf_error);
28365	  else
28366	    decl = build_qualified_name (/*type=*/NULL_TREE,
28367					 parser->scope, name,
28368					 is_template);
28369	}
28370      parser->qualifying_scope = parser->scope;
28371      parser->object_scope = NULL_TREE;
28372    }
28373  else if (object_type)
28374    {
28375      /* Look up the name in the scope of the OBJECT_TYPE, unless the
28376	 OBJECT_TYPE is not a class.  */
28377      if (CLASS_TYPE_P (object_type))
28378	/* If the OBJECT_TYPE is a template specialization, it may
28379	   be instantiated during name lookup.  In that case, errors
28380	   may be issued.  Even if we rollback the current tentative
28381	   parse, those errors are valid.  */
28382	decl = lookup_member (object_type,
28383			      name,
28384			      /*protect=*/0,
28385			      prefer_type_arg (tag_type),
28386			      tf_warning_or_error);
28387      else
28388	decl = NULL_TREE;
28389
28390      if (!decl)
28391	/* Look it up in the enclosing context.  DR 141: When looking for a
28392	   template-name after -> or ., only consider class templates.  */
28393	decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
28394				 /*nonclass=*/0,
28395				 /*block_p=*/true, is_namespace, 0);
28396      if (object_type == unknown_type_node)
28397	/* The object is type-dependent, so we can't look anything up; we used
28398	   this to get the DR 141 behavior.  */
28399	object_type = NULL_TREE;
28400      parser->object_scope = object_type;
28401      parser->qualifying_scope = NULL_TREE;
28402    }
28403  else
28404    {
28405      decl = lookup_name_real (name, prefer_type_arg (tag_type),
28406			       /*nonclass=*/0,
28407			       /*block_p=*/true, is_namespace, 0);
28408      parser->qualifying_scope = NULL_TREE;
28409      parser->object_scope = NULL_TREE;
28410    }
28411
28412  /* If the lookup failed, let our caller know.  */
28413  if (!decl || decl == error_mark_node)
28414    return error_mark_node;
28415
28416  /* Pull out the template from an injected-class-name (or multiple).  */
28417  if (is_template)
28418    decl = maybe_get_template_decl_from_type_decl (decl);
28419
28420  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
28421  if (TREE_CODE (decl) == TREE_LIST)
28422    {
28423      if (ambiguous_decls)
28424	*ambiguous_decls = decl;
28425      /* The error message we have to print is too complicated for
28426	 cp_parser_error, so we incorporate its actions directly.  */
28427      if (!cp_parser_simulate_error (parser))
28428	{
28429	  error_at (name_location, "reference to %qD is ambiguous",
28430		    name);
28431	  print_candidates (decl);
28432	}
28433      return error_mark_node;
28434    }
28435
28436  gcc_assert (DECL_P (decl)
28437	      || TREE_CODE (decl) == OVERLOAD
28438	      || TREE_CODE (decl) == SCOPE_REF
28439	      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
28440	      || BASELINK_P (decl));
28441
28442  /* If we have resolved the name of a member declaration, check to
28443     see if the declaration is accessible.  When the name resolves to
28444     set of overloaded functions, accessibility is checked when
28445     overload resolution is done.
28446
28447     During an explicit instantiation, access is not checked at all,
28448     as per [temp.explicit].  */
28449  if (DECL_P (decl))
28450    check_accessibility_of_qualified_id (decl, object_type, parser->scope);
28451
28452  maybe_record_typedef_use (decl);
28453
28454  return cp_expr (decl, name_location);
28455}
28456
28457/* Like cp_parser_lookup_name, but for use in the typical case where
28458   CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
28459   IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
28460
28461static tree
28462cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
28463{
28464  return cp_parser_lookup_name (parser, name,
28465				none_type,
28466				/*is_template=*/false,
28467				/*is_namespace=*/false,
28468				/*check_dependency=*/true,
28469				/*ambiguous_decls=*/NULL,
28470				location);
28471}
28472
28473/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
28474   the current context, return the TYPE_DECL.  If TAG_NAME_P is
28475   true, the DECL indicates the class being defined in a class-head,
28476   or declared in an elaborated-type-specifier.
28477
28478   Otherwise, return DECL.  */
28479
28480static tree
28481cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
28482{
28483  /* If the TEMPLATE_DECL is being declared as part of a class-head,
28484     the translation from TEMPLATE_DECL to TYPE_DECL occurs:
28485
28486       struct A {
28487	 template <typename T> struct B;
28488       };
28489
28490       template <typename T> struct A::B {};
28491
28492     Similarly, in an elaborated-type-specifier:
28493
28494       namespace N { struct X{}; }
28495
28496       struct A {
28497	 template <typename T> friend struct N::X;
28498       };
28499
28500     However, if the DECL refers to a class type, and we are in
28501     the scope of the class, then the name lookup automatically
28502     finds the TYPE_DECL created by build_self_reference rather
28503     than a TEMPLATE_DECL.  For example, in:
28504
28505       template <class T> struct S {
28506	 S s;
28507       };
28508
28509     there is no need to handle such case.  */
28510
28511  if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
28512    return DECL_TEMPLATE_RESULT (decl);
28513
28514  return decl;
28515}
28516
28517/* If too many, or too few, template-parameter lists apply to the
28518   declarator, issue an error message.  Returns TRUE if all went well,
28519   and FALSE otherwise.  */
28520
28521static bool
28522cp_parser_check_declarator_template_parameters (cp_parser* parser,
28523						cp_declarator *declarator,
28524						location_t declarator_location)
28525{
28526  switch (declarator->kind)
28527    {
28528    case cdk_id:
28529      {
28530	unsigned num_templates = 0;
28531	tree scope = declarator->u.id.qualifying_scope;
28532	bool template_id_p = false;
28533
28534	if (scope)
28535	  num_templates = num_template_headers_for_class (scope);
28536	else if (TREE_CODE (declarator->u.id.unqualified_name)
28537		 == TEMPLATE_ID_EXPR)
28538	  {
28539	    /* If the DECLARATOR has the form `X<y>' then it uses one
28540	       additional level of template parameters.  */
28541	    ++num_templates;
28542	    template_id_p = true;
28543	  }
28544
28545	return cp_parser_check_template_parameters
28546	  (parser, num_templates, template_id_p, declarator_location,
28547	   declarator);
28548      }
28549
28550    case cdk_function:
28551    case cdk_array:
28552    case cdk_pointer:
28553    case cdk_reference:
28554    case cdk_ptrmem:
28555      return (cp_parser_check_declarator_template_parameters
28556	      (parser, declarator->declarator, declarator_location));
28557
28558    case cdk_decomp:
28559    case cdk_error:
28560      return true;
28561
28562    default:
28563      gcc_unreachable ();
28564    }
28565  return false;
28566}
28567
28568/* NUM_TEMPLATES were used in the current declaration.  If that is
28569   invalid, return FALSE and issue an error messages.  Otherwise,
28570   return TRUE.  If DECLARATOR is non-NULL, then we are checking a
28571   declarator and we can print more accurate diagnostics.  */
28572
28573static bool
28574cp_parser_check_template_parameters (cp_parser* parser,
28575				     unsigned num_templates,
28576				     bool template_id_p,
28577				     location_t location,
28578				     cp_declarator *declarator)
28579{
28580  /* If there are the same number of template classes and parameter
28581     lists, that's OK.  */
28582  if (parser->num_template_parameter_lists == num_templates)
28583    return true;
28584  /* If there are more, but only one more, and the name ends in an identifier,
28585     then we are declaring a primary template.  That's OK too.  */
28586  if (!template_id_p
28587      && parser->num_template_parameter_lists == num_templates + 1)
28588    return true;
28589
28590  if (cp_parser_simulate_error (parser))
28591    return false;
28592
28593  /* If there are more template classes than parameter lists, we have
28594     something like:
28595
28596       template <class T> void S<T>::R<T>::f ();  */
28597  if (parser->num_template_parameter_lists < num_templates)
28598    {
28599      if (declarator && !current_function_decl)
28600	error_at (location, "specializing member %<%T::%E%> "
28601		  "requires %<template<>%> syntax",
28602		  declarator->u.id.qualifying_scope,
28603		  declarator->u.id.unqualified_name);
28604      else if (declarator)
28605	error_at (location, "invalid declaration of %<%T::%E%>",
28606		  declarator->u.id.qualifying_scope,
28607		  declarator->u.id.unqualified_name);
28608      else
28609	error_at (location, "too few template-parameter-lists");
28610      return false;
28611    }
28612  /* Otherwise, there are too many template parameter lists.  We have
28613     something like:
28614
28615     template <class T> template <class U> void S::f();  */
28616  error_at (location, "too many template-parameter-lists");
28617  return false;
28618}
28619
28620/* Parse an optional `::' token indicating that the following name is
28621   from the global namespace.  If so, PARSER->SCOPE is set to the
28622   GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
28623   unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
28624   Returns the new value of PARSER->SCOPE, if the `::' token is
28625   present, and NULL_TREE otherwise.  */
28626
28627static tree
28628cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
28629{
28630  cp_token *token;
28631
28632  /* Peek at the next token.  */
28633  token = cp_lexer_peek_token (parser->lexer);
28634  /* If we're looking at a `::' token then we're starting from the
28635     global namespace, not our current location.  */
28636  if (token->type == CPP_SCOPE)
28637    {
28638      /* Consume the `::' token.  */
28639      cp_lexer_consume_token (parser->lexer);
28640      /* Set the SCOPE so that we know where to start the lookup.  */
28641      parser->scope = global_namespace;
28642      parser->qualifying_scope = global_namespace;
28643      parser->object_scope = NULL_TREE;
28644
28645      return parser->scope;
28646    }
28647  else if (!current_scope_valid_p)
28648    {
28649      parser->scope = NULL_TREE;
28650      parser->qualifying_scope = NULL_TREE;
28651      parser->object_scope = NULL_TREE;
28652    }
28653
28654  return NULL_TREE;
28655}
28656
28657/* Returns TRUE if the upcoming token sequence is the start of a
28658   constructor declarator or C++17 deduction guide.  If FRIEND_P is true, the
28659   declarator is preceded by the `friend' specifier.  The parser flags FLAGS
28660   is used to control type-specifier parsing.  */
28661
28662static bool
28663cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
28664				    bool friend_p)
28665{
28666  bool constructor_p;
28667  bool outside_class_specifier_p;
28668  tree nested_name_specifier;
28669  cp_token *next_token;
28670
28671  /* The common case is that this is not a constructor declarator, so
28672     try to avoid doing lots of work if at all possible.  It's not
28673     valid declare a constructor at function scope.  */
28674  if (parser->in_function_body)
28675    return false;
28676  /* And only certain tokens can begin a constructor declarator.  */
28677  next_token = cp_lexer_peek_token (parser->lexer);
28678  if (next_token->type != CPP_NAME
28679      && next_token->type != CPP_SCOPE
28680      && next_token->type != CPP_NESTED_NAME_SPECIFIER
28681      && next_token->type != CPP_TEMPLATE_ID)
28682    return false;
28683
28684  /* Parse tentatively; we are going to roll back all of the tokens
28685     consumed here.  */
28686  cp_parser_parse_tentatively (parser);
28687  /* Assume that we are looking at a constructor declarator.  */
28688  constructor_p = true;
28689
28690  /* Look for the optional `::' operator.  */
28691  cp_parser_global_scope_opt (parser,
28692			      /*current_scope_valid_p=*/false);
28693  /* Look for the nested-name-specifier.  */
28694  nested_name_specifier
28695    = (cp_parser_nested_name_specifier_opt (parser,
28696					    /*typename_keyword_p=*/false,
28697					    /*check_dependency_p=*/false,
28698					    /*type_p=*/false,
28699					    /*is_declaration=*/false));
28700
28701  /* Resolve the TYPENAME_TYPE, because the call above didn't do it.  */
28702  if (nested_name_specifier
28703      && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
28704    {
28705      tree s = resolve_typename_type (nested_name_specifier,
28706				      /*only_current_p=*/false);
28707      if (TREE_CODE (s) != TYPENAME_TYPE)
28708	nested_name_specifier = s;
28709    }
28710
28711  outside_class_specifier_p = (!at_class_scope_p ()
28712			       || !TYPE_BEING_DEFINED (current_class_type)
28713			       || friend_p);
28714
28715  /* Outside of a class-specifier, there must be a
28716     nested-name-specifier.  Except in C++17 mode, where we
28717     might be declaring a guiding declaration.  */
28718  if (!nested_name_specifier && outside_class_specifier_p
28719      && cxx_dialect < cxx17)
28720    constructor_p = false;
28721  else if (nested_name_specifier == error_mark_node)
28722    constructor_p = false;
28723
28724  /* If we have a class scope, this is easy; DR 147 says that S::S always
28725     names the constructor, and no other qualified name could.  */
28726  if (constructor_p && nested_name_specifier
28727      && CLASS_TYPE_P (nested_name_specifier))
28728    {
28729      tree id = cp_parser_unqualified_id (parser,
28730					  /*template_keyword_p=*/false,
28731					  /*check_dependency_p=*/false,
28732					  /*declarator_p=*/true,
28733					  /*optional_p=*/false);
28734      if (is_overloaded_fn (id))
28735	id = DECL_NAME (get_first_fn (id));
28736      if (!constructor_name_p (id, nested_name_specifier))
28737	constructor_p = false;
28738    }
28739  /* If we still think that this might be a constructor-declarator,
28740     look for a class-name.  */
28741  else if (constructor_p)
28742    {
28743      /* If we have:
28744
28745	   template <typename T> struct S {
28746	     S();
28747	   };
28748
28749	 we must recognize that the nested `S' names a class.  */
28750      if (cxx_dialect >= cxx17)
28751	cp_parser_parse_tentatively (parser);
28752
28753      tree type_decl;
28754      type_decl = cp_parser_class_name (parser,
28755					/*typename_keyword_p=*/false,
28756					/*template_keyword_p=*/false,
28757					none_type,
28758					/*check_dependency_p=*/false,
28759					/*class_head_p=*/false,
28760					/*is_declaration=*/false);
28761
28762      if (cxx_dialect >= cxx17
28763	  && !cp_parser_parse_definitely (parser))
28764	{
28765	  type_decl = NULL_TREE;
28766	  tree tmpl = cp_parser_template_name (parser,
28767					       /*template_keyword*/false,
28768					       /*check_dependency_p*/false,
28769					       /*is_declaration*/false,
28770					       none_type,
28771					       /*is_identifier*/NULL);
28772	  if (DECL_CLASS_TEMPLATE_P (tmpl)
28773	      || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28774	    /* It's a deduction guide, return true.  */;
28775	  else
28776	    cp_parser_simulate_error (parser);
28777	}
28778
28779      /* If there was no class-name, then this is not a constructor.
28780	 Otherwise, if we are in a class-specifier and we aren't
28781	 handling a friend declaration, check that its type matches
28782	 current_class_type (c++/38313).  Note: error_mark_node
28783	 is left alone for error recovery purposes.  */
28784      constructor_p = (!cp_parser_error_occurred (parser)
28785		       && (outside_class_specifier_p
28786			   || type_decl == NULL_TREE
28787			   || type_decl == error_mark_node
28788			   || same_type_p (current_class_type,
28789					   TREE_TYPE (type_decl))));
28790
28791      /* If we're still considering a constructor, we have to see a `(',
28792	 to begin the parameter-declaration-clause, followed by either a
28793	 `)', an `...', or a decl-specifier.  We need to check for a
28794	 type-specifier to avoid being fooled into thinking that:
28795
28796	   S (f) (int);
28797
28798	 is a constructor.  (It is actually a function named `f' that
28799	 takes one parameter (of type `int') and returns a value of type
28800	 `S'.  */
28801      if (constructor_p
28802	  && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28803	constructor_p = false;
28804
28805      if (constructor_p
28806	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
28807	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
28808	  /* A parameter declaration begins with a decl-specifier,
28809	     which is either the "attribute" keyword, a storage class
28810	     specifier, or (usually) a type-specifier.  */
28811	  && (!cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
28812	      /* GNU attributes can actually appear both at the start of
28813		 a parameter and parenthesized declarator.
28814		 S (__attribute__((unused)) int);
28815		 is a constructor, but
28816		 S (__attribute__((unused)) foo) (int);
28817		 is a function declaration.  */
28818	      || (cp_parser_allow_gnu_extensions_p (parser)
28819		  && cp_next_tokens_can_be_gnu_attribute_p (parser)))
28820	  /* A parameter declaration can also begin with [[attribute]].  */
28821	  && !cp_next_tokens_can_be_std_attribute_p (parser))
28822	{
28823	  tree type;
28824	  tree pushed_scope = NULL_TREE;
28825	  unsigned saved_num_template_parameter_lists;
28826
28827	  if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28828	    {
28829	      unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
28830	      while (--n)
28831		cp_lexer_consume_token (parser->lexer);
28832	    }
28833
28834	  /* Names appearing in the type-specifier should be looked up
28835	     in the scope of the class.  */
28836	  if (current_class_type)
28837	    type = NULL_TREE;
28838	  else if (type_decl)
28839	    {
28840	      type = TREE_TYPE (type_decl);
28841	      if (TREE_CODE (type) == TYPENAME_TYPE)
28842		{
28843		  type = resolve_typename_type (type,
28844						/*only_current_p=*/false);
28845		  if (TREE_CODE (type) == TYPENAME_TYPE)
28846		    {
28847		      cp_parser_abort_tentative_parse (parser);
28848		      return false;
28849		    }
28850		}
28851	      pushed_scope = push_scope (type);
28852	    }
28853
28854	  /* Inside the constructor parameter list, surrounding
28855	     template-parameter-lists do not apply.  */
28856	  saved_num_template_parameter_lists
28857	    = parser->num_template_parameter_lists;
28858	  parser->num_template_parameter_lists = 0;
28859
28860	  /* Look for the type-specifier.  It's not optional, but its typename
28861	     might be.  Unless this is a friend declaration; we don't want to
28862	     treat
28863
28864	       friend S (T::fn)(int);
28865
28866	     as a constructor, but with P0634, we might assume a type when
28867	     looking for the type-specifier.  It is actually a function named
28868	     `T::fn' that takes one parameter (of type `int') and returns a
28869	     value of type `S'.  Constructors can be friends, but they must
28870	     use a qualified name.
28871
28872	     Parse with an empty set of declaration specifiers since we're
28873	     trying to match a decl-specifier-seq of the first parameter.
28874	     This must be non-null so that cp_parser_simple_type_specifier
28875	     will recognize a constrained placeholder type such as:
28876	     'C<int> auto' where C is a type concept.  */
28877	  cp_decl_specifier_seq ctor_specs;
28878	  clear_decl_specs (&ctor_specs);
28879	  cp_parser_type_specifier (parser,
28880				    (friend_p ? CP_PARSER_FLAGS_NONE
28881				     : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
28882				    /*decl_specs=*/&ctor_specs,
28883				    /*is_declarator=*/true,
28884				    /*declares_class_or_enum=*/NULL,
28885				    /*is_cv_qualifier=*/NULL);
28886
28887	  parser->num_template_parameter_lists
28888	    = saved_num_template_parameter_lists;
28889
28890	  /* Leave the scope of the class.  */
28891	  if (pushed_scope)
28892	    pop_scope (pushed_scope);
28893
28894	  constructor_p = !cp_parser_error_occurred (parser);
28895	}
28896    }
28897
28898  /* We did not really want to consume any tokens.  */
28899  cp_parser_abort_tentative_parse (parser);
28900
28901  return constructor_p;
28902}
28903
28904/* Parse the definition of the function given by the DECL_SPECIFIERS,
28905   ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
28906   they must be performed once we are in the scope of the function.
28907
28908   Returns the function defined.  */
28909
28910static tree
28911cp_parser_function_definition_from_specifiers_and_declarator
28912  (cp_parser* parser,
28913   cp_decl_specifier_seq *decl_specifiers,
28914   tree attributes,
28915   const cp_declarator *declarator)
28916{
28917  tree fn;
28918  bool success_p;
28919
28920  /* Begin the function-definition.  */
28921  success_p = start_function (decl_specifiers, declarator, attributes);
28922
28923  /* The things we're about to see are not directly qualified by any
28924     template headers we've seen thus far.  */
28925  reset_specialization ();
28926
28927  /* If there were names looked up in the decl-specifier-seq that we
28928     did not check, check them now.  We must wait until we are in the
28929     scope of the function to perform the checks, since the function
28930     might be a friend.  */
28931  perform_deferred_access_checks (tf_warning_or_error);
28932
28933  if (success_p)
28934    {
28935      cp_finalize_omp_declare_simd (parser, current_function_decl);
28936      parser->omp_declare_simd = NULL;
28937      cp_finalize_oacc_routine (parser, current_function_decl, true);
28938      parser->oacc_routine = NULL;
28939    }
28940
28941  if (!success_p)
28942    {
28943      /* Skip the entire function.  */
28944      cp_parser_skip_to_end_of_block_or_statement (parser);
28945      fn = error_mark_node;
28946    }
28947  else if (DECL_INITIAL (current_function_decl) != error_mark_node)
28948    {
28949      /* Seen already, skip it.  An error message has already been output.  */
28950      cp_parser_skip_to_end_of_block_or_statement (parser);
28951      fn = current_function_decl;
28952      current_function_decl = NULL_TREE;
28953      /* If this is a function from a class, pop the nested class.  */
28954      if (current_class_name)
28955	pop_nested_class ();
28956    }
28957  else
28958    {
28959      timevar_id_t tv;
28960      if (DECL_DECLARED_INLINE_P (current_function_decl))
28961        tv = TV_PARSE_INLINE;
28962      else
28963        tv = TV_PARSE_FUNC;
28964      timevar_push (tv);
28965      fn = cp_parser_function_definition_after_declarator (parser,
28966							 /*inline_p=*/false);
28967      timevar_pop (tv);
28968    }
28969
28970  return fn;
28971}
28972
28973/* Parse the part of a function-definition that follows the
28974   declarator.  INLINE_P is TRUE iff this function is an inline
28975   function defined within a class-specifier.
28976
28977   Returns the function defined.  */
28978
28979static tree
28980cp_parser_function_definition_after_declarator (cp_parser* parser,
28981						bool inline_p)
28982{
28983  tree fn;
28984  bool saved_in_unbraced_linkage_specification_p;
28985  bool saved_in_function_body;
28986  unsigned saved_num_template_parameter_lists;
28987  cp_token *token;
28988  bool fully_implicit_function_template_p
28989    = parser->fully_implicit_function_template_p;
28990  parser->fully_implicit_function_template_p = false;
28991  tree implicit_template_parms
28992    = parser->implicit_template_parms;
28993  parser->implicit_template_parms = 0;
28994  cp_binding_level* implicit_template_scope
28995    = parser->implicit_template_scope;
28996  parser->implicit_template_scope = 0;
28997
28998  saved_in_function_body = parser->in_function_body;
28999  parser->in_function_body = true;
29000  /* If the next token is `return', then the code may be trying to
29001     make use of the "named return value" extension that G++ used to
29002     support.  */
29003  token = cp_lexer_peek_token (parser->lexer);
29004  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
29005    {
29006      /* Consume the `return' keyword.  */
29007      cp_lexer_consume_token (parser->lexer);
29008      /* Look for the identifier that indicates what value is to be
29009	 returned.  */
29010      cp_parser_identifier (parser);
29011      /* Issue an error message.  */
29012      error_at (token->location,
29013		"named return values are no longer supported");
29014      /* Skip tokens until we reach the start of the function body.  */
29015      while (true)
29016	{
29017	  cp_token *token = cp_lexer_peek_token (parser->lexer);
29018	  if (token->type == CPP_OPEN_BRACE
29019	      || token->type == CPP_EOF
29020	      || token->type == CPP_PRAGMA_EOL)
29021	    break;
29022	  cp_lexer_consume_token (parser->lexer);
29023	}
29024    }
29025  /* The `extern' in `extern "C" void f () { ... }' does not apply to
29026     anything declared inside `f'.  */
29027  saved_in_unbraced_linkage_specification_p
29028    = parser->in_unbraced_linkage_specification_p;
29029  parser->in_unbraced_linkage_specification_p = false;
29030  /* Inside the function, surrounding template-parameter-lists do not
29031     apply.  */
29032  saved_num_template_parameter_lists
29033    = parser->num_template_parameter_lists;
29034  parser->num_template_parameter_lists = 0;
29035
29036  /* If the next token is `try', `__transaction_atomic', or
29037     `__transaction_relaxed`, then we are looking at either function-try-block
29038     or function-transaction-block.  Note that all of these include the
29039     function-body.  */
29040  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
29041    cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
29042  else if (cp_lexer_next_token_is_keyword (parser->lexer,
29043      RID_TRANSACTION_RELAXED))
29044    cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
29045  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29046    cp_parser_function_try_block (parser);
29047  else
29048    cp_parser_ctor_initializer_opt_and_function_body
29049      (parser, /*in_function_try_block=*/false);
29050
29051  /* Finish the function.  */
29052  fn = finish_function (inline_p);
29053  /* Generate code for it, if necessary.  */
29054  expand_or_defer_fn (fn);
29055  /* Restore the saved values.  */
29056  parser->in_unbraced_linkage_specification_p
29057    = saved_in_unbraced_linkage_specification_p;
29058  parser->num_template_parameter_lists
29059    = saved_num_template_parameter_lists;
29060  parser->in_function_body = saved_in_function_body;
29061
29062  parser->fully_implicit_function_template_p
29063    = fully_implicit_function_template_p;
29064  parser->implicit_template_parms
29065    = implicit_template_parms;
29066  parser->implicit_template_scope
29067    = implicit_template_scope;
29068
29069  if (parser->fully_implicit_function_template_p)
29070    finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
29071
29072  return fn;
29073}
29074
29075/* Parse a template-declaration body (following argument list).  */
29076
29077static void
29078cp_parser_template_declaration_after_parameters (cp_parser* parser,
29079						 tree parameter_list,
29080						 bool member_p)
29081{
29082  tree decl = NULL_TREE;
29083  bool friend_p = false;
29084
29085  /* We just processed one more parameter list.  */
29086  ++parser->num_template_parameter_lists;
29087
29088  /* Get the deferred access checks from the parameter list.  These
29089     will be checked once we know what is being declared, as for a
29090     member template the checks must be performed in the scope of the
29091     class containing the member.  */
29092  vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
29093
29094  /* Tentatively parse for a new template parameter list, which can either be
29095     the template keyword or a template introduction.  */
29096  if (cp_parser_template_declaration_after_export (parser, member_p))
29097    /* OK */;
29098  else if (cxx_dialect >= cxx11
29099	   && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29100    decl = cp_parser_alias_declaration (parser);
29101  else if (cxx_dialect >= cxx2a /* Implies flag_concept.  */
29102           && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
29103           && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
29104    /* Allow 'concept bool' to be handled as per the TS.  */
29105    decl = cp_parser_concept_definition (parser);
29106  else
29107    {
29108      /* There are no access checks when parsing a template, as we do not
29109	 know if a specialization will be a friend.  */
29110      push_deferring_access_checks (dk_no_check);
29111      cp_token *token = cp_lexer_peek_token (parser->lexer);
29112      decl = cp_parser_single_declaration (parser,
29113					   checks,
29114					   member_p,
29115                                           /*explicit_specialization_p=*/false,
29116					   &friend_p);
29117      pop_deferring_access_checks ();
29118
29119      /* If this is a member template declaration, let the front
29120	 end know.  */
29121      if (member_p && !friend_p && decl)
29122	{
29123	  if (TREE_CODE (decl) == TYPE_DECL)
29124	    cp_parser_check_access_in_redeclaration (decl, token->location);
29125
29126	  decl = finish_member_template_decl (decl);
29127	}
29128      else if (friend_p && decl
29129	       && DECL_DECLARES_TYPE_P (decl))
29130	make_friend_class (current_class_type, TREE_TYPE (decl),
29131			   /*complain=*/true);
29132    }
29133  /* We are done with the current parameter list.  */
29134  --parser->num_template_parameter_lists;
29135
29136  pop_deferring_access_checks ();
29137
29138  /* Finish up.  */
29139  finish_template_decl (parameter_list);
29140
29141  /* Check the template arguments for a literal operator template.  */
29142  if (decl
29143      && DECL_DECLARES_FUNCTION_P (decl)
29144      && UDLIT_OPER_P (DECL_NAME (decl)))
29145    {
29146      bool ok = true;
29147      if (parameter_list == NULL_TREE)
29148	ok = false;
29149      else
29150	{
29151	  int num_parms = TREE_VEC_LENGTH (parameter_list);
29152	  if (num_parms == 1)
29153	    {
29154	      tree parm_list = TREE_VEC_ELT (parameter_list, 0);
29155	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
29156	      if (TREE_CODE (parm) != PARM_DECL)
29157		ok = false;
29158	      else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
29159		       && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29160		/* OK, C++20 string literal operator template.  We don't need
29161		   to warn in lower dialects here because we will have already
29162		   warned about the template parameter.  */;
29163	      else if (TREE_TYPE (parm) != char_type_node
29164		       || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29165		ok = false;
29166	    }
29167	  else if (num_parms == 2 && cxx_dialect >= cxx14)
29168	    {
29169	      tree parm_type = TREE_VEC_ELT (parameter_list, 0);
29170	      tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
29171	      tree parm_list = TREE_VEC_ELT (parameter_list, 1);
29172	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
29173	      if (TREE_CODE (parm) != PARM_DECL
29174		  || TREE_TYPE (parm) != TREE_TYPE (type)
29175		  || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29176		ok = false;
29177	      else
29178		/* http://cplusplus.github.io/EWG/ewg-active.html#66  */
29179		pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
29180			 "ISO C++ did not adopt string literal operator templa"
29181			 "tes taking an argument pack of characters");
29182	    }
29183	  else
29184	    ok = false;
29185	}
29186      if (!ok)
29187	{
29188	  if (cxx_dialect > cxx17)
29189	    error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
29190		      "template %qD has invalid parameter list; expected "
29191		      "non-type template parameter pack %<<char...>%> or "
29192		      "single non-type parameter of class type",
29193		      decl);
29194	  else
29195	    error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
29196		      "template %qD has invalid parameter list; expected "
29197		      "non-type template parameter pack %<<char...>%>",
29198		      decl);
29199	}
29200    }
29201
29202  /* Register member declarations.  */
29203  if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
29204    finish_member_declaration (decl);
29205  /* If DECL is a function template, we must return to parse it later.
29206     (Even though there is no definition, there might be default
29207     arguments that need handling.)  */
29208  if (member_p && decl
29209      && DECL_DECLARES_FUNCTION_P (decl))
29210    vec_safe_push (unparsed_funs_with_definitions, decl);
29211}
29212
29213/* Parse a template introduction header for a template-declaration.  Returns
29214   false if tentative parse fails.  */
29215
29216static bool
29217cp_parser_template_introduction (cp_parser* parser, bool member_p)
29218{
29219  cp_parser_parse_tentatively (parser);
29220
29221  tree saved_scope = parser->scope;
29222  tree saved_object_scope = parser->object_scope;
29223  tree saved_qualifying_scope = parser->qualifying_scope;
29224  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29225
29226  cp_token *start_token = cp_lexer_peek_token (parser->lexer);
29227
29228  /* In classes don't parse valid unnamed bitfields as invalid
29229     template introductions.  */
29230  if (member_p)
29231    parser->colon_corrects_to_scope_p = false;
29232
29233  /* Look for the optional `::' operator.  */
29234  cp_parser_global_scope_opt (parser,
29235			      /*current_scope_valid_p=*/false);
29236  /* Look for the nested-name-specifier.  */
29237  cp_parser_nested_name_specifier_opt (parser,
29238				       /*typename_keyword_p=*/false,
29239				       /*check_dependency_p=*/true,
29240				       /*type_p=*/false,
29241				       /*is_declaration=*/false);
29242
29243  cp_token *token = cp_lexer_peek_token (parser->lexer);
29244  tree concept_name = cp_parser_identifier (parser);
29245
29246  /* Look up the concept for which we will be matching
29247     template parameters.  */
29248  tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
29249						 token->location);
29250  parser->scope = saved_scope;
29251  parser->object_scope = saved_object_scope;
29252  parser->qualifying_scope = saved_qualifying_scope;
29253  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29254
29255  if (concept_name == error_mark_node
29256      || (seen_error () && !concept_definition_p (tmpl_decl)))
29257    cp_parser_simulate_error (parser);
29258
29259  /* Look for opening brace for introduction.  */
29260  matching_braces braces;
29261  braces.require_open (parser);
29262  location_t open_loc = input_location;
29263
29264  if (!cp_parser_parse_definitely (parser))
29265    return false;
29266
29267  push_deferring_access_checks (dk_deferred);
29268
29269  /* Build vector of placeholder parameters and grab
29270     matching identifiers.  */
29271  tree introduction_list = cp_parser_introduction_list (parser);
29272
29273  /* Look for closing brace for introduction.  */
29274  if (!braces.require_close (parser))
29275    return true;
29276
29277  /* The introduction-list shall not be empty.  */
29278  int nargs = TREE_VEC_LENGTH (introduction_list);
29279  if (nargs == 0)
29280    {
29281      /* In cp_parser_introduction_list we have already issued an error.  */
29282      return true;
29283    }
29284
29285  if (tmpl_decl == error_mark_node)
29286    {
29287      cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
29288				   token->location);
29289      return true;
29290    }
29291
29292  /* Build and associate the constraint.  */
29293  location_t introduction_loc = make_location (open_loc,
29294					       start_token->location,
29295					       parser->lexer);
29296  tree parms = finish_template_introduction (tmpl_decl,
29297					     introduction_list,
29298					     introduction_loc);
29299  if (parms && parms != error_mark_node)
29300    {
29301      if (!flag_concepts_ts)
29302	pedwarn (introduction_loc, 0, "template-introductions"
29303		 " are not part of C++20 concepts [-fconcepts-ts]");
29304
29305      cp_parser_template_declaration_after_parameters (parser, parms,
29306						       member_p);
29307      return true;
29308    }
29309
29310  if (parms == NULL_TREE)
29311    error_at (token->location, "no matching concept for template-introduction");
29312
29313  return true;
29314}
29315
29316/* Parse a normal template-declaration following the template keyword.  */
29317
29318static void
29319cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
29320{
29321  tree parameter_list;
29322  bool need_lang_pop;
29323  location_t location = input_location;
29324
29325  /* Look for the `<' token.  */
29326  if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
29327    return;
29328  if (at_class_scope_p () && current_function_decl)
29329    {
29330      /* 14.5.2.2 [temp.mem]
29331
29332         A local class shall not have member templates.  */
29333      error_at (location,
29334                "invalid declaration of member template in local class");
29335      cp_parser_skip_to_end_of_block_or_statement (parser);
29336      return;
29337    }
29338  /* [temp]
29339
29340     A template ... shall not have C linkage.  */
29341  if (current_lang_name == lang_name_c)
29342    {
29343      error_at (location, "template with C linkage");
29344      maybe_show_extern_c_location ();
29345      /* Give it C++ linkage to avoid confusing other parts of the
29346         front end.  */
29347      push_lang_context (lang_name_cplusplus);
29348      need_lang_pop = true;
29349    }
29350  else
29351    need_lang_pop = false;
29352
29353  /* We cannot perform access checks on the template parameter
29354     declarations until we know what is being declared, just as we
29355     cannot check the decl-specifier list.  */
29356  push_deferring_access_checks (dk_deferred);
29357
29358  /* If the next token is `>', then we have an invalid
29359     specialization.  Rather than complain about an invalid template
29360     parameter, issue an error message here.  */
29361  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
29362    {
29363      cp_parser_error (parser, "invalid explicit specialization");
29364      begin_specialization ();
29365      parameter_list = NULL_TREE;
29366    }
29367  else
29368    {
29369      /* Parse the template parameters.  */
29370      parameter_list = cp_parser_template_parameter_list (parser);
29371    }
29372
29373  /* Look for the `>'.  */
29374  cp_parser_skip_to_end_of_template_parameter_list (parser);
29375
29376  /* Manage template requirements */
29377  if (flag_concepts)
29378  {
29379    tree reqs = get_shorthand_constraints (current_template_parms);
29380    if (tree treqs = cp_parser_requires_clause_opt (parser, false))
29381      reqs = combine_constraint_expressions (reqs, treqs);
29382    TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
29383  }
29384
29385  cp_parser_template_declaration_after_parameters (parser, parameter_list,
29386						   member_p);
29387
29388  /* For the erroneous case of a template with C linkage, we pushed an
29389     implicit C++ linkage scope; exit that scope now.  */
29390  if (need_lang_pop)
29391    pop_lang_context ();
29392}
29393
29394/* Parse a template-declaration, assuming that the `export' (and
29395   `extern') keywords, if present, has already been scanned.  MEMBER_P
29396   is as for cp_parser_template_declaration.  */
29397
29398static bool
29399cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
29400{
29401  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29402    {
29403      cp_lexer_consume_token (parser->lexer);
29404      cp_parser_explicit_template_declaration (parser, member_p);
29405      return true;
29406    }
29407  else if (flag_concepts)
29408    return cp_parser_template_introduction (parser, member_p);
29409
29410  return false;
29411}
29412
29413/* Perform the deferred access checks from a template-parameter-list.
29414   CHECKS is a TREE_LIST of access checks, as returned by
29415   get_deferred_access_checks.  */
29416
29417static void
29418cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
29419{
29420  ++processing_template_parmlist;
29421  perform_access_checks (checks, tf_warning_or_error);
29422  --processing_template_parmlist;
29423}
29424
29425/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
29426   `function-definition' sequence that follows a template header.
29427   If MEMBER_P is true, this declaration appears in a class scope.
29428
29429   Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
29430   *FRIEND_P is set to TRUE iff the declaration is a friend.  */
29431
29432static tree
29433cp_parser_single_declaration (cp_parser* parser,
29434			      vec<deferred_access_check, va_gc> *checks,
29435			      bool member_p,
29436                              bool explicit_specialization_p,
29437			      bool* friend_p)
29438{
29439  int declares_class_or_enum;
29440  tree decl = NULL_TREE;
29441  cp_decl_specifier_seq decl_specifiers;
29442  bool function_definition_p = false;
29443  cp_token *decl_spec_token_start;
29444
29445  /* This function is only used when processing a template
29446     declaration.  */
29447  gcc_assert (innermost_scope_kind () == sk_template_parms
29448	      || innermost_scope_kind () == sk_template_spec);
29449
29450  /* Defer access checks until we know what is being declared.  */
29451  push_deferring_access_checks (dk_deferred);
29452
29453  /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
29454     alternative.  */
29455  decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
29456  cp_parser_decl_specifier_seq (parser,
29457				(CP_PARSER_FLAGS_OPTIONAL
29458				 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
29459				&decl_specifiers,
29460				&declares_class_or_enum);
29461  if (friend_p)
29462    *friend_p = cp_parser_friend_p (&decl_specifiers);
29463
29464  /* There are no template typedefs.  */
29465  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
29466    {
29467      error_at (decl_spec_token_start->location,
29468		"template declaration of %<typedef%>");
29469      decl = error_mark_node;
29470    }
29471
29472  /* Gather up the access checks that occurred the
29473     decl-specifier-seq.  */
29474  stop_deferring_access_checks ();
29475
29476  /* Check for the declaration of a template class.  */
29477  if (declares_class_or_enum)
29478    {
29479      if (cp_parser_declares_only_class_p (parser)
29480	  || (declares_class_or_enum & 2))
29481	{
29482	  /* If this is a declaration, but not a definition, associate
29483	     any constraints with the type declaration. Constraints
29484	     are associated with definitions in cp_parser_class_specifier.  */
29485	  if (declares_class_or_enum == 1)
29486	    associate_classtype_constraints (decl_specifiers.type);
29487
29488	  decl = shadow_tag (&decl_specifiers);
29489
29490	  /* In this case:
29491
29492	       struct C {
29493		 friend template <typename T> struct A<T>::B;
29494	       };
29495
29496	     A<T>::B will be represented by a TYPENAME_TYPE, and
29497	     therefore not recognized by shadow_tag.  */
29498	  if (friend_p && *friend_p
29499	      && !decl
29500	      && decl_specifiers.type
29501	      && TYPE_P (decl_specifiers.type))
29502	    decl = decl_specifiers.type;
29503
29504	  if (decl && decl != error_mark_node)
29505	    decl = TYPE_NAME (decl);
29506	  else
29507	    decl = error_mark_node;
29508
29509	  /* Perform access checks for template parameters.  */
29510	  cp_parser_perform_template_parameter_access_checks (checks);
29511
29512	  /* Give a helpful diagnostic for
29513	       template <class T> struct A { } a;
29514	     if we aren't already recovering from an error.  */
29515	  if (!cp_parser_declares_only_class_p (parser)
29516	      && !seen_error ())
29517	    {
29518	      error_at (cp_lexer_peek_token (parser->lexer)->location,
29519			"a class template declaration must not declare "
29520			"anything else");
29521	      cp_parser_skip_to_end_of_block_or_statement (parser);
29522	      goto out;
29523	    }
29524	}
29525    }
29526
29527  /* Complain about missing 'typename' or other invalid type names.  */
29528  if (!decl_specifiers.any_type_specifiers_p
29529      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
29530    {
29531      /* cp_parser_parse_and_diagnose_invalid_type_name calls
29532	 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
29533	 the rest of this declaration.  */
29534      decl = error_mark_node;
29535      goto out;
29536    }
29537
29538  /* If it's not a template class, try for a template function.  If
29539     the next token is a `;', then this declaration does not declare
29540     anything.  But, if there were errors in the decl-specifiers, then
29541     the error might well have come from an attempted class-specifier.
29542     In that case, there's no need to warn about a missing declarator.  */
29543  if (!decl
29544      && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
29545	  || decl_specifiers.type != error_mark_node))
29546    {
29547      decl = cp_parser_init_declarator (parser,
29548					CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
29549				        &decl_specifiers,
29550				        checks,
29551				        /*function_definition_allowed_p=*/true,
29552				        member_p,
29553				        declares_class_or_enum,
29554				        &function_definition_p,
29555					NULL, NULL, NULL);
29556
29557    /* 7.1.1-1 [dcl.stc]
29558
29559       A storage-class-specifier shall not be specified in an explicit
29560       specialization...  */
29561    if (decl
29562        && explicit_specialization_p
29563        && decl_specifiers.storage_class != sc_none)
29564      {
29565        error_at (decl_spec_token_start->location,
29566		  "explicit template specialization cannot have a storage class");
29567        decl = error_mark_node;
29568      }
29569
29570    if (decl && VAR_P (decl))
29571      check_template_variable (decl);
29572    }
29573
29574  /* Look for a trailing `;' after the declaration.  */
29575  if (!function_definition_p
29576      && (decl == error_mark_node
29577	  || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
29578    cp_parser_skip_to_end_of_block_or_statement (parser);
29579
29580 out:
29581  pop_deferring_access_checks ();
29582
29583  /* Clear any current qualification; whatever comes next is the start
29584     of something new.  */
29585  parser->scope = NULL_TREE;
29586  parser->qualifying_scope = NULL_TREE;
29587  parser->object_scope = NULL_TREE;
29588
29589  return decl;
29590}
29591
29592/* Parse a cast-expression that is not the operand of a unary "&".  */
29593
29594static cp_expr
29595cp_parser_simple_cast_expression (cp_parser *parser)
29596{
29597  return cp_parser_cast_expression (parser, /*address_p=*/false,
29598				    /*cast_p=*/false, /*decltype*/false, NULL);
29599}
29600
29601/* Parse a functional cast to TYPE.  Returns an expression
29602   representing the cast.  */
29603
29604static cp_expr
29605cp_parser_functional_cast (cp_parser* parser, tree type)
29606{
29607  vec<tree, va_gc> *vec;
29608  tree expression_list;
29609  cp_expr cast;
29610  bool nonconst_p;
29611
29612  location_t start_loc = input_location;
29613
29614  if (!type)
29615    type = error_mark_node;
29616
29617  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29618    {
29619      cp_lexer_set_source_position (parser->lexer);
29620      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29621      expression_list = cp_parser_braced_list (parser, &nonconst_p);
29622      CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
29623      if (TREE_CODE (type) == TYPE_DECL)
29624	type = TREE_TYPE (type);
29625
29626      cast = finish_compound_literal (type, expression_list,
29627				      tf_warning_or_error, fcl_functional);
29628      /* Create a location of the form:
29629	    type_name{i, f}
29630	    ^~~~~~~~~~~~~~~
29631	 with caret == start at the start of the type name,
29632	 finishing at the closing brace.  */
29633      location_t combined_loc = make_location (start_loc, start_loc,
29634					       parser->lexer);
29635      cast.set_location (combined_loc);
29636      return cast;
29637   }
29638
29639
29640  vec = cp_parser_parenthesized_expression_list (parser, non_attr,
29641						 /*cast_p=*/true,
29642						 /*allow_expansion_p=*/true,
29643						 /*non_constant_p=*/NULL);
29644  if (vec == NULL)
29645    expression_list = error_mark_node;
29646  else
29647    {
29648      expression_list = build_tree_list_vec (vec);
29649      release_tree_vector (vec);
29650    }
29651
29652  /* Create a location of the form:
29653       float(i)
29654       ^~~~~~~~
29655     with caret == start at the start of the type name,
29656     finishing at the closing paren.  */
29657  location_t combined_loc = make_location (start_loc, start_loc,
29658					   parser->lexer);
29659  cast = build_functional_cast (combined_loc, type, expression_list,
29660                                tf_warning_or_error);
29661
29662  /* [expr.const]/1: In an integral constant expression "only type
29663     conversions to integral or enumeration type can be used".  */
29664  if (TREE_CODE (type) == TYPE_DECL)
29665    type = TREE_TYPE (type);
29666  if (cast != error_mark_node
29667      && !cast_valid_in_integral_constant_expression_p (type)
29668      && cp_parser_non_integral_constant_expression (parser,
29669						     NIC_CONSTRUCTOR))
29670    return error_mark_node;
29671
29672  return cast;
29673}
29674
29675/* Save the tokens that make up the body of a member function defined
29676   in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
29677   already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
29678   specifiers applied to the declaration.  Returns the FUNCTION_DECL
29679   for the member function.  */
29680
29681static tree
29682cp_parser_save_member_function_body (cp_parser* parser,
29683				     cp_decl_specifier_seq *decl_specifiers,
29684				     cp_declarator *declarator,
29685				     tree attributes)
29686{
29687  cp_token *first;
29688  cp_token *last;
29689  tree fn;
29690  bool function_try_block = false;
29691
29692  /* Create the FUNCTION_DECL.  */
29693  fn = grokmethod (decl_specifiers, declarator, attributes);
29694  cp_finalize_omp_declare_simd (parser, fn);
29695  cp_finalize_oacc_routine (parser, fn, true);
29696  /* If something went badly wrong, bail out now.  */
29697  if (fn == error_mark_node)
29698    {
29699      /* If there's a function-body, skip it.  */
29700      if (cp_parser_token_starts_function_definition_p
29701	  (cp_lexer_peek_token (parser->lexer)))
29702	cp_parser_skip_to_end_of_block_or_statement (parser);
29703      return error_mark_node;
29704    }
29705
29706  /* Remember it, if there are default args to post process.  */
29707  cp_parser_save_default_args (parser, fn);
29708
29709  /* Save away the tokens that make up the body of the
29710     function.  */
29711  first = parser->lexer->next_token;
29712
29713  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
29714    cp_lexer_consume_token (parser->lexer);
29715  else if (cp_lexer_next_token_is_keyword (parser->lexer,
29716					   RID_TRANSACTION_ATOMIC))
29717    {
29718      cp_lexer_consume_token (parser->lexer);
29719      /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
29720      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
29721	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
29722	  && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
29723	      || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
29724	  && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
29725	  && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
29726	{
29727	  cp_lexer_consume_token (parser->lexer);
29728	  cp_lexer_consume_token (parser->lexer);
29729	  cp_lexer_consume_token (parser->lexer);
29730	  cp_lexer_consume_token (parser->lexer);
29731	  cp_lexer_consume_token (parser->lexer);
29732	}
29733      else
29734	while (cp_next_tokens_can_be_gnu_attribute_p (parser)
29735	       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
29736	  {
29737	    cp_lexer_consume_token (parser->lexer);
29738	    if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29739	      break;
29740	  }
29741    }
29742
29743  /* Handle function try blocks.  */
29744  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29745    {
29746      cp_lexer_consume_token (parser->lexer);
29747      function_try_block = true;
29748    }
29749  /* We can have braced-init-list mem-initializers before the fn body.  */
29750  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29751    {
29752      cp_lexer_consume_token (parser->lexer);
29753      while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
29754	{
29755	  /* cache_group will stop after an un-nested { } pair, too.  */
29756	  if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29757	    break;
29758
29759	  /* variadic mem-inits have ... after the ')'.  */
29760	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29761	    cp_lexer_consume_token (parser->lexer);
29762	}
29763    }
29764  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29765  /* Handle function try blocks.  */
29766  if (function_try_block)
29767    while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
29768      cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29769  last = parser->lexer->next_token;
29770
29771  /* Save away the inline definition; we will process it when the
29772     class is complete.  */
29773  DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
29774  DECL_PENDING_INLINE_P (fn) = 1;
29775
29776  /* We need to know that this was defined in the class, so that
29777     friend templates are handled correctly.  */
29778  DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
29779
29780  /* Add FN to the queue of functions to be parsed later.  */
29781  vec_safe_push (unparsed_funs_with_definitions, fn);
29782
29783  return fn;
29784}
29785
29786/* Save the tokens that make up the in-class initializer for a non-static
29787   data member.  Returns a DEFERRED_PARSE.  */
29788
29789static tree
29790cp_parser_save_nsdmi (cp_parser* parser)
29791{
29792  return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
29793}
29794
29795/* Parse a template-argument-list, as well as the trailing ">" (but
29796   not the opening "<").  See cp_parser_template_argument_list for the
29797   return value.  */
29798
29799static tree
29800cp_parser_enclosed_template_argument_list (cp_parser* parser)
29801{
29802  tree arguments;
29803  tree saved_scope;
29804  tree saved_qualifying_scope;
29805  tree saved_object_scope;
29806  bool saved_greater_than_is_operator_p;
29807
29808  /* [temp.names]
29809
29810     When parsing a template-id, the first non-nested `>' is taken as
29811     the end of the template-argument-list rather than a greater-than
29812     operator.  */
29813  saved_greater_than_is_operator_p
29814    = parser->greater_than_is_operator_p;
29815  parser->greater_than_is_operator_p = false;
29816  /* Parsing the argument list may modify SCOPE, so we save it
29817     here.  */
29818  saved_scope = parser->scope;
29819  saved_qualifying_scope = parser->qualifying_scope;
29820  saved_object_scope = parser->object_scope;
29821  /* We need to evaluate the template arguments, even though this
29822     template-id may be nested within a "sizeof".  */
29823  cp_evaluated ev;
29824  /* Parse the template-argument-list itself.  */
29825  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
29826      || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29827    arguments = NULL_TREE;
29828  else
29829    arguments = cp_parser_template_argument_list (parser);
29830  /* Look for the `>' that ends the template-argument-list. If we find
29831     a '>>' instead, it's probably just a typo.  */
29832  if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29833    {
29834      if (cxx_dialect != cxx98)
29835        {
29836          /* In C++0x, a `>>' in a template argument list or cast
29837             expression is considered to be two separate `>'
29838             tokens. So, change the current token to a `>', but don't
29839             consume it: it will be consumed later when the outer
29840             template argument list (or cast expression) is parsed.
29841             Note that this replacement of `>' for `>>' is necessary
29842             even if we are parsing tentatively: in the tentative
29843             case, after calling
29844             cp_parser_enclosed_template_argument_list we will always
29845             throw away all of the template arguments and the first
29846             closing `>', either because the template argument list
29847             was erroneous or because we are replacing those tokens
29848             with a CPP_TEMPLATE_ID token.  The second `>' (which will
29849             not have been thrown away) is needed either to close an
29850             outer template argument list or to complete a new-style
29851             cast.  */
29852	  cp_token *token = cp_lexer_peek_token (parser->lexer);
29853          token->type = CPP_GREATER;
29854        }
29855      else if (!saved_greater_than_is_operator_p)
29856	{
29857	  /* If we're in a nested template argument list, the '>>' has
29858	    to be a typo for '> >'. We emit the error message, but we
29859	    continue parsing and we push a '>' as next token, so that
29860	    the argument list will be parsed correctly.  Note that the
29861	    global source location is still on the token before the
29862	    '>>', so we need to say explicitly where we want it.  */
29863	  cp_token *token = cp_lexer_peek_token (parser->lexer);
29864	  gcc_rich_location richloc (token->location);
29865	  richloc.add_fixit_replace ("> >");
29866	  error_at (&richloc, "%<>>%> should be %<> >%> "
29867		    "within a nested template argument list");
29868
29869	  token->type = CPP_GREATER;
29870	}
29871      else
29872	{
29873	  /* If this is not a nested template argument list, the '>>'
29874	    is a typo for '>'. Emit an error message and continue.
29875	    Same deal about the token location, but here we can get it
29876	    right by consuming the '>>' before issuing the diagnostic.  */
29877	  cp_token *token = cp_lexer_consume_token (parser->lexer);
29878	  error_at (token->location,
29879		    "spurious %<>>%>, use %<>%> to terminate "
29880		    "a template argument list");
29881	}
29882    }
29883  else
29884    cp_parser_skip_to_end_of_template_parameter_list (parser);
29885  /* The `>' token might be a greater-than operator again now.  */
29886  parser->greater_than_is_operator_p
29887    = saved_greater_than_is_operator_p;
29888  /* Restore the SAVED_SCOPE.  */
29889  parser->scope = saved_scope;
29890  parser->qualifying_scope = saved_qualifying_scope;
29891  parser->object_scope = saved_object_scope;
29892
29893  return arguments;
29894}
29895
29896/* MEMBER_FUNCTION is a member function, or a friend.  If default
29897   arguments, or the body of the function have not yet been parsed,
29898   parse them now.  */
29899
29900static void
29901cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
29902{
29903  timevar_push (TV_PARSE_INMETH);
29904  /* If this member is a template, get the underlying
29905     FUNCTION_DECL.  */
29906  if (DECL_FUNCTION_TEMPLATE_P (member_function))
29907    member_function = DECL_TEMPLATE_RESULT (member_function);
29908
29909  /* There should not be any class definitions in progress at this
29910     point; the bodies of members are only parsed outside of all class
29911     definitions.  */
29912  gcc_assert (parser->num_classes_being_defined == 0);
29913  /* While we're parsing the member functions we might encounter more
29914     classes.  We want to handle them right away, but we don't want
29915     them getting mixed up with functions that are currently in the
29916     queue.  */
29917  push_unparsed_function_queues (parser);
29918
29919  /* Make sure that any template parameters are in scope.  */
29920  maybe_begin_member_template_processing (member_function);
29921
29922  /* If the body of the function has not yet been parsed, parse it
29923     now.  */
29924  if (DECL_PENDING_INLINE_P (member_function))
29925    {
29926      tree function_scope;
29927      cp_token_cache *tokens;
29928
29929      /* The function is no longer pending; we are processing it.  */
29930      tokens = DECL_PENDING_INLINE_INFO (member_function);
29931      DECL_PENDING_INLINE_INFO (member_function) = NULL;
29932      DECL_PENDING_INLINE_P (member_function) = 0;
29933
29934      /* If this is a local class, enter the scope of the containing
29935	 function.  */
29936      function_scope = current_function_decl;
29937      if (function_scope)
29938	push_function_context ();
29939
29940      /* Push the body of the function onto the lexer stack.  */
29941      cp_parser_push_lexer_for_tokens (parser, tokens);
29942
29943      /* Let the front end know that we going to be defining this
29944	 function.  */
29945      start_preparsed_function (member_function, NULL_TREE,
29946				SF_PRE_PARSED | SF_INCLASS_INLINE);
29947
29948      /* Don't do access checking if it is a templated function.  */
29949      if (processing_template_decl)
29950	push_deferring_access_checks (dk_no_check);
29951
29952      /* #pragma omp declare reduction needs special parsing.  */
29953      if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
29954	{
29955	  parser->lexer->in_pragma = true;
29956	  cp_parser_omp_declare_reduction_exprs (member_function, parser);
29957	  finish_function (/*inline_p=*/true);
29958	  cp_check_omp_declare_reduction (member_function);
29959	}
29960      else
29961	/* Now, parse the body of the function.  */
29962	cp_parser_function_definition_after_declarator (parser,
29963							/*inline_p=*/true);
29964
29965      if (processing_template_decl)
29966	pop_deferring_access_checks ();
29967
29968      /* Leave the scope of the containing function.  */
29969      if (function_scope)
29970	pop_function_context ();
29971      cp_parser_pop_lexer (parser);
29972    }
29973
29974  /* Remove any template parameters from the symbol table.  */
29975  maybe_end_member_template_processing ();
29976
29977  /* Restore the queue.  */
29978  pop_unparsed_function_queues (parser);
29979  timevar_pop (TV_PARSE_INMETH);
29980}
29981
29982/* If DECL contains any default args, remember it on the unparsed
29983   functions queue.  */
29984
29985static void
29986cp_parser_save_default_args (cp_parser* parser, tree decl)
29987{
29988  tree probe;
29989
29990  for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
29991       probe;
29992       probe = TREE_CHAIN (probe))
29993    if (TREE_PURPOSE (probe))
29994      {
29995	cp_default_arg_entry entry = {current_class_type, decl};
29996	vec_safe_push (unparsed_funs_with_default_args, entry);
29997	break;
29998      }
29999
30000  /* Remember if there is a noexcept-specifier to post process.  */
30001  tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
30002  if (UNPARSED_NOEXCEPT_SPEC_P (spec))
30003    vec_safe_push (unparsed_noexcepts, decl);
30004}
30005
30006/* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30007   which is either a FIELD_DECL or PARM_DECL.  Parse it and return
30008   the result.  For a PARM_DECL, PARMTYPE is the corresponding type
30009   from the parameter-type-list.  */
30010
30011static tree
30012cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
30013				      tree default_arg, tree parmtype)
30014{
30015  cp_token_cache *tokens;
30016  tree parsed_arg;
30017  bool dummy;
30018
30019  if (default_arg == error_mark_node)
30020    return error_mark_node;
30021
30022  /* Push the saved tokens for the default argument onto the parser's
30023     lexer stack.  */
30024  tokens = DEFPARSE_TOKENS (default_arg);
30025  cp_parser_push_lexer_for_tokens (parser, tokens);
30026
30027  start_lambda_scope (decl);
30028
30029  /* Parse the default argument.  */
30030  parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
30031  if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
30032    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
30033
30034  finish_lambda_scope ();
30035
30036  if (parsed_arg == error_mark_node)
30037    cp_parser_skip_to_end_of_statement (parser);
30038
30039  if (!processing_template_decl)
30040    {
30041      /* In a non-template class, check conversions now.  In a template,
30042	 we'll wait and instantiate these as needed.  */
30043      if (TREE_CODE (decl) == PARM_DECL)
30044	parsed_arg = check_default_argument (parmtype, parsed_arg,
30045					     tf_warning_or_error);
30046      else if (maybe_reject_flexarray_init (decl, parsed_arg))
30047	parsed_arg = error_mark_node;
30048      else
30049	parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
30050    }
30051
30052  /* If the token stream has not been completely used up, then
30053     there was extra junk after the end of the default
30054     argument.  */
30055  if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30056    {
30057      if (TREE_CODE (decl) == PARM_DECL)
30058	cp_parser_error (parser, "expected %<,%>");
30059      else
30060	cp_parser_error (parser, "expected %<;%>");
30061    }
30062
30063  /* Revert to the main lexer.  */
30064  cp_parser_pop_lexer (parser);
30065
30066  return parsed_arg;
30067}
30068
30069/* FIELD is a non-static data member with an initializer which we saved for
30070   later; parse it now.  */
30071
30072static void
30073cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
30074{
30075  tree def;
30076
30077  maybe_begin_member_template_processing (field);
30078
30079  push_unparsed_function_queues (parser);
30080  def = cp_parser_late_parse_one_default_arg (parser, field,
30081					      DECL_INITIAL (field),
30082					      NULL_TREE);
30083  pop_unparsed_function_queues (parser);
30084
30085  maybe_end_member_template_processing ();
30086
30087  DECL_INITIAL (field) = def;
30088}
30089
30090/* FN is a FUNCTION_DECL which may contains a parameter with an
30091   unparsed DEFERRED_PARSE.  Parse the default args now.  This function
30092   assumes that the current scope is the scope in which the default
30093   argument should be processed.  */
30094
30095static void
30096cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
30097{
30098  unsigned char saved_local_variables_forbidden_p;
30099  tree parm, parmdecl;
30100
30101  /* While we're parsing the default args, we might (due to the
30102     statement expression extension) encounter more classes.  We want
30103     to handle them right away, but we don't want them getting mixed
30104     up with default args that are currently in the queue.  */
30105  push_unparsed_function_queues (parser);
30106
30107  /* Local variable names (and the `this' keyword) may not appear
30108     in a default argument.  */
30109  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
30110  parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
30111
30112  push_defarg_context (fn);
30113
30114  for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
30115	 parmdecl = DECL_ARGUMENTS (fn);
30116       parm && parm != void_list_node;
30117       parm = TREE_CHAIN (parm),
30118	 parmdecl = DECL_CHAIN (parmdecl))
30119    {
30120      tree default_arg = TREE_PURPOSE (parm);
30121      tree parsed_arg;
30122      vec<tree, va_gc> *insts;
30123      tree copy;
30124      unsigned ix;
30125
30126      if (!default_arg)
30127	continue;
30128
30129      if (TREE_CODE (default_arg) != DEFERRED_PARSE)
30130	/* This can happen for a friend declaration for a function
30131	   already declared with default arguments.  */
30132	continue;
30133
30134      parsed_arg
30135	= cp_parser_late_parse_one_default_arg (parser, parmdecl,
30136						default_arg,
30137						TREE_VALUE (parm));
30138      TREE_PURPOSE (parm) = parsed_arg;
30139
30140      /* Update any instantiations we've already created.  */
30141      for (insts = DEFPARSE_INSTANTIATIONS (default_arg), ix = 0;
30142	   vec_safe_iterate (insts, ix, &copy); ix++)
30143	TREE_PURPOSE (copy) = parsed_arg;
30144    }
30145
30146  pop_defarg_context ();
30147
30148  /* Make sure no default arg is missing.  */
30149  check_default_args (fn);
30150
30151  /* Restore the state of local_variables_forbidden_p.  */
30152  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
30153
30154  /* Restore the queue.  */
30155  pop_unparsed_function_queues (parser);
30156}
30157
30158/* Subroutine of cp_parser_sizeof_operand, for handling C++11
30159
30160     sizeof ... ( identifier )
30161
30162   where the 'sizeof' token has already been consumed.  */
30163
30164static tree
30165cp_parser_sizeof_pack (cp_parser *parser)
30166{
30167  /* Consume the `...'.  */
30168  cp_lexer_consume_token (parser->lexer);
30169  maybe_warn_variadic_templates ();
30170
30171  matching_parens parens;
30172  bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
30173  if (paren)
30174    parens.consume_open (parser);
30175  else
30176    permerror (cp_lexer_peek_token (parser->lexer)->location,
30177	       "%<sizeof...%> argument must be surrounded by parentheses");
30178
30179  cp_token *token = cp_lexer_peek_token (parser->lexer);
30180  tree name = cp_parser_identifier (parser);
30181  if (name == error_mark_node)
30182    return error_mark_node;
30183  /* The name is not qualified.  */
30184  parser->scope = NULL_TREE;
30185  parser->qualifying_scope = NULL_TREE;
30186  parser->object_scope = NULL_TREE;
30187  tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
30188  if (expr == error_mark_node)
30189    cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
30190				 token->location);
30191  if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
30192    expr = TREE_TYPE (expr);
30193  else if (TREE_CODE (expr) == CONST_DECL)
30194    expr = DECL_INITIAL (expr);
30195  expr = make_pack_expansion (expr);
30196  PACK_EXPANSION_SIZEOF_P (expr) = true;
30197
30198  if (paren)
30199    parens.require_close (parser);
30200
30201  return expr;
30202}
30203
30204/* Parse the operand of `sizeof' (or a similar operator).  Returns
30205   either a TYPE or an expression, depending on the form of the
30206   input.  The KEYWORD indicates which kind of expression we have
30207   encountered.  */
30208
30209static tree
30210cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
30211{
30212  tree expr = NULL_TREE;
30213  const char *saved_message;
30214  const char *saved_message_arg;
30215  bool saved_integral_constant_expression_p;
30216  bool saved_non_integral_constant_expression_p;
30217
30218  /* If it's a `...', then we are computing the length of a parameter
30219     pack.  */
30220  if (keyword == RID_SIZEOF
30221      && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30222    return cp_parser_sizeof_pack (parser);
30223
30224  /* Types cannot be defined in a `sizeof' expression.  Save away the
30225     old message.  */
30226  saved_message = parser->type_definition_forbidden_message;
30227  saved_message_arg = parser->type_definition_forbidden_message_arg;
30228  parser->type_definition_forbidden_message
30229    = G_("types may not be defined in %qs expressions");
30230  parser->type_definition_forbidden_message_arg
30231    = IDENTIFIER_POINTER (ridpointers[keyword]);
30232
30233  /* The restrictions on constant-expressions do not apply inside
30234     sizeof expressions.  */
30235  saved_integral_constant_expression_p
30236    = parser->integral_constant_expression_p;
30237  saved_non_integral_constant_expression_p
30238    = parser->non_integral_constant_expression_p;
30239  parser->integral_constant_expression_p = false;
30240
30241  /* Do not actually evaluate the expression.  */
30242  ++cp_unevaluated_operand;
30243  ++c_inhibit_evaluation_warnings;
30244  /* If it's a `(', then we might be looking at the type-id
30245     construction.  */
30246  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30247    {
30248      tree type = NULL_TREE;
30249
30250      tentative_firewall firewall (parser);
30251
30252      /* We can't be sure yet whether we're looking at a type-id or an
30253	 expression.  */
30254      cp_parser_parse_tentatively (parser);
30255
30256      matching_parens parens;
30257      parens.consume_open (parser);
30258
30259      /* Note: as a GNU Extension, compound literals are considered
30260	 postfix-expressions as they are in C99, so they are valid
30261	 arguments to sizeof.  See comment in cp_parser_cast_expression
30262	 for details.  */
30263      if (cp_parser_compound_literal_p (parser))
30264	cp_parser_simulate_error (parser);
30265      else
30266	{
30267	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
30268	  parser->in_type_id_in_expr_p = true;
30269	  /* Look for the type-id.  */
30270	  type = cp_parser_type_id (parser);
30271	  /* Look for the closing `)'.  */
30272	  parens.require_close (parser);
30273	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
30274	}
30275
30276      /* If all went well, then we're done.  */
30277      if (cp_parser_parse_definitely (parser))
30278	expr = type;
30279      else
30280	{
30281	  /* Commit to the tentative_firewall so we get syntax errors.  */
30282	  cp_parser_commit_to_tentative_parse (parser);
30283
30284	  expr = cp_parser_unary_expression (parser);
30285	}
30286    }
30287  else
30288    expr = cp_parser_unary_expression (parser);
30289
30290  /* Go back to evaluating expressions.  */
30291  --cp_unevaluated_operand;
30292  --c_inhibit_evaluation_warnings;
30293
30294  /* And restore the old one.  */
30295  parser->type_definition_forbidden_message = saved_message;
30296  parser->type_definition_forbidden_message_arg = saved_message_arg;
30297  parser->integral_constant_expression_p
30298    = saved_integral_constant_expression_p;
30299  parser->non_integral_constant_expression_p
30300    = saved_non_integral_constant_expression_p;
30301
30302  return expr;
30303}
30304
30305/* If the current declaration has no declarator, return true.  */
30306
30307static bool
30308cp_parser_declares_only_class_p (cp_parser *parser)
30309{
30310  /* If the next token is a `;' or a `,' then there is no
30311     declarator.  */
30312  return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30313	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
30314}
30315
30316/* Update the DECL_SPECS to reflect the storage class indicated by
30317   KEYWORD.  */
30318
30319static void
30320cp_parser_set_storage_class (cp_parser *parser,
30321			     cp_decl_specifier_seq *decl_specs,
30322			     enum rid keyword,
30323			     cp_token *token)
30324{
30325  cp_storage_class storage_class;
30326
30327  if (parser->in_unbraced_linkage_specification_p)
30328    {
30329      error_at (token->location, "invalid use of %qD in linkage specification",
30330		ridpointers[keyword]);
30331      return;
30332    }
30333  else if (decl_specs->storage_class != sc_none)
30334    {
30335      decl_specs->conflicting_specifiers_p = true;
30336      return;
30337    }
30338
30339  if ((keyword == RID_EXTERN || keyword == RID_STATIC)
30340      && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
30341      && decl_specs->gnu_thread_keyword_p)
30342    {
30343      pedwarn (decl_specs->locations[ds_thread], 0,
30344		"%<__thread%> before %qD", ridpointers[keyword]);
30345    }
30346
30347  switch (keyword)
30348    {
30349    case RID_AUTO:
30350      storage_class = sc_auto;
30351      break;
30352    case RID_REGISTER:
30353      storage_class = sc_register;
30354      break;
30355    case RID_STATIC:
30356      storage_class = sc_static;
30357      break;
30358    case RID_EXTERN:
30359      storage_class = sc_extern;
30360      break;
30361    case RID_MUTABLE:
30362      storage_class = sc_mutable;
30363      break;
30364    default:
30365      gcc_unreachable ();
30366    }
30367  decl_specs->storage_class = storage_class;
30368  set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
30369
30370  /* A storage class specifier cannot be applied alongside a typedef
30371     specifier. If there is a typedef specifier present then set
30372     conflicting_specifiers_p which will trigger an error later
30373     on in grokdeclarator. */
30374  if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
30375    decl_specs->conflicting_specifiers_p = true;
30376}
30377
30378/* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
30379   is true, the type is a class or enum definition.  */
30380
30381static void
30382cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
30383			      tree type_spec,
30384			      cp_token *token,
30385			      bool type_definition_p)
30386{
30387  decl_specs->any_specifiers_p = true;
30388
30389  /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
30390     wchar_t (with, for example, in "typedef int wchar_t;") we remember that
30391     this is what happened.  In system headers, we ignore these
30392     declarations so that G++ can work with system headers that are not
30393     C++-safe.  */
30394  if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
30395      && !type_definition_p
30396      && (type_spec == boolean_type_node
30397	  || type_spec == char8_type_node
30398	  || type_spec == char16_type_node
30399	  || type_spec == char32_type_node
30400	  || type_spec == wchar_type_node)
30401      && (decl_specs->type
30402	  || decl_spec_seq_has_spec_p (decl_specs, ds_long)
30403	  || decl_spec_seq_has_spec_p (decl_specs, ds_short)
30404	  || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
30405	  || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
30406    {
30407      decl_specs->redefined_builtin_type = type_spec;
30408      set_and_check_decl_spec_loc (decl_specs,
30409				   ds_redefined_builtin_type_spec,
30410				   token);
30411      if (!decl_specs->type)
30412	{
30413	  decl_specs->type = type_spec;
30414	  decl_specs->type_definition_p = false;
30415	  set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
30416	}
30417    }
30418  else if (decl_specs->type)
30419    decl_specs->multiple_types_p = true;
30420  else
30421    {
30422      decl_specs->type = type_spec;
30423      decl_specs->type_definition_p = type_definition_p;
30424      decl_specs->redefined_builtin_type = NULL_TREE;
30425      set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
30426    }
30427}
30428
30429/* True iff TOKEN is the GNU keyword __thread.  */
30430
30431static bool
30432token_is__thread (cp_token *token)
30433{
30434  gcc_assert (token->keyword == RID_THREAD);
30435  return id_equal (token->u.value, "__thread");
30436}
30437
30438/* Set the location for a declarator specifier and check if it is
30439   duplicated.
30440
30441   DECL_SPECS is the sequence of declarator specifiers onto which to
30442   set the location.
30443
30444   DS is the single declarator specifier to set which location  is to
30445   be set onto the existing sequence of declarators.
30446
30447   LOCATION is the location for the declarator specifier to
30448   consider.  */
30449
30450static void
30451set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
30452			     cp_decl_spec ds, cp_token *token)
30453{
30454  gcc_assert (ds < ds_last);
30455
30456  if (decl_specs == NULL)
30457    return;
30458
30459  location_t location = token->location;
30460
30461  if (decl_specs->locations[ds] == 0)
30462    {
30463      decl_specs->locations[ds] = location;
30464      if (ds == ds_thread)
30465	decl_specs->gnu_thread_keyword_p = token_is__thread (token);
30466    }
30467  else
30468    {
30469      if (ds == ds_long)
30470	{
30471	  if (decl_specs->locations[ds_long_long] != 0)
30472	    error_at (location,
30473		      "%<long long long%> is too long for GCC");
30474	  else
30475	    {
30476	      decl_specs->locations[ds_long_long] = location;
30477	      pedwarn_cxx98 (location,
30478			     OPT_Wlong_long,
30479			     "ISO C++ 1998 does not support %<long long%>");
30480	    }
30481	}
30482      else if (ds == ds_thread)
30483	{
30484	  bool gnu = token_is__thread (token);
30485	  gcc_rich_location richloc (location);
30486	  if (gnu != decl_specs->gnu_thread_keyword_p)
30487	    {
30488	      richloc.add_range (decl_specs->locations[ds_thread]);
30489	      error_at (&richloc,
30490			"both %<__thread%> and %<thread_local%> specified");
30491	    }
30492	  else
30493	    {
30494	      richloc.add_fixit_remove ();
30495	      error_at (&richloc, "duplicate %qD", token->u.value);
30496	    }
30497	}
30498      else
30499	{
30500	  static const char *const decl_spec_names[] = {
30501	    "signed",
30502	    "unsigned",
30503	    "short",
30504	    "long",
30505	    "const",
30506	    "volatile",
30507	    "restrict",
30508	    "inline",
30509	    "virtual",
30510	    "explicit",
30511	    "friend",
30512	    "typedef",
30513	    "using",
30514	    "constexpr",
30515	    "__complex",
30516	    "constinit",
30517	    "consteval"
30518	  };
30519	  gcc_rich_location richloc (location);
30520	  richloc.add_fixit_remove ();
30521	  error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
30522	}
30523    }
30524}
30525
30526/* Return true iff the declarator specifier DS is present in the
30527   sequence of declarator specifiers DECL_SPECS.  */
30528
30529bool
30530decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
30531			  cp_decl_spec ds)
30532{
30533  gcc_assert (ds < ds_last);
30534
30535  if (decl_specs == NULL)
30536    return false;
30537
30538  return decl_specs->locations[ds] != 0;
30539}
30540
30541/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
30542   Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
30543
30544static bool
30545cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
30546{
30547  return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
30548}
30549
30550/* Issue an error message indicating that TOKEN_DESC was expected.
30551   If KEYWORD is true, it indicated this function is called by
30552   cp_parser_require_keword and the required token can only be
30553   a indicated keyword.
30554
30555   If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30556   within any error as the location of an "opening" token matching
30557   the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30558   RT_CLOSE_PAREN).  */
30559
30560static void
30561cp_parser_required_error (cp_parser *parser,
30562			  required_token token_desc,
30563			  bool keyword,
30564			  location_t matching_location)
30565{
30566  if (cp_parser_simulate_error (parser))
30567    return;
30568
30569  const char *gmsgid = NULL;
30570  switch (token_desc)
30571    {
30572      case RT_NEW:
30573	gmsgid = G_("expected %<new%>");
30574	break;
30575      case RT_DELETE:
30576	gmsgid = G_("expected %<delete%>");
30577	break;
30578      case RT_RETURN:
30579	gmsgid = G_("expected %<return%>");
30580	break;
30581      case RT_WHILE:
30582	gmsgid = G_("expected %<while%>");
30583	break;
30584      case RT_EXTERN:
30585	gmsgid = G_("expected %<extern%>");
30586	break;
30587      case RT_STATIC_ASSERT:
30588	gmsgid = G_("expected %<static_assert%>");
30589	break;
30590      case RT_DECLTYPE:
30591	gmsgid = G_("expected %<decltype%>");
30592	break;
30593      case RT_OPERATOR:
30594	gmsgid = G_("expected %<operator%>");
30595	break;
30596      case RT_CLASS:
30597	gmsgid = G_("expected %<class%>");
30598	break;
30599      case RT_TEMPLATE:
30600	gmsgid = G_("expected %<template%>");
30601	break;
30602      case RT_NAMESPACE:
30603	gmsgid = G_("expected %<namespace%>");
30604	break;
30605      case RT_USING:
30606	gmsgid = G_("expected %<using%>");
30607	break;
30608      case RT_ASM:
30609	gmsgid = G_("expected %<asm%>");
30610	break;
30611      case RT_TRY:
30612	gmsgid = G_("expected %<try%>");
30613	break;
30614      case RT_CATCH:
30615	gmsgid = G_("expected %<catch%>");
30616	break;
30617      case RT_THROW:
30618	gmsgid = G_("expected %<throw%>");
30619	break;
30620      case RT_AUTO:
30621        gmsgid = G_("expected %<auto%>");
30622        break;
30623      case RT_LABEL:
30624	gmsgid = G_("expected %<__label__%>");
30625	break;
30626      case RT_AT_TRY:
30627	gmsgid = G_("expected %<@try%>");
30628	break;
30629      case RT_AT_SYNCHRONIZED:
30630	gmsgid = G_("expected %<@synchronized%>");
30631	break;
30632      case RT_AT_THROW:
30633	gmsgid = G_("expected %<@throw%>");
30634	break;
30635      case RT_TRANSACTION_ATOMIC:
30636	gmsgid = G_("expected %<__transaction_atomic%>");
30637	break;
30638      case RT_TRANSACTION_RELAXED:
30639	gmsgid = G_("expected %<__transaction_relaxed%>");
30640	break;
30641      case RT_CO_YIELD:
30642	gmsgid = G_("expected %<co_yield%>");
30643	break;
30644      default:
30645	break;
30646    }
30647
30648  if (!gmsgid && !keyword)
30649    {
30650      switch (token_desc)
30651        {
30652	  case RT_SEMICOLON:
30653	    gmsgid = G_("expected %<;%>");
30654	    break;
30655	  case RT_OPEN_PAREN:
30656	    gmsgid = G_("expected %<(%>");
30657	    break;
30658	  case RT_CLOSE_BRACE:
30659	    gmsgid = G_("expected %<}%>");
30660	    break;
30661	  case RT_OPEN_BRACE:
30662	    gmsgid = G_("expected %<{%>");
30663	    break;
30664	  case RT_CLOSE_SQUARE:
30665	    gmsgid = G_("expected %<]%>");
30666	    break;
30667	  case RT_OPEN_SQUARE:
30668	    gmsgid = G_("expected %<[%>");
30669	    break;
30670	  case RT_COMMA:
30671	    gmsgid = G_("expected %<,%>");
30672	    break;
30673	  case RT_SCOPE:
30674	    gmsgid = G_("expected %<::%>");
30675	    break;
30676	  case RT_LESS:
30677	    gmsgid = G_("expected %<<%>");
30678	    break;
30679	  case RT_GREATER:
30680	    gmsgid = G_("expected %<>%>");
30681	    break;
30682	  case RT_EQ:
30683	    gmsgid = G_("expected %<=%>");
30684	    break;
30685	  case RT_ELLIPSIS:
30686	    gmsgid = G_("expected %<...%>");
30687	    break;
30688	  case RT_MULT:
30689	    gmsgid = G_("expected %<*%>");
30690	    break;
30691	  case RT_COMPL:
30692	    gmsgid = G_("expected %<~%>");
30693	    break;
30694	  case RT_COLON:
30695	    gmsgid = G_("expected %<:%>");
30696	    break;
30697	  case RT_COLON_SCOPE:
30698	    gmsgid = G_("expected %<:%> or %<::%>");
30699	    break;
30700	  case RT_CLOSE_PAREN:
30701	    gmsgid = G_("expected %<)%>");
30702	    break;
30703	  case RT_COMMA_CLOSE_PAREN:
30704	    gmsgid = G_("expected %<,%> or %<)%>");
30705	    break;
30706	  case RT_PRAGMA_EOL:
30707	    gmsgid = G_("expected end of line");
30708	    break;
30709	  case RT_NAME:
30710	    gmsgid = G_("expected identifier");
30711	    break;
30712	  case RT_SELECT:
30713	    gmsgid = G_("expected selection-statement");
30714	    break;
30715	  case RT_ITERATION:
30716	    gmsgid = G_("expected iteration-statement");
30717	    break;
30718	  case RT_JUMP:
30719	    gmsgid = G_("expected jump-statement");
30720	    break;
30721	  case RT_CLASS_KEY:
30722	    gmsgid = G_("expected class-key");
30723	    break;
30724	  case RT_CLASS_TYPENAME_TEMPLATE:
30725	    gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
30726	    break;
30727	  default:
30728	    gcc_unreachable ();
30729	}
30730    }
30731
30732  if (gmsgid)
30733    cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
30734}
30735
30736
30737/* If the next token is of the indicated TYPE, consume it.  Otherwise,
30738   issue an error message indicating that TOKEN_DESC was expected.
30739
30740   Returns the token consumed, if the token had the appropriate type.
30741   Otherwise, returns NULL.
30742
30743   If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30744   within any error as the location of an "opening" token matching
30745   the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30746   RT_CLOSE_PAREN).  */
30747
30748static cp_token *
30749cp_parser_require (cp_parser* parser,
30750		   enum cpp_ttype type,
30751		   required_token token_desc,
30752		   location_t matching_location)
30753{
30754  if (cp_lexer_next_token_is (parser->lexer, type))
30755    return cp_lexer_consume_token (parser->lexer);
30756  else
30757    {
30758      /* Output the MESSAGE -- unless we're parsing tentatively.  */
30759      if (!cp_parser_simulate_error (parser))
30760	cp_parser_required_error (parser, token_desc, /*keyword=*/false,
30761				  matching_location);
30762      return NULL;
30763    }
30764}
30765
30766/* An error message is produced if the next token is not '>'.
30767   All further tokens are skipped until the desired token is
30768   found or '{', '}', ';' or an unbalanced ')' or ']'.  */
30769
30770static void
30771cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
30772{
30773  /* Current level of '< ... >'.  */
30774  unsigned level = 0;
30775  /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
30776  unsigned nesting_depth = 0;
30777
30778  /* Are we ready, yet?  If not, issue error message.  */
30779  if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
30780    return;
30781
30782  /* Skip tokens until the desired token is found.  */
30783  while (true)
30784    {
30785      /* Peek at the next token.  */
30786      switch (cp_lexer_peek_token (parser->lexer)->type)
30787	{
30788	case CPP_LESS:
30789	  if (!nesting_depth)
30790	    ++level;
30791	  break;
30792
30793        case CPP_RSHIFT:
30794          if (cxx_dialect == cxx98)
30795            /* C++0x views the `>>' operator as two `>' tokens, but
30796               C++98 does not. */
30797            break;
30798          else if (!nesting_depth && level-- == 0)
30799	    {
30800              /* We've hit a `>>' where the first `>' closes the
30801                 template argument list, and the second `>' is
30802                 spurious.  Just consume the `>>' and stop; we've
30803                 already produced at least one error.  */
30804	      cp_lexer_consume_token (parser->lexer);
30805	      return;
30806	    }
30807          /* Fall through for C++0x, so we handle the second `>' in
30808             the `>>'.  */
30809	  gcc_fallthrough ();
30810
30811	case CPP_GREATER:
30812	  if (!nesting_depth && level-- == 0)
30813	    {
30814	      /* We've reached the token we want, consume it and stop.  */
30815	      cp_lexer_consume_token (parser->lexer);
30816	      return;
30817	    }
30818	  break;
30819
30820	case CPP_OPEN_PAREN:
30821	case CPP_OPEN_SQUARE:
30822	  ++nesting_depth;
30823	  break;
30824
30825	case CPP_CLOSE_PAREN:
30826	case CPP_CLOSE_SQUARE:
30827	  if (nesting_depth-- == 0)
30828	    return;
30829	  break;
30830
30831	case CPP_EOF:
30832	case CPP_PRAGMA_EOL:
30833	case CPP_SEMICOLON:
30834	case CPP_OPEN_BRACE:
30835	case CPP_CLOSE_BRACE:
30836	  /* The '>' was probably forgotten, don't look further.  */
30837	  return;
30838
30839	default:
30840	  break;
30841	}
30842
30843      /* Consume this token.  */
30844      cp_lexer_consume_token (parser->lexer);
30845    }
30846}
30847
30848/* If the next token is the indicated keyword, consume it.  Otherwise,
30849   issue an error message indicating that TOKEN_DESC was expected.
30850
30851   Returns the token consumed, if the token had the appropriate type.
30852   Otherwise, returns NULL.  */
30853
30854static cp_token *
30855cp_parser_require_keyword (cp_parser* parser,
30856			   enum rid keyword,
30857			   required_token token_desc)
30858{
30859  cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
30860
30861  if (token && token->keyword != keyword)
30862    {
30863      cp_parser_required_error (parser, token_desc, /*keyword=*/true,
30864                                UNKNOWN_LOCATION);
30865      return NULL;
30866    }
30867
30868  return token;
30869}
30870
30871/* Returns TRUE iff TOKEN is a token that can begin the body of a
30872   function-definition.  */
30873
30874static bool
30875cp_parser_token_starts_function_definition_p (cp_token* token)
30876{
30877  return (/* An ordinary function-body begins with an `{'.  */
30878	  token->type == CPP_OPEN_BRACE
30879	  /* A ctor-initializer begins with a `:'.  */
30880	  || token->type == CPP_COLON
30881	  /* A function-try-block begins with `try'.  */
30882	  || token->keyword == RID_TRY
30883	  /* A function-transaction-block begins with `__transaction_atomic'
30884	     or `__transaction_relaxed'.  */
30885	  || token->keyword == RID_TRANSACTION_ATOMIC
30886	  || token->keyword == RID_TRANSACTION_RELAXED
30887	  /* The named return value extension begins with `return'.  */
30888	  || token->keyword == RID_RETURN);
30889}
30890
30891/* Returns TRUE iff the next token is the ":" or "{" beginning a class
30892   definition.  */
30893
30894static bool
30895cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
30896{
30897  cp_token *token;
30898
30899  token = cp_lexer_peek_token (parser->lexer);
30900  return (token->type == CPP_OPEN_BRACE
30901	  || (token->type == CPP_COLON
30902	      && !parser->colon_doesnt_start_class_def_p));
30903}
30904
30905/* Returns TRUE iff the next token is the "," or ">" (or `>>', in
30906   C++0x) ending a template-argument.  */
30907
30908static bool
30909cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
30910{
30911  cp_token *token;
30912
30913  token = cp_lexer_peek_token (parser->lexer);
30914  return (token->type == CPP_COMMA
30915          || token->type == CPP_GREATER
30916          || token->type == CPP_ELLIPSIS
30917	  || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
30918}
30919
30920/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
30921   (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
30922
30923static bool
30924cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
30925						     size_t n)
30926{
30927  cp_token *token;
30928
30929  token = cp_lexer_peek_nth_token (parser->lexer, n);
30930  if (token->type == CPP_LESS)
30931    return true;
30932  /* Check for the sequence `<::' in the original code. It would be lexed as
30933     `[:', where `[' is a digraph, and there is no whitespace before
30934     `:'.  */
30935  if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
30936    {
30937      cp_token *token2;
30938      token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
30939      if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
30940	return true;
30941    }
30942  return false;
30943}
30944
30945/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
30946   or none_type otherwise.  */
30947
30948static enum tag_types
30949cp_parser_token_is_class_key (cp_token* token)
30950{
30951  switch (token->keyword)
30952    {
30953    case RID_CLASS:
30954      return class_type;
30955    case RID_STRUCT:
30956      return record_type;
30957    case RID_UNION:
30958      return union_type;
30959
30960    default:
30961      return none_type;
30962    }
30963}
30964
30965/* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
30966   or none_type otherwise or if the token is null.  */
30967
30968static enum tag_types
30969cp_parser_token_is_type_parameter_key (cp_token* token)
30970{
30971  if (!token)
30972    return none_type;
30973
30974  switch (token->keyword)
30975    {
30976    case RID_CLASS:
30977      return class_type;
30978    case RID_TYPENAME:
30979      return typename_type;
30980
30981    default:
30982      return none_type;
30983    }
30984}
30985
30986/* Diagnose redundant enum-keys.  */
30987
30988static void
30989cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
30990			       tree type, rid scoped_key)
30991{
30992  if (!warn_redundant_tags)
30993    return;
30994
30995  tree type_decl = TYPE_MAIN_DECL (type);
30996  tree name = DECL_NAME (type_decl);
30997  /* Look up the NAME to see if it unambiguously refers to the TYPE.  */
30998  push_deferring_access_checks (dk_no_check);
30999  tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31000  pop_deferring_access_checks ();
31001
31002  /* The enum-key is redundant for uses of the TYPE that are not
31003     declarations and for which name lookup returns just the type
31004     itself.  */
31005  if (decl != type_decl)
31006    return;
31007
31008  if (scoped_key != RID_CLASS
31009      && scoped_key != RID_STRUCT
31010      && current_lang_name != lang_name_cplusplus
31011      && current_namespace == global_namespace)
31012    {
31013      /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31014	 enum tag in shared C/C++ code in files (such as headers) included
31015	 in the main source file.  */
31016      const line_map_ordinary *map = NULL;
31017      linemap_resolve_location (line_table, key_loc,
31018				LRK_MACRO_DEFINITION_LOCATION,
31019				&map);
31020      if (!MAIN_FILE_P (map))
31021	return;
31022    }
31023
31024  gcc_rich_location richloc (key_loc);
31025  richloc.add_fixit_remove (key_loc);
31026  warning_at (&richloc, OPT_Wredundant_tags,
31027	      "redundant enum-key %<enum%s%> in reference to %q#T",
31028	      (scoped_key == RID_CLASS ? " class"
31029	       : scoped_key == RID_STRUCT ? " struct" : ""), type);
31030}
31031
31032/* Describes the set of declarations of a struct, class, or class template
31033   or its specializations.  Used for -Wmismatched-tags.  */
31034
31035class class_decl_loc_t
31036{
31037 public:
31038
31039  class_decl_loc_t ()
31040    : locvec (), idxdef (), def_class_key ()
31041  {
31042    locvec.create (4);
31043  }
31044
31045  /* Constructs an object for a single declaration of a class with
31046     CLASS_KEY at the current location in the current function (or
31047     at another scope).  KEY_REDUNDANT is true if the class-key may
31048     be omitted in the current context without an ambiguity with
31049     another symbol with the same name.
31050     DEF_P is true for a class declaration that is a definition.
31051     CURLOC is the associated location.  */
31052  class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
31053		    location_t curloc = input_location)
31054    : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
31055  {
31056    locvec.create (4);
31057    class_key_loc_t ckl (current_function_decl, curloc, class_key,
31058			 key_redundant);
31059    locvec.quick_push (ckl);
31060  }
31061
31062  /* Copy, assign, and destroy the object.  Necessary because LOCVEC
31063     isn't safely copyable and assignable and doesn't release storage
31064     on its own.  */
31065  class_decl_loc_t (const class_decl_loc_t &rhs)
31066    : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
31067      def_class_key (rhs.def_class_key)
31068  { }
31069
31070  class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
31071  {
31072    if (this == &rhs)
31073      return *this;
31074    locvec.release ();
31075    locvec = rhs.locvec.copy ();
31076    idxdef = rhs.idxdef;
31077    def_class_key = rhs.def_class_key;
31078    return *this;
31079  }
31080
31081  ~class_decl_loc_t ()
31082  {
31083    locvec.release ();
31084  }
31085
31086  /* Issues -Wmismatched-tags for a single class.  */
31087  void diag_mismatched_tags (tree);
31088
31089  /* Issues -Wmismatched-tags for all classes.  */
31090  static void diag_mismatched_tags ();
31091
31092  /* Adds TYPE_DECL to the collection of class decls and diagnoses
31093     redundant tags (if -Wredundant-tags is enabled).  */
31094  static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
31095
31096  /* Either adds this decl to the collection of class decls
31097     or diagnoses it, whichever is appropriate.  */
31098  void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
31099
31100private:
31101
31102  tree function (unsigned i) const
31103  {
31104    return locvec[i].func;
31105  }
31106
31107  location_t location (unsigned i) const
31108  {
31109    return locvec[i].loc;
31110  }
31111
31112  bool key_redundant (unsigned i) const
31113  {
31114    return locvec[i].key_redundant;
31115  }
31116
31117  tag_types class_key (unsigned i) const
31118  {
31119    return locvec[i].class_key;
31120  }
31121
31122  /* True if a definition for the class has been seen.  */
31123  bool def_p () const
31124  {
31125    return idxdef < locvec.length ();
31126  }
31127
31128  /* The location of a single mention of a class type with the given
31129     class-key.  */
31130  struct class_key_loc_t
31131  {
31132    class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
31133      : func (func), loc (loc), class_key (key), key_redundant (redundant)
31134    { }
31135
31136    /* The function the type is mentioned in.  */
31137    tree func;
31138    /* The exact location.  */
31139    location_t loc;
31140    /* The class-key used in the mention of the type.  */
31141    tag_types class_key;
31142    /* True when the class-key could be omitted at this location
31143       without an ambiguity with another symbol of the same name.  */
31144    bool key_redundant;
31145  };
31146  /* Avoid using auto_vec here since it's not safe to copy due to pr90904.  */
31147  vec <class_key_loc_t> locvec;
31148  /* LOCVEC index of the definition or UINT_MAX if none exists.  */
31149  unsigned idxdef;
31150  /* The class-key the class was last declared with or none_type when
31151     it has been declared with a mismatched key.  */
31152  tag_types def_class_key;
31153
31154  /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
31155     description above.  */
31156  typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
31157  static class_to_loc_map_t class2loc;
31158};
31159
31160class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
31161
31162/* Issue an error message if the CLASS_KEY does not match the TYPE.
31163   DEF_P is expected to be set for a definition of class TYPE.  DECL_P
31164   is set for a declaration of class TYPE and clear for a reference to
31165   it that is not a declaration of it.  */
31166
31167static void
31168cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
31169			   tag_types class_key, tree type, bool def_p,
31170			   bool decl_p)
31171{
31172  if (type == error_mark_node)
31173    return;
31174
31175  bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
31176  if (seen_as_union != (class_key == union_type))
31177    {
31178      if (permerror (input_location, "%qs tag used in naming %q#T",
31179		     class_key == union_type ? "union"
31180		     : class_key == record_type ? "struct" : "class",
31181		     type))
31182	inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
31183		"%q#T was previously declared here", type);
31184      return;
31185    }
31186
31187  if (!warn_mismatched_tags && !warn_redundant_tags)
31188    return;
31189
31190  /* Only consider the true class-keys below and ignore typename_type,
31191     etc. that are not C++ class-keys.  */
31192  if (class_key != class_type
31193      && class_key != record_type
31194      && class_key != union_type)
31195    return;
31196
31197  class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
31198}
31199
31200/* Returns the template or specialization of one to which the RECORD_TYPE
31201   TYPE corresponds.  */
31202
31203static tree
31204specialization_of (tree type)
31205{
31206  tree ret = type;
31207
31208  /* Determine the template or its partial specialization to which TYPE
31209     corresponds.  */
31210  if (tree spec = most_specialized_partial_spec (type, tf_none))
31211    if (spec != error_mark_node)
31212      ret = TREE_TYPE (TREE_VALUE (spec));
31213
31214  if (ret == type)
31215    ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
31216
31217  return TYPE_MAIN_DECL (ret);
31218}
31219
31220
31221/* Adds the class TYPE to the collection of class decls and diagnoses
31222   redundant tags (if -Wredundant-tags is enabled).
31223   DEF_P is expected to be set for a definition of class TYPE.  DECL_P
31224   is set for a (likely, based on syntactic context) declaration of class
31225   TYPE and clear for a reference to it that is not a declaration of it.  */
31226
31227void
31228class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
31229		       tag_types class_key, tree type, bool def_p, bool decl_p)
31230{
31231  tree type_decl = TYPE_MAIN_DECL (type);
31232  tree name = DECL_NAME (type_decl);
31233  /* Look up the NAME to see if it unambiguously refers to the TYPE
31234     and set KEY_REDUNDANT if so.  */
31235  push_deferring_access_checks (dk_no_check);
31236  tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31237  pop_deferring_access_checks ();
31238
31239  /* The class-key is redundant for uses of the CLASS_TYPE that are
31240     neither definitions of it nor declarations, and for which name
31241     lookup returns just the type itself.  */
31242  bool key_redundant = (!def_p && !decl_p
31243			&& (decl == type_decl
31244			    || TREE_CODE (decl) == TEMPLATE_DECL
31245			    || TYPE_BEING_DEFINED (type)));
31246
31247  if (key_redundant
31248      && class_key != class_type
31249      && current_lang_name != lang_name_cplusplus
31250      && current_namespace == global_namespace)
31251    {
31252      /* Avoid issuing the diagnostic for apparently redundant struct
31253	 and union class-keys in shared C/C++ code in files (such as
31254	 headers) included in the main source file.  */
31255      const line_map_ordinary *map = NULL;
31256      linemap_resolve_location (line_table, key_loc,
31257				LRK_MACRO_DEFINITION_LOCATION,
31258				&map);
31259      if (!MAIN_FILE_P (map))
31260	key_redundant = false;
31261    }
31262
31263  /* Set if a declaration of TYPE has previously been seen or if it must
31264     exist in a precompiled header.  */
31265  bool exist;
31266  class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
31267  if (!exist)
31268    {
31269      tree type = TREE_TYPE (type_decl);
31270      if (def_p || !COMPLETE_TYPE_P (type))
31271	{
31272	  /* TYPE_DECL is the first declaration or definition of the type
31273	     (outside precompiled headers -- see below).  Just create
31274	     a new entry for it and return unless it's a declaration
31275	     involving a template that may need to be diagnosed by
31276	     -Wredundant-tags.  */
31277	  *rdl = class_decl_loc_t (class_key, false, def_p);
31278	  if (TREE_CODE (decl) != TEMPLATE_DECL)
31279	    return;
31280	}
31281      else
31282	{
31283	  /* TYPE was previously defined in some unknown precompiled hdeader.
31284	     Simply add a record of its definition at an unknown location and
31285	     proceed below to add a reference to it at the current location.
31286	     (Declarations in precompiled headers that are not definitions
31287	     are ignored.)  */
31288	  tag_types def_key
31289	    = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
31290	  location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
31291	  *rdl = class_decl_loc_t (def_key, false, true, def_loc);
31292	  exist = true;
31293	}
31294    }
31295
31296  /* A prior declaration of TYPE_DECL has been seen.  */
31297
31298  if (key_redundant)
31299    {
31300      gcc_rich_location richloc (key_loc);
31301      richloc.add_fixit_remove (key_loc);
31302      warning_at (&richloc, OPT_Wredundant_tags,
31303		  "redundant class-key %qs in reference to %q#T",
31304		  class_key == union_type ? "union"
31305		  : class_key == record_type ? "struct" : "class",
31306		  type);
31307    }
31308
31309  if (!exist)
31310    /* Do nothing if this is the first declaration of the type.  */
31311    return;
31312
31313  if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
31314    /* Do nothing if the class-key in this declaration matches
31315       the definition.  */
31316    return;
31317
31318  rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
31319				   def_p);
31320}
31321
31322/* Either adds this DECL corresponding to the TYPE_DECL to the collection
31323   of class decls or diagnoses it, whichever is appropriate.  */
31324
31325void
31326class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
31327					      tag_types class_key,
31328					      bool redundant,
31329					      bool def_p)
31330{
31331  /* Reset the CLASS_KEY associated with this type on mismatch.
31332     This is an optimization that lets the diagnostic code skip
31333     over classes that use the same class-key in all declarations.  */
31334  if (def_class_key != class_key)
31335    def_class_key = none_type;
31336
31337  /* Set IDXDEF to the index of the vector corresponding to
31338     the definition.  */
31339  if (def_p)
31340    idxdef = locvec.length ();
31341
31342  /* Append a record of this declaration to the vector.  */
31343  class_key_loc_t ckl (current_function_decl, input_location, class_key,
31344		       redundant);
31345  locvec.safe_push (ckl);
31346
31347  if (idxdef == UINT_MAX)
31348    return;
31349
31350  /* As a space optimization diagnose declarations of a class
31351     whose definition has been seen and purge the LOCVEC of
31352     all entries except the definition.  */
31353  diag_mismatched_tags (type_decl);
31354  if (idxdef)
31355    {
31356      class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
31357      locvec.release ();
31358      locvec.reserve (2);
31359      locvec.safe_push (ent);
31360      idxdef = 0;
31361    }
31362  else
31363    /* Pop the entry pushed above for this declaration.  */
31364    locvec.pop ();
31365}
31366
31367/* Issues -Wmismatched-tags for a single class.  */
31368
31369void
31370class_decl_loc_t::diag_mismatched_tags (tree type_decl)
31371{
31372  if (!warn_mismatched_tags)
31373    return;
31374
31375  /* Number of uses of the class.  */
31376  const unsigned ndecls = locvec.length ();
31377
31378  /* The class (or template) declaration guiding the decisions about
31379     the diagnostic.  For ordinary classes it's the same as THIS.  For
31380     uses of instantiations of templates other than their declarations
31381     it points to the record for the declaration of the corresponding
31382     primary template or partial specialization.  */
31383  class_decl_loc_t *cdlguide = this;
31384
31385  tree type = TREE_TYPE (type_decl);
31386  if (CLASSTYPE_IMPLICIT_INSTANTIATION (type))
31387    {
31388      /* For implicit instantiations of a primary template look up
31389	 the primary or partial specialization and use it as
31390	 the expected class-key rather than using the class-key of
31391	 the first reference to the instantiation.  The primary must
31392	 be (and inevitably is) at index zero.  */
31393      tree spec = specialization_of (type);
31394      cdlguide = class2loc.get (spec);
31395      gcc_assert (cdlguide != NULL);
31396    }
31397  else
31398    {
31399      /* Skip declarations that consistently use the same class-key.  */
31400      if (def_class_key != none_type)
31401	return;
31402    }
31403
31404  /* Set if a definition for the class has been seen.  */
31405  const bool def_p = cdlguide->def_p ();
31406
31407  /* The index of the declaration whose class-key this declaration
31408     is expected to match.  It's either the class-key of the class
31409     definition if one exists or the first declaration otherwise.  */
31410  const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
31411
31412  /* The class-key the class is expected to be declared with: it's
31413     either the key used in its definition or the first declaration
31414     if no definition has been provided.
31415     For implicit instantiations of a primary template it's
31416     the class-key used to declare the primary with.  The primary
31417     must be at index zero.  */
31418  const tag_types xpect_key = cdlguide->class_key (idxguide);
31419
31420  unsigned idx = 0;
31421  /* Advance IDX to the first declaration that either is not
31422     a definition or that doesn't match the first declaration
31423     if no definition is provided.  */
31424  while (class_key (idx) == xpect_key)
31425    if (++idx == ndecls)
31426      return;
31427
31428  /* Save the current function before changing it below.  */
31429  tree save_func = current_function_decl;
31430  /* Set the function declaration to print in diagnostic context.  */
31431  current_function_decl = function (idx);
31432
31433  const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
31434  const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
31435
31436  location_t loc = location (idx);
31437  bool key_redundant_p = key_redundant (idx);
31438  auto_diagnostic_group d;
31439  /* Issue a warning for the first mismatched declaration.
31440     Avoid using "%#qT" since the class-key for the same type will
31441     be the same regardless of which one was used in the declaraion.  */
31442  if (warning_at (loc, OPT_Wmismatched_tags,
31443		  "%qT declared with a mismatched class-key %qs",
31444		  type_decl, xmatchkstr))
31445    {
31446      /* Suggest how to avoid the warning for each instance since
31447	 the guidance may be different depending on context.  */
31448      inform (loc,
31449	      (key_redundant_p
31450	       ? G_("remove the class-key or replace it with %qs")
31451	       : G_("replace the class-key with %qs")),
31452	      xpectkstr);
31453
31454      /* Also point to the first declaration or definition that guided
31455	 the decision to issue the warning above.  */
31456      inform (cdlguide->location (idxguide),
31457	      (def_p
31458	       ? G_("%qT defined as %qs here")
31459	       : G_("%qT first declared as %qs here")),
31460	      type_decl, xpectkstr);
31461    }
31462
31463  /* Issue warnings for the remaining inconsistent declarations.  */
31464  for (unsigned i = idx + 1; i != ndecls; ++i)
31465    {
31466      tag_types clskey = class_key (i);
31467      /* Skip over the declarations that match either the definition
31468	 if one was provided or the first declaration.  */
31469      if (clskey == xpect_key)
31470	continue;
31471
31472      loc = location (i);
31473      key_redundant_p = key_redundant (i);
31474      /* Set the function declaration to print in diagnostic context.  */
31475      current_function_decl = function (i);
31476      if (warning_at (loc, OPT_Wmismatched_tags,
31477		      "%qT declared with a mismatched class-key %qs",
31478		      type_decl, xmatchkstr))
31479	/* Suggest how to avoid the warning for each instance since
31480	   the guidance may be different depending on context.  */
31481	inform (loc,
31482		(key_redundant_p
31483		 ? G_("remove the class-key or replace it with %qs")
31484		 : G_("replace the class-key with %qs")),
31485		xpectkstr);
31486    }
31487
31488  /* Restore the current function in case it was replaced above.  */
31489  current_function_decl = save_func;
31490}
31491
31492/* Issues -Wmismatched-tags for all classes.  Called at the end
31493   of processing a translation unit, after declarations of all class
31494   types and their uses have been recorded.  */
31495
31496void
31497class_decl_loc_t::diag_mismatched_tags ()
31498{
31499  /* CLASS2LOC should be empty if both -Wmismatched-tags and
31500     -Wredundant-tags are disabled.  */
31501  gcc_assert (warn_mismatched_tags
31502	      || warn_redundant_tags
31503	      || class2loc.is_empty ());
31504
31505  /* Save the current function before changing on return.  It should
31506     be null at this point.  */
31507  temp_override<tree> cleanup (current_function_decl);
31508
31509  if (warn_mismatched_tags)
31510    {
31511      /* Iterate over the collected class/struct/template declarations.  */
31512      typedef class_to_loc_map_t::iterator iter_t;
31513      for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
31514	{
31515	  tree type_decl = (*it).first;
31516	  class_decl_loc_t &recloc = (*it).second;
31517	  recloc.diag_mismatched_tags (type_decl);
31518	}
31519    }
31520
31521  class2loc.empty ();
31522}
31523
31524/* Issue an error message if DECL is redeclared with different
31525   access than its original declaration [class.access.spec/3].
31526   This applies to nested classes, nested class templates and
31527   enumerations [class.mem/1].  */
31528
31529static void
31530cp_parser_check_access_in_redeclaration (tree decl, location_t location)
31531{
31532  if (!decl
31533      || (!CLASS_TYPE_P (TREE_TYPE (decl))
31534	  && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
31535    return;
31536
31537  if ((TREE_PRIVATE (decl)
31538       != (current_access_specifier == access_private_node))
31539      || (TREE_PROTECTED (decl)
31540	  != (current_access_specifier == access_protected_node)))
31541    error_at (location, "%qD redeclared with different access", decl);
31542}
31543
31544/* Look for the `template' keyword, as a syntactic disambiguator.
31545   Return TRUE iff it is present, in which case it will be
31546   consumed.  */
31547
31548static bool
31549cp_parser_optional_template_keyword (cp_parser *parser)
31550{
31551  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31552    {
31553      /* In C++98 the `template' keyword can only be used within templates;
31554	 outside templates the parser can always figure out what is a
31555	 template and what is not.  In C++11,  per the resolution of DR 468,
31556	 `template' is allowed in cases where it is not strictly necessary.  */
31557      if (!processing_template_decl
31558	  && pedantic && cxx_dialect == cxx98)
31559	{
31560	  cp_token *token = cp_lexer_peek_token (parser->lexer);
31561	  pedwarn (token->location, OPT_Wpedantic,
31562		   "in C++98 %<template%> (as a disambiguator) is only "
31563		   "allowed within templates");
31564	  /* If this part of the token stream is rescanned, the same
31565	     error message would be generated.  So, we purge the token
31566	     from the stream.  */
31567	  cp_lexer_purge_token (parser->lexer);
31568	  return false;
31569	}
31570      else
31571	{
31572	  /* Consume the `template' keyword.  */
31573	  cp_lexer_consume_token (parser->lexer);
31574	  return true;
31575	}
31576    }
31577  return false;
31578}
31579
31580/* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
31581   set PARSER->SCOPE, and perform other related actions.  */
31582
31583static void
31584cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
31585{
31586  struct tree_check *check_value;
31587
31588  /* Get the stored value.  */
31589  check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
31590  /* Set the scope from the stored value.  */
31591  parser->scope = saved_checks_value (check_value);
31592  parser->qualifying_scope = check_value->qualifying_scope;
31593  parser->object_scope = NULL_TREE;
31594}
31595
31596/* Consume tokens up through a non-nested END token.  Returns TRUE if we
31597   encounter the end of a block before what we were looking for.  */
31598
31599static bool
31600cp_parser_cache_group (cp_parser *parser,
31601		       enum cpp_ttype end,
31602		       unsigned depth)
31603{
31604  while (true)
31605    {
31606      cp_token *token = cp_lexer_peek_token (parser->lexer);
31607
31608      /* Abort a parenthesized expression if we encounter a semicolon.  */
31609      if ((end == CPP_CLOSE_PAREN || depth == 0)
31610	  && token->type == CPP_SEMICOLON)
31611	return true;
31612      /* If we've reached the end of the file, stop.  */
31613      if (token->type == CPP_EOF
31614	  || (end != CPP_PRAGMA_EOL
31615	      && token->type == CPP_PRAGMA_EOL))
31616	return true;
31617      if (token->type == CPP_CLOSE_BRACE && depth == 0)
31618	/* We've hit the end of an enclosing block, so there's been some
31619	   kind of syntax error.  */
31620	return true;
31621
31622      /* Consume the token.  */
31623      cp_lexer_consume_token (parser->lexer);
31624      /* See if it starts a new group.  */
31625      if (token->type == CPP_OPEN_BRACE)
31626	{
31627	  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
31628	  /* In theory this should probably check end == '}', but
31629	     cp_parser_save_member_function_body needs it to exit
31630	     after either '}' or ')' when called with ')'.  */
31631	  if (depth == 0)
31632	    return false;
31633	}
31634      else if (token->type == CPP_OPEN_PAREN)
31635	{
31636	  cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
31637	  if (depth == 0 && end == CPP_CLOSE_PAREN)
31638	    return false;
31639	}
31640      else if (token->type == CPP_PRAGMA)
31641	cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
31642      else if (token->type == end)
31643	return false;
31644    }
31645}
31646
31647/* Like above, for caching a default argument or NSDMI.  Both of these are
31648   terminated by a non-nested comma, but it can be unclear whether or not a
31649   comma is nested in a template argument list unless we do more parsing.
31650   In order to handle this ambiguity, when we encounter a ',' after a '<'
31651   we try to parse what follows as a parameter-declaration-list (in the
31652   case of a default argument) or a member-declarator (in the case of an
31653   NSDMI).  If that succeeds, then we stop caching.  */
31654
31655static tree
31656cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
31657{
31658  unsigned depth = 0;
31659  int maybe_template_id = 0;
31660  cp_token *first_token;
31661  cp_token *token;
31662  tree default_argument;
31663
31664  /* Add tokens until we have processed the entire default
31665     argument.  We add the range [first_token, token).  */
31666  first_token = cp_lexer_peek_token (parser->lexer);
31667  if (first_token->type == CPP_OPEN_BRACE)
31668    {
31669      /* For list-initialization, this is straightforward.  */
31670      cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
31671      token = cp_lexer_peek_token (parser->lexer);
31672    }
31673  else while (true)
31674    {
31675      bool done = false;
31676
31677      /* Peek at the next token.  */
31678      token = cp_lexer_peek_token (parser->lexer);
31679      /* What we do depends on what token we have.  */
31680      switch (token->type)
31681	{
31682	  /* In valid code, a default argument must be
31683	     immediately followed by a `,' `)', or `...'.  */
31684	case CPP_COMMA:
31685	  if (depth == 0 && maybe_template_id)
31686	    {
31687	      /* If we've seen a '<', we might be in a
31688		 template-argument-list.  Until Core issue 325 is
31689		 resolved, we don't know how this situation ought
31690		 to be handled, so try to DTRT.  We check whether
31691		 what comes after the comma is a valid parameter
31692		 declaration list.  If it is, then the comma ends
31693		 the default argument; otherwise the default
31694		 argument continues.  */
31695	      bool error = false;
31696	      cp_token *peek;
31697
31698	      /* Set ITALP so cp_parser_parameter_declaration_list
31699		 doesn't decide to commit to this parse.  */
31700	      bool saved_italp = parser->in_template_argument_list_p;
31701	      parser->in_template_argument_list_p = true;
31702
31703	      cp_parser_parse_tentatively (parser);
31704
31705	      if (nsdmi)
31706		{
31707		  /* Parse declarators until we reach a non-comma or
31708		     somthing that cannot be an initializer.
31709		     Just checking whether we're looking at a single
31710		     declarator is insufficient.  Consider:
31711		       int var = tuple<T,U>::x;
31712		     The template parameter 'U' looks exactly like a
31713		     declarator.  */
31714		  do
31715		    {
31716		      int ctor_dtor_or_conv_p;
31717		      cp_lexer_consume_token (parser->lexer);
31718		      cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31719					    CP_PARSER_FLAGS_NONE,
31720					    &ctor_dtor_or_conv_p,
31721					    /*parenthesized_p=*/NULL,
31722					    /*member_p=*/true,
31723					    /*friend_p=*/false,
31724					    /*static_p=*/false);
31725		      peek = cp_lexer_peek_token (parser->lexer);
31726		      if (cp_parser_error_occurred (parser))
31727			break;
31728		    }
31729		  while (peek->type == CPP_COMMA);
31730		  /* If we met an '=' or ';' then the original comma
31731		     was the end of the NSDMI.  Otherwise assume
31732		     we're still in the NSDMI.  */
31733		  error = (peek->type != CPP_EQ
31734			   && peek->type != CPP_SEMICOLON);
31735		}
31736	      else
31737		{
31738		  cp_lexer_consume_token (parser->lexer);
31739		  begin_scope (sk_function_parms, NULL_TREE);
31740		  tree t = cp_parser_parameter_declaration_list
31741			    (parser, CP_PARSER_FLAGS_NONE);
31742		  if (t == error_mark_node)
31743		    error = true;
31744		  pop_bindings_and_leave_scope ();
31745		}
31746	      if (!cp_parser_error_occurred (parser) && !error)
31747		done = true;
31748	      cp_parser_abort_tentative_parse (parser);
31749
31750	      parser->in_template_argument_list_p = saved_italp;
31751	      break;
31752	    }
31753	  /* FALLTHRU */
31754	case CPP_CLOSE_PAREN:
31755	case CPP_ELLIPSIS:
31756	  /* If we run into a non-nested `;', `}', or `]',
31757	     then the code is invalid -- but the default
31758	     argument is certainly over.  */
31759	case CPP_SEMICOLON:
31760	case CPP_CLOSE_BRACE:
31761	case CPP_CLOSE_SQUARE:
31762	  if (depth == 0
31763	      /* Handle correctly int n = sizeof ... ( p );  */
31764	      && token->type != CPP_ELLIPSIS)
31765	    done = true;
31766	  /* Update DEPTH, if necessary.  */
31767	  else if (token->type == CPP_CLOSE_PAREN
31768		   || token->type == CPP_CLOSE_BRACE
31769		   || token->type == CPP_CLOSE_SQUARE)
31770	    --depth;
31771	  break;
31772
31773	case CPP_OPEN_PAREN:
31774	case CPP_OPEN_SQUARE:
31775	case CPP_OPEN_BRACE:
31776	  ++depth;
31777	  break;
31778
31779	case CPP_LESS:
31780	  if (depth == 0)
31781	    /* This might be the comparison operator, or it might
31782	       start a template argument list.  */
31783	    ++maybe_template_id;
31784	  break;
31785
31786	case CPP_RSHIFT:
31787	  if (cxx_dialect == cxx98)
31788	    break;
31789	  /* Fall through for C++0x, which treats the `>>'
31790	     operator like two `>' tokens in certain
31791	     cases.  */
31792	  gcc_fallthrough ();
31793
31794	case CPP_GREATER:
31795	  if (depth == 0)
31796	    {
31797	      /* This might be an operator, or it might close a
31798		 template argument list.  But if a previous '<'
31799		 started a template argument list, this will have
31800		 closed it, so we can't be in one anymore.  */
31801	      maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
31802	      if (maybe_template_id < 0)
31803		maybe_template_id = 0;
31804	    }
31805	  break;
31806
31807	  /* If we run out of tokens, issue an error message.  */
31808	case CPP_EOF:
31809	case CPP_PRAGMA_EOL:
31810	  error_at (token->location, "file ends in default argument");
31811	  return error_mark_node;
31812
31813	case CPP_NAME:
31814	case CPP_SCOPE:
31815	  /* In these cases, we should look for template-ids.
31816	     For example, if the default argument is
31817	     `X<int, double>()', we need to do name lookup to
31818	     figure out whether or not `X' is a template; if
31819	     so, the `,' does not end the default argument.
31820
31821	     That is not yet done.  */
31822	  break;
31823
31824	default:
31825	  break;
31826	}
31827
31828      /* If we've reached the end, stop.  */
31829      if (done)
31830	break;
31831
31832      /* Add the token to the token block.  */
31833      token = cp_lexer_consume_token (parser->lexer);
31834    }
31835
31836  /* Create a DEFERRED_PARSE to represent the unparsed default
31837     argument.  */
31838  default_argument = make_node (DEFERRED_PARSE);
31839  DEFPARSE_TOKENS (default_argument)
31840    = cp_token_cache_new (first_token, token);
31841  DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
31842
31843  return default_argument;
31844}
31845
31846/* A location to use for diagnostics about an unparsed DEFERRED_PARSE.  */
31847
31848location_t
31849defparse_location (tree default_argument)
31850{
31851  cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
31852  location_t start = tokens->first->location;
31853  location_t end = tokens->last->location;
31854  return make_location (start, start, end);
31855}
31856
31857/* Begin parsing tentatively.  We always save tokens while parsing
31858   tentatively so that if the tentative parsing fails we can restore the
31859   tokens.  */
31860
31861static void
31862cp_parser_parse_tentatively (cp_parser* parser)
31863{
31864  /* Enter a new parsing context.  */
31865  parser->context = cp_parser_context_new (parser->context);
31866  /* Begin saving tokens.  */
31867  cp_lexer_save_tokens (parser->lexer);
31868  /* In order to avoid repetitive access control error messages,
31869     access checks are queued up until we are no longer parsing
31870     tentatively.  */
31871  push_deferring_access_checks (dk_deferred);
31872}
31873
31874/* Commit to the currently active tentative parse.  */
31875
31876static void
31877cp_parser_commit_to_tentative_parse (cp_parser* parser)
31878{
31879  cp_parser_context *context;
31880  cp_lexer *lexer;
31881
31882  /* Mark all of the levels as committed.  */
31883  lexer = parser->lexer;
31884  for (context = parser->context; context->next; context = context->next)
31885    {
31886      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
31887	break;
31888      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
31889      while (!cp_lexer_saving_tokens (lexer))
31890	lexer = lexer->next;
31891      cp_lexer_commit_tokens (lexer);
31892    }
31893}
31894
31895/* Commit to the topmost currently active tentative parse.
31896
31897   Note that this function shouldn't be called when there are
31898   irreversible side-effects while in a tentative state.  For
31899   example, we shouldn't create a permanent entry in the symbol
31900   table, or issue an error message that might not apply if the
31901   tentative parse is aborted.  */
31902
31903static void
31904cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
31905{
31906  cp_parser_context *context = parser->context;
31907  cp_lexer *lexer = parser->lexer;
31908
31909  if (context)
31910    {
31911      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
31912	return;
31913      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
31914
31915      while (!cp_lexer_saving_tokens (lexer))
31916	lexer = lexer->next;
31917      cp_lexer_commit_tokens (lexer);
31918    }
31919}
31920
31921/* Abort the currently active tentative parse.  All consumed tokens
31922   will be rolled back, and no diagnostics will be issued.  */
31923
31924static void
31925cp_parser_abort_tentative_parse (cp_parser* parser)
31926{
31927  gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
31928	      || errorcount > 0);
31929  cp_parser_simulate_error (parser);
31930  /* Now, pretend that we want to see if the construct was
31931     successfully parsed.  */
31932  cp_parser_parse_definitely (parser);
31933}
31934
31935/* Stop parsing tentatively.  If a parse error has occurred, restore the
31936   token stream.  Otherwise, commit to the tokens we have consumed.
31937   Returns true if no error occurred; false otherwise.  */
31938
31939static bool
31940cp_parser_parse_definitely (cp_parser* parser)
31941{
31942  bool error_occurred;
31943  cp_parser_context *context;
31944
31945  /* Remember whether or not an error occurred, since we are about to
31946     destroy that information.  */
31947  error_occurred = cp_parser_error_occurred (parser);
31948  /* Remove the topmost context from the stack.  */
31949  context = parser->context;
31950  parser->context = context->next;
31951  /* If no parse errors occurred, commit to the tentative parse.  */
31952  if (!error_occurred)
31953    {
31954      /* Commit to the tokens read tentatively, unless that was
31955	 already done.  */
31956      if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
31957	cp_lexer_commit_tokens (parser->lexer);
31958
31959      pop_to_parent_deferring_access_checks ();
31960    }
31961  /* Otherwise, if errors occurred, roll back our state so that things
31962     are just as they were before we began the tentative parse.  */
31963  else
31964    {
31965      cp_lexer_rollback_tokens (parser->lexer);
31966      pop_deferring_access_checks ();
31967    }
31968  /* Add the context to the front of the free list.  */
31969  context->next = cp_parser_context_free_list;
31970  cp_parser_context_free_list = context;
31971
31972  return !error_occurred;
31973}
31974
31975/* Returns true if we are parsing tentatively and are not committed to
31976   this tentative parse.  */
31977
31978static bool
31979cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
31980{
31981  return (cp_parser_parsing_tentatively (parser)
31982	  && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
31983}
31984
31985/* Returns nonzero iff an error has occurred during the most recent
31986   tentative parse.  */
31987
31988static bool
31989cp_parser_error_occurred (cp_parser* parser)
31990{
31991  return (cp_parser_parsing_tentatively (parser)
31992	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
31993}
31994
31995/* Returns nonzero if GNU extensions are allowed.  */
31996
31997static bool
31998cp_parser_allow_gnu_extensions_p (cp_parser* parser)
31999{
32000  return parser->allow_gnu_extensions_p;
32001}
32002
32003/* Objective-C++ Productions */
32004
32005
32006/* Parse an Objective-C expression, which feeds into a primary-expression
32007   above.
32008
32009   objc-expression:
32010     objc-message-expression
32011     objc-string-literal
32012     objc-encode-expression
32013     objc-protocol-expression
32014     objc-selector-expression
32015
32016  Returns a tree representation of the expression.  */
32017
32018static cp_expr
32019cp_parser_objc_expression (cp_parser* parser)
32020{
32021  /* Try to figure out what kind of declaration is present.  */
32022  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32023
32024  switch (kwd->type)
32025    {
32026    case CPP_OPEN_SQUARE:
32027      return cp_parser_objc_message_expression (parser);
32028
32029    case CPP_OBJC_STRING:
32030      kwd = cp_lexer_consume_token (parser->lexer);
32031      return objc_build_string_object (kwd->u.value);
32032
32033    case CPP_KEYWORD:
32034      switch (kwd->keyword)
32035	{
32036	case RID_AT_ENCODE:
32037	  return cp_parser_objc_encode_expression (parser);
32038
32039	case RID_AT_PROTOCOL:
32040	  return cp_parser_objc_protocol_expression (parser);
32041
32042	case RID_AT_SELECTOR:
32043	  return cp_parser_objc_selector_expression (parser);
32044
32045	default:
32046	  break;
32047	}
32048      /* FALLTHRU */
32049    default:
32050      error_at (kwd->location,
32051		"misplaced %<@%D%> Objective-C++ construct",
32052		kwd->u.value);
32053      cp_parser_skip_to_end_of_block_or_statement (parser);
32054    }
32055
32056  return error_mark_node;
32057}
32058
32059/* Parse an Objective-C message expression.
32060
32061   objc-message-expression:
32062     [ objc-message-receiver objc-message-args ]
32063
32064   Returns a representation of an Objective-C message.  */
32065
32066static tree
32067cp_parser_objc_message_expression (cp_parser* parser)
32068{
32069  tree receiver, messageargs;
32070
32071  parser->objective_c_message_context_p = true;
32072  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32073  cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
32074  receiver = cp_parser_objc_message_receiver (parser);
32075  messageargs = cp_parser_objc_message_args (parser);
32076  location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
32077  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32078
32079  tree result = objc_build_message_expr (receiver, messageargs);
32080
32081  /* Construct a location e.g.
32082       [self func1:5]
32083       ^~~~~~~~~~~~~~
32084     ranging from the '[' to the ']', with the caret at the start.  */
32085  location_t combined_loc = make_location (start_loc, start_loc, end_loc);
32086  protected_set_expr_location (result, combined_loc);
32087
32088  parser->objective_c_message_context_p = false;
32089  return result;
32090}
32091
32092/* Parse an objc-message-receiver.
32093
32094   objc-message-receiver:
32095     expression
32096     simple-type-specifier
32097
32098  Returns a representation of the type or expression.  */
32099
32100static tree
32101cp_parser_objc_message_receiver (cp_parser* parser)
32102{
32103  tree rcv;
32104
32105  /* An Objective-C message receiver may be either (1) a type
32106     or (2) an expression.  */
32107  cp_parser_parse_tentatively (parser);
32108  rcv = cp_parser_expression (parser);
32109
32110  /* If that worked out, fine.  */
32111  if (cp_parser_parse_definitely (parser))
32112    return rcv;
32113
32114  cp_parser_parse_tentatively (parser);
32115  rcv = cp_parser_simple_type_specifier (parser,
32116					 /*decl_specs=*/NULL,
32117					 CP_PARSER_FLAGS_NONE);
32118
32119  if (cp_parser_parse_definitely (parser))
32120    return objc_get_class_reference (rcv);
32121
32122  cp_parser_error (parser, "objective-c++ message receiver expected");
32123  return error_mark_node;
32124}
32125
32126/* Parse the arguments and selectors comprising an Objective-C message.
32127
32128   objc-message-args:
32129     objc-selector
32130     objc-selector-args
32131     objc-selector-args , objc-comma-args
32132
32133   objc-selector-args:
32134     objc-selector [opt] : assignment-expression
32135     objc-selector-args objc-selector [opt] : assignment-expression
32136
32137   objc-comma-args:
32138     assignment-expression
32139     objc-comma-args , assignment-expression
32140
32141   Returns a TREE_LIST, with TREE_PURPOSE containing a list of
32142   selector arguments and TREE_VALUE containing a list of comma
32143   arguments.  */
32144
32145static tree
32146cp_parser_objc_message_args (cp_parser* parser)
32147{
32148  tree sel_args = NULL_TREE, addl_args = NULL_TREE;
32149  bool maybe_unary_selector_p = true;
32150  cp_token *token = cp_lexer_peek_token (parser->lexer);
32151
32152  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
32153    {
32154      tree selector = NULL_TREE, arg;
32155
32156      if (token->type != CPP_COLON)
32157	selector = cp_parser_objc_selector (parser);
32158
32159      /* Detect if we have a unary selector.  */
32160      if (maybe_unary_selector_p
32161	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32162	return build_tree_list (selector, NULL_TREE);
32163
32164      maybe_unary_selector_p = false;
32165      cp_parser_require (parser, CPP_COLON, RT_COLON);
32166      arg = cp_parser_assignment_expression (parser);
32167
32168      sel_args
32169	= chainon (sel_args,
32170		   build_tree_list (selector, arg));
32171
32172      token = cp_lexer_peek_token (parser->lexer);
32173    }
32174
32175  /* Handle non-selector arguments, if any. */
32176  while (token->type == CPP_COMMA)
32177    {
32178      tree arg;
32179
32180      cp_lexer_consume_token (parser->lexer);
32181      arg = cp_parser_assignment_expression (parser);
32182
32183      addl_args
32184	= chainon (addl_args,
32185		   build_tree_list (NULL_TREE, arg));
32186
32187      token = cp_lexer_peek_token (parser->lexer);
32188    }
32189
32190  if (sel_args == NULL_TREE && addl_args == NULL_TREE)
32191    {
32192      cp_parser_error (parser, "objective-c++ message argument(s) are expected");
32193      return build_tree_list (error_mark_node, error_mark_node);
32194    }
32195
32196  return build_tree_list (sel_args, addl_args);
32197}
32198
32199/* Parse an Objective-C encode expression.
32200
32201   objc-encode-expression:
32202     @encode objc-typename
32203
32204   Returns an encoded representation of the type argument.  */
32205
32206static cp_expr
32207cp_parser_objc_encode_expression (cp_parser* parser)
32208{
32209  tree type;
32210  cp_token *token;
32211  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32212
32213  cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
32214  matching_parens parens;
32215  parens.require_open (parser);
32216  token = cp_lexer_peek_token (parser->lexer);
32217  type = complete_type (cp_parser_type_id (parser));
32218  parens.require_close (parser);
32219
32220  if (!type)
32221    {
32222      error_at (token->location,
32223		"%<@encode%> must specify a type as an argument");
32224      return error_mark_node;
32225    }
32226
32227  /* This happens if we find @encode(T) (where T is a template
32228     typename or something dependent on a template typename) when
32229     parsing a template.  In that case, we can't compile it
32230     immediately, but we rather create an AT_ENCODE_EXPR which will
32231     need to be instantiated when the template is used.
32232  */
32233  if (dependent_type_p (type))
32234    {
32235      tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
32236      TREE_READONLY (value) = 1;
32237      return value;
32238    }
32239
32240
32241  /* Build a location of the form:
32242       @encode(int)
32243       ^~~~~~~~~~~~
32244     with caret==start at the @ token, finishing at the close paren.  */
32245  location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
32246
32247  return cp_expr (objc_build_encode_expr (type), combined_loc);
32248}
32249
32250/* Parse an Objective-C @defs expression.  */
32251
32252static tree
32253cp_parser_objc_defs_expression (cp_parser *parser)
32254{
32255  tree name;
32256
32257  cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
32258  matching_parens parens;
32259  parens.require_open (parser);
32260  name = cp_parser_identifier (parser);
32261  parens.require_close (parser);
32262
32263  return objc_get_class_ivars (name);
32264}
32265
32266/* Parse an Objective-C protocol expression.
32267
32268  objc-protocol-expression:
32269    @protocol ( identifier )
32270
32271  Returns a representation of the protocol expression.  */
32272
32273static tree
32274cp_parser_objc_protocol_expression (cp_parser* parser)
32275{
32276  tree proto;
32277  location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32278
32279  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
32280  matching_parens parens;
32281  parens.require_open (parser);
32282  proto = cp_parser_identifier (parser);
32283  parens.require_close (parser);
32284
32285  /* Build a location of the form:
32286       @protocol(prot)
32287       ^~~~~~~~~~~~~~~
32288     with caret==start at the @ token, finishing at the close paren.  */
32289  location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
32290  tree result = objc_build_protocol_expr (proto);
32291  protected_set_expr_location (result, combined_loc);
32292  return result;
32293}
32294
32295/* Parse an Objective-C selector expression.
32296
32297   objc-selector-expression:
32298     @selector ( objc-method-signature )
32299
32300   objc-method-signature:
32301     objc-selector
32302     objc-selector-seq
32303
32304   objc-selector-seq:
32305     objc-selector :
32306     objc-selector-seq objc-selector :
32307
32308  Returns a representation of the method selector.  */
32309
32310static tree
32311cp_parser_objc_selector_expression (cp_parser* parser)
32312{
32313  tree sel_seq = NULL_TREE;
32314  bool maybe_unary_selector_p = true;
32315  cp_token *token;
32316  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32317
32318  cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
32319  matching_parens parens;
32320  parens.require_open (parser);
32321  token = cp_lexer_peek_token (parser->lexer);
32322
32323  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
32324	 || token->type == CPP_SCOPE)
32325    {
32326      tree selector = NULL_TREE;
32327
32328      if (token->type != CPP_COLON
32329	  || token->type == CPP_SCOPE)
32330	selector = cp_parser_objc_selector (parser);
32331
32332      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
32333	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
32334	{
32335	  /* Detect if we have a unary selector.  */
32336	  if (maybe_unary_selector_p)
32337	    {
32338	      sel_seq = selector;
32339	      goto finish_selector;
32340	    }
32341	  else
32342	    {
32343	      cp_parser_error (parser, "expected %<:%>");
32344	    }
32345	}
32346      maybe_unary_selector_p = false;
32347      token = cp_lexer_consume_token (parser->lexer);
32348
32349      if (token->type == CPP_SCOPE)
32350	{
32351	  sel_seq
32352	    = chainon (sel_seq,
32353		       build_tree_list (selector, NULL_TREE));
32354	  sel_seq
32355	    = chainon (sel_seq,
32356		       build_tree_list (NULL_TREE, NULL_TREE));
32357	}
32358      else
32359	sel_seq
32360	  = chainon (sel_seq,
32361		     build_tree_list (selector, NULL_TREE));
32362
32363      token = cp_lexer_peek_token (parser->lexer);
32364    }
32365
32366 finish_selector:
32367  parens.require_close (parser);
32368
32369
32370  /* Build a location of the form:
32371       @selector(func)
32372       ^~~~~~~~~~~~~~~
32373     with caret==start at the @ token, finishing at the close paren.  */
32374  location_t combined_loc = make_location (loc, loc, parser->lexer);
32375  tree result = objc_build_selector_expr (combined_loc, sel_seq);
32376  /* TODO: objc_build_selector_expr doesn't always honor the location.  */
32377  protected_set_expr_location (result, combined_loc);
32378  return result;
32379}
32380
32381/* Parse a list of identifiers.
32382
32383   objc-identifier-list:
32384     identifier
32385     objc-identifier-list , identifier
32386
32387   Returns a TREE_LIST of identifier nodes.  */
32388
32389static tree
32390cp_parser_objc_identifier_list (cp_parser* parser)
32391{
32392  tree identifier;
32393  tree list;
32394  cp_token *sep;
32395
32396  identifier = cp_parser_identifier (parser);
32397  if (identifier == error_mark_node)
32398    return error_mark_node;
32399
32400  list = build_tree_list (NULL_TREE, identifier);
32401  sep = cp_lexer_peek_token (parser->lexer);
32402
32403  while (sep->type == CPP_COMMA)
32404    {
32405      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
32406      identifier = cp_parser_identifier (parser);
32407      if (identifier == error_mark_node)
32408	return list;
32409
32410      list = chainon (list, build_tree_list (NULL_TREE,
32411					     identifier));
32412      sep = cp_lexer_peek_token (parser->lexer);
32413    }
32414
32415  return list;
32416}
32417
32418/* Parse an Objective-C alias declaration.
32419
32420   objc-alias-declaration:
32421     @compatibility_alias identifier identifier ;
32422
32423   This function registers the alias mapping with the Objective-C front end.
32424   It returns nothing.  */
32425
32426static void
32427cp_parser_objc_alias_declaration (cp_parser* parser)
32428{
32429  tree alias, orig;
32430
32431  cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
32432  alias = cp_parser_identifier (parser);
32433  orig = cp_parser_identifier (parser);
32434  objc_declare_alias (alias, orig);
32435  cp_parser_consume_semicolon_at_end_of_statement (parser);
32436}
32437
32438/* Parse an Objective-C class forward-declaration.
32439
32440   objc-class-declaration:
32441     @class objc-identifier-list ;
32442
32443   The function registers the forward declarations with the Objective-C
32444   front end.  It returns nothing.  */
32445
32446static void
32447cp_parser_objc_class_declaration (cp_parser* parser)
32448{
32449  cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
32450  while (true)
32451    {
32452      tree id;
32453
32454      id = cp_parser_identifier (parser);
32455      if (id == error_mark_node)
32456	break;
32457
32458      objc_declare_class (id);
32459
32460      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32461	cp_lexer_consume_token (parser->lexer);
32462      else
32463	break;
32464    }
32465  cp_parser_consume_semicolon_at_end_of_statement (parser);
32466}
32467
32468/* Parse a list of Objective-C protocol references.
32469
32470   objc-protocol-refs-opt:
32471     objc-protocol-refs [opt]
32472
32473   objc-protocol-refs:
32474     < objc-identifier-list >
32475
32476   Returns a TREE_LIST of identifiers, if any.  */
32477
32478static tree
32479cp_parser_objc_protocol_refs_opt (cp_parser* parser)
32480{
32481  tree protorefs = NULL_TREE;
32482
32483  if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
32484    {
32485      cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
32486      protorefs = cp_parser_objc_identifier_list (parser);
32487      cp_parser_require (parser, CPP_GREATER, RT_GREATER);
32488    }
32489
32490  return protorefs;
32491}
32492
32493/* Parse a Objective-C visibility specification.  */
32494
32495static void
32496cp_parser_objc_visibility_spec (cp_parser* parser)
32497{
32498  cp_token *vis = cp_lexer_peek_token (parser->lexer);
32499
32500  switch (vis->keyword)
32501    {
32502    case RID_AT_PRIVATE:
32503      objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
32504      break;
32505    case RID_AT_PROTECTED:
32506      objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
32507      break;
32508    case RID_AT_PUBLIC:
32509      objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
32510      break;
32511    case RID_AT_PACKAGE:
32512      objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
32513      break;
32514    default:
32515      return;
32516    }
32517
32518  /* Eat '@private'/'@protected'/'@public'.  */
32519  cp_lexer_consume_token (parser->lexer);
32520}
32521
32522/* Parse an Objective-C method type.  Return 'true' if it is a class
32523   (+) method, and 'false' if it is an instance (-) method.  */
32524
32525static inline bool
32526cp_parser_objc_method_type (cp_parser* parser)
32527{
32528  if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
32529    return true;
32530  else
32531    return false;
32532}
32533
32534/* Parse an Objective-C protocol qualifier.  */
32535
32536static tree
32537cp_parser_objc_protocol_qualifiers (cp_parser* parser)
32538{
32539  tree quals = NULL_TREE, node;
32540  cp_token *token = cp_lexer_peek_token (parser->lexer);
32541
32542  node = token->u.value;
32543
32544  while (node && identifier_p (node)
32545	 && (node == ridpointers [(int) RID_IN]
32546	     || node == ridpointers [(int) RID_OUT]
32547	     || node == ridpointers [(int) RID_INOUT]
32548	     || node == ridpointers [(int) RID_BYCOPY]
32549	     || node == ridpointers [(int) RID_BYREF]
32550	     || node == ridpointers [(int) RID_ONEWAY]))
32551    {
32552      quals = tree_cons (NULL_TREE, node, quals);
32553      cp_lexer_consume_token (parser->lexer);
32554      token = cp_lexer_peek_token (parser->lexer);
32555      node = token->u.value;
32556    }
32557
32558  return quals;
32559}
32560
32561/* Parse an Objective-C typename.  */
32562
32563static tree
32564cp_parser_objc_typename (cp_parser* parser)
32565{
32566  tree type_name = NULL_TREE;
32567
32568  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32569    {
32570      tree proto_quals, cp_type = NULL_TREE;
32571
32572      matching_parens parens;
32573      parens.consume_open (parser); /* Eat '('.  */
32574      proto_quals = cp_parser_objc_protocol_qualifiers (parser);
32575
32576      /* An ObjC type name may consist of just protocol qualifiers, in which
32577	 case the type shall default to 'id'.  */
32578      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
32579	{
32580	  cp_type = cp_parser_type_id (parser);
32581
32582	  /* If the type could not be parsed, an error has already
32583	     been produced.  For error recovery, behave as if it had
32584	     not been specified, which will use the default type
32585	     'id'.  */
32586	  if (cp_type == error_mark_node)
32587	    {
32588	      cp_type = NULL_TREE;
32589	      /* We need to skip to the closing parenthesis as
32590		 cp_parser_type_id() does not seem to do it for
32591		 us.  */
32592	      cp_parser_skip_to_closing_parenthesis (parser,
32593						     /*recovering=*/true,
32594						     /*or_comma=*/false,
32595						     /*consume_paren=*/false);
32596	    }
32597	}
32598
32599      parens.require_close (parser);
32600      type_name = build_tree_list (proto_quals, cp_type);
32601    }
32602
32603  return type_name;
32604}
32605
32606/* Check to see if TYPE refers to an Objective-C selector name.  */
32607
32608static bool
32609cp_parser_objc_selector_p (enum cpp_ttype type)
32610{
32611  return (type == CPP_NAME || type == CPP_KEYWORD
32612	  || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
32613	  || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
32614	  || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
32615	  || type == CPP_XOR || type == CPP_XOR_EQ);
32616}
32617
32618/* Parse an Objective-C selector.  */
32619
32620static tree
32621cp_parser_objc_selector (cp_parser* parser)
32622{
32623  cp_token *token = cp_lexer_consume_token (parser->lexer);
32624
32625  if (!cp_parser_objc_selector_p (token->type))
32626    {
32627      error_at (token->location, "invalid Objective-C++ selector name");
32628      return error_mark_node;
32629    }
32630
32631  /* C++ operator names are allowed to appear in ObjC selectors.  */
32632  switch (token->type)
32633    {
32634    case CPP_AND_AND: return get_identifier ("and");
32635    case CPP_AND_EQ: return get_identifier ("and_eq");
32636    case CPP_AND: return get_identifier ("bitand");
32637    case CPP_OR: return get_identifier ("bitor");
32638    case CPP_COMPL: return get_identifier ("compl");
32639    case CPP_NOT: return get_identifier ("not");
32640    case CPP_NOT_EQ: return get_identifier ("not_eq");
32641    case CPP_OR_OR: return get_identifier ("or");
32642    case CPP_OR_EQ: return get_identifier ("or_eq");
32643    case CPP_XOR: return get_identifier ("xor");
32644    case CPP_XOR_EQ: return get_identifier ("xor_eq");
32645    default: return token->u.value;
32646    }
32647}
32648
32649/* Parse an Objective-C params list.  */
32650
32651static tree
32652cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
32653{
32654  tree params = NULL_TREE;
32655  bool maybe_unary_selector_p = true;
32656  cp_token *token = cp_lexer_peek_token (parser->lexer);
32657
32658  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
32659    {
32660      tree selector = NULL_TREE, type_name, identifier;
32661      tree parm_attr = NULL_TREE;
32662
32663      if (token->keyword == RID_ATTRIBUTE)
32664	break;
32665
32666      if (token->type != CPP_COLON)
32667	selector = cp_parser_objc_selector (parser);
32668
32669      /* Detect if we have a unary selector.  */
32670      if (maybe_unary_selector_p
32671	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32672	{
32673	  params = selector; /* Might be followed by attributes.  */
32674	  break;
32675	}
32676
32677      maybe_unary_selector_p = false;
32678      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32679	{
32680	  /* Something went quite wrong.  There should be a colon
32681	     here, but there is not.  Stop parsing parameters.  */
32682	  break;
32683	}
32684      type_name = cp_parser_objc_typename (parser);
32685      /* New ObjC allows attributes on parameters too.  */
32686      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32687	parm_attr = cp_parser_attributes_opt (parser);
32688      identifier = cp_parser_identifier (parser);
32689
32690      params
32691	= chainon (params,
32692		   objc_build_keyword_decl (selector,
32693					    type_name,
32694					    identifier,
32695					    parm_attr));
32696
32697      token = cp_lexer_peek_token (parser->lexer);
32698    }
32699
32700  if (params == NULL_TREE)
32701    {
32702      cp_parser_error (parser, "objective-c++ method declaration is expected");
32703      return error_mark_node;
32704    }
32705
32706  /* We allow tail attributes for the method.  */
32707  if (token->keyword == RID_ATTRIBUTE)
32708    {
32709      *attributes = cp_parser_attributes_opt (parser);
32710      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32711	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32712	return params;
32713      cp_parser_error (parser,
32714		       "method attributes must be specified at the end");
32715      return error_mark_node;
32716    }
32717
32718  if (params == NULL_TREE)
32719    {
32720      cp_parser_error (parser, "objective-c++ method declaration is expected");
32721      return error_mark_node;
32722    }
32723  return params;
32724}
32725
32726/* Parse the non-keyword Objective-C params.  */
32727
32728static tree
32729cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
32730				       tree* attributes)
32731{
32732  tree params = make_node (TREE_LIST);
32733  cp_token *token = cp_lexer_peek_token (parser->lexer);
32734  *ellipsisp = false;  /* Initially, assume no ellipsis.  */
32735
32736  while (token->type == CPP_COMMA)
32737    {
32738      cp_parameter_declarator *parmdecl;
32739      tree parm;
32740
32741      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
32742      token = cp_lexer_peek_token (parser->lexer);
32743
32744      if (token->type == CPP_ELLIPSIS)
32745	{
32746	  cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
32747	  *ellipsisp = true;
32748	  token = cp_lexer_peek_token (parser->lexer);
32749	  break;
32750	}
32751
32752      /* TODO: parse attributes for tail parameters.  */
32753      parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
32754						  false, NULL);
32755      parm = grokdeclarator (parmdecl->declarator,
32756			     &parmdecl->decl_specifiers,
32757			     PARM, /*initialized=*/0,
32758			     /*attrlist=*/NULL);
32759
32760      chainon (params, build_tree_list (NULL_TREE, parm));
32761      token = cp_lexer_peek_token (parser->lexer);
32762    }
32763
32764  /* We allow tail attributes for the method.  */
32765  if (token->keyword == RID_ATTRIBUTE)
32766    {
32767      if (*attributes == NULL_TREE)
32768	{
32769	  *attributes = cp_parser_attributes_opt (parser);
32770	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32771	      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32772	    return params;
32773	}
32774      else
32775	/* We have an error, but parse the attributes, so that we can
32776	   carry on.  */
32777	*attributes = cp_parser_attributes_opt (parser);
32778
32779      cp_parser_error (parser,
32780		       "method attributes must be specified at the end");
32781      return error_mark_node;
32782    }
32783
32784  return params;
32785}
32786
32787/* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
32788
32789static void
32790cp_parser_objc_interstitial_code (cp_parser* parser)
32791{
32792  cp_token *token = cp_lexer_peek_token (parser->lexer);
32793
32794  /* If the next token is `extern' and the following token is a string
32795     literal, then we have a linkage specification.  */
32796  if (token->keyword == RID_EXTERN
32797      && cp_parser_is_pure_string_literal
32798	 (cp_lexer_peek_nth_token (parser->lexer, 2)))
32799    cp_parser_linkage_specification (parser);
32800  /* Handle #pragma, if any.  */
32801  else if (token->type == CPP_PRAGMA)
32802    cp_parser_pragma (parser, pragma_objc_icode, NULL);
32803  /* Allow stray semicolons.  */
32804  else if (token->type == CPP_SEMICOLON)
32805    cp_lexer_consume_token (parser->lexer);
32806  /* Mark methods as optional or required, when building protocols.  */
32807  else if (token->keyword == RID_AT_OPTIONAL)
32808    {
32809      cp_lexer_consume_token (parser->lexer);
32810      objc_set_method_opt (true);
32811    }
32812  else if (token->keyword == RID_AT_REQUIRED)
32813    {
32814      cp_lexer_consume_token (parser->lexer);
32815      objc_set_method_opt (false);
32816    }
32817  else if (token->keyword == RID_NAMESPACE)
32818    cp_parser_namespace_definition (parser);
32819  /* Other stray characters must generate errors.  */
32820  else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
32821    {
32822      cp_lexer_consume_token (parser->lexer);
32823      error ("stray %qs between Objective-C++ methods",
32824	     token->type == CPP_OPEN_BRACE ? "{" : "}");
32825    }
32826  /* Finally, try to parse a block-declaration, or a function-definition.  */
32827  else
32828    cp_parser_block_declaration (parser, /*statement_p=*/false);
32829}
32830
32831/* Parse a method signature.  */
32832
32833static tree
32834cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
32835{
32836  tree rettype, kwdparms, optparms;
32837  bool ellipsis = false;
32838  bool is_class_method;
32839
32840  is_class_method = cp_parser_objc_method_type (parser);
32841  rettype = cp_parser_objc_typename (parser);
32842  *attributes = NULL_TREE;
32843  kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
32844  if (kwdparms == error_mark_node)
32845    return error_mark_node;
32846  optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
32847  if (optparms == error_mark_node)
32848    return error_mark_node;
32849
32850  return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
32851}
32852
32853static bool
32854cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
32855{
32856  tree tattr;
32857  cp_lexer_save_tokens (parser->lexer);
32858  tattr = cp_parser_attributes_opt (parser);
32859  gcc_assert (tattr) ;
32860
32861  /* If the attributes are followed by a method introducer, this is not allowed.
32862     Dump the attributes and flag the situation.  */
32863  if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
32864      || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32865    return true;
32866
32867  /* Otherwise, the attributes introduce some interstitial code, possibly so
32868     rewind to allow that check.  */
32869  cp_lexer_rollback_tokens (parser->lexer);
32870  return false;
32871}
32872
32873/* Parse an Objective-C method prototype list.  */
32874
32875static void
32876cp_parser_objc_method_prototype_list (cp_parser* parser)
32877{
32878  cp_token *token = cp_lexer_peek_token (parser->lexer);
32879
32880  while (token->keyword != RID_AT_END && token->type != CPP_EOF)
32881    {
32882      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
32883	{
32884	  tree attributes, sig;
32885	  bool is_class_method;
32886	  if (token->type == CPP_PLUS)
32887	    is_class_method = true;
32888	  else
32889	    is_class_method = false;
32890	  sig = cp_parser_objc_method_signature (parser, &attributes);
32891	  if (sig == error_mark_node)
32892	    {
32893	      cp_parser_skip_to_end_of_block_or_statement (parser);
32894	      token = cp_lexer_peek_token (parser->lexer);
32895	      continue;
32896	    }
32897	  objc_add_method_declaration (is_class_method, sig, attributes);
32898	  cp_parser_consume_semicolon_at_end_of_statement (parser);
32899	}
32900      else if (token->keyword == RID_AT_PROPERTY)
32901	cp_parser_objc_at_property_declaration (parser);
32902      else if (token->keyword == RID_ATTRIBUTE
32903      	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
32904	warning_at (cp_lexer_peek_token (parser->lexer)->location,
32905		    OPT_Wattributes,
32906		    "prefix attributes are ignored for methods");
32907      else
32908	/* Allow for interspersed non-ObjC++ code.  */
32909	cp_parser_objc_interstitial_code (parser);
32910
32911      token = cp_lexer_peek_token (parser->lexer);
32912    }
32913
32914  if (token->type != CPP_EOF)
32915    cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
32916  else
32917    cp_parser_error (parser, "expected %<@end%>");
32918
32919  objc_finish_interface ();
32920}
32921
32922/* Parse an Objective-C method definition list.  */
32923
32924static void
32925cp_parser_objc_method_definition_list (cp_parser* parser)
32926{
32927  cp_token *token = cp_lexer_peek_token (parser->lexer);
32928
32929  while (token->keyword != RID_AT_END && token->type != CPP_EOF)
32930    {
32931      tree meth;
32932
32933      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
32934	{
32935	  cp_token *ptk;
32936	  tree sig, attribute;
32937	  bool is_class_method;
32938	  if (token->type == CPP_PLUS)
32939	    is_class_method = true;
32940	  else
32941	    is_class_method = false;
32942	  push_deferring_access_checks (dk_deferred);
32943	  sig = cp_parser_objc_method_signature (parser, &attribute);
32944	  if (sig == error_mark_node)
32945	    {
32946	      cp_parser_skip_to_end_of_block_or_statement (parser);
32947	      token = cp_lexer_peek_token (parser->lexer);
32948	      continue;
32949	    }
32950	  objc_start_method_definition (is_class_method, sig, attribute,
32951					NULL_TREE);
32952
32953	  /* For historical reasons, we accept an optional semicolon.  */
32954	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32955	    cp_lexer_consume_token (parser->lexer);
32956
32957	  ptk = cp_lexer_peek_token (parser->lexer);
32958	  if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
32959		|| ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
32960	    {
32961	      perform_deferred_access_checks (tf_warning_or_error);
32962	      stop_deferring_access_checks ();
32963	      meth = cp_parser_function_definition_after_declarator (parser,
32964								     false);
32965	      pop_deferring_access_checks ();
32966	      objc_finish_method_definition (meth);
32967	    }
32968	}
32969      /* The following case will be removed once @synthesize is
32970	 completely implemented.  */
32971      else if (token->keyword == RID_AT_PROPERTY)
32972	cp_parser_objc_at_property_declaration (parser);
32973      else if (token->keyword == RID_AT_SYNTHESIZE)
32974	cp_parser_objc_at_synthesize_declaration (parser);
32975      else if (token->keyword == RID_AT_DYNAMIC)
32976	cp_parser_objc_at_dynamic_declaration (parser);
32977      else if (token->keyword == RID_ATTRIBUTE
32978      	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
32979	warning_at (token->location, OPT_Wattributes,
32980	       	    "prefix attributes are ignored for methods");
32981      else
32982	/* Allow for interspersed non-ObjC++ code.  */
32983	cp_parser_objc_interstitial_code (parser);
32984
32985      token = cp_lexer_peek_token (parser->lexer);
32986    }
32987
32988  if (token->type != CPP_EOF)
32989    cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
32990  else
32991    cp_parser_error (parser, "expected %<@end%>");
32992
32993  objc_finish_implementation ();
32994}
32995
32996/* Parse Objective-C ivars.  */
32997
32998static void
32999cp_parser_objc_class_ivars (cp_parser* parser)
33000{
33001  cp_token *token = cp_lexer_peek_token (parser->lexer);
33002
33003  if (token->type != CPP_OPEN_BRACE)
33004    return;	/* No ivars specified.  */
33005
33006  cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
33007  token = cp_lexer_peek_token (parser->lexer);
33008
33009  while (token->type != CPP_CLOSE_BRACE
33010	&& token->keyword != RID_AT_END && token->type != CPP_EOF)
33011    {
33012      cp_decl_specifier_seq declspecs;
33013      int decl_class_or_enum_p;
33014      tree prefix_attributes;
33015
33016      cp_parser_objc_visibility_spec (parser);
33017
33018      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33019	break;
33020
33021      cp_parser_decl_specifier_seq (parser,
33022				    CP_PARSER_FLAGS_OPTIONAL,
33023				    &declspecs,
33024				    &decl_class_or_enum_p);
33025
33026      /* auto, register, static, extern, mutable.  */
33027      if (declspecs.storage_class != sc_none)
33028	{
33029	  cp_parser_error (parser, "invalid type for instance variable");
33030	  declspecs.storage_class = sc_none;
33031	}
33032
33033      /* thread_local.  */
33034      if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33035	{
33036	  cp_parser_error (parser, "invalid type for instance variable");
33037	  declspecs.locations[ds_thread] = 0;
33038	}
33039
33040      /* typedef.  */
33041      if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33042	{
33043	  cp_parser_error (parser, "invalid type for instance variable");
33044	  declspecs.locations[ds_typedef] = 0;
33045	}
33046
33047      prefix_attributes = declspecs.attributes;
33048      declspecs.attributes = NULL_TREE;
33049
33050      /* Keep going until we hit the `;' at the end of the
33051	 declaration.  */
33052      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33053	{
33054	  tree width = NULL_TREE, attributes, first_attribute, decl;
33055	  cp_declarator *declarator = NULL;
33056	  int ctor_dtor_or_conv_p;
33057
33058	  /* Check for a (possibly unnamed) bitfield declaration.  */
33059	  token = cp_lexer_peek_token (parser->lexer);
33060	  if (token->type == CPP_COLON)
33061	    goto eat_colon;
33062
33063	  if (token->type == CPP_NAME
33064	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
33065		  == CPP_COLON))
33066	    {
33067	      /* Get the name of the bitfield.  */
33068	      declarator = make_id_declarator (NULL_TREE,
33069					       cp_parser_identifier (parser),
33070					       sfk_none, token->location);
33071
33072	     eat_colon:
33073	      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
33074	      /* Get the width of the bitfield.  */
33075	      width
33076		= cp_parser_constant_expression (parser);
33077	    }
33078	  else
33079	    {
33080	      /* Parse the declarator.  */
33081	      declarator
33082		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33083					CP_PARSER_FLAGS_NONE,
33084					&ctor_dtor_or_conv_p,
33085					/*parenthesized_p=*/NULL,
33086					/*member_p=*/false,
33087					/*friend_p=*/false,
33088					/*static_p=*/false);
33089	    }
33090
33091	  /* Look for attributes that apply to the ivar.  */
33092	  attributes = cp_parser_attributes_opt (parser);
33093	  /* Remember which attributes are prefix attributes and
33094	     which are not.  */
33095	  first_attribute = attributes;
33096	  /* Combine the attributes.  */
33097	  attributes = attr_chainon (prefix_attributes, attributes);
33098
33099	  if (width)
33100	    /* Create the bitfield declaration.  */
33101	    decl = grokbitfield (declarator, &declspecs,
33102				 width, NULL_TREE, attributes);
33103	  else
33104	    decl = grokfield (declarator, &declspecs,
33105			      NULL_TREE, /*init_const_expr_p=*/false,
33106			      NULL_TREE, attributes);
33107
33108	  /* Add the instance variable.  */
33109	  if (decl != error_mark_node && decl != NULL_TREE)
33110	    objc_add_instance_variable (decl);
33111
33112	  /* Reset PREFIX_ATTRIBUTES.  */
33113	  if (attributes != error_mark_node)
33114	    {
33115	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
33116		attributes = TREE_CHAIN (attributes);
33117	      if (attributes)
33118		TREE_CHAIN (attributes) = NULL_TREE;
33119	    }
33120
33121	  token = cp_lexer_peek_token (parser->lexer);
33122
33123	  if (token->type == CPP_COMMA)
33124	    {
33125	      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
33126	      continue;
33127	    }
33128	  break;
33129	}
33130
33131      cp_parser_consume_semicolon_at_end_of_statement (parser);
33132      token = cp_lexer_peek_token (parser->lexer);
33133    }
33134
33135  if (token->keyword == RID_AT_END)
33136    cp_parser_error (parser, "expected %<}%>");
33137
33138  /* Do not consume the RID_AT_END, so it will be read again as terminating
33139     the @interface of @implementation.  */
33140  if (token->keyword != RID_AT_END && token->type != CPP_EOF)
33141    cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
33142
33143  /* For historical reasons, we accept an optional semicolon.  */
33144  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33145    cp_lexer_consume_token (parser->lexer);
33146}
33147
33148/* Parse an Objective-C protocol declaration.  */
33149
33150static void
33151cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
33152{
33153  tree proto, protorefs;
33154  cp_token *tok;
33155
33156  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
33157  if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33158    {
33159      tok = cp_lexer_peek_token (parser->lexer);
33160      error_at (tok->location, "identifier expected after %<@protocol%>");
33161      cp_parser_consume_semicolon_at_end_of_statement (parser);
33162      return;
33163    }
33164
33165  /* See if we have a forward declaration or a definition.  */
33166  tok = cp_lexer_peek_nth_token (parser->lexer, 2);
33167
33168  /* Try a forward declaration first.  */
33169  if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
33170    {
33171      while (true)
33172	{
33173	  tree id;
33174
33175	  id = cp_parser_identifier (parser);
33176	  if (id == error_mark_node)
33177	    break;
33178
33179	  objc_declare_protocol (id, attributes);
33180
33181	  if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33182	    cp_lexer_consume_token (parser->lexer);
33183	  else
33184	    break;
33185	}
33186      cp_parser_consume_semicolon_at_end_of_statement (parser);
33187    }
33188
33189  /* Ok, we got a full-fledged definition (or at least should).  */
33190  else
33191    {
33192      proto = cp_parser_identifier (parser);
33193      protorefs = cp_parser_objc_protocol_refs_opt (parser);
33194      objc_start_protocol (proto, protorefs, attributes);
33195      cp_parser_objc_method_prototype_list (parser);
33196    }
33197}
33198
33199/* Parse an Objective-C superclass or category.  */
33200
33201static void
33202cp_parser_objc_superclass_or_category (cp_parser *parser,
33203				       bool iface_p,
33204				       tree *super,
33205				       tree *categ, bool *is_class_extension)
33206{
33207  cp_token *next = cp_lexer_peek_token (parser->lexer);
33208
33209  *super = *categ = NULL_TREE;
33210  *is_class_extension = false;
33211  if (next->type == CPP_COLON)
33212    {
33213      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
33214      *super = cp_parser_identifier (parser);
33215    }
33216  else if (next->type == CPP_OPEN_PAREN)
33217    {
33218      matching_parens parens;
33219      parens.consume_open (parser);  /* Eat '('.  */
33220
33221      /* If there is no category name, and this is an @interface, we
33222	 have a class extension.  */
33223      if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33224	{
33225	  *categ = NULL_TREE;
33226	  *is_class_extension = true;
33227	}
33228      else
33229	*categ = cp_parser_identifier (parser);
33230
33231      parens.require_close (parser);
33232    }
33233}
33234
33235/* Parse an Objective-C class interface.  */
33236
33237static void
33238cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
33239{
33240  tree name, super, categ, protos;
33241  bool is_class_extension;
33242
33243  cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
33244  name = cp_parser_identifier (parser);
33245  if (name == error_mark_node)
33246    {
33247      /* It's hard to recover because even if valid @interface stuff
33248	 is to follow, we can't compile it (or validate it) if we
33249	 don't even know which class it refers to.  Let's assume this
33250	 was a stray '@interface' token in the stream and skip it.
33251      */
33252      return;
33253    }
33254  cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
33255					 &is_class_extension);
33256  protos = cp_parser_objc_protocol_refs_opt (parser);
33257
33258  /* We have either a class or a category on our hands.  */
33259  if (categ || is_class_extension)
33260    objc_start_category_interface (name, categ, protos, attributes);
33261  else
33262    {
33263      objc_start_class_interface (name, super, protos, attributes);
33264      /* Handle instance variable declarations, if any.  */
33265      cp_parser_objc_class_ivars (parser);
33266      objc_continue_interface ();
33267    }
33268
33269  cp_parser_objc_method_prototype_list (parser);
33270}
33271
33272/* Parse an Objective-C class implementation.  */
33273
33274static void
33275cp_parser_objc_class_implementation (cp_parser* parser)
33276{
33277  tree name, super, categ;
33278  bool is_class_extension;
33279
33280  cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
33281  name = cp_parser_identifier (parser);
33282  if (name == error_mark_node)
33283    {
33284      /* It's hard to recover because even if valid @implementation
33285	 stuff is to follow, we can't compile it (or validate it) if
33286	 we don't even know which class it refers to.  Let's assume
33287	 this was a stray '@implementation' token in the stream and
33288	 skip it.
33289      */
33290      return;
33291    }
33292  cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
33293					 &is_class_extension);
33294
33295  /* We have either a class or a category on our hands.  */
33296  if (categ)
33297    objc_start_category_implementation (name, categ);
33298  else
33299    {
33300      objc_start_class_implementation (name, super);
33301      /* Handle instance variable declarations, if any.  */
33302      cp_parser_objc_class_ivars (parser);
33303      objc_continue_implementation ();
33304    }
33305
33306  cp_parser_objc_method_definition_list (parser);
33307}
33308
33309/* Consume the @end token and finish off the implementation.  */
33310
33311static void
33312cp_parser_objc_end_implementation (cp_parser* parser)
33313{
33314  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
33315  objc_finish_implementation ();
33316}
33317
33318/* Parse an Objective-C declaration.  */
33319
33320static void
33321cp_parser_objc_declaration (cp_parser* parser, tree attributes)
33322{
33323  /* Try to figure out what kind of declaration is present.  */
33324  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
33325
33326  if (attributes)
33327    switch (kwd->keyword)
33328      {
33329	case RID_AT_ALIAS:
33330	case RID_AT_CLASS:
33331	case RID_AT_END:
33332	  error_at (kwd->location, "attributes may not be specified before"
33333	            " the %<@%D%> Objective-C++ keyword",
33334		    kwd->u.value);
33335	  attributes = NULL;
33336	  break;
33337	case RID_AT_IMPLEMENTATION:
33338	  warning_at (kwd->location, OPT_Wattributes,
33339		      "prefix attributes are ignored before %<@%D%>",
33340		      kwd->u.value);
33341	  attributes = NULL;
33342	default:
33343	  break;
33344      }
33345
33346  switch (kwd->keyword)
33347    {
33348    case RID_AT_ALIAS:
33349      cp_parser_objc_alias_declaration (parser);
33350      break;
33351    case RID_AT_CLASS:
33352      cp_parser_objc_class_declaration (parser);
33353      break;
33354    case RID_AT_PROTOCOL:
33355      cp_parser_objc_protocol_declaration (parser, attributes);
33356      break;
33357    case RID_AT_INTERFACE:
33358      cp_parser_objc_class_interface (parser, attributes);
33359      break;
33360    case RID_AT_IMPLEMENTATION:
33361      cp_parser_objc_class_implementation (parser);
33362      break;
33363    case RID_AT_END:
33364      cp_parser_objc_end_implementation (parser);
33365      break;
33366    default:
33367      error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
33368		kwd->u.value);
33369      cp_parser_skip_to_end_of_block_or_statement (parser);
33370    }
33371}
33372
33373/* Parse an Objective-C try-catch-finally statement.
33374
33375   objc-try-catch-finally-stmt:
33376     @try compound-statement objc-catch-clause-seq [opt]
33377       objc-finally-clause [opt]
33378
33379   objc-catch-clause-seq:
33380     objc-catch-clause objc-catch-clause-seq [opt]
33381
33382   objc-catch-clause:
33383     @catch ( objc-exception-declaration ) compound-statement
33384
33385   objc-finally-clause:
33386     @finally compound-statement
33387
33388   objc-exception-declaration:
33389     parameter-declaration
33390     '...'
33391
33392   where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
33393
33394   Returns NULL_TREE.
33395
33396   PS: This function is identical to c_parser_objc_try_catch_finally_statement
33397   for C.  Keep them in sync.  */
33398
33399static tree
33400cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
33401{
33402  location_t location;
33403  tree stmt;
33404
33405  cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
33406  location = cp_lexer_peek_token (parser->lexer)->location;
33407  objc_maybe_warn_exceptions (location);
33408  /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
33409     node, lest it get absorbed into the surrounding block.  */
33410  stmt = push_stmt_list ();
33411  cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33412  objc_begin_try_stmt (location, pop_stmt_list (stmt));
33413
33414  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
33415    {
33416      cp_parameter_declarator *parm;
33417      tree parameter_declaration = error_mark_node;
33418      bool seen_open_paren = false;
33419      matching_parens parens;
33420
33421      cp_lexer_consume_token (parser->lexer);
33422      if (parens.require_open (parser))
33423	seen_open_paren = true;
33424      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33425	{
33426	  /* We have "@catch (...)" (where the '...' are literally
33427	     what is in the code).  Skip the '...'.
33428	     parameter_declaration is set to NULL_TREE, and
33429	     objc_being_catch_clauses() knows that that means
33430	     '...'.  */
33431	  cp_lexer_consume_token (parser->lexer);
33432	  parameter_declaration = NULL_TREE;
33433	}
33434      else
33435	{
33436	  /* We have "@catch (NSException *exception)" or something
33437	     like that.  Parse the parameter declaration.  */
33438	  parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
33439						  false, NULL);
33440	  if (parm == NULL)
33441	    parameter_declaration = error_mark_node;
33442	  else
33443	    parameter_declaration = grokdeclarator (parm->declarator,
33444						    &parm->decl_specifiers,
33445						    PARM, /*initialized=*/0,
33446						    /*attrlist=*/NULL);
33447	}
33448      if (seen_open_paren)
33449	parens.require_close (parser);
33450      else
33451	{
33452	  /* If there was no open parenthesis, we are recovering from
33453	     an error, and we are trying to figure out what mistake
33454	     the user has made.  */
33455
33456	  /* If there is an immediate closing parenthesis, the user
33457	     probably forgot the opening one (ie, they typed "@catch
33458	     NSException *e)".  Parse the closing parenthesis and keep
33459	     going.  */
33460	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33461	    cp_lexer_consume_token (parser->lexer);
33462
33463	  /* If these is no immediate closing parenthesis, the user
33464	     probably doesn't know that parenthesis are required at
33465	     all (ie, they typed "@catch NSException *e").  So, just
33466	     forget about the closing parenthesis and keep going.  */
33467	}
33468      objc_begin_catch_clause (parameter_declaration);
33469      cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33470      objc_finish_catch_clause ();
33471    }
33472  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
33473    {
33474      cp_lexer_consume_token (parser->lexer);
33475      location = cp_lexer_peek_token (parser->lexer)->location;
33476      /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
33477	 node, lest it get absorbed into the surrounding block.  */
33478      stmt = push_stmt_list ();
33479      cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33480      objc_build_finally_clause (location, pop_stmt_list (stmt));
33481    }
33482
33483  return objc_finish_try_stmt ();
33484}
33485
33486/* Parse an Objective-C synchronized statement.
33487
33488   objc-synchronized-stmt:
33489     @synchronized ( expression ) compound-statement
33490
33491   Returns NULL_TREE.  */
33492
33493static tree
33494cp_parser_objc_synchronized_statement (cp_parser *parser)
33495{
33496  location_t location;
33497  tree lock, stmt;
33498
33499  cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
33500
33501  location = cp_lexer_peek_token (parser->lexer)->location;
33502  objc_maybe_warn_exceptions (location);
33503  matching_parens parens;
33504  parens.require_open (parser);
33505  lock = cp_parser_expression (parser);
33506  parens.require_close (parser);
33507
33508  /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
33509     node, lest it get absorbed into the surrounding block.  */
33510  stmt = push_stmt_list ();
33511  cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33512
33513  return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
33514}
33515
33516/* Parse an Objective-C throw statement.
33517
33518   objc-throw-stmt:
33519     @throw assignment-expression [opt] ;
33520
33521   Returns a constructed '@throw' statement.  */
33522
33523static tree
33524cp_parser_objc_throw_statement (cp_parser *parser)
33525{
33526  tree expr = NULL_TREE;
33527  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33528
33529  cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
33530
33531  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33532    expr = cp_parser_expression (parser);
33533
33534  cp_parser_consume_semicolon_at_end_of_statement (parser);
33535
33536  return objc_build_throw_stmt (loc, expr);
33537}
33538
33539/* Parse an Objective-C statement.  */
33540
33541static tree
33542cp_parser_objc_statement (cp_parser * parser)
33543{
33544  /* Try to figure out what kind of declaration is present.  */
33545  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
33546
33547  switch (kwd->keyword)
33548    {
33549    case RID_AT_TRY:
33550      return cp_parser_objc_try_catch_finally_statement (parser);
33551    case RID_AT_SYNCHRONIZED:
33552      return cp_parser_objc_synchronized_statement (parser);
33553    case RID_AT_THROW:
33554      return cp_parser_objc_throw_statement (parser);
33555    default:
33556      error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
33557	       kwd->u.value);
33558      cp_parser_skip_to_end_of_block_or_statement (parser);
33559    }
33560
33561  return error_mark_node;
33562}
33563
33564/* If we are compiling ObjC++ and we see an __attribute__ we neeed to
33565   look ahead to see if an objc keyword follows the attributes.  This
33566   is to detect the use of prefix attributes on ObjC @interface and
33567   @protocol.  */
33568
33569static bool
33570cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
33571{
33572  cp_lexer_save_tokens (parser->lexer);
33573  *attrib = cp_parser_attributes_opt (parser);
33574  gcc_assert (*attrib);
33575  if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
33576    {
33577      cp_lexer_commit_tokens (parser->lexer);
33578      return true;
33579    }
33580  cp_lexer_rollback_tokens (parser->lexer);
33581  return false;
33582}
33583
33584/* This routine is a minimal replacement for
33585   c_parser_struct_declaration () used when parsing the list of
33586   types/names or ObjC++ properties.  For example, when parsing the
33587   code
33588
33589   @property (readonly) int a, b, c;
33590
33591   this function is responsible for parsing "int a, int b, int c" and
33592   returning the declarations as CHAIN of DECLs.
33593
33594   TODO: Share this code with cp_parser_objc_class_ivars.  It's very
33595   similar parsing.  */
33596static tree
33597cp_parser_objc_struct_declaration (cp_parser *parser)
33598{
33599  tree decls = NULL_TREE;
33600  cp_decl_specifier_seq declspecs;
33601  int decl_class_or_enum_p;
33602  tree prefix_attributes;
33603
33604  cp_parser_decl_specifier_seq (parser,
33605				CP_PARSER_FLAGS_NONE,
33606				&declspecs,
33607				&decl_class_or_enum_p);
33608
33609  if (declspecs.type == error_mark_node)
33610    return error_mark_node;
33611
33612  /* auto, register, static, extern, mutable.  */
33613  if (declspecs.storage_class != sc_none)
33614    {
33615      cp_parser_error (parser, "invalid type for property");
33616      declspecs.storage_class = sc_none;
33617    }
33618
33619  /* thread_local.  */
33620  if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33621    {
33622      cp_parser_error (parser, "invalid type for property");
33623      declspecs.locations[ds_thread] = 0;
33624    }
33625
33626  /* typedef.  */
33627  if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33628    {
33629      cp_parser_error (parser, "invalid type for property");
33630      declspecs.locations[ds_typedef] = 0;
33631    }
33632
33633  prefix_attributes = declspecs.attributes;
33634  declspecs.attributes = NULL_TREE;
33635
33636  /* Keep going until we hit the `;' at the end of the declaration. */
33637  while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33638    {
33639      tree attributes, first_attribute, decl;
33640      cp_declarator *declarator;
33641      cp_token *token;
33642
33643      /* Parse the declarator.  */
33644      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33645					 CP_PARSER_FLAGS_NONE,
33646					 NULL, NULL, false, false, false);
33647
33648      /* Look for attributes that apply to the ivar.  */
33649      attributes = cp_parser_attributes_opt (parser);
33650      /* Remember which attributes are prefix attributes and
33651	 which are not.  */
33652      first_attribute = attributes;
33653      /* Combine the attributes.  */
33654      attributes = attr_chainon (prefix_attributes, attributes);
33655
33656      decl = grokfield (declarator, &declspecs,
33657			NULL_TREE, /*init_const_expr_p=*/false,
33658			NULL_TREE, attributes);
33659
33660      if (decl == error_mark_node || decl == NULL_TREE)
33661	return error_mark_node;
33662
33663      /* Reset PREFIX_ATTRIBUTES.  */
33664      if (attributes != error_mark_node)
33665	{
33666	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
33667	    attributes = TREE_CHAIN (attributes);
33668	  if (attributes)
33669	    TREE_CHAIN (attributes) = NULL_TREE;
33670	}
33671
33672      DECL_CHAIN (decl) = decls;
33673      decls = decl;
33674
33675      token = cp_lexer_peek_token (parser->lexer);
33676      if (token->type == CPP_COMMA)
33677	{
33678	  cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
33679	  continue;
33680	}
33681      else
33682	break;
33683    }
33684  return decls;
33685}
33686
33687/* Parse an Objective-C @property declaration.  The syntax is:
33688
33689   objc-property-declaration:
33690     '@property' objc-property-attributes[opt] struct-declaration ;
33691
33692   objc-property-attributes:
33693    '(' objc-property-attribute-list ')'
33694
33695   objc-property-attribute-list:
33696     objc-property-attribute
33697     objc-property-attribute-list, objc-property-attribute
33698
33699   objc-property-attribute
33700     'getter' = identifier
33701     'setter' = identifier
33702     'readonly'
33703     'readwrite'
33704     'assign'
33705     'retain'
33706     'copy'
33707     'nonatomic'
33708
33709  For example:
33710    @property NSString *name;
33711    @property (readonly) id object;
33712    @property (retain, nonatomic, getter=getTheName) id name;
33713    @property int a, b, c;
33714
33715   PS: This function is identical to
33716   c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
33717static void
33718cp_parser_objc_at_property_declaration (cp_parser *parser)
33719{
33720  /* Parse the optional attribute list.
33721
33722     A list of parsed, but not verified, attributes.  */
33723  vec<property_attribute_info *> prop_attr_list = vNULL;
33724  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33725
33726  cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
33727
33728  /* Parse the optional attribute list...  */
33729  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33730    {
33731      /* Eat the '('.  */
33732      matching_parens parens;
33733      location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
33734      parens.consume_open (parser);
33735      bool syntax_error = false;
33736
33737      /* Allow empty @property attribute lists, but with a warning.  */
33738      location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
33739      location_t attr_comb;
33740      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33741	{
33742	  attr_comb = make_location (attr_end, attr_start, attr_end);
33743	  warning_at (attr_comb, OPT_Wattributes,
33744		      "empty property attribute list");
33745	}
33746      else
33747	while (true)
33748	  {
33749	    cp_token *token = cp_lexer_peek_token (parser->lexer);
33750	    attr_start = token->location;
33751	    attr_end = get_finish (token->location);
33752	    attr_comb = make_location (attr_start, attr_start, attr_end);
33753
33754	    if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
33755	      {
33756		warning_at (attr_comb, OPT_Wattributes,
33757			    "missing property attribute");
33758		if (token->type == CPP_CLOSE_PAREN)
33759		  break;
33760		cp_lexer_consume_token (parser->lexer);
33761		continue;
33762	      }
33763
33764	    tree attr_name = NULL_TREE;
33765	    if (identifier_p (token->u.value))
33766	      attr_name = token->u.value;
33767
33768	    enum rid keyword;
33769	    if (token->type == CPP_NAME)
33770	      keyword = C_RID_CODE (token->u.value);
33771	    else
33772	      keyword = RID_MAX; /* By definition, an unknown property.  */
33773	    cp_lexer_consume_token (parser->lexer);
33774
33775	    enum objc_property_attribute_kind prop_kind
33776	      = objc_prop_attr_kind_for_rid (keyword);
33777	    property_attribute_info *prop
33778	      = new property_attribute_info (attr_name, attr_comb, prop_kind);
33779	    prop_attr_list.safe_push (prop);
33780
33781	    tree meth_name;
33782	    switch (prop->prop_kind)
33783	      {
33784	      default: break;
33785	      case OBJC_PROPERTY_ATTR_UNKNOWN:
33786		if (attr_name)
33787		  error_at (attr_start, "unknown property attribute %qE",
33788			    attr_name);
33789		else
33790		  error_at (attr_start, "unknown property attribute");
33791		prop->parse_error = syntax_error = true;
33792		break;
33793
33794	      case OBJC_PROPERTY_ATTR_GETTER:
33795	      case OBJC_PROPERTY_ATTR_SETTER:
33796		if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33797		  {
33798		    attr_comb = make_location (attr_end, attr_start, attr_end);
33799		    error_at (attr_comb, "expected %<=%> after Objective-C %qE",
33800			      attr_name);
33801		    prop->parse_error = syntax_error = true;
33802		    break;
33803		  }
33804
33805		token = cp_lexer_peek_token (parser->lexer);
33806		attr_end = token->location;
33807		cp_lexer_consume_token (parser->lexer); /* eat the = */
33808
33809		if (!cp_parser_objc_selector_p
33810		     (cp_lexer_peek_token (parser->lexer)->type))
33811		  {
33812		    attr_comb = make_location (attr_end, attr_start, attr_end);
33813		    error_at (attr_comb, "expected %qE selector name",
33814			      attr_name);
33815		    prop->parse_error = syntax_error = true;
33816		    break;
33817		  }
33818
33819		/* Get the end of the method name, and consume the name.  */
33820		token = cp_lexer_peek_token (parser->lexer);
33821		attr_end = get_finish (token->location);
33822		/* Because method names may contain C++ keywords, we have a
33823		   routine to fetch them (this also consumes the token).  */
33824		meth_name = cp_parser_objc_selector (parser);
33825
33826		if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
33827		  {
33828		    if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
33829		      {
33830			attr_comb = make_location (attr_end, attr_start,
33831						   attr_end);
33832			error_at (attr_comb, "setter method names must"
33833				  " terminate with %<:%>");
33834			prop->parse_error = syntax_error = true;
33835		      }
33836		    else
33837		      {
33838			attr_end = get_finish (cp_lexer_peek_token
33839					       (parser->lexer)->location);
33840			cp_lexer_consume_token (parser->lexer);
33841		      }
33842		    attr_comb = make_location (attr_start, attr_start,
33843					       attr_end);
33844		  }
33845		else
33846		  attr_comb = make_location (attr_start, attr_start,
33847					     attr_end);
33848		prop->ident = meth_name;
33849		/* Updated location including all that was successfully
33850		   parsed.  */
33851		prop->prop_loc = attr_comb;
33852		break;
33853	      }
33854
33855	    /* If we see a comma here, then keep going - even if we already
33856	       saw a syntax error.  For simple mistakes e.g. (asign, getter=x)
33857	       this makes a more useful output and avoid spurious warnings
33858	       about missing attributes that are, in fact, specified after the
33859	       one with the syntax error.  */
33860	    if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33861	      cp_lexer_consume_token (parser->lexer);
33862	    else
33863	      break;
33864	  }
33865
33866      if (syntax_error || !parens.require_close (parser))
33867	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33868					       /*or_comma=*/false,
33869					       /*consume_paren=*/true);
33870    }
33871
33872  /* 'properties' is the list of properties that we read.  Usually a
33873     single one, but maybe more (eg, in "@property int a, b, c;" there
33874     are three).
33875     TODO: Update this parsing so that it accepts (erroneous) bitfields so
33876     that we can issue a meaningful and consistent (between C/C++) error
33877     message from objc_add_property_declaration ().  */
33878  tree properties = cp_parser_objc_struct_declaration (parser);
33879
33880  if (properties == error_mark_node)
33881    cp_parser_skip_to_end_of_statement (parser);
33882  else if (properties == NULL_TREE)
33883    cp_parser_error (parser, "expected identifier");
33884  else
33885    {
33886      /* Comma-separated properties are chained together in reverse order;
33887	 add them one by one.  */
33888      properties = nreverse (properties);
33889      for (; properties; properties = TREE_CHAIN (properties))
33890	objc_add_property_declaration (loc, copy_node (properties),
33891				       prop_attr_list);
33892    }
33893
33894  cp_parser_consume_semicolon_at_end_of_statement (parser);
33895
33896  while (!prop_attr_list.is_empty())
33897    delete prop_attr_list.pop ();
33898  prop_attr_list.release ();
33899}
33900
33901/* Parse an Objective-C++ @synthesize declaration.  The syntax is:
33902
33903   objc-synthesize-declaration:
33904     @synthesize objc-synthesize-identifier-list ;
33905
33906   objc-synthesize-identifier-list:
33907     objc-synthesize-identifier
33908     objc-synthesize-identifier-list, objc-synthesize-identifier
33909
33910   objc-synthesize-identifier
33911     identifier
33912     identifier = identifier
33913
33914  For example:
33915    @synthesize MyProperty;
33916    @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
33917
33918  PS: This function is identical to c_parser_objc_at_synthesize_declaration
33919  for C.  Keep them in sync.
33920*/
33921static void
33922cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
33923{
33924  tree list = NULL_TREE;
33925  location_t loc;
33926  loc = cp_lexer_peek_token (parser->lexer)->location;
33927
33928  cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
33929  while (true)
33930    {
33931      tree property, ivar;
33932      property = cp_parser_identifier (parser);
33933      if (property == error_mark_node)
33934	{
33935	  cp_parser_consume_semicolon_at_end_of_statement (parser);
33936	  return;
33937	}
33938      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
33939	{
33940	  cp_lexer_consume_token (parser->lexer);
33941	  ivar = cp_parser_identifier (parser);
33942	  if (ivar == error_mark_node)
33943	    {
33944	      cp_parser_consume_semicolon_at_end_of_statement (parser);
33945	      return;
33946	    }
33947	}
33948      else
33949	ivar = NULL_TREE;
33950      list = chainon (list, build_tree_list (ivar, property));
33951      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33952	cp_lexer_consume_token (parser->lexer);
33953      else
33954	break;
33955    }
33956  cp_parser_consume_semicolon_at_end_of_statement (parser);
33957  objc_add_synthesize_declaration (loc, list);
33958}
33959
33960/* Parse an Objective-C++ @dynamic declaration.  The syntax is:
33961
33962   objc-dynamic-declaration:
33963     @dynamic identifier-list ;
33964
33965   For example:
33966     @dynamic MyProperty;
33967     @dynamic MyProperty, AnotherProperty;
33968
33969  PS: This function is identical to c_parser_objc_at_dynamic_declaration
33970  for C.  Keep them in sync.
33971*/
33972static void
33973cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
33974{
33975  tree list = NULL_TREE;
33976  location_t loc;
33977  loc = cp_lexer_peek_token (parser->lexer)->location;
33978
33979  cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
33980  while (true)
33981    {
33982      tree property;
33983      property = cp_parser_identifier (parser);
33984      if (property == error_mark_node)
33985	{
33986	  cp_parser_consume_semicolon_at_end_of_statement (parser);
33987	  return;
33988	}
33989      list = chainon (list, build_tree_list (NULL, property));
33990      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33991	cp_lexer_consume_token (parser->lexer);
33992      else
33993	break;
33994    }
33995  cp_parser_consume_semicolon_at_end_of_statement (parser);
33996  objc_add_dynamic_declaration (loc, list);
33997}
33998
33999
34000/* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines.  */
34001
34002/* Returns name of the next clause.
34003   If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
34004   the token is not consumed.  Otherwise appropriate pragma_omp_clause is
34005   returned and the token is consumed.  */
34006
34007static pragma_omp_clause
34008cp_parser_omp_clause_name (cp_parser *parser)
34009{
34010  pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
34011
34012  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34013    result = PRAGMA_OACC_CLAUSE_AUTO;
34014  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
34015    result = PRAGMA_OMP_CLAUSE_IF;
34016  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
34017    result = PRAGMA_OMP_CLAUSE_DEFAULT;
34018  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
34019    result = PRAGMA_OACC_CLAUSE_DELETE;
34020  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
34021    result = PRAGMA_OMP_CLAUSE_PRIVATE;
34022  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34023    result = PRAGMA_OMP_CLAUSE_FOR;
34024  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34025    {
34026      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34027      const char *p = IDENTIFIER_POINTER (id);
34028
34029      switch (p[0])
34030	{
34031	case 'a':
34032	  if (!strcmp ("aligned", p))
34033	    result = PRAGMA_OMP_CLAUSE_ALIGNED;
34034	  else if (!strcmp ("async", p))
34035	    result = PRAGMA_OACC_CLAUSE_ASYNC;
34036	  else if (!strcmp ("attach", p))
34037	    result = PRAGMA_OACC_CLAUSE_ATTACH;
34038	  break;
34039	case 'b':
34040	  if (!strcmp ("bind", p))
34041	    result = PRAGMA_OMP_CLAUSE_BIND;
34042	  break;
34043	case 'c':
34044	  if (!strcmp ("collapse", p))
34045	    result = PRAGMA_OMP_CLAUSE_COLLAPSE;
34046	  else if (!strcmp ("copy", p))
34047	    result = PRAGMA_OACC_CLAUSE_COPY;
34048	  else if (!strcmp ("copyin", p))
34049	    result = PRAGMA_OMP_CLAUSE_COPYIN;
34050	  else if (!strcmp ("copyout", p))
34051	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
34052	  else if (!strcmp ("copyprivate", p))
34053	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
34054	  else if (!strcmp ("create", p))
34055	    result = PRAGMA_OACC_CLAUSE_CREATE;
34056	  break;
34057	case 'd':
34058	  if (!strcmp ("defaultmap", p))
34059	    result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
34060	  else if (!strcmp ("depend", p))
34061	    result = PRAGMA_OMP_CLAUSE_DEPEND;
34062	  else if (!strcmp ("detach", p))
34063	    result = PRAGMA_OACC_CLAUSE_DETACH;
34064	  else if (!strcmp ("device", p))
34065	    result = PRAGMA_OMP_CLAUSE_DEVICE;
34066	  else if (!strcmp ("deviceptr", p))
34067	    result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
34068	  else if (!strcmp ("device_resident", p))
34069	    result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
34070	  else if (!strcmp ("device_type", p))
34071	    result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
34072	  else if (!strcmp ("dist_schedule", p))
34073	    result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
34074	  break;
34075	case 'f':
34076	  if (!strcmp ("final", p))
34077	    result = PRAGMA_OMP_CLAUSE_FINAL;
34078	  else if (!strcmp ("finalize", p))
34079	    result = PRAGMA_OACC_CLAUSE_FINALIZE;
34080	  else if (!strcmp ("firstprivate", p))
34081	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
34082	  else if (!strcmp ("from", p))
34083	    result = PRAGMA_OMP_CLAUSE_FROM;
34084	  break;
34085	case 'g':
34086	  if (!strcmp ("gang", p))
34087	    result = PRAGMA_OACC_CLAUSE_GANG;
34088	  else if (!strcmp ("grainsize", p))
34089	    result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
34090	  break;
34091	case 'h':
34092	  if (!strcmp ("hint", p))
34093	    result = PRAGMA_OMP_CLAUSE_HINT;
34094	  else if (!strcmp ("host", p))
34095	    result = PRAGMA_OACC_CLAUSE_HOST;
34096	  break;
34097	case 'i':
34098	  if (!strcmp ("if_present", p))
34099	    result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
34100	  else if (!strcmp ("in_reduction", p))
34101	    result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
34102	  else if (!strcmp ("inbranch", p))
34103	    result = PRAGMA_OMP_CLAUSE_INBRANCH;
34104	  else if (!strcmp ("independent", p))
34105	    result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
34106	  else if (!strcmp ("is_device_ptr", p))
34107	    result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
34108	  break;
34109	case 'l':
34110	  if (!strcmp ("lastprivate", p))
34111	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
34112	  else if (!strcmp ("linear", p))
34113	    result = PRAGMA_OMP_CLAUSE_LINEAR;
34114	  else if (!strcmp ("link", p))
34115	    result = PRAGMA_OMP_CLAUSE_LINK;
34116	  break;
34117	case 'm':
34118	  if (!strcmp ("map", p))
34119	    result = PRAGMA_OMP_CLAUSE_MAP;
34120	  else if (!strcmp ("mergeable", p))
34121	    result = PRAGMA_OMP_CLAUSE_MERGEABLE;
34122	  break;
34123	case 'n':
34124	  if (!strcmp ("no_create", p))
34125	    result = PRAGMA_OACC_CLAUSE_NO_CREATE;
34126	  else if (!strcmp ("nogroup", p))
34127	    result = PRAGMA_OMP_CLAUSE_NOGROUP;
34128	  else if (!strcmp ("nontemporal", p))
34129	    result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
34130	  else if (!strcmp ("notinbranch", p))
34131	    result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
34132	  else if (!strcmp ("nowait", p))
34133	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
34134	  else if (!strcmp ("num_gangs", p))
34135	    result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
34136	  else if (!strcmp ("num_tasks", p))
34137	    result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
34138	  else if (!strcmp ("num_teams", p))
34139	    result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
34140	  else if (!strcmp ("num_threads", p))
34141	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
34142	  else if (!strcmp ("num_workers", p))
34143	    result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
34144	  break;
34145	case 'o':
34146	  if (!strcmp ("ordered", p))
34147	    result = PRAGMA_OMP_CLAUSE_ORDERED;
34148	  else if (!strcmp ("order", p))
34149	    result = PRAGMA_OMP_CLAUSE_ORDER;
34150	  break;
34151	case 'p':
34152	  if (!strcmp ("parallel", p))
34153	    result = PRAGMA_OMP_CLAUSE_PARALLEL;
34154	  else if (!strcmp ("present", p))
34155	    result = PRAGMA_OACC_CLAUSE_PRESENT;
34156	  else if (!strcmp ("present_or_copy", p)
34157		   || !strcmp ("pcopy", p))
34158	    result = PRAGMA_OACC_CLAUSE_COPY;
34159	  else if (!strcmp ("present_or_copyin", p)
34160		   || !strcmp ("pcopyin", p))
34161	    result = PRAGMA_OACC_CLAUSE_COPYIN;
34162	  else if (!strcmp ("present_or_copyout", p)
34163		   || !strcmp ("pcopyout", p))
34164	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
34165	  else if (!strcmp ("present_or_create", p)
34166		   || !strcmp ("pcreate", p))
34167	    result = PRAGMA_OACC_CLAUSE_CREATE;
34168	  else if (!strcmp ("priority", p))
34169	    result = PRAGMA_OMP_CLAUSE_PRIORITY;
34170	  else if (!strcmp ("proc_bind", p))
34171	    result = PRAGMA_OMP_CLAUSE_PROC_BIND;
34172	  break;
34173	case 'r':
34174	  if (!strcmp ("reduction", p))
34175	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
34176	  break;
34177	case 's':
34178	  if (!strcmp ("safelen", p))
34179	    result = PRAGMA_OMP_CLAUSE_SAFELEN;
34180	  else if (!strcmp ("schedule", p))
34181	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
34182	  else if (!strcmp ("sections", p))
34183	    result = PRAGMA_OMP_CLAUSE_SECTIONS;
34184	  else if (!strcmp ("self", p)) /* "self" is a synonym for "host".  */
34185	    result = PRAGMA_OACC_CLAUSE_HOST;
34186	  else if (!strcmp ("seq", p))
34187	    result = PRAGMA_OACC_CLAUSE_SEQ;
34188	  else if (!strcmp ("shared", p))
34189	    result = PRAGMA_OMP_CLAUSE_SHARED;
34190	  else if (!strcmp ("simd", p))
34191	    result = PRAGMA_OMP_CLAUSE_SIMD;
34192	  else if (!strcmp ("simdlen", p))
34193	    result = PRAGMA_OMP_CLAUSE_SIMDLEN;
34194	  break;
34195	case 't':
34196	  if (!strcmp ("task_reduction", p))
34197	    result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
34198	  else if (!strcmp ("taskgroup", p))
34199	    result = PRAGMA_OMP_CLAUSE_TASKGROUP;
34200	  else if (!strcmp ("thread_limit", p))
34201	    result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
34202	  else if (!strcmp ("threads", p))
34203	    result = PRAGMA_OMP_CLAUSE_THREADS;
34204	  else if (!strcmp ("tile", p))
34205	    result = PRAGMA_OACC_CLAUSE_TILE;
34206	  else if (!strcmp ("to", p))
34207	    result = PRAGMA_OMP_CLAUSE_TO;
34208	  break;
34209	case 'u':
34210	  if (!strcmp ("uniform", p))
34211	    result = PRAGMA_OMP_CLAUSE_UNIFORM;
34212	  else if (!strcmp ("untied", p))
34213	    result = PRAGMA_OMP_CLAUSE_UNTIED;
34214	  else if (!strcmp ("use_device", p))
34215	    result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
34216	  else if (!strcmp ("use_device_addr", p))
34217	    result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
34218	  else if (!strcmp ("use_device_ptr", p))
34219	    result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
34220	  break;
34221	case 'v':
34222	  if (!strcmp ("vector", p))
34223	    result = PRAGMA_OACC_CLAUSE_VECTOR;
34224	  else if (!strcmp ("vector_length", p))
34225	    result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
34226	  break;
34227	case 'w':
34228	  if (!strcmp ("wait", p))
34229	    result = PRAGMA_OACC_CLAUSE_WAIT;
34230	  else if (!strcmp ("worker", p))
34231	    result = PRAGMA_OACC_CLAUSE_WORKER;
34232	  break;
34233	}
34234    }
34235
34236  if (result != PRAGMA_OMP_CLAUSE_NONE)
34237    cp_lexer_consume_token (parser->lexer);
34238
34239  return result;
34240}
34241
34242/* Validate that a clause of the given type does not already exist.  */
34243
34244static void
34245check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
34246			   const char *name, location_t location)
34247{
34248  if (omp_find_clause (clauses, code))
34249    error_at (location, "too many %qs clauses", name);
34250}
34251
34252/* OpenMP 2.5:
34253   variable-list:
34254     identifier
34255     variable-list , identifier
34256
34257   In addition, we match a closing parenthesis (or, if COLON is non-NULL,
34258   colon).  An opening parenthesis will have been consumed by the caller.
34259
34260   If KIND is nonzero, create the appropriate node and install the decl
34261   in OMP_CLAUSE_DECL and add the node to the head of the list.
34262
34263   If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
34264   return the list created.
34265
34266   COLON can be NULL if only closing parenthesis should end the list,
34267   or pointer to bool which will receive false if the list is terminated
34268   by closing parenthesis or true if the list is terminated by colon.
34269
34270   The optional ALLOW_DEREF argument is true if list items can use the deref
34271   (->) operator.  */
34272
34273static tree
34274cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
34275				tree list, bool *colon,
34276				bool allow_deref = false)
34277{
34278  cp_token *token;
34279  bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34280  if (colon)
34281    {
34282      parser->colon_corrects_to_scope_p = false;
34283      *colon = false;
34284    }
34285  while (1)
34286    {
34287      tree name, decl;
34288
34289      if (kind == OMP_CLAUSE_DEPEND)
34290	cp_parser_parse_tentatively (parser);
34291      token = cp_lexer_peek_token (parser->lexer);
34292      if (kind != 0
34293	  && current_class_ptr
34294	  && cp_parser_is_keyword (token, RID_THIS))
34295	{
34296	  decl = finish_this_expr ();
34297	  if (TREE_CODE (decl) == NON_LVALUE_EXPR
34298	      || CONVERT_EXPR_P (decl))
34299	    decl = TREE_OPERAND (decl, 0);
34300	  cp_lexer_consume_token (parser->lexer);
34301	}
34302      else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
34303	       || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
34304	       || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
34305	{
34306	  cp_id_kind idk;
34307	  decl = cp_parser_primary_expression (parser, false, false, false,
34308					       &idk);
34309	}
34310      else
34311	{
34312	  name = cp_parser_id_expression (parser, /*template_p=*/false,
34313					  /*check_dependency_p=*/true,
34314					  /*template_p=*/NULL,
34315					  /*declarator_p=*/false,
34316					  /*optional_p=*/false);
34317	  if (name == error_mark_node)
34318	    {
34319	      if (kind == OMP_CLAUSE_DEPEND
34320		  && cp_parser_simulate_error (parser))
34321		goto depend_lvalue;
34322	      goto skip_comma;
34323	    }
34324
34325	  if (identifier_p (name))
34326	    decl = cp_parser_lookup_name_simple (parser, name, token->location);
34327	  else
34328	    decl = name;
34329	  if (decl == error_mark_node)
34330	    {
34331	      if (kind == OMP_CLAUSE_DEPEND
34332		  && cp_parser_simulate_error (parser))
34333		goto depend_lvalue;
34334	      cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
34335					   token->location);
34336	    }
34337	}
34338      if (outer_automatic_var_p (decl))
34339	decl = process_outer_var_ref (decl, tf_warning_or_error);
34340      if (decl == error_mark_node)
34341	;
34342      else if (kind != 0)
34343	{
34344	  switch (kind)
34345	    {
34346	    case OMP_CLAUSE__CACHE_:
34347	      /* The OpenACC cache directive explicitly only allows "array
34348		 elements or subarrays".  */
34349	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
34350		{
34351		  error_at (token->location, "expected %<[%>");
34352		  decl = error_mark_node;
34353		  break;
34354		}
34355	      /* FALLTHROUGH.  */
34356	    case OMP_CLAUSE_MAP:
34357	    case OMP_CLAUSE_FROM:
34358	    case OMP_CLAUSE_TO:
34359	      while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
34360		     || (allow_deref
34361			 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
34362		{
34363		  cpp_ttype ttype
34364		    = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
34365		      ? CPP_DOT : CPP_DEREF;
34366		  location_t loc
34367		    = cp_lexer_peek_token (parser->lexer)->location;
34368		  cp_id_kind idk = CP_ID_KIND_NONE;
34369		  cp_lexer_consume_token (parser->lexer);
34370		  decl = convert_from_reference (decl);
34371		  decl
34372		    = cp_parser_postfix_dot_deref_expression (parser, ttype,
34373							      decl, false,
34374							      &idk, loc);
34375		}
34376	      /* FALLTHROUGH.  */
34377	    case OMP_CLAUSE_DEPEND:
34378	    case OMP_CLAUSE_REDUCTION:
34379	    case OMP_CLAUSE_IN_REDUCTION:
34380	    case OMP_CLAUSE_TASK_REDUCTION:
34381	      while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
34382		{
34383		  tree low_bound = NULL_TREE, length = NULL_TREE;
34384
34385		  parser->colon_corrects_to_scope_p = false;
34386		  cp_lexer_consume_token (parser->lexer);
34387		  if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34388		    {
34389		      low_bound = cp_parser_expression (parser);
34390		      /* Later handling is not prepared to see through these.  */
34391		      gcc_checking_assert (!location_wrapper_p (low_bound));
34392		    }
34393		  if (!colon)
34394		    parser->colon_corrects_to_scope_p
34395		      = saved_colon_corrects_to_scope_p;
34396		  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
34397		    length = integer_one_node;
34398		  else
34399		    {
34400		      /* Look for `:'.  */
34401		      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34402			{
34403			  if (kind == OMP_CLAUSE_DEPEND
34404			      && cp_parser_simulate_error (parser))
34405			    goto depend_lvalue;
34406			  goto skip_comma;
34407			}
34408		      if (kind == OMP_CLAUSE_DEPEND)
34409			cp_parser_commit_to_tentative_parse (parser);
34410		      if (!cp_lexer_next_token_is (parser->lexer,
34411						   CPP_CLOSE_SQUARE))
34412			{
34413			  length = cp_parser_expression (parser);
34414			  /* Later handling is not prepared to see through these.  */
34415			  gcc_checking_assert (!location_wrapper_p (length));
34416			}
34417		    }
34418		  /* Look for the closing `]'.  */
34419		  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
34420					  RT_CLOSE_SQUARE))
34421		    {
34422		      if (kind == OMP_CLAUSE_DEPEND
34423			  && cp_parser_simulate_error (parser))
34424			goto depend_lvalue;
34425		      goto skip_comma;
34426		    }
34427
34428		  decl = tree_cons (low_bound, length, decl);
34429		}
34430	      break;
34431	    default:
34432	      break;
34433	    }
34434
34435	  if (kind == OMP_CLAUSE_DEPEND)
34436	    {
34437	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
34438		  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
34439		  && cp_parser_simulate_error (parser))
34440		{
34441		depend_lvalue:
34442		  cp_parser_abort_tentative_parse (parser);
34443		  decl = cp_parser_assignment_expression (parser, NULL,
34444							  false, false);
34445		}
34446	      else
34447		cp_parser_parse_definitely (parser);
34448	    }
34449
34450	  tree u = build_omp_clause (token->location, kind);
34451	  OMP_CLAUSE_DECL (u) = decl;
34452	  OMP_CLAUSE_CHAIN (u) = list;
34453	  list = u;
34454	}
34455      else
34456	list = tree_cons (decl, NULL_TREE, list);
34457
34458    get_comma:
34459      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34460	break;
34461      cp_lexer_consume_token (parser->lexer);
34462    }
34463
34464  if (colon)
34465    parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34466
34467  if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34468    {
34469      *colon = true;
34470      cp_parser_require (parser, CPP_COLON, RT_COLON);
34471      return list;
34472    }
34473
34474  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34475    {
34476      int ending;
34477
34478      /* Try to resync to an unnested comma.  Copied from
34479	 cp_parser_parenthesized_expression_list.  */
34480    skip_comma:
34481      if (colon)
34482	parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34483      ending = cp_parser_skip_to_closing_parenthesis (parser,
34484						      /*recovering=*/true,
34485						      /*or_comma=*/true,
34486						      /*consume_paren=*/true);
34487      if (ending < 0)
34488	goto get_comma;
34489    }
34490
34491  return list;
34492}
34493
34494/* Similarly, but expect leading and trailing parenthesis.  This is a very
34495   common case for omp clauses.  */
34496
34497static tree
34498cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
34499			bool allow_deref = false)
34500{
34501  if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34502    return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
34503					   allow_deref);
34504  return list;
34505}
34506
34507/* OpenACC 2.0:
34508   copy ( variable-list )
34509   copyin ( variable-list )
34510   copyout ( variable-list )
34511   create ( variable-list )
34512   delete ( variable-list )
34513   present ( variable-list )
34514
34515   OpenACC 2.6:
34516   no_create ( variable-list )
34517   attach ( variable-list )
34518   detach ( variable-list ) */
34519
34520static tree
34521cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
34522			    tree list)
34523{
34524  enum gomp_map_kind kind;
34525  switch (c_kind)
34526    {
34527    case PRAGMA_OACC_CLAUSE_ATTACH:
34528      kind = GOMP_MAP_ATTACH;
34529      break;
34530    case PRAGMA_OACC_CLAUSE_COPY:
34531      kind = GOMP_MAP_TOFROM;
34532      break;
34533    case PRAGMA_OACC_CLAUSE_COPYIN:
34534      kind = GOMP_MAP_TO;
34535      break;
34536    case PRAGMA_OACC_CLAUSE_COPYOUT:
34537      kind = GOMP_MAP_FROM;
34538      break;
34539    case PRAGMA_OACC_CLAUSE_CREATE:
34540      kind = GOMP_MAP_ALLOC;
34541      break;
34542    case PRAGMA_OACC_CLAUSE_DELETE:
34543      kind = GOMP_MAP_RELEASE;
34544      break;
34545    case PRAGMA_OACC_CLAUSE_DETACH:
34546      kind = GOMP_MAP_DETACH;
34547      break;
34548    case PRAGMA_OACC_CLAUSE_DEVICE:
34549      kind = GOMP_MAP_FORCE_TO;
34550      break;
34551    case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34552      kind = GOMP_MAP_DEVICE_RESIDENT;
34553      break;
34554    case PRAGMA_OACC_CLAUSE_HOST:
34555      kind = GOMP_MAP_FORCE_FROM;
34556      break;
34557    case PRAGMA_OACC_CLAUSE_LINK:
34558      kind = GOMP_MAP_LINK;
34559      break;
34560    case PRAGMA_OACC_CLAUSE_NO_CREATE:
34561      kind = GOMP_MAP_IF_PRESENT;
34562      break;
34563    case PRAGMA_OACC_CLAUSE_PRESENT:
34564      kind = GOMP_MAP_FORCE_PRESENT;
34565      break;
34566    default:
34567      gcc_unreachable ();
34568    }
34569  tree nl, c;
34570  nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
34571
34572  for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
34573    OMP_CLAUSE_SET_MAP_KIND (c, kind);
34574
34575  return nl;
34576}
34577
34578/* OpenACC 2.0:
34579   deviceptr ( variable-list ) */
34580
34581static tree
34582cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
34583{
34584  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34585  tree vars, t;
34586
34587  /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
34588     cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
34589     variable-list must only allow for pointer variables.  */
34590  vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34591  for (t = vars; t; t = TREE_CHAIN (t))
34592    {
34593      tree v = TREE_PURPOSE (t);
34594      tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
34595      OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
34596      OMP_CLAUSE_DECL (u) = v;
34597      OMP_CLAUSE_CHAIN (u) = list;
34598      list = u;
34599    }
34600
34601  return list;
34602}
34603
34604/* OpenACC 2.5:
34605   auto
34606   finalize
34607   independent
34608   nohost
34609   seq */
34610
34611static tree
34612cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
34613			      tree list)
34614{
34615  check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
34616
34617  tree c = build_omp_clause (loc, code);
34618  OMP_CLAUSE_CHAIN (c) = list;
34619
34620  return c;
34621}
34622
34623 /* OpenACC:
34624   num_gangs ( expression )
34625   num_workers ( expression )
34626   vector_length ( expression )  */
34627
34628static tree
34629cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
34630				  const char *str, tree list)
34631{
34632  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34633
34634  matching_parens parens;
34635  if (!parens.require_open (parser))
34636    return list;
34637
34638  tree t = cp_parser_assignment_expression (parser, NULL, false, false);
34639
34640  if (t == error_mark_node
34641      || !parens.require_close (parser))
34642    {
34643      cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34644					     /*or_comma=*/false,
34645					     /*consume_paren=*/true);
34646      return list;
34647    }
34648
34649  check_no_duplicate_clause (list, code, str, loc);
34650
34651  tree c = build_omp_clause (loc, code);
34652  OMP_CLAUSE_OPERAND (c, 0) = t;
34653  OMP_CLAUSE_CHAIN (c) = list;
34654  return c;
34655}
34656
34657/* OpenACC:
34658
34659    gang [( gang-arg-list )]
34660    worker [( [num:] int-expr )]
34661    vector [( [length:] int-expr )]
34662
34663  where gang-arg is one of:
34664
34665    [num:] int-expr
34666    static: size-expr
34667
34668  and size-expr may be:
34669
34670    *
34671    int-expr
34672*/
34673
34674static tree
34675cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
34676			     omp_clause_code kind,
34677			     const char *str, tree list)
34678{
34679  const char *id = "num";
34680  cp_lexer *lexer = parser->lexer;
34681  tree ops[2] = { NULL_TREE, NULL_TREE }, c;
34682
34683  if (kind == OMP_CLAUSE_VECTOR)
34684    id = "length";
34685
34686  if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
34687    {
34688      matching_parens parens;
34689      parens.consume_open (parser);
34690
34691      do
34692	{
34693	  cp_token *next = cp_lexer_peek_token (lexer);
34694	  int idx = 0;
34695
34696	  /* Gang static argument.  */
34697	  if (kind == OMP_CLAUSE_GANG
34698	      && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
34699	    {
34700	      cp_lexer_consume_token (lexer);
34701
34702	      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34703		goto cleanup_error;
34704
34705	      idx = 1;
34706	      if (ops[idx] != NULL)
34707		{
34708		  cp_parser_error (parser, "too many %<static%> arguments");
34709		  goto cleanup_error;
34710		}
34711
34712	      /* Check for the '*' argument.  */
34713	      if (cp_lexer_next_token_is (lexer, CPP_MULT)
34714		  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
34715		      || cp_lexer_nth_token_is (parser->lexer, 2,
34716						CPP_CLOSE_PAREN)))
34717		{
34718		  cp_lexer_consume_token (lexer);
34719		  ops[idx] = integer_minus_one_node;
34720
34721		  if (cp_lexer_next_token_is (lexer, CPP_COMMA))
34722		    {
34723		      cp_lexer_consume_token (lexer);
34724		      continue;
34725		    }
34726		  else break;
34727		}
34728	    }
34729	  /* Worker num: argument and vector length: arguments.  */
34730	  else if (cp_lexer_next_token_is (lexer, CPP_NAME)
34731		   && id_equal (next->u.value, id)
34732		   && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
34733	    {
34734	      cp_lexer_consume_token (lexer);  /* id  */
34735	      cp_lexer_consume_token (lexer);  /* ':'  */
34736	    }
34737
34738	  /* Now collect the actual argument.  */
34739	  if (ops[idx] != NULL_TREE)
34740	    {
34741	      cp_parser_error (parser, "unexpected argument");
34742	      goto cleanup_error;
34743	    }
34744
34745	  tree expr = cp_parser_assignment_expression (parser, NULL, false,
34746						       false);
34747	  if (expr == error_mark_node)
34748	    goto cleanup_error;
34749
34750	  mark_exp_read (expr);
34751	  ops[idx] = expr;
34752
34753	  if (kind == OMP_CLAUSE_GANG
34754	      && cp_lexer_next_token_is (lexer, CPP_COMMA))
34755	    {
34756	      cp_lexer_consume_token (lexer);
34757	      continue;
34758	    }
34759	  break;
34760	}
34761      while (1);
34762
34763      if (!parens.require_close (parser))
34764	goto cleanup_error;
34765    }
34766
34767  check_no_duplicate_clause (list, kind, str, loc);
34768
34769  c = build_omp_clause (loc, kind);
34770
34771  if (ops[1])
34772    OMP_CLAUSE_OPERAND (c, 1) = ops[1];
34773
34774  OMP_CLAUSE_OPERAND (c, 0) = ops[0];
34775  OMP_CLAUSE_CHAIN (c) = list;
34776
34777  return c;
34778
34779 cleanup_error:
34780  cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34781  return list;
34782}
34783
34784/* OpenACC 2.0:
34785   tile ( size-expr-list ) */
34786
34787static tree
34788cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
34789{
34790  tree c, expr = error_mark_node;
34791  tree tile = NULL_TREE;
34792
34793  /* Collapse and tile are mutually exclusive.  (The spec doesn't say
34794     so, but the spec authors never considered such a case and have
34795     differing opinions on what it might mean, including 'not
34796     allowed'.)  */
34797  check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
34798  check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
34799			     clause_loc);
34800
34801  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34802    return list;
34803
34804  do
34805    {
34806      if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
34807	return list;
34808
34809      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
34810	  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
34811	      || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
34812	{
34813	  cp_lexer_consume_token (parser->lexer);
34814	  expr = integer_zero_node;
34815	}
34816      else
34817	expr = cp_parser_constant_expression (parser);
34818
34819      tile = tree_cons (NULL_TREE, expr, tile);
34820    }
34821  while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
34822
34823  /* Consume the trailing ')'.  */
34824  cp_lexer_consume_token (parser->lexer);
34825
34826  c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
34827  tile = nreverse (tile);
34828  OMP_CLAUSE_TILE_LIST (c) = tile;
34829  OMP_CLAUSE_CHAIN (c) = list;
34830  return c;
34831}
34832
34833/* OpenACC 2.0
34834   Parse wait clause or directive parameters.  */
34835
34836static tree
34837cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
34838{
34839  vec<tree, va_gc> *args;
34840  tree t, args_tree;
34841
34842  args = cp_parser_parenthesized_expression_list (parser, non_attr,
34843						  /*cast_p=*/false,
34844						  /*allow_expansion_p=*/true,
34845						  /*non_constant_p=*/NULL);
34846
34847  if (args == NULL || args->length () == 0)
34848    {
34849      if (args != NULL)
34850	{
34851	  cp_parser_error (parser, "expected integer expression list");
34852	  release_tree_vector (args);
34853	}
34854      return list;
34855    }
34856
34857  args_tree = build_tree_list_vec (args);
34858
34859  release_tree_vector (args);
34860
34861  for (t = args_tree; t; t = TREE_CHAIN (t))
34862    {
34863      tree targ = TREE_VALUE (t);
34864
34865      if (targ != error_mark_node)
34866	{
34867	  if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
34868	    error ("%<wait%> expression must be integral");
34869	  else
34870	    {
34871	      tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
34872
34873	      targ = mark_rvalue_use (targ);
34874	      OMP_CLAUSE_DECL (c) = targ;
34875	      OMP_CLAUSE_CHAIN (c) = list;
34876	      list = c;
34877	    }
34878	}
34879    }
34880
34881  return list;
34882}
34883
34884/* OpenACC:
34885   wait [( int-expr-list )] */
34886
34887static tree
34888cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
34889{
34890  location_t location = cp_lexer_peek_token (parser->lexer)->location;
34891
34892  if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34893    list = cp_parser_oacc_wait_list (parser, location, list);
34894  else
34895    {
34896      tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
34897
34898      OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34899      OMP_CLAUSE_CHAIN (c) = list;
34900      list = c;
34901    }
34902
34903  return list;
34904}
34905
34906/* OpenMP 3.0:
34907   collapse ( constant-expression ) */
34908
34909static tree
34910cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
34911{
34912  tree c, num;
34913  location_t loc;
34914  HOST_WIDE_INT n;
34915
34916  loc = cp_lexer_peek_token (parser->lexer)->location;
34917  matching_parens parens;
34918  if (!parens.require_open (parser))
34919    return list;
34920
34921  num = cp_parser_constant_expression (parser);
34922
34923  if (!parens.require_close (parser))
34924    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34925					   /*or_comma=*/false,
34926					   /*consume_paren=*/true);
34927
34928  if (num == error_mark_node)
34929    return list;
34930  num = fold_non_dependent_expr (num);
34931  if (!tree_fits_shwi_p (num)
34932      || !INTEGRAL_TYPE_P (TREE_TYPE (num))
34933      || (n = tree_to_shwi (num)) <= 0
34934      || (int) n != n)
34935    {
34936      error_at (loc, "collapse argument needs positive constant integer expression");
34937      return list;
34938    }
34939
34940  check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
34941  check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
34942  c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
34943  OMP_CLAUSE_CHAIN (c) = list;
34944  OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
34945
34946  return c;
34947}
34948
34949/* OpenMP 2.5:
34950   default ( none | shared )
34951
34952   OpenACC:
34953   default ( none | present ) */
34954
34955static tree
34956cp_parser_omp_clause_default (cp_parser *parser, tree list,
34957			      location_t location, bool is_oacc)
34958{
34959  enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
34960  tree c;
34961
34962  matching_parens parens;
34963  if (!parens.require_open (parser))
34964    return list;
34965  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34966    {
34967      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34968      const char *p = IDENTIFIER_POINTER (id);
34969
34970      switch (p[0])
34971	{
34972	case 'n':
34973	  if (strcmp ("none", p) != 0)
34974	    goto invalid_kind;
34975	  kind = OMP_CLAUSE_DEFAULT_NONE;
34976	  break;
34977
34978	case 'p':
34979	  if (strcmp ("present", p) != 0 || !is_oacc)
34980	    goto invalid_kind;
34981	  kind = OMP_CLAUSE_DEFAULT_PRESENT;
34982	  break;
34983
34984	case 's':
34985	  if (strcmp ("shared", p) != 0 || is_oacc)
34986	    goto invalid_kind;
34987	  kind = OMP_CLAUSE_DEFAULT_SHARED;
34988	  break;
34989
34990	default:
34991	  goto invalid_kind;
34992	}
34993
34994      cp_lexer_consume_token (parser->lexer);
34995    }
34996  else
34997    {
34998    invalid_kind:
34999      if (is_oacc)
35000	cp_parser_error (parser, "expected %<none%> or %<present%>");
35001      else
35002	cp_parser_error (parser, "expected %<none%> or %<shared%>");
35003    }
35004
35005  if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
35006      || !parens.require_close (parser))
35007    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35008					   /*or_comma=*/false,
35009					   /*consume_paren=*/true);
35010
35011  if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
35012    return list;
35013
35014  check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
35015  c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
35016  OMP_CLAUSE_CHAIN (c) = list;
35017  OMP_CLAUSE_DEFAULT_KIND (c) = kind;
35018
35019  return c;
35020}
35021
35022/* OpenMP 3.1:
35023   final ( expression ) */
35024
35025static tree
35026cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
35027{
35028  tree t, c;
35029
35030  matching_parens parens;
35031  if (!parens.require_open (parser))
35032    return list;
35033
35034  t = cp_parser_assignment_expression (parser);
35035
35036  if (t == error_mark_node
35037      || !parens.require_close (parser))
35038    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35039					   /*or_comma=*/false,
35040					   /*consume_paren=*/true);
35041
35042  check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
35043
35044  c = build_omp_clause (location, OMP_CLAUSE_FINAL);
35045  OMP_CLAUSE_FINAL_EXPR (c) = t;
35046  OMP_CLAUSE_CHAIN (c) = list;
35047
35048  return c;
35049}
35050
35051/* OpenMP 2.5:
35052   if ( expression )
35053
35054   OpenMP 4.5:
35055   if ( directive-name-modifier : expression )
35056
35057   directive-name-modifier:
35058     parallel | task | taskloop | target data | target | target update
35059     | target enter data | target exit data
35060
35061   OpenMP 5.0:
35062   directive-name-modifier:
35063     ... | simd | cancel  */
35064
35065static tree
35066cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
35067			 bool is_omp)
35068{
35069  tree t, c;
35070  enum tree_code if_modifier = ERROR_MARK;
35071
35072  matching_parens parens;
35073  if (!parens.require_open (parser))
35074    return list;
35075
35076  if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35077    {
35078      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35079      const char *p = IDENTIFIER_POINTER (id);
35080      int n = 2;
35081
35082      if (strcmp ("cancel", p) == 0)
35083	if_modifier = VOID_CST;
35084      else if (strcmp ("parallel", p) == 0)
35085	if_modifier = OMP_PARALLEL;
35086      else if (strcmp ("simd", p) == 0)
35087	if_modifier = OMP_SIMD;
35088      else if (strcmp ("task", p) == 0)
35089	if_modifier = OMP_TASK;
35090      else if (strcmp ("taskloop", p) == 0)
35091	if_modifier = OMP_TASKLOOP;
35092      else if (strcmp ("target", p) == 0)
35093	{
35094	  if_modifier = OMP_TARGET;
35095	  if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35096	    {
35097	      id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
35098	      p = IDENTIFIER_POINTER (id);
35099	      if (strcmp ("data", p) == 0)
35100		if_modifier = OMP_TARGET_DATA;
35101	      else if (strcmp ("update", p) == 0)
35102		if_modifier = OMP_TARGET_UPDATE;
35103	      else if (strcmp ("enter", p) == 0)
35104		if_modifier = OMP_TARGET_ENTER_DATA;
35105	      else if (strcmp ("exit", p) == 0)
35106		if_modifier = OMP_TARGET_EXIT_DATA;
35107	      if (if_modifier != OMP_TARGET)
35108		n = 3;
35109	      else
35110		{
35111		  location_t loc
35112		    = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
35113		  error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
35114				 "or %<exit%>");
35115		  if_modifier = ERROR_MARK;
35116		}
35117	      if (if_modifier == OMP_TARGET_ENTER_DATA
35118		  || if_modifier == OMP_TARGET_EXIT_DATA)
35119		{
35120		  if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
35121		    {
35122		      id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
35123		      p = IDENTIFIER_POINTER (id);
35124		      if (strcmp ("data", p) == 0)
35125			n = 4;
35126		    }
35127		  if (n != 4)
35128		    {
35129		      location_t loc
35130			= cp_lexer_peek_nth_token (parser->lexer, 3)->location;
35131		      error_at (loc, "expected %<data%>");
35132		      if_modifier = ERROR_MARK;
35133		    }
35134		}
35135	    }
35136	}
35137      if (if_modifier != ERROR_MARK)
35138	{
35139	  if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
35140	    {
35141	      while (n-- > 0)
35142		cp_lexer_consume_token (parser->lexer);
35143	    }
35144	  else
35145	    {
35146	      if (n > 2)
35147		{
35148		  location_t loc
35149		    = cp_lexer_peek_nth_token (parser->lexer, n)->location;
35150		  error_at (loc, "expected %<:%>");
35151		}
35152	      if_modifier = ERROR_MARK;
35153	    }
35154	}
35155    }
35156
35157  t = cp_parser_assignment_expression (parser);
35158
35159  if (t == error_mark_node
35160      || !parens.require_close (parser))
35161    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35162					   /*or_comma=*/false,
35163					   /*consume_paren=*/true);
35164
35165  for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
35166    if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
35167      {
35168	if (if_modifier != ERROR_MARK
35169	    && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
35170	  {
35171	    const char *p = NULL;
35172	    switch (if_modifier)
35173	      {
35174	      case VOID_CST: p = "cancel"; break;
35175	      case OMP_PARALLEL: p = "parallel"; break;
35176	      case OMP_SIMD: p = "simd"; break;
35177	      case OMP_TASK: p = "task"; break;
35178	      case OMP_TASKLOOP: p = "taskloop"; break;
35179	      case OMP_TARGET_DATA: p = "target data"; break;
35180	      case OMP_TARGET: p = "target"; break;
35181	      case OMP_TARGET_UPDATE: p = "target update"; break;
35182	      case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
35183	      case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
35184	      default: gcc_unreachable ();
35185	      }
35186	    error_at (location, "too many %<if%> clauses with %qs modifier",
35187		      p);
35188	    return list;
35189	  }
35190	else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
35191	  {
35192	    if (!is_omp)
35193	      error_at (location, "too many %<if%> clauses");
35194	    else
35195	      error_at (location, "too many %<if%> clauses without modifier");
35196	    return list;
35197	  }
35198	else if (if_modifier == ERROR_MARK
35199		 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
35200	  {
35201	    error_at (location, "if any %<if%> clause has modifier, then all "
35202				"%<if%> clauses have to use modifier");
35203	    return list;
35204	  }
35205      }
35206
35207  c = build_omp_clause (location, OMP_CLAUSE_IF);
35208  OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
35209  OMP_CLAUSE_IF_EXPR (c) = t;
35210  OMP_CLAUSE_CHAIN (c) = list;
35211
35212  return c;
35213}
35214
35215/* OpenMP 3.1:
35216   mergeable */
35217
35218static tree
35219cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
35220				tree list, location_t location)
35221{
35222  tree c;
35223
35224  check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
35225			     location);
35226
35227  c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
35228  OMP_CLAUSE_CHAIN (c) = list;
35229  return c;
35230}
35231
35232/* OpenMP 2.5:
35233   nowait */
35234
35235static tree
35236cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
35237			     tree list, location_t location)
35238{
35239  tree c;
35240
35241  check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
35242
35243  c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
35244  OMP_CLAUSE_CHAIN (c) = list;
35245  return c;
35246}
35247
35248/* OpenMP 2.5:
35249   num_threads ( expression ) */
35250
35251static tree
35252cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
35253				  location_t location)
35254{
35255  tree t, c;
35256
35257  matching_parens parens;
35258  if (!parens.require_open (parser))
35259    return list;
35260
35261  t = cp_parser_assignment_expression (parser);
35262
35263  if (t == error_mark_node
35264      || !parens.require_close (parser))
35265    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35266					   /*or_comma=*/false,
35267					   /*consume_paren=*/true);
35268
35269  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
35270			     "num_threads", location);
35271
35272  c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
35273  OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
35274  OMP_CLAUSE_CHAIN (c) = list;
35275
35276  return c;
35277}
35278
35279/* OpenMP 4.5:
35280   num_tasks ( expression ) */
35281
35282static tree
35283cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
35284				location_t location)
35285{
35286  tree t, c;
35287
35288  matching_parens parens;
35289  if (!parens.require_open (parser))
35290    return list;
35291
35292  t = cp_parser_assignment_expression (parser);
35293
35294  if (t == error_mark_node
35295      || !parens.require_close (parser))
35296    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35297					   /*or_comma=*/false,
35298					   /*consume_paren=*/true);
35299
35300  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
35301			     "num_tasks", location);
35302
35303  c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
35304  OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
35305  OMP_CLAUSE_CHAIN (c) = list;
35306
35307  return c;
35308}
35309
35310/* OpenMP 4.5:
35311   grainsize ( expression ) */
35312
35313static tree
35314cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
35315				location_t location)
35316{
35317  tree t, c;
35318
35319  matching_parens parens;
35320  if (!parens.require_open (parser))
35321    return list;
35322
35323  t = cp_parser_assignment_expression (parser);
35324
35325  if (t == error_mark_node
35326      || !parens.require_close (parser))
35327    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35328					   /*or_comma=*/false,
35329					   /*consume_paren=*/true);
35330
35331  check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
35332			     "grainsize", location);
35333
35334  c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
35335  OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
35336  OMP_CLAUSE_CHAIN (c) = list;
35337
35338  return c;
35339}
35340
35341/* OpenMP 4.5:
35342   priority ( expression ) */
35343
35344static tree
35345cp_parser_omp_clause_priority (cp_parser *parser, tree list,
35346			       location_t location)
35347{
35348  tree t, c;
35349
35350  matching_parens parens;
35351  if (!parens.require_open (parser))
35352    return list;
35353
35354  t = cp_parser_assignment_expression (parser);
35355
35356  if (t == error_mark_node
35357      || !parens.require_close (parser))
35358    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35359					   /*or_comma=*/false,
35360					   /*consume_paren=*/true);
35361
35362  check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
35363			     "priority", location);
35364
35365  c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
35366  OMP_CLAUSE_PRIORITY_EXPR (c) = t;
35367  OMP_CLAUSE_CHAIN (c) = list;
35368
35369  return c;
35370}
35371
35372/* OpenMP 4.5:
35373   hint ( expression ) */
35374
35375static tree
35376cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
35377{
35378  tree t, c;
35379
35380  matching_parens parens;
35381  if (!parens.require_open (parser))
35382    return list;
35383
35384  t = cp_parser_assignment_expression (parser);
35385
35386  if (t == error_mark_node
35387      || !parens.require_close (parser))
35388    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35389					   /*or_comma=*/false,
35390					   /*consume_paren=*/true);
35391
35392  check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
35393
35394  c = build_omp_clause (location, OMP_CLAUSE_HINT);
35395  OMP_CLAUSE_HINT_EXPR (c) = t;
35396  OMP_CLAUSE_CHAIN (c) = list;
35397
35398  return c;
35399}
35400
35401/* OpenMP 4.5:
35402   defaultmap ( tofrom : scalar )
35403
35404   OpenMP 5.0:
35405   defaultmap ( implicit-behavior [ : variable-category ] ) */
35406
35407static tree
35408cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
35409				 location_t location)
35410{
35411  tree c, id;
35412  const char *p;
35413  enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
35414  enum omp_clause_defaultmap_kind category
35415    = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
35416
35417  matching_parens parens;
35418  if (!parens.require_open (parser))
35419    return list;
35420
35421  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
35422    p = "default";
35423  else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35424    {
35425    invalid_behavior:
35426      cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
35427			       "%<tofrom%>, %<firstprivate%>, %<none%> "
35428			       "or %<default%>");
35429      goto out_err;
35430    }
35431  else
35432    {
35433      id = cp_lexer_peek_token (parser->lexer)->u.value;
35434      p = IDENTIFIER_POINTER (id);
35435    }
35436
35437  switch (p[0])
35438    {
35439    case 'a':
35440      if (strcmp ("alloc", p) == 0)
35441	behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
35442      else
35443	goto invalid_behavior;
35444      break;
35445
35446    case 'd':
35447      if (strcmp ("default", p) == 0)
35448	behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
35449      else
35450	goto invalid_behavior;
35451      break;
35452
35453    case 'f':
35454      if (strcmp ("firstprivate", p) == 0)
35455	behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
35456      else if (strcmp ("from", p) == 0)
35457	behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
35458      else
35459	goto invalid_behavior;
35460      break;
35461
35462    case 'n':
35463      if (strcmp ("none", p) == 0)
35464	behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
35465      else
35466	goto invalid_behavior;
35467      break;
35468
35469    case 't':
35470      if (strcmp ("tofrom", p) == 0)
35471	behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
35472      else if (strcmp ("to", p) == 0)
35473	behavior = OMP_CLAUSE_DEFAULTMAP_TO;
35474      else
35475	goto invalid_behavior;
35476      break;
35477
35478    default:
35479      goto invalid_behavior;
35480    }
35481  cp_lexer_consume_token (parser->lexer);
35482
35483  if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35484    {
35485      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35486	goto out_err;
35487
35488      if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35489	{
35490	invalid_category:
35491	  cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
35492				   "%<pointer%>");
35493	  goto out_err;
35494	}
35495      id = cp_lexer_peek_token (parser->lexer)->u.value;
35496      p = IDENTIFIER_POINTER (id);
35497
35498      switch (p[0])
35499	{
35500	case 'a':
35501	  if (strcmp ("aggregate", p) == 0)
35502	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
35503	  else
35504	    goto invalid_category;
35505	  break;
35506
35507	case 'p':
35508	  if (strcmp ("pointer", p) == 0)
35509	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
35510	  else
35511	    goto invalid_category;
35512	  break;
35513
35514	case 's':
35515	  if (strcmp ("scalar", p) == 0)
35516	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
35517	  else
35518	    goto invalid_category;
35519	  break;
35520
35521	default:
35522	  goto invalid_category;
35523	}
35524
35525      cp_lexer_consume_token (parser->lexer);
35526    }
35527  if (!parens.require_close (parser))
35528    goto out_err;
35529
35530  for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
35531    if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
35532	&& (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
35533	    || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
35534	    || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
35535		== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
35536      {
35537	enum omp_clause_defaultmap_kind cat = category;
35538	location_t loc = OMP_CLAUSE_LOCATION (c);
35539	if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
35540	  cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
35541	p = NULL;
35542	switch (cat)
35543	  {
35544	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
35545	    p = NULL;
35546	    break;
35547	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
35548	    p = "aggregate";
35549	    break;
35550	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
35551	    p = "pointer";
35552	    break;
35553	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
35554	    p = "scalar";
35555	    break;
35556	  default:
35557	    gcc_unreachable ();
35558	  }
35559	if (p)
35560	  error_at (loc, "too many %<defaultmap%> clauses with %qs category",
35561		    p);
35562	else
35563	  error_at (loc, "too many %<defaultmap%> clauses with unspecified "
35564			 "category");
35565	break;
35566      }
35567
35568  c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
35569  OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
35570  OMP_CLAUSE_CHAIN (c) = list;
35571  return c;
35572
35573 out_err:
35574  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35575					 /*or_comma=*/false,
35576					 /*consume_paren=*/true);
35577  return list;
35578}
35579
35580/* OpenMP 5.0:
35581   order ( concurrent ) */
35582
35583static tree
35584cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
35585{
35586  tree c, id;
35587  const char *p;
35588
35589  matching_parens parens;
35590  if (!parens.require_open (parser))
35591    return list;
35592
35593  if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35594    {
35595      cp_parser_error (parser, "expected %<concurrent%>");
35596      goto out_err;
35597    }
35598  else
35599    {
35600      id = cp_lexer_peek_token (parser->lexer)->u.value;
35601      p = IDENTIFIER_POINTER (id);
35602    }
35603  if (strcmp (p, "concurrent") != 0)
35604    {
35605      cp_parser_error (parser, "expected %<concurrent%>");
35606      goto out_err;
35607    }
35608  cp_lexer_consume_token (parser->lexer);
35609  if (!parens.require_close (parser))
35610    goto out_err;
35611
35612  /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
35613  c = build_omp_clause (location, OMP_CLAUSE_ORDER);
35614  OMP_CLAUSE_CHAIN (c) = list;
35615  return c;
35616
35617 out_err:
35618  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35619					 /*or_comma=*/false,
35620					 /*consume_paren=*/true);
35621  return list;
35622}
35623
35624/* OpenMP 5.0:
35625   bind ( teams | parallel | thread ) */
35626
35627static tree
35628cp_parser_omp_clause_bind (cp_parser *parser, tree list,
35629			   location_t location)
35630{
35631  tree c;
35632  const char *p;
35633  enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
35634
35635  matching_parens parens;
35636  if (!parens.require_open (parser))
35637    return list;
35638
35639  if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35640    {
35641    invalid:
35642      cp_parser_error (parser,
35643		       "expected %<teams%>, %<parallel%> or %<thread%>");
35644      goto out_err;
35645    }
35646  else
35647    {
35648      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35649      p = IDENTIFIER_POINTER (id);
35650    }
35651  if (strcmp (p, "teams") == 0)
35652    kind = OMP_CLAUSE_BIND_TEAMS;
35653  else if (strcmp (p, "parallel") == 0)
35654    kind = OMP_CLAUSE_BIND_PARALLEL;
35655  else if (strcmp (p, "thread") != 0)
35656    goto invalid;
35657  cp_lexer_consume_token (parser->lexer);
35658  if (!parens.require_close (parser))
35659    goto out_err;
35660
35661  /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
35662  c = build_omp_clause (location, OMP_CLAUSE_BIND);
35663  OMP_CLAUSE_BIND_KIND (c) = kind;
35664  OMP_CLAUSE_CHAIN (c) = list;
35665  return c;
35666
35667 out_err:
35668  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35669					 /*or_comma=*/false,
35670					 /*consume_paren=*/true);
35671  return list;
35672}
35673
35674/* OpenMP 2.5:
35675   ordered
35676
35677   OpenMP 4.5:
35678   ordered ( constant-expression ) */
35679
35680static tree
35681cp_parser_omp_clause_ordered (cp_parser *parser,
35682			      tree list, location_t location)
35683{
35684  tree c, num = NULL_TREE;
35685  HOST_WIDE_INT n;
35686
35687  check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
35688			     "ordered", location);
35689
35690  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35691    {
35692      matching_parens parens;
35693      parens.consume_open (parser);
35694
35695      num = cp_parser_constant_expression (parser);
35696
35697      if (!parens.require_close (parser))
35698	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35699					       /*or_comma=*/false,
35700					       /*consume_paren=*/true);
35701
35702      if (num == error_mark_node)
35703	return list;
35704      num = fold_non_dependent_expr (num);
35705      if (!tree_fits_shwi_p (num)
35706	  || !INTEGRAL_TYPE_P (TREE_TYPE (num))
35707	  || (n = tree_to_shwi (num)) <= 0
35708	  || (int) n != n)
35709	{
35710	  error_at (location,
35711		    "ordered argument needs positive constant integer "
35712		    "expression");
35713	  return list;
35714	}
35715    }
35716
35717  c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
35718  OMP_CLAUSE_ORDERED_EXPR (c) = num;
35719  OMP_CLAUSE_CHAIN (c) = list;
35720  return c;
35721}
35722
35723/* OpenMP 2.5:
35724   reduction ( reduction-operator : variable-list )
35725
35726   reduction-operator:
35727     One of: + * - & ^ | && ||
35728
35729   OpenMP 3.1:
35730
35731   reduction-operator:
35732     One of: + * - & ^ | && || min max
35733
35734   OpenMP 4.0:
35735
35736   reduction-operator:
35737     One of: + * - & ^ | && ||
35738     id-expression
35739
35740   OpenMP 5.0:
35741   reduction ( reduction-modifier, reduction-operator : variable-list )
35742   in_reduction ( reduction-operator : variable-list )
35743   task_reduction ( reduction-operator : variable-list )  */
35744
35745static tree
35746cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
35747				bool is_omp, tree list)
35748{
35749  enum tree_code code = ERROR_MARK;
35750  tree nlist, c, id = NULL_TREE;
35751  bool task = false;
35752  bool inscan = false;
35753
35754  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35755    return list;
35756
35757  if (kind == OMP_CLAUSE_REDUCTION && is_omp)
35758    {
35759      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
35760	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
35761	{
35762	  cp_lexer_consume_token (parser->lexer);
35763	  cp_lexer_consume_token (parser->lexer);
35764	}
35765      else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35766	       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
35767	{
35768	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35769	  const char *p = IDENTIFIER_POINTER (id);
35770	  if (strcmp (p, "task") == 0)
35771	    task = true;
35772	  else if (strcmp (p, "inscan") == 0)
35773	    inscan = true;
35774	  if (task || inscan)
35775	    {
35776	      cp_lexer_consume_token (parser->lexer);
35777	      cp_lexer_consume_token (parser->lexer);
35778	    }
35779	}
35780    }
35781
35782  switch (cp_lexer_peek_token (parser->lexer)->type)
35783    {
35784    case CPP_PLUS: code = PLUS_EXPR; break;
35785    case CPP_MULT: code = MULT_EXPR; break;
35786    case CPP_MINUS: code = MINUS_EXPR; break;
35787    case CPP_AND: code = BIT_AND_EXPR; break;
35788    case CPP_XOR: code = BIT_XOR_EXPR; break;
35789    case CPP_OR: code = BIT_IOR_EXPR; break;
35790    case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
35791    case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
35792    default: break;
35793    }
35794
35795  if (code != ERROR_MARK)
35796    cp_lexer_consume_token (parser->lexer);
35797  else
35798    {
35799      bool saved_colon_corrects_to_scope_p;
35800      saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
35801      parser->colon_corrects_to_scope_p = false;
35802      id = cp_parser_id_expression (parser, /*template_p=*/false,
35803				    /*check_dependency_p=*/true,
35804				    /*template_p=*/NULL,
35805				    /*declarator_p=*/false,
35806				    /*optional_p=*/false);
35807      parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35808      if (identifier_p (id))
35809	{
35810	  const char *p = IDENTIFIER_POINTER (id);
35811
35812	  if (strcmp (p, "min") == 0)
35813	    code = MIN_EXPR;
35814	  else if (strcmp (p, "max") == 0)
35815	    code = MAX_EXPR;
35816	  else if (id == ovl_op_identifier (false, PLUS_EXPR))
35817	    code = PLUS_EXPR;
35818	  else if (id == ovl_op_identifier (false, MULT_EXPR))
35819	    code = MULT_EXPR;
35820	  else if (id == ovl_op_identifier (false, MINUS_EXPR))
35821	    code = MINUS_EXPR;
35822	  else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
35823	    code = BIT_AND_EXPR;
35824	  else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
35825	    code = BIT_IOR_EXPR;
35826	  else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
35827	    code = BIT_XOR_EXPR;
35828	  else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
35829	    code = TRUTH_ANDIF_EXPR;
35830	  else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
35831	    code = TRUTH_ORIF_EXPR;
35832	  id = omp_reduction_id (code, id, NULL_TREE);
35833	  tree scope = parser->scope;
35834	  if (scope)
35835	    id = build_qualified_name (NULL_TREE, scope, id, false);
35836	  parser->scope = NULL_TREE;
35837	  parser->qualifying_scope = NULL_TREE;
35838	  parser->object_scope = NULL_TREE;
35839	}
35840      else
35841	{
35842	  error ("invalid reduction-identifier");
35843	 resync_fail:
35844	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35845						 /*or_comma=*/false,
35846						 /*consume_paren=*/true);
35847	  return list;
35848	}
35849    }
35850
35851  if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35852    goto resync_fail;
35853
35854  nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
35855					  NULL);
35856  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35857    {
35858      OMP_CLAUSE_REDUCTION_CODE (c) = code;
35859      if (task)
35860	OMP_CLAUSE_REDUCTION_TASK (c) = 1;
35861      else if (inscan)
35862	OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
35863      OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
35864    }
35865
35866  return nlist;
35867}
35868
35869/* OpenMP 2.5:
35870   schedule ( schedule-kind )
35871   schedule ( schedule-kind , expression )
35872
35873   schedule-kind:
35874     static | dynamic | guided | runtime | auto
35875
35876   OpenMP 4.5:
35877   schedule ( schedule-modifier : schedule-kind )
35878   schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
35879
35880   schedule-modifier:
35881     simd
35882     monotonic
35883     nonmonotonic  */
35884
35885static tree
35886cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
35887{
35888  tree c, t;
35889  int modifiers = 0, nmodifiers = 0;
35890
35891  matching_parens parens;
35892  if (!parens.require_open (parser))
35893    return list;
35894
35895  c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
35896
35897  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35898    {
35899      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35900      const char *p = IDENTIFIER_POINTER (id);
35901      if (strcmp ("simd", p) == 0)
35902	OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
35903      else if (strcmp ("monotonic", p) == 0)
35904	modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
35905      else if (strcmp ("nonmonotonic", p) == 0)
35906	modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
35907      else
35908	break;
35909      cp_lexer_consume_token (parser->lexer);
35910      if (nmodifiers++ == 0
35911	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35912	cp_lexer_consume_token (parser->lexer);
35913      else
35914	{
35915	  cp_parser_require (parser, CPP_COLON, RT_COLON);
35916	  break;
35917	}
35918    }
35919
35920  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35921    {
35922      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35923      const char *p = IDENTIFIER_POINTER (id);
35924
35925      switch (p[0])
35926	{
35927	case 'd':
35928	  if (strcmp ("dynamic", p) != 0)
35929	    goto invalid_kind;
35930	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
35931	  break;
35932
35933	case 'g':
35934	  if (strcmp ("guided", p) != 0)
35935	    goto invalid_kind;
35936	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
35937	  break;
35938
35939	case 'r':
35940	  if (strcmp ("runtime", p) != 0)
35941	    goto invalid_kind;
35942	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
35943	  break;
35944
35945	default:
35946	  goto invalid_kind;
35947	}
35948    }
35949  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
35950    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
35951  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
35952    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
35953  else
35954    goto invalid_kind;
35955  cp_lexer_consume_token (parser->lexer);
35956
35957  if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
35958		    | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
35959      == (OMP_CLAUSE_SCHEDULE_MONOTONIC
35960	  | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
35961    {
35962      error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
35963			  "specified");
35964      modifiers = 0;
35965    }
35966
35967  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35968    {
35969      cp_token *token;
35970      cp_lexer_consume_token (parser->lexer);
35971
35972      token = cp_lexer_peek_token (parser->lexer);
35973      t = cp_parser_assignment_expression (parser);
35974
35975      if (t == error_mark_node)
35976	goto resync_fail;
35977      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
35978	error_at (token->location, "schedule %<runtime%> does not take "
35979		  "a %<chunk_size%> parameter");
35980      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
35981	error_at (token->location, "schedule %<auto%> does not take "
35982		  "a %<chunk_size%> parameter");
35983      else
35984	OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
35985
35986      if (!parens.require_close (parser))
35987	goto resync_fail;
35988    }
35989  else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35990    goto resync_fail;
35991
35992  OMP_CLAUSE_SCHEDULE_KIND (c)
35993    = (enum omp_clause_schedule_kind)
35994      (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
35995
35996  check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
35997  OMP_CLAUSE_CHAIN (c) = list;
35998  return c;
35999
36000 invalid_kind:
36001  cp_parser_error (parser, "invalid schedule kind");
36002 resync_fail:
36003  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36004					 /*or_comma=*/false,
36005					 /*consume_paren=*/true);
36006  return list;
36007}
36008
36009/* OpenMP 3.0:
36010   untied */
36011
36012static tree
36013cp_parser_omp_clause_untied (cp_parser * /*parser*/,
36014			     tree list, location_t location)
36015{
36016  tree c;
36017
36018  check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
36019
36020  c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
36021  OMP_CLAUSE_CHAIN (c) = list;
36022  return c;
36023}
36024
36025/* OpenMP 4.0:
36026   inbranch
36027   notinbranch */
36028
36029static tree
36030cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
36031			     tree list, location_t location)
36032{
36033  check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36034  tree c = build_omp_clause (location, code);
36035  OMP_CLAUSE_CHAIN (c) = list;
36036  return c;
36037}
36038
36039/* OpenMP 4.0:
36040   parallel
36041   for
36042   sections
36043   taskgroup */
36044
36045static tree
36046cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
36047				 enum omp_clause_code code,
36048				 tree list, location_t location)
36049{
36050  tree c = build_omp_clause (location, code);
36051  OMP_CLAUSE_CHAIN (c) = list;
36052  return c;
36053}
36054
36055/* OpenMP 4.5:
36056   nogroup */
36057
36058static tree
36059cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
36060			      tree list, location_t location)
36061{
36062  check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
36063  tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
36064  OMP_CLAUSE_CHAIN (c) = list;
36065  return c;
36066}
36067
36068/* OpenMP 4.5:
36069   simd
36070   threads */
36071
36072static tree
36073cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
36074				  enum omp_clause_code code,
36075				  tree list, location_t location)
36076{
36077  check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36078  tree c = build_omp_clause (location, code);
36079  OMP_CLAUSE_CHAIN (c) = list;
36080  return c;
36081}
36082
36083/* OpenMP 4.0:
36084   num_teams ( expression ) */
36085
36086static tree
36087cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
36088				location_t location)
36089{
36090  tree t, c;
36091
36092  matching_parens parens;
36093  if (!parens.require_open (parser))
36094    return list;
36095
36096  t = cp_parser_assignment_expression (parser);
36097
36098  if (t == error_mark_node
36099      || !parens.require_close (parser))
36100    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36101					   /*or_comma=*/false,
36102					   /*consume_paren=*/true);
36103
36104  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
36105			     "num_teams", location);
36106
36107  c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
36108  OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
36109  OMP_CLAUSE_CHAIN (c) = list;
36110
36111  return c;
36112}
36113
36114/* OpenMP 4.0:
36115   thread_limit ( expression ) */
36116
36117static tree
36118cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
36119				   location_t location)
36120{
36121  tree t, c;
36122
36123  matching_parens parens;
36124  if (!parens.require_open (parser))
36125    return list;
36126
36127  t = cp_parser_assignment_expression (parser);
36128
36129  if (t == error_mark_node
36130      || !parens.require_close (parser))
36131    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36132					   /*or_comma=*/false,
36133					   /*consume_paren=*/true);
36134
36135  check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
36136			     "thread_limit", location);
36137
36138  c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
36139  OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
36140  OMP_CLAUSE_CHAIN (c) = list;
36141
36142  return c;
36143}
36144
36145/* OpenMP 4.0:
36146   aligned ( variable-list )
36147   aligned ( variable-list : constant-expression )  */
36148
36149static tree
36150cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
36151{
36152  tree nlist, c, alignment = NULL_TREE;
36153  bool colon;
36154
36155  matching_parens parens;
36156  if (!parens.require_open (parser))
36157    return list;
36158
36159  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
36160					  &colon);
36161
36162  if (colon)
36163    {
36164      alignment = cp_parser_constant_expression (parser);
36165
36166      if (!parens.require_close (parser))
36167	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36168					       /*or_comma=*/false,
36169					       /*consume_paren=*/true);
36170
36171      if (alignment == error_mark_node)
36172	alignment = NULL_TREE;
36173    }
36174
36175  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36176    OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
36177
36178  return nlist;
36179}
36180
36181/* OpenMP 2.5:
36182   lastprivate ( variable-list )
36183
36184   OpenMP 5.0:
36185   lastprivate ( [ lastprivate-modifier : ] variable-list )  */
36186
36187static tree
36188cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
36189{
36190  bool conditional = false;
36191
36192  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36193    return list;
36194
36195  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36196      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
36197    {
36198      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36199      const char *p = IDENTIFIER_POINTER (id);
36200
36201      if (strcmp ("conditional", p) == 0)
36202	{
36203	  conditional = true;
36204	  cp_lexer_consume_token (parser->lexer);
36205	  cp_lexer_consume_token (parser->lexer);
36206	}
36207    }
36208
36209  tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
36210					       list, NULL);
36211
36212  if (conditional)
36213    for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36214      OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
36215  return nlist;
36216}
36217
36218/* OpenMP 4.0:
36219   linear ( variable-list )
36220   linear ( variable-list : expression )
36221
36222   OpenMP 4.5:
36223   linear ( modifier ( variable-list ) )
36224   linear ( modifier ( variable-list ) : expression ) */
36225
36226static tree
36227cp_parser_omp_clause_linear (cp_parser *parser, tree list,
36228			     bool declare_simd)
36229{
36230  tree nlist, c, step = integer_one_node;
36231  bool colon;
36232  enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
36233
36234  matching_parens parens;
36235  if (!parens.require_open (parser))
36236    return list;
36237
36238  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36239    {
36240      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36241      const char *p = IDENTIFIER_POINTER (id);
36242
36243      if (strcmp ("ref", p) == 0)
36244	kind = OMP_CLAUSE_LINEAR_REF;
36245      else if (strcmp ("val", p) == 0)
36246	kind = OMP_CLAUSE_LINEAR_VAL;
36247      else if (strcmp ("uval", p) == 0)
36248	kind = OMP_CLAUSE_LINEAR_UVAL;
36249      if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
36250	cp_lexer_consume_token (parser->lexer);
36251      else
36252	kind = OMP_CLAUSE_LINEAR_DEFAULT;
36253    }
36254
36255  if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
36256    nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
36257					    &colon);
36258  else
36259    {
36260      nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
36261      colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
36262      if (colon)
36263	cp_parser_require (parser, CPP_COLON, RT_COLON);
36264      else if (!parens.require_close (parser))
36265	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36266					       /*or_comma=*/false,
36267					       /*consume_paren=*/true);
36268    }
36269
36270  if (colon)
36271    {
36272      step = NULL_TREE;
36273      if (declare_simd
36274	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36275	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
36276	{
36277	  cp_token *token = cp_lexer_peek_token (parser->lexer);
36278	  cp_parser_parse_tentatively (parser);
36279	  step = cp_parser_id_expression (parser, /*template_p=*/false,
36280					  /*check_dependency_p=*/true,
36281					  /*template_p=*/NULL,
36282					  /*declarator_p=*/false,
36283					  /*optional_p=*/false);
36284	  if (step != error_mark_node)
36285	    step = cp_parser_lookup_name_simple (parser, step, token->location);
36286	  if (step == error_mark_node)
36287	    {
36288	      step = NULL_TREE;
36289	      cp_parser_abort_tentative_parse (parser);
36290	    }
36291	  else if (!cp_parser_parse_definitely (parser))
36292	    step = NULL_TREE;
36293	}
36294      if (!step)
36295	step = cp_parser_assignment_expression (parser);
36296
36297      if (!parens.require_close (parser))
36298	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36299					       /*or_comma=*/false,
36300					       /*consume_paren=*/true);
36301
36302      if (step == error_mark_node)
36303	return list;
36304    }
36305
36306  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36307    {
36308      OMP_CLAUSE_LINEAR_STEP (c) = step;
36309      OMP_CLAUSE_LINEAR_KIND (c) = kind;
36310    }
36311
36312  return nlist;
36313}
36314
36315/* OpenMP 4.0:
36316   safelen ( constant-expression )  */
36317
36318static tree
36319cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
36320			      location_t location)
36321{
36322  tree t, c;
36323
36324  matching_parens parens;
36325  if (!parens.require_open (parser))
36326    return list;
36327
36328  t = cp_parser_constant_expression (parser);
36329
36330  if (t == error_mark_node
36331      || !parens.require_close (parser))
36332    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36333					   /*or_comma=*/false,
36334					   /*consume_paren=*/true);
36335
36336  check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
36337
36338  c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
36339  OMP_CLAUSE_SAFELEN_EXPR (c) = t;
36340  OMP_CLAUSE_CHAIN (c) = list;
36341
36342  return c;
36343}
36344
36345/* OpenMP 4.0:
36346   simdlen ( constant-expression )  */
36347
36348static tree
36349cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
36350			      location_t location)
36351{
36352  tree t, c;
36353
36354  matching_parens parens;
36355  if (!parens.require_open (parser))
36356    return list;
36357
36358  t = cp_parser_constant_expression (parser);
36359
36360  if (t == error_mark_node
36361      || !parens.require_close (parser))
36362    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36363					   /*or_comma=*/false,
36364					   /*consume_paren=*/true);
36365
36366  check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
36367
36368  c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
36369  OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
36370  OMP_CLAUSE_CHAIN (c) = list;
36371
36372  return c;
36373}
36374
36375/* OpenMP 4.5:
36376   vec:
36377     identifier [+/- integer]
36378     vec , identifier [+/- integer]
36379*/
36380
36381static tree
36382cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
36383				  tree list)
36384{
36385  tree vec = NULL;
36386
36387  if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36388    {
36389      cp_parser_error (parser, "expected identifier");
36390      return list;
36391    }
36392
36393  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36394    {
36395      location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
36396      tree t, identifier = cp_parser_identifier (parser);
36397      tree addend = NULL;
36398
36399      if (identifier == error_mark_node)
36400	t = error_mark_node;
36401      else
36402	{
36403	  t = cp_parser_lookup_name_simple
36404		(parser, identifier,
36405		 cp_lexer_peek_token (parser->lexer)->location);
36406	  if (t == error_mark_node)
36407	    cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
36408					 id_loc);
36409	}
36410
36411      bool neg = false;
36412      if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36413	neg = true;
36414      else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
36415	{
36416	  addend = integer_zero_node;
36417	  goto add_to_vector;
36418	}
36419      cp_lexer_consume_token (parser->lexer);
36420
36421      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
36422	{
36423	  cp_parser_error (parser, "expected integer");
36424	  return list;
36425	}
36426
36427      addend = cp_lexer_peek_token (parser->lexer)->u.value;
36428      if (TREE_CODE (addend) != INTEGER_CST)
36429	{
36430	  cp_parser_error (parser, "expected integer");
36431	  return list;
36432	}
36433      cp_lexer_consume_token (parser->lexer);
36434
36435    add_to_vector:
36436      if (t != error_mark_node)
36437	{
36438	  vec = tree_cons (addend, t, vec);
36439	  if (neg)
36440	    OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
36441	}
36442
36443      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36444	break;
36445
36446      cp_lexer_consume_token (parser->lexer);
36447    }
36448
36449  if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
36450    {
36451      tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
36452      OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
36453      OMP_CLAUSE_DECL (u) = nreverse (vec);
36454      OMP_CLAUSE_CHAIN (u) = list;
36455      return u;
36456    }
36457  return list;
36458}
36459
36460/* OpenMP 5.0:
36461   iterators ( iterators-definition )
36462
36463   iterators-definition:
36464     iterator-specifier
36465     iterator-specifier , iterators-definition
36466
36467   iterator-specifier:
36468     identifier = range-specification
36469     iterator-type identifier = range-specification
36470
36471   range-specification:
36472     begin : end
36473     begin : end : step  */
36474
36475static tree
36476cp_parser_omp_iterators (cp_parser *parser)
36477{
36478  tree ret = NULL_TREE, *last = &ret;
36479  cp_lexer_consume_token (parser->lexer);
36480
36481  matching_parens parens;
36482  if (!parens.require_open (parser))
36483    return error_mark_node;
36484
36485  bool saved_colon_corrects_to_scope_p
36486    = parser->colon_corrects_to_scope_p;
36487  bool saved_colon_doesnt_start_class_def_p
36488    = parser->colon_doesnt_start_class_def_p;
36489
36490  do
36491    {
36492      tree iter_type;
36493      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36494	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
36495	iter_type = integer_type_node;
36496      else
36497	{
36498	  const char *saved_message
36499	    = parser->type_definition_forbidden_message;
36500	  parser->type_definition_forbidden_message
36501	    = G_("types may not be defined in iterator type");
36502
36503	  iter_type = cp_parser_type_id (parser);
36504
36505	  parser->type_definition_forbidden_message = saved_message;
36506	}
36507
36508      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36509      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36510	{
36511	  cp_parser_error (parser, "expected identifier");
36512	  break;
36513	}
36514
36515      tree id = cp_parser_identifier (parser);
36516      if (id == error_mark_node)
36517	break;
36518
36519      if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36520	break;
36521
36522      parser->colon_corrects_to_scope_p = false;
36523      parser->colon_doesnt_start_class_def_p = true;
36524      tree begin = cp_parser_assignment_expression (parser);
36525
36526      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36527	break;
36528
36529      tree end = cp_parser_assignment_expression (parser);
36530
36531      tree step = integer_one_node;
36532      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36533	{
36534	  cp_lexer_consume_token (parser->lexer);
36535	  step = cp_parser_assignment_expression (parser);
36536	}
36537
36538      tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
36539      DECL_ARTIFICIAL (iter_var) = 1;
36540      DECL_CONTEXT (iter_var) = current_function_decl;
36541      pushdecl (iter_var);
36542
36543      *last = make_tree_vec (6);
36544      TREE_VEC_ELT (*last, 0) = iter_var;
36545      TREE_VEC_ELT (*last, 1) = begin;
36546      TREE_VEC_ELT (*last, 2) = end;
36547      TREE_VEC_ELT (*last, 3) = step;
36548      last = &TREE_CHAIN (*last);
36549
36550      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36551	{
36552	  cp_lexer_consume_token (parser->lexer);
36553	  continue;
36554	}
36555      break;
36556    }
36557  while (1);
36558
36559  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36560  parser->colon_doesnt_start_class_def_p
36561    = saved_colon_doesnt_start_class_def_p;
36562
36563  if (!parens.require_close (parser))
36564    cp_parser_skip_to_closing_parenthesis (parser,
36565					   /*recovering=*/true,
36566					   /*or_comma=*/false,
36567					   /*consume_paren=*/true);
36568
36569  return ret ? ret : error_mark_node;
36570}
36571
36572/* OpenMP 4.0:
36573   depend ( depend-kind : variable-list )
36574
36575   depend-kind:
36576     in | out | inout
36577
36578   OpenMP 4.5:
36579   depend ( source )
36580
36581   depend ( sink : vec )
36582
36583   OpenMP 5.0:
36584   depend ( depend-modifier , depend-kind: variable-list )
36585
36586   depend-kind:
36587     in | out | inout | mutexinoutset | depobj
36588
36589   depend-modifier:
36590     iterator ( iterators-definition )  */
36591
36592static tree
36593cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
36594{
36595  tree nlist, c, iterators = NULL_TREE;
36596  enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
36597
36598  matching_parens parens;
36599  if (!parens.require_open (parser))
36600    return list;
36601
36602  do
36603    {
36604      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36605	goto invalid_kind;
36606
36607      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36608      const char *p = IDENTIFIER_POINTER (id);
36609
36610      if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
36611	{
36612	  begin_scope (sk_omp, NULL);
36613	  iterators = cp_parser_omp_iterators (parser);
36614	  cp_parser_require (parser, CPP_COMMA, RT_COMMA);
36615	  continue;
36616	}
36617      if (strcmp ("in", p) == 0)
36618	kind = OMP_CLAUSE_DEPEND_IN;
36619      else if (strcmp ("inout", p) == 0)
36620	kind = OMP_CLAUSE_DEPEND_INOUT;
36621      else if (strcmp ("mutexinoutset", p) == 0)
36622	kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36623      else if (strcmp ("out", p) == 0)
36624	kind = OMP_CLAUSE_DEPEND_OUT;
36625      else if (strcmp ("depobj", p) == 0)
36626	kind = OMP_CLAUSE_DEPEND_DEPOBJ;
36627      else if (strcmp ("sink", p) == 0)
36628	kind = OMP_CLAUSE_DEPEND_SINK;
36629      else if (strcmp ("source", p) == 0)
36630	kind = OMP_CLAUSE_DEPEND_SOURCE;
36631      else
36632	goto invalid_kind;
36633      break;
36634    }
36635  while (1);
36636
36637  cp_lexer_consume_token (parser->lexer);
36638
36639  if (iterators
36640      && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
36641    {
36642      poplevel (0, 1, 0);
36643      error_at (loc, "%<iterator%> modifier incompatible with %qs",
36644		kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
36645      iterators = NULL_TREE;
36646    }
36647
36648  if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36649    {
36650      c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
36651      OMP_CLAUSE_DEPEND_KIND (c) = kind;
36652      OMP_CLAUSE_DECL (c) = NULL_TREE;
36653      OMP_CLAUSE_CHAIN (c) = list;
36654      if (!parens.require_close (parser))
36655	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36656					       /*or_comma=*/false,
36657					       /*consume_paren=*/true);
36658      return c;
36659    }
36660
36661  if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36662    goto resync_fail;
36663
36664  if (kind == OMP_CLAUSE_DEPEND_SINK)
36665    nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
36666  else
36667    {
36668      nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
36669					      list, NULL);
36670
36671      if (iterators)
36672	{
36673	  tree block = poplevel (1, 1, 0);
36674	  if (iterators == error_mark_node)
36675	    iterators = NULL_TREE;
36676	  else
36677	    TREE_VEC_ELT (iterators, 5) = block;
36678	}
36679
36680      for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36681	{
36682	  OMP_CLAUSE_DEPEND_KIND (c) = kind;
36683	  if (iterators)
36684	    OMP_CLAUSE_DECL (c)
36685	      = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
36686	}
36687    }
36688  return nlist;
36689
36690 invalid_kind:
36691  cp_parser_error (parser, "invalid depend kind");
36692 resync_fail:
36693  if (iterators)
36694    poplevel (0, 1, 0);
36695  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36696					 /*or_comma=*/false,
36697					 /*consume_paren=*/true);
36698  return list;
36699}
36700
36701/* OpenMP 4.0:
36702   map ( map-kind : variable-list )
36703   map ( variable-list )
36704
36705   map-kind:
36706     alloc | to | from | tofrom
36707
36708   OpenMP 4.5:
36709   map-kind:
36710     alloc | to | from | tofrom | release | delete
36711
36712   map ( always [,] map-kind: variable-list ) */
36713
36714static tree
36715cp_parser_omp_clause_map (cp_parser *parser, tree list)
36716{
36717  tree nlist, c;
36718  enum gomp_map_kind kind = GOMP_MAP_TOFROM;
36719  bool always = false;
36720
36721  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36722    return list;
36723
36724  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36725    {
36726      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36727      const char *p = IDENTIFIER_POINTER (id);
36728
36729      if (strcmp ("always", p) == 0)
36730	{
36731	  int nth = 2;
36732	  if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
36733	    nth++;
36734	  if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
36735	       || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
36736		   == RID_DELETE))
36737	      && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
36738		  == CPP_COLON))
36739	    {
36740	      always = true;
36741	      cp_lexer_consume_token (parser->lexer);
36742	      if (nth == 3)
36743		cp_lexer_consume_token (parser->lexer);
36744	    }
36745	}
36746    }
36747
36748  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36749      && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
36750    {
36751      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36752      const char *p = IDENTIFIER_POINTER (id);
36753
36754      if (strcmp ("alloc", p) == 0)
36755	kind = GOMP_MAP_ALLOC;
36756      else if (strcmp ("to", p) == 0)
36757	kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
36758      else if (strcmp ("from", p) == 0)
36759	kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
36760      else if (strcmp ("tofrom", p) == 0)
36761	kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
36762      else if (strcmp ("release", p) == 0)
36763	kind = GOMP_MAP_RELEASE;
36764      else
36765	{
36766	  cp_parser_error (parser, "invalid map kind");
36767	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36768						 /*or_comma=*/false,
36769						 /*consume_paren=*/true);
36770	  return list;
36771	}
36772      cp_lexer_consume_token (parser->lexer);
36773      cp_lexer_consume_token (parser->lexer);
36774    }
36775  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
36776	   && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
36777    {
36778      kind = GOMP_MAP_DELETE;
36779      cp_lexer_consume_token (parser->lexer);
36780      cp_lexer_consume_token (parser->lexer);
36781    }
36782
36783  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
36784					  NULL);
36785
36786  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36787    OMP_CLAUSE_SET_MAP_KIND (c, kind);
36788
36789  return nlist;
36790}
36791
36792/* OpenMP 4.0:
36793   device ( expression ) */
36794
36795static tree
36796cp_parser_omp_clause_device (cp_parser *parser, tree list,
36797			     location_t location)
36798{
36799  tree t, c;
36800
36801  matching_parens parens;
36802  if (!parens.require_open (parser))
36803    return list;
36804
36805  t = cp_parser_assignment_expression (parser);
36806
36807  if (t == error_mark_node
36808      || !parens.require_close (parser))
36809    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36810					   /*or_comma=*/false,
36811					   /*consume_paren=*/true);
36812
36813  check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
36814			     "device", location);
36815
36816  c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
36817  OMP_CLAUSE_DEVICE_ID (c) = t;
36818  OMP_CLAUSE_CHAIN (c) = list;
36819
36820  return c;
36821}
36822
36823/* OpenMP 4.0:
36824   dist_schedule ( static )
36825   dist_schedule ( static , expression )  */
36826
36827static tree
36828cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
36829				    location_t location)
36830{
36831  tree c, t;
36832
36833  matching_parens parens;
36834  if (!parens.require_open (parser))
36835    return list;
36836
36837  c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
36838
36839  if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
36840    goto invalid_kind;
36841  cp_lexer_consume_token (parser->lexer);
36842
36843  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36844    {
36845      cp_lexer_consume_token (parser->lexer);
36846
36847      t = cp_parser_assignment_expression (parser);
36848
36849      if (t == error_mark_node)
36850	goto resync_fail;
36851      OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
36852
36853      if (!parens.require_close (parser))
36854	goto resync_fail;
36855    }
36856  else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36857    goto resync_fail;
36858
36859  /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
36860				"dist_schedule", location); */
36861  if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
36862    warning_at (location, 0, "too many %qs clauses", "dist_schedule");
36863  OMP_CLAUSE_CHAIN (c) = list;
36864  return c;
36865
36866 invalid_kind:
36867  cp_parser_error (parser, "invalid dist_schedule kind");
36868 resync_fail:
36869  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36870					 /*or_comma=*/false,
36871					 /*consume_paren=*/true);
36872  return list;
36873}
36874
36875/* OpenMP 4.0:
36876   proc_bind ( proc-bind-kind )
36877
36878   proc-bind-kind:
36879     master | close | spread  */
36880
36881static tree
36882cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
36883				location_t location)
36884{
36885  tree c;
36886  enum omp_clause_proc_bind_kind kind;
36887
36888  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36889    return list;
36890
36891  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36892    {
36893      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36894      const char *p = IDENTIFIER_POINTER (id);
36895
36896      if (strcmp ("master", p) == 0)
36897	kind = OMP_CLAUSE_PROC_BIND_MASTER;
36898      else if (strcmp ("close", p) == 0)
36899	kind = OMP_CLAUSE_PROC_BIND_CLOSE;
36900      else if (strcmp ("spread", p) == 0)
36901	kind = OMP_CLAUSE_PROC_BIND_SPREAD;
36902      else
36903	goto invalid_kind;
36904    }
36905  else
36906    goto invalid_kind;
36907
36908  cp_lexer_consume_token (parser->lexer);
36909  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36910    goto resync_fail;
36911
36912  c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
36913  check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
36914			     location);
36915  OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
36916  OMP_CLAUSE_CHAIN (c) = list;
36917  return c;
36918
36919 invalid_kind:
36920  cp_parser_error (parser, "invalid depend kind");
36921 resync_fail:
36922  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36923					 /*or_comma=*/false,
36924					 /*consume_paren=*/true);
36925  return list;
36926}
36927
36928/* OpenMP 5.0:
36929   device_type ( host | nohost | any )  */
36930
36931static tree
36932cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
36933				  location_t location)
36934{
36935  tree c;
36936  enum omp_clause_device_type_kind kind;
36937
36938  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36939    return list;
36940
36941  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36942    {
36943      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36944      const char *p = IDENTIFIER_POINTER (id);
36945
36946      if (strcmp ("host", p) == 0)
36947	kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
36948      else if (strcmp ("nohost", p) == 0)
36949	kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
36950      else if (strcmp ("any", p) == 0)
36951	kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
36952      else
36953	goto invalid_kind;
36954    }
36955  else
36956    goto invalid_kind;
36957
36958  cp_lexer_consume_token (parser->lexer);
36959  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36960    goto resync_fail;
36961
36962  c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
36963  /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
36964				location);  */
36965  OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
36966  OMP_CLAUSE_CHAIN (c) = list;
36967  return c;
36968
36969 invalid_kind:
36970  cp_parser_error (parser, "invalid depend kind");
36971 resync_fail:
36972  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36973					 /*or_comma=*/false,
36974					 /*consume_paren=*/true);
36975  return list;
36976}
36977
36978/* OpenACC:
36979   async [( int-expr )] */
36980
36981static tree
36982cp_parser_oacc_clause_async (cp_parser *parser, tree list)
36983{
36984  tree c, t;
36985  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36986
36987  t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
36988
36989  if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36990    {
36991      matching_parens parens;
36992      parens.consume_open (parser);
36993
36994      t = cp_parser_assignment_expression (parser);
36995      if (t == error_mark_node
36996	  || !parens.require_close (parser))
36997	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36998						/*or_comma=*/false,
36999						/*consume_paren=*/true);
37000    }
37001
37002  check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
37003
37004  c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
37005  OMP_CLAUSE_ASYNC_EXPR (c) = t;
37006  OMP_CLAUSE_CHAIN (c) = list;
37007  list = c;
37008
37009  return list;
37010}
37011
37012/* Parse all OpenACC clauses.  The set clauses allowed by the directive
37013   is a bitmask in MASK.  Return the list of clauses found.  */
37014
37015static tree
37016cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
37017			    const char *where, cp_token *pragma_tok,
37018			    bool finish_p = true)
37019{
37020  tree clauses = NULL;
37021  bool first = true;
37022
37023  /* Don't create location wrapper nodes within OpenACC clauses.  */
37024  auto_suppress_location_wrappers sentinel;
37025
37026  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37027    {
37028      location_t here;
37029      pragma_omp_clause c_kind;
37030      omp_clause_code code;
37031      const char *c_name;
37032      tree prev = clauses;
37033
37034      if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37035	cp_lexer_consume_token (parser->lexer);
37036
37037      here = cp_lexer_peek_token (parser->lexer)->location;
37038      c_kind = cp_parser_omp_clause_name (parser);
37039
37040      switch (c_kind)
37041	{
37042	case PRAGMA_OACC_CLAUSE_ASYNC:
37043	  clauses = cp_parser_oacc_clause_async (parser, clauses);
37044	  c_name = "async";
37045	  break;
37046	case PRAGMA_OACC_CLAUSE_AUTO:
37047	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
37048						  clauses);
37049	  c_name = "auto";
37050	  break;
37051	case PRAGMA_OACC_CLAUSE_ATTACH:
37052	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37053	  c_name = "attach";
37054	  break;
37055	case PRAGMA_OACC_CLAUSE_COLLAPSE:
37056	  clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
37057	  c_name = "collapse";
37058	  break;
37059	case PRAGMA_OACC_CLAUSE_COPY:
37060	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37061	  c_name = "copy";
37062	  break;
37063	case PRAGMA_OACC_CLAUSE_COPYIN:
37064	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37065	  c_name = "copyin";
37066	  break;
37067	case PRAGMA_OACC_CLAUSE_COPYOUT:
37068	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37069	  c_name = "copyout";
37070	  break;
37071	case PRAGMA_OACC_CLAUSE_CREATE:
37072	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37073	  c_name = "create";
37074	  break;
37075	case PRAGMA_OACC_CLAUSE_DELETE:
37076	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37077	  c_name = "delete";
37078	  break;
37079	case PRAGMA_OMP_CLAUSE_DEFAULT:
37080	  clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
37081	  c_name = "default";
37082	  break;
37083	case PRAGMA_OACC_CLAUSE_DETACH:
37084	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37085	  c_name = "detach";
37086	  break;
37087	case PRAGMA_OACC_CLAUSE_DEVICE:
37088	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37089	  c_name = "device";
37090	  break;
37091	case PRAGMA_OACC_CLAUSE_DEVICEPTR:
37092	  clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
37093	  c_name = "deviceptr";
37094	  break;
37095	case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37096	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37097	  c_name = "device_resident";
37098	  break;
37099	case PRAGMA_OACC_CLAUSE_FINALIZE:
37100	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
37101						  clauses);
37102	  c_name = "finalize";
37103	  break;
37104	case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
37105	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37106					    clauses);
37107	  c_name = "firstprivate";
37108	  break;
37109	case PRAGMA_OACC_CLAUSE_GANG:
37110	  c_name = "gang";
37111	  clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
37112						 c_name, clauses);
37113	  break;
37114	case PRAGMA_OACC_CLAUSE_HOST:
37115	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37116	  c_name = "host";
37117	  break;
37118	case PRAGMA_OACC_CLAUSE_IF:
37119	  clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
37120	  c_name = "if";
37121	  break;
37122	case PRAGMA_OACC_CLAUSE_IF_PRESENT:
37123	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
37124						  clauses);
37125	  c_name = "if_present";
37126	  break;
37127	case PRAGMA_OACC_CLAUSE_INDEPENDENT:
37128	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
37129						  clauses);
37130	  c_name = "independent";
37131	  break;
37132	case PRAGMA_OACC_CLAUSE_LINK:
37133	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37134	  c_name = "link";
37135	  break;
37136	case PRAGMA_OACC_CLAUSE_NO_CREATE:
37137	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37138	  c_name = "no_create";
37139	  break;
37140	case PRAGMA_OACC_CLAUSE_NUM_GANGS:
37141	  code = OMP_CLAUSE_NUM_GANGS;
37142	  c_name = "num_gangs";
37143	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37144						      clauses);
37145	  break;
37146	case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
37147	  c_name = "num_workers";
37148	  code = OMP_CLAUSE_NUM_WORKERS;
37149	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37150						      clauses);
37151	  break;
37152	case PRAGMA_OACC_CLAUSE_PRESENT:
37153	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37154	  c_name = "present";
37155	  break;
37156	case PRAGMA_OACC_CLAUSE_PRIVATE:
37157	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
37158					    clauses);
37159	  c_name = "private";
37160	  break;
37161	case PRAGMA_OACC_CLAUSE_REDUCTION:
37162	  clauses
37163	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
37164					      false, clauses);
37165	  c_name = "reduction";
37166	  break;
37167	case PRAGMA_OACC_CLAUSE_SEQ:
37168	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
37169						  clauses);
37170	  c_name = "seq";
37171	  break;
37172	case PRAGMA_OACC_CLAUSE_TILE:
37173	  clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
37174	  c_name = "tile";
37175	  break;
37176	case PRAGMA_OACC_CLAUSE_USE_DEVICE:
37177	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
37178					    clauses);
37179	  c_name = "use_device";
37180	  break;
37181	case PRAGMA_OACC_CLAUSE_VECTOR:
37182	  c_name = "vector";
37183	  clauses = cp_parser_oacc_shape_clause (parser, here,
37184						 OMP_CLAUSE_VECTOR,
37185						 c_name, clauses);
37186	  break;
37187	case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
37188	  c_name = "vector_length";
37189	  code = OMP_CLAUSE_VECTOR_LENGTH;
37190	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37191						      clauses);
37192	  break;
37193	case PRAGMA_OACC_CLAUSE_WAIT:
37194	  clauses = cp_parser_oacc_clause_wait (parser, clauses);
37195	  c_name = "wait";
37196	  break;
37197	case PRAGMA_OACC_CLAUSE_WORKER:
37198	  c_name = "worker";
37199	  clauses = cp_parser_oacc_shape_clause (parser, here,
37200						 OMP_CLAUSE_WORKER,
37201						 c_name, clauses);
37202	  break;
37203	default:
37204	  cp_parser_error (parser, "expected %<#pragma acc%> clause");
37205	  goto saw_error;
37206	}
37207
37208      first = false;
37209
37210      if (((mask >> c_kind) & 1) == 0)
37211	{
37212	  /* Remove the invalid clause(s) from the list to avoid
37213	     confusing the rest of the compiler.  */
37214	  clauses = prev;
37215	  error_at (here, "%qs is not valid for %qs", c_name, where);
37216	}
37217    }
37218
37219 saw_error:
37220  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37221
37222  if (finish_p)
37223    return finish_omp_clauses (clauses, C_ORT_ACC);
37224
37225  return clauses;
37226}
37227
37228/* Parse all OpenMP clauses.  The set clauses allowed by the directive
37229   is a bitmask in MASK.  Return the list of clauses found.
37230   FINISH_P set if finish_omp_clauses should be called.
37231   NESTED non-zero if clauses should be terminated by closing paren instead
37232   of end of pragma.  If it is 2, additionally commas are required in between
37233   the clauses.  */
37234
37235static tree
37236cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
37237			   const char *where, cp_token *pragma_tok,
37238			   bool finish_p = true, int nested = 0)
37239{
37240  tree clauses = NULL;
37241  bool first = true;
37242  cp_token *token = NULL;
37243
37244  /* Don't create location wrapper nodes within OpenMP clauses.  */
37245  auto_suppress_location_wrappers sentinel;
37246
37247  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37248    {
37249      pragma_omp_clause c_kind;
37250      const char *c_name;
37251      tree prev = clauses;
37252
37253      if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37254	break;
37255
37256      if (!first)
37257	{
37258	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37259	    cp_lexer_consume_token (parser->lexer);
37260	  else if (nested == 2)
37261	    error_at (cp_lexer_peek_token (parser->lexer)->location,
37262		      "clauses in %<simd%> trait should be separated "
37263                      "by %<,%>");
37264	}
37265
37266      token = cp_lexer_peek_token (parser->lexer);
37267      c_kind = cp_parser_omp_clause_name (parser);
37268
37269      switch (c_kind)
37270	{
37271	case PRAGMA_OMP_CLAUSE_BIND:
37272	  clauses = cp_parser_omp_clause_bind (parser, clauses,
37273					       token->location);
37274	  c_name = "bind";
37275	  break;
37276	case PRAGMA_OMP_CLAUSE_COLLAPSE:
37277	  clauses = cp_parser_omp_clause_collapse (parser, clauses,
37278						   token->location);
37279	  c_name = "collapse";
37280	  break;
37281	case PRAGMA_OMP_CLAUSE_COPYIN:
37282	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
37283	  c_name = "copyin";
37284	  break;
37285	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
37286	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
37287					    clauses);
37288	  c_name = "copyprivate";
37289	  break;
37290	case PRAGMA_OMP_CLAUSE_DEFAULT:
37291	  clauses = cp_parser_omp_clause_default (parser, clauses,
37292						  token->location, false);
37293	  c_name = "default";
37294	  break;
37295	case PRAGMA_OMP_CLAUSE_FINAL:
37296	  clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
37297	  c_name = "final";
37298	  break;
37299	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
37300	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37301					    clauses);
37302	  c_name = "firstprivate";
37303	  break;
37304	case PRAGMA_OMP_CLAUSE_GRAINSIZE:
37305	  clauses = cp_parser_omp_clause_grainsize (parser, clauses,
37306						    token->location);
37307	  c_name = "grainsize";
37308	  break;
37309	case PRAGMA_OMP_CLAUSE_HINT:
37310	  clauses = cp_parser_omp_clause_hint (parser, clauses,
37311					       token->location);
37312	  c_name = "hint";
37313	  break;
37314	case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
37315	  clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
37316						     token->location);
37317	  c_name = "defaultmap";
37318	  break;
37319	case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
37320	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
37321					    clauses);
37322	  c_name = "use_device_ptr";
37323	  break;
37324	case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
37325	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
37326					    clauses);
37327	  c_name = "use_device_addr";
37328	  break;
37329	case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
37330	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
37331					    clauses);
37332	  c_name = "is_device_ptr";
37333	  break;
37334	case PRAGMA_OMP_CLAUSE_IF:
37335	  clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
37336					     true);
37337	  c_name = "if";
37338	  break;
37339	case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
37340	  clauses
37341	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
37342					      true, clauses);
37343	  c_name = "in_reduction";
37344	  break;
37345	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
37346	  clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
37347	  c_name = "lastprivate";
37348	  break;
37349	case PRAGMA_OMP_CLAUSE_MERGEABLE:
37350	  clauses = cp_parser_omp_clause_mergeable (parser, clauses,
37351						    token->location);
37352	  c_name = "mergeable";
37353	  break;
37354	case PRAGMA_OMP_CLAUSE_NOWAIT:
37355	  clauses = cp_parser_omp_clause_nowait (parser, clauses,
37356						 token->location);
37357	  c_name = "nowait";
37358	  break;
37359	case PRAGMA_OMP_CLAUSE_NUM_TASKS:
37360	  clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
37361						    token->location);
37362	  c_name = "num_tasks";
37363	  break;
37364	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
37365	  clauses = cp_parser_omp_clause_num_threads (parser, clauses,
37366						      token->location);
37367	  c_name = "num_threads";
37368	  break;
37369	case PRAGMA_OMP_CLAUSE_ORDER:
37370	  clauses = cp_parser_omp_clause_order (parser, clauses,
37371						token->location);
37372	  c_name = "order";
37373	  break;
37374	case PRAGMA_OMP_CLAUSE_ORDERED:
37375	  clauses = cp_parser_omp_clause_ordered (parser, clauses,
37376						  token->location);
37377	  c_name = "ordered";
37378	  break;
37379	case PRAGMA_OMP_CLAUSE_PRIORITY:
37380	  clauses = cp_parser_omp_clause_priority (parser, clauses,
37381						   token->location);
37382	  c_name = "priority";
37383	  break;
37384	case PRAGMA_OMP_CLAUSE_PRIVATE:
37385	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
37386					    clauses);
37387	  c_name = "private";
37388	  break;
37389	case PRAGMA_OMP_CLAUSE_REDUCTION:
37390	  clauses
37391	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
37392					      true, clauses);
37393	  c_name = "reduction";
37394	  break;
37395	case PRAGMA_OMP_CLAUSE_SCHEDULE:
37396	  clauses = cp_parser_omp_clause_schedule (parser, clauses,
37397						   token->location);
37398	  c_name = "schedule";
37399	  break;
37400	case PRAGMA_OMP_CLAUSE_SHARED:
37401	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
37402					    clauses);
37403	  c_name = "shared";
37404	  break;
37405	case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
37406	  clauses
37407	    = cp_parser_omp_clause_reduction (parser,
37408					      OMP_CLAUSE_TASK_REDUCTION,
37409					      true, clauses);
37410	  c_name = "task_reduction";
37411	  break;
37412	case PRAGMA_OMP_CLAUSE_UNTIED:
37413	  clauses = cp_parser_omp_clause_untied (parser, clauses,
37414						 token->location);
37415	  c_name = "untied";
37416	  break;
37417	case PRAGMA_OMP_CLAUSE_INBRANCH:
37418	  clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
37419						 clauses, token->location);
37420	  c_name = "inbranch";
37421	  break;
37422	case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
37423	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
37424					    clauses);
37425	  c_name = "nontemporal";
37426	  break;
37427	case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
37428	  clauses = cp_parser_omp_clause_branch (parser,
37429						 OMP_CLAUSE_NOTINBRANCH,
37430						 clauses, token->location);
37431	  c_name = "notinbranch";
37432	  break;
37433	case PRAGMA_OMP_CLAUSE_PARALLEL:
37434	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
37435						     clauses, token->location);
37436	  c_name = "parallel";
37437	  if (!first)
37438	    {
37439	     clause_not_first:
37440	      error_at (token->location, "%qs must be the first clause of %qs",
37441			c_name, where);
37442	      clauses = prev;
37443	    }
37444	  break;
37445	case PRAGMA_OMP_CLAUSE_FOR:
37446	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
37447						     clauses, token->location);
37448	  c_name = "for";
37449	  if (!first)
37450	    goto clause_not_first;
37451	  break;
37452	case PRAGMA_OMP_CLAUSE_SECTIONS:
37453	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
37454						     clauses, token->location);
37455	  c_name = "sections";
37456	  if (!first)
37457	    goto clause_not_first;
37458	  break;
37459	case PRAGMA_OMP_CLAUSE_TASKGROUP:
37460	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
37461						     clauses, token->location);
37462	  c_name = "taskgroup";
37463	  if (!first)
37464	    goto clause_not_first;
37465	  break;
37466	case PRAGMA_OMP_CLAUSE_LINK:
37467	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
37468	  c_name = "to";
37469	  break;
37470	case PRAGMA_OMP_CLAUSE_TO:
37471	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
37472	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37473					      clauses);
37474	  else
37475	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
37476	  c_name = "to";
37477	  break;
37478	case PRAGMA_OMP_CLAUSE_FROM:
37479	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
37480	  c_name = "from";
37481	  break;
37482	case PRAGMA_OMP_CLAUSE_UNIFORM:
37483	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
37484					    clauses);
37485	  c_name = "uniform";
37486	  break;
37487	case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
37488	  clauses = cp_parser_omp_clause_num_teams (parser, clauses,
37489						    token->location);
37490	  c_name = "num_teams";
37491	  break;
37492	case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
37493	  clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
37494						       token->location);
37495	  c_name = "thread_limit";
37496	  break;
37497	case PRAGMA_OMP_CLAUSE_ALIGNED:
37498	  clauses = cp_parser_omp_clause_aligned (parser, clauses);
37499	  c_name = "aligned";
37500	  break;
37501	case PRAGMA_OMP_CLAUSE_LINEAR:
37502	  {
37503	    bool declare_simd = false;
37504	    if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
37505	      declare_simd = true;
37506	    clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
37507	  }
37508	  c_name = "linear";
37509	  break;
37510	case PRAGMA_OMP_CLAUSE_DEPEND:
37511	  clauses = cp_parser_omp_clause_depend (parser, clauses,
37512						 token->location);
37513	  c_name = "depend";
37514	  break;
37515	case PRAGMA_OMP_CLAUSE_MAP:
37516	  clauses = cp_parser_omp_clause_map (parser, clauses);
37517	  c_name = "map";
37518	  break;
37519	case PRAGMA_OMP_CLAUSE_DEVICE:
37520	  clauses = cp_parser_omp_clause_device (parser, clauses,
37521						 token->location);
37522	  c_name = "device";
37523	  break;
37524	case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
37525	  clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
37526							token->location);
37527	  c_name = "dist_schedule";
37528	  break;
37529	case PRAGMA_OMP_CLAUSE_PROC_BIND:
37530	  clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
37531						    token->location);
37532	  c_name = "proc_bind";
37533	  break;
37534	case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
37535	  clauses = cp_parser_omp_clause_device_type (parser, clauses,
37536						      token->location);
37537	  c_name = "device_type";
37538	  break;
37539	case PRAGMA_OMP_CLAUSE_SAFELEN:
37540	  clauses = cp_parser_omp_clause_safelen (parser, clauses,
37541						  token->location);
37542	  c_name = "safelen";
37543	  break;
37544	case PRAGMA_OMP_CLAUSE_SIMDLEN:
37545	  clauses = cp_parser_omp_clause_simdlen (parser, clauses,
37546						  token->location);
37547	  c_name = "simdlen";
37548	  break;
37549	case PRAGMA_OMP_CLAUSE_NOGROUP:
37550	  clauses = cp_parser_omp_clause_nogroup (parser, clauses,
37551						  token->location);
37552	  c_name = "nogroup";
37553	  break;
37554	case PRAGMA_OMP_CLAUSE_THREADS:
37555	  clauses
37556	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
37557						clauses, token->location);
37558	  c_name = "threads";
37559	  break;
37560	case PRAGMA_OMP_CLAUSE_SIMD:
37561	  clauses
37562	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
37563						clauses, token->location);
37564	  c_name = "simd";
37565	  break;
37566	default:
37567	  cp_parser_error (parser, "expected %<#pragma omp%> clause");
37568	  goto saw_error;
37569	}
37570
37571      first = false;
37572
37573      if (((mask >> c_kind) & 1) == 0)
37574	{
37575	  /* Remove the invalid clause(s) from the list to avoid
37576	     confusing the rest of the compiler.  */
37577	  clauses = prev;
37578	  error_at (token->location, "%qs is not valid for %qs", c_name, where);
37579	}
37580    }
37581 saw_error:
37582  if (!nested)
37583    cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37584  if (finish_p)
37585    {
37586      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
37587	return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
37588      else
37589	return finish_omp_clauses (clauses, C_ORT_OMP);
37590    }
37591  return clauses;
37592}
37593
37594/* OpenMP 2.5:
37595   structured-block:
37596     statement
37597
37598   In practice, we're also interested in adding the statement to an
37599   outer node.  So it is convenient if we work around the fact that
37600   cp_parser_statement calls add_stmt.  */
37601
37602static unsigned
37603cp_parser_begin_omp_structured_block (cp_parser *parser)
37604{
37605  unsigned save = parser->in_statement;
37606
37607  /* Only move the values to IN_OMP_BLOCK if they weren't false.
37608     This preserves the "not within loop or switch" style error messages
37609     for nonsense cases like
37610	void foo() {
37611	#pragma omp single
37612	  break;
37613	}
37614  */
37615  if (parser->in_statement)
37616    parser->in_statement = IN_OMP_BLOCK;
37617
37618  return save;
37619}
37620
37621static void
37622cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
37623{
37624  parser->in_statement = save;
37625}
37626
37627static tree
37628cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
37629{
37630  tree stmt = begin_omp_structured_block ();
37631  unsigned int save = cp_parser_begin_omp_structured_block (parser);
37632
37633  cp_parser_statement (parser, NULL_TREE, false, if_p);
37634
37635  cp_parser_end_omp_structured_block (parser, save);
37636  return finish_omp_structured_block (stmt);
37637}
37638
37639/* OpenMP 2.5:
37640   # pragma omp atomic new-line
37641     expression-stmt
37642
37643   expression-stmt:
37644     x binop= expr | x++ | ++x | x-- | --x
37645   binop:
37646     +, *, -, /, &, ^, |, <<, >>
37647
37648  where x is an lvalue expression with scalar type.
37649
37650   OpenMP 3.1:
37651   # pragma omp atomic new-line
37652     update-stmt
37653
37654   # pragma omp atomic read new-line
37655     read-stmt
37656
37657   # pragma omp atomic write new-line
37658     write-stmt
37659
37660   # pragma omp atomic update new-line
37661     update-stmt
37662
37663   # pragma omp atomic capture new-line
37664     capture-stmt
37665
37666   # pragma omp atomic capture new-line
37667     capture-block
37668
37669   read-stmt:
37670     v = x
37671   write-stmt:
37672     x = expr
37673   update-stmt:
37674     expression-stmt | x = x binop expr
37675   capture-stmt:
37676     v = expression-stmt
37677   capture-block:
37678     { v = x; update-stmt; } | { update-stmt; v = x; }
37679
37680   OpenMP 4.0:
37681   update-stmt:
37682     expression-stmt | x = x binop expr | x = expr binop x
37683   capture-stmt:
37684     v = update-stmt
37685   capture-block:
37686     { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
37687
37688  where x and v are lvalue expressions with scalar type.  */
37689
37690static void
37691cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
37692{
37693  tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
37694  tree rhs1 = NULL_TREE, orig_lhs;
37695  location_t loc = pragma_tok->location;
37696  enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
37697  enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
37698  bool structured_block = false;
37699  bool first = true;
37700  tree clauses = NULL_TREE;
37701
37702  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37703    {
37704      if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37705	cp_lexer_consume_token (parser->lexer);
37706
37707      first = false;
37708
37709      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37710	{
37711	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37712	  location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
37713	  const char *p = IDENTIFIER_POINTER (id);
37714	  enum tree_code new_code = ERROR_MARK;
37715	  enum omp_memory_order new_memory_order
37716	    = OMP_MEMORY_ORDER_UNSPECIFIED;
37717
37718	  if (!strcmp (p, "read"))
37719	    new_code = OMP_ATOMIC_READ;
37720	  else if (!strcmp (p, "write"))
37721	    new_code = NOP_EXPR;
37722	  else if (!strcmp (p, "update"))
37723	    new_code = OMP_ATOMIC;
37724	  else if (!strcmp (p, "capture"))
37725	    new_code = OMP_ATOMIC_CAPTURE_NEW;
37726	  else if (!strcmp (p, "seq_cst"))
37727	    new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37728	  else if (!strcmp (p, "acq_rel"))
37729	    new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
37730	  else if (!strcmp (p, "release"))
37731	    new_memory_order = OMP_MEMORY_ORDER_RELEASE;
37732	  else if (!strcmp (p, "acquire"))
37733	    new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
37734	  else if (!strcmp (p, "relaxed"))
37735	    new_memory_order = OMP_MEMORY_ORDER_RELAXED;
37736	  else if (!strcmp (p, "hint"))
37737	    {
37738	      cp_lexer_consume_token (parser->lexer);
37739	      clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
37740	      continue;
37741	    }
37742	  else
37743	    {
37744	      p = NULL;
37745	      error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
37746			      "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
37747			      "%<release%>, %<relaxed%> or %<hint%> clause");
37748	    }
37749	  if (p)
37750	    {
37751	      if (new_code != ERROR_MARK)
37752		{
37753		  if (code != ERROR_MARK)
37754		    error_at (cloc, "too many atomic clauses");
37755		  else
37756		    code = new_code;
37757		}
37758	      else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
37759		{
37760		  if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
37761		    error_at (cloc, "too many memory order clauses");
37762		  else
37763		    memory_order = new_memory_order;
37764		}
37765	      cp_lexer_consume_token (parser->lexer);
37766	      continue;
37767	    }
37768	}
37769      break;
37770    }
37771  cp_parser_require_pragma_eol (parser, pragma_tok);
37772
37773  if (code == ERROR_MARK)
37774    code = OMP_ATOMIC;
37775  if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
37776    {
37777      omp_requires_mask
37778	= (enum omp_requires) (omp_requires_mask
37779			       | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
37780      switch ((enum omp_memory_order)
37781	      (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
37782	{
37783	case OMP_MEMORY_ORDER_UNSPECIFIED:
37784	case OMP_MEMORY_ORDER_RELAXED:
37785	  memory_order = OMP_MEMORY_ORDER_RELAXED;
37786	  break;
37787	case OMP_MEMORY_ORDER_SEQ_CST:
37788	  memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37789	  break;
37790	case OMP_MEMORY_ORDER_ACQ_REL:
37791	  switch (code)
37792	    {
37793	    case OMP_ATOMIC_READ:
37794	      memory_order = OMP_MEMORY_ORDER_ACQUIRE;
37795	      break;
37796	    case NOP_EXPR: /* atomic write */
37797	    case OMP_ATOMIC:
37798	      memory_order = OMP_MEMORY_ORDER_RELEASE;
37799	      break;
37800	    default:
37801	      memory_order = OMP_MEMORY_ORDER_ACQ_REL;
37802	      break;
37803	    }
37804	  break;
37805	default:
37806	  gcc_unreachable ();
37807	}
37808    }
37809  else
37810    switch (code)
37811      {
37812      case OMP_ATOMIC_READ:
37813	if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
37814	    || memory_order == OMP_MEMORY_ORDER_RELEASE)
37815	  {
37816	    error_at (loc, "%<#pragma omp atomic read%> incompatible with "
37817			   "%<acq_rel%> or %<release%> clauses");
37818	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37819	  }
37820	break;
37821      case NOP_EXPR: /* atomic write */
37822	if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
37823	    || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
37824	  {
37825	    error_at (loc, "%<#pragma omp atomic write%> incompatible with "
37826			   "%<acq_rel%> or %<acquire%> clauses");
37827	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37828	  }
37829	break;
37830      case OMP_ATOMIC:
37831	if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
37832	    || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
37833	  {
37834	    error_at (loc, "%<#pragma omp atomic update%> incompatible with "
37835			   "%<acq_rel%> or %<acquire%> clauses");
37836	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37837	  }
37838	break;
37839      default:
37840	break;
37841      }
37842
37843  switch (code)
37844    {
37845    case OMP_ATOMIC_READ:
37846    case NOP_EXPR: /* atomic write */
37847      v = cp_parser_unary_expression (parser);
37848      if (v == error_mark_node)
37849	goto saw_error;
37850      if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37851	goto saw_error;
37852      if (code == NOP_EXPR)
37853	lhs = cp_parser_expression (parser);
37854      else
37855	lhs = cp_parser_unary_expression (parser);
37856      if (lhs == error_mark_node)
37857	goto saw_error;
37858      if (code == NOP_EXPR)
37859	{
37860	  /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
37861	     opcode.  */
37862	  code = OMP_ATOMIC;
37863	  rhs = lhs;
37864	  lhs = v;
37865	  v = NULL_TREE;
37866	}
37867      goto done;
37868    case OMP_ATOMIC_CAPTURE_NEW:
37869      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37870	{
37871	  cp_lexer_consume_token (parser->lexer);
37872	  structured_block = true;
37873	}
37874      else
37875	{
37876	  v = cp_parser_unary_expression (parser);
37877	  if (v == error_mark_node)
37878	    goto saw_error;
37879	  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37880	    goto saw_error;
37881	}
37882    default:
37883      break;
37884    }
37885
37886restart:
37887  lhs = cp_parser_unary_expression (parser);
37888  orig_lhs = lhs;
37889  switch (TREE_CODE (lhs))
37890    {
37891    case ERROR_MARK:
37892      goto saw_error;
37893
37894    case POSTINCREMENT_EXPR:
37895      if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
37896	code = OMP_ATOMIC_CAPTURE_OLD;
37897      /* FALLTHROUGH */
37898    case PREINCREMENT_EXPR:
37899      lhs = TREE_OPERAND (lhs, 0);
37900      opcode = PLUS_EXPR;
37901      rhs = integer_one_node;
37902      break;
37903
37904    case POSTDECREMENT_EXPR:
37905      if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
37906	code = OMP_ATOMIC_CAPTURE_OLD;
37907      /* FALLTHROUGH */
37908    case PREDECREMENT_EXPR:
37909      lhs = TREE_OPERAND (lhs, 0);
37910      opcode = MINUS_EXPR;
37911      rhs = integer_one_node;
37912      break;
37913
37914    case COMPOUND_EXPR:
37915      if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
37916	 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
37917	 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
37918	 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
37919	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
37920					     (TREE_OPERAND (lhs, 1), 0), 0)))
37921	    == BOOLEAN_TYPE)
37922       /* Undo effects of boolean_increment for post {in,de}crement.  */
37923       lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
37924      /* FALLTHRU */
37925    case MODIFY_EXPR:
37926      if (TREE_CODE (lhs) == MODIFY_EXPR
37927	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
37928	{
37929	  /* Undo effects of boolean_increment.  */
37930	  if (integer_onep (TREE_OPERAND (lhs, 1)))
37931	    {
37932	      /* This is pre or post increment.  */
37933	      rhs = TREE_OPERAND (lhs, 1);
37934	      lhs = TREE_OPERAND (lhs, 0);
37935	      opcode = NOP_EXPR;
37936	      if (code == OMP_ATOMIC_CAPTURE_NEW
37937		  && !structured_block
37938		  && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
37939		code = OMP_ATOMIC_CAPTURE_OLD;
37940	      break;
37941	    }
37942	}
37943      /* FALLTHRU */
37944    default:
37945      switch (cp_lexer_peek_token (parser->lexer)->type)
37946	{
37947	case CPP_MULT_EQ:
37948	  opcode = MULT_EXPR;
37949	  break;
37950	case CPP_DIV_EQ:
37951	  opcode = TRUNC_DIV_EXPR;
37952	  break;
37953	case CPP_PLUS_EQ:
37954	  opcode = PLUS_EXPR;
37955	  break;
37956	case CPP_MINUS_EQ:
37957	  opcode = MINUS_EXPR;
37958	  break;
37959	case CPP_LSHIFT_EQ:
37960	  opcode = LSHIFT_EXPR;
37961	  break;
37962	case CPP_RSHIFT_EQ:
37963	  opcode = RSHIFT_EXPR;
37964	  break;
37965	case CPP_AND_EQ:
37966	  opcode = BIT_AND_EXPR;
37967	  break;
37968	case CPP_OR_EQ:
37969	  opcode = BIT_IOR_EXPR;
37970	  break;
37971	case CPP_XOR_EQ:
37972	  opcode = BIT_XOR_EXPR;
37973	  break;
37974	case CPP_EQ:
37975	  enum cp_parser_prec oprec;
37976	  cp_token *token;
37977	  cp_lexer_consume_token (parser->lexer);
37978	  cp_parser_parse_tentatively (parser);
37979	  rhs1 = cp_parser_simple_cast_expression (parser);
37980	  if (rhs1 == error_mark_node)
37981	    {
37982	      cp_parser_abort_tentative_parse (parser);
37983	      cp_parser_simple_cast_expression (parser);
37984	      goto saw_error;
37985	    }
37986	  token = cp_lexer_peek_token (parser->lexer);
37987	  if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
37988	    {
37989	      cp_parser_abort_tentative_parse (parser);
37990	      cp_parser_parse_tentatively (parser);
37991	      rhs = cp_parser_binary_expression (parser, false, true,
37992						 PREC_NOT_OPERATOR, NULL);
37993	      if (rhs == error_mark_node)
37994		{
37995		  cp_parser_abort_tentative_parse (parser);
37996		  cp_parser_binary_expression (parser, false, true,
37997					       PREC_NOT_OPERATOR, NULL);
37998		  goto saw_error;
37999		}
38000	      switch (TREE_CODE (rhs))
38001		{
38002		case MULT_EXPR:
38003		case TRUNC_DIV_EXPR:
38004		case RDIV_EXPR:
38005		case PLUS_EXPR:
38006		case MINUS_EXPR:
38007		case LSHIFT_EXPR:
38008		case RSHIFT_EXPR:
38009		case BIT_AND_EXPR:
38010		case BIT_IOR_EXPR:
38011		case BIT_XOR_EXPR:
38012		  if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
38013		    {
38014		      if (cp_parser_parse_definitely (parser))
38015			{
38016			  opcode = TREE_CODE (rhs);
38017			  rhs1 = TREE_OPERAND (rhs, 0);
38018			  rhs = TREE_OPERAND (rhs, 1);
38019			  goto stmt_done;
38020			}
38021		      else
38022			goto saw_error;
38023		    }
38024		  break;
38025		default:
38026		  break;
38027		}
38028	      cp_parser_abort_tentative_parse (parser);
38029	      if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
38030		{
38031		  rhs = cp_parser_expression (parser);
38032		  if (rhs == error_mark_node)
38033		    goto saw_error;
38034		  opcode = NOP_EXPR;
38035		  rhs1 = NULL_TREE;
38036		  goto stmt_done;
38037		}
38038	      cp_parser_error (parser,
38039			       "invalid form of %<#pragma omp atomic%>");
38040	      goto saw_error;
38041	    }
38042	  if (!cp_parser_parse_definitely (parser))
38043	    goto saw_error;
38044	  switch (token->type)
38045	    {
38046	    case CPP_SEMICOLON:
38047	      if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
38048		{
38049		  code = OMP_ATOMIC_CAPTURE_OLD;
38050		  v = lhs;
38051		  lhs = NULL_TREE;
38052		  lhs1 = rhs1;
38053		  rhs1 = NULL_TREE;
38054		  cp_lexer_consume_token (parser->lexer);
38055		  goto restart;
38056		}
38057	      else if (structured_block)
38058		{
38059		  opcode = NOP_EXPR;
38060		  rhs = rhs1;
38061		  rhs1 = NULL_TREE;
38062		  goto stmt_done;
38063		}
38064	      cp_parser_error (parser,
38065			       "invalid form of %<#pragma omp atomic%>");
38066	      goto saw_error;
38067	    case CPP_MULT:
38068	      opcode = MULT_EXPR;
38069	      break;
38070	    case CPP_DIV:
38071	      opcode = TRUNC_DIV_EXPR;
38072	      break;
38073	    case CPP_PLUS:
38074	      opcode = PLUS_EXPR;
38075	      break;
38076	    case CPP_MINUS:
38077	      opcode = MINUS_EXPR;
38078	      break;
38079	    case CPP_LSHIFT:
38080	      opcode = LSHIFT_EXPR;
38081	      break;
38082	    case CPP_RSHIFT:
38083	      opcode = RSHIFT_EXPR;
38084	      break;
38085	    case CPP_AND:
38086	      opcode = BIT_AND_EXPR;
38087	      break;
38088	    case CPP_OR:
38089	      opcode = BIT_IOR_EXPR;
38090	      break;
38091	    case CPP_XOR:
38092	      opcode = BIT_XOR_EXPR;
38093	      break;
38094	    default:
38095	      cp_parser_error (parser,
38096			       "invalid operator for %<#pragma omp atomic%>");
38097	      goto saw_error;
38098	    }
38099	  oprec = TOKEN_PRECEDENCE (token);
38100	  gcc_assert (oprec != PREC_NOT_OPERATOR);
38101	  if (commutative_tree_code (opcode))
38102	    oprec = (enum cp_parser_prec) (oprec - 1);
38103	  cp_lexer_consume_token (parser->lexer);
38104	  rhs = cp_parser_binary_expression (parser, false, false,
38105					     oprec, NULL);
38106	  if (rhs == error_mark_node)
38107	    goto saw_error;
38108	  goto stmt_done;
38109	  /* FALLTHROUGH */
38110	default:
38111	  cp_parser_error (parser,
38112			   "invalid operator for %<#pragma omp atomic%>");
38113	  goto saw_error;
38114	}
38115      cp_lexer_consume_token (parser->lexer);
38116
38117      rhs = cp_parser_expression (parser);
38118      if (rhs == error_mark_node)
38119	goto saw_error;
38120      break;
38121    }
38122stmt_done:
38123  if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
38124    {
38125      if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
38126	goto saw_error;
38127      v = cp_parser_unary_expression (parser);
38128      if (v == error_mark_node)
38129	goto saw_error;
38130      if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38131	goto saw_error;
38132      lhs1 = cp_parser_unary_expression (parser);
38133      if (lhs1 == error_mark_node)
38134	goto saw_error;
38135    }
38136  if (structured_block)
38137    {
38138      cp_parser_consume_semicolon_at_end_of_statement (parser);
38139      cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
38140    }
38141done:
38142  clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38143  finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
38144		     rhs1, clauses, memory_order);
38145  if (!structured_block)
38146    cp_parser_consume_semicolon_at_end_of_statement (parser);
38147  return;
38148
38149 saw_error:
38150  cp_parser_skip_to_end_of_block_or_statement (parser);
38151  if (structured_block)
38152    {
38153      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38154        cp_lexer_consume_token (parser->lexer);
38155      else if (code == OMP_ATOMIC_CAPTURE_NEW)
38156	{
38157	  cp_parser_skip_to_end_of_block_or_statement (parser);
38158	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38159	    cp_lexer_consume_token (parser->lexer);
38160	}
38161    }
38162}
38163
38164
38165/* OpenMP 2.5:
38166   # pragma omp barrier new-line  */
38167
38168static void
38169cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
38170{
38171  cp_parser_require_pragma_eol (parser, pragma_tok);
38172  finish_omp_barrier ();
38173}
38174
38175/* OpenMP 2.5:
38176   # pragma omp critical [(name)] new-line
38177     structured-block
38178
38179   OpenMP 4.5:
38180   # pragma omp critical [(name) [hint(expression)]] new-line
38181     structured-block  */
38182
38183#define OMP_CRITICAL_CLAUSE_MASK		\
38184	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
38185
38186static tree
38187cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38188{
38189  tree stmt, name = NULL_TREE, clauses = NULL_TREE;
38190
38191  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38192    {
38193      matching_parens parens;
38194      parens.consume_open (parser);
38195
38196      name = cp_parser_identifier (parser);
38197
38198      if (name == error_mark_node
38199	  || !parens.require_close (parser))
38200	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38201					       /*or_comma=*/false,
38202					       /*consume_paren=*/true);
38203      if (name == error_mark_node)
38204	name = NULL;
38205
38206      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
38207	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38208	cp_lexer_consume_token (parser->lexer);
38209
38210      clauses = cp_parser_omp_all_clauses (parser,
38211					   OMP_CRITICAL_CLAUSE_MASK,
38212					   "#pragma omp critical", pragma_tok);
38213    }
38214  else
38215    cp_parser_require_pragma_eol (parser, pragma_tok);
38216
38217  stmt = cp_parser_omp_structured_block (parser, if_p);
38218  return c_finish_omp_critical (input_location, stmt, name, clauses);
38219}
38220
38221/* OpenMP 5.0:
38222   # pragma omp depobj ( depobj ) depobj-clause new-line
38223
38224   depobj-clause:
38225     depend (dependence-type : locator)
38226     destroy
38227     update (dependence-type)
38228
38229   dependence-type:
38230     in
38231     out
38232     inout
38233     mutexinout  */
38234
38235static void
38236cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
38237{
38238  location_t loc = pragma_tok->location;
38239  matching_parens parens;
38240  if (!parens.require_open (parser))
38241    {
38242      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38243      return;
38244    }
38245
38246  tree depobj = cp_parser_assignment_expression (parser);
38247
38248  if (!parens.require_close (parser))
38249    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38250					   /*or_comma=*/false,
38251					   /*consume_paren=*/true);
38252
38253  tree clause = NULL_TREE;
38254  enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
38255  location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
38256  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38257    {
38258      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38259      const char *p = IDENTIFIER_POINTER (id);
38260
38261      cp_lexer_consume_token (parser->lexer);
38262      if (!strcmp ("depend", p))
38263	{
38264	  /* Don't create location wrapper nodes within the depend clause.  */
38265	  auto_suppress_location_wrappers sentinel;
38266	  clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
38267	  if (clause)
38268	    clause = finish_omp_clauses (clause, C_ORT_OMP);
38269	  if (!clause)
38270	    clause = error_mark_node;
38271	}
38272      else if (!strcmp ("destroy", p))
38273	kind = OMP_CLAUSE_DEPEND_LAST;
38274      else if (!strcmp ("update", p))
38275	{
38276	  matching_parens c_parens;
38277	  if (c_parens.require_open (parser))
38278	    {
38279	      location_t c2_loc
38280		= cp_lexer_peek_token (parser->lexer)->location;
38281	      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38282		{
38283		  tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
38284		  const char *p2 = IDENTIFIER_POINTER (id2);
38285
38286		  cp_lexer_consume_token (parser->lexer);
38287		  if (!strcmp ("in", p2))
38288		    kind = OMP_CLAUSE_DEPEND_IN;
38289		  else if (!strcmp ("out", p2))
38290		    kind = OMP_CLAUSE_DEPEND_OUT;
38291		  else if (!strcmp ("inout", p2))
38292		    kind = OMP_CLAUSE_DEPEND_INOUT;
38293		  else if (!strcmp ("mutexinoutset", p2))
38294		    kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
38295		}
38296	      if (kind == OMP_CLAUSE_DEPEND_SOURCE)
38297		{
38298		  clause = error_mark_node;
38299		  error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
38300				    "%<mutexinoutset%>");
38301		}
38302	      if (!c_parens.require_close (parser))
38303		cp_parser_skip_to_closing_parenthesis (parser,
38304						       /*recovering=*/true,
38305						       /*or_comma=*/false,
38306						       /*consume_paren=*/true);
38307	    }
38308	  else
38309	    clause = error_mark_node;
38310	}
38311    }
38312  if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
38313    {
38314      clause = error_mark_node;
38315      error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
38316    }
38317  cp_parser_require_pragma_eol (parser, pragma_tok);
38318
38319  finish_omp_depobj (loc, depobj, kind, clause);
38320}
38321
38322
38323/* OpenMP 2.5:
38324   # pragma omp flush flush-vars[opt] new-line
38325
38326   flush-vars:
38327     ( variable-list )
38328
38329   OpenMP 5.0:
38330   # pragma omp flush memory-order-clause new-line  */
38331
38332static void
38333cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
38334{
38335  enum memmodel mo = MEMMODEL_LAST;
38336  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38337    {
38338      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38339      const char *p = IDENTIFIER_POINTER (id);
38340      if (!strcmp (p, "acq_rel"))
38341	mo = MEMMODEL_ACQ_REL;
38342      else if (!strcmp (p, "release"))
38343	mo = MEMMODEL_RELEASE;
38344      else if (!strcmp (p, "acquire"))
38345	mo = MEMMODEL_ACQUIRE;
38346      else
38347	error_at (cp_lexer_peek_token (parser->lexer)->location,
38348		  "expected %<acq_rel%>, %<release%> or %<acquire%>");
38349      cp_lexer_consume_token (parser->lexer);
38350    }
38351  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38352    {
38353      if (mo != MEMMODEL_LAST)
38354	error_at (cp_lexer_peek_token (parser->lexer)->location,
38355		  "%<flush%> list specified together with memory order "
38356		  "clause");
38357      (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38358    }
38359  cp_parser_require_pragma_eol (parser, pragma_tok);
38360
38361  finish_omp_flush (mo);
38362}
38363
38364/* Helper function, to parse omp for increment expression.  */
38365
38366static tree
38367cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
38368{
38369  tree cond = cp_parser_binary_expression (parser, false, true,
38370					   PREC_NOT_OPERATOR, NULL);
38371  if (cond == error_mark_node
38372      || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
38373    {
38374      cp_parser_skip_to_end_of_statement (parser);
38375      return error_mark_node;
38376    }
38377
38378  switch (TREE_CODE (cond))
38379    {
38380    case GT_EXPR:
38381    case GE_EXPR:
38382    case LT_EXPR:
38383    case LE_EXPR:
38384      break;
38385    case NE_EXPR:
38386      if (code != OACC_LOOP)
38387	break;
38388      gcc_fallthrough ();
38389    default:
38390      return error_mark_node;
38391    }
38392
38393  /* If decl is an iterator, preserve LHS and RHS of the relational
38394     expr until finish_omp_for.  */
38395  if (decl
38396      && (type_dependent_expression_p (decl)
38397	  || CLASS_TYPE_P (TREE_TYPE (decl))))
38398    return cond;
38399
38400  return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
38401			    TREE_CODE (cond),
38402			    TREE_OPERAND (cond, 0), ERROR_MARK,
38403			    TREE_OPERAND (cond, 1), ERROR_MARK,
38404			    /*overload=*/NULL, tf_warning_or_error);
38405}
38406
38407/* Helper function, to parse omp for increment expression.  */
38408
38409static tree
38410cp_parser_omp_for_incr (cp_parser *parser, tree decl)
38411{
38412  cp_token *token = cp_lexer_peek_token (parser->lexer);
38413  enum tree_code op;
38414  tree lhs, rhs;
38415  cp_id_kind idk;
38416  bool decl_first;
38417
38418  if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
38419    {
38420      op = (token->type == CPP_PLUS_PLUS
38421	    ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
38422      cp_lexer_consume_token (parser->lexer);
38423      lhs = cp_parser_simple_cast_expression (parser);
38424      if (lhs != decl
38425	  && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
38426	return error_mark_node;
38427      return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
38428    }
38429
38430  lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
38431  if (lhs != decl
38432      && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
38433    return error_mark_node;
38434
38435  token = cp_lexer_peek_token (parser->lexer);
38436  if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
38437    {
38438      op = (token->type == CPP_PLUS_PLUS
38439	    ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
38440      cp_lexer_consume_token (parser->lexer);
38441      return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
38442    }
38443
38444  op = cp_parser_assignment_operator_opt (parser);
38445  if (op == ERROR_MARK)
38446    return error_mark_node;
38447
38448  if (op != NOP_EXPR)
38449    {
38450      rhs = cp_parser_assignment_expression (parser);
38451      rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
38452      return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
38453    }
38454
38455  lhs = cp_parser_binary_expression (parser, false, false,
38456				     PREC_ADDITIVE_EXPRESSION, NULL);
38457  token = cp_lexer_peek_token (parser->lexer);
38458  decl_first = (lhs == decl
38459		|| (processing_template_decl && cp_tree_equal (lhs, decl)));
38460  if (decl_first)
38461    lhs = NULL_TREE;
38462  if (token->type != CPP_PLUS
38463      && token->type != CPP_MINUS)
38464    return error_mark_node;
38465
38466  do
38467    {
38468      op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
38469      cp_lexer_consume_token (parser->lexer);
38470      rhs = cp_parser_binary_expression (parser, false, false,
38471					 PREC_ADDITIVE_EXPRESSION, NULL);
38472      token = cp_lexer_peek_token (parser->lexer);
38473      if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
38474	{
38475	  if (lhs == NULL_TREE)
38476	    {
38477	      if (op == PLUS_EXPR)
38478		lhs = rhs;
38479	      else
38480		lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
38481					tf_warning_or_error);
38482	    }
38483	  else
38484	    lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
38485				     ERROR_MARK, NULL, tf_warning_or_error);
38486	}
38487    }
38488  while (token->type == CPP_PLUS || token->type == CPP_MINUS);
38489
38490  if (!decl_first)
38491    {
38492      if ((rhs != decl
38493	   && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
38494	  || op == MINUS_EXPR)
38495	return error_mark_node;
38496      rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
38497    }
38498  else
38499    rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
38500
38501  return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
38502}
38503
38504/* Parse the initialization statement of an OpenMP for loop.
38505
38506   Return true if the resulting construct should have an
38507   OMP_CLAUSE_PRIVATE added to it.  */
38508
38509static tree
38510cp_parser_omp_for_loop_init (cp_parser *parser,
38511			     tree &this_pre_body,
38512			     releasing_vec &for_block,
38513			     tree &init,
38514			     tree &orig_init,
38515			     tree &decl,
38516			     tree &real_decl)
38517{
38518  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
38519    return NULL_TREE;
38520
38521  tree add_private_clause = NULL_TREE;
38522
38523  /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
38524
38525     init-expr:
38526     var = lb
38527     integer-type var = lb
38528     random-access-iterator-type var = lb
38529     pointer-type var = lb
38530  */
38531  cp_decl_specifier_seq type_specifiers;
38532
38533  /* First, try to parse as an initialized declaration.  See
38534     cp_parser_condition, from whence the bulk of this is copied.  */
38535
38536  cp_parser_parse_tentatively (parser);
38537  cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
38538				/*is_declaration=*/true,
38539				/*is_trailing_return=*/false,
38540				&type_specifiers);
38541  if (cp_parser_parse_definitely (parser))
38542    {
38543      /* If parsing a type specifier seq succeeded, then this
38544	 MUST be a initialized declaration.  */
38545      tree asm_specification, attributes;
38546      cp_declarator *declarator;
38547
38548      declarator = cp_parser_declarator (parser,
38549					 CP_PARSER_DECLARATOR_NAMED,
38550					 CP_PARSER_FLAGS_NONE,
38551					 /*ctor_dtor_or_conv_p=*/NULL,
38552					 /*parenthesized_p=*/NULL,
38553					 /*member_p=*/false,
38554					 /*friend_p=*/false,
38555					 /*static_p=*/false);
38556      attributes = cp_parser_attributes_opt (parser);
38557      asm_specification = cp_parser_asm_specification_opt (parser);
38558
38559      if (declarator == cp_error_declarator)
38560	cp_parser_skip_to_end_of_statement (parser);
38561
38562      else
38563	{
38564	  tree pushed_scope, auto_node;
38565
38566	  decl = start_decl (declarator, &type_specifiers,
38567			     SD_INITIALIZED, attributes,
38568			     /*prefix_attributes=*/NULL_TREE,
38569			     &pushed_scope);
38570
38571	  auto_node = type_uses_auto (TREE_TYPE (decl));
38572	  if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
38573	    {
38574	      if (cp_lexer_next_token_is (parser->lexer,
38575					  CPP_OPEN_PAREN))
38576	        error ("parenthesized initialization is not allowed in "
38577		       "OpenMP %<for%> loop");
38578	      else
38579		/* Trigger an error.  */
38580		cp_parser_require (parser, CPP_EQ, RT_EQ);
38581
38582	      init = error_mark_node;
38583	      cp_parser_skip_to_end_of_statement (parser);
38584	    }
38585	  else if (CLASS_TYPE_P (TREE_TYPE (decl))
38586		   || type_dependent_expression_p (decl)
38587		   || auto_node)
38588	    {
38589	      bool is_direct_init, is_non_constant_init;
38590
38591	      init = cp_parser_initializer (parser,
38592					    &is_direct_init,
38593					    &is_non_constant_init);
38594
38595	      if (auto_node)
38596		{
38597		  TREE_TYPE (decl)
38598		    = do_auto_deduction (TREE_TYPE (decl), init,
38599					 auto_node);
38600
38601		  if (!CLASS_TYPE_P (TREE_TYPE (decl))
38602		      && !type_dependent_expression_p (decl))
38603		    goto non_class;
38604		}
38605
38606	      cp_finish_decl (decl, init, !is_non_constant_init,
38607			      asm_specification,
38608			      LOOKUP_ONLYCONVERTING);
38609	      orig_init = init;
38610	      if (CLASS_TYPE_P (TREE_TYPE (decl)))
38611		{
38612		  vec_safe_push (for_block, this_pre_body);
38613		  init = NULL_TREE;
38614		}
38615	      else
38616		{
38617		  init = pop_stmt_list (this_pre_body);
38618		  if (init && TREE_CODE (init) == STATEMENT_LIST)
38619		    {
38620		      tree_stmt_iterator i = tsi_start (init);
38621		      /* Move lambda DECL_EXPRs to FOR_BLOCK.  */
38622		      while (!tsi_end_p (i))
38623			{
38624			  tree t = tsi_stmt (i);
38625			  if (TREE_CODE (t) == DECL_EXPR
38626			      && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
38627			    {
38628			      tsi_delink (&i);
38629			      vec_safe_push (for_block, t);
38630			      continue;
38631			    }
38632			  break;
38633			}
38634		      if (tsi_one_before_end_p (i))
38635			{
38636			  tree t = tsi_stmt (i);
38637			  tsi_delink (&i);
38638			  free_stmt_list (init);
38639			  init = t;
38640			}
38641		    }
38642		}
38643	      this_pre_body = NULL_TREE;
38644	    }
38645	  else
38646	    {
38647	      /* Consume '='.  */
38648	      cp_lexer_consume_token (parser->lexer);
38649	      init = cp_parser_assignment_expression (parser);
38650
38651	    non_class:
38652	      if (TYPE_REF_P (TREE_TYPE (decl)))
38653		init = error_mark_node;
38654	      else
38655		cp_finish_decl (decl, NULL_TREE,
38656				/*init_const_expr_p=*/false,
38657				asm_specification,
38658				LOOKUP_ONLYCONVERTING);
38659	    }
38660
38661	  if (pushed_scope)
38662	    pop_scope (pushed_scope);
38663	}
38664    }
38665  else
38666    {
38667      cp_id_kind idk;
38668      /* If parsing a type specifier sequence failed, then
38669	 this MUST be a simple expression.  */
38670      cp_parser_parse_tentatively (parser);
38671      decl = cp_parser_primary_expression (parser, false, false,
38672					   false, &idk);
38673      cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
38674      if (!cp_parser_error_occurred (parser)
38675	  && decl
38676	  && (TREE_CODE (decl) == COMPONENT_REF
38677	      || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
38678	{
38679	  cp_parser_abort_tentative_parse (parser);
38680	  cp_parser_parse_tentatively (parser);
38681	  cp_token *token = cp_lexer_peek_token (parser->lexer);
38682	  tree name = cp_parser_id_expression (parser, /*template_p=*/false,
38683					       /*check_dependency_p=*/true,
38684					       /*template_p=*/NULL,
38685					       /*declarator_p=*/false,
38686					       /*optional_p=*/false);
38687	  if (name != error_mark_node
38688	      && last_tok == cp_lexer_peek_token (parser->lexer))
38689	    {
38690	      decl = cp_parser_lookup_name_simple (parser, name,
38691						   token->location);
38692	      if (TREE_CODE (decl) == FIELD_DECL)
38693		add_private_clause = omp_privatize_field (decl, false);
38694	    }
38695	  cp_parser_abort_tentative_parse (parser);
38696	  cp_parser_parse_tentatively (parser);
38697	  decl = cp_parser_primary_expression (parser, false, false,
38698					       false, &idk);
38699	}
38700      if (!cp_parser_error_occurred (parser)
38701	  && decl
38702	  && DECL_P (decl)
38703	  && CLASS_TYPE_P (TREE_TYPE (decl)))
38704	{
38705	  tree rhs;
38706
38707	  cp_parser_parse_definitely (parser);
38708	  cp_parser_require (parser, CPP_EQ, RT_EQ);
38709	  rhs = cp_parser_assignment_expression (parser);
38710	  orig_init = rhs;
38711	  finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
38712						 decl, NOP_EXPR,
38713						 rhs,
38714						 tf_warning_or_error));
38715	  if (!add_private_clause)
38716	    add_private_clause = decl;
38717	}
38718      else
38719	{
38720	  decl = NULL;
38721	  cp_parser_abort_tentative_parse (parser);
38722	  init = cp_parser_expression (parser);
38723	  if (init)
38724	    {
38725	      if (TREE_CODE (init) == MODIFY_EXPR
38726		  || TREE_CODE (init) == MODOP_EXPR)
38727		real_decl = TREE_OPERAND (init, 0);
38728	    }
38729	}
38730    }
38731  return add_private_clause;
38732}
38733
38734/* Helper for cp_parser_omp_for_loop, handle one range-for loop.  */
38735
38736void
38737cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
38738			  tree &decl, tree &orig_decl, tree &init,
38739			  tree &orig_init, tree &cond, tree &incr)
38740{
38741  tree begin, end, range_temp_decl = NULL_TREE;
38742  tree iter_type, begin_expr, end_expr;
38743
38744  if (processing_template_decl)
38745    {
38746      if (check_for_bare_parameter_packs (init))
38747	init = error_mark_node;
38748      if (!type_dependent_expression_p (init)
38749	  /* do_auto_deduction doesn't mess with template init-lists.  */
38750	  && !BRACE_ENCLOSED_INITIALIZER_P (init))
38751	{
38752	  tree d = decl;
38753	  if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
38754	    {
38755	      tree v = DECL_VALUE_EXPR (decl);
38756	      if (TREE_CODE (v) == ARRAY_REF
38757		  && VAR_P (TREE_OPERAND (v, 0))
38758		  && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
38759		d = TREE_OPERAND (v, 0);
38760	    }
38761	  do_range_for_auto_deduction (d, init);
38762	}
38763      cond = global_namespace;
38764      incr = NULL_TREE;
38765      orig_init = init;
38766      if (this_pre_body)
38767	this_pre_body = pop_stmt_list (this_pre_body);
38768      return;
38769    }
38770
38771  init = mark_lvalue_use (init);
38772
38773  if (decl == error_mark_node || init == error_mark_node)
38774    /* If an error happened previously do nothing or else a lot of
38775       unhelpful errors would be issued.  */
38776    begin_expr = end_expr = iter_type = error_mark_node;
38777  else
38778    {
38779      tree range_temp;
38780
38781      if (VAR_P (init)
38782	  && array_of_runtime_bound_p (TREE_TYPE (init)))
38783	/* Can't bind a reference to an array of runtime bound.  */
38784	range_temp = init;
38785      else
38786	{
38787	  range_temp = build_range_temp (init);
38788	  DECL_NAME (range_temp) = NULL_TREE;
38789	  pushdecl (range_temp);
38790	  cp_finish_decl (range_temp, init,
38791			  /*is_constant_init*/false, NULL_TREE,
38792			  LOOKUP_ONLYCONVERTING);
38793	  range_temp_decl = range_temp;
38794	  range_temp = convert_from_reference (range_temp);
38795	}
38796      iter_type = cp_parser_perform_range_for_lookup (range_temp,
38797						      &begin_expr, &end_expr);
38798    }
38799
38800  tree end_iter_type = iter_type;
38801  if (cxx_dialect >= cxx17)
38802    end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
38803  end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
38804  TREE_USED (end) = 1;
38805  DECL_ARTIFICIAL (end) = 1;
38806  pushdecl (end);
38807  cp_finish_decl (end, end_expr,
38808		  /*is_constant_init*/false, NULL_TREE,
38809		  LOOKUP_ONLYCONVERTING);
38810
38811  /* The new for initialization statement.  */
38812  begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
38813  TREE_USED (begin) = 1;
38814  DECL_ARTIFICIAL (begin) = 1;
38815  pushdecl (begin);
38816  orig_init = init;
38817  if (CLASS_TYPE_P (iter_type))
38818    init = NULL_TREE;
38819  else
38820    {
38821      init = begin_expr;
38822      begin_expr = NULL_TREE;
38823    }
38824  cp_finish_decl (begin, begin_expr,
38825		  /*is_constant_init*/false, NULL_TREE,
38826		  LOOKUP_ONLYCONVERTING);
38827
38828  /* The new for condition.  */
38829  if (CLASS_TYPE_P (iter_type))
38830    cond = build2 (NE_EXPR, boolean_type_node, begin, end);
38831  else
38832    cond = build_x_binary_op (input_location, NE_EXPR,
38833			      begin, ERROR_MARK,
38834			      end, ERROR_MARK,
38835			      NULL, tf_warning_or_error);
38836
38837  /* The new increment expression.  */
38838  if (CLASS_TYPE_P (iter_type))
38839    incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
38840  else
38841    incr = finish_unary_op_expr (input_location,
38842				 PREINCREMENT_EXPR, begin,
38843				 tf_warning_or_error);
38844
38845  orig_decl = decl;
38846  decl = begin;
38847  if (for_block)
38848    {
38849      vec_safe_push (for_block, this_pre_body);
38850      this_pre_body = NULL_TREE;
38851    }
38852
38853  tree decomp_first_name = NULL_TREE;
38854  unsigned decomp_cnt = 0;
38855  if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
38856    {
38857      tree v = DECL_VALUE_EXPR (orig_decl);
38858      if (TREE_CODE (v) == ARRAY_REF
38859	  && VAR_P (TREE_OPERAND (v, 0))
38860	  && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
38861	{
38862	  tree d = orig_decl;
38863	  orig_decl = TREE_OPERAND (v, 0);
38864	  decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
38865	  decomp_first_name = d;
38866	}
38867    }
38868
38869  tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
38870  if (auto_node)
38871    {
38872      tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
38873				     tf_none);
38874      if (!error_operand_p (t))
38875	TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
38876						   t, auto_node);
38877    }
38878
38879  tree v = make_tree_vec (decomp_cnt + 3);
38880  TREE_VEC_ELT (v, 0) = range_temp_decl;
38881  TREE_VEC_ELT (v, 1) = end;
38882  TREE_VEC_ELT (v, 2) = orig_decl;
38883  for (unsigned i = 0; i < decomp_cnt; i++)
38884    {
38885      TREE_VEC_ELT (v, i + 3) = decomp_first_name;
38886      decomp_first_name = DECL_CHAIN (decomp_first_name);
38887    }
38888  orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
38889}
38890
38891/* Helper for cp_parser_omp_for_loop, finalize part of range for
38892   inside of the collapsed body.  */
38893
38894void
38895cp_finish_omp_range_for (tree orig, tree begin)
38896{
38897  gcc_assert (TREE_CODE (orig) == TREE_LIST
38898	      && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
38899  tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
38900  tree decomp_first_name = NULL_TREE;
38901  unsigned int decomp_cnt = 0;
38902
38903  if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
38904    {
38905      decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
38906      decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
38907      cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
38908    }
38909
38910  /* The declaration is initialized with *__begin inside the loop body.  */
38911  cp_finish_decl (decl,
38912		  build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
38913					tf_warning_or_error),
38914		  /*is_constant_init*/false, NULL_TREE,
38915		  LOOKUP_ONLYCONVERTING);
38916  if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
38917    cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
38918}
38919
38920/* OpenMP 5.0:
38921
38922   scan-loop-body:
38923     { structured-block scan-directive structured-block }  */
38924
38925static void
38926cp_parser_omp_scan_loop_body (cp_parser *parser)
38927{
38928  tree substmt, clauses = NULL_TREE;
38929
38930  matching_braces braces;
38931  if (!braces.require_open (parser))
38932    return;
38933
38934  substmt = cp_parser_omp_structured_block (parser, NULL);
38935  substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
38936  add_stmt (substmt);
38937
38938  cp_token *tok = cp_lexer_peek_token (parser->lexer);
38939  if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
38940    {
38941      enum omp_clause_code clause = OMP_CLAUSE_ERROR;
38942
38943      cp_lexer_consume_token (parser->lexer);
38944
38945      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38946	{
38947	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38948	  const char *p = IDENTIFIER_POINTER (id);
38949	  if (strcmp (p, "inclusive") == 0)
38950	    clause = OMP_CLAUSE_INCLUSIVE;
38951	  else if (strcmp (p, "exclusive") == 0)
38952	    clause = OMP_CLAUSE_EXCLUSIVE;
38953	}
38954      if (clause != OMP_CLAUSE_ERROR)
38955	{
38956	  cp_lexer_consume_token (parser->lexer);
38957	  clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
38958	}
38959      else
38960	cp_parser_error (parser, "expected %<inclusive%> or "
38961				 "%<exclusive%> clause");
38962
38963      cp_parser_require_pragma_eol (parser, tok);
38964    }
38965  else
38966    error ("expected %<#pragma omp scan%>");
38967
38968  clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38969  substmt = cp_parser_omp_structured_block (parser, NULL);
38970  substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
38971			clauses);
38972  add_stmt (substmt);
38973
38974  braces.require_close (parser);
38975}
38976
38977/* Parse the restricted form of the for statement allowed by OpenMP.  */
38978
38979static tree
38980cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
38981			tree *cclauses, bool *if_p)
38982{
38983  tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
38984  tree orig_decl;
38985  tree real_decl, initv, condv, incrv, declv, orig_declv;
38986  tree this_pre_body, cl, ordered_cl = NULL_TREE;
38987  location_t loc_first;
38988  bool collapse_err = false;
38989  int i, collapse = 1, ordered = 0, count, nbraces = 0;
38990  releasing_vec for_block;
38991  auto_vec<tree, 4> orig_inits;
38992  bool tiling = false;
38993  bool inscan = false;
38994
38995  for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
38996    if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
38997      collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
38998    else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
38999      {
39000	tiling = true;
39001	collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
39002      }
39003    else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
39004	     && OMP_CLAUSE_ORDERED_EXPR (cl))
39005      {
39006	ordered_cl = cl;
39007	ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
39008      }
39009    else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
39010	     && OMP_CLAUSE_REDUCTION_INSCAN (cl)
39011	     && (code == OMP_SIMD || code == OMP_FOR))
39012      inscan = true;
39013
39014  if (ordered && ordered < collapse)
39015    {
39016      error_at (OMP_CLAUSE_LOCATION (ordered_cl),
39017		"%<ordered%> clause parameter is less than %<collapse%>");
39018      OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
39019	= build_int_cst (NULL_TREE, collapse);
39020      ordered = collapse;
39021    }
39022  if (ordered)
39023    {
39024      for (tree *pc = &clauses; *pc; )
39025	if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
39026	  {
39027	    error_at (OMP_CLAUSE_LOCATION (*pc),
39028		      "%<linear%> clause may not be specified together "
39029		      "with %<ordered%> clause with a parameter");
39030	    *pc = OMP_CLAUSE_CHAIN (*pc);
39031	  }
39032	else
39033	  pc = &OMP_CLAUSE_CHAIN (*pc);
39034    }
39035
39036  gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
39037  count = ordered ? ordered : collapse;
39038
39039  declv = make_tree_vec (count);
39040  initv = make_tree_vec (count);
39041  condv = make_tree_vec (count);
39042  incrv = make_tree_vec (count);
39043  orig_declv = NULL_TREE;
39044
39045  loc_first = cp_lexer_peek_token (parser->lexer)->location;
39046
39047  for (i = 0; i < count; i++)
39048    {
39049      int bracecount = 0;
39050      tree add_private_clause = NULL_TREE;
39051      location_t loc;
39052
39053      if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39054	{
39055	  if (!collapse_err)
39056	    cp_parser_error (parser, "for statement expected");
39057	  return NULL;
39058	}
39059      loc = cp_lexer_consume_token (parser->lexer)->location;
39060
39061      /* Don't create location wrapper nodes within an OpenMP "for"
39062	 statement.  */
39063      auto_suppress_location_wrappers sentinel;
39064
39065      matching_parens parens;
39066      if (!parens.require_open (parser))
39067	return NULL;
39068
39069      init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
39070      this_pre_body = push_stmt_list ();
39071
39072      if (code != OACC_LOOP && cxx_dialect >= cxx11)
39073	{
39074	  /* Save tokens so that we can put them back.  */
39075	  cp_lexer_save_tokens (parser->lexer);
39076
39077	  /* Look for ':' that is not nested in () or {}.  */
39078	  bool is_range_for
39079	    = (cp_parser_skip_to_closing_parenthesis_1 (parser,
39080							/*recovering=*/false,
39081							CPP_COLON,
39082							/*consume_paren=*/
39083							false) == -1);
39084
39085	  /* Roll back the tokens we skipped.  */
39086	  cp_lexer_rollback_tokens (parser->lexer);
39087
39088	  if (is_range_for)
39089	    {
39090	      bool saved_colon_corrects_to_scope_p
39091		= parser->colon_corrects_to_scope_p;
39092
39093	      /* A colon is used in range-based for.  */
39094	      parser->colon_corrects_to_scope_p = false;
39095
39096	      /* Parse the declaration.  */
39097	      cp_parser_simple_declaration (parser,
39098					    /*function_definition_allowed_p=*/
39099					    false, &decl);
39100	      parser->colon_corrects_to_scope_p
39101		= saved_colon_corrects_to_scope_p;
39102
39103	      cp_parser_require (parser, CPP_COLON, RT_COLON);
39104
39105	      init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
39106					  false, 0, true);
39107
39108	      cp_convert_omp_range_for (this_pre_body, for_block, decl,
39109					orig_decl, init, orig_init,
39110					cond, incr);
39111	      if (this_pre_body)
39112		{
39113		  if (pre_body)
39114		    {
39115		      tree t = pre_body;
39116		      pre_body = push_stmt_list ();
39117		      add_stmt (t);
39118		      add_stmt (this_pre_body);
39119		      pre_body = pop_stmt_list (pre_body);
39120		    }
39121		  else
39122		    pre_body = this_pre_body;
39123		}
39124
39125	      if (ordered_cl)
39126		error_at (OMP_CLAUSE_LOCATION (ordered_cl),
39127			  "%<ordered%> clause with parameter on "
39128			  "range-based %<for%> loop");
39129
39130	      goto parse_close_paren;
39131	    }
39132	}
39133
39134      add_private_clause
39135	= cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
39136				       init, orig_init, decl, real_decl);
39137
39138      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39139      if (this_pre_body)
39140	{
39141	  this_pre_body = pop_stmt_list (this_pre_body);
39142	  if (pre_body)
39143	    {
39144	      tree t = pre_body;
39145	      pre_body = push_stmt_list ();
39146	      add_stmt (t);
39147	      add_stmt (this_pre_body);
39148	      pre_body = pop_stmt_list (pre_body);
39149	    }
39150	  else
39151	    pre_body = this_pre_body;
39152	}
39153
39154      if (decl)
39155	real_decl = decl;
39156      if (cclauses != NULL
39157	  && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
39158	  && real_decl != NULL_TREE
39159	  && code != OMP_LOOP)
39160	{
39161	  tree *c;
39162	  for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
39163	    if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
39164		&& OMP_CLAUSE_DECL (*c) == real_decl)
39165	      {
39166		error_at (loc, "iteration variable %qD"
39167			  " should not be firstprivate", real_decl);
39168		*c = OMP_CLAUSE_CHAIN (*c);
39169	      }
39170	    else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
39171		     && OMP_CLAUSE_DECL (*c) == real_decl)
39172	      {
39173		/* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
39174		tree l = *c;
39175		*c = OMP_CLAUSE_CHAIN (*c);
39176		if (code == OMP_SIMD)
39177		  {
39178		    OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39179		    cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
39180		  }
39181		else
39182		  {
39183		    OMP_CLAUSE_CHAIN (l) = clauses;
39184		    clauses = l;
39185		  }
39186		add_private_clause = NULL_TREE;
39187	      }
39188	    else
39189	      {
39190		if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
39191		    && OMP_CLAUSE_DECL (*c) == real_decl)
39192		  add_private_clause = NULL_TREE;
39193		c = &OMP_CLAUSE_CHAIN (*c);
39194	      }
39195	}
39196
39197      if (add_private_clause)
39198	{
39199	  tree c;
39200	  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
39201	    {
39202	      if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
39203		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
39204		  && OMP_CLAUSE_DECL (c) == decl)
39205		break;
39206	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
39207		       && OMP_CLAUSE_DECL (c) == decl)
39208		error_at (loc, "iteration variable %qD "
39209			  "should not be firstprivate",
39210			  decl);
39211	      else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
39212			|| OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
39213		       && OMP_CLAUSE_DECL (c) == decl)
39214		error_at (loc, "iteration variable %qD should not be reduction",
39215			  decl);
39216	    }
39217	  if (c == NULL)
39218	    {
39219	      if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
39220		c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
39221	      else if (code != OMP_SIMD)
39222		c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
39223	      else
39224		c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
39225	      OMP_CLAUSE_DECL (c) = add_private_clause;
39226	      c = finish_omp_clauses (c, C_ORT_OMP);
39227	      if (c)
39228		{
39229		  OMP_CLAUSE_CHAIN (c) = clauses;
39230		  clauses = c;
39231		  /* For linear, signal that we need to fill up
39232		     the so far unknown linear step.  */
39233		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
39234		    OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
39235		}
39236	    }
39237	}
39238
39239      cond = NULL;
39240      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
39241	cond = cp_parser_omp_for_cond (parser, decl, code);
39242      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39243
39244      incr = NULL;
39245      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
39246	{
39247	  /* If decl is an iterator, preserve the operator on decl
39248	     until finish_omp_for.  */
39249	  if (real_decl
39250	      && ((processing_template_decl
39251		   && (TREE_TYPE (real_decl) == NULL_TREE
39252		       || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
39253		  || CLASS_TYPE_P (TREE_TYPE (real_decl))))
39254	    incr = cp_parser_omp_for_incr (parser, real_decl);
39255	  else
39256	    incr = cp_parser_expression (parser);
39257	  protected_set_expr_location_if_unset (incr, input_location);
39258	}
39259
39260    parse_close_paren:
39261      if (!parens.require_close (parser))
39262	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39263					       /*or_comma=*/false,
39264					       /*consume_paren=*/true);
39265
39266      TREE_VEC_ELT (declv, i) = decl;
39267      TREE_VEC_ELT (initv, i) = init;
39268      TREE_VEC_ELT (condv, i) = cond;
39269      TREE_VEC_ELT (incrv, i) = incr;
39270      if (orig_init)
39271	{
39272	  orig_inits.safe_grow_cleared (i + 1);
39273	  orig_inits[i] = orig_init;
39274	}
39275      if (orig_decl)
39276	{
39277	  if (!orig_declv)
39278	    orig_declv = copy_node (declv);
39279	  TREE_VEC_ELT (orig_declv, i) = orig_decl;
39280	}
39281      else if (orig_declv)
39282	TREE_VEC_ELT (orig_declv, i) = decl;
39283
39284      if (i == count - 1)
39285	break;
39286
39287      /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
39288	 in between the collapsed for loops to be still considered perfectly
39289	 nested.  Hopefully the final version clarifies this.
39290	 For now handle (multiple) {'s and empty statements.  */
39291      cp_parser_parse_tentatively (parser);
39292      for (;;)
39293	{
39294	  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39295	    break;
39296	  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
39297	    {
39298	      cp_lexer_consume_token (parser->lexer);
39299	      bracecount++;
39300	    }
39301	  else if (bracecount
39302		   && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39303	    cp_lexer_consume_token (parser->lexer);
39304	  else
39305	    {
39306	      loc = cp_lexer_peek_token (parser->lexer)->location;
39307	      error_at (loc, "not enough for loops to collapse");
39308	      collapse_err = true;
39309	      cp_parser_abort_tentative_parse (parser);
39310	      declv = NULL_TREE;
39311	      break;
39312	    }
39313	}
39314
39315      if (declv)
39316	{
39317	  cp_parser_parse_definitely (parser);
39318	  nbraces += bracecount;
39319	}
39320    }
39321
39322  if (nbraces)
39323    if_p = NULL;
39324
39325  /* Note that we saved the original contents of this flag when we entered
39326     the structured block, and so we don't need to re-save it here.  */
39327  parser->in_statement = IN_OMP_FOR;
39328
39329  /* Note that the grammar doesn't call for a structured block here,
39330     though the loop as a whole is a structured block.  */
39331  if (orig_declv)
39332    {
39333      body = begin_omp_structured_block ();
39334      for (i = 0; i < count; i++)
39335	if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
39336	  cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
39337				   TREE_VEC_ELT (declv, i));
39338    }
39339  else
39340    body = push_stmt_list ();
39341  if (inscan)
39342    cp_parser_omp_scan_loop_body (parser);
39343  else
39344    cp_parser_statement (parser, NULL_TREE, false, if_p);
39345  if (orig_declv)
39346    body = finish_omp_structured_block (body);
39347  else
39348    body = pop_stmt_list (body);
39349
39350  if (declv == NULL_TREE)
39351    ret = NULL_TREE;
39352  else
39353    ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
39354			  incrv, body, pre_body, &orig_inits, clauses);
39355
39356  while (nbraces)
39357    {
39358      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39359	{
39360	  cp_lexer_consume_token (parser->lexer);
39361	  nbraces--;
39362	}
39363      else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39364	cp_lexer_consume_token (parser->lexer);
39365      else
39366	{
39367	  if (!collapse_err)
39368	    {
39369	      error_at (cp_lexer_peek_token (parser->lexer)->location,
39370			"collapsed loops not perfectly nested");
39371	    }
39372	  collapse_err = true;
39373	  cp_parser_statement_seq_opt (parser, NULL);
39374	  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
39375	    break;
39376	}
39377    }
39378
39379  while (!for_block->is_empty ())
39380    {
39381      tree t = for_block->pop ();
39382      if (TREE_CODE (t) == STATEMENT_LIST)
39383	add_stmt (pop_stmt_list (t));
39384      else
39385	add_stmt (t);
39386    }
39387
39388  return ret;
39389}
39390
39391/* Helper function for OpenMP parsing, split clauses and call
39392   finish_omp_clauses on each of the set of clauses afterwards.  */
39393
39394static void
39395cp_omp_split_clauses (location_t loc, enum tree_code code,
39396		      omp_clause_mask mask, tree clauses, tree *cclauses)
39397{
39398  int i;
39399  c_omp_split_clauses (loc, code, mask, clauses, cclauses);
39400  for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
39401    if (cclauses[i])
39402      cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
39403}
39404
39405/* OpenMP 5.0:
39406   #pragma omp loop loop-clause[optseq] new-line
39407     for-loop  */
39408
39409#define OMP_LOOP_CLAUSE_MASK					\
39410	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
39411	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
39412	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
39413	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
39414	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND)		\
39415	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39416
39417static tree
39418cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
39419		    char *p_name, omp_clause_mask mask, tree *cclauses,
39420		    bool *if_p)
39421{
39422  tree clauses, sb, ret;
39423  unsigned int save;
39424  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39425
39426  strcat (p_name, " loop");
39427  mask |= OMP_LOOP_CLAUSE_MASK;
39428
39429  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39430				       cclauses == NULL);
39431  if (cclauses)
39432    {
39433      cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
39434      clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
39435    }
39436
39437  keep_next_level (true);
39438  sb = begin_omp_structured_block ();
39439  save = cp_parser_begin_omp_structured_block (parser);
39440
39441  ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
39442
39443  cp_parser_end_omp_structured_block (parser, save);
39444  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39445
39446  return ret;
39447}
39448
39449/* OpenMP 4.0:
39450   #pragma omp simd simd-clause[optseq] new-line
39451     for-loop  */
39452
39453#define OMP_SIMD_CLAUSE_MASK					\
39454	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)	\
39455	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
39456	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
39457	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
39458	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
39459	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
39460	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
39461	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
39462	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
39463	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL)	\
39464	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39465
39466static tree
39467cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
39468		    char *p_name, omp_clause_mask mask, tree *cclauses,
39469		    bool *if_p)
39470{
39471  tree clauses, sb, ret;
39472  unsigned int save;
39473  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39474
39475  strcat (p_name, " simd");
39476  mask |= OMP_SIMD_CLAUSE_MASK;
39477
39478  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39479				       cclauses == NULL);
39480  if (cclauses)
39481    {
39482      cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
39483      clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
39484      tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
39485				OMP_CLAUSE_ORDERED);
39486      if (c && OMP_CLAUSE_ORDERED_EXPR (c))
39487	{
39488	  error_at (OMP_CLAUSE_LOCATION (c),
39489		    "%<ordered%> clause with parameter may not be specified "
39490		    "on %qs construct", p_name);
39491	  OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
39492	}
39493    }
39494
39495  keep_next_level (true);
39496  sb = begin_omp_structured_block ();
39497  save = cp_parser_begin_omp_structured_block (parser);
39498
39499  ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
39500
39501  cp_parser_end_omp_structured_block (parser, save);
39502  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39503
39504  return ret;
39505}
39506
39507/* OpenMP 2.5:
39508   #pragma omp for for-clause[optseq] new-line
39509     for-loop
39510
39511   OpenMP 4.0:
39512   #pragma omp for simd for-simd-clause[optseq] new-line
39513     for-loop  */
39514
39515#define OMP_FOR_CLAUSE_MASK					\
39516	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
39517	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
39518	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
39519	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
39520	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
39521	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)	\
39522	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)	\
39523	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
39524	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
39525	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39526
39527static tree
39528cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
39529		   char *p_name, omp_clause_mask mask, tree *cclauses,
39530		   bool *if_p)
39531{
39532  tree clauses, sb, ret;
39533  unsigned int save;
39534  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39535
39536  strcat (p_name, " for");
39537  mask |= OMP_FOR_CLAUSE_MASK;
39538  /* parallel for{, simd} disallows nowait clause, but for
39539     target {teams distribute ,}parallel for{, simd} it should be accepted.  */
39540  if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
39541    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
39542  /* Composite distribute parallel for{, simd} disallows ordered clause.  */
39543  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39544    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
39545
39546  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39547    {
39548      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39549      const char *p = IDENTIFIER_POINTER (id);
39550
39551      if (strcmp (p, "simd") == 0)
39552	{
39553	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39554	  if (cclauses == NULL)
39555	    cclauses = cclauses_buf;
39556
39557	  cp_lexer_consume_token (parser->lexer);
39558	  if (!flag_openmp)  /* flag_openmp_simd  */
39559	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39560				       cclauses, if_p);
39561	  sb = begin_omp_structured_block ();
39562	  save = cp_parser_begin_omp_structured_block (parser);
39563	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39564				    cclauses, if_p);
39565	  cp_parser_end_omp_structured_block (parser, save);
39566	  tree body = finish_omp_structured_block (sb);
39567	  if (ret == NULL)
39568	    return ret;
39569	  ret = make_node (OMP_FOR);
39570	  TREE_TYPE (ret) = void_type_node;
39571	  OMP_FOR_BODY (ret) = body;
39572	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39573	  SET_EXPR_LOCATION (ret, loc);
39574	  add_stmt (ret);
39575	  return ret;
39576	}
39577    }
39578  if (!flag_openmp)  /* flag_openmp_simd  */
39579    {
39580      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39581      return NULL_TREE;
39582    }
39583
39584  /* Composite distribute parallel for disallows linear clause.  */
39585  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39586    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
39587
39588  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39589				       cclauses == NULL);
39590  if (cclauses)
39591    {
39592      cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
39593      clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39594    }
39595
39596  keep_next_level (true);
39597  sb = begin_omp_structured_block ();
39598  save = cp_parser_begin_omp_structured_block (parser);
39599
39600  ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
39601
39602  cp_parser_end_omp_structured_block (parser, save);
39603  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39604
39605  return ret;
39606}
39607
39608static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
39609				    omp_clause_mask, tree *, bool *);
39610
39611/* OpenMP 2.5:
39612   # pragma omp master new-line
39613     structured-block  */
39614
39615static tree
39616cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
39617		      char *p_name, omp_clause_mask mask, tree *cclauses,
39618		      bool *if_p)
39619{
39620  tree clauses, sb, ret;
39621  unsigned int save;
39622  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39623
39624  strcat (p_name, " master");
39625
39626  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39627    {
39628      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39629      const char *p = IDENTIFIER_POINTER (id);
39630
39631      if (strcmp (p, "taskloop") == 0)
39632	{
39633	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39634	  if (cclauses == NULL)
39635	    cclauses = cclauses_buf;
39636
39637	  cp_lexer_consume_token (parser->lexer);
39638	  if (!flag_openmp)  /* flag_openmp_simd  */
39639	    return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
39640					   cclauses, if_p);
39641	  sb = begin_omp_structured_block ();
39642	  save = cp_parser_begin_omp_structured_block (parser);
39643	  ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
39644					cclauses, if_p);
39645	  cp_parser_end_omp_structured_block (parser, save);
39646	  tree body = finish_omp_structured_block (sb);
39647	  if (ret == NULL)
39648	    return ret;
39649	  return c_finish_omp_master (loc, body);
39650	}
39651    }
39652  if (!flag_openmp)  /* flag_openmp_simd  */
39653    {
39654      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39655      return NULL_TREE;
39656    }
39657
39658  if (cclauses)
39659    {
39660      clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39661					   false);
39662      cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
39663    }
39664  else
39665    cp_parser_require_pragma_eol (parser, pragma_tok);
39666
39667  return c_finish_omp_master (loc,
39668			      cp_parser_omp_structured_block (parser, if_p));
39669}
39670
39671/* OpenMP 2.5:
39672   # pragma omp ordered new-line
39673     structured-block
39674
39675   OpenMP 4.5:
39676   # pragma omp ordered ordered-clauses new-line
39677     structured-block  */
39678
39679#define OMP_ORDERED_CLAUSE_MASK					\
39680	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)	\
39681	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
39682
39683#define OMP_ORDERED_DEPEND_CLAUSE_MASK				\
39684	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
39685
39686static bool
39687cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
39688		       enum pragma_context context, bool *if_p)
39689{
39690  location_t loc = pragma_tok->location;
39691
39692  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39693    {
39694      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39695      const char *p = IDENTIFIER_POINTER (id);
39696
39697      if (strcmp (p, "depend") == 0)
39698	{
39699	  if (!flag_openmp)	/* flag_openmp_simd */
39700	    {
39701	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39702	      return false;
39703	    }
39704	  if (context == pragma_stmt)
39705	    {
39706	      error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
39707			"%<depend%> clause may only be used in compound "
39708			"statements");
39709	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39710	      return false;
39711	    }
39712	  tree clauses
39713	    = cp_parser_omp_all_clauses (parser,
39714					 OMP_ORDERED_DEPEND_CLAUSE_MASK,
39715					 "#pragma omp ordered", pragma_tok);
39716	  c_finish_omp_ordered (loc, clauses, NULL_TREE);
39717	  return false;
39718	}
39719    }
39720
39721  tree clauses
39722    = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
39723				 "#pragma omp ordered", pragma_tok);
39724
39725  if (!flag_openmp     /* flag_openmp_simd  */
39726      && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
39727    return false;
39728
39729  c_finish_omp_ordered (loc, clauses,
39730			cp_parser_omp_structured_block (parser, if_p));
39731  return true;
39732}
39733
39734/* OpenMP 2.5:
39735
39736   section-scope:
39737     { section-sequence }
39738
39739   section-sequence:
39740     section-directive[opt] structured-block
39741     section-sequence section-directive structured-block  */
39742
39743static tree
39744cp_parser_omp_sections_scope (cp_parser *parser)
39745{
39746  tree stmt, substmt;
39747  bool error_suppress = false;
39748  cp_token *tok;
39749
39750  matching_braces braces;
39751  if (!braces.require_open (parser))
39752    return NULL_TREE;
39753
39754  stmt = push_stmt_list ();
39755
39756  if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
39757      != PRAGMA_OMP_SECTION)
39758    {
39759      substmt = cp_parser_omp_structured_block (parser, NULL);
39760      substmt = build1 (OMP_SECTION, void_type_node, substmt);
39761      add_stmt (substmt);
39762    }
39763
39764  while (1)
39765    {
39766      tok = cp_lexer_peek_token (parser->lexer);
39767      if (tok->type == CPP_CLOSE_BRACE)
39768	break;
39769      if (tok->type == CPP_EOF)
39770	break;
39771
39772      if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
39773	{
39774	  cp_lexer_consume_token (parser->lexer);
39775	  cp_parser_require_pragma_eol (parser, tok);
39776	  error_suppress = false;
39777	}
39778      else if (!error_suppress)
39779	{
39780	  cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
39781	  error_suppress = true;
39782	}
39783
39784      substmt = cp_parser_omp_structured_block (parser, NULL);
39785      substmt = build1 (OMP_SECTION, void_type_node, substmt);
39786      add_stmt (substmt);
39787    }
39788  braces.require_close (parser);
39789
39790  substmt = pop_stmt_list (stmt);
39791
39792  stmt = make_node (OMP_SECTIONS);
39793  TREE_TYPE (stmt) = void_type_node;
39794  OMP_SECTIONS_BODY (stmt) = substmt;
39795
39796  add_stmt (stmt);
39797  return stmt;
39798}
39799
39800/* OpenMP 2.5:
39801   # pragma omp sections sections-clause[optseq] newline
39802     sections-scope  */
39803
39804#define OMP_SECTIONS_CLAUSE_MASK				\
39805	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
39806	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
39807	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
39808	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
39809	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39810
39811static tree
39812cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
39813			char *p_name, omp_clause_mask mask, tree *cclauses)
39814{
39815  tree clauses, ret;
39816  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39817
39818  strcat (p_name, " sections");
39819  mask |= OMP_SECTIONS_CLAUSE_MASK;
39820  if (cclauses)
39821    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
39822
39823  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39824				       cclauses == NULL);
39825  if (cclauses)
39826    {
39827      cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
39828      clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
39829    }
39830
39831  ret = cp_parser_omp_sections_scope (parser);
39832  if (ret)
39833    OMP_SECTIONS_CLAUSES (ret) = clauses;
39834
39835  return ret;
39836}
39837
39838/* OpenMP 2.5:
39839   # pragma omp parallel parallel-clause[optseq] new-line
39840     structured-block
39841   # pragma omp parallel for parallel-for-clause[optseq] new-line
39842     structured-block
39843   # pragma omp parallel sections parallel-sections-clause[optseq] new-line
39844     structured-block
39845
39846   OpenMP 4.0:
39847   # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
39848     structured-block */
39849
39850#define OMP_PARALLEL_CLAUSE_MASK				\
39851	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
39852	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
39853	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
39854	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
39855	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
39856	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)	\
39857	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
39858	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)	\
39859	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
39860
39861static tree
39862cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
39863			char *p_name, omp_clause_mask mask, tree *cclauses,
39864			bool *if_p)
39865{
39866  tree stmt, clauses, block;
39867  unsigned int save;
39868  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39869
39870  strcat (p_name, " parallel");
39871  mask |= OMP_PARALLEL_CLAUSE_MASK;
39872  /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
39873  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
39874      && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
39875    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
39876
39877  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39878    {
39879      tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39880      if (cclauses == NULL)
39881	cclauses = cclauses_buf;
39882
39883      cp_lexer_consume_token (parser->lexer);
39884      if (!flag_openmp)  /* flag_openmp_simd  */
39885	return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
39886				  if_p);
39887      block = begin_omp_parallel ();
39888      save = cp_parser_begin_omp_structured_block (parser);
39889      tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
39890				    if_p);
39891      cp_parser_end_omp_structured_block (parser, save);
39892      stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39893				  block);
39894      if (ret == NULL_TREE)
39895	return ret;
39896      OMP_PARALLEL_COMBINED (stmt) = 1;
39897      return stmt;
39898    }
39899  /* When combined with distribute, parallel has to be followed by for.
39900     #pragma omp target parallel is allowed though.  */
39901  else if (cclauses
39902	   && (mask & (OMP_CLAUSE_MASK_1
39903		       << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39904    {
39905      error_at (loc, "expected %<for%> after %qs", p_name);
39906      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39907      return NULL_TREE;
39908    }
39909  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39910    {
39911      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39912      const char *p = IDENTIFIER_POINTER (id);
39913      if (cclauses == NULL && strcmp (p, "master") == 0)
39914	{
39915	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39916	  cclauses = cclauses_buf;
39917
39918	  cp_lexer_consume_token (parser->lexer);
39919	  if (!flag_openmp)  /* flag_openmp_simd  */
39920	    return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
39921					 cclauses, if_p);
39922	  block = begin_omp_parallel ();
39923	  save = cp_parser_begin_omp_structured_block (parser);
39924	  tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
39925					   cclauses, if_p);
39926	  cp_parser_end_omp_structured_block (parser, save);
39927	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39928				      block);
39929	  if (ret == NULL_TREE)
39930	    return ret;
39931	  OMP_PARALLEL_COMBINED (stmt) = 1;
39932	  return stmt;
39933	}
39934      else if (strcmp (p, "loop") == 0)
39935	{
39936	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39937	  if (cclauses == NULL)
39938	    cclauses = cclauses_buf;
39939
39940	  cp_lexer_consume_token (parser->lexer);
39941	  if (!flag_openmp)  /* flag_openmp_simd  */
39942	    return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39943				       cclauses, if_p);
39944	  block = begin_omp_parallel ();
39945	  save = cp_parser_begin_omp_structured_block (parser);
39946	  tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39947					 cclauses, if_p);
39948	  cp_parser_end_omp_structured_block (parser, save);
39949	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39950				      block);
39951	  if (ret == NULL_TREE)
39952	    return ret;
39953	  OMP_PARALLEL_COMBINED (stmt) = 1;
39954	  return stmt;
39955	}
39956      else if (!flag_openmp)  /* flag_openmp_simd  */
39957	{
39958	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39959	  return NULL_TREE;
39960	}
39961      else if (cclauses == NULL && strcmp (p, "sections") == 0)
39962	{
39963	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39964	  cclauses = cclauses_buf;
39965
39966	  cp_lexer_consume_token (parser->lexer);
39967	  block = begin_omp_parallel ();
39968	  save = cp_parser_begin_omp_structured_block (parser);
39969	  cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
39970	  cp_parser_end_omp_structured_block (parser, save);
39971	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39972				      block);
39973	  OMP_PARALLEL_COMBINED (stmt) = 1;
39974	  return stmt;
39975	}
39976    }
39977  else if (!flag_openmp)  /* flag_openmp_simd  */
39978    {
39979      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39980      return NULL_TREE;
39981    }
39982
39983  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39984				       cclauses == NULL);
39985  if (cclauses)
39986    {
39987      cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
39988      clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
39989    }
39990
39991  block = begin_omp_parallel ();
39992  save = cp_parser_begin_omp_structured_block (parser);
39993  cp_parser_statement (parser, NULL_TREE, false, if_p);
39994  cp_parser_end_omp_structured_block (parser, save);
39995  stmt = finish_omp_parallel (clauses, block);
39996  return stmt;
39997}
39998
39999/* OpenMP 2.5:
40000   # pragma omp single single-clause[optseq] new-line
40001     structured-block  */
40002
40003#define OMP_SINGLE_CLAUSE_MASK					\
40004	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
40005	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
40006	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)	\
40007	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40008
40009static tree
40010cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40011{
40012  tree stmt = make_node (OMP_SINGLE);
40013  TREE_TYPE (stmt) = void_type_node;
40014  SET_EXPR_LOCATION (stmt, pragma_tok->location);
40015
40016  OMP_SINGLE_CLAUSES (stmt)
40017    = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
40018				 "#pragma omp single", pragma_tok);
40019  OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40020
40021  return add_stmt (stmt);
40022}
40023
40024/* OpenMP 3.0:
40025   # pragma omp task task-clause[optseq] new-line
40026     structured-block  */
40027
40028#define OMP_TASK_CLAUSE_MASK					\
40029	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
40030	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
40031	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
40032	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
40033	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
40034	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
40035	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
40036	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
40037	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
40038	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY)	\
40039	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40040
40041static tree
40042cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40043{
40044  tree clauses, block;
40045  unsigned int save;
40046
40047  clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
40048				       "#pragma omp task", pragma_tok);
40049  block = begin_omp_task ();
40050  save = cp_parser_begin_omp_structured_block (parser);
40051  cp_parser_statement (parser, NULL_TREE, false, if_p);
40052  cp_parser_end_omp_structured_block (parser, save);
40053  return finish_omp_task (clauses, block);
40054}
40055
40056/* OpenMP 3.0:
40057   # pragma omp taskwait new-line
40058
40059   OpenMP 5.0:
40060   # pragma omp taskwait taskwait-clause[opt] new-line  */
40061
40062#define OMP_TASKWAIT_CLAUSE_MASK				\
40063	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40064
40065static void
40066cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
40067{
40068  tree clauses
40069    = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
40070				 "#pragma omp taskwait", pragma_tok);
40071
40072  if (clauses)
40073    {
40074      tree stmt = make_node (OMP_TASK);
40075      TREE_TYPE (stmt) = void_node;
40076      OMP_TASK_CLAUSES (stmt) = clauses;
40077      OMP_TASK_BODY (stmt) = NULL_TREE;
40078      SET_EXPR_LOCATION (stmt, pragma_tok->location);
40079      add_stmt (stmt);
40080    }
40081  else
40082    finish_omp_taskwait ();
40083}
40084
40085/* OpenMP 3.1:
40086   # pragma omp taskyield new-line  */
40087
40088static void
40089cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
40090{
40091  cp_parser_require_pragma_eol (parser, pragma_tok);
40092  finish_omp_taskyield ();
40093}
40094
40095/* OpenMP 4.0:
40096   # pragma omp taskgroup new-line
40097     structured-block
40098
40099   OpenMP 5.0:
40100   # pragma omp taskgroup taskgroup-clause[optseq] new-line  */
40101
40102#define OMP_TASKGROUP_CLAUSE_MASK				\
40103	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
40104
40105static tree
40106cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40107{
40108  tree clauses
40109    = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
40110				 "#pragma omp taskgroup", pragma_tok);
40111  return c_finish_omp_taskgroup (input_location,
40112				 cp_parser_omp_structured_block (parser,
40113								 if_p),
40114				 clauses);
40115}
40116
40117
40118/* OpenMP 2.5:
40119   # pragma omp threadprivate (variable-list) */
40120
40121static void
40122cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
40123{
40124  tree vars;
40125
40126  vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
40127  cp_parser_require_pragma_eol (parser, pragma_tok);
40128
40129  finish_omp_threadprivate (vars);
40130}
40131
40132/* OpenMP 4.0:
40133   # pragma omp cancel cancel-clause[optseq] new-line  */
40134
40135#define OMP_CANCEL_CLAUSE_MASK					\
40136	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
40137	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
40138	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
40139	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)	\
40140	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
40141
40142static void
40143cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
40144{
40145  tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
40146					    "#pragma omp cancel", pragma_tok);
40147  finish_omp_cancel (clauses);
40148}
40149
40150/* OpenMP 4.0:
40151   # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
40152
40153#define OMP_CANCELLATION_POINT_CLAUSE_MASK			\
40154	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
40155	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
40156	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
40157	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
40158
40159static void
40160cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
40161				  enum pragma_context context)
40162{
40163  tree clauses;
40164  bool point_seen = false;
40165
40166  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40167    {
40168      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40169      const char *p = IDENTIFIER_POINTER (id);
40170
40171      if (strcmp (p, "point") == 0)
40172	{
40173	  cp_lexer_consume_token (parser->lexer);
40174	  point_seen = true;
40175	}
40176    }
40177  if (!point_seen)
40178    {
40179      cp_parser_error (parser, "expected %<point%>");
40180      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40181      return;
40182    }
40183
40184  if (context != pragma_compound)
40185    {
40186      if (context == pragma_stmt)
40187	error_at (pragma_tok->location,
40188		  "%<#pragma %s%> may only be used in compound statements",
40189		  "omp cancellation point");
40190      else
40191	cp_parser_error (parser, "expected declaration specifiers");
40192      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40193      return;
40194    }
40195
40196  clauses = cp_parser_omp_all_clauses (parser,
40197				       OMP_CANCELLATION_POINT_CLAUSE_MASK,
40198				       "#pragma omp cancellation point",
40199				       pragma_tok);
40200  finish_omp_cancellation_point (clauses);
40201}
40202
40203/* OpenMP 4.0:
40204   #pragma omp distribute distribute-clause[optseq] new-line
40205     for-loop  */
40206
40207#define OMP_DISTRIBUTE_CLAUSE_MASK				\
40208	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
40209	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
40210	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
40211	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
40212	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
40213
40214static tree
40215cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
40216			  char *p_name, omp_clause_mask mask, tree *cclauses,
40217			  bool *if_p)
40218{
40219  tree clauses, sb, ret;
40220  unsigned int save;
40221  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40222
40223  strcat (p_name, " distribute");
40224  mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
40225
40226  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40227    {
40228      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40229      const char *p = IDENTIFIER_POINTER (id);
40230      bool simd = false;
40231      bool parallel = false;
40232
40233      if (strcmp (p, "simd") == 0)
40234	simd = true;
40235      else
40236	parallel = strcmp (p, "parallel") == 0;
40237      if (parallel || simd)
40238	{
40239	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40240	  if (cclauses == NULL)
40241	    cclauses = cclauses_buf;
40242	  cp_lexer_consume_token (parser->lexer);
40243	  if (!flag_openmp)  /* flag_openmp_simd  */
40244	    {
40245	      if (simd)
40246		return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40247					   cclauses, if_p);
40248	      else
40249		return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
40250					       cclauses, if_p);
40251	    }
40252	  sb = begin_omp_structured_block ();
40253	  save = cp_parser_begin_omp_structured_block (parser);
40254	  if (simd)
40255	    ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40256				      cclauses, if_p);
40257	  else
40258	    ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
40259					  cclauses, if_p);
40260	  cp_parser_end_omp_structured_block (parser, save);
40261	  tree body = finish_omp_structured_block (sb);
40262	  if (ret == NULL)
40263	    return ret;
40264	  ret = make_node (OMP_DISTRIBUTE);
40265	  TREE_TYPE (ret) = void_type_node;
40266	  OMP_FOR_BODY (ret) = body;
40267	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
40268	  SET_EXPR_LOCATION (ret, loc);
40269	  add_stmt (ret);
40270	  return ret;
40271	}
40272    }
40273  if (!flag_openmp)  /* flag_openmp_simd  */
40274    {
40275      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40276      return NULL_TREE;
40277    }
40278
40279  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40280				       cclauses == NULL);
40281  if (cclauses)
40282    {
40283      cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
40284      clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
40285    }
40286
40287  keep_next_level (true);
40288  sb = begin_omp_structured_block ();
40289  save = cp_parser_begin_omp_structured_block (parser);
40290
40291  ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
40292
40293  cp_parser_end_omp_structured_block (parser, save);
40294  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40295
40296  return ret;
40297}
40298
40299/* OpenMP 4.0:
40300   # pragma omp teams teams-clause[optseq] new-line
40301     structured-block  */
40302
40303#define OMP_TEAMS_CLAUSE_MASK					\
40304	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
40305	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
40306	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
40307	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
40308	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)	\
40309	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT)	\
40310	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
40311
40312static tree
40313cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
40314		     char *p_name, omp_clause_mask mask, tree *cclauses,
40315		     bool *if_p)
40316{
40317  tree clauses, sb, ret;
40318  unsigned int save;
40319  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40320
40321  strcat (p_name, " teams");
40322  mask |= OMP_TEAMS_CLAUSE_MASK;
40323
40324  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40325    {
40326      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40327      const char *p = IDENTIFIER_POINTER (id);
40328      if (strcmp (p, "distribute") == 0)
40329	{
40330	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40331	  if (cclauses == NULL)
40332	    cclauses = cclauses_buf;
40333
40334	  cp_lexer_consume_token (parser->lexer);
40335	  if (!flag_openmp)  /* flag_openmp_simd  */
40336	    return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
40337					     cclauses, if_p);
40338	  keep_next_level (true);
40339	  sb = begin_omp_structured_block ();
40340	  save = cp_parser_begin_omp_structured_block (parser);
40341	  ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
40342					  cclauses, if_p);
40343	  cp_parser_end_omp_structured_block (parser, save);
40344	  tree body = finish_omp_structured_block (sb);
40345	  if (ret == NULL)
40346	    return ret;
40347	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40348	  ret = make_node (OMP_TEAMS);
40349	  TREE_TYPE (ret) = void_type_node;
40350	  OMP_TEAMS_CLAUSES (ret) = clauses;
40351	  OMP_TEAMS_BODY (ret) = body;
40352	  OMP_TEAMS_COMBINED (ret) = 1;
40353	  SET_EXPR_LOCATION (ret, loc);
40354	  return add_stmt (ret);
40355	}
40356      else if (strcmp (p, "loop") == 0)
40357	{
40358	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40359	  if (cclauses == NULL)
40360	    cclauses = cclauses_buf;
40361
40362	  cp_lexer_consume_token (parser->lexer);
40363	  if (!flag_openmp)  /* flag_openmp_simd  */
40364	    return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40365				       cclauses, if_p);
40366	  keep_next_level (true);
40367	  sb = begin_omp_structured_block ();
40368	  save = cp_parser_begin_omp_structured_block (parser);
40369	  ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40370				    cclauses, if_p);
40371	  cp_parser_end_omp_structured_block (parser, save);
40372	  tree body = finish_omp_structured_block (sb);
40373	  if (ret == NULL)
40374	    return ret;
40375	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40376	  ret = make_node (OMP_TEAMS);
40377	  TREE_TYPE (ret) = void_type_node;
40378	  OMP_TEAMS_CLAUSES (ret) = clauses;
40379	  OMP_TEAMS_BODY (ret) = body;
40380	  OMP_TEAMS_COMBINED (ret) = 1;
40381	  SET_EXPR_LOCATION (ret, loc);
40382	  return add_stmt (ret);
40383	}
40384    }
40385  if (!flag_openmp)  /* flag_openmp_simd  */
40386    {
40387      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40388      return NULL_TREE;
40389    }
40390
40391  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40392				       cclauses == NULL);
40393  if (cclauses)
40394    {
40395      cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
40396      clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40397    }
40398
40399  tree stmt = make_node (OMP_TEAMS);
40400  TREE_TYPE (stmt) = void_type_node;
40401  OMP_TEAMS_CLAUSES (stmt) = clauses;
40402  keep_next_level (true);
40403  OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40404  SET_EXPR_LOCATION (stmt, loc);
40405
40406  return add_stmt (stmt);
40407}
40408
40409/* OpenMP 4.0:
40410   # pragma omp target data target-data-clause[optseq] new-line
40411     structured-block  */
40412
40413#define OMP_TARGET_DATA_CLAUSE_MASK				\
40414	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
40415	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
40416	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
40417	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
40418	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
40419
40420static tree
40421cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40422{
40423  tree clauses
40424    = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
40425				 "#pragma omp target data", pragma_tok);
40426  int map_seen = 0;
40427  for (tree *pc = &clauses; *pc;)
40428    {
40429      if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40430	switch (OMP_CLAUSE_MAP_KIND (*pc))
40431	  {
40432	  case GOMP_MAP_TO:
40433	  case GOMP_MAP_ALWAYS_TO:
40434	  case GOMP_MAP_FROM:
40435	  case GOMP_MAP_ALWAYS_FROM:
40436	  case GOMP_MAP_TOFROM:
40437	  case GOMP_MAP_ALWAYS_TOFROM:
40438	  case GOMP_MAP_ALLOC:
40439	    map_seen = 3;
40440	    break;
40441	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
40442	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40443	  case GOMP_MAP_ALWAYS_POINTER:
40444	    break;
40445	  default:
40446	    map_seen |= 1;
40447	    error_at (OMP_CLAUSE_LOCATION (*pc),
40448		      "%<#pragma omp target data%> with map-type other "
40449		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40450		      "on %<map%> clause");
40451	    *pc = OMP_CLAUSE_CHAIN (*pc);
40452	    continue;
40453	  }
40454      else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
40455	       || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
40456	map_seen = 3;
40457      pc = &OMP_CLAUSE_CHAIN (*pc);
40458    }
40459
40460  if (map_seen != 3)
40461    {
40462      if (map_seen == 0)
40463	error_at (pragma_tok->location,
40464		  "%<#pragma omp target data%> must contain at least "
40465		  "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
40466		  "clause");
40467      return NULL_TREE;
40468    }
40469
40470  tree stmt = make_node (OMP_TARGET_DATA);
40471  TREE_TYPE (stmt) = void_type_node;
40472  OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
40473
40474  keep_next_level (true);
40475  OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40476
40477  SET_EXPR_LOCATION (stmt, pragma_tok->location);
40478  return add_stmt (stmt);
40479}
40480
40481/* OpenMP 4.5:
40482   # pragma omp target enter data target-enter-data-clause[optseq] new-line
40483     structured-block  */
40484
40485#define OMP_TARGET_ENTER_DATA_CLAUSE_MASK			\
40486	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
40487	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
40488	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
40489	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
40490	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40491
40492static tree
40493cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
40494				 enum pragma_context context)
40495{
40496  bool data_seen = false;
40497  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40498    {
40499      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40500      const char *p = IDENTIFIER_POINTER (id);
40501
40502      if (strcmp (p, "data") == 0)
40503	{
40504	  cp_lexer_consume_token (parser->lexer);
40505	  data_seen = true;
40506	}
40507    }
40508  if (!data_seen)
40509    {
40510      cp_parser_error (parser, "expected %<data%>");
40511      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40512      return NULL_TREE;
40513    }
40514
40515  if (context == pragma_stmt)
40516    {
40517      error_at (pragma_tok->location,
40518		"%<#pragma %s%> may only be used in compound statements",
40519		"omp target enter data");
40520      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40521      return NULL_TREE;
40522    }
40523
40524  tree clauses
40525    = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
40526				 "#pragma omp target enter data", pragma_tok);
40527  int map_seen = 0;
40528  for (tree *pc = &clauses; *pc;)
40529    {
40530      if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40531	switch (OMP_CLAUSE_MAP_KIND (*pc))
40532	  {
40533	  case GOMP_MAP_TO:
40534	  case GOMP_MAP_ALWAYS_TO:
40535	  case GOMP_MAP_ALLOC:
40536	    map_seen = 3;
40537	    break;
40538	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
40539	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40540	  case GOMP_MAP_ALWAYS_POINTER:
40541	    break;
40542	  default:
40543	    map_seen |= 1;
40544	    error_at (OMP_CLAUSE_LOCATION (*pc),
40545		      "%<#pragma omp target enter data%> with map-type other "
40546		      "than %<to%> or %<alloc%> on %<map%> clause");
40547	    *pc = OMP_CLAUSE_CHAIN (*pc);
40548	    continue;
40549	  }
40550      pc = &OMP_CLAUSE_CHAIN (*pc);
40551    }
40552
40553  if (map_seen != 3)
40554    {
40555      if (map_seen == 0)
40556	error_at (pragma_tok->location,
40557		  "%<#pragma omp target enter data%> must contain at least "
40558		  "one %<map%> clause");
40559      return NULL_TREE;
40560    }
40561
40562  tree stmt = make_node (OMP_TARGET_ENTER_DATA);
40563  TREE_TYPE (stmt) = void_type_node;
40564  OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
40565  SET_EXPR_LOCATION (stmt, pragma_tok->location);
40566  return add_stmt (stmt);
40567}
40568
40569/* OpenMP 4.5:
40570   # pragma omp target exit data target-enter-data-clause[optseq] new-line
40571     structured-block  */
40572
40573#define OMP_TARGET_EXIT_DATA_CLAUSE_MASK			\
40574	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
40575	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
40576	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
40577	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
40578	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40579
40580static tree
40581cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
40582				enum pragma_context context)
40583{
40584  bool data_seen = false;
40585  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40586    {
40587      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40588      const char *p = IDENTIFIER_POINTER (id);
40589
40590      if (strcmp (p, "data") == 0)
40591	{
40592	  cp_lexer_consume_token (parser->lexer);
40593	  data_seen = true;
40594	}
40595    }
40596  if (!data_seen)
40597    {
40598      cp_parser_error (parser, "expected %<data%>");
40599      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40600      return NULL_TREE;
40601    }
40602
40603  if (context == pragma_stmt)
40604    {
40605      error_at (pragma_tok->location,
40606		"%<#pragma %s%> may only be used in compound statements",
40607		"omp target exit data");
40608      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40609      return NULL_TREE;
40610    }
40611
40612  tree clauses
40613    = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
40614				 "#pragma omp target exit data", pragma_tok);
40615  int map_seen = 0;
40616  for (tree *pc = &clauses; *pc;)
40617    {
40618      if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40619	switch (OMP_CLAUSE_MAP_KIND (*pc))
40620	  {
40621	  case GOMP_MAP_FROM:
40622	  case GOMP_MAP_ALWAYS_FROM:
40623	  case GOMP_MAP_RELEASE:
40624	  case GOMP_MAP_DELETE:
40625	    map_seen = 3;
40626	    break;
40627	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
40628	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40629	  case GOMP_MAP_ALWAYS_POINTER:
40630	    break;
40631	  default:
40632	    map_seen |= 1;
40633	    error_at (OMP_CLAUSE_LOCATION (*pc),
40634		      "%<#pragma omp target exit data%> with map-type other "
40635		      "than %<from%>, %<release%> or %<delete%> on %<map%>"
40636		      " clause");
40637	    *pc = OMP_CLAUSE_CHAIN (*pc);
40638	    continue;
40639	  }
40640      pc = &OMP_CLAUSE_CHAIN (*pc);
40641    }
40642
40643  if (map_seen != 3)
40644    {
40645      if (map_seen == 0)
40646	error_at (pragma_tok->location,
40647		  "%<#pragma omp target exit data%> must contain at least "
40648		  "one %<map%> clause");
40649      return NULL_TREE;
40650    }
40651
40652  tree stmt = make_node (OMP_TARGET_EXIT_DATA);
40653  TREE_TYPE (stmt) = void_type_node;
40654  OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
40655  SET_EXPR_LOCATION (stmt, pragma_tok->location);
40656  return add_stmt (stmt);
40657}
40658
40659/* OpenMP 4.0:
40660   # pragma omp target update target-update-clause[optseq] new-line */
40661
40662#define OMP_TARGET_UPDATE_CLAUSE_MASK				\
40663	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)		\
40664	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
40665	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
40666	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
40667	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
40668	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40669
40670static bool
40671cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
40672			     enum pragma_context context)
40673{
40674  if (context == pragma_stmt)
40675    {
40676      error_at (pragma_tok->location,
40677		"%<#pragma %s%> may only be used in compound statements",
40678		"omp target update");
40679      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40680      return false;
40681    }
40682
40683  tree clauses
40684    = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
40685				 "#pragma omp target update", pragma_tok);
40686  if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
40687      && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
40688    {
40689      error_at (pragma_tok->location,
40690		"%<#pragma omp target update%> must contain at least one "
40691		"%<from%> or %<to%> clauses");
40692      return false;
40693    }
40694
40695  tree stmt = make_node (OMP_TARGET_UPDATE);
40696  TREE_TYPE (stmt) = void_type_node;
40697  OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
40698  SET_EXPR_LOCATION (stmt, pragma_tok->location);
40699  add_stmt (stmt);
40700  return false;
40701}
40702
40703/* OpenMP 4.0:
40704   # pragma omp target target-clause[optseq] new-line
40705     structured-block  */
40706
40707#define OMP_TARGET_CLAUSE_MASK					\
40708	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
40709	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
40710	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
40711	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
40712	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
40713	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
40714	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
40715	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)	\
40716	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
40717
40718static bool
40719cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
40720		      enum pragma_context context, bool *if_p)
40721{
40722  tree *pc = NULL, stmt;
40723
40724  if (flag_openmp)
40725    omp_requires_mask
40726      = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
40727
40728  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40729    {
40730      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40731      const char *p = IDENTIFIER_POINTER (id);
40732      enum tree_code ccode = ERROR_MARK;
40733
40734      if (strcmp (p, "teams") == 0)
40735	ccode = OMP_TEAMS;
40736      else if (strcmp (p, "parallel") == 0)
40737	ccode = OMP_PARALLEL;
40738      else if (strcmp (p, "simd") == 0)
40739	ccode = OMP_SIMD;
40740      if (ccode != ERROR_MARK)
40741	{
40742	  tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
40743	  char p_name[sizeof ("#pragma omp target teams distribute "
40744			      "parallel for simd")];
40745
40746	  cp_lexer_consume_token (parser->lexer);
40747	  strcpy (p_name, "#pragma omp target");
40748	  if (!flag_openmp)  /* flag_openmp_simd  */
40749	    {
40750	      tree stmt;
40751	      switch (ccode)
40752		{
40753		case OMP_TEAMS:
40754		  stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
40755					      OMP_TARGET_CLAUSE_MASK,
40756					      cclauses, if_p);
40757		  break;
40758		case OMP_PARALLEL:
40759		  stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
40760						 OMP_TARGET_CLAUSE_MASK,
40761						 cclauses, if_p);
40762		  break;
40763		case OMP_SIMD:
40764		  stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
40765					     OMP_TARGET_CLAUSE_MASK,
40766					     cclauses, if_p);
40767		  break;
40768		default:
40769		  gcc_unreachable ();
40770		}
40771	      return stmt != NULL_TREE;
40772	    }
40773	  keep_next_level (true);
40774	  tree sb = begin_omp_structured_block (), ret;
40775	  unsigned save = cp_parser_begin_omp_structured_block (parser);
40776	  switch (ccode)
40777	    {
40778	    case OMP_TEAMS:
40779	      ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
40780					 OMP_TARGET_CLAUSE_MASK, cclauses,
40781					 if_p);
40782	      break;
40783	    case OMP_PARALLEL:
40784	      ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
40785					    OMP_TARGET_CLAUSE_MASK, cclauses,
40786					    if_p);
40787	      break;
40788	    case OMP_SIMD:
40789	      ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
40790					OMP_TARGET_CLAUSE_MASK, cclauses,
40791					if_p);
40792	      break;
40793	    default:
40794	      gcc_unreachable ();
40795	    }
40796	  cp_parser_end_omp_structured_block (parser, save);
40797	  tree body = finish_omp_structured_block (sb);
40798	  if (ret == NULL_TREE)
40799	    return false;
40800	  if (ccode == OMP_TEAMS && !processing_template_decl)
40801	    {
40802	      /* For combined target teams, ensure the num_teams and
40803		 thread_limit clause expressions are evaluated on the host,
40804		 before entering the target construct.  */
40805	      tree c;
40806	      for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40807		   c; c = OMP_CLAUSE_CHAIN (c))
40808		if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
40809		     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
40810		    && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
40811		  {
40812		    tree expr = OMP_CLAUSE_OPERAND (c, 0);
40813		    expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
40814		    if (expr == error_mark_node)
40815		      continue;
40816		    tree tmp = TARGET_EXPR_SLOT (expr);
40817		    add_stmt (expr);
40818		    OMP_CLAUSE_OPERAND (c, 0) = expr;
40819		    tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
40820						OMP_CLAUSE_FIRSTPRIVATE);
40821		    OMP_CLAUSE_DECL (tc) = tmp;
40822		    OMP_CLAUSE_CHAIN (tc)
40823		      = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
40824		    cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
40825		  }
40826	    }
40827	  tree stmt = make_node (OMP_TARGET);
40828	  TREE_TYPE (stmt) = void_type_node;
40829	  OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
40830	  OMP_TARGET_BODY (stmt) = body;
40831	  OMP_TARGET_COMBINED (stmt) = 1;
40832	  SET_EXPR_LOCATION (stmt, pragma_tok->location);
40833	  add_stmt (stmt);
40834	  pc = &OMP_TARGET_CLAUSES (stmt);
40835	  goto check_clauses;
40836	}
40837      else if (!flag_openmp)  /* flag_openmp_simd  */
40838	{
40839	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40840	  return false;
40841	}
40842      else if (strcmp (p, "data") == 0)
40843	{
40844	  cp_lexer_consume_token (parser->lexer);
40845	  cp_parser_omp_target_data (parser, pragma_tok, if_p);
40846	  return true;
40847	}
40848      else if (strcmp (p, "enter") == 0)
40849	{
40850	  cp_lexer_consume_token (parser->lexer);
40851	  cp_parser_omp_target_enter_data (parser, pragma_tok, context);
40852	  return false;
40853	}
40854      else if (strcmp (p, "exit") == 0)
40855	{
40856	  cp_lexer_consume_token (parser->lexer);
40857	  cp_parser_omp_target_exit_data (parser, pragma_tok, context);
40858	  return false;
40859	}
40860      else if (strcmp (p, "update") == 0)
40861	{
40862	  cp_lexer_consume_token (parser->lexer);
40863	  return cp_parser_omp_target_update (parser, pragma_tok, context);
40864	}
40865    }
40866  if (!flag_openmp)  /* flag_openmp_simd  */
40867    {
40868      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40869      return false;
40870    }
40871
40872  stmt = make_node (OMP_TARGET);
40873  TREE_TYPE (stmt) = void_type_node;
40874
40875  OMP_TARGET_CLAUSES (stmt)
40876    = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
40877				 "#pragma omp target", pragma_tok);
40878  pc = &OMP_TARGET_CLAUSES (stmt);
40879  keep_next_level (true);
40880  OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40881
40882  SET_EXPR_LOCATION (stmt, pragma_tok->location);
40883  add_stmt (stmt);
40884
40885check_clauses:
40886  while (*pc)
40887    {
40888      if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40889	switch (OMP_CLAUSE_MAP_KIND (*pc))
40890	  {
40891	  case GOMP_MAP_TO:
40892	  case GOMP_MAP_ALWAYS_TO:
40893	  case GOMP_MAP_FROM:
40894	  case GOMP_MAP_ALWAYS_FROM:
40895	  case GOMP_MAP_TOFROM:
40896	  case GOMP_MAP_ALWAYS_TOFROM:
40897	  case GOMP_MAP_ALLOC:
40898	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
40899	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40900	  case GOMP_MAP_ALWAYS_POINTER:
40901	    break;
40902	  default:
40903	    error_at (OMP_CLAUSE_LOCATION (*pc),
40904		      "%<#pragma omp target%> with map-type other "
40905		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40906		      "on %<map%> clause");
40907	    *pc = OMP_CLAUSE_CHAIN (*pc);
40908	    continue;
40909	  }
40910      pc = &OMP_CLAUSE_CHAIN (*pc);
40911    }
40912  return true;
40913}
40914
40915/* OpenACC 2.0:
40916   # pragma acc cache (variable-list) new-line
40917*/
40918
40919static tree
40920cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
40921{
40922  /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
40923     clauses.  */
40924  auto_suppress_location_wrappers sentinel;
40925
40926  tree stmt, clauses;
40927
40928  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
40929  clauses = finish_omp_clauses (clauses, C_ORT_ACC);
40930
40931  cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
40932
40933  stmt = make_node (OACC_CACHE);
40934  TREE_TYPE (stmt) = void_type_node;
40935  OACC_CACHE_CLAUSES (stmt) = clauses;
40936  SET_EXPR_LOCATION (stmt, pragma_tok->location);
40937  add_stmt (stmt);
40938
40939  return stmt;
40940}
40941
40942/* OpenACC 2.0:
40943   # pragma acc data oacc-data-clause[optseq] new-line
40944     structured-block  */
40945
40946#define OACC_DATA_CLAUSE_MASK						\
40947	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
40948	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
40949	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
40950	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
40951	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
40952	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH)		\
40953	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
40954	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
40955	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
40956	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
40957
40958static tree
40959cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40960{
40961  tree stmt, clauses, block;
40962  unsigned int save;
40963
40964  clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
40965					"#pragma acc data", pragma_tok);
40966
40967  block = begin_omp_parallel ();
40968  save = cp_parser_begin_omp_structured_block (parser);
40969  cp_parser_statement (parser, NULL_TREE, false, if_p);
40970  cp_parser_end_omp_structured_block (parser, save);
40971  stmt = finish_oacc_data (clauses, block);
40972  return stmt;
40973}
40974
40975/* OpenACC 2.0:
40976  # pragma acc host_data <clauses> new-line
40977  structured-block  */
40978
40979#define OACC_HOST_DATA_CLAUSE_MASK					\
40980  ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE)                \
40981   | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                        \
40982   | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
40983
40984static tree
40985cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40986{
40987  tree stmt, clauses, block;
40988  unsigned int save;
40989
40990  clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
40991					"#pragma acc host_data", pragma_tok);
40992
40993  block = begin_omp_parallel ();
40994  save = cp_parser_begin_omp_structured_block (parser);
40995  cp_parser_statement (parser, NULL_TREE, false, if_p);
40996  cp_parser_end_omp_structured_block (parser, save);
40997  stmt = finish_oacc_host_data (clauses, block);
40998  return stmt;
40999}
41000
41001/* OpenACC 2.0:
41002   # pragma acc declare oacc-data-clause[optseq] new-line
41003*/
41004
41005#define OACC_DECLARE_CLAUSE_MASK					\
41006	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
41007	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
41008	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
41009	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
41010	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
41011	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)	\
41012	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)		\
41013	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
41014
41015static tree
41016cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
41017{
41018  tree clauses, stmt;
41019  bool error = false;
41020  bool found_in_scope = global_bindings_p ();
41021
41022  clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
41023					"#pragma acc declare", pragma_tok, true);
41024
41025
41026  if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41027    {
41028      error_at (pragma_tok->location,
41029		"no valid clauses specified in %<#pragma acc declare%>");
41030      return NULL_TREE;
41031    }
41032
41033  for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
41034    {
41035      location_t loc = OMP_CLAUSE_LOCATION (t);
41036      tree decl = OMP_CLAUSE_DECL (t);
41037      if (!DECL_P (decl))
41038	{
41039	  error_at (loc, "array section in %<#pragma acc declare%>");
41040	  error = true;
41041	  continue;
41042	}
41043      gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
41044      switch (OMP_CLAUSE_MAP_KIND (t))
41045	{
41046	case GOMP_MAP_FIRSTPRIVATE_POINTER:
41047	case GOMP_MAP_ALLOC:
41048	case GOMP_MAP_TO:
41049	case GOMP_MAP_FORCE_DEVICEPTR:
41050	case GOMP_MAP_DEVICE_RESIDENT:
41051	  break;
41052
41053	case GOMP_MAP_LINK:
41054	  if (!global_bindings_p ()
41055	      && (TREE_STATIC (decl)
41056	       || !DECL_EXTERNAL (decl)))
41057	    {
41058	      error_at (loc,
41059			"%qD must be a global variable in "
41060			"%<#pragma acc declare link%>",
41061			decl);
41062	      error = true;
41063	      continue;
41064	    }
41065	  break;
41066
41067	default:
41068	  if (global_bindings_p ())
41069	    {
41070	      error_at (loc, "invalid OpenACC clause at file scope");
41071	      error = true;
41072	      continue;
41073	    }
41074	  if (DECL_EXTERNAL (decl))
41075	    {
41076	      error_at (loc,
41077			"invalid use of %<extern%> variable %qD "
41078			"in %<#pragma acc declare%>", decl);
41079	      error = true;
41080	      continue;
41081	    }
41082	  else if (TREE_PUBLIC (decl))
41083	    {
41084	      error_at (loc,
41085			"invalid use of %<global%> variable %qD "
41086			"in %<#pragma acc declare%>", decl);
41087	      error = true;
41088	      continue;
41089	    }
41090	  break;
41091	}
41092
41093      if (!found_in_scope)
41094	for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
41095	  if (d == decl)
41096	    {
41097	      found_in_scope = true;
41098	      break;
41099	    }
41100      if (!found_in_scope)
41101	{
41102	  error_at (loc,
41103		    "%qD must be a variable declared in the same scope as "
41104		    "%<#pragma acc declare%>", decl);
41105	  error = true;
41106	  continue;
41107	}
41108
41109      if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
41110	  || lookup_attribute ("omp declare target link",
41111			       DECL_ATTRIBUTES (decl)))
41112	{
41113	  error_at (loc, "variable %qD used more than once with "
41114		    "%<#pragma acc declare%>", decl);
41115	  error = true;
41116	  continue;
41117	}
41118
41119      if (!error)
41120	{
41121	  tree id;
41122
41123	  if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
41124	    id = get_identifier ("omp declare target link");
41125	  else
41126	    id = get_identifier ("omp declare target");
41127
41128	  DECL_ATTRIBUTES (decl)
41129	    = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
41130	  if (current_binding_level->kind == sk_namespace)
41131	    {
41132	      symtab_node *node = symtab_node::get (decl);
41133	      if (node != NULL)
41134		{
41135		  node->offloadable = 1;
41136		  if (ENABLE_OFFLOADING)
41137		    {
41138		      g->have_offload = true;
41139		      if (is_a <varpool_node *> (node))
41140			vec_safe_push (offload_vars, decl);
41141		    }
41142		}
41143	    }
41144	}
41145    }
41146
41147  if (error || current_binding_level->kind == sk_namespace)
41148    return NULL_TREE;
41149
41150  stmt = make_node (OACC_DECLARE);
41151  TREE_TYPE (stmt) = void_type_node;
41152  OACC_DECLARE_CLAUSES (stmt) = clauses;
41153  SET_EXPR_LOCATION (stmt, pragma_tok->location);
41154
41155  add_stmt (stmt);
41156
41157  return NULL_TREE;
41158}
41159
41160/* OpenACC 2.0:
41161   # pragma acc enter data oacc-enter-data-clause[optseq] new-line
41162
41163   or
41164
41165   # pragma acc exit data oacc-exit-data-clause[optseq] new-line
41166
41167   LOC is the location of the #pragma token.
41168*/
41169
41170#define OACC_ENTER_DATA_CLAUSE_MASK					\
41171	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
41172	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
41173	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
41174	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
41175	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
41176	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41177
41178#define OACC_EXIT_DATA_CLAUSE_MASK					\
41179	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
41180	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
41181	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
41182	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) 		\
41183	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH)		\
41184	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) 		\
41185	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41186
41187static tree
41188cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
41189				bool enter)
41190{
41191  location_t loc = pragma_tok->location;
41192  tree stmt, clauses;
41193  const char *p = "";
41194
41195  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41196    p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41197
41198  if (strcmp (p, "data") != 0)
41199    {
41200      error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
41201		enter ? "enter" : "exit");
41202      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41203      return NULL_TREE;
41204    }
41205
41206  cp_lexer_consume_token (parser->lexer);
41207
41208  if (enter)
41209    clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
41210					 "#pragma acc enter data", pragma_tok);
41211  else
41212    clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
41213					 "#pragma acc exit data", pragma_tok);
41214
41215  if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41216    {
41217      error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
41218		enter ? "enter" : "exit");
41219      return NULL_TREE;
41220    }
41221
41222  stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
41223  TREE_TYPE (stmt) = void_type_node;
41224  OMP_STANDALONE_CLAUSES (stmt) = clauses;
41225  SET_EXPR_LOCATION (stmt, loc);
41226  add_stmt (stmt);
41227  return stmt;
41228}
41229
41230/* OpenACC 2.0:
41231   # pragma acc loop oacc-loop-clause[optseq] new-line
41232     structured-block  */
41233
41234#define OACC_LOOP_CLAUSE_MASK						\
41235	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)		\
41236	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
41237	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
41238	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
41239	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
41240	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
41241	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)		\
41242	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)		\
41243	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)			\
41244	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
41245
41246static tree
41247cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
41248		     omp_clause_mask mask, tree *cclauses, bool *if_p)
41249{
41250  bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
41251
41252  strcat (p_name, " loop");
41253  mask |= OACC_LOOP_CLAUSE_MASK;
41254
41255  tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
41256					     cclauses == NULL);
41257  if (cclauses)
41258    {
41259      clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
41260      if (*cclauses)
41261	*cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
41262      if (clauses)
41263	clauses = finish_omp_clauses (clauses, C_ORT_ACC);
41264    }
41265
41266  tree block = begin_omp_structured_block ();
41267  int save = cp_parser_begin_omp_structured_block (parser);
41268  tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
41269  cp_parser_end_omp_structured_block (parser, save);
41270  add_stmt (finish_omp_structured_block (block));
41271
41272  return stmt;
41273}
41274
41275/* OpenACC 2.0:
41276   # pragma acc kernels oacc-kernels-clause[optseq] new-line
41277     structured-block
41278
41279   or
41280
41281   # pragma acc parallel oacc-parallel-clause[optseq] new-line
41282     structured-block
41283
41284   OpenACC 2.6:
41285
41286   # pragma acc serial oacc-serial-clause[optseq] new-line
41287*/
41288
41289#define OACC_KERNELS_CLAUSE_MASK					\
41290	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
41291	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
41292	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
41293	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
41294	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
41295	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
41296	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
41297	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
41298	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
41299	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
41300	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
41301	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
41302	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
41303	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)	\
41304	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41305
41306#define OACC_PARALLEL_CLAUSE_MASK					\
41307	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
41308	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
41309	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
41310	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
41311	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
41312	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
41313	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
41314	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
41315	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)	\
41316	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
41317	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
41318	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
41319	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
41320	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
41321	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
41322	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
41323	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
41324	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41325
41326#define OACC_SERIAL_CLAUSE_MASK						\
41327	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
41328	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
41329	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
41330	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
41331	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
41332	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
41333	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
41334	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
41335	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
41336	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
41337	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
41338	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)	\
41339	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
41340	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
41341	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41342
41343static tree
41344cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
41345			char *p_name, bool *if_p)
41346{
41347  omp_clause_mask mask;
41348  enum tree_code code;
41349  switch (cp_parser_pragma_kind (pragma_tok))
41350    {
41351    case PRAGMA_OACC_KERNELS:
41352      strcat (p_name, " kernels");
41353      mask = OACC_KERNELS_CLAUSE_MASK;
41354      code = OACC_KERNELS;
41355      break;
41356    case PRAGMA_OACC_PARALLEL:
41357      strcat (p_name, " parallel");
41358      mask = OACC_PARALLEL_CLAUSE_MASK;
41359      code = OACC_PARALLEL;
41360      break;
41361    case PRAGMA_OACC_SERIAL:
41362      strcat (p_name, " serial");
41363      mask = OACC_SERIAL_CLAUSE_MASK;
41364      code = OACC_SERIAL;
41365      break;
41366    default:
41367      gcc_unreachable ();
41368    }
41369
41370  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41371    {
41372      const char *p
41373	= IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41374      if (strcmp (p, "loop") == 0)
41375	{
41376	  cp_lexer_consume_token (parser->lexer);
41377	  tree block = begin_omp_parallel ();
41378	  tree clauses;
41379	  tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
41380					   &clauses, if_p);
41381	  protected_set_expr_location (stmt, pragma_tok->location);
41382	  return finish_omp_construct (code, block, clauses);
41383	}
41384    }
41385
41386  tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
41387
41388  tree block = begin_omp_parallel ();
41389  unsigned int save = cp_parser_begin_omp_structured_block (parser);
41390  cp_parser_statement (parser, NULL_TREE, false, if_p);
41391  cp_parser_end_omp_structured_block (parser, save);
41392  return finish_omp_construct (code, block, clauses);
41393}
41394
41395/* OpenACC 2.0:
41396   # pragma acc update oacc-update-clause[optseq] new-line
41397*/
41398
41399#define OACC_UPDATE_CLAUSE_MASK						\
41400	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
41401	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)		\
41402	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)		\
41403	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
41404	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT)		\
41405	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
41406
41407static tree
41408cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
41409{
41410  tree stmt, clauses;
41411
41412  clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
41413					 "#pragma acc update", pragma_tok);
41414
41415  if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41416    {
41417      error_at (pragma_tok->location,
41418		"%<#pragma acc update%> must contain at least one "
41419		"%<device%> or %<host%> or %<self%> clause");
41420      return NULL_TREE;
41421    }
41422
41423  stmt = make_node (OACC_UPDATE);
41424  TREE_TYPE (stmt) = void_type_node;
41425  OACC_UPDATE_CLAUSES (stmt) = clauses;
41426  SET_EXPR_LOCATION (stmt, pragma_tok->location);
41427  add_stmt (stmt);
41428  return stmt;
41429}
41430
41431/* OpenACC 2.0:
41432   # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
41433
41434   LOC is the location of the #pragma token.
41435*/
41436
41437#define OACC_WAIT_CLAUSE_MASK					\
41438	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
41439
41440static tree
41441cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
41442{
41443  tree clauses, list = NULL_TREE, stmt = NULL_TREE;
41444  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41445
41446  if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41447    list = cp_parser_oacc_wait_list (parser, loc, list);
41448
41449  clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
41450					"#pragma acc wait", pragma_tok);
41451
41452  stmt = c_finish_oacc_wait (loc, list, clauses);
41453  stmt = finish_expr_stmt (stmt);
41454
41455  return stmt;
41456}
41457
41458/* OpenMP 4.0:
41459   # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
41460
41461#define OMP_DECLARE_SIMD_CLAUSE_MASK				\
41462	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
41463	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
41464	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
41465	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)	\
41466	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)	\
41467	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
41468
41469static void
41470cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
41471			    enum pragma_context context,
41472			    bool variant_p)
41473{
41474  bool first_p = parser->omp_declare_simd == NULL;
41475  cp_omp_declare_simd_data data;
41476  if (first_p)
41477    {
41478      data.error_seen = false;
41479      data.fndecl_seen = false;
41480      data.variant_p = variant_p;
41481      data.tokens = vNULL;
41482      data.clauses = NULL_TREE;
41483      /* It is safe to take the address of a local variable; it will only be
41484	 used while this scope is live.  */
41485      parser->omp_declare_simd = &data;
41486    }
41487  else if (parser->omp_declare_simd->variant_p != variant_p)
41488    {
41489      error_at (pragma_tok->location,
41490		"%<#pragma omp declare %s%> followed by "
41491		"%<#pragma omp declare %s%>",
41492		parser->omp_declare_simd->variant_p ? "variant" : "simd",
41493		parser->omp_declare_simd->variant_p ? "simd" : "variant");
41494      parser->omp_declare_simd->error_seen = true;
41495    }
41496
41497  /* Store away all pragma tokens.  */
41498  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
41499	 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
41500    cp_lexer_consume_token (parser->lexer);
41501  if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41502    parser->omp_declare_simd->error_seen = true;
41503  cp_parser_require_pragma_eol (parser, pragma_tok);
41504  struct cp_token_cache *cp
41505    = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
41506  parser->omp_declare_simd->tokens.safe_push (cp);
41507
41508  if (first_p)
41509    {
41510      while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
41511	cp_parser_pragma (parser, context, NULL);
41512      switch (context)
41513	{
41514	case pragma_external:
41515	  cp_parser_declaration (parser);
41516	  break;
41517	case pragma_member:
41518	  cp_parser_member_declaration (parser);
41519	  break;
41520	case pragma_objc_icode:
41521	  cp_parser_block_declaration (parser, /*statement_p=*/false);
41522	  break;
41523	default:
41524	  cp_parser_declaration_statement (parser);
41525	  break;
41526	}
41527      if (parser->omp_declare_simd
41528	  && !parser->omp_declare_simd->error_seen
41529	  && !parser->omp_declare_simd->fndecl_seen)
41530	error_at (pragma_tok->location,
41531		  "%<#pragma omp declare %s%> not immediately followed by "
41532		  "function declaration or definition",
41533		  parser->omp_declare_simd->variant_p ? "variant" : "simd");
41534      data.tokens.release ();
41535      parser->omp_declare_simd = NULL;
41536    }
41537}
41538
41539static const char *const omp_construct_selectors[] = {
41540  "simd", "target", "teams", "parallel", "for", NULL };
41541static const char *const omp_device_selectors[] = {
41542  "kind", "isa", "arch", NULL };
41543static const char *const omp_implementation_selectors[] = {
41544  "vendor", "extension", "atomic_default_mem_order", "unified_address",
41545  "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
41546static const char *const omp_user_selectors[] = {
41547  "condition", NULL };
41548
41549/* OpenMP 5.0:
41550
41551   trait-selector:
41552     trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
41553
41554   trait-score:
41555     score(score-expression)  */
41556
41557static tree
41558cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
41559{
41560  tree ret = NULL_TREE;
41561  do
41562    {
41563      tree selector;
41564      if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41565	  || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41566	selector = cp_lexer_peek_token (parser->lexer)->u.value;
41567      else
41568	{
41569	  cp_parser_error (parser, "expected trait selector name");
41570	  return error_mark_node;
41571	}
41572
41573      tree properties = NULL_TREE;
41574      const char *const *selectors = NULL;
41575      bool allow_score = true;
41576      bool allow_user = false;
41577      int property_limit = 0;
41578      enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
41579	     CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
41580	     CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
41581      switch (IDENTIFIER_POINTER (set)[0])
41582	{
41583	case 'c': /* construct */
41584	  selectors = omp_construct_selectors;
41585	  allow_score = false;
41586	  property_limit = 1;
41587	  property_kind = CTX_PROPERTY_SIMD;
41588	  break;
41589	case 'd': /* device */
41590	  selectors = omp_device_selectors;
41591	  allow_score = false;
41592	  allow_user = true;
41593	  property_limit = 3;
41594	  property_kind = CTX_PROPERTY_NAME_LIST;
41595	  break;
41596	case 'i': /* implementation */
41597	  selectors = omp_implementation_selectors;
41598	  allow_user = true;
41599	  property_limit = 3;
41600	  property_kind = CTX_PROPERTY_NAME_LIST;
41601	  break;
41602	case 'u': /* user */
41603	  selectors = omp_user_selectors;
41604	  property_limit = 1;
41605	  property_kind = CTX_PROPERTY_EXPR;
41606	  break;
41607	default:
41608	  gcc_unreachable ();
41609	}
41610      for (int i = 0; ; i++)
41611	{
41612	  if (selectors[i] == NULL)
41613	    {
41614	      if (allow_user)
41615		{
41616		  property_kind = CTX_PROPERTY_USER;
41617		  break;
41618		}
41619	      else
41620		{
41621		  error ("selector %qs not allowed for context selector "
41622			 "set %qs", IDENTIFIER_POINTER (selector),
41623			 IDENTIFIER_POINTER (set));
41624		  cp_lexer_consume_token (parser->lexer);
41625		  return error_mark_node;
41626		}
41627	    }
41628	  if (i == property_limit)
41629	    property_kind = CTX_PROPERTY_NONE;
41630	  if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
41631	    break;
41632	}
41633      if (property_kind == CTX_PROPERTY_NAME_LIST
41634	  && IDENTIFIER_POINTER (set)[0] == 'i'
41635	  && strcmp (IDENTIFIER_POINTER (selector),
41636		     "atomic_default_mem_order") == 0)
41637	property_kind = CTX_PROPERTY_ID;
41638
41639      cp_lexer_consume_token (parser->lexer);
41640
41641      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41642	{
41643	  if (property_kind == CTX_PROPERTY_NONE)
41644	    {
41645	      error ("selector %qs does not accept any properties",
41646		     IDENTIFIER_POINTER (selector));
41647	      return error_mark_node;
41648	    }
41649
41650	  matching_parens parens;
41651	  parens.consume_open (parser);
41652
41653	  cp_token *token = cp_lexer_peek_token (parser->lexer);
41654	  if (allow_score
41655	      && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41656	      && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
41657	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
41658	    {
41659	      cp_lexer_save_tokens (parser->lexer);
41660	      cp_lexer_consume_token (parser->lexer);
41661	      cp_lexer_consume_token (parser->lexer);
41662	      if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
41663							 true)
41664		  && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
41665		{
41666		  cp_lexer_rollback_tokens (parser->lexer);
41667		  cp_lexer_consume_token (parser->lexer);
41668
41669		  matching_parens parens2;
41670		  parens2.require_open (parser);
41671		  tree score = cp_parser_constant_expression (parser);
41672		  if (!parens2.require_close (parser))
41673		    cp_parser_skip_to_closing_parenthesis (parser, true,
41674							   false, true);
41675		  cp_parser_require (parser, CPP_COLON, RT_COLON);
41676		  if (score != error_mark_node)
41677		    {
41678		      score = fold_non_dependent_expr (score);
41679		      if (value_dependent_expression_p (score))
41680			properties = tree_cons (get_identifier (" score"),
41681						score, properties);
41682		      else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
41683			       || TREE_CODE (score) != INTEGER_CST)
41684			error_at (token->location, "score argument must be "
41685				  "constant integer expression");
41686		      else if (tree_int_cst_sgn (score) < 0)
41687			error_at (token->location, "score argument must be "
41688				  "non-negative");
41689		      else
41690			properties = tree_cons (get_identifier (" score"),
41691						score, properties);
41692		    }
41693		}
41694	      else
41695		cp_lexer_rollback_tokens (parser->lexer);
41696
41697	      token = cp_lexer_peek_token (parser->lexer);
41698	    }
41699
41700	  switch (property_kind)
41701	    {
41702	      tree t;
41703	    case CTX_PROPERTY_USER:
41704	      do
41705		{
41706		  t = cp_parser_constant_expression (parser);
41707		  if (t != error_mark_node)
41708		    {
41709		      t = fold_non_dependent_expr (t);
41710		      if (TREE_CODE (t) == STRING_CST)
41711			properties = tree_cons (NULL_TREE, t, properties);
41712		      else if (!value_dependent_expression_p (t)
41713			       && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
41714				   || !tree_fits_shwi_p (t)))
41715			error_at (token->location, "property must be "
41716				  "constant integer expression or string "
41717				  "literal");
41718		      else
41719			properties = tree_cons (NULL_TREE, t, properties);
41720		    }
41721		  else
41722		    return error_mark_node;
41723
41724		  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41725		    cp_lexer_consume_token (parser->lexer);
41726		  else
41727		    break;
41728		}
41729	      while (1);
41730	      break;
41731	    case CTX_PROPERTY_ID:
41732	      if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41733		  || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41734		{
41735		  tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
41736		  cp_lexer_consume_token (parser->lexer);
41737		  properties = tree_cons (prop, NULL_TREE, properties);
41738		}
41739	      else
41740		{
41741		  cp_parser_error (parser, "expected identifier");
41742		  return error_mark_node;
41743		}
41744	      break;
41745	    case CTX_PROPERTY_NAME_LIST:
41746	      do
41747		{
41748		  tree prop = NULL_TREE, value = NULL_TREE;
41749		  if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41750		      || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41751		    {
41752		      prop = cp_lexer_peek_token (parser->lexer)->u.value;
41753		      cp_lexer_consume_token (parser->lexer);
41754		    }
41755		  else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
41756		    value = cp_parser_string_literal (parser, false, false);
41757		  else
41758		    {
41759		      cp_parser_error (parser, "expected identifier or "
41760					       "string literal");
41761		      return error_mark_node;
41762		    }
41763
41764		  properties = tree_cons (prop, value, properties);
41765
41766		  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41767		    cp_lexer_consume_token (parser->lexer);
41768		  else
41769		    break;
41770		}
41771	      while (1);
41772	      break;
41773	    case CTX_PROPERTY_EXPR:
41774	      t = cp_parser_constant_expression (parser);
41775	      if (t != error_mark_node)
41776		{
41777		  t = fold_non_dependent_expr (t);
41778		  if (!value_dependent_expression_p (t)
41779		      && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
41780			  || !tree_fits_shwi_p (t)))
41781		    error_at (token->location, "property must be "
41782			      "constant integer expression");
41783		  else
41784		    properties = tree_cons (NULL_TREE, t, properties);
41785		}
41786	      else
41787		return error_mark_node;
41788	      break;
41789	    case CTX_PROPERTY_SIMD:
41790	      if (!has_parms_p)
41791		{
41792		  error_at (token->location, "properties for %<simd%> "
41793			    "selector may not be specified in "
41794			    "%<metadirective%>");
41795		  return error_mark_node;
41796		}
41797	      properties
41798		= cp_parser_omp_all_clauses (parser,
41799					     OMP_DECLARE_SIMD_CLAUSE_MASK,
41800					     "simd", NULL, true, 2);
41801	      break;
41802	    default:
41803	      gcc_unreachable ();
41804	    }
41805
41806	  if (!parens.require_close (parser))
41807	    cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41808
41809	  properties = nreverse (properties);
41810	}
41811      else if (property_kind == CTX_PROPERTY_NAME_LIST
41812	       || property_kind == CTX_PROPERTY_ID
41813	       || property_kind == CTX_PROPERTY_EXPR)
41814	{
41815	  cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
41816	  return error_mark_node;
41817	}
41818
41819      ret = tree_cons (selector, properties, ret);
41820
41821      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41822	cp_lexer_consume_token (parser->lexer);
41823      else
41824	break;
41825    }
41826  while (1);
41827
41828  return nreverse (ret);
41829}
41830
41831/* OpenMP 5.0:
41832
41833   trait-set-selector[,trait-set-selector[,...]]
41834
41835   trait-set-selector:
41836     trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
41837
41838   trait-set-selector-name:
41839     constructor
41840     device
41841     implementation
41842     user  */
41843
41844static tree
41845cp_parser_omp_context_selector_specification (cp_parser *parser,
41846					      bool has_parms_p)
41847{
41848  tree ret = NULL_TREE;
41849  do
41850    {
41851      const char *setp = "";
41852      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41853	setp
41854	  = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41855      switch (setp[0])
41856	{
41857	case 'c':
41858	  if (strcmp (setp, "construct") == 0)
41859	    setp = NULL;
41860	  break;
41861	case 'd':
41862	  if (strcmp (setp, "device") == 0)
41863	    setp = NULL;
41864	  break;
41865	case 'i':
41866	  if (strcmp (setp, "implementation") == 0)
41867	    setp = NULL;
41868	  break;
41869	case 'u':
41870	  if (strcmp (setp, "user") == 0)
41871	    setp = NULL;
41872	  break;
41873	default:
41874	  break;
41875	}
41876      if (setp)
41877	{
41878	  cp_parser_error (parser, "expected %<construct%>, %<device%>, "
41879				   "%<implementation%> or %<user%>");
41880	  return error_mark_node;
41881	}
41882
41883      tree set = cp_lexer_peek_token (parser->lexer)->u.value;
41884      cp_lexer_consume_token (parser->lexer);
41885
41886      if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41887	return error_mark_node;
41888
41889      matching_braces braces;
41890      if (!braces.require_open (parser))
41891	return error_mark_node;
41892
41893      tree selectors
41894	= cp_parser_omp_context_selector (parser, set, has_parms_p);
41895      if (selectors == error_mark_node)
41896	{
41897	  cp_parser_skip_to_closing_brace (parser);
41898	  ret = error_mark_node;
41899	}
41900      else if (ret != error_mark_node)
41901	ret = tree_cons (set, selectors, ret);
41902
41903      braces.require_close (parser);
41904
41905      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41906	cp_lexer_consume_token (parser->lexer);
41907      else
41908	break;
41909    }
41910  while (1);
41911
41912  if (ret == error_mark_node)
41913    return ret;
41914  return nreverse (ret);
41915}
41916
41917/* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
41918   that into "omp declare variant base" attribute.  */
41919
41920static tree
41921cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
41922			       tree attrs)
41923{
41924  matching_parens parens;
41925  if (!parens.require_open (parser))
41926    {
41927     fail:
41928      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41929      return attrs;
41930    }
41931
41932  bool template_p;
41933  cp_id_kind idk = CP_ID_KIND_NONE;
41934  cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
41935  cp_expr varid
41936    = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
41937			       /*check_dependency_p=*/true,
41938			       /*template_p=*/&template_p,
41939			       /*declarator_p=*/false,
41940			       /*optional_p=*/false);
41941  parens.require_close (parser);
41942
41943  tree variant;
41944  if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
41945      || TREE_CODE (varid) == TYPE_DECL
41946      || varid == error_mark_node)
41947    variant = varid;
41948  else if (varid_token->type == CPP_NAME && varid_token->error_reported)
41949    variant = NULL_TREE;
41950  else
41951    {
41952      tree ambiguous_decls;
41953      variant = cp_parser_lookup_name (parser, varid, none_type,
41954				       template_p, /*is_namespace=*/false,
41955				       /*check_dependency=*/true,
41956				       &ambiguous_decls,
41957				       varid.get_location ());
41958      if (ambiguous_decls)
41959	variant = NULL_TREE;
41960    }
41961  if (variant == NULL_TREE)
41962    variant = error_mark_node;
41963  else if (TREE_CODE (variant) != SCOPE_REF)
41964    {
41965      const char *error_msg;
41966      variant
41967	= finish_id_expression (varid, variant, parser->scope,
41968				&idk, false, true,
41969				&parser->non_integral_constant_expression_p,
41970				template_p, true, false, false, &error_msg,
41971				varid.get_location ());
41972      if (error_msg)
41973	cp_parser_error (parser, error_msg);
41974    }
41975  location_t caret_loc = get_pure_location (varid.get_location ());
41976  location_t start_loc = get_start (varid_token->location);
41977  location_t finish_loc = get_finish (varid.get_location ());
41978  location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
41979
41980  const char *clause = "";
41981  location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
41982  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41983    clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41984  if (strcmp (clause, "match"))
41985    {
41986      cp_parser_error (parser, "expected %<match%>");
41987      goto fail;
41988    }
41989
41990  cp_lexer_consume_token (parser->lexer);
41991
41992  if (!parens.require_open (parser))
41993    goto fail;
41994
41995  tree ctx = cp_parser_omp_context_selector_specification (parser, true);
41996  if (ctx == error_mark_node)
41997    goto fail;
41998  ctx = c_omp_check_context_selector (match_loc, ctx);
41999  if (ctx != error_mark_node && variant != error_mark_node)
42000    {
42001      tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
42002						      match_loc);
42003      tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
42004      loc_node = tree_cons (match_loc_node,
42005			    build_int_cst (integer_type_node, idk),
42006			    build_tree_list (loc_node, integer_zero_node));
42007      attrs = tree_cons (get_identifier ("omp declare variant base"),
42008			 tree_cons (variant, ctx, loc_node), attrs);
42009      if (processing_template_decl)
42010	ATTR_IS_DEPENDENT (attrs) = 1;
42011    }
42012
42013  parens.require_close (parser);
42014  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42015  return attrs;
42016}
42017
42018
42019/* Finalize #pragma omp declare simd clauses after direct declarator has
42020   been parsed, and put that into "omp declare simd" attribute.  */
42021
42022static tree
42023cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
42024{
42025  struct cp_token_cache *ce;
42026  cp_omp_declare_simd_data *data = parser->omp_declare_simd;
42027  int i;
42028
42029  if (!data->error_seen && data->fndecl_seen)
42030    {
42031      error ("%<#pragma omp declare %s%> not immediately followed by "
42032	     "a single function declaration or definition",
42033	     data->variant_p ? "variant" : "simd");
42034      data->error_seen = true;
42035    }
42036  if (data->error_seen)
42037    return attrs;
42038
42039  FOR_EACH_VEC_ELT (data->tokens, i, ce)
42040    {
42041      tree c, cl;
42042
42043      cp_parser_push_lexer_for_tokens (parser, ce);
42044      parser->lexer->in_pragma = true;
42045      gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
42046      cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
42047      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42048      const char *kind = IDENTIFIER_POINTER (id);
42049      cp_lexer_consume_token (parser->lexer);
42050      if (strcmp (kind, "simd") == 0)
42051	{
42052	  cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
42053					  "#pragma omp declare simd",
42054					  pragma_tok);
42055	  if (cl)
42056	    cl = tree_cons (NULL_TREE, cl, NULL_TREE);
42057	  c = build_tree_list (get_identifier ("omp declare simd"), cl);
42058	  TREE_CHAIN (c) = attrs;
42059	  if (processing_template_decl)
42060	    ATTR_IS_DEPENDENT (c) = 1;
42061	  attrs = c;
42062	}
42063      else
42064	{
42065	  gcc_assert (strcmp (kind, "variant") == 0);
42066	  attrs = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
42067	}
42068      cp_parser_pop_lexer (parser);
42069    }
42070
42071  data->fndecl_seen = true;
42072  return attrs;
42073}
42074
42075
42076/* OpenMP 4.0:
42077   # pragma omp declare target new-line
42078   declarations and definitions
42079   # pragma omp end declare target new-line
42080
42081   OpenMP 4.5:
42082   # pragma omp declare target ( extended-list ) new-line
42083
42084   # pragma omp declare target declare-target-clauses[seq] new-line  */
42085
42086#define OMP_DECLARE_TARGET_CLAUSE_MASK				\
42087	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
42088	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)		\
42089	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
42090
42091static void
42092cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
42093{
42094  tree clauses = NULL_TREE;
42095  int device_type = 0;
42096  bool only_device_type = true;
42097  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42098    clauses
42099      = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
42100				   "#pragma omp declare target", pragma_tok);
42101  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42102    {
42103      clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
42104					clauses);
42105      clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42106      cp_parser_require_pragma_eol (parser, pragma_tok);
42107    }
42108  else
42109    {
42110      cp_parser_require_pragma_eol (parser, pragma_tok);
42111      scope_chain->omp_declare_target_attribute++;
42112      return;
42113    }
42114  for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
42115    if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
42116      device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
42117  for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
42118    {
42119      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
42120	continue;
42121      tree t = OMP_CLAUSE_DECL (c), id;
42122      tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
42123      tree at2 = lookup_attribute ("omp declare target link",
42124				   DECL_ATTRIBUTES (t));
42125      only_device_type = false;
42126      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
42127	{
42128	  id = get_identifier ("omp declare target link");
42129	  std::swap (at1, at2);
42130	}
42131      else
42132	id = get_identifier ("omp declare target");
42133      if (at2)
42134	{
42135	  error_at (OMP_CLAUSE_LOCATION (c),
42136		    "%qD specified both in declare target %<link%> and %<to%>"
42137		    " clauses", t);
42138	  continue;
42139	}
42140      if (!at1)
42141	{
42142	  DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42143	  if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
42144	    continue;
42145
42146	  symtab_node *node = symtab_node::get (t);
42147	  if (node != NULL)
42148	    {
42149	      node->offloadable = 1;
42150	      if (ENABLE_OFFLOADING)
42151		{
42152		  g->have_offload = true;
42153		  if (is_a <varpool_node *> (node))
42154		    vec_safe_push (offload_vars, t);
42155		}
42156	    }
42157	}
42158      if (TREE_CODE (t) != FUNCTION_DECL)
42159	continue;
42160      if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
42161	{
42162	  tree at3 = lookup_attribute ("omp declare target host",
42163				       DECL_ATTRIBUTES (t));
42164	  if (at3 == NULL_TREE)
42165	    {
42166	      id = get_identifier ("omp declare target host");
42167	      DECL_ATTRIBUTES (t)
42168		= tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42169	    }
42170	}
42171      if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
42172	{
42173	  tree at3 = lookup_attribute ("omp declare target nohost",
42174				       DECL_ATTRIBUTES (t));
42175	  if (at3 == NULL_TREE)
42176	    {
42177	      id = get_identifier ("omp declare target nohost");
42178	      DECL_ATTRIBUTES (t)
42179		= tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42180	    }
42181	}
42182    }
42183  if (device_type && only_device_type)
42184    warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
42185		"directive with only %<device_type%> clauses ignored");
42186}
42187
42188static void
42189cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
42190{
42191  const char *p = "";
42192  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42193    {
42194      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42195      p = IDENTIFIER_POINTER (id);
42196    }
42197  if (strcmp (p, "declare") == 0)
42198    {
42199      cp_lexer_consume_token (parser->lexer);
42200      p = "";
42201      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42202	{
42203	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42204	  p = IDENTIFIER_POINTER (id);
42205	}
42206      if (strcmp (p, "target") == 0)
42207	cp_lexer_consume_token (parser->lexer);
42208      else
42209	{
42210	  cp_parser_error (parser, "expected %<target%>");
42211	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42212	  return;
42213	}
42214    }
42215  else
42216    {
42217      cp_parser_error (parser, "expected %<declare%>");
42218      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42219      return;
42220    }
42221  cp_parser_require_pragma_eol (parser, pragma_tok);
42222  if (!scope_chain->omp_declare_target_attribute)
42223    error_at (pragma_tok->location,
42224	      "%<#pragma omp end declare target%> without corresponding "
42225	      "%<#pragma omp declare target%>");
42226  else
42227    scope_chain->omp_declare_target_attribute--;
42228}
42229
42230/* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
42231   expression and optional initializer clause of
42232   #pragma omp declare reduction.  We store the expression(s) as
42233   either 3, 6 or 7 special statements inside of the artificial function's
42234   body.  The first two statements are DECL_EXPRs for the artificial
42235   OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
42236   expression that uses those variables.
42237   If there was any INITIALIZER clause, this is followed by further statements,
42238   the fourth and fifth statements are DECL_EXPRs for the artificial
42239   OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
42240   constructor variant (first token after open paren is not omp_priv),
42241   then the sixth statement is a statement with the function call expression
42242   that uses the OMP_PRIV and optionally OMP_ORIG variable.
42243   Otherwise, the sixth statement is whatever statement cp_finish_decl emits
42244   to initialize the OMP_PRIV artificial variable and there is seventh
42245   statement, a DECL_EXPR of the OMP_PRIV statement again.  */
42246
42247static bool
42248cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
42249{
42250  tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
42251  gcc_assert (TYPE_REF_P (type));
42252  type = TREE_TYPE (type);
42253  tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
42254  DECL_ARTIFICIAL (omp_out) = 1;
42255  pushdecl (omp_out);
42256  add_decl_expr (omp_out);
42257  tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
42258  DECL_ARTIFICIAL (omp_in) = 1;
42259  pushdecl (omp_in);
42260  add_decl_expr (omp_in);
42261  tree combiner;
42262  tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
42263
42264  keep_next_level (true);
42265  tree block = begin_omp_structured_block ();
42266  combiner = cp_parser_expression (parser);
42267  finish_expr_stmt (combiner);
42268  block = finish_omp_structured_block (block);
42269  if (processing_template_decl)
42270    block = build_stmt (input_location, EXPR_STMT, block);
42271  add_stmt (block);
42272
42273  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
42274    return false;
42275
42276  const char *p = "";
42277  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42278    {
42279      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42280      p = IDENTIFIER_POINTER (id);
42281    }
42282
42283  if (strcmp (p, "initializer") == 0)
42284    {
42285      cp_lexer_consume_token (parser->lexer);
42286      matching_parens parens;
42287      if (!parens.require_open (parser))
42288	return false;
42289
42290      p = "";
42291      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42292	{
42293	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42294	  p = IDENTIFIER_POINTER (id);
42295	}
42296
42297      omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
42298      DECL_ARTIFICIAL (omp_priv) = 1;
42299      pushdecl (omp_priv);
42300      add_decl_expr (omp_priv);
42301      omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
42302      DECL_ARTIFICIAL (omp_orig) = 1;
42303      pushdecl (omp_orig);
42304      add_decl_expr (omp_orig);
42305
42306      keep_next_level (true);
42307      block = begin_omp_structured_block ();
42308
42309      bool ctor = false;
42310      if (strcmp (p, "omp_priv") == 0)
42311	{
42312	  bool is_direct_init, is_non_constant_init;
42313	  ctor = true;
42314	  cp_lexer_consume_token (parser->lexer);
42315	  /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
42316	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
42317	      || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
42318		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
42319		     == CPP_CLOSE_PAREN
42320		  && cp_lexer_peek_nth_token (parser->lexer, 3)->type
42321		     == CPP_CLOSE_PAREN))
42322	    {
42323	      finish_omp_structured_block (block);
42324	      error ("invalid initializer clause");
42325	      return false;
42326	    }
42327	  initializer = cp_parser_initializer (parser, &is_direct_init,
42328					       &is_non_constant_init);
42329	  cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
42330			  NULL_TREE, LOOKUP_ONLYCONVERTING);
42331	}
42332      else
42333	{
42334	  cp_parser_parse_tentatively (parser);
42335	  /* Don't create location wrapper nodes here.  */
42336	  auto_suppress_location_wrappers sentinel;
42337	  tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
42338						  /*check_dependency_p=*/true,
42339						  /*template_p=*/NULL,
42340						  /*declarator_p=*/false,
42341						  /*optional_p=*/false);
42342	  vec<tree, va_gc> *args;
42343	  if (fn_name == error_mark_node
42344	      || cp_parser_error_occurred (parser)
42345	      || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
42346	      || ((args = cp_parser_parenthesized_expression_list
42347				(parser, non_attr, /*cast_p=*/false,
42348				 /*allow_expansion_p=*/true,
42349				 /*non_constant_p=*/NULL)),
42350		  cp_parser_error_occurred (parser)))
42351	    {
42352	      finish_omp_structured_block (block);
42353	      cp_parser_abort_tentative_parse (parser);
42354	      cp_parser_error (parser, "expected id-expression (arguments)");
42355	      return false;
42356	    }
42357	  unsigned int i;
42358	  tree arg;
42359	  FOR_EACH_VEC_SAFE_ELT (args, i, arg)
42360	    if (arg == omp_priv
42361		|| (TREE_CODE (arg) == ADDR_EXPR
42362		    && TREE_OPERAND (arg, 0) == omp_priv))
42363	      break;
42364	  cp_parser_abort_tentative_parse (parser);
42365	  if (arg == NULL_TREE)
42366	    error ("one of the initializer call arguments should be %<omp_priv%>"
42367		   " or %<&omp_priv%>");
42368	  initializer = cp_parser_postfix_expression (parser, false, false, false,
42369						      false, NULL);
42370	  finish_expr_stmt (initializer);
42371	}
42372
42373      block = finish_omp_structured_block (block);
42374      cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
42375      if (processing_template_decl)
42376	block = build_stmt (input_location, EXPR_STMT, block);
42377      add_stmt (block);
42378
42379      if (ctor)
42380	add_decl_expr (omp_orig);
42381
42382      if (!parens.require_close (parser))
42383	return false;
42384    }
42385
42386  if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
42387    cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
42388                              UNKNOWN_LOCATION);
42389
42390  return true;
42391}
42392
42393/* OpenMP 4.0
42394   #pragma omp declare reduction (reduction-id : typename-list : expression) \
42395      initializer-clause[opt] new-line
42396
42397   initializer-clause:
42398      initializer (omp_priv initializer)
42399      initializer (function-name (argument-list))  */
42400
42401static void
42402cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
42403				 enum pragma_context)
42404{
42405  auto_vec<tree> types;
42406  enum tree_code reduc_code = ERROR_MARK;
42407  tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
42408  unsigned int i;
42409  cp_token *first_token;
42410  cp_token_cache *cp;
42411  int errs;
42412  void *p;
42413
42414  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
42415  p = obstack_alloc (&declarator_obstack, 0);
42416
42417  if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
42418    goto fail;
42419
42420  switch (cp_lexer_peek_token (parser->lexer)->type)
42421    {
42422    case CPP_PLUS:
42423      reduc_code = PLUS_EXPR;
42424      break;
42425    case CPP_MULT:
42426      reduc_code = MULT_EXPR;
42427      break;
42428    case CPP_MINUS:
42429      reduc_code = MINUS_EXPR;
42430      break;
42431    case CPP_AND:
42432      reduc_code = BIT_AND_EXPR;
42433      break;
42434    case CPP_XOR:
42435      reduc_code = BIT_XOR_EXPR;
42436      break;
42437    case CPP_OR:
42438      reduc_code = BIT_IOR_EXPR;
42439      break;
42440    case CPP_AND_AND:
42441      reduc_code = TRUTH_ANDIF_EXPR;
42442      break;
42443    case CPP_OR_OR:
42444      reduc_code = TRUTH_ORIF_EXPR;
42445      break;
42446    case CPP_NAME:
42447      reduc_id = orig_reduc_id = cp_parser_identifier (parser);
42448      break;
42449    default:
42450      cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
42451			       "%<|%>, %<&&%>, %<||%> or identifier");
42452      goto fail;
42453    }
42454
42455  if (reduc_code != ERROR_MARK)
42456    cp_lexer_consume_token (parser->lexer);
42457
42458  reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
42459  if (reduc_id == error_mark_node)
42460    goto fail;
42461
42462  if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
42463    goto fail;
42464
42465  /* Types may not be defined in declare reduction type list.  */
42466  const char *saved_message;
42467  saved_message = parser->type_definition_forbidden_message;
42468  parser->type_definition_forbidden_message
42469    = G_("types may not be defined in declare reduction type list");
42470  bool saved_colon_corrects_to_scope_p;
42471  saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
42472  parser->colon_corrects_to_scope_p = false;
42473  bool saved_colon_doesnt_start_class_def_p;
42474  saved_colon_doesnt_start_class_def_p
42475    = parser->colon_doesnt_start_class_def_p;
42476  parser->colon_doesnt_start_class_def_p = true;
42477
42478  while (true)
42479    {
42480      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42481      type = cp_parser_type_id (parser);
42482      if (type == error_mark_node)
42483	;
42484      else if (ARITHMETIC_TYPE_P (type)
42485	       && (orig_reduc_id == NULL_TREE
42486		   || (TREE_CODE (type) != COMPLEX_TYPE
42487		       && (id_equal (orig_reduc_id, "min")
42488			   || id_equal (orig_reduc_id, "max")))))
42489	error_at (loc, "predeclared arithmetic type %qT in "
42490		       "%<#pragma omp declare reduction%>", type);
42491      else if (FUNC_OR_METHOD_TYPE_P (type)
42492	       || TREE_CODE (type) == ARRAY_TYPE)
42493	error_at (loc, "function or array type %qT in "
42494		       "%<#pragma omp declare reduction%>", type);
42495      else if (TYPE_REF_P (type))
42496	error_at (loc, "reference type %qT in "
42497		       "%<#pragma omp declare reduction%>", type);
42498      else if (TYPE_QUALS_NO_ADDR_SPACE (type))
42499	error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
42500		  "type %qT in %<#pragma omp declare reduction%>", type);
42501      else
42502	types.safe_push (type);
42503
42504      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42505	cp_lexer_consume_token (parser->lexer);
42506      else
42507	break;
42508    }
42509
42510  /* Restore the saved message.  */
42511  parser->type_definition_forbidden_message = saved_message;
42512  parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
42513  parser->colon_doesnt_start_class_def_p
42514    = saved_colon_doesnt_start_class_def_p;
42515
42516  if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
42517      || types.is_empty ())
42518    {
42519     fail:
42520      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42521      goto done;
42522    }
42523
42524  first_token = cp_lexer_peek_token (parser->lexer);
42525  cp = NULL;
42526  errs = errorcount;
42527  FOR_EACH_VEC_ELT (types, i, type)
42528    {
42529      tree fntype
42530	= build_function_type_list (void_type_node,
42531				    cp_build_reference_type (type, false),
42532				    NULL_TREE);
42533      tree this_reduc_id = reduc_id;
42534      if (!dependent_type_p (type))
42535	this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
42536      tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
42537      DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
42538      DECL_ARTIFICIAL (fndecl) = 1;
42539      DECL_EXTERNAL (fndecl) = 1;
42540      DECL_DECLARED_INLINE_P (fndecl) = 1;
42541      DECL_IGNORED_P (fndecl) = 1;
42542      DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
42543      SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
42544      DECL_ATTRIBUTES (fndecl)
42545	= tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
42546		     DECL_ATTRIBUTES (fndecl));
42547      if (processing_template_decl)
42548	fndecl = push_template_decl (fndecl);
42549      bool block_scope = false;
42550      tree block = NULL_TREE;
42551      if (current_function_decl)
42552	{
42553	  block_scope = true;
42554	  DECL_CONTEXT (fndecl) = global_namespace;
42555	  if (!processing_template_decl)
42556	    pushdecl (fndecl);
42557	}
42558      else if (current_class_type)
42559	{
42560	  if (cp == NULL)
42561	    {
42562	      while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
42563		     && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
42564		cp_lexer_consume_token (parser->lexer);
42565	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42566		goto fail;
42567	      cp = cp_token_cache_new (first_token,
42568				       cp_lexer_peek_nth_token (parser->lexer,
42569								2));
42570	    }
42571	  DECL_STATIC_FUNCTION_P (fndecl) = 1;
42572	  finish_member_declaration (fndecl);
42573	  DECL_PENDING_INLINE_INFO (fndecl) = cp;
42574	  DECL_PENDING_INLINE_P (fndecl) = 1;
42575	  vec_safe_push (unparsed_funs_with_definitions, fndecl);
42576	  continue;
42577	}
42578      else
42579	{
42580	  DECL_CONTEXT (fndecl) = current_namespace;
42581	  pushdecl (fndecl);
42582	}
42583      if (!block_scope)
42584	start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
42585      else
42586	block = begin_omp_structured_block ();
42587      if (cp)
42588	{
42589	  cp_parser_push_lexer_for_tokens (parser, cp);
42590	  parser->lexer->in_pragma = true;
42591	}
42592      if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
42593	{
42594	  if (!block_scope)
42595	    finish_function (/*inline_p=*/false);
42596	  else
42597	    DECL_CONTEXT (fndecl) = current_function_decl;
42598	  if (cp)
42599	    cp_parser_pop_lexer (parser);
42600	  goto fail;
42601	}
42602      if (cp)
42603	cp_parser_pop_lexer (parser);
42604      if (!block_scope)
42605	finish_function (/*inline_p=*/false);
42606      else
42607	{
42608	  DECL_CONTEXT (fndecl) = current_function_decl;
42609	  block = finish_omp_structured_block (block);
42610	  if (TREE_CODE (block) == BIND_EXPR)
42611	    DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
42612	  else if (TREE_CODE (block) == STATEMENT_LIST)
42613	    DECL_SAVED_TREE (fndecl) = block;
42614	  if (processing_template_decl)
42615	    add_decl_expr (fndecl);
42616	}
42617      cp_check_omp_declare_reduction (fndecl);
42618      if (cp == NULL && types.length () > 1)
42619	cp = cp_token_cache_new (first_token,
42620				 cp_lexer_peek_nth_token (parser->lexer, 2));
42621      if (errs != errorcount)
42622	break;
42623    }
42624
42625  cp_parser_require_pragma_eol (parser, pragma_tok);
42626
42627 done:
42628  /* Free any declarators allocated.  */
42629  obstack_free (&declarator_obstack, p);
42630}
42631
42632/* OpenMP 4.0
42633   #pragma omp declare simd declare-simd-clauses[optseq] new-line
42634   #pragma omp declare reduction (reduction-id : typename-list : expression) \
42635      initializer-clause[opt] new-line
42636   #pragma omp declare target new-line
42637
42638   OpenMP 5.0
42639   #pragma omp declare variant (identifier) match (context-selector)  */
42640
42641static bool
42642cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
42643		       enum pragma_context context)
42644{
42645  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42646    {
42647      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42648      const char *p = IDENTIFIER_POINTER (id);
42649
42650      if (strcmp (p, "simd") == 0)
42651	{
42652	  cp_lexer_consume_token (parser->lexer);
42653	  cp_parser_omp_declare_simd (parser, pragma_tok,
42654				      context, false);
42655	  return true;
42656	}
42657      if (flag_openmp && strcmp (p, "variant") == 0)
42658	{
42659	  cp_lexer_consume_token (parser->lexer);
42660	  cp_parser_omp_declare_simd (parser, pragma_tok,
42661				      context, true);
42662	  return true;
42663	}
42664      cp_ensure_no_omp_declare_simd (parser);
42665      if (strcmp (p, "reduction") == 0)
42666	{
42667	  cp_lexer_consume_token (parser->lexer);
42668	  cp_parser_omp_declare_reduction (parser, pragma_tok,
42669					   context);
42670	  return false;
42671	}
42672      if (!flag_openmp)  /* flag_openmp_simd  */
42673	{
42674	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42675	  return false;
42676	}
42677      if (strcmp (p, "target") == 0)
42678	{
42679	  cp_lexer_consume_token (parser->lexer);
42680	  cp_parser_omp_declare_target (parser, pragma_tok);
42681	  return false;
42682	}
42683    }
42684  cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
42685			   "%<target%> or %<variant%>");
42686  cp_parser_require_pragma_eol (parser, pragma_tok);
42687  return false;
42688}
42689
42690/* OpenMP 5.0
42691   #pragma omp requires clauses[optseq] new-line  */
42692
42693static bool
42694cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
42695{
42696  bool first = true;
42697  enum omp_requires new_req = (enum omp_requires) 0;
42698
42699  location_t loc = pragma_tok->location;
42700  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42701    {
42702      if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42703	cp_lexer_consume_token (parser->lexer);
42704
42705      first = false;
42706
42707      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42708	{
42709	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42710	  const char *p = IDENTIFIER_POINTER (id);
42711	  location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42712	  enum omp_requires this_req = (enum omp_requires) 0;
42713
42714	  if (!strcmp (p, "unified_address"))
42715	    this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
42716	  else if (!strcmp (p, "unified_shared_memory"))
42717	    this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
42718	  else if (!strcmp (p, "dynamic_allocators"))
42719	    this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
42720	  else if (!strcmp (p, "reverse_offload"))
42721	    this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
42722	  else if (!strcmp (p, "atomic_default_mem_order"))
42723	    {
42724	      cp_lexer_consume_token (parser->lexer);
42725
42726	      matching_parens parens;
42727	      if (parens.require_open (parser))
42728		{
42729		  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42730		    {
42731		      id = cp_lexer_peek_token (parser->lexer)->u.value;
42732		      p = IDENTIFIER_POINTER (id);
42733
42734		      if (!strcmp (p, "seq_cst"))
42735			this_req
42736			  = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
42737		      else if (!strcmp (p, "relaxed"))
42738			this_req
42739			  = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
42740		      else if (!strcmp (p, "acq_rel"))
42741			this_req
42742			  = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
42743		    }
42744		  if (this_req == 0)
42745		    {
42746		      error_at (cp_lexer_peek_token (parser->lexer)->location,
42747				"expected %<seq_cst%>, %<relaxed%> or "
42748				"%<acq_rel%>");
42749		      if (cp_lexer_nth_token_is (parser->lexer, 2,
42750						 CPP_CLOSE_PAREN))
42751			cp_lexer_consume_token (parser->lexer);
42752		    }
42753		  else
42754		    cp_lexer_consume_token (parser->lexer);
42755
42756		  if (!parens.require_close (parser))
42757		    cp_parser_skip_to_closing_parenthesis (parser,
42758							   /*recovering=*/true,
42759							   /*or_comma=*/false,
42760							   /*consume_paren=*/
42761							   true);
42762
42763		  if (this_req == 0)
42764		    {
42765		      cp_parser_require_pragma_eol (parser, pragma_tok);
42766		      return false;
42767		    }
42768		}
42769	      p = NULL;
42770	    }
42771	  else
42772	    {
42773	      error_at (cloc, "expected %<unified_address%>, "
42774			      "%<unified_shared_memory%>, "
42775			      "%<dynamic_allocators%>, "
42776			       "%<reverse_offload%> "
42777			       "or %<atomic_default_mem_order%> clause");
42778	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42779	      return false;
42780	    }
42781	  if (p)
42782	    sorry_at (cloc, "%qs clause on %<requires%> directive not "
42783			    "supported yet", p);
42784	  if (p)
42785	    cp_lexer_consume_token (parser->lexer);
42786	  if (this_req)
42787	    {
42788	      if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
42789		{
42790		  if ((this_req & new_req) != 0)
42791		    error_at (cloc, "too many %qs clauses", p);
42792		  if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
42793		      && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
42794		    error_at (cloc, "%qs clause used lexically after first "
42795				    "target construct or offloading API", p);
42796		}
42797	      else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
42798		{
42799		  error_at (cloc, "too many %qs clauses",
42800			    "atomic_default_mem_order");
42801		  this_req = (enum omp_requires) 0;
42802		}
42803	      else if ((omp_requires_mask
42804			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
42805		{
42806		  error_at (cloc, "more than one %<atomic_default_mem_order%>"
42807				  " clause in a single compilation unit");
42808		  this_req
42809		    = (enum omp_requires)
42810		       (omp_requires_mask
42811			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
42812		}
42813	      else if ((omp_requires_mask
42814			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
42815		error_at (cloc, "%<atomic_default_mem_order%> clause used "
42816				"lexically after first %<atomic%> construct "
42817				"without memory order clause");
42818	      new_req = (enum omp_requires) (new_req | this_req);
42819	      omp_requires_mask
42820		= (enum omp_requires) (omp_requires_mask | this_req);
42821	      continue;
42822	    }
42823	}
42824      break;
42825    }
42826  cp_parser_require_pragma_eol (parser, pragma_tok);
42827
42828  if (new_req == 0)
42829    error_at (loc, "%<pragma omp requires%> requires at least one clause");
42830  return false;
42831}
42832
42833
42834/* OpenMP 4.5:
42835   #pragma omp taskloop taskloop-clause[optseq] new-line
42836     for-loop
42837
42838   #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
42839     for-loop  */
42840
42841#define OMP_TASKLOOP_CLAUSE_MASK				\
42842	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
42843	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
42844	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
42845	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
42846	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
42847	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)	\
42848	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)	\
42849	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
42850	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
42851	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
42852	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
42853	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
42854	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)	\
42855	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY)	\
42856	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
42857	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
42858
42859static tree
42860cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
42861			char *p_name, omp_clause_mask mask, tree *cclauses,
42862			bool *if_p)
42863{
42864  tree clauses, sb, ret;
42865  unsigned int save;
42866  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42867
42868  strcat (p_name, " taskloop");
42869  mask |= OMP_TASKLOOP_CLAUSE_MASK;
42870  /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
42871     clause.  */
42872  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
42873    mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
42874
42875  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42876    {
42877      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42878      const char *p = IDENTIFIER_POINTER (id);
42879
42880      if (strcmp (p, "simd") == 0)
42881	{
42882	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42883	  if (cclauses == NULL)
42884	    cclauses = cclauses_buf;
42885
42886	  cp_lexer_consume_token (parser->lexer);
42887	  if (!flag_openmp)  /* flag_openmp_simd  */
42888	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42889				       cclauses, if_p);
42890	  sb = begin_omp_structured_block ();
42891	  save = cp_parser_begin_omp_structured_block (parser);
42892	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42893				    cclauses, if_p);
42894	  cp_parser_end_omp_structured_block (parser, save);
42895	  tree body = finish_omp_structured_block (sb);
42896	  if (ret == NULL)
42897	    return ret;
42898	  ret = make_node (OMP_TASKLOOP);
42899	  TREE_TYPE (ret) = void_type_node;
42900	  OMP_FOR_BODY (ret) = body;
42901	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
42902	  SET_EXPR_LOCATION (ret, loc);
42903	  add_stmt (ret);
42904	  return ret;
42905	}
42906    }
42907  if (!flag_openmp)  /* flag_openmp_simd  */
42908    {
42909      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42910      return NULL_TREE;
42911    }
42912
42913  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42914				       cclauses == NULL);
42915  if (cclauses)
42916    {
42917      cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
42918      clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
42919    }
42920
42921  keep_next_level (true);
42922  sb = begin_omp_structured_block ();
42923  save = cp_parser_begin_omp_structured_block (parser);
42924
42925  ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
42926				if_p);
42927
42928  cp_parser_end_omp_structured_block (parser, save);
42929  add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42930
42931  return ret;
42932}
42933
42934
42935/* OpenACC 2.0:
42936   # pragma acc routine oacc-routine-clause[optseq] new-line
42937     function-definition
42938
42939   # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
42940*/
42941
42942#define OACC_ROUTINE_CLAUSE_MASK					\
42943	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
42944	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
42945	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
42946	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
42947
42948
42949/* Parse the OpenACC routine pragma.  This has an optional '( name )'
42950   component, which must resolve to a declared namespace-scope
42951   function.  The clauses are either processed directly (for a named
42952   function), or defered until the immediatley following declaration
42953   is parsed.  */
42954
42955static void
42956cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
42957			enum pragma_context context)
42958{
42959  gcc_checking_assert (context == pragma_external);
42960  /* The checking for "another pragma following this one" in the "no optional
42961     '( name )'" case makes sure that we dont re-enter.  */
42962  gcc_checking_assert (parser->oacc_routine == NULL);
42963
42964  cp_oacc_routine_data data;
42965  data.error_seen = false;
42966  data.fndecl_seen = false;
42967  data.tokens = vNULL;
42968  data.clauses = NULL_TREE;
42969  data.loc = pragma_tok->location;
42970  /* It is safe to take the address of a local variable; it will only be
42971     used while this scope is live.  */
42972  parser->oacc_routine = &data;
42973
42974  /* Look for optional '( name )'.  */
42975  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42976    {
42977      matching_parens parens;
42978      parens.consume_open (parser); /* '(' */
42979
42980      /* We parse the name as an id-expression.  If it resolves to
42981	 anything other than a non-overloaded function at namespace
42982	 scope, it's an error.  */
42983      location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
42984      tree name = cp_parser_id_expression (parser,
42985					   /*template_keyword_p=*/false,
42986					   /*check_dependency_p=*/false,
42987					   /*template_p=*/NULL,
42988					   /*declarator_p=*/false,
42989					   /*optional_p=*/false);
42990      tree decl = (identifier_p (name)
42991		   ? cp_parser_lookup_name_simple (parser, name, name_loc)
42992		   : name);
42993      if (name != error_mark_node && decl == error_mark_node)
42994	cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
42995
42996      if (decl == error_mark_node
42997	  || !parens.require_close (parser))
42998	{
42999	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43000	  parser->oacc_routine = NULL;
43001	  return;
43002	}
43003
43004      data.clauses
43005	= cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
43006				      "#pragma acc routine",
43007				      cp_lexer_peek_token (parser->lexer));
43008      /* The clauses are in reverse order; fix that to make later diagnostic
43009	 emission easier.  */
43010      data.clauses = nreverse (data.clauses);
43011
43012      if (decl && is_overloaded_fn (decl)
43013	  && (TREE_CODE (decl) != FUNCTION_DECL
43014	      || DECL_FUNCTION_TEMPLATE_P  (decl)))
43015	{
43016	  error_at (name_loc,
43017		    "%<#pragma acc routine%> names a set of overloads");
43018	  parser->oacc_routine = NULL;
43019	  return;
43020	}
43021
43022      /* Perhaps we should use the same rule as declarations in different
43023	 namespaces?  */
43024      if (!DECL_NAMESPACE_SCOPE_P (decl))
43025	{
43026	  error_at (name_loc,
43027		    "%qD does not refer to a namespace scope function", decl);
43028	  parser->oacc_routine = NULL;
43029	  return;
43030	}
43031
43032      if (TREE_CODE (decl) != FUNCTION_DECL)
43033	{
43034	  error_at (name_loc, "%qD does not refer to a function", decl);
43035	  parser->oacc_routine = NULL;
43036	  return;
43037	}
43038
43039      cp_finalize_oacc_routine (parser, decl, false);
43040      parser->oacc_routine = NULL;
43041    }
43042  else /* No optional '( name )'.  */
43043    {
43044      /* Store away all pragma tokens.  */
43045      while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
43046	     && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
43047	cp_lexer_consume_token (parser->lexer);
43048      if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43049	parser->oacc_routine->error_seen = true;
43050      cp_parser_require_pragma_eol (parser, pragma_tok);
43051      struct cp_token_cache *cp
43052	= cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
43053      parser->oacc_routine->tokens.safe_push (cp);
43054
43055      /* Emit a helpful diagnostic if there's another pragma following this
43056	 one.  */
43057      if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
43058	{
43059	  cp_ensure_no_oacc_routine (parser);
43060	  data.tokens.release ();
43061	  /* ..., and then just keep going.  */
43062	  return;
43063	}
43064
43065      /* We only have to consider the pragma_external case here.  */
43066      cp_parser_declaration (parser);
43067      if (parser->oacc_routine
43068	  && !parser->oacc_routine->fndecl_seen)
43069	cp_ensure_no_oacc_routine (parser);
43070      else
43071	parser->oacc_routine = NULL;
43072      data.tokens.release ();
43073    }
43074}
43075
43076/* Finalize #pragma acc routine clauses after direct declarator has
43077   been parsed.  */
43078
43079static tree
43080cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
43081{
43082  struct cp_token_cache *ce;
43083  cp_oacc_routine_data *data = parser->oacc_routine;
43084
43085  if (!data->error_seen && data->fndecl_seen)
43086    {
43087      error_at (data->loc,
43088		"%<#pragma acc routine%> not immediately followed by "
43089		"a single function declaration or definition");
43090      data->error_seen = true;
43091    }
43092  if (data->error_seen)
43093    return attrs;
43094
43095  gcc_checking_assert (data->tokens.length () == 1);
43096  ce = data->tokens[0];
43097
43098  cp_parser_push_lexer_for_tokens (parser, ce);
43099  parser->lexer->in_pragma = true;
43100  gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
43101
43102  cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
43103  gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
43104  parser->oacc_routine->clauses
43105    = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
43106				  "#pragma acc routine", pragma_tok);
43107  /* The clauses are in reverse order; fix that to make later diagnostic
43108     emission easier.  */
43109  parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
43110  cp_parser_pop_lexer (parser);
43111  /* Later, cp_finalize_oacc_routine will process the clauses, and then set
43112     fndecl_seen.  */
43113
43114  return attrs;
43115}
43116
43117/* Apply any saved OpenACC routine clauses to a just-parsed
43118   declaration.  */
43119
43120static void
43121cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
43122{
43123  if (__builtin_expect (parser->oacc_routine != NULL, 0))
43124    {
43125      /* Keep going if we're in error reporting mode.  */
43126      if (parser->oacc_routine->error_seen
43127	  || fndecl == error_mark_node)
43128	return;
43129
43130      if (parser->oacc_routine->fndecl_seen)
43131	{
43132	  error_at (parser->oacc_routine->loc,
43133		    "%<#pragma acc routine%> not immediately followed by"
43134		    " a single function declaration or definition");
43135	  parser->oacc_routine = NULL;
43136	  return;
43137	}
43138      if (TREE_CODE (fndecl) != FUNCTION_DECL)
43139	{
43140	  cp_ensure_no_oacc_routine (parser);
43141	  return;
43142	}
43143
43144      int compatible
43145	= oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
43146				       parser->oacc_routine->loc,
43147				       "#pragma acc routine");
43148      if (compatible < 0)
43149	{
43150	  parser->oacc_routine = NULL;
43151	  return;
43152	}
43153      if (compatible > 0)
43154	{
43155	}
43156      else
43157	{
43158	  if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
43159	    {
43160	      error_at (parser->oacc_routine->loc,
43161			TREE_USED (fndecl)
43162			? G_("%<#pragma acc routine%> must be applied before"
43163			     " use")
43164			: G_("%<#pragma acc routine%> must be applied before"
43165			     " definition"));
43166	      parser->oacc_routine = NULL;
43167	      return;
43168	    }
43169
43170	  /* Set the routine's level of parallelism.  */
43171	  tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
43172	  oacc_replace_fn_attrib (fndecl, dims);
43173
43174	  /* Add an "omp declare target" attribute.  */
43175	  DECL_ATTRIBUTES (fndecl)
43176	    = tree_cons (get_identifier ("omp declare target"),
43177			 parser->oacc_routine->clauses,
43178			 DECL_ATTRIBUTES (fndecl));
43179	}
43180
43181      /* Don't unset parser->oacc_routine here: we may still need it to
43182	 diagnose wrong usage.  But, remember that we've used this "#pragma acc
43183	 routine".  */
43184      parser->oacc_routine->fndecl_seen = true;
43185    }
43186}
43187
43188/* Main entry point to OpenMP statement pragmas.  */
43189
43190static void
43191cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43192{
43193  tree stmt;
43194  char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
43195  omp_clause_mask mask (0);
43196
43197  switch (cp_parser_pragma_kind (pragma_tok))
43198    {
43199    case PRAGMA_OACC_ATOMIC:
43200      cp_parser_omp_atomic (parser, pragma_tok);
43201      return;
43202    case PRAGMA_OACC_CACHE:
43203      stmt = cp_parser_oacc_cache (parser, pragma_tok);
43204      break;
43205    case PRAGMA_OACC_DATA:
43206      stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
43207      break;
43208    case PRAGMA_OACC_ENTER_DATA:
43209      stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
43210      break;
43211    case PRAGMA_OACC_EXIT_DATA:
43212      stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
43213      break;
43214    case PRAGMA_OACC_HOST_DATA:
43215      stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
43216      break;
43217    case PRAGMA_OACC_KERNELS:
43218    case PRAGMA_OACC_PARALLEL:
43219    case PRAGMA_OACC_SERIAL:
43220      strcpy (p_name, "#pragma acc");
43221      stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
43222      break;
43223    case PRAGMA_OACC_LOOP:
43224      strcpy (p_name, "#pragma acc");
43225      stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
43226				  if_p);
43227      break;
43228    case PRAGMA_OACC_UPDATE:
43229      stmt = cp_parser_oacc_update (parser, pragma_tok);
43230      break;
43231    case PRAGMA_OACC_WAIT:
43232      stmt = cp_parser_oacc_wait (parser, pragma_tok);
43233      break;
43234    case PRAGMA_OMP_ATOMIC:
43235      cp_parser_omp_atomic (parser, pragma_tok);
43236      return;
43237    case PRAGMA_OMP_CRITICAL:
43238      stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
43239      break;
43240    case PRAGMA_OMP_DISTRIBUTE:
43241      strcpy (p_name, "#pragma omp");
43242      stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
43243				       if_p);
43244      break;
43245    case PRAGMA_OMP_FOR:
43246      strcpy (p_name, "#pragma omp");
43247      stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
43248				if_p);
43249      break;
43250    case PRAGMA_OMP_LOOP:
43251      strcpy (p_name, "#pragma omp");
43252      stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
43253				 if_p);
43254      break;
43255    case PRAGMA_OMP_MASTER:
43256      strcpy (p_name, "#pragma omp");
43257      stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
43258				   if_p);
43259      break;
43260    case PRAGMA_OMP_PARALLEL:
43261      strcpy (p_name, "#pragma omp");
43262      stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
43263				     if_p);
43264      break;
43265    case PRAGMA_OMP_SECTIONS:
43266      strcpy (p_name, "#pragma omp");
43267      stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
43268      break;
43269    case PRAGMA_OMP_SIMD:
43270      strcpy (p_name, "#pragma omp");
43271      stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
43272				 if_p);
43273      break;
43274    case PRAGMA_OMP_SINGLE:
43275      stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
43276      break;
43277    case PRAGMA_OMP_TASK:
43278      stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
43279      break;
43280    case PRAGMA_OMP_TASKGROUP:
43281      stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
43282      break;
43283    case PRAGMA_OMP_TASKLOOP:
43284      strcpy (p_name, "#pragma omp");
43285      stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
43286				     if_p);
43287      break;
43288    case PRAGMA_OMP_TEAMS:
43289      strcpy (p_name, "#pragma omp");
43290      stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
43291				  if_p);
43292      break;
43293    default:
43294      gcc_unreachable ();
43295    }
43296
43297  protected_set_expr_location (stmt, pragma_tok->location);
43298}
43299
43300/* Transactional Memory parsing routines.  */
43301
43302/* Parse a transaction attribute.
43303
43304   txn-attribute:
43305	attribute
43306	[ [ identifier ] ]
43307
43308   We use this instead of cp_parser_attributes_opt for transactions to avoid
43309   the pedwarn in C++98 mode.  */
43310
43311static tree
43312cp_parser_txn_attribute_opt (cp_parser *parser)
43313{
43314  cp_token *token;
43315  tree attr_name, attr = NULL;
43316
43317  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
43318    return cp_parser_attributes_opt (parser);
43319
43320  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
43321    return NULL_TREE;
43322  cp_lexer_consume_token (parser->lexer);
43323  if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
43324    goto error1;
43325
43326  token = cp_lexer_peek_token (parser->lexer);
43327  if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
43328    {
43329      token = cp_lexer_consume_token (parser->lexer);
43330
43331      attr_name = (token->type == CPP_KEYWORD
43332		   /* For keywords, use the canonical spelling,
43333		      not the parsed identifier.  */
43334		   ? ridpointers[(int) token->keyword]
43335		   : token->u.value);
43336      attr = build_tree_list (attr_name, NULL_TREE);
43337    }
43338  else
43339    cp_parser_error (parser, "expected identifier");
43340
43341  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43342 error1:
43343  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43344  return attr;
43345}
43346
43347/* Parse a __transaction_atomic or __transaction_relaxed statement.
43348
43349   transaction-statement:
43350     __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
43351       compound-statement
43352     __transaction_relaxed txn-noexcept-spec[opt] compound-statement
43353*/
43354
43355static tree
43356cp_parser_transaction (cp_parser *parser, cp_token *token)
43357{
43358  unsigned char old_in = parser->in_transaction;
43359  unsigned char this_in = 1, new_in;
43360  enum rid keyword = token->keyword;
43361  tree stmt, attrs, noex;
43362
43363  cp_lexer_consume_token (parser->lexer);
43364
43365  if (keyword == RID_TRANSACTION_RELAXED
43366      || keyword == RID_SYNCHRONIZED)
43367    this_in |= TM_STMT_ATTR_RELAXED;
43368  else
43369    {
43370      attrs = cp_parser_txn_attribute_opt (parser);
43371      if (attrs)
43372	this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
43373    }
43374
43375  /* Parse a noexcept specification.  */
43376  if (keyword == RID_ATOMIC_NOEXCEPT)
43377    noex = boolean_true_node;
43378  else if (keyword == RID_ATOMIC_CANCEL)
43379    {
43380      /* cancel-and-throw is unimplemented.  */
43381      sorry ("%<atomic_cancel%>");
43382      noex = NULL_TREE;
43383    }
43384  else
43385    noex = cp_parser_noexcept_specification_opt (parser,
43386						 CP_PARSER_FLAGS_NONE,
43387						 /*require_constexpr=*/true,
43388						 /*consumed_expr=*/NULL,
43389						 /*return_cond=*/true,
43390						 TYPE_UNQUALIFIED);
43391
43392  /* Keep track if we're in the lexical scope of an outer transaction.  */
43393  new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
43394
43395  stmt = begin_transaction_stmt (token->location, NULL, this_in);
43396
43397  parser->in_transaction = new_in;
43398  cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
43399  parser->in_transaction = old_in;
43400
43401  finish_transaction_stmt (stmt, NULL, this_in, noex);
43402
43403  return stmt;
43404}
43405
43406/* Parse a __transaction_atomic or __transaction_relaxed expression.
43407
43408   transaction-expression:
43409     __transaction_atomic txn-noexcept-spec[opt] ( expression )
43410     __transaction_relaxed txn-noexcept-spec[opt] ( expression )
43411*/
43412
43413static tree
43414cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
43415{
43416  unsigned char old_in = parser->in_transaction;
43417  unsigned char this_in = 1;
43418  cp_token *token;
43419  tree expr, noex;
43420  bool noex_expr;
43421  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43422
43423  gcc_assert (keyword == RID_TRANSACTION_ATOMIC
43424      || keyword == RID_TRANSACTION_RELAXED);
43425
43426  if (!flag_tm)
43427    error_at (loc,
43428	      keyword == RID_TRANSACTION_RELAXED
43429	      ? G_("%<__transaction_relaxed%> without transactional memory "
43430		  "support enabled")
43431	      : G_("%<__transaction_atomic%> without transactional memory "
43432		   "support enabled"));
43433
43434  token = cp_parser_require_keyword (parser, keyword,
43435      (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
43436	  : RT_TRANSACTION_RELAXED));
43437  gcc_assert (token != NULL);
43438
43439  if (keyword == RID_TRANSACTION_RELAXED)
43440    this_in |= TM_STMT_ATTR_RELAXED;
43441
43442  /* Set this early.  This might mean that we allow transaction_cancel in
43443     an expression that we find out later actually has to be a constexpr.
43444     However, we expect that cxx_constant_value will be able to deal with
43445     this; also, if the noexcept has no constexpr, then what we parse next
43446     really is a transaction's body.  */
43447  parser->in_transaction = this_in;
43448
43449  /* Parse a noexcept specification.  */
43450  noex = cp_parser_noexcept_specification_opt (parser,
43451					       CP_PARSER_FLAGS_NONE,
43452					       /*require_constexpr=*/false,
43453					       &noex_expr,
43454					       /*return_cond=*/true,
43455					       TYPE_UNQUALIFIED);
43456
43457  if (!noex || !noex_expr
43458      || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
43459    {
43460      matching_parens parens;
43461      parens.require_open (parser);
43462
43463      expr = cp_parser_expression (parser);
43464      expr = finish_parenthesized_expr (expr);
43465
43466      parens.require_close (parser);
43467    }
43468  else
43469    {
43470      /* The only expression that is available got parsed for the noexcept
43471         already.  noexcept is true then.  */
43472      expr = noex;
43473      noex = boolean_true_node;
43474    }
43475
43476  expr = build_transaction_expr (token->location, expr, this_in, noex);
43477  parser->in_transaction = old_in;
43478
43479  if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
43480    return error_mark_node;
43481
43482  return (flag_tm ? expr : error_mark_node);
43483}
43484
43485/* Parse a function-transaction-block.
43486
43487   function-transaction-block:
43488     __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
43489	 function-body
43490     __transaction_atomic txn-attribute[opt] function-try-block
43491     __transaction_relaxed ctor-initializer[opt] function-body
43492     __transaction_relaxed function-try-block
43493*/
43494
43495static void
43496cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
43497{
43498  unsigned char old_in = parser->in_transaction;
43499  unsigned char new_in = 1;
43500  tree compound_stmt, stmt, attrs;
43501  cp_token *token;
43502
43503  gcc_assert (keyword == RID_TRANSACTION_ATOMIC
43504      || keyword == RID_TRANSACTION_RELAXED);
43505  token = cp_parser_require_keyword (parser, keyword,
43506      (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
43507	  : RT_TRANSACTION_RELAXED));
43508  gcc_assert (token != NULL);
43509
43510  if (keyword == RID_TRANSACTION_RELAXED)
43511    new_in |= TM_STMT_ATTR_RELAXED;
43512  else
43513    {
43514      attrs = cp_parser_txn_attribute_opt (parser);
43515      if (attrs)
43516	new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
43517    }
43518
43519  stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
43520
43521  parser->in_transaction = new_in;
43522
43523  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
43524    cp_parser_function_try_block (parser);
43525  else
43526    cp_parser_ctor_initializer_opt_and_function_body
43527      (parser, /*in_function_try_block=*/false);
43528
43529  parser->in_transaction = old_in;
43530
43531  finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
43532}
43533
43534/* Parse a __transaction_cancel statement.
43535
43536   cancel-statement:
43537     __transaction_cancel txn-attribute[opt] ;
43538     __transaction_cancel txn-attribute[opt] throw-expression ;
43539
43540   ??? Cancel and throw is not yet implemented.  */
43541
43542static tree
43543cp_parser_transaction_cancel (cp_parser *parser)
43544{
43545  cp_token *token;
43546  bool is_outer = false;
43547  tree stmt, attrs;
43548
43549  token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
43550				     RT_TRANSACTION_CANCEL);
43551  gcc_assert (token != NULL);
43552
43553  attrs = cp_parser_txn_attribute_opt (parser);
43554  if (attrs)
43555    is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
43556
43557  /* ??? Parse cancel-and-throw here.  */
43558
43559  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43560
43561  if (!flag_tm)
43562    {
43563      error_at (token->location, "%<__transaction_cancel%> without "
43564		"transactional memory support enabled");
43565      return error_mark_node;
43566    }
43567  else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
43568    {
43569      error_at (token->location, "%<__transaction_cancel%> within a "
43570		"%<__transaction_relaxed%>");
43571      return error_mark_node;
43572    }
43573  else if (is_outer)
43574    {
43575      if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
43576	  && !is_tm_may_cancel_outer (current_function_decl))
43577	{
43578	  error_at (token->location, "outer %<__transaction_cancel%> not "
43579		    "within outer %<__transaction_atomic%>");
43580	  error_at (token->location,
43581		    "  or a %<transaction_may_cancel_outer%> function");
43582	  return error_mark_node;
43583	}
43584    }
43585  else if (parser->in_transaction == 0)
43586    {
43587      error_at (token->location, "%<__transaction_cancel%> not within "
43588		"%<__transaction_atomic%>");
43589      return error_mark_node;
43590    }
43591
43592  stmt = build_tm_abort_call (token->location, is_outer);
43593  add_stmt (stmt);
43594
43595  return stmt;
43596}
43597
43598/* The parser.  */
43599
43600static GTY (()) cp_parser *the_parser;
43601
43602
43603/* Special handling for the first token or line in the file.  The first
43604   thing in the file might be #pragma GCC pch_preprocess, which loads a
43605   PCH file, which is a GC collection point.  So we need to handle this
43606   first pragma without benefit of an existing lexer structure.
43607
43608   Always returns one token to the caller in *FIRST_TOKEN.  This is
43609   either the true first token of the file, or the first token after
43610   the initial pragma.  */
43611
43612static void
43613cp_parser_initial_pragma (cp_token *first_token)
43614{
43615  tree name = NULL;
43616
43617  cp_lexer_get_preprocessor_token (NULL, first_token);
43618  if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
43619    {
43620      c_common_no_more_pch ();
43621      return;
43622    }
43623
43624  cp_lexer_get_preprocessor_token (NULL, first_token);
43625  if (first_token->type == CPP_STRING)
43626    {
43627      name = first_token->u.value;
43628
43629      cp_lexer_get_preprocessor_token (NULL, first_token);
43630      if (first_token->type != CPP_PRAGMA_EOL)
43631	error_at (first_token->location,
43632		  "junk at end of %<#pragma GCC pch_preprocess%>");
43633    }
43634  else
43635    error_at (first_token->location, "expected string literal");
43636
43637  /* Skip to the end of the pragma.  */
43638  while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
43639    cp_lexer_get_preprocessor_token (NULL, first_token);
43640
43641  /* Now actually load the PCH file.  */
43642  if (name)
43643    c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
43644
43645  /* Read one more token to return to our caller.  We have to do this
43646     after reading the PCH file in, since its pointers have to be
43647     live.  */
43648  cp_lexer_get_preprocessor_token (NULL, first_token);
43649}
43650
43651/* Parse a pragma GCC ivdep.  */
43652
43653static bool
43654cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
43655{
43656  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43657  return true;
43658}
43659
43660/* Parse a pragma GCC unroll.  */
43661
43662static unsigned short
43663cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
43664{
43665  location_t location = cp_lexer_peek_token (parser->lexer)->location;
43666  tree expr = cp_parser_constant_expression (parser);
43667  unsigned short unroll;
43668  expr = maybe_constant_value (expr);
43669  HOST_WIDE_INT lunroll = 0;
43670  if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
43671      || TREE_CODE (expr) != INTEGER_CST
43672      || (lunroll = tree_to_shwi (expr)) < 0
43673      || lunroll >= USHRT_MAX)
43674    {
43675      error_at (location, "%<#pragma GCC unroll%> requires an"
43676		" assignment-expression that evaluates to a non-negative"
43677		" integral constant less than %u", USHRT_MAX);
43678      unroll = 0;
43679    }
43680  else
43681    {
43682      unroll = (unsigned short)lunroll;
43683      if (unroll == 0)
43684	unroll = 1;
43685    }
43686  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43687  return unroll;
43688}
43689
43690/* Normal parsing of a pragma token.  Here we can (and must) use the
43691   regular lexer.  */
43692
43693static bool
43694cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
43695{
43696  cp_token *pragma_tok;
43697  unsigned int id;
43698  tree stmt;
43699  bool ret;
43700
43701  pragma_tok = cp_lexer_consume_token (parser->lexer);
43702  gcc_assert (pragma_tok->type == CPP_PRAGMA);
43703  parser->lexer->in_pragma = true;
43704
43705  id = cp_parser_pragma_kind (pragma_tok);
43706  if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
43707    cp_ensure_no_omp_declare_simd (parser);
43708  switch (id)
43709    {
43710    case PRAGMA_GCC_PCH_PREPROCESS:
43711      error_at (pragma_tok->location,
43712		"%<#pragma GCC pch_preprocess%> must be first");
43713      break;
43714
43715    case PRAGMA_OMP_BARRIER:
43716      switch (context)
43717	{
43718	case pragma_compound:
43719	  cp_parser_omp_barrier (parser, pragma_tok);
43720	  return false;
43721	case pragma_stmt:
43722	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
43723		    "used in compound statements", "omp barrier");
43724	  break;
43725	default:
43726	  goto bad_stmt;
43727	}
43728      break;
43729
43730    case PRAGMA_OMP_DEPOBJ:
43731      switch (context)
43732	{
43733	case pragma_compound:
43734	  cp_parser_omp_depobj (parser, pragma_tok);
43735	  return false;
43736	case pragma_stmt:
43737	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
43738		    "used in compound statements", "omp depobj");
43739	  break;
43740	default:
43741	  goto bad_stmt;
43742	}
43743      break;
43744
43745    case PRAGMA_OMP_FLUSH:
43746      switch (context)
43747	{
43748	case pragma_compound:
43749	  cp_parser_omp_flush (parser, pragma_tok);
43750	  return false;
43751	case pragma_stmt:
43752	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
43753		    "used in compound statements", "omp flush");
43754	  break;
43755	default:
43756	  goto bad_stmt;
43757	}
43758      break;
43759
43760    case PRAGMA_OMP_TASKWAIT:
43761      switch (context)
43762	{
43763	case pragma_compound:
43764	  cp_parser_omp_taskwait (parser, pragma_tok);
43765	  return false;
43766	case pragma_stmt:
43767	  error_at (pragma_tok->location,
43768		    "%<#pragma %s%> may only be used in compound statements",
43769		    "omp taskwait");
43770	  break;
43771	default:
43772	  goto bad_stmt;
43773	}
43774      break;
43775
43776    case PRAGMA_OMP_TASKYIELD:
43777      switch (context)
43778	{
43779	case pragma_compound:
43780	  cp_parser_omp_taskyield (parser, pragma_tok);
43781	  return false;
43782	case pragma_stmt:
43783	  error_at (pragma_tok->location,
43784		    "%<#pragma %s%> may only be used in compound statements",
43785		    "omp taskyield");
43786	  break;
43787	default:
43788	  goto bad_stmt;
43789	}
43790      break;
43791
43792    case PRAGMA_OMP_CANCEL:
43793      switch (context)
43794	{
43795	case pragma_compound:
43796	  cp_parser_omp_cancel (parser, pragma_tok);
43797	  return false;
43798	case pragma_stmt:
43799	  error_at (pragma_tok->location,
43800		    "%<#pragma %s%> may only be used in compound statements",
43801		    "omp cancel");
43802	  break;
43803	default:
43804	  goto bad_stmt;
43805	}
43806      break;
43807
43808    case PRAGMA_OMP_CANCELLATION_POINT:
43809      cp_parser_omp_cancellation_point (parser, pragma_tok, context);
43810      return false;
43811
43812    case PRAGMA_OMP_THREADPRIVATE:
43813      cp_parser_omp_threadprivate (parser, pragma_tok);
43814      return false;
43815
43816    case PRAGMA_OMP_DECLARE:
43817      return cp_parser_omp_declare (parser, pragma_tok, context);
43818
43819    case PRAGMA_OACC_DECLARE:
43820      cp_parser_oacc_declare (parser, pragma_tok);
43821      return false;
43822
43823    case PRAGMA_OACC_ENTER_DATA:
43824      if (context == pragma_stmt)
43825	{
43826	  error_at (pragma_tok->location,
43827		    "%<#pragma %s%> may only be used in compound statements",
43828		    "acc enter data");
43829	  break;
43830	}
43831      else if (context != pragma_compound)
43832	goto bad_stmt;
43833      cp_parser_omp_construct (parser, pragma_tok, if_p);
43834      return true;
43835
43836    case PRAGMA_OACC_EXIT_DATA:
43837      if (context == pragma_stmt)
43838	{
43839	  error_at (pragma_tok->location,
43840		    "%<#pragma %s%> may only be used in compound statements",
43841		    "acc exit data");
43842	  break;
43843	}
43844      else if (context != pragma_compound)
43845	goto bad_stmt;
43846      cp_parser_omp_construct (parser, pragma_tok, if_p);
43847      return true;
43848
43849    case PRAGMA_OACC_ROUTINE:
43850      if (context != pragma_external)
43851	{
43852	  error_at (pragma_tok->location,
43853		    "%<#pragma acc routine%> must be at file scope");
43854	  break;
43855	}
43856      cp_parser_oacc_routine (parser, pragma_tok, context);
43857      return false;
43858
43859    case PRAGMA_OACC_UPDATE:
43860      if (context == pragma_stmt)
43861	{
43862	  error_at (pragma_tok->location,
43863		    "%<#pragma %s%> may only be used in compound statements",
43864		    "acc update");
43865	  break;
43866	}
43867      else if (context != pragma_compound)
43868	goto bad_stmt;
43869      cp_parser_omp_construct (parser, pragma_tok, if_p);
43870      return true;
43871
43872    case PRAGMA_OACC_WAIT:
43873      if (context == pragma_stmt)
43874	{
43875	  error_at (pragma_tok->location,
43876		    "%<#pragma %s%> may only be used in compound statements",
43877		    "acc wait");
43878	  break;
43879	}
43880      else if (context != pragma_compound)
43881	goto bad_stmt;
43882      cp_parser_omp_construct (parser, pragma_tok, if_p);
43883      return true;
43884
43885    case PRAGMA_OACC_ATOMIC:
43886    case PRAGMA_OACC_CACHE:
43887    case PRAGMA_OACC_DATA:
43888    case PRAGMA_OACC_HOST_DATA:
43889    case PRAGMA_OACC_KERNELS:
43890    case PRAGMA_OACC_LOOP:
43891    case PRAGMA_OACC_PARALLEL:
43892    case PRAGMA_OACC_SERIAL:
43893    case PRAGMA_OMP_ATOMIC:
43894    case PRAGMA_OMP_CRITICAL:
43895    case PRAGMA_OMP_DISTRIBUTE:
43896    case PRAGMA_OMP_FOR:
43897    case PRAGMA_OMP_LOOP:
43898    case PRAGMA_OMP_MASTER:
43899    case PRAGMA_OMP_PARALLEL:
43900    case PRAGMA_OMP_SECTIONS:
43901    case PRAGMA_OMP_SIMD:
43902    case PRAGMA_OMP_SINGLE:
43903    case PRAGMA_OMP_TASK:
43904    case PRAGMA_OMP_TASKGROUP:
43905    case PRAGMA_OMP_TASKLOOP:
43906    case PRAGMA_OMP_TEAMS:
43907      if (context != pragma_stmt && context != pragma_compound)
43908	goto bad_stmt;
43909      stmt = push_omp_privatization_clauses (false);
43910      cp_parser_omp_construct (parser, pragma_tok, if_p);
43911      pop_omp_privatization_clauses (stmt);
43912      return true;
43913
43914    case PRAGMA_OMP_REQUIRES:
43915      if (context != pragma_external)
43916	{
43917	  error_at (pragma_tok->location,
43918		    "%<#pragma omp requires%> may only be used at file or "
43919		    "namespace scope");
43920	  break;
43921	}
43922      return cp_parser_omp_requires (parser, pragma_tok);
43923
43924    case PRAGMA_OMP_ORDERED:
43925      if (context != pragma_stmt && context != pragma_compound)
43926	goto bad_stmt;
43927      stmt = push_omp_privatization_clauses (false);
43928      ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
43929      pop_omp_privatization_clauses (stmt);
43930      return ret;
43931
43932    case PRAGMA_OMP_TARGET:
43933      if (context != pragma_stmt && context != pragma_compound)
43934	goto bad_stmt;
43935      stmt = push_omp_privatization_clauses (false);
43936      ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
43937      pop_omp_privatization_clauses (stmt);
43938      return ret;
43939
43940    case PRAGMA_OMP_END_DECLARE_TARGET:
43941      cp_parser_omp_end_declare_target (parser, pragma_tok);
43942      return false;
43943
43944    case PRAGMA_OMP_SCAN:
43945      error_at (pragma_tok->location,
43946		"%<#pragma omp scan%> may only be used in "
43947		"a loop construct with %<inscan%> %<reduction%> clause");
43948      break;
43949
43950    case PRAGMA_OMP_SECTION:
43951      error_at (pragma_tok->location,
43952		"%<#pragma omp section%> may only be used in "
43953		"%<#pragma omp sections%> construct");
43954      break;
43955
43956    case PRAGMA_IVDEP:
43957      {
43958	if (context == pragma_external)
43959	  {
43960	    error_at (pragma_tok->location,
43961		      "%<#pragma GCC ivdep%> must be inside a function");
43962	    break;
43963	  }
43964	const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
43965	unsigned short unroll;
43966	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
43967	if (tok->type == CPP_PRAGMA
43968	    && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
43969	  {
43970	    tok = cp_lexer_consume_token (parser->lexer);
43971	    unroll = cp_parser_pragma_unroll (parser, tok);
43972	    tok = cp_lexer_peek_token (the_parser->lexer);
43973	  }
43974	else
43975	  unroll = 0;
43976	if (tok->type != CPP_KEYWORD
43977	    || (tok->keyword != RID_FOR
43978		&& tok->keyword != RID_WHILE
43979		&& tok->keyword != RID_DO))
43980	  {
43981	    cp_parser_error (parser, "for, while or do statement expected");
43982	    return false;
43983	  }
43984	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
43985	return true;
43986      }
43987
43988    case PRAGMA_UNROLL:
43989      {
43990	if (context == pragma_external)
43991	  {
43992	    error_at (pragma_tok->location,
43993		      "%<#pragma GCC unroll%> must be inside a function");
43994	    break;
43995	  }
43996	const unsigned short unroll
43997	  = cp_parser_pragma_unroll (parser, pragma_tok);
43998	bool ivdep;
43999	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
44000	if (tok->type == CPP_PRAGMA
44001	    && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
44002	  {
44003	    tok = cp_lexer_consume_token (parser->lexer);
44004	    ivdep = cp_parser_pragma_ivdep (parser, tok);
44005	    tok = cp_lexer_peek_token (the_parser->lexer);
44006	  }
44007	else
44008	  ivdep = false;
44009	if (tok->type != CPP_KEYWORD
44010	    || (tok->keyword != RID_FOR
44011		&& tok->keyword != RID_WHILE
44012		&& tok->keyword != RID_DO))
44013	  {
44014	    cp_parser_error (parser, "for, while or do statement expected");
44015	    return false;
44016	  }
44017	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
44018	return true;
44019      }
44020
44021    default:
44022      gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
44023      c_invoke_pragma_handler (id);
44024      break;
44025
44026    bad_stmt:
44027      cp_parser_error (parser, "expected declaration specifiers");
44028      break;
44029    }
44030
44031  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44032  return false;
44033}
44034
44035/* The interface the pragma parsers have to the lexer.  */
44036
44037enum cpp_ttype
44038pragma_lex (tree *value, location_t *loc)
44039{
44040  cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
44041  enum cpp_ttype ret = tok->type;
44042
44043  *value = tok->u.value;
44044  if (loc)
44045    *loc = tok->location;
44046
44047  if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
44048    ret = CPP_EOF;
44049  else if (ret == CPP_STRING)
44050    *value = cp_parser_string_literal (the_parser, false, false);
44051  else
44052    {
44053      if (ret == CPP_KEYWORD)
44054	ret = CPP_NAME;
44055      cp_lexer_consume_token (the_parser->lexer);
44056    }
44057
44058  return ret;
44059}
44060
44061
44062/* External interface.  */
44063
44064/* Parse one entire translation unit.  */
44065
44066void
44067c_parse_file (void)
44068{
44069  static bool already_called = false;
44070
44071  if (already_called)
44072    fatal_error (input_location,
44073		 "inter-module optimizations not implemented for C++");
44074  already_called = true;
44075
44076  the_parser = cp_parser_new ();
44077  push_deferring_access_checks (flag_access_control
44078				? dk_no_deferred : dk_no_check);
44079  cp_parser_translation_unit (the_parser);
44080  class_decl_loc_t::diag_mismatched_tags ();
44081
44082  the_parser = NULL;
44083
44084  finish_translation_unit ();
44085}
44086
44087/* Create an identifier for a generic parameter type (a synthesized
44088   template parameter implied by `auto' or a concept identifier). */
44089
44090static GTY(()) int generic_parm_count;
44091static tree
44092make_generic_type_name ()
44093{
44094  char buf[32];
44095  sprintf (buf, "auto:%d", ++generic_parm_count);
44096  return get_identifier (buf);
44097}
44098
44099/* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
44100   (creating a new template parameter list if necessary).  Returns the newly
44101   created template type parm.  */
44102
44103static tree
44104synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
44105{
44106  /* A requires-clause is not a function and cannot have placeholders.  */
44107  if (current_binding_level->kind == sk_block)
44108    {
44109      error ("placeholder type not allowed in this context");
44110      return error_mark_node;
44111    }
44112
44113  gcc_assert (current_binding_level->kind == sk_function_parms);
44114
44115  /* We are either continuing a function template that already contains implicit
44116     template parameters, creating a new fully-implicit function template, or
44117     extending an existing explicit function template with implicit template
44118     parameters.  */
44119
44120  cp_binding_level *const entry_scope = current_binding_level;
44121
44122  bool become_template = false;
44123  cp_binding_level *parent_scope = 0;
44124
44125  if (parser->implicit_template_scope)
44126    {
44127      gcc_assert (parser->implicit_template_parms);
44128
44129      current_binding_level = parser->implicit_template_scope;
44130    }
44131  else
44132    {
44133      /* Roll back to the existing template parameter scope (in the case of
44134	 extending an explicit function template) or introduce a new template
44135	 parameter scope ahead of the function parameter scope (or class scope
44136	 in the case of out-of-line member definitions).  The function scope is
44137	 added back after template parameter synthesis below.  */
44138
44139      cp_binding_level *scope = entry_scope;
44140
44141      while (scope->kind == sk_function_parms)
44142	{
44143	  parent_scope = scope;
44144	  scope = scope->level_chain;
44145	}
44146      if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
44147	{
44148	  /* If not defining a class, then any class scope is a scope level in
44149	     an out-of-line member definition.  In this case simply wind back
44150	     beyond the first such scope to inject the template parameter list.
44151	     Otherwise wind back to the class being defined.  The latter can
44152	     occur in class member friend declarations such as:
44153
44154	       class A {
44155		 void foo (auto);
44156	       };
44157	       class B {
44158		 friend void A::foo (auto);
44159	       };
44160
44161	    The template parameter list synthesized for the friend declaration
44162	    must be injected in the scope of 'B'.  This can also occur in
44163	    erroneous cases such as:
44164
44165	       struct A {
44166	         struct B {
44167		   void foo (auto);
44168		 };
44169		 void B::foo (auto) {}
44170	       };
44171
44172	    Here the attempted definition of 'B::foo' within 'A' is ill-formed
44173	    but, nevertheless, the template parameter list synthesized for the
44174	    declarator should be injected into the scope of 'A' as if the
44175	    ill-formed template was specified explicitly.  */
44176
44177	  while (scope->kind == sk_class && !scope->defining_class_p)
44178	    {
44179	      parent_scope = scope;
44180	      scope = scope->level_chain;
44181	    }
44182	}
44183
44184      current_binding_level = scope;
44185
44186      if (scope->kind != sk_template_parms
44187	  || !function_being_declared_is_template_p (parser))
44188	{
44189	  /* Introduce a new template parameter list for implicit template
44190	     parameters.  */
44191
44192	  become_template = true;
44193
44194	  parser->implicit_template_scope
44195	      = begin_scope (sk_template_parms, NULL);
44196
44197	  ++processing_template_decl;
44198
44199	  parser->fully_implicit_function_template_p = true;
44200	  ++parser->num_template_parameter_lists;
44201	}
44202      else
44203	{
44204	  /* Synthesize implicit template parameters at the end of the explicit
44205	     template parameter list.  */
44206
44207	  gcc_assert (current_template_parms);
44208
44209	  parser->implicit_template_scope = scope;
44210
44211	  tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
44212	  parser->implicit_template_parms
44213	    = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
44214	}
44215    }
44216
44217  /* Synthesize a new template parameter and track the current template
44218     parameter chain with implicit_template_parms.  */
44219
44220  tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
44221  tree synth_id = make_generic_type_name ();
44222  tree synth_tmpl_parm;
44223  bool non_type = false;
44224
44225  /* Synthesize the type template parameter.  */
44226  gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
44227  synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
44228
44229  /* Attach the constraint to the parm before processing.  */
44230  tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
44231  TREE_TYPE (node) = constr;
44232  tree new_parm
44233    = process_template_parm (parser->implicit_template_parms,
44234			     input_location,
44235			     node,
44236			     /*non_type=*/non_type,
44237			     /*param_pack=*/false);
44238
44239  /* Mark the synthetic declaration "virtual". This is used when
44240     comparing template-heads to determine if whether an abbreviated
44241     function template is equivalent to an explicit template.
44242
44243     Note that DECL_ARTIFICIAL is used elsewhere for template parameters.  */
44244  DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
44245
44246  // Chain the new parameter to the list of implicit parameters.
44247  if (parser->implicit_template_parms)
44248    parser->implicit_template_parms
44249      = TREE_CHAIN (parser->implicit_template_parms);
44250  else
44251    parser->implicit_template_parms = new_parm;
44252
44253  tree new_decl = get_local_decls ();
44254  if (non_type)
44255    /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
44256    new_decl = DECL_INITIAL (new_decl);
44257
44258  /* If creating a fully implicit function template, start the new implicit
44259     template parameter list with this synthesized type, otherwise grow the
44260     current template parameter list.  */
44261
44262  if (become_template)
44263    {
44264      parent_scope->level_chain = current_binding_level;
44265
44266      tree new_parms = make_tree_vec (1);
44267      TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
44268      current_template_parms = tree_cons (size_int (processing_template_decl),
44269					  new_parms, current_template_parms);
44270    }
44271  else
44272    {
44273      tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
44274      int new_parm_idx = TREE_VEC_LENGTH (new_parms);
44275      new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
44276      TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
44277    }
44278
44279  /* If the new parameter was constrained, we need to add that to the
44280     constraints in the template parameter list.  */
44281  if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
44282    {
44283      tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
44284      reqs = combine_constraint_expressions (reqs, req);
44285      TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
44286    }
44287
44288  current_binding_level = entry_scope;
44289
44290  return new_decl;
44291}
44292
44293/* Finish the declaration of a fully implicit function template.  Such a
44294   template has no explicit template parameter list so has not been through the
44295   normal template head and tail processing.  synthesize_implicit_template_parm
44296   tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
44297   provided if the declaration is a class member such that its template
44298   declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
44299   form is returned.  Otherwise NULL_TREE is returned. */
44300
44301static tree
44302finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
44303{
44304  gcc_assert (parser->fully_implicit_function_template_p);
44305
44306  if (member_decl_opt && member_decl_opt != error_mark_node
44307      && DECL_VIRTUAL_P (member_decl_opt))
44308    {
44309      error_at (DECL_SOURCE_LOCATION (member_decl_opt),
44310		"implicit templates may not be %<virtual%>");
44311      DECL_VIRTUAL_P (member_decl_opt) = false;
44312    }
44313
44314  if (member_decl_opt)
44315    member_decl_opt = finish_member_template_decl (member_decl_opt);
44316  end_template_decl ();
44317
44318  parser->fully_implicit_function_template_p = false;
44319  parser->implicit_template_parms = 0;
44320  parser->implicit_template_scope = 0;
44321  --parser->num_template_parameter_lists;
44322
44323  return member_decl_opt;
44324}
44325
44326/* Like finish_fully_implicit_template, but to be used in error
44327   recovery, rearranging scopes so that we restore the state we had
44328   before synthesize_implicit_template_parm inserted the implement
44329   template parms scope.  */
44330
44331static void
44332abort_fully_implicit_template (cp_parser *parser)
44333{
44334  cp_binding_level *return_to_scope = current_binding_level;
44335
44336  if (parser->implicit_template_scope
44337      && return_to_scope != parser->implicit_template_scope)
44338    {
44339      cp_binding_level *child = return_to_scope;
44340      for (cp_binding_level *scope = child->level_chain;
44341	   scope != parser->implicit_template_scope;
44342	   scope = child->level_chain)
44343	child = scope;
44344      child->level_chain = parser->implicit_template_scope->level_chain;
44345      parser->implicit_template_scope->level_chain = return_to_scope;
44346      current_binding_level = parser->implicit_template_scope;
44347    }
44348  else
44349    return_to_scope = return_to_scope->level_chain;
44350
44351  finish_fully_implicit_template (parser, NULL);
44352
44353  gcc_assert (current_binding_level == return_to_scope);
44354}
44355
44356/* Helper function for diagnostics that have complained about things
44357   being used with 'extern "C"' linkage.
44358
44359   Attempt to issue a note showing where the 'extern "C"' linkage began.  */
44360
44361void
44362maybe_show_extern_c_location (void)
44363{
44364  if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
44365    inform (the_parser->innermost_linkage_specification_location,
44366	    "%<extern \"C\"%> linkage started here");
44367}
44368
44369#include "gt-cp-parser.h"
44370